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