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