root/src/w32proc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. malloc_before_init
  2. realloc_before_init
  3. free_before_init
  4. _start
  5. sys_signal
  6. sigaction
  7. sigemptyset
  8. sigaddset
  9. sigfillset
  10. sigprocmask
  11. pthread_sigmask
  12. sigismember
  13. w32_raise
  14. getpgrp
  15. tcgetpgrp
  16. setpgid
  17. setsid
  18. w32_get_timer_time
  19. timer_loop
  20. stop_timer_thread
  21. term_timers
  22. init_timers
  23. start_timer_thread
  24. getitimer
  25. setitimer
  26. alarm
  27. new_child
  28. delete_child
  29. find_child_pid
  30. release_listen_threads
  31. reader_thread
  32. create_child
  33. register_child
  34. reap_subprocess
  35. waitpid
  36. open_input_file
  37. rva_to_section
  38. close_file_data
  39. w32_executable_type
  40. compare_env
  41. merge_and_sort_env
  42. sys_spawnve
  43. sys_select
  44. find_child_console
  45. sys_kill
  46. prepare_standard_handles
  47. reset_standard_handles
  48. set_process_dir
  49. DEFUN
  50. DEFUN
  51. DEFUN
  52. DEFUN
  53. DEFUN
  54. nl_langinfo
  55. DEFUN
  56. int_from_hex
  57. enum_locale_fn
  58. DEFUN
  59. DEFUN
  60. DEFUN
  61. enum_codepage_fn
  62. DEFUN
  63. DEFUN
  64. DEFUN
  65. DEFUN
  66. DEFUN
  67. DEFUN
  68. DEFUN
  69. DEFUN
  70. DEFUN
  71. get_lcid_callback
  72. get_lcid
  73. w32_compare_strings
  74. syms_of_ntproc

     1 /* Process support for GNU Emacs on the Microsoft Windows API.
     2 
     3 Copyright (C) 1992, 1995, 1999-2023 Free Software Foundation, Inc.
     4 
     5 This file is part of GNU Emacs.
     6 
     7 GNU Emacs is free software: you can redistribute it and/or modify
     8 it under the terms of the GNU General Public License as published by
     9 the Free Software Foundation, either version 3 of the License, or (at
    10 your option) any later version.
    11 
    12 GNU Emacs is distributed in the hope that it will be useful,
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15 GNU General Public License for more details.
    16 
    17 You should have received a copy of the GNU General Public License
    18 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
    19 
    20 /*
    21    Drew Bliss                   Oct 14, 1993
    22      Adapted from alarm.c by Tim Fleehart
    23 */
    24 
    25 #define DEFER_MS_W32_H
    26 #include <config.h>
    27 
    28 #include <mingw_time.h>
    29 #include <stdio.h>
    30 #include <stdlib.h>
    31 #include <errno.h>
    32 #include <ctype.h>
    33 #include <io.h>
    34 #include <fcntl.h>
    35 #include <unistd.h>
    36 #include <signal.h>
    37 #include <sys/file.h>
    38 #include <mbstring.h>
    39 #include <locale.h>
    40 
    41 /* Include CRT headers *before* ms-w32.h.  */
    42 #include <ms-w32.h>
    43 
    44 #undef signal
    45 #undef wait
    46 #undef spawnve
    47 #undef select
    48 #undef kill
    49 
    50 #include <windows.h>
    51 
    52 #ifdef HAVE_LANGINFO_CODESET
    53 #include <nl_types.h>
    54 #include <langinfo.h>
    55 #endif
    56 
    57 #include "lisp.h"
    58 #include "w32.h"
    59 #include "w32common.h"
    60 #include "w32heap.h"
    61 #include "syswait.h"    /* for WNOHANG */
    62 #include "syssignal.h"
    63 #include "w32term.h"
    64 #include "coding.h"
    65 
    66 void w32_raise (int);
    67 
    68 #define RVA_TO_PTR(var,section,filedata) \
    69   ((void *)((section)->PointerToRawData                                 \
    70             + ((DWORD_PTR)(var) - (section)->VirtualAddress)            \
    71             + (filedata).file_base))
    72 
    73 extern BOOL g_b_init_compare_string_w;
    74 extern BOOL g_b_init_debug_break_process;
    75 
    76 int sys_select (int, SELECT_TYPE *, SELECT_TYPE *, SELECT_TYPE *,
    77                 const struct timespec *, const sigset_t *);
    78 
    79 /* Signal handlers...SIG_DFL == 0 so this is initialized correctly.  */
    80 static signal_handler sig_handlers[NSIG];
    81 
    82 static sigset_t sig_mask;
    83 
    84 static CRITICAL_SECTION crit_sig;
    85 
    86 /* Catch memory allocation before the heap allocation scheme is set
    87    up.  These functions should never be called, unless code is added
    88    early on in 'main' that runs before init_heap is called.  */
    89 _Noreturn void * malloc_before_init (size_t);
    90 _Noreturn void * realloc_before_init (void *, size_t);
    91 _Noreturn void   free_before_init (void *);
    92 
    93 _Noreturn void *
    94 malloc_before_init (size_t size)
    95 {
    96   fprintf (stderr,
    97            "error: 'malloc' called before setting up heap allocation; exiting.\n");
    98   exit (-1);
    99 }
   100 
   101 _Noreturn void *
   102 realloc_before_init (void *ptr, size_t size)
   103 {
   104   fprintf (stderr,
   105            "error: 'realloc' called before setting up heap allocation; exiting.\n");
   106   exit (-1);
   107 }
   108 
   109 _Noreturn void
   110 free_before_init (void *ptr)
   111 {
   112   fprintf (stderr,
   113            "error: 'free' called before setting up heap allocation; exiting.\n");
   114   exit (-1);
   115 }
   116 
   117 extern BOOL ctrl_c_handler (unsigned long type);
   118 
   119 /* MinGW64 doesn't add a leading underscore to external symbols,
   120    whereas configure.ac sets up LD_SWITCH_SYSTEM_TEMACS to force the
   121    entry point at __start, with two underscores.  */
   122 #ifdef __MINGW64__
   123 #define _start __start
   124 #endif
   125 
   126 extern void mainCRTStartup (void);
   127 
   128 /* Startup code for running on NT.  When we are running as the dumped
   129    version, we need to bootstrap our heap and .bss section into our
   130    address space before we can actually hand off control to the startup
   131    code supplied by NT (primarily because that code relies upon malloc ()).  */
   132 void _start (void);
   133 
   134 void
   135 _start (void)
   136 {
   137 
   138 #if 1
   139   /* Give us a way to debug problems with crashes on startup when
   140      running under the MSVC profiler. */
   141   if (GetEnvironmentVariable ("EMACS_DEBUG", NULL, 0) > 0)
   142     DebugBreak ();
   143 #endif
   144 
   145   the_malloc_fn = malloc_before_init;
   146   the_realloc_fn = realloc_before_init;
   147   the_free_fn = free_before_init;
   148 
   149   /* Cache system info, e.g., the NT page size.  */
   150   cache_system_info ();
   151 
   152   /* This prevents ctrl-c's in shells running while we're suspended from
   153      having us exit.  */
   154   SetConsoleCtrlHandler ((PHANDLER_ROUTINE) ctrl_c_handler, TRUE);
   155 
   156   /* Prevent Emacs from being locked up (eg. in batch mode) when
   157      accessing devices that aren't mounted (eg. removable media drives).  */
   158   SetErrorMode (SEM_FAILCRITICALERRORS);
   159   mainCRTStartup ();
   160 }
   161 
   162 /* Improve on the CRT 'signal' implementation so that we could record
   163    the SIGCHLD handler and fake interval timers.  */
   164 signal_handler
   165 sys_signal (int sig, signal_handler handler)
   166 {
   167   signal_handler old;
   168 
   169   /* SIGCHLD is needed for supporting subprocesses, see sys_kill
   170      below.  SIGALRM and SIGPROF are used by setitimer.  All the
   171      others are the only ones supported by the MS runtime.  */
   172   if (!(sig == SIGINT || sig == SIGSEGV || sig == SIGILL
   173         || sig == SIGFPE || sig == SIGABRT || sig == SIGTERM
   174         || sig == SIGCHLD || sig == SIGALRM || sig == SIGPROF))
   175     {
   176       errno = EINVAL;
   177       return SIG_ERR;
   178     }
   179   old = sig_handlers[sig];
   180   /* SIGABRT is treated specially because w32.c installs term_ntproc
   181      as its handler, so we don't want to override that afterwards.
   182      Aborting Emacs works specially anyway: either by calling
   183      emacs_abort directly or through terminate_due_to_signal, which
   184      calls emacs_abort through emacs_raise.  */
   185   if (!(sig == SIGABRT && old == term_ntproc))
   186     {
   187       sig_handlers[sig] = handler;
   188       if (!(sig == SIGCHLD || sig == SIGALRM || sig == SIGPROF))
   189         signal (sig, handler);
   190     }
   191   return old;
   192 }
   193 
   194 /* Emulate sigaction. */
   195 int
   196 sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
   197 {
   198   signal_handler old = SIG_DFL;
   199   int retval = 0;
   200 
   201   if (act)
   202     old = sys_signal (sig, act->sa_handler);
   203   else if (oact)
   204     old = sig_handlers[sig];
   205 
   206   if (old == SIG_ERR)
   207     {
   208       errno = EINVAL;
   209       retval = -1;
   210     }
   211   if (oact)
   212     {
   213       oact->sa_handler = old;
   214       oact->sa_flags = 0;
   215       oact->sa_mask = empty_mask;
   216     }
   217   return retval;
   218 }
   219 
   220 /* Emulate signal sets and blocking of signals used by timers.  */
   221 
   222 int
   223 sigemptyset (sigset_t *set)
   224 {
   225   *set = 0;
   226   return 0;
   227 }
   228 
   229 int
   230 sigaddset (sigset_t *set, int signo)
   231 {
   232   if (!set)
   233     {
   234       errno = EINVAL;
   235       return -1;
   236     }
   237   if (signo < 0 || signo >= NSIG)
   238     {
   239       errno = EINVAL;
   240       return -1;
   241     }
   242 
   243   *set |= (1U << signo);
   244 
   245   return 0;
   246 }
   247 
   248 int
   249 sigfillset (sigset_t *set)
   250 {
   251   if (!set)
   252     {
   253       errno = EINVAL;
   254       return -1;
   255     }
   256 
   257   *set = 0xFFFFFFFF;
   258   return 0;
   259 }
   260 
   261 int
   262 sigprocmask (int how, const sigset_t *set, sigset_t *oset)
   263 {
   264   if (!(how == SIG_BLOCK || how == SIG_UNBLOCK || how == SIG_SETMASK))
   265     {
   266       errno = EINVAL;
   267       return -1;
   268     }
   269 
   270   if (oset)
   271     *oset = sig_mask;
   272 
   273   if (!set)
   274     return 0;
   275 
   276   switch (how)
   277     {
   278     case SIG_BLOCK:
   279       sig_mask |= *set;
   280       break;
   281     case SIG_SETMASK:
   282       sig_mask = *set;
   283       break;
   284     case SIG_UNBLOCK:
   285       /* FIXME: Catch signals that are blocked and reissue them when
   286          they are unblocked.  Important for SIGALRM and SIGPROF only.  */
   287       sig_mask &= ~(*set);
   288       break;
   289     }
   290 
   291   return 0;
   292 }
   293 
   294 int
   295 pthread_sigmask (int how, const sigset_t *set, sigset_t *oset)
   296 {
   297   if (sigprocmask (how, set, oset) == -1)
   298     return EINVAL;
   299   return 0;
   300 }
   301 
   302 int
   303 sigismember (const sigset_t *set, int signo)
   304 {
   305   if (signo < 0 || signo >= NSIG)
   306     {
   307       errno = EINVAL;
   308       return -1;
   309     }
   310   if (signo > sizeof (*set) * CHAR_BIT)
   311     emacs_abort ();
   312 
   313   return (*set & (1U << signo)) != 0;
   314 }
   315 
   316 /* A fuller emulation of 'raise', which supports signals that MS
   317    runtime doesn't know about.  */
   318 void
   319 w32_raise (int signo)
   320 {
   321   if (!(signo == SIGCHLD || signo == SIGALRM || signo == SIGPROF))
   322     raise (signo);
   323 
   324   /* Call the handler directly for the signals that we handle
   325      ourselves.  */
   326   signal_handler handler = sig_handlers[signo];
   327   if (!(handler == SIG_DFL || handler == SIG_IGN || handler == SIG_ERR))
   328     handler (signo);
   329 }
   330 
   331 pid_t
   332 getpgrp (void)
   333 {
   334   return getpid ();
   335 }
   336 
   337 pid_t
   338 tcgetpgrp (int fd)
   339 {
   340   return getpid ();
   341 }
   342 
   343 int
   344 setpgid (pid_t pid, pid_t pgid)
   345 {
   346   return 0;
   347 }
   348 
   349 pid_t
   350 setsid (void)
   351 {
   352   return getpid ();
   353 }
   354 
   355 /* Emulations of interval timers.
   356 
   357    Limitations: only ITIMER_REAL and ITIMER_PROF are supported.
   358 
   359    Implementation: a separate thread is started for each timer type,
   360    the thread calls the appropriate signal handler when the timer
   361    expires, after stopping the thread which installed the timer.  */
   362 
   363 struct itimer_data {
   364   volatile ULONGLONG expire;
   365   volatile ULONGLONG reload;
   366   volatile int terminate;
   367   int type;
   368   HANDLE caller_thread;
   369   HANDLE timer_thread;
   370 };
   371 
   372 static ULONGLONG ticks_now;
   373 static struct itimer_data real_itimer, prof_itimer;
   374 static ULONGLONG clocks_min;
   375 /* If non-zero, itimers are disabled.  Used during shutdown, when we
   376    delete the critical sections used by the timer threads.  */
   377 static int disable_itimers;
   378 
   379 static CRITICAL_SECTION crit_real, crit_prof;
   380 
   381 /* GetThreadTimes is not available on Windows 9X and possibly also on 2K.  */
   382 typedef BOOL (WINAPI *GetThreadTimes_Proc) (
   383   HANDLE hThread,
   384   LPFILETIME lpCreationTime,
   385   LPFILETIME lpExitTime,
   386   LPFILETIME lpKernelTime,
   387   LPFILETIME lpUserTime);
   388 
   389 static GetThreadTimes_Proc s_pfn_Get_Thread_Times;
   390 
   391 #define MAX_SINGLE_SLEEP    30
   392 #define TIMER_TICKS_PER_SEC 1000
   393 
   394 /* Return a suitable time value, in 1-ms units, for THREAD, a handle
   395    to a thread.  If THREAD is NULL or an invalid handle, return the
   396    current wall-clock time since January 1, 1601 (UTC).  Otherwise,
   397    return the sum of kernel and user times used by THREAD since it was
   398    created, plus its creation time.  */
   399 static ULONGLONG
   400 w32_get_timer_time (HANDLE thread)
   401 {
   402   ULONGLONG retval;
   403   int use_system_time = 1;
   404   /* The functions below return times in 100-ns units.  */
   405   const int tscale = 10 * TIMER_TICKS_PER_SEC;
   406 
   407   if (thread && thread != INVALID_HANDLE_VALUE
   408       && s_pfn_Get_Thread_Times != NULL)
   409     {
   410       FILETIME creation_ftime, exit_ftime, kernel_ftime, user_ftime;
   411       ULARGE_INTEGER temp_creation, temp_kernel, temp_user;
   412 
   413       if (s_pfn_Get_Thread_Times (thread, &creation_ftime, &exit_ftime,
   414                                   &kernel_ftime, &user_ftime))
   415         {
   416           use_system_time = 0;
   417           temp_creation.LowPart = creation_ftime.dwLowDateTime;
   418           temp_creation.HighPart = creation_ftime.dwHighDateTime;
   419           temp_kernel.LowPart = kernel_ftime.dwLowDateTime;
   420           temp_kernel.HighPart = kernel_ftime.dwHighDateTime;
   421           temp_user.LowPart = user_ftime.dwLowDateTime;
   422           temp_user.HighPart = user_ftime.dwHighDateTime;
   423           retval =
   424             temp_creation.QuadPart / tscale + temp_kernel.QuadPart / tscale
   425             + temp_user.QuadPart / tscale;
   426         }
   427       else
   428         DebPrint (("GetThreadTimes failed with error code %lu\n",
   429                    GetLastError ()));
   430     }
   431 
   432   if (use_system_time)
   433     {
   434       FILETIME current_ftime;
   435       ULARGE_INTEGER temp;
   436 
   437       GetSystemTimeAsFileTime (&current_ftime);
   438 
   439       temp.LowPart = current_ftime.dwLowDateTime;
   440       temp.HighPart = current_ftime.dwHighDateTime;
   441 
   442       retval = temp.QuadPart / tscale;
   443     }
   444 
   445   return retval;
   446 }
   447 
   448 /* Thread function for a timer thread.  */
   449 static DWORD WINAPI
   450 timer_loop (LPVOID arg)
   451 {
   452   struct itimer_data *itimer = (struct itimer_data *)arg;
   453   int which = itimer->type;
   454   int sig = (which == ITIMER_REAL) ? SIGALRM : SIGPROF;
   455   CRITICAL_SECTION *crit = (which == ITIMER_REAL) ? &crit_real : &crit_prof;
   456   const DWORD max_sleep = MAX_SINGLE_SLEEP * 1000 / TIMER_TICKS_PER_SEC;
   457   HANDLE hth = (which == ITIMER_REAL) ? NULL : itimer->caller_thread;
   458 
   459   while (1)
   460     {
   461       DWORD sleep_time;
   462       signal_handler handler;
   463       ULONGLONG now, expire, reload;
   464 
   465       /* Load new values if requested by setitimer.  */
   466       EnterCriticalSection (crit);
   467       expire = itimer->expire;
   468       reload = itimer->reload;
   469       LeaveCriticalSection (crit);
   470       if (itimer->terminate)
   471         return 0;
   472 
   473       if (expire == 0)
   474         {
   475           /* We are idle.  */
   476           Sleep (max_sleep);
   477           continue;
   478         }
   479 
   480       if (expire > (now = w32_get_timer_time (hth)))
   481         sleep_time = expire - now;
   482       else
   483         sleep_time = 0;
   484       /* Don't sleep too long at a time, to be able to see the
   485          termination flag without too long a delay.  */
   486       while (sleep_time > max_sleep)
   487         {
   488           if (itimer->terminate)
   489             return 0;
   490           Sleep (max_sleep);
   491           EnterCriticalSection (crit);
   492           expire = itimer->expire;
   493           LeaveCriticalSection (crit);
   494           sleep_time =
   495             (expire > (now = w32_get_timer_time (hth))) ? expire - now : 0;
   496         }
   497       if (itimer->terminate)
   498         return 0;
   499       if (sleep_time > 0)
   500         {
   501           Sleep (sleep_time * 1000 / TIMER_TICKS_PER_SEC);
   502           /* Always sleep past the expiration time, to make sure we
   503              never call the handler _before_ the expiration time,
   504              always slightly after it.  Sleep(5) makes sure we don't
   505              hog the CPU by calling 'w32_get_timer_time' with high
   506              frequency, and also let other threads work.  */
   507           while (w32_get_timer_time (hth) < expire)
   508             Sleep (5);
   509         }
   510 
   511       EnterCriticalSection (crit);
   512       expire = itimer->expire;
   513       LeaveCriticalSection (crit);
   514       if (expire == 0)
   515         continue;
   516 
   517       /* Time's up.  */
   518       handler = sig_handlers[sig];
   519       if (!(handler == SIG_DFL || handler == SIG_IGN || handler == SIG_ERR)
   520           /* FIXME: Don't ignore masked signals.  Instead, record that
   521              they happened and reissue them when the signal is
   522              unblocked.  */
   523           && !sigismember (&sig_mask, sig)
   524           /* Simulate masking of SIGALRM and SIGPROF when processing
   525              fatal signals.  */
   526           && !fatal_error_in_progress
   527           && itimer->caller_thread)
   528         {
   529           /* Simulate a signal delivered to the thread which installed
   530              the timer, by suspending that thread while the handler
   531              runs.  */
   532           HANDLE th = itimer->caller_thread;
   533           DWORD result = SuspendThread (th);
   534 
   535           if (result == (DWORD)-1)
   536             return 2;
   537 
   538           handler (sig);
   539           ResumeThread (th);
   540         }
   541 
   542       /* Update expiration time and loop.  */
   543       EnterCriticalSection (crit);
   544       expire = itimer->expire;
   545       if (expire == 0)
   546         {
   547           LeaveCriticalSection (crit);
   548           continue;
   549         }
   550       reload = itimer->reload;
   551       if (reload > 0)
   552         {
   553           now = w32_get_timer_time (hth);
   554           if (expire <= now)
   555             {
   556               ULONGLONG lag = now - expire;
   557 
   558               /* If we missed some opportunities (presumably while
   559                  sleeping or while the signal handler ran), skip
   560                  them.  */
   561               if (lag > reload)
   562                 expire = now - (lag % reload);
   563 
   564               expire += reload;
   565             }
   566         }
   567       else
   568         expire = 0;     /* become idle */
   569       itimer->expire = expire;
   570       LeaveCriticalSection (crit);
   571     }
   572   return 0;
   573 }
   574 
   575 static void
   576 stop_timer_thread (int which)
   577 {
   578   struct itimer_data *itimer =
   579     (which == ITIMER_REAL) ? &real_itimer : &prof_itimer;
   580   int i;
   581   DWORD err = 0, exit_code = 255;
   582   BOOL status;
   583 
   584   /* Signal the thread that it should terminate.  */
   585   itimer->terminate = 1;
   586 
   587   if (itimer->timer_thread == NULL)
   588     return;
   589 
   590   /* Wait for the timer thread to terminate voluntarily, then kill it
   591      if it doesn't.  This loop waits twice more than the maximum
   592      amount of time a timer thread sleeps, see above.  */
   593   for (i = 0; i < MAX_SINGLE_SLEEP / 5; i++)
   594     {
   595       if (!((status = GetExitCodeThread (itimer->timer_thread, &exit_code))
   596             && exit_code == STILL_ACTIVE))
   597         break;
   598       Sleep (10);
   599     }
   600   if ((status == FALSE && (err = GetLastError ()) == ERROR_INVALID_HANDLE)
   601       || exit_code == STILL_ACTIVE)
   602     {
   603       if (!(status == FALSE && err == ERROR_INVALID_HANDLE))
   604         TerminateThread (itimer->timer_thread, 0);
   605     }
   606 
   607   /* Clean up.  */
   608   CloseHandle (itimer->timer_thread);
   609   itimer->timer_thread = NULL;
   610   if (itimer->caller_thread)
   611     {
   612       CloseHandle (itimer->caller_thread);
   613       itimer->caller_thread = NULL;
   614     }
   615 }
   616 
   617 /* This is called at shutdown time from term_ntproc.  */
   618 void
   619 term_timers (void)
   620 {
   621   if (real_itimer.timer_thread)
   622     stop_timer_thread (ITIMER_REAL);
   623   if (prof_itimer.timer_thread)
   624     stop_timer_thread (ITIMER_PROF);
   625 
   626   /* We are going to delete the critical sections, so timers cannot
   627      work after this.  */
   628   disable_itimers = 1;
   629 
   630   DeleteCriticalSection (&crit_real);
   631   DeleteCriticalSection (&crit_prof);
   632   DeleteCriticalSection (&crit_sig);
   633 }
   634 
   635 /* This is called at initialization time from init_ntproc.  */
   636 void
   637 init_timers (void)
   638 {
   639   /* GetThreadTimes is not available on all versions of Windows, so
   640      need to probe for its availability dynamically, and call it
   641      through a pointer.  */
   642   s_pfn_Get_Thread_Times = NULL; /* in case dumped Emacs comes with a value */
   643   if (os_subtype != OS_SUBTYPE_9X)
   644     s_pfn_Get_Thread_Times = (GetThreadTimes_Proc)
   645       get_proc_addr (GetModuleHandle ("kernel32.dll"), "GetThreadTimes");
   646 
   647   /* Make sure we start with zeroed out itimer structures, since
   648      dumping may have left there traces of threads long dead.  */
   649   memset (&real_itimer, 0, sizeof real_itimer);
   650   memset (&prof_itimer, 0, sizeof prof_itimer);
   651 
   652   InitializeCriticalSection (&crit_real);
   653   InitializeCriticalSection (&crit_prof);
   654   InitializeCriticalSection (&crit_sig);
   655 
   656   disable_itimers = 0;
   657 }
   658 
   659 static int
   660 start_timer_thread (int which)
   661 {
   662   DWORD exit_code, tid;
   663   HANDLE th;
   664   struct itimer_data *itimer =
   665     (which == ITIMER_REAL) ? &real_itimer : &prof_itimer;
   666 
   667   if (itimer->timer_thread
   668       && GetExitCodeThread (itimer->timer_thread, &exit_code)
   669       && exit_code == STILL_ACTIVE)
   670     return 0;
   671 
   672   /* Clean up after possibly exited thread.  */
   673   if (itimer->timer_thread)
   674     {
   675       CloseHandle (itimer->timer_thread);
   676       itimer->timer_thread = NULL;
   677     }
   678   if (itimer->caller_thread)
   679     {
   680       CloseHandle (itimer->caller_thread);
   681       itimer->caller_thread = NULL;
   682     }
   683 
   684   /* Start a new thread.  */
   685   if (!DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
   686                         GetCurrentProcess (), &th, 0, FALSE,
   687                         DUPLICATE_SAME_ACCESS))
   688     {
   689       errno = ESRCH;
   690       return -1;
   691     }
   692   itimer->terminate = 0;
   693   itimer->type = which;
   694   itimer->caller_thread = th;
   695   /* Request that no more than 64KB of stack be reserved for this
   696      thread, to avoid reserving too much memory, which would get in
   697      the way of threads we start to wait for subprocesses.  See also
   698      new_child below.  */
   699   itimer->timer_thread = CreateThread (NULL, 64 * 1024, timer_loop,
   700                                        (void *)itimer, 0x00010000, &tid);
   701 
   702   if (!itimer->timer_thread)
   703     {
   704       CloseHandle (itimer->caller_thread);
   705       itimer->caller_thread = NULL;
   706       errno = EAGAIN;
   707       return -1;
   708     }
   709 
   710   /* This is needed to make sure that the timer thread running for
   711      profiling gets CPU as soon as the Sleep call terminates. */
   712   if (which == ITIMER_PROF)
   713     SetThreadPriority (itimer->timer_thread, THREAD_PRIORITY_TIME_CRITICAL);
   714 
   715   return 0;
   716 }
   717 
   718 /* Most of the code of getitimer and setitimer (but not of their
   719    subroutines) was shamelessly stolen from itimer.c in the DJGPP
   720    library, see www.delorie.com/djgpp.  */
   721 int
   722 getitimer (int which, struct itimerval *value)
   723 {
   724   volatile ULONGLONG *t_expire;
   725   volatile ULONGLONG *t_reload;
   726   ULONGLONG expire, reload;
   727   __int64 usecs;
   728   CRITICAL_SECTION *crit;
   729   struct itimer_data *itimer;
   730 
   731   if (disable_itimers)
   732     return -1;
   733 
   734   if (!value)
   735     {
   736       errno = EFAULT;
   737       return -1;
   738     }
   739 
   740   if (which != ITIMER_REAL && which != ITIMER_PROF)
   741     {
   742       errno = EINVAL;
   743       return -1;
   744     }
   745 
   746   itimer = (which == ITIMER_REAL) ? &real_itimer : &prof_itimer;
   747 
   748   ticks_now = w32_get_timer_time ((which == ITIMER_REAL)
   749                                   ? NULL
   750                                   : GetCurrentThread ());
   751 
   752   t_expire = &itimer->expire;
   753   t_reload = &itimer->reload;
   754   crit = (which == ITIMER_REAL) ? &crit_real : &crit_prof;
   755 
   756   EnterCriticalSection (crit);
   757   reload = *t_reload;
   758   expire = *t_expire;
   759   LeaveCriticalSection (crit);
   760 
   761   if (expire)
   762     expire -= ticks_now;
   763 
   764   value->it_value.tv_sec    = expire / TIMER_TICKS_PER_SEC;
   765   usecs =
   766     (expire % TIMER_TICKS_PER_SEC) * (__int64)1000000 / TIMER_TICKS_PER_SEC;
   767   value->it_value.tv_usec   = usecs;
   768   value->it_interval.tv_sec = reload / TIMER_TICKS_PER_SEC;
   769   usecs =
   770     (reload % TIMER_TICKS_PER_SEC) * (__int64)1000000 / TIMER_TICKS_PER_SEC;
   771   value->it_interval.tv_usec= usecs;
   772 
   773   return 0;
   774 }
   775 
   776 int
   777 setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
   778 {
   779   volatile ULONGLONG *t_expire, *t_reload;
   780   ULONGLONG expire, reload, expire_old, reload_old;
   781   __int64 usecs;
   782   CRITICAL_SECTION *crit;
   783   struct itimerval tem, *ptem;
   784 
   785   if (disable_itimers)
   786     return -1;
   787 
   788   /* Posix systems expect timer values smaller than the resolution of
   789      the system clock be rounded up to the clock resolution.  First
   790      time we are called, measure the clock tick resolution.  */
   791   if (!clocks_min)
   792     {
   793       ULONGLONG t1, t2;
   794 
   795       for (t1 = w32_get_timer_time (NULL);
   796            (t2 = w32_get_timer_time (NULL)) == t1; )
   797         ;
   798       clocks_min = t2 - t1;
   799     }
   800 
   801   if (ovalue)
   802     ptem = ovalue;
   803   else
   804     ptem = &tem;
   805 
   806   if (getitimer (which, ptem)) /* also sets ticks_now */
   807     return -1;                 /* errno already set */
   808 
   809   t_expire =
   810     (which == ITIMER_REAL) ? &real_itimer.expire : &prof_itimer.expire;
   811   t_reload =
   812     (which == ITIMER_REAL) ? &real_itimer.reload : &prof_itimer.reload;
   813 
   814   crit = (which == ITIMER_REAL) ? &crit_real : &crit_prof;
   815 
   816   if (!value
   817       || (value->it_value.tv_sec == 0 && value->it_value.tv_usec == 0))
   818     {
   819       EnterCriticalSection (crit);
   820       /* Disable the timer.  */
   821       *t_expire = 0;
   822       *t_reload = 0;
   823       LeaveCriticalSection (crit);
   824       return 0;
   825     }
   826 
   827   reload = value->it_interval.tv_sec * TIMER_TICKS_PER_SEC;
   828 
   829   usecs = value->it_interval.tv_usec;
   830   if (value->it_interval.tv_sec == 0
   831       && usecs && usecs * TIMER_TICKS_PER_SEC < clocks_min * 1000000)
   832     reload = clocks_min;
   833   else
   834     {
   835       usecs *= TIMER_TICKS_PER_SEC;
   836       reload += usecs / 1000000;
   837     }
   838 
   839   expire = value->it_value.tv_sec * TIMER_TICKS_PER_SEC;
   840   usecs = value->it_value.tv_usec;
   841   if (value->it_value.tv_sec == 0
   842       && usecs * TIMER_TICKS_PER_SEC < clocks_min * 1000000)
   843     expire = clocks_min;
   844   else
   845     {
   846       usecs *= TIMER_TICKS_PER_SEC;
   847       expire += usecs / 1000000;
   848     }
   849 
   850   expire += ticks_now;
   851 
   852   EnterCriticalSection (crit);
   853   expire_old = *t_expire;
   854   reload_old = *t_reload;
   855   if (!(expire == expire_old && reload == reload_old))
   856     {
   857       *t_reload = reload;
   858       *t_expire = expire;
   859     }
   860   LeaveCriticalSection (crit);
   861 
   862   return start_timer_thread (which);
   863 }
   864 
   865 int
   866 alarm (int seconds)
   867 {
   868 #ifdef HAVE_SETITIMER
   869   struct itimerval new_values, old_values;
   870 
   871   new_values.it_value.tv_sec = seconds;
   872   new_values.it_value.tv_usec = 0;
   873   new_values.it_interval.tv_sec = new_values.it_interval.tv_usec = 0;
   874 
   875   if (setitimer (ITIMER_REAL, &new_values, &old_values) < 0)
   876     return 0;
   877   return old_values.it_value.tv_sec;
   878 #else
   879   return seconds;
   880 #endif
   881 }
   882 
   883 
   884 
   885 /* Here's an overview of how support for subprocesses and
   886    network/serial streams is implemented on MS-Windows.
   887 
   888    The management of both subprocesses and network/serial streams
   889    circles around the child_procs[] array, which can record up to the
   890    grand total of MAX_CHILDREN (= 32) of these.  (The reasons for the
   891    32 limitation will become clear below.)  Each member of
   892    child_procs[] is a child_process structure, defined on w32.h.
   893 
   894    A related data structure is the fd_info[] array, which holds twice
   895    as many members, 64, and records the information about file
   896    descriptors used for communicating with subprocesses and
   897    network/serial devices.  Each member of the array is the filedesc
   898    structure, which records the Windows handle for communications,
   899    such as the read end of the pipe to a subprocess, a socket handle,
   900    etc.
   901 
   902    Both these arrays reference each other: there's a member of
   903    child_process structure that records the corresponding file
   904    descriptor, and there's a member of filedesc structure that holds a
   905    pointer to the corresponding child_process.
   906 
   907    Whenever Emacs starts a subprocess or opens a network/serial
   908    stream, the function new_child is called to prepare a new
   909    child_process structure.  new_child looks for the first vacant slot
   910    in the child_procs[] array, initializes it, and starts a "reader
   911    thread" that will watch the output of the subprocess/stream and its
   912    status.  (If no vacant slot can be found, new_child returns a
   913    failure indication to its caller, and the higher-level Emacs
   914    primitive that called it will then fail with EMFILE or EAGAIN.)
   915 
   916    The reader thread started by new_child communicates with the main
   917    (a.k.a. "Lisp") thread via two event objects and a status, all of
   918    them recorded by the members of the child_process structure in
   919    child_procs[].  The event objects serve as semaphores between the
   920    reader thread and the 'pselect' emulation in sys_select, as follows:
   921 
   922      . Initially, the reader thread is waiting for the char_consumed
   923        event to become signaled by sys_select, which is an indication
   924        for the reader thread to go ahead and try reading more stuff
   925        from the subprocess/stream.
   926 
   927      . The reader thread then attempts to read by calling a
   928        blocking-read function.  When the read call returns, either
   929        successfully or with some failure indication, the reader thread
   930        updates the status of the read accordingly, and signals the 2nd
   931        event object, char_avail, on whose handle sys_select is
   932        waiting.  This tells sys_select that the file descriptor
   933        allocated for the subprocess or the stream is ready to be
   934        read from.
   935 
   936    When the subprocess exits or the network/serial stream is closed,
   937    the reader thread sets the status accordingly and exits.  It also
   938    exits when the main thread sets the status to STATUS_READ_ERROR
   939    and/or the char_avail and char_consumed event handles become NULL;
   940    this is how delete_child, called by Emacs when a subprocess or a
   941    stream is terminated, terminates the reader thread as part of
   942    deleting the child_process object.
   943 
   944    The sys_select function emulates the Posix 'pselect' functionality;
   945    it is needed because the Windows 'select' function supports only
   946    network sockets, while Emacs expects 'pselect' to work for any file
   947    descriptor, including pipes and serial streams.
   948 
   949    When sys_select is called, it uses the information in fd_info[]
   950    array to convert the file descriptors which it was asked to watch
   951    into Windows handles.  In general, the handle to watch is the
   952    handle of the char_avail event of the child_process structure that
   953    corresponds to the file descriptor.  In addition, for subprocesses,
   954    sys_select watches one more handle: the handle for the subprocess,
   955    so that it could emulate the SIGCHLD signal when the subprocess
   956    exits.
   957 
   958    If file descriptor zero (stdin) doesn't have its bit set in the
   959    'rfds' argument to sys_select, the function always watches for
   960    keyboard interrupts, to be able to interrupt the wait and return
   961    when the user presses C-g.
   962 
   963    Having collected the handles to watch, sys_select calls
   964    WaitForMultipleObjects to wait for any one of them to become
   965    signaled.  Since WaitForMultipleObjects can only watch up to 64
   966    handles, Emacs on Windows is limited to maximum 32 child_process
   967    objects (since a subprocess consumes 2 handles to be watched, see
   968    above).
   969 
   970    When any of the handles become signaled, sys_select does whatever
   971    is appropriate for the corresponding child_process object:
   972 
   973      . If it's a handle to the char_avail event, sys_select marks the
   974        corresponding bit in 'rfds', and Emacs will then read from that
   975        file descriptor.
   976 
   977      . If it's a handle to the process, sys_select calls the SIGCHLD
   978        handler, to inform Emacs of the fact that the subprocess
   979        exited.
   980 
   981    The waitpid emulation works very similar to sys_select, except that
   982    it only watches handles of subprocesses, and doesn't synchronize
   983    with the reader thread.
   984 
   985    Because socket descriptors on Windows are handles, while Emacs
   986    expects them to be file descriptors, all low-level I/O functions,
   987    such as 'read' and 'write', and all socket operations, like
   988    'connect', 'recvfrom', 'accept', etc., are redirected to the
   989    corresponding 'sys_*' functions, which must convert a file
   990    descriptor to a handle using the fd_info[] array, and then invoke
   991    the corresponding Windows API on the handle.  Most of these
   992    redirected 'sys_*' functions are implemented on w32.c.
   993 
   994    When the file descriptor was produced by functions such as 'open',
   995    the corresponding handle is obtained by calling _get_osfhandle.  To
   996    produce a file descriptor for a socket handle, which has no file
   997    descriptor as far as Windows is concerned, the function
   998    socket_to_fd opens the null device; the resulting file descriptor
   999    will never be used directly in any I/O API, but serves as an index
  1000    into the fd_info[] array, where the socket handle is stored.  The
  1001    SOCK_HANDLE macro retrieves the handle when given the file
  1002    descriptor.
  1003 
  1004    The function sys_kill emulates the Posix 'kill' functionality to
  1005    terminate other processes.  It does that by attaching to the
  1006    foreground window of the process and sending a Ctrl-C or Ctrl-BREAK
  1007    signal to the process; if that doesn't work, then it calls
  1008    TerminateProcess to forcibly terminate the process.  Note that this
  1009    only terminates the immediate process whose PID was passed to
  1010    sys_kill; it doesn't terminate the child processes of that process.
  1011    This means, for example, that an Emacs subprocess run through a
  1012    shell might not be killed, because sys_kill will only terminate the
  1013    shell.  (In practice, however, such problems are very rare.)  */
  1014 
  1015 /* Defined in <process.h> which conflicts with the local copy */
  1016 #define _P_NOWAIT 1
  1017 
  1018 /* Child process management list.  */
  1019 int child_proc_count = 0;
  1020 child_process child_procs[ MAX_CHILDREN ];
  1021 
  1022 static DWORD WINAPI reader_thread (void *arg);
  1023 
  1024 /* Find an unused process slot.  */
  1025 child_process *
  1026 new_child (void)
  1027 {
  1028   child_process *cp;
  1029   DWORD id;
  1030 
  1031   for (cp = child_procs + (child_proc_count-1); cp >= child_procs; cp--)
  1032     if (!CHILD_ACTIVE (cp) && cp->procinfo.hProcess == NULL)
  1033       goto Initialize;
  1034   if (child_proc_count == MAX_CHILDREN)
  1035     {
  1036       int i = 0;
  1037       child_process *dead_cp = NULL;
  1038 
  1039       DebPrint (("new_child: No vacant slots, looking for dead processes\n"));
  1040       for (cp = child_procs + (child_proc_count-1); cp >= child_procs; cp--)
  1041         if (!CHILD_ACTIVE (cp) && cp->procinfo.hProcess)
  1042           {
  1043             DWORD status = 0;
  1044 
  1045             if (!GetExitCodeProcess (cp->procinfo.hProcess, &status))
  1046               {
  1047                 DebPrint (("new_child.GetExitCodeProcess: error %lu for PID %lu\n",
  1048                            GetLastError (), cp->procinfo.dwProcessId));
  1049                 status = STILL_ACTIVE;
  1050               }
  1051             if (status != STILL_ACTIVE
  1052                 || WaitForSingleObject (cp->procinfo.hProcess, 0) == WAIT_OBJECT_0)
  1053               {
  1054                 DebPrint (("new_child: Freeing slot of dead process %d, fd %d\n",
  1055                            cp->procinfo.dwProcessId, cp->fd));
  1056                 CloseHandle (cp->procinfo.hProcess);
  1057                 cp->procinfo.hProcess = NULL;
  1058                 CloseHandle (cp->procinfo.hThread);
  1059                 cp->procinfo.hThread = NULL;
  1060                 /* Free up to 2 dead slots at a time, so that if we
  1061                    have a lot of them, they will eventually all be
  1062                    freed when the tornado ends.  */
  1063                 if (i == 0)
  1064                   dead_cp = cp;
  1065                 else
  1066                   break;
  1067                 i++;
  1068               }
  1069           }
  1070       if (dead_cp)
  1071         {
  1072           cp = dead_cp;
  1073           goto Initialize;
  1074         }
  1075     }
  1076   if (child_proc_count == MAX_CHILDREN)
  1077     return NULL;
  1078   cp = &child_procs[child_proc_count++];
  1079 
  1080  Initialize:
  1081   /* Last opportunity to avoid leaking handles before we forget them
  1082      for good.  */
  1083   if (cp->procinfo.hProcess)
  1084     CloseHandle (cp->procinfo.hProcess);
  1085   if (cp->procinfo.hThread)
  1086     CloseHandle (cp->procinfo.hThread);
  1087   memset (cp, 0, sizeof (*cp));
  1088   cp->fd = -1;
  1089   cp->pid = -1;
  1090   cp->procinfo.hProcess = NULL;
  1091   cp->status = STATUS_READ_ERROR;
  1092 
  1093   /* use manual reset event so that select() will function properly */
  1094   cp->char_avail = CreateEvent (NULL, TRUE, FALSE, NULL);
  1095   if (cp->char_avail)
  1096     {
  1097       cp->char_consumed = CreateEvent (NULL, FALSE, FALSE, NULL);
  1098       if (cp->char_consumed)
  1099         {
  1100           /* The 0x00010000 flag is STACK_SIZE_PARAM_IS_A_RESERVATION.
  1101              It means that the 64K stack we are requesting in the 2nd
  1102              argument is how much memory should be reserved for the
  1103              stack.  If we don't use this flag, the memory requested
  1104              by the 2nd argument is the amount actually _committed_,
  1105              but Windows reserves 8MB of memory for each thread's
  1106              stack.  (The 8MB figure comes from the -stack
  1107              command-line argument we pass to the linker when building
  1108              Emacs, but that's because we need a large stack for
  1109              Emacs's main thread.)  Since we request 2GB of reserved
  1110              memory at startup (see w32heap.c), which is close to the
  1111              maximum memory available for a 32-bit process on Windows,
  1112              the 8MB reservation for each thread causes failures in
  1113              starting subprocesses, because we create a thread running
  1114              reader_thread for each subprocess.  As 8MB of stack is
  1115              way too much for reader_thread, forcing Windows to
  1116              reserve less wins the day.  */
  1117           cp->thrd = CreateThread (NULL, 64 * 1024, reader_thread, cp,
  1118                                    0x00010000, &id);
  1119           if (cp->thrd)
  1120             return cp;
  1121         }
  1122     }
  1123   delete_child (cp);
  1124   return NULL;
  1125 }
  1126 
  1127 void
  1128 delete_child (child_process *cp)
  1129 {
  1130   int i;
  1131 
  1132   /* Should not be deleting a child that is still needed. */
  1133   for (i = 0; i < MAXDESC; i++)
  1134     if (fd_info[i].cp == cp)
  1135       emacs_abort ();
  1136 
  1137   if (!CHILD_ACTIVE (cp) && cp->procinfo.hProcess == NULL)
  1138     return;
  1139 
  1140   /* reap thread if necessary */
  1141   if (cp->thrd)
  1142     {
  1143       DWORD rc;
  1144 
  1145       if (GetExitCodeThread (cp->thrd, &rc) && rc == STILL_ACTIVE)
  1146         {
  1147           /* let the thread exit cleanly if possible */
  1148           cp->status = STATUS_READ_ERROR;
  1149           SetEvent (cp->char_consumed);
  1150 #if 0
  1151           /* We used to forcibly terminate the thread here, but it
  1152              is normally unnecessary, and in abnormal cases, the worst that
  1153              will happen is we have an extra idle thread hanging around
  1154              waiting for the zombie process.  */
  1155           if (WaitForSingleObject (cp->thrd, 1000) != WAIT_OBJECT_0)
  1156             {
  1157               DebPrint (("delete_child.WaitForSingleObject (thread) failed "
  1158                          "with %lu for fd %ld\n", GetLastError (), cp->fd));
  1159               TerminateThread (cp->thrd, 0);
  1160             }
  1161 #endif
  1162         }
  1163       CloseHandle (cp->thrd);
  1164       cp->thrd = NULL;
  1165     }
  1166   if (cp->char_avail)
  1167     {
  1168       CloseHandle (cp->char_avail);
  1169       cp->char_avail = NULL;
  1170     }
  1171   if (cp->char_consumed)
  1172     {
  1173       CloseHandle (cp->char_consumed);
  1174       cp->char_consumed = NULL;
  1175     }
  1176 
  1177   /* update child_proc_count (highest numbered slot in use plus one) */
  1178   if (cp == child_procs + child_proc_count - 1)
  1179     {
  1180       for (i = child_proc_count-1; i >= 0; i--)
  1181         if (CHILD_ACTIVE (&child_procs[i])
  1182             || child_procs[i].procinfo.hProcess != NULL)
  1183           {
  1184             child_proc_count = i + 1;
  1185             break;
  1186           }
  1187     }
  1188   if (i < 0)
  1189     child_proc_count = 0;
  1190 }
  1191 
  1192 /* Find a child by pid.  */
  1193 static child_process *
  1194 find_child_pid (DWORD pid)
  1195 {
  1196   child_process *cp;
  1197 
  1198   for (cp = child_procs + (child_proc_count-1); cp >= child_procs; cp--)
  1199     if ((CHILD_ACTIVE (cp) || cp->procinfo.hProcess != NULL)
  1200         && pid == cp->pid)
  1201       return cp;
  1202   return NULL;
  1203 }
  1204 
  1205 void
  1206 release_listen_threads (void)
  1207 {
  1208   int i;
  1209 
  1210   for (i = child_proc_count - 1; i >= 0; i--)
  1211     {
  1212       if (CHILD_ACTIVE (&child_procs[i])
  1213           && (fd_info[child_procs[i].fd].flags & FILE_LISTEN))
  1214         child_procs[i].status = STATUS_READ_ERROR;
  1215     }
  1216 }
  1217 
  1218 /* Thread proc for child process and socket reader threads. Each thread
  1219    is normally blocked until woken by select() to check for input by
  1220    reading one char.  When the read completes, char_avail is signaled
  1221    to wake up the select emulator and the thread blocks itself again. */
  1222 static DWORD WINAPI
  1223 reader_thread (void *arg)
  1224 {
  1225   child_process *cp;
  1226   int fd;
  1227 
  1228   /* Our identity */
  1229   cp = (child_process *)arg;
  1230 
  1231   /* We have to wait for the go-ahead before we can start */
  1232   if (cp == NULL
  1233       || WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0
  1234       || cp->fd < 0)
  1235     return 1;
  1236 
  1237   for (;;)
  1238     {
  1239       int rc;
  1240 
  1241       fd = cp->fd;
  1242       if (fd >= 0 && (fd_info[fd].flags & FILE_CONNECT) != 0)
  1243         rc = _sys_wait_connect (fd);
  1244       else if (fd >= 0 && (fd_info[fd].flags & FILE_LISTEN) != 0)
  1245         rc = _sys_wait_accept (fd);
  1246       else
  1247         rc = _sys_read_ahead (fd);
  1248 
  1249       /* Don't bother waiting for the event if we already have been
  1250          told to exit by delete_child.  */
  1251       if (cp->status == STATUS_READ_ERROR || !cp->char_avail)
  1252         break;
  1253 
  1254       /* The name char_avail is a misnomer - it really just means the
  1255          read-ahead has completed, whether successfully or not. */
  1256       if (!SetEvent (cp->char_avail))
  1257         {
  1258           DebPrint (("reader_thread.SetEvent(0x%x) failed with %lu for fd %ld (PID %d)\n",
  1259                      (DWORD_PTR)cp->char_avail, GetLastError (),
  1260                      fd, cp->pid));
  1261           return 1;
  1262         }
  1263 
  1264       if (rc == STATUS_READ_ERROR || rc == STATUS_CONNECT_FAILED)
  1265         return 2;
  1266 
  1267       /* If the read died, the child has died so let the thread die */
  1268       if (rc == STATUS_READ_FAILED)
  1269         break;
  1270 
  1271       /* Don't bother waiting for the acknowledge if we already have
  1272          been told to exit by delete_child.  */
  1273       if (cp->status == STATUS_READ_ERROR || !cp->char_consumed)
  1274         break;
  1275 
  1276       /* Wait until our input is acknowledged before reading again */
  1277       if (WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
  1278         {
  1279           DebPrint (("reader_thread.WaitForSingleObject failed with "
  1280                      "%lu for fd %ld\n", GetLastError (), cp->fd));
  1281           break;
  1282         }
  1283       /* delete_child sets status to STATUS_READ_ERROR when it wants
  1284          us to exit.  */
  1285       if (cp->status == STATUS_READ_ERROR)
  1286         break;
  1287     }
  1288   /* If this thread was reading from a pipe process, close the
  1289      descriptor used for reading, as sys_close doesn't in that case.  */
  1290   if ((fd_info[fd].flags & FILE_DONT_CLOSE) == FILE_DONT_CLOSE)
  1291     {
  1292       int i;
  1293       /* If w32.c:sys_close is still processing this descriptor, wait
  1294          for a while for it to finish.  */
  1295       for (i = 0; i < 5; i++)
  1296         {
  1297           if (fd_info[fd].flags == FILE_DONT_CLOSE)
  1298             {
  1299               fd_info[fd].flags = 0;
  1300               _close (fd);
  1301               break;
  1302             }
  1303           Sleep (5);
  1304         }
  1305     }
  1306   return 0;
  1307 }
  1308 
  1309 /* To avoid Emacs changing directory, we just record here the
  1310    directory the new process should start in.  This is set just before
  1311    calling sys_spawnve, and is not generally valid at any other time.
  1312    Note that this directory's name is UTF-8 encoded.  */
  1313 static char * process_dir;
  1314 
  1315 static BOOL
  1316 create_child (char *exe, char *cmdline, char *env, int is_gui_app,
  1317               pid_t * pPid, child_process *cp)
  1318 {
  1319   STARTUPINFO start;
  1320   SECURITY_ATTRIBUTES sec_attrs;
  1321 #if 0
  1322   SECURITY_DESCRIPTOR sec_desc;
  1323 #endif
  1324   DWORD flags;
  1325   char dir[ MAX_PATH ];
  1326   char *p;
  1327   const char *ext;
  1328 
  1329   if (cp == NULL) emacs_abort ();
  1330 
  1331   memset (&start, 0, sizeof (start));
  1332   start.cb = sizeof (start);
  1333 
  1334 #ifdef HAVE_NTGUI
  1335   if (NILP (Vw32_start_process_show_window) && !is_gui_app)
  1336     start.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  1337   else
  1338     start.dwFlags = STARTF_USESTDHANDLES;
  1339   start.wShowWindow = SW_HIDE;
  1340 
  1341   start.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
  1342   start.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
  1343   start.hStdError = GetStdHandle (STD_ERROR_HANDLE);
  1344 #endif /* HAVE_NTGUI */
  1345 
  1346 #if 0
  1347   /* Explicitly specify no security */
  1348   if (!InitializeSecurityDescriptor (&sec_desc, SECURITY_DESCRIPTOR_REVISION))
  1349     goto EH_Fail;
  1350   if (!SetSecurityDescriptorDacl (&sec_desc, TRUE, NULL, FALSE))
  1351     goto EH_Fail;
  1352 #endif
  1353   sec_attrs.nLength = sizeof (sec_attrs);
  1354   sec_attrs.lpSecurityDescriptor = NULL /* &sec_desc */;
  1355   sec_attrs.bInheritHandle = FALSE;
  1356 
  1357   filename_to_ansi (process_dir, dir);
  1358   /* Can't use unixtodos_filename here, since that needs its file name
  1359      argument encoded in UTF-8.  OTOH, process_dir, which _is_ in
  1360      UTF-8, points, to the directory computed by our caller, and we
  1361      don't want to modify that, either.  */
  1362   for (p = dir; *p; p = CharNextA (p))
  1363     if (*p == '/')
  1364       *p = '\\';
  1365 
  1366   /* CreateProcess handles batch files as exe specially.  This special
  1367      handling fails when both the batch file and arguments are quoted.
  1368      We pass NULL as exe to avoid the special handling. */
  1369   if (exe && cmdline[0] == '"' &&
  1370       (ext = strrchr (exe, '.')) &&
  1371       (xstrcasecmp (ext, ".bat") == 0
  1372        || xstrcasecmp (ext, ".cmd") == 0))
  1373       exe = NULL;
  1374 
  1375   flags = (!NILP (Vw32_start_process_share_console)
  1376            ? CREATE_NEW_PROCESS_GROUP
  1377            : CREATE_NEW_CONSOLE);
  1378   if (NILP (Vw32_start_process_inherit_error_mode))
  1379     flags |= CREATE_DEFAULT_ERROR_MODE;
  1380   if (!CreateProcessA (exe, cmdline, &sec_attrs, NULL, TRUE,
  1381                        flags, env, dir, &start, &cp->procinfo))
  1382     goto EH_Fail;
  1383 
  1384   cp->pid = (int) cp->procinfo.dwProcessId;
  1385 
  1386   /* Hack for Windows 95, which assigns large (ie negative) pids */
  1387   if (cp->pid < 0)
  1388     cp->pid = -cp->pid;
  1389 
  1390   *pPid = cp->pid;
  1391 
  1392   return TRUE;
  1393 
  1394  EH_Fail:
  1395   DebPrint (("create_child.CreateProcess failed: %ld\n", GetLastError ()););
  1396   return FALSE;
  1397 }
  1398 
  1399 /* create_child doesn't know what emacs's file handle will be for waiting
  1400    on output from the child, so we need to make this additional call
  1401    to register the handle with the process
  1402    This way the select emulator knows how to match file handles with
  1403    entries in child_procs.  */
  1404 void
  1405 register_child (pid_t pid, int fd)
  1406 {
  1407   child_process *cp;
  1408 
  1409   cp = find_child_pid ((DWORD)pid);
  1410   if (cp == NULL)
  1411     {
  1412       DebPrint (("register_child unable to find pid %lu\n", pid));
  1413       return;
  1414     }
  1415 
  1416 #ifdef FULL_DEBUG
  1417   DebPrint (("register_child registered fd %d with pid %lu\n", fd, pid));
  1418 #endif
  1419 
  1420   cp->fd = fd;
  1421 
  1422   /* thread is initially blocked until select is called; set status so
  1423      that select will release thread */
  1424   cp->status = STATUS_READ_ACKNOWLEDGED;
  1425 
  1426   /* attach child_process to fd_info */
  1427   if (fd_info[fd].cp != NULL)
  1428     {
  1429       DebPrint (("register_child: fd_info[%d] apparently in use!\n", fd));
  1430       emacs_abort ();
  1431     }
  1432 
  1433   fd_info[fd].cp = cp;
  1434 }
  1435 
  1436 /* Called from waitpid when a process exits.  */
  1437 static void
  1438 reap_subprocess (child_process *cp)
  1439 {
  1440   if (cp->procinfo.hProcess)
  1441     {
  1442       /* Reap the process */
  1443 #ifdef FULL_DEBUG
  1444       /* Process should have already died before we are called.  */
  1445       if (WaitForSingleObject (cp->procinfo.hProcess, 0) != WAIT_OBJECT_0)
  1446         DebPrint (("reap_subprocess: child for fd %d has not died yet!", cp->fd));
  1447 #endif
  1448       CloseHandle (cp->procinfo.hProcess);
  1449       cp->procinfo.hProcess = NULL;
  1450       CloseHandle (cp->procinfo.hThread);
  1451       cp->procinfo.hThread = NULL;
  1452     }
  1453 
  1454   /* If cp->fd was not closed yet, we might be still reading the
  1455      process output, so don't free its resources just yet.  The call
  1456      to delete_child on behalf of this subprocess will be made by
  1457      sys_read when the subprocess output is fully read.  */
  1458   if (cp->fd < 0)
  1459     delete_child (cp);
  1460 }
  1461 
  1462 /* Wait for a child process specified by PID, or for any of our
  1463    existing child processes (if PID is nonpositive) to die.  When it
  1464    does, close its handle.  Return the pid of the process that died
  1465    and fill in STATUS if non-NULL.  */
  1466 
  1467 pid_t
  1468 waitpid (pid_t pid, int *status, int options)
  1469 {
  1470   DWORD active, retval;
  1471   int nh;
  1472   child_process *cp, *cps[MAX_CHILDREN];
  1473   HANDLE wait_hnd[MAX_CHILDREN];
  1474   DWORD timeout_ms;
  1475   int dont_wait = (options & WNOHANG) != 0;
  1476 
  1477   nh = 0;
  1478   /* According to Posix:
  1479 
  1480      PID = -1 means status is requested for any child process.
  1481 
  1482      PID > 0 means status is requested for a single child process
  1483      whose pid is PID.
  1484 
  1485      PID = 0 means status is requested for any child process whose
  1486      process group ID is equal to that of the calling process.  But
  1487      since Windows has only a limited support for process groups (only
  1488      for console processes and only for the purposes of passing
  1489      Ctrl-BREAK signal to them), and since we have no documented way
  1490      of determining whether a given process belongs to our group, we
  1491      treat 0 as -1.
  1492 
  1493      PID < -1 means status is requested for any child process whose
  1494      process group ID is equal to the absolute value of PID.  Again,
  1495      since we don't support process groups, we treat that as -1.  */
  1496   if (pid > 0)
  1497     {
  1498       int our_child = 0;
  1499 
  1500       /* We are requested to wait for a specific child.  */
  1501       for (cp = child_procs + (child_proc_count-1); cp >= child_procs; cp--)
  1502         {
  1503           /* Some child_procs might be sockets; ignore them.  Also
  1504              ignore subprocesses whose output is not yet completely
  1505              read.  */
  1506           if (CHILD_ACTIVE (cp)
  1507               && cp->procinfo.hProcess
  1508               && cp->pid == pid)
  1509             {
  1510               our_child = 1;
  1511               break;
  1512             }
  1513         }
  1514       if (our_child)
  1515         {
  1516           if (cp->fd < 0 || (fd_info[cp->fd].flags & FILE_AT_EOF) != 0)
  1517             {
  1518               wait_hnd[nh] = cp->procinfo.hProcess;
  1519               cps[nh] = cp;
  1520               nh++;
  1521             }
  1522           else if (dont_wait)
  1523             {
  1524               /* PID specifies our subprocess, but its status is not
  1525                  yet available.  */
  1526               return 0;
  1527             }
  1528         }
  1529       if (nh == 0)
  1530         {
  1531           /* No such child process, or nothing to wait for, so fail.  */
  1532           errno = ECHILD;
  1533           return -1;
  1534         }
  1535     }
  1536   else
  1537     {
  1538       for (cp = child_procs + (child_proc_count-1); cp >= child_procs; cp--)
  1539         {
  1540           if (CHILD_ACTIVE (cp)
  1541               && cp->procinfo.hProcess
  1542               && (cp->fd < 0 || (fd_info[cp->fd].flags & FILE_AT_EOF) != 0))
  1543             {
  1544               wait_hnd[nh] = cp->procinfo.hProcess;
  1545               cps[nh] = cp;
  1546               nh++;
  1547             }
  1548         }
  1549       if (nh == 0)
  1550         {
  1551           /* Nothing to wait on, so fail.  */
  1552           errno = ECHILD;
  1553           return -1;
  1554         }
  1555     }
  1556 
  1557   if (dont_wait)
  1558     timeout_ms = 0;
  1559   else
  1560     timeout_ms = 1000;  /* check for quit about once a second. */
  1561 
  1562   do
  1563     {
  1564       /* When child_status_changed calls us with WNOHANG in OPTIONS,
  1565          we are supposed to be non-interruptible, so don't allow
  1566          quitting in that case.  */
  1567       if (!dont_wait)
  1568         maybe_quit ();
  1569       active = WaitForMultipleObjects (nh, wait_hnd, FALSE, timeout_ms);
  1570     } while (active == WAIT_TIMEOUT && !dont_wait);
  1571 
  1572   if (active == WAIT_FAILED)
  1573     {
  1574       errno = EBADF;
  1575       return -1;
  1576     }
  1577   else if (active == WAIT_TIMEOUT && dont_wait)
  1578     {
  1579       /* PID specifies our subprocess, but it didn't exit yet, so its
  1580          status is not yet available.  */
  1581 #ifdef FULL_DEBUG
  1582       DebPrint (("Wait: PID %d not reap yet\n", cp->pid));
  1583 #endif
  1584       return 0;
  1585     }
  1586   else if (active >= WAIT_OBJECT_0
  1587            && active < WAIT_OBJECT_0+MAXIMUM_WAIT_OBJECTS)
  1588     {
  1589       active -= WAIT_OBJECT_0;
  1590     }
  1591   else if (active >= WAIT_ABANDONED_0
  1592            && active < WAIT_ABANDONED_0+MAXIMUM_WAIT_OBJECTS)
  1593     {
  1594       active -= WAIT_ABANDONED_0;
  1595     }
  1596   else
  1597     emacs_abort ();
  1598 
  1599   if (!GetExitCodeProcess (wait_hnd[active], &retval))
  1600     {
  1601       DebPrint (("Wait.GetExitCodeProcess failed with %lu\n",
  1602                  GetLastError ()));
  1603       retval = 1;
  1604     }
  1605   if (retval == STILL_ACTIVE)
  1606     {
  1607       /* Should never happen.  But it does, with invoking git-gui.exe
  1608          asynchronously.  So we punt, and just report this process as
  1609          exited with exit code 259, when we are called with WNOHANG
  1610          from child_status_changed, because in that case we already
  1611          _know_ the process has died.  */
  1612       DebPrint (("Wait.WaitForMultipleObjects returned an active process\n"));
  1613       if (!(pid > 0 && dont_wait))
  1614         {
  1615           errno = EINVAL;
  1616           return -1;
  1617         }
  1618     }
  1619 
  1620   /* Massage the exit code from the process to match the format expected
  1621      by the WIFSTOPPED et al macros in syswait.h.  Only WIFSIGNALED and
  1622      WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT.  */
  1623 
  1624   if (retval == STATUS_CONTROL_C_EXIT)
  1625     retval = SIGINT;
  1626   else
  1627     retval <<= 8;
  1628 
  1629   if (pid > 0 && active != 0)
  1630     emacs_abort ();
  1631   cp = cps[active];
  1632   pid = cp->pid;
  1633 #ifdef FULL_DEBUG
  1634   DebPrint (("Wait signaled with process pid %d\n", cp->pid));
  1635 #endif
  1636 
  1637   if (status)
  1638     *status = retval;
  1639   reap_subprocess (cp);
  1640 
  1641   return pid;
  1642 }
  1643 
  1644 int
  1645 open_input_file (file_data *p_file, char *filename)
  1646 {
  1647   HANDLE file;
  1648   HANDLE file_mapping;
  1649   void  *file_base;
  1650   unsigned long size, upper_size;
  1651 
  1652   file = CreateFileA (filename, GENERIC_READ, FILE_SHARE_READ, NULL,
  1653                       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  1654   if (file == INVALID_HANDLE_VALUE)
  1655     return FALSE;
  1656 
  1657   size = GetFileSize (file, &upper_size);
  1658   file_mapping = CreateFileMapping (file, NULL, PAGE_READONLY,
  1659                                     0, size, NULL);
  1660   if (!file_mapping)
  1661     return FALSE;
  1662 
  1663   file_base = MapViewOfFile (file_mapping, FILE_MAP_READ, 0, 0, size);
  1664   if (file_base == 0)
  1665     return FALSE;
  1666 
  1667   p_file->name = filename;
  1668   p_file->size = size;
  1669   p_file->file = file;
  1670   p_file->file_mapping = file_mapping;
  1671   p_file->file_base = file_base;
  1672 
  1673   return TRUE;
  1674 }
  1675 
  1676 /* Return pointer to section header for section containing the given
  1677    relative virtual address. */
  1678 IMAGE_SECTION_HEADER *
  1679 rva_to_section (DWORD_PTR rva, IMAGE_NT_HEADERS * nt_header)
  1680 {
  1681   PIMAGE_SECTION_HEADER section;
  1682   int i;
  1683 
  1684   section = IMAGE_FIRST_SECTION (nt_header);
  1685 
  1686   for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++)
  1687     {
  1688       /* Some linkers (eg. the NT SDK linker I believe) swapped the
  1689          meaning of these two values - or rather, they ignored
  1690          VirtualSize entirely and always set it to zero.  This affects
  1691          some very old exes (eg. gzip dated Dec 1993).  Since
  1692          w32_executable_type relies on this function to work reliably,
  1693          we need to cope with this.  */
  1694       DWORD_PTR real_size = max (section->SizeOfRawData,
  1695                              section->Misc.VirtualSize);
  1696       if (rva >= section->VirtualAddress
  1697           && rva < section->VirtualAddress + real_size)
  1698         return section;
  1699       section++;
  1700     }
  1701   return NULL;
  1702 }
  1703 
  1704 /* Close the system structures associated with the given file.  */
  1705 void
  1706 close_file_data (file_data *p_file)
  1707 {
  1708   UnmapViewOfFile (p_file->file_base);
  1709   CloseHandle (p_file->file_mapping);
  1710   /* For the case of output files, set final size.  */
  1711   SetFilePointer (p_file->file, p_file->size, NULL, FILE_BEGIN);
  1712   SetEndOfFile (p_file->file);
  1713   CloseHandle (p_file->file);
  1714 }
  1715 
  1716 /* Old versions of w32api headers don't have separate 32-bit and
  1717    64-bit defines, but the one they have matches the 32-bit variety.  */
  1718 #ifndef IMAGE_NT_OPTIONAL_HDR32_MAGIC
  1719 # define IMAGE_NT_OPTIONAL_HDR32_MAGIC IMAGE_NT_OPTIONAL_HDR_MAGIC
  1720 # define IMAGE_OPTIONAL_HEADER32 IMAGE_OPTIONAL_HEADER
  1721 #endif
  1722 
  1723 /* Implementation note: This function works with file names encoded in
  1724    the current ANSI codepage.  */
  1725 static int
  1726 w32_executable_type (char * filename,
  1727                      int * is_dos_app,
  1728                      int * is_cygnus_app,
  1729                      int * is_msys_app,
  1730                      int * is_gui_app)
  1731 {
  1732   file_data executable;
  1733   char * p;
  1734   int retval = 0;
  1735 
  1736   /* Default values in case we can't tell for sure.  */
  1737   *is_dos_app = FALSE;
  1738   *is_cygnus_app = FALSE;
  1739   *is_msys_app = FALSE;
  1740   *is_gui_app = FALSE;
  1741 
  1742   if (!open_input_file (&executable, filename))
  1743     return -1;
  1744 
  1745   p = strrchr (filename, '.');
  1746 
  1747   /* We can only identify DOS .com programs from the extension. */
  1748   if (p && xstrcasecmp (p, ".com") == 0)
  1749     *is_dos_app = TRUE;
  1750   else if (p && (xstrcasecmp (p, ".bat") == 0
  1751                  || xstrcasecmp (p, ".cmd") == 0))
  1752     {
  1753       /* A DOS shell script - it appears that CreateProcess is happy to
  1754          accept this (somewhat surprisingly); presumably it looks at
  1755          COMSPEC to determine what executable to actually invoke.
  1756          Therefore, we have to do the same here as well. */
  1757       /* Actually, I think it uses the program association for that
  1758          extension, which is defined in the registry.  */
  1759       p = egetenv ("COMSPEC");
  1760       if (p)
  1761         retval = w32_executable_type (p, is_dos_app, is_cygnus_app, is_msys_app,
  1762                                       is_gui_app);
  1763     }
  1764   else
  1765     {
  1766       /* Look for DOS .exe signature - if found, we must also check that
  1767          it isn't really a 16- or 32-bit Windows exe, since both formats
  1768          start with a DOS program stub.  Note that 16-bit Windows
  1769          executables use the OS/2 1.x format. */
  1770 
  1771       IMAGE_DOS_HEADER * dos_header;
  1772       IMAGE_NT_HEADERS * nt_header;
  1773 
  1774       dos_header = (PIMAGE_DOS_HEADER) executable.file_base;
  1775       if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
  1776         goto unwind;
  1777 
  1778       nt_header = (PIMAGE_NT_HEADERS) ((unsigned char *) dos_header + dos_header->e_lfanew);
  1779 
  1780       if ((char *) nt_header > (char *) dos_header + executable.size)
  1781         {
  1782           /* Some dos headers (pkunzip) have bogus e_lfanew fields.  */
  1783           *is_dos_app = TRUE;
  1784         }
  1785       else if (nt_header->Signature != IMAGE_NT_SIGNATURE
  1786                && LOWORD (nt_header->Signature) != IMAGE_OS2_SIGNATURE)
  1787         {
  1788           *is_dos_app = TRUE;
  1789         }
  1790       else if (nt_header->Signature == IMAGE_NT_SIGNATURE)
  1791         {
  1792           IMAGE_DATA_DIRECTORY *data_dir = NULL;
  1793           if (nt_header->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
  1794             {
  1795               /* Ensure we are using the 32 bit structure.  */
  1796               IMAGE_OPTIONAL_HEADER32 *opt
  1797                 = (IMAGE_OPTIONAL_HEADER32*) &(nt_header->OptionalHeader);
  1798               data_dir = opt->DataDirectory;
  1799               *is_gui_app = (opt->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI);
  1800             }
  1801           /* MingW 3.12 has the required 64 bit structs, but in case older
  1802              versions don't, only check 64 bit exes if we know how.  */
  1803 #ifdef IMAGE_NT_OPTIONAL_HDR64_MAGIC
  1804           else if (nt_header->OptionalHeader.Magic
  1805                    == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
  1806             {
  1807               IMAGE_OPTIONAL_HEADER64 *opt
  1808                 = (IMAGE_OPTIONAL_HEADER64*) &(nt_header->OptionalHeader);
  1809               data_dir = opt->DataDirectory;
  1810               *is_gui_app = (opt->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI);
  1811             }
  1812 #endif
  1813           if (data_dir)
  1814             {
  1815               /* Look for Cygwin DLL in the DLL import list. */
  1816               IMAGE_DATA_DIRECTORY import_dir
  1817                 = data_dir[IMAGE_DIRECTORY_ENTRY_IMPORT];
  1818 
  1819               /* Import directory can be missing in .NET DLLs.  */
  1820               if (import_dir.VirtualAddress != 0)
  1821                 {
  1822                   IMAGE_SECTION_HEADER *section
  1823                     = rva_to_section (import_dir.VirtualAddress, nt_header);
  1824                   if (!section)
  1825                     emacs_abort ();
  1826 
  1827                   IMAGE_IMPORT_DESCRIPTOR * imports =
  1828                     RVA_TO_PTR (import_dir.VirtualAddress, section,
  1829                                 executable);
  1830 
  1831                   for ( ; imports->Name; imports++)
  1832                     {
  1833                       section = rva_to_section (imports->Name, nt_header);
  1834                       if (!section)
  1835                         emacs_abort ();
  1836 
  1837                       char * dllname = RVA_TO_PTR (imports->Name, section,
  1838                                                    executable);
  1839 
  1840                       /* The exact name of the Cygwin DLL has changed with
  1841                          various releases, but hopefully this will be
  1842                          reasonably future-proof.  */
  1843                       if (strncmp (dllname, "cygwin", 6) == 0)
  1844                         {
  1845                           *is_cygnus_app = TRUE;
  1846                           break;
  1847                         }
  1848                       else if (strncmp (dllname, "msys-", 5) == 0)
  1849                         {
  1850                           /* This catches both MSYS 1.x and MSYS2
  1851                              executables (the DLL name is msys-1.0.dll and
  1852                              msys-2.0.dll, respectively).  There doesn't
  1853                              seem to be a reason to distinguish between
  1854                              the two, for now.  */
  1855                           *is_msys_app = TRUE;
  1856                           break;
  1857                         }
  1858                     }
  1859                 }
  1860             }
  1861         }
  1862     }
  1863 
  1864 unwind:
  1865   close_file_data (&executable);
  1866   return retval;
  1867 }
  1868 
  1869 static int
  1870 compare_env (const void *strp1, const void *strp2)
  1871 {
  1872   const char *str1 = *(const char **)strp1, *str2 = *(const char **)strp2;
  1873 
  1874   while (*str1 && *str2 && *str1 != '=' && *str2 != '=')
  1875     {
  1876       /* Sort order in command.com/cmd.exe is based on uppercasing
  1877          names, so do the same here.  */
  1878       if (toupper (*str1) > toupper (*str2))
  1879         return 1;
  1880       else if (toupper (*str1) < toupper (*str2))
  1881         return -1;
  1882       str1++, str2++;
  1883     }
  1884 
  1885   if (*str1 == '=' && *str2 == '=')
  1886     return 0;
  1887   else if (*str1 == '=')
  1888     return -1;
  1889   else
  1890     return 1;
  1891 }
  1892 
  1893 static void
  1894 merge_and_sort_env (char **envp1, char **envp2, char **new_envp)
  1895 {
  1896   char **optr, **nptr;
  1897   int num;
  1898 
  1899   nptr = new_envp;
  1900   optr = envp1;
  1901   while (*optr)
  1902     *nptr++ = *optr++;
  1903   num = optr - envp1;
  1904 
  1905   optr = envp2;
  1906   while (*optr)
  1907     *nptr++ = *optr++;
  1908   num += optr - envp2;
  1909 
  1910   qsort (new_envp, num, sizeof (char *), compare_env);
  1911 
  1912   *nptr = NULL;
  1913 }
  1914 
  1915 /* When a new child process is created we need to register it in our list,
  1916    so intercept spawn requests.  */
  1917 int
  1918 sys_spawnve (int mode, char *cmdname, char **argv, char **envp)
  1919 {
  1920   Lisp_Object program, full;
  1921   char *cmdline, *env, *parg, **targ;
  1922   int arglen, numenv;
  1923   pid_t pid;
  1924   child_process *cp;
  1925   int is_dos_app, is_cygnus_app, is_msys_app, is_gui_app;
  1926   int do_quoting = 0;
  1927   /* We pass our process ID to our children by setting up an environment
  1928      variable in their environment.  */
  1929   char ppid_env_var_buffer[64];
  1930   char *extra_env[] = {ppid_env_var_buffer, NULL};
  1931   /* These are the characters that cause an argument to need quoting.
  1932      Arguments with whitespace characters need quoting to prevent the
  1933      argument being split into two or more. Arguments with wildcards
  1934      are also quoted, for consistency with posix platforms, where wildcards
  1935      are not expanded if we run the program directly without a shell.
  1936      Some extra whitespace characters need quoting in Cygwin/MSYS programs,
  1937      so this list is conditionally modified below.  */
  1938   const char *sepchars = " \t*?";
  1939   /* This is for native w32 apps; modified below for Cygwin/MSUS apps.  */
  1940   char escape_char = '\\';
  1941   char cmdname_a[MAX_PATH];
  1942 
  1943   /* We don't care about the other modes */
  1944   if (mode != _P_NOWAIT)
  1945     {
  1946       errno = EINVAL;
  1947       return -1;
  1948     }
  1949 
  1950   /* Handle executable names without an executable suffix.  The caller
  1951      already searched exec-path and verified the file is executable,
  1952      but start-process doesn't do that for file names that are already
  1953      absolute.  So we double-check this here, just in case.  */
  1954   if (faccessat (AT_FDCWD, cmdname, X_OK, AT_EACCESS) != 0)
  1955     {
  1956       program = build_string (cmdname);
  1957       full = Qnil;
  1958       openp (Vexec_path, program, Vexec_suffixes, &full, make_fixnum (X_OK),
  1959              0, 0, NULL);
  1960       if (NILP (full))
  1961         {
  1962           errno = EINVAL;
  1963           return -1;
  1964         }
  1965       program = ENCODE_FILE (full);
  1966       cmdname = SSDATA (program);
  1967     }
  1968   else
  1969     {
  1970       char *p = alloca (strlen (cmdname) + 1);
  1971 
  1972       /* Don't change the command name we were passed by our caller
  1973          (unixtodos_filename below will destructively mirror forward
  1974          slashes).  */
  1975       cmdname = strcpy (p, cmdname);
  1976     }
  1977 
  1978   /* make sure argv[0] and cmdname are both in DOS format */
  1979   unixtodos_filename (cmdname);
  1980   /* argv[0] was encoded by caller using ENCODE_FILE, so it is in
  1981      UTF-8.  All the other arguments are encoded by ENCODE_SYSTEM or
  1982      some such, and are in some ANSI codepage.  We need to have
  1983      argv[0] encoded in ANSI codepage.  */
  1984   filename_to_ansi (cmdname, cmdname_a);
  1985   /* We explicitly require that the command's file name be encodable
  1986      in the current ANSI codepage, because we will be invoking it via
  1987      the ANSI APIs.  */
  1988   if (_mbspbrk ((unsigned char *)cmdname_a, (const unsigned char *)"?"))
  1989     {
  1990       errno = ENOENT;
  1991       return -1;
  1992     }
  1993   /* From here on, CMDNAME is an ANSI-encoded string.  */
  1994   cmdname = cmdname_a;
  1995   argv[0] = cmdname;
  1996 
  1997   /* Determine whether program is a 16-bit DOS executable, or a 32-bit
  1998      Windows executable that is implicitly linked to the Cygnus or
  1999      MSYS dll (implying it was compiled with the Cygnus/MSYS GNU
  2000      toolchain and hence relies on cygwin.dll or MSYS DLL to parse the
  2001      command line - we use this to decide how to escape quote chars in
  2002      command line args that must be quoted).
  2003 
  2004      Also determine whether it is a GUI app, so that we don't hide its
  2005      initial window unless specifically requested.  */
  2006   w32_executable_type (cmdname, &is_dos_app, &is_cygnus_app, &is_msys_app,
  2007                        &is_gui_app);
  2008 
  2009   /* On Windows 95, if cmdname is a DOS app, we invoke a helper
  2010      application to start it by specifying the helper app as cmdname,
  2011      while leaving the real app name as argv[0].  */
  2012   if (is_dos_app)
  2013     {
  2014       char *p;
  2015 
  2016       cmdname = alloca (MAX_PATH);
  2017       if (egetenv ("CMDPROXY"))
  2018         {
  2019           /* Implementation note: since process-environment, where
  2020              'egetenv' looks, is encoded in the system codepage, we
  2021              don't need to encode the cmdproxy file name if we get it
  2022              from the environment.  */
  2023           strcpy (cmdname, egetenv ("CMDPROXY"));
  2024         }
  2025       else
  2026         {
  2027           char *q = lispstpcpy (cmdname,
  2028                                 /* exec-directory needs to be encoded.  */
  2029                                 ansi_encode_filename (Vexec_directory));
  2030           /* If we are run from the source tree, use cmdproxy.exe from
  2031              the same source tree.  */
  2032           for (p = q - 2; p > cmdname; p = CharPrevA (cmdname, p))
  2033             if (*p == '/')
  2034               break;
  2035           if (*p == '/' && xstrcasecmp (p, "/lib-src/") == 0)
  2036             q = stpcpy (p, "/nt/");
  2037           strcpy (q, "cmdproxy.exe");
  2038         }
  2039 
  2040       /* Can't use unixtodos_filename here, since that needs its file
  2041          name argument encoded in UTF-8.  */
  2042       for (p = cmdname; *p; p = CharNextA (p))
  2043         if (*p == '/')
  2044           *p = '\\';
  2045     }
  2046 
  2047   /* we have to do some conjuring here to put argv and envp into the
  2048      form CreateProcess wants...  argv needs to be a space separated/null
  2049      terminated list of parameters, and envp is a null
  2050      separated/double-null terminated list of parameters.
  2051 
  2052      Additionally, zero-length args and args containing whitespace or
  2053      quote chars need to be wrapped in double quotes - for this to work,
  2054      embedded quotes need to be escaped as well.  The aim is to ensure
  2055      the child process reconstructs the argv array we start with
  2056      exactly, so we treat quotes at the beginning and end of arguments
  2057      as embedded quotes.
  2058 
  2059      The w32 GNU-based library from Cygnus doubles quotes to escape
  2060      them, while MSVC uses backslash for escaping.  (Actually the MSVC
  2061      startup code does attempt to recognize doubled quotes and accept
  2062      them, but gets it wrong and ends up requiring three quotes to get a
  2063      single embedded quote!)  So by default we decide whether to use
  2064      quote or backslash as the escape character based on whether the
  2065      binary is apparently a Cygnus compiled app.
  2066 
  2067      Note that using backslash to escape embedded quotes requires
  2068      additional special handling if an embedded quote is already
  2069      preceded by backslash, or if an arg requiring quoting ends with
  2070      backslash.  In such cases, the run of escape characters needs to be
  2071      doubled.  For consistency, we apply this special handling as long
  2072      as the escape character is not quote.
  2073 
  2074      Since we have no idea how large argv and envp are likely to be we
  2075      figure out list lengths on the fly and allocate them.  */
  2076 
  2077   if (!NILP (Vw32_quote_process_args))
  2078     {
  2079       do_quoting = 1;
  2080       /* Override escape char by binding w32-quote-process-args to
  2081          desired character, or use t for auto-selection.  */
  2082       if (FIXNUMP (Vw32_quote_process_args))
  2083         escape_char = XFIXNUM (Vw32_quote_process_args);
  2084       else
  2085         escape_char = (is_cygnus_app || is_msys_app) ? '"' : '\\';
  2086     }
  2087 
  2088   /* Cygwin/MSYS apps need quoting a bit more often.  */
  2089   if (escape_char == '"')
  2090     sepchars = "\r\n\t\f '";
  2091 
  2092   /* do argv...  */
  2093   arglen = 0;
  2094   targ = argv;
  2095   while (*targ)
  2096     {
  2097       char * p = *targ;
  2098       int need_quotes = 0;
  2099       int escape_char_run = 0;
  2100 
  2101       if (*p == 0)
  2102         need_quotes = 1;
  2103       for ( ; *p; p++)
  2104         {
  2105           if (escape_char == '"' && *p == '\\')
  2106             /* If it's a Cygwin/MSYS app, \ needs to be escaped.  */
  2107             arglen++;
  2108           else if (*p == '"')
  2109             {
  2110               /* allow for embedded quotes to be escaped */
  2111               arglen++;
  2112               need_quotes = 1;
  2113               /* handle the case where the embedded quote is already escaped */
  2114               if (escape_char_run > 0)
  2115                 {
  2116                   /* To preserve the arg exactly, we need to double the
  2117                      preceding escape characters (plus adding one to
  2118                      escape the quote character itself).  */
  2119                   arglen += escape_char_run;
  2120                 }
  2121             }
  2122           else if (strchr (sepchars, *p) != NULL)
  2123             {
  2124               need_quotes = 1;
  2125             }
  2126 
  2127           if (*p == escape_char && escape_char != '"')
  2128             escape_char_run++;
  2129           else
  2130             escape_char_run = 0;
  2131         }
  2132       if (need_quotes)
  2133         {
  2134           arglen += 2;
  2135           /* handle the case where the arg ends with an escape char - we
  2136              must not let the enclosing quote be escaped.  */
  2137           if (escape_char_run > 0)
  2138             arglen += escape_char_run;
  2139         }
  2140       arglen += strlen (*targ++) + 1;
  2141     }
  2142   cmdline = alloca (arglen);
  2143   targ = argv;
  2144   parg = cmdline;
  2145   while (*targ)
  2146     {
  2147       char * p = *targ;
  2148       int need_quotes = 0;
  2149 
  2150       if (*p == 0)
  2151         need_quotes = 1;
  2152 
  2153       if (do_quoting)
  2154         {
  2155           for ( ; *p; p++)
  2156             if ((strchr (sepchars, *p) != NULL) || *p == '"')
  2157               need_quotes = 1;
  2158         }
  2159       if (need_quotes)
  2160         {
  2161           int escape_char_run = 0;
  2162           /* char * first; */
  2163           /* char * last; */
  2164 
  2165           p = *targ;
  2166           /* first = p; */
  2167           /* last = p + strlen (p) - 1; */
  2168           *parg++ = '"';
  2169 #if 0
  2170           /* This version does not escape quotes if they occur at the
  2171              beginning or end of the arg - this could lead to incorrect
  2172              behavior when the arg itself represents a command line
  2173              containing quoted args.  I believe this was originally done
  2174              as a hack to make some things work, before
  2175              `w32-quote-process-args' was added.  */
  2176           while (*p)
  2177             {
  2178               if (*p == '"' && p > first && p < last)
  2179                 *parg++ = escape_char;  /* escape embedded quotes */
  2180               *parg++ = *p++;
  2181             }
  2182 #else
  2183           for ( ; *p; p++)
  2184             {
  2185               if (*p == '"')
  2186                 {
  2187                   /* double preceding escape chars if any */
  2188                   while (escape_char_run > 0)
  2189                     {
  2190                       *parg++ = escape_char;
  2191                       escape_char_run--;
  2192                     }
  2193                   /* escape all quote chars, even at beginning or end */
  2194                   *parg++ = escape_char;
  2195                 }
  2196               else if (escape_char == '"' && *p == '\\')
  2197                 *parg++ = '\\';
  2198               *parg++ = *p;
  2199 
  2200               if (*p == escape_char && escape_char != '"')
  2201                 escape_char_run++;
  2202               else
  2203                 escape_char_run = 0;
  2204             }
  2205           /* double escape chars before enclosing quote */
  2206           while (escape_char_run > 0)
  2207             {
  2208               *parg++ = escape_char;
  2209               escape_char_run--;
  2210             }
  2211 #endif
  2212           *parg++ = '"';
  2213         }
  2214       else
  2215         {
  2216           strcpy (parg, *targ);
  2217           parg += strlen (*targ);
  2218         }
  2219       *parg++ = ' ';
  2220       targ++;
  2221     }
  2222   *--parg = '\0';
  2223 
  2224   /* and envp...  */
  2225   arglen = 1;
  2226   targ = envp;
  2227   numenv = 1; /* for end null */
  2228   while (*targ)
  2229     {
  2230       arglen += strlen (*targ++) + 1;
  2231       numenv++;
  2232     }
  2233   /* extra env vars... */
  2234   sprintf (ppid_env_var_buffer, "EM_PARENT_PROCESS_ID=%lu",
  2235            GetCurrentProcessId ());
  2236   arglen += strlen (ppid_env_var_buffer) + 1;
  2237   numenv++;
  2238 
  2239   /* merge env passed in and extra env into one, and sort it.  */
  2240   targ = (char **) alloca (numenv * sizeof (char *));
  2241   merge_and_sort_env (envp, extra_env, targ);
  2242 
  2243   /* concatenate env entries.  */
  2244   env = alloca (arglen);
  2245   parg = env;
  2246   while (*targ)
  2247     {
  2248       strcpy (parg, *targ);
  2249       parg += strlen (*targ++);
  2250       *parg++ = '\0';
  2251     }
  2252   *parg++ = '\0';
  2253   *parg = '\0';
  2254 
  2255   cp = new_child ();
  2256   if (cp == NULL)
  2257     {
  2258       errno = EAGAIN;
  2259       return -1;
  2260     }
  2261 
  2262   /* Now create the process.  */
  2263   if (!create_child (cmdname, cmdline, env, is_gui_app, &pid, cp))
  2264     {
  2265       delete_child (cp);
  2266       errno = ENOEXEC;
  2267       return -1;
  2268     }
  2269 
  2270   return pid;
  2271 }
  2272 
  2273 /* Emulate the select call.
  2274    Wait for available input on any of the given rfds, or timeout if
  2275    a timeout is given and no input is detected.  wfds are supported
  2276    only for asynchronous 'connect' calls.  efds are not supported
  2277    and must be NULL.
  2278 
  2279    For simplicity, we detect the death of child processes here and
  2280    synchronously call the SIGCHLD handler.  Since it is possible for
  2281    children to be created without a corresponding pipe handle from which
  2282    to read output, we wait separately on the process handles as well as
  2283    the char_avail events for each process pipe.  We only call
  2284    wait/reap_process when the process actually terminates.
  2285 
  2286    To reduce the number of places in which Emacs can be hung such that
  2287    C-g is not able to interrupt it, we always wait on interrupt_handle
  2288    (which is signaled by the input thread when C-g is detected).  If we
  2289    detect that we were woken up by C-g, we return -1 with errno set to
  2290    EINTR as on Unix.  */
  2291 
  2292 /* From w32console.c */
  2293 extern HANDLE keyboard_handle;
  2294 
  2295 /* From w32xfns.c */
  2296 extern HANDLE interrupt_handle;
  2297 
  2298 /* From process.c */
  2299 extern int proc_buffered_char[];
  2300 
  2301 int
  2302 sys_select (int nfds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
  2303             const struct timespec *timeout, const sigset_t *ignored)
  2304 {
  2305   SELECT_TYPE orfds, owfds;
  2306   DWORD timeout_ms, start_time;
  2307   int i, nh, nc, nr;
  2308   DWORD active;
  2309   child_process *cp, *cps[MAX_CHILDREN];
  2310   HANDLE wait_hnd[MAXDESC + MAX_CHILDREN];
  2311   int fdindex[MAXDESC];   /* mapping from wait handles back to descriptors */
  2312 
  2313   timeout_ms =
  2314     timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 1000000) : INFINITE;
  2315 
  2316   /* If the descriptor sets are NULL but timeout isn't, then just Sleep.  */
  2317   if (rfds == NULL && wfds == NULL && efds == NULL && timeout != NULL)
  2318     {
  2319       Sleep (timeout_ms);
  2320       return 0;
  2321     }
  2322 
  2323   /* Otherwise, we only handle rfds and wfds, so fail otherwise.  */
  2324   if ((rfds == NULL && wfds == NULL) || efds != NULL)
  2325     {
  2326       errno = EINVAL;
  2327       return -1;
  2328     }
  2329 
  2330   if (rfds)
  2331     {
  2332       orfds = *rfds;
  2333       FD_ZERO (rfds);
  2334     }
  2335   else
  2336     FD_ZERO (&orfds);
  2337   if (wfds)
  2338     {
  2339       owfds = *wfds;
  2340       FD_ZERO (wfds);
  2341     }
  2342   else
  2343     FD_ZERO (&owfds);
  2344   nr = 0;
  2345 
  2346   /* If interrupt_handle is available and valid, always wait on it, to
  2347      detect C-g (quit).  */
  2348   nh = 0;
  2349   if (interrupt_handle && interrupt_handle != INVALID_HANDLE_VALUE)
  2350     {
  2351       wait_hnd[0] = interrupt_handle;
  2352       fdindex[0] = -1;
  2353       nh++;
  2354     }
  2355 
  2356   /* Build a list of pipe handles to wait on.  */
  2357   for (i = 0; i < nfds; i++)
  2358     if (FD_ISSET (i, &orfds) || FD_ISSET (i, &owfds))
  2359       {
  2360         if (i == 0)
  2361           {
  2362             if (keyboard_handle)
  2363               {
  2364                 /* Handle stdin specially */
  2365                 wait_hnd[nh] = keyboard_handle;
  2366                 fdindex[nh] = i;
  2367                 nh++;
  2368               }
  2369 
  2370             /* Check for any emacs-generated input in the queue since
  2371                it won't be detected in the wait */
  2372             if (rfds && detect_input_pending ())
  2373               {
  2374                 FD_SET (i, rfds);
  2375                 return 1;
  2376               }
  2377             else if (noninteractive)
  2378               {
  2379                 if (handle_file_notifications (NULL))
  2380                   return 1;
  2381               }
  2382           }
  2383         else
  2384           {
  2385             /* Child process and socket/comm port input.  */
  2386             cp = fd_info[i].cp;
  2387             if (FD_ISSET (i, &owfds)
  2388                 && cp
  2389                 && (fd_info[i].flags & FILE_CONNECT) == 0)
  2390               {
  2391                 DebPrint (("sys_select: fd %d is in wfds, but FILE_CONNECT is reset!\n", i));
  2392                 cp = NULL;
  2393               }
  2394             if (cp)
  2395               {
  2396                 int current_status = cp->status;
  2397 
  2398                 if (current_status == STATUS_READ_ACKNOWLEDGED)
  2399                   {
  2400                     /* Tell reader thread which file handle to use. */
  2401                     cp->fd = i;
  2402                     /* Zero out the error code.  */
  2403                     cp->errcode = 0;
  2404                     /* Wake up the reader thread for this process */
  2405                     cp->status = STATUS_READ_READY;
  2406                     if (!SetEvent (cp->char_consumed))
  2407                       DebPrint (("sys_select.SetEvent failed with "
  2408                                  "%lu for fd %ld\n", GetLastError (), i));
  2409                   }
  2410 
  2411 #ifdef CHECK_INTERLOCK
  2412                 /* slightly crude cross-checking of interlock between threads */
  2413 
  2414                 current_status = cp->status;
  2415                 if (WaitForSingleObject (cp->char_avail, 0) == WAIT_OBJECT_0)
  2416                   {
  2417                     /* char_avail has been signaled, so status (which may
  2418                        have changed) should indicate read has completed
  2419                        but has not been acknowledged. */
  2420                     current_status = cp->status;
  2421                     if (current_status != STATUS_READ_SUCCEEDED
  2422                         && current_status != STATUS_READ_FAILED)
  2423                       DebPrint (("char_avail set, but read not completed: status %d\n",
  2424                                  current_status));
  2425                   }
  2426                 else
  2427                   {
  2428                     /* char_avail has not been signaled, so status should
  2429                        indicate that read is in progress; small possibility
  2430                        that read has completed but event wasn't yet signaled
  2431                        when we tested it (because a context switch occurred
  2432                        or if running on separate CPUs). */
  2433                     if (current_status != STATUS_READ_READY
  2434                         && current_status != STATUS_READ_IN_PROGRESS
  2435                         && current_status != STATUS_READ_SUCCEEDED
  2436                         && current_status != STATUS_READ_FAILED)
  2437                       DebPrint (("char_avail reset, but read status is bad: %d\n",
  2438                                  current_status));
  2439                   }
  2440 #endif
  2441                 wait_hnd[nh] = cp->char_avail;
  2442                 fdindex[nh] = i;
  2443                 if (!wait_hnd[nh]) emacs_abort ();
  2444                 nh++;
  2445 #ifdef FULL_DEBUG
  2446                 DebPrint (("select waiting on child %d fd %d\n",
  2447                            cp-child_procs, i));
  2448 #endif
  2449               }
  2450             else
  2451               {
  2452                 /* Unable to find something to wait on for this fd, skip */
  2453 
  2454                 /* Note that this is not a fatal error, and can in fact
  2455                    happen in unusual circumstances.  Specifically, if
  2456                    sys_spawnve fails, eg. because the program doesn't
  2457                    exist, and debug-on-error is t so Fsignal invokes a
  2458                    nested input loop, then the process output pipe is
  2459                    still included in input_wait_mask with no child_proc
  2460                    associated with it.  (It is removed when the debugger
  2461                    exits the nested input loop and the error is thrown.)  */
  2462 
  2463                 DebPrint (("sys_select: fd %ld is invalid! ignoring\n", i));
  2464               }
  2465           }
  2466       }
  2467 
  2468 count_children:
  2469   /* Add handles of child processes.  */
  2470   nc = 0;
  2471   for (cp = child_procs + (child_proc_count-1); cp >= child_procs; cp--)
  2472     /* Some child_procs might be sockets; ignore them.  Also some
  2473        children may have died already, but we haven't finished reading
  2474        the process output; ignore them too.  */
  2475     if ((CHILD_ACTIVE (cp) && cp->procinfo.hProcess)
  2476         && (cp->fd < 0
  2477             || (fd_info[cp->fd].flags & FILE_SEND_SIGCHLD) == 0
  2478             || (fd_info[cp->fd].flags & FILE_AT_EOF) != 0)
  2479         )
  2480       {
  2481         wait_hnd[nh + nc] = cp->procinfo.hProcess;
  2482         cps[nc] = cp;
  2483         nc++;
  2484       }
  2485 
  2486   /* Nothing to look for, so we didn't find anything */
  2487   if (nh + nc == 0)
  2488     {
  2489       if (timeout)
  2490         Sleep (timeout_ms);
  2491       if (noninteractive)
  2492         {
  2493           if (handle_file_notifications (NULL))
  2494             return 1;
  2495         }
  2496       return 0;
  2497     }
  2498 
  2499   start_time = GetTickCount ();
  2500 
  2501   /* Wait for input or child death to be signaled.  If user input is
  2502      allowed, then also accept window messages.  */
  2503   if (FD_ISSET (0, &orfds))
  2504     active = MsgWaitForMultipleObjects (nh + nc, wait_hnd, FALSE, timeout_ms,
  2505                                         QS_ALLINPUT);
  2506   else
  2507     active = WaitForMultipleObjects (nh + nc, wait_hnd, FALSE, timeout_ms);
  2508 
  2509   if (active == WAIT_FAILED)
  2510     {
  2511       DebPrint (("select.WaitForMultipleObjects (%d, %lu) failed with %lu\n",
  2512                  nh + nc, timeout_ms, GetLastError ()));
  2513       /* don't return EBADF - this causes wait_reading_process_output to
  2514          abort; WAIT_FAILED is returned when single-stepping under
  2515          Windows 95 after switching thread focus in debugger, and
  2516          possibly at other times. */
  2517       errno = EINTR;
  2518       return -1;
  2519     }
  2520   else if (active == WAIT_TIMEOUT)
  2521     {
  2522       if (noninteractive)
  2523         {
  2524           if (handle_file_notifications (NULL))
  2525             return 1;
  2526         }
  2527       return 0;
  2528     }
  2529   else if (active >= WAIT_OBJECT_0
  2530            && active < WAIT_OBJECT_0+MAXIMUM_WAIT_OBJECTS)
  2531     {
  2532       active -= WAIT_OBJECT_0;
  2533     }
  2534   else if (active >= WAIT_ABANDONED_0
  2535            && active < WAIT_ABANDONED_0+MAXIMUM_WAIT_OBJECTS)
  2536     {
  2537       active -= WAIT_ABANDONED_0;
  2538     }
  2539   else
  2540     emacs_abort ();
  2541 
  2542   /* Loop over all handles after active (now officially documented as
  2543      being the first signaled handle in the array).  We do this to
  2544      ensure fairness, so that all channels with data available will be
  2545      processed - otherwise higher numbered channels could be starved. */
  2546   do
  2547     {
  2548       if (active == nh + nc)
  2549         {
  2550           /* There are messages in the lisp thread's queue; we must
  2551              drain the queue now to ensure they are processed promptly,
  2552              because if we don't do so, we will not be woken again until
  2553              further messages arrive.
  2554 
  2555              NB. If ever we allow window message procedures to callback
  2556              into lisp, we will need to ensure messages are dispatched
  2557              at a safe time for lisp code to be run (*), and we may also
  2558              want to provide some hooks in the dispatch loop to cater
  2559              for modeless dialogs created by lisp (ie. to register
  2560              window handles to pass to IsDialogMessage).
  2561 
  2562              (*) Note that MsgWaitForMultipleObjects above is an
  2563              internal dispatch point for messages that are sent to
  2564              windows created by this thread.  */
  2565           if (drain_message_queue ()
  2566               /* If drain_message_queue returns non-zero, that means
  2567                  we received a WM_EMACS_FILENOTIFY message.  If this
  2568                  is a TTY frame, we must signal the caller that keyboard
  2569                  input is available, so that w32_console_read_socket
  2570                  will be called to pick up the notifications.  If we
  2571                  don't do that, file notifications will only work when
  2572                  the Emacs TTY frame has focus.  */
  2573               && FRAME_TERMCAP_P (SELECTED_FRAME ())
  2574               /* they asked for stdin reads */
  2575               && FD_ISSET (0, &orfds)
  2576               /* the stdin handle is valid */
  2577               && keyboard_handle)
  2578             {
  2579               FD_SET (0, rfds);
  2580               if (nr == 0)
  2581                 nr = 1;
  2582             }
  2583         }
  2584       else if (active >= nh)
  2585         {
  2586           cp = cps[active - nh];
  2587 
  2588           /* We cannot always signal SIGCHLD immediately; if we have not
  2589              finished reading the process output, we must delay sending
  2590              SIGCHLD until we do.  */
  2591 
  2592           if (cp->fd >= 0 && (fd_info[cp->fd].flags & FILE_AT_EOF) == 0)
  2593             fd_info[cp->fd].flags |= FILE_SEND_SIGCHLD;
  2594           /* SIG_DFL for SIGCHLD is ignored */
  2595           else if (sig_handlers[SIGCHLD] != SIG_DFL &&
  2596                    sig_handlers[SIGCHLD] != SIG_IGN)
  2597             {
  2598 #ifdef FULL_DEBUG
  2599               DebPrint (("select calling SIGCHLD handler for pid %d\n",
  2600                          cp->pid));
  2601 #endif
  2602               sig_handlers[SIGCHLD] (SIGCHLD);
  2603             }
  2604         }
  2605       else if (fdindex[active] == -1)
  2606         {
  2607           /* Quit (C-g) was detected.  */
  2608           errno = EINTR;
  2609           return -1;
  2610         }
  2611       else if (rfds && fdindex[active] == 0)
  2612         {
  2613           /* Keyboard input available */
  2614           FD_SET (0, rfds);
  2615           nr++;
  2616         }
  2617       else
  2618         {
  2619           /* Must be a socket or pipe - read ahead should have
  2620              completed, either succeeding or failing.  If this handle
  2621              was waiting for an async 'connect', reset the connect
  2622              flag, so it could read from now on.  */
  2623           if (wfds && (fd_info[fdindex[active]].flags & FILE_CONNECT) != 0)
  2624             {
  2625               cp = fd_info[fdindex[active]].cp;
  2626               if (cp)
  2627                 {
  2628                   /* Don't reset the FILE_CONNECT bit and don't
  2629                      acknowledge the read if the status is
  2630                      STATUS_CONNECT_FAILED or some other
  2631                      failure. That's because the thread exits in those
  2632                      cases, so it doesn't need the ACK, and we want to
  2633                      keep the FILE_CONNECT bit as evidence that the
  2634                      connect failed, to be checked in sys_read.  */
  2635                   if (cp->status == STATUS_READ_SUCCEEDED)
  2636                     {
  2637                       fd_info[cp->fd].flags &= ~FILE_CONNECT;
  2638                       cp->status = STATUS_READ_ACKNOWLEDGED;
  2639                     }
  2640                   ResetEvent (cp->char_avail);
  2641                 }
  2642               FD_SET (fdindex[active], wfds);
  2643             }
  2644           else if (rfds)
  2645             FD_SET (fdindex[active], rfds);
  2646           nr++;
  2647         }
  2648 
  2649       /* Even though wait_reading_process_output only reads from at most
  2650          one channel, we must process all channels here so that we reap
  2651          all children that have died.  */
  2652       while (++active < nh + nc)
  2653         if (WaitForSingleObject (wait_hnd[active], 0) == WAIT_OBJECT_0)
  2654           break;
  2655     } while (active < nh + nc);
  2656 
  2657   if (noninteractive)
  2658     {
  2659       if (handle_file_notifications (NULL))
  2660         nr++;
  2661     }
  2662 
  2663   /* If no input has arrived and timeout hasn't expired, wait again.  */
  2664   if (nr == 0)
  2665     {
  2666       DWORD elapsed = GetTickCount () - start_time;
  2667 
  2668       if (timeout_ms > elapsed) /* INFINITE is MAX_UINT */
  2669         {
  2670           if (timeout_ms != INFINITE)
  2671             timeout_ms -= elapsed;
  2672           goto count_children;
  2673         }
  2674     }
  2675 
  2676   return nr;
  2677 }
  2678 
  2679 /* Substitute for certain kill () operations */
  2680 
  2681 static BOOL CALLBACK
  2682 find_child_console (HWND hwnd, LPARAM arg)
  2683 {
  2684   child_process * cp = (child_process *) arg;
  2685   DWORD process_id;
  2686 
  2687   GetWindowThreadProcessId (hwnd, &process_id);
  2688   if (process_id == cp->procinfo.dwProcessId)
  2689     {
  2690       char window_class[32];
  2691 
  2692       GetClassName (hwnd, window_class, sizeof (window_class));
  2693       if (strcmp (window_class,
  2694                   (os_subtype == OS_SUBTYPE_9X)
  2695                   ? "tty"
  2696                   : "ConsoleWindowClass") == 0)
  2697         {
  2698           cp->hwnd = hwnd;
  2699           return FALSE;
  2700         }
  2701     }
  2702   /* keep looking */
  2703   return TRUE;
  2704 }
  2705 
  2706 typedef BOOL (WINAPI * DebugBreakProcess_Proc) (
  2707     HANDLE hProcess);
  2708 
  2709 /* Emulate 'kill', but only for other processes.  */
  2710 int
  2711 sys_kill (pid_t pid, int sig)
  2712 {
  2713   child_process *cp;
  2714   HANDLE proc_hand;
  2715   int need_to_free = 0;
  2716   int rc = 0;
  2717 
  2718   /* Each process is in its own process group.  */
  2719   if (pid < 0)
  2720     pid = -pid;
  2721 
  2722   /* Only handle signals that can be mapped to a similar behavior on Windows */
  2723   if (sig != 0
  2724       && sig != SIGINT && sig != SIGKILL && sig != SIGQUIT && sig != SIGHUP && sig != SIGTRAP)
  2725     {
  2726       errno = EINVAL;
  2727       return -1;
  2728     }
  2729 
  2730   if (sig == 0)
  2731     {
  2732       /* It will take _some_ time before PID 4 or less on Windows will
  2733          be Emacs...  */
  2734       if (pid <= 4)
  2735         {
  2736           errno = EPERM;
  2737           return -1;
  2738         }
  2739       proc_hand = OpenProcess (PROCESS_QUERY_INFORMATION, 0, pid);
  2740       if (proc_hand == NULL)
  2741         {
  2742           DWORD err = GetLastError ();
  2743 
  2744           switch (err)
  2745             {
  2746             case ERROR_ACCESS_DENIED: /* existing process, but access denied */
  2747               errno = EPERM;
  2748               return -1;
  2749             case ERROR_INVALID_PARAMETER: /* process PID does not exist */
  2750               errno = ESRCH;
  2751               return -1;
  2752             }
  2753         }
  2754       else
  2755         CloseHandle (proc_hand);
  2756       return 0;
  2757     }
  2758 
  2759   cp = find_child_pid (pid);
  2760   if (cp == NULL)
  2761     {
  2762       /* We were passed a PID of something other than our subprocess.
  2763          If that is our own PID, we will send to ourself a message to
  2764          close the selected frame, which does not necessarily
  2765          terminates Emacs.  But then we are not supposed to call
  2766          sys_kill with our own PID.  */
  2767 
  2768       DWORD desiredAccess =
  2769         (sig == SIGTRAP) ? PROCESS_ALL_ACCESS : PROCESS_TERMINATE;
  2770 
  2771       proc_hand = OpenProcess (desiredAccess, 0, pid);
  2772       if (proc_hand == NULL)
  2773         {
  2774           errno = EPERM;
  2775           return -1;
  2776         }
  2777       need_to_free = 1;
  2778     }
  2779   else
  2780     {
  2781       proc_hand = cp->procinfo.hProcess;
  2782       pid = cp->procinfo.dwProcessId;
  2783 
  2784       /* Try to locate console window for process. */
  2785       EnumWindows (find_child_console, (LPARAM) cp);
  2786     }
  2787 
  2788   if (sig == SIGINT || sig == SIGQUIT)
  2789     {
  2790       if (NILP (Vw32_start_process_share_console) && cp && cp->hwnd)
  2791         {
  2792           BYTE control_scan_code = (BYTE) MapVirtualKey (VK_CONTROL, 0);
  2793           /* Fake Ctrl-C for SIGINT, and Ctrl-Break for SIGQUIT.  */
  2794           BYTE vk_break_code = (sig == SIGINT) ? 'C' : VK_CANCEL;
  2795           BYTE break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
  2796           HWND foreground_window;
  2797 
  2798           if (break_scan_code == 0)
  2799             {
  2800               /* Fake Ctrl-C for SIGQUIT if we can't manage Ctrl-Break. */
  2801               vk_break_code = 'C';
  2802               break_scan_code = (BYTE) MapVirtualKey (vk_break_code, 0);
  2803             }
  2804 
  2805           foreground_window = GetForegroundWindow ();
  2806           if (foreground_window)
  2807             {
  2808               /* NT 5.0, and apparently also Windows 98, will not allow
  2809                  a Window to be set to foreground directly without the
  2810                  user's involvement. The workaround is to attach
  2811                  ourselves to the thread that owns the foreground
  2812                  window, since that is the only thread that can set the
  2813                  foreground window.  */
  2814               DWORD foreground_thread, child_thread;
  2815               foreground_thread =
  2816                 GetWindowThreadProcessId (foreground_window, NULL);
  2817               if (foreground_thread == GetCurrentThreadId ()
  2818                   || !AttachThreadInput (GetCurrentThreadId (),
  2819                                          foreground_thread, TRUE))
  2820                 foreground_thread = 0;
  2821 
  2822               child_thread = GetWindowThreadProcessId (cp->hwnd, NULL);
  2823               if (child_thread == GetCurrentThreadId ()
  2824                   || !AttachThreadInput (GetCurrentThreadId (),
  2825                                          child_thread, TRUE))
  2826                 child_thread = 0;
  2827 
  2828               /* Set the foreground window to the child.  */
  2829               if (SetForegroundWindow (cp->hwnd))
  2830                 {
  2831                   /* Record the state of the left Ctrl key: the user
  2832                      could have it depressed while we are simulating
  2833                      Ctrl-C, in which case we will have to leave the
  2834                      state of that Ctrl depressed when we are done.  */
  2835                   short ctrl_state = GetKeyState (VK_LCONTROL) & 0x8000;
  2836 
  2837                   /* Generate keystrokes as if user had typed Ctrl-Break or
  2838                      Ctrl-C.  */
  2839                   keybd_event (VK_CONTROL, control_scan_code, 0, 0);
  2840                   keybd_event (vk_break_code, break_scan_code,
  2841                     (vk_break_code == 'C' ? 0 : KEYEVENTF_EXTENDEDKEY), 0);
  2842                   keybd_event (vk_break_code, break_scan_code,
  2843                     (vk_break_code == 'C' ? 0 : KEYEVENTF_EXTENDEDKEY)
  2844                     | KEYEVENTF_KEYUP, 0);
  2845                   keybd_event (VK_CONTROL, control_scan_code,
  2846                                KEYEVENTF_KEYUP, 0);
  2847 
  2848                   /* Sleep for a bit to give time for Emacs frame to respond
  2849                      to focus change events (if Emacs was active app).  */
  2850                   Sleep (100);
  2851 
  2852                   SetForegroundWindow (foreground_window);
  2853                   /* If needed, restore the state of Ctrl.  */
  2854                   if (ctrl_state != 0)
  2855                     keybd_event (VK_CONTROL, control_scan_code, 0, 0);
  2856                 }
  2857               /* Detach from the foreground and child threads now that
  2858                  the foreground switching is over.  */
  2859               if (foreground_thread)
  2860                 AttachThreadInput (GetCurrentThreadId (),
  2861                                    foreground_thread, FALSE);
  2862               if (child_thread)
  2863                 AttachThreadInput (GetCurrentThreadId (),
  2864                                    child_thread, FALSE);
  2865             }
  2866         }
  2867       /* Ctrl-Break is NT equivalent of SIGINT.  */
  2868       else if (!GenerateConsoleCtrlEvent (CTRL_BREAK_EVENT, pid))
  2869         {
  2870           DebPrint (("sys_kill.GenerateConsoleCtrlEvent return %d "
  2871                      "for pid %lu\n", GetLastError (), pid));
  2872           errno = EINVAL;
  2873           rc = -1;
  2874         }
  2875     }
  2876   else if (sig == SIGTRAP)
  2877     {
  2878       static DebugBreakProcess_Proc s_pfn_Debug_Break_Process = NULL;
  2879 
  2880       if (g_b_init_debug_break_process == 0)
  2881         {
  2882           g_b_init_debug_break_process = 1;
  2883           s_pfn_Debug_Break_Process = (DebugBreakProcess_Proc)
  2884             get_proc_addr (GetModuleHandle ("kernel32.dll"),
  2885                                   "DebugBreakProcess");
  2886         }
  2887 
  2888       if (s_pfn_Debug_Break_Process == NULL)
  2889         {
  2890           errno = ENOTSUP;
  2891           rc = -1;
  2892         }
  2893       else if (!s_pfn_Debug_Break_Process (proc_hand))
  2894         {
  2895           DWORD err = GetLastError ();
  2896 
  2897           DebPrint (("sys_kill.DebugBreakProcess return %d "
  2898                      "for pid %lu\n", err, pid));
  2899 
  2900           switch (err)
  2901             {
  2902             case ERROR_ACCESS_DENIED:
  2903               errno = EPERM;
  2904               break;
  2905             default:
  2906               errno = EINVAL;
  2907               break;
  2908             }
  2909 
  2910           rc = -1;
  2911         }
  2912     }
  2913   else
  2914     {
  2915       if (NILP (Vw32_start_process_share_console) && cp && cp->hwnd)
  2916         {
  2917 #if 1
  2918           if (os_subtype == OS_SUBTYPE_9X)
  2919             {
  2920 /*
  2921    Another possibility is to try terminating the VDM out-right by
  2922    calling the Shell VxD (id 0x17) V86 interface, function #4
  2923    "SHELL_Destroy_VM", ie.
  2924 
  2925      mov edx,4
  2926      mov ebx,vm_handle
  2927      call shellapi
  2928 
  2929    First need to determine the current VM handle, and then arrange for
  2930    the shellapi call to be made from the system vm (by using
  2931    Switch_VM_and_callback).
  2932 
  2933    Could try to invoke DestroyVM through CallVxD.
  2934 
  2935 */
  2936 #if 0
  2937               /* On Windows 95, posting WM_QUIT causes the 16-bit subsystem
  2938                  to hang when cmdproxy is used in conjunction with
  2939                  command.com for an interactive shell.  Posting
  2940                  WM_CLOSE pops up a dialog that, when Yes is selected,
  2941                  does the same thing.  TerminateProcess is also less
  2942                  than ideal in that subprocesses tend to stick around
  2943                  until the machine is shutdown, but at least it
  2944                  doesn't freeze the 16-bit subsystem.  */
  2945               PostMessage (cp->hwnd, WM_QUIT, 0xff, 0);
  2946 #endif
  2947               if (!TerminateProcess (proc_hand, 0xff))
  2948                 {
  2949                   DebPrint (("sys_kill.TerminateProcess returned %d "
  2950                              "for pid %lu\n", GetLastError (), pid));
  2951                   errno = EINVAL;
  2952                   rc = -1;
  2953                 }
  2954             }
  2955           else
  2956 #endif
  2957             PostMessage (cp->hwnd, WM_CLOSE, 0, 0);
  2958         }
  2959       /* Kill the process.  On W32 this doesn't kill child processes
  2960          so it doesn't work very well for shells which is why it's not
  2961          used in every case.  */
  2962       else if (!TerminateProcess (proc_hand, 0xff))
  2963         {
  2964           DebPrint (("sys_kill.TerminateProcess returned %d "
  2965                      "for pid %lu\n", GetLastError (), pid));
  2966           errno = EINVAL;
  2967           rc = -1;
  2968         }
  2969     }
  2970 
  2971   if (need_to_free)
  2972     CloseHandle (proc_hand);
  2973 
  2974   return rc;
  2975 }
  2976 
  2977 /* The following two routines are used to manipulate stdin, stdout, and
  2978    stderr of our child processes.
  2979 
  2980    Assuming that in, out, and err are *not* inheritable, we make them
  2981    stdin, stdout, and stderr of the child as follows:
  2982 
  2983    - Save the parent's current standard handles.
  2984    - Set the std handles to inheritable duplicates of the ones being passed in.
  2985      (Note that _get_osfhandle() is an io.h procedure that retrieves the
  2986      NT file handle for a crt file descriptor.)
  2987    - Spawn the child, which inherits in, out, and err as stdin,
  2988      stdout, and stderr. (see Spawnve)
  2989    - Close the std handles passed to the child.
  2990    - Reset the parent's standard handles to the saved handles.
  2991      (see reset_standard_handles)
  2992    We assume that the caller closes in, out, and err after calling us.  */
  2993 
  2994 void
  2995 prepare_standard_handles (int in, int out, int err, HANDLE handles[3])
  2996 {
  2997   HANDLE parent;
  2998   HANDLE newstdin, newstdout, newstderr;
  2999 
  3000   parent = GetCurrentProcess ();
  3001 
  3002   handles[0] = GetStdHandle (STD_INPUT_HANDLE);
  3003   handles[1] = GetStdHandle (STD_OUTPUT_HANDLE);
  3004   handles[2] = GetStdHandle (STD_ERROR_HANDLE);
  3005 
  3006   /* make inheritable copies of the new handles */
  3007   if (!DuplicateHandle (parent,
  3008                        (HANDLE) _get_osfhandle (in),
  3009                        parent,
  3010                        &newstdin,
  3011                        0,
  3012                        TRUE,
  3013                        DUPLICATE_SAME_ACCESS))
  3014     report_file_error ("Duplicating input handle for child", Qnil);
  3015 
  3016   if (!DuplicateHandle (parent,
  3017                        (HANDLE) _get_osfhandle (out),
  3018                        parent,
  3019                        &newstdout,
  3020                        0,
  3021                        TRUE,
  3022                        DUPLICATE_SAME_ACCESS))
  3023     report_file_error ("Duplicating output handle for child", Qnil);
  3024 
  3025   if (!DuplicateHandle (parent,
  3026                        (HANDLE) _get_osfhandle (err),
  3027                        parent,
  3028                        &newstderr,
  3029                        0,
  3030                        TRUE,
  3031                        DUPLICATE_SAME_ACCESS))
  3032     report_file_error ("Duplicating error handle for child", Qnil);
  3033 
  3034   /* and store them as our std handles */
  3035   if (!SetStdHandle (STD_INPUT_HANDLE, newstdin))
  3036     report_file_error ("Changing stdin handle", Qnil);
  3037 
  3038   if (!SetStdHandle (STD_OUTPUT_HANDLE, newstdout))
  3039     report_file_error ("Changing stdout handle", Qnil);
  3040 
  3041   if (!SetStdHandle (STD_ERROR_HANDLE, newstderr))
  3042     report_file_error ("Changing stderr handle", Qnil);
  3043 }
  3044 
  3045 void
  3046 reset_standard_handles (int in, int out, int err, HANDLE handles[3])
  3047 {
  3048   /* close the duplicated handles passed to the child */
  3049   CloseHandle (GetStdHandle (STD_INPUT_HANDLE));
  3050   CloseHandle (GetStdHandle (STD_OUTPUT_HANDLE));
  3051   CloseHandle (GetStdHandle (STD_ERROR_HANDLE));
  3052 
  3053   /* now restore parent's saved std handles */
  3054   SetStdHandle (STD_INPUT_HANDLE, handles[0]);
  3055   SetStdHandle (STD_OUTPUT_HANDLE, handles[1]);
  3056   SetStdHandle (STD_ERROR_HANDLE, handles[2]);
  3057 }
  3058 
  3059 void
  3060 set_process_dir (const char * dir)
  3061 {
  3062   process_dir = (char *) dir;
  3063 }
  3064 
  3065 /* To avoid problems with winsock implementations that work over dial-up
  3066    connections causing or requiring a connection to exist while Emacs is
  3067    running, Emacs no longer automatically loads winsock on startup if it
  3068    is present.  Instead, it will be loaded when open-network-stream is
  3069    first called.
  3070 
  3071    To allow full control over when winsock is loaded, we provide these
  3072    two functions to dynamically load and unload winsock.  This allows
  3073    dial-up users to only be connected when they actually need to use
  3074    socket services.  */
  3075 
  3076 /* From w32.c */
  3077 extern HANDLE winsock_lib;
  3078 extern BOOL term_winsock (void);
  3079 
  3080 DEFUN ("w32-has-winsock", Fw32_has_winsock, Sw32_has_winsock, 0, 1, 0,
  3081        doc: /* Test for presence of the Windows socket library `winsock'.
  3082 Returns non-nil if winsock support is present, nil otherwise.
  3083 
  3084 If the optional argument LOAD-NOW is non-nil, the winsock library is
  3085 also loaded immediately if not already loaded.  If winsock is loaded,
  3086 the winsock local hostname is returned (since this may be different from
  3087 the value of `system-name' and should supplant it), otherwise t is
  3088 returned to indicate winsock support is present.  */)
  3089   (Lisp_Object load_now)
  3090 {
  3091   int have_winsock;
  3092 
  3093   have_winsock = init_winsock (!NILP (load_now));
  3094   if (have_winsock)
  3095     {
  3096       if (winsock_lib != NULL)
  3097         {
  3098           /* Return new value for system-name.  The best way to do this
  3099              is to call init_system_name, saving and restoring the
  3100              original value to avoid side-effects.  */
  3101           Lisp_Object orig_hostname = Vsystem_name;
  3102           Lisp_Object hostname;
  3103 
  3104           init_system_name ();
  3105           hostname = Vsystem_name;
  3106           Vsystem_name = orig_hostname;
  3107           return hostname;
  3108         }
  3109       return Qt;
  3110     }
  3111   return Qnil;
  3112 }
  3113 
  3114 DEFUN ("w32-unload-winsock", Fw32_unload_winsock, Sw32_unload_winsock,
  3115        0, 0, 0,
  3116        doc: /* Unload the Windows socket library `winsock' if loaded.
  3117 This is provided to allow dial-up socket connections to be disconnected
  3118 when no longer needed.  Returns nil without unloading winsock if any
  3119 socket connections still exist.  */)
  3120   (void)
  3121 {
  3122   return term_winsock () ? Qt : Qnil;
  3123 }
  3124 
  3125 
  3126 /* Some miscellaneous functions that are Windows specific, but not GUI
  3127    specific (ie. are applicable in terminal or batch mode as well).  */
  3128 
  3129 DEFUN ("w32-short-file-name", Fw32_short_file_name, Sw32_short_file_name, 1, 1, 0,
  3130        doc: /* Return the short file name version (8.3) of the full path of FILENAME.
  3131 If FILENAME does not exist, return nil.
  3132 All path elements in FILENAME are converted to their short names.  */)
  3133   (Lisp_Object filename)
  3134 {
  3135   char shortname[MAX_PATH];
  3136 
  3137   CHECK_STRING (filename);
  3138 
  3139   /* first expand it.  */
  3140   filename = Fexpand_file_name (filename, Qnil);
  3141 
  3142   /* luckily, this returns the short version of each element in the path.  */
  3143   if (w32_get_short_filename (SSDATA (ENCODE_FILE (filename)),
  3144                               shortname, MAX_PATH) == 0)
  3145     return Qnil;
  3146 
  3147   dostounix_filename (shortname);
  3148 
  3149   /* No need to DECODE_FILE, because 8.3 names are pure ASCII.   */
  3150   return build_string (shortname);
  3151 }
  3152 
  3153 
  3154 DEFUN ("w32-long-file-name", Fw32_long_file_name, Sw32_long_file_name,
  3155        1, 1, 0,
  3156        doc: /* Return the long file name version of the full path of FILENAME.
  3157 If FILENAME does not exist, return nil.
  3158 All path elements in FILENAME are converted to their long names.  */)
  3159   (Lisp_Object filename)
  3160 {
  3161   char longname[ MAX_UTF8_PATH ];
  3162   int drive_only = 0;
  3163 
  3164   CHECK_STRING (filename);
  3165 
  3166   if (SBYTES (filename) == 2
  3167       && *(SDATA (filename) + 1) == ':')
  3168     drive_only = 1;
  3169 
  3170   /* first expand it.  */
  3171   filename = Fexpand_file_name (filename, Qnil);
  3172 
  3173   if (!w32_get_long_filename (SSDATA (ENCODE_FILE (filename)), longname,
  3174                               MAX_UTF8_PATH))
  3175     return Qnil;
  3176 
  3177   dostounix_filename (longname);
  3178 
  3179   /* If we were passed only a drive, make sure that a slash is not appended
  3180      for consistency with directories.  Allow for drive mapping via SUBST
  3181      in case expand-file-name is ever changed to expand those.  */
  3182   if (drive_only && longname[1] == ':' && longname[2] == '/' && !longname[3])
  3183     longname[2] = '\0';
  3184 
  3185   return DECODE_FILE (build_unibyte_string (longname));
  3186 }
  3187 
  3188 DEFUN ("w32-set-process-priority", Fw32_set_process_priority,
  3189        Sw32_set_process_priority, 2, 2, 0,
  3190        doc: /* Set the priority of PROCESS to PRIORITY.
  3191 If PROCESS is nil, the priority of Emacs is changed, otherwise the
  3192 priority of the process whose pid is PROCESS is changed.
  3193 PRIORITY should be one of the symbols high, normal, or low;
  3194 any other symbol will be interpreted as normal.
  3195 
  3196 If successful, the return value is t, otherwise nil.  */)
  3197   (Lisp_Object process, Lisp_Object priority)
  3198 {
  3199   HANDLE proc_handle = GetCurrentProcess ();
  3200   DWORD  priority_class = NORMAL_PRIORITY_CLASS;
  3201   Lisp_Object result = Qnil;
  3202 
  3203   CHECK_SYMBOL (priority);
  3204 
  3205   if (!NILP (process))
  3206     {
  3207       DWORD pid;
  3208       child_process *cp;
  3209 
  3210       CHECK_FIXNUM (process);
  3211 
  3212       /* Allow pid to be an internally generated one, or one obtained
  3213          externally.  This is necessary because real pids on Windows 95 are
  3214          negative.  */
  3215 
  3216       pid = XFIXNUM (process);
  3217       cp = find_child_pid (pid);
  3218       if (cp != NULL)
  3219         pid = cp->procinfo.dwProcessId;
  3220 
  3221       proc_handle = OpenProcess (PROCESS_SET_INFORMATION, FALSE, pid);
  3222     }
  3223 
  3224   if (EQ (priority, Qhigh))
  3225     priority_class = HIGH_PRIORITY_CLASS;
  3226   else if (EQ (priority, Qlow))
  3227     priority_class = IDLE_PRIORITY_CLASS;
  3228 
  3229   if (proc_handle != NULL)
  3230     {
  3231       if (SetPriorityClass (proc_handle, priority_class))
  3232         result = Qt;
  3233       if (!NILP (process))
  3234         CloseHandle (proc_handle);
  3235     }
  3236 
  3237   return result;
  3238 }
  3239 
  3240 DEFUN ("w32-application-type", Fw32_application_type,
  3241        Sw32_application_type, 1, 1, 0,
  3242        doc: /* Return the type of an MS-Windows PROGRAM.
  3243 
  3244 Knowing the type of an executable could be useful for formatting
  3245 file names passed to it or for quoting its command-line arguments.
  3246 
  3247 PROGRAM should specify an executable file, including the extension.
  3248 
  3249 The value is one of the following:
  3250 
  3251 `dos'        -- a DOS .com program or some other non-PE executable
  3252 `cygwin'     -- a Cygwin program that depends on Cygwin DLL
  3253 `msys'       -- an MSYS 1.x or MSYS2 program
  3254 `w32-native' -- a native Windows application
  3255 `unknown'    -- a file that doesn't exist, or cannot be open, or whose
  3256                 name is not encodable in the current ANSI codepage.
  3257 
  3258 Note that for .bat and .cmd batch files the function returns the type
  3259 of their command interpreter, as specified by the \"COMSPEC\"
  3260 environment variable.
  3261 
  3262 This function returns `unknown' for programs whose file names
  3263 include characters not supported by the current ANSI codepage, as
  3264 such programs cannot be invoked by Emacs anyway.  */)
  3265      (Lisp_Object program)
  3266 {
  3267   int is_dos_app, is_cygwin_app, is_msys_app, dummy;
  3268   Lisp_Object encoded_progname;
  3269   char *progname, progname_a[MAX_PATH];
  3270 
  3271   program = Fexpand_file_name (program, Qnil);
  3272   encoded_progname = Fcopy_sequence (ENCODE_FILE (program));
  3273   progname = SSDATA (encoded_progname);
  3274   unixtodos_filename (progname);
  3275   filename_to_ansi (progname, progname_a);
  3276   /* Reject file names that cannot be encoded in the current ANSI
  3277      codepage.  */
  3278   if (_mbspbrk ((unsigned char *)progname_a, (const unsigned char *)"?"))
  3279     return Qunknown;
  3280 
  3281   if (w32_executable_type (progname_a, &is_dos_app, &is_cygwin_app,
  3282                            &is_msys_app, &dummy) != 0)
  3283     return Qunknown;
  3284   if (is_dos_app)
  3285     return Qdos;
  3286   if (is_cygwin_app)
  3287     return Qcygwin;
  3288   if (is_msys_app)
  3289     return Qmsys;
  3290   return Qw32_native;
  3291 }
  3292 
  3293 #ifdef HAVE_LANGINFO_CODESET
  3294 
  3295 /* If we are compiling for compatibility with older 32-bit Windows
  3296    versions, this might not be defined by the Windows headers.  */
  3297 #ifndef LOCALE_IPAPERSIZE
  3298 # define LOCALE_IPAPERSIZE 0x100A
  3299 #endif
  3300 /* Emulation of nl_langinfo.  Used in fns.c:Flocale_info.  */
  3301 char *
  3302 nl_langinfo (nl_item item)
  3303 {
  3304   /* Conversion of Posix item numbers to their Windows equivalents.  */
  3305   static const LCTYPE w32item[] = {
  3306     LOCALE_IDEFAULTANSICODEPAGE,
  3307     LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
  3308     LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6, LOCALE_SDAYNAME7,
  3309     LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3,
  3310     LOCALE_SMONTHNAME4, LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6,
  3311     LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8, LOCALE_SMONTHNAME9,
  3312     LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
  3313     LOCALE_IPAPERSIZE, LOCALE_IPAPERSIZE
  3314   };
  3315 
  3316   static char *nl_langinfo_buf = NULL;
  3317   static int   nl_langinfo_len = 0;
  3318 
  3319   if (nl_langinfo_len <= 0)
  3320     nl_langinfo_buf = xmalloc (nl_langinfo_len = 1);
  3321 
  3322   char *retval = nl_langinfo_buf;
  3323 
  3324   if (item < 0 || item >= _NL_NUM)
  3325     nl_langinfo_buf[0] = 0;
  3326   else
  3327     {
  3328       LCID cloc = GetThreadLocale ();
  3329       int need_len = GetLocaleInfo (cloc, w32item[item] | LOCALE_USE_CP_ACP,
  3330                                     NULL, 0);
  3331 
  3332       if (need_len <= 0)
  3333         nl_langinfo_buf[0] = 0;
  3334       else
  3335         {
  3336           if (item == CODESET)
  3337             {
  3338               need_len += 2;    /* for the "cp" prefix */
  3339               if (need_len < 8) /* for the case we call GetACP */
  3340                 need_len = 8;
  3341             }
  3342           if (nl_langinfo_len <= need_len)
  3343             nl_langinfo_buf = xrealloc (nl_langinfo_buf,
  3344                                         nl_langinfo_len = need_len);
  3345           retval = nl_langinfo_buf;
  3346 
  3347           if (!GetLocaleInfo (cloc, w32item[item] | LOCALE_USE_CP_ACP,
  3348                               nl_langinfo_buf, nl_langinfo_len))
  3349             nl_langinfo_buf[0] = 0;
  3350           else if (item == CODESET)
  3351             {
  3352               if (strcmp (nl_langinfo_buf, "0") == 0 /* CP_ACP */
  3353                   || strcmp (nl_langinfo_buf, "1") == 0) /* CP_OEMCP */
  3354                 sprintf (nl_langinfo_buf, "cp%u", GetACP ());
  3355               else
  3356                 {
  3357                   memmove (nl_langinfo_buf + 2, nl_langinfo_buf,
  3358                            strlen (nl_langinfo_buf) + 1);
  3359                   nl_langinfo_buf[0] = 'c';
  3360                   nl_langinfo_buf[1] = 'p';
  3361                 }
  3362             }
  3363           else if (item == _NL_PAPER_WIDTH || item == _NL_PAPER_HEIGHT)
  3364             {
  3365               static const int paper_size[][2] =
  3366                 {
  3367                  { -1, -1 },
  3368                  { 216, 279 },
  3369                  { -1, -1 },
  3370                  { -1, -1 },
  3371                  { -1, -1 },
  3372                  { 216, 356 },
  3373                  { -1, -1 },
  3374                  { -1, -1 },
  3375                  { 297, 420 },
  3376                  { 210, 297 }
  3377                 };
  3378               int idx = atoi (nl_langinfo_buf);
  3379               if (0 <= idx && idx < ARRAYELTS (paper_size))
  3380                 retval = (char *)(intptr_t) (item == _NL_PAPER_WIDTH
  3381                                              ? paper_size[idx][0]
  3382                                              : paper_size[idx][1]);
  3383               else
  3384                 retval = (char *)(intptr_t) -1;
  3385             }
  3386         }
  3387     }
  3388   return retval;
  3389 }
  3390 #endif  /* HAVE_LANGINFO_CODESET */
  3391 
  3392 DEFUN ("w32-get-locale-info", Fw32_get_locale_info,
  3393        Sw32_get_locale_info, 1, 2, 0,
  3394        doc: /* Return information about the Windows locale LCID.
  3395 By default, return a three letter locale code which encodes the default
  3396 language as the first two characters, and the country or regional variant
  3397 as the third letter.  For example, ENU refers to `English (United States)',
  3398 while ENC means `English (Canadian)'.
  3399 
  3400 If the optional argument LONGFORM is t, the long form of the locale
  3401 name is returned, e.g. `English (United States)' instead; if LONGFORM
  3402 is a number, it is interpreted as an LCTYPE constant and the corresponding
  3403 locale information is returned.
  3404 
  3405 If LCID (a 16-bit number) is not a valid locale, the result is nil.  */)
  3406   (Lisp_Object lcid, Lisp_Object longform)
  3407 {
  3408   int got_abbrev;
  3409   int got_full;
  3410   char abbrev_name[32] = { 0 };
  3411   char full_name[256] = { 0 };
  3412 
  3413   CHECK_FIXNUM (lcid);
  3414 
  3415   if (!IsValidLocale (XFIXNUM (lcid), LCID_SUPPORTED))
  3416     return Qnil;
  3417 
  3418   if (NILP (longform))
  3419     {
  3420       got_abbrev = GetLocaleInfo (XFIXNUM (lcid),
  3421                                   LOCALE_SABBREVLANGNAME | LOCALE_USE_CP_ACP,
  3422                                   abbrev_name, sizeof (abbrev_name));
  3423       if (got_abbrev)
  3424         return build_string (abbrev_name);
  3425     }
  3426   else if (EQ (longform, Qt))
  3427     {
  3428       got_full = GetLocaleInfo (XFIXNUM (lcid),
  3429                                 LOCALE_SLANGUAGE | LOCALE_USE_CP_ACP,
  3430                                 full_name, sizeof (full_name));
  3431       if (got_full)
  3432         return DECODE_SYSTEM (build_string (full_name));
  3433     }
  3434   else if (FIXNUMP (longform))
  3435     {
  3436       got_full = GetLocaleInfo (XFIXNUM (lcid),
  3437                                 XFIXNUM (longform),
  3438                                 full_name, sizeof (full_name));
  3439       /* GetLocaleInfo's return value includes the terminating null
  3440          character, when the returned information is a string, whereas
  3441          make_unibyte_string needs the string length without the
  3442          terminating null.  */
  3443       if (got_full)
  3444         return make_unibyte_string (full_name, got_full - 1);
  3445     }
  3446 
  3447   return Qnil;
  3448 }
  3449 
  3450 
  3451 DEFUN ("w32-get-current-locale-id", Fw32_get_current_locale_id,
  3452        Sw32_get_current_locale_id, 0, 0, 0,
  3453        doc: /* Return Windows locale id for current locale setting.
  3454 This is a numerical value; use `w32-get-locale-info' to convert to a
  3455 human-readable form.  */)
  3456   (void)
  3457 {
  3458   return make_fixnum (GetThreadLocale ());
  3459 }
  3460 
  3461 static DWORD
  3462 int_from_hex (char * s)
  3463 {
  3464   DWORD val = 0;
  3465   static char hex[] = "0123456789abcdefABCDEF";
  3466   char * p;
  3467 
  3468   while (*s && (p = strchr (hex, *s)) != NULL)
  3469     {
  3470       unsigned digit = p - hex;
  3471       if (digit > 15)
  3472         digit -= 6;
  3473       val = val * 16 + digit;
  3474       s++;
  3475     }
  3476   return val;
  3477 }
  3478 
  3479 /* We need to build a global list, since the EnumSystemLocale callback
  3480    function isn't given a context pointer.  */
  3481 Lisp_Object Vw32_valid_locale_ids;
  3482 
  3483 static BOOL CALLBACK ALIGN_STACK
  3484 enum_locale_fn (LPTSTR localeNum)
  3485 {
  3486   DWORD id = int_from_hex (localeNum);
  3487   Vw32_valid_locale_ids = Fcons (make_fixnum (id), Vw32_valid_locale_ids);
  3488   return TRUE;
  3489 }
  3490 
  3491 DEFUN ("w32-get-valid-locale-ids", Fw32_get_valid_locale_ids,
  3492        Sw32_get_valid_locale_ids, 0, 0, 0,
  3493        doc: /* Return list of all valid Windows locale ids.
  3494 Each id is a numerical value; use `w32-get-locale-info' to convert to a
  3495 human-readable form.  */)
  3496   (void)
  3497 {
  3498   Vw32_valid_locale_ids = Qnil;
  3499 
  3500   EnumSystemLocales (enum_locale_fn, LCID_SUPPORTED);
  3501 
  3502   Vw32_valid_locale_ids = Fnreverse (Vw32_valid_locale_ids);
  3503   return Vw32_valid_locale_ids;
  3504 }
  3505 
  3506 
  3507 DEFUN ("w32-get-default-locale-id", Fw32_get_default_locale_id, Sw32_get_default_locale_id, 0, 1, 0,
  3508        doc: /* Return Windows locale id for default locale setting.
  3509 By default, the system default locale setting is returned; if the optional
  3510 parameter USERP is non-nil, the user default locale setting is returned.
  3511 This is a numerical value; use `w32-get-locale-info' to convert to a
  3512 human-readable form.  */)
  3513   (Lisp_Object userp)
  3514 {
  3515   if (NILP (userp))
  3516     return make_fixnum (GetSystemDefaultLCID ());
  3517   return make_fixnum (GetUserDefaultLCID ());
  3518 }
  3519 
  3520 
  3521 DEFUN ("w32-set-current-locale", Fw32_set_current_locale, Sw32_set_current_locale, 1, 1, 0,
  3522        doc: /* Make Windows locale LCID be the current locale setting for Emacs.
  3523 If successful, the new locale id is returned, otherwise nil.  */)
  3524   (Lisp_Object lcid)
  3525 {
  3526   CHECK_FIXNUM (lcid);
  3527 
  3528   if (!IsValidLocale (XFIXNUM (lcid), LCID_SUPPORTED))
  3529     return Qnil;
  3530 
  3531   if (!SetThreadLocale (XFIXNUM (lcid)))
  3532     return Qnil;
  3533 
  3534   /* Need to set input thread locale if present.  */
  3535   if (dwWindowsThreadId)
  3536     /* Reply is not needed.  */
  3537     PostThreadMessage (dwWindowsThreadId, WM_EMACS_SETLOCALE, XFIXNUM (lcid), 0);
  3538 
  3539   return make_fixnum (GetThreadLocale ());
  3540 }
  3541 
  3542 
  3543 /* We need to build a global list, since the EnumCodePages callback
  3544    function isn't given a context pointer.  */
  3545 Lisp_Object Vw32_valid_codepages;
  3546 
  3547 static BOOL CALLBACK ALIGN_STACK
  3548 enum_codepage_fn (LPTSTR codepageNum)
  3549 {
  3550   DWORD id = atoi (codepageNum);
  3551   Vw32_valid_codepages = Fcons (make_fixnum (id), Vw32_valid_codepages);
  3552   return TRUE;
  3553 }
  3554 
  3555 DEFUN ("w32-get-valid-codepages", Fw32_get_valid_codepages,
  3556        Sw32_get_valid_codepages, 0, 0, 0,
  3557        doc: /* Return list of all valid Windows codepages.  */)
  3558   (void)
  3559 {
  3560   Vw32_valid_codepages = Qnil;
  3561 
  3562   EnumSystemCodePages (enum_codepage_fn, CP_SUPPORTED);
  3563 
  3564   Vw32_valid_codepages = Fnreverse (Vw32_valid_codepages);
  3565   return Vw32_valid_codepages;
  3566 }
  3567 
  3568 
  3569 DEFUN ("w32-get-console-codepage", Fw32_get_console_codepage,
  3570        Sw32_get_console_codepage, 0, 0, 0,
  3571        doc: /* Return current Windows codepage for console input.  */)
  3572   (void)
  3573 {
  3574   return make_fixnum (GetConsoleCP ());
  3575 }
  3576 
  3577 
  3578 DEFUN ("w32-set-console-codepage", Fw32_set_console_codepage,
  3579        Sw32_set_console_codepage, 1, 1, 0,
  3580        doc: /* Make Windows codepage CP be the codepage for Emacs tty keyboard input.
  3581 This codepage setting affects keyboard input in tty mode.
  3582 If successful, the new CP is returned, otherwise nil.  */)
  3583   (Lisp_Object cp)
  3584 {
  3585   CHECK_FIXNUM (cp);
  3586 
  3587   if (!IsValidCodePage (XFIXNUM (cp)))
  3588     return Qnil;
  3589 
  3590   if (!SetConsoleCP (XFIXNUM (cp)))
  3591     return Qnil;
  3592 
  3593   return make_fixnum (GetConsoleCP ());
  3594 }
  3595 
  3596 
  3597 DEFUN ("w32-get-console-output-codepage", Fw32_get_console_output_codepage,
  3598        Sw32_get_console_output_codepage, 0, 0, 0,
  3599        doc: /* Return current Windows codepage for console output.  */)
  3600   (void)
  3601 {
  3602   return make_fixnum (GetConsoleOutputCP ());
  3603 }
  3604 
  3605 
  3606 DEFUN ("w32-set-console-output-codepage", Fw32_set_console_output_codepage,
  3607        Sw32_set_console_output_codepage, 1, 1, 0,
  3608        doc: /* Make Windows codepage CP be the codepage for Emacs console output.
  3609 This codepage setting affects display in tty mode.
  3610 If successful, the new CP is returned, otherwise nil.  */)
  3611   (Lisp_Object cp)
  3612 {
  3613   CHECK_FIXNUM (cp);
  3614 
  3615   if (!IsValidCodePage (XFIXNUM (cp)))
  3616     return Qnil;
  3617 
  3618   if (!SetConsoleOutputCP (XFIXNUM (cp)))
  3619     return Qnil;
  3620 
  3621   return make_fixnum (GetConsoleOutputCP ());
  3622 }
  3623 
  3624 
  3625 DEFUN ("w32-get-codepage-charset", Fw32_get_codepage_charset,
  3626        Sw32_get_codepage_charset, 1, 1, 0,
  3627        doc: /* Return charset ID corresponding to codepage CP.
  3628 Returns nil if the codepage is not valid or its charset ID could
  3629 not be determined.
  3630 
  3631 Note that this function is only guaranteed to work with ANSI
  3632 codepages; most console codepages are not supported and will
  3633 yield nil.  */)
  3634   (Lisp_Object cp)
  3635 {
  3636   CHARSETINFO info;
  3637   DWORD_PTR dwcp;
  3638 
  3639   CHECK_FIXNUM (cp);
  3640 
  3641   if (!IsValidCodePage (XFIXNUM (cp)))
  3642     return Qnil;
  3643 
  3644   /* Going through a temporary DWORD_PTR variable avoids compiler warning
  3645      about cast to pointer from integer of different size, when
  3646      building --with-wide-int or building for 64bit.  */
  3647   dwcp = XFIXNUM (cp);
  3648   if (TranslateCharsetInfo ((DWORD *) dwcp, &info, TCI_SRCCODEPAGE))
  3649     return make_fixnum (info.ciCharset);
  3650 
  3651   return Qnil;
  3652 }
  3653 
  3654 
  3655 DEFUN ("w32-get-valid-keyboard-layouts", Fw32_get_valid_keyboard_layouts,
  3656        Sw32_get_valid_keyboard_layouts, 0, 0, 0,
  3657        doc: /* Return list of Windows keyboard languages and layouts.
  3658 The return value is a list of pairs of language id and layout id.  */)
  3659   (void)
  3660 {
  3661   int num_layouts = GetKeyboardLayoutList (0, NULL);
  3662   HKL * layouts = (HKL *) alloca (num_layouts * sizeof (HKL));
  3663   Lisp_Object obj = Qnil;
  3664 
  3665   if (GetKeyboardLayoutList (num_layouts, layouts) == num_layouts)
  3666     {
  3667       while (--num_layouts >= 0)
  3668         {
  3669           HKL kl = layouts[num_layouts];
  3670 
  3671           obj = Fcons (Fcons (make_fixnum (LOWORD (kl)),
  3672                               make_fixnum (HIWORD (kl))),
  3673                        obj);
  3674         }
  3675     }
  3676 
  3677   return obj;
  3678 }
  3679 
  3680 
  3681 DEFUN ("w32-get-keyboard-layout", Fw32_get_keyboard_layout,
  3682        Sw32_get_keyboard_layout, 0, 0, 0,
  3683        doc: /* Return current Windows keyboard language and layout.
  3684 The return value is the cons of the language id and the layout id.  */)
  3685   (void)
  3686 {
  3687   HKL kl = GetKeyboardLayout (dwWindowsThreadId);
  3688 
  3689   return Fcons (make_fixnum (LOWORD (kl)),
  3690                 make_fixnum (HIWORD (kl)));
  3691 }
  3692 
  3693 
  3694 DEFUN ("w32-set-keyboard-layout", Fw32_set_keyboard_layout,
  3695        Sw32_set_keyboard_layout, 1, 1, 0,
  3696        doc: /* Make LAYOUT be the current keyboard layout for Emacs.
  3697 The keyboard layout setting affects interpretation of keyboard input.
  3698 If successful, the new layout id is returned, otherwise nil.  */)
  3699   (Lisp_Object layout)
  3700 {
  3701   HKL kl;
  3702 
  3703   CHECK_CONS (layout);
  3704   CHECK_FIXNUM (XCAR (layout));
  3705   CHECK_FIXNUM (XCDR (layout));
  3706 
  3707   kl = (HKL) (UINT_PTR) ((XFIXNUM (XCAR (layout)) & 0xffff)
  3708                          | (XFIXNUM (XCDR (layout)) << 16));
  3709 
  3710   /* Synchronize layout with input thread.  */
  3711   if (dwWindowsThreadId)
  3712     {
  3713       if (PostThreadMessage (dwWindowsThreadId, WM_EMACS_SETKEYBOARDLAYOUT,
  3714                              (WPARAM) kl, 0))
  3715         {
  3716           MSG msg;
  3717           GetMessage (&msg, NULL, WM_EMACS_DONE, WM_EMACS_DONE);
  3718 
  3719           if (msg.wParam == 0)
  3720             return Qnil;
  3721         }
  3722     }
  3723   else if (!ActivateKeyboardLayout (kl, 0))
  3724     return Qnil;
  3725 
  3726   return Fw32_get_keyboard_layout ();
  3727 }
  3728 
  3729 /* Two variables to interface between get_lcid and the EnumLocales
  3730    callback function below.  */
  3731 #ifndef LOCALE_NAME_MAX_LENGTH
  3732 # define LOCALE_NAME_MAX_LENGTH 85
  3733 #endif
  3734 static LCID found_lcid;
  3735 static char lname[3 * LOCALE_NAME_MAX_LENGTH + 1 + 1];
  3736 
  3737 /* Callback function for EnumLocales.  */
  3738 static BOOL CALLBACK
  3739 get_lcid_callback (LPTSTR locale_num_str)
  3740 {
  3741   char *endp;
  3742   char locval[2 * LOCALE_NAME_MAX_LENGTH + 1 + 1];
  3743   LCID try_lcid = strtoul (locale_num_str, &endp, 16);
  3744 
  3745   if (GetLocaleInfo (try_lcid, LOCALE_SABBREVLANGNAME,
  3746                      locval, LOCALE_NAME_MAX_LENGTH))
  3747     {
  3748       size_t locval_len;
  3749 
  3750       /* This is for when they only specify the language, as in "ENU".  */
  3751       if (stricmp (locval, lname) == 0)
  3752         {
  3753           found_lcid = try_lcid;
  3754           return FALSE;
  3755         }
  3756       locval_len = strlen (locval);
  3757       strcpy (locval + locval_len, "_");
  3758       if (GetLocaleInfo (try_lcid, LOCALE_SABBREVCTRYNAME,
  3759                          locval + locval_len + 1, LOCALE_NAME_MAX_LENGTH))
  3760         {
  3761           locval_len = strlen (locval);
  3762           if (strnicmp (locval, lname, locval_len) == 0
  3763               && (lname[locval_len] == '.'
  3764                   || lname[locval_len] == '\0'))
  3765             {
  3766               found_lcid = try_lcid;
  3767               return FALSE;
  3768             }
  3769         }
  3770     }
  3771   return TRUE;
  3772 }
  3773 
  3774 /* Return the Locale ID (LCID) number given the locale's name, a
  3775    string, in LOCALE_NAME.  This works by enumerating all the locales
  3776    supported by the system, until we find one whose name matches
  3777    LOCALE_NAME.  */
  3778 static LCID
  3779 get_lcid (const char *locale_name)
  3780 {
  3781   /* A simple cache.  */
  3782   static LCID last_lcid;
  3783   static char last_locale[1000];
  3784 
  3785   /* The code below is not thread-safe, as it uses static variables.
  3786      But this function is called only from the Lisp thread.  */
  3787   if (last_lcid > 0 && strcmp (locale_name, last_locale) == 0)
  3788     return last_lcid;
  3789 
  3790   strncpy (lname, locale_name, sizeof (lname) - 1);
  3791   lname[sizeof (lname) - 1] = '\0';
  3792   found_lcid = 0;
  3793   EnumSystemLocales (get_lcid_callback, LCID_SUPPORTED);
  3794   if (found_lcid > 0)
  3795     {
  3796       last_lcid = found_lcid;
  3797       strcpy (last_locale, locale_name);
  3798     }
  3799   return found_lcid;
  3800 }
  3801 
  3802 #ifndef _NLSCMPERROR
  3803 # define _NLSCMPERROR INT_MAX
  3804 #endif
  3805 #ifndef LINGUISTIC_IGNORECASE
  3806 # define LINGUISTIC_IGNORECASE  0x00000010
  3807 #endif
  3808 
  3809 typedef int (WINAPI *CompareStringW_Proc)
  3810   (LCID, DWORD, LPCWSTR, int, LPCWSTR, int);
  3811 
  3812 int
  3813 w32_compare_strings (const char *s1, const char *s2, char *locname,
  3814                      int ignore_case)
  3815 {
  3816   LCID lcid = GetThreadLocale ();
  3817   wchar_t *string1_w, *string2_w;
  3818   int val, needed;
  3819   static CompareStringW_Proc pCompareStringW;
  3820   DWORD flags = 0;
  3821 
  3822   USE_SAFE_ALLOCA;
  3823 
  3824   /* The LCID machinery doesn't seem to support the "C" locale, so we
  3825      need to do that by hand.  */
  3826   if (locname
  3827       && ((locname[0] == 'C' && (locname[1] == '\0' || locname[1] == '.'))
  3828           || strcmp (locname, "POSIX") == 0))
  3829     return (ignore_case ? stricmp (s1, s2) : strcmp (s1, s2));
  3830 
  3831   if (!g_b_init_compare_string_w)
  3832     {
  3833       if (os_subtype == OS_SUBTYPE_9X)
  3834         {
  3835           pCompareStringW = (CompareStringW_Proc)
  3836             get_proc_addr (LoadLibrary ("Unicows.dll"),
  3837                                   "CompareStringW");
  3838           if (!pCompareStringW)
  3839             {
  3840               errno = EINVAL;
  3841               /* This return value is compatible with wcscoll and
  3842                  other MS CRT functions.  */
  3843               return _NLSCMPERROR;
  3844             }
  3845         }
  3846       else
  3847         pCompareStringW = CompareStringW;
  3848 
  3849       g_b_init_compare_string_w = 1;
  3850     }
  3851 
  3852   needed = pMultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, s1, -1, NULL, 0);
  3853   if (needed > 0)
  3854     {
  3855       SAFE_NALLOCA (string1_w, 1, needed + 1);
  3856       pMultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, s1, -1,
  3857                             string1_w, needed);
  3858     }
  3859   else
  3860     {
  3861       errno = EINVAL;
  3862       return _NLSCMPERROR;
  3863     }
  3864 
  3865   needed = pMultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, s2, -1, NULL, 0);
  3866   if (needed > 0)
  3867     {
  3868       SAFE_NALLOCA (string2_w, 1, needed + 1);
  3869       pMultiByteToWideChar (CP_UTF8, MB_ERR_INVALID_CHARS, s2, -1,
  3870                             string2_w, needed);
  3871     }
  3872   else
  3873     {
  3874       SAFE_FREE ();
  3875       errno = EINVAL;
  3876       return _NLSCMPERROR;
  3877     }
  3878 
  3879   if (locname)
  3880     {
  3881       /* Convert locale name string to LCID.  We don't want to use
  3882          LocaleNameToLCID because (a) it is only available since
  3883          Vista, and (b) it doesn't accept locale names returned by
  3884          'setlocale' and 'GetLocaleInfo'.  */
  3885       LCID new_lcid = get_lcid (locname);
  3886 
  3887       if (new_lcid > 0)
  3888         lcid = new_lcid;
  3889       else
  3890         error ("Invalid locale %s: Invalid argument", locname);
  3891     }
  3892 
  3893   if (ignore_case)
  3894     {
  3895       /* NORM_IGNORECASE ignores any tertiary distinction, not just
  3896          case variants.  LINGUISTIC_IGNORECASE is more selective, and
  3897          is sensitive to the locale's language, but it is not
  3898          available before Vista.  */
  3899       if (w32_major_version >= 6)
  3900         flags |= LINGUISTIC_IGNORECASE;
  3901       else
  3902         flags |= NORM_IGNORECASE;
  3903     }
  3904   /* This approximates what glibc collation functions do when the
  3905      locale's codeset is UTF-8.  */
  3906   if (!NILP (Vw32_collate_ignore_punctuation))
  3907     flags |= NORM_IGNORESYMBOLS;
  3908   val = pCompareStringW (lcid, flags, string1_w, -1, string2_w, -1);
  3909   SAFE_FREE ();
  3910   if (!val)
  3911     {
  3912       errno = EINVAL;
  3913       return _NLSCMPERROR;
  3914     }
  3915   return val - 2;
  3916 }
  3917 
  3918 
  3919 void
  3920 syms_of_ntproc (void)
  3921 {
  3922   DEFSYM (Qhigh, "high");
  3923   DEFSYM (Qlow, "low");
  3924   DEFSYM (Qcygwin, "cygwin");
  3925   DEFSYM (Qmsys, "msys");
  3926   DEFSYM (Qw32_native, "w32-native");
  3927 
  3928   defsubr (&Sw32_has_winsock);
  3929   defsubr (&Sw32_unload_winsock);
  3930 
  3931   defsubr (&Sw32_short_file_name);
  3932   defsubr (&Sw32_long_file_name);
  3933   defsubr (&Sw32_set_process_priority);
  3934   defsubr (&Sw32_application_type);
  3935   defsubr (&Sw32_get_locale_info);
  3936   defsubr (&Sw32_get_current_locale_id);
  3937   defsubr (&Sw32_get_default_locale_id);
  3938   defsubr (&Sw32_get_valid_locale_ids);
  3939   defsubr (&Sw32_set_current_locale);
  3940 
  3941   defsubr (&Sw32_get_console_codepage);
  3942   defsubr (&Sw32_set_console_codepage);
  3943   defsubr (&Sw32_get_console_output_codepage);
  3944   defsubr (&Sw32_set_console_output_codepage);
  3945   defsubr (&Sw32_get_valid_codepages);
  3946   defsubr (&Sw32_get_codepage_charset);
  3947 
  3948   defsubr (&Sw32_get_valid_keyboard_layouts);
  3949   defsubr (&Sw32_get_keyboard_layout);
  3950   defsubr (&Sw32_set_keyboard_layout);
  3951 
  3952   DEFVAR_LISP ("w32-quote-process-args", Vw32_quote_process_args,
  3953                doc: /* Non-nil enables quoting of process arguments to ensure correct parsing.
  3954 Because Windows does not directly pass argv arrays to child processes,
  3955 programs have to reconstruct the argv array by parsing the command
  3956 line string.  For an argument to contain a space, it must be enclosed
  3957 in double quotes or it will be parsed as multiple arguments.
  3958 
  3959 If the value is a character, that character will be used to escape any
  3960 quote characters that appear, otherwise a suitable escape character
  3961 will be chosen based on the type of the program.  */);
  3962   Vw32_quote_process_args = Qt;
  3963 
  3964   DEFVAR_LISP ("w32-start-process-show-window",
  3965                Vw32_start_process_show_window,
  3966                doc: /* When nil, new child processes hide their windows.
  3967 When non-nil, they show their window in the method of their choice.
  3968 This variable doesn't affect GUI applications, which will never be hidden.  */);
  3969   Vw32_start_process_show_window = Qnil;
  3970 
  3971   DEFVAR_LISP ("w32-start-process-share-console",
  3972                Vw32_start_process_share_console,
  3973                doc: /* When nil, new child processes are given a new console.
  3974 When non-nil, they share the Emacs console; this has the limitation of
  3975 allowing only one DOS subprocess to run at a time (whether started directly
  3976 or indirectly by Emacs), and preventing Emacs from cleanly terminating the
  3977 subprocess group, but may allow Emacs to interrupt a subprocess that doesn't
  3978 otherwise respond to interrupts from Emacs.  */);
  3979   Vw32_start_process_share_console = Qnil;
  3980 
  3981   DEFVAR_LISP ("w32-start-process-inherit-error-mode",
  3982                Vw32_start_process_inherit_error_mode,
  3983                doc: /* When nil, new child processes revert to the default error mode.
  3984 When non-nil, they inherit their error mode setting from Emacs, which stops
  3985 them blocking when trying to access unmounted drives etc.  */);
  3986   Vw32_start_process_inherit_error_mode = Qt;
  3987 
  3988   DEFVAR_INT ("w32-pipe-read-delay", w32_pipe_read_delay,
  3989               doc: /* Forced delay before reading subprocess output.
  3990 This may need to be done to improve the buffering of subprocess output,
  3991 by avoiding the inefficiency of frequently reading small amounts of data.
  3992 Typically needed only with DOS programs on Windows 9X; set to 50 if
  3993 throughput with such programs is slow.
  3994 
  3995 If positive, the value is the number of milliseconds to sleep before
  3996 signaling that output from a subprocess is ready to be read.
  3997 If negative, the value is the number of time slices to wait (effectively
  3998 boosting the priority of the child process temporarily).
  3999 A value of zero disables waiting entirely.  */);
  4000   w32_pipe_read_delay = 0;
  4001 
  4002   DEFVAR_INT ("w32-pipe-buffer-size", w32_pipe_buffer_size,
  4003               doc: /* Size of buffer for pipes created to communicate with subprocesses.
  4004 The size is in bytes, and must be non-negative.  The default is zero,
  4005 which lets the OS use its default size, usually 4KB (4096 bytes).
  4006 Any negative value means to use the default value of zero.  */);
  4007   w32_pipe_buffer_size = 0;
  4008 
  4009   DEFVAR_LISP ("w32-downcase-file-names", Vw32_downcase_file_names,
  4010                doc: /* Non-nil means convert all-upper case file names to lower case.
  4011 This applies when performing completions and file name expansion.
  4012 Note that the value of this setting also affects remote file names,
  4013 so you probably don't want to set to non-nil if you use case-sensitive
  4014 filesystems via ange-ftp.  */);
  4015   Vw32_downcase_file_names = Qnil;
  4016 
  4017 #if 0
  4018   DEFVAR_LISP ("w32-generate-fake-inodes", Vw32_generate_fake_inodes,
  4019                doc: /* Non-nil means attempt to fake realistic inode values.
  4020 This works by hashing the truename of files, and should detect
  4021 aliasing between long and short (8.3 DOS) names, but can have
  4022 false positives because of hash collisions.  Note that determining
  4023 the truename of a file can be slow.  */);
  4024   Vw32_generate_fake_inodes = Qnil;
  4025 #endif
  4026 
  4027   DEFVAR_LISP ("w32-get-true-file-attributes", Vw32_get_true_file_attributes,
  4028                doc: /* Non-nil means determine accurate file attributes in `file-attributes'.
  4029 This option controls whether to issue additional system calls to determine
  4030 accurate link counts, file type, and ownership information.  It is more
  4031 useful for files on NTFS volumes, where hard links and file security are
  4032 supported, than on volumes of the FAT family.
  4033 
  4034 Without these system calls, link count will always be reported as 1 and file
  4035 ownership will be attributed to the current user.
  4036 The default value `local' means only issue these system calls for files
  4037 on local fixed drives.  A value of nil means never issue them.
  4038 Any other non-nil value means do this even on remote and removable drives
  4039 where the performance impact may be noticeable even on modern hardware.  */);
  4040   Vw32_get_true_file_attributes = Qlocal;
  4041 
  4042   DEFVAR_LISP ("w32-collate-ignore-punctuation",
  4043                Vw32_collate_ignore_punctuation,
  4044                doc: /* Non-nil causes string collation functions ignore punctuation on MS-Windows.
  4045 On Posix platforms, `string-collate-lessp' and `string-collate-equalp'
  4046 ignore punctuation characters when they compare strings, if the
  4047 locale's codeset is UTF-8, as in \"en_US.UTF-8\".  Binding this option
  4048 to a non-nil value will achieve a similar effect on MS-Windows, where
  4049 locales with UTF-8 codeset are not supported.
  4050 
  4051 Note that setting this to non-nil will also ignore blanks and symbols
  4052 in the strings.  So do NOT use this option when comparing file names
  4053 for equality, only when you need to sort them.  */);
  4054   Vw32_collate_ignore_punctuation = Qnil;
  4055 
  4056   staticpro (&Vw32_valid_locale_ids);
  4057   staticpro (&Vw32_valid_codepages);
  4058 }
  4059 /* end of w32proc.c */

/* [<][>][^][v][top][bottom][index][help] */