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