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