(detect_coding_iso2022): Do not exclude posibility of
[bpt/emacs.git] / src / callproc.c
index 3256202..63556f3 100644 (file)
@@ -15,7 +15,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 
 #include <signal.h>
@@ -46,7 +47,7 @@ extern char *strerror ();
 #include <windows.h>
 #include <stdlib.h>    /* for proper declaration of environ */
 #include <fcntl.h>
-#include "nt.h"
+#include "w32.h"
 #define _P_NOWAIT 1    /* from process.h */
 #endif
 
@@ -70,6 +71,8 @@ extern char *strerror ();
 #include "lisp.h"
 #include "commands.h"
 #include "buffer.h"
+#include "charset.h"
+#include "coding.h"
 #include <paths.h>
 #include "process.h"
 #include "syssignal.h"
@@ -94,6 +97,7 @@ Lisp_Object Vbinary_process_output;
 
 Lisp_Object Vexec_path, Vexec_directory, Vdata_directory, Vdoc_directory;
 Lisp_Object Vconfigure_info_directory;
+Lisp_Object Vtemp_file_name_pattern;
 
 Lisp_Object Vshell_file_name;
 
@@ -200,7 +204,9 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
   int fd[2];
   int filefd;
   register int pid;
-  char buf[1024];
+  char buf[16384];
+  char *bufptr = buf;
+  int bufsize = 16384;
   int count = specpdl_ptr - specpdl;
   register unsigned char **new_argv
     = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
@@ -215,6 +221,9 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
 #if 0
   int mask;
 #endif
+  struct coding_system process_coding; /* coding-system of process output */
+  struct coding_system argument_coding;        /* coding-system of arguments */
+
   CHECK_STRING (args[0], 0);
 
   error_file = Qt;
@@ -225,6 +234,83 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
     error ("Operating system cannot handle asynchronous subprocesses");
 #endif /* subprocesses */
 
+  /* Decide the coding-system for giving arguments and reading process
+     output.  */
+  {
+    Lisp_Object val, *args2;
+    /* Qt denotes we have not yet called Ffind_operation_coding_system.  */
+    Lisp_Object coding_systems = Qt;
+    int i;
+
+    /* If arguments are supplied, we may have to encode them.  */
+    if (nargs >= 5)
+      {
+       if (!NILP (Vcoding_system_for_write))
+         val = Vcoding_system_for_write;
+       else if (NILP (current_buffer->enable_multibyte_characters))
+         val = Qnil;
+       else
+         {
+           args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
+           args2[0] = Qcall_process;
+           for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
+           coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
+           if (CONSP (coding_systems))
+             val = XCONS (coding_systems)->cdr;
+           else if (CONSP (Vdefault_process_coding_system))
+             val = XCONS (Vdefault_process_coding_system)->cdr;
+           else
+             val = Qnil;
+         }
+       setup_coding_system (Fcheck_coding_system (val), &argument_coding);
+      }
+
+    /* If BUFFER is nil, we must read process output once and then
+       discard it, so setup coding system but with nil.  If BUFFER is
+       an integer, we can discard it without reading.  */
+    if (nargs < 3 || NILP (args[2]))
+      setup_coding_system (Qnil, &process_coding);
+    else if (!INTEGERP (args[2]))
+      {
+       val = Qnil;
+       if (!NILP (Vcoding_system_for_read))
+         val = Vcoding_system_for_read;
+       else if (NILP (current_buffer->enable_multibyte_characters))
+         val = Qemacs_mule;
+       else
+         {
+           if (!EQ (coding_systems, Qt))
+             {
+               args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
+               args2[0] = Qcall_process;
+               for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
+               coding_systems
+                 = Ffind_operation_coding_system (nargs + 1, args2);
+             }
+           if (CONSP (coding_systems))
+             val = XCONS (coding_systems)->car;
+           else if (CONSP (Vdefault_process_coding_system))
+             val = XCONS (Vdefault_process_coding_system)->car;
+           else
+             val = Qnil;
+         }
+       setup_coding_system (Fcheck_coding_system (val), &process_coding);
+#ifdef MSDOS
+       /* On MSDOS, if the user did not ask for binary,
+          treat it as "text" which means doing CRLF conversion.  */
+       /* FIXME: this probably should be moved into the guts of
+          `Ffind_operation_coding_system' for the case of `call-process'.  */
+       if (NILP (Vbinary_process_output))
+         {
+           process_coding.eol_type = CODING_EOL_CRLF;
+           if (process_coding.type == coding_type_no_conversion)
+             /* FIXME: should we set type to undecided?  */
+             process_coding.type = coding_type_emacs_mule;
+         }
+#endif
+      }
+  }
+
   if (nargs >= 2 && ! NILP (args[1]))
     {
       infile = Fexpand_file_name (args[1], current_buffer->directory);
@@ -242,7 +328,16 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
       if (CONSP (buffer))
        {
          if (CONSP (XCONS (buffer)->cdr))
-           error_file = XCONS (XCONS (buffer)->cdr)->car;
+           {
+             Lisp_Object stderr_file;
+             stderr_file = XCONS (XCONS (buffer)->cdr)->car;
+
+             if (NILP (stderr_file) || EQ (Qt, stderr_file))
+               error_file = stderr_file;
+             else
+               error_file = Fexpand_file_name (stderr_file, Qnil);
+           }
+
          buffer = XCONS (buffer)->car;
        }
 
@@ -315,18 +410,28 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
     for (i = 4; i < nargs; i++)
       {
        CHECK_STRING (args[i], i);
-       new_argv[i - 3] = XSTRING (args[i])->data;
+       if (argument_coding.type == coding_type_no_conversion)
+         new_argv[i - 3] = XSTRING (args[i])->data;
+       else
+         {
+           /* We must encode the arguments.  */
+           int size = encoding_buffer_size (&argument_coding,
+                                            XSTRING (args[i])->size);
+           int produced, dummy;
+           unsigned char *dummy1 = (unsigned char *) alloca (size);
+
+           /* The Irix 4.0 compiler barfs if we eliminate dummy.  */
+           new_argv[i - 3] = dummy1;
+           produced = encode_coding (&argument_coding,
+                                     XSTRING (args[i])->data, new_argv[i - 3],
+                                     XSTRING (args[i])->size, size, &dummy);
+           new_argv[i - 3][produced] = 0;
+         }
       }
     new_argv[i - 3] = 0;
   }
 
 #ifdef MSDOS /* MW, July 1993 */
-  /* These vars record information from process termination.
-     Clear them now before process can possibly terminate,
-     to avoid timing error if process terminates soon.  */
-  synch_process_death = 0;
-  synch_process_retcode = 0;
-
   if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
     strcpy (tempfile = alloca (strlen (outf) + 20), outf);
   else
@@ -344,20 +449,19 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
   if (outfilefd < 0)
     {
       close (filefd);
-      report_file_error ("Opening process output file", Fcons (tempfile, Qnil));
+      report_file_error ("Opening process output file",
+                        Fcons (build_string (tempfile), Qnil));
     }
-#endif
+  fd[0] = filefd;
+  fd[1] = outfilefd;
+#endif /* MSDOS */
 
   if (INTEGERP (buffer))
     fd[1] = open (NULL_DEVICE, O_WRONLY), fd[0] = -1;
   else
     {
 #ifndef MSDOS
-#ifdef WINDOWSNT
-      pipe_with_inherited_out (fd);
-#else  /* not WINDOWSNT */
       pipe (fd);
-#endif /* not WINDOWSNT */
 #endif
 #if 0
       /* Replaced by close_process_descs */
@@ -385,21 +489,6 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
     synch_process_death = 0;
     synch_process_retcode = 0;
 
-#ifdef MSDOS /* MW, July 1993 */
-    /* ??? Someone who knows MSDOG needs to check whether this properly
-       closes all descriptors that it opens.  */
-    pid = run_msdos_command (new_argv, current_dir, filefd, outfilefd);
-    close (outfilefd);
-    fd1 = -1; /* No harm in closing that one!  */
-    fd[0] = open (tempfile, NILP (Vbinary_process_output) ? O_TEXT : O_BINARY);
-    if (fd[0] < 0)
-      {
-       unlink (tempfile);
-       close (filefd);
-       report_file_error ("Cannot re-open temporary file", Qnil);
-      }
-#else /* not MSDOS */
-
     if (NILP (error_file))
       fd_error = open (NULL_DEVICE, O_WRONLY);
     else if (STRINGP (error_file))
@@ -416,12 +505,49 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
     if (fd_error < 0)
       {
        close (filefd);
-       close (fd[0]);
+       if (fd[0] != filefd)
+         close (fd[0]);
        if (fd1 >= 0)
          close (fd1);
-       report_file_error ("Cannot open", error_file);
+#ifdef MSDOS
+       unlink (tempfile);
+#endif
+       report_file_error ("Cannot redirect stderr",
+                          Fcons ((NILP (error_file)
+                                  ? build_string (NULL_DEVICE) : error_file),
+                                 Qnil));
       }
+#ifdef MSDOS /* MW, July 1993 */
+    /* ??? Someone who knows MSDOG needs to check whether this properly
+       closes all descriptors that it opens.
+
+       Note that run_msdos_command() actually returns the child process
+       exit status, not its PID, so we assign it to `synch_process_retcode'
+       below.  */
+    pid = run_msdos_command (new_argv, current_dir,
+                            filefd, outfilefd, fd_error);
 
+    /* Record that the synchronous process exited and note its
+       termination status.  */
+    synch_process_alive = 0;
+    synch_process_retcode = pid;
+    if (synch_process_retcode < 0)  /* means it couldn't be exec'ed */
+      synch_process_death = strerror(errno);
+
+    close (outfilefd);
+    if (fd_error != outfilefd)
+      close (fd_error);
+    fd1 = -1; /* No harm in closing that one!  */
+    /* Since CRLF is converted to LF within `decode_coding', we can
+       always open a file with binary mode.  */
+    fd[0] = open (tempfile, O_BINARY);
+    if (fd[0] < 0)
+      {
+       unlink (tempfile);
+       close (filefd);
+       report_file_error ("Cannot re-open temporary file", Qnil);
+      }
+#else /* not MSDOS */
 #ifdef WINDOWSNT
     pid = child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
 #else  /* not WINDOWSNT */
@@ -431,22 +557,29 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
       {
        if (fd[0] >= 0)
          close (fd[0]);
-#ifdef USG
+#ifdef HAVE_SETSID
+        setsid ();
+#endif
+#if defined (USG) && !defined (BSD_PGRPS)
         setpgrp ();
 #else
         setpgrp (pid, pid);
 #endif /* USG */
        child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
       }
-#endif /* not MSDOS */
 #endif /* not WINDOWSNT */
 
+    /* The MSDOS case did this already.  */
+    if (fd_error >= 0)
+      close (fd_error);
+#endif /* not MSDOS */
+
     environ = save_environ;
 
     /* Close most of our fd's, but not fd[0]
        since we will use that to read input from.  */
     close (filefd);
-    if (fd1 >= 0)
+    if (fd1 >= 0 && fd1 != fd_error)
       close (fd1);
   }
 
@@ -492,20 +625,74 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
   {
     register int nread;
     int first = 1;
+    int total_read = 0;
 
-    while ((nread = read (fd[0], buf, sizeof buf)) != 0)
+    while (1)
       {
-       if (nread < 0)
+       /* Repeatedly read until we've filled as much as possible
+          of the buffer size we have.  But don't read
+          less than 1024--save that for the next bufferful.  */
+
+       nread = process_coding.carryover_size; /* This value is initially 0. */
+       while (nread < bufsize - 1024)
          {
-#if defined (__osf__) && defined (__alpha)
-           continue;           /* Work around bug in DEC OSF/1 V3.0.  */
-#else
-           break;
-#endif
+           int this_read
+             = read (fd[0], bufptr + nread, bufsize - nread);
+
+           if (this_read < 0)
+             goto give_up;
+
+           if (this_read == 0)
+             goto give_up_1;
+
+           nread += this_read;
          }
+
+      give_up_1:
+
+       /* Now NREAD is the total amount of data in the buffer.  */
+       if (nread == 0)
+         /* Here, just tell decode_coding that we are processing the
+             last block.  We break the loop after decoding.  */
+         process_coding.last_block = 1;
+
        immediate_quit = 0;
+       total_read += nread;
+       
        if (!NILP (buffer))
-         insert (buf, nread);
+         {
+           if (process_coding.type == coding_type_no_conversion)
+             insert (bufptr, nread);
+           else
+             {                 /* We have to decode the input.  */
+               int size = decoding_buffer_size (&process_coding, bufsize);
+               char *decoding_buf = get_conversion_buffer (size);
+               int dummy;
+
+               nread = decode_coding (&process_coding, bufptr, decoding_buf,
+                                      nread, size, &dummy);
+               if (nread > 0)
+                 insert (decoding_buf, nread);
+             }
+         }
+
+       if (process_coding.last_block)
+         break;
+
+       /* Make the buffer bigger as we continue to read more data,
+          but not past 64k.  */
+       if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
+         {
+           bufsize *= 2;
+           bufptr = (char *) alloca (bufsize);
+         }
+
+       if (!NILP (buffer) && process_coding.carryover_size > 0)
+         /* We have carryover in the last decoding.  It should be
+             processed again after reading more data.  */
+         bcopy (process_coding.carryover, bufptr,
+                process_coding.carryover_size);
+
        if (!NILP (display) && INTERACTIVE)
          {
            if (first)
@@ -516,6 +703,7 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
        immediate_quit = 1;
        QUIT;
       }
+  give_up: ;
   }
 
   /* Wait for it to terminate, unless it already has.  */
@@ -573,13 +761,13 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
   struct gcpro gcpro1;
   Lisp_Object filename_string;
   register Lisp_Object start, end;
-#ifdef DOS_NT
-  char *tempfile;
-#else
-  char tempfile[20];
-#endif
   int count = specpdl_ptr - specpdl;
+  /* Qt denotes we have not yet called Ffind_operation_coding_system.  */
+  Lisp_Object coding_systems = Qt;
+  Lisp_Object val, *args2;
+  int i;
 #ifdef DOS_NT
+  char *tempfile;
   char *outf = '\0';
 
   if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
@@ -589,17 +777,21 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
       tempfile = alloca (20);
       *tempfile = '\0';
     }
-  dostounix_filename (tempfile);
-  if (tempfile[strlen (tempfile) - 1] != '/')
+  if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
     strcat (tempfile, "/");
-  strcat (tempfile, "detmp.XXX");
-#else /* not DOS_NT */
-
-#ifdef VMS
-  strcpy (tempfile, "tmp:emacsXXXXXX.");
+  if ('/' == DIRECTORY_SEP)
+    dostounix_filename (tempfile);
+  else
+    unixtodos_filename (tempfile);
+#ifdef WINDOWSNT
+  strcat (tempfile, "emXXXXXX");
 #else
-  strcpy (tempfile, "/tmp/emacsXXXXXX");
+  strcat (tempfile, "detmp.XXX");
 #endif
+#else /* not DOS_NT */
+  char *tempfile = (char *) alloca (XSTRING (Vtemp_file_name_pattern)->size + 1);
+  bcopy (XSTRING (Vtemp_file_name_pattern)->data, tempfile,
+        XSTRING (Vtemp_file_name_pattern)->size + 1);
 #endif /* not DOS_NT */
 
   mktemp (tempfile);
@@ -608,13 +800,59 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
   GCPRO1 (filename_string);
   start = args[0];
   end = args[1];
+  /* Decide coding-system of the contents of the temporary file.  */
 #ifdef DOS_NT
   specbind (Qbuffer_file_type, Vbinary_process_input);
-  Fwrite_region (start, end, filename_string, Qnil, Qlambda);
-  unbind_to (count, Qnil);
-#else  /* not DOS_NT */
-  Fwrite_region (start, end, filename_string, Qnil, Qlambda);
-#endif /* not DOS_NT */
+  if (NILP (Vbinary_process_input))
+    val = Qnil;
+  else
+#endif
+    {
+      if (!NILP (Vcoding_system_for_write))
+       val = Vcoding_system_for_write;
+      else if (NILP (current_buffer->enable_multibyte_characters))
+       val = Qnil;
+      else
+       {
+         args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
+         args2[0] = Qcall_process_region;
+         for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
+         coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
+         if (CONSP (coding_systems))
+           val = XCONS (coding_systems)->cdr;
+         else if (CONSP (Vdefault_process_coding_system))
+           val = XCONS (Vdefault_process_coding_system)->car;
+         else
+           val = Qnil;
+       }
+    }
+  specbind (intern ("coding-system-for-write"), val);
+  Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil);
+
+#ifdef DOS_NT
+  if (NILP (Vbinary_process_input))
+    val = Qnil;
+  else
+#endif
+    {
+      if (!NILP (Vcoding_system_for_read))
+       val = Vcoding_system_for_read;
+      else if (NILP (current_buffer->enable_multibyte_characters))
+       val = Qemacs_mule;
+      else
+       {
+         if (EQ (coding_systems, Qt))
+           {
+             args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
+             args2[0] = Qcall_process_region;
+             for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
+             coding_systems = Ffind_operation_coding_system (nargs + 1,
+                                                             args2);
+           }
+         val = CONSP (coding_systems) ? XCONS (coding_systems)->car : Qnil;
+       }
+    }
+  specbind (intern ("coding-system-for-read"), val);
 
   record_unwind_protect (delete_temp_file, filename_string);
 
@@ -662,7 +900,7 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
   char *pwd_var;
 #ifdef WINDOWSNT
   int cpid;
-  HANDLE handles[4];
+  HANDLE handles[3];
 #endif /* WINDOWSNT */
 
   int pid = getpid ();
@@ -770,14 +1008,25 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
      descriptors zero, one, or two; this could happen if Emacs is
      started with its standard in, out, or error closed, as might
      happen under X.  */
-  in = relocate_fd (in, 3);
-  if (out == err)
-    err = out = relocate_fd (out, 3);
-  else
-    {
+  {
+    int oin = in, oout = out;
+
+    /* We have to avoid relocating the same descriptor twice!  */
+
+    in = relocate_fd (in, 3);
+
+    if (out == oin)
+      out = in;
+    else
       out = relocate_fd (out, 3);
+
+    if (err == oin)
+      err = in;
+    else if (err == oout)
+      err = out;
+    else
       err = relocate_fd (err, 3);
-    }
+  }
 
   close (0);
   close (1);
@@ -791,7 +1040,7 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
   close (err);
 #endif /* not WINDOWSNT */
 
-#ifdef USG
+#if defined(USG) && !defined(BSD_PGRPS)
 #ifndef SETPGRP_RELEASES_CTTY
   setpgrp ();                  /* No arguments but equivalent in this case */
 #endif
@@ -820,8 +1069,9 @@ child_setup (in, out, err, new_argv, set_pgrp, current_dir)
   environ = env;
   execvp (new_argv[0], new_argv);
 
-  write (1, "Couldn't exec the program ", 26);
+  write (1, "Can't exec program: ", 20);
   write (1, new_argv[0], strlen (new_argv[0]));
+  write (1, "\n", 1);
   _exit (1);
 #endif /* not WINDOWSNT */
 #endif /* not MSDOS */
@@ -946,7 +1196,7 @@ init_callproc_1 ()
   Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
 }
 
-/* This is run after init_cmdargs, so that Vinvocation_directory is valid.  */
+/* This is run after init_cmdargs, when Vinstallation_directory is valid.  */
 
 init_callproc ()
 {
@@ -968,21 +1218,20 @@ init_callproc ()
          Vexec_path = nconc2 (Vexec_path, Fcons (tem, Qnil));
          Vexec_directory = Ffile_name_as_directory (tem);
 #endif /* not DOS_NT */
+       }
 
-         /* If we use ../lib-src, maybe use ../etc as well.
-            Do so if ../etc exists and has our DOC-... file in it.  */
-         if (data_dir == 0)
-           {
-             tem = Fexpand_file_name (build_string ("etc"),
-                                      Vinstallation_directory);
-             Vdoc_directory = Ffile_name_as_directory (tem);
-           }
+      /* Maybe use ../etc as well as ../lib-src.  */
+      if (data_dir == 0)
+       {
+         tem = Fexpand_file_name (build_string ("etc"),
+                                  Vinstallation_directory);
+         Vdoc_directory = Ffile_name_as_directory (tem);
        }
     }
 
   /* Look for the files that should be in etc.  We don't use
      Vinstallation_directory, because these files are never installed
-     in /bin near the executable, and they are never in the build
+     near the executable, and they are never in the build
      directory when that's different from the source directory.
 
      Instead, if these files are not in the nominal place, we try the
@@ -1006,21 +1255,13 @@ init_callproc ()
 
   tempdir = Fdirectory_file_name (Vexec_directory);
   if (access (XSTRING (tempdir)->data, 0) < 0)
-    {
-      fprintf (stderr,
-              "Warning: arch-dependent data dir (%s) does not exist.\n",
-              XSTRING (Vexec_directory)->data);
-      sleep (2);
-    }
+    dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
+                Vexec_directory);
 
   tempdir = Fdirectory_file_name (Vdata_directory);
   if (access (XSTRING (tempdir)->data, 0) < 0)
-    {
-      fprintf (stderr,
-              "Warning: arch-independent data dir (%s) does not exist.\n",
-              XSTRING (Vdata_directory)->data);
-      sleep (2);
-    }
+    dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
+                Vdata_directory);
 
 #ifdef VMS
   Vshell_file_name = build_string ("*dcl*");
@@ -1028,6 +1269,20 @@ init_callproc ()
   sh = (char *) getenv ("SHELL");
   Vshell_file_name = build_string (sh ? sh : "/bin/sh");
 #endif
+
+#ifdef VMS
+  Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX.");
+#else
+  if (getenv ("TMPDIR"))
+    {
+      char *dir = getenv ("TMPDIR");
+      Vtemp_file_name_pattern
+       = Fexpand_file_name (build_string ("emacsXXXXXX"),
+                            build_string (dir));
+    }
+  else
+    Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
+#endif
 }
 
 set_process_environment ()
@@ -1085,6 +1340,11 @@ Emacs's info files; the default value for Info-default-directory-list\n\
 includes this.");
   Vconfigure_info_directory = build_string (PATH_INFO);
 
+  DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern,
+    "Pattern for making names for temporary files.\n\
+This is used by `call-process-region'.");
+  /* This variable is initialized in init_callproc.  */
+
   DEFVAR_LISP ("process-environment", &Vprocess_environment,
     "List of environment variables for subprocesses to inherit.\n\
 Each element should be a string of the form ENVVARNAME=VALUE.\n\