2
|
1 ;;; executable.el --- base functionality for executable interpreter scripts
|
|
2
|
|
3 ;; Copyright (C) 1994, 1995, 1996 by Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Daniel.Pfeiffer@Informatik.START.dbp.de, fax (+49 69) 7588-2389
|
|
6 ;; Keywords: languages, unix
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
23 ;; 02111-1307, USA.
|
|
24
|
|
25 ;;; Synched up with: FSF 19.34.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; executable.el is used by certain major modes to insert a suitable
|
|
30 ;; #! line at the beginning of the file, if the file does not already
|
|
31 ;; have one.
|
|
32
|
|
33 ;; Unless it has a magic number, a Unix file with executable mode is passed to
|
|
34 ;; a new instance of the running shell (or to a Bourne shell if a csh is
|
|
35 ;; running and the file starts with `:'). Only a shell can start such a file,
|
|
36 ;; exec() cannot, which is why it is important to have a magic number in every
|
|
37 ;; executable script. Such a magic number is made up by the characters `#!'
|
|
38 ;; the filename of an interpreter (in COFF, ELF or somesuch format) and one
|
|
39 ;; optional argument.
|
|
40
|
|
41 ;; This library is for certain major modes like sh-, awk-, perl-, tcl- or
|
|
42 ;; makefile-mode to insert or update a suitable #! line at the beginning of
|
|
43 ;; the file, if the file does not already have one and the file is not a
|
|
44 ;; default file of that interpreter (like .profile or makefile). It also
|
|
45 ;; makes the file executable if it wasn't, as soon as it's saved.
|
|
46
|
|
47 ;; It also allows debugging scripts, with an adaptation of compile, as far
|
|
48 ;; as interpreters give out meaningful error messages.
|
|
49
|
|
50 ;; Modes that use this should nconc `executable-map' to the end of their own
|
|
51 ;; keymap and `executable-font-lock-keywords' to the end of their own font
|
|
52 ;; lock keywords. Their mode-setting commands should call
|
|
53 ;; `executable-set-magic'.
|
|
54
|
|
55 ;;; Code:
|
|
56
|
120
|
57
|
|
58 (defgroup executable nil
|
|
59 "Base functionality for executable interpreter scripts"
|
|
60 :group 'processes)
|
|
61
|
|
62
|
|
63 (defcustom executable-insert 'other
|
2
|
64 "*What to do when newly found file has no or wrong magic number:
|
|
65 nil do nothing
|
|
66 t insert or update magic number
|
|
67 other insert or update magic number, but mark as unmodified.
|
|
68 When the insertion is marked as unmodified, you can save it with \\[write-file] RET.
|
|
69 This variable is used when `executable-set-magic' is called as a function,
|
|
70 e.g. when Emacs sets some Un*x interpreter script mode.
|
120
|
71 With \\[executable-set-magic], this is always treated as if it were `t'."
|
|
72 :type '(choice (const :tag "off" nil)
|
|
73 (const :tag "on" t)
|
|
74 symbol)
|
|
75 :group 'executable)
|
2
|
76
|
|
77
|
120
|
78 (defcustom executable-query 'function
|
2
|
79 "*If non-`nil', ask user before inserting or changing magic number.
|
120
|
80 When this is `function', only ask when called non-interactively."
|
|
81 :type '(choice (const :tag "Don't Ask" nil)
|
|
82 (const :tag "Ask" t)
|
|
83 (const :tag "Ask when non-interactive" function))
|
|
84 :group 'executable)
|
2
|
85
|
|
86
|
120
|
87 (defcustom executable-magicless-file-regexp "/[Mm]akefile$\\|/\\.\\(z?profile\\|bash_profile\\|z?login\\|bash_login\\|z?logout\\|bash_logout\\|.+shrc\\|esrc\\|rcrc\\|[kz]shenv\\)$"
|
|
88 "*On files with this kind of name no magic is inserted or changed."
|
|
89 :type 'regexp
|
|
90 :group 'executable)
|
2
|
91
|
|
92
|
120
|
93 (defcustom executable-prefix "#! "
|
|
94 "*Interpreter magic number prefix inserted when there was no magic number."
|
|
95 :type 'string
|
|
96 :group 'executable)
|
2
|
97
|
|
98
|
|
99
|
120
|
100 (defcustom executable-chmod 73
|
2
|
101 "*After saving, if the file is not executable, set this mode.
|
|
102 This mode passed to `set-file-modes' is taken absolutely when negative, or
|
|
103 relative to the files existing modes. Do nothing if this is nil.
|
120
|
104 Typical values are 73 (+x) or -493 (rwxr-xr-x)."
|
|
105 :type 'integer
|
|
106 :group 'executable)
|
2
|
107
|
|
108
|
|
109 (defvar executable-command nil)
|
|
110
|
120
|
111 (defcustom executable-self-display "tail"
|
2
|
112 "*Command you use with argument `+2' to make text files self-display.
|
120
|
113 Note that the like of `more' doesn't work too well under Emacs \\[shell]."
|
|
114 :type 'string
|
|
115 :group 'executable)
|
2
|
116
|
|
117
|
|
118 (defvar executable-font-lock-keywords
|
|
119 '(("\\`#!.*/\\([^ \t\n]+\\)" 1 font-lock-keyword-face t))
|
|
120 "*Rules for highlighting executable scripts' magic number.
|
|
121 This can be included in `font-lock-keywords' by modes that call `executable'.")
|
|
122
|
|
123
|
|
124 (defvar executable-error-regexp-alist
|
|
125 '(;; /bin/xyz: syntax error at line 14: `(' unexpected
|
|
126 ;; /bin/xyz[5]: syntax error at line 8 : ``' unmatched
|
|
127 ("^\\(.*[^[/]\\)\\(\\[[0-9]+\\]\\)?: .* error .* line \\([0-9]+\\)" 1 3)
|
|
128 ;; /bin/xyz[27]: ehco: not found
|
|
129 ("^\\(.*[^/]\\)\\[\\([0-9]+\\)\\]: .*: " 1 2)
|
|
130 ;; /bin/xyz: syntax error near unexpected token `)'
|
|
131 ;; /bin/xyz: /bin/xyz: line 2: `)'
|
|
132 ("^\\(.*[^/]\\): [^0-9\n]+\n\\1: \\1: line \\([0-9]+\\):" 1 2)
|
|
133 ;; /usr/bin/awk: syntax error at line 5 of file /bin/xyz
|
|
134 (" error .* line \\([0-9]+\\) of file \\(.+\\)$" 2 1)
|
|
135 ;; /usr/bin/awk: calling undefined function toto
|
|
136 ;; input record number 3, file awktestdata
|
|
137 ;; source line 4 of file /bin/xyz
|
|
138 ("^[^ ].+\n\\( .+\n\\)* line \\([0-9]+\\) of file \\(.+\\)$" 3 2)
|
|
139 ;; makefile:1: *** target pattern contains no `%'. Stop.
|
|
140 ("^\\(.+\\):\\([0-9]+\\): " 1 2))
|
|
141 "Alist of regexps used to match script errors.
|
|
142 See `compilation-error-regexp-alist'.")
|
|
143
|
|
144 ;; The C function openp slightly modified would do the trick fine
|
|
145 (defun executable-find (command)
|
|
146 "Search for COMMAND in exec-path and return the absolute file name.
|
|
147 Return nil if COMMAND is not found anywhere in `exec-path'."
|
|
148 (let ((list exec-path)
|
|
149 file)
|
|
150 (while list
|
|
151 (setq list (if (and (setq file (expand-file-name command (car list)))
|
|
152 (file-executable-p file)
|
|
153 (not (file-directory-p file)))
|
|
154 nil
|
|
155 (setq file nil)
|
|
156 (cdr list))))
|
|
157 file))
|
|
158
|
|
159
|
|
160 (defun executable-chmod ()
|
|
161 "This gets called after saving a file to assure that it be executable.
|
|
162 You can set the absolute or relative mode in variable `executable-chmod' for
|
|
163 non-executable files."
|
|
164 (and executable-chmod
|
|
165 buffer-file-name
|
|
166 (or (file-executable-p buffer-file-name)
|
|
167 (set-file-modes buffer-file-name
|
|
168 (if (< executable-chmod 0)
|
|
169 (- executable-chmod)
|
|
170 (logior executable-chmod
|
|
171 (file-modes buffer-file-name)))))))
|
|
172
|
|
173
|
|
174 (defun executable-interpret (command)
|
|
175 "Run script with user-specified args, and collect output in a buffer.
|
|
176 While script runs asynchronously, you can use the \\[next-error] command
|
|
177 to find the next error."
|
|
178 (interactive (list (read-string "Run script: "
|
|
179 (or executable-command
|
|
180 buffer-file-name))))
|
|
181 (require 'compile)
|
|
182 (save-some-buffers (not compilation-ask-about-save))
|
|
183 (make-local-variable 'executable-command)
|
|
184 (compile-internal (setq executable-command command)
|
|
185 "No more errors." "Interpretation"
|
|
186 ;; Give it a simpler regexp to match.
|
|
187 nil executable-error-regexp-alist))
|
|
188
|
|
189
|
|
190
|
|
191 ;;;###autoload
|
|
192 (defun executable-set-magic (interpreter &optional argument
|
|
193 no-query-flag insert-flag)
|
|
194 "Set this buffer's interpreter to INTERPRETER with optional ARGUMENT.
|
|
195 The variables `executable-magicless-file-regexp', `executable-prefix',
|
|
196 `executable-insert', `executable-query' and `executable-chmod' control
|
|
197 when and how magic numbers are inserted or replaced and scripts made
|
|
198 executable."
|
|
199 (interactive
|
|
200 (let* ((name (read-string "Name or file name of interpreter: "))
|
|
201 (arg (read-string (format "Argument for %s: " name))))
|
|
202 (list name arg (eq executable-query 'function) t)))
|
|
203 (setq interpreter (if (file-name-absolute-p interpreter)
|
|
204 interpreter
|
|
205 (or (executable-find interpreter)
|
|
206 (error "Interpreter %s not recognized" interpreter)))
|
|
207 argument (concat interpreter
|
|
208 (and argument (string< "" argument) " ")
|
|
209 argument))
|
|
210 (or buffer-read-only
|
|
211 (if buffer-file-name
|
|
212 (string-match executable-magicless-file-regexp
|
|
213 buffer-file-name))
|
|
214 (not (or insert-flag executable-insert))
|
|
215 (> (point-min) 1)
|
|
216 (save-excursion
|
|
217 (let ((point (point-marker))
|
|
218 (buffer-modified-p (buffer-modified-p)))
|
|
219 (goto-char (point-min))
|
|
220 (make-local-hook 'after-save-hook)
|
|
221 (add-hook 'after-save-hook 'executable-chmod nil t)
|
|
222 (if (looking-at "#![ \t]*\\(.*\\)$")
|
|
223 (and (goto-char (match-beginning 1))
|
|
224 ;; If the line ends in a space,
|
|
225 ;; don't offer to change it.
|
|
226 (not (= (char-after (1- (match-end 1))) ?\ ))
|
|
227 (not (string= argument
|
|
228 (buffer-substring (point) (match-end 1))))
|
|
229 (if (or (not executable-query) no-query-flag
|
|
230 (save-window-excursion
|
|
231 ;; Make buffer visible before question.
|
|
232 (switch-to-buffer (current-buffer))
|
|
233 (y-or-n-p (concat "Replace magic number by `"
|
|
234 executable-prefix argument "'? "))))
|
|
235 (progn
|
98
|
236 (replace-match (concat executable-prefix argument)
|
|
237 t t nil 1)
|
2
|
238 (message "Magic number changed to `%s'"
|
|
239 (concat executable-prefix argument)))))
|
|
240 (insert executable-prefix argument ?\n)
|
|
241 (message "Magic number changed to `%s'"
|
|
242 (concat executable-prefix argument)))
|
|
243 ;;; (or insert-flag
|
|
244 ;;; (eq executable-insert t)
|
|
245 ;;; (set-buffer-modified-p buffer-modified-p))
|
|
246 )))
|
|
247 interpreter)
|
|
248
|
|
249
|
|
250
|
|
251 ;;;###autoload
|
|
252 (defun executable-self-display ()
|
|
253 "Turn a text file into a self-displaying Un*x command.
|
|
254 The magic number of such a command displays all lines but itself."
|
|
255 (interactive)
|
|
256 (if (eq this-command 'executable-self-display)
|
|
257 (setq this-command 'executable-set-magic))
|
|
258 (executable-set-magic executable-self-display "+2"))
|
|
259
|
|
260
|
|
261
|
|
262 (provide 'executable)
|
|
263
|
|
264 ;; executable.el ends here
|
|
265
|
|
266
|