611
|
1 ;;; win32-native.el --- Lisp routines when running on native MS Windows.
|
442
|
2
|
|
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
|
|
4 ;; Copyright (C) 2000 Ben Wing.
|
|
5
|
|
6 ;; Maintainer: XEmacs Development Team
|
|
7 ;; Keywords: mouse, dumped
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
611
|
26 ;;; Synched up with: Not in FSF.
|
442
|
27 ;;; (FSF has stuff in w32-fns.el and term/w32-win.el.)
|
|
28
|
|
29 ;;; Commentary:
|
|
30
|
|
31 ;; This file is dumped with XEmacs for MS Windows (without cygwin).
|
611
|
32 ;; It is for stuff that is used specifically when `system-type' eq
|
|
33 ;; `windows-nt' (i.e. also applies to MinGW), and has nothing to do
|
|
34 ;; with the `mswindows' device type. Thus, it probably applies in
|
|
35 ;; non-interactive mode as well, and it DOES NOT APPLY to Cygwin.
|
442
|
36
|
611
|
37 ;; Based (originally) on NT Emacs version by Geoff Voelker
|
|
38 ;; (voelker@cs.washington.edu)
|
442
|
39 ;; Ported to XEmacs by Marc Paquette <marcpa@cam.org>
|
|
40 ;; Largely modified by Kirill M. Katsnelson <kkm@kis.ru>
|
611
|
41 ;; Rewritten from scratch by Ben Wing <ben@xemacs.org>. No code in common
|
|
42 ;; with FSF.
|
442
|
43
|
|
44 ;;; Code:
|
|
45
|
|
46 ;; For appending suffixes to directories and files in shell
|
|
47 ;; completions. This screws up cygwin users so we leave it out for
|
|
48 ;; now. Uncomment this if you only ever want to use cmd.
|
|
49
|
|
50 ;(defun nt-shell-mode-hook ()
|
|
51 ; (setq comint-completion-addsuffix '("\\" . " ")
|
|
52 ; comint-process-echoes t))
|
|
53 ;(add-hook 'shell-mode-hook 'nt-shell-mode-hook)
|
|
54
|
|
55 ;; Use ";" instead of ":" as a path separator (from files.el).
|
|
56 (setq path-separator ";")
|
|
57
|
|
58 ;; Set the null device (for compile.el).
|
|
59 ;; #### There should be such a global thingy as null-device - kkm
|
776
|
60 (defvar grep-null-device)
|
442
|
61 (setq grep-null-device "NUL")
|
|
62
|
|
63 ;; Set the grep regexp to match entries with drive letters.
|
776
|
64 (defvar grep-regexp-alist)
|
442
|
65 (setq grep-regexp-alist
|
|
66 '(("^\\(\\([a-zA-Z]:\\)?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 3)))
|
|
67
|
611
|
68 (defvar mswindows-system-shells '("cmd" "cmd.exe" "command" "command.com"
|
|
69 "4nt" "4nt.exe" "4dos" "4dos.exe"
|
|
70 "ndos" "ndos.exe")
|
|
71 "List of strings recognized as Windows NT/9X system shells.
|
|
72 These are shells with native semantics, e.g. they use `/c', not '-c',
|
|
73 to pass a command in.")
|
|
74
|
|
75 (defun mswindows-system-shell-p (shell-name)
|
|
76 (member (downcase (file-name-nondirectory shell-name))
|
|
77 mswindows-system-shells))
|
|
78
|
|
79 (defun init-mswindows-at-startup ()
|
|
80 ;; shell-file-name is initialized in the C code (callproc.c) from
|
|
81 ;; SHELL or COMSPEC.
|
|
82 ;; #### If only shell-command-switch could be a function. But there
|
|
83 ;; is code littered around that uses it.
|
|
84 ;; #### Maybe we should set a symbol-value handler on `shell-file-name'
|
|
85 ;; that automatically sets shell-command-switch?
|
|
86 (if (mswindows-system-shell-p shell-file-name)
|
|
87 (setq shell-command-switch "/c")))
|
|
88
|
442
|
89 ;;----------------------------------------------------------------------
|
|
90 ;; Quoting process args
|
|
91 ;;--------------------
|
|
92
|
|
93 (defvar debug-mswindows-process-command-lines nil
|
|
94 "If non-nil, output debug information about the command lines constructed.
|
|
95 This can be useful if you are getting process errors where the arguments
|
|
96 to the process appear to be getting passed incorrectly.")
|
|
97
|
|
98 ;; properly quotify one arg for the vc runtime argv constructor.
|
|
99 (defun mswindows-quote-one-vc-runtime-arg (arg &optional quote-shell)
|
|
100 ;; we mess with any arg with whitespace, quotes, or globbing chars in it.
|
|
101 ;; we also include shell metachars if asked.
|
|
102 ;; note that \ is NOT included! it's perfectly OK to include an
|
|
103 ;; arg like c:\ or c:\foo.
|
611
|
104 (cond ((equal arg "") "\"\"")
|
|
105 ((string-match
|
|
106 (if quote-shell "[ \t\n\r\f*?\"<>|&^%]" "[ \t\n\r\f*?\"]")
|
|
107 arg)
|
|
108 ;; handle nested quotes, possibly preceded by backslashes
|
|
109 (setq arg (replace-in-string arg "\\([\\]*\\)\"" "\\1\\1\\\\\""))
|
|
110 ;; handle trailing backslashes
|
|
111 (setq arg (replace-in-string arg "\\([\\]+\\)$" "\\1\\1"))
|
|
112 (concat "\"" arg "\""))
|
|
113 (t arg)))
|
442
|
114
|
|
115 (defun mswindows-quote-one-simple-arg (arg &optional quote-shell)
|
|
116 ;; just put double quotes around args with spaces (and maybe shell
|
|
117 ;; metachars).
|
611
|
118 (cond ((equal arg "") "\"\"")
|
|
119 ((string-match
|
|
120 (if quote-shell "[ \t\n\r\f*?\"<>|&^%]" "[ \t\n\r\f*?]")
|
|
121 arg)
|
|
122 (concat "\"" arg "\""))
|
|
123 (t arg)))
|
442
|
124
|
|
125 (defun mswindows-quote-one-command-arg (arg)
|
|
126 ;; quote an arg to get it past COMMAND.COM/CMD.EXE: need to quote shell
|
|
127 ;; metachars with ^.
|
611
|
128 (cond ((equal arg "") "\"\"")
|
|
129 (t (replace-in-string "[<>|&^%]" "^\\1" arg))))
|
442
|
130
|
|
131 (defun mswindows-construct-verbatim-command-line (program args)
|
|
132 (mapconcat #'identity args " "))
|
|
133
|
|
134 ;; for use with either standard VC++ compiled programs or Cygwin programs,
|
|
135 ;; which emulate the same behavior.
|
|
136 (defun mswindows-construct-vc-runtime-command-line (program args)
|
|
137 (mapconcat #'mswindows-quote-one-vc-runtime-arg args " "))
|
|
138
|
|
139 ;; note: for pulling apart an arg:
|
|
140 ;; each arg consists of either
|
|
141
|
|
142 ;; something surrounded by single quotes
|
|
143
|
|
144 ;; or
|
|
145
|
|
146 ;; one or more of
|
|
147
|
|
148 ;; 1. a non-ws, non-" char
|
|
149 ;; 2. a section of double-quoted text
|
|
150 ;; 3. a section of double-quoted text with end-of-string instead of the final
|
|
151 ;; quote.
|
|
152
|
|
153 ;; 2 and 3 get handled together.
|
|
154
|
|
155 ;; quoted text is one of
|
|
156 ;;
|
|
157 ;; 1. quote + even number of backslashes + quote, or
|
|
158 ;; 2. quote + non-greedy anything + non-backslash + even number of
|
|
159 ;; backslashes + quote.
|
|
160
|
|
161 ;; we need to separate the two because we unfortunately have no non-greedy
|
|
162 ;; ? operator. (urk! we actually do, but it wasn't documented.) --ben
|
|
163
|
|
164 ;; if you want to mess around, keep this test case in mind:
|
|
165
|
|
166 ;; this string
|
|
167
|
|
168 ;; " as'f 'FOO BAR' '' \"\" \"asdf \\ \\\" \\\\\\\" asdfasdf\\\\\" foo\" "
|
|
169
|
|
170 ;; should tokenize into this:
|
|
171
|
|
172 ;; (" " "as'f" " " "'FOO BAR' " "'' " "\"\"" " " "\"asdf \\ \\\" \\\\\\\" asdfasdf\\\\\"" " " "foo" "\" ")
|
|
173
|
|
174 ;; this regexp actually separates the arg into individual args, like a
|
|
175 ;; shell (such as sh) does, but using vc-runtime rules. it's easy to
|
|
176 ;; derive the tokenizing regexp from it, and that's exactly what i did.
|
|
177 ;; but oh was it hard to get this first regexp right. --ben
|
|
178 ;(defvar mswindows-match-one-cmd-exe-arg-regexp
|
|
179 ; (concat
|
|
180 ; "^\\("
|
|
181 ; "'\\([\\]*\\)\\2'" "\\|"
|
|
182 ; "'.*?[^\\]\\(\\([\\]*\\)\\4'\\)" "\\|"
|
|
183 ; "\\("
|
|
184 ; "[^ \t\n\r\f\v\"]" "\\|"
|
|
185 ; "\"\\([\\]*\\)\\6\"" "\\|"
|
|
186 ; "\".*?[^\\]\\(\\([\\]*\\)\\8\"\\|$\\)"
|
|
187 ; "\\)+"
|
|
188 ; "\\)"
|
|
189 ; "\\([ \t\n\r\f\v]+\\|$\\)"))
|
|
190
|
|
191 (defvar mswindows-match-one-cmd-exe-token-regexp
|
|
192 (concat
|
|
193 "^\\("
|
|
194 "[ \t\n\r\f\v]+" "\\|"
|
|
195 "'\\([\\]*\\)\\2'" "\\([ \t\n\r\f\v]+\\|$\\)" "\\|"
|
|
196 "'.*?[^\\]\\(\\([\\]*\\)\\5'\\)" "\\([ \t\n\r\f\v]+\\|$\\)" "\\|"
|
|
197 "[^ \t\n\r\f\v\"]+" "\\|"
|
|
198 "\"\\([\\]*\\)\\7\"" "\\|"
|
|
199 "\".*?[^\\]\\(\\([\\]*\\)\\9\"\\|$\\)"
|
|
200 "\\)"))
|
|
201
|
|
202 (defun mswindows-construct-command-command-line (program args)
|
|
203 ;; for use with COMMAND.COM and CMD.EXE:
|
|
204 ;; for each arg, tokenize it into quoted and non-quoted sections;
|
|
205 ;; then quote all the shell meta-chars with ^; then put everything
|
|
206 ;; back together. the truly hard part is the tokenizing -- typically
|
|
207 ;; we get a single argument (the command to execute) and we have to
|
|
208 ;; worry about quotes that are backslash-quoted and such.
|
|
209 (mapconcat
|
|
210 #'(lambda (arg)
|
|
211 (mapconcat
|
|
212 #'(lambda (part)
|
|
213 (if (string-match "^'" part)
|
|
214 (replace-in-string part "\\([<>|^&%]\\)" "^\\1")
|
|
215 part))
|
|
216 (let (parts)
|
|
217 (while (and (> (length arg) 0)
|
|
218 (string-match
|
|
219 mswindows-match-one-cmd-exe-token-regexp
|
|
220 arg))
|
|
221 (push (match-string 0 arg) parts)
|
|
222 (setq arg (substring arg (match-end 0))))
|
|
223 (if (> (length arg) 0)
|
|
224 (push arg parts))
|
|
225 (nreverse parts))
|
|
226 ""))
|
|
227 args " "))
|
|
228
|
|
229 (defvar mswindows-construct-process-command-line-alist
|
611
|
230 '(
|
|
231 ;; at one point (pre-1.0), this was required for Cygwin bash.
|
|
232 ;; evidently, Cygwin changed its arg handling to work just like
|
|
233 ;; any standard VC program, so we no longer need it.
|
|
234 ;;("[\\/].?.?sh\\." . mswindows-construct-verbatim-command-line)
|
442
|
235 ("[\\/]command\\.com$" . mswindows-construct-command-command-line)
|
|
236 ("[\\/]cmd\\.exe$" . mswindows-construct-command-command-line)
|
|
237 ("" . mswindows-construct-vc-runtime-command-line))
|
|
238 "An alist for determining proper argument quoting given executable
|
|
239 file name. Car of each cons should be a string, a regexp against
|
|
240 which the file name is matched. Matching is case-insensitive but does
|
|
241 include the directory, so you should begin your regexp with [\\\\/] if
|
|
242 you don't want the directory to matter. Alternatively, the car can be
|
|
243 a function of one arg, which is called with the executable's name and
|
|
244 should return t if this entry should be processed. Cdr is a function
|
|
245 symbol, which is called with two args, the executable name and a list
|
|
246 of the args passed to it. It should return a string, which includes
|
|
247 the executable's args (but not the executable name itself) properly
|
|
248 quoted and pasted together. The list is matched in order, and the
|
|
249 first matching entry specifies how the processing will happen.")
|
|
250
|
|
251 (defun mswindows-construct-process-command-line (args)
|
|
252 ;;Properly quote process ARGS for executing (car ARGS).
|
|
253 ;;Called from the C code.
|
|
254 (let ((fname (car args))
|
|
255 (alist mswindows-construct-process-command-line-alist)
|
|
256 (case-fold-search t)
|
|
257 (return-me nil)
|
|
258 (assoc nil))
|
|
259 (while (and alist
|
|
260 (null return-me))
|
|
261 (setq assoc (pop alist))
|
|
262 (if (if (stringp (car assoc))
|
|
263 (string-match (car assoc) fname)
|
|
264 (funcall (car assoc) fname))
|
|
265 (setq return-me (cdr assoc))))
|
|
266 (let* ((called-fun (or return-me
|
|
267 #'mswindows-construct-vc-runtime-command-line))
|
|
268 (retval
|
|
269 (let ((str (funcall called-fun fname (cdr args)))
|
|
270 (quoted-fname (mswindows-quote-one-simple-arg fname)))
|
|
271 (if (and str (> (length str) 0))
|
|
272 (concat quoted-fname " " str)
|
|
273 quoted-fname))))
|
|
274 (when debug-mswindows-process-command-lines
|
|
275 (debug-print "mswindows-construct-process-command-line called:\n")
|
|
276 (debug-print "received args: \n%s"
|
|
277 (let ((n -1))
|
|
278 (mapconcat #'(lambda (arg)
|
|
279 (incf n)
|
|
280 (format " %d %s\n" n arg))
|
|
281 args
|
|
282 "")))
|
|
283 (debug-print "called fun %s\n" called-fun)
|
|
284 (debug-print "resulting command line: %s\n" retval))
|
|
285 retval)))
|
|
286
|
|
287 ;;; win32-native.el ends here
|