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