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
|
100
|
386 /* Hack for Windows 95, which assigns large (ie negative) pids */
|
|
387 if (cp->pid < 0)
|
|
388 cp->pid = -cp->pid;
|
|
389
|
|
390 /* pid must fit in a Lisp_Int */
|
282
|
391 #ifdef USE_UNION_TYPE
|
|
392 cp->pid = (cp->pid & ((1U << VALBITS) - 1));
|
|
393 #else
|
100
|
394 cp->pid = (cp->pid & VALMASK);
|
282
|
395 #endif
|
100
|
396
|
|
397 *pPid = cp->pid;
|
|
398
|
|
399 return TRUE;
|
|
400
|
|
401 EH_Fail:
|
|
402 DebPrint (("create_child.CreateProcess failed: %ld\n", GetLastError()););
|
|
403 return FALSE;
|
|
404 }
|
|
405
|
209
|
406 void
|
|
407 win32_executable_type (char * filename, int * is_dos_app, int * is_cygnus_app)
|
100
|
408 {
|
209
|
409 file_data executable;
|
|
410 char * p;
|
100
|
411
|
209
|
412 /* Default values in case we can't tell for sure. */
|
|
413 *is_dos_app = FALSE;
|
|
414 *is_cygnus_app = FALSE;
|
|
415
|
|
416 if (!open_input_file (&executable, filename))
|
|
417 return;
|
|
418
|
|
419 p = strrchr (filename, '.');
|
100
|
420
|
|
421 /* We can only identify DOS .com programs from the extension. */
|
|
422 if (p && stricmp (p, ".com") == 0)
|
209
|
423 *is_dos_app = TRUE;
|
|
424 else if (p && (stricmp (p, ".bat") == 0 ||
|
|
425 stricmp (p, ".cmd") == 0))
|
|
426 {
|
|
427 /* A DOS shell script - it appears that CreateProcess is happy to
|
|
428 accept this (somewhat surprisingly); presumably it looks at
|
|
429 COMSPEC to determine what executable to actually invoke.
|
100
|
430 Therefore, we have to do the same here as well. */
|
209
|
431 /* Actually, I think it uses the program association for that
|
|
432 extension, which is defined in the registry. */
|
|
433 p = egetenv ("COMSPEC");
|
100
|
434 if (p)
|
209
|
435 win32_executable_type (p, is_dos_app, is_cygnus_app);
|
100
|
436 }
|
|
437 else
|
|
438 {
|
209
|
439 /* Look for DOS .exe signature - if found, we must also check that
|
|
440 it isn't really a 16- or 32-bit Windows exe, since both formats
|
|
441 start with a DOS program stub. Note that 16-bit Windows
|
|
442 executables use the OS/2 1.x format. */
|
|
443
|
|
444 IMAGE_DOS_HEADER * dos_header;
|
|
445 IMAGE_NT_HEADERS * nt_header;
|
|
446
|
|
447 dos_header = (PIMAGE_DOS_HEADER) executable.file_base;
|
|
448 if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
|
|
449 goto unwind;
|
|
450
|
|
451 nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);
|
|
452
|
|
453 if ((char *) nt_header > (char *) dos_header + executable.size)
|
|
454 {
|
|
455 /* Some dos headers (pkunzip) have bogus e_lfanew fields. */
|
|
456 *is_dos_app = TRUE;
|
|
457 }
|
|
458 else if (nt_header->Signature != IMAGE_NT_SIGNATURE &&
|
|
459 LOWORD (nt_header->Signature) != IMAGE_OS2_SIGNATURE)
|
|
460 {
|
|
461 *is_dos_app = TRUE;
|
|
462 }
|
|
463 else if (nt_header->Signature == IMAGE_NT_SIGNATURE)
|
|
464 {
|
|
465 /* Look for cygwin.dll in DLL import list. */
|
|
466 IMAGE_DATA_DIRECTORY import_dir =
|
|
467 nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
|
|
468 IMAGE_IMPORT_DESCRIPTOR * imports;
|
|
469 IMAGE_SECTION_HEADER * section;
|
|
470
|
|
471 section = rva_to_section (import_dir.VirtualAddress, nt_header);
|
|
472 imports = RVA_TO_PTR (import_dir.VirtualAddress, section, executable);
|
|
473
|
|
474 for ( ; imports->Name; imports++)
|
100
|
475 {
|
209
|
476 char * dllname = RVA_TO_PTR (imports->Name, section, executable);
|
|
477
|
|
478 if (strcmp (dllname, "cygwin.dll") == 0)
|
|
479 {
|
|
480 *is_cygnus_app = TRUE;
|
|
481 break;
|
|
482 }
|
100
|
483 }
|
|
484 }
|
209
|
485 }
|
|
486
|
|
487 unwind:
|
|
488 close_file_data (&executable);
|
|
489 }
|
|
490
|
|
491 int
|
|
492 compare_env (const char **strp1, const char **strp2)
|
|
493 {
|
|
494 const char *str1 = *strp1, *str2 = *strp2;
|
|
495
|
|
496 while (*str1 && *str2 && *str1 != '=' && *str2 != '=')
|
|
497 {
|
|
498 if ((*str1) > (*str2))
|
|
499 return 1;
|
|
500 else if ((*str1) < (*str2))
|
|
501 return -1;
|
|
502 str1++, str2++;
|
100
|
503 }
|
|
504
|
209
|
505 if (*str1 == '=' && *str2 == '=')
|
|
506 return 0;
|
|
507 else if (*str1 == '=')
|
|
508 return -1;
|
|
509 else
|
|
510 return 1;
|
100
|
511 }
|
|
512
|
209
|
513 void
|
|
514 merge_and_sort_env (char **envp1, char **envp2, char **new_envp)
|
|
515 {
|
|
516 char **optr, **nptr;
|
|
517 int num;
|
|
518
|
|
519 nptr = new_envp;
|
|
520 optr = envp1;
|
|
521 while (*optr)
|
|
522 *nptr++ = *optr++;
|
|
523 num = optr - envp1;
|
|
524
|
|
525 optr = envp2;
|
|
526 while (*optr)
|
|
527 *nptr++ = *optr++;
|
|
528 num += optr - envp2;
|
|
529
|
|
530 qsort (new_envp, num, sizeof (char *), compare_env);
|
|
531
|
|
532 *nptr = NULL;
|
|
533 }
|
100
|
534
|
|
535 /* When a new child process is created we need to register it in our list,
|
|
536 so intercept spawn requests. */
|
|
537 int
|
239
|
538 sys_spawnve (int mode, CONST char *cmdname,
|
|
539 CONST char * CONST *argv, CONST char *CONST *envp)
|
100
|
540 {
|
|
541 Lisp_Object program, full;
|
|
542 char *cmdline, *env, *parg, **targ;
|
209
|
543 int arglen, numenv;
|
100
|
544 int pid;
|
|
545 child_process *cp;
|
209
|
546 int is_dos_app, is_cygnus_app;
|
|
547 int do_quoting = 0;
|
|
548 char escape_char;
|
|
549 /* We pass our process ID to our children by setting up an environment
|
|
550 variable in their environment. */
|
|
551 char ppid_env_var_buffer[64];
|
|
552 char *extra_env[] = {ppid_env_var_buffer, NULL};
|
124
|
553 struct gcpro gcpro1;
|
|
554
|
100
|
555 /* We don't care about the other modes */
|
|
556 if (mode != _P_NOWAIT)
|
|
557 {
|
|
558 errno = EINVAL;
|
|
559 return -1;
|
|
560 }
|
|
561
|
|
562 /* Handle executable names without an executable suffix. */
|
|
563 program = make_string (cmdname, strlen (cmdname));
|
124
|
564 GCPRO1 (program);
|
100
|
565 if (NILP (Ffile_executable_p (program)))
|
|
566 {
|
|
567 full = Qnil;
|
|
568 locate_file (Vexec_path, program, EXEC_SUFFIXES, &full, 1);
|
|
569 if (NILP (full))
|
|
570 {
|
124
|
571 UNGCPRO;
|
100
|
572 errno = EINVAL;
|
|
573 return -1;
|
|
574 }
|
347
|
575 GET_C_STRING_FILENAME_DATA_ALLOCA (full, cmdname);
|
|
576 }
|
|
577 else
|
|
578 {
|
|
579 (char*)cmdname = alloca (strlen (argv[0]) + 1);
|
|
580 strcpy ((char*)cmdname, argv[0]);
|
100
|
581 }
|
124
|
582 UNGCPRO;
|
|
583
|
209
|
584 /* make sure argv[0] and cmdname are both in DOS format */
|
100
|
585 unixtodos_filename (cmdname);
|
239
|
586 /* #### KLUDGE */
|
347
|
587 ((CONST char**)argv)[0] = cmdname;
|
100
|
588
|
209
|
589 /* Determine whether program is a 16-bit DOS executable, or a Win32
|
|
590 executable that is implicitly linked to the Cygnus dll (implying it
|
|
591 was compiled with the Cygnus GNU toolchain and hence relies on
|
|
592 cygwin.dll to parse the command line - we use this to decide how to
|
|
593 escape quote chars in command line args that must be quoted). */
|
|
594 win32_executable_type (cmdname, &is_dos_app, &is_cygnus_app);
|
|
595
|
|
596 /* On Windows 95, if cmdname is a DOS app, we invoke a helper
|
|
597 application to start it by specifying the helper app as cmdname,
|
|
598 while leaving the real app name as argv[0]. */
|
|
599 if (is_dos_app)
|
100
|
600 {
|
209
|
601 cmdname = alloca (MAXPATHLEN);
|
|
602 if (egetenv ("CMDPROXY"))
|
|
603 strcpy (cmdname, egetenv ("CMDPROXY"));
|
|
604 else
|
|
605 {
|
|
606 strcpy (cmdname, XSTRING_DATA (Vinvocation_directory));
|
|
607 strcat (cmdname, "cmdproxy.exe");
|
|
608 }
|
|
609 unixtodos_filename (cmdname);
|
100
|
610 }
|
|
611
|
|
612 /* we have to do some conjuring here to put argv and envp into the
|
|
613 form CreateProcess wants... argv needs to be a space separated/null
|
|
614 terminated list of parameters, and envp is a null
|
|
615 separated/double-null terminated list of parameters.
|
|
616
|
209
|
617 Additionally, zero-length args and args containing whitespace or
|
|
618 quote chars need to be wrapped in double quotes - for this to work,
|
|
619 embedded quotes need to be escaped as well. The aim is to ensure
|
|
620 the child process reconstructs the argv array we start with
|
|
621 exactly, so we treat quotes at the beginning and end of arguments
|
|
622 as embedded quotes.
|
|
623
|
|
624 The Win32 GNU-based library from Cygnus doubles quotes to escape
|
|
625 them, while MSVC uses backslash for escaping. (Actually the MSVC
|
|
626 startup code does attempt to recognise doubled quotes and accept
|
|
627 them, but gets it wrong and ends up requiring three quotes to get a
|
|
628 single embedded quote!) So by default we decide whether to use
|
|
629 quote or backslash as the escape character based on whether the
|
|
630 binary is apparently a Cygnus compiled app.
|
|
631
|
|
632 Note that using backslash to escape embedded quotes requires
|
|
633 additional special handling if an embedded quote is already
|
|
634 preceeded by backslash, or if an arg requiring quoting ends with
|
|
635 backslash. In such cases, the run of escape characters needs to be
|
|
636 doubled. For consistency, we apply this special handling as long
|
|
637 as the escape character is not quote.
|
100
|
638
|
209
|
639 Since we have no idea how large argv and envp are likely to be we
|
|
640 figure out list lengths on the fly and allocate them. */
|
|
641
|
|
642 if (!NILP (Vwin32_quote_process_args))
|
|
643 {
|
|
644 do_quoting = 1;
|
|
645 /* Override escape char by binding win32-quote-process-args to
|
|
646 desired character, or use t for auto-selection. */
|
|
647 if (INTP (Vwin32_quote_process_args))
|
|
648 escape_char = XINT (Vwin32_quote_process_args);
|
|
649 else
|
|
650 escape_char = is_cygnus_app ? '"' : '\\';
|
|
651 }
|
100
|
652
|
|
653 /* do argv... */
|
|
654 arglen = 0;
|
|
655 targ = argv;
|
|
656 while (*targ)
|
|
657 {
|
|
658 char * p = *targ;
|
209
|
659 int need_quotes = 0;
|
|
660 int escape_char_run = 0;
|
100
|
661
|
|
662 if (*p == 0)
|
209
|
663 need_quotes = 1;
|
|
664 for ( ; *p; p++)
|
|
665 {
|
|
666 if (*p == '"')
|
100
|
667 {
|
209
|
668 /* allow for embedded quotes to be escaped */
|
100
|
669 arglen++;
|
209
|
670 need_quotes = 1;
|
|
671 /* handle the case where the embedded quote is already escaped */
|
|
672 if (escape_char_run > 0)
|
|
673 {
|
|
674 /* To preserve the arg exactly, we need to double the
|
|
675 preceding escape characters (plus adding one to
|
|
676 escape the quote character itself). */
|
|
677 arglen += escape_char_run;
|
100
|
678 }
|
209
|
679 }
|
100
|
680 else if (*p == ' ' || *p == '\t')
|
209
|
681 {
|
|
682 need_quotes = 1;
|
|
683 }
|
|
684
|
|
685 if (*p == escape_char && escape_char != '"')
|
|
686 escape_char_run++;
|
|
687 else
|
|
688 escape_char_run = 0;
|
|
689 }
|
|
690 if (need_quotes)
|
|
691 {
|
100
|
692 arglen += 2;
|
209
|
693 /* handle the case where the arg ends with an escape char - we
|
|
694 must not let the enclosing quote be escaped. */
|
|
695 if (escape_char_run > 0)
|
|
696 arglen += escape_char_run;
|
|
697 }
|
100
|
698 arglen += strlen (*targ++) + 1;
|
|
699 }
|
|
700 cmdline = alloca (arglen);
|
|
701 targ = argv;
|
|
702 parg = cmdline;
|
|
703 while (*targ)
|
|
704 {
|
|
705 char * p = *targ;
|
209
|
706 int need_quotes = 0;
|
100
|
707
|
|
708 if (*p == 0)
|
209
|
709 need_quotes = 1;
|
100
|
710
|
209
|
711 if (do_quoting)
|
100
|
712 {
|
|
713 for ( ; *p; p++)
|
|
714 if (*p == ' ' || *p == '\t' || *p == '"')
|
209
|
715 need_quotes = 1;
|
100
|
716 }
|
209
|
717 if (need_quotes)
|
100
|
718 {
|
209
|
719 int escape_char_run = 0;
|
100
|
720 char * first;
|
|
721 char * last;
|
|
722
|
|
723 p = *targ;
|
|
724 first = p;
|
|
725 last = p + strlen (p) - 1;
|
|
726 *parg++ = '"';
|
209
|
727 #if 0
|
|
728 /* This version does not escape quotes if they occur at the
|
|
729 beginning or end of the arg - this could lead to incorrect
|
|
730 behaviour when the arg itself represents a command line
|
|
731 containing quoted args. I believe this was originally done
|
|
732 as a hack to make some things work, before
|
|
733 `win32-quote-process-args' was added. */
|
100
|
734 while (*p)
|
|
735 {
|
|
736 if (*p == '"' && p > first && p < last)
|
209
|
737 *parg++ = escape_char; /* escape embedded quotes */
|
100
|
738 *parg++ = *p++;
|
|
739 }
|
209
|
740 #else
|
|
741 for ( ; *p; p++)
|
|
742 {
|
|
743 if (*p == '"')
|
|
744 {
|
|
745 /* double preceding escape chars if any */
|
|
746 while (escape_char_run > 0)
|
|
747 {
|
|
748 *parg++ = escape_char;
|
|
749 escape_char_run--;
|
|
750 }
|
|
751 /* escape all quote chars, even at beginning or end */
|
|
752 *parg++ = escape_char;
|
|
753 }
|
|
754 *parg++ = *p;
|
|
755
|
|
756 if (*p == escape_char && escape_char != '"')
|
|
757 escape_char_run++;
|
|
758 else
|
|
759 escape_char_run = 0;
|
|
760 }
|
|
761 /* double escape chars before enclosing quote */
|
|
762 while (escape_char_run > 0)
|
|
763 {
|
|
764 *parg++ = escape_char;
|
|
765 escape_char_run--;
|
|
766 }
|
|
767 #endif
|
100
|
768 *parg++ = '"';
|
|
769 }
|
|
770 else
|
|
771 {
|
|
772 strcpy (parg, *targ);
|
|
773 parg += strlen (*targ);
|
|
774 }
|
|
775 *parg++ = ' ';
|
|
776 targ++;
|
|
777 }
|
|
778 *--parg = '\0';
|
|
779
|
|
780 /* and envp... */
|
|
781 arglen = 1;
|
|
782 targ = envp;
|
209
|
783 numenv = 1; /* for end null */
|
100
|
784 while (*targ)
|
|
785 {
|
|
786 arglen += strlen (*targ++) + 1;
|
209
|
787 numenv++;
|
100
|
788 }
|
209
|
789 /* extra env vars... */
|
100
|
790 sprintf (ppid_env_var_buffer, "__PARENT_PROCESS_ID=%d",
|
|
791 GetCurrentProcessId ());
|
|
792 arglen += strlen (ppid_env_var_buffer) + 1;
|
209
|
793 numenv++;
|
100
|
794
|
209
|
795 /* merge env passed in and extra env into one, and sort it. */
|
|
796 targ = (char **) alloca (numenv * sizeof (char *));
|
|
797 merge_and_sort_env (envp, extra_env, targ);
|
|
798
|
|
799 /* concatenate env entries. */
|
100
|
800 env = alloca (arglen);
|
|
801 parg = env;
|
|
802 while (*targ)
|
|
803 {
|
|
804 strcpy (parg, *targ);
|
|
805 parg += strlen (*targ++);
|
|
806 *parg++ = '\0';
|
|
807 }
|
|
808 *parg++ = '\0';
|
|
809 *parg = '\0';
|
|
810
|
|
811 cp = new_child ();
|
|
812 if (cp == NULL)
|
|
813 {
|
|
814 errno = EAGAIN;
|
|
815 return -1;
|
|
816 }
|
|
817
|
|
818 /* Now create the process. */
|
|
819 if (!create_child (cmdname, cmdline, env, &pid, cp))
|
|
820 {
|
|
821 delete_child (cp);
|
|
822 errno = ENOEXEC;
|
|
823 return -1;
|
|
824 }
|
|
825
|
|
826 return pid;
|
|
827 }
|
|
828
|
|
829 /* Substitute for certain kill () operations */
|
209
|
830
|
|
831 static BOOL CALLBACK
|
|
832 find_child_console (HWND hwnd, child_process * cp)
|
|
833 {
|
|
834 DWORD thread_id;
|
|
835 DWORD process_id;
|
|
836
|
|
837 thread_id = GetWindowThreadProcessId (hwnd, &process_id);
|
|
838 if (process_id == cp->procinfo.dwProcessId)
|
|
839 {
|
|
840 char window_class[32];
|
|
841
|
|
842 GetClassName (hwnd, window_class, sizeof (window_class));
|
|
843 if (strcmp (window_class,
|
|
844 (os_subtype == OS_WIN95)
|
|
845 ? "tty"
|
|
846 : "ConsoleWindowClass") == 0)
|
|
847 {
|
|
848 cp->hwnd = hwnd;
|
|
849 return FALSE;
|
|
850 }
|
|
851 }
|
|
852 /* keep looking */
|
|
853 return TRUE;
|
|
854 }
|
|
855
|
100
|
856 int
|
|
857 sys_kill (int pid, int sig)
|
|
858 {
|
|
859 child_process *cp;
|
|
860 HANDLE proc_hand;
|
|
861 int need_to_free = 0;
|
|
862 int rc = 0;
|
|
863
|
|
864 /* Only handle signals that will result in the process dying */
|
|
865 if (sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP)
|
|
866 {
|
|
867 errno = EINVAL;
|
|
868 return -1;
|
|
869 }
|
|
870
|
|
871 cp = find_child_pid (pid);
|
|
872 if (cp == NULL)
|
|
873 {
|
|
874 proc_hand = OpenProcess (PROCESS_TERMINATE, 0, pid);
|
|
875 if (proc_hand == NULL)
|
|
876 {
|
|
877 errno = EPERM;
|
|
878 return -1;
|
|
879 }
|
|
880 need_to_free = 1;
|
|
881 }
|
|
882 else
|
|
883 {
|
|
884 proc_hand = cp->procinfo.hProcess;
|
|
885 pid = cp->procinfo.dwProcessId;
|
209
|
886
|
|
887 /* Try to locate console window for process. */
|
|
888 EnumWindows (find_child_console, (LPARAM) cp);
|
100
|
889 }
|
|
890
|
|
891 if (sig == SIGINT)
|
|
892 {
|
209
|
893 if (NILP (Vwin32_start_process_share_console) && cp && cp->hwnd)
|
|
894 {
|
|
895 BYTE control_scan_code = (BYTE) MapVirtualKey (VK_CONTROL, 0);
|
|
896 BYTE vk_break_code = VK_CANCEL;
|
|
897 BYTE break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
|
|
898 HWND foreground_window;
|
|
899
|
|
900 if (break_scan_code == 0)
|
|
901 {
|
|
902 /* Fake Ctrl-C if we can't manage Ctrl-Break. */
|
|
903 vk_break_code = 'C';
|
|
904 break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
|
|
905 }
|
|
906
|
|
907 foreground_window = GetForegroundWindow ();
|
|
908 if (foreground_window && SetForegroundWindow (cp->hwnd))
|
|
909 {
|
|
910 /* Generate keystrokes as if user had typed Ctrl-Break or Ctrl-C. */
|
|
911 keybd_event (VK_CONTROL, control_scan_code, 0, 0);
|
|
912 keybd_event (vk_break_code, break_scan_code, 0, 0);
|
|
913 keybd_event (vk_break_code, break_scan_code, KEYEVENTF_KEYUP, 0);
|
|
914 keybd_event (VK_CONTROL, control_scan_code, KEYEVENTF_KEYUP, 0);
|
|
915
|
|
916 /* Sleep for a bit to give time for Emacs frame to respond
|
|
917 to focus change events (if Emacs was active app). */
|
|
918 Sleep (10);
|
|
919
|
|
920 SetForegroundWindow (foreground_window);
|
|
921 }
|
|
922 }
|
100
|
923 /* Ctrl-Break is NT equivalent of SIGINT. */
|
209
|
924 else if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, pid))
|
100
|
925 {
|
|
926 DebPrint (("sys_kill.GenerateConsoleCtrlEvent return %d "
|
|
927 "for pid %lu\n", GetLastError (), pid));
|
|
928 errno = EINVAL;
|
|
929 rc = -1;
|
|
930 }
|
|
931 }
|
|
932 else
|
|
933 {
|
209
|
934 if (NILP (Vwin32_start_process_share_console) && cp && cp->hwnd)
|
|
935 {
|
|
936 #if 1
|
|
937 if (os_subtype == OS_WIN95)
|
|
938 {
|
|
939 /*
|
|
940 Another possibility is to try terminating the VDM out-right by
|
|
941 calling the Shell VxD (id 0x17) V86 interface, function #4
|
|
942 "SHELL_Destroy_VM", ie.
|
|
943
|
|
944 mov edx,4
|
|
945 mov ebx,vm_handle
|
|
946 call shellapi
|
|
947
|
|
948 First need to determine the current VM handle, and then arrange for
|
|
949 the shellapi call to be made from the system vm (by using
|
|
950 Switch_VM_and_callback).
|
|
951
|
|
952 Could try to invoke DestroyVM through CallVxD.
|
|
953
|
|
954 */
|
|
955 #if 0
|
|
956 /* On Win95, posting WM_QUIT causes the 16-bit subsystem
|
|
957 to hang when cmdproxy is used in conjunction with
|
|
958 command.com for an interactive shell. Posting
|
|
959 WM_CLOSE pops up a dialog that, when Yes is selected,
|
|
960 does the same thing. TerminateProcess is also less
|
|
961 than ideal in that subprocesses tend to stick around
|
|
962 until the machine is shutdown, but at least it
|
|
963 doesn't freeze the 16-bit subsystem. */
|
|
964 PostMessage (cp->hwnd, WM_QUIT, 0xff, 0);
|
|
965 #endif
|
|
966 if (!TerminateProcess (proc_hand, 0xff))
|
|
967 {
|
|
968 DebPrint (("sys_kill.TerminateProcess returned %d "
|
|
969 "for pid %lu\n", GetLastError (), pid));
|
|
970 errno = EINVAL;
|
|
971 rc = -1;
|
|
972 }
|
|
973 }
|
|
974 else
|
|
975 #endif
|
|
976 PostMessage (cp->hwnd, WM_CLOSE, 0, 0);
|
|
977 }
|
100
|
978 /* Kill the process. On Win32 this doesn't kill child processes
|
|
979 so it doesn't work very well for shells which is why it's not
|
209
|
980 used in every case. */
|
|
981 else if (!TerminateProcess (proc_hand, 0xff))
|
100
|
982 {
|
|
983 DebPrint (("sys_kill.TerminateProcess returned %d "
|
|
984 "for pid %lu\n", GetLastError (), pid));
|
|
985 errno = EINVAL;
|
|
986 rc = -1;
|
|
987 }
|
|
988 }
|
|
989
|
|
990 if (need_to_free)
|
|
991 CloseHandle (proc_hand);
|
|
992
|
|
993 return rc;
|
|
994 }
|
|
995
|
|
996 #if 0
|
209
|
997 /* Sync with FSF Emacs 19.34.6 note: ifdef'ed out in XEmacs */
|
100
|
998 extern int report_file_error (CONST char *, Lisp_Object);
|
|
999 #endif
|
|
1000 /* The following two routines are used to manipulate stdin, stdout, and
|
|
1001 stderr of our child processes.
|
|
1002
|
|
1003 Assuming that in, out, and err are *not* inheritable, we make them
|
|
1004 stdin, stdout, and stderr of the child as follows:
|
|
1005
|
|
1006 - Save the parent's current standard handles.
|
|
1007 - Set the std handles to inheritable duplicates of the ones being passed in.
|
|
1008 (Note that _get_osfhandle() is an io.h procedure that retrieves the
|
|
1009 NT file handle for a crt file descriptor.)
|
|
1010 - Spawn the child, which inherits in, out, and err as stdin,
|
|
1011 stdout, and stderr. (see Spawnve)
|
|
1012 - Close the std handles passed to the child.
|
|
1013 - Reset the parent's standard handles to the saved handles.
|
|
1014 (see reset_standard_handles)
|
|
1015 We assume that the caller closes in, out, and err after calling us. */
|
|
1016
|
|
1017 void
|
|
1018 prepare_standard_handles (int in, int out, int err, HANDLE handles[3])
|
|
1019 {
|
|
1020 HANDLE parent;
|
|
1021 HANDLE newstdin, newstdout, newstderr;
|
|
1022
|
|
1023 parent = GetCurrentProcess ();
|
|
1024
|
|
1025 handles[0] = GetStdHandle (STD_INPUT_HANDLE);
|
|
1026 handles[1] = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
1027 handles[2] = GetStdHandle (STD_ERROR_HANDLE);
|
|
1028
|
|
1029 /* make inheritable copies of the new handles */
|
|
1030 if (!DuplicateHandle (parent,
|
|
1031 (HANDLE) _get_osfhandle (in),
|
|
1032 parent,
|
|
1033 &newstdin,
|
|
1034 0,
|
|
1035 TRUE,
|
|
1036 DUPLICATE_SAME_ACCESS))
|
|
1037 report_file_error ("Duplicating input handle for child", Qnil);
|
|
1038
|
|
1039 if (!DuplicateHandle (parent,
|
|
1040 (HANDLE) _get_osfhandle (out),
|
|
1041 parent,
|
|
1042 &newstdout,
|
|
1043 0,
|
|
1044 TRUE,
|
|
1045 DUPLICATE_SAME_ACCESS))
|
|
1046 report_file_error ("Duplicating output handle for child", Qnil);
|
|
1047
|
|
1048 if (!DuplicateHandle (parent,
|
|
1049 (HANDLE) _get_osfhandle (err),
|
|
1050 parent,
|
|
1051 &newstderr,
|
|
1052 0,
|
|
1053 TRUE,
|
|
1054 DUPLICATE_SAME_ACCESS))
|
|
1055 report_file_error ("Duplicating error handle for child", Qnil);
|
|
1056
|
|
1057 /* and store them as our std handles */
|
|
1058 if (!SetStdHandle (STD_INPUT_HANDLE, newstdin))
|
|
1059 report_file_error ("Changing stdin handle", Qnil);
|
|
1060
|
|
1061 if (!SetStdHandle (STD_OUTPUT_HANDLE, newstdout))
|
|
1062 report_file_error ("Changing stdout handle", Qnil);
|
|
1063
|
|
1064 if (!SetStdHandle (STD_ERROR_HANDLE, newstderr))
|
|
1065 report_file_error ("Changing stderr handle", Qnil);
|
|
1066 }
|
|
1067
|
|
1068 void
|
|
1069 reset_standard_handles (int in, int out, int err, HANDLE handles[3])
|
|
1070 {
|
|
1071 /* close the duplicated handles passed to the child */
|
|
1072 CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
|
|
1073 CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
|
|
1074 CloseHandle (GetStdHandle (STD_ERROR_HANDLE));
|
|
1075
|
|
1076 /* now restore parent's saved std handles */
|
|
1077 SetStdHandle (STD_INPUT_HANDLE, handles[0]);
|
|
1078 SetStdHandle (STD_OUTPUT_HANDLE, handles[1]);
|
|
1079 SetStdHandle (STD_ERROR_HANDLE, handles[2]);
|
|
1080 }
|
|
1081
|
209
|
1082 void
|
288
|
1083 set_process_dir (const char * dir)
|
209
|
1084 {
|
|
1085 process_dir = dir;
|
|
1086 }
|
100
|
1087
|
209
|
1088 /* Some miscellaneous functions that are Windows specific, but not GUI
|
|
1089 specific (ie. are applicable in terminal or batch mode as well). */
|
|
1090
|
|
1091 /* lifted from fileio.c */
|
|
1092 #define CORRECT_DIR_SEPS(s) \
|
|
1093 do { if ('/' == DIRECTORY_SEP) dostounix_filename (s); \
|
|
1094 else unixtodos_filename (s); \
|
|
1095 } while (0)
|
|
1096
|
|
1097 DEFUN ("win32-short-file-name", Fwin32_short_file_name, 1, 1, "", /*
|
|
1098 Return the short file name version (8.3) of the full path of FILENAME.
|
|
1099 If FILENAME does not exist, return nil.
|
|
1100 All path elements in FILENAME are converted to their short names.
|
|
1101 */
|
|
1102 (filename))
|
|
1103 {
|
|
1104 char shortname[MAX_PATH];
|
|
1105
|
288
|
1106 CHECK_STRING (filename);
|
209
|
1107
|
|
1108 /* first expand it. */
|
|
1109 filename = Fexpand_file_name (filename, Qnil);
|
|
1110
|
|
1111 /* luckily, this returns the short version of each element in the path. */
|
|
1112 if (GetShortPathName (XSTRING_DATA (filename), shortname, MAX_PATH) == 0)
|
|
1113 return Qnil;
|
|
1114
|
|
1115 CORRECT_DIR_SEPS (shortname);
|
|
1116
|
|
1117 return build_string (shortname);
|
|
1118 }
|
|
1119
|
|
1120
|
|
1121 DEFUN ("win32-long-file-name", Fwin32_long_file_name, 1, 1, "", /*
|
|
1122 Return the long file name version of the full path of FILENAME.
|
|
1123 If FILENAME does not exist, return nil.
|
|
1124 All path elements in FILENAME are converted to their long names.
|
|
1125 */
|
|
1126 (filename))
|
|
1127 {
|
|
1128 char longname[ MAX_PATH ];
|
|
1129
|
288
|
1130 CHECK_STRING (filename);
|
209
|
1131
|
|
1132 /* first expand it. */
|
|
1133 filename = Fexpand_file_name (filename, Qnil);
|
|
1134
|
|
1135 if (!win32_get_long_filename (XSTRING_DATA (filename), longname, MAX_PATH))
|
|
1136 return Qnil;
|
|
1137
|
|
1138 CORRECT_DIR_SEPS (longname);
|
|
1139
|
|
1140 return build_string (longname);
|
|
1141 }
|
|
1142
|
|
1143 DEFUN ("win32-set-process-priority", Fwin32_set_process_priority, 2, 2, "", /*
|
|
1144 Set the priority of PROCESS to PRIORITY.
|
|
1145 If PROCESS is nil, the priority of Emacs is changed, otherwise the
|
|
1146 priority of the process whose pid is PROCESS is changed.
|
|
1147 PRIORITY should be one of the symbols high, normal, or low;
|
|
1148 any other symbol will be interpreted as normal.
|
|
1149
|
|
1150 If successful, the return value is t, otherwise nil.
|
|
1151 */
|
|
1152 (process, priority))
|
|
1153 {
|
|
1154 HANDLE proc_handle = GetCurrentProcess ();
|
|
1155 DWORD priority_class = NORMAL_PRIORITY_CLASS;
|
|
1156 Lisp_Object result = Qnil;
|
|
1157
|
288
|
1158 CHECK_SYMBOL (priority);
|
209
|
1159
|
|
1160 if (!NILP (process))
|
|
1161 {
|
|
1162 DWORD pid;
|
|
1163 child_process *cp;
|
|
1164
|
|
1165 CHECK_INT (process);
|
|
1166
|
|
1167 /* Allow pid to be an internally generated one, or one obtained
|
|
1168 externally. This is necessary because real pids on Win95 are
|
|
1169 negative. */
|
|
1170
|
|
1171 pid = XINT (process);
|
|
1172 cp = find_child_pid (pid);
|
|
1173 if (cp != NULL)
|
|
1174 pid = cp->procinfo.dwProcessId;
|
|
1175
|
|
1176 proc_handle = OpenProcess (PROCESS_SET_INFORMATION, FALSE, pid);
|
|
1177 }
|
|
1178
|
|
1179 if (EQ (priority, Qhigh))
|
|
1180 priority_class = HIGH_PRIORITY_CLASS;
|
|
1181 else if (EQ (priority, Qlow))
|
|
1182 priority_class = IDLE_PRIORITY_CLASS;
|
|
1183
|
|
1184 if (proc_handle != NULL)
|
|
1185 {
|
|
1186 if (SetPriorityClass (proc_handle, priority_class))
|
|
1187 result = Qt;
|
|
1188 if (!NILP (process))
|
|
1189 CloseHandle (proc_handle);
|
|
1190 }
|
|
1191
|
|
1192 return result;
|
|
1193 }
|
|
1194
|
|
1195
|
|
1196 DEFUN ("win32-get-locale-info", Fwin32_get_locale_info, 1, 2, "", /*
|
|
1197 "Return information about the Windows locale LCID.
|
|
1198 By default, return a three letter locale code which encodes the default
|
|
1199 language as the first two characters, and the country or regionial variant
|
|
1200 as the third letter. For example, ENU refers to `English (United States)',
|
|
1201 while ENC means `English (Canadian)'.
|
|
1202
|
|
1203 If the optional argument LONGFORM is non-nil, the long form of the locale
|
|
1204 name is returned, e.g. `English (United States)' instead.
|
|
1205
|
|
1206 If LCID (a 16-bit number) is not a valid locale, the result is nil.
|
|
1207 */
|
|
1208 (lcid, longform))
|
|
1209 {
|
|
1210 int got_abbrev;
|
|
1211 int got_full;
|
|
1212 char abbrev_name[32] = { 0 };
|
|
1213 char full_name[256] = { 0 };
|
|
1214
|
|
1215 CHECK_INT (lcid);
|
|
1216
|
|
1217 if (!IsValidLocale (XINT (lcid), LCID_SUPPORTED))
|
|
1218 return Qnil;
|
|
1219
|
|
1220 if (NILP (longform))
|
|
1221 {
|
|
1222 got_abbrev = GetLocaleInfo (XINT (lcid),
|
|
1223 LOCALE_SABBREVLANGNAME | LOCALE_USE_CP_ACP,
|
|
1224 abbrev_name, sizeof (abbrev_name));
|
|
1225 if (got_abbrev)
|
|
1226 return build_string (abbrev_name);
|
|
1227 }
|
|
1228 else
|
|
1229 {
|
|
1230 got_full = GetLocaleInfo (XINT (lcid),
|
|
1231 LOCALE_SLANGUAGE | LOCALE_USE_CP_ACP,
|
|
1232 full_name, sizeof (full_name));
|
|
1233 if (got_full)
|
|
1234 return build_string (full_name);
|
|
1235 }
|
|
1236
|
|
1237 return Qnil;
|
|
1238 }
|
|
1239
|
|
1240
|
|
1241 DEFUN ("win32-get-current-locale-id", Fwin32_get_current_locale_id, 0, 0, "", /*
|
|
1242 "Return Windows locale id for current locale setting.
|
|
1243 This is a numerical value; use `win32-get-locale-info' to convert to a
|
|
1244 human-readable form.
|
|
1245 */
|
|
1246 ())
|
|
1247 {
|
|
1248 return make_int (GetThreadLocale ());
|
|
1249 }
|
|
1250
|
|
1251
|
|
1252 DEFUN ("win32-get-default-locale-id", Fwin32_get_default_locale_id, 0, 1, "", /*
|
|
1253 "Return Windows locale id for default locale setting.
|
|
1254 By default, the system default locale setting is returned; if the optional
|
|
1255 parameter USERP is non-nil, the user default locale setting is returned.
|
|
1256 This is a numerical value; use `win32-get-locale-info' to convert to a
|
|
1257 human-readable form.
|
|
1258 */
|
|
1259 (userp))
|
|
1260 {
|
|
1261 if (NILP (userp))
|
|
1262 return make_int (GetSystemDefaultLCID ());
|
|
1263 return make_int (GetUserDefaultLCID ());
|
|
1264 }
|
|
1265
|
|
1266 DWORD int_from_hex (char * s)
|
|
1267 {
|
|
1268 DWORD val = 0;
|
|
1269 static char hex[] = "0123456789abcdefABCDEF";
|
|
1270 char * p;
|
|
1271
|
|
1272 while (*s && (p = strchr(hex, *s)) != NULL)
|
|
1273 {
|
|
1274 unsigned digit = p - hex;
|
|
1275 if (digit > 15)
|
|
1276 digit -= 6;
|
|
1277 val = val * 16 + digit;
|
|
1278 s++;
|
|
1279 }
|
|
1280 return val;
|
|
1281 }
|
|
1282
|
|
1283 /* We need to build a global list, since the EnumSystemLocale callback
|
|
1284 function isn't given a context pointer. */
|
|
1285 Lisp_Object Vwin32_valid_locale_ids;
|
|
1286
|
|
1287 BOOL CALLBACK enum_locale_fn (LPTSTR localeNum)
|
|
1288 {
|
|
1289 DWORD id = int_from_hex (localeNum);
|
|
1290 Vwin32_valid_locale_ids = Fcons (make_int (id), Vwin32_valid_locale_ids);
|
|
1291 return TRUE;
|
|
1292 }
|
|
1293
|
|
1294 DEFUN ("win32-get-valid-locale-ids", Fwin32_get_valid_locale_ids, 0, 0, "", /*
|
|
1295 Return list of all valid Windows locale ids.
|
|
1296 Each id is a numerical value; use `win32-get-locale-info' to convert to a
|
|
1297 human-readable form.
|
|
1298 */
|
|
1299 ())
|
|
1300 {
|
|
1301 Vwin32_valid_locale_ids = Qnil;
|
|
1302
|
|
1303 EnumSystemLocales (enum_locale_fn, LCID_SUPPORTED);
|
|
1304
|
|
1305 Vwin32_valid_locale_ids = Fnreverse (Vwin32_valid_locale_ids);
|
|
1306 return Vwin32_valid_locale_ids;
|
|
1307 }
|
|
1308
|
|
1309
|
|
1310 DEFUN ("win32-set-current-locale", Fwin32_set_current_locale, 1, 1, "", /*
|
|
1311 Make Windows locale LCID be the current locale setting for Emacs.
|
|
1312 If successful, the new locale id is returned, otherwise nil.
|
|
1313 */
|
|
1314 (lcid))
|
|
1315 {
|
|
1316 CHECK_INT (lcid);
|
|
1317
|
|
1318 if (!IsValidLocale (XINT (lcid), LCID_SUPPORTED))
|
|
1319 return Qnil;
|
|
1320
|
|
1321 if (!SetThreadLocale (XINT (lcid)))
|
|
1322 return Qnil;
|
|
1323
|
|
1324 /* Sync with FSF Emacs 19.34.6 note: dwWinThreadId declared in
|
|
1325 w32term.h and defined in w32fns.c, both of which are not in current
|
|
1326 XEmacs. ### Check what we lose by ifdef'ing out these. --marcpa */
|
|
1327 #if 0
|
|
1328 /* Need to set input thread locale if present. */
|
|
1329 if (dwWinThreadId)
|
|
1330 /* Reply is not needed. */
|
|
1331 PostThreadMessage (dwWinThreadId, WM_EMACS_SETLOCALE, XINT (lcid), 0);
|
|
1332 #endif
|
|
1333
|
|
1334 return make_int (GetThreadLocale ());
|
|
1335 }
|
|
1336
|
|
1337
|
288
|
1338 void
|
100
|
1339 syms_of_ntproc ()
|
|
1340 {
|
209
|
1341 DEFSUBR (Fwin32_short_file_name);
|
|
1342 DEFSUBR (Fwin32_long_file_name);
|
|
1343 DEFSUBR (Fwin32_set_process_priority);
|
|
1344 DEFSUBR (Fwin32_get_locale_info);
|
|
1345 DEFSUBR (Fwin32_get_current_locale_id);
|
|
1346 DEFSUBR (Fwin32_get_default_locale_id);
|
|
1347 DEFSUBR (Fwin32_get_valid_locale_ids);
|
|
1348 DEFSUBR (Fwin32_set_current_locale);
|
347
|
1349 }
|
|
1350
|
|
1351
|
|
1352 void
|
|
1353 vars_of_ntproc (void)
|
|
1354 {
|
|
1355 Qhigh = intern ("high");
|
|
1356 Qlow = intern ("low");
|
100
|
1357
|
|
1358 DEFVAR_LISP ("win32-quote-process-args", &Vwin32_quote_process_args /*
|
209
|
1359 Non-nil enables quoting of process arguments to ensure correct parsing.
|
100
|
1360 Because Windows does not directly pass argv arrays to child processes,
|
|
1361 programs have to reconstruct the argv array by parsing the command
|
|
1362 line string. For an argument to contain a space, it must be enclosed
|
|
1363 in double quotes or it will be parsed as multiple arguments.
|
|
1364
|
209
|
1365 If the value is a character, that character will be used to escape any
|
|
1366 quote characters that appear, otherwise a suitable escape character
|
|
1367 will be chosen based on the type of the program.
|
|
1368 */ );
|
|
1369 Vwin32_quote_process_args = Qt;
|
|
1370
|
|
1371 DEFVAR_LISP ("win32-start-process-show-window",
|
|
1372 &Vwin32_start_process_show_window /*
|
|
1373 When nil, processes started via start-process hide their windows.
|
|
1374 When non-nil, they show their window in the method of their choice.
|
|
1375 */ );
|
|
1376 Vwin32_start_process_show_window = Qnil;
|
|
1377
|
|
1378 DEFVAR_LISP ("win32-start-process-share-console",
|
|
1379 &Vwin32_start_process_share_console /*
|
|
1380 When nil, processes started via start-process are given a new console.
|
|
1381 When non-nil, they share the Emacs console; this has the limitation of
|
|
1382 allowing only only DOS subprocess to run at a time (whether started directly
|
|
1383 or indirectly by Emacs), and preventing Emacs from cleanly terminating the
|
|
1384 subprocess group, but may allow Emacs to interrupt a subprocess that doesn't
|
|
1385 otherwise respond to interrupts from Emacs.
|
|
1386 */ );
|
347
|
1387 Vwin32_start_process_share_console = Qt;
|
100
|
1388
|
282
|
1389 DEFVAR_LISP ("win32-pipe-read-delay", &Vwin32_pipe_read_delay /*
|
209
|
1390 Forced delay before reading subprocess output.
|
100
|
1391 This is done to improve the buffering of subprocess output, by
|
|
1392 avoiding the inefficiency of frequently reading small amounts of data.
|
|
1393
|
|
1394 If positive, the value is the number of milliseconds to sleep before
|
|
1395 reading the subprocess output. If negative, the magnitude is the number
|
|
1396 of time slices to wait (effectively boosting the priority of the child
|
209
|
1397 process temporarily). A value of zero disables waiting entirely.
|
|
1398 */ );
|
282
|
1399 Vwin32_pipe_read_delay = make_int (50);
|
100
|
1400
|
209
|
1401 #if 0
|
|
1402 DEFVAR_LISP ("win32-generate-fake-inodes", &Vwin32_generate_fake_inodes /*
|
|
1403 "Non-nil means attempt to fake realistic inode values.
|
|
1404 This works by hashing the truename of files, and should detect
|
|
1405 aliasing between long and short (8.3 DOS) names, but can have
|
|
1406 false positives because of hash collisions. Note that determing
|
|
1407 the truename of a file can be slow.
|
|
1408 */ );
|
|
1409 Vwin32_generate_fake_inodes = Qnil;
|
|
1410 #endif
|
100
|
1411 }
|
347
|
1412
|
100
|
1413 /* end of ntproc.c */
|