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