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