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