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