100
|
1 /* Process support for Windows NT port of XEMACS.
|
|
2 Copyright (C) 1992, 1995 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA.
|
|
20
|
|
21 Drew Bliss Oct 14, 1993
|
|
22 Adapted from alarm.c by Tim Fleehart */
|
|
23
|
|
24 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
|
209
|
25 /* Synced with FSF Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
|
100
|
26
|
|
27 #include <stdio.h>
|
|
28 #include <stdlib.h>
|
|
29 #include <errno.h>
|
|
30 #include <io.h>
|
|
31 #include <fcntl.h>
|
|
32 #include <signal.h>
|
|
33
|
|
34 /* must include CRT headers *before* config.h */
|
274
|
35 /* ### I don't believe it - martin */
|
|
36 #include <config.h>
|
100
|
37 #undef signal
|
|
38 #undef wait
|
|
39 #undef spawnve
|
|
40 #undef select
|
|
41 #undef kill
|
|
42
|
|
43 #include <windows.h>
|
239
|
44 #include <sys/socket.h>
|
100
|
45
|
|
46 #include "lisp.h"
|
239
|
47 #include "sysproc.h"
|
100
|
48 #include "nt.h"
|
209
|
49 #include "ntheap.h" /* From 19.34.6 */
|
100
|
50 #include "systime.h"
|
223
|
51 #include "syssignal.h"
|
100
|
52 #include "syswait.h"
|
347
|
53 #include "buffer.h"
|
100
|
54 #include "process.h"
|
209
|
55 /*#include "w32term.h"*/ /* From 19.34.6: sync in ? --marcpa */
|
100
|
56
|
288
|
57 /* #### I'm not going to play with shit. */
|
|
58 #pragma warning (disable:4013 4024 4090)
|
|
59
|
100
|
60 /* Control whether spawnve quotes arguments as necessary to ensure
|
|
61 correct parsing by child process. Because not all uses of spawnve
|
|
62 are careful about constructing argv arrays, we make this behaviour
|
|
63 conditional (off by default). */
|
|
64 Lisp_Object Vwin32_quote_process_args;
|
|
65
|
209
|
66 /* Control whether create_child causes the process' window to be
|
|
67 hidden. The default is nil. */
|
|
68 Lisp_Object Vwin32_start_process_show_window;
|
|
69
|
|
70 /* Control whether create_child causes the process to inherit Emacs'
|
|
71 console window, or be given a new one of its own. The default is
|
|
72 nil, to allow multiple DOS programs to run on Win95. Having separate
|
|
73 consoles also allows Emacs to cleanly terminate process groups. */
|
|
74 Lisp_Object Vwin32_start_process_share_console;
|
|
75
|
100
|
76 /* Time to sleep before reading from a subprocess output pipe - this
|
|
77 avoids the inefficiency of frequently reading small amounts of data.
|
|
78 This is primarily necessary for handling DOS processes on Windows 95,
|
|
79 but is useful for Win32 processes on both Win95 and NT as well. */
|
|
80 Lisp_Object Vwin32_pipe_read_delay;
|
|
81
|
209
|
82 /* Control whether stat() attempts to generate fake but hopefully
|
|
83 "accurate" inode values, by hashing the absolute truenames of files.
|
|
84 This should detect aliasing between long and short names, but still
|
|
85 allows the possibility of hash collisions. */
|
|
86 Lisp_Object Vwin32_generate_fake_inodes;
|
|
87
|
|
88 Lisp_Object Qhigh, Qlow;
|
100
|
89
|
239
|
90 #ifndef DEBUG_XEMACS
|
|
91 __inline
|
100
|
92 #endif
|
|
93 void _DebPrint (const char *fmt, ...)
|
|
94 {
|
239
|
95 #ifdef DEBUG_XEMACS
|
100
|
96 char buf[1024];
|
|
97 va_list args;
|
|
98
|
|
99 va_start (args, fmt);
|
|
100 vsprintf (buf, fmt, args);
|
|
101 va_end (args);
|
|
102 OutputDebugString (buf);
|
239
|
103 #endif
|
100
|
104 }
|
|
105
|
223
|
106 /* sys_signal moved to nt.c. It's now called msw_signal... */
|
100
|
107
|
|
108 /* Defined in <process.h> which conflicts with the local copy */
|
|
109 #define _P_NOWAIT 1
|
|
110
|
|
111 /* Child process management list. */
|
|
112 int child_proc_count = 0;
|
|
113 child_process child_procs[ MAX_CHILDREN ];
|
|
114 child_process *dead_child = NULL;
|
|
115
|
|
116 DWORD WINAPI reader_thread (void *arg);
|
|
117
|
|
118 /* Find an unused process slot. */
|
|
119 child_process *
|
|
120 new_child (void)
|
|
121 {
|
|
122 child_process *cp;
|
|
123 DWORD id;
|
|
124
|
|
125 for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
|
|
126 if (!CHILD_ACTIVE (cp))
|
272
|
127 goto Initialize;
|
100
|
128 if (child_proc_count == MAX_CHILDREN)
|
|
129 return NULL;
|
|
130 cp = &child_procs[child_proc_count++];
|
|
131
|
272
|
132 Initialize:
|
|
133 xzero (*cp);
|
100
|
134 cp->fd = -1;
|
|
135 cp->pid = -1;
|
308
|
136 if (cp->procinfo.hProcess)
|
|
137 CloseHandle(cp->procinfo.hProcess);
|
100
|
138 cp->procinfo.hProcess = NULL;
|
|
139 cp->status = STATUS_READ_ERROR;
|
|
140
|
|
141 /* use manual reset event so that select() will function properly */
|
|
142 cp->char_avail = CreateEvent (NULL, TRUE, FALSE, NULL);
|
|
143 if (cp->char_avail)
|
|
144 {
|
|
145 cp->char_consumed = CreateEvent (NULL, FALSE, FALSE, NULL);
|
|
146 if (cp->char_consumed)
|
|
147 {
|
|
148 cp->thrd = CreateThread (NULL, 1024, reader_thread, cp, 0, &id);
|
|
149 if (cp->thrd)
|
|
150 return cp;
|
|
151 }
|
|
152 }
|
|
153 delete_child (cp);
|
|
154 return NULL;
|
|
155 }
|
|
156
|
|
157 void
|
|
158 delete_child (child_process *cp)
|
|
159 {
|
|
160 int i;
|
|
161
|
|
162 /* Should not be deleting a child that is still needed. */
|
|
163 for (i = 0; i < MAXDESC; i++)
|
|
164 if (fd_info[i].cp == cp)
|
|
165 abort ();
|
|
166
|
|
167 if (!CHILD_ACTIVE (cp))
|
|
168 return;
|
|
169
|
|
170 /* reap thread if necessary */
|
|
171 if (cp->thrd)
|
|
172 {
|
|
173 DWORD rc;
|
|
174
|
|
175 if (GetExitCodeThread (cp->thrd, &rc) && rc == STILL_ACTIVE)
|
|
176 {
|
|
177 /* let the thread exit cleanly if possible */
|
|
178 cp->status = STATUS_READ_ERROR;
|
|
179 SetEvent (cp->char_consumed);
|
|
180 if (WaitForSingleObject (cp->thrd, 1000) != WAIT_OBJECT_0)
|
|
181 {
|
|
182 DebPrint (("delete_child.WaitForSingleObject (thread) failed "
|
|
183 "with %lu for fd %ld\n", GetLastError (), cp->fd));
|
|
184 TerminateThread (cp->thrd, 0);
|
|
185 }
|
|
186 }
|
|
187 CloseHandle (cp->thrd);
|
|
188 cp->thrd = NULL;
|
|
189 }
|
|
190 if (cp->char_avail)
|
|
191 {
|
|
192 CloseHandle (cp->char_avail);
|
|
193 cp->char_avail = NULL;
|
|
194 }
|
|
195 if (cp->char_consumed)
|
|
196 {
|
|
197 CloseHandle (cp->char_consumed);
|
|
198 cp->char_consumed = NULL;
|
|
199 }
|
|
200
|
|
201 /* update child_proc_count (highest numbered slot in use plus one) */
|
|
202 if (cp == child_procs + child_proc_count - 1)
|
|
203 {
|
|
204 for (i = child_proc_count-1; i >= 0; i--)
|
|
205 if (CHILD_ACTIVE (&child_procs[i]))
|
|
206 {
|
|
207 child_proc_count = i + 1;
|
|
208 break;
|
|
209 }
|
|
210 }
|
|
211 if (i < 0)
|
|
212 child_proc_count = 0;
|
|
213 }
|
|
214
|
|
215 /* Find a child by pid. */
|
|
216 static child_process *
|
|
217 find_child_pid (DWORD pid)
|
|
218 {
|
|
219 child_process *cp;
|
|
220
|
|
221 for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
|
|
222 if (CHILD_ACTIVE (cp) && pid == cp->pid)
|
|
223 return cp;
|
|
224 return NULL;
|
|
225 }
|
|
226
|
|
227
|
|
228 /* Thread proc for child process and socket reader threads. Each thread
|
|
229 is normally blocked until woken by select() to check for input by
|
|
230 reading one char. When the read completes, char_avail is signalled
|
|
231 to wake up the select emulator and the thread blocks itself again. */
|
|
232 DWORD WINAPI
|
|
233 reader_thread (void *arg)
|
|
234 {
|
|
235 child_process *cp;
|
|
236
|
|
237 /* Our identity */
|
|
238 cp = (child_process *)arg;
|
|
239
|
308
|
240 /* <matts@tibco.com> - I think the test below is wrong - we don't
|
|
241 want to wait for someone to signal char_consumed, as we haven't
|
|
242 read anything for them to consume yet! */
|
|
243
|
|
244 /*
|
100
|
245 if (cp == NULL ||
|
|
246 WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
|
308
|
247 */
|
|
248
|
|
249 if (cp == NULL)
|
|
250 {
|
|
251 return 1;
|
|
252 }
|
100
|
253
|
|
254 for (;;)
|
|
255 {
|
|
256 int rc;
|
|
257
|
|
258 rc = _sys_read_ahead (cp->fd);
|
|
259
|
|
260 /* The name char_avail is a misnomer - it really just means the
|
|
261 read-ahead has completed, whether successfully or not. */
|
|
262 if (!SetEvent (cp->char_avail))
|
|
263 {
|
|
264 DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n",
|
|
265 GetLastError (), cp->fd));
|
|
266 return 1;
|
|
267 }
|
|
268
|
|
269 if (rc == STATUS_READ_ERROR)
|
308
|
270 {
|
|
271 /* We are finished, so clean up handles and set to NULL so
|
|
272 that CHILD_ACTIVE will see what is going on */
|
|
273 if (cp->char_avail) {
|
|
274 CloseHandle (cp->char_avail);
|
|
275 cp->char_avail = NULL;
|
|
276 }
|
|
277 if (cp->thrd) {
|
|
278 CloseHandle (cp->thrd);
|
|
279 cp->thrd = NULL;
|
|
280 }
|
|
281 if (cp->char_consumed) {
|
|
282 CloseHandle(cp->char_consumed);
|
|
283 cp->char_consumed = NULL;
|
|
284 }
|
|
285 if (cp->procinfo.hProcess)
|
|
286 {
|
|
287 CloseHandle (cp->procinfo.hProcess);
|
|
288 cp->procinfo.hProcess=NULL;
|
|
289 }
|
|
290 return 1;
|
|
291 }
|
100
|
292
|
|
293 /* If the read died, the child has died so let the thread die */
|
|
294 if (rc == STATUS_READ_FAILED)
|
|
295 break;
|
|
296
|
|
297 /* Wait until our input is acknowledged before reading again */
|
|
298 if (WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
|
|
299 {
|
|
300 DebPrint (("reader_thread.WaitForSingleObject failed with "
|
|
301 "%lu for fd %ld\n", GetLastError (), cp->fd));
|
|
302 break;
|
|
303 }
|
|
304 }
|
308
|
305 /* We are finished, so clean up handles and set to NULL so that
|
|
306 CHILD_ACTIVE will see what is going on */
|
|
307 if (cp->char_avail) {
|
|
308 CloseHandle (cp->char_avail);
|
|
309 cp->char_avail = NULL;
|
|
310 }
|
|
311 if (cp->thrd) {
|
|
312 CloseHandle (cp->thrd);
|
|
313 cp->thrd = NULL;
|
|
314 }
|
|
315 if (cp->char_consumed) {
|
|
316 CloseHandle(cp->char_consumed);
|
|
317 cp->char_consumed = NULL;
|
|
318 }
|
|
319 if (cp->procinfo.hProcess)
|
|
320 {
|
|
321 CloseHandle (cp->procinfo.hProcess);
|
|
322 cp->procinfo.hProcess=NULL;
|
|
323 }
|
|
324
|
100
|
325 return 0;
|
|
326 }
|
|
327
|
209
|
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. */
|
288
|
331 static const char * process_dir;
|
209
|
332
|
100
|
333 static BOOL
|
|
334 create_child (char *exe, char *cmdline, char *env,
|
|
335 int * pPid, child_process *cp)
|
|
336 {
|
|
337 STARTUPINFO start;
|
|
338 SECURITY_ATTRIBUTES sec_attrs;
|
|
339 SECURITY_DESCRIPTOR sec_desc;
|
209
|
340 char dir[ MAXPATHLEN ];
|
100
|
341
|
|
342 if (cp == NULL) abort ();
|
|
343
|
272
|
344 xzero (start);
|
100
|
345 start.cb = sizeof (start);
|
|
346
|
|
347 #ifdef HAVE_NTGUI
|
209
|
348 if (NILP (Vwin32_start_process_show_window))
|
100
|
349 start.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
|
209
|
350 else
|
|
351 start.dwFlags = STARTF_USESTDHANDLES;
|
100
|
352 start.wShowWindow = SW_HIDE;
|
|
353
|
|
354 start.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
|
|
355 start.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
356 start.hStdError = GetStdHandle (STD_ERROR_HANDLE);
|
|
357 #endif /* HAVE_NTGUI */
|
|
358
|
|
359 /* Explicitly specify no security */
|
|
360 if (!InitializeSecurityDescriptor (&sec_desc, SECURITY_DESCRIPTOR_REVISION))
|
|
361 goto EH_Fail;
|
|
362 if (!SetSecurityDescriptorDacl (&sec_desc, TRUE, NULL, FALSE))
|
|
363 goto EH_Fail;
|
|
364 sec_attrs.nLength = sizeof (sec_attrs);
|
|
365 sec_attrs.lpSecurityDescriptor = &sec_desc;
|
|
366 sec_attrs.bInheritHandle = FALSE;
|
|
367
|
209
|
368 strcpy (dir, process_dir);
|
|
369 unixtodos_filename (dir);
|
|
370
|
100
|
371 if (!CreateProcess (exe, cmdline, &sec_attrs, NULL, TRUE,
|
209
|
372 (!NILP (Vwin32_start_process_share_console)
|
|
373 ? CREATE_NEW_PROCESS_GROUP
|
|
374 : CREATE_NEW_CONSOLE),
|
|
375 env, dir,
|
100
|
376 &start, &cp->procinfo))
|
|
377 goto EH_Fail;
|
|
378
|
|
379 cp->pid = (int) cp->procinfo.dwProcessId;
|
|
380
|
308
|
381 CloseHandle (cp->procinfo.hThread);
|
|
382 CloseHandle (cp->procinfo.hProcess);
|
|
383 cp->procinfo.hThread=NULL;
|
|
384 cp->procinfo.hProcess=NULL;
|
|
385
|
357
|
386 /* pid must fit in a Lisp_Int */
|
100
|
387
|
|
388
|
|
389 *pPid = cp->pid;
|
|
390
|
|
391 return TRUE;
|
|
392
|
|
393 EH_Fail:
|
|
394 DebPrint (("create_child.CreateProcess failed: %ld\n", GetLastError()););
|
|
395 return FALSE;
|
|
396 }
|
|
397
|
209
|
398 void
|
|
399 win32_executable_type (char * filename, int * is_dos_app, int * is_cygnus_app)
|
100
|
400 {
|
209
|
401 file_data executable;
|
|
402 char * p;
|
100
|
403
|
209
|
404 /* Default values in case we can't tell for sure. */
|
|
405 *is_dos_app = FALSE;
|
|
406 *is_cygnus_app = FALSE;
|
|
407
|
|
408 if (!open_input_file (&executable, filename))
|
|
409 return;
|
|
410
|
|
411 p = strrchr (filename, '.');
|
100
|
412
|
|
413 /* We can only identify DOS .com programs from the extension. */
|
|
414 if (p && stricmp (p, ".com") == 0)
|
209
|
415 *is_dos_app = TRUE;
|
|
416 else if (p && (stricmp (p, ".bat") == 0 ||
|
|
417 stricmp (p, ".cmd") == 0))
|
|
418 {
|
|
419 /* A DOS shell script - it appears that CreateProcess is happy to
|
|
420 accept this (somewhat surprisingly); presumably it looks at
|
|
421 COMSPEC to determine what executable to actually invoke.
|
100
|
422 Therefore, we have to do the same here as well. */
|
209
|
423 /* Actually, I think it uses the program association for that
|
|
424 extension, which is defined in the registry. */
|
|
425 p = egetenv ("COMSPEC");
|
100
|
426 if (p)
|
209
|
427 win32_executable_type (p, is_dos_app, is_cygnus_app);
|
100
|
428 }
|
|
429 else
|
|
430 {
|
209
|
431 /* Look for DOS .exe signature - if found, we must also check that
|
|
432 it isn't really a 16- or 32-bit Windows exe, since both formats
|
|
433 start with a DOS program stub. Note that 16-bit Windows
|
|
434 executables use the OS/2 1.x format. */
|
|
435
|
|
436 IMAGE_DOS_HEADER * dos_header;
|
|
437 IMAGE_NT_HEADERS * nt_header;
|
|
438
|
|
439 dos_header = (PIMAGE_DOS_HEADER) executable.file_base;
|
|
440 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
|
|
441 goto unwind;
|
|
442
|
|
443 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
|
|
444
|
|
445 if ((char *) nt_header > (char *) dos_header + executable.size)
|
|
446 {
|
|
447 /* Some dos headers (pkunzip) have bogus e_lfanew fields. */
|
|
448 *is_dos_app = TRUE;
|
|
449 }
|
|
450 else if (nt_header->Signature != IMAGE_NT_SIGNATURE &&
|
|
451 LOWORD (nt_header->Signature) != IMAGE_OS2_SIGNATURE)
|
|
452 {
|
|
453 *is_dos_app = TRUE;
|
|
454 }
|
|
455 else if (nt_header->Signature == IMAGE_NT_SIGNATURE)
|
|
456 {
|
|
457 /* Look for cygwin.dll in DLL import list. */
|
|
458 IMAGE_DATA_DIRECTORY import_dir =
|
|
459 nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
|
|
460 IMAGE_IMPORT_DESCRIPTOR * imports;
|
|
461 IMAGE_SECTION_HEADER * section;
|
|
462
|
|
463 section = rva_to_section (import_dir.VirtualAddress, nt_header);
|
|
464 imports = RVA_TO_PTR (import_dir.VirtualAddress, section, executable);
|
|
465
|
|
466 for ( ; imports->Name; imports++)
|
100
|
467 {
|
209
|
468 char * dllname = RVA_TO_PTR (imports->Name, section, executable);
|
|
469
|
|
470 if (strcmp (dllname, "cygwin.dll") == 0)
|
|
471 {
|
|
472 *is_cygnus_app = TRUE;
|
|
473 break;
|
|
474 }
|
100
|
475 }
|
|
476 }
|
209
|
477 }
|
|
478
|
|
479 unwind:
|
|
480 close_file_data (&executable);
|
|
481 }
|
|
482
|
|
483 int
|
|
484 compare_env (const char **strp1, const char **strp2)
|
|
485 {
|
|
486 const char *str1 = *strp1, *str2 = *strp2;
|
|
487
|
|
488 while (*str1 && *str2 && *str1 != '=' && *str2 != '=')
|
|
489 {
|
|
490 if ((*str1) > (*str2))
|
|
491 return 1;
|
|
492 else if ((*str1) < (*str2))
|
|
493 return -1;
|
|
494 str1++, str2++;
|
100
|
495 }
|
|
496
|
209
|
497 if (*str1 == '=' && *str2 == '=')
|
|
498 return 0;
|
|
499 else if (*str1 == '=')
|
|
500 return -1;
|
|
501 else
|
|
502 return 1;
|
100
|
503 }
|
|
504
|
209
|
505 void
|
|
506 merge_and_sort_env (char **envp1, char **envp2, char **new_envp)
|
|
507 {
|
|
508 char **optr, **nptr;
|
|
509 int num;
|
|
510
|
|
511 nptr = new_envp;
|
|
512 optr = envp1;
|
|
513 while (*optr)
|
|
514 *nptr++ = *optr++;
|
|
515 num = optr - envp1;
|
|
516
|
|
517 optr = envp2;
|
|
518 while (*optr)
|
|
519 *nptr++ = *optr++;
|
|
520 num += optr - envp2;
|
|
521
|
|
522 qsort (new_envp, num, sizeof (char *), compare_env);
|
|
523
|
|
524 *nptr = NULL;
|
|
525 }
|
100
|
526
|
|
527 /* When a new child process is created we need to register it in our list,
|
|
528 so intercept spawn requests. */
|
|
529 int
|
239
|
530 sys_spawnve (int mode, CONST char *cmdname,
|
|
531 CONST char * CONST *argv, CONST char *CONST *envp)
|
100
|
532 {
|
|
533 Lisp_Object program, full;
|
|
534 char *cmdline, *env, *parg, **targ;
|
209
|
535 int arglen, numenv;
|
100
|
536 int pid;
|
|
537 child_process *cp;
|
209
|
538 int is_dos_app, is_cygnus_app;
|
|
539 int do_quoting = 0;
|
|
540 char escape_char;
|
|
541 /* We pass our process ID to our children by setting up an environment
|
|
542 variable in their environment. */
|
|
543 char ppid_env_var_buffer[64];
|
|
544 char *extra_env[] = {ppid_env_var_buffer, NULL};
|
124
|
545 struct gcpro gcpro1;
|
|
546
|
100
|
547 /* We don't care about the other modes */
|
|
548 if (mode != _P_NOWAIT)
|
|
549 {
|
|
550 errno = EINVAL;
|
|
551 return -1;
|
|
552 }
|
|
553
|
|
554 /* Handle executable names without an executable suffix. */
|
|
555 program = make_string (cmdname, strlen (cmdname));
|
124
|
556 GCPRO1 (program);
|
100
|
557 if (NILP (Ffile_executable_p (program)))
|
|
558 {
|
|
559 full = Qnil;
|
|
560 locate_file (Vexec_path, program, EXEC_SUFFIXES, &full, 1);
|
|
561 if (NILP (full))
|
|
562 {
|
124
|
563 UNGCPRO;
|
100
|
564 errno = EINVAL;
|
|
565 return -1;
|
|
566 }
|
347
|
567 GET_C_STRING_FILENAME_DATA_ALLOCA (full, cmdname);
|
|
568 }
|
|
569 else
|
|
570 {
|
|
571 (char*)cmdname = alloca (strlen (argv[0]) + 1);
|
|
572 strcpy ((char*)cmdname, argv[0]);
|
100
|
573 }
|
124
|
574 UNGCPRO;
|
|
575
|
209
|
576 /* make sure argv[0] and cmdname are both in DOS format */
|
100
|
577 unixtodos_filename (cmdname);
|
239
|
578 /* #### KLUDGE */
|
347
|
579 ((CONST char**)argv)[0] = cmdname;
|
100
|
580
|
209
|
581 /* Determine whether program is a 16-bit DOS executable, or a Win32
|
|
582 executable that is implicitly linked to the Cygnus dll (implying it
|
|
583 was compiled with the Cygnus GNU toolchain and hence relies on
|
|
584 cygwin.dll to parse the command line - we use this to decide how to
|
|
585 escape quote chars in command line args that must be quoted). */
|
|
586 win32_executable_type (cmdname, &is_dos_app, &is_cygnus_app);
|
|
587
|
|
588 /* On Windows 95, if cmdname is a DOS app, we invoke a helper
|
|
589 application to start it by specifying the helper app as cmdname,
|
|
590 while leaving the real app name as argv[0]. */
|
|
591 if (is_dos_app)
|
100
|
592 {
|
209
|
593 cmdname = alloca (MAXPATHLEN);
|
|
594 if (egetenv ("CMDPROXY"))
|
|
595 strcpy (cmdname, egetenv ("CMDPROXY"));
|
|
596 else
|
|
597 {
|
|
598 strcpy (cmdname, XSTRING_DATA (Vinvocation_directory));
|
|
599 strcat (cmdname, "cmdproxy.exe");
|
|
600 }
|
|
601 unixtodos_filename (cmdname);
|
100
|
602 }
|
|
603
|
|
604 /* we have to do some conjuring here to put argv and envp into the
|
|
605 form CreateProcess wants... argv needs to be a space separated/null
|
|
606 terminated list of parameters, and envp is a null
|
|
607 separated/double-null terminated list of parameters.
|
|
608
|
209
|
609 Additionally, zero-length args and args containing whitespace or
|
|
610 quote chars need to be wrapped in double quotes - for this to work,
|
|
611 embedded quotes need to be escaped as well. The aim is to ensure
|
|
612 the child process reconstructs the argv array we start with
|
|
613 exactly, so we treat quotes at the beginning and end of arguments
|
|
614 as embedded quotes.
|
|
615
|
|
616 The Win32 GNU-based library from Cygnus doubles quotes to escape
|
|
617 them, while MSVC uses backslash for escaping. (Actually the MSVC
|
|
618 startup code does attempt to recognise doubled quotes and accept
|
|
619 them, but gets it wrong and ends up requiring three quotes to get a
|
|
620 single embedded quote!) So by default we decide whether to use
|
|
621 quote or backslash as the escape character based on whether the
|
|
622 binary is apparently a Cygnus compiled app.
|
|
623
|
|
624 Note that using backslash to escape embedded quotes requires
|
|
625 additional special handling if an embedded quote is already
|
|
626 preceeded by backslash, or if an arg requiring quoting ends with
|
|
627 backslash. In such cases, the run of escape characters needs to be
|
|
628 doubled. For consistency, we apply this special handling as long
|
|
629 as the escape character is not quote.
|
100
|
630
|
209
|
631 Since we have no idea how large argv and envp are likely to be we
|
|
632 figure out list lengths on the fly and allocate them. */
|
|
633
|
|
634 if (!NILP (Vwin32_quote_process_args))
|
|
635 {
|
|
636 do_quoting = 1;
|
|
637 /* Override escape char by binding win32-quote-process-args to
|
|
638 desired character, or use t for auto-selection. */
|
|
639 if (INTP (Vwin32_quote_process_args))
|
|
640 escape_char = XINT (Vwin32_quote_process_args);
|
|
641 else
|
|
642 escape_char = is_cygnus_app ? '"' : '\\';
|
|
643 }
|
100
|
644
|
|
645 /* do argv... */
|
|
646 arglen = 0;
|
|
647 targ = argv;
|
|
648 while (*targ)
|
|
649 {
|
|
650 char * p = *targ;
|
209
|
651 int need_quotes = 0;
|
|
652 int escape_char_run = 0;
|
100
|
653
|
|
654 if (*p == 0)
|
209
|
655 need_quotes = 1;
|
|
656 for ( ; *p; p++)
|
|
657 {
|
|
658 if (*p == '"')
|
100
|
659 {
|
209
|
660 /* allow for embedded quotes to be escaped */
|
100
|
661 arglen++;
|
209
|
662 need_quotes = 1;
|
|
663 /* handle the case where the embedded quote is already escaped */
|
|
664 if (escape_char_run > 0)
|
|
665 {
|
|
666 /* To preserve the arg exactly, we need to double the
|
|
667 preceding escape characters (plus adding one to
|
|
668 escape the quote character itself). */
|
|
669 arglen += escape_char_run;
|
100
|
670 }
|
209
|
671 }
|
100
|
672 else if (*p == ' ' || *p == '\t')
|
209
|
673 {
|
|
674 need_quotes = 1;
|
|
675 }
|
|
676
|
|
677 if (*p == escape_char && escape_char != '"')
|
|
678 escape_char_run++;
|
|
679 else
|
|
680 escape_char_run = 0;
|
|
681 }
|
|
682 if (need_quotes)
|
|
683 {
|
100
|
684 arglen += 2;
|
209
|
685 /* handle the case where the arg ends with an escape char - we
|
|
686 must not let the enclosing quote be escaped. */
|
|
687 if (escape_char_run > 0)
|
|
688 arglen += escape_char_run;
|
|
689 }
|
100
|
690 arglen += strlen (*targ++) + 1;
|
|
691 }
|
|
692 cmdline = alloca (arglen);
|
|
693 targ = argv;
|
|
694 parg = cmdline;
|
|
695 while (*targ)
|
|
696 {
|
|
697 char * p = *targ;
|
209
|
698 int need_quotes = 0;
|
100
|
699
|
|
700 if (*p == 0)
|
209
|
701 need_quotes = 1;
|
100
|
702
|
209
|
703 if (do_quoting)
|
100
|
704 {
|
|
705 for ( ; *p; p++)
|
|
706 if (*p == ' ' || *p == '\t' || *p == '"')
|
209
|
707 need_quotes = 1;
|
100
|
708 }
|
209
|
709 if (need_quotes)
|
100
|
710 {
|
209
|
711 int escape_char_run = 0;
|
100
|
712 char * first;
|
|
713 char * last;
|
|
714
|
|
715 p = *targ;
|
|
716 first = p;
|
|
717 last = p + strlen (p) - 1;
|
|
718 *parg++ = '"';
|
209
|
719 #if 0
|
|
720 /* This version does not escape quotes if they occur at the
|
|
721 beginning or end of the arg - this could lead to incorrect
|
|
722 behaviour when the arg itself represents a command line
|
|
723 containing quoted args. I believe this was originally done
|
|
724 as a hack to make some things work, before
|
|
725 `win32-quote-process-args' was added. */
|
100
|
726 while (*p)
|
|
727 {
|
|
728 if (*p == '"' && p > first && p < last)
|
209
|
729 *parg++ = escape_char; /* escape embedded quotes */
|
100
|
730 *parg++ = *p++;
|
|
731 }
|
209
|
732 #else
|
|
733 for ( ; *p; p++)
|
|
734 {
|
|
735 if (*p == '"')
|
|
736 {
|
|
737 /* double preceding escape chars if any */
|
|
738 while (escape_char_run > 0)
|
|
739 {
|
|
740 *parg++ = escape_char;
|
|
741 escape_char_run--;
|
|
742 }
|
|
743 /* escape all quote chars, even at beginning or end */
|
|
744 *parg++ = escape_char;
|
|
745 }
|
|
746 *parg++ = *p;
|
|
747
|
|
748 if (*p == escape_char && escape_char != '"')
|
|
749 escape_char_run++;
|
|
750 else
|
|
751 escape_char_run = 0;
|
|
752 }
|
|
753 /* double escape chars before enclosing quote */
|
|
754 while (escape_char_run > 0)
|
|
755 {
|
|
756 *parg++ = escape_char;
|
|
757 escape_char_run--;
|
|
758 }
|
|
759 #endif
|
100
|
760 *parg++ = '"';
|
|
761 }
|
|
762 else
|
|
763 {
|
|
764 strcpy (parg, *targ);
|
|
765 parg += strlen (*targ);
|
|
766 }
|
|
767 *parg++ = ' ';
|
|
768 targ++;
|
|
769 }
|
|
770 *--parg = '\0';
|
|
771
|
|
772 /* and envp... */
|
|
773 arglen = 1;
|
|
774 targ = envp;
|
209
|
775 numenv = 1; /* for end null */
|
100
|
776 while (*targ)
|
|
777 {
|
|
778 arglen += strlen (*targ++) + 1;
|
209
|
779 numenv++;
|
100
|
780 }
|
209
|
781 /* extra env vars... */
|
100
|
782 sprintf (ppid_env_var_buffer, "__PARENT_PROCESS_ID=%d",
|
|
783 GetCurrentProcessId ());
|
|
784 arglen += strlen (ppid_env_var_buffer) + 1;
|
209
|
785 numenv++;
|
100
|
786
|
209
|
787 /* merge env passed in and extra env into one, and sort it. */
|
|
788 targ = (char **) alloca (numenv * sizeof (char *));
|
|
789 merge_and_sort_env (envp, extra_env, targ);
|
|
790
|
|
791 /* concatenate env entries. */
|
100
|
792 env = alloca (arglen);
|
|
793 parg = env;
|
|
794 while (*targ)
|
|
795 {
|
|
796 strcpy (parg, *targ);
|
|
797 parg += strlen (*targ++);
|
|
798 *parg++ = '\0';
|
|
799 }
|
|
800 *parg++ = '\0';
|
|
801 *parg = '\0';
|
|
802
|
|
803 cp = new_child ();
|
|
804 if (cp == NULL)
|
|
805 {
|
|
806 errno = EAGAIN;
|
|
807 return -1;
|
|
808 }
|
|
809
|
|
810 /* Now create the process. */
|
|
811 if (!create_child (cmdname, cmdline, env, &pid, cp))
|
|
812 {
|
|
813 delete_child (cp);
|
|
814 errno = ENOEXEC;
|
|
815 return -1;
|
|
816 }
|
|
817
|
|
818 return pid;
|
|
819 }
|
|
820
|
|
821 /* Substitute for certain kill () operations */
|
209
|
822
|
|
823 static BOOL CALLBACK
|
|
824 find_child_console (HWND hwnd, child_process * cp)
|
|
825 {
|
|
826 DWORD thread_id;
|
|
827 DWORD process_id;
|
|
828
|
|
829 thread_id = GetWindowThreadProcessId (hwnd, &process_id);
|
|
830 if (process_id == cp->procinfo.dwProcessId)
|
|
831 {
|
|
832 char window_class[32];
|
|
833
|
|
834 GetClassName (hwnd, window_class, sizeof (window_class));
|
|
835 if (strcmp (window_class,
|
|
836 (os_subtype == OS_WIN95)
|
|
837 ? "tty"
|
|
838 : "ConsoleWindowClass") == 0)
|
|
839 {
|
|
840 cp->hwnd = hwnd;
|
|
841 return FALSE;
|
|
842 }
|
|
843 }
|
|
844 /* keep looking */
|
|
845 return TRUE;
|
|
846 }
|
|
847
|
100
|
848 int
|
|
849 sys_kill (int pid, int sig)
|
|
850 {
|
|
851 child_process *cp;
|
|
852 HANDLE proc_hand;
|
|
853 int need_to_free = 0;
|
|
854 int rc = 0;
|
|
855
|
|
856 /* Only handle signals that will result in the process dying */
|
|
857 if (sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP)
|
|
858 {
|
|
859 errno = EINVAL;
|
|
860 return -1;
|
|
861 }
|
|
862
|
|
863 cp = find_child_pid (pid);
|
|
864 if (cp == NULL)
|
|
865 {
|
|
866 proc_hand = OpenProcess (PROCESS_TERMINATE, 0, pid);
|
|
867 if (proc_hand == NULL)
|
|
868 {
|
|
869 errno = EPERM;
|
|
870 return -1;
|
|
871 }
|
|
872 need_to_free = 1;
|
|
873 }
|
|
874 else
|
|
875 {
|
|
876 proc_hand = cp->procinfo.hProcess;
|
|
877 pid = cp->procinfo.dwProcessId;
|
209
|
878
|
|
879 /* Try to locate console window for process. */
|
|
880 EnumWindows (find_child_console, (LPARAM) cp);
|
100
|
881 }
|
|
882
|
|
883 if (sig == SIGINT)
|
|
884 {
|
209
|
885 if (NILP (Vwin32_start_process_share_console) && cp && cp->hwnd)
|
|
886 {
|
|
887 BYTE control_scan_code = (BYTE) MapVirtualKey (VK_CONTROL, 0);
|
|
888 BYTE vk_break_code = VK_CANCEL;
|
|
889 BYTE break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
|
|
890 HWND foreground_window;
|
|
891
|
|
892 if (break_scan_code == 0)
|
|
893 {
|
|
894 /* Fake Ctrl-C if we can't manage Ctrl-Break. */
|
|
895 vk_break_code = 'C';
|
|
896 break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
|
|
897 }
|
|
898
|
|
899 foreground_window = GetForegroundWindow ();
|
|
900 if (foreground_window && SetForegroundWindow (cp->hwnd))
|
|
901 {
|
|
902 /* Generate keystrokes as if user had typed Ctrl-Break or Ctrl-C. */
|
|
903 keybd_event (VK_CONTROL, control_scan_code, 0, 0);
|
|
904 keybd_event (vk_break_code, break_scan_code, 0, 0);
|
|
905 keybd_event (vk_break_code, break_scan_code, KEYEVENTF_KEYUP, 0);
|
|
906 keybd_event (VK_CONTROL, control_scan_code, KEYEVENTF_KEYUP, 0);
|
|
907
|
|
908 /* Sleep for a bit to give time for Emacs frame to respond
|
|
909 to focus change events (if Emacs was active app). */
|
|
910 Sleep (10);
|
|
911
|
|
912 SetForegroundWindow (foreground_window);
|
|
913 }
|
|
914 }
|
100
|
915 /* Ctrl-Break is NT equivalent of SIGINT. */
|
209
|
916 else if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, pid))
|
100
|
917 {
|
|
918 DebPrint (("sys_kill.GenerateConsoleCtrlEvent return %d "
|
|
919 "for pid %lu\n", GetLastError (), pid));
|
|
920 errno = EINVAL;
|
|
921 rc = -1;
|
|
922 }
|
|
923 }
|
|
924 else
|
|
925 {
|
209
|
926 if (NILP (Vwin32_start_process_share_console) && cp && cp->hwnd)
|
|
927 {
|
|
928 #if 1
|
|
929 if (os_subtype == OS_WIN95)
|
|
930 {
|
|
931 /*
|
|
932 Another possibility is to try terminating the VDM out-right by
|
|
933 calling the Shell VxD (id 0x17) V86 interface, function #4
|
|
934 "SHELL_Destroy_VM", ie.
|
|
935
|
|
936 mov edx,4
|
|
937 mov ebx,vm_handle
|
|
938 call shellapi
|
|
939
|
|
940 First need to determine the current VM handle, and then arrange for
|
|
941 the shellapi call to be made from the system vm (by using
|
|
942 Switch_VM_and_callback).
|
|
943
|
|
944 Could try to invoke DestroyVM through CallVxD.
|
|
945
|
|
946 */
|
|
947 #if 0
|
|
948 /* On Win95, posting WM_QUIT causes the 16-bit subsystem
|
|
949 to hang when cmdproxy is used in conjunction with
|
|
950 command.com for an interactive shell. Posting
|
|
951 WM_CLOSE pops up a dialog that, when Yes is selected,
|
|
952 does the same thing. TerminateProcess is also less
|
|
953 than ideal in that subprocesses tend to stick around
|
|
954 until the machine is shutdown, but at least it
|
|
955 doesn't freeze the 16-bit subsystem. */
|
|
956 PostMessage (cp->hwnd, WM_QUIT, 0xff, 0);
|
|
957 #endif
|
|
958 if (!TerminateProcess (proc_hand, 0xff))
|
|
959 {
|
|
960 DebPrint (("sys_kill.TerminateProcess returned %d "
|
|
961 "for pid %lu\n", GetLastError (), pid));
|
|
962 errno = EINVAL;
|
|
963 rc = -1;
|
|
964 }
|
|
965 }
|
|
966 else
|
|
967 #endif
|
|
968 PostMessage (cp->hwnd, WM_CLOSE, 0, 0);
|
|
969 }
|
100
|
970 /* Kill the process. On Win32 this doesn't kill child processes
|
|
971 so it doesn't work very well for shells which is why it's not
|
209
|
972 used in every case. */
|
|
973 else if (!TerminateProcess (proc_hand, 0xff))
|
100
|
974 {
|
|
975 DebPrint (("sys_kill.TerminateProcess returned %d "
|
|
976 "for pid %lu\n", GetLastError (), pid));
|
|
977 errno = EINVAL;
|
|
978 rc = -1;
|
|
979 }
|
|
980 }
|
|
981
|
|
982 if (need_to_free)
|
|
983 CloseHandle (proc_hand);
|
|
984
|
|
985 return rc;
|
|
986 }
|
|
987
|
|
988 #if 0
|
209
|
989 /* Sync with FSF Emacs 19.34.6 note: ifdef'ed out in XEmacs */
|
100
|
990 extern int report_file_error (CONST char *, Lisp_Object);
|
|
991 #endif
|
|
992 /* The following two routines are used to manipulate stdin, stdout, and
|
|
993 stderr of our child processes.
|
|
994
|
|
995 Assuming that in, out, and err are *not* inheritable, we make them
|
|
996 stdin, stdout, and stderr of the child as follows:
|
|
997
|
|
998 - Save the parent's current standard handles.
|
|
999 - Set the std handles to inheritable duplicates of the ones being passed in.
|
|
1000 (Note that _get_osfhandle() is an io.h procedure that retrieves the
|
|
1001 NT file handle for a crt file descriptor.)
|
|
1002 - Spawn the child, which inherits in, out, and err as stdin,
|
|
1003 stdout, and stderr. (see Spawnve)
|
|
1004 - Close the std handles passed to the child.
|
|
1005 - Reset the parent's standard handles to the saved handles.
|
|
1006 (see reset_standard_handles)
|
|
1007 We assume that the caller closes in, out, and err after calling us. */
|
|
1008
|
|
1009 void
|
|
1010 prepare_standard_handles (int in, int out, int err, HANDLE handles[3])
|
|
1011 {
|
|
1012 HANDLE parent;
|
|
1013 HANDLE newstdin, newstdout, newstderr;
|
|
1014
|
|
1015 parent = GetCurrentProcess ();
|
|
1016
|
|
1017 handles[0] = GetStdHandle (STD_INPUT_HANDLE);
|
|
1018 handles[1] = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
1019 handles[2] = GetStdHandle (STD_ERROR_HANDLE);
|
|
1020
|
|
1021 /* make inheritable copies of the new handles */
|
|
1022 if (!DuplicateHandle (parent,
|
|
1023 (HANDLE) _get_osfhandle (in),
|
|
1024 parent,
|
|
1025 &newstdin,
|
|
1026 0,
|
|
1027 TRUE,
|
|
1028 DUPLICATE_SAME_ACCESS))
|
|
1029 report_file_error ("Duplicating input handle for child", Qnil);
|
|
1030
|
|
1031 if (!DuplicateHandle (parent,
|
|
1032 (HANDLE) _get_osfhandle (out),
|
|
1033 parent,
|
|
1034 &newstdout,
|
|
1035 0,
|
|
1036 TRUE,
|
|
1037 DUPLICATE_SAME_ACCESS))
|
|
1038 report_file_error ("Duplicating output handle for child", Qnil);
|
|
1039
|
|
1040 if (!DuplicateHandle (parent,
|
|
1041 (HANDLE) _get_osfhandle (err),
|
|
1042 parent,
|
|
1043 &newstderr,
|
|
1044 0,
|
|
1045 TRUE,
|
|
1046 DUPLICATE_SAME_ACCESS))
|
|
1047 report_file_error ("Duplicating error handle for child", Qnil);
|
|
1048
|
|
1049 /* and store them as our std handles */
|
|
1050 if (!SetStdHandle (STD_INPUT_HANDLE, newstdin))
|
|
1051 report_file_error ("Changing stdin handle", Qnil);
|
|
1052
|
|
1053 if (!SetStdHandle (STD_OUTPUT_HANDLE, newstdout))
|
|
1054 report_file_error ("Changing stdout handle", Qnil);
|
|
1055
|
|
1056 if (!SetStdHandle (STD_ERROR_HANDLE, newstderr))
|
|
1057 report_file_error ("Changing stderr handle", Qnil);
|
|
1058 }
|
|
1059
|
|
1060 void
|
|
1061 reset_standard_handles (int in, int out, int err, HANDLE handles[3])
|
|
1062 {
|
|
1063 /* close the duplicated handles passed to the child */
|
|
1064 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
|
|
1065 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
|
|
1066 CloseHandle (GetStdHandle (STD_ERROR_HANDLE));
|
|
1067
|
|
1068 /* now restore parent's saved std handles */
|
|
1069 SetStdHandle (STD_INPUT_HANDLE, handles[0]);
|
|
1070 SetStdHandle (STD_OUTPUT_HANDLE, handles[1]);
|
|
1071 SetStdHandle (STD_ERROR_HANDLE, handles[2]);
|
|
1072 }
|
|
1073
|
209
|
1074 void
|
288
|
1075 set_process_dir (const char * dir)
|
209
|
1076 {
|
|
1077 process_dir = dir;
|
|
1078 }
|
100
|
1079
|
209
|
1080 /* Some miscellaneous functions that are Windows specific, but not GUI
|
|
1081 specific (ie. are applicable in terminal or batch mode as well). */
|
|
1082
|
|
1083 /* lifted from fileio.c */
|
|
1084 #define CORRECT_DIR_SEPS(s) \
|
|
1085 do { if ('/' == DIRECTORY_SEP) dostounix_filename (s); \
|
|
1086 else unixtodos_filename (s); \
|
|
1087 } while (0)
|
|
1088
|
|
1089 DEFUN ("win32-short-file-name", Fwin32_short_file_name, 1, 1, "", /*
|
|
1090 Return the short file name version (8.3) of the full path of FILENAME.
|
|
1091 If FILENAME does not exist, return nil.
|
|
1092 All path elements in FILENAME are converted to their short names.
|
|
1093 */
|
|
1094 (filename))
|
|
1095 {
|
|
1096 char shortname[MAX_PATH];
|
|
1097
|
288
|
1098 CHECK_STRING (filename);
|
209
|
1099
|
|
1100 /* first expand it. */
|
|
1101 filename = Fexpand_file_name (filename, Qnil);
|
|
1102
|
|
1103 /* luckily, this returns the short version of each element in the path. */
|
|
1104 if (GetShortPathName (XSTRING_DATA (filename), shortname, MAX_PATH) == 0)
|
|
1105 return Qnil;
|
|
1106
|
|
1107 CORRECT_DIR_SEPS (shortname);
|
|
1108
|
|
1109 return build_string (shortname);
|
|
1110 }
|
|
1111
|
|
1112
|
|
1113 DEFUN ("win32-long-file-name", Fwin32_long_file_name, 1, 1, "", /*
|
|
1114 Return the long file name version of the full path of FILENAME.
|
|
1115 If FILENAME does not exist, return nil.
|
|
1116 All path elements in FILENAME are converted to their long names.
|
|
1117 */
|
|
1118 (filename))
|
|
1119 {
|
|
1120 char longname[ MAX_PATH ];
|
|
1121
|
288
|
1122 CHECK_STRING (filename);
|
209
|
1123
|
|
1124 /* first expand it. */
|
|
1125 filename = Fexpand_file_name (filename, Qnil);
|
|
1126
|
|
1127 if (!win32_get_long_filename (XSTRING_DATA (filename), longname, MAX_PATH))
|
|
1128 return Qnil;
|
|
1129
|
|
1130 CORRECT_DIR_SEPS (longname);
|
|
1131
|
|
1132 return build_string (longname);
|
|
1133 }
|
|
1134
|
|
1135 DEFUN ("win32-set-process-priority", Fwin32_set_process_priority, 2, 2, "", /*
|
|
1136 Set the priority of PROCESS to PRIORITY.
|
|
1137 If PROCESS is nil, the priority of Emacs is changed, otherwise the
|
|
1138 priority of the process whose pid is PROCESS is changed.
|
|
1139 PRIORITY should be one of the symbols high, normal, or low;
|
|
1140 any other symbol will be interpreted as normal.
|
|
1141
|
|
1142 If successful, the return value is t, otherwise nil.
|
|
1143 */
|
|
1144 (process, priority))
|
|
1145 {
|
|
1146 HANDLE proc_handle = GetCurrentProcess ();
|
|
1147 DWORD priority_class = NORMAL_PRIORITY_CLASS;
|
|
1148 Lisp_Object result = Qnil;
|
|
1149
|
288
|
1150 CHECK_SYMBOL (priority);
|
209
|
1151
|
|
1152 if (!NILP (process))
|
|
1153 {
|
|
1154 DWORD pid;
|
|
1155 child_process *cp;
|
|
1156
|
|
1157 CHECK_INT (process);
|
|
1158
|
|
1159 /* Allow pid to be an internally generated one, or one obtained
|
|
1160 externally. This is necessary because real pids on Win95 are
|
|
1161 negative. */
|
|
1162
|
|
1163 pid = XINT (process);
|
|
1164 cp = find_child_pid (pid);
|
|
1165 if (cp != NULL)
|
|
1166 pid = cp->procinfo.dwProcessId;
|
|
1167
|
|
1168 proc_handle = OpenProcess (PROCESS_SET_INFORMATION, FALSE, pid);
|
|
1169 }
|
|
1170
|
|
1171 if (EQ (priority, Qhigh))
|
|
1172 priority_class = HIGH_PRIORITY_CLASS;
|
|
1173 else if (EQ (priority, Qlow))
|
|
1174 priority_class = IDLE_PRIORITY_CLASS;
|
|
1175
|
|
1176 if (proc_handle != NULL)
|
|
1177 {
|
|
1178 if (SetPriorityClass (proc_handle, priority_class))
|
|
1179 result = Qt;
|
|
1180 if (!NILP (process))
|
|
1181 CloseHandle (proc_handle);
|
|
1182 }
|
|
1183
|
|
1184 return result;
|
|
1185 }
|
|
1186
|
|
1187
|
|
1188 DEFUN ("win32-get-locale-info", Fwin32_get_locale_info, 1, 2, "", /*
|
|
1189 "Return information about the Windows locale LCID.
|
|
1190 By default, return a three letter locale code which encodes the default
|
|
1191 language as the first two characters, and the country or regionial variant
|
|
1192 as the third letter. For example, ENU refers to `English (United States)',
|
|
1193 while ENC means `English (Canadian)'.
|
|
1194
|
|
1195 If the optional argument LONGFORM is non-nil, the long form of the locale
|
|
1196 name is returned, e.g. `English (United States)' instead.
|
|
1197
|
|
1198 If LCID (a 16-bit number) is not a valid locale, the result is nil.
|
|
1199 */
|
|
1200 (lcid, longform))
|
|
1201 {
|
|
1202 int got_abbrev;
|
|
1203 int got_full;
|
|
1204 char abbrev_name[32] = { 0 };
|
|
1205 char full_name[256] = { 0 };
|
|
1206
|
|
1207 CHECK_INT (lcid);
|
|
1208
|
|
1209 if (!IsValidLocale (XINT (lcid), LCID_SUPPORTED))
|
|
1210 return Qnil;
|
|
1211
|
|
1212 if (NILP (longform))
|
|
1213 {
|
|
1214 got_abbrev = GetLocaleInfo (XINT (lcid),
|
|
1215 LOCALE_SABBREVLANGNAME | LOCALE_USE_CP_ACP,
|
|
1216 abbrev_name, sizeof (abbrev_name));
|
|
1217 if (got_abbrev)
|
|
1218 return build_string (abbrev_name);
|
|
1219 }
|
|
1220 else
|
|
1221 {
|
|
1222 got_full = GetLocaleInfo (XINT (lcid),
|
|
1223 LOCALE_SLANGUAGE | LOCALE_USE_CP_ACP,
|
|
1224 full_name, sizeof (full_name));
|
|
1225 if (got_full)
|
|
1226 return build_string (full_name);
|
|
1227 }
|
|
1228
|
|
1229 return Qnil;
|
|
1230 }
|
|
1231
|
|
1232
|
|
1233 DEFUN ("win32-get-current-locale-id", Fwin32_get_current_locale_id, 0, 0, "", /*
|
|
1234 "Return Windows locale id for current locale setting.
|
|
1235 This is a numerical value; use `win32-get-locale-info' to convert to a
|
|
1236 human-readable form.
|
|
1237 */
|
|
1238 ())
|
|
1239 {
|
|
1240 return make_int (GetThreadLocale ());
|
|
1241 }
|
|
1242
|
|
1243
|
|
1244 DEFUN ("win32-get-default-locale-id", Fwin32_get_default_locale_id, 0, 1, "", /*
|
|
1245 "Return Windows locale id for default locale setting.
|
|
1246 By default, the system default locale setting is returned; if the optional
|
|
1247 parameter USERP is non-nil, the user default locale setting is returned.
|
|
1248 This is a numerical value; use `win32-get-locale-info' to convert to a
|
|
1249 human-readable form.
|
|
1250 */
|
|
1251 (userp))
|
|
1252 {
|
|
1253 if (NILP (userp))
|
|
1254 return make_int (GetSystemDefaultLCID ());
|
|
1255 return make_int (GetUserDefaultLCID ());
|
|
1256 }
|
|
1257
|
|
1258 DWORD int_from_hex (char * s)
|
|
1259 {
|
|
1260 DWORD val = 0;
|
|
1261 static char hex[] = "0123456789abcdefABCDEF";
|
|
1262 char * p;
|
|
1263
|
|
1264 while (*s && (p = strchr(hex, *s)) != NULL)
|
|
1265 {
|
|
1266 unsigned digit = p - hex;
|
|
1267 if (digit > 15)
|
|
1268 digit -= 6;
|
|
1269 val = val * 16 + digit;
|
|
1270 s++;
|
|
1271 }
|
|
1272 return val;
|
|
1273 }
|
|
1274
|
|
1275 /* We need to build a global list, since the EnumSystemLocale callback
|
|
1276 function isn't given a context pointer. */
|
|
1277 Lisp_Object Vwin32_valid_locale_ids;
|
|
1278
|
|
1279 BOOL CALLBACK enum_locale_fn (LPTSTR localeNum)
|
|
1280 {
|
|
1281 DWORD id = int_from_hex (localeNum);
|
|
1282 Vwin32_valid_locale_ids = Fcons (make_int (id), Vwin32_valid_locale_ids);
|
|
1283 return TRUE;
|
|
1284 }
|
|
1285
|
|
1286 DEFUN ("win32-get-valid-locale-ids", Fwin32_get_valid_locale_ids, 0, 0, "", /*
|
|
1287 Return list of all valid Windows locale ids.
|
|
1288 Each id is a numerical value; use `win32-get-locale-info' to convert to a
|
|
1289 human-readable form.
|
|
1290 */
|
|
1291 ())
|
|
1292 {
|
|
1293 Vwin32_valid_locale_ids = Qnil;
|
|
1294
|
|
1295 EnumSystemLocales (enum_locale_fn, LCID_SUPPORTED);
|
|
1296
|
|
1297 Vwin32_valid_locale_ids = Fnreverse (Vwin32_valid_locale_ids);
|
|
1298 return Vwin32_valid_locale_ids;
|
|
1299 }
|
|
1300
|
|
1301
|
|
1302 DEFUN ("win32-set-current-locale", Fwin32_set_current_locale, 1, 1, "", /*
|
|
1303 Make Windows locale LCID be the current locale setting for Emacs.
|
|
1304 If successful, the new locale id is returned, otherwise nil.
|
|
1305 */
|
|
1306 (lcid))
|
|
1307 {
|
|
1308 CHECK_INT (lcid);
|
|
1309
|
|
1310 if (!IsValidLocale (XINT (lcid), LCID_SUPPORTED))
|
|
1311 return Qnil;
|
|
1312
|
|
1313 if (!SetThreadLocale (XINT (lcid)))
|
|
1314 return Qnil;
|
|
1315
|
|
1316 /* Sync with FSF Emacs 19.34.6 note: dwWinThreadId declared in
|
|
1317 w32term.h and defined in w32fns.c, both of which are not in current
|
|
1318 XEmacs. ### Check what we lose by ifdef'ing out these. --marcpa */
|
|
1319 #if 0
|
|
1320 /* Need to set input thread locale if present. */
|
|
1321 if (dwWinThreadId)
|
|
1322 /* Reply is not needed. */
|
|
1323 PostThreadMessage (dwWinThreadId, WM_EMACS_SETLOCALE, XINT (lcid), 0);
|
|
1324 #endif
|
|
1325
|
|
1326 return make_int (GetThreadLocale ());
|
|
1327 }
|
|
1328
|
|
1329
|
288
|
1330 void
|
100
|
1331 syms_of_ntproc ()
|
|
1332 {
|
209
|
1333 DEFSUBR (Fwin32_short_file_name);
|
|
1334 DEFSUBR (Fwin32_long_file_name);
|
|
1335 DEFSUBR (Fwin32_set_process_priority);
|
|
1336 DEFSUBR (Fwin32_get_locale_info);
|
|
1337 DEFSUBR (Fwin32_get_current_locale_id);
|
|
1338 DEFSUBR (Fwin32_get_default_locale_id);
|
|
1339 DEFSUBR (Fwin32_get_valid_locale_ids);
|
|
1340 DEFSUBR (Fwin32_set_current_locale);
|
347
|
1341 }
|
|
1342
|
|
1343
|
|
1344 void
|
|
1345 vars_of_ntproc (void)
|
|
1346 {
|
|
1347 Qhigh = intern ("high");
|
|
1348 Qlow = intern ("low");
|
100
|
1349
|
|
1350 DEFVAR_LISP ("win32-quote-process-args", &Vwin32_quote_process_args /*
|
209
|
1351 Non-nil enables quoting of process arguments to ensure correct parsing.
|
100
|
1352 Because Windows does not directly pass argv arrays to child processes,
|
|
1353 programs have to reconstruct the argv array by parsing the command
|
|
1354 line string. For an argument to contain a space, it must be enclosed
|
|
1355 in double quotes or it will be parsed as multiple arguments.
|
|
1356
|
209
|
1357 If the value is a character, that character will be used to escape any
|
|
1358 quote characters that appear, otherwise a suitable escape character
|
|
1359 will be chosen based on the type of the program.
|
|
1360 */ );
|
|
1361 Vwin32_quote_process_args = Qt;
|
|
1362
|
|
1363 DEFVAR_LISP ("win32-start-process-show-window",
|
|
1364 &Vwin32_start_process_show_window /*
|
|
1365 When nil, processes started via start-process hide their windows.
|
|
1366 When non-nil, they show their window in the method of their choice.
|
|
1367 */ );
|
|
1368 Vwin32_start_process_show_window = Qnil;
|
|
1369
|
|
1370 DEFVAR_LISP ("win32-start-process-share-console",
|
|
1371 &Vwin32_start_process_share_console /*
|
|
1372 When nil, processes started via start-process are given a new console.
|
|
1373 When non-nil, they share the Emacs console; this has the limitation of
|
|
1374 allowing only only DOS subprocess to run at a time (whether started directly
|
|
1375 or indirectly by Emacs), and preventing Emacs from cleanly terminating the
|
|
1376 subprocess group, but may allow Emacs to interrupt a subprocess that doesn't
|
|
1377 otherwise respond to interrupts from Emacs.
|
|
1378 */ );
|
347
|
1379 Vwin32_start_process_share_console = Qt;
|
100
|
1380
|
282
|
1381 DEFVAR_LISP ("win32-pipe-read-delay", &Vwin32_pipe_read_delay /*
|
209
|
1382 Forced delay before reading subprocess output.
|
100
|
1383 This is done to improve the buffering of subprocess output, by
|
|
1384 avoiding the inefficiency of frequently reading small amounts of data.
|
|
1385
|
|
1386 If positive, the value is the number of milliseconds to sleep before
|
|
1387 reading the subprocess output. If negative, the magnitude is the number
|
|
1388 of time slices to wait (effectively boosting the priority of the child
|
209
|
1389 process temporarily). A value of zero disables waiting entirely.
|
|
1390 */ );
|
282
|
1391 Vwin32_pipe_read_delay = make_int (50);
|
100
|
1392
|
209
|
1393 #if 0
|
|
1394 DEFVAR_LISP ("win32-generate-fake-inodes", &Vwin32_generate_fake_inodes /*
|
|
1395 "Non-nil means attempt to fake realistic inode values.
|
|
1396 This works by hashing the truename of files, and should detect
|
|
1397 aliasing between long and short (8.3 DOS) names, but can have
|
|
1398 false positives because of hash collisions. Note that determing
|
|
1399 the truename of a file can be slow.
|
|
1400 */ );
|
|
1401 Vwin32_generate_fake_inodes = Qnil;
|
|
1402 #endif
|
100
|
1403 }
|
347
|
1404
|
100
|
1405 /* end of ntproc.c */
|