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