1 /* Interfaces to system-dependent kernel and library entries.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
32 /* Including stdlib.h isn't necessarily enough to get srandom
33 declared, e.g. without __USE_XOPEN_EXTENDED with glibc 2. */
35 #if 0 /* Don't prototype srandom; it takes an unsigned argument on
36 some systems, and an unsigned long on others, like FreeBSD
38 extern void srandom
P_ ((unsigned int));
42 /* The w32 build defines select stuff in w32.h, which is included by
43 sys/select.h (included below). */
45 #include "sysselect.h"
48 #include "blockinput.h"
52 #define write sys_write
57 #endif /* not WINDOWSNT */
59 /* Does anyone other than VMS need this? */
61 #define sys_fwrite fwrite
66 #include <sys/types.h>
71 #if !defined (USG) || defined (BSD_PGRPS)
73 #define setpgrp setpgid
77 /* Get SI_SRPC_DOMAIN, if it is available. */
78 #ifdef HAVE_SYS_SYSTEMINFO_H
79 #include <sys/systeminfo.h>
82 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
86 #include <sys/param.h>
90 extern unsigned start
__asm__ ("start");
112 #include <sys/file.h>
120 #define MAXIOSIZE (32 * PAGESIZE) /* Don't I/O more than 32 blocks at a time */
124 #include <sys/file.h>
132 #include <sys/ioctl.h>
138 #ifdef BROKEN_TIOCGWINSZ
144 #include <sys/utsname.h>
146 #if defined (TIOCGWINSZ)
148 #include <sys/sioctl.h>
151 #include <sys/stream.h>
152 #include <sys/ptem.h>
154 #endif /* TIOCGWINSZ */
157 extern int quit_char
;
159 #include "keyboard.h"
162 #include "termhooks.h"
163 #include "termchar.h"
164 #include "termopts.h"
165 #include "dispextern.h"
167 #include "cm.h" /* for reset_sys_modes */
171 /* In process.h which conflicts with the local copy. */
173 int _CRTAPI1
_spawnlp (int, const char *, const char *, ...);
174 int _CRTAPI1
_getpid (void);
175 extern char *getwd (char *);
178 #ifdef NONSYSTEM_DIR_LIBRARY
180 #endif /* NONSYSTEM_DIR_LIBRARY */
182 #include "syssignal.h"
189 #ifndef HAVE_STRUCT_UTIMBUF
190 /* We want to use utime rather than utimes, but we couldn't find the
191 structure declaration. We'll use the traditional one. */
199 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
205 #define LNOFLSH 0100000
208 static int baud_convert
[] =
213 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
214 1800, 2400, 4800, 9600, 19200, 38400
221 #if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
223 #if defined (HAVE_TERMIOS_H) && defined (GNU_LINUX)
231 void croak
P_ ((char *)) NO_RETURN
;
234 void hft_init
P_ ((struct tty_display_info
*));
235 void hft_reset
P_ ((struct tty_display_info
*));
238 /* Temporary used by `sigblock' when defined in terms of signprocmask. */
240 SIGMASKTYPE sigprocmask_set
;
243 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
245 /* Return the current working directory. Returns NULL on errors.
246 Any other returned value must be freed with free. This is used
247 only when get_current_dir_name is not defined on the system. */
249 get_current_dir_name ()
253 struct stat dotstat
, pwdstat
;
254 /* If PWD is accurate, use it instead of calling getwd. PWD is
255 sometimes a nicer name, and using it may avoid a fatal error if a
256 parent directory is searchable but not readable. */
257 if ((pwd
= getenv ("PWD")) != 0
258 && (IS_DIRECTORY_SEP (*pwd
) || (*pwd
&& IS_DEVICE_SEP (pwd
[1])))
259 && stat (pwd
, &pwdstat
) == 0
260 && stat (".", &dotstat
) == 0
261 && dotstat
.st_ino
== pwdstat
.st_ino
262 && dotstat
.st_dev
== pwdstat
.st_dev
264 && strlen (pwd
) < MAXPATHLEN
268 buf
= (char *) malloc (strlen (pwd
) + 1);
276 size_t buf_size
= 1024;
277 buf
= (char *) malloc (buf_size
);
282 if (getcwd (buf
, buf_size
) == buf
)
286 int tmp_errno
= errno
;
292 buf
= (char *) realloc (buf
, buf_size
);
300 /* We need MAXPATHLEN here. */
301 buf
= (char *) malloc (MAXPATHLEN
+ 1);
304 if (getwd (buf
) == NULL
)
306 int tmp_errno
= errno
;
318 /* Discard pending input on all input descriptors. */
324 struct emacs_tty buf
;
331 SYS$
QIOW (0, fileno (CURTTY()->input
), IO$_READVBLK
|IO$M_PURGE
, input_iosb
, 0, 0,
332 &buf
.main
, 0, 0, terminator_mask
, 0, 0);
335 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
336 while (dos_keyread () != -1)
338 #else /* not MSDOS */
340 struct tty_display_info
*tty
;
341 for (tty
= tty_list
; tty
; tty
= tty
->next
)
343 if (tty
->input
) /* Is the device suspended? */
345 EMACS_GET_TTY (fileno (tty
->input
), &buf
);
346 EMACS_SET_TTY (fileno (tty
->input
), &buf
, 0);
350 #endif /* not MSDOS */
352 #endif /* not WINDOWSNT */
358 /* Arrange for character C to be read as the next input from
360 XXX What if we have multiple ttys?
366 if (! FRAME_TERMCAP_P (SELECTED_FRAME ()))
369 /* Should perhaps error if in batch mode */
371 ioctl (fileno (CURTTY()->input
), TIOCSTI
, &c
);
372 #else /* no TIOCSTI */
373 error ("Cannot stuff terminal input characters in this version of Unix");
374 #endif /* no TIOCSTI */
380 init_baud_rate (int fd
)
388 #else /* not DOS_NT */
392 SYS$
QIOW (0, fd
, IO$_SENSEMODE
, &sg
, 0, 0,
393 &sg
.class, 12, 0, 0, 0, 0 );
394 emacs_ospeed
= sg
.xmit_baud
;
401 emacs_ospeed
= cfgetospeed (&sg
);
402 #else /* neither VMS nor TERMIOS */
410 ioctl (fd
, TCGETA
, &sg
);
412 emacs_ospeed
= sg
.c_cflag
& CBAUD
;
413 #else /* neither VMS nor TERMIOS nor TERMIO */
416 sg
.sg_ospeed
= B9600
;
417 if (ioctl (fd
, TIOCGETP
, &sg
) < 0)
419 emacs_ospeed
= sg
.sg_ospeed
;
420 #endif /* not HAVE_TERMIO */
421 #endif /* not HAVE_TERMIOS */
423 #endif /* not DOS_NT */
426 baud_rate
= (emacs_ospeed
< sizeof baud_convert
/ sizeof baud_convert
[0]
427 ? baud_convert
[emacs_ospeed
] : 9600);
435 set_exclusive_use (fd
)
439 ioctl (fd
, FIOCLEX
, 0);
441 /* Ok to do nothing if this feature does not exist */
446 wait_without_blocking ()
449 wait3 (0, WNOHANG
| WUNTRACED
, 0);
451 croak ("wait_without_blocking");
453 synch_process_alive
= 0;
456 #endif /* not subprocesses */
458 int wait_debugging
; /* Set nonzero to make following function work under dbx
459 (at least for bsd). */
462 wait_for_termination_signal ()
465 /* Wait for subprocess with process id `pid' to terminate and
466 make sure it will get eliminated (not remain forever as a zombie) */
469 wait_for_termination (pid
)
478 status
= SYS$
FORCEX (&pid
, 0, 0);
481 #if defined (BSD_SYSTEM) || (defined (HPUX) && !defined (HPUX_5))
482 /* Note that kill returns -1 even if the process is just a zombie now.
483 But inevitably a SIGCHLD interrupt should be generated
484 and child_sig will do wait3 and make the process go away. */
485 /* There is some indication that there is a bug involved with
486 termination of subprocesses, perhaps involving a kernel bug too,
487 but no idea what it is. Just as a hunch we signal SIGCHLD to see
488 if that causes the problem to go away or get worse. */
489 sigsetmask (sigmask (SIGCHLD
));
490 if (0 > kill (pid
, 0))
492 sigsetmask (SIGEMPTYMASK
);
493 kill (getpid (), SIGCHLD
);
499 sigpause (SIGEMPTYMASK
);
500 #else /* not BSD_SYSTEM, and not HPUX version >= 6 */
501 #ifdef POSIX_SIGNALS /* would this work for GNU/Linux as well? */
502 sigblock (sigmask (SIGCHLD
));
504 if (kill (pid
, 0) == -1 && errno
== ESRCH
)
506 sigunblock (sigmask (SIGCHLD
));
510 sigsuspend (&empty_mask
);
511 #else /* not POSIX_SIGNALS */
512 #ifdef HAVE_SYSV_SIGPAUSE
514 if (0 > kill (pid
, 0))
520 #else /* not HAVE_SYSV_SIGPAUSE */
524 #else /* not WINDOWSNT */
525 if (0 > kill (pid
, 0))
527 /* Using sleep instead of pause avoids timing error.
528 If the inferior dies just before the sleep,
529 we lose just one second. */
531 #endif /* not WINDOWSNT */
532 #endif /* not HAVE_SYSV_SIGPAUSE */
533 #endif /* not POSIX_SIGNALS */
534 #endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
536 #else /* not subprocesses */
539 #else /* not __DJGPP__ > 1 */
541 if (kill (pid
, 0) < 0)
547 if (status
== pid
|| status
== -1)
550 #endif /* not __DJGPP__ > 1*/
551 #endif /* not subprocesses */
558 * flush any pending output
559 * (may flush input as well; it does not matter the way we use it)
563 flush_pending_output (channel
)
567 /* If we try this, we get hit with SIGTTIN, because
568 the child's tty belongs to the child's pgrp. */
571 ioctl (channel
, TCFLSH
, 1);
575 /* 3rd arg should be ignored
576 but some 4.2 kernels actually want the address of an int
577 and nonzero means something different. */
578 ioctl (channel
, TIOCFLUSH
, &zero
);
585 /* Set up the terminal at the other end of a pseudo-terminal that
586 we will be controlling an inferior through.
587 It should not echo or do line-editing, since that is done
588 in Emacs. No padding needed for insertion into an Emacs buffer. */
591 child_setup_tty (out
)
597 EMACS_GET_TTY (out
, &s
);
599 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
600 s
.main
.c_oflag
|= OPOST
; /* Enable output postprocessing */
601 s
.main
.c_oflag
&= ~ONLCR
; /* Disable map of NL to CR-NL on output */
603 s
.main
.c_oflag
&= ~(NLDLY
|CRDLY
|TABDLY
|BSDLY
|VTDLY
|FFDLY
);
604 /* No output delays */
606 s
.main
.c_lflag
&= ~ECHO
; /* Disable echo */
607 s
.main
.c_lflag
|= ISIG
; /* Enable signals */
608 #if 0 /* This causes bugs in (for instance) telnet to certain sites. */
609 s
.main
.c_iflag
&= ~ICRNL
; /* Disable map of CR to NL on input */
610 #ifdef INLCR /* Just being cautious, since I can't check how
611 widespread INLCR is--rms. */
612 s
.main
.c_iflag
&= ~INLCR
; /* Disable map of NL to CR on input */
616 s
.main
.c_iflag
&= ~IUCLC
; /* Disable downcasing on input. */
619 s
.main
.c_iflag
&= ~ISTRIP
; /* don't strip 8th bit on input */
622 s
.main
.c_oflag
&= ~OLCUC
; /* Disable upcasing on output. */
624 s
.main
.c_oflag
&= ~TAB3
; /* Disable tab expansion */
625 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CSIZE
) | CS8
; /* Don't strip 8th bit */
627 /* Said to be unnecessary: */
628 s
.main
.c_cc
[VMIN
] = 1; /* minimum number of characters to accept */
629 s
.main
.c_cc
[VTIME
] = 0; /* wait forever for at least 1 character */
632 s
.main
.c_lflag
|= ICANON
; /* Enable erase/kill and eof processing */
633 s
.main
.c_cc
[VEOF
] = 04; /* insure that EOF is Control-D */
634 s
.main
.c_cc
[VERASE
] = CDISABLE
; /* disable erase processing */
635 s
.main
.c_cc
[VKILL
] = CDISABLE
; /* disable kill processing */
638 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CBAUD
) | B9600
; /* baud rate sanity */
641 #ifdef SIGNALS_VIA_CHARACTERS
642 /* the QUIT and INTR character are used in process_send_signal
643 so set them here to something useful. */
644 if (s
.main
.c_cc
[VQUIT
] == CDISABLE
)
645 s
.main
.c_cc
[VQUIT
] = '\\'&037; /* Control-\ */
646 if (s
.main
.c_cc
[VINTR
] == CDISABLE
)
647 s
.main
.c_cc
[VINTR
] = 'C'&037; /* Control-C */
648 #endif /* not SIGNALS_VIA_CHARACTERS */
651 /* AIX enhanced edit loses NULs, so disable it */
654 s
.main
.c_iflag
&= ~ASCEDIT
;
656 /* Also, PTY overloads NUL and BREAK.
657 don't ignore break, but don't signal either, so it looks like NUL. */
658 s
.main
.c_iflag
&= ~IGNBRK
;
659 s
.main
.c_iflag
&= ~BRKINT
;
660 /* rms: Formerly it set s.main.c_cc[VINTR] to 0377 here
661 unconditionally. Then a SIGNALS_VIA_CHARACTERS conditional
662 would force it to 0377. That looks like duplicated code. */
663 #ifndef SIGNALS_VIA_CHARACTERS
664 /* QUIT and INTR work better as signals, so disable character forms */
665 s
.main
.c_cc
[VQUIT
] = CDISABLE
;
666 s
.main
.c_cc
[VINTR
] = CDISABLE
;
667 s
.main
.c_lflag
&= ~ISIG
;
668 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
669 s
.main
.c_cc
[VEOL
] = CDISABLE
;
670 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CBAUD
) | B9600
; /* baud rate sanity */
673 #else /* not HAVE_TERMIO */
675 s
.main
.sg_flags
&= ~(ECHO
| CRMOD
| ANYP
| ALLDELAY
| RAW
| LCASE
677 s
.main
.sg_flags
|= LPASS8
;
678 s
.main
.sg_erase
= 0377;
679 s
.main
.sg_kill
= 0377;
680 s
.lmode
= LLITOUT
| s
.lmode
; /* Don't strip 8th bit */
682 #endif /* not HAVE_TERMIO */
684 EMACS_SET_TTY (out
, &s
, 0);
690 #endif /* not DOS_NT */
694 #endif /* subprocesses */
696 /* Record a signal code and the handler for it. */
700 SIGTYPE (*handler
) P_ ((int));
703 static void save_signal_handlers
P_ ((struct save_signal
*));
704 static void restore_signal_handlers
P_ ((struct save_signal
*));
706 /* Suspend the Emacs process; give terminal to its superior. */
712 /* "Foster" parentage allows emacs to return to a subprocess that attached
713 to the current emacs as a cheaper than starting a whole new process. This
714 is set up by KEPTEDITOR.COM. */
715 unsigned long parent_id
, foster_parent_id
;
718 fpid_string
= getenv ("EMACS_PARENT_PID");
719 if (fpid_string
!= NULL
)
721 sscanf (fpid_string
, "%x", &foster_parent_id
);
722 if (foster_parent_id
!= 0)
723 parent_id
= foster_parent_id
;
725 parent_id
= getppid ();
728 parent_id
= getppid ();
730 xfree (fpid_string
); /* On VMS, this was malloc'd */
732 if (parent_id
&& parent_id
!= 0xffffffff)
734 SIGTYPE (*oldsig
)() = (int) signal (SIGINT
, SIG_IGN
);
735 int status
= LIB$
ATTACH (&parent_id
) & 1;
736 signal (SIGINT
, oldsig
);
745 d_prompt
.l
= sizeof ("Emacs: "); /* Our special prompt */
746 d_prompt
.a
= "Emacs: "; /* Just a reminder */
747 LIB$
SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt
, 0);
752 #if defined (SIGTSTP) && !defined (MSDOS)
755 int pgrp
= EMACS_GETPGRP (0);
756 EMACS_KILLPG (pgrp
, SIGTSTP
);
759 #else /* No SIGTSTP */
760 #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
761 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
762 kill (getpid (), SIGQUIT
);
764 #else /* No SIGTSTP or USG_JOBCTRL */
766 /* On a system where suspending is not implemented,
767 instead fork a subshell and let it talk directly to the terminal
771 #endif /* no USG_JOBCTRL */
772 #endif /* no SIGTSTP */
776 /* Fork a subshell. */
782 #ifdef DOS_NT /* Demacs 1.1.2 91/10/20 Manabu Higashida */
784 char oldwd
[MAXPATHLEN
+1]; /* Fixed length is safe on MSDOS. */
787 struct save_signal saved_handlers
[5];
789 unsigned char *str
= 0;
792 saved_handlers
[0].code
= SIGINT
;
793 saved_handlers
[1].code
= SIGQUIT
;
794 saved_handlers
[2].code
= SIGTERM
;
796 saved_handlers
[3].code
= SIGIO
;
797 saved_handlers
[4].code
= 0;
799 saved_handlers
[3].code
= 0;
802 /* Mentioning current_buffer->buffer would mean including buffer.h,
803 which somehow wedges the hp compiler. So instead... */
805 dir
= intern ("default-directory");
806 if (NILP (Fboundp (dir
)))
808 dir
= Fsymbol_value (dir
);
812 dir
= expand_and_dir_to_file (Funhandled_file_name_directory (dir
), Qnil
);
813 str
= (unsigned char *) alloca (SCHARS (dir
) + 2);
815 bcopy (SDATA (dir
), str
, len
);
816 if (str
[len
- 1] != '/') str
[len
++] = '/';
823 save_signal_handlers (saved_handlers
);
824 synch_process_alive
= 1;
825 #endif /* __DJGPP__ > 1 */
829 error ("Can't spawn subshell");
836 #ifdef DOS_NT /* MW, Aug 1993 */
839 sh
= (char *) egetenv ("SUSPEND"); /* KFS, 1994-12-14 */
842 sh
= (char *) egetenv ("SHELL");
846 /* Use our buffer's default directory for the subshell. */
848 chdir ((char *) str
);
851 close_process_descs (); /* Close Emacs's pipes/ptys */
854 #ifdef SET_EMACS_PRIORITY
856 extern EMACS_INT emacs_priority
;
858 if (emacs_priority
< 0)
859 nice (-emacs_priority
);
863 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
865 char *epwd
= getenv ("PWD");
866 char old_pwd
[MAXPATHLEN
+1+4];
868 /* If PWD is set, pass it with corrected value. */
871 strcpy (old_pwd
, epwd
);
872 if (str
[len
- 1] == '/')
874 setenv ("PWD", str
, 1);
879 putenv (old_pwd
); /* restore previous value */
881 #if 0 /* This is also reported if last command executed in subshell failed, KFS */
883 report_file_error ("Can't execute subshell", Fcons (build_string (sh
), Qnil
));
885 #else /* not MSDOS */
887 /* Waits for process completion */
888 pid
= _spawnlp (_P_WAIT
, sh
, sh
, NULL
);
891 write (1, "Can't execute subshell", 22);
892 #else /* not WINDOWSNT */
893 execlp (sh
, sh
, (char *) 0);
894 write (1, "Can't execute subshell", 22);
896 #endif /* not WINDOWSNT */
897 #endif /* not MSDOS */
900 /* Do this now if we did not do it before. */
901 #if !defined (MSDOS) || __DJGPP__ == 1
902 save_signal_handlers (saved_handlers
);
903 synch_process_alive
= 1;
907 wait_for_termination (pid
);
909 restore_signal_handlers (saved_handlers
);
910 synch_process_alive
= 0;
915 save_signal_handlers (saved_handlers
)
916 struct save_signal
*saved_handlers
;
918 while (saved_handlers
->code
)
920 saved_handlers
->handler
921 = (SIGTYPE (*) P_ ((int))) signal (saved_handlers
->code
, SIG_IGN
);
927 restore_signal_handlers (saved_handlers
)
928 struct save_signal
*saved_handlers
;
930 while (saved_handlers
->code
)
932 signal (saved_handlers
->code
, saved_handlers
->handler
);
938 /* If SIGIO is broken, don't do anything. */
955 unrequest_sigio (void)
962 int old_fcntl_flags
[MAXDESC
];
969 old_fcntl_flags
[fd
] = fcntl (fd
, F_GETFL
, 0) & ~FASYNC
;
970 fcntl (fd
, F_SETFL
, old_fcntl_flags
[fd
] | FASYNC
);
972 interrupts_deferred
= 0;
980 fcntl (fd
, F_SETFL
, old_fcntl_flags
[fd
]);
984 #ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
985 /* XXX Uhm, FASYNC is not used anymore here. */
986 /* XXX Yeah, but you need it for SIGIO, don't you? */
995 sigunblock (sigmask (SIGWINCH
));
997 sigunblock (sigmask (SIGIO
));
999 interrupts_deferred
= 0;
1003 unrequest_sigio (void)
1008 #if 0 /* XXX What's wrong with blocking SIGIO under X? */
1014 sigblock (sigmask (SIGWINCH
));
1016 sigblock (sigmask (SIGIO
));
1017 interrupts_deferred
= 1;
1020 #else /* no FASYNC */
1026 if (noninteractive
|| read_socket_hook
)
1029 croak ("request_sigio");
1035 if (noninteractive
|| read_socket_hook
)
1038 croak ("unrequest_sigio");
1043 #endif /* F_SETFL */
1046 /* Saving and restoring the process group of Emacs's terminal. */
1050 /* The process group of which Emacs was a member when it initially
1053 If Emacs was in its own process group (i.e. inherited_pgroup ==
1054 getpid ()), then we know we're running under a shell with job
1055 control (Emacs would never be run as part of a pipeline).
1058 If Emacs was not in its own process group, then we know we're
1059 running under a shell (or a caller) that doesn't know how to
1060 separate itself from Emacs (like sh). Emacs must be in its own
1061 process group in order to receive SIGIO correctly. In this
1062 situation, we put ourselves in our own pgroup, forcibly set the
1063 tty's pgroup to our pgroup, and make sure to restore and reinstate
1064 the tty's pgroup just like any other terminal setting. If
1065 inherited_group was not the tty's pgroup, then we'll get a
1066 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
1067 it goes foreground in the future, which is what should happen.
1069 This variable is initialized in emacs.c. */
1070 int inherited_pgroup
;
1072 /* Split off the foreground process group to Emacs alone. When we are
1073 in the foreground, but not started in our own process group,
1074 redirect the tty device handle FD to point to our own process
1075 group. We need to be in our own process group to receive SIGIO
1078 narrow_foreground_group (int fd
)
1082 setpgrp (0, inherited_pgroup
);
1084 /* XXX inherited_pgroup should not be zero here, but GTK seems to
1086 if (! inherited_pgroup
)
1087 abort (); /* Should not happen. */
1089 if (inherited_pgroup
!= me
)
1090 EMACS_SET_TTY_PGRP (fd
, &me
); /* XXX This only works on the controlling tty. */
1094 /* Set the tty to our original foreground group. */
1096 widen_foreground_group (int fd
)
1098 if (inherited_pgroup
!= getpid ())
1099 EMACS_SET_TTY_PGRP (fd
, &inherited_pgroup
);
1100 setpgrp (0, inherited_pgroup
);
1103 #endif /* BSD_PGRPS */
1105 /* Getting and setting emacs_tty structures. */
1107 /* Set *TC to the parameters associated with the terminal FD.
1108 Return zero if all's well, or -1 if we ran into an error we
1109 couldn't deal with. */
1111 emacs_get_tty (fd
, settings
)
1113 struct emacs_tty
*settings
;
1115 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
1117 /* We have those nifty POSIX tcmumbleattr functions. */
1118 bzero (&settings
->main
, sizeof (settings
->main
));
1119 if (tcgetattr (fd
, &settings
->main
) < 0)
1124 /* The SYSV-style interface? */
1125 if (ioctl (fd
, TCGETA
, &settings
->main
) < 0)
1130 /* Vehemently Monstrous System? :-) */
1131 if (! (SYS$
QIOW (0, fd
, IO$_SENSEMODE
, settings
, 0, 0,
1132 &settings
->main
.class, 12, 0, 0, 0, 0)
1138 /* I give up - I hope you have the BSD ioctls. */
1139 if (ioctl (fd
, TIOCGETP
, &settings
->main
) < 0)
1141 #endif /* not DOS_NT */
1146 /* Suivant - Do we have to get struct ltchars data? */
1148 if (ioctl (fd
, TIOCGLTC
, &settings
->ltchars
) < 0)
1152 /* How about a struct tchars and a wordful of lmode bits? */
1154 if (ioctl (fd
, TIOCGETC
, &settings
->tchars
) < 0
1155 || ioctl (fd
, TIOCLGET
, &settings
->lmode
) < 0)
1159 /* We have survived the tempest. */
1164 /* Set the parameters of the tty on FD according to the contents of
1165 *SETTINGS. If FLUSHP is non-zero, we discard input.
1166 Return 0 if all went well, and -1 if anything failed. */
1169 emacs_set_tty (fd
, settings
, flushp
)
1171 struct emacs_tty
*settings
;
1174 /* Set the primary parameters - baud rate, character size, etcetera. */
1177 /* We have those nifty POSIX tcmumbleattr functions.
1178 William J. Smith <wjs@wiis.wang.com> writes:
1179 "POSIX 1003.1 defines tcsetattr to return success if it was
1180 able to perform any of the requested actions, even if some
1181 of the requested actions could not be performed.
1182 We must read settings back to ensure tty setup properly.
1183 AIX requires this to keep tty from hanging occasionally." */
1184 /* This make sure that we don't loop indefinitely in here. */
1185 for (i
= 0 ; i
< 10 ; i
++)
1186 if (tcsetattr (fd
, flushp
? TCSAFLUSH
: TCSADRAIN
, &settings
->main
) < 0)
1197 bzero (&new, sizeof (new));
1198 /* Get the current settings, and see if they're what we asked for. */
1199 tcgetattr (fd
, &new);
1200 /* We cannot use memcmp on the whole structure here because under
1201 * aix386 the termios structure has some reserved field that may
1204 if ( new.c_iflag
== settings
->main
.c_iflag
1205 && new.c_oflag
== settings
->main
.c_oflag
1206 && new.c_cflag
== settings
->main
.c_cflag
1207 && new.c_lflag
== settings
->main
.c_lflag
1208 && memcmp (new.c_cc
, settings
->main
.c_cc
, NCCS
) == 0)
1216 /* The SYSV-style interface? */
1217 if (ioctl (fd
, flushp
? TCSETAF
: TCSETAW
, &settings
->main
) < 0)
1222 /* Vehemently Monstrous System? :-) */
1223 if (! (SYS$
QIOW (0, fd
, IO$_SETMODE
, &input_iosb
, 0, 0,
1224 &settings
->main
.class, 12, 0, 0, 0, 0)
1230 /* I give up - I hope you have the BSD ioctls. */
1231 if (ioctl (fd
, (flushp
) ? TIOCSETP
: TIOCSETN
, &settings
->main
) < 0)
1233 #endif /* not DOS_NT */
1239 /* Suivant - Do we have to get struct ltchars data? */
1241 if (ioctl (fd
, TIOCSLTC
, &settings
->ltchars
) < 0)
1245 /* How about a struct tchars and a wordful of lmode bits? */
1247 if (ioctl (fd
, TIOCSETC
, &settings
->tchars
) < 0
1248 || ioctl (fd
, TIOCLSET
, &settings
->lmode
) < 0)
1252 /* We have survived the tempest. */
1259 /* BSD 4.1 needs to keep track of the lmode bits in order to start
1265 int old_fcntl_owner
[MAXDESC
];
1266 #endif /* F_SETOWN */
1268 /* This may also be defined in stdio,
1269 but if so, this does no harm,
1270 and using the same name avoids wasting the other one's space. */
1273 extern char *_sobuf
;
1275 #if defined (USG) || defined (DGUX)
1276 unsigned char _sobuf
[BUFSIZ
+8];
1278 char _sobuf
[BUFSIZ
];
1283 static struct ltchars new_ltchars
= {-1,-1,-1,-1,-1,-1};
1286 static struct tchars new_tchars
= {-1,-1,-1,-1,-1,-1};
1289 /* Initialize the terminal mode on all tty devices that are currently
1293 init_all_sys_modes (void)
1295 struct tty_display_info
*tty
;
1296 for (tty
= tty_list
; tty
; tty
= tty
->next
)
1297 init_sys_modes (tty
);
1300 /* Initialize the terminal mode on the given tty device. */
1303 init_sys_modes (tty_out
)
1304 struct tty_display_info
*tty_out
;
1306 struct emacs_tty tty
;
1310 static int oob_chars
[2] = {0, 1 << 7}; /* catch C-g's */
1311 extern int (*interrupt_signal
) ();
1315 Vtty_erase_char
= Qnil
;
1320 if (!tty_out
->output
)
1321 return; /* The tty is suspended. */
1325 input_ef
= get_kbd_event_flag ();
1326 /* LIB$GET_EF (&input_ef); */
1327 SYS$
CLREF (input_ef
);
1328 waiting_for_ast
= 0;
1330 timer_ef
= get_timer_event_flag ();
1331 /* LIB$GET_EF (&timer_ef); */
1332 SYS$
CLREF (timer_ef
);
1336 LIB$
GET_EF (&process_ef
);
1337 SYS$
CLREF (process_ef
);
1339 if (input_ef
/ 32 != process_ef
/ 32)
1340 croak ("Input and process event flags in different clusters.");
1342 if (input_ef
/ 32 != timer_ef
/ 32)
1343 croak ("Input and timer event flags in different clusters.");
1345 input_eflist
= ((unsigned) 1 << (input_ef
% 32)) |
1346 ((unsigned) 1 << (process_ef
% 32));
1348 timer_eflist
= ((unsigned) 1 << (input_ef
% 32)) |
1349 ((unsigned) 1 << (timer_ef
% 32));
1351 sys_access_reinit ();
1357 /* read_socket_hook is not global anymore. I think doing this
1358 unconditionally will not cause any problems. */
1359 if (! read_socket_hook
&& EQ (Vinitial_window_system
, Qnil
))
1361 narrow_foreground_group (fileno (tty_out
->input
));
1364 if (! tty_out
->old_tty
)
1365 tty_out
->old_tty
= (struct emacs_tty
*) xmalloc (sizeof (struct emacs_tty
));
1367 EMACS_GET_TTY (fileno (tty_out
->input
), tty_out
->old_tty
);
1369 tty
= *tty_out
->old_tty
;
1371 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
1372 XSETINT (Vtty_erase_char
, tty
.main
.c_cc
[VERASE
]);
1374 tty
.main
.c_iflag
|= (IGNBRK
); /* Ignore break condition */
1375 tty
.main
.c_iflag
&= ~ICRNL
; /* Disable map of CR to NL on input */
1376 #ifdef INLCR /* I'm just being cautious,
1377 since I can't check how widespread INLCR is--rms. */
1378 tty
.main
.c_iflag
&= ~INLCR
; /* Disable map of NL to CR on input */
1381 tty
.main
.c_iflag
&= ~ISTRIP
; /* don't strip 8th bit on input */
1383 tty
.main
.c_lflag
&= ~ECHO
; /* Disable echo */
1384 tty
.main
.c_lflag
&= ~ICANON
; /* Disable erase/kill processing */
1386 tty
.main
.c_lflag
&= ~IEXTEN
; /* Disable other editing characters. */
1388 tty
.main
.c_lflag
|= ISIG
; /* Enable signals */
1389 if (tty_out
->flow_control
)
1391 tty
.main
.c_iflag
|= IXON
; /* Enable start/stop output control */
1393 tty
.main
.c_iflag
&= ~IXANY
;
1397 tty
.main
.c_iflag
&= ~IXON
; /* Disable start/stop output control */
1398 tty
.main
.c_oflag
&= ~ONLCR
; /* Disable map of NL to CR-NL
1400 tty
.main
.c_oflag
&= ~TAB3
; /* Disable tab expansion */
1402 if (tty_out
->meta_key
)
1404 tty
.main
.c_cflag
|= CS8
; /* allow 8th bit on input */
1405 tty
.main
.c_cflag
&= ~PARENB
;/* Don't check parity */
1408 if (tty_out
->input
== stdin
)
1410 tty
.main
.c_cc
[VINTR
] = quit_char
; /* C-g (usually) gives SIGINT */
1411 /* Set up C-g for both SIGQUIT and SIGINT.
1412 We don't know which we will get, but we handle both alike
1413 so which one it really gives us does not matter. */
1414 tty
.main
.c_cc
[VQUIT
] = quit_char
;
1418 /* We normally don't get interrupt or quit signals from tty
1419 devices other than our controlling terminal; therefore,
1420 we must handle C-g as normal input. Unfortunately, this
1421 means that the interrupt and quit feature must be
1422 disabled on secondary ttys, or we would not even see the
1425 Note that even though emacsclient could have special code
1426 to pass SIGINT to Emacs, we should _not_ enable
1427 interrupt/quit keys for emacsclient frames. This means
1428 that we can't break out of loops in C code from a
1429 secondary tty frame, but we can always decide what
1430 display the C-g came from, which is more important from a
1431 usability point of view. (Consider the case when two
1432 people work together using the same Emacs instance.) */
1433 tty
.main
.c_cc
[VINTR
] = CDISABLE
;
1434 tty
.main
.c_cc
[VQUIT
] = CDISABLE
;
1436 tty
.main
.c_cc
[VMIN
] = 1; /* Input should wait for at least 1 char */
1437 tty
.main
.c_cc
[VTIME
] = 0; /* no matter how long that takes. */
1439 tty
.main
.c_cc
[VSWTCH
] = CDISABLE
; /* Turn off shell layering use
1443 #if defined (mips) || defined (HAVE_TCATTR)
1445 tty
.main
.c_cc
[VSUSP
] = CDISABLE
; /* Turn off mips handling of C-z. */
1448 tty
.main
.c_cc
[V_DSUSP
] = CDISABLE
; /* Turn off mips handling of C-y. */
1449 #endif /* V_DSUSP */
1450 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1451 tty
.main
.c_cc
[VDSUSP
] = CDISABLE
;
1454 tty
.main
.c_cc
[VLNEXT
] = CDISABLE
;
1457 tty
.main
.c_cc
[VREPRINT
] = CDISABLE
;
1458 #endif /* VREPRINT */
1460 tty
.main
.c_cc
[VWERASE
] = CDISABLE
;
1461 #endif /* VWERASE */
1463 tty
.main
.c_cc
[VDISCARD
] = CDISABLE
;
1464 #endif /* VDISCARD */
1466 if (tty_out
->flow_control
)
1469 tty
.main
.c_cc
[VSTART
] = '\021';
1472 tty
.main
.c_cc
[VSTOP
] = '\023';
1478 tty
.main
.c_cc
[VSTART
] = CDISABLE
;
1481 tty
.main
.c_cc
[VSTOP
] = CDISABLE
;
1484 #endif /* mips or HAVE_TCATTR */
1486 #ifdef SET_LINE_DISCIPLINE
1487 /* Need to explicitly request TERMIODISC line discipline or
1488 Ultrix's termios does not work correctly. */
1489 tty
.main
.c_line
= SET_LINE_DISCIPLINE
;
1493 /* AIX enhanced edit loses NULs, so disable it. */
1494 tty
.main
.c_line
= 0;
1495 tty
.main
.c_iflag
&= ~ASCEDIT
;
1497 tty
.main
.c_cc
[VSTRT
] = CDISABLE
;
1498 tty
.main
.c_cc
[VSTOP
] = CDISABLE
;
1499 tty
.main
.c_cc
[VSUSP
] = CDISABLE
;
1500 tty
.main
.c_cc
[VDSUSP
] = CDISABLE
;
1501 #endif /* IBMR2AIX */
1502 if (tty_out
->flow_control
)
1505 tty
.main
.c_cc
[VSTART
] = '\021';
1508 tty
.main
.c_cc
[VSTOP
] = '\023';
1511 /* Also, PTY overloads NUL and BREAK.
1512 don't ignore break, but don't signal either, so it looks like NUL.
1513 This really serves a purpose only if running in an XTERM window
1514 or via TELNET or the like, but does no harm elsewhere. */
1515 tty
.main
.c_iflag
&= ~IGNBRK
;
1516 tty
.main
.c_iflag
&= ~BRKINT
;
1518 #else /* if not HAVE_TERMIO */
1520 tty
.main
.tt_char
|= TT$M_NOECHO
;
1522 tty
.main
.tt_char
|= TT$M_EIGHTBIT
;
1523 if (tty_out
->flow_control
)
1524 tty
.main
.tt_char
|= TT$M_TTSYNC
;
1526 tty
.main
.tt_char
&= ~TT$M_TTSYNC
;
1527 tty
.main
.tt2_char
|= TT2$M_PASTHRU
| TT2$M_XON
;
1528 #else /* not VMS (BSD, that is) */
1530 XSETINT (Vtty_erase_char
, tty
.main
.sg_erase
);
1531 tty
.main
.sg_flags
&= ~(ECHO
| CRMOD
| XTABS
);
1533 tty
.main
.sg_flags
|= ANYP
;
1534 tty
.main
.sg_flags
|= interrupt_input
? RAW
: CBREAK
;
1535 #endif /* not DOS_NT */
1536 #endif /* not VMS (BSD, that is) */
1537 #endif /* not HAVE_TERMIO */
1539 /* If going to use CBREAK mode, we must request C-g to interrupt
1540 and turn off start and stop chars, etc. If not going to use
1541 CBREAK mode, do this anyway so as to turn off local flow
1542 control for user coming over network on 4.2; in this case,
1543 only t_stopc and t_startc really matter. */
1546 /* Note: if not using CBREAK mode, it makes no difference how we
1548 tty
.tchars
= new_tchars
;
1549 tty
.tchars
.t_intrc
= quit_char
;
1550 if (tty_out
->flow_control
)
1552 tty
.tchars
.t_startc
= '\021';
1553 tty
.tchars
.t_stopc
= '\023';
1556 tty
.lmode
= LDECCTQ
| LLITOUT
| LPASS8
| LNOFLSH
| tty_out
->old_tty
.lmode
;
1558 /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
1559 anything, and leaving it in breaks the meta key. Go figure. */
1560 tty
.lmode
&= ~LLITOUT
;
1567 #endif /* HAVE_TCHARS */
1568 #endif /* not HAVE_TERMIO */
1571 tty
.ltchars
= new_ltchars
;
1572 #endif /* HAVE_LTCHARS */
1573 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
1574 if (!tty_out
->term_initted
)
1575 internal_terminal_init ();
1579 EMACS_SET_TTY (fileno (tty_out
->input
), &tty
, 0);
1581 /* This code added to insure that, if flow-control is not to be used,
1582 we have an unlocked terminal at the start. */
1585 if (!tty_out
->flow_control
) ioctl (fileno (tty_out
->input
), TCXONC
, 1);
1588 if (!tty_out
->flow_control
) ioctl (fileno (tty_out
->input
), TIOCSTART
, 0);
1591 #if defined (HAVE_TERMIOS) || defined (HPUX9)
1593 if (!tty_out
->flow_control
) tcflow (fileno (tty_out
->input
), TCOON
);
1601 /* IBM's HFT device usually thinks a ^J should be LF/CR. We need it
1602 to be only LF. This is the way that is done. */
1605 if (ioctl (1, HFTGETID
, &tty
) != -1)
1606 write (1, "\033[20l", 5);
1612 /* Appears to do nothing when in PASTHRU mode.
1613 SYS$QIOW (0, fileno (tty_out->input), IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
1614 interrupt_signal, oob_chars, 0, 0, 0, 0);
1616 queue_kbd_input (0);
1620 #ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
1621 if (interrupt_input
)
1623 old_fcntl_owner
[fileno (tty_out
->input
)] =
1624 fcntl (fileno (tty_out
->input
), F_GETOWN
, 0);
1625 fcntl (fileno (tty_out
->input
), F_SETOWN
, getpid ());
1626 init_sigio (fileno (tty_out
->input
));
1628 if (gpm_tty
== tty_out
)
1630 /* Arrange for mouse events to give us SIGIO signals. */
1631 fcntl (gpm_fd
, F_SETOWN
, getpid ());
1632 fcntl (gpm_fd
, F_SETFL
, fcntl (gpm_fd
, F_GETFL
, 0) | O_NONBLOCK
);
1633 init_sigio (gpm_fd
);
1635 #endif /* HAVE_GPM */
1637 #endif /* F_GETOWN */
1638 #endif /* F_SETFL */
1641 if (interrupt_input
)
1642 init_sigio (fileno (tty_out
->input
));
1645 #ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1649 /* This symbol is defined on recent USG systems.
1650 Someone says without this call USG won't really buffer the file
1651 even with a call to setbuf. */
1652 setvbuf (tty_out
->output
, (char *) _sobuf
, _IOFBF
, sizeof _sobuf
);
1654 setbuf (tty_out
->output
, (char *) _sobuf
);
1657 if (tty_out
->terminal
->set_terminal_modes_hook
)
1658 tty_out
->terminal
->set_terminal_modes_hook (tty_out
->terminal
);
1660 if (!tty_out
->term_initted
)
1662 Lisp_Object tail
, frame
;
1663 FOR_EACH_FRAME (tail
, frame
)
1665 /* XXX This needs to be revised. */
1666 if (FRAME_TERMCAP_P (XFRAME (frame
))
1667 && FRAME_TTY (XFRAME (frame
)) == tty_out
)
1668 init_frame_faces (XFRAME (frame
));
1672 if (tty_out
->term_initted
&& no_redraw_on_reenter
)
1674 /* XXX This seems wrong on multi-tty. */
1675 if (display_completed
)
1676 direct_output_forward_char (0);
1680 Lisp_Object tail
, frame
;
1682 FOR_EACH_FRAME (tail
, frame
)
1684 if (FRAME_TERMCAP_P (XFRAME (frame
))
1685 && FRAME_TTY (XFRAME (frame
)) == tty_out
)
1686 FRAME_GARBAGED_P (XFRAME (frame
)) = 1;
1690 tty_out
->term_initted
= 1;
1693 /* Return nonzero if safe to use tabs in output.
1694 At the time this is called, init_sys_modes has not been done yet. */
1697 tabs_safe_p (int fd
)
1699 struct emacs_tty etty
;
1701 EMACS_GET_TTY (fd
, &etty
);
1702 return EMACS_TTY_TABS_OK (&etty
);
1705 /* Get terminal size from system.
1706 Store number of lines into *HEIGHTP and width into *WIDTHP.
1707 We store 0 if there's no valid information. */
1710 get_tty_size (int fd
, int *widthp
, int *heightp
)
1716 struct winsize size
;
1718 if (ioctl (fd
, TIOCGWINSZ
, &size
) == -1)
1719 *widthp
= *heightp
= 0;
1722 *widthp
= size
.ws_col
;
1723 *heightp
= size
.ws_row
;
1729 /* SunOS - style. */
1730 struct ttysize size
;
1732 if (ioctl (fd
, TIOCGSIZE
, &size
) == -1)
1733 *widthp
= *heightp
= 0;
1736 *widthp
= size
.ts_cols
;
1737 *heightp
= size
.ts_lines
;
1743 /* Use a fresh channel since the current one may have stale info
1744 (for example, from prior to a suspend); and to avoid a dependency
1745 in the init sequence. */
1747 struct sensemode tty
;
1749 SYS$
ASSIGN (&input_dsc
, &chan
, 0, 0);
1750 SYS$
QIOW (0, chan
, IO$_SENSEMODE
, &tty
, 0, 0,
1751 &tty
.class, 12, 0, 0, 0, 0);
1753 *widthp
= tty
.scr_wid
;
1754 *heightp
= tty
.scr_len
;
1758 *widthp
= ScreenCols ();
1759 *heightp
= ScreenRows ();
1760 #else /* system doesn't know size */
1764 #endif /* not VMS */
1765 #endif /* not SunOS-style */
1766 #endif /* not BSD-style */
1769 /* Set the logical window size associated with descriptor FD
1770 to HEIGHT and WIDTH. This is used mainly with ptys. */
1773 set_window_size (fd
, height
, width
)
1774 int fd
, height
, width
;
1779 struct winsize size
;
1780 size
.ws_row
= height
;
1781 size
.ws_col
= width
;
1783 if (ioctl (fd
, TIOCSWINSZ
, &size
) == -1)
1784 return 0; /* error */
1791 /* SunOS - style. */
1792 struct ttysize size
;
1793 size
.ts_lines
= height
;
1794 size
.ts_cols
= width
;
1796 if (ioctl (fd
, TIOCGSIZE
, &size
) == -1)
1802 #endif /* not SunOS-style */
1803 #endif /* not BSD-style */
1808 /* Prepare all terminal devices for exiting Emacs. */
1811 reset_all_sys_modes (void)
1813 struct tty_display_info
*tty
;
1814 for (tty
= tty_list
; tty
; tty
= tty
->next
)
1815 reset_sys_modes (tty
);
1818 /* Prepare the terminal for closing it; move the cursor to the
1819 bottom of the frame, turn off interrupt-driven I/O, etc. */
1822 reset_sys_modes (tty_out
)
1823 struct tty_display_info
*tty_out
;
1830 if (!tty_out
->term_initted
)
1833 if (!tty_out
->output
)
1834 return; /* The tty is suspended. */
1836 /* Go to and clear the last line of the terminal. */
1838 cmgoto (tty_out
, FrameRows (tty_out
) - 1, 0);
1840 /* Code adapted from tty_clear_end_of_line. */
1841 if (tty_out
->TS_clr_line
)
1843 emacs_tputs (tty_out
, tty_out
->TS_clr_line
, 1, cmputc
);
1846 { /* have to do it the hard way */
1848 tty_turn_off_insert (tty_out
);
1850 for (i
= curX (tty_out
); i
< FrameCols (tty_out
) - 1; i
++)
1852 fputc (' ', tty_out
->output
);
1856 cmgoto (tty_out
, FrameRows (tty_out
) - 1, 0);
1857 fflush (tty_out
->output
);
1859 #if defined (IBMR2AIX) && defined (AIXHFT)
1861 /* HFT devices normally use ^J as a LF/CR. We forced it to
1862 do the LF only. Now, we need to reset it. */
1865 if (ioctl (1, HFTGETID
, &tty
) != -1)
1866 write (1, "\033[20h", 5);
1870 if (tty_out
->terminal
->reset_terminal_modes_hook
)
1871 tty_out
->terminal
->reset_terminal_modes_hook (tty_out
->terminal
);
1875 /* Avoid possible loss of output when changing terminal modes. */
1876 fsync (fileno (tty_out
->output
));
1881 #ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
1882 if (interrupt_input
)
1884 reset_sigio (fileno (tty_out
->input
));
1885 fcntl (fileno (tty_out
->input
), F_SETOWN
,
1886 old_fcntl_owner
[fileno (tty_out
->input
)]);
1888 #endif /* F_SETOWN */
1890 fcntl (fileno (tty_out
->input
), F_SETFL
,
1891 fcntl (fileno (tty_out
->input
), F_GETFL
, 0) & ~O_NDELAY
);
1893 #endif /* F_SETFL */
1895 if (interrupt_input
)
1896 reset_sigio (fileno (tty_out
->input
));
1899 if (tty_out
->old_tty
)
1900 while (EMACS_SET_TTY (fileno (tty_out
->input
),
1901 tty_out
->old_tty
, 0) < 0 && errno
== EINTR
)
1904 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1908 #ifdef SET_LINE_DISCIPLINE
1909 /* Ultrix's termios *ignores* any line discipline except TERMIODISC.
1910 A different old line discipline is therefore not restored, yet.
1911 Restore the old line discipline by hand. */
1912 ioctl (0, TIOCSETD
, &tty_out
->old_tty
.main
.c_line
);
1920 widen_foreground_group (fileno (tty_out
->input
));
1926 /* Set up the proper status flags for use of a pty. */
1932 /* I'm told that TOICREMOTE does not mean control chars
1933 "can't be sent" but rather that they don't have
1934 input-editing or signaling effects.
1935 That should be good, because we have other ways
1936 to do those things in Emacs.
1937 However, telnet mode seems not to work on 4.2.
1938 So TIOCREMOTE is turned off now. */
1940 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1941 will hang. In particular, the "timeout" feature (which
1942 causes a read to return if there is no data available)
1943 does this. Also it is known that telnet mode will hang
1944 in such a way that Emacs must be stopped (perhaps this
1945 is the same problem).
1947 If TIOCREMOTE is turned off, then there is a bug in
1948 hp-ux which sometimes loses data. Apparently the
1949 code which blocks the master process when the internal
1950 buffer fills up does not work. Other than this,
1951 though, everything else seems to work fine.
1953 Since the latter lossage is more benign, we may as well
1954 lose that way. -- cph */
1956 #if defined(SYSV_PTYS) || defined(UNIX98_PTYS)
1959 ioctl (fd
, FIONBIO
, &on
);
1964 #endif /* HAVE_PTYS */
1968 /* Assigning an input channel is done at the start of Emacs execution.
1969 This is called each time Emacs is resumed, also, but does nothing
1970 because input_chain is no longer zero. */
1977 if (fileno (CURTTY ()->input
)) == 0)
1979 status
= SYS$
ASSIGN (&input_dsc
, &fileno (CURTTY ()->input
)), 0, 0);
1985 /* Deassigning the input channel is done before exiting. */
1990 return SYS$
DASSGN (fileno (CURTTY ()->input
)));
1995 /* Request reading one character into the keyboard buffer.
1996 This is done as soon as the buffer becomes empty. */
2002 extern kbd_input_ast ();
2004 waiting_for_ast
= 0;
2006 status
= SYS$
QIO (0, fileno (CURTTY()->input
), IO$_READVBLK
,
2007 &input_iosb
, kbd_input_ast
, 1,
2008 &input_buffer
, 1, 0, terminator_mask
, 0, 0);
2013 /* Ast routine that is called when keyboard input comes in
2014 in accord with the SYS$QIO above. */
2019 register int c
= -1;
2020 int old_errno
= errno
;
2021 extern EMACS_TIME
*input_available_clear_time
;
2023 if (waiting_for_ast
)
2024 SYS$
SETEF (input_ef
);
2025 waiting_for_ast
= 0;
2028 if (input_count
== 25)
2030 printf ("Ast # %d,", input_count
);
2031 printf (" iosb = %x, %x, %x, %x",
2032 input_iosb
.offset
, input_iosb
.status
, input_iosb
.termlen
,
2035 if (input_iosb
.offset
)
2039 printf (", char = 0%o", c
);
2051 struct input_event e
;
2054 e
.kind
= ASCII_KEYSTROKE_EVENT
;
2055 XSETINT (e
.code
, c
);
2056 e
.frame_or_window
= selected_frame
;
2057 kbd_buffer_store_event (&e
);
2059 if (input_available_clear_time
)
2060 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
2064 /* Wait until there is something in kbd_buffer. */
2067 wait_for_kbd_input ()
2069 extern int have_process_input
, process_exited
;
2071 /* If already something, avoid doing system calls. */
2072 if (detect_input_pending ())
2076 /* Clear a flag, and tell ast routine above to set it. */
2077 SYS$
CLREF (input_ef
);
2078 waiting_for_ast
= 1;
2079 /* Check for timing error: ast happened while we were doing that. */
2080 if (!detect_input_pending ())
2082 /* No timing error: wait for flag to be set. */
2083 set_waiting_for_input (0);
2084 SYS$
WFLOR (input_ef
, input_eflist
);
2085 clear_waiting_for_input ();
2086 if (!detect_input_pending ())
2087 /* Check for subprocess input availability */
2089 int dsp
= have_process_input
|| process_exited
;
2091 SYS$
CLREF (process_ef
);
2092 if (have_process_input
)
2093 process_command_input ();
2098 update_mode_lines
++;
2099 prepare_menu_bars ();
2100 redisplay_preserve_echo_area (18);
2104 waiting_for_ast
= 0;
2107 /* Get rid of any pending QIO, when we are about to suspend
2108 or when we want to throw away pending input.
2109 We wait for a positive sign that the AST routine has run
2110 and therefore there is no I/O request queued when we return.
2111 SYS$SETAST is used to avoid a timing error. */
2117 printf ("At end_kbd_input.\n");
2121 if (LIB$
AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
2123 SYS$
CANCEL (fileno (CURTTY()->input
));
2128 /* Clear a flag, and tell ast routine above to set it. */
2129 SYS$
CLREF (input_ef
);
2130 waiting_for_ast
= 1;
2132 SYS$
CANCEL (fileno (CURTTY()->input
));
2134 SYS$
WAITFR (input_ef
);
2135 waiting_for_ast
= 0;
2138 /* Wait for either input available or time interval expiry. */
2141 input_wait_timeout (timeval
)
2142 int timeval
; /* Time to wait, in seconds */
2145 static int zero
= 0;
2146 static int large
= -10000000;
2148 LIB$
EMUL (&timeval
, &large
, &zero
, time
); /* Convert to VMS format */
2150 /* If already something, avoid doing system calls. */
2151 if (detect_input_pending ())
2155 /* Clear a flag, and tell ast routine above to set it. */
2156 SYS$
CLREF (input_ef
);
2157 waiting_for_ast
= 1;
2158 /* Check for timing error: ast happened while we were doing that. */
2159 if (!detect_input_pending ())
2161 /* No timing error: wait for flag to be set. */
2163 if (SYS$
SETIMR (timer_ef
, time
, 0, 1) & 1) /* Set timer */
2164 SYS$
WFLOR (timer_ef
, timer_eflist
); /* Wait for timer expiry or input */
2166 waiting_for_ast
= 0;
2169 /* The standard `sleep' routine works some other way
2170 and it stops working if you have ever quit out of it.
2171 This one continues to work. */
2177 static int zero
= 0;
2178 static int large
= -10000000;
2180 LIB$
EMUL (&timeval
, &large
, &zero
, time
); /* Convert to VMS format */
2183 if (SYS$
SETIMR (timer_ef
, time
, 0, 1) & 1) /* Set timer */
2184 SYS$
WAITFR (timer_ef
); /* Wait for timer expiry only */
2205 croak ("request sigio");
2213 croak ("unrequest sigio");
2218 /* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
2223 #ifndef SYSTEM_MALLOC
2230 /* Some systems that cannot dump also cannot implement these. */
2233 * Return the address of the start of the text segment prior to
2234 * doing an unexec. After unexec the return value is undefined.
2235 * See crt0.c for further explanation and _start.
2239 #if !(defined (__NetBSD__) && defined (__ELF__))
2240 #ifndef HAVE_TEXT_START
2245 return ((char *) TEXT_START
);
2247 extern int _start ();
2248 return ((char *) _start
);
2249 #endif /* TEXT_START */
2251 #endif /* not HAVE_TEXT_START */
2255 * Return the address of the start of the data segment prior to
2256 * doing an unexec. After unexec the return value is undefined.
2257 * See crt0.c for further information and definition of data_start.
2259 * Apparently, on BSD systems this is etext at startup. On
2260 * USG systems (swapping) this is highly mmu dependent and
2261 * is also dependent on whether or not the program is running
2262 * with shared text. Generally there is a (possibly large)
2263 * gap between end of text and start of data with shared text.
2265 * On Uniplus+ systems with shared text, data starts at a
2266 * fixed address. Each port (from a given oem) is generally
2267 * different, and the specific value of the start of data can
2268 * be obtained via the UniPlus+ specific "uvar" system call,
2269 * however the method outlined in crt0.c seems to be more portable.
2271 * Probably what will have to happen when a USG unexec is available,
2272 * at least on UniPlus, is temacs will have to be made unshared so
2273 * that text and data are contiguous. Then once loadup is complete,
2274 * unexec will produce a shared executable where the data can be
2275 * at the normal shared text boundary and the startofdata variable
2276 * will be patched by unexec to the correct value.
2280 #ifndef start_of_data
2285 return ((char *) DATA_START
);
2287 #ifdef ORDINARY_LINK
2289 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
2290 * data_start isn't defined. We take the address of environ, which
2291 * is known to live at or near the start of the system crt0.c, and
2292 * we don't sweat the handful of bytes that might lose.
2294 extern char **environ
;
2296 return ((char *) &environ
);
2298 extern int data_start
;
2299 return ((char *) &data_start
);
2300 #endif /* ORDINARY_LINK */
2301 #endif /* DATA_START */
2303 #endif /* start_of_data */
2304 #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
2306 /* init_system_name sets up the string for the Lisp function
2307 system-name to return. */
2313 extern Lisp_Object Vsystem_name
;
2318 #include <sys/socket.h>
2320 #endif /* HAVE_SOCKETS */
2321 #endif /* not VMS */
2322 #endif /* not BSD4_1 */
2325 #ifndef HAVE_H_ERRNO
2328 #endif /* TRY_AGAIN */
2334 Vsystem_name
= build_string (sysname
);
2338 if ((sp
= egetenv ("SYS$NODE")) == 0)
2339 Vsystem_name
= build_string ("vax-vms");
2340 else if ((end
= index (sp
, ':')) == 0)
2341 Vsystem_name
= build_string (sp
);
2343 Vsystem_name
= make_string (sp
, end
- sp
);
2345 #ifndef HAVE_GETHOSTNAME
2348 Vsystem_name
= build_string (uts
.nodename
);
2349 #else /* HAVE_GETHOSTNAME */
2350 unsigned int hostname_size
= 256;
2351 char *hostname
= (char *) alloca (hostname_size
);
2353 /* Try to get the host name; if the buffer is too short, try
2354 again. Apparently, the only indication gethostname gives of
2355 whether the buffer was large enough is the presence or absence
2356 of a '\0' in the string. Eech. */
2359 gethostname (hostname
, hostname_size
- 1);
2360 hostname
[hostname_size
- 1] = '\0';
2362 /* Was the buffer large enough for the '\0'? */
2363 if (strlen (hostname
) < hostname_size
- 1)
2366 hostname_size
<<= 1;
2367 hostname
= (char *) alloca (hostname_size
);
2370 /* Turn the hostname into the official, fully-qualified hostname.
2371 Don't do this if we're going to dump; this can confuse system
2372 libraries on some machines and make the dumped emacs core dump. */
2375 #endif /* not CANNOT_DUMP */
2376 if (! index (hostname
, '.'))
2379 #ifdef HAVE_GETADDRINFO
2380 struct addrinfo
*res
;
2381 struct addrinfo hints
;
2384 memset (&hints
, 0, sizeof(hints
));
2385 hints
.ai_socktype
= SOCK_STREAM
;
2386 hints
.ai_flags
= AI_CANONNAME
;
2388 for (count
= 0;; count
++)
2390 if ((ret
= getaddrinfo (hostname
, NULL
, &hints
, &res
)) == 0
2391 || ret
!= EAI_AGAIN
)
2396 Fsleep_for (make_number (1), Qnil
);
2401 struct addrinfo
*it
= res
;
2404 char *fqdn
= it
->ai_canonname
;
2405 if (fqdn
&& index (fqdn
, '.')
2406 && strcmp (fqdn
, "localhost.localdomain") != 0)
2412 hostname
= alloca (strlen (it
->ai_canonname
) + 1);
2413 strcpy (hostname
, it
->ai_canonname
);
2417 #else /* !HAVE_GETADDRINFO */
2419 for (count
= 0;; count
++)
2425 hp
= gethostbyname (hostname
);
2427 if (! (hp
== 0 && h_errno
== TRY_AGAIN
))
2434 Fsleep_for (make_number (1), Qnil
);
2439 char *fqdn
= (char *) hp
->h_name
;
2444 if (!index (fqdn
, '.'))
2446 /* We still don't have a fully qualified domain name.
2447 Try to find one in the list of alternate names */
2448 char **alias
= hp
->h_aliases
;
2450 && (!index (*alias
, '.')
2451 || !strcmp (*alias
, "localhost.localdomain")))
2458 /* Convert the host name to lower case. */
2459 /* Using ctype.h here would introduce a possible locale
2460 dependence that is probably wrong for hostnames. */
2464 if (*p
>= 'A' && *p
<= 'Z')
2470 #endif /* !HAVE_GETADDRINFO */
2472 #endif /* HAVE_SOCKETS */
2473 /* We used to try using getdomainname here,
2474 but NIIBE Yutaka <gniibe@etl.go.jp> says that
2475 getdomainname gets the NIS/YP domain which often is not the same
2476 as in Internet domain name. */
2477 #if 0 /* Turned off because sysinfo is not really likely to return the
2478 correct Internet domain. */
2479 #if (HAVE_SYSINFO && defined (SI_SRPC_DOMAIN))
2480 if (! index (hostname
, '.'))
2482 /* The hostname is not fully qualified. Append the domain name. */
2484 int hostlen
= strlen (hostname
);
2485 int domain_size
= 256;
2489 char *domain
= (char *) alloca (domain_size
+ 1);
2490 char *fqdn
= (char *) alloca (hostlen
+ 1 + domain_size
+ 1);
2491 int sys_domain_size
= sysinfo (SI_SRPC_DOMAIN
, domain
, domain_size
);
2492 if (sys_domain_size
<= 0)
2494 if (domain_size
< sys_domain_size
)
2496 domain_size
= sys_domain_size
;
2499 strcpy (fqdn
, hostname
);
2500 if (domain
[0] == '.')
2501 strcpy (fqdn
+ hostlen
, domain
);
2502 else if (domain
[0] != 0)
2504 fqdn
[hostlen
] = '.';
2505 strcpy (fqdn
+ hostlen
+ 1, domain
);
2511 #endif /* HAVE_SYSINFO && defined (SI_SRPC_DOMAIN) */
2513 Vsystem_name
= build_string (hostname
);
2514 #endif /* HAVE_GETHOSTNAME */
2519 for (p
= SDATA (Vsystem_name
); *p
; p
++)
2520 if (*p
== ' ' || *p
== '\t')
2527 #if !defined (HAVE_SELECT)
2529 #include "sysselect.h"
2532 #if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
2533 /* Cause explanatory error message at compile time,
2534 since the select emulation is not good enough for X. */
2535 int *x
= &x_windows_lose_if_no_select_system_call
;
2538 /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2539 * Only checks read descriptors.
2541 /* How long to wait between checking fds in select */
2542 #define SELECT_PAUSE 1
2545 /* For longjmp'ing back to read_input_waiting. */
2547 jmp_buf read_alarm_throw
;
2549 /* Nonzero if the alarm signal should throw back to read_input_waiting.
2550 The read_socket_hook function sets this to 1 while it is waiting. */
2552 int read_alarm_should_throw
;
2560 #else /* not BSD4_1 */
2561 signal (SIGALRM
, SIG_IGN
);
2562 #endif /* not BSD4_1 */
2563 SIGNAL_THREAD_CHECK (SIGALRM
);
2564 if (read_alarm_should_throw
)
2565 longjmp (read_alarm_throw
, 1);
2569 /* Only rfds are checked. */
2571 sys_select (nfds
, rfds
, wfds
, efds
, timeout
)
2573 SELECT_TYPE
*rfds
, *wfds
, *efds
;
2574 EMACS_TIME
*timeout
;
2576 /* XXX This needs to be updated for multi-tty support. Is there
2577 anybody who needs to emulate select these days? */
2582 extern int proc_buffered_char
[];
2583 #ifndef subprocesses
2584 int process_tick
= 0, update_tick
= 0;
2586 extern int process_tick
, update_tick
;
2590 #if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
2591 /* If we're using X, then the native select will work; we only need the
2592 emulation for non-X usage. */
2593 if (!NILP (Vinitial_window_system
))
2594 return select (nfds
, rfds
, wfds
, efds
, timeout
);
2596 timeoutval
= timeout
? EMACS_SECS (*timeout
) : 100000;
2597 local_timeout
= &timeoutval
;
2609 /* If we are looking only for the terminal, with no timeout,
2610 just read it and wait -- that's more efficient. */
2611 if (*local_timeout
== 100000 && process_tick
== update_tick
2612 && FD_ISSET (0, &orfds
))
2615 for (fd
= 1; fd
< nfds
; ++fd
)
2616 if (FD_ISSET (fd
, &orfds
))
2618 if (! detect_input_pending ())
2619 read_input_waiting ();
2625 /* Once a second, till the timer expires, check all the flagged read
2626 * descriptors to see if any input is available. If there is some then
2627 * set the corresponding bit in the return copy of rfds.
2631 register int to_check
, fd
;
2635 for (to_check
= nfds
, fd
= 0; --to_check
>= 0; fd
++)
2637 if (FD_ISSET (fd
, &orfds
))
2639 int avail
= 0, status
= 0;
2642 avail
= detect_input_pending (); /* Special keyboard handler */
2646 status
= ioctl (fd
, FIONREAD
, &avail
);
2647 #else /* no FIONREAD */
2648 /* Hoping it will return -1 if nothing available
2649 or 0 if all 0 chars requested are read. */
2650 if (proc_buffered_char
[fd
] >= 0)
2654 avail
= read (fd
, &buf
, 1);
2656 proc_buffered_char
[fd
] = buf
;
2658 #endif /* no FIONREAD */
2660 if (status
>= 0 && avail
> 0)
2668 if (*local_timeout
== 0 || ravail
!= 0 || process_tick
!= update_tick
)
2671 turn_on_atimers (0);
2672 signal (SIGALRM
, select_alarm
);
2674 alarm (SELECT_PAUSE
);
2676 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2677 while (select_alarmed
== 0 && *local_timeout
!= 0
2678 && process_tick
== update_tick
)
2680 /* If we are interested in terminal input,
2681 wait by reading the terminal.
2682 That makes instant wakeup for terminal input at least. */
2683 if (FD_ISSET (0, &orfds
))
2685 read_input_waiting ();
2686 if (detect_input_pending ())
2692 (*local_timeout
) -= SELECT_PAUSE
;
2694 /* Reset the old alarm if there was one. */
2695 turn_on_atimers (1);
2697 if (*local_timeout
== 0) /* Stop on timer being cleared */
2702 #endif /* not WINDOWSNT */
2704 /* Read keyboard input into the standard buffer,
2705 waiting for at least one character. */
2708 read_input_waiting ()
2710 /* XXX This needs to be updated for multi-tty support. Is there
2711 anybody who needs to emulate select these days? */
2713 extern int quit_char
;
2715 if (read_socket_hook
)
2717 struct input_event hold_quit
;
2719 EVENT_INIT (hold_quit
);
2720 hold_quit
.kind
= NO_EVENT
;
2722 read_alarm_should_throw
= 0;
2723 if (! setjmp (read_alarm_throw
))
2724 nread
= (*read_socket_hook
) (0, 1, &hold_quit
);
2728 if (hold_quit
.kind
!= NO_EVENT
)
2729 kbd_buffer_store_event (&hold_quit
);
2733 struct input_event e
;
2735 nread
= read (fileno (stdin
), buf
, 1);
2738 /* Scan the chars for C-g and store them in kbd_buffer. */
2739 e
.kind
= ASCII_KEYSTROKE_EVENT
;
2740 e
.frame_or_window
= selected_frame
;
2742 for (i
= 0; i
< nread
; i
++)
2744 /* Convert chars > 0177 to meta events if desired.
2745 We do this under the same conditions that read_avail_input does. */
2746 if (read_socket_hook
== 0)
2748 /* If the user says she has a meta key, then believe her. */
2749 if (meta_key
== 1 && (buf
[i
] & 0x80))
2750 e
.modifiers
= meta_modifier
;
2755 XSETINT (e
.code
, buf
[i
]);
2756 kbd_buffer_store_event (&e
);
2757 /* Don't look at input that follows a C-g too closely.
2758 This reduces lossage due to autorepeat on C-g. */
2759 if (buf
[i
] == quit_char
)
2765 #if !defined (HAVE_SELECT)
2766 #define select sys_select
2769 #endif /* not HAVE_SELECT */
2770 #endif /* not VMS */
2771 #endif /* not MSDOS */
2780 lmode
= LINTRUP
| lmode
;
2781 ioctl (fd
, TIOCLSET
, &lmode
);
2790 lmode
= ~LINTRUP
& lmode
;
2791 ioctl (fd
, TIOCLSET
, &lmode
);
2801 interrupts_deferred
= 0;
2811 interrupts_deferred
= 1;
2814 /* still inside #ifdef BSD4_1 */
2817 int sigheld
; /* Mask of held signals */
2823 sigheld
|= sigbit (signum
);
2831 sigheld
|= sigbit (signum
);
2838 sigheld
&= ~sigbit (signum
);
2843 sigfree () /* Free all held signals */
2846 for (i
= 0; i
< NSIG
; i
++)
2847 if (sigheld
& sigbit (i
))
2855 return 1 << (i
- 1);
2857 #endif /* subprocesses */
2860 /* POSIX signals support - DJB */
2861 /* Anyone with POSIX signals should have ANSI C declarations */
2863 #ifdef POSIX_SIGNALS
2865 sigset_t empty_mask
, full_mask
;
2868 sys_signal (int signal_number
, signal_handler_t action
)
2870 struct sigaction new_action
, old_action
;
2871 sigemptyset (&new_action
.sa_mask
);
2872 new_action
.sa_handler
= action
;
2873 #if defined (SA_RESTART) && ! defined (BROKEN_SA_RESTART) && !defined(SYNC_INPUT)
2874 /* Emacs mostly works better with restartable system services. If this
2875 flag exists, we probably want to turn it on here.
2876 However, on some systems this resets the timeout of `select'
2877 which means that `select' never finishes if it keeps getting signals.
2878 BROKEN_SA_RESTART is defined on those systems. */
2879 /* It's not clear why the comment above says "mostly works better". --Stef
2880 When SYNC_INPUT is set, we don't want SA_RESTART because we need to poll
2881 for pending input so we need long-running syscalls to be interrupted
2882 after a signal that sets the interrupt_input_pending flag. */
2883 new_action
.sa_flags
= SA_RESTART
;
2885 new_action
.sa_flags
= 0;
2887 sigaction (signal_number
, &new_action
, &old_action
);
2888 return (old_action
.sa_handler
);
2892 /* If we're compiling with GCC, we don't need this function, since it
2893 can be written as a macro. */
2895 sys_sigmask (int sig
)
2898 sigemptyset (&mask
);
2899 sigaddset (&mask
, sig
);
2904 /* I'd like to have these guys return pointers to the mask storage in here,
2905 but there'd be trouble if the code was saving multiple masks. I'll be
2906 safe and pass the structure. It normally won't be more than 2 bytes
2910 sys_sigblock (sigset_t new_mask
)
2913 sigprocmask (SIG_BLOCK
, &new_mask
, &old_mask
);
2918 sys_sigunblock (sigset_t new_mask
)
2921 sigprocmask (SIG_UNBLOCK
, &new_mask
, &old_mask
);
2926 sys_sigsetmask (sigset_t new_mask
)
2929 sigprocmask (SIG_SETMASK
, &new_mask
, &old_mask
);
2933 #endif /* POSIX_SIGNALS */
2935 #if !defined HAVE_STRSIGNAL && !HAVE_DECL_SYS_SIGLIST
2936 static char *my_sys_siglist
[NSIG
];
2940 # define sys_siglist my_sys_siglist
2946 #ifdef POSIX_SIGNALS
2947 sigemptyset (&empty_mask
);
2948 sigfillset (&full_mask
);
2951 #if !defined HAVE_STRSIGNAL && !HAVE_DECL_SYS_SIGLIST
2955 sys_siglist
[SIGABRT
] = "Aborted";
2958 sys_siglist
[SIGAIO
] = "LAN I/O interrupt";
2961 sys_siglist
[SIGALRM
] = "Alarm clock";
2964 sys_siglist
[SIGBUS
] = "Bus error";
2967 sys_siglist
[SIGCLD
] = "Child status changed";
2970 sys_siglist
[SIGCHLD
] = "Child status changed";
2973 sys_siglist
[SIGCONT
] = "Continued";
2976 sys_siglist
[SIGDANGER
] = "Swap space dangerously low";
2979 sys_siglist
[SIGDGNOTIFY
] = "Notification message in queue";
2982 sys_siglist
[SIGEMT
] = "Emulation trap";
2985 sys_siglist
[SIGFPE
] = "Arithmetic exception";
2988 sys_siglist
[SIGFREEZE
] = "SIGFREEZE";
2991 sys_siglist
[SIGGRANT
] = "Monitor mode granted";
2994 sys_siglist
[SIGHUP
] = "Hangup";
2997 sys_siglist
[SIGILL
] = "Illegal instruction";
3000 sys_siglist
[SIGINT
] = "Interrupt";
3003 sys_siglist
[SIGIO
] = "I/O possible";
3006 sys_siglist
[SIGIOINT
] = "I/O intervention required";
3009 sys_siglist
[SIGIOT
] = "IOT trap";
3012 sys_siglist
[SIGKILL
] = "Killed";
3015 sys_siglist
[SIGLOST
] = "Resource lost";
3018 sys_siglist
[SIGLWP
] = "SIGLWP";
3021 sys_siglist
[SIGMSG
] = "Monitor mode data available";
3024 sys_siglist
[SIGWIND
] = "SIGPHONE";
3027 sys_siglist
[SIGPIPE
] = "Broken pipe";
3030 sys_siglist
[SIGPOLL
] = "Pollable event occurred";
3033 sys_siglist
[SIGPROF
] = "Profiling timer expired";
3036 sys_siglist
[SIGPTY
] = "PTY I/O interrupt";
3039 sys_siglist
[SIGPWR
] = "Power-fail restart";
3042 sys_siglist
[SIGQUIT
] = "Quit";
3045 sys_siglist
[SIGRETRACT
] = "Need to relinguish monitor mode";
3048 sys_siglist
[SIGSAK
] = "Secure attention";
3051 sys_siglist
[SIGSEGV
] = "Segmentation violation";
3054 sys_siglist
[SIGSOUND
] = "Sound completed";
3057 sys_siglist
[SIGSTOP
] = "Stopped (signal)";
3060 sys_siglist
[SIGSTP
] = "Stopped (user)";
3063 sys_siglist
[SIGSYS
] = "Bad argument to system call";
3066 sys_siglist
[SIGTERM
] = "Terminated";
3069 sys_siglist
[SIGTHAW
] = "SIGTHAW";
3072 sys_siglist
[SIGTRAP
] = "Trace/breakpoint trap";
3075 sys_siglist
[SIGTSTP
] = "Stopped (user)";
3078 sys_siglist
[SIGTTIN
] = "Stopped (tty input)";
3081 sys_siglist
[SIGTTOU
] = "Stopped (tty output)";
3084 sys_siglist
[SIGURG
] = "Urgent I/O condition";
3087 sys_siglist
[SIGUSR1
] = "User defined signal 1";
3090 sys_siglist
[SIGUSR2
] = "User defined signal 2";
3093 sys_siglist
[SIGVTALRM
] = "Virtual timer expired";
3096 sys_siglist
[SIGWAITING
] = "Process's LWPs are blocked";
3099 sys_siglist
[SIGWINCH
] = "Window size changed";
3102 sys_siglist
[SIGWIND
] = "SIGWIND";
3105 sys_siglist
[SIGXCPU
] = "CPU time limit exceeded";
3108 sys_siglist
[SIGXFSZ
] = "File size limit exceeded";
3111 #endif /* !defined HAVE_STRSIGNAL && !defined HAVE_DECL_SYS_SIGLIST */
3120 /* Figure out how many bits the system's random number generator uses.
3121 `random' and `lrand48' are assumed to return 31 usable bits.
3122 BSD `rand' returns a 31 bit value but the low order bits are unusable;
3123 so we'll shift it and treat it like the 15-bit USG `rand'. */
3127 # define RAND_BITS 31
3128 # else /* !HAVE_RANDOM */
3129 # ifdef HAVE_LRAND48
3130 # define RAND_BITS 31
3131 # define random lrand48
3132 # else /* !HAVE_LRAND48 */
3133 # define RAND_BITS 15
3134 # if RAND_MAX == 32767
3135 # define random rand
3136 # else /* RAND_MAX != 32767 */
3137 # if RAND_MAX == 2147483647
3138 # define random() (rand () >> 16)
3139 # else /* RAND_MAX != 2147483647 */
3141 # define random rand
3143 # define random() (rand () >> 16)
3145 # endif /* RAND_MAX != 2147483647 */
3146 # endif /* RAND_MAX != 32767 */
3147 # endif /* !HAVE_LRAND48 */
3148 # endif /* !HAVE_RANDOM */
3149 #endif /* !RAND_BITS */
3156 srandom ((unsigned int)arg
);
3158 # ifdef HAVE_LRAND48
3161 srand ((unsigned int)arg
);
3167 * Build a full Emacs-sized word out of whatever we've got.
3168 * This suffices even for a 64-bit architecture with a 15-bit rand.
3173 long val
= random ();
3174 #if VALBITS > RAND_BITS
3175 val
= (val
<< RAND_BITS
) ^ random ();
3176 #if VALBITS > 2*RAND_BITS
3177 val
= (val
<< RAND_BITS
) ^ random ();
3178 #if VALBITS > 3*RAND_BITS
3179 val
= (val
<< RAND_BITS
) ^ random ();
3180 #if VALBITS > 4*RAND_BITS
3181 val
= (val
<< RAND_BITS
) ^ random ();
3182 #endif /* need at least 5 */
3183 #endif /* need at least 4 */
3184 #endif /* need at least 3 */
3185 #endif /* need at least 2 */
3186 return val
& ((1L << VALBITS
) - 1);
3192 /* If any place else asks for the TERM variable,
3193 allow it to be overridden with the EMACS_TERM variable
3194 before attempting to translate the logical name TERM. As a last
3195 resort, ask for VAX C's special idea of the TERM variable. */
3202 static char buf
[256];
3203 static struct dsc$descriptor_s equiv
3204 = {sizeof (buf
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, buf
};
3205 static struct dsc$descriptor_s d_name
3206 = {0, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, 0};
3209 if (!strcmp (name
, "TERM"))
3211 val
= (char *) getenv ("EMACS_TERM");
3216 d_name
.dsc$w_length
= strlen (name
);
3217 d_name
.dsc$a_pointer
= name
;
3218 if (LIB$
SYS_TRNLOG (&d_name
, &eqlen
, &equiv
) == 1)
3220 char *str
= (char *) xmalloc (eqlen
+ 1);
3221 bcopy (buf
, str
, eqlen
);
3223 /* This is a storage leak, but a pain to fix. With luck,
3224 no one will ever notice. */
3227 return (char *) getenv (name
);
3232 /* Since VMS doesn't believe in core dumps, the only way to debug this beast is
3233 to force a call on the debugger from within the image. */
3237 reset_all_sys_modes ();
3238 LIB$
SIGNAL (SS$_DEBUG
);
3244 #ifdef LINK_CRTL_SHARE
3245 #ifdef SHARABLE_LIB_BUG
3246 /* Variables declared noshare and initialized in sharable libraries
3247 cannot be shared. The VMS linker incorrectly forces you to use a private
3248 version which is uninitialized... If not for this "feature", we
3249 could use the C library definition of sys_nerr and sys_errlist. */
3251 char *sys_errlist
[] =
3255 "no such file or directory",
3257 "interrupted system call",
3259 "no such device or address",
3260 "argument list too long",
3261 "exec format error",
3264 "no more processes",
3265 "not enough memory",
3266 "permission denied",
3268 "block device required",
3269 "mount devices busy",
3271 "cross-device link",
3276 "file table overflow",
3277 "too many open files",
3281 "no space left on device",
3283 "read-only file system",
3289 "vax/vms specific error code nontranslatable error"
3291 #endif /* SHARABLE_LIB_BUG */
3292 #endif /* LINK_CRTL_SHARE */
3295 #ifndef HAVE_STRERROR
3301 extern char *sys_errlist
[];
3302 extern int sys_nerr
;
3304 if (errnum
>= 0 && errnum
< sys_nerr
)
3305 return sys_errlist
[errnum
];
3306 return (char *) "Unknown error";
3308 #endif /* not WINDOWSNT */
3309 #endif /* ! HAVE_STRERROR */
3312 emacs_open (path
, oflag
, mode
)
3316 register int rtnval
;
3319 if (oflag
& O_CREAT
)
3320 return creat (path
, mode
);
3323 while ((rtnval
= open (path
, oflag
, mode
)) == -1
3324 && (errno
== EINTR
))
3334 register int rtnval
;
3336 #if defined (MAC_OSX) && defined (HAVE_CARBON)
3338 extern int mac_try_close_socket
P_ ((int));
3340 if (mac_try_close_socket (fd
))
3345 while ((rtnval
= close (fd
)) == -1
3346 && (errno
== EINTR
))
3349 /* If close is interrupted SunOS 4.1 may or may not have closed the
3350 file descriptor. If it did the second close will fail with
3351 errno = EBADF. That means we have succeeded. */
3352 if (rtnval
== -1 && did_retry
&& errno
== EBADF
)
3359 emacs_read (fildes
, buf
, nbyte
)
3364 register int rtnval
;
3366 while ((rtnval
= read (fildes
, buf
, nbyte
)) == -1
3367 && (errno
== EINTR
))
3373 emacs_write (fildes
, buf
, nbyte
)
3378 register int rtnval
, bytes_written
;
3384 rtnval
= write (fildes
, buf
, nbyte
);
3391 /* I originally used `QUIT' but that might causes files to
3392 be truncated if you hit C-g in the middle of it. --Stef */
3393 if (interrupt_input_pending
)
3394 handle_async_input ();
3395 if (pending_atimers
)
3396 do_pending_atimers ();
3401 return (bytes_written
? bytes_written
: -1);
3406 bytes_written
+= rtnval
;
3408 return (bytes_written
);
3413 * All of the following are for USG.
3415 * On USG systems the system calls are INTERRUPTIBLE by signals
3416 * that the user program has elected to catch. Thus the system call
3417 * must be retried in these cases. To handle this without massive
3418 * changes in the source code, we remap the standard system call names
3419 * to names for our own functions in sysdep.c that do the system call
3420 * with retries. Actually, for portability reasons, it is good
3421 * programming practice, as this example shows, to limit all actual
3422 * system calls to a single occurrence in the source. Sure, this
3423 * adds an extra level of function call overhead but it is almost
3424 * always negligible. Fred Fish, Unisoft Systems Inc.
3428 * Warning, this function may not duplicate 4.2 action properly
3429 * under error conditions.
3433 /* In 4.1, param.h fails to define this. */
3434 #define MAXPATHLEN 1024
3443 char *npath
, *spath
;
3444 extern char *getcwd ();
3446 BLOCK_INPUT
; /* getcwd uses malloc */
3447 spath
= npath
= getcwd ((char *) 0, MAXPATHLEN
);
3453 /* On Altos 3068, getcwd can return @hostname/dir, so discard
3454 up to first slash. Should be harmless on other systems. */
3455 while (*npath
&& *npath
!= '/')
3457 strcpy (pathname
, npath
);
3458 free (spath
); /* getcwd uses malloc */
3463 #endif /* HAVE_GETWD */
3466 * Emulate rename using unlink/link. Note that this is
3467 * only partially correct. Also, doesn't enforce restriction
3468 * that files be of same type (regular->regular, dir->dir, etc).
3477 if (access (from
, 0) == 0)
3480 if (link (from
, to
) == 0)
3481 if (unlink (from
) == 0)
3493 /* HPUX curses library references perror, but as far as we know
3494 it won't be called. Anyway this definition will do for now. */
3500 #endif /* not HAVE_PERROR */
3506 * Emulate BSD dup2. First close newd if it already exists.
3507 * Then, attempt to dup oldd. If not successful, call dup2 recursively
3508 * until we are, then close the unsuccessful ones.
3515 register int fd
, ret
;
3520 return fcntl (oldd
, F_DUPFD
, newd
);
3527 ret
= dup2 (old
,new);
3533 #endif /* not HAVE_DUP2 */
3536 * Gettimeofday. Simulate as much as possible. Only accurate
3537 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3538 * Only needed when subprocesses are defined.
3543 #ifndef HAVE_GETTIMEOFDAY
3548 gettimeofday (tp
, tzp
)
3550 struct timezone
*tzp
;
3552 extern long time ();
3554 tp
->tv_sec
= time ((long *)0);
3557 tzp
->tz_minuteswest
= -1;
3564 #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
3567 * This function will go away as soon as all the stubs fixed. (fnf)
3574 printf ("%s not yet implemented\r\n", badfunc
);
3575 reset_all_sys_modes ();
3581 /* Directory routines for systems that don't have them. */
3583 #ifdef SYSV_SYSTEM_DIR
3587 #if defined (BROKEN_CLOSEDIR) || !defined (HAVE_CLOSEDIR)
3591 register DIR *dirp
; /* stream from opendir */
3595 rtnval
= emacs_close (dirp
->dd_fd
);
3597 /* Some systems (like Solaris) allocate the buffer and the DIR all
3598 in one block. Why in the world are we freeing this ourselves
3600 #if ! (defined (sun) && defined (USG5_4))
3601 xfree ((char *) dirp
->dd_buf
); /* directory block defined in <dirent.h> */
3603 xfree ((char *) dirp
);
3607 #endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
3608 #endif /* SYSV_SYSTEM_DIR */
3610 #ifdef NONSYSTEM_DIR_LIBRARY
3614 char *filename
; /* name of directory */
3616 register DIR *dirp
; /* -> malloc'ed storage */
3617 register int fd
; /* file descriptor for read */
3618 struct stat sbuf
; /* result of fstat */
3620 fd
= emacs_open (filename
, O_RDONLY
, 0);
3625 if (fstat (fd
, &sbuf
) < 0
3626 || (sbuf
.st_mode
& S_IFMT
) != S_IFDIR
3627 || (dirp
= (DIR *) xmalloc (sizeof (DIR))) == 0)
3631 return 0; /* bad luck today */
3636 dirp
->dd_loc
= dirp
->dd_size
= 0; /* refill needed */
3643 register DIR *dirp
; /* stream from opendir */
3645 emacs_close (dirp
->dd_fd
);
3646 xfree ((char *) dirp
);
3654 ino_t od_ino
; /* inode */
3655 char od_name
[DIRSIZ
]; /* filename */
3657 #endif /* not VMS */
3659 struct direct dir_static
; /* simulated directory contents */
3664 register DIR *dirp
; /* stream from opendir */
3667 register struct olddir
*dp
; /* -> directory data */
3669 register struct dir$_name
*dp
; /* -> directory data */
3670 register struct dir$_version
*dv
; /* -> version data */
3675 if (dirp
->dd_loc
>= dirp
->dd_size
)
3676 dirp
->dd_loc
= dirp
->dd_size
= 0;
3678 if (dirp
->dd_size
== 0 /* refill buffer */
3679 && (dirp
->dd_size
= emacs_read (dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
)) <= 0)
3683 dp
= (struct olddir
*) &dirp
->dd_buf
[dirp
->dd_loc
];
3684 dirp
->dd_loc
+= sizeof (struct olddir
);
3686 if (dp
->od_ino
!= 0) /* not deleted entry */
3688 dir_static
.d_ino
= dp
->od_ino
;
3689 strncpy (dir_static
.d_name
, dp
->od_name
, DIRSIZ
);
3690 dir_static
.d_name
[DIRSIZ
] = '\0';
3691 dir_static
.d_namlen
= strlen (dir_static
.d_name
);
3692 dir_static
.d_reclen
= sizeof (struct direct
)
3694 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3695 return &dir_static
; /* -> simulated structure */
3698 dp
= (struct dir$_name
*) dirp
->dd_buf
;
3699 if (dirp
->dd_loc
== 0)
3700 dirp
->dd_loc
= (dp
->dir$b_namecount
&1) ? dp
->dir$b_namecount
+ 1
3701 : dp
->dir$b_namecount
;
3702 dv
= (struct dir$_version
*)&dp
->dir$t_name
[dirp
->dd_loc
];
3703 dir_static
.d_ino
= dv
->dir$w_fid_num
;
3704 dir_static
.d_namlen
= dp
->dir$b_namecount
;
3705 dir_static
.d_reclen
= sizeof (struct direct
)
3707 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3708 strncpy (dir_static
.d_name
, dp
->dir$t_name
, dp
->dir$b_namecount
);
3709 dir_static
.d_name
[dir_static
.d_namlen
] = '\0';
3710 dirp
->dd_loc
= dirp
->dd_size
; /* only one record at a time */
3717 /* readdirver is just like readdir except it returns all versions of a file
3718 as separate entries. */
3723 register DIR *dirp
; /* stream from opendir */
3725 register struct dir$_name
*dp
; /* -> directory data */
3726 register struct dir$_version
*dv
; /* -> version data */
3728 if (dirp
->dd_loc
>= dirp
->dd_size
- sizeof (struct dir$_name
))
3729 dirp
->dd_loc
= dirp
->dd_size
= 0;
3731 if (dirp
->dd_size
== 0 /* refill buffer */
3732 && (dirp
->dd_size
= sys_read (dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
)) <= 0)
3735 dp
= (struct dir$_name
*) dirp
->dd_buf
;
3736 if (dirp
->dd_loc
== 0)
3737 dirp
->dd_loc
= (dp
->dir$b_namecount
& 1) ? dp
->dir$b_namecount
+ 1
3738 : dp
->dir$b_namecount
;
3739 dv
= (struct dir$_version
*) &dp
->dir$t_name
[dirp
->dd_loc
];
3740 strncpy (dir_static
.d_name
, dp
->dir$t_name
, dp
->dir$b_namecount
);
3741 sprintf (&dir_static
.d_name
[dp
->dir$b_namecount
], ";%d", dv
->dir$w_version
);
3742 dir_static
.d_namlen
= strlen (dir_static
.d_name
);
3743 dir_static
.d_ino
= dv
->dir$w_fid_num
;
3744 dir_static
.d_reclen
= sizeof (struct direct
) - MAXNAMLEN
+ 3
3745 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3746 dirp
->dd_loc
= ((char *) (++dv
) - dp
->dir$t_name
);
3752 #endif /* NONSYSTEM_DIR_LIBRARY */
3756 set_file_times (filename
, atime
, mtime
)
3757 const char *filename
;
3758 EMACS_TIME atime
, mtime
;
3761 struct timeval tv
[2];
3764 return utimes (filename
, tv
);
3765 #else /* not HAVE_UTIMES */
3767 utb
.actime
= EMACS_SECS (atime
);
3768 utb
.modtime
= EMACS_SECS (mtime
);
3769 return utime (filename
, &utb
);
3770 #endif /* not HAVE_UTIMES */
3773 /* mkdir and rmdir functions, for systems which don't have them. */
3777 * Written by Robert Rother, Mariah Corporation, August 1985.
3779 * If you want it, it's yours. All I ask in return is that if you
3780 * figure out how to do this in a Bourne Shell script you send me
3782 * sdcsvax!rmr or rmr@uscd
3784 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
3785 * subroutine. 11Mar86; hoptoad!gnu
3787 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3788 * subroutine didn't return EEXIST. It does now.
3794 #ifdef MKDIR_PROTOTYPE
3798 mkdir (dpath
, dmode
)
3803 int cpid
, status
, fd
;
3804 struct stat statbuf
;
3806 if (stat (dpath
, &statbuf
) == 0)
3808 errno
= EEXIST
; /* Stat worked, so it already exists */
3812 /* If stat fails for a reason other than non-existence, return error */
3813 if (errno
!= ENOENT
)
3816 synch_process_alive
= 1;
3817 switch (cpid
= fork ())
3820 case -1: /* Error in fork */
3821 return (-1); /* Errno is set already */
3823 case 0: /* Child process */
3825 * Cheap hack to set mode of new directory. Since this
3826 * child process is going away anyway, we zap its umask.
3827 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3828 * directory. Does anybody care?
3830 status
= umask (0); /* Get current umask */
3831 status
= umask (status
| (0777 & ~dmode
)); /* Set for mkdir */
3832 fd
= emacs_open ("/dev/null", O_RDWR
, 0);
3839 execl ("/bin/mkdir", "mkdir", dpath
, (char *) 0);
3840 _exit (-1); /* Can't exec /bin/mkdir */
3842 default: /* Parent process */
3843 wait_for_termination (cpid
);
3846 if (synch_process_death
!= 0 || synch_process_retcode
!= 0
3847 || synch_process_termsig
!= 0)
3849 errno
= EIO
; /* We don't know why, but */
3850 return -1; /* /bin/mkdir failed */
3855 #endif /* not HAVE_MKDIR */
3862 int cpid
, status
, fd
;
3863 struct stat statbuf
;
3865 if (stat (dpath
, &statbuf
) != 0)
3867 /* Stat just set errno. We don't have to */
3871 synch_process_alive
= 1;
3872 switch (cpid
= fork ())
3875 case -1: /* Error in fork */
3876 return (-1); /* Errno is set already */
3878 case 0: /* Child process */
3879 fd
= emacs_open ("/dev/null", O_RDWR
, 0);
3886 execl ("/bin/rmdir", "rmdir", dpath
, (char *) 0);
3887 _exit (-1); /* Can't exec /bin/rmdir */
3889 default: /* Parent process */
3890 wait_for_termination (cpid
);
3893 if (synch_process_death
!= 0 || synch_process_retcode
!= 0
3894 || synch_process_termsig
!= 0)
3896 errno
= EIO
; /* We don't know why, but */
3897 return -1; /* /bin/rmdir failed */
3902 #endif /* !HAVE_RMDIR */
3906 /* Functions for VMS */
3912 /* Return as a string the VMS error string pertaining to STATUS.
3913 Reuses the same static buffer each time it is called. */
3917 int status
; /* VMS status code */
3921 static char buf
[257];
3923 bufadr
[0] = sizeof buf
- 1;
3924 bufadr
[1] = (int) buf
;
3925 if (! (SYS$
GETMSG (status
, &len
, bufadr
, 0x1, 0) & 1))
3926 return "untranslatable VMS error status";
3934 /* The following is necessary because 'access' emulation by VMS C (2.0) does
3935 * not work correctly. (It also doesn't work well in version 2.3.)
3940 #define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3941 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3945 unsigned short s_buflen
;
3946 unsigned short s_code
;
3948 unsigned short *s_retlenadr
;
3952 #define buflen s.s_buflen
3953 #define code s.s_code
3954 #define bufadr s.s_bufadr
3955 #define retlenadr s.s_retlenadr
3957 #define R_OK 4 /* test for read permission */
3958 #define W_OK 2 /* test for write permission */
3959 #define X_OK 1 /* test for execute (search) permission */
3960 #define F_OK 0 /* test for presence of file */
3963 sys_access (path
, mode
)
3967 static char *user
= NULL
;
3970 /* translate possible directory spec into .DIR file name, so brain-dead
3971 * access can treat the directory like a file. */
3972 if (directory_file_name (path
, dir_fn
))
3976 return access (path
, mode
);
3977 if (user
== NULL
&& (user
= (char *) getenv ("USER")) == NULL
)
3983 unsigned short int dummy
;
3985 static int constant
= ACL$C_FILE
;
3986 DESCRIPTOR (path_desc
, path
);
3987 DESCRIPTOR (user_desc
, user
);
3991 if ((mode
& X_OK
) && ((stat
= access (path
, mode
)) < 0 || mode
== X_OK
))
3994 acces
|= CHP$M_READ
;
3996 acces
|= CHP$M_WRITE
;
3997 itemlst
[0].buflen
= sizeof (int);
3998 itemlst
[0].code
= CHP$_FLAGS
;
3999 itemlst
[0].bufadr
= (char *) &flags
;
4000 itemlst
[0].retlenadr
= &dummy
;
4001 itemlst
[1].buflen
= sizeof (int);
4002 itemlst
[1].code
= CHP$_ACCESS
;
4003 itemlst
[1].bufadr
= (char *) &acces
;
4004 itemlst
[1].retlenadr
= &dummy
;
4005 itemlst
[2].end
= CHP$_END
;
4006 stat
= SYS$
CHECK_ACCESS (&constant
, &path_desc
, &user_desc
, itemlst
);
4007 return stat
== SS$_NORMAL
? 0 : -1;
4011 #else /* not VMS4_4 */
4014 #define ACE$M_WRITE 2
4015 #define ACE$C_KEYID 1
4017 static unsigned short memid
, grpid
;
4018 static unsigned int uic
;
4020 /* Called from init_sys_modes, so it happens not very often
4021 but at least each time Emacs is loaded. */
4023 sys_access_reinit ()
4029 sys_access (filename
, type
)
4035 int status
, size
, i
, typecode
, acl_controlled
;
4036 unsigned int *aclptr
, *aclend
, aclbuf
[60];
4037 union prvdef prvmask
;
4039 /* Get UIC and GRP values for protection checking. */
4042 status
= LIB$
GETJPI (&JPI$_UIC
, 0, 0, &uic
, 0, 0);
4045 memid
= uic
& 0xFFFF;
4049 if (type
!= 2) /* not checking write access */
4050 return access (filename
, type
);
4052 /* Check write protection. */
4054 #define CHECKPRIV(bit) (prvmask.bit)
4055 #define WRITABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
4057 /* Find privilege bits */
4058 status
= SYS$
SETPRV (0, 0, 0, prvmask
);
4060 error ("Unable to find privileges: %s", vmserrstr (status
));
4061 if (CHECKPRIV (PRV$V_BYPASS
))
4062 return 0; /* BYPASS enabled */
4064 fab
.fab$b_fac
= FAB$M_GET
;
4065 fab
.fab$l_fna
= filename
;
4066 fab
.fab$b_fns
= strlen (filename
);
4067 fab
.fab$l_xab
= &xab
;
4068 xab
= cc$rms_xabpro
;
4069 xab
.xab$l_aclbuf
= aclbuf
;
4070 xab
.xab$w_aclsiz
= sizeof (aclbuf
);
4071 status
= SYS$
OPEN (&fab
, 0, 0);
4074 SYS$
CLOSE (&fab
, 0, 0);
4075 /* Check system access */
4076 if (CHECKPRIV (PRV$V_SYSPRV
) && WRITABLE (XAB$V_SYS
))
4078 /* Check ACL entries, if any */
4080 if (xab
.xab$w_acllen
> 0)
4083 aclend
= &aclbuf
[xab
.xab$w_acllen
/ 4];
4084 while (*aclptr
&& aclptr
< aclend
)
4086 size
= (*aclptr
& 0xff) / 4;
4087 typecode
= (*aclptr
>> 8) & 0xff;
4088 if (typecode
== ACE$C_KEYID
)
4089 for (i
= size
- 1; i
> 1; i
--)
4090 if (aclptr
[i
] == uic
)
4093 if (aclptr
[1] & ACE$M_WRITE
)
4094 return 0; /* Write access through ACL */
4096 aclptr
= &aclptr
[size
];
4098 if (acl_controlled
) /* ACL specified, prohibits write access */
4101 /* No ACL entries specified, check normal protection */
4102 if (WRITABLE (XAB$V_WLD
)) /* World writable */
4104 if (WRITABLE (XAB$V_GRP
) &&
4105 (unsigned short) (xab
.xab$l_uic
>> 16) == grpid
)
4106 return 0; /* Group writable */
4107 if (WRITABLE (XAB$V_OWN
) &&
4108 (xab
.xab$l_uic
& 0xFFFF) == memid
)
4109 return 0; /* Owner writable */
4111 return -1; /* Not writable */
4113 #endif /* not VMS4_4 */
4116 static char vtbuf
[NAM$C_MAXRSS
+1];
4118 /* translate a vms file spec to a unix path */
4120 sys_translate_vms (vfile
)
4131 /* leading device or logical name is a root directory */
4132 if (p
= strchr (vfile
, ':'))
4141 if (*p
== '[' || *p
== '<')
4143 while (*++vfile
!= *p
+ 2)
4147 if (vfile
[-1] == *p
)
4170 static char utbuf
[NAM$C_MAXRSS
+1];
4172 /* translate a unix path to a VMS file spec */
4174 sys_translate_unix (ufile
)
4197 if (index (&ufile
[1], '/'))
4204 if (index (&ufile
[1], '/'))
4211 if (strncmp (ufile
, "./", 2) == 0)
4218 ufile
++; /* skip the dot */
4219 if (index (&ufile
[1], '/'))
4224 else if (strncmp (ufile
, "../", 3) == 0)
4232 ufile
+= 2; /* skip the dots */
4233 if (index (&ufile
[1], '/'))
4258 extern char *getcwd ();
4260 #define MAXPATHLEN 1024
4262 ptr
= xmalloc (MAXPATHLEN
);
4263 val
= getcwd (ptr
, MAXPATHLEN
);
4269 strcpy (pathname
, ptr
);
4278 long item_code
= JPI$_OWNER
;
4279 unsigned long parent_id
;
4282 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &parent_id
)) & 1) == 0)
4285 vaxc$errno
= status
;
4295 return (getgid () << 16) | getuid ();
4300 sys_read (fildes
, buf
, nbyte
)
4305 return read (fildes
, buf
, (nbyte
< MAXIOSIZE
? nbyte
: MAXIOSIZE
));
4310 sys_write (fildes
, buf
, nbyte
)
4315 register int nwrote
, rtnval
= 0;
4317 while (nbyte
> MAXIOSIZE
&& (nwrote
= write (fildes
, buf
, MAXIOSIZE
)) > 0) {
4323 return rtnval
? rtnval
: -1;
4324 if ((nwrote
= write (fildes
, buf
, nbyte
)) < 0)
4325 return rtnval
? rtnval
: -1;
4326 return (rtnval
+ nwrote
);
4331 * VAX/VMS VAX C RTL really loses. It insists that records
4332 * end with a newline (carriage return) character, and if they
4333 * don't it adds one (nice of it isn't it!)
4335 * Thus we do this stupidity below.
4340 sys_write (fildes
, buf
, nbytes
)
4343 unsigned int nbytes
;
4350 fstat (fildes
, &st
);
4356 /* Handle fixed-length files with carriage control. */
4357 if (st
.st_fab_rfm
== FAB$C_FIX
4358 && ((st
.st_fab_rat
& (FAB$M_FTN
| FAB$M_CR
)) != 0))
4360 len
= st
.st_fab_mrs
;
4361 retval
= write (fildes
, p
, min (len
, nbytes
));
4364 retval
++; /* This skips the implied carriage control */
4368 e
= p
+ min (MAXIOSIZE
, nbytes
) - 1;
4369 while (*e
!= '\n' && e
> p
) e
--;
4370 if (p
== e
) /* Ok.. so here we add a newline... sigh. */
4371 e
= p
+ min (MAXIOSIZE
, nbytes
) - 1;
4373 retval
= write (fildes
, p
, len
);
4384 /* Create file NEW copying its attributes from file OLD. If
4385 OLD is 0 or does not exist, create based on the value of
4388 /* Protection value the file should ultimately have.
4389 Set by create_copy_attrs, and use by rename_sansversions. */
4390 static unsigned short int fab_final_pro
;
4393 creat_copy_attrs (old
, new)
4396 struct FAB fab
= cc$rms_fab
;
4397 struct XABPRO xabpro
;
4398 char aclbuf
[256]; /* Choice of size is arbitrary. See below. */
4399 extern int vms_stmlf_recfm
;
4403 fab
.fab$b_fac
= FAB$M_GET
;
4404 fab
.fab$l_fna
= old
;
4405 fab
.fab$b_fns
= strlen (old
);
4406 fab
.fab$l_xab
= (char *) &xabpro
;
4407 xabpro
= cc$rms_xabpro
;
4408 xabpro
.xab$l_aclbuf
= aclbuf
;
4409 xabpro
.xab$w_aclsiz
= sizeof aclbuf
;
4410 /* Call $OPEN to fill in the fab & xabpro fields. */
4411 if (SYS$
OPEN (&fab
, 0, 0) & 1)
4413 SYS$
CLOSE (&fab
, 0, 0);
4414 fab
.fab$l_alq
= 0; /* zero the allocation quantity */
4415 if (xabpro
.xab$w_acllen
> 0)
4417 if (xabpro
.xab$w_acllen
> sizeof aclbuf
)
4418 /* If the acl buffer was too short, redo open with longer one.
4419 Wouldn't need to do this if there were some system imposed
4420 limit on the size of an ACL, but I can't find any such. */
4422 xabpro
.xab$l_aclbuf
= (char *) alloca (xabpro
.xab$w_acllen
);
4423 xabpro
.xab$w_aclsiz
= xabpro
.xab$w_acllen
;
4424 if (SYS$
OPEN (&fab
, 0, 0) & 1)
4425 SYS$
CLOSE (&fab
, 0, 0);
4431 xabpro
.xab$l_aclbuf
= 0;
4436 fab
.fab$l_fna
= new;
4437 fab
.fab$b_fns
= strlen (new);
4441 fab
.fab$b_rfm
= vms_stmlf_recfm
? FAB$C_STMLF
: FAB$C_VAR
;
4442 fab
.fab$b_rat
= FAB$M_CR
;
4445 /* Set the file protections such that we will be able to manipulate
4446 this file. Once we are done writing and renaming it, we will set
4447 the protections back. */
4449 fab_final_pro
= xabpro
.xab$w_pro
;
4451 SYS$
SETDFPROT (0, &fab_final_pro
);
4452 xabpro
.xab$w_pro
&= 0xff0f; /* set O:rewd for now. This is set back later. */
4454 /* Create the new file with either default attrs or attrs copied
4456 if (!(SYS$
CREATE (&fab
, 0, 0) & 1))
4458 SYS$
CLOSE (&fab
, 0, 0);
4459 /* As this is a "replacement" for creat, return a file descriptor
4460 opened for writing. */
4461 return open (new, O_WRONLY
);
4466 #include <varargs.h>
4469 #define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
4474 sys_creat (va_alist
)
4477 va_list list_incrementer
;
4480 int rfd
; /* related file descriptor */
4481 int fd
; /* Our new file descriptor */
4488 extern int vms_stmlf_recfm
;
4491 va_start (list_incrementer
);
4492 name
= va_arg (list_incrementer
, char *);
4493 mode
= va_arg (list_incrementer
, int);
4495 rfd
= va_arg (list_incrementer
, int);
4496 va_end (list_incrementer
);
4499 /* Use information from the related file descriptor to set record
4500 format of the newly created file. */
4501 fstat (rfd
, &st_buf
);
4502 switch (st_buf
.st_fab_rfm
)
4505 strcpy (rfm
, "rfm = fix");
4506 sprintf (mrs
, "mrs = %d", st_buf
.st_fab_mrs
);
4507 strcpy (rat
, "rat = ");
4508 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4510 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4511 strcat (rat
, "ftn");
4512 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4513 strcat (rat
, "prn");
4514 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4515 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4516 strcat (rat
, ", blk");
4518 strcat (rat
, "blk");
4519 return creat (name
, 0, rfm
, rat
, mrs
);
4522 strcpy (rfm
, "rfm = vfc");
4523 sprintf (fsz
, "fsz = %d", st_buf
.st_fab_fsz
);
4524 strcpy (rat
, "rat = ");
4525 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4527 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4528 strcat (rat
, "ftn");
4529 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4530 strcat (rat
, "prn");
4531 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4532 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4533 strcat (rat
, ", blk");
4535 strcat (rat
, "blk");
4536 return creat (name
, 0, rfm
, rat
, fsz
);
4539 strcpy (rfm
, "rfm = stm");
4543 strcpy (rfm
, "rfm = stmcr");
4547 strcpy (rfm
, "rfm = stmlf");
4551 strcpy (rfm
, "rfm = udf");
4555 strcpy (rfm
, "rfm = var");
4558 strcpy (rat
, "rat = ");
4559 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4561 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4562 strcat (rat
, "ftn");
4563 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4564 strcat (rat
, "prn");
4565 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4566 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4567 strcat (rat
, ", blk");
4569 strcat (rat
, "blk");
4573 strcpy (rfm
, vms_stmlf_recfm
? "rfm = stmlf" : "rfm=var");
4574 strcpy (rat
, "rat=cr");
4576 /* Until the VAX C RTL fixes the many bugs with modes, always use
4577 mode 0 to get the user's default protection. */
4578 fd
= creat (name
, 0, rfm
, rat
);
4579 if (fd
< 0 && errno
== EEXIST
)
4581 if (unlink (name
) < 0)
4582 report_file_error ("delete", build_string (name
));
4583 fd
= creat (name
, 0, rfm
, rat
);
4589 /* fwrite to stdout is S L O W. Speed it up by using fputc...*/
4591 sys_fwrite (ptr
, size
, num
, fp
)
4592 register char * ptr
;
4595 register int tot
= num
* size
;
4603 * The VMS C library routine creat actually creates a new version of an
4604 * existing file rather than truncating the old version. There are times
4605 * when this is not the desired behavior, for instance, when writing an
4606 * auto save file (you only want one version), or when you don't have
4607 * write permission in the directory containing the file (but the file
4608 * itself is writable). Hence this routine, which is equivalent to
4609 * "close (creat (fn, 0));" on Unix if fn already exists.
4615 struct FAB xfab
= cc$rms_fab
;
4616 struct RAB xrab
= cc$rms_rab
;
4619 xfab
.fab$l_fop
= FAB$M_TEF
; /* free allocated but unused blocks on close */
4620 xfab
.fab$b_fac
= FAB$M_TRN
| FAB$M_GET
; /* allow truncate and get access */
4621 xfab
.fab$b_shr
= FAB$M_NIL
; /* allow no sharing - file must be locked */
4622 xfab
.fab$l_fna
= fn
;
4623 xfab
.fab$b_fns
= strlen (fn
);
4624 xfab
.fab$l_dna
= ";0"; /* default to latest version of the file */
4626 xrab
.rab$l_fab
= &xfab
;
4628 /* This gibberish opens the file, positions to the first record, and
4629 deletes all records from there until the end of file. */
4630 if ((SYS$
OPEN (&xfab
) & 01) == 01)
4632 if ((SYS$
CONNECT (&xrab
) & 01) == 01 &&
4633 (SYS$
FIND (&xrab
) & 01) == 01 &&
4634 (SYS$
TRUNCATE (&xrab
) & 01) == 01)
4645 /* Define this symbol to actually read SYSUAF.DAT. This requires either
4646 SYSPRV or a readable SYSUAF.DAT. */
4652 * Routine to read the VMS User Authorization File and return
4653 * a specific user's record.
4656 static struct UAF retuaf
;
4659 get_uaf_name (uname
)
4666 uaf_fab
= cc$rms_fab
;
4667 uaf_rab
= cc$rms_rab
;
4668 /* initialize fab fields */
4669 uaf_fab
.fab$l_fna
= "SYS$SYSTEM:SYSUAF.DAT";
4670 uaf_fab
.fab$b_fns
= 21;
4671 uaf_fab
.fab$b_fac
= FAB$M_GET
;
4672 uaf_fab
.fab$b_org
= FAB$C_IDX
;
4673 uaf_fab
.fab$b_shr
= FAB$M_GET
|FAB$M_PUT
|FAB$M_UPD
|FAB$M_DEL
;
4674 /* initialize rab fields */
4675 uaf_rab
.rab$l_fab
= &uaf_fab
;
4676 /* open the User Authorization File */
4677 status
= SYS$
OPEN (&uaf_fab
);
4681 vaxc$errno
= status
;
4684 status
= SYS$
CONNECT (&uaf_rab
);
4688 vaxc$errno
= status
;
4691 /* read the requested record - index is in uname */
4692 uaf_rab
.rab$l_kbf
= uname
;
4693 uaf_rab
.rab$b_ksz
= strlen (uname
);
4694 uaf_rab
.rab$b_rac
= RAB$C_KEY
;
4695 uaf_rab
.rab$l_ubf
= (char *)&retuaf
;
4696 uaf_rab
.rab$w_usz
= sizeof retuaf
;
4697 status
= SYS$
GET (&uaf_rab
);
4701 vaxc$errno
= status
;
4704 /* close the User Authorization File */
4705 status
= SYS$
DISCONNECT (&uaf_rab
);
4709 vaxc$errno
= status
;
4712 status
= SYS$
CLOSE (&uaf_fab
);
4716 vaxc$errno
= status
;
4730 uaf_fab
= cc$rms_fab
;
4731 uaf_rab
= cc$rms_rab
;
4732 /* initialize fab fields */
4733 uaf_fab
.fab$l_fna
= "SYS$SYSTEM:SYSUAF.DAT";
4734 uaf_fab
.fab$b_fns
= 21;
4735 uaf_fab
.fab$b_fac
= FAB$M_GET
;
4736 uaf_fab
.fab$b_org
= FAB$C_IDX
;
4737 uaf_fab
.fab$b_shr
= FAB$M_GET
|FAB$M_PUT
|FAB$M_UPD
|FAB$M_DEL
;
4738 /* initialize rab fields */
4739 uaf_rab
.rab$l_fab
= &uaf_fab
;
4740 /* open the User Authorization File */
4741 status
= SYS$
OPEN (&uaf_fab
);
4745 vaxc$errno
= status
;
4748 status
= SYS$
CONNECT (&uaf_rab
);
4752 vaxc$errno
= status
;
4755 /* read the requested record - index is in uic */
4756 uaf_rab
.rab$b_krf
= 1; /* 1st alternate key */
4757 uaf_rab
.rab$l_kbf
= (char *) &uic
;
4758 uaf_rab
.rab$b_ksz
= sizeof uic
;
4759 uaf_rab
.rab$b_rac
= RAB$C_KEY
;
4760 uaf_rab
.rab$l_ubf
= (char *)&retuaf
;
4761 uaf_rab
.rab$w_usz
= sizeof retuaf
;
4762 status
= SYS$
GET (&uaf_rab
);
4766 vaxc$errno
= status
;
4769 /* close the User Authorization File */
4770 status
= SYS$
DISCONNECT (&uaf_rab
);
4774 vaxc$errno
= status
;
4777 status
= SYS$
CLOSE (&uaf_fab
);
4781 vaxc$errno
= status
;
4787 static struct passwd retpw
;
4795 /* copy these out first because if the username is 32 chars, the next
4796 section will overwrite the first byte of the UIC */
4797 retpw
.pw_uid
= up
->uaf$w_mem
;
4798 retpw
.pw_gid
= up
->uaf$w_grp
;
4800 /* I suppose this is not the best style, to possibly overwrite one
4801 byte beyond the end of the field, but what the heck... */
4802 ptr
= &up
->uaf$t_username
[UAF$S_USERNAME
];
4803 while (ptr
[-1] == ' ')
4806 strcpy (retpw
.pw_name
, up
->uaf$t_username
);
4808 /* the rest of these are counted ascii strings */
4809 strncpy (retpw
.pw_gecos
, &up
->uaf$t_owner
[1], up
->uaf$t_owner
[0]);
4810 retpw
.pw_gecos
[up
->uaf$t_owner
[0]] = '\0';
4811 strncpy (retpw
.pw_dir
, &up
->uaf$t_defdev
[1], up
->uaf$t_defdev
[0]);
4812 retpw
.pw_dir
[up
->uaf$t_defdev
[0]] = '\0';
4813 strncat (retpw
.pw_dir
, &up
->uaf$t_defdir
[1], up
->uaf$t_defdir
[0]);
4814 retpw
.pw_dir
[up
->uaf$t_defdev
[0] + up
->uaf$t_defdir
[0]] = '\0';
4815 strncpy (retpw
.pw_shell
, &up
->uaf$t_defcli
[1], up
->uaf$t_defcli
[0]);
4816 retpw
.pw_shell
[up
->uaf$t_defcli
[0]] = '\0';
4820 #else /* not READ_SYSUAF */
4821 static struct passwd retpw
;
4822 #endif /* not READ_SYSUAF */
4833 unsigned char * full
;
4834 #endif /* READ_SYSUAF */
4839 if ('a' <= *ptr
&& *ptr
<= 'z')
4844 if (!(up
= get_uaf_name (name
)))
4846 return cnv_uaf_pw (up
);
4848 if (strcmp (name
, getenv ("USER")) == 0)
4850 retpw
.pw_uid
= getuid ();
4851 retpw
.pw_gid
= getgid ();
4852 strcpy (retpw
.pw_name
, name
);
4853 if (full
= egetenv ("FULLNAME"))
4854 strcpy (retpw
.pw_gecos
, full
);
4856 *retpw
.pw_gecos
= '\0';
4857 strcpy (retpw
.pw_dir
, egetenv ("HOME"));
4858 *retpw
.pw_shell
= '\0';
4863 #endif /* not READ_SYSUAF */
4873 if (!(up
= get_uaf_uic (uid
)))
4875 return cnv_uaf_pw (up
);
4877 if (uid
== sys_getuid ())
4878 return getpwnam (egetenv ("USER"));
4881 #endif /* not READ_SYSUAF */
4884 /* return total address space available to the current process. This is
4885 the sum of the current p0 size, p1 size and free page table entries
4891 unsigned long free_pages
;
4892 unsigned long frep0va
;
4893 unsigned long frep1va
;
4896 item_code
= JPI$_FREPTECNT
;
4897 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &free_pages
)) & 1) == 0)
4900 vaxc$errno
= status
;
4905 item_code
= JPI$_FREP0VA
;
4906 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &frep0va
)) & 1) == 0)
4909 vaxc$errno
= status
;
4912 item_code
= JPI$_FREP1VA
;
4913 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &frep1va
)) & 1) == 0)
4916 vaxc$errno
= status
;
4920 return free_pages
+ frep0va
+ (0x7fffffff - frep1va
);
4924 define_logical_name (varname
, string
)
4928 struct dsc$descriptor_s strdsc
=
4929 {strlen (string
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, string
};
4930 struct dsc$descriptor_s envdsc
=
4931 {strlen (varname
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, varname
};
4932 struct dsc$descriptor_s lnmdsc
=
4933 {7, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, "LNM$JOB"};
4935 return LIB$
SET_LOGICAL (&envdsc
, &strdsc
, &lnmdsc
, 0, 0);
4939 delete_logical_name (varname
)
4942 struct dsc$descriptor_s envdsc
=
4943 {strlen (varname
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, varname
};
4944 struct dsc$descriptor_s lnmdsc
=
4945 {7, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, "LNM$JOB"};
4947 return LIB$
DELETE_LOGICAL (&envdsc
, &lnmdsc
);
4965 error ("execvp system call not implemented");
4974 struct FAB from_fab
= cc$rms_fab
, to_fab
= cc$rms_fab
;
4975 struct NAM from_nam
= cc$rms_nam
, to_nam
= cc$rms_nam
;
4976 char from_esn
[NAM$C_MAXRSS
];
4977 char to_esn
[NAM$C_MAXRSS
];
4979 from_fab
.fab$l_fna
= from
;
4980 from_fab
.fab$b_fns
= strlen (from
);
4981 from_fab
.fab$l_nam
= &from_nam
;
4982 from_fab
.fab$l_fop
= FAB$M_NAM
;
4984 from_nam
.nam$l_esa
= from_esn
;
4985 from_nam
.nam$b_ess
= sizeof from_esn
;
4987 to_fab
.fab$l_fna
= to
;
4988 to_fab
.fab$b_fns
= strlen (to
);
4989 to_fab
.fab$l_nam
= &to_nam
;
4990 to_fab
.fab$l_fop
= FAB$M_NAM
;
4992 to_nam
.nam$l_esa
= to_esn
;
4993 to_nam
.nam$b_ess
= sizeof to_esn
;
4995 status
= SYS$
RENAME (&from_fab
, 0, 0, &to_fab
);
5001 if (status
== RMS$_DEV
)
5005 vaxc$errno
= status
;
5010 /* This function renames a file like `rename', but it strips
5011 the version number from the "to" filename, such that the "to" file is
5012 will always be a new version. It also sets the file protection once it is
5013 finished. The protection that we will use is stored in fab_final_pro,
5014 and was set when we did a creat_copy_attrs to create the file that we
5017 We could use the chmod function, but Eunichs uses 3 bits per user category
5018 to describe the protection, and VMS uses 4 (write and delete are separate
5019 bits). To maintain portability, the VMS implementation of `chmod' wires
5020 the W and D bits together. */
5023 static struct fibdef fib
; /* We need this initialized to zero */
5024 char vms_file_written
[NAM$C_MAXRSS
];
5027 rename_sans_version (from
,to
)
5034 struct FAB to_fab
= cc$rms_fab
;
5035 struct NAM to_nam
= cc$rms_nam
;
5036 struct dsc$descriptor fib_d
={sizeof (fib
),0,0,(char*) &fib
};
5037 struct dsc$descriptor fib_attr
[2]
5038 = {{sizeof (fab_final_pro
),ATR$C_FPRO
,0,(char*) &fab_final_pro
},{0,0,0,0}};
5039 char to_esn
[NAM$C_MAXRSS
];
5041 $
DESCRIPTOR (disk
,to_esn
);
5043 to_fab
.fab$l_fna
= to
;
5044 to_fab
.fab$b_fns
= strlen (to
);
5045 to_fab
.fab$l_nam
= &to_nam
;
5046 to_fab
.fab$l_fop
= FAB$M_NAM
;
5048 to_nam
.nam$l_esa
= to_esn
;
5049 to_nam
.nam$b_ess
= sizeof to_esn
;
5051 status
= SYS$
PARSE (&to_fab
, 0, 0); /* figure out the full file name */
5053 if (to_nam
.nam$l_fnb
&& NAM$M_EXP_VER
)
5054 *(to_nam
.nam$l_ver
) = '\0';
5056 stat
= rename (from
, to_esn
);
5060 strcpy (vms_file_written
, to_esn
);
5062 to_fab
.fab$l_fna
= vms_file_written
; /* this points to the versionless name */
5063 to_fab
.fab$b_fns
= strlen (vms_file_written
);
5065 /* Now set the file protection to the correct value */
5066 SYS$
OPEN (&to_fab
, 0, 0); /* This fills in the nam$w_fid fields */
5068 /* Copy these fields into the fib */
5069 fib
.fib$r_fid_overlay
.fib$w_fid
[0] = to_nam
.nam$w_fid
[0];
5070 fib
.fib$r_fid_overlay
.fib$w_fid
[1] = to_nam
.nam$w_fid
[1];
5071 fib
.fib$r_fid_overlay
.fib$w_fid
[2] = to_nam
.nam$w_fid
[2];
5073 SYS$
CLOSE (&to_fab
, 0, 0);
5075 stat
= SYS$
ASSIGN (&disk
, &chan
, 0, 0); /* open a channel to the disk */
5078 stat
= SYS$
QIOW (0, chan
, IO$_MODIFY
, iosb
, 0, 0, &fib_d
,
5079 0, 0, 0, &fib_attr
, 0);
5082 stat
= SYS$
DASSGN (chan
);
5085 strcpy (vms_file_written
, to_esn
); /* We will write this to the terminal*/
5096 unsigned short fid
[3];
5097 char esa
[NAM$C_MAXRSS
];
5100 fab
.fab$l_fop
= FAB$M_OFP
;
5101 fab
.fab$l_fna
= file
;
5102 fab
.fab$b_fns
= strlen (file
);
5103 fab
.fab$l_nam
= &nam
;
5106 nam
.nam$l_esa
= esa
;
5107 nam
.nam$b_ess
= NAM$C_MAXRSS
;
5109 status
= SYS$
PARSE (&fab
);
5110 if ((status
& 1) == 0)
5113 vaxc$errno
= status
;
5116 status
= SYS$
SEARCH (&fab
);
5117 if ((status
& 1) == 0)
5120 vaxc$errno
= status
;
5124 fid
[0] = nam
.nam$w_fid
[0];
5125 fid
[1] = nam
.nam$w_fid
[1];
5126 fid
[2] = nam
.nam$w_fid
[2];
5128 fab
.fab$l_fna
= new;
5129 fab
.fab$b_fns
= strlen (new);
5131 status
= SYS$
PARSE (&fab
);
5132 if ((status
& 1) == 0)
5135 vaxc$errno
= status
;
5139 nam
.nam$w_fid
[0] = fid
[0];
5140 nam
.nam$w_fid
[1] = fid
[1];
5141 nam
.nam$w_fid
[2] = fid
[2];
5143 nam
.nam$l_esa
= nam
.nam$l_name
;
5144 nam
.nam$b_esl
= nam
.nam$b_name
+ nam
.nam$b_type
+ nam
.nam$b_ver
;
5146 status
= SYS$
ENTER (&fab
);
5147 if ((status
& 1) == 0)
5150 vaxc$errno
= status
;
5161 printf ("%s not yet implemented\r\n", badfunc
);
5162 reset_all_sys_modes ();
5169 /* Arrange to return a range centered on zero. */
5170 return rand () - (1 << 30);
5182 /* Called from init_sys_modes. */
5184 hft_init (struct tty_display_info
*tty_out
)
5188 /* If we're not on an HFT we shouldn't do any of this. We determine
5189 if we are on an HFT by trying to get an HFT error code. If this
5190 call fails, we're not on an HFT. */
5192 if (ioctl (0, HFQERROR
, &junk
) < 0)
5194 #else /* not IBMR2AIX */
5195 if (ioctl (0, HFQEIO
, 0) < 0)
5197 #endif /* not IBMR2AIX */
5199 /* On AIX the default hft keyboard mapping uses backspace rather than delete
5200 as the rubout key's ASCII code. Here this is changed. The bug is that
5201 there's no way to determine the old mapping, so in reset_sys_modes
5202 we need to assume that the normal map had been present. Of course, this
5203 code also doesn't help if on a terminal emulator which doesn't understand
5207 struct hfkeymap keymap
;
5209 buf
.hf_bufp
= (char *)&keymap
;
5210 buf
.hf_buflen
= sizeof (keymap
);
5211 keymap
.hf_nkeys
= 2;
5212 keymap
.hfkey
[0].hf_kpos
= 15;
5213 keymap
.hfkey
[0].hf_kstate
= HFMAPCHAR
| HFSHFNONE
;
5215 keymap
.hfkey
[0].hf_keyidh
= '<';
5216 #else /* not IBMR2AIX */
5217 keymap
.hfkey
[0].hf_page
= '<';
5218 #endif /* not IBMR2AIX */
5219 keymap
.hfkey
[0].hf_char
= 127;
5220 keymap
.hfkey
[1].hf_kpos
= 15;
5221 keymap
.hfkey
[1].hf_kstate
= HFMAPCHAR
| HFSHFSHFT
;
5223 keymap
.hfkey
[1].hf_keyidh
= '<';
5224 #else /* not IBMR2AIX */
5225 keymap
.hfkey
[1].hf_page
= '<';
5226 #endif /* not IBMR2AIX */
5227 keymap
.hfkey
[1].hf_char
= 127;
5228 hftctl (0, HFSKBD
, &buf
);
5232 /* Reset the rubout key to backspace. */
5235 hft_reset (struct tty_display_info
*tty_out
)
5238 struct hfkeymap keymap
;
5242 if (ioctl (0, HFQERROR
, &junk
) < 0)
5244 #else /* not IBMR2AIX */
5245 if (ioctl (0, HFQEIO
, 0) < 0)
5247 #endif /* not IBMR2AIX */
5249 buf
.hf_bufp
= (char *)&keymap
;
5250 buf
.hf_buflen
= sizeof (keymap
);
5251 keymap
.hf_nkeys
= 2;
5252 keymap
.hfkey
[0].hf_kpos
= 15;
5253 keymap
.hfkey
[0].hf_kstate
= HFMAPCHAR
| HFSHFNONE
;
5255 keymap
.hfkey
[0].hf_keyidh
= '<';
5256 #else /* not IBMR2AIX */
5257 keymap
.hfkey
[0].hf_page
= '<';
5258 #endif /* not IBMR2AIX */
5259 keymap
.hfkey
[0].hf_char
= 8;
5260 keymap
.hfkey
[1].hf_kpos
= 15;
5261 keymap
.hfkey
[1].hf_kstate
= HFMAPCHAR
| HFSHFSHFT
;
5263 keymap
.hfkey
[1].hf_keyidh
= '<';
5264 #else /* not IBMR2AIX */
5265 keymap
.hfkey
[1].hf_page
= '<';
5266 #endif /* not IBMR2AIX */
5267 keymap
.hfkey
[1].hf_char
= 8;
5268 hftctl (0, HFSKBD
, &buf
);
5281 register int length
;
5285 long max_str
= 65535;
5287 while (length
> max_str
) {
5288 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &max_str
, b
);
5293 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &max_str
, b
);
5295 while (length
-- > 0)
5297 #endif /* not VMS */
5300 #endif /* no bzero */
5301 #endif /* BSTRING */
5303 #if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
5306 /* Saying `void' requires a declaration, above, where bcopy is used
5307 and that declaration causes pain for systems where bcopy is a macro. */
5308 bcopy (b1
, b2
, length
)
5311 register int length
;
5314 long max_str
= 65535;
5316 while (length
> max_str
) {
5317 (void) LIB$
MOVC3 (&max_str
, b1
, b2
);
5323 (void) LIB$
MOVC3 (&length
, b1
, b2
);
5325 while (length
-- > 0)
5327 #endif /* not VMS */
5329 #endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
5334 bcmp (b1
, b2
, length
) /* This could be a macro! */
5337 register int length
;
5340 struct dsc$descriptor_s src1
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b1
};
5341 struct dsc$descriptor_s src2
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b2
};
5343 return STR$
COMPARE (&src1
, &src2
);
5345 while (length
-- > 0)
5350 #endif /* not VMS */
5352 #endif /* no bcmp */
5353 #endif /* not BSTRING */
5355 #ifndef HAVE_STRSIGNAL
5362 if (0 <= code
&& code
< NSIG
)
5365 signame
= sys_errlist
[code
];
5367 /* Cast to suppress warning if the table has const char *. */
5368 signame
= (char *) sys_siglist
[code
];
5374 #endif /* HAVE_STRSIGNAL */
5376 /* arch-tag: edb43589-4e09-4544-b325-978b5b121dcf
5377 (do not change this comment) */