611
+ − 1 ;;; win32-native.el --- Lisp routines when running on native MS Windows.
442
+ − 2
+ − 3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
2367
+ − 4 ;; Copyright (C) 2000, 2004 Ben Wing.
442
+ − 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 grep regexp to match entries with drive letters.
776
+ − 59 (defvar grep-regexp-alist)
442
+ − 60 (setq grep-regexp-alist
+ − 61 '(("^\\(\\([a-zA-Z]:\\)?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 3)))
+ − 62
611
+ − 63 (defvar mswindows-system-shells '("cmd" "cmd.exe" "command" "command.com"
+ − 64 "4nt" "4nt.exe" "4dos" "4dos.exe"
+ − 65 "ndos" "ndos.exe")
+ − 66 "List of strings recognized as Windows NT/9X system shells.
+ − 67 These are shells with native semantics, e.g. they use `/c', not '-c',
+ − 68 to pass a command in.")
+ − 69
+ − 70 (defun mswindows-system-shell-p (shell-name)
+ − 71 (member (downcase (file-name-nondirectory shell-name))
+ − 72 mswindows-system-shells))
+ − 73
+ − 74 (defun init-mswindows-at-startup ()
+ − 75 ;; shell-file-name is initialized in the C code (callproc.c) from
+ − 76 ;; SHELL or COMSPEC.
+ − 77 ;; #### If only shell-command-switch could be a function. But there
+ − 78 ;; is code littered around that uses it.
+ − 79 ;; #### Maybe we should set a symbol-value handler on `shell-file-name'
+ − 80 ;; that automatically sets shell-command-switch?
+ − 81 (if (mswindows-system-shell-p shell-file-name)
+ − 82 (setq shell-command-switch "/c")))
+ − 83
2367
+ − 84 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+ − 85 ;; ;;
+ − 86 ;; Quoting process args ;;
+ − 87 ;; ;;
+ − 88 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+ − 89
+ − 90 ;; Converting a bunch of args into a single command line or vice-versa is
+ − 91 ;; extremely hairy due to the quoting conventions needed. There is in fact
+ − 92 ;; code that does this in the CRT, and perhaps we should look at it and
+ − 93 ;; follow the logic.
+ − 94
+ − 95 ;; Here is some further info from MSDN, discovered *AFTER* the actual code
+ − 96 ;; below was written, and hence the code may not follow what it should.
+ − 97 ;; !!#### But this is definitely something to be fixed up. The article is
+ − 98 ;; called "Parsing C++ Command-Line Arguments", Visual Tools and Langs ->
+ − 99 ;; Visual Studio -> Visual C++ -> Reference -> C/C++ Lang and ... -> C++
+ − 100 ;; Lang Ref -> Basic Concepts -> Startup and Termination -> Program
+ − 101 ;; Startup: the main Function.
+ − 102
+ − 103 ;; Microsoft Specific
+ − 104 ;;
+ − 105 ;; Microsoft C/C++ startup code uses the following rules when interpreting
+ − 106 ;; arguments given on the operating system command line:
+ − 107 ;;
+ − 108 ;; Arguments are delimited by white space, which is either a space or a tab.
+ − 109 ;;
+ − 110 ;; The caret character (^) is not recognized as an escape character or
+ − 111 ;; delimiter. The character is handled completely by the command-line parser
+ − 112 ;; in the operating system before being passed to the argv array in the
+ − 113 ;; program.
+ − 114 ;;
+ − 115 ;; A string surrounded by double quotation marks ("string") is interpreted as
+ − 116 ;; a single argument, regardless of white space contained within. A quoted
+ − 117 ;; string can be embedded in an argument.
+ − 118 ;;
+ − 119 ;; A double quotation mark preceded by a backslash ( \") is interpreted as a
+ − 120 ;; literal double quotation mark character (").
+ − 121 ;;
+ − 122 ;; Backslashes are interpreted literally, unless they immediately precede a
+ − 123 ;; double quotation mark.
+ − 124 ;;
+ − 125 ;; If an even number of backslashes is followed by a double quotation mark,
+ − 126 ;; one backslash is placed in the argv array for every pair of backslashes,
+ − 127 ;; and the double quotation mark is interpreted as a string delimiter.
+ − 128 ;;
+ − 129 ;; If an odd number of backslashes is followed by a double quotation mark, one
+ − 130 ;; backslash is placed in the argv array for every pair of backslashes, and
+ − 131 ;; the double quotation mark is "escaped" by the remaining backslash,
+ − 132 ;; causing a literal double quotation mark (") to be placed in argv.
+ − 133 ;;
+ − 134 ;; The following program demonstrates how command-line arguments are passed:
+ − 135 ;;
+ − 136 ;; include <iostream.h>
+ − 137 ;;
+ − 138 ;; void main( int argc, // Number of strings in array argv
+ − 139 ;; char *argv[], // Array of command-line argument strings
+ − 140 ;; char *envp[] ) // Array of environment variable strings
+ − 141 ;; {
+ − 142 ;; int count;
+ − 143 ;;
+ − 144 ;; // Display each command-line argument.
+ − 145 ;; cout << "\nCommand-line arguments:\n";
+ − 146 ;; for( count = 0; count < argc; count++ )
+ − 147 ;; cout << " argv[" << count << "] "
+ − 148 ;; << argv[count] << "\n";
+ − 149 ;; }
+ − 150 ;;
+ − 151 ;; Table 2.2 shows example input and expected output, demonstrating the rules
+ − 152 ;; in the preceding list.
+ − 153 ;;
+ − 154 ;; Table 2.2
+ − 155 ;;
+ − 156 ;; Command-Line Input argv[1] argv[2] argv[3]
+ − 157 ;; ------------------------------------------
+ − 158 ;; "abc" d e abc d e
+ − 159 ;;
+ − 160 ;; a\\\b d"e f"g h a\\\b de fg h
+ − 161 ;;
+ − 162 ;; a\\\"b c d a\"b c d
+ − 163 ;;
+ − 164 ;; a\\\\"b c" d e a\\b c d e
+ − 165 ;;
+ − 166 ;; END Microsoft Specific
+ − 167 ;;
+ − 168 ;; note: for pulling apart an arg:
+ − 169 ;; each arg consists of either
+ − 170
+ − 171 ;; something surrounded by single quotes
+ − 172
+ − 173 ;; or
+ − 174
+ − 175 ;; one or more of
+ − 176
+ − 177 ;; 1. a non-ws, non-" char
+ − 178 ;; 2. a section of double-quoted text
+ − 179 ;; 3. a section of double-quoted text with end-of-string instead of the final
+ − 180 ;; quote.
+ − 181
+ − 182 ;; 2 and 3 get handled together.
+ − 183
+ − 184 ;; quoted text is one of
+ − 185 ;;
+ − 186 ;; 1. quote + even number of backslashes + quote, or
+ − 187 ;; 2. quote + non-greedy anything + non-backslash + even number of
+ − 188 ;; backslashes + quote.
+ − 189
+ − 190 ;; we need to separate the two because we unfortunately have no non-greedy
+ − 191 ;; ? operator. (urk! we actually do, but it wasn't documented.) --ben
+ − 192
+ − 193 ;; if you want to mess around, keep this test case in mind:
+ − 194
+ − 195 ;; this string
+ − 196
+ − 197 ;; " as'f 'FOO BAR' '' \"\" \"asdf \\ \\\" \\\\\\\" asdfasdf\\\\\" foo\" "
+ − 198
+ − 199 ;; should tokenize into this:
+ − 200
+ − 201 ;; (" " "as'f" " " "'FOO BAR' " "'' " "\"\"" " " "\"asdf \\ \\\" \\\\\\\" asdfasdf\\\\\"" " " "foo" "\" ")
+ − 202
442
+ − 203
+ − 204 (defvar debug-mswindows-process-command-lines nil
+ − 205 "If non-nil, output debug information about the command lines constructed.
+ − 206 This can be useful if you are getting process errors where the arguments
+ − 207 to the process appear to be getting passed incorrectly.")
+ − 208
+ − 209 ;; properly quotify one arg for the vc runtime argv constructor.
+ − 210 (defun mswindows-quote-one-vc-runtime-arg (arg &optional quote-shell)
+ − 211 ;; we mess with any arg with whitespace, quotes, or globbing chars in it.
+ − 212 ;; we also include shell metachars if asked.
+ − 213 ;; note that \ is NOT included! it's perfectly OK to include an
+ − 214 ;; arg like c:\ or c:\foo.
611
+ − 215 (cond ((equal arg "") "\"\"")
+ − 216 ((string-match
+ − 217 (if quote-shell "[ \t\n\r\f*?\"<>|&^%]" "[ \t\n\r\f*?\"]")
+ − 218 arg)
+ − 219 ;; handle nested quotes, possibly preceded by backslashes
+ − 220 (setq arg (replace-in-string arg "\\([\\]*\\)\"" "\\1\\1\\\\\""))
+ − 221 ;; handle trailing backslashes
+ − 222 (setq arg (replace-in-string arg "\\([\\]+\\)$" "\\1\\1"))
+ − 223 (concat "\"" arg "\""))
+ − 224 (t arg)))
442
+ − 225
+ − 226 (defun mswindows-quote-one-simple-arg (arg &optional quote-shell)
+ − 227 ;; just put double quotes around args with spaces (and maybe shell
+ − 228 ;; metachars).
611
+ − 229 (cond ((equal arg "") "\"\"")
+ − 230 ((string-match
+ − 231 (if quote-shell "[ \t\n\r\f*?\"<>|&^%]" "[ \t\n\r\f*?]")
+ − 232 arg)
+ − 233 (concat "\"" arg "\""))
+ − 234 (t arg)))
442
+ − 235
+ − 236 (defun mswindows-quote-one-command-arg (arg)
+ − 237 ;; quote an arg to get it past COMMAND.COM/CMD.EXE: need to quote shell
+ − 238 ;; metachars with ^.
611
+ − 239 (cond ((equal arg "") "\"\"")
+ − 240 (t (replace-in-string "[<>|&^%]" "^\\1" arg))))
442
+ − 241
+ − 242 (defun mswindows-construct-verbatim-command-line (program args)
+ − 243 (mapconcat #'identity args " "))
+ − 244
+ − 245 ;; for use with either standard VC++ compiled programs or Cygwin programs,
+ − 246 ;; which emulate the same behavior.
+ − 247 (defun mswindows-construct-vc-runtime-command-line (program args)
+ − 248 (mapconcat #'mswindows-quote-one-vc-runtime-arg args " "))
+ − 249
+ − 250 ;; this regexp actually separates the arg into individual args, like a
+ − 251 ;; shell (such as sh) does, but using vc-runtime rules. it's easy to
+ − 252 ;; derive the tokenizing regexp from it, and that's exactly what i did.
+ − 253 ;; but oh was it hard to get this first regexp right. --ben
+ − 254 ;(defvar mswindows-match-one-cmd-exe-arg-regexp
+ − 255 ; (concat
+ − 256 ; "^\\("
+ − 257 ; "'\\([\\]*\\)\\2'" "\\|"
+ − 258 ; "'.*?[^\\]\\(\\([\\]*\\)\\4'\\)" "\\|"
+ − 259 ; "\\("
+ − 260 ; "[^ \t\n\r\f\v\"]" "\\|"
+ − 261 ; "\"\\([\\]*\\)\\6\"" "\\|"
+ − 262 ; "\".*?[^\\]\\(\\([\\]*\\)\\8\"\\|$\\)"
+ − 263 ; "\\)+"
+ − 264 ; "\\)"
+ − 265 ; "\\([ \t\n\r\f\v]+\\|$\\)"))
+ − 266
+ − 267 (defvar mswindows-match-one-cmd-exe-token-regexp
+ − 268 (concat
+ − 269 "^\\("
+ − 270 "[ \t\n\r\f\v]+" "\\|"
+ − 271 "'\\([\\]*\\)\\2'" "\\([ \t\n\r\f\v]+\\|$\\)" "\\|"
+ − 272 "'.*?[^\\]\\(\\([\\]*\\)\\5'\\)" "\\([ \t\n\r\f\v]+\\|$\\)" "\\|"
+ − 273 "[^ \t\n\r\f\v\"]+" "\\|"
+ − 274 "\"\\([\\]*\\)\\7\"" "\\|"
+ − 275 "\".*?[^\\]\\(\\([\\]*\\)\\9\"\\|$\\)"
+ − 276 "\\)"))
+ − 277
+ − 278 (defun mswindows-construct-command-command-line (program args)
+ − 279 ;; for use with COMMAND.COM and CMD.EXE:
+ − 280 ;; for each arg, tokenize it into quoted and non-quoted sections;
+ − 281 ;; then quote all the shell meta-chars with ^; then put everything
+ − 282 ;; back together. the truly hard part is the tokenizing -- typically
+ − 283 ;; we get a single argument (the command to execute) and we have to
+ − 284 ;; worry about quotes that are backslash-quoted and such.
+ − 285 (mapconcat
+ − 286 #'(lambda (arg)
+ − 287 (mapconcat
+ − 288 #'(lambda (part)
+ − 289 (if (string-match "^'" part)
+ − 290 (replace-in-string part "\\([<>|^&%]\\)" "^\\1")
+ − 291 part))
+ − 292 (let (parts)
+ − 293 (while (and (> (length arg) 0)
+ − 294 (string-match
+ − 295 mswindows-match-one-cmd-exe-token-regexp
+ − 296 arg))
+ − 297 (push (match-string 0 arg) parts)
+ − 298 (setq arg (substring arg (match-end 0))))
+ − 299 (if (> (length arg) 0)
+ − 300 (push arg parts))
+ − 301 (nreverse parts))
+ − 302 ""))
+ − 303 args " "))
+ − 304
+ − 305 (defvar mswindows-construct-process-command-line-alist
611
+ − 306 '(
+ − 307 ;; at one point (pre-1.0), this was required for Cygwin bash.
+ − 308 ;; evidently, Cygwin changed its arg handling to work just like
+ − 309 ;; any standard VC program, so we no longer need it.
+ − 310 ;;("[\\/].?.?sh\\." . mswindows-construct-verbatim-command-line)
442
+ − 311 ("[\\/]command\\.com$" . mswindows-construct-command-command-line)
+ − 312 ("[\\/]cmd\\.exe$" . mswindows-construct-command-command-line)
+ − 313 ("" . mswindows-construct-vc-runtime-command-line))
+ − 314 "An alist for determining proper argument quoting given executable
+ − 315 file name. Car of each cons should be a string, a regexp against
+ − 316 which the file name is matched. Matching is case-insensitive but does
+ − 317 include the directory, so you should begin your regexp with [\\\\/] if
+ − 318 you don't want the directory to matter. Alternatively, the car can be
+ − 319 a function of one arg, which is called with the executable's name and
+ − 320 should return t if this entry should be processed. Cdr is a function
+ − 321 symbol, which is called with two args, the executable name and a list
+ − 322 of the args passed to it. It should return a string, which includes
+ − 323 the executable's args (but not the executable name itself) properly
+ − 324 quoted and pasted together. The list is matched in order, and the
+ − 325 first matching entry specifies how the processing will happen.")
+ − 326
+ − 327 (defun mswindows-construct-process-command-line (args)
+ − 328 ;;Properly quote process ARGS for executing (car ARGS).
+ − 329 ;;Called from the C code.
+ − 330 (let ((fname (car args))
+ − 331 (alist mswindows-construct-process-command-line-alist)
+ − 332 (case-fold-search t)
+ − 333 (return-me nil)
+ − 334 (assoc nil))
+ − 335 (while (and alist
+ − 336 (null return-me))
+ − 337 (setq assoc (pop alist))
+ − 338 (if (if (stringp (car assoc))
+ − 339 (string-match (car assoc) fname)
+ − 340 (funcall (car assoc) fname))
+ − 341 (setq return-me (cdr assoc))))
+ − 342 (let* ((called-fun (or return-me
+ − 343 #'mswindows-construct-vc-runtime-command-line))
+ − 344 (retval
+ − 345 (let ((str (funcall called-fun fname (cdr args)))
+ − 346 (quoted-fname (mswindows-quote-one-simple-arg fname)))
+ − 347 (if (and str (> (length str) 0))
+ − 348 (concat quoted-fname " " str)
+ − 349 quoted-fname))))
+ − 350 (when debug-mswindows-process-command-lines
+ − 351 (debug-print "mswindows-construct-process-command-line called:\n")
+ − 352 (debug-print "received args: \n%s"
+ − 353 (let ((n -1))
+ − 354 (mapconcat #'(lambda (arg)
+ − 355 (incf n)
+ − 356 (format " %d %s\n" n arg))
+ − 357 args
+ − 358 "")))
+ − 359 (debug-print "called fun %s\n" called-fun)
+ − 360 (debug-print "resulting command line: %s\n" retval))
+ − 361 retval)))
+ − 362
+ − 363 ;;; win32-native.el ends here