100
|
1 /* Process support for Windows NT port of XEMACS.
|
|
2 Copyright (C) 1992, 1995 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA.
|
|
20
|
|
21 Drew Bliss Oct 14, 1993
|
|
22 Adapted from alarm.c by Tim Fleehart */
|
|
23
|
|
24 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
|
209
|
25 /* Synced with FSF Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
|
100
|
26
|
|
27 #include <stdio.h>
|
|
28 #include <stdlib.h>
|
|
29 #include <errno.h>
|
|
30 #include <io.h>
|
|
31 #include <fcntl.h>
|
|
32 #include <signal.h>
|
|
33
|
|
34 /* must include CRT headers *before* config.h */
|
274
|
35 /* ### I don't believe it - martin */
|
|
36 #include <config.h>
|
100
|
37 #undef signal
|
|
38 #undef wait
|
|
39 #undef spawnve
|
|
40 #undef select
|
|
41 #undef kill
|
|
42
|
|
43 #include <windows.h>
|
239
|
44 #include <sys/socket.h>
|
100
|
45
|
|
46 #include "lisp.h"
|
239
|
47 #include "sysproc.h"
|
100
|
48 #include "nt.h"
|
209
|
49 #include "ntheap.h" /* From 19.34.6 */
|
100
|
50 #include "systime.h"
|
223
|
51 #include "syssignal.h"
|
100
|
52 #include "syswait.h"
|
|
53 #include "process.h"
|
209
|
54 /*#include "w32term.h"*/ /* From 19.34.6: sync in ? --marcpa */
|
100
|
55
|
288
|
56 /* #### I'm not going to play with shit. */
|
|
57 #pragma warning (disable:4013 4024 4090)
|
|
58
|
100
|
59 /* Control whether spawnve quotes arguments as necessary to ensure
|
|
60 correct parsing by child process. Because not all uses of spawnve
|
|
61 are careful about constructing argv arrays, we make this behaviour
|
|
62 conditional (off by default). */
|
|
63 Lisp_Object Vwin32_quote_process_args;
|
|
64
|
209
|
65 /* Control whether create_child causes the process' window to be
|
|
66 hidden. The default is nil. */
|
|
67 Lisp_Object Vwin32_start_process_show_window;
|
|
68
|
|
69 /* Control whether create_child causes the process to inherit Emacs'
|
|
70 console window, or be given a new one of its own. The default is
|
|
71 nil, to allow multiple DOS programs to run on Win95. Having separate
|
|
72 consoles also allows Emacs to cleanly terminate process groups. */
|
|
73 Lisp_Object Vwin32_start_process_share_console;
|
|
74
|
100
|
75 /* Time to sleep before reading from a subprocess output pipe - this
|
|
76 avoids the inefficiency of frequently reading small amounts of data.
|
|
77 This is primarily necessary for handling DOS processes on Windows 95,
|
|
78 but is useful for Win32 processes on both Win95 and NT as well. */
|
|
79 Lisp_Object Vwin32_pipe_read_delay;
|
|
80
|
209
|
81 /* Control whether stat() attempts to generate fake but hopefully
|
|
82 "accurate" inode values, by hashing the absolute truenames of files.
|
|
83 This should detect aliasing between long and short names, but still
|
|
84 allows the possibility of hash collisions. */
|
|
85 Lisp_Object Vwin32_generate_fake_inodes;
|
|
86
|
|
87 Lisp_Object Qhigh, Qlow;
|
100
|
88
|
239
|
89 #ifndef DEBUG_XEMACS
|
|
90 __inline
|
100
|
91 #endif
|
|
92 void _DebPrint (const char *fmt, ...)
|
|
93 {
|
239
|
94 #ifdef DEBUG_XEMACS
|
100
|
95 char buf[1024];
|
|
96 va_list args;
|
|
97
|
|
98 va_start (args, fmt);
|
|
99 vsprintf (buf, fmt, args);
|
|
100 va_end (args);
|
|
101 OutputDebugString (buf);
|
239
|
102 #endif
|
100
|
103 }
|
|
104
|
223
|
105 /* sys_signal moved to nt.c. It's now called msw_signal... */
|
100
|
106
|
|
107 /* Defined in <process.h> which conflicts with the local copy */
|
|
108 #define _P_NOWAIT 1
|
|
109
|
|
110 /* Child process management list. */
|
|
111 int child_proc_count = 0;
|
|
112 child_process child_procs[ MAX_CHILDREN ];
|
|
113 child_process *dead_child = NULL;
|
|
114
|
|
115 DWORD WINAPI reader_thread (void *arg);
|
|
116
|
|
117 /* Find an unused process slot. */
|
|
118 child_process *
|
|
119 new_child (void)
|
|
120 {
|
|
121 child_process *cp;
|
|
122 DWORD id;
|
|
123
|
|
124 for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
|
|
125 if (!CHILD_ACTIVE (cp))
|
272
|
126 goto Initialize;
|
100
|
127 if (child_proc_count == MAX_CHILDREN)
|
|
128 return NULL;
|
|
129 cp = &child_procs[child_proc_count++];
|
|
130
|
272
|
131 Initialize:
|
|
132 xzero (*cp);
|
100
|
133 cp->fd = -1;
|
|
134 cp->pid = -1;
|
|
135 cp->procinfo.hProcess = NULL;
|
|
136 cp->status = STATUS_READ_ERROR;
|
|
137
|
|
138 /* use manual reset event so that select() will function properly */
|
|
139 cp->char_avail = CreateEvent (NULL, TRUE, FALSE, NULL);
|
|
140 if (cp->char_avail)
|
|
141 {
|
|
142 cp->char_consumed = CreateEvent (NULL, FALSE, FALSE, NULL);
|
|
143 if (cp->char_consumed)
|
|
144 {
|
|
145 cp->thrd = CreateThread (NULL, 1024, reader_thread, cp, 0, &id);
|
|
146 if (cp->thrd)
|
|
147 return cp;
|
|
148 }
|
|
149 }
|
|
150 delete_child (cp);
|
|
151 return NULL;
|
|
152 }
|
|
153
|
|
154 void
|
|
155 delete_child (child_process *cp)
|
|
156 {
|
|
157 int i;
|
|
158
|
|
159 /* Should not be deleting a child that is still needed. */
|
|
160 for (i = 0; i < MAXDESC; i++)
|
|
161 if (fd_info[i].cp == cp)
|
|
162 abort ();
|
|
163
|
|
164 if (!CHILD_ACTIVE (cp))
|
|
165 return;
|
|
166
|
|
167 /* reap thread if necessary */
|
|
168 if (cp->thrd)
|
|
169 {
|
|
170 DWORD rc;
|
|
171
|
|
172 if (GetExitCodeThread (cp->thrd, &rc) && rc == STILL_ACTIVE)
|
|
173 {
|
|
174 /* let the thread exit cleanly if possible */
|
|
175 cp->status = STATUS_READ_ERROR;
|
|
176 SetEvent (cp->char_consumed);
|
|
177 if (WaitForSingleObject (cp->thrd, 1000) != WAIT_OBJECT_0)
|
|
178 {
|
|
179 DebPrint (("delete_child.WaitForSingleObject (thread) failed "
|
|
180 "with %lu for fd %ld\n", GetLastError (), cp->fd));
|
|
181 TerminateThread (cp->thrd, 0);
|
|
182 }
|
|
183 }
|
|
184 CloseHandle (cp->thrd);
|
|
185 cp->thrd = NULL;
|
|
186 }
|
|
187 if (cp->char_avail)
|
|
188 {
|
|
189 CloseHandle (cp->char_avail);
|
|
190 cp->char_avail = NULL;
|
|
191 }
|
|
192 if (cp->char_consumed)
|
|
193 {
|
|
194 CloseHandle (cp->char_consumed);
|
|
195 cp->char_consumed = NULL;
|
|
196 }
|
|
197
|
|
198 /* update child_proc_count (highest numbered slot in use plus one) */
|
|
199 if (cp == child_procs + child_proc_count - 1)
|
|
200 {
|
|
201 for (i = child_proc_count-1; i >= 0; i--)
|
|
202 if (CHILD_ACTIVE (&child_procs[i]))
|
|
203 {
|
|
204 child_proc_count = i + 1;
|
|
205 break;
|
|
206 }
|
|
207 }
|
|
208 if (i < 0)
|
|
209 child_proc_count = 0;
|
|
210 }
|
|
211
|
|
212 /* Find a child by pid. */
|
|
213 static child_process *
|
|
214 find_child_pid (DWORD pid)
|
|
215 {
|
|
216 child_process *cp;
|
|
217
|
|
218 for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
|
|
219 if (CHILD_ACTIVE (cp) && pid == cp->pid)
|
|
220 return cp;
|
|
221 return NULL;
|
|
222 }
|
|
223
|
|
224
|
|
225 /* Thread proc for child process and socket reader threads. Each thread
|
|
226 is normally blocked until woken by select() to check for input by
|
|
227 reading one char. When the read completes, char_avail is signalled
|
|
228 to wake up the select emulator and the thread blocks itself again. */
|
|
229 DWORD WINAPI
|
|
230 reader_thread (void *arg)
|
|
231 {
|
|
232 child_process *cp;
|
|
233
|
|
234 /* Our identity */
|
|
235 cp = (child_process *)arg;
|
|
236
|
|
237 /* We have to wait for the go-ahead before we can start */
|
|
238 if (cp == NULL ||
|
|
239 WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
|
|
240 return 1;
|
|
241
|
|
242 for (;;)
|
|
243 {
|
|
244 int rc;
|
|
245
|
|
246 rc = _sys_read_ahead (cp->fd);
|
|
247
|
|
248 /* The name char_avail is a misnomer - it really just means the
|
|
249 read-ahead has completed, whether successfully or not. */
|
|
250 if (!SetEvent (cp->char_avail))
|
|
251 {
|
|
252 DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n",
|
|
253 GetLastError (), cp->fd));
|
|
254 return 1;
|
|
255 }
|
|
256
|
|
257 if (rc == STATUS_READ_ERROR)
|
|
258 return 1;
|
|
259
|
|
260 /* If the read died, the child has died so let the thread die */
|
|
261 if (rc == STATUS_READ_FAILED)
|
|
262 break;
|
|
263
|
|
264 /* Wait until our input is acknowledged before reading again */
|
|
265 if (WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
|
|
266 {
|
|
267 DebPrint (("reader_thread.WaitForSingleObject failed with "
|
|
268 "%lu for fd %ld\n", GetLastError (), cp->fd));
|
|
269 break;
|
|
270 }
|
|
271 }
|
|
272 return 0;
|
|
273 }
|
|
274
|
209
|
275 /* To avoid Emacs changing directory, we just record here the directory
|
|
276 the new process should start in. This is set just before calling
|
|
277 sys_spawnve, and is not generally valid at any other time. */
|
288
|
278 static const char * process_dir;
|
209
|
279
|
100
|
280 static BOOL
|
|
281 create_child (char *exe, char *cmdline, char *env,
|
|
282 int * pPid, child_process *cp)
|
|
283 {
|
|
284 STARTUPINFO start;
|
|
285 SECURITY_ATTRIBUTES sec_attrs;
|
|
286 SECURITY_DESCRIPTOR sec_desc;
|
209
|
287 char dir[ MAXPATHLEN ];
|
100
|
288
|
|
289 if (cp == NULL) abort ();
|
|
290
|
272
|
291 xzero (start);
|
100
|
292 start.cb = sizeof (start);
|
|
293
|
|
294 #ifdef HAVE_NTGUI
|
209
|
295 if (NILP (Vwin32_start_process_show_window))
|
100
|
296 start.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
209
|
297 else
|
|
298 start.dwFlags = STARTF_USESTDHANDLES;
|
100
|
299 start.wShowWindow = SW_HIDE;
|
|
300
|
|
301 start.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
|
|
302 start.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
303 start.hStdError = GetStdHandle (STD_ERROR_HANDLE);
|
|
304 #endif /* HAVE_NTGUI */
|
|
305
|
|
306 /* Explicitly specify no security */
|
|
307 if (!InitializeSecurityDescriptor (&sec_desc, SECURITY_DESCRIPTOR_REVISION))
|
|
308 goto EH_Fail;
|
|
309 if (!SetSecurityDescriptorDacl (&sec_desc, TRUE, NULL, FALSE))
|
|
310 goto EH_Fail;
|
|
311 sec_attrs.nLength = sizeof (sec_attrs);
|
|
312 sec_attrs.lpSecurityDescriptor = &sec_desc;
|
|
313 sec_attrs.bInheritHandle = FALSE;
|
|
314
|
209
|
315 strcpy (dir, process_dir);
|
|
316 unixtodos_filename (dir);
|
|
317
|
100
|
318 if (!CreateProcess (exe, cmdline, &sec_attrs, NULL, TRUE,
|
209
|
319 (!NILP (Vwin32_start_process_share_console)
|
|
320 ? CREATE_NEW_PROCESS_GROUP
|
|
321 : CREATE_NEW_CONSOLE),
|
|
322 env, dir,
|
100
|
323 &start, &cp->procinfo))
|
|
324 goto EH_Fail;
|
|
325
|
|
326 cp->pid = (int) cp->procinfo.dwProcessId;
|
|
327
|
|
328 /* Hack for Windows 95, which assigns large (ie negative) pids */
|
|
329 if (cp->pid < 0)
|
|
330 cp->pid = -cp->pid;
|
|
331
|
|
332 /* pid must fit in a Lisp_Int */
|
282
|
333 #ifdef USE_UNION_TYPE
|
|
334 cp->pid = (cp->pid & ((1U << VALBITS) - 1));
|
|
335 #else
|
100
|
336 cp->pid = (cp->pid & VALMASK);
|
282
|
337 #endif
|
100
|
338
|
|
339 *pPid = cp->pid;
|
|
340
|
|
341 return TRUE;
|
|
342
|
|
343 EH_Fail:
|
|
344 DebPrint (("create_child.CreateProcess failed: %ld\n", GetLastError()););
|
|
345 return FALSE;
|
|
346 }
|
|
347
|
|
348 /* create_child doesn't know what emacs' file handle will be for waiting
|
|
349 on output from the child, so we need to make this additional call
|
|
350 to register the handle with the process
|
|
351 This way the select emulator knows how to match file handles with
|
|
352 entries in child_procs. */
|
|
353 void
|
|
354 register_child (int pid, int fd)
|
|
355 {
|
|
356 child_process *cp;
|
|
357
|
|
358 cp = find_child_pid (pid);
|
|
359 if (cp == NULL)
|
|
360 {
|
|
361 DebPrint (("register_child unable to find pid %lu\n", pid));
|
|
362 return;
|
|
363 }
|
|
364
|
|
365 #ifdef FULL_DEBUG
|
|
366 DebPrint (("register_child registered fd %d with pid %lu\n", fd, pid));
|
|
367 #endif
|
|
368
|
|
369 cp->fd = fd;
|
|
370
|
|
371 /* thread is initially blocked until select is called; set status so
|
|
372 that select will release thread */
|
|
373 cp->status = STATUS_READ_ACKNOWLEDGED;
|
|
374
|
|
375 /* attach child_process to fd_info */
|
|
376 if (fd_info[fd].cp != NULL)
|
|
377 {
|
|
378 DebPrint (("register_child: fd_info[%d] apparently in use!\n", fd));
|
|
379 abort ();
|
|
380 }
|
|
381
|
|
382 fd_info[fd].cp = cp;
|
|
383 }
|
|
384
|
|
385 /* When a process dies its pipe will break so the reader thread will
|
|
386 signal failure to the select emulator.
|
|
387 The select emulator then calls this routine to clean up.
|
|
388 Since the thread signaled failure we can assume it is exiting. */
|
|
389 static void
|
|
390 reap_subprocess (child_process *cp)
|
|
391 {
|
|
392 if (cp->procinfo.hProcess)
|
|
393 {
|
|
394 /* Reap the process */
|
|
395 if (WaitForSingleObject (cp->procinfo.hProcess, INFINITE) != WAIT_OBJECT_0)
|
|
396 DebPrint (("reap_subprocess.WaitForSingleObject (process) failed "
|
|
397 "with %lu for fd %ld\n", GetLastError (), cp->fd));
|
|
398 CloseHandle (cp->procinfo.hProcess);
|
|
399 cp->procinfo.hProcess = NULL;
|
|
400 CloseHandle (cp->procinfo.hThread);
|
|
401 cp->procinfo.hThread = NULL;
|
|
402 }
|
|
403
|
|
404 /* For asynchronous children, the child_proc resources will be freed
|
|
405 when the last pipe read descriptor is closed; for synchronous
|
|
406 children, we must explicitly free the resources now because
|
|
407 register_child has not been called. */
|
|
408 if (cp->fd == -1)
|
|
409 delete_child (cp);
|
|
410 }
|
|
411
|
|
412 /* Wait for any of our existing child processes to die
|
|
413 When it does, close its handle
|
|
414 Return the pid and fill in the status if non-NULL. */
|
|
415
|
|
416 int
|
|
417 sys_wait (int *status)
|
|
418 {
|
|
419 DWORD active, retval;
|
|
420 int nh;
|
|
421 int pid;
|
|
422 child_process *cp, *cps[MAX_CHILDREN];
|
|
423 HANDLE wait_hnd[MAX_CHILDREN];
|
|
424
|
|
425 nh = 0;
|
|
426 if (dead_child != NULL)
|
|
427 {
|
|
428 /* We want to wait for a specific child */
|
|
429 wait_hnd[nh] = dead_child->procinfo.hProcess;
|
|
430 cps[nh] = dead_child;
|
|
431 if (!wait_hnd[nh]) abort ();
|
|
432 nh++;
|
209
|
433 active = 0;
|
|
434 goto get_result;
|
100
|
435 }
|
|
436 else
|
|
437 {
|
|
438 for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
|
|
439 /* some child_procs might be sockets; ignore them */
|
|
440 if (CHILD_ACTIVE (cp) && cp->procinfo.hProcess)
|
|
441 {
|
|
442 wait_hnd[nh] = cp->procinfo.hProcess;
|
|
443 cps[nh] = cp;
|
209
|
444 if (!wait_hnd[nh]) abort (); /* Sync with FSF Emacs 19.34.6 note: only in XEmacs */
|
100
|
445 nh++;
|
|
446 }
|
|
447 }
|
|
448
|
|
449 if (nh == 0)
|
|
450 {
|
|
451 /* Nothing to wait on, so fail */
|
|
452 errno = ECHILD;
|
|
453 return -1;
|
|
454 }
|
|
455
|
209
|
456 do
|
|
457 {
|
|
458 /* Check for quit about once a second. */
|
|
459 QUIT;
|
|
460 active = WaitForMultipleObjects (nh, wait_hnd, FALSE, 1000);
|
|
461 } while (active == WAIT_TIMEOUT);
|
|
462
|
100
|
463 if (active == WAIT_FAILED)
|
|
464 {
|
|
465 errno = EBADF;
|
|
466 return -1;
|
|
467 }
|
|
468 else if (active >= WAIT_OBJECT_0 &&
|
|
469 active < WAIT_OBJECT_0+MAXIMUM_WAIT_OBJECTS)
|
|
470 {
|
|
471 active -= WAIT_OBJECT_0;
|
|
472 }
|
|
473 else if (active >= WAIT_ABANDONED_0 &&
|
|
474 active < WAIT_ABANDONED_0+MAXIMUM_WAIT_OBJECTS)
|
|
475 {
|
|
476 active -= WAIT_ABANDONED_0;
|
|
477 }
|
209
|
478 else
|
|
479 abort ();
|
100
|
480
|
209
|
481 get_result:
|
100
|
482 if (!GetExitCodeProcess (wait_hnd[active], &retval))
|
|
483 {
|
|
484 DebPrint (("Wait.GetExitCodeProcess failed with %lu\n",
|
|
485 GetLastError ()));
|
|
486 retval = 1;
|
|
487 }
|
|
488 if (retval == STILL_ACTIVE)
|
|
489 {
|
|
490 /* Should never happen */
|
|
491 DebPrint (("Wait.WaitForMultipleObjects returned an active process\n"));
|
|
492 errno = EINVAL;
|
|
493 return -1;
|
|
494 }
|
|
495
|
|
496 /* Massage the exit code from the process to match the format expected
|
|
497 by the WIFSTOPPED et al macros in syswait.h. Only WIFSIGNALED and
|
|
498 WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT. */
|
|
499
|
|
500 if (retval == STATUS_CONTROL_C_EXIT)
|
|
501 retval = SIGINT;
|
|
502 else
|
|
503 retval <<= 8;
|
|
504
|
|
505 cp = cps[active];
|
|
506 pid = cp->pid;
|
|
507 #ifdef FULL_DEBUG
|
|
508 DebPrint (("Wait signaled with process pid %d\n", cp->pid));
|
|
509 #endif
|
|
510
|
|
511 if (status)
|
|
512 {
|
|
513 *status = retval;
|
|
514 }
|
|
515 else if (synch_process_alive)
|
|
516 {
|
|
517 synch_process_alive = 0;
|
|
518
|
|
519 /* Report the status of the synchronous process. */
|
|
520 if (WIFEXITED (retval))
|
|
521 synch_process_retcode = WRETCODE (retval);
|
|
522 else if (WIFSIGNALED (retval))
|
|
523 {
|
|
524 int code = WTERMSIG (retval);
|
|
525 char *signame = 0;
|
|
526
|
|
527 if (code < NSIG)
|
|
528 {
|
|
529 /* Suppress warning if the table has const char *. */
|
|
530 signame = (char *) sys_siglist[code];
|
|
531 }
|
|
532 if (signame == 0)
|
|
533 signame = "unknown";
|
|
534
|
|
535 synch_process_death = signame;
|
|
536 }
|
|
537
|
|
538 reap_subprocess (cp);
|
|
539 }
|
|
540
|
209
|
541 reap_subprocess (cp);
|
|
542
|
100
|
543 return pid;
|
|
544 }
|
|
545
|
209
|
546 void
|
|
547 win32_executable_type (char * filename, int * is_dos_app, int * is_cygnus_app)
|
100
|
548 {
|
209
|
549 file_data executable;
|
|
550 char * p;
|
100
|
551
|
209
|
552 /* Default values in case we can't tell for sure. */
|
|
553 *is_dos_app = FALSE;
|
|
554 *is_cygnus_app = FALSE;
|
|
555
|
|
556 if (!open_input_file (&executable, filename))
|
|
557 return;
|
|
558
|
|
559 p = strrchr (filename, '.');
|
100
|
560
|
|
561 /* We can only identify DOS .com programs from the extension. */
|
|
562 if (p && stricmp (p, ".com") == 0)
|
209
|
563 *is_dos_app = TRUE;
|
|
564 else if (p && (stricmp (p, ".bat") == 0 ||
|
|
565 stricmp (p, ".cmd") == 0))
|
|
566 {
|
|
567 /* A DOS shell script - it appears that CreateProcess is happy to
|
|
568 accept this (somewhat surprisingly); presumably it looks at
|
|
569 COMSPEC to determine what executable to actually invoke.
|
100
|
570 Therefore, we have to do the same here as well. */
|
209
|
571 /* Actually, I think it uses the program association for that
|
|
572 extension, which is defined in the registry. */
|
|
573 p = egetenv ("COMSPEC");
|
100
|
574 if (p)
|
209
|
575 win32_executable_type (p, is_dos_app, is_cygnus_app);
|
100
|
576 }
|
|
577 else
|
|
578 {
|
209
|
579 /* Look for DOS .exe signature - if found, we must also check that
|
|
580 it isn't really a 16- or 32-bit Windows exe, since both formats
|
|
581 start with a DOS program stub. Note that 16-bit Windows
|
|
582 executables use the OS/2 1.x format. */
|
|
583
|
|
584 IMAGE_DOS_HEADER * dos_header;
|
|
585 IMAGE_NT_HEADERS * nt_header;
|
|
586
|
|
587 dos_header = (PIMAGE_DOS_HEADER) executable.file_base;
|
|
588 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
|
|
589 goto unwind;
|
|
590
|
|
591 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
|
|
592
|
|
593 if ((char *) nt_header > (char *) dos_header + executable.size)
|
|
594 {
|
|
595 /* Some dos headers (pkunzip) have bogus e_lfanew fields. */
|
|
596 *is_dos_app = TRUE;
|
|
597 }
|
|
598 else if (nt_header->Signature != IMAGE_NT_SIGNATURE &&
|
|
599 LOWORD (nt_header->Signature) != IMAGE_OS2_SIGNATURE)
|
|
600 {
|
|
601 *is_dos_app = TRUE;
|
|
602 }
|
|
603 else if (nt_header->Signature == IMAGE_NT_SIGNATURE)
|
|
604 {
|
|
605 /* Look for cygwin.dll in DLL import list. */
|
|
606 IMAGE_DATA_DIRECTORY import_dir =
|
|
607 nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
|
|
608 IMAGE_IMPORT_DESCRIPTOR * imports;
|
|
609 IMAGE_SECTION_HEADER * section;
|
|
610
|
|
611 section = rva_to_section (import_dir.VirtualAddress, nt_header);
|
|
612 imports = RVA_TO_PTR (import_dir.VirtualAddress, section, executable);
|
|
613
|
|
614 for ( ; imports->Name; imports++)
|
100
|
615 {
|
209
|
616 char * dllname = RVA_TO_PTR (imports->Name, section, executable);
|
|
617
|
|
618 if (strcmp (dllname, "cygwin.dll") == 0)
|
|
619 {
|
|
620 *is_cygnus_app = TRUE;
|
|
621 break;
|
|
622 }
|
100
|
623 }
|
|
624 }
|
209
|
625 }
|
|
626
|
|
627 unwind:
|
|
628 close_file_data (&executable);
|
|
629 }
|
|
630
|
|
631 int
|
|
632 compare_env (const char **strp1, const char **strp2)
|
|
633 {
|
|
634 const char *str1 = *strp1, *str2 = *strp2;
|
|
635
|
|
636 while (*str1 && *str2 && *str1 != '=' && *str2 != '=')
|
|
637 {
|
|
638 if ((*str1) > (*str2))
|
|
639 return 1;
|
|
640 else if ((*str1) < (*str2))
|
|
641 return -1;
|
|
642 str1++, str2++;
|
100
|
643 }
|
|
644
|
209
|
645 if (*str1 == '=' && *str2 == '=')
|
|
646 return 0;
|
|
647 else if (*str1 == '=')
|
|
648 return -1;
|
|
649 else
|
|
650 return 1;
|
100
|
651 }
|
|
652
|
209
|
653 void
|
|
654 merge_and_sort_env (char **envp1, char **envp2, char **new_envp)
|
|
655 {
|
|
656 char **optr, **nptr;
|
|
657 int num;
|
|
658
|
|
659 nptr = new_envp;
|
|
660 optr = envp1;
|
|
661 while (*optr)
|
|
662 *nptr++ = *optr++;
|
|
663 num = optr - envp1;
|
|
664
|
|
665 optr = envp2;
|
|
666 while (*optr)
|
|
667 *nptr++ = *optr++;
|
|
668 num += optr - envp2;
|
|
669
|
|
670 qsort (new_envp, num, sizeof (char *), compare_env);
|
|
671
|
|
672 *nptr = NULL;
|
|
673 }
|
100
|
674
|
|
675 /* When a new child process is created we need to register it in our list,
|
|
676 so intercept spawn requests. */
|
|
677 int
|
239
|
678 sys_spawnve (int mode, CONST char *cmdname,
|
|
679 CONST char * CONST *argv, CONST char *CONST *envp)
|
100
|
680 {
|
|
681 Lisp_Object program, full;
|
|
682 char *cmdline, *env, *parg, **targ;
|
209
|
683 int arglen, numenv;
|
100
|
684 int pid;
|
|
685 child_process *cp;
|
209
|
686 int is_dos_app, is_cygnus_app;
|
|
687 int do_quoting = 0;
|
|
688 char escape_char;
|
|
689 /* We pass our process ID to our children by setting up an environment
|
|
690 variable in their environment. */
|
|
691 char ppid_env_var_buffer[64];
|
|
692 char *extra_env[] = {ppid_env_var_buffer, NULL};
|
124
|
693 struct gcpro gcpro1;
|
|
694
|
100
|
695 /* We don't care about the other modes */
|
|
696 if (mode != _P_NOWAIT)
|
|
697 {
|
|
698 errno = EINVAL;
|
|
699 return -1;
|
|
700 }
|
|
701
|
|
702 /* Handle executable names without an executable suffix. */
|
|
703 program = make_string (cmdname, strlen (cmdname));
|
124
|
704 GCPRO1 (program);
|
100
|
705 if (NILP (Ffile_executable_p (program)))
|
|
706 {
|
|
707 full = Qnil;
|
|
708 locate_file (Vexec_path, program, EXEC_SUFFIXES, &full, 1);
|
|
709 if (NILP (full))
|
|
710 {
|
124
|
711 UNGCPRO;
|
100
|
712 errno = EINVAL;
|
|
713 return -1;
|
|
714 }
|
209
|
715 cmdname = XSTRING_DATA (full);
|
239
|
716 /* #### KLUDGE */
|
|
717 *(char**)(argv[0]) = cmdname;
|
100
|
718 }
|
124
|
719 UNGCPRO;
|
|
720
|
209
|
721 /* make sure argv[0] and cmdname are both in DOS format */
|
100
|
722 strcpy (cmdname = alloca (strlen (cmdname) + 1), argv[0]);
|
|
723 unixtodos_filename (cmdname);
|
239
|
724 /* #### KLUDGE */
|
|
725 *(char**)(argv[0]) = cmdname;
|
100
|
726
|
209
|
727 /* Determine whether program is a 16-bit DOS executable, or a Win32
|
|
728 executable that is implicitly linked to the Cygnus dll (implying it
|
|
729 was compiled with the Cygnus GNU toolchain and hence relies on
|
|
730 cygwin.dll to parse the command line - we use this to decide how to
|
|
731 escape quote chars in command line args that must be quoted). */
|
|
732 win32_executable_type (cmdname, &is_dos_app, &is_cygnus_app);
|
|
733
|
|
734 /* On Windows 95, if cmdname is a DOS app, we invoke a helper
|
|
735 application to start it by specifying the helper app as cmdname,
|
|
736 while leaving the real app name as argv[0]. */
|
|
737 if (is_dos_app)
|
100
|
738 {
|
209
|
739 cmdname = alloca (MAXPATHLEN);
|
|
740 if (egetenv ("CMDPROXY"))
|
|
741 strcpy (cmdname, egetenv ("CMDPROXY"));
|
|
742 else
|
|
743 {
|
|
744 strcpy (cmdname, XSTRING_DATA (Vinvocation_directory));
|
|
745 strcat (cmdname, "cmdproxy.exe");
|
|
746 }
|
|
747 unixtodos_filename (cmdname);
|
100
|
748 }
|
|
749
|
|
750 /* we have to do some conjuring here to put argv and envp into the
|
|
751 form CreateProcess wants... argv needs to be a space separated/null
|
|
752 terminated list of parameters, and envp is a null
|
|
753 separated/double-null terminated list of parameters.
|
|
754
|
209
|
755 Additionally, zero-length args and args containing whitespace or
|
|
756 quote chars need to be wrapped in double quotes - for this to work,
|
|
757 embedded quotes need to be escaped as well. The aim is to ensure
|
|
758 the child process reconstructs the argv array we start with
|
|
759 exactly, so we treat quotes at the beginning and end of arguments
|
|
760 as embedded quotes.
|
|
761
|
|
762 The Win32 GNU-based library from Cygnus doubles quotes to escape
|
|
763 them, while MSVC uses backslash for escaping. (Actually the MSVC
|
|
764 startup code does attempt to recognise doubled quotes and accept
|
|
765 them, but gets it wrong and ends up requiring three quotes to get a
|
|
766 single embedded quote!) So by default we decide whether to use
|
|
767 quote or backslash as the escape character based on whether the
|
|
768 binary is apparently a Cygnus compiled app.
|
|
769
|
|
770 Note that using backslash to escape embedded quotes requires
|
|
771 additional special handling if an embedded quote is already
|
|
772 preceeded by backslash, or if an arg requiring quoting ends with
|
|
773 backslash. In such cases, the run of escape characters needs to be
|
|
774 doubled. For consistency, we apply this special handling as long
|
|
775 as the escape character is not quote.
|
100
|
776
|
209
|
777 Since we have no idea how large argv and envp are likely to be we
|
|
778 figure out list lengths on the fly and allocate them. */
|
|
779
|
|
780 if (!NILP (Vwin32_quote_process_args))
|
|
781 {
|
|
782 do_quoting = 1;
|
|
783 /* Override escape char by binding win32-quote-process-args to
|
|
784 desired character, or use t for auto-selection. */
|
|
785 if (INTP (Vwin32_quote_process_args))
|
|
786 escape_char = XINT (Vwin32_quote_process_args);
|
|
787 else
|
|
788 escape_char = is_cygnus_app ? '"' : '\\';
|
|
789 }
|
100
|
790
|
|
791 /* do argv... */
|
|
792 arglen = 0;
|
|
793 targ = argv;
|
|
794 while (*targ)
|
|
795 {
|
|
796 char * p = *targ;
|
209
|
797 int need_quotes = 0;
|
|
798 int escape_char_run = 0;
|
100
|
799
|
|
800 if (*p == 0)
|
209
|
801 need_quotes = 1;
|
|
802 for ( ; *p; p++)
|
|
803 {
|
|
804 if (*p == '"')
|
100
|
805 {
|
209
|
806 /* allow for embedded quotes to be escaped */
|
100
|
807 arglen++;
|
209
|
808 need_quotes = 1;
|
|
809 /* handle the case where the embedded quote is already escaped */
|
|
810 if (escape_char_run > 0)
|
|
811 {
|
|
812 /* To preserve the arg exactly, we need to double the
|
|
813 preceding escape characters (plus adding one to
|
|
814 escape the quote character itself). */
|
|
815 arglen += escape_char_run;
|
100
|
816 }
|
209
|
817 }
|
100
|
818 else if (*p == ' ' || *p == '\t')
|
209
|
819 {
|
|
820 need_quotes = 1;
|
|
821 }
|
|
822
|
|
823 if (*p == escape_char && escape_char != '"')
|
|
824 escape_char_run++;
|
|
825 else
|
|
826 escape_char_run = 0;
|
|
827 }
|
|
828 if (need_quotes)
|
|
829 {
|
100
|
830 arglen += 2;
|
209
|
831 /* handle the case where the arg ends with an escape char - we
|
|
832 must not let the enclosing quote be escaped. */
|
|
833 if (escape_char_run > 0)
|
|
834 arglen += escape_char_run;
|
|
835 }
|
100
|
836 arglen += strlen (*targ++) + 1;
|
|
837 }
|
|
838 cmdline = alloca (arglen);
|
|
839 targ = argv;
|
|
840 parg = cmdline;
|
|
841 while (*targ)
|
|
842 {
|
|
843 char * p = *targ;
|
209
|
844 int need_quotes = 0;
|
100
|
845
|
|
846 if (*p == 0)
|
209
|
847 need_quotes = 1;
|
100
|
848
|
209
|
849 if (do_quoting)
|
100
|
850 {
|
|
851 for ( ; *p; p++)
|
|
852 if (*p == ' ' || *p == '\t' || *p == '"')
|
209
|
853 need_quotes = 1;
|
100
|
854 }
|
209
|
855 if (need_quotes)
|
100
|
856 {
|
209
|
857 int escape_char_run = 0;
|
100
|
858 char * first;
|
|
859 char * last;
|
|
860
|
|
861 p = *targ;
|
|
862 first = p;
|
|
863 last = p + strlen (p) - 1;
|
|
864 *parg++ = '"';
|
209
|
865 #if 0
|
|
866 /* This version does not escape quotes if they occur at the
|
|
867 beginning or end of the arg - this could lead to incorrect
|
|
868 behaviour when the arg itself represents a command line
|
|
869 containing quoted args. I believe this was originally done
|
|
870 as a hack to make some things work, before
|
|
871 `win32-quote-process-args' was added. */
|
100
|
872 while (*p)
|
|
873 {
|
|
874 if (*p == '"' && p > first && p < last)
|
209
|
875 *parg++ = escape_char; /* escape embedded quotes */
|
100
|
876 *parg++ = *p++;
|
|
877 }
|
209
|
878 #else
|
|
879 for ( ; *p; p++)
|
|
880 {
|
|
881 if (*p == '"')
|
|
882 {
|
|
883 /* double preceding escape chars if any */
|
|
884 while (escape_char_run > 0)
|
|
885 {
|
|
886 *parg++ = escape_char;
|
|
887 escape_char_run--;
|
|
888 }
|
|
889 /* escape all quote chars, even at beginning or end */
|
|
890 *parg++ = escape_char;
|
|
891 }
|
|
892 *parg++ = *p;
|
|
893
|
|
894 if (*p == escape_char && escape_char != '"')
|
|
895 escape_char_run++;
|
|
896 else
|
|
897 escape_char_run = 0;
|
|
898 }
|
|
899 /* double escape chars before enclosing quote */
|
|
900 while (escape_char_run > 0)
|
|
901 {
|
|
902 *parg++ = escape_char;
|
|
903 escape_char_run--;
|
|
904 }
|
|
905 #endif
|
100
|
906 *parg++ = '"';
|
|
907 }
|
|
908 else
|
|
909 {
|
|
910 strcpy (parg, *targ);
|
|
911 parg += strlen (*targ);
|
|
912 }
|
|
913 *parg++ = ' ';
|
|
914 targ++;
|
|
915 }
|
|
916 *--parg = '\0';
|
|
917
|
|
918 /* and envp... */
|
|
919 arglen = 1;
|
|
920 targ = envp;
|
209
|
921 numenv = 1; /* for end null */
|
100
|
922 while (*targ)
|
|
923 {
|
|
924 arglen += strlen (*targ++) + 1;
|
209
|
925 numenv++;
|
100
|
926 }
|
209
|
927 /* extra env vars... */
|
100
|
928 sprintf (ppid_env_var_buffer, "__PARENT_PROCESS_ID=%d",
|
|
929 GetCurrentProcessId ());
|
|
930 arglen += strlen (ppid_env_var_buffer) + 1;
|
209
|
931 numenv++;
|
100
|
932
|
209
|
933 /* merge env passed in and extra env into one, and sort it. */
|
|
934 targ = (char **) alloca (numenv * sizeof (char *));
|
|
935 merge_and_sort_env (envp, extra_env, targ);
|
|
936
|
|
937 /* concatenate env entries. */
|
100
|
938 env = alloca (arglen);
|
|
939 parg = env;
|
|
940 while (*targ)
|
|
941 {
|
|
942 strcpy (parg, *targ);
|
|
943 parg += strlen (*targ++);
|
|
944 *parg++ = '\0';
|
|
945 }
|
|
946 *parg++ = '\0';
|
|
947 *parg = '\0';
|
|
948
|
|
949 cp = new_child ();
|
|
950 if (cp == NULL)
|
|
951 {
|
|
952 errno = EAGAIN;
|
|
953 return -1;
|
|
954 }
|
|
955
|
|
956 /* Now create the process. */
|
|
957 if (!create_child (cmdname, cmdline, env, &pid, cp))
|
|
958 {
|
|
959 delete_child (cp);
|
|
960 errno = ENOEXEC;
|
|
961 return -1;
|
|
962 }
|
|
963
|
|
964 return pid;
|
|
965 }
|
|
966
|
|
967 /* Substitute for certain kill () operations */
|
209
|
968
|
|
969 static BOOL CALLBACK
|
|
970 find_child_console (HWND hwnd, child_process * cp)
|
|
971 {
|
|
972 DWORD thread_id;
|
|
973 DWORD process_id;
|
|
974
|
|
975 thread_id = GetWindowThreadProcessId (hwnd, &process_id);
|
|
976 if (process_id == cp->procinfo.dwProcessId)
|
|
977 {
|
|
978 char window_class[32];
|
|
979
|
|
980 GetClassName (hwnd, window_class, sizeof (window_class));
|
|
981 if (strcmp (window_class,
|
|
982 (os_subtype == OS_WIN95)
|
|
983 ? "tty"
|
|
984 : "ConsoleWindowClass") == 0)
|
|
985 {
|
|
986 cp->hwnd = hwnd;
|
|
987 return FALSE;
|
|
988 }
|
|
989 }
|
|
990 /* keep looking */
|
|
991 return TRUE;
|
|
992 }
|
|
993
|
100
|
994 int
|
|
995 sys_kill (int pid, int sig)
|
|
996 {
|
|
997 child_process *cp;
|
|
998 HANDLE proc_hand;
|
|
999 int need_to_free = 0;
|
|
1000 int rc = 0;
|
|
1001
|
|
1002 /* Only handle signals that will result in the process dying */
|
|
1003 if (sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP)
|
|
1004 {
|
|
1005 errno = EINVAL;
|
|
1006 return -1;
|
|
1007 }
|
|
1008
|
|
1009 cp = find_child_pid (pid);
|
|
1010 if (cp == NULL)
|
|
1011 {
|
|
1012 proc_hand = OpenProcess (PROCESS_TERMINATE, 0, pid);
|
|
1013 if (proc_hand == NULL)
|
|
1014 {
|
|
1015 errno = EPERM;
|
|
1016 return -1;
|
|
1017 }
|
|
1018 need_to_free = 1;
|
|
1019 }
|
|
1020 else
|
|
1021 {
|
|
1022 proc_hand = cp->procinfo.hProcess;
|
|
1023 pid = cp->procinfo.dwProcessId;
|
209
|
1024
|
|
1025 /* Try to locate console window for process. */
|
|
1026 EnumWindows (find_child_console, (LPARAM) cp);
|
100
|
1027 }
|
|
1028
|
|
1029 if (sig == SIGINT)
|
|
1030 {
|
209
|
1031 if (NILP (Vwin32_start_process_share_console) && cp && cp->hwnd)
|
|
1032 {
|
|
1033 BYTE control_scan_code = (BYTE) MapVirtualKey (VK_CONTROL, 0);
|
|
1034 BYTE vk_break_code = VK_CANCEL;
|
|
1035 BYTE break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
|
|
1036 HWND foreground_window;
|
|
1037
|
|
1038 if (break_scan_code == 0)
|
|
1039 {
|
|
1040 /* Fake Ctrl-C if we can't manage Ctrl-Break. */
|
|
1041 vk_break_code = 'C';
|
|
1042 break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
|
|
1043 }
|
|
1044
|
|
1045 foreground_window = GetForegroundWindow ();
|
|
1046 if (foreground_window && SetForegroundWindow (cp->hwnd))
|
|
1047 {
|
|
1048 /* Generate keystrokes as if user had typed Ctrl-Break or Ctrl-C. */
|
|
1049 keybd_event (VK_CONTROL, control_scan_code, 0, 0);
|
|
1050 keybd_event (vk_break_code, break_scan_code, 0, 0);
|
|
1051 keybd_event (vk_break_code, break_scan_code, KEYEVENTF_KEYUP, 0);
|
|
1052 keybd_event (VK_CONTROL, control_scan_code, KEYEVENTF_KEYUP, 0);
|
|
1053
|
|
1054 /* Sleep for a bit to give time for Emacs frame to respond
|
|
1055 to focus change events (if Emacs was active app). */
|
|
1056 Sleep (10);
|
|
1057
|
|
1058 SetForegroundWindow (foreground_window);
|
|
1059 }
|
|
1060 }
|
100
|
1061 /* Ctrl-Break is NT equivalent of SIGINT. */
|
209
|
1062 else if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, pid))
|
100
|
1063 {
|
|
1064 DebPrint (("sys_kill.GenerateConsoleCtrlEvent return %d "
|
|
1065 "for pid %lu\n", GetLastError (), pid));
|
|
1066 errno = EINVAL;
|
|
1067 rc = -1;
|
|
1068 }
|
|
1069 }
|
|
1070 else
|
|
1071 {
|
209
|
1072 if (NILP (Vwin32_start_process_share_console) && cp && cp->hwnd)
|
|
1073 {
|
|
1074 #if 1
|
|
1075 if (os_subtype == OS_WIN95)
|
|
1076 {
|
|
1077 /*
|
|
1078 Another possibility is to try terminating the VDM out-right by
|
|
1079 calling the Shell VxD (id 0x17) V86 interface, function #4
|
|
1080 "SHELL_Destroy_VM", ie.
|
|
1081
|
|
1082 mov edx,4
|
|
1083 mov ebx,vm_handle
|
|
1084 call shellapi
|
|
1085
|
|
1086 First need to determine the current VM handle, and then arrange for
|
|
1087 the shellapi call to be made from the system vm (by using
|
|
1088 Switch_VM_and_callback).
|
|
1089
|
|
1090 Could try to invoke DestroyVM through CallVxD.
|
|
1091
|
|
1092 */
|
|
1093 #if 0
|
|
1094 /* On Win95, posting WM_QUIT causes the 16-bit subsystem
|
|
1095 to hang when cmdproxy is used in conjunction with
|
|
1096 command.com for an interactive shell. Posting
|
|
1097 WM_CLOSE pops up a dialog that, when Yes is selected,
|
|
1098 does the same thing. TerminateProcess is also less
|
|
1099 than ideal in that subprocesses tend to stick around
|
|
1100 until the machine is shutdown, but at least it
|
|
1101 doesn't freeze the 16-bit subsystem. */
|
|
1102 PostMessage (cp->hwnd, WM_QUIT, 0xff, 0);
|
|
1103 #endif
|
|
1104 if (!TerminateProcess (proc_hand, 0xff))
|
|
1105 {
|
|
1106 DebPrint (("sys_kill.TerminateProcess returned %d "
|
|
1107 "for pid %lu\n", GetLastError (), pid));
|
|
1108 errno = EINVAL;
|
|
1109 rc = -1;
|
|
1110 }
|
|
1111 }
|
|
1112 else
|
|
1113 #endif
|
|
1114 PostMessage (cp->hwnd, WM_CLOSE, 0, 0);
|
|
1115 }
|
100
|
1116 /* Kill the process. On Win32 this doesn't kill child processes
|
|
1117 so it doesn't work very well for shells which is why it's not
|
209
|
1118 used in every case. */
|
|
1119 else if (!TerminateProcess (proc_hand, 0xff))
|
100
|
1120 {
|
|
1121 DebPrint (("sys_kill.TerminateProcess returned %d "
|
|
1122 "for pid %lu\n", GetLastError (), pid));
|
|
1123 errno = EINVAL;
|
|
1124 rc = -1;
|
|
1125 }
|
|
1126 }
|
|
1127
|
|
1128 if (need_to_free)
|
|
1129 CloseHandle (proc_hand);
|
|
1130
|
|
1131 return rc;
|
|
1132 }
|
|
1133
|
|
1134 #if 0
|
209
|
1135 /* Sync with FSF Emacs 19.34.6 note: ifdef'ed out in XEmacs */
|
100
|
1136 extern int report_file_error (CONST char *, Lisp_Object);
|
|
1137 #endif
|
|
1138 /* The following two routines are used to manipulate stdin, stdout, and
|
|
1139 stderr of our child processes.
|
|
1140
|
|
1141 Assuming that in, out, and err are *not* inheritable, we make them
|
|
1142 stdin, stdout, and stderr of the child as follows:
|
|
1143
|
|
1144 - Save the parent's current standard handles.
|
|
1145 - Set the std handles to inheritable duplicates of the ones being passed in.
|
|
1146 (Note that _get_osfhandle() is an io.h procedure that retrieves the
|
|
1147 NT file handle for a crt file descriptor.)
|
|
1148 - Spawn the child, which inherits in, out, and err as stdin,
|
|
1149 stdout, and stderr. (see Spawnve)
|
|
1150 - Close the std handles passed to the child.
|
|
1151 - Reset the parent's standard handles to the saved handles.
|
|
1152 (see reset_standard_handles)
|
|
1153 We assume that the caller closes in, out, and err after calling us. */
|
|
1154
|
|
1155 void
|
|
1156 prepare_standard_handles (int in, int out, int err, HANDLE handles[3])
|
|
1157 {
|
|
1158 HANDLE parent;
|
|
1159 HANDLE newstdin, newstdout, newstderr;
|
|
1160
|
|
1161 parent = GetCurrentProcess ();
|
|
1162
|
|
1163 handles[0] = GetStdHandle (STD_INPUT_HANDLE);
|
|
1164 handles[1] = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
1165 handles[2] = GetStdHandle (STD_ERROR_HANDLE);
|
|
1166
|
|
1167 /* make inheritable copies of the new handles */
|
|
1168 if (!DuplicateHandle (parent,
|
|
1169 (HANDLE) _get_osfhandle (in),
|
|
1170 parent,
|
|
1171 &newstdin,
|
|
1172 0,
|
|
1173 TRUE,
|
|
1174 DUPLICATE_SAME_ACCESS))
|
|
1175 report_file_error ("Duplicating input handle for child", Qnil);
|
|
1176
|
|
1177 if (!DuplicateHandle (parent,
|
|
1178 (HANDLE) _get_osfhandle (out),
|
|
1179 parent,
|
|
1180 &newstdout,
|
|
1181 0,
|
|
1182 TRUE,
|
|
1183 DUPLICATE_SAME_ACCESS))
|
|
1184 report_file_error ("Duplicating output handle for child", Qnil);
|
|
1185
|
|
1186 if (!DuplicateHandle (parent,
|
|
1187 (HANDLE) _get_osfhandle (err),
|
|
1188 parent,
|
|
1189 &newstderr,
|
|
1190 0,
|
|
1191 TRUE,
|
|
1192 DUPLICATE_SAME_ACCESS))
|
|
1193 report_file_error ("Duplicating error handle for child", Qnil);
|
|
1194
|
|
1195 /* and store them as our std handles */
|
|
1196 if (!SetStdHandle (STD_INPUT_HANDLE, newstdin))
|
|
1197 report_file_error ("Changing stdin handle", Qnil);
|
|
1198
|
|
1199 if (!SetStdHandle (STD_OUTPUT_HANDLE, newstdout))
|
|
1200 report_file_error ("Changing stdout handle", Qnil);
|
|
1201
|
|
1202 if (!SetStdHandle (STD_ERROR_HANDLE, newstderr))
|
|
1203 report_file_error ("Changing stderr handle", Qnil);
|
|
1204 }
|
|
1205
|
|
1206 void
|
|
1207 reset_standard_handles (int in, int out, int err, HANDLE handles[3])
|
|
1208 {
|
|
1209 /* close the duplicated handles passed to the child */
|
|
1210 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
|
|
1211 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
|
|
1212 CloseHandle (GetStdHandle (STD_ERROR_HANDLE));
|
|
1213
|
|
1214 /* now restore parent's saved std handles */
|
|
1215 SetStdHandle (STD_INPUT_HANDLE, handles[0]);
|
|
1216 SetStdHandle (STD_OUTPUT_HANDLE, handles[1]);
|
|
1217 SetStdHandle (STD_ERROR_HANDLE, handles[2]);
|
|
1218 }
|
|
1219
|
209
|
1220 void
|
288
|
1221 set_process_dir (const char * dir)
|
209
|
1222 {
|
|
1223 process_dir = dir;
|
|
1224 }
|
100
|
1225
|
209
|
1226 /* Some miscellaneous functions that are Windows specific, but not GUI
|
|
1227 specific (ie. are applicable in terminal or batch mode as well). */
|
|
1228
|
|
1229 /* lifted from fileio.c */
|
|
1230 #define CORRECT_DIR_SEPS(s) \
|
|
1231 do { if ('/' == DIRECTORY_SEP) dostounix_filename (s); \
|
|
1232 else unixtodos_filename (s); \
|
|
1233 } while (0)
|
|
1234
|
|
1235 DEFUN ("win32-short-file-name", Fwin32_short_file_name, 1, 1, "", /*
|
|
1236 Return the short file name version (8.3) of the full path of FILENAME.
|
|
1237 If FILENAME does not exist, return nil.
|
|
1238 All path elements in FILENAME are converted to their short names.
|
|
1239 */
|
|
1240 (filename))
|
|
1241 {
|
|
1242 char shortname[MAX_PATH];
|
|
1243
|
288
|
1244 CHECK_STRING (filename);
|
209
|
1245
|
|
1246 /* first expand it. */
|
|
1247 filename = Fexpand_file_name (filename, Qnil);
|
|
1248
|
|
1249 /* luckily, this returns the short version of each element in the path. */
|
|
1250 if (GetShortPathName (XSTRING_DATA (filename), shortname, MAX_PATH) == 0)
|
|
1251 return Qnil;
|
|
1252
|
|
1253 CORRECT_DIR_SEPS (shortname);
|
|
1254
|
|
1255 return build_string (shortname);
|
|
1256 }
|
|
1257
|
|
1258
|
|
1259 DEFUN ("win32-long-file-name", Fwin32_long_file_name, 1, 1, "", /*
|
|
1260 Return the long file name version of the full path of FILENAME.
|
|
1261 If FILENAME does not exist, return nil.
|
|
1262 All path elements in FILENAME are converted to their long names.
|
|
1263 */
|
|
1264 (filename))
|
|
1265 {
|
|
1266 char longname[ MAX_PATH ];
|
|
1267
|
288
|
1268 CHECK_STRING (filename);
|
209
|
1269
|
|
1270 /* first expand it. */
|
|
1271 filename = Fexpand_file_name (filename, Qnil);
|
|
1272
|
|
1273 if (!win32_get_long_filename (XSTRING_DATA (filename), longname, MAX_PATH))
|
|
1274 return Qnil;
|
|
1275
|
|
1276 CORRECT_DIR_SEPS (longname);
|
|
1277
|
|
1278 return build_string (longname);
|
|
1279 }
|
|
1280
|
|
1281 DEFUN ("win32-set-process-priority", Fwin32_set_process_priority, 2, 2, "", /*
|
|
1282 Set the priority of PROCESS to PRIORITY.
|
|
1283 If PROCESS is nil, the priority of Emacs is changed, otherwise the
|
|
1284 priority of the process whose pid is PROCESS is changed.
|
|
1285 PRIORITY should be one of the symbols high, normal, or low;
|
|
1286 any other symbol will be interpreted as normal.
|
|
1287
|
|
1288 If successful, the return value is t, otherwise nil.
|
|
1289 */
|
|
1290 (process, priority))
|
|
1291 {
|
|
1292 HANDLE proc_handle = GetCurrentProcess ();
|
|
1293 DWORD priority_class = NORMAL_PRIORITY_CLASS;
|
|
1294 Lisp_Object result = Qnil;
|
|
1295
|
288
|
1296 CHECK_SYMBOL (priority);
|
209
|
1297
|
|
1298 if (!NILP (process))
|
|
1299 {
|
|
1300 DWORD pid;
|
|
1301 child_process *cp;
|
|
1302
|
|
1303 CHECK_INT (process);
|
|
1304
|
|
1305 /* Allow pid to be an internally generated one, or one obtained
|
|
1306 externally. This is necessary because real pids on Win95 are
|
|
1307 negative. */
|
|
1308
|
|
1309 pid = XINT (process);
|
|
1310 cp = find_child_pid (pid);
|
|
1311 if (cp != NULL)
|
|
1312 pid = cp->procinfo.dwProcessId;
|
|
1313
|
|
1314 proc_handle = OpenProcess (PROCESS_SET_INFORMATION, FALSE, pid);
|
|
1315 }
|
|
1316
|
|
1317 if (EQ (priority, Qhigh))
|
|
1318 priority_class = HIGH_PRIORITY_CLASS;
|
|
1319 else if (EQ (priority, Qlow))
|
|
1320 priority_class = IDLE_PRIORITY_CLASS;
|
|
1321
|
|
1322 if (proc_handle != NULL)
|
|
1323 {
|
|
1324 if (SetPriorityClass (proc_handle, priority_class))
|
|
1325 result = Qt;
|
|
1326 if (!NILP (process))
|
|
1327 CloseHandle (proc_handle);
|
|
1328 }
|
|
1329
|
|
1330 return result;
|
|
1331 }
|
|
1332
|
|
1333
|
|
1334 DEFUN ("win32-get-locale-info", Fwin32_get_locale_info, 1, 2, "", /*
|
|
1335 "Return information about the Windows locale LCID.
|
|
1336 By default, return a three letter locale code which encodes the default
|
|
1337 language as the first two characters, and the country or regionial variant
|
|
1338 as the third letter. For example, ENU refers to `English (United States)',
|
|
1339 while ENC means `English (Canadian)'.
|
|
1340
|
|
1341 If the optional argument LONGFORM is non-nil, the long form of the locale
|
|
1342 name is returned, e.g. `English (United States)' instead.
|
|
1343
|
|
1344 If LCID (a 16-bit number) is not a valid locale, the result is nil.
|
|
1345 */
|
|
1346 (lcid, longform))
|
|
1347 {
|
|
1348 int got_abbrev;
|
|
1349 int got_full;
|
|
1350 char abbrev_name[32] = { 0 };
|
|
1351 char full_name[256] = { 0 };
|
|
1352
|
|
1353 CHECK_INT (lcid);
|
|
1354
|
|
1355 if (!IsValidLocale (XINT (lcid), LCID_SUPPORTED))
|
|
1356 return Qnil;
|
|
1357
|
|
1358 if (NILP (longform))
|
|
1359 {
|
|
1360 got_abbrev = GetLocaleInfo (XINT (lcid),
|
|
1361 LOCALE_SABBREVLANGNAME | LOCALE_USE_CP_ACP,
|
|
1362 abbrev_name, sizeof (abbrev_name));
|
|
1363 if (got_abbrev)
|
|
1364 return build_string (abbrev_name);
|
|
1365 }
|
|
1366 else
|
|
1367 {
|
|
1368 got_full = GetLocaleInfo (XINT (lcid),
|
|
1369 LOCALE_SLANGUAGE | LOCALE_USE_CP_ACP,
|
|
1370 full_name, sizeof (full_name));
|
|
1371 if (got_full)
|
|
1372 return build_string (full_name);
|
|
1373 }
|
|
1374
|
|
1375 return Qnil;
|
|
1376 }
|
|
1377
|
|
1378
|
|
1379 DEFUN ("win32-get-current-locale-id", Fwin32_get_current_locale_id, 0, 0, "", /*
|
|
1380 "Return Windows locale id for current locale setting.
|
|
1381 This is a numerical value; use `win32-get-locale-info' to convert to a
|
|
1382 human-readable form.
|
|
1383 */
|
|
1384 ())
|
|
1385 {
|
|
1386 return make_int (GetThreadLocale ());
|
|
1387 }
|
|
1388
|
|
1389
|
|
1390 DEFUN ("win32-get-default-locale-id", Fwin32_get_default_locale_id, 0, 1, "", /*
|
|
1391 "Return Windows locale id for default locale setting.
|
|
1392 By default, the system default locale setting is returned; if the optional
|
|
1393 parameter USERP is non-nil, the user default locale setting is returned.
|
|
1394 This is a numerical value; use `win32-get-locale-info' to convert to a
|
|
1395 human-readable form.
|
|
1396 */
|
|
1397 (userp))
|
|
1398 {
|
|
1399 if (NILP (userp))
|
|
1400 return make_int (GetSystemDefaultLCID ());
|
|
1401 return make_int (GetUserDefaultLCID ());
|
|
1402 }
|
|
1403
|
|
1404 DWORD int_from_hex (char * s)
|
|
1405 {
|
|
1406 DWORD val = 0;
|
|
1407 static char hex[] = "0123456789abcdefABCDEF";
|
|
1408 char * p;
|
|
1409
|
|
1410 while (*s && (p = strchr(hex, *s)) != NULL)
|
|
1411 {
|
|
1412 unsigned digit = p - hex;
|
|
1413 if (digit > 15)
|
|
1414 digit -= 6;
|
|
1415 val = val * 16 + digit;
|
|
1416 s++;
|
|
1417 }
|
|
1418 return val;
|
|
1419 }
|
|
1420
|
|
1421 /* We need to build a global list, since the EnumSystemLocale callback
|
|
1422 function isn't given a context pointer. */
|
|
1423 Lisp_Object Vwin32_valid_locale_ids;
|
|
1424
|
|
1425 BOOL CALLBACK enum_locale_fn (LPTSTR localeNum)
|
|
1426 {
|
|
1427 DWORD id = int_from_hex (localeNum);
|
|
1428 Vwin32_valid_locale_ids = Fcons (make_int (id), Vwin32_valid_locale_ids);
|
|
1429 return TRUE;
|
|
1430 }
|
|
1431
|
|
1432 DEFUN ("win32-get-valid-locale-ids", Fwin32_get_valid_locale_ids, 0, 0, "", /*
|
|
1433 Return list of all valid Windows locale ids.
|
|
1434 Each id is a numerical value; use `win32-get-locale-info' to convert to a
|
|
1435 human-readable form.
|
|
1436 */
|
|
1437 ())
|
|
1438 {
|
|
1439 Vwin32_valid_locale_ids = Qnil;
|
|
1440
|
|
1441 EnumSystemLocales (enum_locale_fn, LCID_SUPPORTED);
|
|
1442
|
|
1443 Vwin32_valid_locale_ids = Fnreverse (Vwin32_valid_locale_ids);
|
|
1444 return Vwin32_valid_locale_ids;
|
|
1445 }
|
|
1446
|
|
1447
|
|
1448 DEFUN ("win32-set-current-locale", Fwin32_set_current_locale, 1, 1, "", /*
|
|
1449 Make Windows locale LCID be the current locale setting for Emacs.
|
|
1450 If successful, the new locale id is returned, otherwise nil.
|
|
1451 */
|
|
1452 (lcid))
|
|
1453 {
|
|
1454 CHECK_INT (lcid);
|
|
1455
|
|
1456 if (!IsValidLocale (XINT (lcid), LCID_SUPPORTED))
|
|
1457 return Qnil;
|
|
1458
|
|
1459 if (!SetThreadLocale (XINT (lcid)))
|
|
1460 return Qnil;
|
|
1461
|
|
1462 /* Sync with FSF Emacs 19.34.6 note: dwWinThreadId declared in
|
|
1463 w32term.h and defined in w32fns.c, both of which are not in current
|
|
1464 XEmacs. ### Check what we lose by ifdef'ing out these. --marcpa */
|
|
1465 #if 0
|
|
1466 /* Need to set input thread locale if present. */
|
|
1467 if (dwWinThreadId)
|
|
1468 /* Reply is not needed. */
|
|
1469 PostThreadMessage (dwWinThreadId, WM_EMACS_SETLOCALE, XINT (lcid), 0);
|
|
1470 #endif
|
|
1471
|
|
1472 return make_int (GetThreadLocale ());
|
|
1473 }
|
|
1474
|
|
1475
|
288
|
1476 void
|
100
|
1477 syms_of_ntproc ()
|
|
1478 {
|
209
|
1479 Qhigh = intern ("high");
|
|
1480 Qlow = intern ("low");
|
|
1481
|
|
1482 DEFSUBR (Fwin32_short_file_name);
|
|
1483 DEFSUBR (Fwin32_long_file_name);
|
|
1484 DEFSUBR (Fwin32_set_process_priority);
|
|
1485 DEFSUBR (Fwin32_get_locale_info);
|
|
1486 DEFSUBR (Fwin32_get_current_locale_id);
|
|
1487 DEFSUBR (Fwin32_get_default_locale_id);
|
|
1488 DEFSUBR (Fwin32_get_valid_locale_ids);
|
|
1489 DEFSUBR (Fwin32_set_current_locale);
|
100
|
1490
|
|
1491 DEFVAR_LISP ("win32-quote-process-args", &Vwin32_quote_process_args /*
|
209
|
1492 Non-nil enables quoting of process arguments to ensure correct parsing.
|
100
|
1493 Because Windows does not directly pass argv arrays to child processes,
|
|
1494 programs have to reconstruct the argv array by parsing the command
|
|
1495 line string. For an argument to contain a space, it must be enclosed
|
|
1496 in double quotes or it will be parsed as multiple arguments.
|
|
1497
|
209
|
1498 If the value is a character, that character will be used to escape any
|
|
1499 quote characters that appear, otherwise a suitable escape character
|
|
1500 will be chosen based on the type of the program.
|
|
1501 */ );
|
|
1502 Vwin32_quote_process_args = Qt;
|
|
1503
|
|
1504 DEFVAR_LISP ("win32-start-process-show-window",
|
|
1505 &Vwin32_start_process_show_window /*
|
|
1506 When nil, processes started via start-process hide their windows.
|
|
1507 When non-nil, they show their window in the method of their choice.
|
|
1508 */ );
|
|
1509 Vwin32_start_process_show_window = Qnil;
|
|
1510
|
|
1511 DEFVAR_LISP ("win32-start-process-share-console",
|
|
1512 &Vwin32_start_process_share_console /*
|
|
1513 When nil, processes started via start-process are given a new console.
|
|
1514 When non-nil, they share the Emacs console; this has the limitation of
|
|
1515 allowing only only DOS subprocess to run at a time (whether started directly
|
|
1516 or indirectly by Emacs), and preventing Emacs from cleanly terminating the
|
|
1517 subprocess group, but may allow Emacs to interrupt a subprocess that doesn't
|
|
1518 otherwise respond to interrupts from Emacs.
|
|
1519 */ );
|
|
1520 Vwin32_start_process_share_console = Qnil;
|
100
|
1521
|
282
|
1522 DEFVAR_LISP ("win32-pipe-read-delay", &Vwin32_pipe_read_delay /*
|
209
|
1523 Forced delay before reading subprocess output.
|
100
|
1524 This is done to improve the buffering of subprocess output, by
|
|
1525 avoiding the inefficiency of frequently reading small amounts of data.
|
|
1526
|
|
1527 If positive, the value is the number of milliseconds to sleep before
|
|
1528 reading the subprocess output. If negative, the magnitude is the number
|
|
1529 of time slices to wait (effectively boosting the priority of the child
|
209
|
1530 process temporarily). A value of zero disables waiting entirely.
|
|
1531 */ );
|
282
|
1532 Vwin32_pipe_read_delay = make_int (50);
|
100
|
1533
|
209
|
1534 #if 0
|
|
1535 DEFVAR_LISP ("win32-generate-fake-inodes", &Vwin32_generate_fake_inodes /*
|
|
1536 "Non-nil means attempt to fake realistic inode values.
|
|
1537 This works by hashing the truename of files, and should detect
|
|
1538 aliasing between long and short (8.3 DOS) names, but can have
|
|
1539 false positives because of hash collisions. Note that determing
|
|
1540 the truename of a file can be slow.
|
|
1541 */ );
|
|
1542 Vwin32_generate_fake_inodes = Qnil;
|
|
1543 #endif
|
100
|
1544 }
|
|
1545 /* end of ntproc.c */
|