(c-before-hack-hook): New function
[bpt/emacs.git] / src / sysdep.c
CommitLineData
86a5659e 1/* Interfaces to system-dependent kernel and library entries.
275464e7 2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
8cabe764
GM
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
86a5659e
JB
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
86a5659e 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
86a5659e
JB
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
86a5659e 20
4838e624
PJ
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
86a5659e
JB
24
25#include <signal.h>
5890e9f7 26#include <stdio.h>
86a5659e 27#include <setjmp.h>
d3eb3bfa
DL
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
86a5659e 31#include "lisp.h"
8296bbf8
DL
32/* Including stdlib.h isn't necessarily enough to get srandom
33 declared, e.g. without __USE_XOPEN_EXTENDED with glibc 2. */
8296bbf8 34
4d395425
EZ
35/* The w32 build defines select stuff in w32.h, which is included by
36 sys/select.h (included below). */
37#ifndef WINDOWSNT
819b8f00 38#include "sysselect.h"
4d395425 39#endif
819b8f00 40
9ac0d9e0 41#include "blockinput.h"
86a5659e 42
fe03522b 43#ifdef WINDOWSNT
e15b6288
JR
44#define read sys_read
45#define write sys_write
fe03522b 46#include <windows.h>
e36ec798
AI
47#ifndef NULL
48#define NULL 0
49#endif
fe03522b
RS
50#endif /* not WINDOWSNT */
51
986ffb24
JB
52/* Does anyone other than VMS need this? */
53#ifndef fwrite
54#define sys_fwrite fwrite
55#else
56#undef fwrite
57#endif
58
86a5659e
JB
59#include <sys/types.h>
60#include <sys/stat.h>
61#include <errno.h>
62
f95c3f91 63#ifdef HAVE_SETPGID
2b7e8799 64#if !defined (USG) || defined (BSD_PGRPS)
c8875a65 65#undef setpgrp
f95c3f91
GM
66#define setpgrp setpgid
67#endif
2b7e8799 68#endif
f95c3f91 69
b05af5d3
PE
70/* Get SI_SRPC_DOMAIN, if it is available. */
71#ifdef HAVE_SYS_SYSTEMINFO_H
72#include <sys/systeminfo.h>
73#endif
74
207bdbdb
RS
75#ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
76#include <dos.h>
77#include "dosfns.h"
78#include "msdos.h"
79#include <sys/param.h>
15614e61
RS
80
81#if __DJGPP__ > 1
82extern int etext;
83extern unsigned start __asm__ ("start");
84#endif
207bdbdb
RS
85#endif
86
e36ec798 87#ifndef USE_CRT_DLL
9ee9af7a 88#ifndef errno
86a5659e 89extern int errno;
9ee9af7a 90#endif
e36ec798 91#endif
86a5659e 92
bb4bc8e2
RM
93#ifdef VMS
94#include <rms.h>
95#include <ttdef.h>
96#include <tt2def.h>
97#include <iodef.h>
98#include <ssdef.h>
99#include <descrip.h>
100#include <fibdef.h>
101#include <atrdef.h>
102#include <ctype.h>
103#include <string.h>
104#ifdef __GNUC__
105#include <sys/file.h>
106#else
107#include <file.h>
108#endif
109#undef F_SETFL
110#ifndef RAB$C_BID
111#include <rab.h>
112#endif
fe03522b 113#define MAXIOSIZE (32 * PAGESIZE) /* Don't I/O more than 32 blocks at a time */
bb4bc8e2
RM
114#endif /* VMS */
115
7ce1c4de
RS
116#ifndef VMS
117#include <sys/file.h>
118#endif /* not VMS */
119
612221ab 120#ifdef HAVE_FCNTL_H
bb4bc8e2
RM
121#include <fcntl.h>
122#endif
86a5659e 123
207bdbdb 124#ifndef MSDOS
86a5659e 125#include <sys/ioctl.h>
207bdbdb 126#endif
2a633456 127
e04a4e0d 128#include "systty.h"
94c8642a 129#include "syswait.h"
86a5659e 130
e39a993c 131#if defined (USG)
86a5659e 132#include <sys/utsname.h>
86a5659e 133#include <memory.h>
e39a993c 134#endif /* USG */
86a5659e 135
86a5659e
JB
136extern int quit_char;
137
e36ec798 138#include "keyboard.h"
0137dbf7 139#include "frame.h"
86a5659e
JB
140#include "window.h"
141#include "termhooks.h"
142#include "termchar.h"
143#include "termopts.h"
144#include "dispextern.h"
145#include "process.h"
0a125897 146#include "cm.h" /* for reset_sys_modes */
86a5659e 147
1eb8fd91 148/* For serial_configure and serial_open. */
d888760c
GM
149extern Lisp_Object QCport, QCspeed, QCprocess;
150extern Lisp_Object QCbytesize, QCstopbits, QCparity, Qodd, Qeven;
151extern Lisp_Object QCflowcontrol, Qhw, Qsw, QCsummary;
152
fe03522b
RS
153#ifdef WINDOWSNT
154#include <direct.h>
155/* In process.h which conflicts with the local copy. */
156#define _P_WAIT 0
43db14bb
JB
157int _cdecl _spawnlp (int, const char *, const char *, ...);
158int _cdecl _getpid (void);
b6682dd9 159extern char *getwd (char *);
fe03522b
RS
160#endif
161
86a5659e
JB
162#ifdef NONSYSTEM_DIR_LIBRARY
163#include "ndir.h"
164#endif /* NONSYSTEM_DIR_LIBRARY */
165
91bac16a
JB
166#include "syssignal.h"
167#include "systime.h"
d79998bc
KH
168#ifdef HAVE_UTIME_H
169#include <utime.h>
170#endif
171
172#ifndef HAVE_UTIMES
173#ifndef HAVE_STRUCT_UTIMBUF
174/* We want to use utime rather than utimes, but we couldn't find the
175 structure declaration. We'll use the traditional one. */
176struct utimbuf {
177 long actime;
178 long modtime;
179};
180#endif
181#endif
86a5659e 182
a00d5589
RS
183/* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
184#ifndef LPASS8
185#define LPASS8 0
186#endif
187
86a5659e 188static int baud_convert[] =
86a5659e
JB
189 {
190 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
191 1800, 2400, 4800, 9600, 19200, 38400
192 };
86a5659e 193
f0d21750
GM
194#ifdef HAVE_SPEED_T
195#include <termios.h>
f0d21750 196#else
ba5971b3 197#if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
ba5971b3 198#else
8a2a6032 199#if defined (HAVE_TERMIOS_H) && defined (GNU_LINUX)
515b04d0 200#include <termios.h>
f2361c60 201#endif
ba5971b3 202#endif
f0d21750 203#endif
86a5659e 204
5bdd7bdd
GM
205int emacs_ospeed;
206
01d09305 207void croak P_ ((char *)) NO_RETURN;
b9c4113e 208
89723395
GM
209/* Temporary used by `sigblock' when defined in terms of signprocmask. */
210
211SIGMASKTYPE sigprocmask_set;
212
f78f1a83 213
3480d92b 214#if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
f78f1a83 215
b6682dd9 216/* Return the current working directory. Returns NULL on errors.
f78f1a83
EZ
217 Any other returned value must be freed with free. This is used
218 only when get_current_dir_name is not defined on the system. */
219char*
220get_current_dir_name ()
221{
222 char *buf;
223 char *pwd;
224 struct stat dotstat, pwdstat;
225 /* If PWD is accurate, use it instead of calling getwd. PWD is
226 sometimes a nicer name, and using it may avoid a fatal error if a
227 parent directory is searchable but not readable. */
228 if ((pwd = getenv ("PWD")) != 0
229 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
230 && stat (pwd, &pwdstat) == 0
231 && stat (".", &dotstat) == 0
232 && dotstat.st_ino == pwdstat.st_ino
233 && dotstat.st_dev == pwdstat.st_dev
234#ifdef MAXPATHLEN
235 && strlen (pwd) < MAXPATHLEN
236#endif
237 )
238 {
239 buf = (char *) malloc (strlen (pwd) + 1);
240 if (!buf)
241 return NULL;
242 strcpy (buf, pwd);
243 }
244#ifdef HAVE_GETCWD
245 else
246 {
247 size_t buf_size = 1024;
b6682dd9 248 buf = (char *) malloc (buf_size);
f78f1a83
EZ
249 if (!buf)
250 return NULL;
251 for (;;)
252 {
253 if (getcwd (buf, buf_size) == buf)
254 break;
255 if (errno != ERANGE)
256 {
257 int tmp_errno = errno;
258 free (buf);
259 errno = tmp_errno;
260 return NULL;
261 }
262 buf_size *= 2;
263 buf = (char *) realloc (buf, buf_size);
264 if (!buf)
265 return NULL;
266 }
267 }
268#else
269 else
270 {
271 /* We need MAXPATHLEN here. */
272 buf = (char *) malloc (MAXPATHLEN + 1);
273 if (!buf)
274 return NULL;
275 if (getwd (buf) == NULL)
276 {
277 int tmp_errno = errno;
278 free (buf);
279 errno = tmp_errno;
280 return NULL;
281 }
282 }
283#endif
284 return buf;
285}
286#endif
287
64e971c3 288\f
9628b887 289/* Discard pending input on all input descriptors. */
91bac16a 290
08633194 291void
86a5659e
JB
292discard_tty_input ()
293{
fe03522b 294#ifndef WINDOWSNT
91bac16a 295 struct emacs_tty buf;
86a5659e
JB
296
297 if (noninteractive)
298 return;
299
86a5659e
JB
300#ifdef VMS
301 end_kbd_input ();
0b0d3e0b 302 SYS$QIOW (0, fileno (CURTTY()->input), IO$_READVBLK|IO$M_PURGE, input_iosb, 0, 0,
91bac16a 303 &buf.main, 0, 0, terminator_mask, 0, 0);
86a5659e
JB
304 queue_kbd_input ();
305#else /* not VMS */
fe03522b 306#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
207bdbdb 307 while (dos_keyread () != -1)
fe03522b 308 ;
207bdbdb 309#else /* not MSDOS */
9628b887 310 {
28d7d09f 311 struct tty_display_info *tty;
9628b887
KL
312 for (tty = tty_list; tty; tty = tty->next)
313 {
0b0d3e0b
KL
314 if (tty->input) /* Is the device suspended? */
315 {
316 EMACS_GET_TTY (fileno (tty->input), &buf);
317 EMACS_SET_TTY (fileno (tty->input), &buf, 0);
318 }
9628b887
KL
319 }
320 }
207bdbdb 321#endif /* not MSDOS */
86a5659e 322#endif /* not VMS */
fe03522b 323#endif /* not WINDOWSNT */
86a5659e
JB
324}
325
16c290d8 326\f
86a5659e
JB
327#ifdef SIGTSTP
328
64e971c3 329/* Arrange for character C to be read as the next input from
16c290d8
KL
330 the terminal.
331 XXX What if we have multiple ttys?
332*/
64e971c3 333
dfcf069d 334void
0c2338d8 335stuff_char (char c)
86a5659e 336{
428a555e 337 if (! FRAME_TERMCAP_P (SELECTED_FRAME ()))
23dab951
RS
338 return;
339
86a5659e
JB
340/* Should perhaps error if in batch mode */
341#ifdef TIOCSTI
0b0d3e0b 342 ioctl (fileno (CURTTY()->input), TIOCSTI, &c);
86a5659e 343#else /* no TIOCSTI */
71f06467 344 error ("Cannot stuff terminal input characters in this version of Unix");
86a5659e
JB
345#endif /* no TIOCSTI */
346}
347
348#endif /* SIGTSTP */
64e971c3 349\f
08633194 350void
16c290d8 351init_baud_rate (int fd)
86a5659e 352{
86a5659e 353 if (noninteractive)
5bdd7bdd 354 emacs_ospeed = 0;
86a5659e
JB
355 else
356 {
fe03522b 357#ifdef DOS_NT
5bdd7bdd 358 emacs_ospeed = 15;
fe03522b 359#else /* not DOS_NT */
86a5659e 360#ifdef VMS
91bac16a
JB
361 struct sensemode sg;
362
16c290d8 363 SYS$QIOW (0, fd, IO$_SENSEMODE, &sg, 0, 0,
86a5659e 364 &sg.class, 12, 0, 0, 0, 0 );
5bdd7bdd 365 emacs_ospeed = sg.xmit_baud;
91bac16a 366#else /* not VMS */
e04a4e0d
JB
367#ifdef HAVE_TERMIOS
368 struct termios sg;
91bac16a 369
71f06467 370 sg.c_cflag = B9600;
16c290d8 371 tcgetattr (fd, &sg);
5bdd7bdd 372 emacs_ospeed = cfgetospeed (&sg);
e04a4e0d
JB
373#else /* neither VMS nor TERMIOS */
374#ifdef HAVE_TERMIO
375 struct termio sg;
91bac16a 376
71f06467 377 sg.c_cflag = B9600;
e04a4e0d 378#ifdef HAVE_TCATTR
16c290d8 379 tcgetattr (fd, &sg);
e04a4e0d 380#else
16c290d8 381 ioctl (fd, TCGETA, &sg);
e04a4e0d 382#endif
5bdd7bdd 383 emacs_ospeed = sg.c_cflag & CBAUD;
e04a4e0d 384#else /* neither VMS nor TERMIOS nor TERMIO */
91bac16a 385 struct sgttyb sg;
177c0ea7 386
91bac16a 387 sg.sg_ospeed = B9600;
16c290d8 388 if (ioctl (fd, TIOCGETP, &sg) < 0)
d7272cff 389 abort ();
5bdd7bdd 390 emacs_ospeed = sg.sg_ospeed;
91bac16a 391#endif /* not HAVE_TERMIO */
e04a4e0d 392#endif /* not HAVE_TERMIOS */
86a5659e 393#endif /* not VMS */
fe03522b 394#endif /* not DOS_NT */
86a5659e 395 }
177c0ea7 396
5bdd7bdd
GM
397 baud_rate = (emacs_ospeed < sizeof baud_convert / sizeof baud_convert[0]
398 ? baud_convert[emacs_ospeed] : 9600);
86a5659e
JB
399 if (baud_rate == 0)
400 baud_rate = 1200;
401}
402
16c290d8 403\f
86a5659e 404/*ARGSUSED*/
dfcf069d 405void
86a5659e
JB
406set_exclusive_use (fd)
407 int fd;
408{
409#ifdef FIOCLEX
410 ioctl (fd, FIOCLEX, 0);
411#endif
412 /* Ok to do nothing if this feature does not exist */
413}
64e971c3 414\f
86a5659e
JB
415#ifndef subprocesses
416
417wait_without_blocking ()
418{
2a633456 419#ifdef BSD_SYSTEM
86a5659e
JB
420 wait3 (0, WNOHANG | WUNTRACED, 0);
421#else
422 croak ("wait_without_blocking");
423#endif
424 synch_process_alive = 0;
425}
426
427#endif /* not subprocesses */
428
429int wait_debugging; /* Set nonzero to make following function work under dbx
fe03522b 430 (at least for bsd). */
86a5659e
JB
431
432SIGTYPE
433wait_for_termination_signal ()
434{}
435
436/* Wait for subprocess with process id `pid' to terminate and
437 make sure it will get eliminated (not remain forever as a zombie) */
438
08633194 439void
86a5659e
JB
440wait_for_termination (pid)
441 int pid;
442{
443 while (1)
444 {
445#ifdef subprocesses
446#ifdef VMS
447 int status;
448
986ffb24 449 status = SYS$FORCEX (&pid, 0, 0);
86a5659e
JB
450 break;
451#else /* not VMS */
f2a77c3a 452#if defined (BSD_SYSTEM) || defined (HPUX)
4c8975ad
RS
453 /* Note that kill returns -1 even if the process is just a zombie now.
454 But inevitably a SIGCHLD interrupt should be generated
455 and child_sig will do wait3 and make the process go away. */
456 /* There is some indication that there is a bug involved with
457 termination of subprocesses, perhaps involving a kernel bug too,
458 but no idea what it is. Just as a hunch we signal SIGCHLD to see
459 if that causes the problem to go away or get worse. */
460 sigsetmask (sigmask (SIGCHLD));
461 if (0 > kill (pid, 0))
fe03522b 462 {
4c8975ad
RS
463 sigsetmask (SIGEMPTYMASK);
464 kill (getpid (), SIGCHLD);
465 break;
466 }
467 if (wait_debugging)
468 sleep (1);
469 else
470 sigpause (SIGEMPTYMASK);
2a633456 471#else /* not BSD_SYSTEM, and not HPUX version >= 6 */
8a2a6032 472#ifdef POSIX_SIGNALS /* would this work for GNU/Linux as well? */
9ab714c7 473 sigblock (sigmask (SIGCHLD));
59957f85
GM
474 errno = 0;
475 if (kill (pid, 0) == -1 && errno == ESRCH)
9ab714c7
RS
476 {
477 sigunblock (sigmask (SIGCHLD));
478 break;
479 }
ac3ac859 480
0fdcb867 481 sigsuspend (&empty_mask);
9ab714c7 482#else /* not POSIX_SIGNALS */
4c8975ad
RS
483#ifdef HAVE_SYSV_SIGPAUSE
484 sighold (SIGCHLD);
485 if (0 > kill (pid, 0))
486 {
487 sigrelse (SIGCHLD);
488 break;
489 }
490 sigpause (SIGCHLD);
491#else /* not HAVE_SYSV_SIGPAUSE */
fe03522b
RS
492#ifdef WINDOWSNT
493 wait (0);
494 break;
495#else /* not WINDOWSNT */
4c8975ad 496 if (0 > kill (pid, 0))
86a5659e 497 break;
4c8975ad
RS
498 /* Using sleep instead of pause avoids timing error.
499 If the inferior dies just before the sleep,
500 we lose just one second. */
501 sleep (1);
fe03522b 502#endif /* not WINDOWSNT */
4c8975ad 503#endif /* not HAVE_SYSV_SIGPAUSE */
9ab714c7 504#endif /* not POSIX_SIGNALS */
2a633456 505#endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
86a5659e
JB
506#endif /* not VMS */
507#else /* not subprocesses */
15614e61
RS
508#if __DJGPP__ > 1
509 break;
510#else /* not __DJGPP__ > 1 */
86a5659e
JB
511 if (kill (pid, 0) < 0)
512 break;
513 wait (0);
15614e61 514#endif /* not __DJGPP__ > 1*/
86a5659e
JB
515#endif /* not subprocesses */
516 }
517}
518
519#ifdef subprocesses
520
521/*
522 * flush any pending output
523 * (may flush input as well; it does not matter the way we use it)
524 */
177c0ea7 525
08633194 526void
86a5659e
JB
527flush_pending_output (channel)
528 int channel;
529{
530#ifdef HAVE_TERMIOS
531 /* If we try this, we get hit with SIGTTIN, because
532 the child's tty belongs to the child's pgrp. */
533#else
534#ifdef TCFLSH
535 ioctl (channel, TCFLSH, 1);
536#else
537#ifdef TIOCFLUSH
538 int zero = 0;
539 /* 3rd arg should be ignored
540 but some 4.2 kernels actually want the address of an int
541 and nonzero means something different. */
542 ioctl (channel, TIOCFLUSH, &zero);
543#endif
544#endif
545#endif
546}
64e971c3 547\f
86a5659e
JB
548#ifndef VMS
549/* Set up the terminal at the other end of a pseudo-terminal that
550 we will be controlling an inferior through.
551 It should not echo or do line-editing, since that is done
552 in Emacs. No padding needed for insertion into an Emacs buffer. */
553
08633194 554void
86a5659e
JB
555child_setup_tty (out)
556 int out;
557{
fe03522b 558#ifndef DOS_NT
91bac16a
JB
559 struct emacs_tty s;
560
561 EMACS_GET_TTY (out, &s);
86a5659e 562
31be8d24 563#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
91bac16a
JB
564 s.main.c_oflag |= OPOST; /* Enable output postprocessing */
565 s.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL on output */
9d4e5eea 566#ifdef NLDLY
07cfc4e7
GM
567 /* http://lists.gnu.org/archive/html/emacs-devel/2008-05/msg00406.html
568 Some versions of GNU Hurd do not have FFDLY? */
569#ifdef FFDLY
91bac16a
JB
570 s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
571 /* No output delays */
07cfc4e7
GM
572#else
573 s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY);
574 /* No output delays */
575#endif
9d4e5eea 576#endif
91bac16a
JB
577 s.main.c_lflag &= ~ECHO; /* Disable echo */
578 s.main.c_lflag |= ISIG; /* Enable signals */
9d4e5eea
RS
579#ifdef IUCLC
580 s.main.c_iflag &= ~IUCLC; /* Disable downcasing on input. */
581#endif
f7097b2a
RS
582#ifdef ISTRIP
583 s.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
584#endif
23e4c8be 585#ifdef OLCUC
9d4e5eea
RS
586 s.main.c_oflag &= ~OLCUC; /* Disable upcasing on output. */
587#endif
f7097b2a 588 s.main.c_oflag &= ~TAB3; /* Disable tab expansion */
1bf96fb5 589 s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
91bac16a
JB
590 s.main.c_lflag |= ICANON; /* Enable erase/kill and eof processing */
591 s.main.c_cc[VEOF] = 04; /* insure that EOF is Control-D */
441f6399
RS
592 s.main.c_cc[VERASE] = CDISABLE; /* disable erase processing */
593 s.main.c_cc[VKILL] = CDISABLE; /* disable kill processing */
91bac16a 594
86a5659e 595#ifdef HPUX
91bac16a 596 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
86a5659e 597#endif /* HPUX */
91bac16a 598
dfe598b5
RS
599#ifdef SIGNALS_VIA_CHARACTERS
600 /* the QUIT and INTR character are used in process_send_signal
601 so set them here to something useful. */
fa51fa32 602 if (s.main.c_cc[VQUIT] == CDISABLE)
dfe598b5 603 s.main.c_cc[VQUIT] = '\\'&037; /* Control-\ */
fa51fa32 604 if (s.main.c_cc[VINTR] == CDISABLE)
dfe598b5
RS
605 s.main.c_cc[VINTR] = 'C'&037; /* Control-C */
606#endif /* not SIGNALS_VIA_CHARACTERS */
607
86a5659e
JB
608#ifdef AIX
609/* AIX enhanced edit loses NULs, so disable it */
610#ifndef IBMR2AIX
91bac16a
JB
611 s.main.c_line = 0;
612 s.main.c_iflag &= ~ASCEDIT;
86a5659e
JB
613#endif
614 /* Also, PTY overloads NUL and BREAK.
615 don't ignore break, but don't signal either, so it looks like NUL. */
91bac16a
JB
616 s.main.c_iflag &= ~IGNBRK;
617 s.main.c_iflag &= ~BRKINT;
dfe598b5
RS
618 /* rms: Formerly it set s.main.c_cc[VINTR] to 0377 here
619 unconditionally. Then a SIGNALS_VIA_CHARACTERS conditional
620 would force it to 0377. That looks like duplicated code. */
621#ifndef SIGNALS_VIA_CHARACTERS
e6cc3307 622 /* QUIT and INTR work better as signals, so disable character forms */
fa51fa32
KS
623 s.main.c_cc[VQUIT] = CDISABLE;
624 s.main.c_cc[VINTR] = CDISABLE;
91bac16a 625 s.main.c_lflag &= ~ISIG;
e6cc3307 626#endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
fa51fa32 627 s.main.c_cc[VEOL] = CDISABLE;
91bac16a 628 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
86a5659e
JB
629#endif /* AIX */
630
631#else /* not HAVE_TERMIO */
91bac16a
JB
632
633 s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
634 | CBREAK | TANDEM);
a00d5589 635 s.main.sg_flags |= LPASS8;
91bac16a
JB
636 s.main.sg_erase = 0377;
637 s.main.sg_kill = 0377;
1bf96fb5 638 s.lmode = LLITOUT | s.lmode; /* Don't strip 8th bit */
91bac16a 639
86a5659e
JB
640#endif /* not HAVE_TERMIO */
641
91bac16a 642 EMACS_SET_TTY (out, &s, 0);
86a5659e 643
fe03522b 644#endif /* not DOS_NT */
86a5659e
JB
645}
646#endif /* not VMS */
647
648#endif /* subprocesses */
64e971c3 649\f
86a5659e
JB
650/* Record a signal code and the handler for it. */
651struct save_signal
652{
653 int code;
35a05cca 654 SIGTYPE (*handler) P_ ((int));
86a5659e
JB
655};
656
35a05cca
AS
657static void save_signal_handlers P_ ((struct save_signal *));
658static void restore_signal_handlers P_ ((struct save_signal *));
659
86a5659e
JB
660/* Suspend the Emacs process; give terminal to its superior. */
661
08633194 662void
86a5659e
JB
663sys_suspend ()
664{
665#ifdef VMS
88191e36
RS
666 /* "Foster" parentage allows emacs to return to a subprocess that attached
667 to the current emacs as a cheaper than starting a whole new process. This
668 is set up by KEPTEDITOR.COM. */
669 unsigned long parent_id, foster_parent_id;
670 char *fpid_string;
671
672 fpid_string = getenv ("EMACS_PARENT_PID");
673 if (fpid_string != NULL)
674 {
675 sscanf (fpid_string, "%x", &foster_parent_id);
676 if (foster_parent_id != 0)
677 parent_id = foster_parent_id;
678 else
679 parent_id = getppid ();
680 }
681 else
682 parent_id = getppid ();
683
9ac0d9e0 684 xfree (fpid_string); /* On VMS, this was malloc'd */
86a5659e 685
86a5659e
JB
686 if (parent_id && parent_id != 0xffffffff)
687 {
688 SIGTYPE (*oldsig)() = (int) signal (SIGINT, SIG_IGN);
689 int status = LIB$ATTACH (&parent_id) & 1;
690 signal (SIGINT, oldsig);
691 return status;
692 }
693 else
694 {
695 struct {
696 int l;
697 char *a;
698 } d_prompt;
699 d_prompt.l = sizeof ("Emacs: "); /* Our special prompt */
700 d_prompt.a = "Emacs: "; /* Just a reminder */
986ffb24 701 LIB$SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt, 0);
86a5659e
JB
702 return 1;
703 }
704 return -1;
705#else
c4ea52a6 706#if defined (SIGTSTP) && !defined (MSDOS)
86a5659e 707
5a570e37 708 {
e89a2cd5 709 int pgrp = EMACS_GETPGRP (0);
5a570e37
JB
710 EMACS_KILLPG (pgrp, SIGTSTP);
711 }
86a5659e
JB
712
713#else /* No SIGTSTP */
714#ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
715 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
716 kill (getpid (), SIGQUIT);
717
718#else /* No SIGTSTP or USG_JOBCTRL */
719
720/* On a system where suspending is not implemented,
721 instead fork a subshell and let it talk directly to the terminal
722 while we wait. */
a0932daa
KH
723 sys_subshell ();
724
725#endif /* no USG_JOBCTRL */
726#endif /* no SIGTSTP */
727#endif /* not VMS */
728}
729
730/* Fork a subshell. */
731
08633194 732void
a0932daa
KH
733sys_subshell ()
734{
735#ifndef VMS
ad00c243 736#ifdef DOS_NT /* Demacs 1.1.2 91/10/20 Manabu Higashida */
a0932daa
KH
737 int st;
738 char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS. */
739#endif
efa04277 740 int pid;
86a5659e 741 struct save_signal saved_handlers[5];
efa04277
RS
742 Lisp_Object dir;
743 unsigned char *str = 0;
744 int len;
86a5659e
JB
745
746 saved_handlers[0].code = SIGINT;
747 saved_handlers[1].code = SIGQUIT;
748 saved_handlers[2].code = SIGTERM;
749#ifdef SIGIO
750 saved_handlers[3].code = SIGIO;
751 saved_handlers[4].code = 0;
752#else
753 saved_handlers[3].code = 0;
754#endif
755
efa04277
RS
756 /* Mentioning current_buffer->buffer would mean including buffer.h,
757 which somehow wedges the hp compiler. So instead... */
758
759 dir = intern ("default-directory");
0e7e7a58 760 if (NILP (Fboundp (dir)))
efa04277
RS
761 goto xyzzy;
762 dir = Fsymbol_value (dir);
914e81a2 763 if (!STRINGP (dir))
efa04277
RS
764 goto xyzzy;
765
766 dir = expand_and_dir_to_file (Funhandled_file_name_directory (dir), Qnil);
d5db4077
KR
767 str = (unsigned char *) alloca (SCHARS (dir) + 2);
768 len = SCHARS (dir);
769 bcopy (SDATA (dir), str, len);
efa04277
RS
770 if (str[len - 1] != '/') str[len++] = '/';
771 str[len] = 0;
772 xyzzy:
773
ad00c243 774#ifdef DOS_NT
7964ba9e 775 pid = 0;
718ca3cf
RS
776#if __DJGPP__ > 1
777 save_signal_handlers (saved_handlers);
778 synch_process_alive = 1;
779#endif /* __DJGPP__ > 1 */
177c0ea7 780#else
efa04277 781 pid = vfork ();
86a5659e
JB
782 if (pid == -1)
783 error ("Can't spawn subshell");
7964ba9e
RS
784#endif
785
86a5659e
JB
786 if (pid == 0)
787 {
7964ba9e 788 char *sh = 0;
86a5659e 789
ad00c243 790#ifdef DOS_NT /* MW, Aug 1993 */
207bdbdb 791 getwd (oldwd);
7964ba9e
RS
792 if (sh == 0)
793 sh = (char *) egetenv ("SUSPEND"); /* KFS, 1994-12-14 */
207bdbdb 794#endif
7964ba9e
RS
795 if (sh == 0)
796 sh = (char *) egetenv ("SHELL");
86a5659e
JB
797 if (sh == 0)
798 sh = "sh";
207bdbdb 799
86a5659e 800 /* Use our buffer's default directory for the subshell. */
efa04277 801 if (str)
074c438c 802 chdir ((char *) str);
efa04277 803
86a5659e
JB
804#ifdef subprocesses
805 close_process_descs (); /* Close Emacs's pipes/ptys */
806#endif
1593c2fe 807
6f8b4d01 808#ifdef SET_EMACS_PRIORITY
1593c2fe 809 {
31ade731 810 extern EMACS_INT emacs_priority;
1593c2fe 811
6f8b4d01 812 if (emacs_priority < 0)
1593c2fe
JB
813 nice (-emacs_priority);
814 }
815#endif
816
fe03522b 817#ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
c0917202 818 {
48178f9a
EZ
819 char *epwd = getenv ("PWD");
820 char old_pwd[MAXPATHLEN+1+4];
c0917202
EZ
821
822 /* If PWD is set, pass it with corrected value. */
48178f9a 823 if (epwd)
c0917202 824 {
48178f9a 825 strcpy (old_pwd, epwd);
c0917202
EZ
826 if (str[len - 1] == '/')
827 str[len - 1] = '\0';
828 setenv ("PWD", str, 1);
829 }
830 st = system (sh);
831 chdir (oldwd);
48178f9a 832 if (epwd)
c0917202
EZ
833 putenv (old_pwd); /* restore previous value */
834 }
207bdbdb 835#else /* not MSDOS */
fe03522b 836#ifdef WINDOWSNT
fe03522b
RS
837 /* Waits for process completion */
838 pid = _spawnlp (_P_WAIT, sh, sh, NULL);
ad00c243 839 chdir (oldwd);
fe03522b
RS
840 if (pid == -1)
841 write (1, "Can't execute subshell", 22);
fe03522b 842#else /* not WINDOWSNT */
e120ea40 843 execlp (sh, sh, (char *) 0);
86a5659e
JB
844 write (1, "Can't execute subshell", 22);
845 _exit (1);
fe03522b 846#endif /* not WINDOWSNT */
207bdbdb 847#endif /* not MSDOS */
86a5659e
JB
848 }
849
718ca3cf
RS
850 /* Do this now if we did not do it before. */
851#if !defined (MSDOS) || __DJGPP__ == 1
86a5659e 852 save_signal_handlers (saved_handlers);
ffafc793 853 synch_process_alive = 1;
718ca3cf
RS
854#endif
855
ad00c243 856#ifndef DOS_NT
86a5659e 857 wait_for_termination (pid);
7964ba9e 858#endif
86a5659e 859 restore_signal_handlers (saved_handlers);
718ca3cf 860 synch_process_alive = 0;
a0932daa 861#endif /* !VMS */
86a5659e
JB
862}
863
35a05cca 864static void
86a5659e
JB
865save_signal_handlers (saved_handlers)
866 struct save_signal *saved_handlers;
867{
868 while (saved_handlers->code)
869 {
508b171c 870 saved_handlers->handler
35a05cca 871 = (SIGTYPE (*) P_ ((int))) signal (saved_handlers->code, SIG_IGN);
86a5659e
JB
872 saved_handlers++;
873 }
874}
875
35a05cca 876static void
86a5659e
JB
877restore_signal_handlers (saved_handlers)
878 struct save_signal *saved_handlers;
879{
880 while (saved_handlers->code)
881 {
882 signal (saved_handlers->code, saved_handlers->handler);
883 saved_handlers++;
884 }
885}
886\f
4d553a13
KL
887#ifndef SIGIO
888/* If SIGIO is broken, don't do anything. */
889void
890init_sigio (int fd)
891{
892}
893
894void
895reset_sigio (int fd)
896{
897}
898
899void
900request_sigio (void)
901{
902}
903
904void
905unrequest_sigio (void)
906{
907}
908
909#else
86a5659e
JB
910#ifdef F_SETFL
911
819b8f00 912int old_fcntl_flags[MAXDESC];
86a5659e 913
08633194 914void
23dab951
RS
915init_sigio (fd)
916 int fd;
86a5659e
JB
917{
918#ifdef FASYNC
819b8f00
KL
919 old_fcntl_flags[fd] = fcntl (fd, F_GETFL, 0) & ~FASYNC;
920 fcntl (fd, F_SETFL, old_fcntl_flags[fd] | FASYNC);
86a5659e 921#endif
23dab951 922 interrupts_deferred = 0;
86a5659e
JB
923}
924
590034f9 925void
0a125897
KL
926reset_sigio (fd)
927 int fd;
86a5659e 928{
4d553a13 929#ifdef FASYNC
819b8f00 930 fcntl (fd, F_SETFL, old_fcntl_flags[fd]);
4d553a13 931#endif
86a5659e
JB
932}
933
eb8c3be9 934#ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
428a555e 935/* XXX Uhm, FASYNC is not used anymore here. */
6d8cf830 936/* XXX Yeah, but you need it for SIGIO, don't you? */
86a5659e 937
08633194 938void
86a5659e
JB
939request_sigio ()
940{
6bbba5a6 941 if (noninteractive)
23dab951
RS
942 return;
943
86a5659e 944#ifdef SIGWINCH
e065a56e 945 sigunblock (sigmask (SIGWINCH));
86a5659e 946#endif
0a125897 947 sigunblock (sigmask (SIGIO));
86a5659e
JB
948
949 interrupts_deferred = 0;
950}
951
08633194 952void
0a125897 953unrequest_sigio (void)
428a555e 954{
6bbba5a6
KL
955 if (noninteractive)
956 return;
957
958#if 0 /* XXX What's wrong with blocking SIGIO under X? */
959 if (x_display_list)
23dab951 960 return;
428a555e 961#endif
6bbba5a6 962
86a5659e 963#ifdef SIGWINCH
e065a56e 964 sigblock (sigmask (SIGWINCH));
86a5659e 965#endif
0a125897 966 sigblock (sigmask (SIGIO));
86a5659e
JB
967 interrupts_deferred = 1;
968}
969
970#else /* no FASYNC */
feb67dfe 971#ifndef MSDOS
25ab68af 972
08633194 973void
86a5659e
JB
974request_sigio ()
975{
6e5b2385 976 if (noninteractive || read_socket_hook)
23dab951
RS
977 return;
978
86a5659e
JB
979 croak ("request_sigio");
980}
177c0ea7 981
08633194 982void
86a5659e
JB
983unrequest_sigio ()
984{
6e5b2385 985 if (noninteractive || read_socket_hook)
23dab951
RS
986 return;
987
86a5659e
JB
988 croak ("unrequest_sigio");
989}
feb67dfe
EZ
990
991#endif /* MSDOS */
86a5659e
JB
992#endif /* FASYNC */
993#endif /* F_SETFL */
4d553a13 994#endif /* SIGIO */
86a5659e 995\f
9ae8f997
JB
996/* Saving and restoring the process group of Emacs's terminal. */
997
0ba73609 998#ifdef BSD_PGRPS
9ae8f997
JB
999
1000/* The process group of which Emacs was a member when it initially
1001 started.
1002
1003 If Emacs was in its own process group (i.e. inherited_pgroup ==
1004 getpid ()), then we know we're running under a shell with job
1005 control (Emacs would never be run as part of a pipeline).
1006 Everything is fine.
1007
1008 If Emacs was not in its own process group, then we know we're
1009 running under a shell (or a caller) that doesn't know how to
1010 separate itself from Emacs (like sh). Emacs must be in its own
1011 process group in order to receive SIGIO correctly. In this
1012 situation, we put ourselves in our own pgroup, forcibly set the
1013 tty's pgroup to our pgroup, and make sure to restore and reinstate
1014 the tty's pgroup just like any other terminal setting. If
1015 inherited_group was not the tty's pgroup, then we'll get a
1016 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
4edd8a5c
KL
1017 it goes foreground in the future, which is what should happen.
1018
1019 This variable is initialized in emacs.c. */
9ae8f997
JB
1020int inherited_pgroup;
1021
4edd8a5c
KL
1022/* Split off the foreground process group to Emacs alone. When we are
1023 in the foreground, but not started in our own process group,
1024 redirect the tty device handle FD to point to our own process
1025 group. We need to be in our own process group to receive SIGIO
1026 properly. */
75a8734b 1027static void
16c290d8 1028narrow_foreground_group (int fd)
9ae8f997
JB
1029{
1030 int me = getpid ();
1031
4edd8a5c 1032 setpgrp (0, inherited_pgroup);
3453bb3a
KL
1033#if 0
1034 /* XXX inherited_pgroup should not be zero here, but GTK seems to
1035 mess this up. */
819b8f00 1036 if (! inherited_pgroup)
4edd8a5c 1037 abort (); /* Should not happen. */
3453bb3a 1038#endif
9ae8f997 1039 if (inherited_pgroup != me)
4edd8a5c
KL
1040 EMACS_SET_TTY_PGRP (fd, &me); /* XXX This only works on the controlling tty. */
1041 setpgrp (0, me);
9ae8f997
JB
1042}
1043
1044/* Set the tty to our original foreground group. */
75a8734b 1045static void
16c290d8 1046widen_foreground_group (int fd)
9ae8f997
JB
1047{
1048 if (inherited_pgroup != getpid ())
16c290d8 1049 EMACS_SET_TTY_PGRP (fd, &inherited_pgroup);
4edd8a5c 1050 setpgrp (0, inherited_pgroup);
9ae8f997
JB
1051}
1052
0ba73609 1053#endif /* BSD_PGRPS */
9ae8f997 1054\f
68936329
JB
1055/* Getting and setting emacs_tty structures. */
1056
1057/* Set *TC to the parameters associated with the terminal FD.
1058 Return zero if all's well, or -1 if we ran into an error we
1059 couldn't deal with. */
1060int
1061emacs_get_tty (fd, settings)
1062 int fd;
1063 struct emacs_tty *settings;
1064{
1065 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
1066#ifdef HAVE_TCATTR
1067 /* We have those nifty POSIX tcmumbleattr functions. */
1d9f9f9e 1068 bzero (&settings->main, sizeof (settings->main));
68936329
JB
1069 if (tcgetattr (fd, &settings->main) < 0)
1070 return -1;
1071
1072#else
1073#ifdef HAVE_TERMIO
1074 /* The SYSV-style interface? */
1075 if (ioctl (fd, TCGETA, &settings->main) < 0)
1076 return -1;
1077
1078#else
1079#ifdef VMS
1080 /* Vehemently Monstrous System? :-) */
1081 if (! (SYS$QIOW (0, fd, IO$_SENSEMODE, settings, 0, 0,
1082 &settings->main.class, 12, 0, 0, 0, 0)
1083 & 1))
1084 return -1;
1085
1086#else
fe03522b 1087#ifndef DOS_NT
68936329
JB
1088 /* I give up - I hope you have the BSD ioctls. */
1089 if (ioctl (fd, TIOCGETP, &settings->main) < 0)
1090 return -1;
fe03522b 1091#endif /* not DOS_NT */
68936329
JB
1092#endif
1093#endif
1094#endif
1095
1096 /* Suivant - Do we have to get struct ltchars data? */
50b8cf60 1097#ifdef HAVE_LTCHARS
68936329
JB
1098 if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
1099 return -1;
1100#endif
1101
1102 /* How about a struct tchars and a wordful of lmode bits? */
50b8cf60 1103#ifdef HAVE_TCHARS
68936329
JB
1104 if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
1105 || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
1106 return -1;
1107#endif
1108
1109 /* We have survived the tempest. */
1110 return 0;
1111}
1112
1113
1114/* Set the parameters of the tty on FD according to the contents of
394049ec 1115 *SETTINGS. If FLUSHP is non-zero, we discard input.
68936329 1116 Return 0 if all went well, and -1 if anything failed. */
394049ec 1117
68936329 1118int
394049ec 1119emacs_set_tty (fd, settings, flushp)
68936329
JB
1120 int fd;
1121 struct emacs_tty *settings;
394049ec 1122 int flushp;
68936329
JB
1123{
1124 /* Set the primary parameters - baud rate, character size, etcetera. */
1125#ifdef HAVE_TCATTR
e6cc3307 1126 int i;
68936329
JB
1127 /* We have those nifty POSIX tcmumbleattr functions.
1128 William J. Smith <wjs@wiis.wang.com> writes:
c4ea52a6 1129 "POSIX 1003.1 defines tcsetattr to return success if it was
68936329
JB
1130 able to perform any of the requested actions, even if some
1131 of the requested actions could not be performed.
1132 We must read settings back to ensure tty setup properly.
1133 AIX requires this to keep tty from hanging occasionally." */
eb8c3be9 1134 /* This make sure that we don't loop indefinitely in here. */
e6cc3307 1135 for (i = 0 ; i < 10 ; i++)
394049ec 1136 if (tcsetattr (fd, flushp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
68936329
JB
1137 {
1138 if (errno == EINTR)
1139 continue;
1140 else
1141 return -1;
1142 }
1143 else
1144 {
1145 struct termios new;
1146
1d9f9f9e 1147 bzero (&new, sizeof (new));
68936329
JB
1148 /* Get the current settings, and see if they're what we asked for. */
1149 tcgetattr (fd, &new);
e6cc3307
RS
1150 /* We cannot use memcmp on the whole structure here because under
1151 * aix386 the termios structure has some reserved field that may
1152 * not be filled in.
1153 */
1154 if ( new.c_iflag == settings->main.c_iflag
1155 && new.c_oflag == settings->main.c_oflag
1156 && new.c_cflag == settings->main.c_cflag
1157 && new.c_lflag == settings->main.c_lflag
c4ea52a6 1158 && memcmp (new.c_cc, settings->main.c_cc, NCCS) == 0)
68936329 1159 break;
e6cc3307
RS
1160 else
1161 continue;
68936329
JB
1162 }
1163
1164#else
1165#ifdef HAVE_TERMIO
1166 /* The SYSV-style interface? */
394049ec 1167 if (ioctl (fd, flushp ? TCSETAF : TCSETAW, &settings->main) < 0)
68936329
JB
1168 return -1;
1169
1170#else
1171#ifdef VMS
1172 /* Vehemently Monstrous System? :-) */
1173 if (! (SYS$QIOW (0, fd, IO$_SETMODE, &input_iosb, 0, 0,
1174 &settings->main.class, 12, 0, 0, 0, 0)
1175 & 1))
1176 return -1;
1177
1178#else
fe03522b 1179#ifndef DOS_NT
68936329 1180 /* I give up - I hope you have the BSD ioctls. */
394049ec 1181 if (ioctl (fd, (flushp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
68936329 1182 return -1;
fe03522b 1183#endif /* not DOS_NT */
68936329
JB
1184
1185#endif
1186#endif
1187#endif
1188
1189 /* Suivant - Do we have to get struct ltchars data? */
50b8cf60 1190#ifdef HAVE_LTCHARS
68936329
JB
1191 if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
1192 return -1;
1193#endif
1194
1195 /* How about a struct tchars and a wordful of lmode bits? */
50b8cf60 1196#ifdef HAVE_TCHARS
68936329
JB
1197 if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
1198 || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
1199 return -1;
1200#endif
177c0ea7 1201
68936329
JB
1202 /* We have survived the tempest. */
1203 return 0;
1204}
1205
1206\f
86a5659e
JB
1207
1208#ifdef F_SETOWN
819b8f00 1209int old_fcntl_owner[MAXDESC];
86a5659e
JB
1210#endif /* F_SETOWN */
1211
86a5659e
JB
1212/* This may also be defined in stdio,
1213 but if so, this does no harm,
1214 and using the same name avoids wasting the other one's space. */
1215
9d9a7716 1216#if defined (USG)
86a5659e
JB
1217unsigned char _sobuf[BUFSIZ+8];
1218#else
1219char _sobuf[BUFSIZ];
1220#endif
177c0ea7 1221
50b8cf60 1222#ifdef HAVE_LTCHARS
86a5659e
JB
1223static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
1224#endif
50b8cf60 1225#ifdef HAVE_TCHARS
4ec5cb58 1226static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
177c0ea7 1227#endif
86a5659e 1228
da8e1115
KL
1229/* Initialize the terminal mode on all tty devices that are currently
1230 open. */
1231
08633194 1232void
28d440ab
KL
1233init_all_sys_modes (void)
1234{
28d7d09f 1235 struct tty_display_info *tty;
16c290d8 1236 for (tty = tty_list; tty; tty = tty->next)
28d440ab 1237 init_sys_modes (tty);
28d440ab
KL
1238}
1239
da8e1115
KL
1240/* Initialize the terminal mode on the given tty device. */
1241
28d440ab 1242void
9628b887 1243init_sys_modes (tty_out)
28d7d09f 1244 struct tty_display_info *tty_out;
86a5659e 1245{
91bac16a
JB
1246 struct emacs_tty tty;
1247
4ec5cb58
RS
1248 Vtty_erase_char = Qnil;
1249
86a5659e
JB
1250 if (noninteractive)
1251 return;
1252
0b0d3e0b
KL
1253 if (!tty_out->output)
1254 return; /* The tty is suspended. */
1255
86a5659e
JB
1256#ifdef VMS
1257 if (!input_ef)
1258 input_ef = get_kbd_event_flag ();
1259 /* LIB$GET_EF (&input_ef); */
1260 SYS$CLREF (input_ef);
1261 waiting_for_ast = 0;
1262 if (!timer_ef)
1263 timer_ef = get_timer_event_flag ();
1264 /* LIB$GET_EF (&timer_ef); */
1265 SYS$CLREF (timer_ef);
86a5659e 1266 if (input_ef / 32 != timer_ef / 32)
210b2b4f 1267 croak ("Input and timer event flags in different clusters.");
86a5659e
JB
1268 timer_eflist = ((unsigned) 1 << (input_ef % 32)) |
1269 ((unsigned) 1 << (timer_ef % 32));
86a5659e
JB
1270#ifndef VMS4_4
1271 sys_access_reinit ();
1272#endif
9628b887 1273#endif /* VMS */
91bac16a 1274
0ba73609 1275#ifdef BSD_PGRPS
428a555e
KL
1276#if 0
1277 /* read_socket_hook is not global anymore. I think doing this
1278 unconditionally will not cause any problems. */
2246281f 1279 if (! read_socket_hook && EQ (Vinitial_window_system, Qnil))
428a555e 1280#endif
0b0d3e0b 1281 narrow_foreground_group (fileno (tty_out->input));
9ae8f997
JB
1282#endif
1283
2246281f
KL
1284 if (! tty_out->old_tty)
1285 tty_out->old_tty = (struct emacs_tty *) xmalloc (sizeof (struct emacs_tty));
fca177d4 1286
0b0d3e0b 1287 EMACS_GET_TTY (fileno (tty_out->input), tty_out->old_tty);
23dab951 1288
2246281f 1289 tty = *tty_out->old_tty;
86a5659e 1290
31be8d24 1291#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
2246281f 1292 XSETINT (Vtty_erase_char, tty.main.c_cc[VERASE]);
4ec5cb58 1293
2246281f
KL
1294 tty.main.c_iflag |= (IGNBRK); /* Ignore break condition */
1295 tty.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
7f371164
RS
1296#ifdef INLCR /* I'm just being cautious,
1297 since I can't check how widespread INLCR is--rms. */
2246281f 1298 tty.main.c_iflag &= ~INLCR; /* Disable map of NL to CR on input */
7f371164 1299#endif
86a5659e 1300#ifdef ISTRIP
2246281f 1301 tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
86a5659e 1302#endif
2246281f
KL
1303 tty.main.c_lflag &= ~ECHO; /* Disable echo */
1304 tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */
e2b40c23 1305#ifdef IEXTEN
2246281f 1306 tty.main.c_lflag &= ~IEXTEN; /* Disable other editing characters. */
e2b40c23 1307#endif
2246281f
KL
1308 tty.main.c_lflag |= ISIG; /* Enable signals */
1309 if (tty_out->flow_control)
1310 {
1311 tty.main.c_iflag |= IXON; /* Enable start/stop output control */
86a5659e 1312#ifdef IXANY
2246281f 1313 tty.main.c_iflag &= ~IXANY;
86a5659e 1314#endif /* IXANY */
2246281f
KL
1315 }
1316 else
1317 tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */
1318 tty.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL
1319 on output */
1320 tty.main.c_oflag &= ~TAB3; /* Disable tab expansion */
86a5659e 1321#ifdef CS8
2246281f
KL
1322 if (tty_out->meta_key)
1323 {
1324 tty.main.c_cflag |= CS8; /* allow 8th bit on input */
1325 tty.main.c_cflag &= ~PARENB;/* Don't check parity */
1326 }
86a5659e 1327#endif
2246281f
KL
1328 if (tty_out->input == stdin)
1329 {
1330 tty.main.c_cc[VINTR] = quit_char; /* C-g (usually) gives SIGINT */
1331 /* Set up C-g for both SIGQUIT and SIGINT.
1332 We don't know which we will get, but we handle both alike
1333 so which one it really gives us does not matter. */
1334 tty.main.c_cc[VQUIT] = quit_char;
1335 }
1336 else
1337 {
1338 /* We normally don't get interrupt or quit signals from tty
1339 devices other than our controlling terminal; therefore,
1340 we must handle C-g as normal input. Unfortunately, this
1341 means that the interrupt and quit feature must be
1342 disabled on secondary ttys, or we would not even see the
1343 keypress.
1344
1345 Note that even though emacsclient could have special code
1346 to pass SIGINT to Emacs, we should _not_ enable
1347 interrupt/quit keys for emacsclient frames. This means
1348 that we can't break out of loops in C code from a
1349 secondary tty frame, but we can always decide what
1350 display the C-g came from, which is more important from a
1351 usability point of view. (Consider the case when two
1352 people work together using the same Emacs instance.) */
1353 tty.main.c_cc[VINTR] = CDISABLE;
1354 tty.main.c_cc[VQUIT] = CDISABLE;
1355 }
1356 tty.main.c_cc[VMIN] = 1; /* Input should wait for at least 1 char */
1357 tty.main.c_cc[VTIME] = 0; /* no matter how long that takes. */
86a5659e 1358#ifdef VSWTCH
2246281f 1359 tty.main.c_cc[VSWTCH] = CDISABLE; /* Turn off shell layering use
91bac16a 1360 of C-z */
86a5659e 1361#endif /* VSWTCH */
2246281f 1362
b8ff87cc 1363#if defined (__mips__) || defined (HAVE_TCATTR)
86a5659e 1364#ifdef VSUSP
2246281f 1365 tty.main.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z. */
86a5659e
JB
1366#endif /* VSUSP */
1367#ifdef V_DSUSP
2246281f 1368 tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y. */
86a5659e 1369#endif /* V_DSUSP */
e2b40c23 1370#ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
2246281f 1371 tty.main.c_cc[VDSUSP] = CDISABLE;
e2b40c23 1372#endif /* VDSUSP */
92c995de 1373#ifdef VLNEXT
2246281f 1374 tty.main.c_cc[VLNEXT] = CDISABLE;
92c995de
RS
1375#endif /* VLNEXT */
1376#ifdef VREPRINT
2246281f 1377 tty.main.c_cc[VREPRINT] = CDISABLE;
92c995de
RS
1378#endif /* VREPRINT */
1379#ifdef VWERASE
2246281f 1380 tty.main.c_cc[VWERASE] = CDISABLE;
92c995de
RS
1381#endif /* VWERASE */
1382#ifdef VDISCARD
2246281f 1383 tty.main.c_cc[VDISCARD] = CDISABLE;
92c995de 1384#endif /* VDISCARD */
c179a6d1 1385
2246281f
KL
1386 if (tty_out->flow_control)
1387 {
421dd92f 1388#ifdef VSTART
2246281f 1389 tty.main.c_cc[VSTART] = '\021';
421dd92f
RS
1390#endif /* VSTART */
1391#ifdef VSTOP
2246281f 1392 tty.main.c_cc[VSTOP] = '\023';
421dd92f 1393#endif /* VSTOP */
2246281f
KL
1394 }
1395 else
1396 {
c179a6d1 1397#ifdef VSTART
2246281f 1398 tty.main.c_cc[VSTART] = CDISABLE;
c179a6d1
RS
1399#endif /* VSTART */
1400#ifdef VSTOP
2246281f 1401 tty.main.c_cc[VSTOP] = CDISABLE;
c179a6d1 1402#endif /* VSTOP */
2246281f 1403 }
86a5659e 1404#endif /* mips or HAVE_TCATTR */
c179a6d1 1405
86a5659e
JB
1406#ifdef AIX
1407#ifndef IBMR2AIX
2246281f
KL
1408 /* AIX enhanced edit loses NULs, so disable it. */
1409 tty.main.c_line = 0;
1410 tty.main.c_iflag &= ~ASCEDIT;
86a5659e 1411#else
f5272227
KL
1412 tty.main.c_cc[VSTRT] = CDISABLE;
1413 tty.main.c_cc[VSTOP] = CDISABLE;
1414 tty.main.c_cc[VSUSP] = CDISABLE;
1415 tty.main.c_cc[VDSUSP] = CDISABLE;
86a5659e 1416#endif /* IBMR2AIX */
2246281f
KL
1417 if (tty_out->flow_control)
1418 {
ac567c95 1419#ifdef VSTART
2246281f 1420 tty.main.c_cc[VSTART] = '\021';
ac567c95
RS
1421#endif /* VSTART */
1422#ifdef VSTOP
2246281f 1423 tty.main.c_cc[VSTOP] = '\023';
ac567c95 1424#endif /* VSTOP */
2246281f
KL
1425 }
1426 /* Also, PTY overloads NUL and BREAK.
1427 don't ignore break, but don't signal either, so it looks like NUL.
1428 This really serves a purpose only if running in an XTERM window
1429 or via TELNET or the like, but does no harm elsewhere. */
1430 tty.main.c_iflag &= ~IGNBRK;
1431 tty.main.c_iflag &= ~BRKINT;
86a5659e
JB
1432#endif
1433#else /* if not HAVE_TERMIO */
1434#ifdef VMS
2246281f
KL
1435 tty.main.tt_char |= TT$M_NOECHO;
1436 if (meta_key)
1437 tty.main.tt_char |= TT$M_EIGHTBIT;
1438 if (tty_out->flow_control)
1439 tty.main.tt_char |= TT$M_TTSYNC;
1440 else
1441 tty.main.tt_char &= ~TT$M_TTSYNC;
1442 tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
86a5659e 1443#else /* not VMS (BSD, that is) */
fe03522b 1444#ifndef DOS_NT
2246281f
KL
1445 XSETINT (Vtty_erase_char, tty.main.sg_erase);
1446 tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
1447 if (meta_key)
1448 tty.main.sg_flags |= ANYP;
1449 tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
fe03522b 1450#endif /* not DOS_NT */
86a5659e
JB
1451#endif /* not VMS (BSD, that is) */
1452#endif /* not HAVE_TERMIO */
1453
2246281f
KL
1454 /* If going to use CBREAK mode, we must request C-g to interrupt
1455 and turn off start and stop chars, etc. If not going to use
1456 CBREAK mode, do this anyway so as to turn off local flow
1457 control for user coming over network on 4.2; in this case,
1458 only t_stopc and t_startc really matter. */
91bac16a 1459#ifndef HAVE_TERMIO
50b8cf60 1460#ifdef HAVE_TCHARS
2246281f
KL
1461 /* Note: if not using CBREAK mode, it makes no difference how we
1462 set this */
1463 tty.tchars = new_tchars;
1464 tty.tchars.t_intrc = quit_char;
1465 if (tty_out->flow_control)
1466 {
1467 tty.tchars.t_startc = '\021';
1468 tty.tchars.t_stopc = '\023';
1469 }
1470
1471 tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | tty_out->old_tty.lmode;
2246281f 1472
50b8cf60 1473#endif /* HAVE_TCHARS */
91bac16a
JB
1474#endif /* not HAVE_TERMIO */
1475
50b8cf60 1476#ifdef HAVE_LTCHARS
2246281f 1477 tty.ltchars = new_ltchars;
50b8cf60 1478#endif /* HAVE_LTCHARS */
207bdbdb 1479#ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
2246281f
KL
1480 if (!tty_out->term_initted)
1481 internal_terminal_init ();
1482 dos_ttraw ();
207bdbdb 1483#endif
91bac16a 1484
0b0d3e0b 1485 EMACS_SET_TTY (fileno (tty_out->input), &tty, 0);
86a5659e 1486
2246281f
KL
1487 /* This code added to insure that, if flow-control is not to be used,
1488 we have an unlocked terminal at the start. */
91bac16a 1489
86a5659e 1490#ifdef TCXONC
0b0d3e0b 1491 if (!tty_out->flow_control) ioctl (fileno (tty_out->input), TCXONC, 1);
86a5659e 1492#endif
86a5659e 1493#ifdef TIOCSTART
0b0d3e0b 1494 if (!tty_out->flow_control) ioctl (fileno (tty_out->input), TIOCSTART, 0);
86a5659e 1495#endif
86a5659e 1496
f2a77c3a 1497#if defined (HAVE_TERMIOS) || defined (HPUX)
51417996 1498#ifdef TCOON
0b0d3e0b 1499 if (!tty_out->flow_control) tcflow (fileno (tty_out->input), TCOON);
d228f207 1500#endif
51417996 1501#endif
d228f207 1502
86a5659e
JB
1503#ifdef VMS
1504/* Appears to do nothing when in PASTHRU mode.
0b0d3e0b 1505 SYS$QIOW (0, fileno (tty_out->input), IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
86a5659e
JB
1506 interrupt_signal, oob_chars, 0, 0, 0, 0);
1507*/
2246281f 1508 queue_kbd_input (0);
86a5659e 1509#endif /* VMS */
86a5659e
JB
1510
1511#ifdef F_SETFL
eb8c3be9 1512#ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
2246281f 1513 if (interrupt_input)
86a5659e 1514 {
0b0d3e0b
KL
1515 old_fcntl_owner[fileno (tty_out->input)] =
1516 fcntl (fileno (tty_out->input), F_GETOWN, 0);
1517 fcntl (fileno (tty_out->input), F_SETOWN, getpid ());
1518 init_sigio (fileno (tty_out->input));
7e5a23bd 1519#ifdef HAVE_GPM
75a8734b 1520 if (gpm_tty == tty_out)
3e748bcb 1521 {
75a8734b 1522 /* Arrange for mouse events to give us SIGIO signals. */
3e748bcb 1523 fcntl (gpm_fd, F_SETOWN, getpid ());
57669cf1 1524 fcntl (gpm_fd, F_SETFL, fcntl (gpm_fd, F_GETFL, 0) | O_NONBLOCK);
3e748bcb
NR
1525 init_sigio (gpm_fd);
1526 }
7e5a23bd 1527#endif /* HAVE_GPM */
86a5659e
JB
1528 }
1529#endif /* F_GETOWN */
1530#endif /* F_SETFL */
1531
86a5659e
JB
1532#ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1533#undef _IOFBF
1534#endif
1535#ifdef _IOFBF
1536 /* This symbol is defined on recent USG systems.
1537 Someone says without this call USG won't really buffer the file
1538 even with a call to setbuf. */
0b0d3e0b 1539 setvbuf (tty_out->output, (char *) _sobuf, _IOFBF, sizeof _sobuf);
86a5659e 1540#else
0b0d3e0b 1541 setbuf (tty_out->output, (char *) _sobuf);
86a5659e 1542#endif
4d553a13 1543
69fb5031
JR
1544 if (tty_out->terminal->set_terminal_modes_hook)
1545 tty_out->terminal->set_terminal_modes_hook (tty_out->terminal);
4b311aaf 1546
9628b887
KL
1547 if (!tty_out->term_initted)
1548 {
1549 Lisp_Object tail, frame;
1550 FOR_EACH_FRAME (tail, frame)
1551 {
daf01701 1552 /* XXX This needs to be revised. */
9628b887
KL
1553 if (FRAME_TERMCAP_P (XFRAME (frame))
1554 && FRAME_TTY (XFRAME (frame)) == tty_out)
1555 init_frame_faces (XFRAME (frame));
1556 }
1557 }
045942b2 1558
9628b887 1559 if (tty_out->term_initted && no_redraw_on_reenter)
86a5659e 1560 {
1fb8c4ad 1561 /* XXX This seems wrong on multi-tty. */
86a5659e
JB
1562 if (display_completed)
1563 direct_output_forward_char (0);
1564 }
1565 else
1566 {
9628b887 1567 Lisp_Object tail, frame;
0137dbf7 1568 frame_garbaged = 1;
9628b887
KL
1569 FOR_EACH_FRAME (tail, frame)
1570 {
1571 if (FRAME_TERMCAP_P (XFRAME (frame))
1572 && FRAME_TTY (XFRAME (frame)) == tty_out)
1573 FRAME_GARBAGED_P (XFRAME (frame)) = 1;
1574 }
86a5659e 1575 }
91bac16a 1576
9628b887 1577 tty_out->term_initted = 1;
86a5659e
JB
1578}
1579
1580/* Return nonzero if safe to use tabs in output.
1581 At the time this is called, init_sys_modes has not been done yet. */
177c0ea7 1582
dfcf069d 1583int
16c290d8 1584tabs_safe_p (int fd)
86a5659e 1585{
6548cf00 1586 struct emacs_tty etty;
91bac16a 1587
16c290d8 1588 EMACS_GET_TTY (fd, &etty);
6548cf00 1589 return EMACS_TTY_TABS_OK (&etty);
86a5659e 1590}
73d5358f 1591\f
86a5659e 1592/* Get terminal size from system.
73d5358f
RS
1593 Store number of lines into *HEIGHTP and width into *WIDTHP.
1594 We store 0 if there's no valid information. */
86a5659e 1595
08633194 1596void
16c290d8 1597get_tty_size (int fd, int *widthp, int *heightp)
86a5659e 1598{
86a5659e 1599
86a5659e 1600#ifdef TIOCGWINSZ
91bac16a
JB
1601
1602 /* BSD-style. */
86a5659e 1603 struct winsize size;
91bac16a 1604
16c290d8 1605 if (ioctl (fd, TIOCGWINSZ, &size) == -1)
91bac16a
JB
1606 *widthp = *heightp = 0;
1607 else
1608 {
1609 *widthp = size.ws_col;
1610 *heightp = size.ws_row;
1611 }
1612
1613#else
1614#ifdef TIOCGSIZE
1615
1616 /* SunOS - style. */
177c0ea7 1617 struct ttysize size;
91bac16a 1618
16c290d8 1619 if (ioctl (fd, TIOCGSIZE, &size) == -1)
91bac16a
JB
1620 *widthp = *heightp = 0;
1621 else
1622 {
1623 *widthp = size.ts_cols;
1624 *heightp = size.ts_lines;
1625 }
1626
1627#else
86a5659e 1628#ifdef VMS
91bac16a 1629
a72c2d97
TTN
1630 /* Use a fresh channel since the current one may have stale info
1631 (for example, from prior to a suspend); and to avoid a dependency
1632 in the init sequence. */
1633 int chan;
91bac16a 1634 struct sensemode tty;
177c0ea7 1635
a72c2d97
TTN
1636 SYS$ASSIGN (&input_dsc, &chan, 0, 0);
1637 SYS$QIOW (0, chan, IO$_SENSEMODE, &tty, 0, 0,
1638 &tty.class, 12, 0, 0, 0, 0);
1639 SYS$DASSGN (chan);
86a5659e
JB
1640 *widthp = tty.scr_wid;
1641 *heightp = tty.scr_len;
91bac16a 1642
207bdbdb
RS
1643#else
1644#ifdef MSDOS
1645 *widthp = ScreenCols ();
1646 *heightp = ScreenRows ();
86a5659e
JB
1647#else /* system doesn't know size */
1648 *widthp = 0;
1649 *heightp = 0;
207bdbdb 1650#endif
91bac16a
JB
1651#endif /* not VMS */
1652#endif /* not SunOS-style */
1653#endif /* not BSD-style */
86a5659e 1654}
91bac16a 1655
73d5358f
RS
1656/* Set the logical window size associated with descriptor FD
1657 to HEIGHT and WIDTH. This is used mainly with ptys. */
1658
1659int
1660set_window_size (fd, height, width)
1661 int fd, height, width;
1662{
1663#ifdef TIOCSWINSZ
1664
1665 /* BSD-style. */
1666 struct winsize size;
1667 size.ws_row = height;
1668 size.ws_col = width;
1669
1670 if (ioctl (fd, TIOCSWINSZ, &size) == -1)
1671 return 0; /* error */
1672 else
1673 return 1;
1674
1675#else
1676#ifdef TIOCSSIZE
1677
1678 /* SunOS - style. */
177c0ea7 1679 struct ttysize size;
73d5358f
RS
1680 size.ts_lines = height;
1681 size.ts_cols = width;
1682
1683 if (ioctl (fd, TIOCGSIZE, &size) == -1)
1684 return 0;
1685 else
1686 return 1;
1687#else
1688 return -1;
1689#endif /* not SunOS-style */
1690#endif /* not BSD-style */
1691}
1692
86a5659e 1693\f
da8e1115
KL
1694
1695/* Prepare all terminal devices for exiting Emacs. */
1696
28d440ab
KL
1697void
1698reset_all_sys_modes (void)
1699{
28d7d09f 1700 struct tty_display_info *tty;
16c290d8 1701 for (tty = tty_list; tty; tty = tty->next)
28d440ab 1702 reset_sys_modes (tty);
28d440ab
KL
1703}
1704
0a125897 1705/* Prepare the terminal for closing it; move the cursor to the
0137dbf7 1706 bottom of the frame, turn off interrupt-driven I/O, etc. */
da8e1115 1707
08633194 1708void
9628b887 1709reset_sys_modes (tty_out)
28d7d09f 1710 struct tty_display_info *tty_out;
86a5659e
JB
1711{
1712 if (noninteractive)
1713 {
1714 fflush (stdout);
1715 return;
1716 }
9628b887 1717 if (!tty_out->term_initted)
86a5659e 1718 return;
da8e1115 1719
0b0d3e0b
KL
1720 if (!tty_out->output)
1721 return; /* The tty is suspended. */
1722
da8e1115
KL
1723 /* Go to and clear the last line of the terminal. */
1724
0a125897 1725 cmgoto (tty_out, FrameRows (tty_out) - 1, 0);
0b0d3e0b 1726
da8e1115
KL
1727 /* Code adapted from tty_clear_end_of_line. */
1728 if (tty_out->TS_clr_line)
1729 {
1730 emacs_tputs (tty_out, tty_out->TS_clr_line, 1, cmputc);
1731 }
1732 else
1733 { /* have to do it the hard way */
1734 int i;
ed8dad6b 1735 tty_turn_off_insert (tty_out);
0b0d3e0b 1736
da8e1115 1737 for (i = curX (tty_out); i < FrameCols (tty_out) - 1; i++)
0b0d3e0b
KL
1738 {
1739 fputc (' ', tty_out->output);
1740 }
da8e1115 1741 }
0b0d3e0b 1742
0a125897 1743 cmgoto (tty_out, FrameRows (tty_out) - 1, 0);
819b8f00
KL
1744 fflush (tty_out->output);
1745
69fb5031
JR
1746 if (tty_out->terminal->reset_terminal_modes_hook)
1747 tty_out->terminal->reset_terminal_modes_hook (tty_out->terminal);
2f98e6e3 1748
2a633456 1749#ifdef BSD_SYSTEM
86a5659e 1750 /* Avoid possible loss of output when changing terminal modes. */
0b0d3e0b 1751 fsync (fileno (tty_out->output));
86a5659e 1752#endif
91bac16a 1753
86a5659e 1754#ifdef F_SETFL
eb8c3be9 1755#ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
86a5659e
JB
1756 if (interrupt_input)
1757 {
0b0d3e0b
KL
1758 reset_sigio (fileno (tty_out->input));
1759 fcntl (fileno (tty_out->input), F_SETOWN,
1760 old_fcntl_owner[fileno (tty_out->input)]);
86a5659e
JB
1761 }
1762#endif /* F_SETOWN */
a6b00318 1763#ifdef O_NDELAY
0b0d3e0b
KL
1764 fcntl (fileno (tty_out->input), F_SETFL,
1765 fcntl (fileno (tty_out->input), F_GETFL, 0) & ~O_NDELAY);
a6b00318 1766#endif
86a5659e 1767#endif /* F_SETFL */
91bac16a 1768
fca177d4 1769 if (tty_out->old_tty)
0b0d3e0b 1770 while (EMACS_SET_TTY (fileno (tty_out->input),
fca177d4 1771 tty_out->old_tty, 0) < 0 && errno == EINTR)
7e32a4fb 1772 ;
86a5659e 1773
207bdbdb
RS
1774#ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1775 dos_ttcooked ();
1776#endif
1777
0ba73609 1778#ifdef BSD_PGRPS
0b0d3e0b 1779 widen_foreground_group (fileno (tty_out->input));
9ae8f997 1780#endif
86a5659e
JB
1781}
1782\f
1783#ifdef HAVE_PTYS
1784
1785/* Set up the proper status flags for use of a pty. */
1786
08633194 1787void
86a5659e
JB
1788setup_pty (fd)
1789 int fd;
1790{
1791 /* I'm told that TOICREMOTE does not mean control chars
1792 "can't be sent" but rather that they don't have
1793 input-editing or signaling effects.
1794 That should be good, because we have other ways
1795 to do those things in Emacs.
1796 However, telnet mode seems not to work on 4.2.
1797 So TIOCREMOTE is turned off now. */
1798
1799 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1800 will hang. In particular, the "timeout" feature (which
1801 causes a read to return if there is no data available)
1802 does this. Also it is known that telnet mode will hang
1803 in such a way that Emacs must be stopped (perhaps this
1804 is the same problem).
177c0ea7 1805
86a5659e
JB
1806 If TIOCREMOTE is turned off, then there is a bug in
1807 hp-ux which sometimes loses data. Apparently the
1808 code which blocks the master process when the internal
1809 buffer fills up does not work. Other than this,
1810 though, everything else seems to work fine.
177c0ea7 1811
86a5659e
JB
1812 Since the latter lossage is more benign, we may as well
1813 lose that way. -- cph */
1814#ifdef FIONBIO
bdee2ef3 1815#if defined(UNIX98_PTYS)
86a5659e
JB
1816 {
1817 int on = 1;
1818 ioctl (fd, FIONBIO, &on);
1819 }
1820#endif
1821#endif
86a5659e
JB
1822}
1823#endif /* HAVE_PTYS */
1824\f
1825#ifdef VMS
1826
1827/* Assigning an input channel is done at the start of Emacs execution.
1828 This is called each time Emacs is resumed, also, but does nothing
1829 because input_chain is no longer zero. */
1830
dfcf069d 1831void
86a5659e
JB
1832init_vms_input ()
1833{
1834 int status;
177c0ea7 1835
0b0d3e0b 1836 if (fileno (CURTTY ()->input)) == 0)
86a5659e 1837 {
0b0d3e0b 1838 status = SYS$ASSIGN (&input_dsc, &fileno (CURTTY ()->input)), 0, 0);
86a5659e
JB
1839 if (! (status & 1))
1840 LIB$STOP (status);
1841 }
1842}
1843
1844/* Deassigning the input channel is done before exiting. */
1845
dfcf069d 1846void
86a5659e
JB
1847stop_vms_input ()
1848{
0b0d3e0b 1849 return SYS$DASSGN (fileno (CURTTY ()->input)));
86a5659e
JB
1850}
1851
1852short input_buffer;
1853
1854/* Request reading one character into the keyboard buffer.
1855 This is done as soon as the buffer becomes empty. */
1856
dfcf069d 1857void
86a5659e
JB
1858queue_kbd_input ()
1859{
1860 int status;
210b2b4f
JB
1861 extern kbd_input_ast ();
1862
86a5659e
JB
1863 waiting_for_ast = 0;
1864 stop_input = 0;
0b0d3e0b 1865 status = SYS$QIO (0, fileno (CURTTY()->input), IO$_READVBLK,
86a5659e
JB
1866 &input_iosb, kbd_input_ast, 1,
1867 &input_buffer, 1, 0, terminator_mask, 0, 0);
1868}
1869
1870int input_count;
1871
1872/* Ast routine that is called when keyboard input comes in
1873 in accord with the SYS$QIO above. */
1874
dfcf069d 1875void
86a5659e
JB
1876kbd_input_ast ()
1877{
1878 register int c = -1;
1879 int old_errno = errno;
ffd56f97 1880 extern EMACS_TIME *input_available_clear_time;
86a5659e
JB
1881
1882 if (waiting_for_ast)
1883 SYS$SETEF (input_ef);
1884 waiting_for_ast = 0;
1885 input_count++;
1886#ifdef ASTDEBUG
1887 if (input_count == 25)
1888 exit (1);
1889 printf ("Ast # %d,", input_count);
1890 printf (" iosb = %x, %x, %x, %x",
1891 input_iosb.offset, input_iosb.status, input_iosb.termlen,
1892 input_iosb.term);
1893#endif
1894 if (input_iosb.offset)
1895 {
1896 c = input_buffer;
1897#ifdef ASTDEBUG
1898 printf (", char = 0%o", c);
1899#endif
1900 }
1901#ifdef ASTDEBUG
1902 printf ("\n");
1903 fflush (stdout);
1904 sleep (1);
1905#endif
1906 if (! stop_input)
1907 queue_kbd_input ();
1908 if (c >= 0)
1909 {
1910 struct input_event e;
50acda85
SM
1911 EVENT_INIT (e);
1912
e43bd4f5 1913 e.kind = ASCII_KEYSTROKE_EVENT;
c81d47b4 1914 XSETINT (e.code, c);
2d064114 1915 e.frame_or_window = selected_frame;
86a5659e
JB
1916 kbd_buffer_store_event (&e);
1917 }
ffd56f97
JB
1918 if (input_available_clear_time)
1919 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
86a5659e
JB
1920 errno = old_errno;
1921}
1922
1923/* Wait until there is something in kbd_buffer. */
1924
dfcf069d 1925void
86a5659e
JB
1926wait_for_kbd_input ()
1927{
1928 extern int have_process_input, process_exited;
1929
1930 /* If already something, avoid doing system calls. */
1931 if (detect_input_pending ())
1932 {
1933 return;
1934 }
1935 /* Clear a flag, and tell ast routine above to set it. */
1936 SYS$CLREF (input_ef);
1937 waiting_for_ast = 1;
1938 /* Check for timing error: ast happened while we were doing that. */
1939 if (!detect_input_pending ())
1940 {
1941 /* No timing error: wait for flag to be set. */
1942 set_waiting_for_input (0);
1943 SYS$WFLOR (input_ef, input_eflist);
61851c0e 1944 clear_waiting_for_input ();
86a5659e
JB
1945 if (!detect_input_pending ())
1946 /* Check for subprocess input availability */
1947 {
1948 int dsp = have_process_input || process_exited;
1949
1950 SYS$CLREF (process_ef);
1951 if (have_process_input)
1952 process_command_input ();
1953 if (process_exited)
1954 process_exit ();
1955 if (dsp)
1956 {
1957 update_mode_lines++;
56b18525 1958 prepare_menu_bars ();
3007ebfb 1959 redisplay_preserve_echo_area (18);
86a5659e
JB
1960 }
1961 }
1962 }
1963 waiting_for_ast = 0;
1964}
1965
1966/* Get rid of any pending QIO, when we are about to suspend
1967 or when we want to throw away pending input.
1968 We wait for a positive sign that the AST routine has run
1969 and therefore there is no I/O request queued when we return.
1970 SYS$SETAST is used to avoid a timing error. */
1971
dfcf069d 1972void
86a5659e
JB
1973end_kbd_input ()
1974{
1975#ifdef ASTDEBUG
1976 printf ("At end_kbd_input.\n");
1977 fflush (stdout);
1978 sleep (1);
1979#endif
1980 if (LIB$AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
1981 {
0b0d3e0b 1982 SYS$CANCEL (fileno (CURTTY()->input));
86a5659e
JB
1983 return;
1984 }
1985
1986 SYS$SETAST (0);
1987 /* Clear a flag, and tell ast routine above to set it. */
1988 SYS$CLREF (input_ef);
1989 waiting_for_ast = 1;
1990 stop_input = 1;
0b0d3e0b 1991 SYS$CANCEL (fileno (CURTTY()->input));
86a5659e
JB
1992 SYS$SETAST (1);
1993 SYS$WAITFR (input_ef);
1994 waiting_for_ast = 0;
1995}
1996
1997/* Wait for either input available or time interval expiry. */
1998
dfcf069d 1999void
86a5659e
JB
2000input_wait_timeout (timeval)
2001 int timeval; /* Time to wait, in seconds */
2002{
2003 int time [2];
2004 static int zero = 0;
2005 static int large = -10000000;
2006
2007 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
2008
2009 /* If already something, avoid doing system calls. */
2010 if (detect_input_pending ())
2011 {
2012 return;
2013 }
2014 /* Clear a flag, and tell ast routine above to set it. */
2015 SYS$CLREF (input_ef);
2016 waiting_for_ast = 1;
2017 /* Check for timing error: ast happened while we were doing that. */
2018 if (!detect_input_pending ())
2019 {
2020 /* No timing error: wait for flag to be set. */
2021 SYS$CANTIM (1, 0);
2022 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
2023 SYS$WFLOR (timer_ef, timer_eflist); /* Wait for timer expiry or input */
2024 }
2025 waiting_for_ast = 0;
2026}
2027
2028/* The standard `sleep' routine works some other way
2029 and it stops working if you have ever quit out of it.
2030 This one continues to work. */
2031
2032sys_sleep (timeval)
2033 int timeval;
2034{
2035 int time [2];
2036 static int zero = 0;
2037 static int large = -10000000;
177c0ea7 2038
86a5659e
JB
2039 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
2040
2041 SYS$CANTIM (1, 0);
2042 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
2043 SYS$WAITFR (timer_ef); /* Wait for timer expiry only */
2044}
2045
08633194 2046void
23dab951
RS
2047init_sigio (fd)
2048 int fd;
86a5659e
JB
2049{
2050 request_sigio ();
2051}
2052
0a125897
KL
2053reset_sigio (fd)
2054 int fd;
86a5659e
JB
2055{
2056 unrequest_sigio ();
2057}
2058
08633194 2059void
86a5659e
JB
2060request_sigio ()
2061{
6e5b2385
AS
2062 if (noninteractive)
2063 return;
86a5659e
JB
2064 croak ("request sigio");
2065}
2066
08633194 2067void
86a5659e
JB
2068unrequest_sigio ()
2069{
6e5b2385
AS
2070 if (noninteractive)
2071 return;
86a5659e
JB
2072 croak ("unrequest sigio");
2073}
2074
2075#endif /* VMS */
2076\f
2077/* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
2078#ifndef CANNOT_DUMP
2079#define NEED_STARTS
2080#endif
2081
2082#ifndef SYSTEM_MALLOC
2083#ifndef NEED_STARTS
2084#define NEED_STARTS
2085#endif
2086#endif
2087
2088#ifdef NEED_STARTS
2089/* Some systems that cannot dump also cannot implement these. */
2090
2091/*
2092 * Return the address of the start of the text segment prior to
2093 * doing an unexec. After unexec the return value is undefined.
2094 * See crt0.c for further explanation and _start.
2095 *
2096 */
2097
c0c9ac48 2098#if !(defined (__NetBSD__) && defined (__ELF__))
260fe597 2099#ifndef HAVE_TEXT_START
86a5659e
JB
2100char *
2101start_of_text ()
2102{
2103#ifdef TEXT_START
2104 return ((char *) TEXT_START);
2105#else
86a5659e
JB
2106 extern int _start ();
2107 return ((char *) _start);
86a5659e
JB
2108#endif /* TEXT_START */
2109}
260fe597 2110#endif /* not HAVE_TEXT_START */
c0c9ac48 2111#endif
86a5659e
JB
2112
2113/*
2114 * Return the address of the start of the data segment prior to
2115 * doing an unexec. After unexec the return value is undefined.
2116 * See crt0.c for further information and definition of data_start.
2117 *
2118 * Apparently, on BSD systems this is etext at startup. On
2119 * USG systems (swapping) this is highly mmu dependent and
2120 * is also dependent on whether or not the program is running
2121 * with shared text. Generally there is a (possibly large)
2122 * gap between end of text and start of data with shared text.
2123 *
2124 * On Uniplus+ systems with shared text, data starts at a
2125 * fixed address. Each port (from a given oem) is generally
2126 * different, and the specific value of the start of data can
2127 * be obtained via the UniPlus+ specific "uvar" system call,
2128 * however the method outlined in crt0.c seems to be more portable.
2129 *
2130 * Probably what will have to happen when a USG unexec is available,
2131 * at least on UniPlus, is temacs will have to be made unshared so
2132 * that text and data are contiguous. Then once loadup is complete,
2133 * unexec will produce a shared executable where the data can be
ea5a0917 2134 * at the normal shared text boundary and the startofdata variable
86a5659e
JB
2135 * will be patched by unexec to the correct value.
2136 *
2137 */
177c0ea7 2138
be3abfa2 2139#ifndef start_of_data
86a5659e
JB
2140char *
2141start_of_data ()
2142{
2143#ifdef DATA_START
2144 return ((char *) DATA_START);
6c65530f
JB
2145#else
2146#ifdef ORDINARY_LINK
2147 /*
2148 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
2149 * data_start isn't defined. We take the address of environ, which
2150 * is known to live at or near the start of the system crt0.c, and
2151 * we don't sweat the handful of bytes that might lose.
2152 */
2153 extern char **environ;
2154
c4ea52a6 2155 return ((char *) &environ);
86a5659e
JB
2156#else
2157 extern int data_start;
2158 return ((char *) &data_start);
6c65530f
JB
2159#endif /* ORDINARY_LINK */
2160#endif /* DATA_START */
86a5659e 2161}
be3abfa2 2162#endif /* start_of_data */
86a5659e 2163#endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
86a5659e 2164\f
c0c86835
KH
2165/* init_system_name sets up the string for the Lisp function
2166 system-name to return. */
86a5659e 2167
c0c86835 2168extern Lisp_Object Vsystem_name;
86a5659e 2169
f8a80313
RS
2170#ifndef VMS
2171#ifdef HAVE_SOCKETS
2172#include <sys/socket.h>
2173#include <netdb.h>
2174#endif /* HAVE_SOCKETS */
2175#endif /* not VMS */
f8a80313 2176
8581cd64
KH
2177#ifdef TRY_AGAIN
2178#ifndef HAVE_H_ERRNO
2179extern int h_errno;
2180#endif
2181#endif /* TRY_AGAIN */
2182
c0c86835
KH
2183void
2184init_system_name ()
86a5659e 2185{
67004ffb 2186#ifdef VMS
c0c86835
KH
2187 char *sp, *end;
2188 if ((sp = egetenv ("SYS$NODE")) == 0)
2189 Vsystem_name = build_string ("vax-vms");
2190 else if ((end = index (sp, ':')) == 0)
2191 Vsystem_name = build_string (sp);
2192 else
2193 Vsystem_name = make_string (sp, end - sp);
67004ffb 2194#else
210b2b4f 2195#ifndef HAVE_GETHOSTNAME
c0c86835
KH
2196 struct utsname uts;
2197 uname (&uts);
2198 Vsystem_name = build_string (uts.nodename);
67004ffb 2199#else /* HAVE_GETHOSTNAME */
cc6e7269 2200 unsigned int hostname_size = 256;
c0c86835
KH
2201 char *hostname = (char *) alloca (hostname_size);
2202
2203 /* Try to get the host name; if the buffer is too short, try
2204 again. Apparently, the only indication gethostname gives of
2205 whether the buffer was large enough is the presence or absence
2206 of a '\0' in the string. Eech. */
2207 for (;;)
2208 {
2209 gethostname (hostname, hostname_size - 1);
2210 hostname[hostname_size - 1] = '\0';
2211
2212 /* Was the buffer large enough for the '\0'? */
2213 if (strlen (hostname) < hostname_size - 1)
2214 break;
2215
2216 hostname_size <<= 1;
2217 hostname = (char *) alloca (hostname_size);
2218 }
67004ffb 2219#ifdef HAVE_SOCKETS
c0c86835
KH
2220 /* Turn the hostname into the official, fully-qualified hostname.
2221 Don't do this if we're going to dump; this can confuse system
2222 libraries on some machines and make the dumped emacs core dump. */
67004ffb 2223#ifndef CANNOT_DUMP
c0c86835 2224 if (initialized)
67004ffb 2225#endif /* not CANNOT_DUMP */
960d894c
KH
2226 if (! index (hostname, '.'))
2227 {
960d894c 2228 int count;
3d66b985
JD
2229#ifdef HAVE_GETADDRINFO
2230 struct addrinfo *res;
2231 struct addrinfo hints;
2232 int ret;
2233
2234 memset (&hints, 0, sizeof(hints));
2235 hints.ai_socktype = SOCK_STREAM;
2236 hints.ai_flags = AI_CANONNAME;
2237
960d894c
KH
2238 for (count = 0;; count++)
2239 {
3d66b985
JD
2240 if ((ret = getaddrinfo (hostname, NULL, &hints, &res)) == 0
2241 || ret != EAI_AGAIN)
2242 break;
2243
2244 if (count >= 5)
2245 break;
2246 Fsleep_for (make_number (1), Qnil);
2247 }
2248
2249 if (ret == 0)
2250 {
2251 struct addrinfo *it = res;
2252 while (it)
2253 {
2254 char *fqdn = it->ai_canonname;
2255 if (fqdn && index (fqdn, '.')
2256 && strcmp (fqdn, "localhost.localdomain") != 0)
2257 break;
2258 it = it->ai_next;
2259 }
2260 if (it)
2261 {
2262 hostname = alloca (strlen (it->ai_canonname) + 1);
2263 strcpy (hostname, it->ai_canonname);
2264 }
2265 freeaddrinfo (res);
2266 }
2267#else /* !HAVE_GETADDRINFO */
2268 struct hostent *hp;
2269 for (count = 0;; count++)
2270 {
2271
e24f1d55 2272#ifdef TRY_AGAIN
960d894c 2273 h_errno = 0;
e24f1d55 2274#endif
960d894c 2275 hp = gethostbyname (hostname);
efa04277 2276#ifdef TRY_AGAIN
960d894c
KH
2277 if (! (hp == 0 && h_errno == TRY_AGAIN))
2278#endif
3d66b985 2279
960d894c 2280 break;
3d66b985 2281
960d894c
KH
2282 if (count >= 5)
2283 break;
2284 Fsleep_for (make_number (1), Qnil);
2285 }
3d66b985 2286
960d894c
KH
2287 if (hp)
2288 {
2289 char *fqdn = (char *) hp->h_name;
960d894c
KH
2290
2291 if (!index (fqdn, '.'))
2292 {
2293 /* We still don't have a fully qualified domain name.
2294 Try to find one in the list of alternate names */
2295 char **alias = hp->h_aliases;
923721f4
CY
2296 while (*alias
2297 && (!index (*alias, '.')
2298 || !strcmp (*alias, "localhost.localdomain")))
960d894c
KH
2299 alias++;
2300 if (*alias)
2301 fqdn = *alias;
2302 }
2303 hostname = fqdn;
960d894c 2304 }
3d66b985 2305#endif /* !HAVE_GETADDRINFO */
960d894c 2306 }
67004ffb 2307#endif /* HAVE_SOCKETS */
c0c86835 2308 Vsystem_name = build_string (hostname);
67004ffb 2309#endif /* HAVE_GETHOSTNAME */
210b2b4f 2310#endif /* VMS */
c0c86835
KH
2311 {
2312 unsigned char *p;
d5db4077 2313 for (p = SDATA (Vsystem_name); *p; p++)
c0c86835
KH
2314 if (*p == ' ' || *p == '\t')
2315 *p = '-';
2316 }
67004ffb 2317}
86a5659e 2318\f
7964ba9e 2319#ifndef MSDOS
86a5659e 2320#ifndef VMS
4624371d 2321#if !defined (HAVE_SELECT)
86a5659e 2322
86d1f23a
KH
2323#include "sysselect.h"
2324#undef select
2325
2326#if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
86a5659e
JB
2327/* Cause explanatory error message at compile time,
2328 since the select emulation is not good enough for X. */
2329int *x = &x_windows_lose_if_no_select_system_call;
2330#endif
2331
2332/* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2333 * Only checks read descriptors.
2334 */
2335/* How long to wait between checking fds in select */
2336#define SELECT_PAUSE 1
2337int select_alarmed;
2338
2339/* For longjmp'ing back to read_input_waiting. */
2340
2341jmp_buf read_alarm_throw;
2342
2343/* Nonzero if the alarm signal should throw back to read_input_waiting.
2344 The read_socket_hook function sets this to 1 while it is waiting. */
2345
2346int read_alarm_should_throw;
2347
2348SIGTYPE
2349select_alarm ()
2350{
2351 select_alarmed = 1;
86a5659e 2352 signal (SIGALRM, SIG_IGN);
333f1b6f 2353 SIGNAL_THREAD_CHECK (SIGALRM);
86a5659e
JB
2354 if (read_alarm_should_throw)
2355 longjmp (read_alarm_throw, 1);
2356}
2357
9d9f1812 2358#ifndef WINDOWSNT
86a5659e
JB
2359/* Only rfds are checked. */
2360int
86d1f23a 2361sys_select (nfds, rfds, wfds, efds, timeout)
86a5659e 2362 int nfds;
86d1f23a
KH
2363 SELECT_TYPE *rfds, *wfds, *efds;
2364 EMACS_TIME *timeout;
86a5659e 2365{
2246281f
KL
2366 /* XXX This needs to be updated for multi-tty support. Is there
2367 anybody who needs to emulate select these days? */
2368 int ravail = 0;
86d1f23a
KH
2369 SELECT_TYPE orfds;
2370 int timeoutval;
2371 int *local_timeout;
86a5659e
JB
2372 extern int proc_buffered_char[];
2373#ifndef subprocesses
2374 int process_tick = 0, update_tick = 0;
2375#else
2376 extern int process_tick, update_tick;
2377#endif
86a5659e
JB
2378 unsigned char buf;
2379
86d1f23a
KH
2380#if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
2381 /* If we're using X, then the native select will work; we only need the
2382 emulation for non-X usage. */
2246281f 2383 if (!NILP (Vinitial_window_system))
86d1f23a
KH
2384 return select (nfds, rfds, wfds, efds, timeout);
2385#endif
2386 timeoutval = timeout ? EMACS_SECS (*timeout) : 100000;
2387 local_timeout = &timeoutval;
2388 FD_ZERO (&orfds);
86a5659e
JB
2389 if (rfds)
2390 {
2391 orfds = *rfds;
86d1f23a 2392 FD_ZERO (rfds);
86a5659e
JB
2393 }
2394 if (wfds)
86d1f23a 2395 FD_ZERO (wfds);
86a5659e 2396 if (efds)
86d1f23a 2397 FD_ZERO (efds);
86a5659e
JB
2398
2399 /* If we are looking only for the terminal, with no timeout,
2400 just read it and wait -- that's more efficient. */
86d1f23a
KH
2401 if (*local_timeout == 100000 && process_tick == update_tick
2402 && FD_ISSET (0, &orfds))
86a5659e 2403 {
86d1f23a
KH
2404 int fd;
2405 for (fd = 1; fd < nfds; ++fd)
2406 if (FD_ISSET (fd, &orfds))
2407 goto hardway;
86a5659e
JB
2408 if (! detect_input_pending ())
2409 read_input_waiting ();
86d1f23a 2410 FD_SET (0, rfds);
86a5659e
JB
2411 return 1;
2412 }
2413
86d1f23a 2414 hardway:
86a5659e
JB
2415 /* Once a second, till the timer expires, check all the flagged read
2416 * descriptors to see if any input is available. If there is some then
2417 * set the corresponding bit in the return copy of rfds.
177c0ea7 2418 */
86a5659e
JB
2419 while (1)
2420 {
86d1f23a 2421 register int to_check, fd;
86a5659e
JB
2422
2423 if (rfds)
2424 {
86d1f23a 2425 for (to_check = nfds, fd = 0; --to_check >= 0; fd++)
86a5659e 2426 {
86d1f23a 2427 if (FD_ISSET (fd, &orfds))
86a5659e
JB
2428 {
2429 int avail = 0, status = 0;
2430
86d1f23a 2431 if (fd == 0)
86a5659e
JB
2432 avail = detect_input_pending (); /* Special keyboard handler */
2433 else
2434 {
2435#ifdef FIONREAD
2436 status = ioctl (fd, FIONREAD, &avail);
2437#else /* no FIONREAD */
2438 /* Hoping it will return -1 if nothing available
2439 or 0 if all 0 chars requested are read. */
2440 if (proc_buffered_char[fd] >= 0)
2441 avail = 1;
2442 else
2443 {
2444 avail = read (fd, &buf, 1);
2445 if (avail > 0)
2446 proc_buffered_char[fd] = buf;
2447 }
2448#endif /* no FIONREAD */
2449 }
2450 if (status >= 0 && avail > 0)
2451 {
86d1f23a 2452 FD_SET (fd, rfds);
86a5659e
JB
2453 ravail++;
2454 }
2455 }
2456 }
2457 }
2458 if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
2459 break;
ffdcc91d
GM
2460
2461 turn_on_atimers (0);
2462 signal (SIGALRM, select_alarm);
86a5659e
JB
2463 select_alarmed = 0;
2464 alarm (SELECT_PAUSE);
177c0ea7 2465
86a5659e
JB
2466 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2467 while (select_alarmed == 0 && *local_timeout != 0
2468 && process_tick == update_tick)
2469 {
2470 /* If we are interested in terminal input,
2471 wait by reading the terminal.
2472 That makes instant wakeup for terminal input at least. */
86d1f23a 2473 if (FD_ISSET (0, &orfds))
86a5659e
JB
2474 {
2475 read_input_waiting ();
2476 if (detect_input_pending ())
2477 select_alarmed = 1;
2478 }
2479 else
2480 pause ();
2481 }
2482 (*local_timeout) -= SELECT_PAUSE;
177c0ea7 2483
ffdcc91d
GM
2484 /* Reset the old alarm if there was one. */
2485 turn_on_atimers (1);
177c0ea7 2486
86a5659e
JB
2487 if (*local_timeout == 0) /* Stop on timer being cleared */
2488 break;
2489 }
2490 return ravail;
2491}
30790a37 2492#endif /* not WINDOWSNT */
86a5659e
JB
2493
2494/* Read keyboard input into the standard buffer,
2495 waiting for at least one character. */
2496
dfcf069d 2497void
86a5659e
JB
2498read_input_waiting ()
2499{
2246281f
KL
2500 /* XXX This needs to be updated for multi-tty support. Is there
2501 anybody who needs to emulate select these days? */
34567704
JB
2502 int nread, i;
2503 extern int quit_char;
86a5659e
JB
2504
2505 if (read_socket_hook)
2506 {
9f893fc6
KS
2507 struct input_event hold_quit;
2508
2509 EVENT_INIT (hold_quit);
2510 hold_quit.kind = NO_EVENT;
2511
86a5659e
JB
2512 read_alarm_should_throw = 0;
2513 if (! setjmp (read_alarm_throw))
9f893fc6 2514 nread = (*read_socket_hook) (0, 1, &hold_quit);
86a5659e
JB
2515 else
2516 nread = -1;
f4a7e5bd 2517
9f893fc6
KS
2518 if (hold_quit.kind != NO_EVENT)
2519 kbd_buffer_store_event (&hold_quit);
86a5659e
JB
2520 }
2521 else
86a5659e 2522 {
50acda85 2523 struct input_event e;
f4a7e5bd
RS
2524 char buf[3];
2525 nread = read (fileno (stdin), buf, 1);
50acda85 2526 EVENT_INIT (e);
f4a7e5bd
RS
2527
2528 /* Scan the chars for C-g and store them in kbd_buffer. */
e43bd4f5 2529 e.kind = ASCII_KEYSTROKE_EVENT;
2d064114 2530 e.frame_or_window = selected_frame;
f4a7e5bd
RS
2531 e.modifiers = 0;
2532 for (i = 0; i < nread; i++)
a00d5589 2533 {
f4a7e5bd
RS
2534 /* Convert chars > 0177 to meta events if desired.
2535 We do this under the same conditions that read_avail_input does. */
2536 if (read_socket_hook == 0)
2537 {
2538 /* If the user says she has a meta key, then believe her. */
2539 if (meta_key == 1 && (buf[i] & 0x80))
2540 e.modifiers = meta_modifier;
2541 if (meta_key != 2)
2542 buf[i] &= ~0x80;
2543 }
b95520f5 2544
c81d47b4 2545 XSETINT (e.code, buf[i]);
f4a7e5bd
RS
2546 kbd_buffer_store_event (&e);
2547 /* Don't look at input that follows a C-g too closely.
2548 This reduces lossage due to autorepeat on C-g. */
2549 if (buf[i] == quit_char)
2550 break;
2551 }
86a5659e
JB
2552 }
2553}
2554
4624371d 2555#if !defined (HAVE_SELECT)
819b8f00
KL
2556#define select sys_select
2557#endif
2558
86a5659e
JB
2559#endif /* not HAVE_SELECT */
2560#endif /* not VMS */
7964ba9e 2561#endif /* not MSDOS */
86a5659e 2562\f
86a5659e
JB
2563/* POSIX signals support - DJB */
2564/* Anyone with POSIX signals should have ANSI C declarations */
2565
2566#ifdef POSIX_SIGNALS
2567
c639b0e3 2568sigset_t empty_mask, full_mask;
86a5659e 2569
86a5659e
JB
2570signal_handler_t
2571sys_signal (int signal_number, signal_handler_t action)
2572{
c639b0e3 2573 struct sigaction new_action, old_action;
86a5659e 2574 sigemptyset (&new_action.sa_mask);
e5c99298 2575 new_action.sa_handler = action;
275464e7 2576#if defined (SA_RESTART) && ! defined (BROKEN_SA_RESTART) && !defined(SYNC_INPUT)
25ab68af 2577 /* Emacs mostly works better with restartable system services. If this
55fafcf0
RS
2578 flag exists, we probably want to turn it on here.
2579 However, on some systems this resets the timeout of `select'
2580 which means that `select' never finishes if it keeps getting signals.
2581 BROKEN_SA_RESTART is defined on those systems. */
275464e7
SM
2582 /* It's not clear why the comment above says "mostly works better". --Stef
2583 When SYNC_INPUT is set, we don't want SA_RESTART because we need to poll
2584 for pending input so we need long-running syscalls to be interrupted
2585 after a signal that sets the interrupt_input_pending flag. */
25ab68af
RS
2586 new_action.sa_flags = SA_RESTART;
2587#else
4a785b6e 2588 new_action.sa_flags = 0;
25ab68af 2589#endif
d32b2f3c 2590 sigaction (signal_number, &new_action, &old_action);
e5c99298 2591 return (old_action.sa_handler);
86a5659e
JB
2592}
2593
e065a56e
JB
2594#ifndef __GNUC__
2595/* If we're compiling with GCC, we don't need this function, since it
2596 can be written as a macro. */
2597sigset_t
2598sys_sigmask (int sig)
2599{
2600 sigset_t mask;
2601 sigemptyset (&mask);
2602 sigaddset (&mask, sig);
2603 return mask;
2604}
2605#endif
2606
86a5659e
JB
2607/* I'd like to have these guys return pointers to the mask storage in here,
2608 but there'd be trouble if the code was saving multiple masks. I'll be
2609 safe and pass the structure. It normally won't be more than 2 bytes
2610 anyhow. - DJB */
2611
2612sigset_t
2613sys_sigblock (sigset_t new_mask)
2614{
2615 sigset_t old_mask;
2616 sigprocmask (SIG_BLOCK, &new_mask, &old_mask);
2617 return (old_mask);
2618}
2619
2620sigset_t
2621sys_sigunblock (sigset_t new_mask)
2622{
2623 sigset_t old_mask;
2624 sigprocmask (SIG_UNBLOCK, &new_mask, &old_mask);
2625 return (old_mask);
2626}
2627
2628sigset_t
2629sys_sigsetmask (sigset_t new_mask)
2630{
2631 sigset_t old_mask;
2632 sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
2633 return (old_mask);
2634}
2635
2636#endif /* POSIX_SIGNALS */
2637\f
b80263be 2638#if !defined HAVE_STRSIGNAL && !HAVE_DECL_SYS_SIGLIST
ca9c0567
PE
2639static char *my_sys_siglist[NSIG];
2640# ifdef sys_siglist
2641# undef sys_siglist
2642# endif
2643# define sys_siglist my_sys_siglist
2644#endif
2645
2646void
2647init_signals ()
2648{
2649#ifdef POSIX_SIGNALS
2650 sigemptyset (&empty_mask);
2651 sigfillset (&full_mask);
2652#endif
2653
b80263be 2654#if !defined HAVE_STRSIGNAL && !HAVE_DECL_SYS_SIGLIST
ca9c0567
PE
2655 if (! initialized)
2656 {
2657# ifdef SIGABRT
2658 sys_siglist[SIGABRT] = "Aborted";
2659# endif
2660# ifdef SIGAIO
2661 sys_siglist[SIGAIO] = "LAN I/O interrupt";
2662# endif
2663# ifdef SIGALRM
2664 sys_siglist[SIGALRM] = "Alarm clock";
2665# endif
2666# ifdef SIGBUS
2667 sys_siglist[SIGBUS] = "Bus error";
2668# endif
2669# ifdef SIGCLD
2670 sys_siglist[SIGCLD] = "Child status changed";
2671# endif
2672# ifdef SIGCHLD
2673 sys_siglist[SIGCHLD] = "Child status changed";
2674# endif
2675# ifdef SIGCONT
2676 sys_siglist[SIGCONT] = "Continued";
2677# endif
2678# ifdef SIGDANGER
2679 sys_siglist[SIGDANGER] = "Swap space dangerously low";
2680# endif
2681# ifdef SIGDGNOTIFY
2682 sys_siglist[SIGDGNOTIFY] = "Notification message in queue";
2683# endif
2684# ifdef SIGEMT
2685 sys_siglist[SIGEMT] = "Emulation trap";
2686# endif
2687# ifdef SIGFPE
2688 sys_siglist[SIGFPE] = "Arithmetic exception";
2689# endif
2690# ifdef SIGFREEZE
2691 sys_siglist[SIGFREEZE] = "SIGFREEZE";
2692# endif
2693# ifdef SIGGRANT
2694 sys_siglist[SIGGRANT] = "Monitor mode granted";
2695# endif
2696# ifdef SIGHUP
2697 sys_siglist[SIGHUP] = "Hangup";
2698# endif
2699# ifdef SIGILL
2700 sys_siglist[SIGILL] = "Illegal instruction";
2701# endif
2702# ifdef SIGINT
2703 sys_siglist[SIGINT] = "Interrupt";
2704# endif
2705# ifdef SIGIO
2706 sys_siglist[SIGIO] = "I/O possible";
2707# endif
2708# ifdef SIGIOINT
2709 sys_siglist[SIGIOINT] = "I/O intervention required";
2710# endif
2711# ifdef SIGIOT
2712 sys_siglist[SIGIOT] = "IOT trap";
2713# endif
2714# ifdef SIGKILL
2715 sys_siglist[SIGKILL] = "Killed";
2716# endif
2717# ifdef SIGLOST
2718 sys_siglist[SIGLOST] = "Resource lost";
2719# endif
2720# ifdef SIGLWP
2721 sys_siglist[SIGLWP] = "SIGLWP";
2722# endif
2723# ifdef SIGMSG
2724 sys_siglist[SIGMSG] = "Monitor mode data available";
2725# endif
2726# ifdef SIGPHONE
2727 sys_siglist[SIGWIND] = "SIGPHONE";
2728# endif
2729# ifdef SIGPIPE
2730 sys_siglist[SIGPIPE] = "Broken pipe";
2731# endif
2732# ifdef SIGPOLL
2733 sys_siglist[SIGPOLL] = "Pollable event occurred";
2734# endif
2735# ifdef SIGPROF
2736 sys_siglist[SIGPROF] = "Profiling timer expired";
2737# endif
2738# ifdef SIGPTY
2739 sys_siglist[SIGPTY] = "PTY I/O interrupt";
2740# endif
2741# ifdef SIGPWR
2742 sys_siglist[SIGPWR] = "Power-fail restart";
2743# endif
2744# ifdef SIGQUIT
2745 sys_siglist[SIGQUIT] = "Quit";
2746# endif
2747# ifdef SIGRETRACT
2748 sys_siglist[SIGRETRACT] = "Need to relinguish monitor mode";
2749# endif
2750# ifdef SIGSAK
2751 sys_siglist[SIGSAK] = "Secure attention";
2752# endif
2753# ifdef SIGSEGV
2754 sys_siglist[SIGSEGV] = "Segmentation violation";
2755# endif
2756# ifdef SIGSOUND
2757 sys_siglist[SIGSOUND] = "Sound completed";
2758# endif
2759# ifdef SIGSTOP
2760 sys_siglist[SIGSTOP] = "Stopped (signal)";
2761# endif
2762# ifdef SIGSTP
2763 sys_siglist[SIGSTP] = "Stopped (user)";
2764# endif
2765# ifdef SIGSYS
2766 sys_siglist[SIGSYS] = "Bad argument to system call";
2767# endif
2768# ifdef SIGTERM
2769 sys_siglist[SIGTERM] = "Terminated";
2770# endif
2771# ifdef SIGTHAW
2772 sys_siglist[SIGTHAW] = "SIGTHAW";
2773# endif
2774# ifdef SIGTRAP
2775 sys_siglist[SIGTRAP] = "Trace/breakpoint trap";
2776# endif
2777# ifdef SIGTSTP
2778 sys_siglist[SIGTSTP] = "Stopped (user)";
2779# endif
2780# ifdef SIGTTIN
2781 sys_siglist[SIGTTIN] = "Stopped (tty input)";
2782# endif
2783# ifdef SIGTTOU
2784 sys_siglist[SIGTTOU] = "Stopped (tty output)";
2785# endif
2786# ifdef SIGURG
2787 sys_siglist[SIGURG] = "Urgent I/O condition";
2788# endif
2789# ifdef SIGUSR1
2790 sys_siglist[SIGUSR1] = "User defined signal 1";
2791# endif
2792# ifdef SIGUSR2
2793 sys_siglist[SIGUSR2] = "User defined signal 2";
2794# endif
2795# ifdef SIGVTALRM
2796 sys_siglist[SIGVTALRM] = "Virtual timer expired";
2797# endif
2798# ifdef SIGWAITING
2799 sys_siglist[SIGWAITING] = "Process's LWPs are blocked";
2800# endif
2801# ifdef SIGWINCH
2802 sys_siglist[SIGWINCH] = "Window size changed";
2803# endif
2804# ifdef SIGWIND
2805 sys_siglist[SIGWIND] = "SIGWIND";
2806# endif
2807# ifdef SIGXCPU
2808 sys_siglist[SIGXCPU] = "CPU time limit exceeded";
2809# endif
2810# ifdef SIGXFSZ
2811 sys_siglist[SIGXFSZ] = "File size limit exceeded";
2812# endif
2813 }
b80263be 2814#endif /* !defined HAVE_STRSIGNAL && !defined HAVE_DECL_SYS_SIGLIST */
ca9c0567
PE
2815}
2816\f
9927a7b1 2817#ifndef HAVE_RANDOM
4bb8c8b7
KH
2818#ifdef random
2819#define HAVE_RANDOM
2820#endif
2821#endif
2822
2823/* Figure out how many bits the system's random number generator uses.
2824 `random' and `lrand48' are assumed to return 31 usable bits.
2825 BSD `rand' returns a 31 bit value but the low order bits are unusable;
2826 so we'll shift it and treat it like the 15-bit USG `rand'. */
2827
2828#ifndef RAND_BITS
2829# ifdef HAVE_RANDOM
2830# define RAND_BITS 31
2831# else /* !HAVE_RANDOM */
2832# ifdef HAVE_LRAND48
2833# define RAND_BITS 31
2834# define random lrand48
2835# else /* !HAVE_LRAND48 */
2836# define RAND_BITS 15
2837# if RAND_MAX == 32767
2838# define random rand
2839# else /* RAND_MAX != 32767 */
2840# if RAND_MAX == 2147483647
2841# define random() (rand () >> 16)
2842# else /* RAND_MAX != 2147483647 */
2843# ifdef USG
2844# define random rand
2845# else
2846# define random() (rand () >> 16)
2a633456 2847# endif /* !USG */
4bb8c8b7
KH
2848# endif /* RAND_MAX != 2147483647 */
2849# endif /* RAND_MAX != 32767 */
2850# endif /* !HAVE_LRAND48 */
2851# endif /* !HAVE_RANDOM */
2852#endif /* !RAND_BITS */
2e46c7c6 2853
4bb8c8b7
KH
2854void
2855seed_random (arg)
2856 long arg;
86a5659e 2857{
4bb8c8b7
KH
2858#ifdef HAVE_RANDOM
2859 srandom ((unsigned int)arg);
f8b53a82 2860#else
4bb8c8b7 2861# ifdef HAVE_LRAND48
76425a49 2862 srand48 (arg);
4bb8c8b7
KH
2863# else
2864 srand ((unsigned int)arg);
2865# endif
2e46c7c6 2866#endif
86a5659e
JB
2867}
2868
4bb8c8b7
KH
2869/*
2870 * Build a full Emacs-sized word out of whatever we've got.
2871 * This suffices even for a 64-bit architecture with a 15-bit rand.
2872 */
2873long
2874get_random ()
2875{
2876 long val = random ();
2877#if VALBITS > RAND_BITS
2878 val = (val << RAND_BITS) ^ random ();
2879#if VALBITS > 2*RAND_BITS
2880 val = (val << RAND_BITS) ^ random ();
2881#if VALBITS > 3*RAND_BITS
2882 val = (val << RAND_BITS) ^ random ();
2883#if VALBITS > 4*RAND_BITS
2884 val = (val << RAND_BITS) ^ random ();
2885#endif /* need at least 5 */
2886#endif /* need at least 4 */
2887#endif /* need at least 3 */
2888#endif /* need at least 2 */
2889 return val & ((1L << VALBITS) - 1);
2890}
86a5659e 2891\f
86a5659e
JB
2892#ifdef VMS
2893
2894#ifdef getenv
2895/* If any place else asks for the TERM variable,
2896 allow it to be overridden with the EMACS_TERM variable
2897 before attempting to translate the logical name TERM. As a last
2898 resort, ask for VAX C's special idea of the TERM variable. */
2899#undef getenv
2900char *
2901sys_getenv (name)
2902 char *name;
2903{
2904 register char *val;
2905 static char buf[256];
2906 static struct dsc$descriptor_s equiv
2907 = {sizeof (buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf};
2908 static struct dsc$descriptor_s d_name
2909 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2910 short eqlen;
2911
2912 if (!strcmp (name, "TERM"))
2913 {
2914 val = (char *) getenv ("EMACS_TERM");
2915 if (val)
2916 return val;
2917 }
2918
2919 d_name.dsc$w_length = strlen (name);
2920 d_name.dsc$a_pointer = name;
986ffb24 2921 if (LIB$SYS_TRNLOG (&d_name, &eqlen, &equiv) == 1)
86a5659e
JB
2922 {
2923 char *str = (char *) xmalloc (eqlen + 1);
2924 bcopy (buf, str, eqlen);
2925 str[eqlen] = '\0';
2926 /* This is a storage leak, but a pain to fix. With luck,
2927 no one will ever notice. */
2928 return str;
2929 }
2930 return (char *) getenv (name);
2931}
2932#endif /* getenv */
2933
2934#ifdef abort
2935/* Since VMS doesn't believe in core dumps, the only way to debug this beast is
2936 to force a call on the debugger from within the image. */
2937#undef abort
2938sys_abort ()
2939{
0a125897 2940 reset_all_sys_modes ();
86a5659e
JB
2941 LIB$SIGNAL (SS$_DEBUG);
2942}
2943#endif /* abort */
2944#endif /* VMS */
2945\f
2946#ifdef VMS
2947#ifdef LINK_CRTL_SHARE
ea5a0917 2948#ifdef SHARABLE_LIB_BUG
eb8c3be9 2949/* Variables declared noshare and initialized in sharable libraries
86a5659e
JB
2950 cannot be shared. The VMS linker incorrectly forces you to use a private
2951 version which is uninitialized... If not for this "feature", we
2952 could use the C library definition of sys_nerr and sys_errlist. */
2953int sys_nerr = 35;
2954char *sys_errlist[] =
2955 {
2956 "error 0",
2957 "not owner",
2958 "no such file or directory",
2959 "no such process",
2960 "interrupted system call",
2961 "i/o error",
2962 "no such device or address",
2963 "argument list too long",
2964 "exec format error",
2965 "bad file number",
2966 "no child process",
2967 "no more processes",
2968 "not enough memory",
2969 "permission denied",
2970 "bad address",
2971 "block device required",
2972 "mount devices busy",
2973 "file exists",
2974 "cross-device link",
2975 "no such device",
2976 "not a directory",
2977 "is a directory",
2978 "invalid argument",
2979 "file table overflow",
2980 "too many open files",
2981 "not a typewriter",
2982 "text file busy",
2983 "file too big",
2984 "no space left on device",
2985 "illegal seek",
2986 "read-only file system",
2987 "too many links",
2988 "broken pipe",
2989 "math argument",
2990 "result too large",
2991 "I/O stream empty",
2992 "vax/vms specific error code nontranslatable error"
2993 };
ea5a0917 2994#endif /* SHARABLE_LIB_BUG */
86a5659e
JB
2995#endif /* LINK_CRTL_SHARE */
2996#endif /* VMS */
7088d1ca
RM
2997
2998#ifndef HAVE_STRERROR
fe03522b 2999#ifndef WINDOWSNT
7088d1ca
RM
3000char *
3001strerror (errnum)
3002 int errnum;
3003{
3004 extern char *sys_errlist[];
3005 extern int sys_nerr;
3006
3007 if (errnum >= 0 && errnum < sys_nerr)
3008 return sys_errlist[errnum];
3009 return (char *) "Unknown error";
3010}
fe03522b 3011#endif /* not WINDOWSNT */
7088d1ca 3012#endif /* ! HAVE_STRERROR */
86a5659e 3013\f
86a5659e 3014int
68c45bf0 3015emacs_open (path, oflag, mode)
8c2ba7de 3016 const char *path;
86a5659e
JB
3017 int oflag, mode;
3018{
3019 register int rtnval;
68c45bf0 3020
86a5659e 3021 while ((rtnval = open (path, oflag, mode)) == -1
275464e7
SM
3022 && (errno == EINTR))
3023 QUIT;
86a5659e
JB
3024 return (rtnval);
3025}
3026
dfcf069d 3027int
68c45bf0 3028emacs_close (fd)
86a5659e
JB
3029 int fd;
3030{
fe111daf 3031 int did_retry = 0;
86a5659e
JB
3032 register int rtnval;
3033
3034 while ((rtnval = close (fd)) == -1
fe111daf
KH
3035 && (errno == EINTR))
3036 did_retry = 1;
3037
3038 /* If close is interrupted SunOS 4.1 may or may not have closed the
3039 file descriptor. If it did the second close will fail with
3040 errno = EBADF. That means we have succeeded. */
3041 if (rtnval == -1 && did_retry && errno == EBADF)
3042 return 0;
3043
86a5659e
JB
3044 return rtnval;
3045}
3046
86a5659e 3047int
68c45bf0 3048emacs_read (fildes, buf, nbyte)
86a5659e
JB
3049 int fildes;
3050 char *buf;
3051 unsigned int nbyte;
3052{
3053 register int rtnval;
177c0ea7 3054
86a5659e 3055 while ((rtnval = read (fildes, buf, nbyte)) == -1
275464e7
SM
3056 && (errno == EINTR))
3057 QUIT;
86a5659e
JB
3058 return (rtnval);
3059}
3060
3061int
68c45bf0 3062emacs_write (fildes, buf, nbyte)
86a5659e 3063 int fildes;
7b1cc119 3064 const char *buf;
86a5659e
JB
3065 unsigned int nbyte;
3066{
b95520f5 3067 register int rtnval, bytes_written;
86a5659e 3068
b95520f5
BF
3069 bytes_written = 0;
3070
3071 while (nbyte > 0)
3072 {
3073 rtnval = write (fildes, buf, nbyte);
3074
3075 if (rtnval == -1)
3076 {
3077 if (errno == EINTR)
77220eeb
SM
3078 {
3079#ifdef SYNC_INPUT
3080 /* I originally used `QUIT' but that might causes files to
3081 be truncated if you hit C-g in the middle of it. --Stef */
3082 if (interrupt_input_pending)
3083 handle_async_input ();
cb5c5b3d
JD
3084 if (pending_atimers)
3085 do_pending_atimers ();
77220eeb
SM
3086#endif
3087 continue;
3088 }
b95520f5 3089 else
aa670904 3090 return (bytes_written ? bytes_written : -1);
b95520f5
BF
3091 }
3092
3093 buf += rtnval;
3094 nbyte -= rtnval;
3095 bytes_written += rtnval;
3096 }
3097 return (bytes_written);
86a5659e 3098}
86a5659e
JB
3099\f
3100#ifdef USG
3101/*
3102 * All of the following are for USG.
3103 *
3104 * On USG systems the system calls are INTERRUPTIBLE by signals
3105 * that the user program has elected to catch. Thus the system call
3106 * must be retried in these cases. To handle this without massive
3107 * changes in the source code, we remap the standard system call names
3108 * to names for our own functions in sysdep.c that do the system call
3109 * with retries. Actually, for portability reasons, it is good
3110 * programming practice, as this example shows, to limit all actual
eb8c3be9 3111 * system calls to a single occurrence in the source. Sure, this
86a5659e
JB
3112 * adds an extra level of function call overhead but it is almost
3113 * always negligible. Fred Fish, Unisoft Systems Inc.
3114 */
3115
86a5659e
JB
3116/*
3117 * Warning, this function may not duplicate 4.2 action properly
3118 * under error conditions.
3119 */
3120
3121#ifndef MAXPATHLEN
3122/* In 4.1, param.h fails to define this. */
3123#define MAXPATHLEN 1024
3124#endif
3125
3126#ifndef HAVE_GETWD
3127
3128char *
3129getwd (pathname)
3130 char *pathname;
3131{
3132 char *npath, *spath;
3133 extern char *getcwd ();
3134
9ac0d9e0 3135 BLOCK_INPUT; /* getcwd uses malloc */
86a5659e 3136 spath = npath = getcwd ((char *) 0, MAXPATHLEN);
f4a7e5bd 3137 if (spath == 0)
18b6bc73
GM
3138 {
3139 UNBLOCK_INPUT;
3140 return spath;
3141 }
86a5659e
JB
3142 /* On Altos 3068, getcwd can return @hostname/dir, so discard
3143 up to first slash. Should be harmless on other systems. */
3144 while (*npath && *npath != '/')
3145 npath++;
3146 strcpy (pathname, npath);
3147 free (spath); /* getcwd uses malloc */
9ac0d9e0 3148 UNBLOCK_INPUT;
86a5659e
JB
3149 return pathname;
3150}
3151
3152#endif /* HAVE_GETWD */
3153
3154/*
3155 * Emulate rename using unlink/link. Note that this is
3156 * only partially correct. Also, doesn't enforce restriction
3157 * that files be of same type (regular->regular, dir->dir, etc).
3158 */
3159
4746118a
JB
3160#ifndef HAVE_RENAME
3161
86a5659e 3162rename (from, to)
19c7afdf
JB
3163 const char *from;
3164 const char *to;
86a5659e
JB
3165{
3166 if (access (from, 0) == 0)
3167 {
3168 unlink (to);
3169 if (link (from, to) == 0)
3170 if (unlink (from) == 0)
3171 return (0);
3172 }
3173 return (-1);
3174}
3175
4746118a
JB
3176#endif
3177
86a5659e
JB
3178
3179#ifdef HPUX
3180#ifndef HAVE_PERROR
3181
3182/* HPUX curses library references perror, but as far as we know
3183 it won't be called. Anyway this definition will do for now. */
3184
3185perror ()
3186{
3187}
3188
3189#endif /* not HAVE_PERROR */
3190#endif /* HPUX */
3191
3192#ifndef HAVE_DUP2
3193
3194/*
3195 * Emulate BSD dup2. First close newd if it already exists.
3196 * Then, attempt to dup oldd. If not successful, call dup2 recursively
3197 * until we are, then close the unsuccessful ones.
3198 */
3199
3200dup2 (oldd, newd)
3201 int oldd;
3202 int newd;
3203{
3204 register int fd, ret;
177c0ea7 3205
68c45bf0 3206 emacs_close (newd);
86a5659e
JB
3207
3208#ifdef F_DUPFD
68c45bf0 3209 return fcntl (oldd, F_DUPFD, newd);
86a5659e
JB
3210#else
3211 fd = dup (old);
3212 if (fd == -1)
3213 return -1;
3214 if (fd == new)
3215 return new;
3216 ret = dup2 (old,new);
68c45bf0 3217 emacs_close (fd);
86a5659e
JB
3218 return ret;
3219#endif
3220}
3221
3222#endif /* not HAVE_DUP2 */
3223
3224/*
3225 * Gettimeofday. Simulate as much as possible. Only accurate
3226 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3227 * Only needed when subprocesses are defined.
3228 */
3229
3230#ifdef subprocesses
3231#ifndef VMS
3232#ifndef HAVE_GETTIMEOFDAY
3233#ifdef HAVE_TIMEVAL
177c0ea7 3234
86a5659e 3235/* ARGSUSED */
dfcf069d 3236int
86a5659e
JB
3237gettimeofday (tp, tzp)
3238 struct timeval *tp;
3239 struct timezone *tzp;
3240{
3241 extern long time ();
3242
177c0ea7 3243 tp->tv_sec = time ((long *)0);
86a5659e 3244 tp->tv_usec = 0;
4ca7594f
RS
3245 if (tzp != 0)
3246 tzp->tz_minuteswest = -1;
dfcf069d 3247 return 0;
86a5659e 3248}
177c0ea7 3249
86a5659e
JB
3250#endif
3251#endif
3252#endif
3253#endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
177c0ea7 3254
86a5659e
JB
3255/*
3256 * This function will go away as soon as all the stubs fixed. (fnf)
3257 */
3258
dfcf069d 3259void
86a5659e
JB
3260croak (badfunc)
3261 char *badfunc;
3262{
3263 printf ("%s not yet implemented\r\n", badfunc);
28d440ab 3264 reset_all_sys_modes ();
86a5659e
JB
3265 exit (1);
3266}
3267
3268#endif /* USG */
3269\f
86a5659e
JB
3270/* Directory routines for systems that don't have them. */
3271
3272#ifdef SYSV_SYSTEM_DIR
3273
3274#include <dirent.h>
3275
9d9a7716 3276#if !defined (HAVE_CLOSEDIR)
cfdc57af 3277
86a5659e 3278int
9d9a7716 3279closedir (DIR *dirp /* stream from opendir */)
86a5659e 3280{
cfdc57af
RS
3281 int rtnval;
3282
68c45bf0 3283 rtnval = emacs_close (dirp->dd_fd);
1b929d25 3284
65aa44ac
JB
3285 /* Some systems (like Solaris) allocate the buffer and the DIR all
3286 in one block. Why in the world are we freeing this ourselves
3287 anyway? */
89f1e540 3288#if ! defined (SOLARIS2)
65aa44ac
JB
3289 xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
3290#endif
9ac0d9e0 3291 xfree ((char *) dirp);
cfdc57af
RS
3292
3293 return rtnval;
86a5659e 3294}
9d9a7716 3295#endif /* not HAVE_CLOSEDIR */
86a5659e
JB
3296#endif /* SYSV_SYSTEM_DIR */
3297
3298#ifdef NONSYSTEM_DIR_LIBRARY
3299
3300DIR *
3301opendir (filename)
3302 char *filename; /* name of directory */
3303{
3304 register DIR *dirp; /* -> malloc'ed storage */
3305 register int fd; /* file descriptor for read */
3306 struct stat sbuf; /* result of fstat */
3307
68c45bf0 3308 fd = emacs_open (filename, O_RDONLY, 0);
86a5659e
JB
3309 if (fd < 0)
3310 return 0;
3311
9ac0d9e0 3312 BLOCK_INPUT;
86a5659e
JB
3313 if (fstat (fd, &sbuf) < 0
3314 || (sbuf.st_mode & S_IFMT) != S_IFDIR
7f77dbe3 3315 || (dirp = (DIR *) xmalloc (sizeof (DIR))) == 0)
86a5659e 3316 {
68c45bf0 3317 emacs_close (fd);
9ac0d9e0 3318 UNBLOCK_INPUT;
86a5659e
JB
3319 return 0; /* bad luck today */
3320 }
9ac0d9e0 3321 UNBLOCK_INPUT;
86a5659e
JB
3322
3323 dirp->dd_fd = fd;
3324 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
3325
3326 return dirp;
3327}
3328
3329void
3330closedir (dirp)
3331 register DIR *dirp; /* stream from opendir */
3332{
68c45bf0 3333 emacs_close (dirp->dd_fd);
9ac0d9e0 3334 xfree ((char *) dirp);
86a5659e
JB
3335}
3336
3337
3338#ifndef VMS
3339#define DIRSIZ 14
3340struct olddir
3341 {
3342 ino_t od_ino; /* inode */
3343 char od_name[DIRSIZ]; /* filename */
3344 };
3345#endif /* not VMS */
3346
3347struct direct dir_static; /* simulated directory contents */
3348
3349/* ARGUSED */
3350struct direct *
3351readdir (dirp)
3352 register DIR *dirp; /* stream from opendir */
3353{
3354#ifndef VMS
3355 register struct olddir *dp; /* -> directory data */
3356#else /* VMS */
3357 register struct dir$_name *dp; /* -> directory data */
3358 register struct dir$_version *dv; /* -> version data */
3359#endif /* VMS */
3360
3361 for (; ;)
3362 {
3363 if (dirp->dd_loc >= dirp->dd_size)
3364 dirp->dd_loc = dirp->dd_size = 0;
3365
3366 if (dirp->dd_size == 0 /* refill buffer */
68c45bf0 3367 && (dirp->dd_size = emacs_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
86a5659e
JB
3368 return 0;
3369
3370#ifndef VMS
3371 dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
3372 dirp->dd_loc += sizeof (struct olddir);
3373
3374 if (dp->od_ino != 0) /* not deleted entry */
3375 {
3376 dir_static.d_ino = dp->od_ino;
3377 strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
3378 dir_static.d_name[DIRSIZ] = '\0';
3379 dir_static.d_namlen = strlen (dir_static.d_name);
3380 dir_static.d_reclen = sizeof (struct direct)
3381 - MAXNAMLEN + 3
3382 + dir_static.d_namlen - dir_static.d_namlen % 4;
3383 return &dir_static; /* -> simulated structure */
3384 }
3385#else /* VMS */
3386 dp = (struct dir$_name *) dirp->dd_buf;
3387 if (dirp->dd_loc == 0)
3388 dirp->dd_loc = (dp->dir$b_namecount&1) ? dp->dir$b_namecount + 1
3389 : dp->dir$b_namecount;
3390 dv = (struct dir$_version *)&dp->dir$t_name[dirp->dd_loc];
3391 dir_static.d_ino = dv->dir$w_fid_num;
3392 dir_static.d_namlen = dp->dir$b_namecount;
3393 dir_static.d_reclen = sizeof (struct direct)
3394 - MAXNAMLEN + 3
3395 + dir_static.d_namlen - dir_static.d_namlen % 4;
3396 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3397 dir_static.d_name[dir_static.d_namlen] = '\0';
3398 dirp->dd_loc = dirp->dd_size; /* only one record at a time */
3399 return &dir_static;
3400#endif /* VMS */
3401 }
3402}
3403
3404#ifdef VMS
3405/* readdirver is just like readdir except it returns all versions of a file
3406 as separate entries. */
3407
3408/* ARGUSED */
3409struct direct *
3410readdirver (dirp)
3411 register DIR *dirp; /* stream from opendir */
3412{
3413 register struct dir$_name *dp; /* -> directory data */
3414 register struct dir$_version *dv; /* -> version data */
3415
3416 if (dirp->dd_loc >= dirp->dd_size - sizeof (struct dir$_name))
3417 dirp->dd_loc = dirp->dd_size = 0;
3418
3419 if (dirp->dd_size == 0 /* refill buffer */
3420 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3421 return 0;
3422
3423 dp = (struct dir$_name *) dirp->dd_buf;
3424 if (dirp->dd_loc == 0)
3425 dirp->dd_loc = (dp->dir$b_namecount & 1) ? dp->dir$b_namecount + 1
3426 : dp->dir$b_namecount;
3427 dv = (struct dir$_version *) &dp->dir$t_name[dirp->dd_loc];
3428 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3429 sprintf (&dir_static.d_name[dp->dir$b_namecount], ";%d", dv->dir$w_version);
3430 dir_static.d_namlen = strlen (dir_static.d_name);
3431 dir_static.d_ino = dv->dir$w_fid_num;
3432 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3
3433 + dir_static.d_namlen - dir_static.d_namlen % 4;
3434 dirp->dd_loc = ((char *) (++dv) - dp->dir$t_name);
3435 return &dir_static;
3436}
3437
3438#endif /* VMS */
3439
3440#endif /* NONSYSTEM_DIR_LIBRARY */
23524fb9
JB
3441
3442\f
53ea491a 3443int
061ea326 3444set_file_times (filename, atime, mtime)
8c2ba7de 3445 const char *filename;
53ea491a
KH
3446 EMACS_TIME atime, mtime;
3447{
3448#ifdef HAVE_UTIMES
3449 struct timeval tv[2];
3450 tv[0] = atime;
3451 tv[1] = mtime;
8334eb21
RS
3452 return utimes (filename, tv);
3453#else /* not HAVE_UTIMES */
53ea491a
KH
3454 struct utimbuf utb;
3455 utb.actime = EMACS_SECS (atime);
3456 utb.modtime = EMACS_SECS (mtime);
8334eb21
RS
3457 return utime (filename, &utb);
3458#endif /* not HAVE_UTIMES */
53ea491a
KH
3459}
3460\f
23524fb9
JB
3461/* mkdir and rmdir functions, for systems which don't have them. */
3462
3463#ifndef HAVE_MKDIR
3464/*
3465 * Written by Robert Rother, Mariah Corporation, August 1985.
3466 *
3467 * If you want it, it's yours. All I ask in return is that if you
3468 * figure out how to do this in a Bourne Shell script you send me
3469 * a copy.
3470 * sdcsvax!rmr or rmr@uscd
3471 *
3472 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
db9cd97a 3473 * subroutine. 11Mar86; hoptoad!gnu
23524fb9
JB
3474 *
3475 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3476 * subroutine didn't return EEXIST. It does now.
3477 */
3478
3479/*
3480 * Make a directory.
3481 */
f3892946
RS
3482#ifdef MKDIR_PROTOTYPE
3483MKDIR_PROTOTYPE
3484#else
23524fb9
JB
3485int
3486mkdir (dpath, dmode)
3487 char *dpath;
3488 int dmode;
f3892946 3489#endif
23524fb9 3490{
039f26a4 3491 int cpid, status, fd;
23524fb9
JB
3492 struct stat statbuf;
3493
3494 if (stat (dpath, &statbuf) == 0)
3495 {
3496 errno = EEXIST; /* Stat worked, so it already exists */
3497 return -1;
3498 }
3499
3500 /* If stat fails for a reason other than non-existence, return error */
3501 if (errno != ENOENT)
3502 return -1;
3503
039f26a4 3504 synch_process_alive = 1;
23524fb9
JB
3505 switch (cpid = fork ())
3506 {
3507
039f26a4 3508 case -1: /* Error in fork */
23524fb9
JB
3509 return (-1); /* Errno is set already */
3510
3511 case 0: /* Child process */
3512 /*
3513 * Cheap hack to set mode of new directory. Since this
3514 * child process is going away anyway, we zap its umask.
3515 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3516 * directory. Does anybody care?
3517 */
3518 status = umask (0); /* Get current umask */
3519 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */
68c45bf0 3520 fd = emacs_open ("/dev/null", O_RDWR, 0);
039f26a4
RS
3521 if (fd >= 0)
3522 {
3523 dup2 (fd, 0);
3524 dup2 (fd, 1);
3525 dup2 (fd, 2);
3526 }
23524fb9
JB
3527 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
3528 _exit (-1); /* Can't exec /bin/mkdir */
3529
3530 default: /* Parent process */
039f26a4 3531 wait_for_termination (cpid);
23524fb9
JB
3532 }
3533
6b0824c9
JD
3534 if (synch_process_death != 0 || synch_process_retcode != 0
3535 || synch_process_termsig != 0)
23524fb9
JB
3536 {
3537 errno = EIO; /* We don't know why, but */
3538 return -1; /* /bin/mkdir failed */
3539 }
3540
3541 return 0;
3542}
3543#endif /* not HAVE_MKDIR */
3544
3545#ifndef HAVE_RMDIR
3546int
3547rmdir (dpath)
3548 char *dpath;
3549{
039f26a4 3550 int cpid, status, fd;
23524fb9
JB
3551 struct stat statbuf;
3552
3553 if (stat (dpath, &statbuf) != 0)
3554 {
3555 /* Stat just set errno. We don't have to */
3556 return -1;
3557 }
3558
039f26a4 3559 synch_process_alive = 1;
23524fb9
JB
3560 switch (cpid = fork ())
3561 {
3562
039f26a4 3563 case -1: /* Error in fork */
23524fb9
JB
3564 return (-1); /* Errno is set already */
3565
3566 case 0: /* Child process */
68c45bf0 3567 fd = emacs_open ("/dev/null", O_RDWR, 0);
039f26a4
RS
3568 if (fd >= 0)
3569 {
3570 dup2 (fd, 0);
3571 dup2 (fd, 1);
3572 dup2 (fd, 2);
3573 }
f560db78
RS
3574 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
3575 _exit (-1); /* Can't exec /bin/rmdir */
3576
207bdbdb 3577 default: /* Parent process */
f560db78 3578 wait_for_termination (cpid);
23524fb9
JB
3579 }
3580
6b0824c9
JD
3581 if (synch_process_death != 0 || synch_process_retcode != 0
3582 || synch_process_termsig != 0)
23524fb9
JB
3583 {
3584 errno = EIO; /* We don't know why, but */
f560db78 3585 return -1; /* /bin/rmdir failed */
23524fb9
JB
3586 }
3587
3588 return 0;
3589}
3590#endif /* !HAVE_RMDIR */
3591
3592
86a5659e
JB
3593\f
3594/* Functions for VMS */
3595#ifdef VMS
86a5659e
JB
3596#include <acldef.h>
3597#include <chpdef.h>
3598#include <jpidef.h>
3599
3600/* Return as a string the VMS error string pertaining to STATUS.
3601 Reuses the same static buffer each time it is called. */
3602
3603char *
3604vmserrstr (status)
3605 int status; /* VMS status code */
3606{
3607 int bufadr[2];
3608 short len;
3609 static char buf[257];
3610
3611 bufadr[0] = sizeof buf - 1;
3612 bufadr[1] = (int) buf;
3613 if (! (SYS$GETMSG (status, &len, bufadr, 0x1, 0) & 1))
3614 return "untranslatable VMS error status";
3615 buf[len] = '\0';
3616 return buf;
3617}
3618
3619#ifdef access
3620#undef access
177c0ea7 3621
86a5659e
JB
3622/* The following is necessary because 'access' emulation by VMS C (2.0) does
3623 * not work correctly. (It also doesn't work well in version 2.3.)
3624 */
3625
3626#ifdef VMS4_4
3627
3628#define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3629 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3630
3631typedef union {
3632 struct {
3633 unsigned short s_buflen;
3634 unsigned short s_code;
3635 char *s_bufadr;
3636 unsigned short *s_retlenadr;
3637 } s;
3638 int end;
3639} item;
3640#define buflen s.s_buflen
3641#define code s.s_code
3642#define bufadr s.s_bufadr
3643#define retlenadr s.s_retlenadr
3644
3645#define R_OK 4 /* test for read permission */
3646#define W_OK 2 /* test for write permission */
3647#define X_OK 1 /* test for execute (search) permission */
3648#define F_OK 0 /* test for presence of file */
3649
3650int
3651sys_access (path, mode)
3652 char *path;
3653 int mode;
3654{
3655 static char *user = NULL;
3656 char dir_fn[512];
3657
3658 /* translate possible directory spec into .DIR file name, so brain-dead
3659 * access can treat the directory like a file. */
3660 if (directory_file_name (path, dir_fn))
3661 path = dir_fn;
177c0ea7 3662
86a5659e
JB
3663 if (mode == F_OK)
3664 return access (path, mode);
3665 if (user == NULL && (user = (char *) getenv ("USER")) == NULL)
3666 return -1;
3667 {
3668 int stat;
3669 int flags;
3670 int acces;
3671 unsigned short int dummy;
3672 item itemlst[3];
3673 static int constant = ACL$C_FILE;
3674 DESCRIPTOR (path_desc, path);
3675 DESCRIPTOR (user_desc, user);
177c0ea7 3676
86a5659e
JB
3677 flags = 0;
3678 acces = 0;
3679 if ((mode & X_OK) && ((stat = access (path, mode)) < 0 || mode == X_OK))
3680 return stat;
3681 if (mode & R_OK)
3682 acces |= CHP$M_READ;
3683 if (mode & W_OK)
3684 acces |= CHP$M_WRITE;
3685 itemlst[0].buflen = sizeof (int);
3686 itemlst[0].code = CHP$_FLAGS;
3687 itemlst[0].bufadr = (char *) &flags;
3688 itemlst[0].retlenadr = &dummy;
3689 itemlst[1].buflen = sizeof (int);
3690 itemlst[1].code = CHP$_ACCESS;
3691 itemlst[1].bufadr = (char *) &acces;
3692 itemlst[1].retlenadr = &dummy;
3693 itemlst[2].end = CHP$_END;
3694 stat = SYS$CHECK_ACCESS (&constant, &path_desc, &user_desc, itemlst);
3695 return stat == SS$_NORMAL ? 0 : -1;
3696 }
3697}
3698
3699#else /* not VMS4_4 */
3700
3701#include <prvdef.h>
fe03522b
RS
3702#define ACE$M_WRITE 2
3703#define ACE$C_KEYID 1
86a5659e
JB
3704
3705static unsigned short memid, grpid;
3706static unsigned int uic;
3707
3708/* Called from init_sys_modes, so it happens not very often
3709 but at least each time Emacs is loaded. */
dfcf069d 3710void
86a5659e
JB
3711sys_access_reinit ()
3712{
3713 uic = 0;
3714}
3715
3716int
3717sys_access (filename, type)
3718 char * filename;
3719 int type;
3720{
3721 struct FAB fab;
3722 struct XABPRO xab;
3723 int status, size, i, typecode, acl_controlled;
3724 unsigned int *aclptr, *aclend, aclbuf[60];
3725 union prvdef prvmask;
3726
3727 /* Get UIC and GRP values for protection checking. */
3728 if (uic == 0)
3729 {
3730 status = LIB$GETJPI (&JPI$_UIC, 0, 0, &uic, 0, 0);
3731 if (! (status & 1))
3732 return -1;
3733 memid = uic & 0xFFFF;
3734 grpid = uic >> 16;
3735 }
3736
fe03522b 3737 if (type != 2) /* not checking write access */
86a5659e
JB
3738 return access (filename, type);
3739
3740 /* Check write protection. */
177c0ea7 3741
fe03522b 3742#define CHECKPRIV(bit) (prvmask.bit)
ea5a0917 3743#define WRITABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
86a5659e
JB
3744
3745 /* Find privilege bits */
986ffb24 3746 status = SYS$SETPRV (0, 0, 0, prvmask);
86a5659e
JB
3747 if (! (status & 1))
3748 error ("Unable to find privileges: %s", vmserrstr (status));
3749 if (CHECKPRIV (PRV$V_BYPASS))
3750 return 0; /* BYPASS enabled */
3751 fab = cc$rms_fab;
3752 fab.fab$b_fac = FAB$M_GET;
3753 fab.fab$l_fna = filename;
3754 fab.fab$b_fns = strlen (filename);
3755 fab.fab$l_xab = &xab;
3756 xab = cc$rms_xabpro;
3757 xab.xab$l_aclbuf = aclbuf;
3758 xab.xab$w_aclsiz = sizeof (aclbuf);
986ffb24 3759 status = SYS$OPEN (&fab, 0, 0);
86a5659e
JB
3760 if (! (status & 1))
3761 return -1;
986ffb24 3762 SYS$CLOSE (&fab, 0, 0);
86a5659e 3763 /* Check system access */
ea5a0917 3764 if (CHECKPRIV (PRV$V_SYSPRV) && WRITABLE (XAB$V_SYS))
86a5659e
JB
3765 return 0;
3766 /* Check ACL entries, if any */
3767 acl_controlled = 0;
3768 if (xab.xab$w_acllen > 0)
3769 {
3770 aclptr = aclbuf;
3771 aclend = &aclbuf[xab.xab$w_acllen / 4];
3772 while (*aclptr && aclptr < aclend)
3773 {
3774 size = (*aclptr & 0xff) / 4;
3775 typecode = (*aclptr >> 8) & 0xff;
3776 if (typecode == ACE$C_KEYID)
3777 for (i = size - 1; i > 1; i--)
3778 if (aclptr[i] == uic)
3779 {
3780 acl_controlled = 1;
3781 if (aclptr[1] & ACE$M_WRITE)
3782 return 0; /* Write access through ACL */
3783 }
3784 aclptr = &aclptr[size];
3785 }
3786 if (acl_controlled) /* ACL specified, prohibits write access */
3787 return -1;
3788 }
3789 /* No ACL entries specified, check normal protection */
ea5a0917 3790 if (WRITABLE (XAB$V_WLD)) /* World writable */
86a5659e 3791 return 0;
ea5a0917 3792 if (WRITABLE (XAB$V_GRP) &&
86a5659e 3793 (unsigned short) (xab.xab$l_uic >> 16) == grpid)
ea5a0917
KH
3794 return 0; /* Group writable */
3795 if (WRITABLE (XAB$V_OWN) &&
86a5659e 3796 (xab.xab$l_uic & 0xFFFF) == memid)
ea5a0917 3797 return 0; /* Owner writable */
86a5659e 3798
ea5a0917 3799 return -1; /* Not writable */
86a5659e
JB
3800}
3801#endif /* not VMS4_4 */
3802#endif /* access */
177c0ea7 3803
86a5659e
JB
3804static char vtbuf[NAM$C_MAXRSS+1];
3805
3806/* translate a vms file spec to a unix path */
3807char *
3808sys_translate_vms (vfile)
3809 char * vfile;
3810{
3811 char * p;
3812 char * targ;
3813
3814 if (!vfile)
3815 return 0;
3816
3817 targ = vtbuf;
3818
3819 /* leading device or logical name is a root directory */
3820 if (p = strchr (vfile, ':'))
3821 {
3822 *targ++ = '/';
3823 while (vfile < p)
3824 *targ++ = *vfile++;
3825 vfile++;
3826 *targ++ = '/';
3827 }
3828 p = vfile;
3829 if (*p == '[' || *p == '<')
3830 {
3831 while (*++vfile != *p + 2)
3832 switch (*vfile)
3833 {
3834 case '.':
3835 if (vfile[-1] == *p)
3836 *targ++ = '.';
3837 *targ++ = '/';
3838 break;
3839
3840 case '-':
3841 *targ++ = '.';
3842 *targ++ = '.';
3843 break;
177c0ea7 3844
86a5659e
JB
3845 default:
3846 *targ++ = *vfile;
3847 break;
3848 }
3849 vfile++;
3850 *targ++ = '/';
3851 }
3852 while (*vfile)
3853 *targ++ = *vfile++;
3854
3855 return vtbuf;
3856}
3857
3858static char utbuf[NAM$C_MAXRSS+1];
3859
3860/* translate a unix path to a VMS file spec */
3861char *
3862sys_translate_unix (ufile)
3863 char * ufile;
3864{
3865 int slash_seen = 0;
3866 char *p;
3867 char * targ;
3868
3869 if (!ufile)
3870 return 0;
3871
3872 targ = utbuf;
3873
3874 if (*ufile == '/')
3875 {
3876 ufile++;
3877 }
3878
3879 while (*ufile)
3880 {
3881 switch (*ufile)
3882 {
3883 case '/':
3884 if (slash_seen)
3885 if (index (&ufile[1], '/'))
3886 *targ++ = '.';
3887 else
3888 *targ++ = ']';
3889 else
3890 {
3891 *targ++ = ':';
3892 if (index (&ufile[1], '/'))
3893 *targ++ = '[';
3894 slash_seen = 1;
3895 }
3896 break;
3897
3898 case '.':
3899 if (strncmp (ufile, "./", 2) == 0)
3900 {
3901 if (!slash_seen)
3902 {
3903 *targ++ = '[';
3904 slash_seen = 1;
3905 }
3906 ufile++; /* skip the dot */
3907 if (index (&ufile[1], '/'))
3908 *targ++ = '.';
3909 else
3910 *targ++ = ']';
3911 }
3912 else if (strncmp (ufile, "../", 3) == 0)
3913 {
3914 if (!slash_seen)
3915 {
3916 *targ++ = '[';
3917 slash_seen = 1;
3918 }
3919 *targ++ = '-';
3920 ufile += 2; /* skip the dots */
3921 if (index (&ufile[1], '/'))
3922 *targ++ = '.';
3923 else
3924 *targ++ = ']';
3925 }
3926 else
3927 *targ++ = *ufile;
3928 break;
3929
3930 default:
3931 *targ++ = *ufile;
3932 break;
3933 }
3934 ufile++;
3935 }
3936 *targ = '\0';
177c0ea7 3937
86a5659e
JB
3938 return utbuf;
3939}
3940
3941char *
3942getwd (pathname)
3943 char *pathname;
3944{
f4a7e5bd 3945 char *ptr, *val;
210b2b4f 3946 extern char *getcwd ();
86a5659e 3947
210b2b4f
JB
3948#define MAXPATHLEN 1024
3949
9ac0d9e0 3950 ptr = xmalloc (MAXPATHLEN);
f4a7e5bd
RS
3951 val = getcwd (ptr, MAXPATHLEN);
3952 if (val == 0)
3953 {
3954 xfree (ptr);
3955 return val;
3956 }
210b2b4f 3957 strcpy (pathname, ptr);
9ac0d9e0 3958 xfree (ptr);
177c0ea7 3959
210b2b4f 3960 return pathname;
86a5659e
JB
3961}
3962
dfcf069d 3963int
86a5659e
JB
3964getppid ()
3965{
3966 long item_code = JPI$_OWNER;
3967 unsigned long parent_id;
3968 int status;
3969
3970 if (((status = LIB$GETJPI (&item_code, 0, 0, &parent_id)) & 1) == 0)
3971 {
3972 errno = EVMSERR;
3973 vaxc$errno = status;
3974 return -1;
3975 }
3976 return parent_id;
3977}
3978
3979#undef getuid
3980unsigned
3981sys_getuid ()
3982{
3983 return (getgid () << 16) | getuid ();
3984}
3985
68c45bf0 3986#undef read
86a5659e
JB
3987int
3988sys_read (fildes, buf, nbyte)
3989 int fildes;
3990 char *buf;
3991 unsigned int nbyte;
3992{
3993 return read (fildes, buf, (nbyte < MAXIOSIZE ? nbyte : MAXIOSIZE));
3994}
3995
86a5659e
JB
3996/*
3997 * VAX/VMS VAX C RTL really loses. It insists that records
3998 * end with a newline (carriage return) character, and if they
3999 * don't it adds one (nice of it isn't it!)
4000 *
4001 * Thus we do this stupidity below.
4002 */
4003
68c45bf0 4004#undef write
86a5659e
JB
4005int
4006sys_write (fildes, buf, nbytes)
4007 int fildes;
4008 char *buf;
4009 unsigned int nbytes;
4010{
4011 register char *p;
4012 register char *e;
23b0668c
JB
4013 int sum = 0;
4014 struct stat st;
4015
4016 fstat (fildes, &st);
86a5659e 4017 p = buf;
86a5659e
JB
4018 while (nbytes > 0)
4019 {
23b0668c
JB
4020 int len, retval;
4021
4022 /* Handle fixed-length files with carriage control. */
4023 if (st.st_fab_rfm == FAB$C_FIX
4024 && ((st.st_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
4025 {
4026 len = st.st_fab_mrs;
4027 retval = write (fildes, p, min (len, nbytes));
4028 if (retval != len)
4029 return -1;
4030 retval++; /* This skips the implied carriage control */
4031 }
4032 else
4033 {
4034 e = p + min (MAXIOSIZE, nbytes) - 1;
4035 while (*e != '\n' && e > p) e--;
4036 if (p == e) /* Ok.. so here we add a newline... sigh. */
4037 e = p + min (MAXIOSIZE, nbytes) - 1;
4038 len = e + 1 - p;
4039 retval = write (fildes, p, len);
4040 if (retval != len)
4041 return -1;
4042 }
4043 p += retval;
4044 sum += retval;
86a5659e
JB
4045 nbytes -= retval;
4046 }
4047 return sum;
4048}
4049
4050/* Create file NEW copying its attributes from file OLD. If
4051 OLD is 0 or does not exist, create based on the value of
4052 vms_stmlf_recfm. */
4053
4054/* Protection value the file should ultimately have.
4055 Set by create_copy_attrs, and use by rename_sansversions. */
4056static unsigned short int fab_final_pro;
4057
4058int
4059creat_copy_attrs (old, new)
4060 char *old, *new;
4061{
4062 struct FAB fab = cc$rms_fab;
4063 struct XABPRO xabpro;
4064 char aclbuf[256]; /* Choice of size is arbitrary. See below. */
4065 extern int vms_stmlf_recfm;
4066
4067 if (old)
4068 {
4069 fab.fab$b_fac = FAB$M_GET;
4070 fab.fab$l_fna = old;
4071 fab.fab$b_fns = strlen (old);
4072 fab.fab$l_xab = (char *) &xabpro;
4073 xabpro = cc$rms_xabpro;
4074 xabpro.xab$l_aclbuf = aclbuf;
4075 xabpro.xab$w_aclsiz = sizeof aclbuf;
4076 /* Call $OPEN to fill in the fab & xabpro fields. */
986ffb24 4077 if (SYS$OPEN (&fab, 0, 0) & 1)
86a5659e 4078 {
986ffb24 4079 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
4080 fab.fab$l_alq = 0; /* zero the allocation quantity */
4081 if (xabpro.xab$w_acllen > 0)
4082 {
4083 if (xabpro.xab$w_acllen > sizeof aclbuf)
4084 /* If the acl buffer was too short, redo open with longer one.
4085 Wouldn't need to do this if there were some system imposed
4086 limit on the size of an ACL, but I can't find any such. */
4087 {
4088 xabpro.xab$l_aclbuf = (char *) alloca (xabpro.xab$w_acllen);
4089 xabpro.xab$w_aclsiz = xabpro.xab$w_acllen;
986ffb24
JB
4090 if (SYS$OPEN (&fab, 0, 0) & 1)
4091 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
4092 else
4093 old = 0;
4094 }
4095 }
4096 else
4097 xabpro.xab$l_aclbuf = 0;
4098 }
4099 else
4100 old = 0;
4101 }
4102 fab.fab$l_fna = new;
4103 fab.fab$b_fns = strlen (new);
4104 if (!old)
4105 {
4106 fab.fab$l_xab = 0;
4107 fab.fab$b_rfm = vms_stmlf_recfm ? FAB$C_STMLF : FAB$C_VAR;
4108 fab.fab$b_rat = FAB$M_CR;
4109 }
4110
4111 /* Set the file protections such that we will be able to manipulate
4112 this file. Once we are done writing and renaming it, we will set
4113 the protections back. */
4114 if (old)
4115 fab_final_pro = xabpro.xab$w_pro;
4116 else
986ffb24 4117 SYS$SETDFPROT (0, &fab_final_pro);
86a5659e
JB
4118 xabpro.xab$w_pro &= 0xff0f; /* set O:rewd for now. This is set back later. */
4119
4120 /* Create the new file with either default attrs or attrs copied
4121 from old file. */
4122 if (!(SYS$CREATE (&fab, 0, 0) & 1))
4123 return -1;
986ffb24 4124 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
4125 /* As this is a "replacement" for creat, return a file descriptor
4126 opened for writing. */
4127 return open (new, O_WRONLY);
4128}
4129
4130#ifdef creat
4131#undef creat
4132#include <varargs.h>
4133#ifdef __GNUC__
4134#ifndef va_count
4135#define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
4136#endif
4137#endif
4138
dfcf069d 4139int
86a5659e
JB
4140sys_creat (va_alist)
4141 va_dcl
4142{
eb8c3be9 4143 va_list list_incrementer;
86a5659e
JB
4144 char *name;
4145 int mode;
4146 int rfd; /* related file descriptor */
4147 int fd; /* Our new file descriptor */
4148 int count;
4149 struct stat st_buf;
4150 char rfm[12];
4151 char rat[15];
4152 char mrs[13];
4153 char fsz[13];
4154 extern int vms_stmlf_recfm;
4155
4156 va_count (count);
eb8c3be9
JB
4157 va_start (list_incrementer);
4158 name = va_arg (list_incrementer, char *);
4159 mode = va_arg (list_incrementer, int);
86a5659e 4160 if (count > 2)
eb8c3be9
JB
4161 rfd = va_arg (list_incrementer, int);
4162 va_end (list_incrementer);
86a5659e
JB
4163 if (count > 2)
4164 {
4165 /* Use information from the related file descriptor to set record
4166 format of the newly created file. */
4167 fstat (rfd, &st_buf);
4168 switch (st_buf.st_fab_rfm)
4169 {
4170 case FAB$C_FIX:
4171 strcpy (rfm, "rfm = fix");
4172 sprintf (mrs, "mrs = %d", st_buf.st_fab_mrs);
4173 strcpy (rat, "rat = ");
4174 if (st_buf.st_fab_rat & FAB$M_CR)
4175 strcat (rat, "cr");
4176 else if (st_buf.st_fab_rat & FAB$M_FTN)
4177 strcat (rat, "ftn");
4178 else if (st_buf.st_fab_rat & FAB$M_PRN)
4179 strcat (rat, "prn");
4180 if (st_buf.st_fab_rat & FAB$M_BLK)
4181 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4182 strcat (rat, ", blk");
4183 else
4184 strcat (rat, "blk");
4185 return creat (name, 0, rfm, rat, mrs);
4186
4187 case FAB$C_VFC:
4188 strcpy (rfm, "rfm = vfc");
4189 sprintf (fsz, "fsz = %d", st_buf.st_fab_fsz);
4190 strcpy (rat, "rat = ");
4191 if (st_buf.st_fab_rat & FAB$M_CR)
4192 strcat (rat, "cr");
4193 else if (st_buf.st_fab_rat & FAB$M_FTN)
4194 strcat (rat, "ftn");
4195 else if (st_buf.st_fab_rat & FAB$M_PRN)
4196 strcat (rat, "prn");
4197 if (st_buf.st_fab_rat & FAB$M_BLK)
4198 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4199 strcat (rat, ", blk");
4200 else
4201 strcat (rat, "blk");
4202 return creat (name, 0, rfm, rat, fsz);
4203
4204 case FAB$C_STM:
4205 strcpy (rfm, "rfm = stm");
4206 break;
4207
4208 case FAB$C_STMCR:
4209 strcpy (rfm, "rfm = stmcr");
4210 break;
4211
4212 case FAB$C_STMLF:
4213 strcpy (rfm, "rfm = stmlf");
4214 break;
4215
4216 case FAB$C_UDF:
4217 strcpy (rfm, "rfm = udf");
4218 break;
4219
4220 case FAB$C_VAR:
4221 strcpy (rfm, "rfm = var");
4222 break;
4223 }
4224 strcpy (rat, "rat = ");
4225 if (st_buf.st_fab_rat & FAB$M_CR)
4226 strcat (rat, "cr");
4227 else if (st_buf.st_fab_rat & FAB$M_FTN)
4228 strcat (rat, "ftn");
4229 else if (st_buf.st_fab_rat & FAB$M_PRN)
4230 strcat (rat, "prn");
4231 if (st_buf.st_fab_rat & FAB$M_BLK)
4232 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4233 strcat (rat, ", blk");
4234 else
4235 strcat (rat, "blk");
4236 }
4237 else
4238 {
4239 strcpy (rfm, vms_stmlf_recfm ? "rfm = stmlf" : "rfm=var");
4240 strcpy (rat, "rat=cr");
4241 }
4242 /* Until the VAX C RTL fixes the many bugs with modes, always use
4243 mode 0 to get the user's default protection. */
4244 fd = creat (name, 0, rfm, rat);
4245 if (fd < 0 && errno == EEXIST)
4246 {
4247 if (unlink (name) < 0)
4248 report_file_error ("delete", build_string (name));
4249 fd = creat (name, 0, rfm, rat);
4250 }
4251 return fd;
4252}
4253#endif /* creat */
4254
4255/* fwrite to stdout is S L O W. Speed it up by using fputc...*/
dfcf069d 4256int
86a5659e
JB
4257sys_fwrite (ptr, size, num, fp)
4258 register char * ptr;
4259 FILE * fp;
4260{
4261 register int tot = num * size;
4262
4263 while (tot--)
4264 fputc (*ptr++, fp);
dfcf069d 4265 return num;
86a5659e
JB
4266}
4267
4268/*
4269 * The VMS C library routine creat actually creates a new version of an
4270 * existing file rather than truncating the old version. There are times
4271 * when this is not the desired behavior, for instance, when writing an
4272 * auto save file (you only want one version), or when you don't have
4273 * write permission in the directory containing the file (but the file
177c0ea7 4274 * itself is writable). Hence this routine, which is equivalent to
86a5659e
JB
4275 * "close (creat (fn, 0));" on Unix if fn already exists.
4276 */
4277int
4278vms_truncate (fn)
4279 char *fn;
4280{
4281 struct FAB xfab = cc$rms_fab;
4282 struct RAB xrab = cc$rms_rab;
4283 int status;
4284
4285 xfab.fab$l_fop = FAB$M_TEF; /* free allocated but unused blocks on close */
4286 xfab.fab$b_fac = FAB$M_TRN | FAB$M_GET; /* allow truncate and get access */
4287 xfab.fab$b_shr = FAB$M_NIL; /* allow no sharing - file must be locked */
4288 xfab.fab$l_fna = fn;
4289 xfab.fab$b_fns = strlen (fn);
4290 xfab.fab$l_dna = ";0"; /* default to latest version of the file */
4291 xfab.fab$b_dns = 2;
4292 xrab.rab$l_fab = &xfab;
4293
4294 /* This gibberish opens the file, positions to the first record, and
4295 deletes all records from there until the end of file. */
986ffb24 4296 if ((SYS$OPEN (&xfab) & 01) == 01)
86a5659e 4297 {
986ffb24
JB
4298 if ((SYS$CONNECT (&xrab) & 01) == 01 &&
4299 (SYS$FIND (&xrab) & 01) == 01 &&
4300 (SYS$TRUNCATE (&xrab) & 01) == 01)
86a5659e
JB
4301 status = 0;
4302 else
4303 status = -1;
4304 }
4305 else
4306 status = -1;
986ffb24 4307 SYS$CLOSE (&xfab);
86a5659e
JB
4308 return status;
4309}
4310
4311/* Define this symbol to actually read SYSUAF.DAT. This requires either
4312 SYSPRV or a readable SYSUAF.DAT. */
4313
4314#ifdef READ_SYSUAF
4315/*
4316 * getuaf.c
4317 *
4318 * Routine to read the VMS User Authorization File and return
4319 * a specific user's record.
4320 */
4321
4322static struct UAF retuaf;
4323
4324struct UAF *
4325get_uaf_name (uname)
4326 char * uname;
4327{
4328 register status;
4329 struct FAB uaf_fab;
4330 struct RAB uaf_rab;
177c0ea7 4331
86a5659e
JB
4332 uaf_fab = cc$rms_fab;
4333 uaf_rab = cc$rms_rab;
4334 /* initialize fab fields */
4335 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4336 uaf_fab.fab$b_fns = 21;
4337 uaf_fab.fab$b_fac = FAB$M_GET;
4338 uaf_fab.fab$b_org = FAB$C_IDX;
4339 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4340 /* initialize rab fields */
4341 uaf_rab.rab$l_fab = &uaf_fab;
4342 /* open the User Authorization File */
986ffb24 4343 status = SYS$OPEN (&uaf_fab);
86a5659e
JB
4344 if (!(status&1))
4345 {
4346 errno = EVMSERR;
4347 vaxc$errno = status;
4348 return 0;
4349 }
986ffb24 4350 status = SYS$CONNECT (&uaf_rab);
86a5659e
JB
4351 if (!(status&1))
4352 {
4353 errno = EVMSERR;
4354 vaxc$errno = status;
4355 return 0;
4356 }
4357 /* read the requested record - index is in uname */
4358 uaf_rab.rab$l_kbf = uname;
4359 uaf_rab.rab$b_ksz = strlen (uname);
4360 uaf_rab.rab$b_rac = RAB$C_KEY;
4361 uaf_rab.rab$l_ubf = (char *)&retuaf;
4362 uaf_rab.rab$w_usz = sizeof retuaf;
986ffb24 4363 status = SYS$GET (&uaf_rab);
86a5659e
JB
4364 if (!(status&1))
4365 {
4366 errno = EVMSERR;
4367 vaxc$errno = status;
4368 return 0;
4369 }
4370 /* close the User Authorization File */
986ffb24 4371 status = SYS$DISCONNECT (&uaf_rab);
86a5659e
JB
4372 if (!(status&1))
4373 {
4374 errno = EVMSERR;
4375 vaxc$errno = status;
4376 return 0;
4377 }
986ffb24 4378 status = SYS$CLOSE (&uaf_fab);
86a5659e
JB
4379 if (!(status&1))
4380 {
4381 errno = EVMSERR;
4382 vaxc$errno = status;
4383 return 0;
4384 }
4385 return &retuaf;
4386}
4387
4388struct UAF *
4389get_uaf_uic (uic)
4390 unsigned long uic;
4391{
4392 register status;
4393 struct FAB uaf_fab;
4394 struct RAB uaf_rab;
177c0ea7 4395
86a5659e
JB
4396 uaf_fab = cc$rms_fab;
4397 uaf_rab = cc$rms_rab;
4398 /* initialize fab fields */
4399 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4400 uaf_fab.fab$b_fns = 21;
4401 uaf_fab.fab$b_fac = FAB$M_GET;
4402 uaf_fab.fab$b_org = FAB$C_IDX;
4403 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4404 /* initialize rab fields */
4405 uaf_rab.rab$l_fab = &uaf_fab;
4406 /* open the User Authorization File */
986ffb24 4407 status = SYS$OPEN (&uaf_fab);
86a5659e
JB
4408 if (!(status&1))
4409 {
4410 errno = EVMSERR;
4411 vaxc$errno = status;
4412 return 0;
4413 }
986ffb24 4414 status = SYS$CONNECT (&uaf_rab);
86a5659e
JB
4415 if (!(status&1))
4416 {
4417 errno = EVMSERR;
4418 vaxc$errno = status;
4419 return 0;
4420 }
4421 /* read the requested record - index is in uic */
4422 uaf_rab.rab$b_krf = 1; /* 1st alternate key */
4423 uaf_rab.rab$l_kbf = (char *) &uic;
4424 uaf_rab.rab$b_ksz = sizeof uic;
4425 uaf_rab.rab$b_rac = RAB$C_KEY;
4426 uaf_rab.rab$l_ubf = (char *)&retuaf;
4427 uaf_rab.rab$w_usz = sizeof retuaf;
986ffb24 4428 status = SYS$GET (&uaf_rab);
86a5659e
JB
4429 if (!(status&1))
4430 {
4431 errno = EVMSERR;
4432 vaxc$errno = status;
4433 return 0;
4434 }
4435 /* close the User Authorization File */
986ffb24 4436 status = SYS$DISCONNECT (&uaf_rab);
86a5659e
JB
4437 if (!(status&1))
4438 {
4439 errno = EVMSERR;
4440 vaxc$errno = status;
4441 return 0;
4442 }
986ffb24 4443 status = SYS$CLOSE (&uaf_fab);
86a5659e
JB
4444 if (!(status&1))
4445 {
4446 errno = EVMSERR;
4447 vaxc$errno = status;
4448 return 0;
4449 }
4450 return &retuaf;
4451}
4452
4453static struct passwd retpw;
4454
4455struct passwd *
4456cnv_uaf_pw (up)
4457 struct UAF * up;
4458{
4459 char * ptr;
4460
4461 /* copy these out first because if the username is 32 chars, the next
4462 section will overwrite the first byte of the UIC */
4463 retpw.pw_uid = up->uaf$w_mem;
4464 retpw.pw_gid = up->uaf$w_grp;
4465
ea5a0917 4466 /* I suppose this is not the best style, to possibly overwrite one
86a5659e
JB
4467 byte beyond the end of the field, but what the heck... */
4468 ptr = &up->uaf$t_username[UAF$S_USERNAME];
4469 while (ptr[-1] == ' ')
4470 ptr--;
4471 *ptr = '\0';
4472 strcpy (retpw.pw_name, up->uaf$t_username);
4473
4474 /* the rest of these are counted ascii strings */
4475 strncpy (retpw.pw_gecos, &up->uaf$t_owner[1], up->uaf$t_owner[0]);
4476 retpw.pw_gecos[up->uaf$t_owner[0]] = '\0';
4477 strncpy (retpw.pw_dir, &up->uaf$t_defdev[1], up->uaf$t_defdev[0]);
4478 retpw.pw_dir[up->uaf$t_defdev[0]] = '\0';
4479 strncat (retpw.pw_dir, &up->uaf$t_defdir[1], up->uaf$t_defdir[0]);
4480 retpw.pw_dir[up->uaf$t_defdev[0] + up->uaf$t_defdir[0]] = '\0';
4481 strncpy (retpw.pw_shell, &up->uaf$t_defcli[1], up->uaf$t_defcli[0]);
4482 retpw.pw_shell[up->uaf$t_defcli[0]] = '\0';
4483
4484 return &retpw;
4485}
4486#else /* not READ_SYSUAF */
4487static struct passwd retpw;
4488#endif /* not READ_SYSUAF */
4489
4490struct passwd *
4491getpwnam (name)
4492 char * name;
4493{
4494#ifdef READ_SYSUAF
4495 struct UAF *up;
4496#else
4497 char * user;
4498 char * dir;
4499 unsigned char * full;
4500#endif /* READ_SYSUAF */
4501 char *ptr = name;
4502
4503 while (*ptr)
4504 {
4505 if ('a' <= *ptr && *ptr <= 'z')
4506 *ptr -= 040;
4507 ptr++;
4508 }
4509#ifdef READ_SYSUAF
4510 if (!(up = get_uaf_name (name)))
4511 return 0;
4512 return cnv_uaf_pw (up);
4513#else
4514 if (strcmp (name, getenv ("USER")) == 0)
4515 {
4516 retpw.pw_uid = getuid ();
4517 retpw.pw_gid = getgid ();
4518 strcpy (retpw.pw_name, name);
4519 if (full = egetenv ("FULLNAME"))
4520 strcpy (retpw.pw_gecos, full);
4521 else
4522 *retpw.pw_gecos = '\0';
4523 strcpy (retpw.pw_dir, egetenv ("HOME"));
4524 *retpw.pw_shell = '\0';
4525 return &retpw;
4526 }
4527 else
4528 return 0;
4529#endif /* not READ_SYSUAF */
4530}
4531
4532struct passwd *
4533getpwuid (uid)
4534 unsigned long uid;
4535{
4536#ifdef READ_SYSUAF
4537 struct UAF * up;
4538
4539 if (!(up = get_uaf_uic (uid)))
4540 return 0;
4541 return cnv_uaf_pw (up);
4542#else
4543 if (uid == sys_getuid ())
4544 return getpwnam (egetenv ("USER"));
4545 else
4546 return 0;
4547#endif /* not READ_SYSUAF */
4548}
4549
4550/* return total address space available to the current process. This is
4551 the sum of the current p0 size, p1 size and free page table entries
4552 available. */
dfcf069d 4553int
86a5659e
JB
4554vlimit ()
4555{
4556 int item_code;
4557 unsigned long free_pages;
4558 unsigned long frep0va;
4559 unsigned long frep1va;
4560 register status;
4561
4562 item_code = JPI$_FREPTECNT;
4563 if (((status = LIB$GETJPI (&item_code, 0, 0, &free_pages)) & 1) == 0)
4564 {
4565 errno = EVMSERR;
4566 vaxc$errno = status;
4567 return -1;
4568 }
4569 free_pages *= 512;
4570
4571 item_code = JPI$_FREP0VA;
4572 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep0va)) & 1) == 0)
4573 {
4574 errno = EVMSERR;
4575 vaxc$errno = status;
4576 return -1;
4577 }
4578 item_code = JPI$_FREP1VA;
4579 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep1va)) & 1) == 0)
4580 {
4581 errno = EVMSERR;
4582 vaxc$errno = status;
4583 return -1;
4584 }
4585
4586 return free_pages + frep0va + (0x7fffffff - frep1va);
4587}
4588
dfcf069d 4589int
86a5659e
JB
4590define_logical_name (varname, string)
4591 char *varname;
4592 char *string;
4593{
4594 struct dsc$descriptor_s strdsc =
4595 {strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string};
4596 struct dsc$descriptor_s envdsc =
4597 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4598 struct dsc$descriptor_s lnmdsc =
4599 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4600
4601 return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
4602}
4603
dfcf069d 4604int
86a5659e
JB
4605delete_logical_name (varname)
4606 char *varname;
4607{
4608 struct dsc$descriptor_s envdsc =
4609 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4610 struct dsc$descriptor_s lnmdsc =
4611 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4612
4613 return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
4614}
4615
dfcf069d 4616int
86a5659e 4617ulimit ()
dfcf069d
AS
4618{
4619 return 0;
4620}
86a5659e 4621
dfcf069d 4622int
86a5659e 4623setpgrp ()
dfcf069d
AS
4624{
4625 return 0;
4626}
86a5659e 4627
dfcf069d 4628int
86a5659e
JB
4629execvp ()
4630{
4631 error ("execvp system call not implemented");
dfcf069d 4632 return -1;
86a5659e
JB
4633}
4634
4635int
4636rename (from, to)
4637 char *from, *to;
4638{
4639 int status;
4640 struct FAB from_fab = cc$rms_fab, to_fab = cc$rms_fab;
4641 struct NAM from_nam = cc$rms_nam, to_nam = cc$rms_nam;
4642 char from_esn[NAM$C_MAXRSS];
4643 char to_esn[NAM$C_MAXRSS];
4644
4645 from_fab.fab$l_fna = from;
4646 from_fab.fab$b_fns = strlen (from);
4647 from_fab.fab$l_nam = &from_nam;
4648 from_fab.fab$l_fop = FAB$M_NAM;
4649
4650 from_nam.nam$l_esa = from_esn;
4651 from_nam.nam$b_ess = sizeof from_esn;
4652
4653 to_fab.fab$l_fna = to;
4654 to_fab.fab$b_fns = strlen (to);
4655 to_fab.fab$l_nam = &to_nam;
4656 to_fab.fab$l_fop = FAB$M_NAM;
4657
4658 to_nam.nam$l_esa = to_esn;
4659 to_nam.nam$b_ess = sizeof to_esn;
4660
4661 status = SYS$RENAME (&from_fab, 0, 0, &to_fab);
4662
4663 if (status & 1)
4664 return 0;
4665 else
4666 {
4667 if (status == RMS$_DEV)
4668 errno = EXDEV;
4669 else
4670 errno = EVMSERR;
4671 vaxc$errno = status;
4672 return -1;
4673 }
4674}
4675
4676/* This function renames a file like `rename', but it strips
4677 the version number from the "to" filename, such that the "to" file is
4678 will always be a new version. It also sets the file protection once it is
4679 finished. The protection that we will use is stored in fab_final_pro,
4680 and was set when we did a creat_copy_attrs to create the file that we
4681 are renaming.
4682
4683 We could use the chmod function, but Eunichs uses 3 bits per user category
eb8c3be9 4684 to describe the protection, and VMS uses 4 (write and delete are separate
86a5659e
JB
4685 bits). To maintain portability, the VMS implementation of `chmod' wires
4686 the W and D bits together. */
4687
177c0ea7 4688
86a5659e
JB
4689static struct fibdef fib; /* We need this initialized to zero */
4690char vms_file_written[NAM$C_MAXRSS];
4691
4692int
4693rename_sans_version (from,to)
4694 char *from, *to;
4695{
4696 short int chan;
4697 int stat;
4698 short int iosb[4];
4699 int status;
4700 struct FAB to_fab = cc$rms_fab;
4701 struct NAM to_nam = cc$rms_nam;
4702 struct dsc$descriptor fib_d ={sizeof (fib),0,0,(char*) &fib};
4703 struct dsc$descriptor fib_attr[2]
4704 = {{sizeof (fab_final_pro),ATR$C_FPRO,0,(char*) &fab_final_pro},{0,0,0,0}};
4705 char to_esn[NAM$C_MAXRSS];
4706
4707 $DESCRIPTOR (disk,to_esn);
4708
4709 to_fab.fab$l_fna = to;
4710 to_fab.fab$b_fns = strlen (to);
4711 to_fab.fab$l_nam = &to_nam;
4712 to_fab.fab$l_fop = FAB$M_NAM;
4713
4714 to_nam.nam$l_esa = to_esn;
4715 to_nam.nam$b_ess = sizeof to_esn;
4716
4717 status = SYS$PARSE (&to_fab, 0, 0); /* figure out the full file name */
4718
4719 if (to_nam.nam$l_fnb && NAM$M_EXP_VER)
4720 *(to_nam.nam$l_ver) = '\0';
4721
4722 stat = rename (from, to_esn);
4723 if (stat < 0)
4724 return stat;
4725
4726 strcpy (vms_file_written, to_esn);
4727
4728 to_fab.fab$l_fna = vms_file_written; /* this points to the versionless name */
4729 to_fab.fab$b_fns = strlen (vms_file_written);
4730
4731 /* Now set the file protection to the correct value */
986ffb24 4732 SYS$OPEN (&to_fab, 0, 0); /* This fills in the nam$w_fid fields */
86a5659e
JB
4733
4734 /* Copy these fields into the fib */
4735 fib.fib$r_fid_overlay.fib$w_fid[0] = to_nam.nam$w_fid[0];
4736 fib.fib$r_fid_overlay.fib$w_fid[1] = to_nam.nam$w_fid[1];
4737 fib.fib$r_fid_overlay.fib$w_fid[2] = to_nam.nam$w_fid[2];
4738
986ffb24 4739 SYS$CLOSE (&to_fab, 0, 0);
86a5659e 4740
986ffb24 4741 stat = SYS$ASSIGN (&disk, &chan, 0, 0); /* open a channel to the disk */
86a5659e 4742 if (!stat)
986ffb24
JB
4743 LIB$SIGNAL (stat);
4744 stat = SYS$QIOW (0, chan, IO$_MODIFY, iosb, 0, 0, &fib_d,
86a5659e
JB
4745 0, 0, 0, &fib_attr, 0);
4746 if (!stat)
986ffb24
JB
4747 LIB$SIGNAL (stat);
4748 stat = SYS$DASSGN (chan);
86a5659e 4749 if (!stat)
986ffb24 4750 LIB$SIGNAL (stat);
0137dbf7 4751 strcpy (vms_file_written, to_esn); /* We will write this to the terminal*/
86a5659e
JB
4752 return 0;
4753}
4754
dfcf069d 4755int
86a5659e
JB
4756link (file, new)
4757 char * file, * new;
4758{
4759 register status;
4760 struct FAB fab;
4761 struct NAM nam;
4762 unsigned short fid[3];
4763 char esa[NAM$C_MAXRSS];
4764
4765 fab = cc$rms_fab;
4766 fab.fab$l_fop = FAB$M_OFP;
4767 fab.fab$l_fna = file;
4768 fab.fab$b_fns = strlen (file);
4769 fab.fab$l_nam = &nam;
4770
4771 nam = cc$rms_nam;
4772 nam.nam$l_esa = esa;
4773 nam.nam$b_ess = NAM$C_MAXRSS;
4774
4775 status = SYS$PARSE (&fab);
4776 if ((status & 1) == 0)
4777 {
4778 errno = EVMSERR;
4779 vaxc$errno = status;
4780 return -1;
4781 }
4782 status = SYS$SEARCH (&fab);
4783 if ((status & 1) == 0)
4784 {
4785 errno = EVMSERR;
4786 vaxc$errno = status;
4787 return -1;
4788 }
4789
4790 fid[0] = nam.nam$w_fid[0];
4791 fid[1] = nam.nam$w_fid[1];
4792 fid[2] = nam.nam$w_fid[2];
4793
4794 fab.fab$l_fna = new;
4795 fab.fab$b_fns = strlen (new);
4796
4797 status = SYS$PARSE (&fab);
4798 if ((status & 1) == 0)
4799 {
4800 errno = EVMSERR;
4801 vaxc$errno = status;
4802 return -1;
4803 }
4804
4805 nam.nam$w_fid[0] = fid[0];
4806 nam.nam$w_fid[1] = fid[1];
4807 nam.nam$w_fid[2] = fid[2];
4808
4809 nam.nam$l_esa = nam.nam$l_name;
4810 nam.nam$b_esl = nam.nam$b_name + nam.nam$b_type + nam.nam$b_ver;
4811
4812 status = SYS$ENTER (&fab);
4813 if ((status & 1) == 0)
4814 {
4815 errno = EVMSERR;
4816 vaxc$errno = status;
4817 return -1;
4818 }
4819
4820 return 0;
4821}
4822
dfcf069d 4823void
86a5659e
JB
4824croak (badfunc)
4825 char *badfunc;
4826{
4827 printf ("%s not yet implemented\r\n", badfunc);
28d440ab 4828 reset_all_sys_modes ();
86a5659e
JB
4829 exit (1);
4830}
4831
4832long
4833random ()
4834{
4835 /* Arrange to return a range centered on zero. */
4836 return rand () - (1 << 30);
4837}
4838
dfcf069d 4839void
86a5659e
JB
4840srandom (seed)
4841{
4842 srand (seed);
4843}
4844#endif /* VMS */
4845\f
51417996
RS
4846#ifndef BSTRING
4847
4848#ifndef bzero
4849
4850void
4851bzero (b, length)
4852 register char *b;
4853 register int length;
4854{
4855#ifdef VMS
4856 short zero = 0;
4857 long max_str = 65535;
4858
4859 while (length > max_str) {
4860 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
4861 length -= max_str;
4862 b += max_str;
4863 }
4864 max_str = length;
4865 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
4866#else
4867 while (length-- > 0)
4868 *b++ = 0;
4869#endif /* not VMS */
4870}
4871
4872#endif /* no bzero */
4873#endif /* BSTRING */
4874
c7f93f28 4875#if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
51417996
RS
4876#undef bcopy
4877
4878/* Saying `void' requires a declaration, above, where bcopy is used
4879 and that declaration causes pain for systems where bcopy is a macro. */
4880bcopy (b1, b2, length)
4881 register char *b1;
4882 register char *b2;
4883 register int length;
4884{
4885#ifdef VMS
4886 long max_str = 65535;
4887
4888 while (length > max_str) {
4889 (void) LIB$MOVC3 (&max_str, b1, b2);
4890 length -= max_str;
4891 b1 += max_str;
4892 b2 += max_str;
4893 }
4894 max_str = length;
4895 (void) LIB$MOVC3 (&length, b1, b2);
4896#else
4897 while (length-- > 0)
4898 *b2++ = *b1++;
4899#endif /* not VMS */
4900}
dfcf069d 4901#endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
51417996 4902
c7f93f28 4903#ifndef BSTRING
51417996
RS
4904#ifndef bcmp
4905int
4906bcmp (b1, b2, length) /* This could be a macro! */
4907 register char *b1;
4908 register char *b2;
4909 register int length;
4910{
4911#ifdef VMS
4912 struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
4913 struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
4914
4915 return STR$COMPARE (&src1, &src2);
4916#else
4917 while (length-- > 0)
4918 if (*b1++ != *b2++)
4919 return 1;
4920
4921 return 0;
4922#endif /* not VMS */
4923}
4924#endif /* no bcmp */
4925#endif /* not BSTRING */
68c45bf0
PE
4926\f
4927#ifndef HAVE_STRSIGNAL
4928char *
4929strsignal (code)
4930 int code;
4931{
4932 char *signame = 0;
4933
4934 if (0 <= code && code < NSIG)
4935 {
4936#ifdef VMS
4937 signame = sys_errlist[code];
4938#else
4939 /* Cast to suppress warning if the table has const char *. */
4940 signame = (char *) sys_siglist[code];
4941#endif
4942 }
c4ea52a6 4943
68c45bf0
PE
4944 return signame;
4945}
4946#endif /* HAVE_STRSIGNAL */
d888760c
GM
4947\f
4948#ifdef HAVE_TERMIOS
4949/* For make-serial-process */
4950int serial_open (char *port)
4951{
4952 int fd = -1;
4953
4954 fd = emacs_open ((char*) port,
4955 O_RDWR
4956#ifdef O_NONBLOCK
4957 | O_NONBLOCK
4958#else
4959 | O_NDELAY
4960#endif
4961#ifdef O_NOCTTY
4962 | O_NOCTTY
4963#endif
4964 , 0);
4965 if (fd < 0)
4966 {
4967 error ("Could not open %s: %s",
4968 port, emacs_strerror (errno));
4969 }
4970#ifdef TIOCEXCL
4971 ioctl (fd, TIOCEXCL, (char *) 0);
4972#endif
4973
4974 return fd;
4975}
4976#endif /* TERMIOS */
4977
4978#ifdef HAVE_TERMIOS
759d3f32
SM
4979
4980#if !defined (HAVE_CFMAKERAW)
4981/* Workaround for targets which are missing cfmakeraw. */
4982/* Pasted from man page. */
4983static void cfmakeraw (struct termios *termios_p)
4984{
4985 termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
4986 termios_p->c_oflag &= ~OPOST;
4987 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
4988 termios_p->c_cflag &= ~(CSIZE|PARENB);
4989 termios_p->c_cflag |= CS8;
4990}
4991#endif /* !defined (HAVE_CFMAKERAW */
4992
4993#if !defined (HAVE_CFSETSPEED)
4994/* Workaround for targets which are missing cfsetspeed. */
4995static int cfsetspeed (struct termios *termios_p, speed_t vitesse)
4996{
4997 return (cfsetispeed (termios_p, vitesse)
4998 + cfsetospeed (termios_p, vitesse));
4999}
5000#endif
5001
d888760c
GM
5002/* For serial-process-configure */
5003void
5004serial_configure (struct Lisp_Process *p,
5005 Lisp_Object contact)
5006{
5007 Lisp_Object childp2 = Qnil;
5008 Lisp_Object tem = Qnil;
5009 struct termios attr;
5010 int err = -1;
5011 char summary[4] = "???"; /* This usually becomes "8N1". */
5012
5013 childp2 = Fcopy_sequence (p->childp);
5014
5015 /* Read port attributes and prepare default configuration. */
5016 err = tcgetattr (p->outfd, &attr);
5017 if (err != 0)
5018 error ("tcgetattr() failed: %s", emacs_strerror (errno));
5019 cfmakeraw (&attr);
5020#if defined (CLOCAL)
5021 attr.c_cflag |= CLOCAL;
5022#endif
5023#if defined (CREAD)
5024 attr.c_cflag | CREAD;
5025#endif
5026
5027 /* Configure speed. */
5028 if (!NILP (Fplist_member (contact, QCspeed)))
5029 tem = Fplist_get (contact, QCspeed);
5030 else
5031 tem = Fplist_get (p->childp, QCspeed);
5032 CHECK_NUMBER (tem);
5033 err = cfsetspeed (&attr, XINT (tem));
5034 if (err != 0)
5035 error ("cfsetspeed(%d) failed: %s", XINT (tem), emacs_strerror (errno));
5036 childp2 = Fplist_put (childp2, QCspeed, tem);
5037
5038 /* Configure bytesize. */
5039 if (!NILP (Fplist_member (contact, QCbytesize)))
5040 tem = Fplist_get (contact, QCbytesize);
5041 else
5042 tem = Fplist_get (p->childp, QCbytesize);
5043 if (NILP (tem))
5044 tem = make_number (8);
5045 CHECK_NUMBER (tem);
5046 if (XINT (tem) != 7 && XINT (tem) != 8)
5047 error (":bytesize must be nil (8), 7, or 8");
5048 summary[0] = XINT(tem) + '0';
5049#if defined (CSIZE) && defined (CS7) && defined (CS8)
5050 attr.c_cflag &= ~CSIZE;
5051 attr.c_cflag |= ((XINT (tem) == 7) ? CS7 : CS8);
5052#else
16bad4dd 5053 /* Don't error on bytesize 8, which should be set by cfmakeraw. */
d888760c
GM
5054 if (XINT (tem) != 8)
5055 error ("Bytesize cannot be changed");
5056#endif
5057 childp2 = Fplist_put (childp2, QCbytesize, tem);
5058
5059 /* Configure parity. */
5060 if (!NILP (Fplist_member (contact, QCparity)))
5061 tem = Fplist_get (contact, QCparity);
5062 else
5063 tem = Fplist_get (p->childp, QCparity);
5064 if (!NILP (tem) && !EQ (tem, Qeven) && !EQ (tem, Qodd))
5065 error (":parity must be nil (no parity), `even', or `odd'");
5066#if defined (PARENB) && defined (PARODD) && defined (IGNPAR) && defined (INPCK)
5067 attr.c_cflag &= ~(PARENB | PARODD);
5068 attr.c_iflag &= ~(IGNPAR | INPCK);
5069 if (NILP (tem))
5070 {
5071 summary[1] = 'N';
5072 }
5073 else if (EQ (tem, Qeven))
5074 {
5075 summary[1] = 'E';
5076 attr.c_cflag |= PARENB;
5077 attr.c_iflag |= (IGNPAR | INPCK);
5078 }
5079 else if (EQ (tem, Qodd))
5080 {
5081 summary[1] = 'O';
5082 attr.c_cflag |= (PARENB | PARODD);
5083 attr.c_iflag |= (IGNPAR | INPCK);
5084 }
5085#else
16bad4dd 5086 /* Don't error on no parity, which should be set by cfmakeraw. */
d888760c
GM
5087 if (!NILP (tem))
5088 error ("Parity cannot be configured");
5089#endif
5090 childp2 = Fplist_put (childp2, QCparity, tem);
5091
5092 /* Configure stopbits. */
5093 if (!NILP (Fplist_member (contact, QCstopbits)))
5094 tem = Fplist_get (contact, QCstopbits);
5095 else
5096 tem = Fplist_get (p->childp, QCstopbits);
5097 if (NILP (tem))
5098 tem = make_number (1);
5099 CHECK_NUMBER (tem);
5100 if (XINT (tem) != 1 && XINT (tem) != 2)
5101 error (":stopbits must be nil (1 stopbit), 1, or 2");
5102 summary[2] = XINT (tem) + '0';
5103#if defined (CSTOPB)
5104 attr.c_cflag &= ~CSTOPB;
5105 if (XINT (tem) == 2)
5106 attr.c_cflag |= CSTOPB;
5107#else
16bad4dd 5108 /* Don't error on 1 stopbit, which should be set by cfmakeraw. */
d888760c
GM
5109 if (XINT (tem) != 1)
5110 error ("Stopbits cannot be configured");
5111#endif
5112 childp2 = Fplist_put (childp2, QCstopbits, tem);
5113
5114 /* Configure flowcontrol. */
5115 if (!NILP (Fplist_member (contact, QCflowcontrol)))
5116 tem = Fplist_get (contact, QCflowcontrol);
5117 else
5118 tem = Fplist_get (p->childp, QCflowcontrol);
5119 if (!NILP (tem) && !EQ (tem, Qhw) && !EQ (tem, Qsw))
5120 error (":flowcontrol must be nil (no flowcontrol), `hw', or `sw'");
5121#if defined (CRTSCTS)
5122 attr.c_cflag &= ~CRTSCTS;
5123#endif
5124#if defined (CNEW_RTSCTS)
5125 attr.c_cflag &= ~CNEW_RTSCTS;
5126#endif
5127#if defined (IXON) && defined (IXOFF)
5128 attr.c_iflag &= ~(IXON | IXOFF);
5129#endif
5130 if (NILP (tem))
5131 {
5132 /* Already configured. */
5133 }
5134 else if (EQ (tem, Qhw))
5135 {
5136#if defined (CRTSCTS)
5137 attr.c_cflag |= CRTSCTS;
5138#elif defined (CNEW_RTSCTS)
5139 attr.c_cflag |= CNEW_RTSCTS;
5140#else
5141 error ("Hardware flowcontrol (RTS/CTS) not supported");
5142#endif
5143 }
5144 else if (EQ (tem, Qsw))
5145 {
5146#if defined (IXON) && defined (IXOFF)
5147 attr.c_iflag |= (IXON | IXOFF);
5148#else
5149 error ("Software flowcontrol (XON/XOFF) not supported");
5150#endif
5151 }
5152 childp2 = Fplist_put (childp2, QCflowcontrol, tem);
5153
5154 /* Activate configuration. */
5155 err = tcsetattr (p->outfd, TCSANOW, &attr);
5156 if (err != 0)
5157 error ("tcsetattr() failed: %s", emacs_strerror (errno));
5158
5159 childp2 = Fplist_put (childp2, QCsummary, build_string (summary));
5160 p->childp = childp2;
5161
5162}
5163#endif /* TERMIOS */
c4ea52a6 5164
ab5796a9
MB
5165/* arch-tag: edb43589-4e09-4544-b325-978b5b121dcf
5166 (do not change this comment) */