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