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