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