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