Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / eshell / esh-util.el
CommitLineData
60370d40 1;;; esh-util.el --- general utilities
26b4dc84 2
5fd6d89f 3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
8b72699e 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
26b4dc84 5
7de5b421
GM
6;; Author: John Wiegley <johnw@gnu.org>
7
26b4dc84
GM
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
e0085d62 12;; the Free Software Foundation; either version 3, or (at your option)
26b4dc84
GM
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
26b4dc84 24
99abb67e 25;;; Commentary:
26b4dc84 26
99abb67e 27;;; Code:
26b4dc84
GM
28
29(defgroup eshell-util nil
30 "This is general utility code, meant for use by Eshell itself."
31 :tag "General utilities"
32 :group 'eshell)
33
26b4dc84
GM
34;;; User Variables:
35
dace60cf
JW
36(defcustom eshell-stringify-t t
37 "*If non-nil, the string representation of t is 't'.
38If nil, t will be represented only in the exit code of the function,
39and not printed as a string. This causes Lisp functions to behave
40similarly to external commands, as far as successful result output."
41 :type 'boolean
42 :group 'eshell-util)
43
26b4dc84
GM
44(defcustom eshell-group-file "/etc/group"
45 "*If non-nil, the name of the group file on your system."
46 :type '(choice (const :tag "No group file" nil) file)
47 :group 'eshell-util)
48
49(defcustom eshell-passwd-file "/etc/passwd"
50 "*If non-nil, the name of the passwd file on your system."
51 :type '(choice (const :tag "No passwd file" nil) file)
52 :group 'eshell-util)
53
54(defcustom eshell-hosts-file "/etc/hosts"
55 "*The name of the /etc/hosts file."
56 :type '(choice (const :tag "No hosts file" nil) file)
57 :group 'eshell-util)
58
59(defcustom eshell-handle-errors t
60 "*If non-nil, Eshell will handle errors itself.
61Setting this to nil is offered as an aid to debugging only."
62 :type 'boolean
63 :group 'eshell-util)
64
65(defcustom eshell-private-file-modes 384 ; umask 177
66 "*The file-modes value to use for creating \"private\" files."
67 :type 'integer
68 :group 'eshell-util)
69
70(defcustom eshell-private-directory-modes 448 ; umask 077
71 "*The file-modes value to use for creating \"private\" directories."
72 :type 'integer
73 :group 'eshell-util)
74
75(defcustom eshell-tar-regexp
76 "\\.t\\(ar\\(\\.\\(gz\\|bz2\\|Z\\)\\)?\\|gz\\|a[zZ]\\|z2\\)\\'"
77 "*Regular expression used to match tar file names."
78 :type 'regexp
79 :group 'eshell-util)
80
81(defcustom eshell-convert-numeric-arguments t
82 "*If non-nil, converting arguments of numeric form to Lisp numbers.
83Numeric form is tested using the regular expression
90d94608
JW
84`eshell-number-regexp'.
85
86NOTE: If you find that numeric conversions are intefering with the
87specification of filenames (for example, in calling `find-file', or
88some other Lisp function that deals with files, not numbers), add the
89following in your .emacs file:
90
91 (put 'find-file 'eshell-no-numeric-conversions t)
92
93Any function with the property `eshell-no-numeric-conversions' set to
94a non-nil value, will be passed strings, not numbers, even when an
95argument matches `eshell-number-regexp'."
26b4dc84
GM
96 :type 'boolean
97 :group 'eshell-util)
98
cee38ad6 99(defcustom eshell-number-regexp "-?\\([0-9]*\\.\\)?[0-9]+\\(e[-0-9.]+\\)?"
26b4dc84
GM
100 "*Regular expression used to match numeric arguments.
101If `eshell-convert-numeric-arguments' is non-nil, and an argument
102matches this regexp, it will be converted to a Lisp number, using the
103function `string-to-number'."
104 :type 'regexp
105 :group 'eshell-util)
106
8c6b1d83
JW
107(defcustom eshell-ange-ls-uids nil
108 "*List of user/host/id strings, used to determine remote ownership."
219227ea
JW
109 :type '(repeat (cons :tag "Host for User/UID map"
110 (string :tag "Hostname")
111 (repeat (cons :tag "User/UID List"
112 (string :tag "Username")
113 (repeat :tag "UIDs" string)))))
8c6b1d83
JW
114 :group 'eshell-util)
115
26b4dc84
GM
116;;; Internal Variables:
117
118(defvar eshell-group-names nil
119 "A cache to hold the names of groups.")
120
121(defvar eshell-group-timestamp nil
122 "A timestamp of when the group file was read.")
123
124(defvar eshell-user-names nil
125 "A cache to hold the names of users.")
126
127(defvar eshell-user-timestamp nil
128 "A timestamp of when the user file was read.")
129
130(defvar eshell-host-names nil
131 "A cache the names of frequently accessed hosts.")
132
133(defvar eshell-host-timestamp nil
134 "A timestamp of when the hosts file was read.")
135
136;;; Functions:
137
26b4dc84
GM
138(defsubst eshell-under-windows-p ()
139 "Return non-nil if we are running under MS-DOS/Windows."
140 (memq system-type '(ms-dos windows-nt)))
141
142(defmacro eshell-condition-case (tag form &rest handlers)
143 "Like `condition-case', but only if `eshell-pass-through-errors' is nil."
144 (if eshell-handle-errors
145 `(condition-case ,tag
146 ,form
147 ,@handlers)
148 form))
149
150(put 'eshell-condition-case 'lisp-indent-function 2)
151
152(defmacro eshell-deftest (module name label &rest forms)
153 (if (and (fboundp 'cl-compiling-file) (cl-compiling-file))
154 nil
155 (let ((fsym (intern (concat "eshell-test--" (symbol-name name)))))
156 `(eval-when-compile
157 (ignore
158 (defun ,fsym () ,label
159 (eshell-run-test (quote ,module) (quote ,fsym) ,label
160 (quote (progn ,@forms)))))))))
161
162(put 'eshell-deftest 'lisp-indent-function 2)
163
164(defun eshell-find-delimiter
165 (open close &optional bound reverse-p backslash-p)
166 "From point, find the CLOSE delimiter corresponding to OPEN.
167The matching is bounded by BOUND.
168If REVERSE-P is non-nil, process the region backwards.
169If BACKSLASH-P is non-nil, and OPEN and CLOSE are the same character,
170then quoting is done by a backslash, rather than a doubled delimiter."
171 (save-excursion
172 (let ((depth 1)
173 (bound (or bound (point-max))))
174 (if (if reverse-p
175 (eq (char-before) close)
176 (eq (char-after) open))
177 (forward-char (if reverse-p -1 1)))
178 (while (and (> depth 0)
179 (funcall (if reverse-p '> '<) (point) bound))
180 (let ((c (if reverse-p (char-before) (char-after))) nc)
181 (cond ((and (not reverse-p)
182 (or (not (eq open close))
183 backslash-p)
184 (eq c ?\\)
185 (setq nc (char-after (1+ (point))))
186 (or (eq nc open) (eq nc close)))
187 (forward-char 1))
188 ((and reverse-p
189 (or (not (eq open close))
190 backslash-p)
191 (or (eq c open) (eq c close))
192 (eq (char-before (1- (point)))
193 ?\\))
194 (forward-char -1))
195 ((eq open close)
196 (if (eq c open)
197 (if (and (not backslash-p)
198 (eq (if reverse-p
199 (char-before (1- (point)))
200 (char-after (1+ (point)))) open))
201 (forward-char (if reverse-p -1 1))
202 (setq depth (1- depth)))))
203 ((= c open)
204 (setq depth (+ depth (if reverse-p -1 1))))
205 ((= c close)
206 (setq depth (+ depth (if reverse-p 1 -1))))))
207 (forward-char (if reverse-p -1 1)))
208 (if (= depth 0)
209 (if reverse-p (point) (1- (point)))))))
210
211(defun eshell-convert (string)
212 "Convert STRING into a more native looking Lisp object."
213 (if (not (stringp string))
214 string
215 (let ((len (length string)))
216 (if (= len 0)
217 string
218 (if (eq (aref string (1- len)) ?\n)
219 (setq string (substring string 0 (1- len))))
220 (if (string-match "\n" string)
221 (split-string string "\n")
222 (if (and eshell-convert-numeric-arguments
223 (string-match
224 (concat "\\`\\s-*" eshell-number-regexp
225 "\\s-*\\'") string))
226 (string-to-number string)
227 string))))))
228
229(defun eshell-sublist (l &optional n m)
230 "Return from LIST the N to M elements.
231If N or M is nil, it means the end of the list."
a9304a86 232 (let* ((a (copy-sequence l))
26b4dc84
GM
233 result)
234 (if (and m (consp (nthcdr m a)))
235 (setcdr (nthcdr m a) nil))
236 (if n
237 (setq a (nthcdr n a))
238 (setq n (1- (length a))
239 a (last a)))
240 a))
241
242(defun eshell-split-path (path)
243 "Split a path into multiple subparts."
244 (let ((len (length path))
245 (i 0) (li 0)
246 parts)
247 (if (and (eshell-under-windows-p)
248 (> len 2)
6b0e3e4d
JW
249 (eq (aref path 0) ?/)
250 (eq (aref path 1) ?/))
26b4dc84
GM
251 (setq i 2))
252 (while (< i len)
6b0e3e4d 253 (if (and (eq (aref path i) ?/)
26b4dc84 254 (not (get-text-property i 'escaped path)))
6b0e3e4d 255 (setq parts (cons (if (= li i) "/"
26b4dc84
GM
256 (substring path li (1+ i))) parts)
257 li (1+ i)))
258 (setq i (1+ i)))
259 (if (< li i)
260 (setq parts (cons (substring path li i) parts)))
261 (if (and (eshell-under-windows-p)
262 (string-match "\\`[A-Za-z]:\\'" (car (last parts))))
6b0e3e4d 263 (setcar (last parts) (concat (car (last parts)) "/")))
26b4dc84
GM
264 (nreverse parts)))
265
266(defun eshell-to-flat-string (value)
267 "Make value a string. If separated by newlines change them to spaces."
268 (let ((text (eshell-stringify value)))
269 (if (string-match "\n+\\'" text)
270 (setq text (replace-match "" t t text)))
271 (while (string-match "\n+" text)
272 (setq text (replace-match " " t t text)))
273 text))
274
275(defmacro eshell-for (for-var for-list &rest forms)
276 "Iterate through a list"
277 `(let ((list-iter ,for-list))
278 (while list-iter
279 (let ((,for-var (car list-iter)))
280 ,@forms)
281 (setq list-iter (cdr list-iter)))))
282
283(put 'eshell-for 'lisp-indent-function 2)
284
ca7aae91 285(defun eshell-flatten-list (args)
26b4dc84
GM
286 "Flatten any lists within ARGS, so that there are no sublists."
287 (let ((new-list (list t)))
288 (eshell-for a args
3bab4a46 289 (if (and (listp a)
26b4dc84
GM
290 (listp (cdr a)))
291 (nconc new-list (eshell-flatten-list a))
292 (nconc new-list (list a))))
293 (cdr new-list)))
294
295(defun eshell-uniqify-list (l)
296 "Remove occurring multiples in L. You probably want to sort first."
297 (let ((m l))
298 (while m
299 (while (and (cdr m)
300 (string= (car m)
301 (cadr m)))
302 (setcdr m (cddr m)))
303 (setq m (cdr m))))
304 l)
305
306(defun eshell-stringify (object)
307 "Convert OBJECT into a string value."
308 (cond
309 ((stringp object) object)
310 ((and (listp object)
311 (not (eq object nil)))
312 (let ((string (pp-to-string object)))
313 (substring string 0 (1- (length string)))))
314 ((numberp object)
315 (number-to-string object))
316 (t
dace60cf
JW
317 (unless (and (eq object t)
318 (not eshell-stringify-t))
319 (pp-to-string object)))))
26b4dc84
GM
320
321(defsubst eshell-stringify-list (args)
322 "Convert each element of ARGS into a string value."
323 (mapcar 'eshell-stringify args))
324
325(defsubst eshell-flatten-and-stringify (&rest args)
326 "Flatten and stringify all of the ARGS into a single string."
327 (mapconcat 'eshell-stringify (eshell-flatten-list args) " "))
328
329;; the next two are from GNUS, and really should be made part of Emacs
330;; some day
331(defsubst eshell-time-less-p (t1 t2)
332 "Say whether time T1 is less than time T2."
333 (or (< (car t1) (car t2))
334 (and (= (car t1) (car t2))
335 (< (nth 1 t1) (nth 1 t2)))))
336
337(defsubst eshell-time-to-seconds (time)
338 "Convert TIME to a floating point number."
339 (+ (* (car time) 65536.0)
340 (cadr time)
341 (/ (or (car (cdr (cdr time))) 0) 1000000.0)))
342
343(defsubst eshell-directory-files (regexp &optional directory)
344 "Return a list of files in the given DIRECTORY matching REGEXP."
345 (directory-files (or directory default-directory)
346 directory regexp))
347
348(defun eshell-regexp-arg (prompt)
349 "Return list of regexp and prefix arg using PROMPT."
350 (let* (;; Don't clobber this.
351 (last-command last-command)
352 (regexp (read-from-minibuffer prompt nil nil nil
353 'minibuffer-history-search-history)))
354 (list (if (string-equal regexp "")
355 (setcar minibuffer-history-search-history
356 (nth 1 minibuffer-history-search-history))
357 regexp)
358 (prefix-numeric-value current-prefix-arg))))
359
360(defun eshell-printable-size (filesize &optional human-readable
361 block-size use-colors)
362 "Return a printable FILESIZE."
363 (let ((size (float (or filesize 0))))
364 (if human-readable
365 (if (< size human-readable)
366 (if (= (round size) 0)
367 "0"
368 (if block-size
369 "1.0k"
370 (format "%.0f" size)))
371 (setq size (/ size human-readable))
372 (if (< size human-readable)
373 (if (<= size 9.94)
374 (format "%.1fk" size)
375 (format "%.0fk" size))
376 (setq size (/ size human-readable))
377 (if (< size human-readable)
378 (let ((str (if (<= size 9.94)
379 (format "%.1fM" size)
380 (format "%.0fM" size))))
381 (if use-colors
382 (put-text-property 0 (length str)
383 'face 'bold str))
384 str)
385 (setq size (/ size human-readable))
386 (if (< size human-readable)
387 (let ((str (if (<= size 9.94)
388 (format "%.1fG" size)
389 (format "%.0fG" size))))
390 (if use-colors
391 (put-text-property 0 (length str)
392 'face 'bold-italic str))
393 str)))))
394 (if block-size
395 (setq size (/ size block-size)))
396 (format "%.0f" size))))
397
398(defun eshell-winnow-list (entries exclude &optional predicates)
399 "Pare down the ENTRIES list using the EXCLUDE regexp, and PREDICATES.
400The original list is not affected. If the result is only one element
401long, it will be returned itself, rather than returning a one-element
402list."
403 (let ((flist (list t))
404 valid p listified)
405 (unless (listp entries)
406 (setq entries (list entries)
407 listified t))
408 (eshell-for entry entries
409 (unless (and exclude (string-match exclude entry))
410 (setq p predicates valid (null p))
411 (while p
412 (if (funcall (car p) entry)
413 (setq valid t)
414 (setq p nil valid nil))
415 (setq p (cdr p)))
416 (when valid
417 (nconc flist (list entry)))))
418 (if listified
419 (cadr flist)
420 (cdr flist))))
421
422(defsubst eshell-redisplay ()
423 "Allow Emacs to redisplay buffers."
424 ;; for some strange reason, Emacs 21 is prone to trigger an
425 ;; "args out of range" error in `sit-for', if this function
426 ;; runs while point is in the minibuffer and the users attempt
427 ;; to use completion. Don't ask me.
99abb67e
GM
428 (condition-case nil
429 (sit-for 0 0)
430 (error nil)))
26b4dc84
GM
431
432(defun eshell-read-passwd-file (file)
433 "Return an alist correlating gids to group names in FILE."
434 (let (names)
435 (when (file-readable-p file)
436 (with-temp-buffer
437 (insert-file-contents file)
438 (goto-char (point-min))
439 (while (not (eobp))
440 (let* ((fields
441 (split-string (buffer-substring
442 (point) (progn (end-of-line)
443 (point))) ":")))
b4bd214e 444 (if (and (and fields (nth 0 fields) (nth 2 fields))
6b0e3e4d
JW
445 (not (assq (string-to-number (nth 2 fields)) names)))
446 (setq names (cons (cons (string-to-number (nth 2 fields))
26b4dc84
GM
447 (nth 0 fields))
448 names))))
449 (forward-line))))
450 names))
451
452(defun eshell-read-passwd (file result-var timestamp-var)
453 "Read the contents of /etc/passwd for user names."
454 (if (or (not (symbol-value result-var))
455 (not (symbol-value timestamp-var))
456 (eshell-time-less-p
457 (symbol-value timestamp-var)
458 (nth 5 (file-attributes file))))
459 (progn
460 (set result-var (eshell-read-passwd-file file))
461 (set timestamp-var (current-time))))
462 (symbol-value result-var))
463
464(defun eshell-read-group-names ()
465 "Read the contents of /etc/group for group names."
466 (if eshell-group-file
467 (eshell-read-passwd eshell-group-file 'eshell-group-names
468 'eshell-group-timestamp)))
469
470(defsubst eshell-group-id (name)
471 "Return the user id for user NAME."
472 (car (rassoc name (eshell-read-group-names))))
473
474(defsubst eshell-group-name (gid)
475 "Return the group name for the given GID."
476 (cdr (assoc gid (eshell-read-group-names))))
477
478(defun eshell-read-user-names ()
479 "Read the contents of /etc/passwd for user names."
480 (if eshell-passwd-file
481 (eshell-read-passwd eshell-passwd-file 'eshell-user-names
482 'eshell-user-timestamp)))
483
484(defsubst eshell-user-id (name)
485 "Return the user id for user NAME."
486 (car (rassoc name (eshell-read-user-names))))
487
488(defalias 'eshell-user-name 'user-login-name)
489
490(defun eshell-read-hosts-file (filename)
491 "Read in the hosts from the /etc/hosts file."
492 (let (hosts)
493 (with-temp-buffer
494 (insert-file-contents eshell-hosts-file)
495 (goto-char (point-min))
496 (while (re-search-forward
497 "^\\(\\S-+\\)\\s-+\\(\\S-+\\)\\(\\s-*\\(\\S-+\\)\\)?" nil t)
498 (if (match-string 1)
499 (add-to-list 'hosts (match-string 1)))
500 (if (match-string 2)
501 (add-to-list 'hosts (match-string 2)))
502 (if (match-string 4)
503 (add-to-list 'hosts (match-string 4)))))
504 (sort hosts 'string-lessp)))
505
506(defun eshell-read-hosts (file result-var timestamp-var)
507 "Read the contents of /etc/passwd for user names."
508 (if (or (not (symbol-value result-var))
509 (not (symbol-value timestamp-var))
510 (eshell-time-less-p
511 (symbol-value timestamp-var)
512 (nth 5 (file-attributes file))))
513 (progn
514 (set result-var (eshell-read-hosts-file file))
515 (set timestamp-var (current-time))))
516 (symbol-value result-var))
517
518(defun eshell-read-host-names ()
519 "Read the contents of /etc/hosts for host names."
520 (if eshell-hosts-file
521 (eshell-read-hosts eshell-hosts-file 'eshell-host-names
522 'eshell-host-timestamp)))
523
524(unless (fboundp 'line-end-position)
525 (defsubst line-end-position (&optional N)
526 (save-excursion (end-of-line N) (point))))
527
528(unless (fboundp 'line-beginning-position)
529 (defsubst line-beginning-position (&optional N)
530 (save-excursion (beginning-of-line N) (point))))
531
532(unless (fboundp 'subst-char-in-string)
533 (defun subst-char-in-string (fromchar tochar string &optional inplace)
534 "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
535Unless optional argument INPLACE is non-nil, return a new string."
536 (let ((i (length string))
537 (newstr (if inplace string (copy-sequence string))))
538 (while (> i 0)
539 (setq i (1- i))
540 (if (eq (aref newstr i) fromchar)
541 (aset newstr i tochar)))
542 newstr)))
543
544(defsubst eshell-copy-environment ()
545 "Return an unrelated copy of `process-environment'."
546 (mapcar 'concat process-environment))
547
548(defun eshell-subgroups (groupsym)
549 "Return all of the subgroups of GROUPSYM."
550 (let ((subgroups (get groupsym 'custom-group))
551 (subg (list t)))
552 (while subgroups
553 (if (eq (cadr (car subgroups)) 'custom-group)
554 (nconc subg (list (caar subgroups))))
555 (setq subgroups (cdr subgroups)))
556 (cdr subg)))
557
558(defmacro eshell-with-file-modes (modes &rest forms)
559 "Evaluate, with file-modes set to MODES, the given FORMS."
560 `(let ((modes (default-file-modes)))
561 (set-default-file-modes ,modes)
562 (unwind-protect
563 (progn ,@forms)
564 (set-default-file-modes modes))))
565
566(defmacro eshell-with-private-file-modes (&rest forms)
567 "Evaluate FORMS with private file modes set."
568 `(eshell-with-file-modes ,eshell-private-file-modes ,@forms))
569
570(defsubst eshell-make-private-directory (dir &optional parents)
571 "Make DIR with file-modes set to `eshell-private-directory-modes'."
572 (eshell-with-file-modes eshell-private-directory-modes
573 (make-directory dir parents)))
574
575(defsubst eshell-substring (string sublen)
576 "Return the beginning of STRING, up to SUBLEN bytes."
577 (if string
578 (if (> (length string) sublen)
579 (substring string 0 sublen)
580 string)))
581
582(unless (fboundp 'directory-files-and-attributes)
c4e5b888
JB
583 (defun directory-files-and-attributes (directory &optional full match nosort)
584 "Return a list of names of files and their attributes in DIRECTORY.
585There are three optional arguments:
586If FULL is non-nil, return absolute file names. Otherwise return names
587 that are relative to the specified directory.
588If MATCH is non-nil, mention only file names that match the regexp MATCH.
589If NOSORT is non-nil, the list is not sorted--its order is unpredictable.
590 NOSORT is useful if you plan to sort the result yourself."
591 (let ((directory (expand-file-name directory)) ange-cache)
26b4dc84
GM
592 (mapcar
593 (function
594 (lambda (file)
c4e5b888
JB
595 (cons file (eshell-file-attributes (expand-file-name file directory)))))
596 (directory-files directory full match nosort)))))
26b4dc84 597
8c6b1d83
JW
598(eval-when-compile
599 (defvar ange-cache))
600
26b4dc84
GM
601(defun eshell-directory-files-and-attributes (dir &optional full match nosort)
602 "Make sure to use the handler for `directory-file-and-attributes'."
8c6b1d83
JW
603 (let* ((dir (expand-file-name dir))
604 (dfh (find-file-name-handler dir 'directory-files)))
26b4dc84
GM
605 (if (not dfh)
606 (directory-files-and-attributes dir full match nosort)
8c6b1d83
JW
607 (let ((files (funcall dfh 'directory-files dir full match nosort))
608 (fah (find-file-name-handler dir 'file-attributes)))
26b4dc84
GM
609 (mapcar
610 (function
611 (lambda (file)
8c6b1d83
JW
612 (cons file (if fah
613 (eshell-file-attributes
614 (expand-file-name file dir))
615 (file-attributes (expand-file-name file dir))))))
26b4dc84
GM
616 files)))))
617
8c6b1d83
JW
618(defun eshell-current-ange-uids ()
619 (if (string-match "/\\([^@]+\\)@\\([^:]+\\):" default-directory)
620 (let* ((host (match-string 2 default-directory))
621 (user (match-string 1 default-directory))
622 (host-users (assoc host eshell-ange-ls-uids)))
623 (when host-users
624 (setq host-users (cdr host-users))
625 (cdr (assoc user host-users))))))
626
627;; Add an autoload for parse-time-string
628(if (and (not (fboundp 'parse-time-string))
629 (locate-library "parse-time"))
630 (autoload 'parse-time-string "parse-time"))
631
57a24508 632(eval-when-compile
80903c3a 633 (require 'ange-ftp nil t))
57a24508 634
8c6b1d83
JW
635(defun eshell-parse-ange-ls (dir)
636 (let (entry)
637 (with-temp-buffer
638 (insert (ange-ftp-ls dir "-la" nil))
639 (goto-char (point-min))
640 (if (looking-at "^total [0-9]+$")
641 (forward-line 1))
642 ;; Some systems put in a blank line here.
643 (if (eolp) (forward-line 1))
644 (while (looking-at
645 `,(concat "\\([dlscb-][rwxst-]+\\)"
646 "\\s-*" "\\([0-9]+\\)" "\\s-+"
647 "\\(\\S-+\\)" "\\s-+"
648 "\\(\\S-+\\)" "\\s-+"
649 "\\([0-9]+\\)" "\\s-+" "\\(.*\\)"))
650 (let* ((perms (match-string 1))
651 (links (string-to-number (match-string 2)))
652 (user (match-string 3))
653 (group (match-string 4))
654 (size (string-to-number (match-string 5)))
655 (mtime
656 (if (fboundp 'parse-time-string)
657 (let ((moment (parse-time-string
658 (match-string 6))))
659 (if (nth 0 moment)
660 (setcar (nthcdr 5 moment)
661 (nth 5 (decode-time (current-time))))
662 (setcar (nthcdr 0 moment) 0)
663 (setcar (nthcdr 1 moment) 0)
664 (setcar (nthcdr 2 moment) 0))
665 (apply 'encode-time moment))
666 (ange-ftp-file-modtime (expand-file-name name dir))))
667 (name (ange-ftp-parse-filename))
668 symlink)
669 (if (string-match "\\(.+\\) -> \\(.+\\)" name)
670 (setq symlink (match-string 2 name)
671 name (match-string 1 name)))
672 (setq entry
673 (cons
674 (cons name
675 (list (if (eq (aref perms 0) ?d)
676 t
677 symlink)
678 links user group
679 nil mtime nil
680 size perms nil nil)) entry)))
681 (forward-line)))
682 entry))
683
684(defun eshell-file-attributes (file)
685 "Return the attributes of FILE, playing tricks if it's over ange-ftp."
686 (let* ((file (expand-file-name file))
687 (handler (find-file-name-handler file 'file-attributes))
688 entry)
689 (if (not handler)
690 (file-attributes file)
691 (if (eq (find-file-name-handler (file-name-directory file)
692 'directory-files)
693 'ange-ftp-hook-function)
694 (let ((base (file-name-nondirectory file))
695 (dir (file-name-directory file)))
696 (if (boundp 'ange-cache)
697 (setq entry (cdr (assoc base (cdr (assoc dir ange-cache))))))
698 (unless entry
699 (setq entry (eshell-parse-ange-ls dir))
700 (if (boundp 'ange-cache)
701 (setq ange-cache
702 (cons (cons dir entry)
703 ange-cache)))
704 (if entry
705 (let ((fentry (assoc base (cdr entry))))
706 (if fentry
707 (setq entry (cdr fentry))
708 (setq entry nil)))))))
709 (or entry (funcall handler 'file-attributes file)))))
710
efbddb8e 711(defalias 'eshell-copy-tree 'copy-tree)
26b4dc84 712
b4bd214e
JW
713(defsubst eshell-processp (proc)
714 "If the `processp' function does not exist, PROC is not a process."
715 (and (fboundp 'processp) (processp proc)))
716
26b4dc84
GM
717; (defun eshell-copy-file
718; (file newname &optional ok-if-already-exists keep-date)
719; "Copy FILE to NEWNAME. See docs for `copy-file'."
720; (let (copied)
721; (if (string-match "\\`\\([^:]+\\):\\(.*\\)" file)
722; (let ((front (match-string 1 file))
723; (back (match-string 2 file))
724; buffer)
725; (if (and front (string-match eshell-tar-regexp front)
726; (setq buffer (find-file-noselect front)))
727; (with-current-buffer buffer
728; (goto-char (point-min))
729; (if (re-search-forward (concat " " (regexp-quote back)
730; "$") nil t)
731; (progn
732; (tar-copy (if (file-directory-p newname)
733; (expand-file-name
734; (file-name-nondirectory back) newname)
735; newname))
736; (setq copied t))
737; (error "%s not found in tar file %s" back front))))))
738; (unless copied
739; (copy-file file newname ok-if-already-exists keep-date))))
740
741; (defun eshell-file-attributes (filename)
742; "Return a list of attributes of file FILENAME.
743; See the documentation for `file-attributes'."
744; (let (result)
745; (when (string-match "\\`\\([^:]+\\):\\(.*\\)\\'" filename)
746; (let ((front (match-string 1 filename))
747; (back (match-string 2 filename))
748; buffer)
749; (when (and front (string-match eshell-tar-regexp front)
750; (setq buffer (find-file-noselect front)))
751; (with-current-buffer buffer
752; (goto-char (point-min))
753; (when (re-search-forward (concat " " (regexp-quote back)
754; "\\s-*$") nil t)
755; (let* ((descrip (tar-current-descriptor))
756; (tokens (tar-desc-tokens descrip)))
757; (setq result
758; (list
759; (cond
760; ((eq (tar-header-link-type tokens) 5)
761; t)
762; ((eq (tar-header-link-type tokens) t)
763; (tar-header-link-name tokens)))
764; 1
765; (tar-header-uid tokens)
766; (tar-header-gid tokens)
767; (tar-header-date tokens)
768; (tar-header-date tokens)
769; (tar-header-date tokens)
770; (tar-header-size tokens)
771; (concat
772; (cond
773; ((eq (tar-header-link-type tokens) 5) "d")
774; ((eq (tar-header-link-type tokens) t) "l")
775; (t "-"))
776; (tar-grind-file-mode (tar-header-mode tokens)
777; (make-string 9 ? ) 0))
778; nil nil nil))))))))
779; (or result
780; (file-attributes filename))))
781
99abb67e 782(provide 'esh-util)
26b4dc84 783
cbee283d 784;; arch-tag: 70159778-5c7a-480a-bae4-3ad332fca19d
26b4dc84 785;;; esh-util.el ends here