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