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