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