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