1 /* Interfaces to system-dependent kernel and library entries.
2 Copyright (C) 1985, 86,87,88,93,94,95, 1999 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
27 #include "blockinput.h"
32 __sigfun
sys_signal (int signal
, __sigfun signal_func
);
34 __signal_func_ptr
sys_signal (int signal
, __signal_func_ptr signal_func
);
39 /* Nonzero means delete a process right away if it exits (process.c). */
40 static int delete_exited_processes
;
42 #ifndef HAVE_X_WINDOWS
43 /* Search path for bitmap files (xfns.c). */
44 Lisp_Object Vx_bitmap_file_path
;
46 #endif /* macintosh */
48 #define min(x,y) ((x) > (y) ? (y) : (x))
55 #endif /* not WINDOWSNT */
57 /* Does anyone other than VMS need this? */
59 #define sys_fwrite fwrite
69 #include <sys/types.h>
73 /* Get _POSIX_VDISABLE, if it is available. */
78 /* Get SI_SRPC_DOMAIN, if it is available. */
79 #ifdef HAVE_SYS_SYSTEMINFO_H
80 #include <sys/systeminfo.h>
83 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
87 #include <sys/param.h>
91 extern unsigned start
__asm__ ("start");
111 #include <sys/file.h>
119 #define MAXIOSIZE (32 * PAGESIZE) /* Don't I/O more than 32 blocks at a time */
123 #ifdef BSD_SYSTEM /* avoid writing defined (BSD_SYSTEM) || defined (USG)
124 because the vms compiler doesn't grok `defined' */
132 #endif /* not 4.1 bsd */
135 #include <sys/ioctl.h>
141 #ifdef BROKEN_TIOCGWINSZ
146 #if defined (USG) || defined (DGUX)
147 #include <sys/utsname.h>
149 #ifndef MEMORY_IN_STRING_H
152 #if defined (TIOCGWINSZ) || defined (ISC4_0)
154 #include <sys/sioctl.h>
157 #include <sys/stream.h>
158 #include <sys/ptem.h>
160 #endif /* TIOCGWINSZ or ISC4_0 */
161 #endif /* USG or DGUX */
163 extern int quit_char
;
167 #include "termhooks.h"
168 #include "termchar.h"
169 #include "termopts.h"
170 #include "dispextern.h"
175 /* In process.h which conflicts with the local copy. */
177 int _CRTAPI1
_spawnlp (int, const char *, const char *, ...);
178 int _CRTAPI1
_getpid (void);
181 #ifdef NONSYSTEM_DIR_LIBRARY
183 #endif /* NONSYSTEM_DIR_LIBRARY */
185 #include "syssignal.h"
192 #ifndef HAVE_STRUCT_UTIMBUF
193 /* We want to use utime rather than utimes, but we couldn't find the
194 structure declaration. We'll use the traditional one. */
202 #ifndef VFORK_RETURN_TYPE
203 #define VFORK_RETURN_TYPE int
206 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
212 #define LNOFLSH 0100000
215 static int baud_convert
[] =
220 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
221 1800, 2400, 4800, 9600, 19200, 38400
225 #if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
228 #if defined (HAVE_TERMIOS_H) && defined (LINUX)
230 /* HJL's version of libc is said to need this on the Alpha.
231 On the other hand, DEC OSF1 on the Alpha needs ospeed to be a short. */
232 extern speed_t ospeed
;
238 /* The file descriptor for Emacs's input terminal.
239 Under Unix, this is normally zero except when using X;
240 under VMS, we place the input channel number here. */
243 void croak
P_ ((char *));
251 /* Specify a different file descriptor for further input operations. */
260 /* Discard pending input on descriptor input_fd. */
266 struct emacs_tty buf
;
271 /* Discarding input is not safe when the input could contain
272 replies from the X server. So don't do it. */
273 if (read_socket_hook
)
278 SYS$
QIOW (0, input_fd
, IO$_READVBLK
|IO$M_PURGE
, input_iosb
, 0, 0,
279 &buf
.main
, 0, 0, terminator_mask
, 0, 0);
285 ioctl (input_fd
, TIOCFLUSH
, &zero
);
287 #else /* not Apollo */
288 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
289 while (dos_keyread () != -1)
291 #else /* not MSDOS */
292 EMACS_GET_TTY (input_fd
, &buf
);
293 EMACS_SET_TTY (input_fd
, &buf
, 0);
294 #endif /* not MSDOS */
295 #endif /* not Apollo */
297 #endif /* not WINDOWSNT */
302 /* Arrange for character C to be read as the next input from
309 if (read_socket_hook
)
312 /* Should perhaps error if in batch mode */
314 ioctl (input_fd
, TIOCSTI
, &c
);
315 #else /* no TIOCSTI */
316 error ("Cannot stuff terminal input characters in this version of Unix");
317 #endif /* no TIOCSTI */
329 #ifdef INIT_BAUD_RATE
334 #else /* not DOS_NT */
338 SYS$
QIOW (0, input_fd
, IO$_SENSEMODE
, &sg
, 0, 0,
339 &sg
.class, 12, 0, 0, 0, 0 );
340 ospeed
= sg
.xmit_baud
;
346 tcgetattr (input_fd
, &sg
);
347 ospeed
= cfgetospeed (&sg
);
348 #if defined (USE_GETOBAUD) && defined (getobaud)
349 /* m88k-motorola-sysv3 needs this (ghazi@noc.rutgers.edu) 9/1/94. */
351 ospeed
= getobaud (sg
.c_cflag
);
353 #else /* neither VMS nor TERMIOS */
359 tcgetattr (input_fd
, &sg
);
361 ioctl (input_fd
, TCGETA
, &sg
);
363 ospeed
= sg
.c_cflag
& CBAUD
;
364 #else /* neither VMS nor TERMIOS nor TERMIO */
367 sg
.sg_ospeed
= B9600
;
368 if (ioctl (input_fd
, TIOCGETP
, &sg
) < 0)
370 ospeed
= sg
.sg_ospeed
;
371 #endif /* not HAVE_TERMIO */
372 #endif /* not HAVE_TERMIOS */
374 #endif /* not DOS_NT */
375 #endif /* not INIT_BAUD_RATE */
378 baud_rate
= (ospeed
< sizeof baud_convert
/ sizeof baud_convert
[0]
379 ? baud_convert
[ospeed
] : 9600);
386 set_exclusive_use (fd
)
390 ioctl (fd
, FIOCLEX
, 0);
392 /* Ok to do nothing if this feature does not exist */
397 wait_without_blocking ()
400 wait3 (0, WNOHANG
| WUNTRACED
, 0);
402 croak ("wait_without_blocking");
404 synch_process_alive
= 0;
407 #endif /* not subprocesses */
409 int wait_debugging
; /* Set nonzero to make following function work under dbx
410 (at least for bsd). */
413 wait_for_termination_signal ()
416 /* Wait for subprocess with process id `pid' to terminate and
417 make sure it will get eliminated (not remain forever as a zombie) */
420 wait_for_termination (pid
)
429 status
= SYS$
FORCEX (&pid
, 0, 0);
432 #if defined (BSD_SYSTEM) || (defined (HPUX) && !defined (HPUX_5))
433 /* Note that kill returns -1 even if the process is just a zombie now.
434 But inevitably a SIGCHLD interrupt should be generated
435 and child_sig will do wait3 and make the process go away. */
436 /* There is some indication that there is a bug involved with
437 termination of subprocesses, perhaps involving a kernel bug too,
438 but no idea what it is. Just as a hunch we signal SIGCHLD to see
439 if that causes the problem to go away or get worse. */
440 sigsetmask (sigmask (SIGCHLD
));
441 if (0 > kill (pid
, 0))
443 sigsetmask (SIGEMPTYMASK
);
444 kill (getpid (), SIGCHLD
);
450 sigpause (SIGEMPTYMASK
);
451 #else /* not BSD_SYSTEM, and not HPUX version >= 6 */
452 #if defined (UNIPLUS)
453 if (0 > kill (pid
, 0))
456 #else /* neither BSD_SYSTEM nor UNIPLUS: random sysV */
457 #ifdef POSIX_SIGNALS /* would this work for LINUX as well? */
458 sigblock (sigmask (SIGCHLD
));
459 if (0 > kill (pid
, 0))
461 sigunblock (sigmask (SIGCHLD
));
464 sigpause (SIGEMPTYMASK
);
465 #else /* not POSIX_SIGNALS */
466 #ifdef HAVE_SYSV_SIGPAUSE
468 if (0 > kill (pid
, 0))
474 #else /* not HAVE_SYSV_SIGPAUSE */
478 #else /* not WINDOWSNT */
479 if (0 > kill (pid
, 0))
481 /* Using sleep instead of pause avoids timing error.
482 If the inferior dies just before the sleep,
483 we lose just one second. */
485 #endif /* not WINDOWSNT */
486 #endif /* not HAVE_SYSV_SIGPAUSE */
487 #endif /* not POSIX_SIGNALS */
488 #endif /* not UNIPLUS */
489 #endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
491 #else /* not subprocesses */
494 #else /* not __DJGPP__ > 1 */
496 if (kill (pid
, 0) < 0)
502 if (status
== pid
|| status
== -1)
505 #endif /* not __DJGPP__ > 1*/
506 #endif /* not subprocesses */
513 * flush any pending output
514 * (may flush input as well; it does not matter the way we use it)
518 flush_pending_output (channel
)
522 /* If we try this, we get hit with SIGTTIN, because
523 the child's tty belongs to the child's pgrp. */
526 ioctl (channel
, TCFLSH
, 1);
530 /* 3rd arg should be ignored
531 but some 4.2 kernels actually want the address of an int
532 and nonzero means something different. */
533 ioctl (channel
, TIOCFLUSH
, &zero
);
540 /* Set up the terminal at the other end of a pseudo-terminal that
541 we will be controlling an inferior through.
542 It should not echo or do line-editing, since that is done
543 in Emacs. No padding needed for insertion into an Emacs buffer. */
546 child_setup_tty (out
)
552 EMACS_GET_TTY (out
, &s
);
554 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
555 s
.main
.c_oflag
|= OPOST
; /* Enable output postprocessing */
556 s
.main
.c_oflag
&= ~ONLCR
; /* Disable map of NL to CR-NL on output */
558 s
.main
.c_oflag
&= ~(NLDLY
|CRDLY
|TABDLY
|BSDLY
|VTDLY
|FFDLY
);
559 /* No output delays */
561 s
.main
.c_lflag
&= ~ECHO
; /* Disable echo */
562 s
.main
.c_lflag
|= ISIG
; /* Enable signals */
564 s
.main
.c_iflag
&= ~IUCLC
; /* Disable downcasing on input. */
567 s
.main
.c_iflag
&= ~ISTRIP
; /* don't strip 8th bit on input */
570 s
.main
.c_oflag
&= ~OLCUC
; /* Disable upcasing on output. */
572 s
.main
.c_oflag
&= ~TAB3
; /* Disable tab expansion */
573 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CSIZE
) | CS8
; /* Don't strip 8th bit */
575 /* Said to be unnecessary: */
576 s
.main
.c_cc
[VMIN
] = 1; /* minimum number of characters to accept */
577 s
.main
.c_cc
[VTIME
] = 0; /* wait forever for at least 1 character */
580 s
.main
.c_lflag
|= ICANON
; /* Enable erase/kill and eof processing */
581 s
.main
.c_cc
[VEOF
] = 04; /* insure that EOF is Control-D */
582 s
.main
.c_cc
[VERASE
] = CDISABLE
; /* disable erase processing */
583 s
.main
.c_cc
[VKILL
] = CDISABLE
; /* disable kill processing */
586 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CBAUD
) | B9600
; /* baud rate sanity */
590 /* AIX enhanced edit loses NULs, so disable it */
593 s
.main
.c_iflag
&= ~ASCEDIT
;
595 /* Also, PTY overloads NUL and BREAK.
596 don't ignore break, but don't signal either, so it looks like NUL. */
597 s
.main
.c_iflag
&= ~IGNBRK
;
598 s
.main
.c_iflag
&= ~BRKINT
;
599 /* QUIT and INTR work better as signals, so disable character forms */
600 s
.main
.c_cc
[VINTR
] = 0377;
601 #ifdef SIGNALS_VIA_CHARACTERS
602 /* the QUIT and INTR character are used in process_send_signal
603 so set them here to something useful. */
604 if (s
.main
.c_cc
[VQUIT
] == 0377)
605 s
.main
.c_cc
[VQUIT
] = '\\'&037; /* Control-\ */
606 if (s
.main
.c_cc
[VINTR
] == 0377)
607 s
.main
.c_cc
[VINTR
] = 'C'&037; /* Control-C */
608 #else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
609 /* QUIT and INTR work better as signals, so disable character forms */
610 s
.main
.c_cc
[VQUIT
] = 0377;
611 s
.main
.c_cc
[VINTR
] = 0377;
612 s
.main
.c_lflag
&= ~ISIG
;
613 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
614 s
.main
.c_cc
[VEOL
] = 0377;
615 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CBAUD
) | B9600
; /* baud rate sanity */
618 #else /* not HAVE_TERMIO */
620 s
.main
.sg_flags
&= ~(ECHO
| CRMOD
| ANYP
| ALLDELAY
| RAW
| LCASE
622 s
.main
.sg_flags
|= LPASS8
;
623 s
.main
.sg_erase
= 0377;
624 s
.main
.sg_kill
= 0377;
625 s
.lmode
= LLITOUT
| s
.lmode
; /* Don't strip 8th bit */
627 #endif /* not HAVE_TERMIO */
629 EMACS_SET_TTY (out
, &s
, 0);
638 ioctl (out
, FIOASYNC
, &zero
);
641 #endif /* not DOS_NT */
645 #endif /* subprocesses */
647 /* Record a signal code and the handler for it. */
651 SIGTYPE (*handler
) P_ ((int));
654 static void save_signal_handlers
P_ ((struct save_signal
*));
655 static void restore_signal_handlers
P_ ((struct save_signal
*));
657 /* Suspend the Emacs process; give terminal to its superior. */
663 /* "Foster" parentage allows emacs to return to a subprocess that attached
664 to the current emacs as a cheaper than starting a whole new process. This
665 is set up by KEPTEDITOR.COM. */
666 unsigned long parent_id
, foster_parent_id
;
669 fpid_string
= getenv ("EMACS_PARENT_PID");
670 if (fpid_string
!= NULL
)
672 sscanf (fpid_string
, "%x", &foster_parent_id
);
673 if (foster_parent_id
!= 0)
674 parent_id
= foster_parent_id
;
676 parent_id
= getppid ();
679 parent_id
= getppid ();
681 xfree (fpid_string
); /* On VMS, this was malloc'd */
683 if (parent_id
&& parent_id
!= 0xffffffff)
685 SIGTYPE (*oldsig
)() = (int) signal (SIGINT
, SIG_IGN
);
686 int status
= LIB$
ATTACH (&parent_id
) & 1;
687 signal (SIGINT
, oldsig
);
696 d_prompt
.l
= sizeof ("Emacs: "); /* Our special prompt */
697 d_prompt
.a
= "Emacs: "; /* Just a reminder */
698 LIB$
SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt
, 0);
703 #if defined (SIGTSTP) && !defined (MSDOS)
706 int pgrp
= EMACS_GETPGRP (0);
707 EMACS_KILLPG (pgrp
, SIGTSTP
);
710 #else /* No SIGTSTP */
711 #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
712 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
713 kill (getpid (), SIGQUIT
);
715 #else /* No SIGTSTP or USG_JOBCTRL */
717 /* On a system where suspending is not implemented,
718 instead fork a subshell and let it talk directly to the terminal
722 #endif /* no USG_JOBCTRL */
723 #endif /* no SIGTSTP */
727 /* Fork a subshell. */
733 error ("Can't spawn subshell");
736 #ifdef DOS_NT /* Demacs 1.1.2 91/10/20 Manabu Higashida */
738 char oldwd
[MAXPATHLEN
+1]; /* Fixed length is safe on MSDOS. */
741 struct save_signal saved_handlers
[5];
743 unsigned char *str
= 0;
746 saved_handlers
[0].code
= SIGINT
;
747 saved_handlers
[1].code
= SIGQUIT
;
748 saved_handlers
[2].code
= SIGTERM
;
750 saved_handlers
[3].code
= SIGIO
;
751 saved_handlers
[4].code
= 0;
753 saved_handlers
[3].code
= 0;
756 /* Mentioning current_buffer->buffer would mean including buffer.h,
757 which somehow wedges the hp compiler. So instead... */
759 dir
= intern ("default-directory");
760 if (NILP (Fboundp (dir
)))
762 dir
= Fsymbol_value (dir
);
766 dir
= expand_and_dir_to_file (Funhandled_file_name_directory (dir
), Qnil
);
767 str
= (unsigned char *) alloca (XSTRING (dir
)->size
+ 2);
768 len
= XSTRING (dir
)->size
;
769 bcopy (XSTRING (dir
)->data
, str
, len
);
770 if (str
[len
- 1] != '/') str
[len
++] = '/';
777 save_signal_handlers (saved_handlers
);
778 synch_process_alive
= 1;
779 #endif /* __DJGPP__ > 1 */
783 error ("Can't spawn subshell");
790 #ifdef DOS_NT /* MW, Aug 1993 */
793 sh
= (char *) egetenv ("SUSPEND"); /* KFS, 1994-12-14 */
796 sh
= (char *) egetenv ("SHELL");
800 /* Use our buffer's default directory for the subshell. */
802 chdir ((char *) str
);
805 close_process_descs (); /* Close Emacs's pipes/ptys */
808 #ifdef SET_EMACS_PRIORITY
810 extern int emacs_priority
;
812 if (emacs_priority
< 0)
813 nice (-emacs_priority
);
817 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
820 #if 0 /* This is also reported if last command executed in subshell failed, KFS */
822 report_file_error ("Can't execute subshell", Fcons (build_string (sh
), Qnil
));
824 #else /* not MSDOS */
826 /* Waits for process completion */
827 pid
= _spawnlp (_P_WAIT
, sh
, sh
, NULL
);
830 write (1, "Can't execute subshell", 22);
831 #else /* not WINDOWSNT */
833 write (1, "Can't execute subshell", 22);
835 #endif /* not WINDOWSNT */
836 #endif /* not MSDOS */
839 /* Do this now if we did not do it before. */
840 #if !defined (MSDOS) || __DJGPP__ == 1
841 save_signal_handlers (saved_handlers
);
842 synch_process_alive
= 1;
846 wait_for_termination (pid
);
848 restore_signal_handlers (saved_handlers
);
849 synch_process_alive
= 0;
851 #endif /* !macintosh */
855 save_signal_handlers (saved_handlers
)
856 struct save_signal
*saved_handlers
;
858 while (saved_handlers
->code
)
860 saved_handlers
->handler
861 = (SIGTYPE (*) P_ ((int))) signal (saved_handlers
->code
, SIG_IGN
);
867 restore_signal_handlers (saved_handlers
)
868 struct save_signal
*saved_handlers
;
870 while (saved_handlers
->code
)
872 signal (saved_handlers
->code
, saved_handlers
->handler
);
886 old_fcntl_flags
= fcntl (fd
, F_GETFL
, 0) & ~FASYNC
;
887 fcntl (fd
, F_SETFL
, old_fcntl_flags
| FASYNC
);
889 interrupts_deferred
= 0;
898 #ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
903 if (read_socket_hook
)
907 sigunblock (sigmask (SIGWINCH
));
909 fcntl (input_fd
, F_SETFL
, old_fcntl_flags
| FASYNC
);
911 interrupts_deferred
= 0;
917 if (read_socket_hook
)
921 sigblock (sigmask (SIGWINCH
));
923 fcntl (input_fd
, F_SETFL
, old_fcntl_flags
);
924 interrupts_deferred
= 1;
927 #else /* no FASYNC */
928 #ifdef STRIDE /* Stride doesn't have FASYNC - use FIOASYNC */
935 if (read_socket_hook
)
938 ioctl (input_fd
, FIOASYNC
, &on
);
939 interrupts_deferred
= 0;
947 if (read_socket_hook
)
950 ioctl (input_fd
, FIOASYNC
, &off
);
951 interrupts_deferred
= 1;
954 #else /* not FASYNC, not STRIDE */
966 if (read_socket_hook
)
970 sigaddset (&st
, SIGIO
);
971 ioctl (input_fd
, FIOASYNC
, &on
);
972 interrupts_deferred
= 0;
973 sigprocmask (SIG_UNBLOCK
, &st
, (sigset_t
*)0);
981 if (read_socket_hook
)
984 ioctl (input_fd
, FIOASYNC
, &off
);
985 interrupts_deferred
= 1;
993 if (read_socket_hook
)
996 croak ("request_sigio");
1002 if (read_socket_hook
)
1005 croak ("unrequest_sigio");
1011 #endif /* F_SETFL */
1013 /* Saving and restoring the process group of Emacs's terminal. */
1017 /* The process group of which Emacs was a member when it initially
1020 If Emacs was in its own process group (i.e. inherited_pgroup ==
1021 getpid ()), then we know we're running under a shell with job
1022 control (Emacs would never be run as part of a pipeline).
1025 If Emacs was not in its own process group, then we know we're
1026 running under a shell (or a caller) that doesn't know how to
1027 separate itself from Emacs (like sh). Emacs must be in its own
1028 process group in order to receive SIGIO correctly. In this
1029 situation, we put ourselves in our own pgroup, forcibly set the
1030 tty's pgroup to our pgroup, and make sure to restore and reinstate
1031 the tty's pgroup just like any other terminal setting. If
1032 inherited_group was not the tty's pgroup, then we'll get a
1033 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
1034 it goes foreground in the future, which is what should happen. */
1035 int inherited_pgroup
;
1037 /* Split off the foreground process group to Emacs alone.
1038 When we are in the foreground, but not started in our own process
1039 group, redirect the TTY to point to our own process group. We need
1040 to be in our own process group to receive SIGIO properly. */
1042 narrow_foreground_group ()
1046 setpgrp (0, inherited_pgroup
);
1047 if (inherited_pgroup
!= me
)
1048 EMACS_SET_TTY_PGRP (input_fd
, &me
);
1052 /* Set the tty to our original foreground group. */
1054 widen_foreground_group ()
1056 if (inherited_pgroup
!= getpid ())
1057 EMACS_SET_TTY_PGRP (input_fd
, &inherited_pgroup
);
1058 setpgrp (0, inherited_pgroup
);
1061 #endif /* BSD_PGRPS */
1063 /* Getting and setting emacs_tty structures. */
1065 /* Set *TC to the parameters associated with the terminal FD.
1066 Return zero if all's well, or -1 if we ran into an error we
1067 couldn't deal with. */
1069 emacs_get_tty (fd
, settings
)
1071 struct emacs_tty
*settings
;
1073 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
1075 /* We have those nifty POSIX tcmumbleattr functions. */
1076 bzero (&settings
->main
, sizeof (settings
->main
));
1077 if (tcgetattr (fd
, &settings
->main
) < 0)
1082 /* The SYSV-style interface? */
1083 if (ioctl (fd
, TCGETA
, &settings
->main
) < 0)
1088 /* Vehemently Monstrous System? :-) */
1089 if (! (SYS$
QIOW (0, fd
, IO$_SENSEMODE
, settings
, 0, 0,
1090 &settings
->main
.class, 12, 0, 0, 0, 0)
1096 /* I give up - I hope you have the BSD ioctls. */
1097 if (ioctl (fd
, TIOCGETP
, &settings
->main
) < 0)
1099 #endif /* not DOS_NT */
1104 /* Suivant - Do we have to get struct ltchars data? */
1106 if (ioctl (fd
, TIOCGLTC
, &settings
->ltchars
) < 0)
1110 /* How about a struct tchars and a wordful of lmode bits? */
1112 if (ioctl (fd
, TIOCGETC
, &settings
->tchars
) < 0
1113 || ioctl (fd
, TIOCLGET
, &settings
->lmode
) < 0)
1117 /* We have survived the tempest. */
1122 /* Set the parameters of the tty on FD according to the contents of
1123 *SETTINGS. If FLUSHP is non-zero, we discard input.
1124 Return 0 if all went well, and -1 if anything failed. */
1127 emacs_set_tty (fd
, settings
, flushp
)
1129 struct emacs_tty
*settings
;
1132 /* Set the primary parameters - baud rate, character size, etcetera. */
1135 /* We have those nifty POSIX tcmumbleattr functions.
1136 William J. Smith <wjs@wiis.wang.com> writes:
1137 "POSIX 1003.1 defines tcsetattr to return success if it was
1138 able to perform any of the requested actions, even if some
1139 of the requested actions could not be performed.
1140 We must read settings back to ensure tty setup properly.
1141 AIX requires this to keep tty from hanging occasionally." */
1142 /* This make sure that we don't loop indefinitely in here. */
1143 for (i
= 0 ; i
< 10 ; i
++)
1144 if (tcsetattr (fd
, flushp
? TCSAFLUSH
: TCSADRAIN
, &settings
->main
) < 0)
1155 bzero (&new, sizeof (new));
1156 /* Get the current settings, and see if they're what we asked for. */
1157 tcgetattr (fd
, &new);
1158 /* We cannot use memcmp on the whole structure here because under
1159 * aix386 the termios structure has some reserved field that may
1162 if ( new.c_iflag
== settings
->main
.c_iflag
1163 && new.c_oflag
== settings
->main
.c_oflag
1164 && new.c_cflag
== settings
->main
.c_cflag
1165 && new.c_lflag
== settings
->main
.c_lflag
1166 && memcmp (new.c_cc
, settings
->main
.c_cc
, NCCS
) == 0)
1174 /* The SYSV-style interface? */
1175 if (ioctl (fd
, flushp
? TCSETAF
: TCSETAW
, &settings
->main
) < 0)
1180 /* Vehemently Monstrous System? :-) */
1181 if (! (SYS$
QIOW (0, fd
, IO$_SETMODE
, &input_iosb
, 0, 0,
1182 &settings
->main
.class, 12, 0, 0, 0, 0)
1188 /* I give up - I hope you have the BSD ioctls. */
1189 if (ioctl (fd
, (flushp
) ? TIOCSETP
: TIOCSETN
, &settings
->main
) < 0)
1191 #endif /* not DOS_NT */
1197 /* Suivant - Do we have to get struct ltchars data? */
1199 if (ioctl (fd
, TIOCSLTC
, &settings
->ltchars
) < 0)
1203 /* How about a struct tchars and a wordful of lmode bits? */
1205 if (ioctl (fd
, TIOCSETC
, &settings
->tchars
) < 0
1206 || ioctl (fd
, TIOCLSET
, &settings
->lmode
) < 0)
1210 /* We have survived the tempest. */
1215 /* The initial tty mode bits */
1216 struct emacs_tty old_tty
;
1218 /* 1 if we have been through init_sys_modes. */
1221 /* 1 if outer tty status has been recorded. */
1225 /* BSD 4.1 needs to keep track of the lmode bits in order to start
1230 #ifndef F_SETOWN_BUG
1232 int old_fcntl_owner
;
1233 #endif /* F_SETOWN */
1234 #endif /* F_SETOWN_BUG */
1236 /* This may also be defined in stdio,
1237 but if so, this does no harm,
1238 and using the same name avoids wasting the other one's space. */
1241 extern char *_sobuf
;
1243 #if defined (USG) || defined (DGUX)
1244 unsigned char _sobuf
[BUFSIZ
+8];
1246 char _sobuf
[BUFSIZ
];
1251 static struct ltchars new_ltchars
= {-1,-1,-1,-1,-1,-1};
1254 static struct tchars new_tchars
= {-1,-1,-1,-1,-1,-1};
1260 struct emacs_tty tty
;
1263 Vwindow_system
= intern ("mac");
1264 Vwindow_system_version
= make_number (1);
1266 /* cus-start.el complains if delete-exited-processes and x-bitmap-file-path not defined */
1267 #ifndef subprocesses
1268 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
1269 "*Non-nil means delete processes immediately when they exit.\n\
1270 nil means don't delete them until `list-processes' is run.");
1271 delete_exited_processes
= 0;
1274 #ifndef HAVE_X_WINDOWS
1275 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path
,
1276 "List of directories to search for bitmap files for X.");
1277 Vx_bitmap_file_path
= decode_env_path ((char *) 0, ".");
1280 #endif /* not macintosh */
1284 static int oob_chars
[2] = {0, 1 << 7}; /* catch C-g's */
1285 extern int (*interrupt_signal
) ();
1289 Vtty_erase_char
= Qnil
;
1296 input_ef
= get_kbd_event_flag ();
1297 /* LIB$GET_EF (&input_ef); */
1298 SYS$
CLREF (input_ef
);
1299 waiting_for_ast
= 0;
1301 timer_ef
= get_timer_event_flag ();
1302 /* LIB$GET_EF (&timer_ef); */
1303 SYS$
CLREF (timer_ef
);
1307 LIB$
GET_EF (&process_ef
);
1308 SYS$
CLREF (process_ef
);
1310 if (input_ef
/ 32 != process_ef
/ 32)
1311 croak ("Input and process event flags in different clusters.");
1313 if (input_ef
/ 32 != timer_ef
/ 32)
1314 croak ("Input and timer event flags in different clusters.");
1316 input_eflist
= ((unsigned) 1 << (input_ef
% 32)) |
1317 ((unsigned) 1 << (process_ef
% 32));
1319 timer_eflist
= ((unsigned) 1 << (input_ef
% 32)) |
1320 ((unsigned) 1 << (timer_ef
% 32));
1322 sys_access_reinit ();
1324 #endif /* not VMS */
1327 if (! read_socket_hook
&& EQ (Vwindow_system
, Qnil
))
1328 narrow_foreground_group ();
1331 #ifdef HAVE_WINDOW_SYSTEM
1332 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1333 needs the initialization code below. */
1334 if (!read_socket_hook
&& EQ (Vwindow_system
, Qnil
))
1337 EMACS_GET_TTY (input_fd
, &old_tty
);
1343 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
1344 XSETINT (Vtty_erase_char
, old_tty
.main
.c_cc
[VERASE
]);
1347 /* This allows meta to be sent on 8th bit. */
1348 tty
.main
.c_iflag
&= ~INPCK
; /* don't check input for parity */
1350 tty
.main
.c_iflag
|= (IGNBRK
); /* Ignore break condition */
1351 tty
.main
.c_iflag
&= ~ICRNL
; /* Disable map of CR to NL on input */
1352 #ifdef INLCR /* I'm just being cautious,
1353 since I can't check how widespread INLCR is--rms. */
1354 tty
.main
.c_iflag
&= ~INLCR
; /* Disable map of NL to CR on input */
1357 tty
.main
.c_iflag
&= ~ISTRIP
; /* don't strip 8th bit on input */
1359 tty
.main
.c_lflag
&= ~ECHO
; /* Disable echo */
1360 tty
.main
.c_lflag
&= ~ICANON
; /* Disable erase/kill processing */
1362 tty
.main
.c_lflag
&= ~IEXTEN
; /* Disable other editing characters. */
1364 tty
.main
.c_lflag
|= ISIG
; /* Enable signals */
1367 tty
.main
.c_iflag
|= IXON
; /* Enable start/stop output control */
1369 tty
.main
.c_iflag
&= ~IXANY
;
1373 tty
.main
.c_iflag
&= ~IXON
; /* Disable start/stop output control */
1374 tty
.main
.c_oflag
&= ~ONLCR
; /* Disable map of NL to CR-NL
1376 tty
.main
.c_oflag
&= ~TAB3
; /* Disable tab expansion */
1380 tty
.main
.c_cflag
|= CS8
; /* allow 8th bit on input */
1381 tty
.main
.c_cflag
&= ~PARENB
;/* Don't check parity */
1384 tty
.main
.c_cc
[VINTR
] = quit_char
; /* C-g (usually) gives SIGINT */
1385 /* Set up C-g for both SIGQUIT and SIGINT.
1386 We don't know which we will get, but we handle both alike
1387 so which one it really gives us does not matter. */
1388 tty
.main
.c_cc
[VQUIT
] = quit_char
;
1389 tty
.main
.c_cc
[VMIN
] = 1; /* Input should wait for at least 1 char */
1390 tty
.main
.c_cc
[VTIME
] = 0; /* no matter how long that takes. */
1392 tty
.main
.c_cc
[VSWTCH
] = CDISABLE
; /* Turn off shell layering use
1396 #if defined (mips) || defined (HAVE_TCATTR)
1398 tty
.main
.c_cc
[VSUSP
] = CDISABLE
; /* Turn off mips handling of C-z. */
1401 tty
.main
.c_cc
[V_DSUSP
] = CDISABLE
; /* Turn off mips handling of C-y. */
1402 #endif /* V_DSUSP */
1403 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1404 tty
.main
.c_cc
[VDSUSP
] = CDISABLE
;
1407 tty
.main
.c_cc
[VLNEXT
] = CDISABLE
;
1410 tty
.main
.c_cc
[VREPRINT
] = CDISABLE
;
1411 #endif /* VREPRINT */
1413 tty
.main
.c_cc
[VWERASE
] = CDISABLE
;
1414 #endif /* VWERASE */
1416 tty
.main
.c_cc
[VDISCARD
] = CDISABLE
;
1417 #endif /* VDISCARD */
1422 tty
.main
.c_cc
[VSTART
] = '\021';
1425 tty
.main
.c_cc
[VSTOP
] = '\023';
1431 tty
.main
.c_cc
[VSTART
] = CDISABLE
;
1434 tty
.main
.c_cc
[VSTOP
] = CDISABLE
;
1437 #endif /* mips or HAVE_TCATTR */
1439 #ifdef SET_LINE_DISCIPLINE
1440 /* Need to explicitly request TERMIODISC line discipline or
1441 Ultrix's termios does not work correctly. */
1442 tty
.main
.c_line
= SET_LINE_DISCIPLINE
;
1446 /* AIX enhanced edit loses NULs, so disable it. */
1447 tty
.main
.c_line
= 0;
1448 tty
.main
.c_iflag
&= ~ASCEDIT
;
1450 tty
.main
.c_cc
[VSTRT
] = 255;
1451 tty
.main
.c_cc
[VSTOP
] = 255;
1452 tty
.main
.c_cc
[VSUSP
] = 255;
1453 tty
.main
.c_cc
[VDSUSP
] = 255;
1454 #endif /* IBMR2AIX */
1458 tty
.main
.c_cc
[VSTART
] = '\021';
1461 tty
.main
.c_cc
[VSTOP
] = '\023';
1464 /* Also, PTY overloads NUL and BREAK.
1465 don't ignore break, but don't signal either, so it looks like NUL.
1466 This really serves a purpose only if running in an XTERM window
1467 or via TELNET or the like, but does no harm elsewhere. */
1468 tty
.main
.c_iflag
&= ~IGNBRK
;
1469 tty
.main
.c_iflag
&= ~BRKINT
;
1471 #else /* if not HAVE_TERMIO */
1473 tty
.main
.tt_char
|= TT$M_NOECHO
;
1475 tty
.main
.tt_char
|= TT$M_EIGHTBIT
;
1477 tty
.main
.tt_char
|= TT$M_TTSYNC
;
1479 tty
.main
.tt_char
&= ~TT$M_TTSYNC
;
1480 tty
.main
.tt2_char
|= TT2$M_PASTHRU
| TT2$M_XON
;
1481 #else /* not VMS (BSD, that is) */
1483 XSETINT (Vtty_erase_char
, tty
.main
.sg_erase
);
1484 tty
.main
.sg_flags
&= ~(ECHO
| CRMOD
| XTABS
);
1486 tty
.main
.sg_flags
|= ANYP
;
1487 tty
.main
.sg_flags
|= interrupt_input
? RAW
: CBREAK
;
1488 #endif /* not DOS_NT */
1489 #endif /* not VMS (BSD, that is) */
1490 #endif /* not HAVE_TERMIO */
1492 /* If going to use CBREAK mode, we must request C-g to interrupt
1493 and turn off start and stop chars, etc. If not going to use
1494 CBREAK mode, do this anyway so as to turn off local flow
1495 control for user coming over network on 4.2; in this case,
1496 only t_stopc and t_startc really matter. */
1499 /* Note: if not using CBREAK mode, it makes no difference how we
1501 tty
.tchars
= new_tchars
;
1502 tty
.tchars
.t_intrc
= quit_char
;
1505 tty
.tchars
.t_startc
= '\021';
1506 tty
.tchars
.t_stopc
= '\023';
1509 tty
.lmode
= LDECCTQ
| LLITOUT
| LPASS8
| LNOFLSH
| old_tty
.lmode
;
1511 /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
1512 anything, and leaving it in breaks the meta key. Go figure. */
1513 tty
.lmode
&= ~LLITOUT
;
1520 #endif /* HAVE_TCHARS */
1521 #endif /* not HAVE_TERMIO */
1524 tty
.ltchars
= new_ltchars
;
1525 #endif /* HAVE_LTCHARS */
1526 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
1528 internal_terminal_init ();
1532 EMACS_SET_TTY (input_fd
, &tty
, 0);
1534 /* This code added to insure that, if flow-control is not to be used,
1535 we have an unlocked terminal at the start. */
1538 if (!flow_control
) ioctl (input_fd
, TCXONC
, 1);
1542 if (!flow_control
) ioctl (input_fd
, TIOCSTART
, 0);
1546 #if defined (HAVE_TERMIOS) || defined (HPUX9)
1548 if (!flow_control
) tcflow (input_fd
, TCOON
);
1556 /* IBM's HFT device usually thinks a ^J should be LF/CR. We need it
1557 to be only LF. This is the way that is done. */
1560 if (ioctl (1, HFTGETID
, &tty
) != -1)
1561 write (1, "\033[20l", 5);
1567 /* Appears to do nothing when in PASTHRU mode.
1568 SYS$QIOW (0, input_fd, IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
1569 interrupt_signal, oob_chars, 0, 0, 0, 0);
1571 queue_kbd_input (0);
1576 #ifndef F_SETOWN_BUG
1577 #ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
1579 && ! read_socket_hook
&& EQ (Vwindow_system
, Qnil
))
1581 old_fcntl_owner
= fcntl (input_fd
, F_GETOWN
, 0);
1582 fcntl (input_fd
, F_SETOWN
, getpid ());
1583 init_sigio (input_fd
);
1585 #endif /* F_GETOWN */
1586 #endif /* F_SETOWN_BUG */
1587 #endif /* F_SETFL */
1590 if (interrupt_input
)
1591 init_sigio (input_fd
);
1594 #ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1598 /* This symbol is defined on recent USG systems.
1599 Someone says without this call USG won't really buffer the file
1600 even with a call to setbuf. */
1601 setvbuf (stdout
, (char *) _sobuf
, _IOFBF
, sizeof _sobuf
);
1603 setbuf (stdout
, (char *) _sobuf
);
1605 #ifdef HAVE_WINDOW_SYSTEM
1606 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1607 needs the initialization code below. */
1608 if (EQ (Vwindow_system
, Qnil
)
1610 /* When running in tty mode on NT/Win95, we have a read_socket
1611 hook, but still need the rest of the initialization code below. */
1612 && (! read_socket_hook
)
1616 set_terminal_modes ();
1619 && FRAMEP (Vterminal_frame
)
1620 && FRAME_TERMCAP_P (XFRAME (Vterminal_frame
)))
1621 init_frame_faces (XFRAME (Vterminal_frame
));
1623 if (term_initted
&& no_redraw_on_reenter
)
1625 if (display_completed
)
1626 direct_output_forward_char (0);
1631 if (FRAMEP (Vterminal_frame
))
1632 FRAME_GARBAGED_P (XFRAME (Vterminal_frame
)) = 1;
1638 /* Return nonzero if safe to use tabs in output.
1639 At the time this is called, init_sys_modes has not been done yet. */
1644 struct emacs_tty tty
;
1646 EMACS_GET_TTY (input_fd
, &tty
);
1647 return EMACS_TTY_TABS_OK (&tty
);
1650 /* Get terminal size from system.
1651 Store number of lines into *HEIGHTP and width into *WIDTHP.
1652 We store 0 if there's no valid information. */
1655 get_frame_size (widthp
, heightp
)
1656 int *widthp
, *heightp
;
1662 struct winsize size
;
1664 if (ioctl (input_fd
, TIOCGWINSZ
, &size
) == -1)
1665 *widthp
= *heightp
= 0;
1668 *widthp
= size
.ws_col
;
1669 *heightp
= size
.ws_row
;
1675 /* SunOS - style. */
1676 struct ttysize size
;
1678 if (ioctl (input_fd
, TIOCGSIZE
, &size
) == -1)
1679 *widthp
= *heightp
= 0;
1682 *widthp
= size
.ts_cols
;
1683 *heightp
= size
.ts_lines
;
1689 struct sensemode tty
;
1691 SYS$
QIOW (0, input_fd
, IO$_SENSEMODE
, &tty
, 0, 0,
1692 &tty
.class, 12, 0, 0, 0, 0);
1693 *widthp
= tty
.scr_wid
;
1694 *heightp
= tty
.scr_len
;
1698 *widthp
= ScreenCols ();
1699 *heightp
= ScreenRows ();
1700 #else /* system doesn't know size */
1705 #endif /* not VMS */
1706 #endif /* not SunOS-style */
1707 #endif /* not BSD-style */
1710 /* Set the logical window size associated with descriptor FD
1711 to HEIGHT and WIDTH. This is used mainly with ptys. */
1714 set_window_size (fd
, height
, width
)
1715 int fd
, height
, width
;
1720 struct winsize size
;
1721 size
.ws_row
= height
;
1722 size
.ws_col
= width
;
1724 if (ioctl (fd
, TIOCSWINSZ
, &size
) == -1)
1725 return 0; /* error */
1732 /* SunOS - style. */
1733 struct ttysize size
;
1734 size
.ts_lines
= height
;
1735 size
.ts_cols
= width
;
1737 if (ioctl (fd
, TIOCGSIZE
, &size
) == -1)
1743 #endif /* not SunOS-style */
1744 #endif /* not BSD-style */
1748 /* Prepare the terminal for exiting Emacs; move the cursor to the
1749 bottom of the frame, turn off interrupt-driven I/O, etc. */
1762 #ifdef HAVE_WINDOW_SYSTEM
1763 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1764 needs the clean-up code below. */
1765 if (!EQ (Vwindow_system
, Qnil
)
1767 /* When running in tty mode on NT/Win95, we have a read_socket
1768 hook, but still need the rest of the clean-up code below. */
1774 sf
= SELECTED_FRAME ();
1775 cursor_to (FRAME_HEIGHT (sf
) - 1, 0);
1776 clear_end_of_line (FRAME_WIDTH (sf
));
1777 /* clear_end_of_line may move the cursor */
1778 cursor_to (FRAME_HEIGHT (sf
) - 1, 0);
1779 #if defined (IBMR2AIX) && defined (AIXHFT)
1781 /* HFT devices normally use ^J as a LF/CR. We forced it to
1782 do the LF only. Now, we need to reset it. */
1785 if (ioctl (1, HFTGETID
, &tty
) != -1)
1786 write (1, "\033[20h", 5);
1790 reset_terminal_modes ();
1794 /* Avoid possible loss of output when changing terminal modes. */
1795 fsync (fileno (stdout
));
1800 #ifndef F_SETOWN_BUG
1801 #ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
1802 if (interrupt_input
)
1805 fcntl (input_fd
, F_SETOWN
, old_fcntl_owner
);
1807 #endif /* F_SETOWN */
1808 #endif /* F_SETOWN_BUG */
1810 fcntl (input_fd
, F_SETFL
, fcntl (input_fd
, F_GETFL
, 0) & ~O_NDELAY
);
1812 #endif /* F_SETFL */
1814 if (interrupt_input
)
1819 while (EMACS_SET_TTY (input_fd
, &old_tty
, 0) < 0 && errno
== EINTR
)
1822 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1826 #ifdef SET_LINE_DISCIPLINE
1827 /* Ultrix's termios *ignores* any line discipline except TERMIODISC.
1828 A different old line discipline is therefore not restored, yet.
1829 Restore the old line discipline by hand. */
1830 ioctl (0, TIOCSETD
, &old_tty
.main
.c_line
);
1838 widen_foreground_group ();
1844 /* Set up the proper status flags for use of a pty. */
1850 /* I'm told that TOICREMOTE does not mean control chars
1851 "can't be sent" but rather that they don't have
1852 input-editing or signaling effects.
1853 That should be good, because we have other ways
1854 to do those things in Emacs.
1855 However, telnet mode seems not to work on 4.2.
1856 So TIOCREMOTE is turned off now. */
1858 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1859 will hang. In particular, the "timeout" feature (which
1860 causes a read to return if there is no data available)
1861 does this. Also it is known that telnet mode will hang
1862 in such a way that Emacs must be stopped (perhaps this
1863 is the same problem).
1865 If TIOCREMOTE is turned off, then there is a bug in
1866 hp-ux which sometimes loses data. Apparently the
1867 code which blocks the master process when the internal
1868 buffer fills up does not work. Other than this,
1869 though, everything else seems to work fine.
1871 Since the latter lossage is more benign, we may as well
1872 lose that way. -- cph */
1877 ioctl (fd
, FIONBIO
, &on
);
1882 /* On AIX, the parent gets SIGHUP when a pty attached child dies. So, we */
1883 /* ignore SIGHUP once we've started a child on a pty. Note that this may */
1884 /* cause EMACS not to die when it should, i.e., when its own controlling */
1885 /* tty goes away. I've complained to the AIX developers, and they may */
1886 /* change this behavior, but I'm not going to hold my breath. */
1887 signal (SIGHUP
, SIG_IGN
);
1890 #endif /* HAVE_PTYS */
1894 /* Assigning an input channel is done at the start of Emacs execution.
1895 This is called each time Emacs is resumed, also, but does nothing
1896 because input_chain is no longer zero. */
1905 status
= SYS$
ASSIGN (&input_dsc
, &input_fd
, 0, 0);
1911 /* Deassigning the input channel is done before exiting. */
1916 return SYS$
DASSGN (input_fd
);
1921 /* Request reading one character into the keyboard buffer.
1922 This is done as soon as the buffer becomes empty. */
1928 extern kbd_input_ast ();
1930 waiting_for_ast
= 0;
1932 status
= SYS$
QIO (0, input_fd
, IO$_READVBLK
,
1933 &input_iosb
, kbd_input_ast
, 1,
1934 &input_buffer
, 1, 0, terminator_mask
, 0, 0);
1939 /* Ast routine that is called when keyboard input comes in
1940 in accord with the SYS$QIO above. */
1945 register int c
= -1;
1946 int old_errno
= errno
;
1947 extern EMACS_TIME
*input_available_clear_time
;
1949 if (waiting_for_ast
)
1950 SYS$
SETEF (input_ef
);
1951 waiting_for_ast
= 0;
1954 if (input_count
== 25)
1956 printf ("Ast # %d,", input_count
);
1957 printf (" iosb = %x, %x, %x, %x",
1958 input_iosb
.offset
, input_iosb
.status
, input_iosb
.termlen
,
1961 if (input_iosb
.offset
)
1965 printf (", char = 0%o", c
);
1977 struct input_event e
;
1978 e
.kind
= ascii_keystroke
;
1979 XSETINT (e
.code
, c
);
1980 e
.frame_or_window
= selected_frame
;
1981 kbd_buffer_store_event (&e
);
1983 if (input_available_clear_time
)
1984 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
1988 /* Wait until there is something in kbd_buffer. */
1991 wait_for_kbd_input ()
1993 extern int have_process_input
, process_exited
;
1995 /* If already something, avoid doing system calls. */
1996 if (detect_input_pending ())
2000 /* Clear a flag, and tell ast routine above to set it. */
2001 SYS$
CLREF (input_ef
);
2002 waiting_for_ast
= 1;
2003 /* Check for timing error: ast happened while we were doing that. */
2004 if (!detect_input_pending ())
2006 /* No timing error: wait for flag to be set. */
2007 set_waiting_for_input (0);
2008 SYS$
WFLOR (input_ef
, input_eflist
);
2009 clear_waiting_for_input (0);
2010 if (!detect_input_pending ())
2011 /* Check for subprocess input availability */
2013 int dsp
= have_process_input
|| process_exited
;
2015 SYS$
CLREF (process_ef
);
2016 if (have_process_input
)
2017 process_command_input ();
2022 update_mode_lines
++;
2023 prepare_menu_bars ();
2024 redisplay_preserve_echo_area ();
2028 waiting_for_ast
= 0;
2031 /* Get rid of any pending QIO, when we are about to suspend
2032 or when we want to throw away pending input.
2033 We wait for a positive sign that the AST routine has run
2034 and therefore there is no I/O request queued when we return.
2035 SYS$SETAST is used to avoid a timing error. */
2041 printf ("At end_kbd_input.\n");
2045 if (LIB$
AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
2047 SYS$
CANCEL (input_fd
);
2052 /* Clear a flag, and tell ast routine above to set it. */
2053 SYS$
CLREF (input_ef
);
2054 waiting_for_ast
= 1;
2056 SYS$
CANCEL (input_fd
);
2058 SYS$
WAITFR (input_ef
);
2059 waiting_for_ast
= 0;
2062 /* Wait for either input available or time interval expiry. */
2065 input_wait_timeout (timeval
)
2066 int timeval
; /* Time to wait, in seconds */
2069 static int zero
= 0;
2070 static int large
= -10000000;
2072 LIB$
EMUL (&timeval
, &large
, &zero
, time
); /* Convert to VMS format */
2074 /* If already something, avoid doing system calls. */
2075 if (detect_input_pending ())
2079 /* Clear a flag, and tell ast routine above to set it. */
2080 SYS$
CLREF (input_ef
);
2081 waiting_for_ast
= 1;
2082 /* Check for timing error: ast happened while we were doing that. */
2083 if (!detect_input_pending ())
2085 /* No timing error: wait for flag to be set. */
2087 if (SYS$
SETIMR (timer_ef
, time
, 0, 1) & 1) /* Set timer */
2088 SYS$
WFLOR (timer_ef
, timer_eflist
); /* Wait for timer expiry or input */
2090 waiting_for_ast
= 0;
2093 /* The standard `sleep' routine works some other way
2094 and it stops working if you have ever quit out of it.
2095 This one continues to work. */
2101 static int zero
= 0;
2102 static int large
= -10000000;
2104 LIB$
EMUL (&timeval
, &large
, &zero
, time
); /* Convert to VMS format */
2107 if (SYS$
SETIMR (timer_ef
, time
, 0, 1) & 1) /* Set timer */
2108 SYS$
WAITFR (timer_ef
); /* Wait for timer expiry only */
2126 croak ("request sigio");
2132 croak ("unrequest sigio");
2137 /* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
2142 #ifndef SYSTEM_MALLOC
2149 /* Some systems that cannot dump also cannot implement these. */
2152 * Return the address of the start of the text segment prior to
2153 * doing an unexec. After unexec the return value is undefined.
2154 * See crt0.c for further explanation and _start.
2158 #ifndef HAVE_TEXT_START
2163 return ((char *) TEXT_START
);
2167 return ((char *) csrt
);
2168 #else /* not GOULD */
2169 extern int _start ();
2170 return ((char *) _start
);
2172 #endif /* TEXT_START */
2174 #endif /* not HAVE_TEXT_START */
2177 * Return the address of the start of the data segment prior to
2178 * doing an unexec. After unexec the return value is undefined.
2179 * See crt0.c for further information and definition of data_start.
2181 * Apparently, on BSD systems this is etext at startup. On
2182 * USG systems (swapping) this is highly mmu dependent and
2183 * is also dependent on whether or not the program is running
2184 * with shared text. Generally there is a (possibly large)
2185 * gap between end of text and start of data with shared text.
2187 * On Uniplus+ systems with shared text, data starts at a
2188 * fixed address. Each port (from a given oem) is generally
2189 * different, and the specific value of the start of data can
2190 * be obtained via the UniPlus+ specific "uvar" system call,
2191 * however the method outlined in crt0.c seems to be more portable.
2193 * Probably what will have to happen when a USG unexec is available,
2194 * at least on UniPlus, is temacs will have to be made unshared so
2195 * that text and data are contiguous. Then once loadup is complete,
2196 * unexec will produce a shared executable where the data can be
2197 * at the normal shared text boundary and the startofdata variable
2198 * will be patched by unexec to the correct value.
2206 return ((char *) DATA_START
);
2208 #ifdef ORDINARY_LINK
2210 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
2211 * data_start isn't defined. We take the address of environ, which
2212 * is known to live at or near the start of the system crt0.c, and
2213 * we don't sweat the handful of bytes that might lose.
2215 extern char **environ
;
2217 return ((char *) &environ
);
2219 extern int data_start
;
2220 return ((char *) &data_start
);
2221 #endif /* ORDINARY_LINK */
2222 #endif /* DATA_START */
2224 #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
2227 /* Some systems that cannot dump also cannot implement these. */
2230 * Return the address of the end of the text segment prior to
2231 * doing an unexec. After unexec the return value is undefined.
2238 return ((char *) TEXT_END
);
2241 return ((char *) &etext
);
2246 * Return the address of the end of the data segment prior to
2247 * doing an unexec. After unexec the return value is undefined.
2254 return ((char *) DATA_END
);
2257 return ((char *) &edata
);
2261 #endif /* not CANNOT_DUMP */
2263 /* init_system_name sets up the string for the Lisp function
2264 system-name to return. */
2270 extern Lisp_Object Vsystem_name
;
2275 #include <sys/socket.h>
2277 #endif /* HAVE_SOCKETS */
2278 #endif /* not VMS */
2279 #endif /* not BSD4_1 */
2285 Vsystem_name
= build_string (sysname
);
2289 if ((sp
= egetenv ("SYS$NODE")) == 0)
2290 Vsystem_name
= build_string ("vax-vms");
2291 else if ((end
= index (sp
, ':')) == 0)
2292 Vsystem_name
= build_string (sp
);
2294 Vsystem_name
= make_string (sp
, end
- sp
);
2296 #ifndef HAVE_GETHOSTNAME
2299 Vsystem_name
= build_string (uts
.nodename
);
2300 #else /* HAVE_GETHOSTNAME */
2301 unsigned int hostname_size
= 256;
2302 char *hostname
= (char *) alloca (hostname_size
);
2304 /* Try to get the host name; if the buffer is too short, try
2305 again. Apparently, the only indication gethostname gives of
2306 whether the buffer was large enough is the presence or absence
2307 of a '\0' in the string. Eech. */
2310 gethostname (hostname
, hostname_size
- 1);
2311 hostname
[hostname_size
- 1] = '\0';
2313 /* Was the buffer large enough for the '\0'? */
2314 if (strlen (hostname
) < hostname_size
- 1)
2317 hostname_size
<<= 1;
2318 hostname
= (char *) alloca (hostname_size
);
2321 /* Turn the hostname into the official, fully-qualified hostname.
2322 Don't do this if we're going to dump; this can confuse system
2323 libraries on some machines and make the dumped emacs core dump. */
2326 #endif /* not CANNOT_DUMP */
2327 if (! index (hostname
, '.'))
2331 for (count
= 0;; count
++)
2336 hp
= gethostbyname (hostname
);
2338 if (! (hp
== 0 && h_errno
== TRY_AGAIN
))
2343 Fsleep_for (make_number (1), Qnil
);
2347 char *fqdn
= (char *) hp
->h_name
;
2350 if (!index (fqdn
, '.'))
2352 /* We still don't have a fully qualified domain name.
2353 Try to find one in the list of alternate names */
2354 char **alias
= hp
->h_aliases
;
2355 while (*alias
&& !index (*alias
, '.'))
2362 /* Convert the host name to lower case. */
2363 /* Using ctype.h here would introduce a possible locale
2364 dependence that is probably wrong for hostnames. */
2368 if (*p
>= 'A' && *p
<= 'Z')
2375 #endif /* HAVE_SOCKETS */
2376 /* We used to try using getdomainname here,
2377 but NIIBE Yutaka <gniibe@etl.go.jp> says that
2378 getdomainname gets the NIS/YP domain which often is not the same
2379 as in Internet domain name. */
2380 #if 0 /* Turned off because sysinfo is not really likely to return the
2381 correct Internet domain. */
2382 #if (HAVE_SYSINFO && defined (SI_SRPC_DOMAIN))
2383 if (! index (hostname
, '.'))
2385 /* The hostname is not fully qualified. Append the domain name. */
2387 int hostlen
= strlen (hostname
);
2388 int domain_size
= 256;
2392 char *domain
= (char *) alloca (domain_size
+ 1);
2393 char *fqdn
= (char *) alloca (hostlen
+ 1 + domain_size
+ 1);
2394 int sys_domain_size
= sysinfo (SI_SRPC_DOMAIN
, domain
, domain_size
);
2395 if (sys_domain_size
<= 0)
2397 if (domain_size
< sys_domain_size
)
2399 domain_size
= sys_domain_size
;
2402 strcpy (fqdn
, hostname
);
2403 if (domain
[0] == '.')
2404 strcpy (fqdn
+ hostlen
, domain
);
2405 else if (domain
[0] != 0)
2407 fqdn
[hostlen
] = '.';
2408 strcpy (fqdn
+ hostlen
+ 1, domain
);
2414 #endif /* HAVE_SYSINFO && defined (SI_SRPC_DOMAIN) */
2416 Vsystem_name
= build_string (hostname
);
2417 #endif /* HAVE_GETHOSTNAME */
2422 for (p
= XSTRING (Vsystem_name
)->data
; *p
; p
++)
2423 if (*p
== ' ' || *p
== '\t')
2430 #if !defined (HAVE_SELECT) || defined (BROKEN_SELECT_NON_X)
2432 #include "sysselect.h"
2435 #if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
2436 /* Cause explanatory error message at compile time,
2437 since the select emulation is not good enough for X. */
2438 int *x
= &x_windows_lose_if_no_select_system_call
;
2441 /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2442 * Only checks read descriptors.
2444 /* How long to wait between checking fds in select */
2445 #define SELECT_PAUSE 1
2448 /* For longjmp'ing back to read_input_waiting. */
2450 jmp_buf read_alarm_throw
;
2452 /* Nonzero if the alarm signal should throw back to read_input_waiting.
2453 The read_socket_hook function sets this to 1 while it is waiting. */
2455 int read_alarm_should_throw
;
2463 #else /* not BSD4_1 */
2464 signal (SIGALRM
, SIG_IGN
);
2465 #endif /* not BSD4_1 */
2466 if (read_alarm_should_throw
)
2467 longjmp (read_alarm_throw
, 1);
2471 /* Only rfds are checked. */
2473 sys_select (nfds
, rfds
, wfds
, efds
, timeout
)
2475 SELECT_TYPE
*rfds
, *wfds
, *efds
;
2476 EMACS_TIME
*timeout
;
2478 int ravail
= 0, old_alarm
;
2482 extern int proc_buffered_char
[];
2483 #ifndef subprocesses
2484 int process_tick
= 0, update_tick
= 0;
2486 extern int process_tick
, update_tick
;
2488 SIGTYPE (*old_trap
) ();
2491 #if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
2492 /* If we're using X, then the native select will work; we only need the
2493 emulation for non-X usage. */
2494 if (!NILP (Vwindow_system
))
2495 return select (nfds
, rfds
, wfds
, efds
, timeout
);
2497 timeoutval
= timeout
? EMACS_SECS (*timeout
) : 100000;
2498 local_timeout
= &timeoutval
;
2510 /* If we are looking only for the terminal, with no timeout,
2511 just read it and wait -- that's more efficient. */
2512 if (*local_timeout
== 100000 && process_tick
== update_tick
2513 && FD_ISSET (0, &orfds
))
2516 for (fd
= 1; fd
< nfds
; ++fd
)
2517 if (FD_ISSET (fd
, &orfds
))
2519 if (! detect_input_pending ())
2520 read_input_waiting ();
2526 /* Once a second, till the timer expires, check all the flagged read
2527 * descriptors to see if any input is available. If there is some then
2528 * set the corresponding bit in the return copy of rfds.
2532 register int to_check
, fd
;
2536 for (to_check
= nfds
, fd
= 0; --to_check
>= 0; fd
++)
2538 if (FD_ISSET (fd
, &orfds
))
2540 int avail
= 0, status
= 0;
2543 avail
= detect_input_pending (); /* Special keyboard handler */
2547 status
= ioctl (fd
, FIONREAD
, &avail
);
2548 #else /* no FIONREAD */
2549 /* Hoping it will return -1 if nothing available
2550 or 0 if all 0 chars requested are read. */
2551 if (proc_buffered_char
[fd
] >= 0)
2555 avail
= read (fd
, &buf
, 1);
2557 proc_buffered_char
[fd
] = buf
;
2559 #endif /* no FIONREAD */
2561 if (status
>= 0 && avail
> 0)
2569 if (*local_timeout
== 0 || ravail
!= 0 || process_tick
!= update_tick
)
2571 old_alarm
= alarm (0);
2572 old_trap
= signal (SIGALRM
, select_alarm
);
2574 alarm (SELECT_PAUSE
);
2575 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2576 while (select_alarmed
== 0 && *local_timeout
!= 0
2577 && process_tick
== update_tick
)
2579 /* If we are interested in terminal input,
2580 wait by reading the terminal.
2581 That makes instant wakeup for terminal input at least. */
2582 if (FD_ISSET (0, &orfds
))
2584 read_input_waiting ();
2585 if (detect_input_pending ())
2591 (*local_timeout
) -= SELECT_PAUSE
;
2592 /* Reset the old alarm if there was one */
2594 signal (SIGALRM
, old_trap
);
2597 /* Reset or forge an interrupt for the original handler. */
2598 old_alarm
-= SELECT_PAUSE
;
2600 kill (getpid (), SIGALRM
); /* Fake an alarm with the orig' handler */
2604 if (*local_timeout
== 0) /* Stop on timer being cleared */
2609 #endif /* not WINDOWSNT */
2611 /* Read keyboard input into the standard buffer,
2612 waiting for at least one character. */
2614 /* Make all keyboard buffers much bigger when using a window system. */
2615 #ifdef HAVE_WINDOW_SYSTEM
2616 #define BUFFER_SIZE_FACTOR 16
2618 #define BUFFER_SIZE_FACTOR 1
2622 read_input_waiting ()
2624 struct input_event e
;
2626 extern int quit_char
;
2628 if (read_socket_hook
)
2630 struct input_event buf
[256];
2632 read_alarm_should_throw
= 0;
2633 if (! setjmp (read_alarm_throw
))
2634 nread
= (*read_socket_hook
) (0, buf
, 256, 1);
2638 /* Scan the chars for C-g and store them in kbd_buffer. */
2639 for (i
= 0; i
< nread
; i
++)
2641 kbd_buffer_store_event (&buf
[i
]);
2642 /* Don't look at input that follows a C-g too closely.
2643 This reduces lossage due to autorepeat on C-g. */
2644 if (buf
[i
].kind
== ascii_keystroke
2645 && buf
[i
].code
== quit_char
)
2652 nread
= read (fileno (stdin
), buf
, 1);
2654 /* Scan the chars for C-g and store them in kbd_buffer. */
2655 e
.kind
= ascii_keystroke
;
2656 e
.frame_or_window
= selected_frame
;
2658 for (i
= 0; i
< nread
; i
++)
2660 /* Convert chars > 0177 to meta events if desired.
2661 We do this under the same conditions that read_avail_input does. */
2662 if (read_socket_hook
== 0)
2664 /* If the user says she has a meta key, then believe her. */
2665 if (meta_key
== 1 && (buf
[i
] & 0x80))
2666 e
.modifiers
= meta_modifier
;
2671 XSETINT (e
.code
, buf
[i
]);
2672 kbd_buffer_store_event (&e
);
2673 /* Don't look at input that follows a C-g too closely.
2674 This reduces lossage due to autorepeat on C-g. */
2675 if (buf
[i
] == quit_char
)
2681 #endif /* not HAVE_SELECT */
2682 #endif /* not VMS */
2683 #endif /* not MSDOS */
2692 lmode
= LINTRUP
| lmode
;
2693 ioctl (fd
, TIOCLSET
, &lmode
);
2701 lmode
= ~LINTRUP
& lmode
;
2702 ioctl (0, TIOCLSET
, &lmode
);
2710 interrupts_deferred
= 0;
2718 interrupts_deferred
= 1;
2721 /* still inside #ifdef BSD4_1 */
2724 int sigheld
; /* Mask of held signals */
2730 sigheld
|= sigbit (signum
);
2738 sigheld
|= sigbit (signum
);
2745 sigheld
&= ~sigbit (signum
);
2750 sigfree () /* Free all held signals */
2753 for (i
= 0; i
< NSIG
; i
++)
2754 if (sigheld
& sigbit (i
))
2762 return 1 << (i
- 1);
2764 #endif /* subprocesses */
2767 /* POSIX signals support - DJB */
2768 /* Anyone with POSIX signals should have ANSI C declarations */
2770 #ifdef POSIX_SIGNALS
2772 sigset_t empty_mask
, full_mask
;
2775 sys_signal (int signal_number
, signal_handler_t action
)
2777 struct sigaction new_action
, old_action
;
2778 sigemptyset (&new_action
.sa_mask
);
2779 new_action
.sa_handler
= action
;
2781 /* Emacs mostly works better with restartable system services. If this
2782 * flag exists, we probably want to turn it on here.
2784 new_action
.sa_flags
= SA_RESTART
;
2786 new_action
.sa_flags
= 0;
2788 sigaction (signal_number
, &new_action
, &old_action
);
2789 return (old_action
.sa_handler
);
2793 /* If we're compiling with GCC, we don't need this function, since it
2794 can be written as a macro. */
2796 sys_sigmask (int sig
)
2799 sigemptyset (&mask
);
2800 sigaddset (&mask
, sig
);
2805 /* I'd like to have these guys return pointers to the mask storage in here,
2806 but there'd be trouble if the code was saving multiple masks. I'll be
2807 safe and pass the structure. It normally won't be more than 2 bytes
2811 sys_sigblock (sigset_t new_mask
)
2814 sigprocmask (SIG_BLOCK
, &new_mask
, &old_mask
);
2819 sys_sigunblock (sigset_t new_mask
)
2822 sigprocmask (SIG_UNBLOCK
, &new_mask
, &old_mask
);
2827 sys_sigsetmask (sigset_t new_mask
)
2830 sigprocmask (SIG_SETMASK
, &new_mask
, &old_mask
);
2834 #endif /* POSIX_SIGNALS */
2836 #if !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED
2837 static char *my_sys_siglist
[NSIG
];
2841 # define sys_siglist my_sys_siglist
2847 #ifdef POSIX_SIGNALS
2848 sigemptyset (&empty_mask
);
2849 sigfillset (&full_mask
);
2852 #if !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED
2856 sys_siglist
[SIGABRT
] = "Aborted";
2859 sys_siglist
[SIGAIO
] = "LAN I/O interrupt";
2862 sys_siglist
[SIGALRM
] = "Alarm clock";
2865 sys_siglist
[SIGBUS
] = "Bus error";
2868 sys_siglist
[SIGCLD
] = "Child status changed";
2871 sys_siglist
[SIGCHLD
] = "Child status changed";
2874 sys_siglist
[SIGCONT
] = "Continued";
2877 sys_siglist
[SIGDANGER
] = "Swap space dangerously low";
2880 sys_siglist
[SIGDGNOTIFY
] = "Notification message in queue";
2883 sys_siglist
[SIGEMT
] = "Emulation trap";
2886 sys_siglist
[SIGFPE
] = "Arithmetic exception";
2889 sys_siglist
[SIGFREEZE
] = "SIGFREEZE";
2892 sys_siglist
[SIGGRANT
] = "Monitor mode granted";
2895 sys_siglist
[SIGHUP
] = "Hangup";
2898 sys_siglist
[SIGILL
] = "Illegal instruction";
2901 sys_siglist
[SIGINT
] = "Interrupt";
2904 sys_siglist
[SIGIO
] = "I/O possible";
2907 sys_siglist
[SIGIOINT
] = "I/O intervention required";
2910 sys_siglist
[SIGIOT
] = "IOT trap";
2913 sys_siglist
[SIGKILL
] = "Killed";
2916 sys_siglist
[SIGLOST
] = "Resource lost";
2919 sys_siglist
[SIGLWP
] = "SIGLWP";
2922 sys_siglist
[SIGMSG
] = "Monitor mode data available";
2925 sys_siglist
[SIGWIND
] = "SIGPHONE";
2928 sys_siglist
[SIGPIPE
] = "Broken pipe";
2931 sys_siglist
[SIGPOLL
] = "Pollable event occurred";
2934 sys_siglist
[SIGPROF
] = "Profiling timer expired";
2937 sys_siglist
[SIGPTY
] = "PTY I/O interrupt";
2940 sys_siglist
[SIGPWR
] = "Power-fail restart";
2943 sys_siglist
[SIGQUIT
] = "Quit";
2946 sys_siglist
[SIGRETRACT
] = "Need to relinguish monitor mode";
2949 sys_siglist
[SIGSAK
] = "Secure attention";
2952 sys_siglist
[SIGSEGV
] = "Segmentation violation";
2955 sys_siglist
[SIGSOUND
] = "Sound completed";
2958 sys_siglist
[SIGSTOP
] = "Stopped (signal)";
2961 sys_siglist
[SIGSTP
] = "Stopped (user)";
2964 sys_siglist
[SIGSYS
] = "Bad argument to system call";
2967 sys_siglist
[SIGTERM
] = "Terminated";
2970 sys_siglist
[SIGTHAW
] = "SIGTHAW";
2973 sys_siglist
[SIGTRAP
] = "Trace/breakpoint trap";
2976 sys_siglist
[SIGTSTP
] = "Stopped (user)";
2979 sys_siglist
[SIGTTIN
] = "Stopped (tty input)";
2982 sys_siglist
[SIGTTOU
] = "Stopped (tty output)";
2985 sys_siglist
[SIGURG
] = "Urgent I/O condition";
2988 sys_siglist
[SIGUSR1
] = "User defined signal 1";
2991 sys_siglist
[SIGUSR2
] = "User defined signal 2";
2994 sys_siglist
[SIGVTALRM
] = "Virtual timer expired";
2997 sys_siglist
[SIGWAITING
] = "Process's LWPs are blocked";
3000 sys_siglist
[SIGWINCH
] = "Window size changed";
3003 sys_siglist
[SIGWIND
] = "SIGWIND";
3006 sys_siglist
[SIGXCPU
] = "CPU time limit exceeded";
3009 sys_siglist
[SIGXFSZ
] = "File size limit exceeded";
3012 #endif /* !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED */
3021 /* Figure out how many bits the system's random number generator uses.
3022 `random' and `lrand48' are assumed to return 31 usable bits.
3023 BSD `rand' returns a 31 bit value but the low order bits are unusable;
3024 so we'll shift it and treat it like the 15-bit USG `rand'. */
3028 # define RAND_BITS 31
3029 # else /* !HAVE_RANDOM */
3030 # ifdef HAVE_LRAND48
3031 # define RAND_BITS 31
3032 # define random lrand48
3033 # else /* !HAVE_LRAND48 */
3034 # define RAND_BITS 15
3035 # if RAND_MAX == 32767
3036 # define random rand
3037 # else /* RAND_MAX != 32767 */
3038 # if RAND_MAX == 2147483647
3039 # define random() (rand () >> 16)
3040 # else /* RAND_MAX != 2147483647 */
3042 # define random rand
3044 # define random() (rand () >> 16)
3046 # endif /* RAND_MAX != 2147483647 */
3047 # endif /* RAND_MAX != 32767 */
3048 # endif /* !HAVE_LRAND48 */
3049 # endif /* !HAVE_RANDOM */
3050 #endif /* !RAND_BITS */
3057 srandom ((unsigned int)arg
);
3059 # ifdef HAVE_LRAND48
3062 srand ((unsigned int)arg
);
3068 * Build a full Emacs-sized word out of whatever we've got.
3069 * This suffices even for a 64-bit architecture with a 15-bit rand.
3074 long val
= random ();
3075 #if VALBITS > RAND_BITS
3076 val
= (val
<< RAND_BITS
) ^ random ();
3077 #if VALBITS > 2*RAND_BITS
3078 val
= (val
<< RAND_BITS
) ^ random ();
3079 #if VALBITS > 3*RAND_BITS
3080 val
= (val
<< RAND_BITS
) ^ random ();
3081 #if VALBITS > 4*RAND_BITS
3082 val
= (val
<< RAND_BITS
) ^ random ();
3083 #endif /* need at least 5 */
3084 #endif /* need at least 4 */
3085 #endif /* need at least 3 */
3086 #endif /* need at least 2 */
3087 return val
& ((1L << VALBITS
) - 1);
3090 #ifdef WRONG_NAME_INSQUE
3103 /* If any place else asks for the TERM variable,
3104 allow it to be overridden with the EMACS_TERM variable
3105 before attempting to translate the logical name TERM. As a last
3106 resort, ask for VAX C's special idea of the TERM variable. */
3113 static char buf
[256];
3114 static struct dsc$descriptor_s equiv
3115 = {sizeof (buf
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, buf
};
3116 static struct dsc$descriptor_s d_name
3117 = {0, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, 0};
3120 if (!strcmp (name
, "TERM"))
3122 val
= (char *) getenv ("EMACS_TERM");
3127 d_name
.dsc$w_length
= strlen (name
);
3128 d_name
.dsc$a_pointer
= name
;
3129 if (LIB$
SYS_TRNLOG (&d_name
, &eqlen
, &equiv
) == 1)
3131 char *str
= (char *) xmalloc (eqlen
+ 1);
3132 bcopy (buf
, str
, eqlen
);
3134 /* This is a storage leak, but a pain to fix. With luck,
3135 no one will ever notice. */
3138 return (char *) getenv (name
);
3143 /* Since VMS doesn't believe in core dumps, the only way to debug this beast is
3144 to force a call on the debugger from within the image. */
3149 LIB$
SIGNAL (SS$_DEBUG
);
3155 #ifdef LINK_CRTL_SHARE
3156 #ifdef SHARABLE_LIB_BUG
3157 /* Variables declared noshare and initialized in sharable libraries
3158 cannot be shared. The VMS linker incorrectly forces you to use a private
3159 version which is uninitialized... If not for this "feature", we
3160 could use the C library definition of sys_nerr and sys_errlist. */
3162 char *sys_errlist
[] =
3166 "no such file or directory",
3168 "interrupted system call",
3170 "no such device or address",
3171 "argument list too long",
3172 "exec format error",
3175 "no more processes",
3176 "not enough memory",
3177 "permission denied",
3179 "block device required",
3180 "mount devices busy",
3182 "cross-device link",
3187 "file table overflow",
3188 "too many open files",
3192 "no space left on device",
3194 "read-only file system",
3200 "vax/vms specific error code nontranslatable error"
3202 #endif /* SHARABLE_LIB_BUG */
3203 #endif /* LINK_CRTL_SHARE */
3206 #ifndef HAVE_STRERROR
3212 extern char *sys_errlist
[];
3213 extern int sys_nerr
;
3215 if (errnum
>= 0 && errnum
< sys_nerr
)
3216 return sys_errlist
[errnum
];
3217 return (char *) "Unknown error";
3219 #endif /* not WINDOWSNT */
3220 #endif /* ! HAVE_STRERROR */
3223 emacs_open (path
, oflag
, mode
)
3227 register int rtnval
;
3230 if (oflag
& O_CREAT
)
3231 return creat (path
, mode
);
3234 while ((rtnval
= open (path
, oflag
, mode
)) == -1
3235 && (errno
== EINTR
));
3244 register int rtnval
;
3246 while ((rtnval
= close (fd
)) == -1
3247 && (errno
== EINTR
))
3250 /* If close is interrupted SunOS 4.1 may or may not have closed the
3251 file descriptor. If it did the second close will fail with
3252 errno = EBADF. That means we have succeeded. */
3253 if (rtnval
== -1 && did_retry
&& errno
== EBADF
)
3260 emacs_read (fildes
, buf
, nbyte
)
3265 register int rtnval
;
3267 while ((rtnval
= read (fildes
, buf
, nbyte
)) == -1
3268 && (errno
== EINTR
));
3273 emacs_write (fildes
, buf
, nbyte
)
3278 register int rtnval
, bytes_written
;
3284 rtnval
= write (fildes
, buf
, nbyte
);
3291 return (bytes_written
? bytes_written
: -1);
3296 bytes_written
+= rtnval
;
3298 return (bytes_written
);
3304 * Substitute fork for vfork on USG flavors.
3312 #endif /* not WINDOWSNT */
3313 #endif /* not HAVE_VFORK */
3317 * All of the following are for USG.
3319 * On USG systems the system calls are INTERRUPTIBLE by signals
3320 * that the user program has elected to catch. Thus the system call
3321 * must be retried in these cases. To handle this without massive
3322 * changes in the source code, we remap the standard system call names
3323 * to names for our own functions in sysdep.c that do the system call
3324 * with retries. Actually, for portability reasons, it is good
3325 * programming practice, as this example shows, to limit all actual
3326 * system calls to a single occurrence in the source. Sure, this
3327 * adds an extra level of function call overhead but it is almost
3328 * always negligible. Fred Fish, Unisoft Systems Inc.
3332 * Warning, this function may not duplicate 4.2 action properly
3333 * under error conditions.
3337 /* In 4.1, param.h fails to define this. */
3338 #define MAXPATHLEN 1024
3347 char *npath
, *spath
;
3348 extern char *getcwd ();
3350 BLOCK_INPUT
; /* getcwd uses malloc */
3351 spath
= npath
= getcwd ((char *) 0, MAXPATHLEN
);
3354 /* On Altos 3068, getcwd can return @hostname/dir, so discard
3355 up to first slash. Should be harmless on other systems. */
3356 while (*npath
&& *npath
!= '/')
3358 strcpy (pathname
, npath
);
3359 free (spath
); /* getcwd uses malloc */
3364 #endif /* HAVE_GETWD */
3367 * Emulate rename using unlink/link. Note that this is
3368 * only partially correct. Also, doesn't enforce restriction
3369 * that files be of same type (regular->regular, dir->dir, etc).
3378 if (access (from
, 0) == 0)
3381 if (link (from
, to
) == 0)
3382 if (unlink (from
) == 0)
3394 /* HPUX curses library references perror, but as far as we know
3395 it won't be called. Anyway this definition will do for now. */
3401 #endif /* not HAVE_PERROR */
3407 * Emulate BSD dup2. First close newd if it already exists.
3408 * Then, attempt to dup oldd. If not successful, call dup2 recursively
3409 * until we are, then close the unsuccessful ones.
3416 register int fd
, ret
;
3421 return fcntl (oldd
, F_DUPFD
, newd
);
3428 ret
= dup2 (old
,new);
3434 #endif /* not HAVE_DUP2 */
3437 * Gettimeofday. Simulate as much as possible. Only accurate
3438 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3439 * Only needed when subprocesses are defined.
3444 #ifndef HAVE_GETTIMEOFDAY
3449 gettimeofday (tp
, tzp
)
3451 struct timezone
*tzp
;
3453 extern long time ();
3455 tp
->tv_sec
= time ((long *)0);
3458 tzp
->tz_minuteswest
= -1;
3465 #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
3468 * This function will go away as soon as all the stubs fixed. (fnf)
3475 printf ("%s not yet implemented\r\n", badfunc
);
3482 /* Directory routines for systems that don't have them. */
3484 #ifdef SYSV_SYSTEM_DIR
3488 #if defined (BROKEN_CLOSEDIR) || !defined (HAVE_CLOSEDIR)
3492 register DIR *dirp
; /* stream from opendir */
3496 rtnval
= emacs_close (dirp
->dd_fd
);
3498 /* Some systems (like Solaris) allocate the buffer and the DIR all
3499 in one block. Why in the world are we freeing this ourselves
3501 #if ! (defined (sun) && defined (USG5_4))
3502 xfree ((char *) dirp
->dd_buf
); /* directory block defined in <dirent.h> */
3504 xfree ((char *) dirp
);
3508 #endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
3509 #endif /* SYSV_SYSTEM_DIR */
3511 #ifdef NONSYSTEM_DIR_LIBRARY
3515 char *filename
; /* name of directory */
3517 register DIR *dirp
; /* -> malloc'ed storage */
3518 register int fd
; /* file descriptor for read */
3519 struct stat sbuf
; /* result of fstat */
3521 fd
= emacs_open (filename
, O_RDONLY
, 0);
3526 if (fstat (fd
, &sbuf
) < 0
3527 || (sbuf
.st_mode
& S_IFMT
) != S_IFDIR
3528 || (dirp
= (DIR *) malloc (sizeof (DIR))) == 0)
3532 return 0; /* bad luck today */
3537 dirp
->dd_loc
= dirp
->dd_size
= 0; /* refill needed */
3544 register DIR *dirp
; /* stream from opendir */
3546 emacs_close (dirp
->dd_fd
);
3547 xfree ((char *) dirp
);
3555 ino_t od_ino
; /* inode */
3556 char od_name
[DIRSIZ
]; /* filename */
3558 #endif /* not VMS */
3560 struct direct dir_static
; /* simulated directory contents */
3565 register DIR *dirp
; /* stream from opendir */
3568 register struct olddir
*dp
; /* -> directory data */
3570 register struct dir$_name
*dp
; /* -> directory data */
3571 register struct dir$_version
*dv
; /* -> version data */
3576 if (dirp
->dd_loc
>= dirp
->dd_size
)
3577 dirp
->dd_loc
= dirp
->dd_size
= 0;
3579 if (dirp
->dd_size
== 0 /* refill buffer */
3580 && (dirp
->dd_size
= emacs_read (dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
)) <= 0)
3584 dp
= (struct olddir
*) &dirp
->dd_buf
[dirp
->dd_loc
];
3585 dirp
->dd_loc
+= sizeof (struct olddir
);
3587 if (dp
->od_ino
!= 0) /* not deleted entry */
3589 dir_static
.d_ino
= dp
->od_ino
;
3590 strncpy (dir_static
.d_name
, dp
->od_name
, DIRSIZ
);
3591 dir_static
.d_name
[DIRSIZ
] = '\0';
3592 dir_static
.d_namlen
= strlen (dir_static
.d_name
);
3593 dir_static
.d_reclen
= sizeof (struct direct
)
3595 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3596 return &dir_static
; /* -> simulated structure */
3599 dp
= (struct dir$_name
*) dirp
->dd_buf
;
3600 if (dirp
->dd_loc
== 0)
3601 dirp
->dd_loc
= (dp
->dir$b_namecount
&1) ? dp
->dir$b_namecount
+ 1
3602 : dp
->dir$b_namecount
;
3603 dv
= (struct dir$_version
*)&dp
->dir$t_name
[dirp
->dd_loc
];
3604 dir_static
.d_ino
= dv
->dir$w_fid_num
;
3605 dir_static
.d_namlen
= dp
->dir$b_namecount
;
3606 dir_static
.d_reclen
= sizeof (struct direct
)
3608 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3609 strncpy (dir_static
.d_name
, dp
->dir$t_name
, dp
->dir$b_namecount
);
3610 dir_static
.d_name
[dir_static
.d_namlen
] = '\0';
3611 dirp
->dd_loc
= dirp
->dd_size
; /* only one record at a time */
3618 /* readdirver is just like readdir except it returns all versions of a file
3619 as separate entries. */
3624 register DIR *dirp
; /* stream from opendir */
3626 register struct dir$_name
*dp
; /* -> directory data */
3627 register struct dir$_version
*dv
; /* -> version data */
3629 if (dirp
->dd_loc
>= dirp
->dd_size
- sizeof (struct dir$_name
))
3630 dirp
->dd_loc
= dirp
->dd_size
= 0;
3632 if (dirp
->dd_size
== 0 /* refill buffer */
3633 && (dirp
->dd_size
= sys_read (dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
)) <= 0)
3636 dp
= (struct dir$_name
*) dirp
->dd_buf
;
3637 if (dirp
->dd_loc
== 0)
3638 dirp
->dd_loc
= (dp
->dir$b_namecount
& 1) ? dp
->dir$b_namecount
+ 1
3639 : dp
->dir$b_namecount
;
3640 dv
= (struct dir$_version
*) &dp
->dir$t_name
[dirp
->dd_loc
];
3641 strncpy (dir_static
.d_name
, dp
->dir$t_name
, dp
->dir$b_namecount
);
3642 sprintf (&dir_static
.d_name
[dp
->dir$b_namecount
], ";%d", dv
->dir$w_version
);
3643 dir_static
.d_namlen
= strlen (dir_static
.d_name
);
3644 dir_static
.d_ino
= dv
->dir$w_fid_num
;
3645 dir_static
.d_reclen
= sizeof (struct direct
) - MAXNAMLEN
+ 3
3646 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3647 dirp
->dd_loc
= ((char *) (++dv
) - dp
->dir$t_name
);
3653 #endif /* NONSYSTEM_DIR_LIBRARY */
3657 set_file_times (filename
, atime
, mtime
)
3659 EMACS_TIME atime
, mtime
;
3662 struct timeval tv
[2];
3665 return utimes (filename
, tv
);
3666 #else /* not HAVE_UTIMES */
3668 utb
.actime
= EMACS_SECS (atime
);
3669 utb
.modtime
= EMACS_SECS (mtime
);
3670 return utime (filename
, &utb
);
3671 #endif /* not HAVE_UTIMES */
3674 /* mkdir and rmdir functions, for systems which don't have them. */
3678 * Written by Robert Rother, Mariah Corporation, August 1985.
3680 * If you want it, it's yours. All I ask in return is that if you
3681 * figure out how to do this in a Bourne Shell script you send me
3683 * sdcsvax!rmr or rmr@uscd
3685 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
3686 * subroutine. 11Mar86; hoptoad!gnu
3688 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3689 * subroutine didn't return EEXIST. It does now.
3695 #ifdef MKDIR_PROTOTYPE
3699 mkdir (dpath
, dmode
)
3704 int cpid
, status
, fd
;
3705 struct stat statbuf
;
3707 if (stat (dpath
, &statbuf
) == 0)
3709 errno
= EEXIST
; /* Stat worked, so it already exists */
3713 /* If stat fails for a reason other than non-existence, return error */
3714 if (errno
!= ENOENT
)
3717 synch_process_alive
= 1;
3718 switch (cpid
= fork ())
3721 case -1: /* Error in fork */
3722 return (-1); /* Errno is set already */
3724 case 0: /* Child process */
3726 * Cheap hack to set mode of new directory. Since this
3727 * child process is going away anyway, we zap its umask.
3728 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3729 * directory. Does anybody care?
3731 status
= umask (0); /* Get current umask */
3732 status
= umask (status
| (0777 & ~dmode
)); /* Set for mkdir */
3733 fd
= emacs_open ("/dev/null", O_RDWR
, 0);
3740 execl ("/bin/mkdir", "mkdir", dpath
, (char *) 0);
3741 _exit (-1); /* Can't exec /bin/mkdir */
3743 default: /* Parent process */
3744 wait_for_termination (cpid
);
3747 if (synch_process_death
!= 0 || synch_process_retcode
!= 0)
3749 errno
= EIO
; /* We don't know why, but */
3750 return -1; /* /bin/mkdir failed */
3755 #endif /* not HAVE_MKDIR */
3762 int cpid
, status
, fd
;
3763 struct stat statbuf
;
3765 if (stat (dpath
, &statbuf
) != 0)
3767 /* Stat just set errno. We don't have to */
3771 synch_process_alive
= 1;
3772 switch (cpid
= fork ())
3775 case -1: /* Error in fork */
3776 return (-1); /* Errno is set already */
3778 case 0: /* Child process */
3779 fd
= emacs_open ("/dev/null", O_RDWR
, 0);
3786 execl ("/bin/rmdir", "rmdir", dpath
, (char *) 0);
3787 _exit (-1); /* Can't exec /bin/rmdir */
3789 default: /* Parent process */
3790 wait_for_termination (cpid
);
3793 if (synch_process_death
!= 0 || synch_process_retcode
!= 0)
3795 errno
= EIO
; /* We don't know why, but */
3796 return -1; /* /bin/rmdir failed */
3801 #endif /* !HAVE_RMDIR */
3805 /* Functions for VMS */
3807 #include "vms-pwd.h"
3812 /* Return as a string the VMS error string pertaining to STATUS.
3813 Reuses the same static buffer each time it is called. */
3817 int status
; /* VMS status code */
3821 static char buf
[257];
3823 bufadr
[0] = sizeof buf
- 1;
3824 bufadr
[1] = (int) buf
;
3825 if (! (SYS$
GETMSG (status
, &len
, bufadr
, 0x1, 0) & 1))
3826 return "untranslatable VMS error status";
3834 /* The following is necessary because 'access' emulation by VMS C (2.0) does
3835 * not work correctly. (It also doesn't work well in version 2.3.)
3840 #define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3841 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3845 unsigned short s_buflen
;
3846 unsigned short s_code
;
3848 unsigned short *s_retlenadr
;
3852 #define buflen s.s_buflen
3853 #define code s.s_code
3854 #define bufadr s.s_bufadr
3855 #define retlenadr s.s_retlenadr
3857 #define R_OK 4 /* test for read permission */
3858 #define W_OK 2 /* test for write permission */
3859 #define X_OK 1 /* test for execute (search) permission */
3860 #define F_OK 0 /* test for presence of file */
3863 sys_access (path
, mode
)
3867 static char *user
= NULL
;
3870 /* translate possible directory spec into .DIR file name, so brain-dead
3871 * access can treat the directory like a file. */
3872 if (directory_file_name (path
, dir_fn
))
3876 return access (path
, mode
);
3877 if (user
== NULL
&& (user
= (char *) getenv ("USER")) == NULL
)
3883 unsigned short int dummy
;
3885 static int constant
= ACL$C_FILE
;
3886 DESCRIPTOR (path_desc
, path
);
3887 DESCRIPTOR (user_desc
, user
);
3891 if ((mode
& X_OK
) && ((stat
= access (path
, mode
)) < 0 || mode
== X_OK
))
3894 acces
|= CHP$M_READ
;
3896 acces
|= CHP$M_WRITE
;
3897 itemlst
[0].buflen
= sizeof (int);
3898 itemlst
[0].code
= CHP$_FLAGS
;
3899 itemlst
[0].bufadr
= (char *) &flags
;
3900 itemlst
[0].retlenadr
= &dummy
;
3901 itemlst
[1].buflen
= sizeof (int);
3902 itemlst
[1].code
= CHP$_ACCESS
;
3903 itemlst
[1].bufadr
= (char *) &acces
;
3904 itemlst
[1].retlenadr
= &dummy
;
3905 itemlst
[2].end
= CHP$_END
;
3906 stat
= SYS$
CHECK_ACCESS (&constant
, &path_desc
, &user_desc
, itemlst
);
3907 return stat
== SS$_NORMAL
? 0 : -1;
3911 #else /* not VMS4_4 */
3914 #define ACE$M_WRITE 2
3915 #define ACE$C_KEYID 1
3917 static unsigned short memid
, grpid
;
3918 static unsigned int uic
;
3920 /* Called from init_sys_modes, so it happens not very often
3921 but at least each time Emacs is loaded. */
3923 sys_access_reinit ()
3929 sys_access (filename
, type
)
3935 int status
, size
, i
, typecode
, acl_controlled
;
3936 unsigned int *aclptr
, *aclend
, aclbuf
[60];
3937 union prvdef prvmask
;
3939 /* Get UIC and GRP values for protection checking. */
3942 status
= LIB$
GETJPI (&JPI$_UIC
, 0, 0, &uic
, 0, 0);
3945 memid
= uic
& 0xFFFF;
3949 if (type
!= 2) /* not checking write access */
3950 return access (filename
, type
);
3952 /* Check write protection. */
3954 #define CHECKPRIV(bit) (prvmask.bit)
3955 #define WRITABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
3957 /* Find privilege bits */
3958 status
= SYS$
SETPRV (0, 0, 0, prvmask
);
3960 error ("Unable to find privileges: %s", vmserrstr (status
));
3961 if (CHECKPRIV (PRV$V_BYPASS
))
3962 return 0; /* BYPASS enabled */
3964 fab
.fab$b_fac
= FAB$M_GET
;
3965 fab
.fab$l_fna
= filename
;
3966 fab
.fab$b_fns
= strlen (filename
);
3967 fab
.fab$l_xab
= &xab
;
3968 xab
= cc$rms_xabpro
;
3969 xab
.xab$l_aclbuf
= aclbuf
;
3970 xab
.xab$w_aclsiz
= sizeof (aclbuf
);
3971 status
= SYS$
OPEN (&fab
, 0, 0);
3974 SYS$
CLOSE (&fab
, 0, 0);
3975 /* Check system access */
3976 if (CHECKPRIV (PRV$V_SYSPRV
) && WRITABLE (XAB$V_SYS
))
3978 /* Check ACL entries, if any */
3980 if (xab
.xab$w_acllen
> 0)
3983 aclend
= &aclbuf
[xab
.xab$w_acllen
/ 4];
3984 while (*aclptr
&& aclptr
< aclend
)
3986 size
= (*aclptr
& 0xff) / 4;
3987 typecode
= (*aclptr
>> 8) & 0xff;
3988 if (typecode
== ACE$C_KEYID
)
3989 for (i
= size
- 1; i
> 1; i
--)
3990 if (aclptr
[i
] == uic
)
3993 if (aclptr
[1] & ACE$M_WRITE
)
3994 return 0; /* Write access through ACL */
3996 aclptr
= &aclptr
[size
];
3998 if (acl_controlled
) /* ACL specified, prohibits write access */
4001 /* No ACL entries specified, check normal protection */
4002 if (WRITABLE (XAB$V_WLD
)) /* World writable */
4004 if (WRITABLE (XAB$V_GRP
) &&
4005 (unsigned short) (xab
.xab$l_uic
>> 16) == grpid
)
4006 return 0; /* Group writable */
4007 if (WRITABLE (XAB$V_OWN
) &&
4008 (xab
.xab$l_uic
& 0xFFFF) == memid
)
4009 return 0; /* Owner writable */
4011 return -1; /* Not writable */
4013 #endif /* not VMS4_4 */
4016 static char vtbuf
[NAM$C_MAXRSS
+1];
4018 /* translate a vms file spec to a unix path */
4020 sys_translate_vms (vfile
)
4031 /* leading device or logical name is a root directory */
4032 if (p
= strchr (vfile
, ':'))
4041 if (*p
== '[' || *p
== '<')
4043 while (*++vfile
!= *p
+ 2)
4047 if (vfile
[-1] == *p
)
4070 static char utbuf
[NAM$C_MAXRSS
+1];
4072 /* translate a unix path to a VMS file spec */
4074 sys_translate_unix (ufile
)
4097 if (index (&ufile
[1], '/'))
4104 if (index (&ufile
[1], '/'))
4111 if (strncmp (ufile
, "./", 2) == 0)
4118 ufile
++; /* skip the dot */
4119 if (index (&ufile
[1], '/'))
4124 else if (strncmp (ufile
, "../", 3) == 0)
4132 ufile
+= 2; /* skip the dots */
4133 if (index (&ufile
[1], '/'))
4158 extern char *getcwd ();
4160 #define MAXPATHLEN 1024
4162 ptr
= xmalloc (MAXPATHLEN
);
4163 val
= getcwd (ptr
, MAXPATHLEN
);
4169 strcpy (pathname
, ptr
);
4178 long item_code
= JPI$_OWNER
;
4179 unsigned long parent_id
;
4182 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &parent_id
)) & 1) == 0)
4185 vaxc$errno
= status
;
4195 return (getgid () << 16) | getuid ();
4200 sys_read (fildes
, buf
, nbyte
)
4205 return read (fildes
, buf
, (nbyte
< MAXIOSIZE
? nbyte
: MAXIOSIZE
));
4210 sys_write (fildes
, buf
, nbyte
)
4215 register int nwrote
, rtnval
= 0;
4217 while (nbyte
> MAXIOSIZE
&& (nwrote
= write (fildes
, buf
, MAXIOSIZE
)) > 0) {
4223 return rtnval
? rtnval
: -1;
4224 if ((nwrote
= write (fildes
, buf
, nbyte
)) < 0)
4225 return rtnval
? rtnval
: -1;
4226 return (rtnval
+ nwrote
);
4231 * VAX/VMS VAX C RTL really loses. It insists that records
4232 * end with a newline (carriage return) character, and if they
4233 * don't it adds one (nice of it isn't it!)
4235 * Thus we do this stupidity below.
4240 sys_write (fildes
, buf
, nbytes
)
4243 unsigned int nbytes
;
4250 fstat (fildes
, &st
);
4256 /* Handle fixed-length files with carriage control. */
4257 if (st
.st_fab_rfm
== FAB$C_FIX
4258 && ((st
.st_fab_rat
& (FAB$M_FTN
| FAB$M_CR
)) != 0))
4260 len
= st
.st_fab_mrs
;
4261 retval
= write (fildes
, p
, min (len
, nbytes
));
4264 retval
++; /* This skips the implied carriage control */
4268 e
= p
+ min (MAXIOSIZE
, nbytes
) - 1;
4269 while (*e
!= '\n' && e
> p
) e
--;
4270 if (p
== e
) /* Ok.. so here we add a newline... sigh. */
4271 e
= p
+ min (MAXIOSIZE
, nbytes
) - 1;
4273 retval
= write (fildes
, p
, len
);
4284 /* Create file NEW copying its attributes from file OLD. If
4285 OLD is 0 or does not exist, create based on the value of
4288 /* Protection value the file should ultimately have.
4289 Set by create_copy_attrs, and use by rename_sansversions. */
4290 static unsigned short int fab_final_pro
;
4293 creat_copy_attrs (old
, new)
4296 struct FAB fab
= cc$rms_fab
;
4297 struct XABPRO xabpro
;
4298 char aclbuf
[256]; /* Choice of size is arbitrary. See below. */
4299 extern int vms_stmlf_recfm
;
4303 fab
.fab$b_fac
= FAB$M_GET
;
4304 fab
.fab$l_fna
= old
;
4305 fab
.fab$b_fns
= strlen (old
);
4306 fab
.fab$l_xab
= (char *) &xabpro
;
4307 xabpro
= cc$rms_xabpro
;
4308 xabpro
.xab$l_aclbuf
= aclbuf
;
4309 xabpro
.xab$w_aclsiz
= sizeof aclbuf
;
4310 /* Call $OPEN to fill in the fab & xabpro fields. */
4311 if (SYS$
OPEN (&fab
, 0, 0) & 1)
4313 SYS$
CLOSE (&fab
, 0, 0);
4314 fab
.fab$l_alq
= 0; /* zero the allocation quantity */
4315 if (xabpro
.xab$w_acllen
> 0)
4317 if (xabpro
.xab$w_acllen
> sizeof aclbuf
)
4318 /* If the acl buffer was too short, redo open with longer one.
4319 Wouldn't need to do this if there were some system imposed
4320 limit on the size of an ACL, but I can't find any such. */
4322 xabpro
.xab$l_aclbuf
= (char *) alloca (xabpro
.xab$w_acllen
);
4323 xabpro
.xab$w_aclsiz
= xabpro
.xab$w_acllen
;
4324 if (SYS$
OPEN (&fab
, 0, 0) & 1)
4325 SYS$
CLOSE (&fab
, 0, 0);
4331 xabpro
.xab$l_aclbuf
= 0;
4336 fab
.fab$l_fna
= new;
4337 fab
.fab$b_fns
= strlen (new);
4341 fab
.fab$b_rfm
= vms_stmlf_recfm
? FAB$C_STMLF
: FAB$C_VAR
;
4342 fab
.fab$b_rat
= FAB$M_CR
;
4345 /* Set the file protections such that we will be able to manipulate
4346 this file. Once we are done writing and renaming it, we will set
4347 the protections back. */
4349 fab_final_pro
= xabpro
.xab$w_pro
;
4351 SYS$
SETDFPROT (0, &fab_final_pro
);
4352 xabpro
.xab$w_pro
&= 0xff0f; /* set O:rewd for now. This is set back later. */
4354 /* Create the new file with either default attrs or attrs copied
4356 if (!(SYS$
CREATE (&fab
, 0, 0) & 1))
4358 SYS$
CLOSE (&fab
, 0, 0);
4359 /* As this is a "replacement" for creat, return a file descriptor
4360 opened for writing. */
4361 return open (new, O_WRONLY
);
4366 #include <varargs.h>
4369 #define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
4374 sys_creat (va_alist
)
4377 va_list list_incrementer
;
4380 int rfd
; /* related file descriptor */
4381 int fd
; /* Our new file descriptor */
4388 extern int vms_stmlf_recfm
;
4391 va_start (list_incrementer
);
4392 name
= va_arg (list_incrementer
, char *);
4393 mode
= va_arg (list_incrementer
, int);
4395 rfd
= va_arg (list_incrementer
, int);
4396 va_end (list_incrementer
);
4399 /* Use information from the related file descriptor to set record
4400 format of the newly created file. */
4401 fstat (rfd
, &st_buf
);
4402 switch (st_buf
.st_fab_rfm
)
4405 strcpy (rfm
, "rfm = fix");
4406 sprintf (mrs
, "mrs = %d", st_buf
.st_fab_mrs
);
4407 strcpy (rat
, "rat = ");
4408 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4410 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4411 strcat (rat
, "ftn");
4412 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4413 strcat (rat
, "prn");
4414 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4415 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4416 strcat (rat
, ", blk");
4418 strcat (rat
, "blk");
4419 return creat (name
, 0, rfm
, rat
, mrs
);
4422 strcpy (rfm
, "rfm = vfc");
4423 sprintf (fsz
, "fsz = %d", st_buf
.st_fab_fsz
);
4424 strcpy (rat
, "rat = ");
4425 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4427 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4428 strcat (rat
, "ftn");
4429 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4430 strcat (rat
, "prn");
4431 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4432 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4433 strcat (rat
, ", blk");
4435 strcat (rat
, "blk");
4436 return creat (name
, 0, rfm
, rat
, fsz
);
4439 strcpy (rfm
, "rfm = stm");
4443 strcpy (rfm
, "rfm = stmcr");
4447 strcpy (rfm
, "rfm = stmlf");
4451 strcpy (rfm
, "rfm = udf");
4455 strcpy (rfm
, "rfm = var");
4458 strcpy (rat
, "rat = ");
4459 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4461 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4462 strcat (rat
, "ftn");
4463 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4464 strcat (rat
, "prn");
4465 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4466 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4467 strcat (rat
, ", blk");
4469 strcat (rat
, "blk");
4473 strcpy (rfm
, vms_stmlf_recfm
? "rfm = stmlf" : "rfm=var");
4474 strcpy (rat
, "rat=cr");
4476 /* Until the VAX C RTL fixes the many bugs with modes, always use
4477 mode 0 to get the user's default protection. */
4478 fd
= creat (name
, 0, rfm
, rat
);
4479 if (fd
< 0 && errno
== EEXIST
)
4481 if (unlink (name
) < 0)
4482 report_file_error ("delete", build_string (name
));
4483 fd
= creat (name
, 0, rfm
, rat
);
4489 /* fwrite to stdout is S L O W. Speed it up by using fputc...*/
4491 sys_fwrite (ptr
, size
, num
, fp
)
4492 register char * ptr
;
4495 register int tot
= num
* size
;
4503 * The VMS C library routine creat actually creates a new version of an
4504 * existing file rather than truncating the old version. There are times
4505 * when this is not the desired behavior, for instance, when writing an
4506 * auto save file (you only want one version), or when you don't have
4507 * write permission in the directory containing the file (but the file
4508 * itself is writable). Hence this routine, which is equivalent to
4509 * "close (creat (fn, 0));" on Unix if fn already exists.
4515 struct FAB xfab
= cc$rms_fab
;
4516 struct RAB xrab
= cc$rms_rab
;
4519 xfab
.fab$l_fop
= FAB$M_TEF
; /* free allocated but unused blocks on close */
4520 xfab
.fab$b_fac
= FAB$M_TRN
| FAB$M_GET
; /* allow truncate and get access */
4521 xfab
.fab$b_shr
= FAB$M_NIL
; /* allow no sharing - file must be locked */
4522 xfab
.fab$l_fna
= fn
;
4523 xfab
.fab$b_fns
= strlen (fn
);
4524 xfab
.fab$l_dna
= ";0"; /* default to latest version of the file */
4526 xrab
.rab$l_fab
= &xfab
;
4528 /* This gibberish opens the file, positions to the first record, and
4529 deletes all records from there until the end of file. */
4530 if ((SYS$
OPEN (&xfab
) & 01) == 01)
4532 if ((SYS$
CONNECT (&xrab
) & 01) == 01 &&
4533 (SYS$
FIND (&xrab
) & 01) == 01 &&
4534 (SYS$
TRUNCATE (&xrab
) & 01) == 01)
4545 /* Define this symbol to actually read SYSUAF.DAT. This requires either
4546 SYSPRV or a readable SYSUAF.DAT. */
4552 * Routine to read the VMS User Authorization File and return
4553 * a specific user's record.
4556 static struct UAF retuaf
;
4559 get_uaf_name (uname
)
4566 uaf_fab
= cc$rms_fab
;
4567 uaf_rab
= cc$rms_rab
;
4568 /* initialize fab fields */
4569 uaf_fab
.fab$l_fna
= "SYS$SYSTEM:SYSUAF.DAT";
4570 uaf_fab
.fab$b_fns
= 21;
4571 uaf_fab
.fab$b_fac
= FAB$M_GET
;
4572 uaf_fab
.fab$b_org
= FAB$C_IDX
;
4573 uaf_fab
.fab$b_shr
= FAB$M_GET
|FAB$M_PUT
|FAB$M_UPD
|FAB$M_DEL
;
4574 /* initialize rab fields */
4575 uaf_rab
.rab$l_fab
= &uaf_fab
;
4576 /* open the User Authorization File */
4577 status
= SYS$
OPEN (&uaf_fab
);
4581 vaxc$errno
= status
;
4584 status
= SYS$
CONNECT (&uaf_rab
);
4588 vaxc$errno
= status
;
4591 /* read the requested record - index is in uname */
4592 uaf_rab
.rab$l_kbf
= uname
;
4593 uaf_rab
.rab$b_ksz
= strlen (uname
);
4594 uaf_rab
.rab$b_rac
= RAB$C_KEY
;
4595 uaf_rab
.rab$l_ubf
= (char *)&retuaf
;
4596 uaf_rab
.rab$w_usz
= sizeof retuaf
;
4597 status
= SYS$
GET (&uaf_rab
);
4601 vaxc$errno
= status
;
4604 /* close the User Authorization File */
4605 status
= SYS$
DISCONNECT (&uaf_rab
);
4609 vaxc$errno
= status
;
4612 status
= SYS$
CLOSE (&uaf_fab
);
4616 vaxc$errno
= status
;
4630 uaf_fab
= cc$rms_fab
;
4631 uaf_rab
= cc$rms_rab
;
4632 /* initialize fab fields */
4633 uaf_fab
.fab$l_fna
= "SYS$SYSTEM:SYSUAF.DAT";
4634 uaf_fab
.fab$b_fns
= 21;
4635 uaf_fab
.fab$b_fac
= FAB$M_GET
;
4636 uaf_fab
.fab$b_org
= FAB$C_IDX
;
4637 uaf_fab
.fab$b_shr
= FAB$M_GET
|FAB$M_PUT
|FAB$M_UPD
|FAB$M_DEL
;
4638 /* initialize rab fields */
4639 uaf_rab
.rab$l_fab
= &uaf_fab
;
4640 /* open the User Authorization File */
4641 status
= SYS$
OPEN (&uaf_fab
);
4645 vaxc$errno
= status
;
4648 status
= SYS$
CONNECT (&uaf_rab
);
4652 vaxc$errno
= status
;
4655 /* read the requested record - index is in uic */
4656 uaf_rab
.rab$b_krf
= 1; /* 1st alternate key */
4657 uaf_rab
.rab$l_kbf
= (char *) &uic
;
4658 uaf_rab
.rab$b_ksz
= sizeof uic
;
4659 uaf_rab
.rab$b_rac
= RAB$C_KEY
;
4660 uaf_rab
.rab$l_ubf
= (char *)&retuaf
;
4661 uaf_rab
.rab$w_usz
= sizeof retuaf
;
4662 status
= SYS$
GET (&uaf_rab
);
4666 vaxc$errno
= status
;
4669 /* close the User Authorization File */
4670 status
= SYS$
DISCONNECT (&uaf_rab
);
4674 vaxc$errno
= status
;
4677 status
= SYS$
CLOSE (&uaf_fab
);
4681 vaxc$errno
= status
;
4687 static struct passwd retpw
;
4695 /* copy these out first because if the username is 32 chars, the next
4696 section will overwrite the first byte of the UIC */
4697 retpw
.pw_uid
= up
->uaf$w_mem
;
4698 retpw
.pw_gid
= up
->uaf$w_grp
;
4700 /* I suppose this is not the best style, to possibly overwrite one
4701 byte beyond the end of the field, but what the heck... */
4702 ptr
= &up
->uaf$t_username
[UAF$S_USERNAME
];
4703 while (ptr
[-1] == ' ')
4706 strcpy (retpw
.pw_name
, up
->uaf$t_username
);
4708 /* the rest of these are counted ascii strings */
4709 strncpy (retpw
.pw_gecos
, &up
->uaf$t_owner
[1], up
->uaf$t_owner
[0]);
4710 retpw
.pw_gecos
[up
->uaf$t_owner
[0]] = '\0';
4711 strncpy (retpw
.pw_dir
, &up
->uaf$t_defdev
[1], up
->uaf$t_defdev
[0]);
4712 retpw
.pw_dir
[up
->uaf$t_defdev
[0]] = '\0';
4713 strncat (retpw
.pw_dir
, &up
->uaf$t_defdir
[1], up
->uaf$t_defdir
[0]);
4714 retpw
.pw_dir
[up
->uaf$t_defdev
[0] + up
->uaf$t_defdir
[0]] = '\0';
4715 strncpy (retpw
.pw_shell
, &up
->uaf$t_defcli
[1], up
->uaf$t_defcli
[0]);
4716 retpw
.pw_shell
[up
->uaf$t_defcli
[0]] = '\0';
4720 #else /* not READ_SYSUAF */
4721 static struct passwd retpw
;
4722 #endif /* not READ_SYSUAF */
4733 unsigned char * full
;
4734 #endif /* READ_SYSUAF */
4739 if ('a' <= *ptr
&& *ptr
<= 'z')
4744 if (!(up
= get_uaf_name (name
)))
4746 return cnv_uaf_pw (up
);
4748 if (strcmp (name
, getenv ("USER")) == 0)
4750 retpw
.pw_uid
= getuid ();
4751 retpw
.pw_gid
= getgid ();
4752 strcpy (retpw
.pw_name
, name
);
4753 if (full
= egetenv ("FULLNAME"))
4754 strcpy (retpw
.pw_gecos
, full
);
4756 *retpw
.pw_gecos
= '\0';
4757 strcpy (retpw
.pw_dir
, egetenv ("HOME"));
4758 *retpw
.pw_shell
= '\0';
4763 #endif /* not READ_SYSUAF */
4773 if (!(up
= get_uaf_uic (uid
)))
4775 return cnv_uaf_pw (up
);
4777 if (uid
== sys_getuid ())
4778 return getpwnam (egetenv ("USER"));
4781 #endif /* not READ_SYSUAF */
4784 /* return total address space available to the current process. This is
4785 the sum of the current p0 size, p1 size and free page table entries
4791 unsigned long free_pages
;
4792 unsigned long frep0va
;
4793 unsigned long frep1va
;
4796 item_code
= JPI$_FREPTECNT
;
4797 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &free_pages
)) & 1) == 0)
4800 vaxc$errno
= status
;
4805 item_code
= JPI$_FREP0VA
;
4806 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &frep0va
)) & 1) == 0)
4809 vaxc$errno
= status
;
4812 item_code
= JPI$_FREP1VA
;
4813 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &frep1va
)) & 1) == 0)
4816 vaxc$errno
= status
;
4820 return free_pages
+ frep0va
+ (0x7fffffff - frep1va
);
4824 define_logical_name (varname
, string
)
4828 struct dsc$descriptor_s strdsc
=
4829 {strlen (string
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, string
};
4830 struct dsc$descriptor_s envdsc
=
4831 {strlen (varname
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, varname
};
4832 struct dsc$descriptor_s lnmdsc
=
4833 {7, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, "LNM$JOB"};
4835 return LIB$
SET_LOGICAL (&envdsc
, &strdsc
, &lnmdsc
, 0, 0);
4839 delete_logical_name (varname
)
4842 struct dsc$descriptor_s envdsc
=
4843 {strlen (varname
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, varname
};
4844 struct dsc$descriptor_s lnmdsc
=
4845 {7, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, "LNM$JOB"};
4847 return LIB$
DELETE_LOGICAL (&envdsc
, &lnmdsc
);
4865 error ("execvp system call not implemented");
4874 struct FAB from_fab
= cc$rms_fab
, to_fab
= cc$rms_fab
;
4875 struct NAM from_nam
= cc$rms_nam
, to_nam
= cc$rms_nam
;
4876 char from_esn
[NAM$C_MAXRSS
];
4877 char to_esn
[NAM$C_MAXRSS
];
4879 from_fab
.fab$l_fna
= from
;
4880 from_fab
.fab$b_fns
= strlen (from
);
4881 from_fab
.fab$l_nam
= &from_nam
;
4882 from_fab
.fab$l_fop
= FAB$M_NAM
;
4884 from_nam
.nam$l_esa
= from_esn
;
4885 from_nam
.nam$b_ess
= sizeof from_esn
;
4887 to_fab
.fab$l_fna
= to
;
4888 to_fab
.fab$b_fns
= strlen (to
);
4889 to_fab
.fab$l_nam
= &to_nam
;
4890 to_fab
.fab$l_fop
= FAB$M_NAM
;
4892 to_nam
.nam$l_esa
= to_esn
;
4893 to_nam
.nam$b_ess
= sizeof to_esn
;
4895 status
= SYS$
RENAME (&from_fab
, 0, 0, &to_fab
);
4901 if (status
== RMS$_DEV
)
4905 vaxc$errno
= status
;
4910 /* This function renames a file like `rename', but it strips
4911 the version number from the "to" filename, such that the "to" file is
4912 will always be a new version. It also sets the file protection once it is
4913 finished. The protection that we will use is stored in fab_final_pro,
4914 and was set when we did a creat_copy_attrs to create the file that we
4917 We could use the chmod function, but Eunichs uses 3 bits per user category
4918 to describe the protection, and VMS uses 4 (write and delete are separate
4919 bits). To maintain portability, the VMS implementation of `chmod' wires
4920 the W and D bits together. */
4923 static struct fibdef fib
; /* We need this initialized to zero */
4924 char vms_file_written
[NAM$C_MAXRSS
];
4927 rename_sans_version (from
,to
)
4934 struct FAB to_fab
= cc$rms_fab
;
4935 struct NAM to_nam
= cc$rms_nam
;
4936 struct dsc$descriptor fib_d
={sizeof (fib
),0,0,(char*) &fib
};
4937 struct dsc$descriptor fib_attr
[2]
4938 = {{sizeof (fab_final_pro
),ATR$C_FPRO
,0,(char*) &fab_final_pro
},{0,0,0,0}};
4939 char to_esn
[NAM$C_MAXRSS
];
4941 $
DESCRIPTOR (disk
,to_esn
);
4943 to_fab
.fab$l_fna
= to
;
4944 to_fab
.fab$b_fns
= strlen (to
);
4945 to_fab
.fab$l_nam
= &to_nam
;
4946 to_fab
.fab$l_fop
= FAB$M_NAM
;
4948 to_nam
.nam$l_esa
= to_esn
;
4949 to_nam
.nam$b_ess
= sizeof to_esn
;
4951 status
= SYS$
PARSE (&to_fab
, 0, 0); /* figure out the full file name */
4953 if (to_nam
.nam$l_fnb
&& NAM$M_EXP_VER
)
4954 *(to_nam
.nam$l_ver
) = '\0';
4956 stat
= rename (from
, to_esn
);
4960 strcpy (vms_file_written
, to_esn
);
4962 to_fab
.fab$l_fna
= vms_file_written
; /* this points to the versionless name */
4963 to_fab
.fab$b_fns
= strlen (vms_file_written
);
4965 /* Now set the file protection to the correct value */
4966 SYS$
OPEN (&to_fab
, 0, 0); /* This fills in the nam$w_fid fields */
4968 /* Copy these fields into the fib */
4969 fib
.fib$r_fid_overlay
.fib$w_fid
[0] = to_nam
.nam$w_fid
[0];
4970 fib
.fib$r_fid_overlay
.fib$w_fid
[1] = to_nam
.nam$w_fid
[1];
4971 fib
.fib$r_fid_overlay
.fib$w_fid
[2] = to_nam
.nam$w_fid
[2];
4973 SYS$
CLOSE (&to_fab
, 0, 0);
4975 stat
= SYS$
ASSIGN (&disk
, &chan
, 0, 0); /* open a channel to the disk */
4978 stat
= SYS$
QIOW (0, chan
, IO$_MODIFY
, iosb
, 0, 0, &fib_d
,
4979 0, 0, 0, &fib_attr
, 0);
4982 stat
= SYS$
DASSGN (chan
);
4985 strcpy (vms_file_written
, to_esn
); /* We will write this to the terminal*/
4996 unsigned short fid
[3];
4997 char esa
[NAM$C_MAXRSS
];
5000 fab
.fab$l_fop
= FAB$M_OFP
;
5001 fab
.fab$l_fna
= file
;
5002 fab
.fab$b_fns
= strlen (file
);
5003 fab
.fab$l_nam
= &nam
;
5006 nam
.nam$l_esa
= esa
;
5007 nam
.nam$b_ess
= NAM$C_MAXRSS
;
5009 status
= SYS$
PARSE (&fab
);
5010 if ((status
& 1) == 0)
5013 vaxc$errno
= status
;
5016 status
= SYS$
SEARCH (&fab
);
5017 if ((status
& 1) == 0)
5020 vaxc$errno
= status
;
5024 fid
[0] = nam
.nam$w_fid
[0];
5025 fid
[1] = nam
.nam$w_fid
[1];
5026 fid
[2] = nam
.nam$w_fid
[2];
5028 fab
.fab$l_fna
= new;
5029 fab
.fab$b_fns
= strlen (new);
5031 status
= SYS$
PARSE (&fab
);
5032 if ((status
& 1) == 0)
5035 vaxc$errno
= status
;
5039 nam
.nam$w_fid
[0] = fid
[0];
5040 nam
.nam$w_fid
[1] = fid
[1];
5041 nam
.nam$w_fid
[2] = fid
[2];
5043 nam
.nam$l_esa
= nam
.nam$l_name
;
5044 nam
.nam$b_esl
= nam
.nam$b_name
+ nam
.nam$b_type
+ nam
.nam$b_ver
;
5046 status
= SYS$
ENTER (&fab
);
5047 if ((status
& 1) == 0)
5050 vaxc$errno
= status
;
5061 printf ("%s not yet implemented\r\n", badfunc
);
5069 /* Arrange to return a range centered on zero. */
5070 return rand () - (1 << 30);
5082 /* Called from init_sys_modes. */
5088 /* If we're not on an HFT we shouldn't do any of this. We determine
5089 if we are on an HFT by trying to get an HFT error code. If this
5090 call fails, we're not on an HFT. */
5092 if (ioctl (0, HFQERROR
, &junk
) < 0)
5094 #else /* not IBMR2AIX */
5095 if (ioctl (0, HFQEIO
, 0) < 0)
5097 #endif /* not IBMR2AIX */
5099 /* On AIX the default hft keyboard mapping uses backspace rather than delete
5100 as the rubout key's ASCII code. Here this is changed. The bug is that
5101 there's no way to determine the old mapping, so in reset_sys_modes
5102 we need to assume that the normal map had been present. Of course, this
5103 code also doesn't help if on a terminal emulator which doesn't understand
5107 struct hfkeymap keymap
;
5109 buf
.hf_bufp
= (char *)&keymap
;
5110 buf
.hf_buflen
= sizeof (keymap
);
5111 keymap
.hf_nkeys
= 2;
5112 keymap
.hfkey
[0].hf_kpos
= 15;
5113 keymap
.hfkey
[0].hf_kstate
= HFMAPCHAR
| HFSHFNONE
;
5115 keymap
.hfkey
[0].hf_keyidh
= '<';
5116 #else /* not IBMR2AIX */
5117 keymap
.hfkey
[0].hf_page
= '<';
5118 #endif /* not IBMR2AIX */
5119 keymap
.hfkey
[0].hf_char
= 127;
5120 keymap
.hfkey
[1].hf_kpos
= 15;
5121 keymap
.hfkey
[1].hf_kstate
= HFMAPCHAR
| HFSHFSHFT
;
5123 keymap
.hfkey
[1].hf_keyidh
= '<';
5124 #else /* not IBMR2AIX */
5125 keymap
.hfkey
[1].hf_page
= '<';
5126 #endif /* not IBMR2AIX */
5127 keymap
.hfkey
[1].hf_char
= 127;
5128 hftctl (0, HFSKBD
, &buf
);
5130 /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
5132 line_ins_del_ok
= char_ins_del_ok
= 0;
5135 /* Reset the rubout key to backspace. */
5141 struct hfkeymap keymap
;
5145 if (ioctl (0, HFQERROR
, &junk
) < 0)
5147 #else /* not IBMR2AIX */
5148 if (ioctl (0, HFQEIO
, 0) < 0)
5150 #endif /* not IBMR2AIX */
5152 buf
.hf_bufp
= (char *)&keymap
;
5153 buf
.hf_buflen
= sizeof (keymap
);
5154 keymap
.hf_nkeys
= 2;
5155 keymap
.hfkey
[0].hf_kpos
= 15;
5156 keymap
.hfkey
[0].hf_kstate
= HFMAPCHAR
| HFSHFNONE
;
5158 keymap
.hfkey
[0].hf_keyidh
= '<';
5159 #else /* not IBMR2AIX */
5160 keymap
.hfkey
[0].hf_page
= '<';
5161 #endif /* not IBMR2AIX */
5162 keymap
.hfkey
[0].hf_char
= 8;
5163 keymap
.hfkey
[1].hf_kpos
= 15;
5164 keymap
.hfkey
[1].hf_kstate
= HFMAPCHAR
| HFSHFSHFT
;
5166 keymap
.hfkey
[1].hf_keyidh
= '<';
5167 #else /* not IBMR2AIX */
5168 keymap
.hfkey
[1].hf_page
= '<';
5169 #endif /* not IBMR2AIX */
5170 keymap
.hfkey
[1].hf_char
= 8;
5171 hftctl (0, HFSKBD
, &buf
);
5178 /* These are included on Sunos 4.1 when we do not use shared libraries.
5179 X11 libraries may refer to these functions but (we hope) do not
5180 actually call them. */
5200 #endif /* USE_DL_STUBS */
5209 register int length
;
5213 long max_str
= 65535;
5215 while (length
> max_str
) {
5216 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &max_str
, b
);
5221 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &max_str
, b
);
5223 while (length
-- > 0)
5225 #endif /* not VMS */
5228 #endif /* no bzero */
5229 #endif /* BSTRING */
5231 #if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
5234 /* Saying `void' requires a declaration, above, where bcopy is used
5235 and that declaration causes pain for systems where bcopy is a macro. */
5236 bcopy (b1
, b2
, length
)
5239 register int length
;
5242 long max_str
= 65535;
5244 while (length
> max_str
) {
5245 (void) LIB$
MOVC3 (&max_str
, b1
, b2
);
5251 (void) LIB$
MOVC3 (&length
, b1
, b2
);
5253 while (length
-- > 0)
5255 #endif /* not VMS */
5257 #endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
5262 bcmp (b1
, b2
, length
) /* This could be a macro! */
5265 register int length
;
5268 struct dsc$descriptor_s src1
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b1
};
5269 struct dsc$descriptor_s src2
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b2
};
5271 return STR$
COMPARE (&src1
, &src2
);
5273 while (length
-- > 0)
5278 #endif /* not VMS */
5280 #endif /* no bcmp */
5281 #endif /* not BSTRING */
5283 #ifndef HAVE_STRSIGNAL
5290 if (0 <= code
&& code
< NSIG
)
5293 signame
= sys_errlist
[code
];
5295 /* Cast to suppress warning if the table has const char *. */
5296 signame
= (char *) sys_siglist
[code
];
5302 #endif /* HAVE_STRSIGNAL */
5304 /* All the Macintosh stuffs go here */
5309 #include <MacTypes.h>
5310 #include <TextUtils.h>
5311 #include <Folders.h>
5314 #include <sys/stat.h>
5317 #include <sys/param.h>
5319 /* Convert a Mac pathname to Unix form. A Mac full pathname is one
5320 that does not begin with a ':' and contains at least one ':'. A Mac
5321 full pathname causes an '/' to be prepended to the Unix pathname.
5322 The algorithm for the rest of the pathname is as follows:
5323 For each segment between two ':',
5324 if it is non-null, copy as is and then add a '/' at the end,
5325 otherwise, insert a "../" into the Unix pathname.
5326 Returns 1 if successful; 0 if fails. */
5329 Mac2UnixPathname (const char *mfn
, char *ufn
, int ufnbuflen
)
5331 const char *p
, *q
, *pe
;
5338 p
= strchr (mfn
, ':');
5339 if (p
!= 0 && p
!= mfn
) /* full pathname */
5346 pe
= mfn
+ strlen (mfn
);
5349 q
= strchr (p
, ':');
5353 { /* two consecutive ':' */
5354 if (strlen (ufn
) + 3 >= ufnbuflen
)
5356 strcat (ufn
, "../");
5360 if (strlen (ufn
) + (q
- p
) + 1 >= ufnbuflen
)
5362 strncat (ufn
, p
, q
- p
);
5369 if (strlen (ufn
) + (pe
- p
) >= ufnbuflen
)
5371 strncat (ufn
, p
, pe
- p
); /* no separator for last one */
5379 extern char *GetTempDirName ();
5381 /* Convert a Unix pathname to Mac form. Approximately reverse of the
5382 above in algorithm. */
5384 Unix2MacPathname (const char *ufn
, char *mfn
, int mfnbuflen
)
5386 const char *p
, *q
, *pe
;
5387 char expandedPathname
[MAXPATHLEN
+1];
5396 /* Check for and handle volume names. Last comparison: strangely
5397 somewhere `/.emacs' is passed. A temporary fix for now. */
5398 if (*p
== '/' && strchr (p
+1, '/') == NULL
&& strcmp (p
, "/.emacs") != 0)
5400 if (strlen (p
) + 1 > mfnbuflen
)
5407 if (strncmp (p
, "~emacs/", 7) == 0)
5408 { /* expand to emacs dir found by InitEmacsPasswdDir */
5409 struct passwd
*pw
= getpwnam ("emacs");
5411 if (strlen (pw
->pw_dir
) + strlen (p
) > MAXPATHLEN
)
5413 strcpy (expandedPathname
, pw
->pw_dir
);
5414 strcat (expandedPathname
, p
);
5415 p
= expandedPathname
;
5416 /* Now p points to the pathname with emacs dir prefix. */
5418 else if (strncmp (p
, "/tmp/", 5) == 0)
5420 char *t
= GetTempDirName ();
5422 if (strlen (t
) + strlen (p
) > MAXPATHLEN
)
5424 strcpy (expandedPathname
, t
);
5425 strcat (expandedPathname
, p
);
5426 p
= expandedPathname
;
5427 /* Now p points to the pathname with emacs dir prefix. */
5429 else if (*p
!= '/') /* relative pathname */
5435 pe
= p
+ strlen (p
);
5438 q
= strchr (p
, '/');
5441 if (q
- p
== 2 && *p
== '.' && *(p
+1) == '.')
5443 if (strlen (mfn
) + 1 >= mfnbuflen
)
5449 if (strlen (mfn
) + (q
- p
) + 1 >= mfnbuflen
)
5451 strncat (mfn
, p
, q
- p
);
5458 if (strlen (mfn
) + (pe
- p
) >= mfnbuflen
)
5460 strncat (mfn
, p
, pe
- p
);
5468 /* The following functions with "sys_" prefix are stubs to Unix
5469 functions that have already been implemented by CW or MPW. The
5470 calls to them in Emacs source course are #define'd to call the sys_
5471 versions by the header files s-mac.h. In these stubs pathnames are
5472 converted between their Unix and Mac forms. */
5473 /* Unix Epoch is Jan 1, 1970 while Mac Epoch is Jan 1, 1904: 66 years
5475 #define MAC_UNIX_EPOCH_DIFF ((365L * 66 + 17) * 24 * 60 * 60)
5477 /* CW Epoch is Jan 1, 1900 (aaarghhhhh!); remember, 1900 is not a leap
5479 #define CW_UNIX_EPOCH_DIFF ((365L * 70 + 17) * 24 * 60 * 60)
5481 /* Define our own stat function for both MrC and CW. The reason for
5482 doing this: "stat" is both the name of a struct and function name:
5483 we can't #define stat to something else to
5484 redirect Emacs's calls to our own version that converts Unix style
5485 filenames to Mac style filename because all sorts of compilation
5486 errors will be generated if stat is #define'd to be something else. */
5489 stat (const char *path
, struct stat
*buf
)
5491 char MacPathname
[MAXPATHLEN
+1];
5494 if (Unix2MacPathname (path
, MacPathname
, MAXPATHLEN
+1) == 0)
5497 c2pstr (MacPathname
);
5498 cipb
.hFileInfo
.ioNamePtr
= MacPathname
;
5499 cipb
.hFileInfo
.ioVRefNum
= 0;
5500 cipb
.hFileInfo
.ioDirID
= 0;
5501 cipb
.hFileInfo
.ioFDirIndex
= 0; /* set to 0 to get information about specific dir or file */
5503 errno
= PBGetCatInfo (&cipb
, false);
5504 if (errno
== -43) /* -43: fnfErr defined in Errors.h */
5509 if (cipb
.hFileInfo
.ioFlAttrib
& 0x10)
5510 { /* bit 4 = 1 for directories */
5511 buf
->st_mode
= S_IFDIR
| S_IREAD
| S_IEXEC
;
5512 if (!(cipb
.hFileInfo
.ioFlAttrib
& 0x1)) /* bit 1 = 1 for locked files/directories */
5513 buf
->st_mode
|= S_IWRITE
;
5514 buf
->st_ino
= cipb
.dirInfo
.ioDrDirID
;
5515 buf
->st_dev
= cipb
.dirInfo
.ioVRefNum
;
5516 buf
->st_size
= cipb
.dirInfo
.ioDrNmFls
; /* size of dir = number of files and dirs */
5517 buf
->st_atime
= buf
->st_mtime
= cipb
.dirInfo
.ioDrMdDat
- MAC_UNIX_EPOCH_DIFF
;
5518 buf
->st_ctime
= cipb
.dirInfo
.ioDrCrDat
- MAC_UNIX_EPOCH_DIFF
;
5522 buf
->st_mode
= S_IFREG
| S_IREAD
;
5523 if (!(cipb
.hFileInfo
.ioFlAttrib
& 0x1)) /* bit 1 = 1 for locked files/directories */
5524 buf
->st_mode
|= S_IWRITE
;
5525 if (cipb
.hFileInfo
.ioFlFndrInfo
.fdType
== 'APPL')
5526 buf
->st_mode
|= S_IEXEC
;
5527 buf
->st_ino
= cipb
.hFileInfo
.ioDirID
;
5528 buf
->st_dev
= cipb
.hFileInfo
.ioVRefNum
;
5529 buf
->st_size
= cipb
.hFileInfo
.ioFlLgLen
;
5530 buf
->st_atime
= buf
->st_mtime
= cipb
.hFileInfo
.ioFlMdDat
- MAC_UNIX_EPOCH_DIFF
;
5531 buf
->st_ctime
= cipb
.hFileInfo
.ioFlCrDat
- MAC_UNIX_EPOCH_DIFF
;
5534 buf
->st_uid
= getuid ();
5535 buf
->st_gid
= getgid ();
5543 /* CW defines fstat in stat.mac.c while MPW does not provide this
5544 function. Without the information of how to get from a file
5545 descriptor in MPW StdCLib to a Mac OS file spec, it should be hard
5546 to implement this function. Fortunately, there is only one place
5547 where this function is called in our configuration: in fileio.c,
5548 where only the st_dev and st_ino fields are used to determine
5549 whether two fildes point to different i-nodes to prevent copying
5550 a file onto itself equal. What we have here probably needs
5553 fstat (int fildes
, struct stat
*buf
)
5556 buf
->st_ino
= fildes
;
5557 return 0; /* success */
5560 #endif /* __MRC__ */
5562 /* From Think Reference code example */
5564 mkdir (const char *dirname
, int mode
)
5566 #pragma unused (mode)
5569 char MacPathname
[MAXPATHLEN
+1];
5571 if (Unix2MacPathname (dirname
, MacPathname
, MAXPATHLEN
+1) == 0)
5574 c2pstr (MacPathname
);
5575 hfpb
.ioNamePtr
= MacPathname
;
5576 hfpb
.ioVRefNum
= 0; /*ignored unless name is invalid */
5577 hfpb
.ioDirID
= 0; /*parent is the root */
5579 /* Just return the Mac OSErr code for now. */
5580 errno
= PBDirCreate ((HParmBlkPtr
) &hfpb
, false);
5581 return errno
== noErr
? 0 : -1;
5585 rmdir (const char *dirname
)
5588 char MacPathname
[MAXPATHLEN
+1];
5590 if (Unix2MacPathname (dirname
, MacPathname
, MAXPATHLEN
+1) == 0)
5593 c2pstr (MacPathname
);
5594 hfpb
.ioNamePtr
= MacPathname
;
5595 hfpb
.ioVRefNum
= 0; /*ignored unless name is invalid */
5596 hfpb
.ioDirID
= 0; /*parent is the root */
5598 errno
= PBHDelete ((HParmBlkPtr
) &hfpb
, false);
5599 return errno
== noErr
? 0 : -1;
5604 /* No implementation yet. */
5606 execvp (const char *path
, ...)
5611 #endif /* __MRC__ */
5614 utime (const char *path
, const struct utimbuf
*times
)
5616 char MacPathname
[MAXPATHLEN
+1];
5619 if (Unix2MacPathname (path
, MacPathname
, MAXPATHLEN
+1) == 0)
5622 c2pstr (MacPathname
);
5623 cipb
.hFileInfo
.ioNamePtr
= MacPathname
;
5624 cipb
.hFileInfo
.ioVRefNum
= 0;
5625 cipb
.hFileInfo
.ioDirID
= 0;
5626 /* Set to 0 to get information about specific dir or file. */
5627 cipb
.hFileInfo
.ioFDirIndex
= 0;
5629 errno
= PBGetCatInfo (&cipb
, false);
5633 if (cipb
.hFileInfo
.ioFlAttrib
& 0x10)
5634 { /* bit 4 = 1 for directories */
5636 cipb
.dirInfo
.ioDrMdDat
= times
->modtime
+ MAC_UNIX_EPOCH_DIFF
;
5638 GetDateTime (&cipb
.dirInfo
.ioDrMdDat
);
5643 cipb
.hFileInfo
.ioFlMdDat
= times
->modtime
+ MAC_UNIX_EPOCH_DIFF
;
5645 GetDateTime (&cipb
.hFileInfo
.ioFlMdDat
);
5648 errno
= PBSetCatInfo (&cipb
, false);
5649 return errno
== noErr
? 0 : -1;
5656 /* Like stat, but test for access mode in hfpb.ioFlAttrib. */
5658 access (const char *path
, int mode
)
5660 char MacPathname
[MAXPATHLEN
+1];
5663 if (Unix2MacPathname (path
, MacPathname
, MAXPATHLEN
+1) == 0)
5666 c2pstr (MacPathname
);
5667 cipb
.hFileInfo
.ioNamePtr
= MacPathname
;
5668 cipb
.hFileInfo
.ioVRefNum
= 0;
5669 cipb
.hFileInfo
.ioDirID
= 0;
5670 cipb
.hFileInfo
.ioFDirIndex
= 0; /* set to 0 to get information about specific dir or file */
5672 errno
= PBGetCatInfo (&cipb
, false);
5676 if (mode
== F_OK
) /* got this far, file exists */
5680 if (cipb
.hFileInfo
.ioFlAttrib
& 0x10) /* path refers to a directory */
5684 if (cipb
.hFileInfo
.ioFlFndrInfo
.fdType
== 'APPL')
5691 return (cipb
.hFileInfo
.ioFlAttrib
& 0x1) ? -1 : 0; /* don't allow if lock bit on */
5696 #define DEV_NULL_FD 0x10000
5700 sys_open (const char *path
, int oflag
)
5702 char MacPathname
[MAXPATHLEN
+1];
5704 if (strcmp (path
, "/dev/null") == 0)
5705 return DEV_NULL_FD
; /* some bogus fd to be ignored in write */
5707 if (Unix2MacPathname (path
, MacPathname
, MAXPATHLEN
+1) == 0)
5710 return open (MacPathname
, oflag
);
5715 sys_creat (const char *path
, mode_t mode
)
5717 char MacPathname
[MAXPATHLEN
+1];
5719 if (Unix2MacPathname (path
, MacPathname
, MAXPATHLEN
+1) == 0)
5722 return creat (MacPathname
, mode
);
5727 sys_unlink (const char *path
)
5729 char MacPathname
[MAXPATHLEN
+1];
5731 if (Unix2MacPathname (path
, MacPathname
, MAXPATHLEN
+1) == 0)
5734 return unlink (MacPathname
);
5739 sys_read (int fildes
, char *buf
, int count
)
5742 { /* if stdin, call (non-echoing) "getch" in console.h */
5743 if (MacKeyPending ())
5744 { /* don't wait for a key if none has been pressed */
5745 *buf
= MacGetChar ();
5752 return read (fildes
, buf
, count
);
5757 sys_write (int fildes
, char *buf
, int count
)
5759 if (fildes
== DEV_NULL_FD
)
5762 return write (fildes
, buf
, count
);
5767 sys_rename (const char * old_name
, const char * new_name
)
5769 char MacOldName
[MAXPATHLEN
+1], MacNewName
[MAXPATHLEN
+1];
5771 if (strcmp (old_name
, new_name
) == 0)
5774 if (Unix2MacPathname (old_name
, MacOldName
, MAXPATHLEN
+1) == 0)
5777 if (Unix2MacPathname (new_name
, MacNewName
, MAXPATHLEN
+1) == 0)
5780 return rename (MacOldName
, MacNewName
);
5784 extern FILE *fopen (const char *name
, const char *mode
);
5786 sys_fopen (const char *name
, const char *mode
)
5788 char MacPathname
[MAXPATHLEN
+1];
5790 if (Unix2MacPathname (name
, MacPathname
, MAXPATHLEN
+1) == 0)
5793 return fopen (MacPathname
, mode
);
5798 long targetTicks
= 0;
5801 __sigfun alarm_signal_func
= (__sigfun
) 0;
5803 __signal_func_ptr alarm_signal_func
= (__signal_func_ptr
) 0;
5808 /* These functions simulate SIG_ALRM. The stub for function signal
5809 stores the signal handler function in alarm_signal_func if a
5810 SIG_ALRM is encountered. CheckAlarm is called in mac_read_socket,
5811 which emacs calls periodically. A pending alarm is represented by
5812 a non-zero targetTicks value. CheckAlarm calls the handler
5813 function pointed to by alarm_signal_func if one has been set up and
5814 an alarm is pending. */
5818 if (targetTicks
&& TickCount () > targetTicks
)
5821 if (alarm_signal_func
)
5822 (*alarm_signal_func
)(SIGALRM
);
5826 /* Called in sys_select to wait for an alarm signal to arrive. */
5830 unsigned long finalTick
;
5832 if (!targetTicks
) /* no alarm pending */
5835 while (TickCount () <= targetTicks
)
5836 Delay (1UL, &finalTick
); /* wait for 1/60 second before trying again */
5839 if (alarm_signal_func
)
5840 (*alarm_signal_func
)(SIGALRM
);
5848 long remaining
= targetTicks
? (TickCount () - targetTicks
) / 60 : 0;
5850 targetTicks
= seconds
? TickCount () + 60 * seconds
: 0;
5852 return (remaining
< 0) ? 0 : (unsigned int) remaining
;
5857 extern __sigfun
signal (int signal
, __sigfun signal_func
);
5859 sys_signal (int signal_num
, __sigfun signal_func
)
5861 extern __signal_func_ptr
signal (int signal
, __signal_func_ptr signal_func
);
5863 sys_signal (int signal_num
, __signal_func_ptr signal_func
)
5868 if (signal_num
!= SIGALRM
)
5869 return signal (signal_num
, signal_func
);
5873 __sigfun old_signal_func
;
5875 __signal_func_ptr old_signal_func
;
5879 old_signal_func
= alarm_signal_func
;
5880 alarm_signal_func
= signal_func
;
5881 return old_signal_func
;
5885 /* The time functions adjust time values according to the difference
5886 between the Unix and CW epoches. */
5889 extern struct tm
*gmtime (const time_t *);
5891 sys_gmtime (const time_t *timer
)
5893 time_t unixTime
= *timer
+ CW_UNIX_EPOCH_DIFF
;
5895 return gmtime (&unixTime
);
5899 extern struct tm
*localtime (const time_t *);
5901 sys_localtime (const time_t *timer
)
5903 time_t unixTime
= *timer
+ CW_UNIX_EPOCH_DIFF
;
5905 return localtime (&unixTime
);
5909 extern char *ctime (const time_t *);
5911 sys_ctime (const time_t *timer
)
5913 time_t unixTime
= *timer
+ CW_UNIX_EPOCH_DIFF
;
5915 return ctime (&unixTime
);
5919 extern time_t time (time_t *);
5921 sys_time (time_t *timer
)
5923 time_t macTime
= time (NULL
) - CW_UNIX_EPOCH_DIFF
;
5931 /* no subprocesses, empty wait */
5939 croak (char *badfunc
)
5941 printf ("%s not yet implemented\r\n", badfunc
);
5946 index (const char * str
, int chr
)
5948 return strchr (str
, chr
);
5952 char **environ
= &e
[0];
5955 mktemp (char *template)
5960 len
= strlen (template);
5962 while (k
>= 0 && template[k
] == 'X')
5965 k
++; /* make k index of first 'X' */
5969 /* Zero filled, number of digits equal to the number of X's. */
5970 sprintf (&template[k
], "%0*d", len
-k
, seqnum
++);
5978 /* Emulate getpwuid, getpwnam and others. */
5980 #define PASSWD_FIELD_SIZE 256
5982 static char myPasswdName
[PASSWD_FIELD_SIZE
];
5983 static char myPasswdDir
[MAXPATHLEN
+1];
5985 static struct passwd myPasswd
=
5991 /* Initialized by main () in macterm.c to pathname of emacs directory. */
5992 char emacsPasswdDir
[MAXPATHLEN
+1];
5995 InitEmacsPasswdDir ()
5999 if (getwd (emacsPasswdDir
) && getwd (myPasswdDir
))
6001 /* Need pathname of first ancestor that begins with `emacs' since
6002 Mac emacs application is somewhere in the emacs-20.3 tree. */
6003 int len
= strlen (emacsPasswdDir
);
6004 /* J points to the "/" following the directory name being compared. */
6007 while (i
>= 0 && !found
)
6009 while (i
>= 0 && emacsPasswdDir
[i
] != '/')
6011 if (emacsPasswdDir
[i
] == '/' && i
+5 < len
)
6012 found
= (strncmp (&(emacsPasswdDir
[i
+1]), "emacs", 5) == 0);
6014 emacsPasswdDir
[j
+1] = '\0';
6024 { /* setting to "/" probably won't work,
6025 but set it to something anyway. */
6026 strcpy (emacsPasswdDir
, "/");
6027 strcpy (myPasswdDir
, "/");
6031 static struct passwd emacsPasswd
=
6037 static int myPasswdInited
= 0;
6044 /* Note: myPasswdDir initialized in InitEmacsPasswdDir to directory
6045 where Emacs was started. */
6047 ownerName
= (char **) GetResource ('STR ',-16096);
6051 BlockMove ((unsigned char *) *ownerName
,
6052 (unsigned char *) myPasswdName
, *ownerName
[0] + 1);
6053 HUnlock (ownerName
);
6054 p2cstr ((unsigned char *) myPasswdName
);
6057 myPasswdName
[0] = 0;
6061 getpwuid (uid_t uid
)
6063 if (!myPasswdInited
)
6073 getpwnam (const char *name
)
6075 if (strcmp (name
, "emacs") == 0)
6076 return &emacsPasswd
;
6078 if (!myPasswdInited
)
6087 /* The functions fork, kill, sigsetmask, sigblock, request_sigio,
6088 setpgrp, setpriority, and unrequest_sigio are defined to be empty
6116 request_sigio (void)
6127 unrequest_sigio (void)
6131 /* djgpp does not implement pipe either. */
6133 pipe (int _fildes
[2])
6139 /* Hard and symbolic links. */
6141 symlink (const char *name1
, const char *name2
)
6148 link (const char *name1
, const char *name2
)
6155 lstat (const char *path
, struct stat
*sb
)
6157 return stat (path
, sb
);
6161 readlink (const char *path
, char *buf
, int bufsiz
)
6168 umask (mode_t numask
)
6170 static mode_t mask
= 022;
6171 mode_t oldmask
= mask
;
6177 chmod (const char *path
, mode_t mode
)
6179 /* say it always succeed for now */
6187 return fcntl (oldd
, F_DUPFD
, 0);
6189 /* current implementation of fcntl in fcntl.mac.c simply returns old
6191 return fcntl (oldd
, F_DUPFD
);
6197 /* This is from the original sysdep.c. Emulate BSD dup2. First close
6198 newd if it already exists. Then, attempt to dup oldd. If not
6199 successful, call dup2 recursively until we are, then close the
6200 unsuccessful ones. */
6202 dup2 (int oldd
, int newd
)
6213 ret
= dup2 (oldd
, newd
);
6218 /* let it fail for now */
6232 ioctl (int d
, int request
, void *argp
)
6241 if (fildes
>=0 && fildes
<= 2)
6272 sleep (unsigned int seconds
)
6274 unsigned long finalTick
;
6276 Delay (seconds
* 60UL, &finalTick
);
6279 #endif /* __MRC__ */
6288 #endif /* __MWERKS__ */
6290 /* Return the path to the directory in which Emacs can create
6291 temporary files. The MacOS "temporary items" directory cannot be
6292 used because it removes the file written by a process when it
6293 exits. In that sense it's more like "/dev/null" than "/tmp" (but
6294 again not exactly). And of course Emacs needs to read back the
6295 files written by its subprocesses. So here we write the files to a
6296 directory "Emacs" in the Preferences Folder. This directory is
6297 created if it does not exist. */
6301 static char *TempDirName
= NULL
;
6305 Str255 dirName
, fullPath
;
6307 char unixDirName
[MAXPATHLEN
+1];
6310 /* Cache directory name with pointer TempDirName.
6311 Look for it only the first time. */
6314 err
= FindFolder (kOnSystemDisk
, kPreferencesFolderType
,
6315 kCreateFolder
, &vRefNum
, &dirID
);
6320 cpb
.dirInfo
.ioNamePtr
= dirName
;
6321 cpb
.dirInfo
.ioDrParID
= dirID
;
6323 /* Standard ref num to full path name loop */
6325 cpb
.dirInfo
.ioVRefNum
= vRefNum
;
6326 cpb
.dirInfo
.ioFDirIndex
= -1;
6327 cpb
.dirInfo
.ioDrDirID
= cpb
.dirInfo
.ioDrParID
;
6329 err
= PBGetCatInfo (&cpb
, false);
6332 strcat (dirName
, ":");
6333 if (strlen (fullPath
) + strlen (dirName
) <= MAXPATHLEN
)
6335 strcat (dirName
, fullPath
);
6336 strcpy (fullPath
, dirName
);
6341 while (cpb
.dirInfo
.ioDrDirID
!= fsRtDirID
&& err
== noErr
);
6343 if (strlen (fullPath
) + 6 <= MAXPATHLEN
)
6344 strcat (fullPath
, "Emacs:");
6348 if (Mac2UnixPathname (fullPath
, unixDirName
, MAXPATHLEN
+1) == 0)
6351 dir
= opendir (unixDirName
); /* check whether temp directory exists */
6354 else if (mkdir (unixDirName
, 0700) != 0) /* create it if not */
6357 TempDirName
= (char *) malloc (strlen (unixDirName
) + 1);
6358 strcpy (TempDirName
, unixDirName
);
6365 getenv (const char * name
)
6367 if (strcmp (name
, "TERM") == 0)
6369 else if (strcmp (name
, "TERMCAP") == 0)
6370 /* for debugging purpose when code was still outputting to dumb terminal */
6371 return "d0|vt100|vt100-am|vt100am|dec vt100:do=[do]:co#100:li#32:cl=[cl]:sf=[sf]:km:\
6372 :le=[le]:bs:am:cm=[cm-%d,%d]:nd=[nd]:up=[up]:ce=[ce]:cd=[cd]:so=[so]:se=[se]:\
6373 :us=[us]:ue=[ue]:md=[md]:mr=[mr]:mb=[mb]:me=[me]:is=[is]:\
6374 :rf=/usr/share/lib/tabset/vt100:rs=[rs]:ks=[ks]:ke=[ke]:\
6375 :ku=\\036:kd=\\037:kr=\\035:kl=\\034:kb=[kb]:ho=[ho]:k1=[k1]:k2=[k2]:k3=[k3]:k4=[k4]:\
6376 :pt:sr=[sr]:vt#3:xn:sc=[sc]:rc=[rc]:cs=[cs-%d,%d]";
6377 else if (strcmp (name
, "TMPDIR") == 0)
6378 return GetTempDirName ();
6384 #include <utsname.h>
6387 uname (struct utsname
*name
)
6390 systemName
= GetString (-16413); /* IM - Resource Manager Reference */
6393 BlockMove (*systemName
, name
->nodename
, (*systemName
)[0]+1);
6394 p2cstr (name
->nodename
);
6401 #include <Processes.h>
6404 /* Event class of HLE sent to subprocess. */
6405 const OSType kEmacsSubprocessSend
= 'ESND';
6406 /* Event class of HLE sent back from subprocess. */
6407 const OSType kEmacsSubprocessReply
= 'ERPY';
6410 mystrchr (char *s
, char c
)
6412 while (*s
&& *s
!= c
)
6438 mystrcpy (char *to
, char *from
)
6449 /* Start a Mac subprocess. Arguments for it is passed in argv (null
6450 terminated). The process should run with the default directory
6451 "workdir", read input from "infn", and write output and error to
6452 "outfn" and "errfn", resp. The Process Manager call
6453 LaunchApplication is used to start the subprocess. We use high
6454 level events as the mechanism to pass arguments to the subprocess
6455 and to make Emacs wait for the subprocess to terminate and pass
6456 back a result code. The bulk of the code here packs the arguments
6457 into one message to be passed together with the high level event.
6458 Emacs also sometimes starts a subprocess using a shell to perform
6459 wildcard filename expansion. Since we don't really have a shell on
6460 the Mac, this case is detected and the starting of the shell is
6461 by-passed. We really need to add code here to do filename
6462 expansion to support such functionality. */
6464 run_mac_command (argv
, workdir
, infn
, outfn
, errfn
)
6465 unsigned char **argv
;
6466 const char *workdir
;
6467 const char *infn
, *outfn
, errfn
;
6469 char macappname
[MAXPATHLEN
+1], macworkdir
[MAXPATHLEN
+1];
6470 char macinfn
[MAXPATHLEN
+1], macoutfn
[MAXPATHLEN
+1], macerrfn
[MAXPATHLEN
+1];
6471 int paramlen
, argc
, newargc
, j
, retries
;
6472 char **newargv
, *param
, *p
;
6475 LaunchParamBlockRec lpbr
;
6476 EventRecord sendEvent
, replyEvent
;
6477 RgnHandle cursorRegionHdl
;
6479 unsigned long refCon
, len
;
6481 if (Unix2MacPathname (workdir
, macworkdir
, MAXPATHLEN
+1) == 0)
6483 if (Unix2MacPathname (infn
, macinfn
, MAXPATHLEN
+1) == 0)
6485 if (Unix2MacPathname (outfn
, macoutfn
, MAXPATHLEN
+1) == 0)
6487 if (Unix2MacPathname (errfn
, macerrfn
, MAXPATHLEN
+1) == 0)
6490 paramlen
= strlen (macworkdir
) + strlen (macinfn
) + strlen (macoutfn
) + strlen (macerrfn
) + 4;
6491 /* count nulls at end of strings */
6500 /* If a subprocess is invoked with a shell, we receive 3 arguments of the form:
6501 "<path to emacs bins>/sh" "-c" "<path to emacs bins>/<command> <command args>" */
6502 j
= strlen (argv
[0]);
6503 if (j
>= 3 && strcmp (argv
[0]+j
-3, "/sh") == 0 && argc
== 3 && strcmp (argv
[1], "-c") == 0)
6505 char *command
, *t
, tempmacpathname
[MAXPATHLEN
+1];
6507 /* The arguments for the command in argv[2] are separated by spaces. Count them and put
6508 the count in newargc. */
6509 command
= (char *) alloca (strlen (argv
[2])+2);
6510 strcpy (command
, argv
[2]);
6511 if (command
[strlen (command
) - 1] != ' ')
6512 strcat (command
, " ");
6516 t
= mystrchr (t
, ' ');
6520 t
= mystrchr (t
+1, ' ');
6523 newargv
= (char **) alloca (sizeof (char *) * newargc
);
6526 for (j
= 0; j
< newargc
; j
++)
6528 newargv
[j
] = (char *) alloca (strlen (t
) + 1);
6529 mystrcpy (newargv
[j
], t
);
6532 paramlen
+= strlen (newargv
[j
]) + 1;
6535 if (strncmp (newargv
[0], "~emacs/", 7) == 0)
6537 if (Unix2MacPathname (newargv
[0], tempmacpathname
, MAXPATHLEN
+1) == 0)
6541 { /* sometimes Emacs call "sh" without a path for the command */
6543 char *t
= (char *) alloca (strlen (newargv
[0]) + 7 + 1);
6544 strcpy (t
, "~emacs/");
6545 strcat (t
, newargv
[0]);
6548 openp (Vexec_path
, build_string (newargv
[0]), EXEC_SUFFIXES
, &path
, 1);
6552 if (Unix2MacPathname (XSTRING (path
)->data
, tempmacpathname
, MAXPATHLEN
+1) == 0)
6555 strcpy (macappname
, tempmacpathname
);
6559 if (Unix2MacPathname (argv
[0], macappname
, MAXPATHLEN
+1) == 0)
6562 newargv
= (char **) alloca (sizeof (char *) * argc
);
6564 for (j
= 1; j
< argc
; j
++)
6566 if (strncmp (argv
[j
], "~emacs/", 7) == 0)
6568 char *t
= strchr (argv
[j
], ' ');
6571 char tempcmdname
[MAXPATHLEN
+1], tempmaccmdname
[MAXPATHLEN
+1];
6572 strncpy (tempcmdname
, argv
[j
], t
-argv
[j
]);
6573 tempcmdname
[t
-argv
[j
]] = '\0';
6574 if (Unix2MacPathname (tempcmdname
, tempmaccmdname
, MAXPATHLEN
+1) == 0)
6576 newargv
[j
] = (char *) alloca (strlen (tempmaccmdname
) + strlen (t
) + 1);
6577 strcpy (newargv
[j
], tempmaccmdname
);
6578 strcat (newargv
[j
], t
);
6582 char tempmaccmdname
[MAXPATHLEN
+1];
6583 if (Unix2MacPathname (argv
[j
], tempmaccmdname
, MAXPATHLEN
+1) == 0)
6585 newargv
[j
] = (char *) alloca (strlen (tempmaccmdname
)+1);
6586 strcpy (newargv
[j
], tempmaccmdname
);
6590 newargv
[j
] = argv
[j
];
6591 paramlen
+= strlen (newargv
[j
]) + 1;
6595 /* After expanding all the arguments, we now know the length of the parameter block to be
6596 sent to the subprocess as a message attached to the HLE. */
6597 param
= (char *) malloc (paramlen
+ 1);
6602 *p
++ = newargc
; /* first byte of message contains number of arguments for command */
6603 strcpy (p
, macworkdir
);
6604 p
+= strlen (macworkdir
);
6605 *p
++ = '\0'; /* null terminate strings sent so it's possible to use strcpy over there */
6606 strcpy (p
, macinfn
);
6607 p
+= strlen (macinfn
);
6609 strcpy (p
, macoutfn
);
6610 p
+= strlen (macoutfn
);
6612 strcpy (p
, macerrfn
);
6613 p
+= strlen (macerrfn
);
6615 for (j
= 1; j
< newargc
; j
++) {
6616 strcpy (p
, newargv
[j
]);
6617 p
+= strlen (newargv
[j
]);
6621 c2pstr (macappname
);
6623 iErr
= FSMakeFSSpec (0, 0, macappname
, &spec
);
6625 if (iErr
!= noErr
) {
6630 lpbr
.launchBlockID
= extendedBlock
;
6631 lpbr
.launchEPBLength
= extendedBlockLen
;
6632 lpbr
.launchControlFlags
= launchContinue
+ launchNoFileFlags
;
6633 lpbr
.launchAppSpec
= &spec
;
6634 lpbr
.launchAppParameters
= NULL
;
6636 iErr
= LaunchApplication (&lpbr
); /* call the subprocess */
6637 if (iErr
!= noErr
) {
6642 sendEvent
.what
= kHighLevelEvent
;
6643 sendEvent
.message
= kEmacsSubprocessSend
; /* Event ID stored in "where" unused */
6646 do { /* OS may think current subprocess has terminated if previous one terminated recently */
6647 iErr
= PostHighLevelEvent (&sendEvent
, &lpbr
.launchProcessSN
, 0, param
, paramlen
+ 1, receiverIDisPSN
);
6649 while (iErr
== sessClosedErr
&& retries
-- > 0);
6651 if (iErr
!= noErr
) {
6656 cursorRegionHdl
= NewRgn ();
6658 /* Wait for the subprocess to finish, when it will send us a ERPY high level event */
6660 if (WaitNextEvent (highLevelEventMask
, &replyEvent
, 180, cursorRegionHdl
) && replyEvent
.message
== kEmacsSubprocessReply
)
6663 /* The return code is sent through the refCon */
6664 iErr
= AcceptHighLevelEvent (&targ
, &refCon
, NULL
, &len
);
6665 if (iErr
!= noErr
) {
6666 DisposeHandle ((Handle
) cursorRegionHdl
);
6671 DisposeHandle ((Handle
) cursorRegionHdl
);
6678 opendir (const char *dirname
)
6680 char MacPathname
[MAXPATHLEN
+1];
6685 dirp
= (DIR *) malloc (sizeof (DIR));
6689 /* Handle special case when dirname is "/": sets up for readir to
6690 get all mount volumes. */
6691 if (strcmp (dirname
, "/") == 0) {
6692 dirp
->getting_volumes
= 1; /* special all mounted volumes DIR struct */
6693 dirp
->current_index
= 1; /* index for first volume */
6697 /* Handle typical cases: not accessing all mounted volumes. */
6698 if (Unix2MacPathname (dirname
, MacPathname
, MAXPATHLEN
+1) == 0)
6701 /* Emacs calls opendir without the trailing '/', Mac needs trailing ':' */
6702 len
= strlen (MacPathname
);
6703 if (MacPathname
[len
- 1] != ':' && len
< MAXPATHLEN
)
6704 strcat (MacPathname
, ":");
6706 c2pstr (MacPathname
);
6707 cipb
.hFileInfo
.ioNamePtr
= MacPathname
; /* using full pathname so vRefNum and dirID ignored */
6708 cipb
.hFileInfo
.ioVRefNum
= 0;
6709 cipb
.hFileInfo
.ioDirID
= 0;
6710 cipb
.hFileInfo
.ioFDirIndex
= 0; /* set to 0 to get information about specific dir or file */
6712 errno
= PBGetCatInfo (&cipb
, false);
6713 if (errno
!= noErr
) {
6718 if (!(cipb
.hFileInfo
.ioFlAttrib
& 0x10)) /* bit 4 = 1 for directories */
6719 return 0; /* not a directory */
6721 dirp
->dir_id
= cipb
.dirInfo
.ioDrDirID
; /* used later in readdir */
6722 dirp
->getting_volumes
= 0;
6723 dirp
->current_index
= 1; /* index for first file/directory */
6739 HParamBlockRec HPBlock
;
6741 static struct dirent s_dirent
;
6742 static Str255 s_name
;
6745 /* Handle the root directory containing the mounted volumes. Call
6746 PBHGetVInfo specifying an index to obtain the info for a volume.
6747 PBHGetVInfo returns an error when it receives an index beyond the
6748 last volume, at which time we should return a nil dirent struct
6750 if (dp
->getting_volumes
) {
6751 HPBlock
.volumeParam
.ioNamePtr
= s_name
;
6752 HPBlock
.volumeParam
.ioVRefNum
= 0;
6753 HPBlock
.volumeParam
.ioVolIndex
= dp
->current_index
;
6755 errno
= PBHGetVInfo (&HPBlock
, false);
6756 if (errno
!= noErr
) {
6762 strcat (s_name
, "/"); /* need "/" for stat to work correctly */
6764 dp
->current_index
++;
6766 s_dirent
.d_ino
= cipb
.dirInfo
.ioDrDirID
;
6767 s_dirent
.d_name
= s_name
;
6772 cipb
.hFileInfo
.ioVRefNum
= 0;
6773 cipb
.hFileInfo
.ioNamePtr
= s_name
; /* location to receive filename returned */
6775 /* return only visible files */
6778 cipb
.hFileInfo
.ioDirID
= dp
->dir_id
; /* directory ID found by opendir */
6779 cipb
.hFileInfo
.ioFDirIndex
= dp
->current_index
;
6781 errno
= PBGetCatInfo (&cipb
, false);
6782 if (errno
!= noErr
) {
6787 /* insist on an visibile entry */
6788 if (cipb
.hFileInfo
.ioFlAttrib
& 0x10) /* directory? */
6789 done
= !(cipb
.dirInfo
.ioDrUsrWds
.frFlags
& fInvisible
);
6791 done
= !(cipb
.hFileInfo
.ioFlFndrInfo
.fdFlags
& fInvisible
);
6793 dp
->current_index
++;
6798 s_dirent
.d_ino
= cipb
.dirInfo
.ioDrDirID
; /* value unimportant: non-zero for valid file */
6799 s_dirent
.d_name
= s_name
;
6808 char MacPathname
[MAXPATHLEN
+1];
6809 Str255 directoryName
;
6813 MacPathname
[0] = '\0';
6814 directoryName
[0] = '\0';
6815 cipb
.dirInfo
.ioDrParID
= 0;
6816 cipb
.dirInfo
.ioNamePtr
= directoryName
; /* empty string = default directory */
6819 cipb
.dirInfo
.ioVRefNum
= 0;
6820 cipb
.dirInfo
.ioFDirIndex
= -1;
6821 cipb
.dirInfo
.ioDrDirID
= cipb
.dirInfo
.ioDrParID
; /* go up to parent each time */
6823 errno
= PBGetCatInfo (&cipb
, false);
6824 if (errno
!= noErr
) {
6829 p2cstr (directoryName
);
6830 strcat (directoryName
, ":");
6831 strcat (directoryName
, MacPathname
); /* attach to front since going up directory tree */
6832 strcpy (MacPathname
, directoryName
);
6833 } while (cipb
.dirInfo
.ioDrDirID
!= fsRtDirID
); /* until volume's root directory */
6835 if (Mac2UnixPathname (MacPathname
, path
, MAXPATHLEN
+1) == 0)
6841 #endif /* macintosh */