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