(sys_signal): Always set SA_RESTART when
[bpt/emacs.git] / src / sysdep.c
1 /* Interfaces to system-dependent kernel and library entries.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <signal.h>
26 #include <stdio.h>
27 #include <setjmp.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include "lisp.h"
32 /* Including stdlib.h isn't necessarily enough to get srandom
33 declared, e.g. without __USE_XOPEN_EXTENDED with glibc 2. */
34
35 /* The w32 build defines select stuff in w32.h, which is included by
36 sys/select.h (included below). */
37 #ifndef WINDOWSNT
38 #include "sysselect.h"
39 #endif
40
41 #include "blockinput.h"
42
43 #ifdef WINDOWSNT
44 #define read sys_read
45 #define write sys_write
46 #include <windows.h>
47 #ifndef NULL
48 #define NULL 0
49 #endif
50 #endif /* not WINDOWSNT */
51
52 /* Does anyone other than VMS need this? */
53 #ifndef fwrite
54 #define sys_fwrite fwrite
55 #else
56 #undef fwrite
57 #endif
58
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <errno.h>
62
63 #ifdef HAVE_SETPGID
64 #if !defined (USG) || defined (BSD_PGRPS)
65 #undef setpgrp
66 #define setpgrp setpgid
67 #endif
68 #endif
69
70 /* Get SI_SRPC_DOMAIN, if it is available. */
71 #ifdef HAVE_SYS_SYSTEMINFO_H
72 #include <sys/systeminfo.h>
73 #endif
74
75 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
76 #include <dos.h>
77 #include "dosfns.h"
78 #include "msdos.h"
79 #include <sys/param.h>
80
81 #if __DJGPP__ > 1
82 extern int etext;
83 extern unsigned start __asm__ ("start");
84 #endif
85 #endif
86
87 #ifndef USE_CRT_DLL
88 #ifndef errno
89 extern int errno;
90 #endif
91 #endif
92
93 #include <sys/file.h>
94
95 #ifdef HAVE_FCNTL_H
96 #include <fcntl.h>
97 #endif
98
99 #ifndef MSDOS
100 #include <sys/ioctl.h>
101 #endif
102
103 #include "systty.h"
104 #include "syswait.h"
105
106 #if defined (USG)
107 #include <sys/utsname.h>
108 #include <memory.h>
109 #endif /* USG */
110
111 extern int quit_char;
112
113 #include "keyboard.h"
114 #include "frame.h"
115 #include "window.h"
116 #include "termhooks.h"
117 #include "termchar.h"
118 #include "termopts.h"
119 #include "dispextern.h"
120 #include "process.h"
121 #include "cm.h" /* for reset_sys_modes */
122
123 /* For serial_configure and serial_open. */
124 extern Lisp_Object QCport, QCspeed, QCprocess;
125 extern Lisp_Object QCbytesize, QCstopbits, QCparity, Qodd, Qeven;
126 extern Lisp_Object QCflowcontrol, Qhw, Qsw, QCsummary;
127
128 #ifdef WINDOWSNT
129 #include <direct.h>
130 /* In process.h which conflicts with the local copy. */
131 #define _P_WAIT 0
132 int _cdecl _spawnlp (int, const char *, const char *, ...);
133 int _cdecl _getpid (void);
134 extern char *getwd (char *);
135 #endif
136
137 #include "syssignal.h"
138 #include "systime.h"
139 #ifdef HAVE_UTIME_H
140 #include <utime.h>
141 #endif
142
143 #ifndef HAVE_UTIMES
144 #ifndef HAVE_STRUCT_UTIMBUF
145 /* We want to use utime rather than utimes, but we couldn't find the
146 structure declaration. We'll use the traditional one. */
147 struct utimbuf {
148 long actime;
149 long modtime;
150 };
151 #endif
152 #endif
153
154 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
155 #ifndef LPASS8
156 #define LPASS8 0
157 #endif
158
159 static int baud_convert[] =
160 {
161 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
162 1800, 2400, 4800, 9600, 19200, 38400
163 };
164
165 #ifdef HAVE_SPEED_T
166 #include <termios.h>
167 #else
168 #if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
169 #else
170 #if defined (HAVE_TERMIOS_H) && defined (GNU_LINUX)
171 #include <termios.h>
172 #endif
173 #endif
174 #endif
175
176 int emacs_ospeed;
177
178 void croak P_ ((char *)) NO_RETURN;
179
180 /* Temporary used by `sigblock' when defined in terms of signprocmask. */
181
182 SIGMASKTYPE sigprocmask_set;
183
184
185 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
186
187 /* Return the current working directory. Returns NULL on errors.
188 Any other returned value must be freed with free. This is used
189 only when get_current_dir_name is not defined on the system. */
190 char*
191 get_current_dir_name ()
192 {
193 char *buf;
194 char *pwd;
195 struct stat dotstat, pwdstat;
196 /* If PWD is accurate, use it instead of calling getwd. PWD is
197 sometimes a nicer name, and using it may avoid a fatal error if a
198 parent directory is searchable but not readable. */
199 if ((pwd = getenv ("PWD")) != 0
200 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
201 && stat (pwd, &pwdstat) == 0
202 && stat (".", &dotstat) == 0
203 && dotstat.st_ino == pwdstat.st_ino
204 && dotstat.st_dev == pwdstat.st_dev
205 #ifdef MAXPATHLEN
206 && strlen (pwd) < MAXPATHLEN
207 #endif
208 )
209 {
210 buf = (char *) malloc (strlen (pwd) + 1);
211 if (!buf)
212 return NULL;
213 strcpy (buf, pwd);
214 }
215 #ifdef HAVE_GETCWD
216 else
217 {
218 size_t buf_size = 1024;
219 buf = (char *) malloc (buf_size);
220 if (!buf)
221 return NULL;
222 for (;;)
223 {
224 if (getcwd (buf, buf_size) == buf)
225 break;
226 if (errno != ERANGE)
227 {
228 int tmp_errno = errno;
229 free (buf);
230 errno = tmp_errno;
231 return NULL;
232 }
233 buf_size *= 2;
234 buf = (char *) realloc (buf, buf_size);
235 if (!buf)
236 return NULL;
237 }
238 }
239 #else
240 else
241 {
242 /* We need MAXPATHLEN here. */
243 buf = (char *) malloc (MAXPATHLEN + 1);
244 if (!buf)
245 return NULL;
246 if (getwd (buf) == NULL)
247 {
248 int tmp_errno = errno;
249 free (buf);
250 errno = tmp_errno;
251 return NULL;
252 }
253 }
254 #endif
255 return buf;
256 }
257 #endif
258
259 \f
260 /* Discard pending input on all input descriptors. */
261
262 void
263 discard_tty_input ()
264 {
265 #ifndef WINDOWSNT
266 struct emacs_tty buf;
267
268 if (noninteractive)
269 return;
270
271 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
272 while (dos_keyread () != -1)
273 ;
274 #else /* not MSDOS */
275 {
276 struct tty_display_info *tty;
277 for (tty = tty_list; tty; tty = tty->next)
278 {
279 if (tty->input) /* Is the device suspended? */
280 {
281 EMACS_GET_TTY (fileno (tty->input), &buf);
282 EMACS_SET_TTY (fileno (tty->input), &buf, 0);
283 }
284 }
285 }
286 #endif /* not MSDOS */
287 #endif /* not WINDOWSNT */
288 }
289
290 \f
291 #ifdef SIGTSTP
292
293 /* Arrange for character C to be read as the next input from
294 the terminal.
295 XXX What if we have multiple ttys?
296 */
297
298 void
299 stuff_char (char c)
300 {
301 if (! FRAME_TERMCAP_P (SELECTED_FRAME ()))
302 return;
303
304 /* Should perhaps error if in batch mode */
305 #ifdef TIOCSTI
306 ioctl (fileno (CURTTY()->input), TIOCSTI, &c);
307 #else /* no TIOCSTI */
308 error ("Cannot stuff terminal input characters in this version of Unix");
309 #endif /* no TIOCSTI */
310 }
311
312 #endif /* SIGTSTP */
313 \f
314 void
315 init_baud_rate (int fd)
316 {
317 if (noninteractive)
318 emacs_ospeed = 0;
319 else
320 {
321 #ifdef DOS_NT
322 emacs_ospeed = 15;
323 #else /* not DOS_NT */
324 #ifdef HAVE_TERMIOS
325 struct termios sg;
326
327 sg.c_cflag = B9600;
328 tcgetattr (fd, &sg);
329 emacs_ospeed = cfgetospeed (&sg);
330 #else /* not TERMIOS */
331 #ifdef HAVE_TERMIO
332 struct termio sg;
333
334 sg.c_cflag = B9600;
335 #ifdef HAVE_TCATTR
336 tcgetattr (fd, &sg);
337 #else
338 ioctl (fd, TCGETA, &sg);
339 #endif
340 emacs_ospeed = sg.c_cflag & CBAUD;
341 #else /* neither TERMIOS nor TERMIO */
342 struct sgttyb sg;
343
344 sg.sg_ospeed = B9600;
345 if (ioctl (fd, TIOCGETP, &sg) < 0)
346 abort ();
347 emacs_ospeed = sg.sg_ospeed;
348 #endif /* not HAVE_TERMIO */
349 #endif /* not HAVE_TERMIOS */
350 #endif /* not DOS_NT */
351 }
352
353 baud_rate = (emacs_ospeed < sizeof baud_convert / sizeof baud_convert[0]
354 ? baud_convert[emacs_ospeed] : 9600);
355 if (baud_rate == 0)
356 baud_rate = 1200;
357 }
358
359 \f
360 /*ARGSUSED*/
361 void
362 set_exclusive_use (fd)
363 int fd;
364 {
365 #ifdef FIOCLEX
366 ioctl (fd, FIOCLEX, 0);
367 #endif
368 /* Ok to do nothing if this feature does not exist */
369 }
370 \f
371 #ifndef subprocesses
372
373 wait_without_blocking ()
374 {
375 #ifdef BSD_SYSTEM
376 wait3 (0, WNOHANG | WUNTRACED, 0);
377 #else
378 croak ("wait_without_blocking");
379 #endif
380 synch_process_alive = 0;
381 }
382
383 #endif /* not subprocesses */
384
385 int wait_debugging; /* Set nonzero to make following function work under dbx
386 (at least for bsd). */
387
388 SIGTYPE
389 wait_for_termination_signal ()
390 {}
391
392 /* Wait for subprocess with process id `pid' to terminate and
393 make sure it will get eliminated (not remain forever as a zombie) */
394
395 void
396 wait_for_termination (pid)
397 int pid;
398 {
399 while (1)
400 {
401 #ifdef subprocesses
402 #if defined (BSD_SYSTEM) || defined (HPUX)
403 /* Note that kill returns -1 even if the process is just a zombie now.
404 But inevitably a SIGCHLD interrupt should be generated
405 and child_sig will do wait3 and make the process go away. */
406 /* There is some indication that there is a bug involved with
407 termination of subprocesses, perhaps involving a kernel bug too,
408 but no idea what it is. Just as a hunch we signal SIGCHLD to see
409 if that causes the problem to go away or get worse. */
410 sigsetmask (sigmask (SIGCHLD));
411 if (0 > kill (pid, 0))
412 {
413 sigsetmask (SIGEMPTYMASK);
414 kill (getpid (), SIGCHLD);
415 break;
416 }
417 if (wait_debugging)
418 sleep (1);
419 else
420 sigpause (SIGEMPTYMASK);
421 #else /* not BSD_SYSTEM, and not HPUX version >= 6 */
422 #ifdef POSIX_SIGNALS /* would this work for GNU/Linux as well? */
423 sigblock (sigmask (SIGCHLD));
424 errno = 0;
425 if (kill (pid, 0) == -1 && errno == ESRCH)
426 {
427 sigunblock (sigmask (SIGCHLD));
428 break;
429 }
430
431 sigsuspend (&empty_mask);
432 #else /* not POSIX_SIGNALS */
433 #ifdef HAVE_SYSV_SIGPAUSE
434 sighold (SIGCHLD);
435 if (0 > kill (pid, 0))
436 {
437 sigrelse (SIGCHLD);
438 break;
439 }
440 sigpause (SIGCHLD);
441 #else /* not HAVE_SYSV_SIGPAUSE */
442 #ifdef WINDOWSNT
443 wait (0);
444 break;
445 #else /* not WINDOWSNT */
446 if (0 > kill (pid, 0))
447 break;
448 /* Using sleep instead of pause avoids timing error.
449 If the inferior dies just before the sleep,
450 we lose just one second. */
451 sleep (1);
452 #endif /* not WINDOWSNT */
453 #endif /* not HAVE_SYSV_SIGPAUSE */
454 #endif /* not POSIX_SIGNALS */
455 #endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
456 #else /* not subprocesses */
457 #if __DJGPP__ > 1
458 break;
459 #else /* not __DJGPP__ > 1 */
460 if (kill (pid, 0) < 0)
461 break;
462 wait (0);
463 #endif /* not __DJGPP__ > 1*/
464 #endif /* not subprocesses */
465 }
466 }
467
468 #ifdef subprocesses
469
470 /*
471 * flush any pending output
472 * (may flush input as well; it does not matter the way we use it)
473 */
474
475 void
476 flush_pending_output (channel)
477 int channel;
478 {
479 #ifdef HAVE_TERMIOS
480 /* If we try this, we get hit with SIGTTIN, because
481 the child's tty belongs to the child's pgrp. */
482 #else
483 #ifdef TCFLSH
484 ioctl (channel, TCFLSH, 1);
485 #else
486 #ifdef TIOCFLUSH
487 int zero = 0;
488 /* 3rd arg should be ignored
489 but some 4.2 kernels actually want the address of an int
490 and nonzero means something different. */
491 ioctl (channel, TIOCFLUSH, &zero);
492 #endif
493 #endif
494 #endif
495 }
496 \f
497 /* Set up the terminal at the other end of a pseudo-terminal that
498 we will be controlling an inferior through.
499 It should not echo or do line-editing, since that is done
500 in Emacs. No padding needed for insertion into an Emacs buffer. */
501
502 void
503 child_setup_tty (out)
504 int out;
505 {
506 #ifndef DOS_NT
507 struct emacs_tty s;
508
509 EMACS_GET_TTY (out, &s);
510
511 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
512 s.main.c_oflag |= OPOST; /* Enable output postprocessing */
513 s.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL on output */
514 #ifdef NLDLY
515 /* http://lists.gnu.org/archive/html/emacs-devel/2008-05/msg00406.html
516 Some versions of GNU Hurd do not have FFDLY? */
517 #ifdef FFDLY
518 s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
519 /* No output delays */
520 #else
521 s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY);
522 /* No output delays */
523 #endif
524 #endif
525 s.main.c_lflag &= ~ECHO; /* Disable echo */
526 s.main.c_lflag |= ISIG; /* Enable signals */
527 #ifdef IUCLC
528 s.main.c_iflag &= ~IUCLC; /* Disable downcasing on input. */
529 #endif
530 #ifdef ISTRIP
531 s.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
532 #endif
533 #ifdef OLCUC
534 s.main.c_oflag &= ~OLCUC; /* Disable upcasing on output. */
535 #endif
536 s.main.c_oflag &= ~TAB3; /* Disable tab expansion */
537 s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
538 s.main.c_lflag |= ICANON; /* Enable erase/kill and eof processing */
539 s.main.c_cc[VEOF] = 04; /* insure that EOF is Control-D */
540 s.main.c_cc[VERASE] = CDISABLE; /* disable erase processing */
541 s.main.c_cc[VKILL] = CDISABLE; /* disable kill processing */
542
543 #ifdef HPUX
544 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
545 #endif /* HPUX */
546
547 #ifdef SIGNALS_VIA_CHARACTERS
548 /* the QUIT and INTR character are used in process_send_signal
549 so set them here to something useful. */
550 if (s.main.c_cc[VQUIT] == CDISABLE)
551 s.main.c_cc[VQUIT] = '\\'&037; /* Control-\ */
552 if (s.main.c_cc[VINTR] == CDISABLE)
553 s.main.c_cc[VINTR] = 'C'&037; /* Control-C */
554 #endif /* not SIGNALS_VIA_CHARACTERS */
555
556 #ifdef AIX
557 /* AIX enhanced edit loses NULs, so disable it */
558 #ifndef IBMR2AIX
559 s.main.c_line = 0;
560 s.main.c_iflag &= ~ASCEDIT;
561 #endif
562 /* Also, PTY overloads NUL and BREAK.
563 don't ignore break, but don't signal either, so it looks like NUL. */
564 s.main.c_iflag &= ~IGNBRK;
565 s.main.c_iflag &= ~BRKINT;
566 /* rms: Formerly it set s.main.c_cc[VINTR] to 0377 here
567 unconditionally. Then a SIGNALS_VIA_CHARACTERS conditional
568 would force it to 0377. That looks like duplicated code. */
569 #ifndef SIGNALS_VIA_CHARACTERS
570 /* QUIT and INTR work better as signals, so disable character forms */
571 s.main.c_cc[VQUIT] = CDISABLE;
572 s.main.c_cc[VINTR] = CDISABLE;
573 s.main.c_lflag &= ~ISIG;
574 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
575 s.main.c_cc[VEOL] = CDISABLE;
576 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
577 #endif /* AIX */
578
579 #else /* not HAVE_TERMIO */
580
581 s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
582 | CBREAK | TANDEM);
583 s.main.sg_flags |= LPASS8;
584 s.main.sg_erase = 0377;
585 s.main.sg_kill = 0377;
586 s.lmode = LLITOUT | s.lmode; /* Don't strip 8th bit */
587
588 #endif /* not HAVE_TERMIO */
589
590 EMACS_SET_TTY (out, &s, 0);
591
592 #endif /* not DOS_NT */
593 }
594
595 #endif /* subprocesses */
596 \f
597 /* Record a signal code and the handler for it. */
598 struct save_signal
599 {
600 int code;
601 SIGTYPE (*handler) P_ ((int));
602 };
603
604 static void save_signal_handlers P_ ((struct save_signal *));
605 static void restore_signal_handlers P_ ((struct save_signal *));
606
607 /* Suspend the Emacs process; give terminal to its superior. */
608
609 void
610 sys_suspend ()
611 {
612 #if defined (SIGTSTP) && !defined (MSDOS)
613
614 {
615 int pgrp = EMACS_GETPGRP (0);
616 EMACS_KILLPG (pgrp, SIGTSTP);
617 }
618
619 #else /* No SIGTSTP */
620 #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
621 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
622 kill (getpid (), SIGQUIT);
623
624 #else /* No SIGTSTP or USG_JOBCTRL */
625
626 /* On a system where suspending is not implemented,
627 instead fork a subshell and let it talk directly to the terminal
628 while we wait. */
629 sys_subshell ();
630
631 #endif /* no USG_JOBCTRL */
632 #endif /* no SIGTSTP */
633 }
634
635 /* Fork a subshell. */
636
637 void
638 sys_subshell ()
639 {
640 #ifdef DOS_NT /* Demacs 1.1.2 91/10/20 Manabu Higashida */
641 int st;
642 char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS. */
643 #endif
644 int pid;
645 struct save_signal saved_handlers[5];
646 Lisp_Object dir;
647 unsigned char *str = 0;
648 int len;
649
650 saved_handlers[0].code = SIGINT;
651 saved_handlers[1].code = SIGQUIT;
652 saved_handlers[2].code = SIGTERM;
653 #ifdef SIGIO
654 saved_handlers[3].code = SIGIO;
655 saved_handlers[4].code = 0;
656 #else
657 saved_handlers[3].code = 0;
658 #endif
659
660 /* Mentioning current_buffer->buffer would mean including buffer.h,
661 which somehow wedges the hp compiler. So instead... */
662
663 dir = intern ("default-directory");
664 if (NILP (Fboundp (dir)))
665 goto xyzzy;
666 dir = Fsymbol_value (dir);
667 if (!STRINGP (dir))
668 goto xyzzy;
669
670 dir = expand_and_dir_to_file (Funhandled_file_name_directory (dir), Qnil);
671 str = (unsigned char *) alloca (SCHARS (dir) + 2);
672 len = SCHARS (dir);
673 bcopy (SDATA (dir), str, len);
674 if (str[len - 1] != '/') str[len++] = '/';
675 str[len] = 0;
676 xyzzy:
677
678 #ifdef DOS_NT
679 pid = 0;
680 #if __DJGPP__ > 1
681 save_signal_handlers (saved_handlers);
682 synch_process_alive = 1;
683 #endif /* __DJGPP__ > 1 */
684 #else
685 pid = vfork ();
686 if (pid == -1)
687 error ("Can't spawn subshell");
688 #endif
689
690 if (pid == 0)
691 {
692 char *sh = 0;
693
694 #ifdef DOS_NT /* MW, Aug 1993 */
695 getwd (oldwd);
696 if (sh == 0)
697 sh = (char *) egetenv ("SUSPEND"); /* KFS, 1994-12-14 */
698 #endif
699 if (sh == 0)
700 sh = (char *) egetenv ("SHELL");
701 if (sh == 0)
702 sh = "sh";
703
704 /* Use our buffer's default directory for the subshell. */
705 if (str)
706 chdir ((char *) str);
707
708 #ifdef subprocesses
709 close_process_descs (); /* Close Emacs's pipes/ptys */
710 #endif
711
712 #ifdef SET_EMACS_PRIORITY
713 {
714 extern EMACS_INT emacs_priority;
715
716 if (emacs_priority < 0)
717 nice (-emacs_priority);
718 }
719 #endif
720
721 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
722 {
723 char *epwd = getenv ("PWD");
724 char old_pwd[MAXPATHLEN+1+4];
725
726 /* If PWD is set, pass it with corrected value. */
727 if (epwd)
728 {
729 strcpy (old_pwd, epwd);
730 if (str[len - 1] == '/')
731 str[len - 1] = '\0';
732 setenv ("PWD", str, 1);
733 }
734 st = system (sh);
735 chdir (oldwd);
736 if (epwd)
737 putenv (old_pwd); /* restore previous value */
738 }
739 #else /* not MSDOS */
740 #ifdef WINDOWSNT
741 /* Waits for process completion */
742 pid = _spawnlp (_P_WAIT, sh, sh, NULL);
743 chdir (oldwd);
744 if (pid == -1)
745 write (1, "Can't execute subshell", 22);
746 #else /* not WINDOWSNT */
747 execlp (sh, sh, (char *) 0);
748 write (1, "Can't execute subshell", 22);
749 _exit (1);
750 #endif /* not WINDOWSNT */
751 #endif /* not MSDOS */
752 }
753
754 /* Do this now if we did not do it before. */
755 #if !defined (MSDOS) || __DJGPP__ == 1
756 save_signal_handlers (saved_handlers);
757 synch_process_alive = 1;
758 #endif
759
760 #ifndef DOS_NT
761 wait_for_termination (pid);
762 #endif
763 restore_signal_handlers (saved_handlers);
764 synch_process_alive = 0;
765 }
766
767 static void
768 save_signal_handlers (saved_handlers)
769 struct save_signal *saved_handlers;
770 {
771 while (saved_handlers->code)
772 {
773 saved_handlers->handler
774 = (SIGTYPE (*) P_ ((int))) signal (saved_handlers->code, SIG_IGN);
775 saved_handlers++;
776 }
777 }
778
779 static void
780 restore_signal_handlers (saved_handlers)
781 struct save_signal *saved_handlers;
782 {
783 while (saved_handlers->code)
784 {
785 signal (saved_handlers->code, saved_handlers->handler);
786 saved_handlers++;
787 }
788 }
789 \f
790 #ifndef SIGIO
791 /* If SIGIO is broken, don't do anything. */
792 void
793 init_sigio (int fd)
794 {
795 }
796
797 void
798 reset_sigio (int fd)
799 {
800 }
801
802 void
803 request_sigio (void)
804 {
805 }
806
807 void
808 unrequest_sigio (void)
809 {
810 }
811
812 #else
813 #ifdef F_SETFL
814
815 int old_fcntl_flags[MAXDESC];
816
817 void
818 init_sigio (fd)
819 int fd;
820 {
821 #ifdef FASYNC
822 old_fcntl_flags[fd] = fcntl (fd, F_GETFL, 0) & ~FASYNC;
823 fcntl (fd, F_SETFL, old_fcntl_flags[fd] | FASYNC);
824 #endif
825 interrupts_deferred = 0;
826 }
827
828 void
829 reset_sigio (fd)
830 int fd;
831 {
832 #ifdef FASYNC
833 fcntl (fd, F_SETFL, old_fcntl_flags[fd]);
834 #endif
835 }
836
837 #ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
838 /* XXX Uhm, FASYNC is not used anymore here. */
839 /* XXX Yeah, but you need it for SIGIO, don't you? */
840
841 void
842 request_sigio ()
843 {
844 if (noninteractive)
845 return;
846
847 #ifdef SIGWINCH
848 sigunblock (sigmask (SIGWINCH));
849 #endif
850 sigunblock (sigmask (SIGIO));
851
852 interrupts_deferred = 0;
853 }
854
855 void
856 unrequest_sigio (void)
857 {
858 if (noninteractive)
859 return;
860
861 #if 0 /* XXX What's wrong with blocking SIGIO under X? */
862 if (x_display_list)
863 return;
864 #endif
865
866 #ifdef SIGWINCH
867 sigblock (sigmask (SIGWINCH));
868 #endif
869 sigblock (sigmask (SIGIO));
870 interrupts_deferred = 1;
871 }
872
873 #else /* no FASYNC */
874 #ifndef MSDOS
875
876 void
877 request_sigio ()
878 {
879 if (noninteractive || read_socket_hook)
880 return;
881
882 croak ("request_sigio");
883 }
884
885 void
886 unrequest_sigio ()
887 {
888 if (noninteractive || read_socket_hook)
889 return;
890
891 croak ("unrequest_sigio");
892 }
893
894 #endif /* MSDOS */
895 #endif /* FASYNC */
896 #endif /* F_SETFL */
897 #endif /* SIGIO */
898 \f
899 /* Saving and restoring the process group of Emacs's terminal. */
900
901 #ifdef BSD_PGRPS
902
903 /* The process group of which Emacs was a member when it initially
904 started.
905
906 If Emacs was in its own process group (i.e. inherited_pgroup ==
907 getpid ()), then we know we're running under a shell with job
908 control (Emacs would never be run as part of a pipeline).
909 Everything is fine.
910
911 If Emacs was not in its own process group, then we know we're
912 running under a shell (or a caller) that doesn't know how to
913 separate itself from Emacs (like sh). Emacs must be in its own
914 process group in order to receive SIGIO correctly. In this
915 situation, we put ourselves in our own pgroup, forcibly set the
916 tty's pgroup to our pgroup, and make sure to restore and reinstate
917 the tty's pgroup just like any other terminal setting. If
918 inherited_group was not the tty's pgroup, then we'll get a
919 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
920 it goes foreground in the future, which is what should happen.
921
922 This variable is initialized in emacs.c. */
923 int inherited_pgroup;
924
925 /* Split off the foreground process group to Emacs alone. When we are
926 in the foreground, but not started in our own process group,
927 redirect the tty device handle FD to point to our own process
928 group. We need to be in our own process group to receive SIGIO
929 properly. */
930 static void
931 narrow_foreground_group (int fd)
932 {
933 int me = getpid ();
934
935 setpgrp (0, inherited_pgroup);
936 #if 0
937 /* XXX inherited_pgroup should not be zero here, but GTK seems to
938 mess this up. */
939 if (! inherited_pgroup)
940 abort (); /* Should not happen. */
941 #endif
942 if (inherited_pgroup != me)
943 EMACS_SET_TTY_PGRP (fd, &me); /* XXX This only works on the controlling tty. */
944 setpgrp (0, me);
945 }
946
947 /* Set the tty to our original foreground group. */
948 static void
949 widen_foreground_group (int fd)
950 {
951 if (inherited_pgroup != getpid ())
952 EMACS_SET_TTY_PGRP (fd, &inherited_pgroup);
953 setpgrp (0, inherited_pgroup);
954 }
955
956 #endif /* BSD_PGRPS */
957 \f
958 /* Getting and setting emacs_tty structures. */
959
960 /* Set *TC to the parameters associated with the terminal FD.
961 Return zero if all's well, or -1 if we ran into an error we
962 couldn't deal with. */
963 int
964 emacs_get_tty (fd, settings)
965 int fd;
966 struct emacs_tty *settings;
967 {
968 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
969 #ifdef HAVE_TCATTR
970 /* We have those nifty POSIX tcmumbleattr functions. */
971 bzero (&settings->main, sizeof (settings->main));
972 if (tcgetattr (fd, &settings->main) < 0)
973 return -1;
974
975 #else
976 #ifdef HAVE_TERMIO
977 /* The SYSV-style interface? */
978 if (ioctl (fd, TCGETA, &settings->main) < 0)
979 return -1;
980
981 #else
982 #ifndef DOS_NT
983 /* I give up - I hope you have the BSD ioctls. */
984 if (ioctl (fd, TIOCGETP, &settings->main) < 0)
985 return -1;
986 #endif /* not DOS_NT */
987 #endif
988 #endif
989
990 /* Suivant - Do we have to get struct ltchars data? */
991 #ifdef HAVE_LTCHARS
992 if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
993 return -1;
994 #endif
995
996 /* How about a struct tchars and a wordful of lmode bits? */
997 #ifdef HAVE_TCHARS
998 if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
999 || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
1000 return -1;
1001 #endif
1002
1003 /* We have survived the tempest. */
1004 return 0;
1005 }
1006
1007
1008 /* Set the parameters of the tty on FD according to the contents of
1009 *SETTINGS. If FLUSHP is non-zero, we discard input.
1010 Return 0 if all went well, and -1 if anything failed. */
1011
1012 int
1013 emacs_set_tty (fd, settings, flushp)
1014 int fd;
1015 struct emacs_tty *settings;
1016 int flushp;
1017 {
1018 /* Set the primary parameters - baud rate, character size, etcetera. */
1019 #ifdef HAVE_TCATTR
1020 int i;
1021 /* We have those nifty POSIX tcmumbleattr functions.
1022 William J. Smith <wjs@wiis.wang.com> writes:
1023 "POSIX 1003.1 defines tcsetattr to return success if it was
1024 able to perform any of the requested actions, even if some
1025 of the requested actions could not be performed.
1026 We must read settings back to ensure tty setup properly.
1027 AIX requires this to keep tty from hanging occasionally." */
1028 /* This make sure that we don't loop indefinitely in here. */
1029 for (i = 0 ; i < 10 ; i++)
1030 if (tcsetattr (fd, flushp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
1031 {
1032 if (errno == EINTR)
1033 continue;
1034 else
1035 return -1;
1036 }
1037 else
1038 {
1039 struct termios new;
1040
1041 bzero (&new, sizeof (new));
1042 /* Get the current settings, and see if they're what we asked for. */
1043 tcgetattr (fd, &new);
1044 /* We cannot use memcmp on the whole structure here because under
1045 * aix386 the termios structure has some reserved field that may
1046 * not be filled in.
1047 */
1048 if ( new.c_iflag == settings->main.c_iflag
1049 && new.c_oflag == settings->main.c_oflag
1050 && new.c_cflag == settings->main.c_cflag
1051 && new.c_lflag == settings->main.c_lflag
1052 && memcmp (new.c_cc, settings->main.c_cc, NCCS) == 0)
1053 break;
1054 else
1055 continue;
1056 }
1057
1058 #else
1059 #ifdef HAVE_TERMIO
1060 /* The SYSV-style interface? */
1061 if (ioctl (fd, flushp ? TCSETAF : TCSETAW, &settings->main) < 0)
1062 return -1;
1063
1064 #else
1065 #ifndef DOS_NT
1066 /* I give up - I hope you have the BSD ioctls. */
1067 if (ioctl (fd, (flushp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
1068 return -1;
1069 #endif /* not DOS_NT */
1070
1071 #endif
1072 #endif
1073
1074 /* Suivant - Do we have to get struct ltchars data? */
1075 #ifdef HAVE_LTCHARS
1076 if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
1077 return -1;
1078 #endif
1079
1080 /* How about a struct tchars and a wordful of lmode bits? */
1081 #ifdef HAVE_TCHARS
1082 if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
1083 || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
1084 return -1;
1085 #endif
1086
1087 /* We have survived the tempest. */
1088 return 0;
1089 }
1090
1091 \f
1092
1093 #ifdef F_SETOWN
1094 int old_fcntl_owner[MAXDESC];
1095 #endif /* F_SETOWN */
1096
1097 /* This may also be defined in stdio,
1098 but if so, this does no harm,
1099 and using the same name avoids wasting the other one's space. */
1100
1101 #if defined (USG)
1102 unsigned char _sobuf[BUFSIZ+8];
1103 #else
1104 char _sobuf[BUFSIZ];
1105 #endif
1106
1107 #ifdef HAVE_LTCHARS
1108 static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
1109 #endif
1110 #ifdef HAVE_TCHARS
1111 static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
1112 #endif
1113
1114 /* Initialize the terminal mode on all tty devices that are currently
1115 open. */
1116
1117 void
1118 init_all_sys_modes (void)
1119 {
1120 struct tty_display_info *tty;
1121 for (tty = tty_list; tty; tty = tty->next)
1122 init_sys_modes (tty);
1123 }
1124
1125 /* Initialize the terminal mode on the given tty device. */
1126
1127 void
1128 init_sys_modes (tty_out)
1129 struct tty_display_info *tty_out;
1130 {
1131 struct emacs_tty tty;
1132
1133 Vtty_erase_char = Qnil;
1134
1135 if (noninteractive)
1136 return;
1137
1138 if (!tty_out->output)
1139 return; /* The tty is suspended. */
1140
1141 #ifdef BSD_PGRPS
1142 #if 0
1143 /* read_socket_hook is not global anymore. I think doing this
1144 unconditionally will not cause any problems. */
1145 if (! read_socket_hook && EQ (Vinitial_window_system, Qnil))
1146 #endif
1147 narrow_foreground_group (fileno (tty_out->input));
1148 #endif
1149
1150 if (! tty_out->old_tty)
1151 tty_out->old_tty = (struct emacs_tty *) xmalloc (sizeof (struct emacs_tty));
1152
1153 EMACS_GET_TTY (fileno (tty_out->input), tty_out->old_tty);
1154
1155 tty = *tty_out->old_tty;
1156
1157 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
1158 XSETINT (Vtty_erase_char, tty.main.c_cc[VERASE]);
1159
1160 tty.main.c_iflag |= (IGNBRK); /* Ignore break condition */
1161 tty.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
1162 #ifdef INLCR /* I'm just being cautious,
1163 since I can't check how widespread INLCR is--rms. */
1164 tty.main.c_iflag &= ~INLCR; /* Disable map of NL to CR on input */
1165 #endif
1166 #ifdef ISTRIP
1167 tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
1168 #endif
1169 tty.main.c_lflag &= ~ECHO; /* Disable echo */
1170 tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */
1171 #ifdef IEXTEN
1172 tty.main.c_lflag &= ~IEXTEN; /* Disable other editing characters. */
1173 #endif
1174 tty.main.c_lflag |= ISIG; /* Enable signals */
1175 if (tty_out->flow_control)
1176 {
1177 tty.main.c_iflag |= IXON; /* Enable start/stop output control */
1178 #ifdef IXANY
1179 tty.main.c_iflag &= ~IXANY;
1180 #endif /* IXANY */
1181 }
1182 else
1183 tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */
1184 tty.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL
1185 on output */
1186 tty.main.c_oflag &= ~TAB3; /* Disable tab expansion */
1187 #ifdef CS8
1188 if (tty_out->meta_key)
1189 {
1190 tty.main.c_cflag |= CS8; /* allow 8th bit on input */
1191 tty.main.c_cflag &= ~PARENB;/* Don't check parity */
1192 }
1193 #endif
1194 if (tty_out->input == stdin)
1195 {
1196 tty.main.c_cc[VINTR] = quit_char; /* C-g (usually) gives SIGINT */
1197 /* Set up C-g for both SIGQUIT and SIGINT.
1198 We don't know which we will get, but we handle both alike
1199 so which one it really gives us does not matter. */
1200 tty.main.c_cc[VQUIT] = quit_char;
1201 }
1202 else
1203 {
1204 /* We normally don't get interrupt or quit signals from tty
1205 devices other than our controlling terminal; therefore,
1206 we must handle C-g as normal input. Unfortunately, this
1207 means that the interrupt and quit feature must be
1208 disabled on secondary ttys, or we would not even see the
1209 keypress.
1210
1211 Note that even though emacsclient could have special code
1212 to pass SIGINT to Emacs, we should _not_ enable
1213 interrupt/quit keys for emacsclient frames. This means
1214 that we can't break out of loops in C code from a
1215 secondary tty frame, but we can always decide what
1216 display the C-g came from, which is more important from a
1217 usability point of view. (Consider the case when two
1218 people work together using the same Emacs instance.) */
1219 tty.main.c_cc[VINTR] = CDISABLE;
1220 tty.main.c_cc[VQUIT] = CDISABLE;
1221 }
1222 tty.main.c_cc[VMIN] = 1; /* Input should wait for at least 1 char */
1223 tty.main.c_cc[VTIME] = 0; /* no matter how long that takes. */
1224 #ifdef VSWTCH
1225 tty.main.c_cc[VSWTCH] = CDISABLE; /* Turn off shell layering use
1226 of C-z */
1227 #endif /* VSWTCH */
1228
1229 #if defined (__mips__) || defined (HAVE_TCATTR)
1230 #ifdef VSUSP
1231 tty.main.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z. */
1232 #endif /* VSUSP */
1233 #ifdef V_DSUSP
1234 tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y. */
1235 #endif /* V_DSUSP */
1236 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1237 tty.main.c_cc[VDSUSP] = CDISABLE;
1238 #endif /* VDSUSP */
1239 #ifdef VLNEXT
1240 tty.main.c_cc[VLNEXT] = CDISABLE;
1241 #endif /* VLNEXT */
1242 #ifdef VREPRINT
1243 tty.main.c_cc[VREPRINT] = CDISABLE;
1244 #endif /* VREPRINT */
1245 #ifdef VWERASE
1246 tty.main.c_cc[VWERASE] = CDISABLE;
1247 #endif /* VWERASE */
1248 #ifdef VDISCARD
1249 tty.main.c_cc[VDISCARD] = CDISABLE;
1250 #endif /* VDISCARD */
1251
1252 if (tty_out->flow_control)
1253 {
1254 #ifdef VSTART
1255 tty.main.c_cc[VSTART] = '\021';
1256 #endif /* VSTART */
1257 #ifdef VSTOP
1258 tty.main.c_cc[VSTOP] = '\023';
1259 #endif /* VSTOP */
1260 }
1261 else
1262 {
1263 #ifdef VSTART
1264 tty.main.c_cc[VSTART] = CDISABLE;
1265 #endif /* VSTART */
1266 #ifdef VSTOP
1267 tty.main.c_cc[VSTOP] = CDISABLE;
1268 #endif /* VSTOP */
1269 }
1270 #endif /* mips or HAVE_TCATTR */
1271
1272 #ifdef AIX
1273 #ifndef IBMR2AIX
1274 /* AIX enhanced edit loses NULs, so disable it. */
1275 tty.main.c_line = 0;
1276 tty.main.c_iflag &= ~ASCEDIT;
1277 #else
1278 tty.main.c_cc[VSTRT] = CDISABLE;
1279 tty.main.c_cc[VSTOP] = CDISABLE;
1280 tty.main.c_cc[VSUSP] = CDISABLE;
1281 tty.main.c_cc[VDSUSP] = CDISABLE;
1282 #endif /* IBMR2AIX */
1283 if (tty_out->flow_control)
1284 {
1285 #ifdef VSTART
1286 tty.main.c_cc[VSTART] = '\021';
1287 #endif /* VSTART */
1288 #ifdef VSTOP
1289 tty.main.c_cc[VSTOP] = '\023';
1290 #endif /* VSTOP */
1291 }
1292 /* Also, PTY overloads NUL and BREAK.
1293 don't ignore break, but don't signal either, so it looks like NUL.
1294 This really serves a purpose only if running in an XTERM window
1295 or via TELNET or the like, but does no harm elsewhere. */
1296 tty.main.c_iflag &= ~IGNBRK;
1297 tty.main.c_iflag &= ~BRKINT;
1298 #endif
1299 #else /* if not HAVE_TERMIO */
1300 #ifndef DOS_NT
1301 XSETINT (Vtty_erase_char, tty.main.sg_erase);
1302 tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
1303 if (meta_key)
1304 tty.main.sg_flags |= ANYP;
1305 tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
1306 #endif /* not DOS_NT */
1307 #endif /* not HAVE_TERMIO */
1308
1309 /* If going to use CBREAK mode, we must request C-g to interrupt
1310 and turn off start and stop chars, etc. If not going to use
1311 CBREAK mode, do this anyway so as to turn off local flow
1312 control for user coming over network on 4.2; in this case,
1313 only t_stopc and t_startc really matter. */
1314 #ifndef HAVE_TERMIO
1315 #ifdef HAVE_TCHARS
1316 /* Note: if not using CBREAK mode, it makes no difference how we
1317 set this */
1318 tty.tchars = new_tchars;
1319 tty.tchars.t_intrc = quit_char;
1320 if (tty_out->flow_control)
1321 {
1322 tty.tchars.t_startc = '\021';
1323 tty.tchars.t_stopc = '\023';
1324 }
1325
1326 tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | tty_out->old_tty.lmode;
1327
1328 #endif /* HAVE_TCHARS */
1329 #endif /* not HAVE_TERMIO */
1330
1331 #ifdef HAVE_LTCHARS
1332 tty.ltchars = new_ltchars;
1333 #endif /* HAVE_LTCHARS */
1334 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
1335 if (!tty_out->term_initted)
1336 internal_terminal_init ();
1337 dos_ttraw (tty_out);
1338 #endif
1339
1340 EMACS_SET_TTY (fileno (tty_out->input), &tty, 0);
1341
1342 /* This code added to insure that, if flow-control is not to be used,
1343 we have an unlocked terminal at the start. */
1344
1345 #ifdef TCXONC
1346 if (!tty_out->flow_control) ioctl (fileno (tty_out->input), TCXONC, 1);
1347 #endif
1348 #ifdef TIOCSTART
1349 if (!tty_out->flow_control) ioctl (fileno (tty_out->input), TIOCSTART, 0);
1350 #endif
1351
1352 #if defined (HAVE_TERMIOS) || defined (HPUX)
1353 #ifdef TCOON
1354 if (!tty_out->flow_control) tcflow (fileno (tty_out->input), TCOON);
1355 #endif
1356 #endif
1357
1358 #ifdef F_SETFL
1359 #ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
1360 if (interrupt_input)
1361 {
1362 old_fcntl_owner[fileno (tty_out->input)] =
1363 fcntl (fileno (tty_out->input), F_GETOWN, 0);
1364 fcntl (fileno (tty_out->input), F_SETOWN, getpid ());
1365 init_sigio (fileno (tty_out->input));
1366 #ifdef HAVE_GPM
1367 if (gpm_tty == tty_out)
1368 {
1369 /* Arrange for mouse events to give us SIGIO signals. */
1370 fcntl (gpm_fd, F_SETOWN, getpid ());
1371 fcntl (gpm_fd, F_SETFL, fcntl (gpm_fd, F_GETFL, 0) | O_NONBLOCK);
1372 init_sigio (gpm_fd);
1373 }
1374 #endif /* HAVE_GPM */
1375 }
1376 #endif /* F_GETOWN */
1377 #endif /* F_SETFL */
1378
1379 #ifdef _IOFBF
1380 /* This symbol is defined on recent USG systems.
1381 Someone says without this call USG won't really buffer the file
1382 even with a call to setbuf. */
1383 setvbuf (tty_out->output, (char *) _sobuf, _IOFBF, sizeof _sobuf);
1384 #else
1385 setbuf (tty_out->output, (char *) _sobuf);
1386 #endif
1387
1388 if (tty_out->terminal->set_terminal_modes_hook)
1389 tty_out->terminal->set_terminal_modes_hook (tty_out->terminal);
1390
1391 if (!tty_out->term_initted)
1392 {
1393 Lisp_Object tail, frame;
1394 FOR_EACH_FRAME (tail, frame)
1395 {
1396 /* XXX This needs to be revised. */
1397 if (FRAME_TERMCAP_P (XFRAME (frame))
1398 && FRAME_TTY (XFRAME (frame)) == tty_out)
1399 init_frame_faces (XFRAME (frame));
1400 }
1401 }
1402
1403 if (tty_out->term_initted && no_redraw_on_reenter)
1404 {
1405 /* XXX This seems wrong on multi-tty. */
1406 if (display_completed)
1407 direct_output_forward_char (0);
1408 }
1409 else
1410 {
1411 Lisp_Object tail, frame;
1412 frame_garbaged = 1;
1413 FOR_EACH_FRAME (tail, frame)
1414 {
1415 if ((FRAME_TERMCAP_P (XFRAME (frame))
1416 || FRAME_MSDOS_P (XFRAME (frame)))
1417 && FRAME_TTY (XFRAME (frame)) == tty_out)
1418 FRAME_GARBAGED_P (XFRAME (frame)) = 1;
1419 }
1420 }
1421
1422 tty_out->term_initted = 1;
1423 }
1424
1425 /* Return nonzero if safe to use tabs in output.
1426 At the time this is called, init_sys_modes has not been done yet. */
1427
1428 int
1429 tabs_safe_p (int fd)
1430 {
1431 struct emacs_tty etty;
1432
1433 EMACS_GET_TTY (fd, &etty);
1434 return EMACS_TTY_TABS_OK (&etty);
1435 }
1436 \f
1437 /* Get terminal size from system.
1438 Store number of lines into *HEIGHTP and width into *WIDTHP.
1439 We store 0 if there's no valid information. */
1440
1441 void
1442 get_tty_size (int fd, int *widthp, int *heightp)
1443 {
1444
1445 #ifdef TIOCGWINSZ
1446
1447 /* BSD-style. */
1448 struct winsize size;
1449
1450 if (ioctl (fd, TIOCGWINSZ, &size) == -1)
1451 *widthp = *heightp = 0;
1452 else
1453 {
1454 *widthp = size.ws_col;
1455 *heightp = size.ws_row;
1456 }
1457
1458 #else
1459 #ifdef TIOCGSIZE
1460
1461 /* SunOS - style. */
1462 struct ttysize size;
1463
1464 if (ioctl (fd, TIOCGSIZE, &size) == -1)
1465 *widthp = *heightp = 0;
1466 else
1467 {
1468 *widthp = size.ts_cols;
1469 *heightp = size.ts_lines;
1470 }
1471
1472 #else
1473 #ifdef MSDOS
1474 *widthp = ScreenCols ();
1475 *heightp = ScreenRows ();
1476 #else /* system doesn't know size */
1477 *widthp = 0;
1478 *heightp = 0;
1479 #endif
1480 #endif /* not SunOS-style */
1481 #endif /* not BSD-style */
1482 }
1483
1484 /* Set the logical window size associated with descriptor FD
1485 to HEIGHT and WIDTH. This is used mainly with ptys. */
1486
1487 int
1488 set_window_size (fd, height, width)
1489 int fd, height, width;
1490 {
1491 #ifdef TIOCSWINSZ
1492
1493 /* BSD-style. */
1494 struct winsize size;
1495 size.ws_row = height;
1496 size.ws_col = width;
1497
1498 if (ioctl (fd, TIOCSWINSZ, &size) == -1)
1499 return 0; /* error */
1500 else
1501 return 1;
1502
1503 #else
1504 #ifdef TIOCSSIZE
1505
1506 /* SunOS - style. */
1507 struct ttysize size;
1508 size.ts_lines = height;
1509 size.ts_cols = width;
1510
1511 if (ioctl (fd, TIOCGSIZE, &size) == -1)
1512 return 0;
1513 else
1514 return 1;
1515 #else
1516 return -1;
1517 #endif /* not SunOS-style */
1518 #endif /* not BSD-style */
1519 }
1520
1521 \f
1522
1523 /* Prepare all terminal devices for exiting Emacs. */
1524
1525 void
1526 reset_all_sys_modes (void)
1527 {
1528 struct tty_display_info *tty;
1529 for (tty = tty_list; tty; tty = tty->next)
1530 reset_sys_modes (tty);
1531 }
1532
1533 /* Prepare the terminal for closing it; move the cursor to the
1534 bottom of the frame, turn off interrupt-driven I/O, etc. */
1535
1536 void
1537 reset_sys_modes (tty_out)
1538 struct tty_display_info *tty_out;
1539 {
1540 if (noninteractive)
1541 {
1542 fflush (stdout);
1543 return;
1544 }
1545 if (!tty_out->term_initted)
1546 return;
1547
1548 if (!tty_out->output)
1549 return; /* The tty is suspended. */
1550
1551 /* Go to and clear the last line of the terminal. */
1552
1553 cmgoto (tty_out, FrameRows (tty_out) - 1, 0);
1554
1555 /* Code adapted from tty_clear_end_of_line. */
1556 if (tty_out->TS_clr_line)
1557 {
1558 emacs_tputs (tty_out, tty_out->TS_clr_line, 1, cmputc);
1559 }
1560 else
1561 { /* have to do it the hard way */
1562 int i;
1563 tty_turn_off_insert (tty_out);
1564
1565 for (i = curX (tty_out); i < FrameCols (tty_out) - 1; i++)
1566 {
1567 fputc (' ', tty_out->output);
1568 }
1569 }
1570
1571 cmgoto (tty_out, FrameRows (tty_out) - 1, 0);
1572 fflush (tty_out->output);
1573
1574 if (tty_out->terminal->reset_terminal_modes_hook)
1575 tty_out->terminal->reset_terminal_modes_hook (tty_out->terminal);
1576
1577 #ifdef BSD_SYSTEM
1578 /* Avoid possible loss of output when changing terminal modes. */
1579 fsync (fileno (tty_out->output));
1580 #endif
1581
1582 #ifdef F_SETFL
1583 #ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
1584 if (interrupt_input)
1585 {
1586 reset_sigio (fileno (tty_out->input));
1587 fcntl (fileno (tty_out->input), F_SETOWN,
1588 old_fcntl_owner[fileno (tty_out->input)]);
1589 }
1590 #endif /* F_SETOWN */
1591 #ifdef O_NDELAY
1592 fcntl (fileno (tty_out->input), F_SETFL,
1593 fcntl (fileno (tty_out->input), F_GETFL, 0) & ~O_NDELAY);
1594 #endif
1595 #endif /* F_SETFL */
1596
1597 if (tty_out->old_tty)
1598 while (EMACS_SET_TTY (fileno (tty_out->input),
1599 tty_out->old_tty, 0) < 0 && errno == EINTR)
1600 ;
1601
1602 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1603 dos_ttcooked ();
1604 #endif
1605
1606 #ifdef BSD_PGRPS
1607 widen_foreground_group (fileno (tty_out->input));
1608 #endif
1609 }
1610 \f
1611 #ifdef HAVE_PTYS
1612
1613 /* Set up the proper status flags for use of a pty. */
1614
1615 void
1616 setup_pty (fd)
1617 int fd;
1618 {
1619 /* I'm told that TOICREMOTE does not mean control chars
1620 "can't be sent" but rather that they don't have
1621 input-editing or signaling effects.
1622 That should be good, because we have other ways
1623 to do those things in Emacs.
1624 However, telnet mode seems not to work on 4.2.
1625 So TIOCREMOTE is turned off now. */
1626
1627 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1628 will hang. In particular, the "timeout" feature (which
1629 causes a read to return if there is no data available)
1630 does this. Also it is known that telnet mode will hang
1631 in such a way that Emacs must be stopped (perhaps this
1632 is the same problem).
1633
1634 If TIOCREMOTE is turned off, then there is a bug in
1635 hp-ux which sometimes loses data. Apparently the
1636 code which blocks the master process when the internal
1637 buffer fills up does not work. Other than this,
1638 though, everything else seems to work fine.
1639
1640 Since the latter lossage is more benign, we may as well
1641 lose that way. -- cph */
1642 #ifdef FIONBIO
1643 #if defined(UNIX98_PTYS)
1644 {
1645 int on = 1;
1646 ioctl (fd, FIONBIO, &on);
1647 }
1648 #endif
1649 #endif
1650 }
1651 #endif /* HAVE_PTYS */
1652 \f
1653 #if !defined(CANNOT_DUMP) || !defined(SYSTEM_MALLOC)
1654 /* Some systems that cannot dump also cannot implement these. */
1655
1656 /*
1657 * Return the address of the start of the text segment prior to
1658 * doing an unexec. After unexec the return value is undefined.
1659 * See crt0.c for further explanation and _start.
1660 *
1661 */
1662
1663 #if !(defined (__NetBSD__) && defined (__ELF__))
1664 #ifndef HAVE_TEXT_START
1665 char *
1666 start_of_text ()
1667 {
1668 #ifdef TEXT_START
1669 return ((char *) TEXT_START);
1670 #else
1671 extern int _start ();
1672 return ((char *) _start);
1673 #endif /* TEXT_START */
1674 }
1675 #endif /* not HAVE_TEXT_START */
1676 #endif
1677
1678 /*
1679 * Return the address of the start of the data segment prior to
1680 * doing an unexec. After unexec the return value is undefined.
1681 * See crt0.c for further information and definition of data_start.
1682 *
1683 * Apparently, on BSD systems this is etext at startup. On
1684 * USG systems (swapping) this is highly mmu dependent and
1685 * is also dependent on whether or not the program is running
1686 * with shared text. Generally there is a (possibly large)
1687 * gap between end of text and start of data with shared text.
1688 *
1689 * On Uniplus+ systems with shared text, data starts at a
1690 * fixed address. Each port (from a given oem) is generally
1691 * different, and the specific value of the start of data can
1692 * be obtained via the UniPlus+ specific "uvar" system call,
1693 * however the method outlined in crt0.c seems to be more portable.
1694 *
1695 * Probably what will have to happen when a USG unexec is available,
1696 * at least on UniPlus, is temacs will have to be made unshared so
1697 * that text and data are contiguous. Then once loadup is complete,
1698 * unexec will produce a shared executable where the data can be
1699 * at the normal shared text boundary and the startofdata variable
1700 * will be patched by unexec to the correct value.
1701 *
1702 */
1703
1704 #ifndef start_of_data
1705 char *
1706 start_of_data ()
1707 {
1708 #ifdef DATA_START
1709 return ((char *) DATA_START);
1710 #else
1711 #ifdef ORDINARY_LINK
1712 /*
1713 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
1714 * data_start isn't defined. We take the address of environ, which
1715 * is known to live at or near the start of the system crt0.c, and
1716 * we don't sweat the handful of bytes that might lose.
1717 */
1718 extern char **environ;
1719
1720 return ((char *) &environ);
1721 #else
1722 extern int data_start;
1723 return ((char *) &data_start);
1724 #endif /* ORDINARY_LINK */
1725 #endif /* DATA_START */
1726 }
1727 #endif /* start_of_data */
1728 #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
1729 \f
1730 /* init_system_name sets up the string for the Lisp function
1731 system-name to return. */
1732
1733 extern Lisp_Object Vsystem_name;
1734
1735 #ifdef HAVE_SOCKETS
1736 #include <sys/socket.h>
1737 #include <netdb.h>
1738 #endif /* HAVE_SOCKETS */
1739
1740 #ifdef TRY_AGAIN
1741 #ifndef HAVE_H_ERRNO
1742 extern int h_errno;
1743 #endif
1744 #endif /* TRY_AGAIN */
1745
1746 void
1747 init_system_name ()
1748 {
1749 #ifndef HAVE_GETHOSTNAME
1750 struct utsname uts;
1751 uname (&uts);
1752 Vsystem_name = build_string (uts.nodename);
1753 #else /* HAVE_GETHOSTNAME */
1754 unsigned int hostname_size = 256;
1755 char *hostname = (char *) alloca (hostname_size);
1756
1757 /* Try to get the host name; if the buffer is too short, try
1758 again. Apparently, the only indication gethostname gives of
1759 whether the buffer was large enough is the presence or absence
1760 of a '\0' in the string. Eech. */
1761 for (;;)
1762 {
1763 gethostname (hostname, hostname_size - 1);
1764 hostname[hostname_size - 1] = '\0';
1765
1766 /* Was the buffer large enough for the '\0'? */
1767 if (strlen (hostname) < hostname_size - 1)
1768 break;
1769
1770 hostname_size <<= 1;
1771 hostname = (char *) alloca (hostname_size);
1772 }
1773 #ifdef HAVE_SOCKETS
1774 /* Turn the hostname into the official, fully-qualified hostname.
1775 Don't do this if we're going to dump; this can confuse system
1776 libraries on some machines and make the dumped emacs core dump. */
1777 #ifndef CANNOT_DUMP
1778 if (initialized)
1779 #endif /* not CANNOT_DUMP */
1780 if (! index (hostname, '.'))
1781 {
1782 int count;
1783 #ifdef HAVE_GETADDRINFO
1784 struct addrinfo *res;
1785 struct addrinfo hints;
1786 int ret;
1787
1788 memset (&hints, 0, sizeof(hints));
1789 hints.ai_socktype = SOCK_STREAM;
1790 hints.ai_flags = AI_CANONNAME;
1791
1792 for (count = 0;; count++)
1793 {
1794 if ((ret = getaddrinfo (hostname, NULL, &hints, &res)) == 0
1795 || ret != EAI_AGAIN)
1796 break;
1797
1798 if (count >= 5)
1799 break;
1800 Fsleep_for (make_number (1), Qnil);
1801 }
1802
1803 if (ret == 0)
1804 {
1805 struct addrinfo *it = res;
1806 while (it)
1807 {
1808 char *fqdn = it->ai_canonname;
1809 if (fqdn && index (fqdn, '.')
1810 && strcmp (fqdn, "localhost.localdomain") != 0)
1811 break;
1812 it = it->ai_next;
1813 }
1814 if (it)
1815 {
1816 hostname = alloca (strlen (it->ai_canonname) + 1);
1817 strcpy (hostname, it->ai_canonname);
1818 }
1819 freeaddrinfo (res);
1820 }
1821 #else /* !HAVE_GETADDRINFO */
1822 struct hostent *hp;
1823 for (count = 0;; count++)
1824 {
1825
1826 #ifdef TRY_AGAIN
1827 h_errno = 0;
1828 #endif
1829 hp = gethostbyname (hostname);
1830 #ifdef TRY_AGAIN
1831 if (! (hp == 0 && h_errno == TRY_AGAIN))
1832 #endif
1833
1834 break;
1835
1836 if (count >= 5)
1837 break;
1838 Fsleep_for (make_number (1), Qnil);
1839 }
1840
1841 if (hp)
1842 {
1843 char *fqdn = (char *) hp->h_name;
1844
1845 if (!index (fqdn, '.'))
1846 {
1847 /* We still don't have a fully qualified domain name.
1848 Try to find one in the list of alternate names */
1849 char **alias = hp->h_aliases;
1850 while (*alias
1851 && (!index (*alias, '.')
1852 || !strcmp (*alias, "localhost.localdomain")))
1853 alias++;
1854 if (*alias)
1855 fqdn = *alias;
1856 }
1857 hostname = fqdn;
1858 }
1859 #endif /* !HAVE_GETADDRINFO */
1860 }
1861 #endif /* HAVE_SOCKETS */
1862 Vsystem_name = build_string (hostname);
1863 #endif /* HAVE_GETHOSTNAME */
1864 {
1865 unsigned char *p;
1866 for (p = SDATA (Vsystem_name); *p; p++)
1867 if (*p == ' ' || *p == '\t')
1868 *p = '-';
1869 }
1870 }
1871 \f
1872 #ifndef MSDOS
1873 #if !defined (HAVE_SELECT)
1874
1875 #include "sysselect.h"
1876 #undef select
1877
1878 #if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
1879 /* Cause explanatory error message at compile time,
1880 since the select emulation is not good enough for X. */
1881 int *x = &x_windows_lose_if_no_select_system_call;
1882 #endif
1883
1884 /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
1885 * Only checks read descriptors.
1886 */
1887 /* How long to wait between checking fds in select */
1888 #define SELECT_PAUSE 1
1889 int select_alarmed;
1890
1891 /* For longjmp'ing back to read_input_waiting. */
1892
1893 jmp_buf read_alarm_throw;
1894
1895 /* Nonzero if the alarm signal should throw back to read_input_waiting.
1896 The read_socket_hook function sets this to 1 while it is waiting. */
1897
1898 int read_alarm_should_throw;
1899
1900 SIGTYPE
1901 select_alarm ()
1902 {
1903 select_alarmed = 1;
1904 signal (SIGALRM, SIG_IGN);
1905 SIGNAL_THREAD_CHECK (SIGALRM);
1906 if (read_alarm_should_throw)
1907 longjmp (read_alarm_throw, 1);
1908 }
1909
1910 #ifndef WINDOWSNT
1911 /* Only rfds are checked. */
1912 int
1913 sys_select (nfds, rfds, wfds, efds, timeout)
1914 int nfds;
1915 SELECT_TYPE *rfds, *wfds, *efds;
1916 EMACS_TIME *timeout;
1917 {
1918 /* XXX This needs to be updated for multi-tty support. Is there
1919 anybody who needs to emulate select these days? */
1920 int ravail = 0;
1921 SELECT_TYPE orfds;
1922 int timeoutval;
1923 int *local_timeout;
1924 extern int proc_buffered_char[];
1925 #ifndef subprocesses
1926 int process_tick = 0, update_tick = 0;
1927 #else
1928 extern int process_tick, update_tick;
1929 #endif
1930 unsigned char buf;
1931
1932 #if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
1933 /* If we're using X, then the native select will work; we only need the
1934 emulation for non-X usage. */
1935 if (!NILP (Vinitial_window_system))
1936 return select (nfds, rfds, wfds, efds, timeout);
1937 #endif
1938 timeoutval = timeout ? EMACS_SECS (*timeout) : 100000;
1939 local_timeout = &timeoutval;
1940 FD_ZERO (&orfds);
1941 if (rfds)
1942 {
1943 orfds = *rfds;
1944 FD_ZERO (rfds);
1945 }
1946 if (wfds)
1947 FD_ZERO (wfds);
1948 if (efds)
1949 FD_ZERO (efds);
1950
1951 /* If we are looking only for the terminal, with no timeout,
1952 just read it and wait -- that's more efficient. */
1953 if (*local_timeout == 100000 && process_tick == update_tick
1954 && FD_ISSET (0, &orfds))
1955 {
1956 int fd;
1957 for (fd = 1; fd < nfds; ++fd)
1958 if (FD_ISSET (fd, &orfds))
1959 goto hardway;
1960 if (! detect_input_pending ())
1961 read_input_waiting ();
1962 FD_SET (0, rfds);
1963 return 1;
1964 }
1965
1966 hardway:
1967 /* Once a second, till the timer expires, check all the flagged read
1968 * descriptors to see if any input is available. If there is some then
1969 * set the corresponding bit in the return copy of rfds.
1970 */
1971 while (1)
1972 {
1973 register int to_check, fd;
1974
1975 if (rfds)
1976 {
1977 for (to_check = nfds, fd = 0; --to_check >= 0; fd++)
1978 {
1979 if (FD_ISSET (fd, &orfds))
1980 {
1981 int avail = 0, status = 0;
1982
1983 if (fd == 0)
1984 avail = detect_input_pending (); /* Special keyboard handler */
1985 else
1986 {
1987 #ifdef FIONREAD
1988 status = ioctl (fd, FIONREAD, &avail);
1989 #else /* no FIONREAD */
1990 /* Hoping it will return -1 if nothing available
1991 or 0 if all 0 chars requested are read. */
1992 if (proc_buffered_char[fd] >= 0)
1993 avail = 1;
1994 else
1995 {
1996 avail = read (fd, &buf, 1);
1997 if (avail > 0)
1998 proc_buffered_char[fd] = buf;
1999 }
2000 #endif /* no FIONREAD */
2001 }
2002 if (status >= 0 && avail > 0)
2003 {
2004 FD_SET (fd, rfds);
2005 ravail++;
2006 }
2007 }
2008 }
2009 }
2010 if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
2011 break;
2012
2013 turn_on_atimers (0);
2014 signal (SIGALRM, select_alarm);
2015 select_alarmed = 0;
2016 alarm (SELECT_PAUSE);
2017
2018 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2019 while (select_alarmed == 0 && *local_timeout != 0
2020 && process_tick == update_tick)
2021 {
2022 /* If we are interested in terminal input,
2023 wait by reading the terminal.
2024 That makes instant wakeup for terminal input at least. */
2025 if (FD_ISSET (0, &orfds))
2026 {
2027 read_input_waiting ();
2028 if (detect_input_pending ())
2029 select_alarmed = 1;
2030 }
2031 else
2032 pause ();
2033 }
2034 (*local_timeout) -= SELECT_PAUSE;
2035
2036 /* Reset the old alarm if there was one. */
2037 turn_on_atimers (1);
2038
2039 if (*local_timeout == 0) /* Stop on timer being cleared */
2040 break;
2041 }
2042 return ravail;
2043 }
2044 #endif /* not WINDOWSNT */
2045
2046 /* Read keyboard input into the standard buffer,
2047 waiting for at least one character. */
2048
2049 void
2050 read_input_waiting ()
2051 {
2052 /* XXX This needs to be updated for multi-tty support. Is there
2053 anybody who needs to emulate select these days? */
2054 int nread, i;
2055 extern int quit_char;
2056
2057 if (read_socket_hook)
2058 {
2059 struct input_event hold_quit;
2060
2061 EVENT_INIT (hold_quit);
2062 hold_quit.kind = NO_EVENT;
2063
2064 read_alarm_should_throw = 0;
2065 if (! setjmp (read_alarm_throw))
2066 nread = (*read_socket_hook) (0, 1, &hold_quit);
2067 else
2068 nread = -1;
2069
2070 if (hold_quit.kind != NO_EVENT)
2071 kbd_buffer_store_event (&hold_quit);
2072 }
2073 else
2074 {
2075 struct input_event e;
2076 char buf[3];
2077 nread = read (fileno (stdin), buf, 1);
2078 EVENT_INIT (e);
2079
2080 /* Scan the chars for C-g and store them in kbd_buffer. */
2081 e.kind = ASCII_KEYSTROKE_EVENT;
2082 e.frame_or_window = selected_frame;
2083 e.modifiers = 0;
2084 for (i = 0; i < nread; i++)
2085 {
2086 /* Convert chars > 0177 to meta events if desired.
2087 We do this under the same conditions that read_avail_input does. */
2088 if (read_socket_hook == 0)
2089 {
2090 /* If the user says she has a meta key, then believe her. */
2091 if (meta_key == 1 && (buf[i] & 0x80))
2092 e.modifiers = meta_modifier;
2093 if (meta_key != 2)
2094 buf[i] &= ~0x80;
2095 }
2096
2097 XSETINT (e.code, buf[i]);
2098 kbd_buffer_store_event (&e);
2099 /* Don't look at input that follows a C-g too closely.
2100 This reduces lossage due to autorepeat on C-g. */
2101 if (buf[i] == quit_char)
2102 break;
2103 }
2104 }
2105 }
2106
2107 #if !defined (HAVE_SELECT)
2108 #define select sys_select
2109 #endif
2110
2111 #endif /* not HAVE_SELECT */
2112 #endif /* not MSDOS */
2113 \f
2114 /* POSIX signals support - DJB */
2115 /* Anyone with POSIX signals should have ANSI C declarations */
2116
2117 #ifdef POSIX_SIGNALS
2118
2119 sigset_t empty_mask, full_mask;
2120
2121 signal_handler_t
2122 sys_signal (int signal_number, signal_handler_t action)
2123 {
2124 struct sigaction new_action, old_action;
2125 sigemptyset (&new_action.sa_mask);
2126 new_action.sa_handler = action;
2127 new_action.sa_flags = 0;
2128 #if defined (SA_RESTART)
2129 /* Emacs mostly works better with restartable system services. If this
2130 flag exists, we probably want to turn it on here.
2131 However, on some systems this resets the timeout of `select'
2132 which means that `select' never finishes if it keeps getting signals.
2133 BROKEN_SA_RESTART is defined on those systems. */
2134 /* It's not clear why the comment above says "mostly works better". --Stef
2135 When SYNC_INPUT is set, we don't want SA_RESTART because we need to poll
2136 for pending input so we need long-running syscalls to be interrupted
2137 after a signal that sets the interrupt_input_pending flag. */
2138 # if defined (BROKEN_SA_RESTART) || defined(SYNC_INPUT)
2139 if (noninteractive)
2140 # endif
2141 new_action.sa_flags = SA_RESTART;
2142 #endif
2143 sigaction (signal_number, &new_action, &old_action);
2144 return (old_action.sa_handler);
2145 }
2146
2147 #ifndef __GNUC__
2148 /* If we're compiling with GCC, we don't need this function, since it
2149 can be written as a macro. */
2150 sigset_t
2151 sys_sigmask (int sig)
2152 {
2153 sigset_t mask;
2154 sigemptyset (&mask);
2155 sigaddset (&mask, sig);
2156 return mask;
2157 }
2158 #endif
2159
2160 /* I'd like to have these guys return pointers to the mask storage in here,
2161 but there'd be trouble if the code was saving multiple masks. I'll be
2162 safe and pass the structure. It normally won't be more than 2 bytes
2163 anyhow. - DJB */
2164
2165 sigset_t
2166 sys_sigblock (sigset_t new_mask)
2167 {
2168 sigset_t old_mask;
2169 sigprocmask (SIG_BLOCK, &new_mask, &old_mask);
2170 return (old_mask);
2171 }
2172
2173 sigset_t
2174 sys_sigunblock (sigset_t new_mask)
2175 {
2176 sigset_t old_mask;
2177 sigprocmask (SIG_UNBLOCK, &new_mask, &old_mask);
2178 return (old_mask);
2179 }
2180
2181 sigset_t
2182 sys_sigsetmask (sigset_t new_mask)
2183 {
2184 sigset_t old_mask;
2185 sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
2186 return (old_mask);
2187 }
2188
2189 #endif /* POSIX_SIGNALS */
2190 \f
2191 #if !defined HAVE_STRSIGNAL && !HAVE_DECL_SYS_SIGLIST
2192 static char *my_sys_siglist[NSIG];
2193 # ifdef sys_siglist
2194 # undef sys_siglist
2195 # endif
2196 # define sys_siglist my_sys_siglist
2197 #endif
2198
2199 void
2200 init_signals ()
2201 {
2202 #ifdef POSIX_SIGNALS
2203 sigemptyset (&empty_mask);
2204 sigfillset (&full_mask);
2205 #endif
2206
2207 #if !defined HAVE_STRSIGNAL && !HAVE_DECL_SYS_SIGLIST
2208 if (! initialized)
2209 {
2210 # ifdef SIGABRT
2211 sys_siglist[SIGABRT] = "Aborted";
2212 # endif
2213 # ifdef SIGAIO
2214 sys_siglist[SIGAIO] = "LAN I/O interrupt";
2215 # endif
2216 # ifdef SIGALRM
2217 sys_siglist[SIGALRM] = "Alarm clock";
2218 # endif
2219 # ifdef SIGBUS
2220 sys_siglist[SIGBUS] = "Bus error";
2221 # endif
2222 # ifdef SIGCLD
2223 sys_siglist[SIGCLD] = "Child status changed";
2224 # endif
2225 # ifdef SIGCHLD
2226 sys_siglist[SIGCHLD] = "Child status changed";
2227 # endif
2228 # ifdef SIGCONT
2229 sys_siglist[SIGCONT] = "Continued";
2230 # endif
2231 # ifdef SIGDANGER
2232 sys_siglist[SIGDANGER] = "Swap space dangerously low";
2233 # endif
2234 # ifdef SIGDGNOTIFY
2235 sys_siglist[SIGDGNOTIFY] = "Notification message in queue";
2236 # endif
2237 # ifdef SIGEMT
2238 sys_siglist[SIGEMT] = "Emulation trap";
2239 # endif
2240 # ifdef SIGFPE
2241 sys_siglist[SIGFPE] = "Arithmetic exception";
2242 # endif
2243 # ifdef SIGFREEZE
2244 sys_siglist[SIGFREEZE] = "SIGFREEZE";
2245 # endif
2246 # ifdef SIGGRANT
2247 sys_siglist[SIGGRANT] = "Monitor mode granted";
2248 # endif
2249 # ifdef SIGHUP
2250 sys_siglist[SIGHUP] = "Hangup";
2251 # endif
2252 # ifdef SIGILL
2253 sys_siglist[SIGILL] = "Illegal instruction";
2254 # endif
2255 # ifdef SIGINT
2256 sys_siglist[SIGINT] = "Interrupt";
2257 # endif
2258 # ifdef SIGIO
2259 sys_siglist[SIGIO] = "I/O possible";
2260 # endif
2261 # ifdef SIGIOINT
2262 sys_siglist[SIGIOINT] = "I/O intervention required";
2263 # endif
2264 # ifdef SIGIOT
2265 sys_siglist[SIGIOT] = "IOT trap";
2266 # endif
2267 # ifdef SIGKILL
2268 sys_siglist[SIGKILL] = "Killed";
2269 # endif
2270 # ifdef SIGLOST
2271 sys_siglist[SIGLOST] = "Resource lost";
2272 # endif
2273 # ifdef SIGLWP
2274 sys_siglist[SIGLWP] = "SIGLWP";
2275 # endif
2276 # ifdef SIGMSG
2277 sys_siglist[SIGMSG] = "Monitor mode data available";
2278 # endif
2279 # ifdef SIGPHONE
2280 sys_siglist[SIGWIND] = "SIGPHONE";
2281 # endif
2282 # ifdef SIGPIPE
2283 sys_siglist[SIGPIPE] = "Broken pipe";
2284 # endif
2285 # ifdef SIGPOLL
2286 sys_siglist[SIGPOLL] = "Pollable event occurred";
2287 # endif
2288 # ifdef SIGPROF
2289 sys_siglist[SIGPROF] = "Profiling timer expired";
2290 # endif
2291 # ifdef SIGPTY
2292 sys_siglist[SIGPTY] = "PTY I/O interrupt";
2293 # endif
2294 # ifdef SIGPWR
2295 sys_siglist[SIGPWR] = "Power-fail restart";
2296 # endif
2297 # ifdef SIGQUIT
2298 sys_siglist[SIGQUIT] = "Quit";
2299 # endif
2300 # ifdef SIGRETRACT
2301 sys_siglist[SIGRETRACT] = "Need to relinguish monitor mode";
2302 # endif
2303 # ifdef SIGSAK
2304 sys_siglist[SIGSAK] = "Secure attention";
2305 # endif
2306 # ifdef SIGSEGV
2307 sys_siglist[SIGSEGV] = "Segmentation violation";
2308 # endif
2309 # ifdef SIGSOUND
2310 sys_siglist[SIGSOUND] = "Sound completed";
2311 # endif
2312 # ifdef SIGSTOP
2313 sys_siglist[SIGSTOP] = "Stopped (signal)";
2314 # endif
2315 # ifdef SIGSTP
2316 sys_siglist[SIGSTP] = "Stopped (user)";
2317 # endif
2318 # ifdef SIGSYS
2319 sys_siglist[SIGSYS] = "Bad argument to system call";
2320 # endif
2321 # ifdef SIGTERM
2322 sys_siglist[SIGTERM] = "Terminated";
2323 # endif
2324 # ifdef SIGTHAW
2325 sys_siglist[SIGTHAW] = "SIGTHAW";
2326 # endif
2327 # ifdef SIGTRAP
2328 sys_siglist[SIGTRAP] = "Trace/breakpoint trap";
2329 # endif
2330 # ifdef SIGTSTP
2331 sys_siglist[SIGTSTP] = "Stopped (user)";
2332 # endif
2333 # ifdef SIGTTIN
2334 sys_siglist[SIGTTIN] = "Stopped (tty input)";
2335 # endif
2336 # ifdef SIGTTOU
2337 sys_siglist[SIGTTOU] = "Stopped (tty output)";
2338 # endif
2339 # ifdef SIGURG
2340 sys_siglist[SIGURG] = "Urgent I/O condition";
2341 # endif
2342 # ifdef SIGUSR1
2343 sys_siglist[SIGUSR1] = "User defined signal 1";
2344 # endif
2345 # ifdef SIGUSR2
2346 sys_siglist[SIGUSR2] = "User defined signal 2";
2347 # endif
2348 # ifdef SIGVTALRM
2349 sys_siglist[SIGVTALRM] = "Virtual timer expired";
2350 # endif
2351 # ifdef SIGWAITING
2352 sys_siglist[SIGWAITING] = "Process's LWPs are blocked";
2353 # endif
2354 # ifdef SIGWINCH
2355 sys_siglist[SIGWINCH] = "Window size changed";
2356 # endif
2357 # ifdef SIGWIND
2358 sys_siglist[SIGWIND] = "SIGWIND";
2359 # endif
2360 # ifdef SIGXCPU
2361 sys_siglist[SIGXCPU] = "CPU time limit exceeded";
2362 # endif
2363 # ifdef SIGXFSZ
2364 sys_siglist[SIGXFSZ] = "File size limit exceeded";
2365 # endif
2366 }
2367 #endif /* !defined HAVE_STRSIGNAL && !defined HAVE_DECL_SYS_SIGLIST */
2368 }
2369 \f
2370 #ifndef HAVE_RANDOM
2371 #ifdef random
2372 #define HAVE_RANDOM
2373 #endif
2374 #endif
2375
2376 /* Figure out how many bits the system's random number generator uses.
2377 `random' and `lrand48' are assumed to return 31 usable bits.
2378 BSD `rand' returns a 31 bit value but the low order bits are unusable;
2379 so we'll shift it and treat it like the 15-bit USG `rand'. */
2380
2381 #ifndef RAND_BITS
2382 # ifdef HAVE_RANDOM
2383 # define RAND_BITS 31
2384 # else /* !HAVE_RANDOM */
2385 # ifdef HAVE_LRAND48
2386 # define RAND_BITS 31
2387 # define random lrand48
2388 # else /* !HAVE_LRAND48 */
2389 # define RAND_BITS 15
2390 # if RAND_MAX == 32767
2391 # define random rand
2392 # else /* RAND_MAX != 32767 */
2393 # if RAND_MAX == 2147483647
2394 # define random() (rand () >> 16)
2395 # else /* RAND_MAX != 2147483647 */
2396 # ifdef USG
2397 # define random rand
2398 # else
2399 # define random() (rand () >> 16)
2400 # endif /* !USG */
2401 # endif /* RAND_MAX != 2147483647 */
2402 # endif /* RAND_MAX != 32767 */
2403 # endif /* !HAVE_LRAND48 */
2404 # endif /* !HAVE_RANDOM */
2405 #endif /* !RAND_BITS */
2406
2407 void
2408 seed_random (arg)
2409 long arg;
2410 {
2411 #ifdef HAVE_RANDOM
2412 srandom ((unsigned int)arg);
2413 #else
2414 # ifdef HAVE_LRAND48
2415 srand48 (arg);
2416 # else
2417 srand ((unsigned int)arg);
2418 # endif
2419 #endif
2420 }
2421
2422 /*
2423 * Build a full Emacs-sized word out of whatever we've got.
2424 * This suffices even for a 64-bit architecture with a 15-bit rand.
2425 */
2426 long
2427 get_random ()
2428 {
2429 long val = random ();
2430 #if VALBITS > RAND_BITS
2431 val = (val << RAND_BITS) ^ random ();
2432 #if VALBITS > 2*RAND_BITS
2433 val = (val << RAND_BITS) ^ random ();
2434 #if VALBITS > 3*RAND_BITS
2435 val = (val << RAND_BITS) ^ random ();
2436 #if VALBITS > 4*RAND_BITS
2437 val = (val << RAND_BITS) ^ random ();
2438 #endif /* need at least 5 */
2439 #endif /* need at least 4 */
2440 #endif /* need at least 3 */
2441 #endif /* need at least 2 */
2442 return val & ((1L << VALBITS) - 1);
2443 }
2444
2445 #ifndef HAVE_STRERROR
2446 #ifndef WINDOWSNT
2447 char *
2448 strerror (errnum)
2449 int errnum;
2450 {
2451 extern char *sys_errlist[];
2452 extern int sys_nerr;
2453
2454 if (errnum >= 0 && errnum < sys_nerr)
2455 return sys_errlist[errnum];
2456 return (char *) "Unknown error";
2457 }
2458 #endif /* not WINDOWSNT */
2459 #endif /* ! HAVE_STRERROR */
2460 \f
2461 int
2462 emacs_open (path, oflag, mode)
2463 const char *path;
2464 int oflag, mode;
2465 {
2466 register int rtnval;
2467
2468 while ((rtnval = open (path, oflag, mode)) == -1
2469 && (errno == EINTR))
2470 QUIT;
2471 return (rtnval);
2472 }
2473
2474 int
2475 emacs_close (fd)
2476 int fd;
2477 {
2478 int did_retry = 0;
2479 register int rtnval;
2480
2481 while ((rtnval = close (fd)) == -1
2482 && (errno == EINTR))
2483 did_retry = 1;
2484
2485 /* If close is interrupted SunOS 4.1 may or may not have closed the
2486 file descriptor. If it did the second close will fail with
2487 errno = EBADF. That means we have succeeded. */
2488 if (rtnval == -1 && did_retry && errno == EBADF)
2489 return 0;
2490
2491 return rtnval;
2492 }
2493
2494 int
2495 emacs_read (fildes, buf, nbyte)
2496 int fildes;
2497 char *buf;
2498 unsigned int nbyte;
2499 {
2500 register int rtnval;
2501
2502 while ((rtnval = read (fildes, buf, nbyte)) == -1
2503 && (errno == EINTR))
2504 QUIT;
2505 return (rtnval);
2506 }
2507
2508 int
2509 emacs_write (fildes, buf, nbyte)
2510 int fildes;
2511 const char *buf;
2512 unsigned int nbyte;
2513 {
2514 register int rtnval, bytes_written;
2515
2516 bytes_written = 0;
2517
2518 while (nbyte > 0)
2519 {
2520 rtnval = write (fildes, buf, nbyte);
2521
2522 if (rtnval == -1)
2523 {
2524 if (errno == EINTR)
2525 {
2526 #ifdef SYNC_INPUT
2527 /* I originally used `QUIT' but that might causes files to
2528 be truncated if you hit C-g in the middle of it. --Stef */
2529 if (interrupt_input_pending)
2530 handle_async_input ();
2531 if (pending_atimers)
2532 do_pending_atimers ();
2533 #endif
2534 continue;
2535 }
2536 else
2537 return (bytes_written ? bytes_written : -1);
2538 }
2539
2540 buf += rtnval;
2541 nbyte -= rtnval;
2542 bytes_written += rtnval;
2543 }
2544 return (bytes_written);
2545 }
2546 \f
2547 #ifdef USG
2548 /*
2549 * All of the following are for USG.
2550 *
2551 * On USG systems the system calls are INTERRUPTIBLE by signals
2552 * that the user program has elected to catch. Thus the system call
2553 * must be retried in these cases. To handle this without massive
2554 * changes in the source code, we remap the standard system call names
2555 * to names for our own functions in sysdep.c that do the system call
2556 * with retries. Actually, for portability reasons, it is good
2557 * programming practice, as this example shows, to limit all actual
2558 * system calls to a single occurrence in the source. Sure, this
2559 * adds an extra level of function call overhead but it is almost
2560 * always negligible. Fred Fish, Unisoft Systems Inc.
2561 */
2562
2563 /*
2564 * Warning, this function may not duplicate 4.2 action properly
2565 * under error conditions.
2566 */
2567
2568 #ifndef MAXPATHLEN
2569 /* In 4.1, param.h fails to define this. */
2570 #define MAXPATHLEN 1024
2571 #endif
2572
2573 #ifndef HAVE_GETWD
2574
2575 char *
2576 getwd (pathname)
2577 char *pathname;
2578 {
2579 char *npath, *spath;
2580 extern char *getcwd ();
2581
2582 BLOCK_INPUT; /* getcwd uses malloc */
2583 spath = npath = getcwd ((char *) 0, MAXPATHLEN);
2584 if (spath == 0)
2585 {
2586 UNBLOCK_INPUT;
2587 return spath;
2588 }
2589 /* On Altos 3068, getcwd can return @hostname/dir, so discard
2590 up to first slash. Should be harmless on other systems. */
2591 while (*npath && *npath != '/')
2592 npath++;
2593 strcpy (pathname, npath);
2594 free (spath); /* getcwd uses malloc */
2595 UNBLOCK_INPUT;
2596 return pathname;
2597 }
2598
2599 #endif /* HAVE_GETWD */
2600
2601 /*
2602 * Emulate rename using unlink/link. Note that this is
2603 * only partially correct. Also, doesn't enforce restriction
2604 * that files be of same type (regular->regular, dir->dir, etc).
2605 */
2606
2607 #ifndef HAVE_RENAME
2608
2609 rename (from, to)
2610 const char *from;
2611 const char *to;
2612 {
2613 if (access (from, 0) == 0)
2614 {
2615 unlink (to);
2616 if (link (from, to) == 0)
2617 if (unlink (from) == 0)
2618 return (0);
2619 }
2620 return (-1);
2621 }
2622
2623 #endif
2624
2625
2626 #if defined(HPUX) && !defined(HAVE_PERROR)
2627
2628 /* HPUX curses library references perror, but as far as we know
2629 it won't be called. Anyway this definition will do for now. */
2630
2631 perror ()
2632 {
2633 }
2634 #endif /* HPUX and not HAVE_PERROR */
2635
2636 #ifndef HAVE_DUP2
2637
2638 /*
2639 * Emulate BSD dup2. First close newd if it already exists.
2640 * Then, attempt to dup oldd. If not successful, call dup2 recursively
2641 * until we are, then close the unsuccessful ones.
2642 */
2643
2644 dup2 (oldd, newd)
2645 int oldd;
2646 int newd;
2647 {
2648 register int fd, ret;
2649
2650 emacs_close (newd);
2651
2652 #ifdef F_DUPFD
2653 return fcntl (oldd, F_DUPFD, newd);
2654 #else
2655 fd = dup (old);
2656 if (fd == -1)
2657 return -1;
2658 if (fd == new)
2659 return new;
2660 ret = dup2 (old,new);
2661 emacs_close (fd);
2662 return ret;
2663 #endif
2664 }
2665
2666 #endif /* not HAVE_DUP2 */
2667
2668 /*
2669 * Gettimeofday. Simulate as much as possible. Only accurate
2670 * to nearest second. Emacs doesn't use tzp so ignore it for now.
2671 * Only needed when subprocesses are defined.
2672 */
2673
2674 #ifdef subprocesses
2675 #ifndef HAVE_GETTIMEOFDAY
2676 #ifdef HAVE_TIMEVAL
2677
2678 /* ARGSUSED */
2679 int
2680 gettimeofday (tp, tzp)
2681 struct timeval *tp;
2682 struct timezone *tzp;
2683 {
2684 extern long time ();
2685
2686 tp->tv_sec = time ((long *)0);
2687 tp->tv_usec = 0;
2688 if (tzp != 0)
2689 tzp->tz_minuteswest = -1;
2690 return 0;
2691 }
2692
2693 #endif
2694 #endif
2695 #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL */
2696
2697 /*
2698 * This function will go away as soon as all the stubs fixed. (fnf)
2699 */
2700
2701 void
2702 croak (badfunc)
2703 char *badfunc;
2704 {
2705 printf ("%s not yet implemented\r\n", badfunc);
2706 reset_all_sys_modes ();
2707 exit (1);
2708 }
2709
2710 #endif /* USG */
2711 \f
2712 /* Directory routines for systems that don't have them. */
2713
2714 #ifdef SYSV_SYSTEM_DIR
2715
2716 #include <dirent.h>
2717
2718 #if !defined (HAVE_CLOSEDIR)
2719
2720 int
2721 closedir (DIR *dirp /* stream from opendir */)
2722 {
2723 int rtnval;
2724
2725 rtnval = emacs_close (dirp->dd_fd);
2726
2727 /* Some systems (like Solaris) allocate the buffer and the DIR all
2728 in one block. Why in the world are we freeing this ourselves
2729 anyway? */
2730 #if ! defined (SOLARIS2)
2731 xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
2732 #endif
2733 xfree ((char *) dirp);
2734
2735 return rtnval;
2736 }
2737 #endif /* not HAVE_CLOSEDIR */
2738 #endif /* SYSV_SYSTEM_DIR */
2739
2740 \f
2741 int
2742 set_file_times (filename, atime, mtime)
2743 const char *filename;
2744 EMACS_TIME atime, mtime;
2745 {
2746 #ifdef HAVE_UTIMES
2747 struct timeval tv[2];
2748 tv[0] = atime;
2749 tv[1] = mtime;
2750 return utimes (filename, tv);
2751 #else /* not HAVE_UTIMES */
2752 struct utimbuf utb;
2753 utb.actime = EMACS_SECS (atime);
2754 utb.modtime = EMACS_SECS (mtime);
2755 return utime (filename, &utb);
2756 #endif /* not HAVE_UTIMES */
2757 }
2758 \f
2759 /* mkdir and rmdir functions, for systems which don't have them. */
2760
2761 #ifndef HAVE_MKDIR
2762 /*
2763 * Written by Robert Rother, Mariah Corporation, August 1985.
2764 *
2765 * If you want it, it's yours. All I ask in return is that if you
2766 * figure out how to do this in a Bourne Shell script you send me
2767 * a copy.
2768 * sdcsvax!rmr or rmr@uscd
2769 *
2770 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
2771 * subroutine. 11Mar86; hoptoad!gnu
2772 *
2773 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
2774 * subroutine didn't return EEXIST. It does now.
2775 */
2776
2777 /*
2778 * Make a directory.
2779 */
2780 #ifdef MKDIR_PROTOTYPE
2781 MKDIR_PROTOTYPE
2782 #else
2783 int
2784 mkdir (dpath, dmode)
2785 char *dpath;
2786 int dmode;
2787 #endif
2788 {
2789 int cpid, status, fd;
2790 struct stat statbuf;
2791
2792 if (stat (dpath, &statbuf) == 0)
2793 {
2794 errno = EEXIST; /* Stat worked, so it already exists */
2795 return -1;
2796 }
2797
2798 /* If stat fails for a reason other than non-existence, return error */
2799 if (errno != ENOENT)
2800 return -1;
2801
2802 synch_process_alive = 1;
2803 switch (cpid = fork ())
2804 {
2805
2806 case -1: /* Error in fork */
2807 return (-1); /* Errno is set already */
2808
2809 case 0: /* Child process */
2810 /*
2811 * Cheap hack to set mode of new directory. Since this
2812 * child process is going away anyway, we zap its umask.
2813 * FIXME, this won't suffice to set SUID, SGID, etc. on this
2814 * directory. Does anybody care?
2815 */
2816 status = umask (0); /* Get current umask */
2817 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */
2818 fd = emacs_open ("/dev/null", O_RDWR, 0);
2819 if (fd >= 0)
2820 {
2821 dup2 (fd, 0);
2822 dup2 (fd, 1);
2823 dup2 (fd, 2);
2824 }
2825 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
2826 _exit (-1); /* Can't exec /bin/mkdir */
2827
2828 default: /* Parent process */
2829 wait_for_termination (cpid);
2830 }
2831
2832 if (synch_process_death != 0 || synch_process_retcode != 0
2833 || synch_process_termsig != 0)
2834 {
2835 errno = EIO; /* We don't know why, but */
2836 return -1; /* /bin/mkdir failed */
2837 }
2838
2839 return 0;
2840 }
2841 #endif /* not HAVE_MKDIR */
2842
2843 #ifndef HAVE_RMDIR
2844 int
2845 rmdir (dpath)
2846 char *dpath;
2847 {
2848 int cpid, status, fd;
2849 struct stat statbuf;
2850
2851 if (stat (dpath, &statbuf) != 0)
2852 {
2853 /* Stat just set errno. We don't have to */
2854 return -1;
2855 }
2856
2857 synch_process_alive = 1;
2858 switch (cpid = fork ())
2859 {
2860
2861 case -1: /* Error in fork */
2862 return (-1); /* Errno is set already */
2863
2864 case 0: /* Child process */
2865 fd = emacs_open ("/dev/null", O_RDWR, 0);
2866 if (fd >= 0)
2867 {
2868 dup2 (fd, 0);
2869 dup2 (fd, 1);
2870 dup2 (fd, 2);
2871 }
2872 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
2873 _exit (-1); /* Can't exec /bin/rmdir */
2874
2875 default: /* Parent process */
2876 wait_for_termination (cpid);
2877 }
2878
2879 if (synch_process_death != 0 || synch_process_retcode != 0
2880 || synch_process_termsig != 0)
2881 {
2882 errno = EIO; /* We don't know why, but */
2883 return -1; /* /bin/rmdir failed */
2884 }
2885
2886 return 0;
2887 }
2888 #endif /* !HAVE_RMDIR */
2889
2890 \f
2891 #ifndef BSTRING
2892
2893 #ifndef bzero
2894
2895 void
2896 bzero (b, length)
2897 register char *b;
2898 register int length;
2899 {
2900 while (length-- > 0)
2901 *b++ = 0;
2902 }
2903
2904 #endif /* no bzero */
2905 #endif /* BSTRING */
2906
2907 #if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
2908 #undef bcopy
2909
2910 /* Saying `void' requires a declaration, above, where bcopy is used
2911 and that declaration causes pain for systems where bcopy is a macro. */
2912 bcopy (b1, b2, length)
2913 register char *b1;
2914 register char *b2;
2915 register int length;
2916 {
2917 while (length-- > 0)
2918 *b2++ = *b1++;
2919 }
2920 #endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
2921
2922 #ifndef BSTRING
2923 #ifndef bcmp
2924 int
2925 bcmp (b1, b2, length) /* This could be a macro! */
2926 register char *b1;
2927 register char *b2;
2928 register int length;
2929 {
2930 while (length-- > 0)
2931 if (*b1++ != *b2++)
2932 return 1;
2933
2934 return 0;
2935 }
2936 #endif /* no bcmp */
2937 #endif /* not BSTRING */
2938 \f
2939 #ifndef HAVE_STRSIGNAL
2940 char *
2941 strsignal (code)
2942 int code;
2943 {
2944 char *signame = 0;
2945
2946 if (0 <= code && code < NSIG)
2947 {
2948 /* Cast to suppress warning if the table has const char *. */
2949 signame = (char *) sys_siglist[code];
2950 }
2951
2952 return signame;
2953 }
2954 #endif /* HAVE_STRSIGNAL */
2955 \f
2956 #ifdef HAVE_TERMIOS
2957 /* For make-serial-process */
2958 int serial_open (char *port)
2959 {
2960 int fd = -1;
2961
2962 fd = emacs_open ((char*) port,
2963 O_RDWR
2964 #ifdef O_NONBLOCK
2965 | O_NONBLOCK
2966 #else
2967 | O_NDELAY
2968 #endif
2969 #ifdef O_NOCTTY
2970 | O_NOCTTY
2971 #endif
2972 , 0);
2973 if (fd < 0)
2974 {
2975 error ("Could not open %s: %s",
2976 port, emacs_strerror (errno));
2977 }
2978 #ifdef TIOCEXCL
2979 ioctl (fd, TIOCEXCL, (char *) 0);
2980 #endif
2981
2982 return fd;
2983 }
2984 #endif /* TERMIOS */
2985
2986 #ifdef HAVE_TERMIOS
2987
2988 #if !defined (HAVE_CFMAKERAW)
2989 /* Workaround for targets which are missing cfmakeraw. */
2990 /* Pasted from man page. */
2991 static void cfmakeraw (struct termios *termios_p)
2992 {
2993 termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
2994 termios_p->c_oflag &= ~OPOST;
2995 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
2996 termios_p->c_cflag &= ~(CSIZE|PARENB);
2997 termios_p->c_cflag |= CS8;
2998 }
2999 #endif /* !defined (HAVE_CFMAKERAW */
3000
3001 #if !defined (HAVE_CFSETSPEED)
3002 /* Workaround for targets which are missing cfsetspeed. */
3003 static int cfsetspeed (struct termios *termios_p, speed_t vitesse)
3004 {
3005 return (cfsetispeed (termios_p, vitesse)
3006 + cfsetospeed (termios_p, vitesse));
3007 }
3008 #endif
3009
3010 /* For serial-process-configure */
3011 void
3012 serial_configure (struct Lisp_Process *p,
3013 Lisp_Object contact)
3014 {
3015 Lisp_Object childp2 = Qnil;
3016 Lisp_Object tem = Qnil;
3017 struct termios attr;
3018 int err = -1;
3019 char summary[4] = "???"; /* This usually becomes "8N1". */
3020
3021 childp2 = Fcopy_sequence (p->childp);
3022
3023 /* Read port attributes and prepare default configuration. */
3024 err = tcgetattr (p->outfd, &attr);
3025 if (err != 0)
3026 error ("tcgetattr() failed: %s", emacs_strerror (errno));
3027 cfmakeraw (&attr);
3028 #if defined (CLOCAL)
3029 attr.c_cflag |= CLOCAL;
3030 #endif
3031 #if defined (CREAD)
3032 attr.c_cflag | CREAD;
3033 #endif
3034
3035 /* Configure speed. */
3036 if (!NILP (Fplist_member (contact, QCspeed)))
3037 tem = Fplist_get (contact, QCspeed);
3038 else
3039 tem = Fplist_get (p->childp, QCspeed);
3040 CHECK_NUMBER (tem);
3041 err = cfsetspeed (&attr, XINT (tem));
3042 if (err != 0)
3043 error ("cfsetspeed(%d) failed: %s", XINT (tem), emacs_strerror (errno));
3044 childp2 = Fplist_put (childp2, QCspeed, tem);
3045
3046 /* Configure bytesize. */
3047 if (!NILP (Fplist_member (contact, QCbytesize)))
3048 tem = Fplist_get (contact, QCbytesize);
3049 else
3050 tem = Fplist_get (p->childp, QCbytesize);
3051 if (NILP (tem))
3052 tem = make_number (8);
3053 CHECK_NUMBER (tem);
3054 if (XINT (tem) != 7 && XINT (tem) != 8)
3055 error (":bytesize must be nil (8), 7, or 8");
3056 summary[0] = XINT(tem) + '0';
3057 #if defined (CSIZE) && defined (CS7) && defined (CS8)
3058 attr.c_cflag &= ~CSIZE;
3059 attr.c_cflag |= ((XINT (tem) == 7) ? CS7 : CS8);
3060 #else
3061 /* Don't error on bytesize 8, which should be set by cfmakeraw. */
3062 if (XINT (tem) != 8)
3063 error ("Bytesize cannot be changed");
3064 #endif
3065 childp2 = Fplist_put (childp2, QCbytesize, tem);
3066
3067 /* Configure parity. */
3068 if (!NILP (Fplist_member (contact, QCparity)))
3069 tem = Fplist_get (contact, QCparity);
3070 else
3071 tem = Fplist_get (p->childp, QCparity);
3072 if (!NILP (tem) && !EQ (tem, Qeven) && !EQ (tem, Qodd))
3073 error (":parity must be nil (no parity), `even', or `odd'");
3074 #if defined (PARENB) && defined (PARODD) && defined (IGNPAR) && defined (INPCK)
3075 attr.c_cflag &= ~(PARENB | PARODD);
3076 attr.c_iflag &= ~(IGNPAR | INPCK);
3077 if (NILP (tem))
3078 {
3079 summary[1] = 'N';
3080 }
3081 else if (EQ (tem, Qeven))
3082 {
3083 summary[1] = 'E';
3084 attr.c_cflag |= PARENB;
3085 attr.c_iflag |= (IGNPAR | INPCK);
3086 }
3087 else if (EQ (tem, Qodd))
3088 {
3089 summary[1] = 'O';
3090 attr.c_cflag |= (PARENB | PARODD);
3091 attr.c_iflag |= (IGNPAR | INPCK);
3092 }
3093 #else
3094 /* Don't error on no parity, which should be set by cfmakeraw. */
3095 if (!NILP (tem))
3096 error ("Parity cannot be configured");
3097 #endif
3098 childp2 = Fplist_put (childp2, QCparity, tem);
3099
3100 /* Configure stopbits. */
3101 if (!NILP (Fplist_member (contact, QCstopbits)))
3102 tem = Fplist_get (contact, QCstopbits);
3103 else
3104 tem = Fplist_get (p->childp, QCstopbits);
3105 if (NILP (tem))
3106 tem = make_number (1);
3107 CHECK_NUMBER (tem);
3108 if (XINT (tem) != 1 && XINT (tem) != 2)
3109 error (":stopbits must be nil (1 stopbit), 1, or 2");
3110 summary[2] = XINT (tem) + '0';
3111 #if defined (CSTOPB)
3112 attr.c_cflag &= ~CSTOPB;
3113 if (XINT (tem) == 2)
3114 attr.c_cflag |= CSTOPB;
3115 #else
3116 /* Don't error on 1 stopbit, which should be set by cfmakeraw. */
3117 if (XINT (tem) != 1)
3118 error ("Stopbits cannot be configured");
3119 #endif
3120 childp2 = Fplist_put (childp2, QCstopbits, tem);
3121
3122 /* Configure flowcontrol. */
3123 if (!NILP (Fplist_member (contact, QCflowcontrol)))
3124 tem = Fplist_get (contact, QCflowcontrol);
3125 else
3126 tem = Fplist_get (p->childp, QCflowcontrol);
3127 if (!NILP (tem) && !EQ (tem, Qhw) && !EQ (tem, Qsw))
3128 error (":flowcontrol must be nil (no flowcontrol), `hw', or `sw'");
3129 #if defined (CRTSCTS)
3130 attr.c_cflag &= ~CRTSCTS;
3131 #endif
3132 #if defined (CNEW_RTSCTS)
3133 attr.c_cflag &= ~CNEW_RTSCTS;
3134 #endif
3135 #if defined (IXON) && defined (IXOFF)
3136 attr.c_iflag &= ~(IXON | IXOFF);
3137 #endif
3138 if (NILP (tem))
3139 {
3140 /* Already configured. */
3141 }
3142 else if (EQ (tem, Qhw))
3143 {
3144 #if defined (CRTSCTS)
3145 attr.c_cflag |= CRTSCTS;
3146 #elif defined (CNEW_RTSCTS)
3147 attr.c_cflag |= CNEW_RTSCTS;
3148 #else
3149 error ("Hardware flowcontrol (RTS/CTS) not supported");
3150 #endif
3151 }
3152 else if (EQ (tem, Qsw))
3153 {
3154 #if defined (IXON) && defined (IXOFF)
3155 attr.c_iflag |= (IXON | IXOFF);
3156 #else
3157 error ("Software flowcontrol (XON/XOFF) not supported");
3158 #endif
3159 }
3160 childp2 = Fplist_put (childp2, QCflowcontrol, tem);
3161
3162 /* Activate configuration. */
3163 err = tcsetattr (p->outfd, TCSANOW, &attr);
3164 if (err != 0)
3165 error ("tcsetattr() failed: %s", emacs_strerror (errno));
3166
3167 childp2 = Fplist_put (childp2, QCsummary, build_string (summary));
3168 p->childp = childp2;
3169
3170 }
3171 #endif /* TERMIOS */
3172
3173 /* arch-tag: edb43589-4e09-4544-b325-978b5b121dcf
3174 (do not change this comment) */