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