1 /* Interfaces to system-dependent kernel and library entries.
2 Copyright (C) 1985, 86, 87, 88, 93, 94 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include "blockinput.h"
29 #define min(x,y) ((x) > (y) ? (y) : (x))
31 /* In this file, open, read and write refer to the system calls,
32 not our sugared interfaces sys_open, sys_read and sys_write.
33 Contrariwise, for systems where we use the system calls directly,
34 define sys_read, etc. here as aliases for them. */
37 #define sys_write write
38 #endif /* `read' is not a macro */
44 #define sys_close close
51 #else /* `open' is a macro */
53 #endif /* `open' is a macro */
55 /* Does anyone other than VMS need this? */
57 #define sys_fwrite fwrite
63 #include <sys/types.h>
67 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
71 #include <sys/param.h>
96 #define MAXIOSIZE ( 32 * PAGESIZE ) /* Don't I/O more than 32 blocks at a time */
100 #ifdef BSD /* this is done this way to avoid defined (BSD) || defined (USG)
101 because the vms compiler doesn't grok `defined' */
109 #endif /* not 4.1 bsd */
112 /* On some systems (DGUX comes to mind real fast) FASYNC causes
113 background writes to the terminal to stop all processes in the
114 process group when invoked under the csh (and probably any shell
115 with job control). This stops Emacs dead in its tracks when coming
121 #include <sys/ioctl.h>
126 #ifdef BROKEN_TIOCGWINSZ
131 #include <sys/utsname.h>
133 #ifndef MEMORY_IN_STRING_H
138 #include <sys/sioctl.h>
141 #include <sys/stream.h>
142 #include <sys/ptem.h>
144 #endif /* TIOCGWINSZ */
147 extern int quit_char
;
151 #include "termhooks.h"
152 #include "termchar.h"
153 #include "termopts.h"
154 #include "dispextern.h"
157 #ifdef NONSYSTEM_DIR_LIBRARY
159 #endif /* NONSYSTEM_DIR_LIBRARY */
161 #include "syssignal.h"
164 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
170 #define LNOFLSH 0100000
173 static int baud_convert
[] =
178 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
179 1800, 2400, 4800, 9600, 19200, 38400
185 /* The file descriptor for Emacs's input terminal.
186 Under Unix, this is normaly zero except when using X;
187 under VMS, we place the input channel number here.
188 This allows us to write more code that works for both VMS and Unix. */
191 /* Specify a different file descriptor for further input operations. */
200 /* Discard pending input on descriptor input_fd. */
204 struct emacs_tty buf
;
209 /* Discarding input is not safe when the input could contain
210 replies from the X server. So don't do it. */
211 if (read_socket_hook
)
216 SYS$
QIOW (0, input_fd
, IO$_READVBLK
|IO$M_PURGE
, input_iosb
, 0, 0,
217 &buf
.main
, 0, 0, terminator_mask
, 0, 0);
223 ioctl (input_fd
, TIOCFLUSH
, &zero
);
225 #else /* not Apollo */
226 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
227 while (dos_keyread () != -1)
229 #else /* not MSDOS */
230 EMACS_GET_TTY (input_fd
, &buf
);
231 EMACS_SET_TTY (input_fd
, &buf
, 0);
232 #endif /* not MSDOS */
233 #endif /* not Apollo */
239 /* Arrange for character C to be read as the next input from
245 /* Should perhaps error if in batch mode */
247 ioctl (input_fd
, TIOCSTI
, &c
);
248 #else /* no TIOCSTI */
249 error ("Cannot stuff terminal input characters in this version of Unix");
250 #endif /* no TIOCSTI */
267 SYS$
QIOW (0, input_fd
, IO$_SENSEMODE
, &sg
, 0, 0,
268 &sg
.class, 12, 0, 0, 0, 0 );
269 ospeed
= sg
.xmit_baud
;
275 tcgetattr (input_fd
, &sg
);
276 ospeed
= cfgetospeed (&sg
);
277 #else /* neither VMS nor TERMIOS */
283 tcgetattr (input_fd
, &sg
);
285 ioctl (input_fd
, TCGETA
, &sg
);
287 ospeed
= sg
.c_cflag
& CBAUD
;
288 #else /* neither VMS nor TERMIOS nor TERMIO */
291 sg
.sg_ospeed
= B9600
;
292 if (ioctl (input_fd
, TIOCGETP
, &sg
) < 0)
294 ospeed
= sg
.sg_ospeed
;
295 #endif /* not HAVE_TERMIO */
296 #endif /* not HAVE_TERMIOS */
298 #endif /* not MSDOS */
301 baud_rate
= (ospeed
< sizeof baud_convert
/ sizeof baud_convert
[0]
302 ? baud_convert
[ospeed
] : 9600);
308 set_exclusive_use (fd
)
312 ioctl (fd
, FIOCLEX
, 0);
314 /* Ok to do nothing if this feature does not exist */
319 wait_without_blocking ()
322 wait3 (0, WNOHANG
| WUNTRACED
, 0);
324 croak ("wait_without_blocking");
326 synch_process_alive
= 0;
329 #endif /* not subprocesses */
331 int wait_debugging
; /* Set nonzero to make following function work under dbx
332 (at least for bsd). */
335 wait_for_termination_signal ()
338 /* Wait for subprocess with process id `pid' to terminate and
339 make sure it will get eliminated (not remain forever as a zombie) */
341 wait_for_termination (pid
)
350 status
= SYS$
FORCEX (&pid
, 0, 0);
353 #if defined (BSD) || (defined (HPUX) && !defined (HPUX_5))
354 /* Note that kill returns -1 even if the process is just a zombie now.
355 But inevitably a SIGCHLD interrupt should be generated
356 and child_sig will do wait3 and make the process go away. */
357 /* There is some indication that there is a bug involved with
358 termination of subprocesses, perhaps involving a kernel bug too,
359 but no idea what it is. Just as a hunch we signal SIGCHLD to see
360 if that causes the problem to go away or get worse. */
361 sigsetmask (sigmask (SIGCHLD
));
362 if (0 > kill (pid
, 0))
364 sigsetmask (SIGEMPTYMASK
);
365 kill (getpid (), SIGCHLD
);
371 sigpause (SIGEMPTYMASK
);
372 #else /* not BSD, and not HPUX version >= 6 */
373 #if defined (UNIPLUS)
374 if (0 > kill (pid
, 0))
377 #else /* neither BSD nor UNIPLUS: random sysV */
378 #ifdef POSIX_SIGNALS /* would this work for LINUX as well? */
379 sigblock (sigmask (SIGCHLD
));
380 if (0 > kill (pid
, 0))
382 sigunblock (sigmask (SIGCHLD
));
385 sigpause (SIGEMPTYMASK
);
386 #else /* not POSIX_SIGNALS */
387 #ifdef HAVE_SYSV_SIGPAUSE
389 if (0 > kill (pid
, 0))
395 #else /* not HAVE_SYSV_SIGPAUSE */
396 if (0 > kill (pid
, 0))
398 /* Using sleep instead of pause avoids timing error.
399 If the inferior dies just before the sleep,
400 we lose just one second. */
402 #endif /* not HAVE_SYSV_SIGPAUSE */
403 #endif /* not POSIX_SIGNALS */
404 #endif /* not UNIPLUS */
405 #endif /* not BSD, and not HPUX version >= 6 */
407 #else /* not subprocesses */
409 if (kill (pid
, 0) < 0)
415 if (status
== pid
|| status
== -1)
418 #endif /* not subprocesses */
425 * flush any pending output
426 * (may flush input as well; it does not matter the way we use it)
429 flush_pending_output (channel
)
433 /* If we try this, we get hit with SIGTTIN, because
434 the child's tty belongs to the child's pgrp. */
437 ioctl (channel
, TCFLSH
, 1);
441 /* 3rd arg should be ignored
442 but some 4.2 kernels actually want the address of an int
443 and nonzero means something different. */
444 ioctl (channel
, TIOCFLUSH
, &zero
);
451 /* Set up the terminal at the other end of a pseudo-terminal that
452 we will be controlling an inferior through.
453 It should not echo or do line-editing, since that is done
454 in Emacs. No padding needed for insertion into an Emacs buffer. */
456 child_setup_tty (out
)
462 EMACS_GET_TTY (out
, &s
);
464 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
465 s
.main
.c_oflag
|= OPOST
; /* Enable output postprocessing */
466 s
.main
.c_oflag
&= ~ONLCR
; /* Disable map of NL to CR-NL on output */
467 s
.main
.c_oflag
&= ~(NLDLY
|CRDLY
|TABDLY
|BSDLY
|VTDLY
|FFDLY
);
468 /* No output delays */
469 s
.main
.c_lflag
&= ~ECHO
; /* Disable echo */
470 s
.main
.c_lflag
|= ISIG
; /* Enable signals */
471 s
.main
.c_iflag
&= ~IUCLC
; /* Disable map of upper case to lower on
473 s
.main
.c_oflag
&= ~OLCUC
; /* Disable map of lower case to upper on
475 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CSIZE
) | CS8
; /* Don't strip 8th bit */
477 /* Said to be unnecessary: */
478 s
.main
.c_cc
[VMIN
] = 1; /* minimum number of characters to accept */
479 s
.main
.c_cc
[VTIME
] = 0; /* wait forever for at least 1 character */
482 s
.main
.c_lflag
|= ICANON
; /* Enable erase/kill and eof processing */
483 s
.main
.c_cc
[VEOF
] = 04; /* insure that EOF is Control-D */
484 s
.main
.c_cc
[VERASE
] = 0377; /* disable erase processing */
485 s
.main
.c_cc
[VKILL
] = 0377; /* disable kill processing */
488 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CBAUD
) | B9600
; /* baud rate sanity */
492 /* AIX enhanced edit loses NULs, so disable it */
495 s
.main
.c_iflag
&= ~ASCEDIT
;
497 /* Also, PTY overloads NUL and BREAK.
498 don't ignore break, but don't signal either, so it looks like NUL. */
499 s
.main
.c_iflag
&= ~IGNBRK
;
500 s
.main
.c_iflag
&= ~BRKINT
;
501 /* QUIT and INTR work better as signals, so disable character forms */
502 s
.main
.c_cc
[VINTR
] = 0377;
503 #ifdef SIGNALS_VIA_CHARACTERS
504 /* the QUIT and INTR character are used in process_send_signal
505 so set them here to something useful. */
506 if (s
.main
.c_cc
[VQUIT
] == 0377)
507 s
.main
.c_cc
[VQUIT
] = '\\'&037; /* Control-\ */
508 if (s
.main
.c_cc
[VINTR
] == 0377)
509 s
.main
.c_cc
[VINTR
] = 'C'&037; /* Control-C */
510 #else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
511 /* QUIT and INTR work better as signals, so disable character forms */
512 s
.main
.c_cc
[VQUIT
] = 0377;
513 s
.main
.c_cc
[VINTR
] = 0377;
514 s
.main
.c_lflag
&= ~ISIG
;
515 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
516 s
.main
.c_cc
[VEOL
] = 0377;
517 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CBAUD
) | B9600
; /* baud rate sanity */
520 #else /* not HAVE_TERMIO */
522 s
.main
.sg_flags
&= ~(ECHO
| CRMOD
| ANYP
| ALLDELAY
| RAW
| LCASE
524 s
.main
.sg_flags
|= LPASS8
;
525 s
.main
.sg_erase
= 0377;
526 s
.main
.sg_kill
= 0377;
527 s
.lmode
= LLITOUT
| s
.lmode
; /* Don't strip 8th bit */
529 #endif /* not HAVE_TERMIO */
531 EMACS_SET_TTY (out
, &s
, 0);
540 ioctl (out
, FIOASYNC
, &zero
);
543 #endif /* not MSDOS */
547 #endif /* subprocesses */
553 EMACS_SET_TTY_PGRP (input_fd
, &pid
);
556 /* Record a signal code and the handler for it. */
560 SIGTYPE (*handler
) ();
563 /* Suspend the Emacs process; give terminal to its superior. */
567 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
569 char oldwd
[MAXPATHLEN
+1]; /* Fixed length is safe on MSDOS. */
572 /* "Foster" parentage allows emacs to return to a subprocess that attached
573 to the current emacs as a cheaper than starting a whole new process. This
574 is set up by KEPTEDITOR.COM. */
575 unsigned long parent_id
, foster_parent_id
;
578 fpid_string
= getenv ("EMACS_PARENT_PID");
579 if (fpid_string
!= NULL
)
581 sscanf (fpid_string
, "%x", &foster_parent_id
);
582 if (foster_parent_id
!= 0)
583 parent_id
= foster_parent_id
;
585 parent_id
= getppid ();
588 parent_id
= getppid ();
590 xfree (fpid_string
); /* On VMS, this was malloc'd */
592 if (parent_id
&& parent_id
!= 0xffffffff)
594 SIGTYPE (*oldsig
)() = (int) signal (SIGINT
, SIG_IGN
);
595 int status
= LIB$
ATTACH (&parent_id
) & 1;
596 signal (SIGINT
, oldsig
);
605 d_prompt
.l
= sizeof ("Emacs: "); /* Our special prompt */
606 d_prompt
.a
= "Emacs: "; /* Just a reminder */
607 LIB$
SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt
, 0);
612 #if defined(SIGTSTP) && !defined(MSDOS)
615 int pgrp
= EMACS_GETPGRP (0);
616 EMACS_KILLPG (pgrp
, SIGTSTP
);
619 #else /* No SIGTSTP */
620 #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
621 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
622 kill (getpid (), SIGQUIT
);
624 #else /* No SIGTSTP or USG_JOBCTRL */
626 /* On a system where suspending is not implemented,
627 instead fork a subshell and let it talk directly to the terminal
630 struct save_signal saved_handlers
[5];
632 saved_handlers
[0].code
= SIGINT
;
633 saved_handlers
[1].code
= SIGQUIT
;
634 saved_handlers
[2].code
= SIGTERM
;
636 saved_handlers
[3].code
= SIGIO
;
637 saved_handlers
[4].code
= 0;
639 saved_handlers
[3].code
= 0;
643 error ("Can't spawn subshell");
648 #ifdef MSDOS /* MW, Aug 1993 */
651 sh
= (char *) egetenv ("SHELL");
655 /* Use our buffer's default directory for the subshell. */
661 /* mentioning current_buffer->buffer would mean including buffer.h,
662 which somehow wedges the hp compiler. So instead... */
664 dir
= intern ("default-directory");
666 if (XFASTINT (Fboundp (dir
)) == XFASTINT (Qnil
))
668 dir
= Fsymbol_value (dir
);
669 if (XTYPE (dir
) != Lisp_String
)
672 str
= (unsigned char *) alloca (XSTRING (dir
)->size
+ 2);
673 len
= XSTRING (dir
)->size
;
674 bcopy (XSTRING (dir
)->data
, str
, len
);
675 if (str
[len
- 1] != '/') str
[len
++] = '/';
681 close_process_descs (); /* Close Emacs's pipes/ptys */
686 extern int emacs_priority
;
689 nice (-emacs_priority
);
693 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
697 report_file_error ("Can't execute subshell", Fcons (build_string (sh
), Qnil
));
698 #else /* not MSDOS */
700 write (1, "Can't execute subshell", 22);
702 #endif /* not MSDOS */
705 save_signal_handlers (saved_handlers
);
706 synch_process_alive
= 1;
707 wait_for_termination (pid
);
708 restore_signal_handlers (saved_handlers
);
710 #endif /* no USG_JOBCTRL */
711 #endif /* no SIGTSTP */
715 save_signal_handlers (saved_handlers
)
716 struct save_signal
*saved_handlers
;
718 while (saved_handlers
->code
)
720 saved_handlers
->handler
721 = (SIGTYPE (*) ()) signal (saved_handlers
->code
, SIG_IGN
);
726 restore_signal_handlers (saved_handlers
)
727 struct save_signal
*saved_handlers
;
729 while (saved_handlers
->code
)
731 signal (saved_handlers
->code
, saved_handlers
->handler
);
743 old_fcntl_flags
= fcntl (input_fd
, F_GETFL
, 0) & ~FASYNC
;
753 #ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
758 sigunblock (sigmask (SIGWINCH
));
760 fcntl (input_fd
, F_SETFL
, old_fcntl_flags
| FASYNC
);
762 interrupts_deferred
= 0;
768 sigblock (sigmask (SIGWINCH
));
770 fcntl (input_fd
, F_SETFL
, old_fcntl_flags
);
771 interrupts_deferred
= 1;
774 #else /* no FASYNC */
775 #ifdef STRIDE /* Stride doesn't have FASYNC - use FIOASYNC */
780 ioctl (input_fd
, FIOASYNC
, &on
);
781 interrupts_deferred
= 0;
788 ioctl (input_fd
, FIOASYNC
, &off
);
789 interrupts_deferred
= 1;
792 #else /* not FASYNC, not STRIDE */
804 sigaddset(&st
, SIGIO
);
805 ioctl (input_fd
, FIOASYNC
, &on
);
806 interrupts_deferred
= 0;
807 sigprocmask(SIG_UNBLOCK
, &st
, (sigset_t
*)0);
814 ioctl (input_fd
, FIOASYNC
, &off
);
815 interrupts_deferred
= 1;
822 croak ("request_sigio");
827 croak ("unrequest_sigio");
835 /* Saving and restoring the process group of Emacs's terminal. */
839 /* The process group of which Emacs was a member when it initially
842 If Emacs was in its own process group (i.e. inherited_pgroup ==
843 getpid ()), then we know we're running under a shell with job
844 control (Emacs would never be run as part of a pipeline).
847 If Emacs was not in its own process group, then we know we're
848 running under a shell (or a caller) that doesn't know how to
849 separate itself from Emacs (like sh). Emacs must be in its own
850 process group in order to receive SIGIO correctly. In this
851 situation, we put ourselves in our own pgroup, forcibly set the
852 tty's pgroup to our pgroup, and make sure to restore and reinstate
853 the tty's pgroup just like any other terminal setting. If
854 inherited_group was not the tty's pgroup, then we'll get a
855 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
856 it goes foreground in the future, which is what should happen. */
857 int inherited_pgroup
;
859 /* Split off the foreground process group to Emacs alone.
860 When we are in the foreground, but not started in our own process
861 group, redirect the TTY to point to our own process group. We need
862 to be in our own process group to receive SIGIO properly. */
863 narrow_foreground_group ()
867 setpgrp (0, inherited_pgroup
);
868 if (inherited_pgroup
!= me
)
869 EMACS_SET_TTY_PGRP (input_fd
, &me
);
873 /* Set the tty to our original foreground group. */
874 widen_foreground_group ()
876 if (inherited_pgroup
!= getpid ())
877 EMACS_SET_TTY_PGRP (input_fd
, &inherited_pgroup
);
878 setpgrp (0, inherited_pgroup
);
883 /* Getting and setting emacs_tty structures. */
885 /* Set *TC to the parameters associated with the terminal FD.
886 Return zero if all's well, or -1 if we ran into an error we
887 couldn't deal with. */
889 emacs_get_tty (fd
, settings
)
891 struct emacs_tty
*settings
;
893 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
895 /* We have those nifty POSIX tcmumbleattr functions. */
896 if (tcgetattr (fd
, &settings
->main
) < 0)
901 /* The SYSV-style interface? */
902 if (ioctl (fd
, TCGETA
, &settings
->main
) < 0)
907 /* Vehemently Monstrous System? :-) */
908 if (! (SYS$
QIOW (0, fd
, IO$_SENSEMODE
, settings
, 0, 0,
909 &settings
->main
.class, 12, 0, 0, 0, 0)
915 /* I give up - I hope you have the BSD ioctls. */
916 if (ioctl (fd
, TIOCGETP
, &settings
->main
) < 0)
918 #endif /* not MSDOS */
923 /* Suivant - Do we have to get struct ltchars data? */
925 if (ioctl (fd
, TIOCGLTC
, &settings
->ltchars
) < 0)
929 /* How about a struct tchars and a wordful of lmode bits? */
931 if (ioctl (fd
, TIOCGETC
, &settings
->tchars
) < 0
932 || ioctl (fd
, TIOCLGET
, &settings
->lmode
) < 0)
936 /* We have survived the tempest. */
941 /* Set the parameters of the tty on FD according to the contents of
942 *SETTINGS. If WAITP is non-zero, we wait for all queued output to
943 be written before making the change; otherwise, we forget any
944 queued input and make the change immediately.
945 Return 0 if all went well, and -1 if anything failed. */
947 emacs_set_tty (fd
, settings
, waitp
)
949 struct emacs_tty
*settings
;
952 /* Set the primary parameters - baud rate, character size, etcetera. */
955 /* We have those nifty POSIX tcmumbleattr functions.
956 William J. Smith <wjs@wiis.wang.com> writes:
957 "POSIX 1003.1 defines tcsetattr() to return success if it was
958 able to perform any of the requested actions, even if some
959 of the requested actions could not be performed.
960 We must read settings back to ensure tty setup properly.
961 AIX requires this to keep tty from hanging occasionally." */
962 /* This make sure that we don't loop indefinitely in here. */
963 for (i
= 0 ; i
< 10 ; i
++)
964 if (tcsetattr (fd
, waitp
? TCSAFLUSH
: TCSADRAIN
, &settings
->main
) < 0)
975 /* Get the current settings, and see if they're what we asked for. */
976 tcgetattr (fd
, &new);
977 /* We cannot use memcmp on the whole structure here because under
978 * aix386 the termios structure has some reserved field that may
981 if ( new.c_iflag
== settings
->main
.c_iflag
982 && new.c_oflag
== settings
->main
.c_oflag
983 && new.c_cflag
== settings
->main
.c_cflag
984 && new.c_lflag
== settings
->main
.c_lflag
985 && memcmp(new.c_cc
, settings
->main
.c_cc
, NCCS
) == 0)
993 /* The SYSV-style interface? */
994 if (ioctl (fd
, waitp
? TCSETAW
: TCSETAF
, &settings
->main
) < 0)
999 /* Vehemently Monstrous System? :-) */
1000 if (! (SYS$
QIOW (0, fd
, IO$_SETMODE
, &input_iosb
, 0, 0,
1001 &settings
->main
.class, 12, 0, 0, 0, 0)
1007 /* I give up - I hope you have the BSD ioctls. */
1008 if (ioctl (fd
, (waitp
) ? TIOCSETP
: TIOCSETN
, &settings
->main
) < 0)
1010 #endif /* not MSDOS */
1016 /* Suivant - Do we have to get struct ltchars data? */
1018 if (ioctl (fd
, TIOCSLTC
, &settings
->ltchars
) < 0)
1022 /* How about a struct tchars and a wordful of lmode bits? */
1024 if (ioctl (fd
, TIOCSETC
, &settings
->tchars
) < 0
1025 || ioctl (fd
, TIOCLSET
, &settings
->lmode
) < 0)
1029 /* We have survived the tempest. */
1034 /* The initial tty mode bits */
1035 struct emacs_tty old_tty
;
1037 int term_initted
; /* 1 if outer tty status has been recorded */
1040 /* BSD 4.1 needs to keep track of the lmode bits in order to start
1045 #ifndef F_SETOWN_BUG
1047 int old_fcntl_owner
;
1048 #endif /* F_SETOWN */
1049 #endif /* F_SETOWN_BUG */
1051 /* This may also be defined in stdio,
1052 but if so, this does no harm,
1053 and using the same name avoids wasting the other one's space. */
1055 #if defined (USG) || defined (DGUX)
1056 unsigned char _sobuf
[BUFSIZ
+8];
1058 char _sobuf
[BUFSIZ
];
1062 static struct ltchars new_ltchars
= {-1,-1,-1,-1,-1,-1};
1065 static struct tchars new_tchars
= {-1,-1,-1,-1,-1,-1};
1070 struct emacs_tty tty
;
1074 static int oob_chars
[2] = {0, 1 << 7}; /* catch C-g's */
1075 extern int (*interrupt_signal
) ();
1084 input_ef
= get_kbd_event_flag ();
1085 /* LIB$GET_EF (&input_ef); */
1086 SYS$
CLREF (input_ef
);
1087 waiting_for_ast
= 0;
1089 timer_ef
= get_timer_event_flag ();
1090 /* LIB$GET_EF (&timer_ef); */
1091 SYS$
CLREF (timer_ef
);
1095 LIB$
GET_EF (&process_ef
);
1096 SYS$
CLREF (process_ef
);
1098 if (input_ef
/ 32 != process_ef
/ 32)
1099 croak ("Input and process event flags in different clusters.");
1101 if (input_ef
/ 32 != timer_ef
/ 32)
1102 croak ("Input and timer event flags in different clusters.");
1104 input_eflist
= ((unsigned) 1 << (input_ef
% 32)) |
1105 ((unsigned) 1 << (process_ef
% 32));
1107 timer_eflist
= ((unsigned) 1 << (input_ef
% 32)) |
1108 ((unsigned) 1 << (timer_ef
% 32));
1110 sys_access_reinit ();
1112 #endif /* not VMS */
1115 if (! read_socket_hook
&& EQ (Vwindow_system
, Qnil
))
1116 narrow_foreground_group ();
1119 EMACS_GET_TTY (input_fd
, &old_tty
);
1121 if (!read_socket_hook
&& EQ (Vwindow_system
, Qnil
))
1125 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
1126 tty
.main
.c_iflag
|= (IGNBRK
); /* Ignore break condition */
1127 tty
.main
.c_iflag
&= ~ICRNL
; /* Disable map of CR to NL on input */
1129 tty
.main
.c_iflag
&= ~ISTRIP
; /* don't strip 8th bit on input */
1131 tty
.main
.c_lflag
&= ~ECHO
; /* Disable echo */
1132 tty
.main
.c_lflag
&= ~ICANON
; /* Disable erase/kill processing */
1134 tty
.main
.c_iflag
&= ~IEXTEN
; /* Disable other editing characters. */
1136 tty
.main
.c_lflag
|= ISIG
; /* Enable signals */
1139 tty
.main
.c_iflag
|= IXON
; /* Enable start/stop output control */
1141 tty
.main
.c_iflag
&= ~IXANY
;
1145 tty
.main
.c_iflag
&= ~IXON
; /* Disable start/stop output control */
1146 tty
.main
.c_oflag
&= ~ONLCR
; /* Disable map of NL to CR-NL
1148 tty
.main
.c_oflag
&= ~TAB3
; /* Disable tab expansion */
1152 tty
.main
.c_cflag
|= CS8
; /* allow 8th bit on input */
1153 tty
.main
.c_cflag
&= ~PARENB
;/* Don't check parity */
1156 tty
.main
.c_cc
[VINTR
] = quit_char
; /* C-g (usually) gives SIGINT */
1157 /* Set up C-g for both SIGQUIT and SIGINT.
1158 We don't know which we will get, but we handle both alike
1159 so which one it really gives us does not matter. */
1160 tty
.main
.c_cc
[VQUIT
] = quit_char
;
1161 tty
.main
.c_cc
[VMIN
] = 1; /* Input should wait for at least 1 char */
1162 tty
.main
.c_cc
[VTIME
] = 0; /* no matter how long that takes. */
1164 tty
.main
.c_cc
[VSWTCH
] = CDISABLE
; /* Turn off shell layering use
1167 #if defined (mips) || defined (HAVE_TCATTR)
1169 tty
.main
.c_cc
[VSUSP
] = CDISABLE
; /* Turn off mips handling of C-z. */
1172 tty
.main
.c_cc
[V_DSUSP
] = CDISABLE
; /* Turn off mips handling of C-y. */
1173 #endif /* V_DSUSP */
1174 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1175 tty
.main
.c_cc
[VDSUSP
] = CDISABLE
;
1178 tty
.main
.c_cc
[VLNEXT
] = CDISABLE
;
1181 tty
.main
.c_cc
[VREPRINT
] = CDISABLE
;
1182 #endif /* VREPRINT */
1184 tty
.main
.c_cc
[VWERASE
] = CDISABLE
;
1185 #endif /* VWERASE */
1187 tty
.main
.c_cc
[VDISCARD
] = CDISABLE
;
1188 #endif /* VDISCARD */
1189 #endif /* mips or HAVE_TCATTR */
1192 /* AIX enhanced edit loses NULs, so disable it */
1193 tty
.main
.c_line
= 0;
1194 tty
.main
.c_iflag
&= ~ASCEDIT
;
1196 tty
.main
.c_cc
[VSTRT
] = 255;
1197 tty
.main
.c_cc
[VSTOP
] = 255;
1198 tty
.main
.c_cc
[VSUSP
] = 255;
1199 tty
.main
.c_cc
[VDSUSP
] = 255;
1200 #endif /* IBMR2AIX */
1201 /* Also, PTY overloads NUL and BREAK.
1202 don't ignore break, but don't signal either, so it looks like NUL.
1203 This really serves a purpose only if running in an XTERM window
1204 or via TELNET or the like, but does no harm elsewhere. */
1205 tty
.main
.c_iflag
&= ~IGNBRK
;
1206 tty
.main
.c_iflag
&= ~BRKINT
;
1208 #else /* if not HAVE_TERMIO */
1210 tty
.main
.tt_char
|= TT$M_NOECHO
;
1212 tty
.main
.tt_char
|= TT$M_EIGHTBIT
;
1214 tty
.main
.tt_char
|= TT$M_TTSYNC
;
1216 tty
.main
.tt_char
&= ~TT$M_TTSYNC
;
1217 tty
.main
.tt2_char
|= TT2$M_PASTHRU
| TT2$M_XON
;
1218 #else /* not VMS (BSD, that is) */
1220 tty
.main
.sg_flags
&= ~(ECHO
| CRMOD
| XTABS
);
1222 tty
.main
.sg_flags
|= ANYP
;
1223 tty
.main
.sg_flags
|= interrupt_input
? RAW
: CBREAK
;
1225 #endif /* not VMS (BSD, that is) */
1226 #endif /* not HAVE_TERMIO */
1228 /* If going to use CBREAK mode, we must request C-g to interrupt
1229 and turn off start and stop chars, etc. If not going to use
1230 CBREAK mode, do this anyway so as to turn off local flow
1231 control for user coming over network on 4.2; in this case,
1232 only t_stopc and t_startc really matter. */
1235 /* Note: if not using CBREAK mode, it makes no difference how we
1237 tty
.tchars
= new_tchars
;
1238 tty
.tchars
.t_intrc
= quit_char
;
1241 tty
.tchars
.t_startc
= '\021';
1242 tty
.tchars
.t_stopc
= '\023';
1245 tty
.lmode
= LDECCTQ
| LLITOUT
| LPASS8
| LNOFLSH
| old_tty
.lmode
;
1247 /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
1248 anything, and leaving it in breaks the meta key. Go figure. */
1249 tty
.lmode
&= ~LLITOUT
;
1256 #endif /* HAVE_TCHARS */
1257 #endif /* not HAVE_TERMIO */
1260 tty
.ltchars
= new_ltchars
;
1261 #endif /* HAVE_LTCHARS */
1262 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
1263 internal_terminal_init ();
1267 EMACS_SET_TTY (input_fd
, &tty
, 0);
1269 /* This code added to insure that, if flow-control is not to be used,
1270 we have an unlocked terminal at the start. */
1273 if (!flow_control
) ioctl (input_fd
, TCXONC
, 1);
1277 if (!flow_control
) ioctl (input_fd
, TIOCSTART
, 0);
1285 /* IBM's HFT device usually thinks a ^J should be LF/CR. We need it
1286 to be only LF. This is the way that is done. */
1289 if (ioctl (1, HFTGETID
, &tty
) != -1)
1290 write (1, "\033[20l", 5);
1296 /* Appears to do nothing when in PASTHRU mode.
1297 SYS$QIOW (0, input_fd, IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
1298 interrupt_signal, oob_chars, 0, 0, 0, 0);
1300 queue_kbd_input (0);
1305 #ifndef F_SETOWN_BUG
1306 #ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
1307 if (interrupt_input
)
1309 old_fcntl_owner
= fcntl (input_fd
, F_GETOWN
, 0);
1310 fcntl (input_fd
, F_SETOWN
, getpid ());
1313 #endif /* F_GETOWN */
1314 #endif /* F_SETOWN_BUG */
1315 #endif /* F_SETFL */
1318 if (interrupt_input
)
1322 #ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1326 /* This symbol is defined on recent USG systems.
1327 Someone says without this call USG won't really buffer the file
1328 even with a call to setbuf. */
1329 setvbuf (stdout
, _sobuf
, _IOFBF
, sizeof _sobuf
);
1331 setbuf (stdout
, _sobuf
);
1333 set_terminal_modes ();
1334 if (term_initted
&& no_redraw_on_reenter
)
1336 if (display_completed
)
1337 direct_output_forward_char (0);
1343 if (FRAMEP (Vterminal_frame
))
1344 FRAME_GARBAGED_P (XFRAME (Vterminal_frame
)) = 1;
1351 /* Return nonzero if safe to use tabs in output.
1352 At the time this is called, init_sys_modes has not been done yet. */
1356 struct emacs_tty tty
;
1358 EMACS_GET_TTY (input_fd
, &tty
);
1359 return EMACS_TTY_TABS_OK (&tty
);
1362 /* Get terminal size from system.
1363 Store number of lines into *heightp and width into *widthp.
1364 If zero or a negative number is stored, the value is not valid. */
1366 get_frame_size (widthp
, heightp
)
1367 int *widthp
, *heightp
;
1373 struct winsize size
;
1375 if (ioctl (input_fd
, TIOCGWINSZ
, &size
) == -1)
1376 *widthp
= *heightp
= 0;
1379 *widthp
= size
.ws_col
;
1380 *heightp
= size
.ws_row
;
1386 /* SunOS - style. */
1387 struct ttysize size
;
1389 if (ioctl (input_fd
, TIOCGSIZE
, &size
) == -1)
1390 *widthp
= *heightp
= 0;
1393 *widthp
= size
.ts_cols
;
1394 *heightp
= size
.ts_lines
;
1400 struct sensemode tty
;
1402 SYS$
QIOW (0, input_fd
, IO$_SENSEMODE
, &tty
, 0, 0,
1403 &tty
.class, 12, 0, 0, 0, 0);
1404 *widthp
= tty
.scr_wid
;
1405 *heightp
= tty
.scr_len
;
1409 *widthp
= ScreenCols ();
1410 *heightp
= ScreenRows ();
1411 #else /* system doesn't know size */
1416 #endif /* not VMS */
1417 #endif /* not SunOS-style */
1418 #endif /* not BSD-style */
1422 /* Prepare the terminal for exiting Emacs; move the cursor to the
1423 bottom of the frame, turn off interrupt-driven I/O, etc. */
1433 if (read_socket_hook
|| !EQ (Vwindow_system
, Qnil
))
1435 cursor_to (FRAME_HEIGHT (selected_frame
) - 1, 0);
1436 clear_end_of_line (FRAME_WIDTH (selected_frame
));
1437 /* clear_end_of_line may move the cursor */
1438 cursor_to (FRAME_HEIGHT (selected_frame
) - 1, 0);
1441 /* HFT devices normally use ^J as a LF/CR. We forced it to
1442 do the LF only. Now, we need to reset it. */
1445 if (ioctl (1, HFTGETID
, &tty
) != -1)
1446 write (1, "\033[20h", 5);
1450 reset_terminal_modes ();
1454 /* Avoid possible loss of output when changing terminal modes. */
1455 fsync (fileno (stdout
));
1460 #ifndef F_SETOWN_BUG
1461 #ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
1462 if (interrupt_input
)
1465 fcntl (input_fd
, F_SETOWN
, old_fcntl_owner
);
1467 #endif /* F_SETOWN */
1468 #endif /* F_SETOWN_BUG */
1469 #endif /* F_SETFL */
1471 if (interrupt_input
)
1475 while (EMACS_SET_TTY (input_fd
, &old_tty
, 0) < 0 && errno
== EINTR
)
1478 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1487 widen_foreground_group ();
1493 /* Set up the proper status flags for use of a pty. */
1498 /* I'm told that TOICREMOTE does not mean control chars
1499 "can't be sent" but rather that they don't have
1500 input-editing or signaling effects.
1501 That should be good, because we have other ways
1502 to do those things in Emacs.
1503 However, telnet mode seems not to work on 4.2.
1504 So TIOCREMOTE is turned off now. */
1506 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1507 will hang. In particular, the "timeout" feature (which
1508 causes a read to return if there is no data available)
1509 does this. Also it is known that telnet mode will hang
1510 in such a way that Emacs must be stopped (perhaps this
1511 is the same problem).
1513 If TIOCREMOTE is turned off, then there is a bug in
1514 hp-ux which sometimes loses data. Apparently the
1515 code which blocks the master process when the internal
1516 buffer fills up does not work. Other than this,
1517 though, everything else seems to work fine.
1519 Since the latter lossage is more benign, we may as well
1520 lose that way. -- cph */
1525 ioctl (fd
, FIONBIO
, &on
);
1530 /* On AIX, the parent gets SIGHUP when a pty attached child dies. So, we */
1531 /* ignore SIGHUP once we've started a child on a pty. Note that this may */
1532 /* cause EMACS not to die when it should, i.e., when its own controlling */
1533 /* tty goes away. I've complained to the AIX developers, and they may */
1534 /* change this behavior, but I'm not going to hold my breath. */
1535 signal (SIGHUP
, SIG_IGN
);
1538 #endif /* HAVE_PTYS */
1542 /* Assigning an input channel is done at the start of Emacs execution.
1543 This is called each time Emacs is resumed, also, but does nothing
1544 because input_chain is no longer zero. */
1552 status
= SYS$
ASSIGN (&input_dsc
, &input_fd
, 0, 0);
1558 /* Deassigning the input channel is done before exiting. */
1562 return SYS$
DASSGN (input_fd
);
1567 /* Request reading one character into the keyboard buffer.
1568 This is done as soon as the buffer becomes empty. */
1573 extern kbd_input_ast ();
1575 waiting_for_ast
= 0;
1577 status
= SYS$
QIO (0, input_fd
, IO$_READVBLK
,
1578 &input_iosb
, kbd_input_ast
, 1,
1579 &input_buffer
, 1, 0, terminator_mask
, 0, 0);
1584 /* Ast routine that is called when keyboard input comes in
1585 in accord with the SYS$QIO above. */
1589 register int c
= -1;
1590 int old_errno
= errno
;
1591 extern EMACS_TIME
*input_available_clear_time
;
1593 if (waiting_for_ast
)
1594 SYS$
SETEF (input_ef
);
1595 waiting_for_ast
= 0;
1598 if (input_count
== 25)
1600 printf ("Ast # %d,", input_count
);
1601 printf (" iosb = %x, %x, %x, %x",
1602 input_iosb
.offset
, input_iosb
.status
, input_iosb
.termlen
,
1605 if (input_iosb
.offset
)
1609 printf (", char = 0%o", c
);
1621 struct input_event e
;
1622 e
.kind
= ascii_keystroke
;
1623 XSET (e
.code
, Lisp_Int
, c
);
1625 XSET(e
.frame_or_window
, Lisp_Frame
, selected_frame
);
1627 e
.frame_or_window
= Qnil
;
1629 kbd_buffer_store_event (&e
);
1631 if (input_available_clear_time
)
1632 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
1636 /* Wait until there is something in kbd_buffer. */
1638 wait_for_kbd_input ()
1640 extern int have_process_input
, process_exited
;
1642 /* If already something, avoid doing system calls. */
1643 if (detect_input_pending ())
1647 /* Clear a flag, and tell ast routine above to set it. */
1648 SYS$
CLREF (input_ef
);
1649 waiting_for_ast
= 1;
1650 /* Check for timing error: ast happened while we were doing that. */
1651 if (!detect_input_pending ())
1653 /* No timing error: wait for flag to be set. */
1654 set_waiting_for_input (0);
1655 SYS$
WFLOR (input_ef
, input_eflist
);
1656 clear_waiting_for_input (0);
1657 if (!detect_input_pending ())
1658 /* Check for subprocess input availability */
1660 int dsp
= have_process_input
|| process_exited
;
1662 SYS$
CLREF (process_ef
);
1663 if (have_process_input
)
1664 process_command_input ();
1669 update_mode_lines
++;
1670 prepare_menu_bars ();
1671 redisplay_preserve_echo_area ();
1675 waiting_for_ast
= 0;
1678 /* Get rid of any pending QIO, when we are about to suspend
1679 or when we want to throw away pending input.
1680 We wait for a positive sign that the AST routine has run
1681 and therefore there is no I/O request queued when we return.
1682 SYS$SETAST is used to avoid a timing error. */
1687 printf ("At end_kbd_input.\n");
1691 if (LIB$
AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
1693 SYS$
CANCEL (input_fd
);
1698 /* Clear a flag, and tell ast routine above to set it. */
1699 SYS$
CLREF (input_ef
);
1700 waiting_for_ast
= 1;
1702 SYS$
CANCEL (input_fd
);
1704 SYS$
WAITFR (input_ef
);
1705 waiting_for_ast
= 0;
1708 /* Wait for either input available or time interval expiry. */
1710 input_wait_timeout (timeval
)
1711 int timeval
; /* Time to wait, in seconds */
1714 static int zero
= 0;
1715 static int large
= -10000000;
1717 LIB$
EMUL (&timeval
, &large
, &zero
, time
); /* Convert to VMS format */
1719 /* If already something, avoid doing system calls. */
1720 if (detect_input_pending ())
1724 /* Clear a flag, and tell ast routine above to set it. */
1725 SYS$
CLREF (input_ef
);
1726 waiting_for_ast
= 1;
1727 /* Check for timing error: ast happened while we were doing that. */
1728 if (!detect_input_pending ())
1730 /* No timing error: wait for flag to be set. */
1732 if (SYS$
SETIMR (timer_ef
, time
, 0, 1) & 1) /* Set timer */
1733 SYS$
WFLOR (timer_ef
, timer_eflist
); /* Wait for timer expiry or input */
1735 waiting_for_ast
= 0;
1738 /* The standard `sleep' routine works some other way
1739 and it stops working if you have ever quit out of it.
1740 This one continues to work. */
1746 static int zero
= 0;
1747 static int large
= -10000000;
1749 LIB$
EMUL (&timeval
, &large
, &zero
, time
); /* Convert to VMS format */
1752 if (SYS$
SETIMR (timer_ef
, time
, 0, 1) & 1) /* Set timer */
1753 SYS$
WAITFR (timer_ef
); /* Wait for timer expiry only */
1768 croak ("request sigio");
1773 croak ("unrequest sigio");
1778 /* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
1783 #ifndef SYSTEM_MALLOC
1790 /* Some systems that cannot dump also cannot implement these. */
1793 * Return the address of the start of the text segment prior to
1794 * doing an unexec. After unexec the return value is undefined.
1795 * See crt0.c for further explanation and _start.
1799 #ifndef CANNOT_UNEXEC
1804 return ((char *) TEXT_START
);
1808 return ((char *) csrt
);
1809 #else /* not GOULD */
1810 extern int _start ();
1811 return ((char *) _start
);
1813 #endif /* TEXT_START */
1815 #endif /* not CANNOT_UNEXEC */
1818 * Return the address of the start of the data segment prior to
1819 * doing an unexec. After unexec the return value is undefined.
1820 * See crt0.c for further information and definition of data_start.
1822 * Apparently, on BSD systems this is etext at startup. On
1823 * USG systems (swapping) this is highly mmu dependent and
1824 * is also dependent on whether or not the program is running
1825 * with shared text. Generally there is a (possibly large)
1826 * gap between end of text and start of data with shared text.
1828 * On Uniplus+ systems with shared text, data starts at a
1829 * fixed address. Each port (from a given oem) is generally
1830 * different, and the specific value of the start of data can
1831 * be obtained via the UniPlus+ specific "uvar" system call,
1832 * however the method outlined in crt0.c seems to be more portable.
1834 * Probably what will have to happen when a USG unexec is available,
1835 * at least on UniPlus, is temacs will have to be made unshared so
1836 * that text and data are contiguous. Then once loadup is complete,
1837 * unexec will produce a shared executable where the data can be
1838 * at the normal shared text boundry and the startofdata variable
1839 * will be patched by unexec to the correct value.
1847 return ((char *) DATA_START
);
1849 #ifdef ORDINARY_LINK
1851 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
1852 * data_start isn't defined. We take the address of environ, which
1853 * is known to live at or near the start of the system crt0.c, and
1854 * we don't sweat the handful of bytes that might lose.
1856 extern char **environ
;
1858 return((char *) &environ
);
1860 extern int data_start
;
1861 return ((char *) &data_start
);
1862 #endif /* ORDINARY_LINK */
1863 #endif /* DATA_START */
1865 #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
1868 /* Some systems that cannot dump also cannot implement these. */
1871 * Return the address of the end of the text segment prior to
1872 * doing an unexec. After unexec the return value is undefined.
1879 return ((char *) TEXT_END
);
1882 return ((char *) &etext
);
1887 * Return the address of the end of the data segment prior to
1888 * doing an unexec. After unexec the return value is undefined.
1895 return ((char *) DATA_END
);
1898 return ((char *) &edata
);
1902 #endif /* not CANNOT_DUMP */
1904 /* Get_system_name returns as its value
1905 a string for the Lisp function system-name to return. */
1911 /* Can't have this within the function since `static' is #defined to
1912 nothing for some USG systems. */
1914 #ifdef HAVE_GETHOSTNAME
1915 static char get_system_name_name
[256];
1916 #else /* not HAVE_GETHOSTNAME */
1917 static struct utsname get_system_name_name
;
1918 #endif /* not HAVE_GETHOSTNAME */
1925 #include <sys/socket.h>
1927 #endif /* HAVE_SOCKETS */
1928 #endif /* not VMS */
1929 #endif /* not USG */
1930 #endif /* not BSD4_1 */
1936 #ifdef HAVE_GETHOSTNAME
1937 gethostname (get_system_name_name
, sizeof (get_system_name_name
));
1938 return get_system_name_name
;
1939 #else /* not HAVE_GETHOSTNAME */
1940 uname (&get_system_name_name
);
1941 return (get_system_name_name
.nodename
);
1942 #endif /* not HAVE_GETHOSTNAME */
1946 #else /* not USG, not 4.1 */
1947 static char system_name_saved
[32];
1950 if ((sp
= egetenv ("SYS$NODE")) == 0)
1956 if ((end
= index (sp
, ':')) != 0)
1959 strcpy (system_name_saved
, sp
);
1961 gethostname (system_name_saved
, sizeof (system_name_saved
));
1963 /* Turn the hostname into the official, fully-qualified hostname.
1964 Don't do this if we're going to dump; this can confuse system
1965 libraries on some machines and make the dumped emacs core dump. */
1968 #endif /* not CANNOT_DUMP */
1971 hp
= gethostbyname (system_name_saved
);
1972 if (hp
&& strlen (hp
->h_name
) < sizeof(system_name_saved
))
1973 strcpy (system_name_saved
, hp
->h_name
);
1974 if (hp
&& !index (system_name_saved
, '.'))
1976 /* We still don't have a fully qualified domain name.
1977 Try to find one in the list of alternate names */
1978 char **alias
= hp
->h_aliases
;
1979 while (*alias
&& !index (*alias
, '.'))
1981 if (*alias
&& strlen (*alias
) < sizeof (system_name_saved
))
1982 strcpy (system_name_saved
, *alias
);
1985 #endif /* HAVE_SOCKETS */
1986 #endif /* not VMS */
1987 return system_name_saved
;
1988 #endif /* not USG, not 4.1 */
1989 #endif /* not USG */
1993 #ifndef HAVE_GETHOSTNAME
1994 void gethostname(buf
, len
)
1999 s
= getenv ("SYS$NODE");
2003 strncpy (buf
, s
, len
- 2);
2004 buf
[len
- 1] = '\0';
2006 } /* static void gethostname */
2007 #endif /* ! HAVE_GETHOSTNAME */
2014 #ifdef HAVE_X_WINDOWS
2015 /* Cause explanatory error message at compile time,
2016 since the select emulation is not good enough for X. */
2017 int *x
= &x_windows_lose_if_no_select_system_call
;
2020 /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2021 * Only checks read descriptors.
2023 /* How long to wait between checking fds in select */
2024 #define SELECT_PAUSE 1
2027 /* For longjmp'ing back to read_input_waiting. */
2029 jmp_buf read_alarm_throw
;
2031 /* Nonzero if the alarm signal should throw back to read_input_waiting.
2032 The read_socket_hook function sets this to 1 while it is waiting. */
2034 int read_alarm_should_throw
;
2042 #else /* not BSD4_1 */
2043 signal (SIGALRM
, SIG_IGN
);
2044 #endif /* not BSD4_1 */
2045 if (read_alarm_should_throw
)
2046 longjmp (read_alarm_throw
, 1);
2049 /* Only rfds are checked. */
2051 select (nfds
, rfds
, wfds
, efds
, timeout
)
2053 int *rfds
, *wfds
, *efds
, *timeout
;
2055 int ravail
= 0, orfds
= 0, old_alarm
;
2056 int timeoutval
= timeout
? *timeout
: 100000;
2057 int *local_timeout
= &timeoutval
;
2058 extern int proc_buffered_char
[];
2059 #ifndef subprocesses
2060 int process_tick
= 0, update_tick
= 0;
2062 extern int process_tick
, update_tick
;
2064 SIGTYPE (*old_trap
) ();
2077 /* If we are looking only for the terminal, with no timeout,
2078 just read it and wait -- that's more efficient. */
2079 if (orfds
== 1 && *local_timeout
== 100000 && process_tick
== update_tick
)
2081 if (! detect_input_pending ())
2082 read_input_waiting ();
2087 /* Once a second, till the timer expires, check all the flagged read
2088 * descriptors to see if any input is available. If there is some then
2089 * set the corresponding bit in the return copy of rfds.
2093 register int to_check
, bit
, fd
;
2097 for (to_check
= nfds
, bit
= 1, fd
= 0; --to_check
>= 0; bit
<<= 1, fd
++)
2101 int avail
= 0, status
= 0;
2104 avail
= detect_input_pending (); /* Special keyboard handler */
2108 status
= ioctl (fd
, FIONREAD
, &avail
);
2109 #else /* no FIONREAD */
2111 abort (); /* I don't think we need it. */
2112 #else /* not MSDOS */
2113 /* Hoping it will return -1 if nothing available
2114 or 0 if all 0 chars requested are read. */
2115 if (proc_buffered_char
[fd
] >= 0)
2119 avail
= read (fd
, &buf
, 1);
2121 proc_buffered_char
[fd
] = buf
;
2123 #endif /* not MSDOS */
2124 #endif /* no FIONREAD */
2126 if (status
>= 0 && avail
> 0)
2134 if (*local_timeout
== 0 || ravail
!= 0 || process_tick
!= update_tick
)
2136 old_alarm
= alarm (0);
2137 old_trap
= signal (SIGALRM
, select_alarm
);
2139 alarm (SELECT_PAUSE
);
2140 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2141 while (select_alarmed
== 0 && *local_timeout
!= 0
2142 && process_tick
== update_tick
)
2145 sleep_or_kbd_hit (SELECT_PAUSE
, (orfds
& 1) != 0);
2147 #else /* not MSDOS */
2148 /* If we are interested in terminal input,
2149 wait by reading the terminal.
2150 That makes instant wakeup for terminal input at least. */
2153 read_input_waiting ();
2154 if (detect_input_pending ())
2159 #endif /* not MSDOS */
2161 (*local_timeout
) -= SELECT_PAUSE
;
2162 /* Reset the old alarm if there was one */
2164 signal (SIGALRM
, old_trap
);
2167 /* Reset or forge an interrupt for the original handler. */
2168 old_alarm
-= SELECT_PAUSE
;
2170 kill (getpid (), SIGALRM
); /* Fake an alarm with the orig' handler */
2174 if (*local_timeout
== 0) /* Stop on timer being cleared */
2180 /* Read keyboard input into the standard buffer,
2181 waiting for at least one character. */
2183 /* Make all keyboard buffers much bigger when using X windows. */
2184 #ifdef HAVE_X_WINDOWS
2185 #define BUFFER_SIZE_FACTOR 16
2187 #define BUFFER_SIZE_FACTOR 1
2190 read_input_waiting ()
2192 struct input_event e
;
2194 extern int quit_char
;
2196 if (read_socket_hook
)
2198 struct input_event buf
[256];
2200 read_alarm_should_throw
= 0;
2201 if (! setjmp (read_alarm_throw
))
2202 nread
= (*read_socket_hook
) (0, buf
, 256, 1, 0);
2206 /* Scan the chars for C-g and store them in kbd_buffer. */
2207 for (i
= 0; i
< nread
; i
++)
2209 kbd_buffer_store_event (&buf
[i
]);
2210 /* Don't look at input that follows a C-g too closely.
2211 This reduces lossage due to autorepeat on C-g. */
2212 if (buf
[i
].kind
== ascii_keystroke
2213 && XINT(buf
[i
].code
) == quit_char
)
2220 nread
= read (fileno (stdin
), buf
, 1);
2222 /* Scan the chars for C-g and store them in kbd_buffer. */
2223 e
.kind
= ascii_keystroke
;
2224 e
.frame_or_window
= selected_frame
;
2226 for (i
= 0; i
< nread
; i
++)
2228 /* Convert chars > 0177 to meta events if desired.
2229 We do this under the same conditions that read_avail_input does. */
2230 if (read_socket_hook
== 0)
2232 /* If the user says she has a meta key, then believe her. */
2233 if (meta_key
== 1 && (buf
[i
] & 0x80))
2234 e
.modifiers
= meta_modifier
;
2239 XSET (e
.code
, Lisp_Int
, buf
[i
]);
2240 kbd_buffer_store_event (&e
);
2241 /* Don't look at input that follows a C-g too closely.
2242 This reduces lossage due to autorepeat on C-g. */
2243 if (buf
[i
] == quit_char
)
2249 #endif /* not HAVE_SELECT */
2250 #endif /* not VMS */
2254 * Partially emulate 4.2 open call.
2255 * open is defined as this in 4.1.
2257 * - added by Michael Bloom @ Citicorp/TTI
2262 sys_open (path
, oflag
, mode
)
2266 if (oflag
& O_CREAT
)
2267 return creat (path
, mode
);
2269 return open (path
, oflag
);
2276 lmode
= LINTRUP
| lmode
;
2277 ioctl (0, TIOCLSET
, &lmode
);
2284 lmode
= ~LINTRUP
& lmode
;
2285 ioctl (0, TIOCLSET
, &lmode
);
2292 interrupts_deferred
= 0;
2299 interrupts_deferred
= 1;
2302 /* still inside #ifdef BSD4_1 */
2305 int sigheld
; /* Mask of held signals */
2310 sigheld
|= sigbit (signum
);
2317 sigheld
|= sigbit (signum
);
2323 sigheld
&= ~sigbit (signum
);
2327 sigfree () /* Free all held signals */
2330 for (i
= 0; i
< NSIG
; i
++)
2331 if (sigheld
& sigbit (i
))
2338 return 1 << (i
- 1);
2340 #endif /* subprocesses */
2343 /* POSIX signals support - DJB */
2344 /* Anyone with POSIX signals should have ANSI C declarations */
2346 #ifdef POSIX_SIGNALS
2348 sigset_t old_mask
, empty_mask
, full_mask
, temp_mask
;
2349 static struct sigaction new_action
, old_action
;
2353 sigemptyset (&empty_mask
);
2354 sigfillset (&full_mask
);
2358 sys_signal (int signal_number
, signal_handler_t action
)
2361 /* This gets us restartable system calls for efficiency.
2362 The "else" code will works as well. */
2363 return (berk_signal (signal_number
, action
));
2365 sigemptyset (&new_action
.sa_mask
);
2366 new_action
.sa_handler
= action
;
2368 /* Emacs mostly works better with restartable system services. If this
2369 * flag exists, we probably want to turn it on here.
2371 new_action
.sa_flags
= SA_RESTART
;
2373 new_action
.sa_flags
= 0;
2375 sigaction (signal_number
, &new_action
, &old_action
);
2376 return (old_action
.sa_handler
);
2381 /* If we're compiling with GCC, we don't need this function, since it
2382 can be written as a macro. */
2384 sys_sigmask (int sig
)
2387 sigemptyset (&mask
);
2388 sigaddset (&mask
, sig
);
2394 sys_sigpause (sigset_t new_mask
)
2396 /* pause emulating berk sigpause... */
2397 sigsuspend (&new_mask
);
2401 /* I'd like to have these guys return pointers to the mask storage in here,
2402 but there'd be trouble if the code was saving multiple masks. I'll be
2403 safe and pass the structure. It normally won't be more than 2 bytes
2407 sys_sigblock (sigset_t new_mask
)
2410 sigprocmask (SIG_BLOCK
, &new_mask
, &old_mask
);
2415 sys_sigunblock (sigset_t new_mask
)
2418 sigprocmask (SIG_UNBLOCK
, &new_mask
, &old_mask
);
2423 sys_sigsetmask (sigset_t new_mask
)
2426 sigprocmask (SIG_SETMASK
, &new_mask
, &old_mask
);
2430 #endif /* POSIX_SIGNALS */
2439 register int length
;
2443 long max_str
= 65535;
2445 while (length
> max_str
) {
2446 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &max_str
, b
);
2451 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &max_str
, b
);
2453 while (length
-- > 0)
2455 #endif /* not VMS */
2458 #endif /* no bzero */
2461 /* Saying `void' requires a declaration, above, where bcopy is used
2462 and that declaration causes pain for systems where bcopy is a macro. */
2463 bcopy (b1
, b2
, length
)
2466 register int length
;
2469 long max_str
= 65535;
2471 while (length
> max_str
) {
2472 (void) LIB$
MOVC3 (&max_str
, b1
, b2
);
2478 (void) LIB$
MOVC3 (&length
, b1
, b2
);
2480 while (length
-- > 0)
2482 #endif /* not VMS */
2484 #endif /* no bcopy */
2488 bcmp (b1
, b2
, length
) /* This could be a macro! */
2491 register int length
;
2494 struct dsc$descriptor_s src1
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b1
};
2495 struct dsc$descriptor_s src2
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b2
};
2497 return STR$
COMPARE (&src1
, &src2
);
2499 while (length
-- > 0)
2504 #endif /* not VMS */
2506 #endif /* no bcmp */
2508 #endif /* not BSTRING */
2513 * The BSD random returns numbers in the range of
2514 * 0 to 2e31 - 1. The USG rand returns numbers in the
2515 * range of 0 to 2e15 - 1. This is probably not significant
2522 /* Arrange to return a range centered on zero. */
2523 return (rand () << 15) + rand () - (1 << 29);
2537 /* Arrange to return a range centered on zero. */
2538 return (rand () << 15) + rand () - (1 << 29);
2549 #ifdef WRONG_NAME_INSQUE
2562 /* If any place else asks for the TERM variable,
2563 allow it to be overridden with the EMACS_TERM variable
2564 before attempting to translate the logical name TERM. As a last
2565 resort, ask for VAX C's special idea of the TERM variable. */
2572 static char buf
[256];
2573 static struct dsc$descriptor_s equiv
2574 = {sizeof (buf
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, buf
};
2575 static struct dsc$descriptor_s d_name
2576 = {0, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, 0};
2579 if (!strcmp (name
, "TERM"))
2581 val
= (char *) getenv ("EMACS_TERM");
2586 d_name
.dsc$w_length
= strlen (name
);
2587 d_name
.dsc$a_pointer
= name
;
2588 if (LIB$
SYS_TRNLOG (&d_name
, &eqlen
, &equiv
) == 1)
2590 char *str
= (char *) xmalloc (eqlen
+ 1);
2591 bcopy (buf
, str
, eqlen
);
2593 /* This is a storage leak, but a pain to fix. With luck,
2594 no one will ever notice. */
2597 return (char *) getenv (name
);
2602 /* Since VMS doesn't believe in core dumps, the only way to debug this beast is
2603 to force a call on the debugger from within the image. */
2608 LIB$
SIGNAL (SS$_DEBUG
);
2614 #ifdef LINK_CRTL_SHARE
2615 #ifdef SHAREABLE_LIB_BUG
2616 /* Variables declared noshare and initialized in sharable libraries
2617 cannot be shared. The VMS linker incorrectly forces you to use a private
2618 version which is uninitialized... If not for this "feature", we
2619 could use the C library definition of sys_nerr and sys_errlist. */
2621 char *sys_errlist
[] =
2625 "no such file or directory",
2627 "interrupted system call",
2629 "no such device or address",
2630 "argument list too long",
2631 "exec format error",
2634 "no more processes",
2635 "not enough memory",
2636 "permission denied",
2638 "block device required",
2639 "mount devices busy",
2641 "cross-device link",
2646 "file table overflow",
2647 "too many open files",
2651 "no space left on device",
2653 "read-only file system",
2659 "vax/vms specific error code nontranslatable error"
2661 #endif /* SHAREABLE_LIB_BUG */
2662 #endif /* LINK_CRTL_SHARE */
2665 #ifndef HAVE_STRERROR
2670 extern char *sys_errlist
[];
2671 extern int sys_nerr
;
2673 if (errnum
>= 0 && errnum
< sys_nerr
)
2674 return sys_errlist
[errnum
];
2675 return (char *) "Unknown error";
2678 #endif /* ! HAVE_STRERROR */
2680 #ifdef INTERRUPTIBLE_OPEN
2684 sys_open (path
, oflag
, mode
)
2688 register int rtnval
;
2690 while ((rtnval
= open (path
, oflag
, mode
)) == -1
2691 && (errno
== EINTR
));
2695 #endif /* INTERRUPTIBLE_OPEN */
2697 #ifdef INTERRUPTIBLE_CLOSE
2702 register int rtnval
;
2704 while ((rtnval
= close (fd
)) == -1
2705 && (errno
== EINTR
));
2709 #endif /* INTERRUPTIBLE_CLOSE */
2711 #ifdef INTERRUPTIBLE_IO
2714 sys_read (fildes
, buf
, nbyte
)
2719 register int rtnval
;
2721 while ((rtnval
= read (fildes
, buf
, nbyte
)) == -1
2722 && (errno
== EINTR
));
2727 sys_write (fildes
, buf
, nbyte
)
2732 register int rtnval
, bytes_written
;
2738 rtnval
= write (fildes
, buf
, nbyte
);
2750 bytes_written
+= rtnval
;
2752 return (bytes_written
);
2755 #endif /* INTERRUPTIBLE_IO */
2760 * Substitute fork for vfork on USG flavors.
2768 #endif /* not HAVE_VFORK */
2772 * All of the following are for USG.
2774 * On USG systems the system calls are INTERRUPTIBLE by signals
2775 * that the user program has elected to catch. Thus the system call
2776 * must be retried in these cases. To handle this without massive
2777 * changes in the source code, we remap the standard system call names
2778 * to names for our own functions in sysdep.c that do the system call
2779 * with retries. Actually, for portability reasons, it is good
2780 * programming practice, as this example shows, to limit all actual
2781 * system calls to a single occurrence in the source. Sure, this
2782 * adds an extra level of function call overhead but it is almost
2783 * always negligible. Fred Fish, Unisoft Systems Inc.
2786 #ifndef HAVE_SYS_SIGLIST
2787 char *sys_siglist
[NSIG
+ 1] =
2790 /* AIX has changed the signals a bit */
2791 "bogus signal", /* 0 */
2792 "hangup", /* 1 SIGHUP */
2793 "interrupt", /* 2 SIGINT */
2794 "quit", /* 3 SIGQUIT */
2795 "illegal instruction", /* 4 SIGILL */
2796 "trace trap", /* 5 SIGTRAP */
2797 "IOT instruction", /* 6 SIGIOT */
2798 "crash likely", /* 7 SIGDANGER */
2799 "floating point exception", /* 8 SIGFPE */
2800 "kill", /* 9 SIGKILL */
2801 "bus error", /* 10 SIGBUS */
2802 "segmentation violation", /* 11 SIGSEGV */
2803 "bad argument to system call", /* 12 SIGSYS */
2804 "write on a pipe with no one to read it", /* 13 SIGPIPE */
2805 "alarm clock", /* 14 SIGALRM */
2806 "software termination signum", /* 15 SIGTERM */
2807 "user defined signal 1", /* 16 SIGUSR1 */
2808 "user defined signal 2", /* 17 SIGUSR2 */
2809 "death of a child", /* 18 SIGCLD */
2810 "power-fail restart", /* 19 SIGPWR */
2811 "bogus signal", /* 20 */
2812 "bogus signal", /* 21 */
2813 "bogus signal", /* 22 */
2814 "bogus signal", /* 23 */
2815 "bogus signal", /* 24 */
2816 "LAN I/O interrupt", /* 25 SIGAIO */
2817 "PTY I/O interrupt", /* 26 SIGPTY */
2818 "I/O intervention required", /* 27 SIGIOINT */
2819 "HFT grant", /* 28 SIGGRANT */
2820 "HFT retract", /* 29 SIGRETRACT */
2821 "HFT sound done", /* 30 SIGSOUND */
2822 "HFT input ready", /* 31 SIGMSG */
2824 "bogus signal", /* 0 */
2825 "hangup", /* 1 SIGHUP */
2826 "interrupt", /* 2 SIGINT */
2827 "quit", /* 3 SIGQUIT */
2828 "illegal instruction", /* 4 SIGILL */
2829 "trace trap", /* 5 SIGTRAP */
2830 "IOT instruction", /* 6 SIGIOT */
2831 "EMT instruction", /* 7 SIGEMT */
2832 "floating point exception", /* 8 SIGFPE */
2833 "kill", /* 9 SIGKILL */
2834 "bus error", /* 10 SIGBUS */
2835 "segmentation violation", /* 11 SIGSEGV */
2836 "bad argument to system call", /* 12 SIGSYS */
2837 "write on a pipe with no one to read it", /* 13 SIGPIPE */
2838 "alarm clock", /* 14 SIGALRM */
2839 "software termination signum", /* 15 SIGTERM */
2840 "user defined signal 1", /* 16 SIGUSR1 */
2841 "user defined signal 2", /* 17 SIGUSR2 */
2842 "death of a child", /* 18 SIGCLD */
2843 "power-fail restart", /* 19 SIGPWR */
2845 "window size change", /* 20 SIGWINCH */
2846 "urgent socket condition", /* 21 SIGURG */
2847 "pollable event occured", /* 22 SIGPOLL */
2848 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */
2849 "user stop requested from tty", /* 24 SIGTSTP */
2850 "stopped process has been continued", /* 25 SIGCONT */
2851 "background tty read attempted", /* 26 SIGTTIN */
2852 "background tty write attempted", /* 27 SIGTTOU */
2853 "virtual timer expired", /* 28 SIGVTALRM */
2854 "profiling timer expired", /* 29 SIGPROF */
2855 "exceeded cpu limit", /* 30 SIGXCPU */
2856 "exceeded file size limit", /* 31 SIGXFSZ */
2857 "process's lwps are blocked", /* 32 SIGWAITING */
2858 "special signal used by thread library", /* 33 SIGLWP */
2859 "Special Signal Used By CPR", /* 34 SIGFREEZE */
2860 "Special Signal Used By CPR", /* 35 SIGTHAW */
2862 #endif /* not AIX */
2865 #endif /* HAVE_SYS_SIGLIST */
2868 * Warning, this function may not duplicate 4.2 action properly
2869 * under error conditions.
2873 /* In 4.1, param.h fails to define this. */
2874 #define MAXPATHLEN 1024
2883 char *npath
, *spath
;
2884 extern char *getcwd ();
2886 BLOCK_INPUT
; /* getcwd uses malloc */
2887 spath
= npath
= getcwd ((char *) 0, MAXPATHLEN
);
2890 /* On Altos 3068, getcwd can return @hostname/dir, so discard
2891 up to first slash. Should be harmless on other systems. */
2892 while (*npath
&& *npath
!= '/')
2894 strcpy (pathname
, npath
);
2895 free (spath
); /* getcwd uses malloc */
2900 #endif /* HAVE_GETWD */
2903 * Emulate rename using unlink/link. Note that this is
2904 * only partially correct. Also, doesn't enforce restriction
2905 * that files be of same type (regular->regular, dir->dir, etc).
2914 if (access (from
, 0) == 0)
2917 if (link (from
, to
) == 0)
2918 if (unlink (from
) == 0)
2926 #ifdef MISSING_UTIMES
2928 /* HPUX (among others) sets HAVE_TIMEVAL but does not implement utimes. */
2937 /* The IRIS (3.5) has timevals, but uses sys V utime, and doesn't have the
2938 utimbuf structure defined anywhere but in the man page. */
2948 struct timeval tvp
[];
2951 utb
.actime
= tvp
[0].tv_sec
;
2952 utb
.modtime
= tvp
[1].tv_sec
;
2955 #endif /* IRIS_UTIME */
2961 /* HPUX curses library references perror, but as far as we know
2962 it won't be called. Anyway this definition will do for now. */
2968 #endif /* not HAVE_PERROR */
2974 * Emulate BSD dup2. First close newd if it already exists.
2975 * Then, attempt to dup oldd. If not successful, call dup2 recursively
2976 * until we are, then close the unsuccessful ones.
2983 register int fd
, ret
;
2988 fd
= fcntl (oldd
, F_DUPFD
, newd
);
2990 error ("can't dup2 (%i,%i) : %s", oldd
, newd
, strerror (errno
));
2997 ret
= dup2 (old
,new);
3003 #endif /* not HAVE_DUP2 */
3006 * Gettimeofday. Simulate as much as possible. Only accurate
3007 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3008 * Only needed when subprocesses are defined.
3013 #ifndef HAVE_GETTIMEOFDAY
3017 gettimeofday (tp
, tzp
)
3019 struct timezone
*tzp
;
3021 extern long time ();
3023 tp
->tv_sec
= time ((long *)0);
3026 tzp
->tz_minuteswest
= -1;
3032 #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
3035 * This function will go away as soon as all the stubs fixed. (fnf)
3041 printf ("%s not yet implemented\r\n", badfunc
);
3050 char *sys_siglist
[NSIG
+ 1] =
3052 "null signal", /* 0 SIGNULL */
3053 "hangup", /* 1 SIGHUP */
3054 "interrupt", /* 2 SIGINT */
3055 "quit", /* 3 SIGQUIT */
3056 "illegal instruction", /* 4 SIGILL */
3057 "trace trap", /* 5 SIGTRAP */
3058 "abort termination", /* 6 SIGABRT */
3059 "SIGEMT", /* 7 SIGEMT */
3060 "floating point exception", /* 8 SIGFPE */
3061 "kill", /* 9 SIGKILL */
3062 "bus error", /* 10 SIGBUS */
3063 "segmentation violation", /* 11 SIGSEGV */
3064 "bad argument to system call", /* 12 SIGSYS */
3065 "write on a pipe with no reader", /* 13 SIGPIPE */
3066 "alarm clock", /* 14 SIGALRM */
3067 "software termination signal", /* 15 SIGTERM */
3068 "user defined signal 1", /* 16 SIGUSR1 */
3069 "user defined signal 2", /* 17 SIGUSR2 */
3070 "child stopped or terminated", /* 18 SIGCLD */
3071 "power-fail restart", /* 19 SIGPWR */
3072 "window size changed", /* 20 SIGWINCH */
3073 "undefined", /* 21 */
3074 "pollable event occurred", /* 22 SIGPOLL */
3075 "sendable stop signal not from tty", /* 23 SIGSTOP */
3076 "stop signal from tty", /* 24 SIGSTP */
3077 "continue a stopped process", /* 25 SIGCONT */
3078 "attempted background tty read", /* 26 SIGTTIN */
3079 "attempted background tty write", /* 27 SIGTTOU */
3080 "undefined", /* 28 */
3081 "undefined", /* 29 */
3082 "undefined", /* 30 */
3083 "undefined", /* 31 */
3084 "undefined", /* 32 */
3085 "socket (TCP/IP) urgent data arrival", /* 33 SIGURG */
3086 "I/O is possible", /* 34 SIGIO */
3087 "exceeded cpu time limit", /* 35 SIGXCPU */
3088 "exceeded file size limit", /* 36 SIGXFSZ */
3089 "virtual time alarm", /* 37 SIGVTALRM */
3090 "profiling time alarm", /* 38 SIGPROF */
3091 "undefined", /* 39 */
3092 "file record locks revoked", /* 40 SIGLOST */
3093 "undefined", /* 41 */
3094 "undefined", /* 42 */
3095 "undefined", /* 43 */
3096 "undefined", /* 44 */
3097 "undefined", /* 45 */
3098 "undefined", /* 46 */
3099 "undefined", /* 47 */
3100 "undefined", /* 48 */
3101 "undefined", /* 49 */
3102 "undefined", /* 50 */
3103 "undefined", /* 51 */
3104 "undefined", /* 52 */
3105 "undefined", /* 53 */
3106 "undefined", /* 54 */
3107 "undefined", /* 55 */
3108 "undefined", /* 56 */
3109 "undefined", /* 57 */
3110 "undefined", /* 58 */
3111 "undefined", /* 59 */
3112 "undefined", /* 60 */
3113 "undefined", /* 61 */
3114 "undefined", /* 62 */
3115 "undefined", /* 63 */
3116 "notification message in mess. queue", /* 64 SIGDGNOTIFY */
3122 /* Directory routines for systems that don't have them. */
3124 #ifdef SYSV_SYSTEM_DIR
3128 #if defined(BROKEN_CLOSEDIR) || !defined(HAVE_CLOSEDIR)
3132 register DIR *dirp
; /* stream from opendir */
3136 rtnval
= sys_close (dirp
->dd_fd
);
3138 /* Some systems (like Solaris) allocate the buffer and the DIR all
3139 in one block. Why in the world are we freeing this ourselves
3141 #if ! (defined (sun) && defined (USG5_4))
3142 xfree ((char *) dirp
->dd_buf
); /* directory block defined in <dirent.h> */
3144 xfree ((char *) dirp
);
3148 #endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
3149 #endif /* SYSV_SYSTEM_DIR */
3151 #ifdef NONSYSTEM_DIR_LIBRARY
3155 char *filename
; /* name of directory */
3157 register DIR *dirp
; /* -> malloc'ed storage */
3158 register int fd
; /* file descriptor for read */
3159 struct stat sbuf
; /* result of fstat */
3161 fd
= sys_open (filename
, 0);
3166 if (fstat (fd
, &sbuf
) < 0
3167 || (sbuf
.st_mode
& S_IFMT
) != S_IFDIR
3168 || (dirp
= (DIR *) malloc (sizeof (DIR))) == 0)
3172 return 0; /* bad luck today */
3177 dirp
->dd_loc
= dirp
->dd_size
= 0; /* refill needed */
3184 register DIR *dirp
; /* stream from opendir */
3186 sys_close (dirp
->dd_fd
);
3187 xfree ((char *) dirp
);
3195 ino_t od_ino
; /* inode */
3196 char od_name
[DIRSIZ
]; /* filename */
3198 #endif /* not VMS */
3200 struct direct dir_static
; /* simulated directory contents */
3205 register DIR *dirp
; /* stream from opendir */
3208 register struct olddir
*dp
; /* -> directory data */
3210 register struct dir$_name
*dp
; /* -> directory data */
3211 register struct dir$_version
*dv
; /* -> version data */
3216 if (dirp
->dd_loc
>= dirp
->dd_size
)
3217 dirp
->dd_loc
= dirp
->dd_size
= 0;
3219 if (dirp
->dd_size
== 0 /* refill buffer */
3220 && (dirp
->dd_size
= sys_read (dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
)) <= 0)
3224 dp
= (struct olddir
*) &dirp
->dd_buf
[dirp
->dd_loc
];
3225 dirp
->dd_loc
+= sizeof (struct olddir
);
3227 if (dp
->od_ino
!= 0) /* not deleted entry */
3229 dir_static
.d_ino
= dp
->od_ino
;
3230 strncpy (dir_static
.d_name
, dp
->od_name
, DIRSIZ
);
3231 dir_static
.d_name
[DIRSIZ
] = '\0';
3232 dir_static
.d_namlen
= strlen (dir_static
.d_name
);
3233 dir_static
.d_reclen
= sizeof (struct direct
)
3235 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3236 return &dir_static
; /* -> simulated structure */
3239 dp
= (struct dir$_name
*) dirp
->dd_buf
;
3240 if (dirp
->dd_loc
== 0)
3241 dirp
->dd_loc
= (dp
->dir$b_namecount
&1) ? dp
->dir$b_namecount
+ 1
3242 : dp
->dir$b_namecount
;
3243 dv
= (struct dir$_version
*)&dp
->dir$t_name
[dirp
->dd_loc
];
3244 dir_static
.d_ino
= dv
->dir$w_fid_num
;
3245 dir_static
.d_namlen
= dp
->dir$b_namecount
;
3246 dir_static
.d_reclen
= sizeof (struct direct
)
3248 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3249 strncpy (dir_static
.d_name
, dp
->dir$t_name
, dp
->dir$b_namecount
);
3250 dir_static
.d_name
[dir_static
.d_namlen
] = '\0';
3251 dirp
->dd_loc
= dirp
->dd_size
; /* only one record at a time */
3258 /* readdirver is just like readdir except it returns all versions of a file
3259 as separate entries. */
3264 register DIR *dirp
; /* stream from opendir */
3266 register struct dir$_name
*dp
; /* -> directory data */
3267 register struct dir$_version
*dv
; /* -> version data */
3269 if (dirp
->dd_loc
>= dirp
->dd_size
- sizeof (struct dir$_name
))
3270 dirp
->dd_loc
= dirp
->dd_size
= 0;
3272 if (dirp
->dd_size
== 0 /* refill buffer */
3273 && (dirp
->dd_size
= sys_read (dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
)) <= 0)
3276 dp
= (struct dir$_name
*) dirp
->dd_buf
;
3277 if (dirp
->dd_loc
== 0)
3278 dirp
->dd_loc
= (dp
->dir$b_namecount
& 1) ? dp
->dir$b_namecount
+ 1
3279 : dp
->dir$b_namecount
;
3280 dv
= (struct dir$_version
*) &dp
->dir$t_name
[dirp
->dd_loc
];
3281 strncpy (dir_static
.d_name
, dp
->dir$t_name
, dp
->dir$b_namecount
);
3282 sprintf (&dir_static
.d_name
[dp
->dir$b_namecount
], ";%d", dv
->dir$w_version
);
3283 dir_static
.d_namlen
= strlen (dir_static
.d_name
);
3284 dir_static
.d_ino
= dv
->dir$w_fid_num
;
3285 dir_static
.d_reclen
= sizeof (struct direct
) - MAXNAMLEN
+ 3
3286 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3287 dirp
->dd_loc
= ((char *) (++dv
) - dp
->dir$t_name
);
3293 #endif /* NONSYSTEM_DIR_LIBRARY */
3296 /* mkdir and rmdir functions, for systems which don't have them. */
3300 * Written by Robert Rother, Mariah Corporation, August 1985.
3302 * If you want it, it's yours. All I ask in return is that if you
3303 * figure out how to do this in a Bourne Shell script you send me
3305 * sdcsvax!rmr or rmr@uscd
3307 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
3308 * subroutine. 11Mar86; hoptoad!gnu
3310 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3311 * subroutine didn't return EEXIST. It does now.
3317 #ifdef MKDIR_PROTOTYPE
3321 mkdir (dpath
, dmode
)
3326 int cpid
, status
, fd
;
3327 struct stat statbuf
;
3329 if (stat (dpath
, &statbuf
) == 0)
3331 errno
= EEXIST
; /* Stat worked, so it already exists */
3335 /* If stat fails for a reason other than non-existence, return error */
3336 if (errno
!= ENOENT
)
3339 synch_process_alive
= 1;
3340 switch (cpid
= fork ())
3343 case -1: /* Error in fork */
3344 return (-1); /* Errno is set already */
3346 case 0: /* Child process */
3348 * Cheap hack to set mode of new directory. Since this
3349 * child process is going away anyway, we zap its umask.
3350 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3351 * directory. Does anybody care?
3353 status
= umask (0); /* Get current umask */
3354 status
= umask (status
| (0777 & ~dmode
)); /* Set for mkdir */
3355 fd
= sys_open("/dev/null", 2);
3362 execl ("/bin/mkdir", "mkdir", dpath
, (char *) 0);
3363 _exit (-1); /* Can't exec /bin/mkdir */
3365 default: /* Parent process */
3366 wait_for_termination (cpid
);
3369 if (synch_process_death
!= 0 || synch_process_retcode
!= 0)
3371 errno
= EIO
; /* We don't know why, but */
3372 return -1; /* /bin/mkdir failed */
3377 #endif /* not HAVE_MKDIR */
3384 int cpid
, status
, fd
;
3385 struct stat statbuf
;
3387 if (stat (dpath
, &statbuf
) != 0)
3389 /* Stat just set errno. We don't have to */
3393 synch_process_alive
= 1;
3394 switch (cpid
= fork ())
3397 case -1: /* Error in fork */
3398 return (-1); /* Errno is set already */
3400 case 0: /* Child process */
3401 fd
= sys_open("/dev/null", 2);
3408 wait_for_termination (cpid
);
3409 if (synch_process_death
!= 0 || synch_process_retcode
!= 0)
3410 return -1; /* /bin/rmdir failed */
3411 default: /* Parent process */
3412 while (cpid
!= wait (&status
)); /* Wait for kid to finish */
3415 if (WIFSIGNALED (status
) || WEXITSTATUS (status
) != 0)
3417 errno
= EIO
; /* We don't know why, but */
3418 return -1; /* /bin/mkdir failed */
3423 #endif /* !HAVE_RMDIR */
3427 /* Functions for VMS */
3429 #include "vms-pwd.h"
3434 /* Return as a string the VMS error string pertaining to STATUS.
3435 Reuses the same static buffer each time it is called. */
3439 int status
; /* VMS status code */
3443 static char buf
[257];
3445 bufadr
[0] = sizeof buf
- 1;
3446 bufadr
[1] = (int) buf
;
3447 if (! (SYS$
GETMSG (status
, &len
, bufadr
, 0x1, 0) & 1))
3448 return "untranslatable VMS error status";
3456 /* The following is necessary because 'access' emulation by VMS C (2.0) does
3457 * not work correctly. (It also doesn't work well in version 2.3.)
3462 #define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3463 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3467 unsigned short s_buflen
;
3468 unsigned short s_code
;
3470 unsigned short *s_retlenadr
;
3474 #define buflen s.s_buflen
3475 #define code s.s_code
3476 #define bufadr s.s_bufadr
3477 #define retlenadr s.s_retlenadr
3479 #define R_OK 4 /* test for read permission */
3480 #define W_OK 2 /* test for write permission */
3481 #define X_OK 1 /* test for execute (search) permission */
3482 #define F_OK 0 /* test for presence of file */
3485 sys_access (path
, mode
)
3489 static char *user
= NULL
;
3492 /* translate possible directory spec into .DIR file name, so brain-dead
3493 * access can treat the directory like a file. */
3494 if (directory_file_name (path
, dir_fn
))
3498 return access (path
, mode
);
3499 if (user
== NULL
&& (user
= (char *) getenv ("USER")) == NULL
)
3505 unsigned short int dummy
;
3507 static int constant
= ACL$C_FILE
;
3508 DESCRIPTOR (path_desc
, path
);
3509 DESCRIPTOR (user_desc
, user
);
3513 if ((mode
& X_OK
) && ((stat
= access (path
, mode
)) < 0 || mode
== X_OK
))
3516 acces
|= CHP$M_READ
;
3518 acces
|= CHP$M_WRITE
;
3519 itemlst
[0].buflen
= sizeof (int);
3520 itemlst
[0].code
= CHP$_FLAGS
;
3521 itemlst
[0].bufadr
= (char *) &flags
;
3522 itemlst
[0].retlenadr
= &dummy
;
3523 itemlst
[1].buflen
= sizeof (int);
3524 itemlst
[1].code
= CHP$_ACCESS
;
3525 itemlst
[1].bufadr
= (char *) &acces
;
3526 itemlst
[1].retlenadr
= &dummy
;
3527 itemlst
[2].end
= CHP$_END
;
3528 stat
= SYS$
CHECK_ACCESS (&constant
, &path_desc
, &user_desc
, itemlst
);
3529 return stat
== SS$_NORMAL
? 0 : -1;
3533 #else /* not VMS4_4 */
3536 #define ACE$M_WRITE 2
3537 #define ACE$C_KEYID 1
3539 static unsigned short memid
, grpid
;
3540 static unsigned int uic
;
3542 /* Called from init_sys_modes, so it happens not very often
3543 but at least each time Emacs is loaded. */
3544 sys_access_reinit ()
3550 sys_access (filename
, type
)
3556 int status
, size
, i
, typecode
, acl_controlled
;
3557 unsigned int *aclptr
, *aclend
, aclbuf
[60];
3558 union prvdef prvmask
;
3560 /* Get UIC and GRP values for protection checking. */
3563 status
= LIB$
GETJPI (&JPI$_UIC
, 0, 0, &uic
, 0, 0);
3566 memid
= uic
& 0xFFFF;
3570 if (type
!= 2) /* not checking write access */
3571 return access (filename
, type
);
3573 /* Check write protection. */
3575 #define CHECKPRIV(bit) (prvmask.bit)
3576 #define WRITEABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
3578 /* Find privilege bits */
3579 status
= SYS$
SETPRV (0, 0, 0, prvmask
);
3581 error ("Unable to find privileges: %s", vmserrstr (status
));
3582 if (CHECKPRIV (PRV$V_BYPASS
))
3583 return 0; /* BYPASS enabled */
3585 fab
.fab$b_fac
= FAB$M_GET
;
3586 fab
.fab$l_fna
= filename
;
3587 fab
.fab$b_fns
= strlen (filename
);
3588 fab
.fab$l_xab
= &xab
;
3589 xab
= cc$rms_xabpro
;
3590 xab
.xab$l_aclbuf
= aclbuf
;
3591 xab
.xab$w_aclsiz
= sizeof (aclbuf
);
3592 status
= SYS$
OPEN (&fab
, 0, 0);
3595 SYS$
CLOSE (&fab
, 0, 0);
3596 /* Check system access */
3597 if (CHECKPRIV (PRV$V_SYSPRV
) && WRITEABLE (XAB$V_SYS
))
3599 /* Check ACL entries, if any */
3601 if (xab
.xab$w_acllen
> 0)
3604 aclend
= &aclbuf
[xab
.xab$w_acllen
/ 4];
3605 while (*aclptr
&& aclptr
< aclend
)
3607 size
= (*aclptr
& 0xff) / 4;
3608 typecode
= (*aclptr
>> 8) & 0xff;
3609 if (typecode
== ACE$C_KEYID
)
3610 for (i
= size
- 1; i
> 1; i
--)
3611 if (aclptr
[i
] == uic
)
3614 if (aclptr
[1] & ACE$M_WRITE
)
3615 return 0; /* Write access through ACL */
3617 aclptr
= &aclptr
[size
];
3619 if (acl_controlled
) /* ACL specified, prohibits write access */
3622 /* No ACL entries specified, check normal protection */
3623 if (WRITEABLE (XAB$V_WLD
)) /* World writeable */
3625 if (WRITEABLE (XAB$V_GRP
) &&
3626 (unsigned short) (xab
.xab$l_uic
>> 16) == grpid
)
3627 return 0; /* Group writeable */
3628 if (WRITEABLE (XAB$V_OWN
) &&
3629 (xab
.xab$l_uic
& 0xFFFF) == memid
)
3630 return 0; /* Owner writeable */
3632 return -1; /* Not writeable */
3634 #endif /* not VMS4_4 */
3637 static char vtbuf
[NAM$C_MAXRSS
+1];
3639 /* translate a vms file spec to a unix path */
3641 sys_translate_vms (vfile
)
3652 /* leading device or logical name is a root directory */
3653 if (p
= strchr (vfile
, ':'))
3662 if (*p
== '[' || *p
== '<')
3664 while (*++vfile
!= *p
+ 2)
3668 if (vfile
[-1] == *p
)
3691 static char utbuf
[NAM$C_MAXRSS
+1];
3693 /* translate a unix path to a VMS file spec */
3695 sys_translate_unix (ufile
)
3718 if (index (&ufile
[1], '/'))
3725 if (index (&ufile
[1], '/'))
3732 if (strncmp (ufile
, "./", 2) == 0)
3739 ufile
++; /* skip the dot */
3740 if (index (&ufile
[1], '/'))
3745 else if (strncmp (ufile
, "../", 3) == 0)
3753 ufile
+= 2; /* skip the dots */
3754 if (index (&ufile
[1], '/'))
3779 extern char *getcwd ();
3781 #define MAXPATHLEN 1024
3783 ptr
= xmalloc (MAXPATHLEN
);
3784 val
= getcwd (ptr
, MAXPATHLEN
);
3790 strcpy (pathname
, ptr
);
3798 long item_code
= JPI$_OWNER
;
3799 unsigned long parent_id
;
3802 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &parent_id
)) & 1) == 0)
3805 vaxc$errno
= status
;
3815 return (getgid () << 16) | getuid ();
3819 sys_read (fildes
, buf
, nbyte
)
3824 return read (fildes
, buf
, (nbyte
< MAXIOSIZE
? nbyte
: MAXIOSIZE
));
3829 sys_write (fildes
, buf
, nbyte
)
3834 register int nwrote
, rtnval
= 0;
3836 while (nbyte
> MAXIOSIZE
&& (nwrote
= write (fildes
, buf
, MAXIOSIZE
)) > 0) {
3842 return rtnval
? rtnval
: -1;
3843 if ((nwrote
= write (fildes
, buf
, nbyte
)) < 0)
3844 return rtnval
? rtnval
: -1;
3845 return (rtnval
+ nwrote
);
3850 * VAX/VMS VAX C RTL really loses. It insists that records
3851 * end with a newline (carriage return) character, and if they
3852 * don't it adds one (nice of it isn't it!)
3854 * Thus we do this stupidity below.
3858 sys_write (fildes
, buf
, nbytes
)
3861 unsigned int nbytes
;
3868 fstat (fildes
, &st
);
3874 /* Handle fixed-length files with carriage control. */
3875 if (st
.st_fab_rfm
== FAB$C_FIX
3876 && ((st
.st_fab_rat
& (FAB$M_FTN
| FAB$M_CR
)) != 0))
3878 len
= st
.st_fab_mrs
;
3879 retval
= write (fildes
, p
, min (len
, nbytes
));
3882 retval
++; /* This skips the implied carriage control */
3886 e
= p
+ min (MAXIOSIZE
, nbytes
) - 1;
3887 while (*e
!= '\n' && e
> p
) e
--;
3888 if (p
== e
) /* Ok.. so here we add a newline... sigh. */
3889 e
= p
+ min (MAXIOSIZE
, nbytes
) - 1;
3891 retval
= write (fildes
, p
, len
);
3902 /* Create file NEW copying its attributes from file OLD. If
3903 OLD is 0 or does not exist, create based on the value of
3906 /* Protection value the file should ultimately have.
3907 Set by create_copy_attrs, and use by rename_sansversions. */
3908 static unsigned short int fab_final_pro
;
3911 creat_copy_attrs (old
, new)
3914 struct FAB fab
= cc$rms_fab
;
3915 struct XABPRO xabpro
;
3916 char aclbuf
[256]; /* Choice of size is arbitrary. See below. */
3917 extern int vms_stmlf_recfm
;
3921 fab
.fab$b_fac
= FAB$M_GET
;
3922 fab
.fab$l_fna
= old
;
3923 fab
.fab$b_fns
= strlen (old
);
3924 fab
.fab$l_xab
= (char *) &xabpro
;
3925 xabpro
= cc$rms_xabpro
;
3926 xabpro
.xab$l_aclbuf
= aclbuf
;
3927 xabpro
.xab$w_aclsiz
= sizeof aclbuf
;
3928 /* Call $OPEN to fill in the fab & xabpro fields. */
3929 if (SYS$
OPEN (&fab
, 0, 0) & 1)
3931 SYS$
CLOSE (&fab
, 0, 0);
3932 fab
.fab$l_alq
= 0; /* zero the allocation quantity */
3933 if (xabpro
.xab$w_acllen
> 0)
3935 if (xabpro
.xab$w_acllen
> sizeof aclbuf
)
3936 /* If the acl buffer was too short, redo open with longer one.
3937 Wouldn't need to do this if there were some system imposed
3938 limit on the size of an ACL, but I can't find any such. */
3940 xabpro
.xab$l_aclbuf
= (char *) alloca (xabpro
.xab$w_acllen
);
3941 xabpro
.xab$w_aclsiz
= xabpro
.xab$w_acllen
;
3942 if (SYS$
OPEN (&fab
, 0, 0) & 1)
3943 SYS$
CLOSE (&fab
, 0, 0);
3949 xabpro
.xab$l_aclbuf
= 0;
3954 fab
.fab$l_fna
= new;
3955 fab
.fab$b_fns
= strlen (new);
3959 fab
.fab$b_rfm
= vms_stmlf_recfm
? FAB$C_STMLF
: FAB$C_VAR
;
3960 fab
.fab$b_rat
= FAB$M_CR
;
3963 /* Set the file protections such that we will be able to manipulate
3964 this file. Once we are done writing and renaming it, we will set
3965 the protections back. */
3967 fab_final_pro
= xabpro
.xab$w_pro
;
3969 SYS$
SETDFPROT (0, &fab_final_pro
);
3970 xabpro
.xab$w_pro
&= 0xff0f; /* set O:rewd for now. This is set back later. */
3972 /* Create the new file with either default attrs or attrs copied
3974 if (!(SYS$
CREATE (&fab
, 0, 0) & 1))
3976 SYS$
CLOSE (&fab
, 0, 0);
3977 /* As this is a "replacement" for creat, return a file descriptor
3978 opened for writing. */
3979 return open (new, O_WRONLY
);
3984 #include <varargs.h>
3987 #define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
3991 sys_creat (va_alist
)
3994 va_list list_incrementer
;
3997 int rfd
; /* related file descriptor */
3998 int fd
; /* Our new file descriptor */
4005 extern int vms_stmlf_recfm
;
4008 va_start (list_incrementer
);
4009 name
= va_arg (list_incrementer
, char *);
4010 mode
= va_arg (list_incrementer
, int);
4012 rfd
= va_arg (list_incrementer
, int);
4013 va_end (list_incrementer
);
4016 /* Use information from the related file descriptor to set record
4017 format of the newly created file. */
4018 fstat (rfd
, &st_buf
);
4019 switch (st_buf
.st_fab_rfm
)
4022 strcpy (rfm
, "rfm = fix");
4023 sprintf (mrs
, "mrs = %d", st_buf
.st_fab_mrs
);
4024 strcpy (rat
, "rat = ");
4025 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4027 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4028 strcat (rat
, "ftn");
4029 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4030 strcat (rat
, "prn");
4031 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4032 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4033 strcat (rat
, ", blk");
4035 strcat (rat
, "blk");
4036 return creat (name
, 0, rfm
, rat
, mrs
);
4039 strcpy (rfm
, "rfm = vfc");
4040 sprintf (fsz
, "fsz = %d", st_buf
.st_fab_fsz
);
4041 strcpy (rat
, "rat = ");
4042 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4044 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4045 strcat (rat
, "ftn");
4046 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4047 strcat (rat
, "prn");
4048 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4049 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4050 strcat (rat
, ", blk");
4052 strcat (rat
, "blk");
4053 return creat (name
, 0, rfm
, rat
, fsz
);
4056 strcpy (rfm
, "rfm = stm");
4060 strcpy (rfm
, "rfm = stmcr");
4064 strcpy (rfm
, "rfm = stmlf");
4068 strcpy (rfm
, "rfm = udf");
4072 strcpy (rfm
, "rfm = var");
4075 strcpy (rat
, "rat = ");
4076 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4078 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4079 strcat (rat
, "ftn");
4080 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4081 strcat (rat
, "prn");
4082 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4083 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4084 strcat (rat
, ", blk");
4086 strcat (rat
, "blk");
4090 strcpy (rfm
, vms_stmlf_recfm
? "rfm = stmlf" : "rfm=var");
4091 strcpy (rat
, "rat=cr");
4093 /* Until the VAX C RTL fixes the many bugs with modes, always use
4094 mode 0 to get the user's default protection. */
4095 fd
= creat (name
, 0, rfm
, rat
);
4096 if (fd
< 0 && errno
== EEXIST
)
4098 if (unlink (name
) < 0)
4099 report_file_error ("delete", build_string (name
));
4100 fd
= creat (name
, 0, rfm
, rat
);
4106 /* fwrite to stdout is S L O W. Speed it up by using fputc...*/
4107 sys_fwrite (ptr
, size
, num
, fp
)
4108 register char * ptr
;
4111 register int tot
= num
* size
;
4118 * The VMS C library routine creat actually creates a new version of an
4119 * existing file rather than truncating the old version. There are times
4120 * when this is not the desired behavior, for instance, when writing an
4121 * auto save file (you only want one version), or when you don't have
4122 * write permission in the directory containing the file (but the file
4123 * itself is writable). Hence this routine, which is equivalent to
4124 * "close (creat (fn, 0));" on Unix if fn already exists.
4130 struct FAB xfab
= cc$rms_fab
;
4131 struct RAB xrab
= cc$rms_rab
;
4134 xfab
.fab$l_fop
= FAB$M_TEF
; /* free allocated but unused blocks on close */
4135 xfab
.fab$b_fac
= FAB$M_TRN
| FAB$M_GET
; /* allow truncate and get access */
4136 xfab
.fab$b_shr
= FAB$M_NIL
; /* allow no sharing - file must be locked */
4137 xfab
.fab$l_fna
= fn
;
4138 xfab
.fab$b_fns
= strlen (fn
);
4139 xfab
.fab$l_dna
= ";0"; /* default to latest version of the file */
4141 xrab
.rab$l_fab
= &xfab
;
4143 /* This gibberish opens the file, positions to the first record, and
4144 deletes all records from there until the end of file. */
4145 if ((SYS$
OPEN (&xfab
) & 01) == 01)
4147 if ((SYS$
CONNECT (&xrab
) & 01) == 01 &&
4148 (SYS$
FIND (&xrab
) & 01) == 01 &&
4149 (SYS$
TRUNCATE (&xrab
) & 01) == 01)
4160 /* Define this symbol to actually read SYSUAF.DAT. This requires either
4161 SYSPRV or a readable SYSUAF.DAT. */
4167 * Routine to read the VMS User Authorization File and return
4168 * a specific user's record.
4171 static struct UAF retuaf
;
4174 get_uaf_name (uname
)
4181 uaf_fab
= cc$rms_fab
;
4182 uaf_rab
= cc$rms_rab
;
4183 /* initialize fab fields */
4184 uaf_fab
.fab$l_fna
= "SYS$SYSTEM:SYSUAF.DAT";
4185 uaf_fab
.fab$b_fns
= 21;
4186 uaf_fab
.fab$b_fac
= FAB$M_GET
;
4187 uaf_fab
.fab$b_org
= FAB$C_IDX
;
4188 uaf_fab
.fab$b_shr
= FAB$M_GET
|FAB$M_PUT
|FAB$M_UPD
|FAB$M_DEL
;
4189 /* initialize rab fields */
4190 uaf_rab
.rab$l_fab
= &uaf_fab
;
4191 /* open the User Authorization File */
4192 status
= SYS$
OPEN (&uaf_fab
);
4196 vaxc$errno
= status
;
4199 status
= SYS$
CONNECT (&uaf_rab
);
4203 vaxc$errno
= status
;
4206 /* read the requested record - index is in uname */
4207 uaf_rab
.rab$l_kbf
= uname
;
4208 uaf_rab
.rab$b_ksz
= strlen (uname
);
4209 uaf_rab
.rab$b_rac
= RAB$C_KEY
;
4210 uaf_rab
.rab$l_ubf
= (char *)&retuaf
;
4211 uaf_rab
.rab$w_usz
= sizeof retuaf
;
4212 status
= SYS$
GET (&uaf_rab
);
4216 vaxc$errno
= status
;
4219 /* close the User Authorization File */
4220 status
= SYS$
DISCONNECT (&uaf_rab
);
4224 vaxc$errno
= status
;
4227 status
= SYS$
CLOSE (&uaf_fab
);
4231 vaxc$errno
= status
;
4245 uaf_fab
= cc$rms_fab
;
4246 uaf_rab
= cc$rms_rab
;
4247 /* initialize fab fields */
4248 uaf_fab
.fab$l_fna
= "SYS$SYSTEM:SYSUAF.DAT";
4249 uaf_fab
.fab$b_fns
= 21;
4250 uaf_fab
.fab$b_fac
= FAB$M_GET
;
4251 uaf_fab
.fab$b_org
= FAB$C_IDX
;
4252 uaf_fab
.fab$b_shr
= FAB$M_GET
|FAB$M_PUT
|FAB$M_UPD
|FAB$M_DEL
;
4253 /* initialize rab fields */
4254 uaf_rab
.rab$l_fab
= &uaf_fab
;
4255 /* open the User Authorization File */
4256 status
= SYS$
OPEN (&uaf_fab
);
4260 vaxc$errno
= status
;
4263 status
= SYS$
CONNECT (&uaf_rab
);
4267 vaxc$errno
= status
;
4270 /* read the requested record - index is in uic */
4271 uaf_rab
.rab$b_krf
= 1; /* 1st alternate key */
4272 uaf_rab
.rab$l_kbf
= (char *) &uic
;
4273 uaf_rab
.rab$b_ksz
= sizeof uic
;
4274 uaf_rab
.rab$b_rac
= RAB$C_KEY
;
4275 uaf_rab
.rab$l_ubf
= (char *)&retuaf
;
4276 uaf_rab
.rab$w_usz
= sizeof retuaf
;
4277 status
= SYS$
GET (&uaf_rab
);
4281 vaxc$errno
= status
;
4284 /* close the User Authorization File */
4285 status
= SYS$
DISCONNECT (&uaf_rab
);
4289 vaxc$errno
= status
;
4292 status
= SYS$
CLOSE (&uaf_fab
);
4296 vaxc$errno
= status
;
4302 static struct passwd retpw
;
4310 /* copy these out first because if the username is 32 chars, the next
4311 section will overwrite the first byte of the UIC */
4312 retpw
.pw_uid
= up
->uaf$w_mem
;
4313 retpw
.pw_gid
= up
->uaf$w_grp
;
4315 /* I suppose this is not the best sytle, to possibly overwrite one
4316 byte beyond the end of the field, but what the heck... */
4317 ptr
= &up
->uaf$t_username
[UAF$S_USERNAME
];
4318 while (ptr
[-1] == ' ')
4321 strcpy (retpw
.pw_name
, up
->uaf$t_username
);
4323 /* the rest of these are counted ascii strings */
4324 strncpy (retpw
.pw_gecos
, &up
->uaf$t_owner
[1], up
->uaf$t_owner
[0]);
4325 retpw
.pw_gecos
[up
->uaf$t_owner
[0]] = '\0';
4326 strncpy (retpw
.pw_dir
, &up
->uaf$t_defdev
[1], up
->uaf$t_defdev
[0]);
4327 retpw
.pw_dir
[up
->uaf$t_defdev
[0]] = '\0';
4328 strncat (retpw
.pw_dir
, &up
->uaf$t_defdir
[1], up
->uaf$t_defdir
[0]);
4329 retpw
.pw_dir
[up
->uaf$t_defdev
[0] + up
->uaf$t_defdir
[0]] = '\0';
4330 strncpy (retpw
.pw_shell
, &up
->uaf$t_defcli
[1], up
->uaf$t_defcli
[0]);
4331 retpw
.pw_shell
[up
->uaf$t_defcli
[0]] = '\0';
4335 #else /* not READ_SYSUAF */
4336 static struct passwd retpw
;
4337 #endif /* not READ_SYSUAF */
4348 unsigned char * full
;
4349 #endif /* READ_SYSUAF */
4354 if ('a' <= *ptr
&& *ptr
<= 'z')
4359 if (!(up
= get_uaf_name (name
)))
4361 return cnv_uaf_pw (up
);
4363 if (strcmp (name
, getenv ("USER")) == 0)
4365 retpw
.pw_uid
= getuid ();
4366 retpw
.pw_gid
= getgid ();
4367 strcpy (retpw
.pw_name
, name
);
4368 if (full
= egetenv ("FULLNAME"))
4369 strcpy (retpw
.pw_gecos
, full
);
4371 *retpw
.pw_gecos
= '\0';
4372 strcpy (retpw
.pw_dir
, egetenv ("HOME"));
4373 *retpw
.pw_shell
= '\0';
4378 #endif /* not READ_SYSUAF */
4388 if (!(up
= get_uaf_uic (uid
)))
4390 return cnv_uaf_pw (up
);
4392 if (uid
== sys_getuid ())
4393 return getpwnam (egetenv ("USER"));
4396 #endif /* not READ_SYSUAF */
4399 /* return total address space available to the current process. This is
4400 the sum of the current p0 size, p1 size and free page table entries
4405 unsigned long free_pages
;
4406 unsigned long frep0va
;
4407 unsigned long frep1va
;
4410 item_code
= JPI$_FREPTECNT
;
4411 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &free_pages
)) & 1) == 0)
4414 vaxc$errno
= status
;
4419 item_code
= JPI$_FREP0VA
;
4420 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &frep0va
)) & 1) == 0)
4423 vaxc$errno
= status
;
4426 item_code
= JPI$_FREP1VA
;
4427 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &frep1va
)) & 1) == 0)
4430 vaxc$errno
= status
;
4434 return free_pages
+ frep0va
+ (0x7fffffff - frep1va
);
4437 define_logical_name (varname
, string
)
4441 struct dsc$descriptor_s strdsc
=
4442 {strlen (string
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, string
};
4443 struct dsc$descriptor_s envdsc
=
4444 {strlen (varname
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, varname
};
4445 struct dsc$descriptor_s lnmdsc
=
4446 {7, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, "LNM$JOB"};
4448 return LIB$
SET_LOGICAL (&envdsc
, &strdsc
, &lnmdsc
, 0, 0);
4451 delete_logical_name (varname
)
4454 struct dsc$descriptor_s envdsc
=
4455 {strlen (varname
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, varname
};
4456 struct dsc$descriptor_s lnmdsc
=
4457 {7, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, "LNM$JOB"};
4459 return LIB$
DELETE_LOGICAL (&envdsc
, &lnmdsc
);
4470 error ("execvp system call not implemented");
4478 struct FAB from_fab
= cc$rms_fab
, to_fab
= cc$rms_fab
;
4479 struct NAM from_nam
= cc$rms_nam
, to_nam
= cc$rms_nam
;
4480 char from_esn
[NAM$C_MAXRSS
];
4481 char to_esn
[NAM$C_MAXRSS
];
4483 from_fab
.fab$l_fna
= from
;
4484 from_fab
.fab$b_fns
= strlen (from
);
4485 from_fab
.fab$l_nam
= &from_nam
;
4486 from_fab
.fab$l_fop
= FAB$M_NAM
;
4488 from_nam
.nam$l_esa
= from_esn
;
4489 from_nam
.nam$b_ess
= sizeof from_esn
;
4491 to_fab
.fab$l_fna
= to
;
4492 to_fab
.fab$b_fns
= strlen (to
);
4493 to_fab
.fab$l_nam
= &to_nam
;
4494 to_fab
.fab$l_fop
= FAB$M_NAM
;
4496 to_nam
.nam$l_esa
= to_esn
;
4497 to_nam
.nam$b_ess
= sizeof to_esn
;
4499 status
= SYS$
RENAME (&from_fab
, 0, 0, &to_fab
);
4505 if (status
== RMS$_DEV
)
4509 vaxc$errno
= status
;
4514 /* This function renames a file like `rename', but it strips
4515 the version number from the "to" filename, such that the "to" file is
4516 will always be a new version. It also sets the file protection once it is
4517 finished. The protection that we will use is stored in fab_final_pro,
4518 and was set when we did a creat_copy_attrs to create the file that we
4521 We could use the chmod function, but Eunichs uses 3 bits per user category
4522 to describe the protection, and VMS uses 4 (write and delete are separate
4523 bits). To maintain portability, the VMS implementation of `chmod' wires
4524 the W and D bits together. */
4527 static struct fibdef fib
; /* We need this initialized to zero */
4528 char vms_file_written
[NAM$C_MAXRSS
];
4531 rename_sans_version (from
,to
)
4538 struct FAB to_fab
= cc$rms_fab
;
4539 struct NAM to_nam
= cc$rms_nam
;
4540 struct dsc$descriptor fib_d
={sizeof (fib
),0,0,(char*) &fib
};
4541 struct dsc$descriptor fib_attr
[2]
4542 = {{sizeof (fab_final_pro
),ATR$C_FPRO
,0,(char*) &fab_final_pro
},{0,0,0,0}};
4543 char to_esn
[NAM$C_MAXRSS
];
4545 $
DESCRIPTOR (disk
,to_esn
);
4547 to_fab
.fab$l_fna
= to
;
4548 to_fab
.fab$b_fns
= strlen (to
);
4549 to_fab
.fab$l_nam
= &to_nam
;
4550 to_fab
.fab$l_fop
= FAB$M_NAM
;
4552 to_nam
.nam$l_esa
= to_esn
;
4553 to_nam
.nam$b_ess
= sizeof to_esn
;
4555 status
= SYS$
PARSE (&to_fab
, 0, 0); /* figure out the full file name */
4557 if (to_nam
.nam$l_fnb
&& NAM$M_EXP_VER
)
4558 *(to_nam
.nam$l_ver
) = '\0';
4560 stat
= rename (from
, to_esn
);
4564 strcpy (vms_file_written
, to_esn
);
4566 to_fab
.fab$l_fna
= vms_file_written
; /* this points to the versionless name */
4567 to_fab
.fab$b_fns
= strlen (vms_file_written
);
4569 /* Now set the file protection to the correct value */
4570 SYS$
OPEN (&to_fab
, 0, 0); /* This fills in the nam$w_fid fields */
4572 /* Copy these fields into the fib */
4573 fib
.fib$r_fid_overlay
.fib$w_fid
[0] = to_nam
.nam$w_fid
[0];
4574 fib
.fib$r_fid_overlay
.fib$w_fid
[1] = to_nam
.nam$w_fid
[1];
4575 fib
.fib$r_fid_overlay
.fib$w_fid
[2] = to_nam
.nam$w_fid
[2];
4577 SYS$
CLOSE (&to_fab
, 0, 0);
4579 stat
= SYS$
ASSIGN (&disk
, &chan
, 0, 0); /* open a channel to the disk */
4582 stat
= SYS$
QIOW (0, chan
, IO$_MODIFY
, iosb
, 0, 0, &fib_d
,
4583 0, 0, 0, &fib_attr
, 0);
4586 stat
= SYS$
DASSGN (chan
);
4589 strcpy (vms_file_written
, to_esn
); /* We will write this to the terminal*/
4599 unsigned short fid
[3];
4600 char esa
[NAM$C_MAXRSS
];
4603 fab
.fab$l_fop
= FAB$M_OFP
;
4604 fab
.fab$l_fna
= file
;
4605 fab
.fab$b_fns
= strlen (file
);
4606 fab
.fab$l_nam
= &nam
;
4609 nam
.nam$l_esa
= esa
;
4610 nam
.nam$b_ess
= NAM$C_MAXRSS
;
4612 status
= SYS$
PARSE (&fab
);
4613 if ((status
& 1) == 0)
4616 vaxc$errno
= status
;
4619 status
= SYS$
SEARCH (&fab
);
4620 if ((status
& 1) == 0)
4623 vaxc$errno
= status
;
4627 fid
[0] = nam
.nam$w_fid
[0];
4628 fid
[1] = nam
.nam$w_fid
[1];
4629 fid
[2] = nam
.nam$w_fid
[2];
4631 fab
.fab$l_fna
= new;
4632 fab
.fab$b_fns
= strlen (new);
4634 status
= SYS$
PARSE (&fab
);
4635 if ((status
& 1) == 0)
4638 vaxc$errno
= status
;
4642 nam
.nam$w_fid
[0] = fid
[0];
4643 nam
.nam$w_fid
[1] = fid
[1];
4644 nam
.nam$w_fid
[2] = fid
[2];
4646 nam
.nam$l_esa
= nam
.nam$l_name
;
4647 nam
.nam$b_esl
= nam
.nam$b_name
+ nam
.nam$b_type
+ nam
.nam$b_ver
;
4649 status
= SYS$
ENTER (&fab
);
4650 if ((status
& 1) == 0)
4653 vaxc$errno
= status
;
4663 printf ("%s not yet implemented\r\n", badfunc
);
4671 /* Arrange to return a range centered on zero. */
4672 return rand () - (1 << 30);
4683 /* Called from init_sys_modes. */
4688 /* If we're not on an HFT we shouldn't do any of this. We determine
4689 if we are on an HFT by trying to get an HFT error code. If this
4690 call fails, we're not on an HFT. */
4692 if (ioctl (0, HFQERROR
, &junk
) < 0)
4694 #else /* not IBMR2AIX */
4695 if (ioctl (0, HFQEIO
, 0) < 0)
4697 #endif /* not IBMR2AIX */
4699 /* On AIX the default hft keyboard mapping uses backspace rather than delete
4700 as the rubout key's ASCII code. Here this is changed. The bug is that
4701 there's no way to determine the old mapping, so in reset_sys_modes
4702 we need to assume that the normal map had been present. Of course, this
4703 code also doesn't help if on a terminal emulator which doesn't understand
4707 struct hfkeymap keymap
;
4709 buf
.hf_bufp
= (char *)&keymap
;
4710 buf
.hf_buflen
= sizeof (keymap
);
4711 keymap
.hf_nkeys
= 2;
4712 keymap
.hfkey
[0].hf_kpos
= 15;
4713 keymap
.hfkey
[0].hf_kstate
= HFMAPCHAR
| HFSHFNONE
;
4715 keymap
.hfkey
[0].hf_keyidh
= '<';
4716 #else /* not IBMR2AIX */
4717 keymap
.hfkey
[0].hf_page
= '<';
4718 #endif /* not IBMR2AIX */
4719 keymap
.hfkey
[0].hf_char
= 127;
4720 keymap
.hfkey
[1].hf_kpos
= 15;
4721 keymap
.hfkey
[1].hf_kstate
= HFMAPCHAR
| HFSHFSHFT
;
4723 keymap
.hfkey
[1].hf_keyidh
= '<';
4724 #else /* not IBMR2AIX */
4725 keymap
.hfkey
[1].hf_page
= '<';
4726 #endif /* not IBMR2AIX */
4727 keymap
.hfkey
[1].hf_char
= 127;
4728 hftctl (0, HFSKBD
, &buf
);
4730 /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
4732 line_ins_del_ok
= char_ins_del_ok
= 0;
4735 /* Reset the rubout key to backspace. */
4740 struct hfkeymap keymap
;
4744 if (ioctl (0, HFQERROR
, &junk
) < 0)
4746 #else /* not IBMR2AIX */
4747 if (ioctl (0, HFQEIO
, 0) < 0)
4749 #endif /* not IBMR2AIX */
4751 buf
.hf_bufp
= (char *)&keymap
;
4752 buf
.hf_buflen
= sizeof (keymap
);
4753 keymap
.hf_nkeys
= 2;
4754 keymap
.hfkey
[0].hf_kpos
= 15;
4755 keymap
.hfkey
[0].hf_kstate
= HFMAPCHAR
| HFSHFNONE
;
4757 keymap
.hfkey
[0].hf_keyidh
= '<';
4758 #else /* not IBMR2AIX */
4759 keymap
.hfkey
[0].hf_page
= '<';
4760 #endif /* not IBMR2AIX */
4761 keymap
.hfkey
[0].hf_char
= 8;
4762 keymap
.hfkey
[1].hf_kpos
= 15;
4763 keymap
.hfkey
[1].hf_kstate
= HFMAPCHAR
| HFSHFSHFT
;
4765 keymap
.hfkey
[1].hf_keyidh
= '<';
4766 #else /* not IBMR2AIX */
4767 keymap
.hfkey
[1].hf_page
= '<';
4768 #endif /* not IBMR2AIX */
4769 keymap
.hfkey
[1].hf_char
= 8;
4770 hftctl (0, HFSKBD
, &buf
);