| 1 | /* Synchronous subprocess invocation for GNU Emacs. |
| 2 | Copyright (C) 1985, 86,87,88,93,94,95, 1999 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Emacs. |
| 5 | |
| 6 | GNU Emacs is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GNU Emacs is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Emacs; see the file COPYING. If not, write to |
| 18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | |
| 22 | #include <config.h> |
| 23 | #include <signal.h> |
| 24 | #include <errno.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | extern int errno; |
| 28 | extern char *strerror (); |
| 29 | |
| 30 | /* Define SIGCHLD as an alias for SIGCLD. */ |
| 31 | |
| 32 | #if !defined (SIGCHLD) && defined (SIGCLD) |
| 33 | #define SIGCHLD SIGCLD |
| 34 | #endif /* SIGCLD */ |
| 35 | |
| 36 | #include <sys/types.h> |
| 37 | |
| 38 | #ifdef HAVE_UNISTD_H |
| 39 | #include <unistd.h> |
| 40 | #endif |
| 41 | |
| 42 | #include <sys/file.h> |
| 43 | #ifdef USG5 |
| 44 | #define INCLUDED_FCNTL |
| 45 | #include <fcntl.h> |
| 46 | #endif |
| 47 | |
| 48 | #ifdef WINDOWSNT |
| 49 | #define NOMINMAX |
| 50 | #include <windows.h> |
| 51 | #include <stdlib.h> /* for proper declaration of environ */ |
| 52 | #include <fcntl.h> |
| 53 | #include "w32.h" |
| 54 | #define _P_NOWAIT 1 /* from process.h */ |
| 55 | #endif |
| 56 | |
| 57 | #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */ |
| 58 | #define INCLUDED_FCNTL |
| 59 | #include <fcntl.h> |
| 60 | #include <sys/stat.h> |
| 61 | #include <sys/param.h> |
| 62 | #include <errno.h> |
| 63 | #endif /* MSDOS */ |
| 64 | |
| 65 | #ifndef O_RDONLY |
| 66 | #define O_RDONLY 0 |
| 67 | #endif |
| 68 | |
| 69 | #ifndef O_WRONLY |
| 70 | #define O_WRONLY 1 |
| 71 | #endif |
| 72 | |
| 73 | #include "lisp.h" |
| 74 | #include "commands.h" |
| 75 | #include "buffer.h" |
| 76 | #include "charset.h" |
| 77 | #include "ccl.h" |
| 78 | #include "coding.h" |
| 79 | #include <epaths.h> |
| 80 | #include "process.h" |
| 81 | #include "syssignal.h" |
| 82 | #include "systty.h" |
| 83 | |
| 84 | #ifdef MSDOS |
| 85 | #include "msdos.h" |
| 86 | #endif |
| 87 | |
| 88 | #ifdef VMS |
| 89 | extern noshare char **environ; |
| 90 | #else |
| 91 | extern char **environ; |
| 92 | #endif |
| 93 | |
| 94 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 95 | |
| 96 | Lisp_Object Vexec_path, Vexec_directory, Vdata_directory, Vdoc_directory; |
| 97 | Lisp_Object Vconfigure_info_directory; |
| 98 | Lisp_Object Vtemp_file_name_pattern; |
| 99 | |
| 100 | Lisp_Object Vshell_file_name; |
| 101 | |
| 102 | Lisp_Object Vprocess_environment; |
| 103 | |
| 104 | #ifdef DOS_NT |
| 105 | Lisp_Object Qbuffer_file_type; |
| 106 | #endif /* DOS_NT */ |
| 107 | |
| 108 | /* True iff we are about to fork off a synchronous process or if we |
| 109 | are waiting for it. */ |
| 110 | int synch_process_alive; |
| 111 | |
| 112 | /* Nonzero => this is a string explaining death of synchronous subprocess. */ |
| 113 | char *synch_process_death; |
| 114 | |
| 115 | /* If synch_process_death is zero, |
| 116 | this is exit code of synchronous subprocess. */ |
| 117 | int synch_process_retcode; |
| 118 | |
| 119 | extern Lisp_Object Vdoc_file_name; |
| 120 | |
| 121 | extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system; |
| 122 | \f |
| 123 | /* Clean up when exiting Fcall_process. |
| 124 | On MSDOS, delete the temporary file on any kind of termination. |
| 125 | On Unix, kill the process and any children on termination by signal. */ |
| 126 | |
| 127 | /* Nonzero if this is termination due to exit. */ |
| 128 | static int call_process_exited; |
| 129 | |
| 130 | #ifndef VMS /* VMS version is in vmsproc.c. */ |
| 131 | |
| 132 | static Lisp_Object |
| 133 | call_process_kill (fdpid) |
| 134 | Lisp_Object fdpid; |
| 135 | { |
| 136 | emacs_close (XFASTINT (Fcar (fdpid))); |
| 137 | EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL); |
| 138 | synch_process_alive = 0; |
| 139 | return Qnil; |
| 140 | } |
| 141 | |
| 142 | Lisp_Object |
| 143 | call_process_cleanup (fdpid) |
| 144 | Lisp_Object fdpid; |
| 145 | { |
| 146 | #if defined (MSDOS) || defined (macintosh) |
| 147 | /* for MSDOS fdpid is really (fd . tempfile) */ |
| 148 | register Lisp_Object file; |
| 149 | file = Fcdr (fdpid); |
| 150 | emacs_close (XFASTINT (Fcar (fdpid))); |
| 151 | if (strcmp (XSTRING (file)-> data, NULL_DEVICE) != 0) |
| 152 | unlink (XSTRING (file)->data); |
| 153 | #else /* not MSDOS and not macintosh */ |
| 154 | register int pid = XFASTINT (Fcdr (fdpid)); |
| 155 | |
| 156 | if (call_process_exited) |
| 157 | { |
| 158 | emacs_close (XFASTINT (Fcar (fdpid))); |
| 159 | return Qnil; |
| 160 | } |
| 161 | |
| 162 | if (EMACS_KILLPG (pid, SIGINT) == 0) |
| 163 | { |
| 164 | int count = specpdl_ptr - specpdl; |
| 165 | record_unwind_protect (call_process_kill, fdpid); |
| 166 | message1 ("Waiting for process to die...(type C-g again to kill it instantly)"); |
| 167 | immediate_quit = 1; |
| 168 | QUIT; |
| 169 | wait_for_termination (pid); |
| 170 | immediate_quit = 0; |
| 171 | specpdl_ptr = specpdl + count; /* Discard the unwind protect. */ |
| 172 | message1 ("Waiting for process to die...done"); |
| 173 | } |
| 174 | synch_process_alive = 0; |
| 175 | emacs_close (XFASTINT (Fcar (fdpid))); |
| 176 | #endif /* not MSDOS */ |
| 177 | return Qnil; |
| 178 | } |
| 179 | |
| 180 | DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0, |
| 181 | "Call PROGRAM synchronously in separate process.\n\ |
| 182 | The remaining arguments are optional.\n\ |
| 183 | The program's input comes from file INFILE (nil means `/dev/null').\n\ |
| 184 | Insert output in BUFFER before point; t means current buffer;\n\ |
| 185 | nil for BUFFER means discard it; 0 means discard and don't wait.\n\ |
| 186 | BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\ |
| 187 | REAL-BUFFER says what to do with standard output, as above,\n\ |
| 188 | while STDERR-FILE says what to do with standard error in the child.\n\ |
| 189 | STDERR-FILE may be nil (discard standard error output),\n\ |
| 190 | t (mix it with ordinary output), or a file name string.\n\ |
| 191 | \n\ |
| 192 | Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\ |
| 193 | Remaining arguments are strings passed as command arguments to PROGRAM.\n\ |
| 194 | \n\ |
| 195 | If BUFFER is 0, `call-process' returns immediately with value nil.\n\ |
| 196 | Otherwise it waits for PROGRAM to terminate\n\ |
| 197 | and returns a numeric exit status or a signal description string.\n\ |
| 198 | If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.") |
| 199 | (nargs, args) |
| 200 | int nargs; |
| 201 | register Lisp_Object *args; |
| 202 | { |
| 203 | Lisp_Object infile, buffer, current_dir, display, path; |
| 204 | int fd[2]; |
| 205 | int filefd; |
| 206 | register int pid; |
| 207 | char buf[16384]; |
| 208 | char *bufptr = buf; |
| 209 | int bufsize = 16384; |
| 210 | int count = specpdl_ptr - specpdl; |
| 211 | |
| 212 | register unsigned char **new_argv |
| 213 | = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *)); |
| 214 | struct buffer *old = current_buffer; |
| 215 | /* File to use for stderr in the child. |
| 216 | t means use same as standard output. */ |
| 217 | Lisp_Object error_file; |
| 218 | #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */ |
| 219 | char *outf, *tempfile; |
| 220 | int outfilefd; |
| 221 | #endif |
| 222 | #ifdef macintosh |
| 223 | char *tempfile; |
| 224 | int outfilefd; |
| 225 | #endif |
| 226 | #if 0 |
| 227 | int mask; |
| 228 | #endif |
| 229 | struct coding_system process_coding; /* coding-system of process output */ |
| 230 | struct coding_system argument_coding; /* coding-system of arguments */ |
| 231 | /* Set to the return value of Ffind_operation_coding_system. */ |
| 232 | Lisp_Object coding_systems; |
| 233 | |
| 234 | /* Qt denotes that Ffind_operation_coding_system is not yet called. */ |
| 235 | coding_systems = Qt; |
| 236 | |
| 237 | CHECK_STRING (args[0], 0); |
| 238 | |
| 239 | error_file = Qt; |
| 240 | |
| 241 | #ifndef subprocesses |
| 242 | /* Without asynchronous processes we cannot have BUFFER == 0. */ |
| 243 | if (nargs >= 3 |
| 244 | && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2]))) |
| 245 | error ("Operating system cannot handle asynchronous subprocesses"); |
| 246 | #endif /* subprocesses */ |
| 247 | |
| 248 | /* Decide the coding-system for giving arguments. */ |
| 249 | { |
| 250 | Lisp_Object val, *args2; |
| 251 | int i; |
| 252 | |
| 253 | /* If arguments are supplied, we may have to encode them. */ |
| 254 | if (nargs >= 5) |
| 255 | { |
| 256 | int must_encode = 0; |
| 257 | |
| 258 | for (i = 4; i < nargs; i++) |
| 259 | CHECK_STRING (args[i], i); |
| 260 | |
| 261 | for (i = 4; i < nargs; i++) |
| 262 | if (STRING_MULTIBYTE (args[i])) |
| 263 | must_encode = 1; |
| 264 | |
| 265 | if (!NILP (Vcoding_system_for_write)) |
| 266 | val = Vcoding_system_for_write; |
| 267 | else if (! must_encode) |
| 268 | val = Qnil; |
| 269 | else |
| 270 | { |
| 271 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); |
| 272 | args2[0] = Qcall_process; |
| 273 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; |
| 274 | coding_systems = Ffind_operation_coding_system (nargs + 1, args2); |
| 275 | if (CONSP (coding_systems)) |
| 276 | val = XCDR (coding_systems); |
| 277 | else if (CONSP (Vdefault_process_coding_system)) |
| 278 | val = XCDR (Vdefault_process_coding_system); |
| 279 | else |
| 280 | val = Qnil; |
| 281 | } |
| 282 | setup_coding_system (Fcheck_coding_system (val), &argument_coding); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | if (nargs >= 2 && ! NILP (args[1])) |
| 287 | { |
| 288 | infile = Fexpand_file_name (args[1], current_buffer->directory); |
| 289 | CHECK_STRING (infile, 1); |
| 290 | } |
| 291 | else |
| 292 | infile = build_string (NULL_DEVICE); |
| 293 | |
| 294 | if (nargs >= 3) |
| 295 | { |
| 296 | buffer = args[2]; |
| 297 | |
| 298 | /* If BUFFER is a list, its meaning is |
| 299 | (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */ |
| 300 | if (CONSP (buffer)) |
| 301 | { |
| 302 | if (CONSP (XCDR (buffer))) |
| 303 | { |
| 304 | Lisp_Object stderr_file; |
| 305 | stderr_file = XCAR (XCDR (buffer)); |
| 306 | |
| 307 | if (NILP (stderr_file) || EQ (Qt, stderr_file)) |
| 308 | error_file = stderr_file; |
| 309 | else |
| 310 | error_file = Fexpand_file_name (stderr_file, Qnil); |
| 311 | } |
| 312 | |
| 313 | buffer = XCAR (buffer); |
| 314 | } |
| 315 | |
| 316 | if (!(EQ (buffer, Qnil) |
| 317 | || EQ (buffer, Qt) |
| 318 | || INTEGERP (buffer))) |
| 319 | { |
| 320 | Lisp_Object spec_buffer; |
| 321 | spec_buffer = buffer; |
| 322 | buffer = Fget_buffer_create (buffer); |
| 323 | /* Mention the buffer name for a better error message. */ |
| 324 | if (NILP (buffer)) |
| 325 | CHECK_BUFFER (spec_buffer, 2); |
| 326 | CHECK_BUFFER (buffer, 2); |
| 327 | } |
| 328 | } |
| 329 | else |
| 330 | buffer = Qnil; |
| 331 | |
| 332 | /* Make sure that the child will be able to chdir to the current |
| 333 | buffer's current directory, or its unhandled equivalent. We |
| 334 | can't just have the child check for an error when it does the |
| 335 | chdir, since it's in a vfork. |
| 336 | |
| 337 | We have to GCPRO around this because Fexpand_file_name, |
| 338 | Funhandled_file_name_directory, and Ffile_accessible_directory_p |
| 339 | might call a file name handling function. The argument list is |
| 340 | protected by the caller, so all we really have to worry about is |
| 341 | buffer. */ |
| 342 | { |
| 343 | struct gcpro gcpro1, gcpro2, gcpro3; |
| 344 | |
| 345 | current_dir = current_buffer->directory; |
| 346 | |
| 347 | GCPRO3 (infile, buffer, current_dir); |
| 348 | |
| 349 | current_dir |
| 350 | = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir), |
| 351 | Qnil); |
| 352 | if (NILP (Ffile_accessible_directory_p (current_dir))) |
| 353 | report_file_error ("Setting current directory", |
| 354 | Fcons (current_buffer->directory, Qnil)); |
| 355 | |
| 356 | UNGCPRO; |
| 357 | } |
| 358 | |
| 359 | display = nargs >= 4 ? args[3] : Qnil; |
| 360 | |
| 361 | filefd = emacs_open (XSTRING (infile)->data, O_RDONLY, 0); |
| 362 | if (filefd < 0) |
| 363 | { |
| 364 | report_file_error ("Opening process input file", Fcons (infile, Qnil)); |
| 365 | } |
| 366 | /* Search for program; barf if not found. */ |
| 367 | { |
| 368 | struct gcpro gcpro1; |
| 369 | |
| 370 | GCPRO1 (current_dir); |
| 371 | openp (Vexec_path, args[0], EXEC_SUFFIXES, &path, 1); |
| 372 | UNGCPRO; |
| 373 | } |
| 374 | if (NILP (path)) |
| 375 | { |
| 376 | emacs_close (filefd); |
| 377 | report_file_error ("Searching for program", Fcons (args[0], Qnil)); |
| 378 | } |
| 379 | new_argv[0] = XSTRING (path)->data; |
| 380 | if (nargs > 4) |
| 381 | { |
| 382 | register int i; |
| 383 | |
| 384 | if (! CODING_REQUIRE_ENCODING (&argument_coding)) |
| 385 | { |
| 386 | for (i = 4; i < nargs; i++) |
| 387 | new_argv[i - 3] = XSTRING (args[i])->data; |
| 388 | } |
| 389 | else |
| 390 | { |
| 391 | /* We must encode the arguments. */ |
| 392 | struct gcpro gcpro1, gcpro2, gcpro3; |
| 393 | |
| 394 | GCPRO3 (infile, buffer, current_dir); |
| 395 | for (i = 4; i < nargs; i++) |
| 396 | { |
| 397 | int size = encoding_buffer_size (&argument_coding, |
| 398 | STRING_BYTES (XSTRING (args[i]))); |
| 399 | unsigned char *dummy1 = (unsigned char *) alloca (size); |
| 400 | |
| 401 | /* The Irix 4.0 compiler barfs if we eliminate dummy. */ |
| 402 | new_argv[i - 3] = dummy1; |
| 403 | argument_coding.mode |= CODING_MODE_LAST_BLOCK; |
| 404 | encode_coding (&argument_coding, |
| 405 | XSTRING (args[i])->data, |
| 406 | new_argv[i - 3], |
| 407 | STRING_BYTES (XSTRING (args[i])), |
| 408 | size); |
| 409 | new_argv[i - 3][argument_coding.produced] = 0; |
| 410 | /* We have to initialize CCL program status again. */ |
| 411 | if (argument_coding.type == coding_type_ccl) |
| 412 | setup_ccl_program (&(argument_coding.spec.ccl.encoder), Qnil); |
| 413 | } |
| 414 | UNGCPRO; |
| 415 | } |
| 416 | new_argv[nargs - 3] = 0; |
| 417 | } |
| 418 | else |
| 419 | new_argv[1] = 0; |
| 420 | |
| 421 | #ifdef MSDOS /* MW, July 1993 */ |
| 422 | if ((outf = egetenv ("TMPDIR"))) |
| 423 | strcpy (tempfile = alloca (strlen (outf) + 20), outf); |
| 424 | else |
| 425 | { |
| 426 | tempfile = alloca (20); |
| 427 | *tempfile = '\0'; |
| 428 | } |
| 429 | dostounix_filename (tempfile); |
| 430 | if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/') |
| 431 | strcat (tempfile, "/"); |
| 432 | strcat (tempfile, "detmp.XXX"); |
| 433 | mktemp (tempfile); |
| 434 | |
| 435 | outfilefd = creat (tempfile, S_IREAD | S_IWRITE); |
| 436 | if (outfilefd < 0) |
| 437 | { |
| 438 | emacs_close (filefd); |
| 439 | report_file_error ("Opening process output file", |
| 440 | Fcons (build_string (tempfile), Qnil)); |
| 441 | } |
| 442 | fd[0] = filefd; |
| 443 | fd[1] = outfilefd; |
| 444 | #endif /* MSDOS */ |
| 445 | |
| 446 | #ifdef macintosh |
| 447 | /* Since we don't have pipes on the Mac, create a temporary file to |
| 448 | hold the output of the subprocess. */ |
| 449 | tempfile = (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1); |
| 450 | bcopy (XSTRING (Vtemp_file_name_pattern)->data, tempfile, |
| 451 | STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1); |
| 452 | |
| 453 | mktemp (tempfile); |
| 454 | |
| 455 | outfilefd = creat (tempfile, S_IREAD | S_IWRITE); |
| 456 | if (outfilefd < 0) |
| 457 | { |
| 458 | close (filefd); |
| 459 | report_file_error ("Opening process output file", |
| 460 | Fcons (build_string (tempfile), Qnil)); |
| 461 | } |
| 462 | fd[0] = filefd; |
| 463 | fd[1] = outfilefd; |
| 464 | #endif /* macintosh */ |
| 465 | |
| 466 | if (INTEGERP (buffer)) |
| 467 | fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1; |
| 468 | else |
| 469 | { |
| 470 | #ifndef MSDOS |
| 471 | #ifndef macintosh |
| 472 | pipe (fd); |
| 473 | #endif |
| 474 | #endif |
| 475 | #if 0 |
| 476 | /* Replaced by close_process_descs */ |
| 477 | set_exclusive_use (fd[0]); |
| 478 | #endif |
| 479 | } |
| 480 | |
| 481 | { |
| 482 | /* child_setup must clobber environ in systems with true vfork. |
| 483 | Protect it from permanent change. */ |
| 484 | register char **save_environ = environ; |
| 485 | register int fd1 = fd[1]; |
| 486 | int fd_error = fd1; |
| 487 | |
| 488 | #if 0 /* Some systems don't have sigblock. */ |
| 489 | mask = sigblock (sigmask (SIGCHLD)); |
| 490 | #endif |
| 491 | |
| 492 | /* Record that we're about to create a synchronous process. */ |
| 493 | synch_process_alive = 1; |
| 494 | |
| 495 | /* These vars record information from process termination. |
| 496 | Clear them now before process can possibly terminate, |
| 497 | to avoid timing error if process terminates soon. */ |
| 498 | synch_process_death = 0; |
| 499 | synch_process_retcode = 0; |
| 500 | |
| 501 | if (NILP (error_file)) |
| 502 | fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0); |
| 503 | else if (STRINGP (error_file)) |
| 504 | { |
| 505 | #ifdef DOS_NT |
| 506 | fd_error = emacs_open (XSTRING (error_file)->data, |
| 507 | O_WRONLY | O_TRUNC | O_CREAT | O_TEXT, |
| 508 | S_IREAD | S_IWRITE); |
| 509 | #else /* not DOS_NT */ |
| 510 | fd_error = creat (XSTRING (error_file)->data, 0666); |
| 511 | #endif /* not DOS_NT */ |
| 512 | } |
| 513 | |
| 514 | if (fd_error < 0) |
| 515 | { |
| 516 | emacs_close (filefd); |
| 517 | if (fd[0] != filefd) |
| 518 | emacs_close (fd[0]); |
| 519 | if (fd1 >= 0) |
| 520 | emacs_close (fd1); |
| 521 | #ifdef MSDOS |
| 522 | unlink (tempfile); |
| 523 | #endif |
| 524 | report_file_error ("Cannot redirect stderr", |
| 525 | Fcons ((NILP (error_file) |
| 526 | ? build_string (NULL_DEVICE) : error_file), |
| 527 | Qnil)); |
| 528 | } |
| 529 | |
| 530 | current_dir = ENCODE_FILE (current_dir); |
| 531 | |
| 532 | #ifdef macintosh |
| 533 | { |
| 534 | /* Call run_mac_command in sysdep.c here directly instead of doing |
| 535 | a child_setup as for MSDOS and other platforms. Note that this |
| 536 | code does not handle passing the environment to the synchronous |
| 537 | Mac subprocess. */ |
| 538 | char *infn, *outfn, *errfn, *currdn; |
| 539 | |
| 540 | /* close these files so subprocess can write to them */ |
| 541 | close (outfilefd); |
| 542 | if (fd_error != outfilefd) |
| 543 | close (fd_error); |
| 544 | fd1 = -1; /* No harm in closing that one! */ |
| 545 | |
| 546 | infn = XSTRING (infile)->data; |
| 547 | outfn = tempfile; |
| 548 | if (NILP (error_file)) |
| 549 | errfn = NULL_DEVICE; |
| 550 | else if (EQ (Qt, error_file)) |
| 551 | errfn = outfn; |
| 552 | else |
| 553 | errfn = XSTRING (error_file)->data; |
| 554 | currdn = XSTRING (current_dir)->data; |
| 555 | pid = run_mac_command (new_argv, currdn, infn, outfn, errfn); |
| 556 | |
| 557 | /* Record that the synchronous process exited and note its |
| 558 | termination status. */ |
| 559 | synch_process_alive = 0; |
| 560 | synch_process_retcode = pid; |
| 561 | if (synch_process_retcode < 0) /* means it couldn't be exec'ed */ |
| 562 | { |
| 563 | synchronize_messages_locale (); |
| 564 | synch_process_death = strerror (errno); |
| 565 | } |
| 566 | |
| 567 | /* Since CRLF is converted to LF within `decode_coding', we can |
| 568 | always open a file with binary mode. */ |
| 569 | fd[0] = open (tempfile, O_BINARY); |
| 570 | if (fd[0] < 0) |
| 571 | { |
| 572 | unlink (tempfile); |
| 573 | close (filefd); |
| 574 | report_file_error ("Cannot re-open temporary file", Qnil); |
| 575 | } |
| 576 | } |
| 577 | #else /* not macintosh */ |
| 578 | #ifdef MSDOS /* MW, July 1993 */ |
| 579 | /* Note that on MSDOS `child_setup' actually returns the child process |
| 580 | exit status, not its PID, so we assign it to `synch_process_retcode' |
| 581 | below. */ |
| 582 | pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv, |
| 583 | 0, current_dir); |
| 584 | |
| 585 | /* Record that the synchronous process exited and note its |
| 586 | termination status. */ |
| 587 | synch_process_alive = 0; |
| 588 | synch_process_retcode = pid; |
| 589 | if (synch_process_retcode < 0) /* means it couldn't be exec'ed */ |
| 590 | { |
| 591 | synchronize_messages_locale (); |
| 592 | synch_process_death = strerror (errno); |
| 593 | } |
| 594 | |
| 595 | emacs_close (outfilefd); |
| 596 | if (fd_error != outfilefd) |
| 597 | emacs_close (fd_error); |
| 598 | fd1 = -1; /* No harm in closing that one! */ |
| 599 | /* Since CRLF is converted to LF within `decode_coding', we can |
| 600 | always open a file with binary mode. */ |
| 601 | fd[0] = emacs_open (tempfile, O_RDONLY | O_BINARY, 0); |
| 602 | if (fd[0] < 0) |
| 603 | { |
| 604 | unlink (tempfile); |
| 605 | emacs_close (filefd); |
| 606 | report_file_error ("Cannot re-open temporary file", Qnil); |
| 607 | } |
| 608 | #else /* not MSDOS */ |
| 609 | #ifdef WINDOWSNT |
| 610 | pid = child_setup (filefd, fd1, fd_error, (char **) new_argv, |
| 611 | 0, current_dir); |
| 612 | #else /* not WINDOWSNT */ |
| 613 | pid = vfork (); |
| 614 | |
| 615 | if (pid == 0) |
| 616 | { |
| 617 | if (fd[0] >= 0) |
| 618 | emacs_close (fd[0]); |
| 619 | #ifdef HAVE_SETSID |
| 620 | setsid (); |
| 621 | #endif |
| 622 | #if defined (USG) && !defined (BSD_PGRPS) |
| 623 | setpgrp (); |
| 624 | #else |
| 625 | setpgrp (pid, pid); |
| 626 | #endif /* USG */ |
| 627 | child_setup (filefd, fd1, fd_error, (char **) new_argv, |
| 628 | 0, current_dir); |
| 629 | } |
| 630 | #endif /* not WINDOWSNT */ |
| 631 | |
| 632 | /* The MSDOS case did this already. */ |
| 633 | if (fd_error >= 0) |
| 634 | emacs_close (fd_error); |
| 635 | #endif /* not MSDOS */ |
| 636 | #endif /* not macintosh */ |
| 637 | |
| 638 | environ = save_environ; |
| 639 | |
| 640 | /* Close most of our fd's, but not fd[0] |
| 641 | since we will use that to read input from. */ |
| 642 | emacs_close (filefd); |
| 643 | if (fd1 >= 0 && fd1 != fd_error) |
| 644 | emacs_close (fd1); |
| 645 | } |
| 646 | |
| 647 | if (pid < 0) |
| 648 | { |
| 649 | if (fd[0] >= 0) |
| 650 | emacs_close (fd[0]); |
| 651 | report_file_error ("Doing vfork", Qnil); |
| 652 | } |
| 653 | |
| 654 | if (INTEGERP (buffer)) |
| 655 | { |
| 656 | if (fd[0] >= 0) |
| 657 | emacs_close (fd[0]); |
| 658 | #ifndef subprocesses |
| 659 | /* If Emacs has been built with asynchronous subprocess support, |
| 660 | we don't need to do this, I think because it will then have |
| 661 | the facilities for handling SIGCHLD. */ |
| 662 | wait_without_blocking (); |
| 663 | #endif /* subprocesses */ |
| 664 | return Qnil; |
| 665 | } |
| 666 | |
| 667 | /* Enable sending signal if user quits below. */ |
| 668 | call_process_exited = 0; |
| 669 | |
| 670 | #if defined(MSDOS) || defined(macintosh) |
| 671 | /* MSDOS needs different cleanup information. */ |
| 672 | record_unwind_protect (call_process_cleanup, |
| 673 | Fcons (make_number (fd[0]), build_string (tempfile))); |
| 674 | #else |
| 675 | record_unwind_protect (call_process_cleanup, |
| 676 | Fcons (make_number (fd[0]), make_number (pid))); |
| 677 | #endif /* not MSDOS and not macintosh */ |
| 678 | |
| 679 | |
| 680 | if (BUFFERP (buffer)) |
| 681 | Fset_buffer (buffer); |
| 682 | |
| 683 | if (NILP (buffer)) |
| 684 | { |
| 685 | /* If BUFFER is nil, we must read process output once and then |
| 686 | discard it, so setup coding system but with nil. */ |
| 687 | setup_coding_system (Qnil, &process_coding); |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | Lisp_Object val, *args2; |
| 692 | |
| 693 | val = Qnil; |
| 694 | if (!NILP (Vcoding_system_for_read)) |
| 695 | val = Vcoding_system_for_read; |
| 696 | else |
| 697 | { |
| 698 | if (EQ (coding_systems, Qt)) |
| 699 | { |
| 700 | int i; |
| 701 | |
| 702 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); |
| 703 | args2[0] = Qcall_process; |
| 704 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; |
| 705 | coding_systems |
| 706 | = Ffind_operation_coding_system (nargs + 1, args2); |
| 707 | } |
| 708 | if (CONSP (coding_systems)) |
| 709 | val = XCAR (coding_systems); |
| 710 | else if (CONSP (Vdefault_process_coding_system)) |
| 711 | val = XCAR (Vdefault_process_coding_system); |
| 712 | else |
| 713 | val = Qnil; |
| 714 | } |
| 715 | setup_coding_system (Fcheck_coding_system (val), &process_coding); |
| 716 | /* In unibyte mode, character code conversion should not take |
| 717 | place but EOL conversion should. So, setup raw-text or one |
| 718 | of the subsidiary according to the information just setup. */ |
| 719 | if (NILP (current_buffer->enable_multibyte_characters) |
| 720 | && !NILP (val)) |
| 721 | setup_raw_text_coding_system (&process_coding); |
| 722 | } |
| 723 | |
| 724 | immediate_quit = 1; |
| 725 | QUIT; |
| 726 | |
| 727 | { |
| 728 | register int nread; |
| 729 | int first = 1; |
| 730 | int total_read = 0; |
| 731 | int carryover = 0; |
| 732 | int display_on_the_fly = !NILP (display) && INTERACTIVE; |
| 733 | struct coding_system saved_coding; |
| 734 | |
| 735 | saved_coding = process_coding; |
| 736 | |
| 737 | while (1) |
| 738 | { |
| 739 | /* Repeatedly read until we've filled as much as possible |
| 740 | of the buffer size we have. But don't read |
| 741 | less than 1024--save that for the next bufferful. */ |
| 742 | nread = carryover; |
| 743 | while (nread < bufsize - 1024) |
| 744 | { |
| 745 | int this_read = emacs_read (fd[0], bufptr + nread, |
| 746 | bufsize - nread); |
| 747 | |
| 748 | if (this_read < 0) |
| 749 | goto give_up; |
| 750 | |
| 751 | if (this_read == 0) |
| 752 | { |
| 753 | process_coding.mode |= CODING_MODE_LAST_BLOCK; |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | nread += this_read; |
| 758 | total_read += this_read; |
| 759 | |
| 760 | if (display_on_the_fly) |
| 761 | break; |
| 762 | } |
| 763 | |
| 764 | /* Now NREAD is the total amount of data in the buffer. */ |
| 765 | immediate_quit = 0; |
| 766 | |
| 767 | if (!NILP (buffer)) |
| 768 | { |
| 769 | if (process_coding.type == coding_type_no_conversion) |
| 770 | insert (bufptr, nread); |
| 771 | else |
| 772 | { /* We have to decode the input. */ |
| 773 | int size = decoding_buffer_size (&process_coding, nread); |
| 774 | char *decoding_buf = (char *) xmalloc (size); |
| 775 | |
| 776 | decode_coding (&process_coding, bufptr, decoding_buf, |
| 777 | nread, size); |
| 778 | if (display_on_the_fly |
| 779 | && saved_coding.type == coding_type_undecided |
| 780 | && process_coding.type != coding_type_undecided) |
| 781 | { |
| 782 | /* We have detected some coding system. But, |
| 783 | there's a possibility that the detection was |
| 784 | done by insufficient data. So, we give up |
| 785 | displaying on the fly. */ |
| 786 | xfree (decoding_buf); |
| 787 | display_on_the_fly = 0; |
| 788 | process_coding = saved_coding; |
| 789 | carryover = nread; |
| 790 | continue; |
| 791 | } |
| 792 | if (process_coding.produced > 0) |
| 793 | insert (decoding_buf, process_coding.produced); |
| 794 | xfree (decoding_buf); |
| 795 | carryover = nread - process_coding.consumed; |
| 796 | if (carryover > 0) |
| 797 | { |
| 798 | /* As CARRYOVER should not be that large, we had |
| 799 | better avoid overhead of bcopy. */ |
| 800 | char *p = bufptr + process_coding.consumed; |
| 801 | char *pend = p + carryover; |
| 802 | char *dst = bufptr; |
| 803 | |
| 804 | while (p < pend) *dst++ = *p++; |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | if (process_coding.mode & CODING_MODE_LAST_BLOCK) |
| 809 | { |
| 810 | if (carryover > 0) |
| 811 | insert (bufptr, carryover); |
| 812 | break; |
| 813 | } |
| 814 | |
| 815 | /* Make the buffer bigger as we continue to read more data, |
| 816 | but not past 64k. */ |
| 817 | if (bufsize < 64 * 1024 && total_read > 32 * bufsize) |
| 818 | { |
| 819 | bufsize *= 2; |
| 820 | bufptr = (char *) alloca (bufsize); |
| 821 | } |
| 822 | |
| 823 | if (!NILP (display) && INTERACTIVE) |
| 824 | { |
| 825 | if (first) |
| 826 | prepare_menu_bars (); |
| 827 | first = 0; |
| 828 | redisplay_preserve_echo_area (); |
| 829 | } |
| 830 | immediate_quit = 1; |
| 831 | QUIT; |
| 832 | } |
| 833 | give_up: ; |
| 834 | |
| 835 | Vlast_coding_system_used = process_coding.symbol; |
| 836 | |
| 837 | /* If the caller required, let the buffer inherit the |
| 838 | coding-system used to decode the process output. */ |
| 839 | if (inherit_process_coding_system) |
| 840 | call1 (intern ("after-insert-file-set-buffer-file-coding-system"), |
| 841 | make_number (total_read)); |
| 842 | } |
| 843 | |
| 844 | /* Wait for it to terminate, unless it already has. */ |
| 845 | wait_for_termination (pid); |
| 846 | |
| 847 | immediate_quit = 0; |
| 848 | |
| 849 | set_buffer_internal (old); |
| 850 | |
| 851 | /* Don't kill any children that the subprocess may have left behind |
| 852 | when exiting. */ |
| 853 | call_process_exited = 1; |
| 854 | |
| 855 | unbind_to (count, Qnil); |
| 856 | |
| 857 | if (synch_process_death) |
| 858 | return code_convert_string_norecord (build_string (synch_process_death), |
| 859 | Vlocale_coding_system, 0); |
| 860 | return make_number (synch_process_retcode); |
| 861 | } |
| 862 | #endif |
| 863 | \f |
| 864 | static Lisp_Object |
| 865 | delete_temp_file (name) |
| 866 | Lisp_Object name; |
| 867 | { |
| 868 | /* Use Fdelete_file (indirectly) because that runs a file name handler. |
| 869 | We did that when writing the file, so we should do so when deleting. */ |
| 870 | internal_delete_file (name); |
| 871 | } |
| 872 | |
| 873 | DEFUN ("call-process-region", Fcall_process_region, Scall_process_region, |
| 874 | 3, MANY, 0, |
| 875 | "Send text from START to END to a synchronous process running PROGRAM.\n\ |
| 876 | The remaining arguments are optional.\n\ |
| 877 | Delete the text if fourth arg DELETE is non-nil.\n\ |
| 878 | \n\ |
| 879 | Insert output in BUFFER before point; t means current buffer;\n\ |
| 880 | nil for BUFFER means discard it; 0 means discard and don't wait.\n\ |
| 881 | BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\ |
| 882 | REAL-BUFFER says what to do with standard output, as above,\n\ |
| 883 | while STDERR-FILE says what to do with standard error in the child.\n\ |
| 884 | STDERR-FILE may be nil (discard standard error output),\n\ |
| 885 | t (mix it with ordinary output), or a file name string.\n\ |
| 886 | \n\ |
| 887 | Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\ |
| 888 | Remaining args are passed to PROGRAM at startup as command args.\n\ |
| 889 | \n\ |
| 890 | If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\ |
| 891 | Otherwise it waits for PROGRAM to terminate\n\ |
| 892 | and returns a numeric exit status or a signal description string.\n\ |
| 893 | If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.") |
| 894 | (nargs, args) |
| 895 | int nargs; |
| 896 | register Lisp_Object *args; |
| 897 | { |
| 898 | struct gcpro gcpro1; |
| 899 | Lisp_Object filename_string; |
| 900 | register Lisp_Object start, end; |
| 901 | int count = specpdl_ptr - specpdl; |
| 902 | /* Qt denotes we have not yet called Ffind_operation_coding_system. */ |
| 903 | Lisp_Object coding_systems; |
| 904 | Lisp_Object val, *args2; |
| 905 | int i; |
| 906 | #ifdef DOS_NT |
| 907 | char *tempfile; |
| 908 | char *outf = '\0'; |
| 909 | |
| 910 | if ((outf = egetenv ("TMPDIR")) |
| 911 | || (outf = egetenv ("TMP")) |
| 912 | || (outf = egetenv ("TEMP"))) |
| 913 | strcpy (tempfile = alloca (strlen (outf) + 20), outf); |
| 914 | else |
| 915 | { |
| 916 | tempfile = alloca (20); |
| 917 | *tempfile = '\0'; |
| 918 | } |
| 919 | if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1])) |
| 920 | strcat (tempfile, "/"); |
| 921 | if ('/' == DIRECTORY_SEP) |
| 922 | dostounix_filename (tempfile); |
| 923 | else |
| 924 | unixtodos_filename (tempfile); |
| 925 | #ifdef WINDOWSNT |
| 926 | strcat (tempfile, "emXXXXXX"); |
| 927 | #else |
| 928 | strcat (tempfile, "detmp.XXX"); |
| 929 | #endif |
| 930 | #else /* not DOS_NT */ |
| 931 | char *tempfile = (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1); |
| 932 | bcopy (XSTRING (Vtemp_file_name_pattern)->data, tempfile, |
| 933 | STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1); |
| 934 | #endif /* not DOS_NT */ |
| 935 | |
| 936 | coding_systems = Qt; |
| 937 | |
| 938 | mktemp (tempfile); |
| 939 | |
| 940 | filename_string = build_string (tempfile); |
| 941 | GCPRO1 (filename_string); |
| 942 | start = args[0]; |
| 943 | end = args[1]; |
| 944 | /* Decide coding-system of the contents of the temporary file. */ |
| 945 | if (!NILP (Vcoding_system_for_write)) |
| 946 | val = Vcoding_system_for_write; |
| 947 | else if (NILP (current_buffer->enable_multibyte_characters)) |
| 948 | val = Qnil; |
| 949 | else |
| 950 | { |
| 951 | args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2); |
| 952 | args2[0] = Qcall_process_region; |
| 953 | for (i = 0; i < nargs; i++) args2[i + 1] = args[i]; |
| 954 | coding_systems = Ffind_operation_coding_system (nargs + 1, args2); |
| 955 | if (CONSP (coding_systems)) |
| 956 | val = XCDR (coding_systems); |
| 957 | else if (CONSP (Vdefault_process_coding_system)) |
| 958 | val = XCDR (Vdefault_process_coding_system); |
| 959 | else |
| 960 | val = Qnil; |
| 961 | } |
| 962 | |
| 963 | { |
| 964 | int count1 = specpdl_ptr - specpdl; |
| 965 | |
| 966 | specbind (intern ("coding-system-for-write"), val); |
| 967 | Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil); |
| 968 | |
| 969 | unbind_to (count1, Qnil); |
| 970 | } |
| 971 | |
| 972 | /* Note that Fcall_process takes care of binding |
| 973 | coding-system-for-read. */ |
| 974 | |
| 975 | record_unwind_protect (delete_temp_file, filename_string); |
| 976 | |
| 977 | if (nargs > 3 && !NILP (args[3])) |
| 978 | Fdelete_region (start, end); |
| 979 | |
| 980 | if (nargs > 3) |
| 981 | { |
| 982 | args += 2; |
| 983 | nargs -= 2; |
| 984 | } |
| 985 | else |
| 986 | { |
| 987 | args[0] = args[2]; |
| 988 | nargs = 2; |
| 989 | } |
| 990 | args[1] = filename_string; |
| 991 | |
| 992 | RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args))); |
| 993 | } |
| 994 | \f |
| 995 | #ifndef VMS /* VMS version is in vmsproc.c. */ |
| 996 | |
| 997 | static int relocate_fd (); |
| 998 | |
| 999 | /* This is the last thing run in a newly forked inferior |
| 1000 | either synchronous or asynchronous. |
| 1001 | Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2. |
| 1002 | Initialize inferior's priority, pgrp, connected dir and environment. |
| 1003 | then exec another program based on new_argv. |
| 1004 | |
| 1005 | This function may change environ for the superior process. |
| 1006 | Therefore, the superior process must save and restore the value |
| 1007 | of environ around the vfork and the call to this function. |
| 1008 | |
| 1009 | SET_PGRP is nonzero if we should put the subprocess into a separate |
| 1010 | process group. |
| 1011 | |
| 1012 | CURRENT_DIR is an elisp string giving the path of the current |
| 1013 | directory the subprocess should have. Since we can't really signal |
| 1014 | a decent error from within the child, this should be verified as an |
| 1015 | executable directory by the parent. */ |
| 1016 | |
| 1017 | int |
| 1018 | child_setup (in, out, err, new_argv, set_pgrp, current_dir) |
| 1019 | int in, out, err; |
| 1020 | register char **new_argv; |
| 1021 | int set_pgrp; |
| 1022 | Lisp_Object current_dir; |
| 1023 | { |
| 1024 | char **env; |
| 1025 | char *pwd_var; |
| 1026 | #ifdef WINDOWSNT |
| 1027 | int cpid; |
| 1028 | HANDLE handles[3]; |
| 1029 | #endif /* WINDOWSNT */ |
| 1030 | |
| 1031 | int pid = getpid (); |
| 1032 | |
| 1033 | #ifdef SET_EMACS_PRIORITY |
| 1034 | { |
| 1035 | extern int emacs_priority; |
| 1036 | |
| 1037 | if (emacs_priority < 0) |
| 1038 | nice (- emacs_priority); |
| 1039 | } |
| 1040 | #endif |
| 1041 | |
| 1042 | #ifdef subprocesses |
| 1043 | /* Close Emacs's descriptors that this process should not have. */ |
| 1044 | close_process_descs (); |
| 1045 | #endif |
| 1046 | /* DOS_NT isn't in a vfork, so if we are in the middle of load-file, |
| 1047 | we will lose if we call close_load_descs here. */ |
| 1048 | #ifndef DOS_NT |
| 1049 | close_load_descs (); |
| 1050 | #endif |
| 1051 | |
| 1052 | /* Note that use of alloca is always safe here. It's obvious for systems |
| 1053 | that do not have true vfork or that have true (stack) alloca. |
| 1054 | If using vfork and C_ALLOCA it is safe because that changes |
| 1055 | the superior's static variables as if the superior had done alloca |
| 1056 | and will be cleaned up in the usual way. */ |
| 1057 | { |
| 1058 | register char *temp; |
| 1059 | register int i; |
| 1060 | |
| 1061 | i = STRING_BYTES (XSTRING (current_dir)); |
| 1062 | pwd_var = (char *) alloca (i + 6); |
| 1063 | temp = pwd_var + 4; |
| 1064 | bcopy ("PWD=", pwd_var, 4); |
| 1065 | bcopy (XSTRING (current_dir)->data, temp, i); |
| 1066 | if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP; |
| 1067 | temp[i] = 0; |
| 1068 | |
| 1069 | #ifndef DOS_NT |
| 1070 | /* We can't signal an Elisp error here; we're in a vfork. Since |
| 1071 | the callers check the current directory before forking, this |
| 1072 | should only return an error if the directory's permissions |
| 1073 | are changed between the check and this chdir, but we should |
| 1074 | at least check. */ |
| 1075 | if (chdir (temp) < 0) |
| 1076 | _exit (errno); |
| 1077 | #endif |
| 1078 | |
| 1079 | #ifdef DOS_NT |
| 1080 | /* Get past the drive letter, so that d:/ is left alone. */ |
| 1081 | if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2])) |
| 1082 | { |
| 1083 | temp += 2; |
| 1084 | i -= 2; |
| 1085 | } |
| 1086 | #endif |
| 1087 | |
| 1088 | /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */ |
| 1089 | while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1])) |
| 1090 | temp[--i] = 0; |
| 1091 | } |
| 1092 | |
| 1093 | /* Set `env' to a vector of the strings in Vprocess_environment. */ |
| 1094 | { |
| 1095 | register Lisp_Object tem; |
| 1096 | register char **new_env; |
| 1097 | register int new_length; |
| 1098 | |
| 1099 | new_length = 0; |
| 1100 | for (tem = Vprocess_environment; |
| 1101 | CONSP (tem) && STRINGP (XCAR (tem)); |
| 1102 | tem = XCDR (tem)) |
| 1103 | new_length++; |
| 1104 | |
| 1105 | /* new_length + 2 to include PWD and terminating 0. */ |
| 1106 | env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *)); |
| 1107 | |
| 1108 | /* If we have a PWD envvar, pass one down, |
| 1109 | but with corrected value. */ |
| 1110 | if (getenv ("PWD")) |
| 1111 | *new_env++ = pwd_var; |
| 1112 | |
| 1113 | /* Copy the Vprocess_environment strings into new_env. */ |
| 1114 | for (tem = Vprocess_environment; |
| 1115 | CONSP (tem) && STRINGP (XCAR (tem)); |
| 1116 | tem = XCDR (tem)) |
| 1117 | { |
| 1118 | char **ep = env; |
| 1119 | char *string = (char *) XSTRING (XCAR (tem))->data; |
| 1120 | /* See if this string duplicates any string already in the env. |
| 1121 | If so, don't put it in. |
| 1122 | When an env var has multiple definitions, |
| 1123 | we keep the definition that comes first in process-environment. */ |
| 1124 | for (; ep != new_env; ep++) |
| 1125 | { |
| 1126 | char *p = *ep, *q = string; |
| 1127 | while (1) |
| 1128 | { |
| 1129 | if (*q == 0) |
| 1130 | /* The string is malformed; might as well drop it. */ |
| 1131 | goto duplicate; |
| 1132 | if (*q != *p) |
| 1133 | break; |
| 1134 | if (*q == '=') |
| 1135 | goto duplicate; |
| 1136 | p++, q++; |
| 1137 | } |
| 1138 | } |
| 1139 | *new_env++ = string; |
| 1140 | duplicate: ; |
| 1141 | } |
| 1142 | *new_env = 0; |
| 1143 | } |
| 1144 | #ifdef WINDOWSNT |
| 1145 | prepare_standard_handles (in, out, err, handles); |
| 1146 | set_process_dir (XSTRING (current_dir)->data); |
| 1147 | #else /* not WINDOWSNT */ |
| 1148 | /* Make sure that in, out, and err are not actually already in |
| 1149 | descriptors zero, one, or two; this could happen if Emacs is |
| 1150 | started with its standard in, out, or error closed, as might |
| 1151 | happen under X. */ |
| 1152 | { |
| 1153 | int oin = in, oout = out; |
| 1154 | |
| 1155 | /* We have to avoid relocating the same descriptor twice! */ |
| 1156 | |
| 1157 | in = relocate_fd (in, 3); |
| 1158 | |
| 1159 | if (out == oin) |
| 1160 | out = in; |
| 1161 | else |
| 1162 | out = relocate_fd (out, 3); |
| 1163 | |
| 1164 | if (err == oin) |
| 1165 | err = in; |
| 1166 | else if (err == oout) |
| 1167 | err = out; |
| 1168 | else |
| 1169 | err = relocate_fd (err, 3); |
| 1170 | } |
| 1171 | |
| 1172 | #ifndef MSDOS |
| 1173 | emacs_close (0); |
| 1174 | emacs_close (1); |
| 1175 | emacs_close (2); |
| 1176 | |
| 1177 | dup2 (in, 0); |
| 1178 | dup2 (out, 1); |
| 1179 | dup2 (err, 2); |
| 1180 | emacs_close (in); |
| 1181 | emacs_close (out); |
| 1182 | emacs_close (err); |
| 1183 | #endif /* not MSDOS */ |
| 1184 | #endif /* not WINDOWSNT */ |
| 1185 | |
| 1186 | #if defined(USG) && !defined(BSD_PGRPS) |
| 1187 | #ifndef SETPGRP_RELEASES_CTTY |
| 1188 | setpgrp (); /* No arguments but equivalent in this case */ |
| 1189 | #endif |
| 1190 | #else |
| 1191 | setpgrp (pid, pid); |
| 1192 | #endif /* USG */ |
| 1193 | /* setpgrp_of_tty is incorrect here; it uses input_fd. */ |
| 1194 | EMACS_SET_TTY_PGRP (0, &pid); |
| 1195 | |
| 1196 | #ifdef vipc |
| 1197 | something missing here; |
| 1198 | #endif /* vipc */ |
| 1199 | |
| 1200 | #ifdef MSDOS |
| 1201 | pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env); |
| 1202 | if (pid == -1) |
| 1203 | /* An error occurred while trying to run the subprocess. */ |
| 1204 | report_file_error ("Spawning child process", Qnil); |
| 1205 | return pid; |
| 1206 | #else /* not MSDOS */ |
| 1207 | #ifdef WINDOWSNT |
| 1208 | /* Spawn the child. (See ntproc.c:Spawnve). */ |
| 1209 | cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env); |
| 1210 | reset_standard_handles (in, out, err, handles); |
| 1211 | if (cpid == -1) |
| 1212 | /* An error occurred while trying to spawn the process. */ |
| 1213 | report_file_error ("Spawning child process", Qnil); |
| 1214 | return cpid; |
| 1215 | #else /* not WINDOWSNT */ |
| 1216 | /* execvp does not accept an environment arg so the only way |
| 1217 | to pass this environment is to set environ. Our caller |
| 1218 | is responsible for restoring the ambient value of environ. */ |
| 1219 | environ = env; |
| 1220 | execvp (new_argv[0], new_argv); |
| 1221 | |
| 1222 | emacs_write (1, "Can't exec program: ", 20); |
| 1223 | emacs_write (1, new_argv[0], strlen (new_argv[0])); |
| 1224 | emacs_write (1, "\n", 1); |
| 1225 | _exit (1); |
| 1226 | #endif /* not WINDOWSNT */ |
| 1227 | #endif /* not MSDOS */ |
| 1228 | } |
| 1229 | |
| 1230 | /* Move the file descriptor FD so that its number is not less than MINFD. |
| 1231 | If the file descriptor is moved at all, the original is freed. */ |
| 1232 | static int |
| 1233 | relocate_fd (fd, minfd) |
| 1234 | int fd, minfd; |
| 1235 | { |
| 1236 | if (fd >= minfd) |
| 1237 | return fd; |
| 1238 | else |
| 1239 | { |
| 1240 | int new = dup (fd); |
| 1241 | if (new == -1) |
| 1242 | { |
| 1243 | char *message1 = "Error while setting up child: "; |
| 1244 | char *errmessage = strerror (errno); |
| 1245 | char *message2 = "\n"; |
| 1246 | emacs_write (2, message1, strlen (message1)); |
| 1247 | emacs_write (2, errmessage, strlen (errmessage)); |
| 1248 | emacs_write (2, message2, strlen (message2)); |
| 1249 | _exit (1); |
| 1250 | } |
| 1251 | /* Note that we hold the original FD open while we recurse, |
| 1252 | to guarantee we'll get a new FD if we need it. */ |
| 1253 | new = relocate_fd (new, minfd); |
| 1254 | emacs_close (fd); |
| 1255 | return new; |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | static int |
| 1260 | getenv_internal (var, varlen, value, valuelen) |
| 1261 | char *var; |
| 1262 | int varlen; |
| 1263 | char **value; |
| 1264 | int *valuelen; |
| 1265 | { |
| 1266 | Lisp_Object scan; |
| 1267 | |
| 1268 | for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan)) |
| 1269 | { |
| 1270 | Lisp_Object entry; |
| 1271 | |
| 1272 | entry = XCAR (scan); |
| 1273 | if (STRINGP (entry) |
| 1274 | && STRING_BYTES (XSTRING (entry)) > varlen |
| 1275 | && XSTRING (entry)->data[varlen] == '=' |
| 1276 | #ifdef WINDOWSNT |
| 1277 | /* NT environment variables are case insensitive. */ |
| 1278 | && ! strnicmp (XSTRING (entry)->data, var, varlen) |
| 1279 | #else /* not WINDOWSNT */ |
| 1280 | && ! bcmp (XSTRING (entry)->data, var, varlen) |
| 1281 | #endif /* not WINDOWSNT */ |
| 1282 | ) |
| 1283 | { |
| 1284 | *value = (char *) XSTRING (entry)->data + (varlen + 1); |
| 1285 | *valuelen = STRING_BYTES (XSTRING (entry)) - (varlen + 1); |
| 1286 | return 1; |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, 0, |
| 1294 | "Return the value of environment variable VAR, as a string.\n\ |
| 1295 | VAR should be a string. Value is nil if VAR is undefined in the environment.\n\ |
| 1296 | This function consults the variable ``process-environment'' for its value.") |
| 1297 | (var) |
| 1298 | Lisp_Object var; |
| 1299 | { |
| 1300 | char *value; |
| 1301 | int valuelen; |
| 1302 | |
| 1303 | CHECK_STRING (var, 0); |
| 1304 | if (getenv_internal (XSTRING (var)->data, STRING_BYTES (XSTRING (var)), |
| 1305 | &value, &valuelen)) |
| 1306 | return make_string (value, valuelen); |
| 1307 | else |
| 1308 | return Qnil; |
| 1309 | } |
| 1310 | |
| 1311 | /* A version of getenv that consults process_environment, easily |
| 1312 | callable from C. */ |
| 1313 | char * |
| 1314 | egetenv (var) |
| 1315 | char *var; |
| 1316 | { |
| 1317 | char *value; |
| 1318 | int valuelen; |
| 1319 | |
| 1320 | if (getenv_internal (var, strlen (var), &value, &valuelen)) |
| 1321 | return value; |
| 1322 | else |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
| 1326 | #endif /* not VMS */ |
| 1327 | \f |
| 1328 | /* This is run before init_cmdargs. */ |
| 1329 | |
| 1330 | void |
| 1331 | init_callproc_1 () |
| 1332 | { |
| 1333 | char *data_dir = egetenv ("EMACSDATA"); |
| 1334 | char *doc_dir = egetenv ("EMACSDOC"); |
| 1335 | |
| 1336 | Vdata_directory |
| 1337 | = Ffile_name_as_directory (build_string (data_dir ? data_dir |
| 1338 | : PATH_DATA)); |
| 1339 | Vdoc_directory |
| 1340 | = Ffile_name_as_directory (build_string (doc_dir ? doc_dir |
| 1341 | : PATH_DOC)); |
| 1342 | |
| 1343 | /* Check the EMACSPATH environment variable, defaulting to the |
| 1344 | PATH_EXEC path from epaths.h. */ |
| 1345 | Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC); |
| 1346 | Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path)); |
| 1347 | Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path); |
| 1348 | } |
| 1349 | |
| 1350 | /* This is run after init_cmdargs, when Vinstallation_directory is valid. */ |
| 1351 | |
| 1352 | void |
| 1353 | init_callproc () |
| 1354 | { |
| 1355 | char *data_dir = egetenv ("EMACSDATA"); |
| 1356 | |
| 1357 | register char * sh; |
| 1358 | Lisp_Object tempdir; |
| 1359 | |
| 1360 | if (!NILP (Vinstallation_directory)) |
| 1361 | { |
| 1362 | /* Add to the path the lib-src subdir of the installation dir. */ |
| 1363 | Lisp_Object tem; |
| 1364 | tem = Fexpand_file_name (build_string ("lib-src"), |
| 1365 | Vinstallation_directory); |
| 1366 | #ifndef DOS_NT |
| 1367 | /* MSDOS uses wrapped binaries, so don't do this. */ |
| 1368 | if (NILP (Fmember (tem, Vexec_path))) |
| 1369 | Vexec_path = nconc2 (Vexec_path, Fcons (tem, Qnil)); |
| 1370 | |
| 1371 | Vexec_directory = Ffile_name_as_directory (tem); |
| 1372 | #endif /* not DOS_NT */ |
| 1373 | |
| 1374 | /* Maybe use ../etc as well as ../lib-src. */ |
| 1375 | if (data_dir == 0) |
| 1376 | { |
| 1377 | tem = Fexpand_file_name (build_string ("etc"), |
| 1378 | Vinstallation_directory); |
| 1379 | Vdoc_directory = Ffile_name_as_directory (tem); |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | /* Look for the files that should be in etc. We don't use |
| 1384 | Vinstallation_directory, because these files are never installed |
| 1385 | near the executable, and they are never in the build |
| 1386 | directory when that's different from the source directory. |
| 1387 | |
| 1388 | Instead, if these files are not in the nominal place, we try the |
| 1389 | source directory. */ |
| 1390 | if (data_dir == 0) |
| 1391 | { |
| 1392 | Lisp_Object tem, tem1, newdir; |
| 1393 | |
| 1394 | tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory); |
| 1395 | tem1 = Ffile_exists_p (tem); |
| 1396 | if (NILP (tem1)) |
| 1397 | { |
| 1398 | newdir = Fexpand_file_name (build_string ("../etc/"), |
| 1399 | build_string (PATH_DUMPLOADSEARCH)); |
| 1400 | tem = Fexpand_file_name (build_string ("GNU"), newdir); |
| 1401 | tem1 = Ffile_exists_p (tem); |
| 1402 | if (!NILP (tem1)) |
| 1403 | Vdata_directory = newdir; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | #ifndef CANNOT_DUMP |
| 1408 | if (initialized) |
| 1409 | #endif |
| 1410 | { |
| 1411 | tempdir = Fdirectory_file_name (Vexec_directory); |
| 1412 | if (access (XSTRING (tempdir)->data, 0) < 0) |
| 1413 | dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n", |
| 1414 | Vexec_directory); |
| 1415 | } |
| 1416 | |
| 1417 | tempdir = Fdirectory_file_name (Vdata_directory); |
| 1418 | if (access (XSTRING (tempdir)->data, 0) < 0) |
| 1419 | dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n", |
| 1420 | Vdata_directory); |
| 1421 | |
| 1422 | #ifdef VMS |
| 1423 | Vshell_file_name = build_string ("*dcl*"); |
| 1424 | #else |
| 1425 | sh = (char *) getenv ("SHELL"); |
| 1426 | Vshell_file_name = build_string (sh ? sh : "/bin/sh"); |
| 1427 | #endif |
| 1428 | |
| 1429 | #ifdef VMS |
| 1430 | Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX."); |
| 1431 | #else |
| 1432 | if (getenv ("TMPDIR")) |
| 1433 | { |
| 1434 | char *dir = getenv ("TMPDIR"); |
| 1435 | Vtemp_file_name_pattern |
| 1436 | = Fexpand_file_name (build_string ("emacsXXXXXX"), |
| 1437 | build_string (dir)); |
| 1438 | } |
| 1439 | else |
| 1440 | Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX"); |
| 1441 | #endif |
| 1442 | } |
| 1443 | |
| 1444 | void |
| 1445 | set_process_environment () |
| 1446 | { |
| 1447 | register char **envp; |
| 1448 | |
| 1449 | Vprocess_environment = Qnil; |
| 1450 | #ifndef CANNOT_DUMP |
| 1451 | if (initialized) |
| 1452 | #endif |
| 1453 | for (envp = environ; *envp; envp++) |
| 1454 | Vprocess_environment = Fcons (build_string (*envp), |
| 1455 | Vprocess_environment); |
| 1456 | } |
| 1457 | |
| 1458 | void |
| 1459 | syms_of_callproc () |
| 1460 | { |
| 1461 | #ifdef DOS_NT |
| 1462 | Qbuffer_file_type = intern ("buffer-file-type"); |
| 1463 | staticpro (&Qbuffer_file_type); |
| 1464 | #endif /* DOS_NT */ |
| 1465 | |
| 1466 | DEFVAR_LISP ("shell-file-name", &Vshell_file_name, |
| 1467 | "*File name to load inferior shells from.\n\ |
| 1468 | Initialized from the SHELL environment variable."); |
| 1469 | |
| 1470 | DEFVAR_LISP ("exec-path", &Vexec_path, |
| 1471 | "*List of directories to search programs to run in subprocesses.\n\ |
| 1472 | Each element is a string (directory name) or nil (try default directory)."); |
| 1473 | |
| 1474 | DEFVAR_LISP ("exec-directory", &Vexec_directory, |
| 1475 | "Directory for executables for Emacs to invoke.\n\ |
| 1476 | More generally, this includes any architecture-dependent files\n\ |
| 1477 | that are built and installed from the Emacs distribution."); |
| 1478 | |
| 1479 | DEFVAR_LISP ("data-directory", &Vdata_directory, |
| 1480 | "Directory of machine-independent files that come with GNU Emacs.\n\ |
| 1481 | These are files intended for Emacs to use while it runs."); |
| 1482 | |
| 1483 | DEFVAR_LISP ("doc-directory", &Vdoc_directory, |
| 1484 | "Directory containing the DOC file that comes with GNU Emacs.\n\ |
| 1485 | This is usually the same as data-directory."); |
| 1486 | |
| 1487 | DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory, |
| 1488 | "For internal use by the build procedure only.\n\ |
| 1489 | This is the name of the directory in which the build procedure installed\n\ |
| 1490 | Emacs's info files; the default value for Info-default-directory-list\n\ |
| 1491 | includes this."); |
| 1492 | Vconfigure_info_directory = build_string (PATH_INFO); |
| 1493 | |
| 1494 | DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern, |
| 1495 | "Pattern for making names for temporary files.\n\ |
| 1496 | This is used by `call-process-region'."); |
| 1497 | /* This variable is initialized in init_callproc. */ |
| 1498 | |
| 1499 | DEFVAR_LISP ("process-environment", &Vprocess_environment, |
| 1500 | "List of environment variables for subprocesses to inherit.\n\ |
| 1501 | Each element should be a string of the form ENVVARNAME=VALUE.\n\ |
| 1502 | The environment which Emacs inherits is placed in this variable\n\ |
| 1503 | when Emacs starts."); |
| 1504 | |
| 1505 | #ifndef VMS |
| 1506 | defsubr (&Scall_process); |
| 1507 | defsubr (&Sgetenv); |
| 1508 | #endif |
| 1509 | defsubr (&Scall_process_region); |
| 1510 | } |