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