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