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