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