esh-util.el 25 KB

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