executable.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ;;; executable.el --- base functionality for executable interpreter scripts -*- byte-compile-dynamic: t -*-
  2. ;; Copyright (C) 1994-1996, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Daniel Pfeiffer <occitan@esperanto.org>
  4. ;; Keywords: languages, unix
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; executable.el is used by certain major modes to insert a suitable
  18. ;; #! line at the beginning of the file, if the file does not already
  19. ;; have one.
  20. ;; Unless it has a magic number, a Unix file with executable mode is passed to
  21. ;; a new instance of the running shell (or to a Bourne shell if a csh is
  22. ;; running and the file starts with `:'). Only a shell can start such a file,
  23. ;; exec() cannot, which is why it is important to have a magic number in every
  24. ;; executable script. Such a magic number is made up by the characters `#!'
  25. ;; the filename of an interpreter (in COFF, ELF or somesuch format) and one
  26. ;; optional argument.
  27. ;; This library is for certain major modes like sh-, awk-, perl-, tcl- or
  28. ;; makefile-mode to insert or update a suitable #! line at the beginning of
  29. ;; the file, if the file does not already have one and the file is not a
  30. ;; default file of that interpreter (like .profile or makefile). It also
  31. ;; makes the file executable if it wasn't, as soon as it's saved.
  32. ;; It also allows debugging scripts, with an adaptation of compile, as far
  33. ;; as interpreters give out meaningful error messages.
  34. ;; Modes that use this should nconc `executable-map' to the end of their own
  35. ;; keymap and `executable-font-lock-keywords' to the end of their own font
  36. ;; lock keywords. Their mode-setting commands should call
  37. ;; `executable-set-magic'.
  38. ;;; Code:
  39. (defgroup executable nil
  40. "Base functionality for executable interpreter scripts."
  41. :group 'processes)
  42. ;; This used to default to `other', but that doesn't seem to have any
  43. ;; significance. fx 2000-02-11.
  44. (defcustom executable-insert t ; 'other
  45. "*Non-nil means offer to add a magic number to a file.
  46. This takes effect when you switch to certain major modes,
  47. including Shell-script mode (`sh-mode').
  48. When you type \\[executable-set-magic], it always offers to add or
  49. update the magic number."
  50. ;;; :type '(choice (const :tag "off" nil)
  51. ;;; (const :tag "on" t)
  52. ;;; symbol)
  53. :type 'boolean
  54. :group 'executable)
  55. (defcustom executable-query 'function
  56. "*If non-nil, ask user before changing an existing magic number.
  57. When this is `function', only ask when called non-interactively."
  58. :type '(choice (const :tag "Don't Ask" nil)
  59. (const :tag "Ask when non-interactive" function)
  60. (other :tag "Ask" t))
  61. :group 'executable)
  62. (defcustom executable-magicless-file-regexp "/[Mm]akefile$\\|/\\.\\(z?profile\\|bash_profile\\|z?login\\|bash_login\\|z?logout\\|bash_logout\\|.+shrc\\|esrc\\|rcrc\\|[kz]shenv\\)$"
  63. "*On files with this kind of name no magic is inserted or changed."
  64. :type 'regexp
  65. :group 'executable)
  66. (defcustom executable-prefix "#! "
  67. "*Interpreter magic number prefix inserted when there was no magic number."
  68. :type 'string
  69. :group 'executable)
  70. (defcustom executable-chmod 73
  71. "*After saving, if the file is not executable, set this mode.
  72. This mode passed to `set-file-modes' is taken absolutely when negative, or
  73. relative to the files existing modes. Do nothing if this is nil.
  74. Typical values are 73 (+x) or -493 (rwxr-xr-x)."
  75. :type '(choice integer
  76. (const nil))
  77. :group 'executable)
  78. (defvar executable-command nil)
  79. (defcustom executable-self-display "tail"
  80. "*Command you use with argument `+2' to make text files self-display.
  81. Note that the like of `more' doesn't work too well under Emacs \\[shell]."
  82. :type 'string
  83. :group 'executable)
  84. (defvar executable-font-lock-keywords
  85. '(("\\`#!.*/\\([^ \t\n]+\\)" 1 font-lock-keyword-face t))
  86. "*Rules for highlighting executable scripts' magic number.
  87. This can be included in `font-lock-keywords' by modes that call `executable'.")
  88. (defvar executable-error-regexp-alist
  89. '(;; /bin/xyz: syntax error at line 14: `(' unexpected
  90. ;; /bin/xyz[5]: syntax error at line 8 : ``' unmatched
  91. ("^\\(.*[^[/]\\)\\(\\[[0-9]+\\]\\)?: .* error .* line \\([0-9]+\\)" 1 3)
  92. ;; /bin/xyz[27]: ehco: not found
  93. ("^\\(.*[^/]\\)\\[\\([0-9]+\\)\\]: .*: " 1 2)
  94. ;; /bin/xyz: syntax error near unexpected token `)'
  95. ;; /bin/xyz: /bin/xyz: line 2: `)'
  96. ("^\\(.*[^/]\\): [^0-9\n]+\n\\1: \\1: line \\([0-9]+\\):" 1 2)
  97. ;; /usr/bin/awk: syntax error at line 5 of file /bin/xyz
  98. (" error .* line \\([0-9]+\\) of file \\(.+\\)$" 2 1)
  99. ;; /usr/bin/awk: calling undefined function toto
  100. ;; input record number 3, file awktestdata
  101. ;; source line 4 of file /bin/xyz
  102. ("^[^ ].+\n\\( .+\n\\)* line \\([0-9]+\\) of file \\(.+\\)$" 3 2)
  103. ;; makefile:1: *** target pattern contains no `%'. Stop.
  104. ("^\\(.+\\):\\([0-9]+\\): " 1 2))
  105. "Alist of regexps used to match script errors.
  106. See `compilation-error-regexp-alist'.")
  107. ;; The C function openp slightly modified would do the trick fine
  108. (defvaralias 'executable-binary-suffixes 'exec-suffixes)
  109. ;;;###autoload
  110. (defun executable-command-find-posix-p (&optional program)
  111. "Check if PROGRAM handles arguments Posix-style.
  112. If PROGRAM is non-nil, use that instead of \"find\"."
  113. ;; Pick file to search from location we know
  114. (let* ((dir (file-truename data-directory))
  115. (file (car (directory-files dir nil "^[^.]"))))
  116. (with-temp-buffer
  117. (call-process (or program "find")
  118. nil
  119. (current-buffer)
  120. nil
  121. dir
  122. "-name"
  123. file
  124. "-maxdepth"
  125. "1")
  126. (goto-char (point-min))
  127. (if (search-forward file nil t)
  128. t))))
  129. (defun executable-chmod ()
  130. "This gets called after saving a file to assure that it be executable.
  131. You can set the absolute or relative mode in variable `executable-chmod' for
  132. non-executable files."
  133. (and executable-chmod
  134. buffer-file-name
  135. (or (file-executable-p buffer-file-name)
  136. (set-file-modes buffer-file-name
  137. (if (< executable-chmod 0)
  138. (- executable-chmod)
  139. (logior executable-chmod
  140. (file-modes buffer-file-name)))))))
  141. (defvar compilation-error-regexp-alist) ; from compile.el
  142. ;;;###autoload
  143. (defun executable-interpret (command)
  144. "Run script with user-specified args, and collect output in a buffer.
  145. While script runs asynchronously, you can use the \\[next-error]
  146. command to find the next error. The buffer is also in `comint-mode' and
  147. `compilation-shell-minor-mode', so that you can answer any prompts."
  148. (interactive (list (read-string "Run script: "
  149. (or executable-command
  150. buffer-file-name))))
  151. (require 'compile)
  152. (save-some-buffers (not compilation-ask-about-save))
  153. (set (make-local-variable 'executable-command) command)
  154. (let ((compilation-error-regexp-alist executable-error-regexp-alist))
  155. (compilation-start command t (lambda (_x) "*interpretation*"))))
  156. ;;;###autoload
  157. (defun executable-set-magic (interpreter &optional argument
  158. no-query-flag insert-flag)
  159. "Set this buffer's interpreter to INTERPRETER with optional ARGUMENT.
  160. The variables `executable-magicless-file-regexp', `executable-prefix',
  161. `executable-insert', `executable-query' and `executable-chmod' control
  162. when and how magic numbers are inserted or replaced and scripts made
  163. executable."
  164. (interactive
  165. (let* ((name (read-string "Name or file name of interpreter: "))
  166. (arg (read-string (format "Argument for %s: " name))))
  167. (list name arg (eq executable-query 'function) t)))
  168. (setq interpreter (if (file-name-absolute-p interpreter)
  169. interpreter
  170. (or (executable-find interpreter)
  171. (error "Interpreter %s not recognized"
  172. interpreter))))
  173. (setq argument (concat (if (string-match "\\`/:" interpreter)
  174. (replace-match "" nil nil interpreter)
  175. interpreter)
  176. (and argument (string< "" argument) " ")
  177. argument))
  178. (or buffer-read-only
  179. (if buffer-file-name
  180. (string-match executable-magicless-file-regexp
  181. buffer-file-name))
  182. (not (or insert-flag executable-insert))
  183. (> (point-min) 1)
  184. (save-excursion
  185. (goto-char (point-min))
  186. (add-hook 'after-save-hook 'executable-chmod nil t)
  187. (if (looking-at "#![ \t]*\\(.*\\)$")
  188. (and (goto-char (match-beginning 1))
  189. ;; If the line ends in a space,
  190. ;; don't offer to change it.
  191. (not (= (char-after (1- (match-end 1))) ?\s))
  192. (not (string= argument
  193. (buffer-substring (point) (match-end 1))))
  194. (if (or (not executable-query) no-query-flag
  195. (save-window-excursion
  196. ;; Make buffer visible before question.
  197. (switch-to-buffer (current-buffer))
  198. (y-or-n-p (concat "Replace magic number by `"
  199. executable-prefix argument "'? "))))
  200. (progn
  201. (replace-match argument t t nil 1)
  202. (message "Magic number changed to `%s'"
  203. (concat executable-prefix argument)))))
  204. (insert executable-prefix argument ?\n)
  205. (message "Magic number changed to `%s'"
  206. (concat executable-prefix argument)))))
  207. interpreter)
  208. ;;;###autoload
  209. (defun executable-self-display ()
  210. "Turn a text file into a self-displaying Un*x command.
  211. The magic number of such a command displays all lines but itself."
  212. (interactive)
  213. (if (eq this-command 'executable-self-display)
  214. (setq this-command 'executable-set-magic))
  215. (executable-set-magic executable-self-display "+2"))
  216. ;;;###autoload
  217. (defun executable-make-buffer-file-executable-if-script-p ()
  218. "Make file executable according to umask if not already executable.
  219. If file already has any execute bits set at all, do not change existing
  220. file modes."
  221. (and (>= (buffer-size) 2)
  222. (save-restriction
  223. (widen)
  224. (string= "#!" (buffer-substring (point-min) (+ 2 (point-min)))))
  225. (condition-case nil
  226. (let* ((current-mode (file-modes (buffer-file-name)))
  227. (add-mode (logand ?\111 (default-file-modes))))
  228. (or (/= (logand ?\111 current-mode) 0)
  229. (zerop add-mode)
  230. (set-file-modes (buffer-file-name)
  231. (logior current-mode add-mode))))
  232. ;; Eg file-modes can return nil (bug#9879). It should not,
  233. ;; in this context, but we should handle it all the same.
  234. (error (message "Unable to make file executable")))))
  235. (provide 'executable)
  236. ;;; executable.el ends here