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