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