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