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