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