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