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