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
|
|
57 (defvar executable-insert 'not-modified
|
|
58 "*What to do when newly found file has no or wrong magic number:
|
|
59 nil do nothing
|
|
60 t insert or update magic number
|
|
61 other insert or update magic number, but mark as unmodified.
|
|
62 When the insertion is marked as unmodified, you can save it with \\[write-file] RET.
|
|
63 This variable is used when `executable-set-magic' is called as a function,
|
|
64 e.g. when Emacs sets some Un*x interpreter script mode.
|
|
65 With \\[executable-set-magic], this is always treated as if it were `t'.")
|
|
66
|
|
67
|
|
68 (defvar executable-query 'function
|
|
69 "*If non-`nil', ask user before inserting or changing magic number.
|
|
70 When this is `function', only ask when called non-interactively.")
|
|
71
|
|
72
|
|
73 (defvar executable-magicless-file-regexp "/[Mm]akefile$\\|/\\.\\(z?profile\\|bash_profile\\|z?login\\|bash_login\\|z?logout\\|bash_logout\\|.+shrc\\|esrc\\|rcrc\\|[kz]shenv\\)$"
|
|
74 "*On files with this kind of name no magic is inserted or changed.")
|
|
75
|
|
76
|
|
77 (defvar executable-prefix "#! "
|
|
78 "*Interpreter magic number prefix inserted when there was no magic number.")
|
|
79
|
|
80
|
|
81
|
|
82 (defvar executable-chmod 73
|
|
83 "*After saving, if the file is not executable, set this mode.
|
|
84 This mode passed to `set-file-modes' is taken absolutely when negative, or
|
|
85 relative to the files existing modes. Do nothing if this is nil.
|
|
86 Typical values are 73 (+x) or -493 (rwxr-xr-x).")
|
|
87
|
|
88
|
|
89 (defvar executable-command nil)
|
|
90
|
|
91 (defvar executable-self-display "tail"
|
|
92 "*Command you use with argument `+2' to make text files self-display.
|
|
93 Note that the like of `more' doesn't work too well under Emacs \\[shell].")
|
|
94
|
|
95
|
|
96 (defvar executable-font-lock-keywords
|
|
97 '(("\\`#!.*/\\([^ \t\n]+\\)" 1 font-lock-keyword-face t))
|
|
98 "*Rules for highlighting executable scripts' magic number.
|
|
99 This can be included in `font-lock-keywords' by modes that call `executable'.")
|
|
100
|
|
101
|
|
102 (defvar executable-error-regexp-alist
|
|
103 '(;; /bin/xyz: syntax error at line 14: `(' unexpected
|
|
104 ;; /bin/xyz[5]: syntax error at line 8 : ``' unmatched
|
|
105 ("^\\(.*[^[/]\\)\\(\\[[0-9]+\\]\\)?: .* error .* line \\([0-9]+\\)" 1 3)
|
|
106 ;; /bin/xyz[27]: ehco: not found
|
|
107 ("^\\(.*[^/]\\)\\[\\([0-9]+\\)\\]: .*: " 1 2)
|
|
108 ;; /bin/xyz: syntax error near unexpected token `)'
|
|
109 ;; /bin/xyz: /bin/xyz: line 2: `)'
|
|
110 ("^\\(.*[^/]\\): [^0-9\n]+\n\\1: \\1: line \\([0-9]+\\):" 1 2)
|
|
111 ;; /usr/bin/awk: syntax error at line 5 of file /bin/xyz
|
|
112 (" error .* line \\([0-9]+\\) of file \\(.+\\)$" 2 1)
|
|
113 ;; /usr/bin/awk: calling undefined function toto
|
|
114 ;; input record number 3, file awktestdata
|
|
115 ;; source line 4 of file /bin/xyz
|
|
116 ("^[^ ].+\n\\( .+\n\\)* line \\([0-9]+\\) of file \\(.+\\)$" 3 2)
|
|
117 ;; makefile:1: *** target pattern contains no `%'. Stop.
|
|
118 ("^\\(.+\\):\\([0-9]+\\): " 1 2))
|
|
119 "Alist of regexps used to match script errors.
|
|
120 See `compilation-error-regexp-alist'.")
|
|
121
|
|
122 ;; The C function openp slightly modified would do the trick fine
|
|
123 (defun executable-find (command)
|
|
124 "Search for COMMAND in exec-path and return the absolute file name.
|
|
125 Return nil if COMMAND is not found anywhere in `exec-path'."
|
|
126 (let ((list exec-path)
|
|
127 file)
|
|
128 (while list
|
|
129 (setq list (if (and (setq file (expand-file-name command (car list)))
|
|
130 (file-executable-p file)
|
|
131 (not (file-directory-p file)))
|
|
132 nil
|
|
133 (setq file nil)
|
|
134 (cdr list))))
|
|
135 file))
|
|
136
|
|
137
|
|
138 (defun executable-chmod ()
|
|
139 "This gets called after saving a file to assure that it be executable.
|
|
140 You can set the absolute or relative mode in variable `executable-chmod' for
|
|
141 non-executable files."
|
|
142 (and executable-chmod
|
|
143 buffer-file-name
|
|
144 (or (file-executable-p buffer-file-name)
|
|
145 (set-file-modes buffer-file-name
|
|
146 (if (< executable-chmod 0)
|
|
147 (- executable-chmod)
|
|
148 (logior executable-chmod
|
|
149 (file-modes buffer-file-name)))))))
|
|
150
|
|
151
|
|
152 (defun executable-interpret (command)
|
|
153 "Run script with user-specified args, and collect output in a buffer.
|
|
154 While script runs asynchronously, you can use the \\[next-error] command
|
|
155 to find the next error."
|
|
156 (interactive (list (read-string "Run script: "
|
|
157 (or executable-command
|
|
158 buffer-file-name))))
|
|
159 (require 'compile)
|
|
160 (save-some-buffers (not compilation-ask-about-save))
|
|
161 (make-local-variable 'executable-command)
|
|
162 (compile-internal (setq executable-command command)
|
|
163 "No more errors." "Interpretation"
|
|
164 ;; Give it a simpler regexp to match.
|
|
165 nil executable-error-regexp-alist))
|
|
166
|
|
167
|
|
168
|
|
169 ;;;###autoload
|
|
170 (defun executable-set-magic (interpreter &optional argument
|
|
171 no-query-flag insert-flag)
|
|
172 "Set this buffer's interpreter to INTERPRETER with optional ARGUMENT.
|
|
173 The variables `executable-magicless-file-regexp', `executable-prefix',
|
|
174 `executable-insert', `executable-query' and `executable-chmod' control
|
|
175 when and how magic numbers are inserted or replaced and scripts made
|
|
176 executable."
|
|
177 (interactive
|
|
178 (let* ((name (read-string "Name or file name of interpreter: "))
|
|
179 (arg (read-string (format "Argument for %s: " name))))
|
|
180 (list name arg (eq executable-query 'function) t)))
|
|
181 (setq interpreter (if (file-name-absolute-p interpreter)
|
|
182 interpreter
|
|
183 (or (executable-find interpreter)
|
|
184 (error "Interpreter %s not recognized" interpreter)))
|
|
185 argument (concat interpreter
|
|
186 (and argument (string< "" argument) " ")
|
|
187 argument))
|
|
188 (or buffer-read-only
|
|
189 (if buffer-file-name
|
|
190 (string-match executable-magicless-file-regexp
|
|
191 buffer-file-name))
|
|
192 (not (or insert-flag executable-insert))
|
|
193 (> (point-min) 1)
|
|
194 (save-excursion
|
|
195 (let ((point (point-marker))
|
|
196 (buffer-modified-p (buffer-modified-p)))
|
|
197 (goto-char (point-min))
|
|
198 (make-local-hook 'after-save-hook)
|
|
199 (add-hook 'after-save-hook 'executable-chmod nil t)
|
|
200 (if (looking-at "#![ \t]*\\(.*\\)$")
|
|
201 (and (goto-char (match-beginning 1))
|
|
202 ;; If the line ends in a space,
|
|
203 ;; don't offer to change it.
|
|
204 (not (= (char-after (1- (match-end 1))) ?\ ))
|
|
205 (not (string= argument
|
|
206 (buffer-substring (point) (match-end 1))))
|
|
207 (if (or (not executable-query) no-query-flag
|
|
208 (save-window-excursion
|
|
209 ;; Make buffer visible before question.
|
|
210 (switch-to-buffer (current-buffer))
|
|
211 (y-or-n-p (concat "Replace magic number by `"
|
|
212 executable-prefix argument "'? "))))
|
|
213 (progn
|
70
|
214 (replace-match argument t t nil 1)
|
2
|
215 (message "Magic number changed to `%s'"
|
|
216 (concat executable-prefix argument)))))
|
|
217 (insert executable-prefix argument ?\n)
|
|
218 (message "Magic number changed to `%s'"
|
|
219 (concat executable-prefix argument)))
|
|
220 ;;; (or insert-flag
|
|
221 ;;; (eq executable-insert t)
|
|
222 ;;; (set-buffer-modified-p buffer-modified-p))
|
|
223 )))
|
|
224 interpreter)
|
|
225
|
|
226
|
|
227
|
|
228 ;;;###autoload
|
|
229 (defun executable-self-display ()
|
|
230 "Turn a text file into a self-displaying Un*x command.
|
|
231 The magic number of such a command displays all lines but itself."
|
|
232 (interactive)
|
|
233 (if (eq this-command 'executable-self-display)
|
|
234 (setq this-command 'executable-set-magic))
|
|
235 (executable-set-magic executable-self-display "+2"))
|
|
236
|
|
237
|
|
238
|
|
239 (provide 'executable)
|
|
240
|
|
241 ;; executable.el ends here
|
|
242
|
|
243
|