1 /* Interfaces to system-dependent kernel and library entries.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
32 /* Including stdlib.h isn't necessarily enough to get srandom
33 declared, e.g. without __USE_XOPEN_EXTENDED with glibc 2. */
35 /* The w32 build defines select stuff in w32.h, which is included by
36 sys/select.h (included below). */
38 #include "sysselect.h"
41 #include "blockinput.h"
45 #define write sys_write
50 #endif /* not WINDOWSNT */
52 /* Does anyone other than VMS need this? */
54 #define sys_fwrite fwrite
59 #include <sys/types.h>
64 #if !defined (USG) || defined (BSD_PGRPS)
66 #define setpgrp setpgid
70 /* Get SI_SRPC_DOMAIN, if it is available. */
71 #ifdef HAVE_SYS_SYSTEMINFO_H
72 #include <sys/systeminfo.h>
75 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
79 #include <sys/param.h>
83 extern unsigned start
__asm__ ("start");
105 #include <sys/file.h>
113 #define MAXIOSIZE (32 * PAGESIZE) /* Don't I/O more than 32 blocks at a time */
117 #include <sys/file.h>
125 #include <sys/ioctl.h>
132 #include <sys/utsname.h>
136 extern int quit_char
;
138 #include "keyboard.h"
141 #include "termhooks.h"
142 #include "termchar.h"
143 #include "termopts.h"
144 #include "dispextern.h"
146 #include "cm.h" /* for reset_sys_modes */
148 /* For serial_configure and serial_open. */
149 extern Lisp_Object QCport
, QCspeed
, QCprocess
;
150 extern Lisp_Object QCbytesize
, QCstopbits
, QCparity
, Qodd
, Qeven
;
151 extern Lisp_Object QCflowcontrol
, Qhw
, Qsw
, QCsummary
;
155 /* In process.h which conflicts with the local copy. */
157 int _cdecl
_spawnlp (int, const char *, const char *, ...);
158 int _cdecl
_getpid (void);
159 extern char *getwd (char *);
162 #ifdef NONSYSTEM_DIR_LIBRARY
164 #endif /* NONSYSTEM_DIR_LIBRARY */
166 #include "syssignal.h"
173 #ifndef HAVE_STRUCT_UTIMBUF
174 /* We want to use utime rather than utimes, but we couldn't find the
175 structure declaration. We'll use the traditional one. */
183 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
188 static int baud_convert
[] =
190 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
191 1800, 2400, 4800, 9600, 19200, 38400
197 #if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
199 #if defined (HAVE_TERMIOS_H) && defined (GNU_LINUX)
207 void croak
P_ ((char *)) NO_RETURN
;
209 /* Temporary used by `sigblock' when defined in terms of signprocmask. */
211 SIGMASKTYPE sigprocmask_set
;
214 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
216 /* Return the current working directory. Returns NULL on errors.
217 Any other returned value must be freed with free. This is used
218 only when get_current_dir_name is not defined on the system. */
220 get_current_dir_name ()
224 struct stat dotstat
, pwdstat
;
225 /* If PWD is accurate, use it instead of calling getwd. PWD is
226 sometimes a nicer name, and using it may avoid a fatal error if a
227 parent directory is searchable but not readable. */
228 if ((pwd
= getenv ("PWD")) != 0
229 && (IS_DIRECTORY_SEP (*pwd
) || (*pwd
&& IS_DEVICE_SEP (pwd
[1])))
230 && stat (pwd
, &pwdstat
) == 0
231 && stat (".", &dotstat
) == 0
232 && dotstat
.st_ino
== pwdstat
.st_ino
233 && dotstat
.st_dev
== pwdstat
.st_dev
235 && strlen (pwd
) < MAXPATHLEN
239 buf
= (char *) malloc (strlen (pwd
) + 1);
247 size_t buf_size
= 1024;
248 buf
= (char *) malloc (buf_size
);
253 if (getcwd (buf
, buf_size
) == buf
)
257 int tmp_errno
= errno
;
263 buf
= (char *) realloc (buf
, buf_size
);
271 /* We need MAXPATHLEN here. */
272 buf
= (char *) malloc (MAXPATHLEN
+ 1);
275 if (getwd (buf
) == NULL
)
277 int tmp_errno
= errno
;
289 /* Discard pending input on all input descriptors. */
295 struct emacs_tty buf
;
302 SYS$
QIOW (0, fileno (CURTTY()->input
), IO$_READVBLK
|IO$M_PURGE
, input_iosb
, 0, 0,
303 &buf
.main
, 0, 0, terminator_mask
, 0, 0);
306 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
307 while (dos_keyread () != -1)
309 #else /* not MSDOS */
311 struct tty_display_info
*tty
;
312 for (tty
= tty_list
; tty
; tty
= tty
->next
)
314 if (tty
->input
) /* Is the device suspended? */
316 EMACS_GET_TTY (fileno (tty
->input
), &buf
);
317 EMACS_SET_TTY (fileno (tty
->input
), &buf
, 0);
321 #endif /* not MSDOS */
323 #endif /* not WINDOWSNT */
329 /* Arrange for character C to be read as the next input from
331 XXX What if we have multiple ttys?
337 if (! FRAME_TERMCAP_P (SELECTED_FRAME ()))
340 /* Should perhaps error if in batch mode */
342 ioctl (fileno (CURTTY()->input
), TIOCSTI
, &c
);
343 #else /* no TIOCSTI */
344 error ("Cannot stuff terminal input characters in this version of Unix");
345 #endif /* no TIOCSTI */
351 init_baud_rate (int fd
)
359 #else /* not DOS_NT */
363 SYS$
QIOW (0, fd
, IO$_SENSEMODE
, &sg
, 0, 0,
364 &sg
.class, 12, 0, 0, 0, 0 );
365 emacs_ospeed
= sg
.xmit_baud
;
372 emacs_ospeed
= cfgetospeed (&sg
);
373 #else /* neither VMS nor TERMIOS */
381 ioctl (fd
, TCGETA
, &sg
);
383 emacs_ospeed
= sg
.c_cflag
& CBAUD
;
384 #else /* neither VMS nor TERMIOS nor TERMIO */
387 sg
.sg_ospeed
= B9600
;
388 if (ioctl (fd
, TIOCGETP
, &sg
) < 0)
390 emacs_ospeed
= sg
.sg_ospeed
;
391 #endif /* not HAVE_TERMIO */
392 #endif /* not HAVE_TERMIOS */
394 #endif /* not DOS_NT */
397 baud_rate
= (emacs_ospeed
< sizeof baud_convert
/ sizeof baud_convert
[0]
398 ? baud_convert
[emacs_ospeed
] : 9600);
406 set_exclusive_use (fd
)
410 ioctl (fd
, FIOCLEX
, 0);
412 /* Ok to do nothing if this feature does not exist */
417 wait_without_blocking ()
420 wait3 (0, WNOHANG
| WUNTRACED
, 0);
422 croak ("wait_without_blocking");
424 synch_process_alive
= 0;
427 #endif /* not subprocesses */
429 int wait_debugging
; /* Set nonzero to make following function work under dbx
430 (at least for bsd). */
433 wait_for_termination_signal ()
436 /* Wait for subprocess with process id `pid' to terminate and
437 make sure it will get eliminated (not remain forever as a zombie) */
440 wait_for_termination (pid
)
449 status
= SYS$
FORCEX (&pid
, 0, 0);
452 #if defined (BSD_SYSTEM) || defined (HPUX)
453 /* Note that kill returns -1 even if the process is just a zombie now.
454 But inevitably a SIGCHLD interrupt should be generated
455 and child_sig will do wait3 and make the process go away. */
456 /* There is some indication that there is a bug involved with
457 termination of subprocesses, perhaps involving a kernel bug too,
458 but no idea what it is. Just as a hunch we signal SIGCHLD to see
459 if that causes the problem to go away or get worse. */
460 sigsetmask (sigmask (SIGCHLD
));
461 if (0 > kill (pid
, 0))
463 sigsetmask (SIGEMPTYMASK
);
464 kill (getpid (), SIGCHLD
);
470 sigpause (SIGEMPTYMASK
);
471 #else /* not BSD_SYSTEM, and not HPUX version >= 6 */
472 #ifdef POSIX_SIGNALS /* would this work for GNU/Linux as well? */
473 sigblock (sigmask (SIGCHLD
));
475 if (kill (pid
, 0) == -1 && errno
== ESRCH
)
477 sigunblock (sigmask (SIGCHLD
));
481 sigsuspend (&empty_mask
);
482 #else /* not POSIX_SIGNALS */
483 #ifdef HAVE_SYSV_SIGPAUSE
485 if (0 > kill (pid
, 0))
491 #else /* not HAVE_SYSV_SIGPAUSE */
495 #else /* not WINDOWSNT */
496 if (0 > kill (pid
, 0))
498 /* Using sleep instead of pause avoids timing error.
499 If the inferior dies just before the sleep,
500 we lose just one second. */
502 #endif /* not WINDOWSNT */
503 #endif /* not HAVE_SYSV_SIGPAUSE */
504 #endif /* not POSIX_SIGNALS */
505 #endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
507 #else /* not subprocesses */
510 #else /* not __DJGPP__ > 1 */
511 if (kill (pid
, 0) < 0)
514 #endif /* not __DJGPP__ > 1*/
515 #endif /* not subprocesses */
522 * flush any pending output
523 * (may flush input as well; it does not matter the way we use it)
527 flush_pending_output (channel
)
531 /* If we try this, we get hit with SIGTTIN, because
532 the child's tty belongs to the child's pgrp. */
535 ioctl (channel
, TCFLSH
, 1);
539 /* 3rd arg should be ignored
540 but some 4.2 kernels actually want the address of an int
541 and nonzero means something different. */
542 ioctl (channel
, TIOCFLUSH
, &zero
);
549 /* Set up the terminal at the other end of a pseudo-terminal that
550 we will be controlling an inferior through.
551 It should not echo or do line-editing, since that is done
552 in Emacs. No padding needed for insertion into an Emacs buffer. */
555 child_setup_tty (out
)
561 EMACS_GET_TTY (out
, &s
);
563 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
564 s
.main
.c_oflag
|= OPOST
; /* Enable output postprocessing */
565 s
.main
.c_oflag
&= ~ONLCR
; /* Disable map of NL to CR-NL on output */
567 /* http://lists.gnu.org/archive/html/emacs-devel/2008-05/msg00406.html
568 Some versions of GNU Hurd do not have FFDLY? */
570 s
.main
.c_oflag
&= ~(NLDLY
|CRDLY
|TABDLY
|BSDLY
|VTDLY
|FFDLY
);
571 /* No output delays */
573 s
.main
.c_oflag
&= ~(NLDLY
|CRDLY
|TABDLY
|BSDLY
|VTDLY
);
574 /* No output delays */
577 s
.main
.c_lflag
&= ~ECHO
; /* Disable echo */
578 s
.main
.c_lflag
|= ISIG
; /* Enable signals */
580 s
.main
.c_iflag
&= ~IUCLC
; /* Disable downcasing on input. */
583 s
.main
.c_iflag
&= ~ISTRIP
; /* don't strip 8th bit on input */
586 s
.main
.c_oflag
&= ~OLCUC
; /* Disable upcasing on output. */
588 s
.main
.c_oflag
&= ~TAB3
; /* Disable tab expansion */
589 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CSIZE
) | CS8
; /* Don't strip 8th bit */
590 s
.main
.c_lflag
|= ICANON
; /* Enable erase/kill and eof processing */
591 s
.main
.c_cc
[VEOF
] = 04; /* insure that EOF is Control-D */
592 s
.main
.c_cc
[VERASE
] = CDISABLE
; /* disable erase processing */
593 s
.main
.c_cc
[VKILL
] = CDISABLE
; /* disable kill processing */
596 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CBAUD
) | B9600
; /* baud rate sanity */
599 #ifdef SIGNALS_VIA_CHARACTERS
600 /* the QUIT and INTR character are used in process_send_signal
601 so set them here to something useful. */
602 if (s
.main
.c_cc
[VQUIT
] == CDISABLE
)
603 s
.main
.c_cc
[VQUIT
] = '\\'&037; /* Control-\ */
604 if (s
.main
.c_cc
[VINTR
] == CDISABLE
)
605 s
.main
.c_cc
[VINTR
] = 'C'&037; /* Control-C */
606 #endif /* not SIGNALS_VIA_CHARACTERS */
609 /* AIX enhanced edit loses NULs, so disable it */
612 s
.main
.c_iflag
&= ~ASCEDIT
;
614 /* Also, PTY overloads NUL and BREAK.
615 don't ignore break, but don't signal either, so it looks like NUL. */
616 s
.main
.c_iflag
&= ~IGNBRK
;
617 s
.main
.c_iflag
&= ~BRKINT
;
618 /* rms: Formerly it set s.main.c_cc[VINTR] to 0377 here
619 unconditionally. Then a SIGNALS_VIA_CHARACTERS conditional
620 would force it to 0377. That looks like duplicated code. */
621 #ifndef SIGNALS_VIA_CHARACTERS
622 /* QUIT and INTR work better as signals, so disable character forms */
623 s
.main
.c_cc
[VQUIT
] = CDISABLE
;
624 s
.main
.c_cc
[VINTR
] = CDISABLE
;
625 s
.main
.c_lflag
&= ~ISIG
;
626 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
627 s
.main
.c_cc
[VEOL
] = CDISABLE
;
628 s
.main
.c_cflag
= (s
.main
.c_cflag
& ~CBAUD
) | B9600
; /* baud rate sanity */
631 #else /* not HAVE_TERMIO */
633 s
.main
.sg_flags
&= ~(ECHO
| CRMOD
| ANYP
| ALLDELAY
| RAW
| LCASE
635 s
.main
.sg_flags
|= LPASS8
;
636 s
.main
.sg_erase
= 0377;
637 s
.main
.sg_kill
= 0377;
638 s
.lmode
= LLITOUT
| s
.lmode
; /* Don't strip 8th bit */
640 #endif /* not HAVE_TERMIO */
642 EMACS_SET_TTY (out
, &s
, 0);
644 #endif /* not DOS_NT */
648 #endif /* subprocesses */
650 /* Record a signal code and the handler for it. */
654 SIGTYPE (*handler
) P_ ((int));
657 static void save_signal_handlers
P_ ((struct save_signal
*));
658 static void restore_signal_handlers
P_ ((struct save_signal
*));
660 /* Suspend the Emacs process; give terminal to its superior. */
666 /* "Foster" parentage allows emacs to return to a subprocess that attached
667 to the current emacs as a cheaper than starting a whole new process. This
668 is set up by KEPTEDITOR.COM. */
669 unsigned long parent_id
, foster_parent_id
;
672 fpid_string
= getenv ("EMACS_PARENT_PID");
673 if (fpid_string
!= NULL
)
675 sscanf (fpid_string
, "%x", &foster_parent_id
);
676 if (foster_parent_id
!= 0)
677 parent_id
= foster_parent_id
;
679 parent_id
= getppid ();
682 parent_id
= getppid ();
684 xfree (fpid_string
); /* On VMS, this was malloc'd */
686 if (parent_id
&& parent_id
!= 0xffffffff)
688 SIGTYPE (*oldsig
)() = (int) signal (SIGINT
, SIG_IGN
);
689 int status
= LIB$
ATTACH (&parent_id
) & 1;
690 signal (SIGINT
, oldsig
);
699 d_prompt
.l
= sizeof ("Emacs: "); /* Our special prompt */
700 d_prompt
.a
= "Emacs: "; /* Just a reminder */
701 LIB$
SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt
, 0);
706 #if defined (SIGTSTP) && !defined (MSDOS)
709 int pgrp
= EMACS_GETPGRP (0);
710 EMACS_KILLPG (pgrp
, SIGTSTP
);
713 #else /* No SIGTSTP */
714 #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
715 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
716 kill (getpid (), SIGQUIT
);
718 #else /* No SIGTSTP or USG_JOBCTRL */
720 /* On a system where suspending is not implemented,
721 instead fork a subshell and let it talk directly to the terminal
725 #endif /* no USG_JOBCTRL */
726 #endif /* no SIGTSTP */
730 /* Fork a 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 (SCHARS (dir
) + 2);
769 bcopy (SDATA (dir
), 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 EMACS_INT emacs_priority
;
812 if (emacs_priority
< 0)
813 nice (-emacs_priority
);
817 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
819 char *epwd
= getenv ("PWD");
820 char old_pwd
[MAXPATHLEN
+1+4];
822 /* If PWD is set, pass it with corrected value. */
825 strcpy (old_pwd
, epwd
);
826 if (str
[len
- 1] == '/')
828 setenv ("PWD", str
, 1);
833 putenv (old_pwd
); /* restore previous value */
835 #else /* not MSDOS */
837 /* Waits for process completion */
838 pid
= _spawnlp (_P_WAIT
, sh
, sh
, NULL
);
841 write (1, "Can't execute subshell", 22);
842 #else /* not WINDOWSNT */
843 execlp (sh
, sh
, (char *) 0);
844 write (1, "Can't execute subshell", 22);
846 #endif /* not WINDOWSNT */
847 #endif /* not MSDOS */
850 /* Do this now if we did not do it before. */
851 #if !defined (MSDOS) || __DJGPP__ == 1
852 save_signal_handlers (saved_handlers
);
853 synch_process_alive
= 1;
857 wait_for_termination (pid
);
859 restore_signal_handlers (saved_handlers
);
860 synch_process_alive
= 0;
865 save_signal_handlers (saved_handlers
)
866 struct save_signal
*saved_handlers
;
868 while (saved_handlers
->code
)
870 saved_handlers
->handler
871 = (SIGTYPE (*) P_ ((int))) signal (saved_handlers
->code
, SIG_IGN
);
877 restore_signal_handlers (saved_handlers
)
878 struct save_signal
*saved_handlers
;
880 while (saved_handlers
->code
)
882 signal (saved_handlers
->code
, saved_handlers
->handler
);
888 /* If SIGIO is broken, don't do anything. */
905 unrequest_sigio (void)
912 int old_fcntl_flags
[MAXDESC
];
919 old_fcntl_flags
[fd
] = fcntl (fd
, F_GETFL
, 0) & ~FASYNC
;
920 fcntl (fd
, F_SETFL
, old_fcntl_flags
[fd
] | FASYNC
);
922 interrupts_deferred
= 0;
930 fcntl (fd
, F_SETFL
, old_fcntl_flags
[fd
]);
934 #ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
935 /* XXX Uhm, FASYNC is not used anymore here. */
936 /* XXX Yeah, but you need it for SIGIO, don't you? */
945 sigunblock (sigmask (SIGWINCH
));
947 sigunblock (sigmask (SIGIO
));
949 interrupts_deferred
= 0;
953 unrequest_sigio (void)
958 #if 0 /* XXX What's wrong with blocking SIGIO under X? */
964 sigblock (sigmask (SIGWINCH
));
966 sigblock (sigmask (SIGIO
));
967 interrupts_deferred
= 1;
970 #else /* no FASYNC */
976 if (noninteractive
|| read_socket_hook
)
979 croak ("request_sigio");
985 if (noninteractive
|| read_socket_hook
)
988 croak ("unrequest_sigio");
996 /* Saving and restoring the process group of Emacs's terminal. */
1000 /* The process group of which Emacs was a member when it initially
1003 If Emacs was in its own process group (i.e. inherited_pgroup ==
1004 getpid ()), then we know we're running under a shell with job
1005 control (Emacs would never be run as part of a pipeline).
1008 If Emacs was not in its own process group, then we know we're
1009 running under a shell (or a caller) that doesn't know how to
1010 separate itself from Emacs (like sh). Emacs must be in its own
1011 process group in order to receive SIGIO correctly. In this
1012 situation, we put ourselves in our own pgroup, forcibly set the
1013 tty's pgroup to our pgroup, and make sure to restore and reinstate
1014 the tty's pgroup just like any other terminal setting. If
1015 inherited_group was not the tty's pgroup, then we'll get a
1016 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
1017 it goes foreground in the future, which is what should happen.
1019 This variable is initialized in emacs.c. */
1020 int inherited_pgroup
;
1022 /* Split off the foreground process group to Emacs alone. When we are
1023 in the foreground, but not started in our own process group,
1024 redirect the tty device handle FD to point to our own process
1025 group. We need to be in our own process group to receive SIGIO
1028 narrow_foreground_group (int fd
)
1032 setpgrp (0, inherited_pgroup
);
1034 /* XXX inherited_pgroup should not be zero here, but GTK seems to
1036 if (! inherited_pgroup
)
1037 abort (); /* Should not happen. */
1039 if (inherited_pgroup
!= me
)
1040 EMACS_SET_TTY_PGRP (fd
, &me
); /* XXX This only works on the controlling tty. */
1044 /* Set the tty to our original foreground group. */
1046 widen_foreground_group (int fd
)
1048 if (inherited_pgroup
!= getpid ())
1049 EMACS_SET_TTY_PGRP (fd
, &inherited_pgroup
);
1050 setpgrp (0, inherited_pgroup
);
1053 #endif /* BSD_PGRPS */
1055 /* Getting and setting emacs_tty structures. */
1057 /* Set *TC to the parameters associated with the terminal FD.
1058 Return zero if all's well, or -1 if we ran into an error we
1059 couldn't deal with. */
1061 emacs_get_tty (fd
, settings
)
1063 struct emacs_tty
*settings
;
1065 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
1067 /* We have those nifty POSIX tcmumbleattr functions. */
1068 bzero (&settings
->main
, sizeof (settings
->main
));
1069 if (tcgetattr (fd
, &settings
->main
) < 0)
1074 /* The SYSV-style interface? */
1075 if (ioctl (fd
, TCGETA
, &settings
->main
) < 0)
1080 /* Vehemently Monstrous System? :-) */
1081 if (! (SYS$
QIOW (0, fd
, IO$_SENSEMODE
, settings
, 0, 0,
1082 &settings
->main
.class, 12, 0, 0, 0, 0)
1088 /* I give up - I hope you have the BSD ioctls. */
1089 if (ioctl (fd
, TIOCGETP
, &settings
->main
) < 0)
1091 #endif /* not DOS_NT */
1096 /* Suivant - Do we have to get struct ltchars data? */
1098 if (ioctl (fd
, TIOCGLTC
, &settings
->ltchars
) < 0)
1102 /* How about a struct tchars and a wordful of lmode bits? */
1104 if (ioctl (fd
, TIOCGETC
, &settings
->tchars
) < 0
1105 || ioctl (fd
, TIOCLGET
, &settings
->lmode
) < 0)
1109 /* We have survived the tempest. */
1114 /* Set the parameters of the tty on FD according to the contents of
1115 *SETTINGS. If FLUSHP is non-zero, we discard input.
1116 Return 0 if all went well, and -1 if anything failed. */
1119 emacs_set_tty (fd
, settings
, flushp
)
1121 struct emacs_tty
*settings
;
1124 /* Set the primary parameters - baud rate, character size, etcetera. */
1127 /* We have those nifty POSIX tcmumbleattr functions.
1128 William J. Smith <wjs@wiis.wang.com> writes:
1129 "POSIX 1003.1 defines tcsetattr to return success if it was
1130 able to perform any of the requested actions, even if some
1131 of the requested actions could not be performed.
1132 We must read settings back to ensure tty setup properly.
1133 AIX requires this to keep tty from hanging occasionally." */
1134 /* This make sure that we don't loop indefinitely in here. */
1135 for (i
= 0 ; i
< 10 ; i
++)
1136 if (tcsetattr (fd
, flushp
? TCSAFLUSH
: TCSADRAIN
, &settings
->main
) < 0)
1147 bzero (&new, sizeof (new));
1148 /* Get the current settings, and see if they're what we asked for. */
1149 tcgetattr (fd
, &new);
1150 /* We cannot use memcmp on the whole structure here because under
1151 * aix386 the termios structure has some reserved field that may
1154 if ( new.c_iflag
== settings
->main
.c_iflag
1155 && new.c_oflag
== settings
->main
.c_oflag
1156 && new.c_cflag
== settings
->main
.c_cflag
1157 && new.c_lflag
== settings
->main
.c_lflag
1158 && memcmp (new.c_cc
, settings
->main
.c_cc
, NCCS
) == 0)
1166 /* The SYSV-style interface? */
1167 if (ioctl (fd
, flushp
? TCSETAF
: TCSETAW
, &settings
->main
) < 0)
1172 /* Vehemently Monstrous System? :-) */
1173 if (! (SYS$
QIOW (0, fd
, IO$_SETMODE
, &input_iosb
, 0, 0,
1174 &settings
->main
.class, 12, 0, 0, 0, 0)
1180 /* I give up - I hope you have the BSD ioctls. */
1181 if (ioctl (fd
, (flushp
) ? TIOCSETP
: TIOCSETN
, &settings
->main
) < 0)
1183 #endif /* not DOS_NT */
1189 /* Suivant - Do we have to get struct ltchars data? */
1191 if (ioctl (fd
, TIOCSLTC
, &settings
->ltchars
) < 0)
1195 /* How about a struct tchars and a wordful of lmode bits? */
1197 if (ioctl (fd
, TIOCSETC
, &settings
->tchars
) < 0
1198 || ioctl (fd
, TIOCLSET
, &settings
->lmode
) < 0)
1202 /* We have survived the tempest. */
1209 int old_fcntl_owner
[MAXDESC
];
1210 #endif /* F_SETOWN */
1212 /* This may also be defined in stdio,
1213 but if so, this does no harm,
1214 and using the same name avoids wasting the other one's space. */
1216 #if defined (USG) || defined (DGUX)
1217 unsigned char _sobuf
[BUFSIZ
+8];
1219 char _sobuf
[BUFSIZ
];
1223 static struct ltchars new_ltchars
= {-1,-1,-1,-1,-1,-1};
1226 static struct tchars new_tchars
= {-1,-1,-1,-1,-1,-1};
1229 /* Initialize the terminal mode on all tty devices that are currently
1233 init_all_sys_modes (void)
1235 struct tty_display_info
*tty
;
1236 for (tty
= tty_list
; tty
; tty
= tty
->next
)
1237 init_sys_modes (tty
);
1240 /* Initialize the terminal mode on the given tty device. */
1243 init_sys_modes (tty_out
)
1244 struct tty_display_info
*tty_out
;
1246 struct emacs_tty tty
;
1248 Vtty_erase_char
= Qnil
;
1253 if (!tty_out
->output
)
1254 return; /* The tty is suspended. */
1258 input_ef
= get_kbd_event_flag ();
1259 /* LIB$GET_EF (&input_ef); */
1260 SYS$
CLREF (input_ef
);
1261 waiting_for_ast
= 0;
1263 timer_ef
= get_timer_event_flag ();
1264 /* LIB$GET_EF (&timer_ef); */
1265 SYS$
CLREF (timer_ef
);
1266 if (input_ef
/ 32 != timer_ef
/ 32)
1267 croak ("Input and timer event flags in different clusters.");
1268 timer_eflist
= ((unsigned) 1 << (input_ef
% 32)) |
1269 ((unsigned) 1 << (timer_ef
% 32));
1271 sys_access_reinit ();
1277 /* read_socket_hook is not global anymore. I think doing this
1278 unconditionally will not cause any problems. */
1279 if (! read_socket_hook
&& EQ (Vinitial_window_system
, Qnil
))
1281 narrow_foreground_group (fileno (tty_out
->input
));
1284 if (! tty_out
->old_tty
)
1285 tty_out
->old_tty
= (struct emacs_tty
*) xmalloc (sizeof (struct emacs_tty
));
1287 EMACS_GET_TTY (fileno (tty_out
->input
), tty_out
->old_tty
);
1289 tty
= *tty_out
->old_tty
;
1291 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
1292 XSETINT (Vtty_erase_char
, tty
.main
.c_cc
[VERASE
]);
1294 tty
.main
.c_iflag
|= (IGNBRK
); /* Ignore break condition */
1295 tty
.main
.c_iflag
&= ~ICRNL
; /* Disable map of CR to NL on input */
1296 #ifdef INLCR /* I'm just being cautious,
1297 since I can't check how widespread INLCR is--rms. */
1298 tty
.main
.c_iflag
&= ~INLCR
; /* Disable map of NL to CR on input */
1301 tty
.main
.c_iflag
&= ~ISTRIP
; /* don't strip 8th bit on input */
1303 tty
.main
.c_lflag
&= ~ECHO
; /* Disable echo */
1304 tty
.main
.c_lflag
&= ~ICANON
; /* Disable erase/kill processing */
1306 tty
.main
.c_lflag
&= ~IEXTEN
; /* Disable other editing characters. */
1308 tty
.main
.c_lflag
|= ISIG
; /* Enable signals */
1309 if (tty_out
->flow_control
)
1311 tty
.main
.c_iflag
|= IXON
; /* Enable start/stop output control */
1313 tty
.main
.c_iflag
&= ~IXANY
;
1317 tty
.main
.c_iflag
&= ~IXON
; /* Disable start/stop output control */
1318 tty
.main
.c_oflag
&= ~ONLCR
; /* Disable map of NL to CR-NL
1320 tty
.main
.c_oflag
&= ~TAB3
; /* Disable tab expansion */
1322 if (tty_out
->meta_key
)
1324 tty
.main
.c_cflag
|= CS8
; /* allow 8th bit on input */
1325 tty
.main
.c_cflag
&= ~PARENB
;/* Don't check parity */
1328 if (tty_out
->input
== stdin
)
1330 tty
.main
.c_cc
[VINTR
] = quit_char
; /* C-g (usually) gives SIGINT */
1331 /* Set up C-g for both SIGQUIT and SIGINT.
1332 We don't know which we will get, but we handle both alike
1333 so which one it really gives us does not matter. */
1334 tty
.main
.c_cc
[VQUIT
] = quit_char
;
1338 /* We normally don't get interrupt or quit signals from tty
1339 devices other than our controlling terminal; therefore,
1340 we must handle C-g as normal input. Unfortunately, this
1341 means that the interrupt and quit feature must be
1342 disabled on secondary ttys, or we would not even see the
1345 Note that even though emacsclient could have special code
1346 to pass SIGINT to Emacs, we should _not_ enable
1347 interrupt/quit keys for emacsclient frames. This means
1348 that we can't break out of loops in C code from a
1349 secondary tty frame, but we can always decide what
1350 display the C-g came from, which is more important from a
1351 usability point of view. (Consider the case when two
1352 people work together using the same Emacs instance.) */
1353 tty
.main
.c_cc
[VINTR
] = CDISABLE
;
1354 tty
.main
.c_cc
[VQUIT
] = CDISABLE
;
1356 tty
.main
.c_cc
[VMIN
] = 1; /* Input should wait for at least 1 char */
1357 tty
.main
.c_cc
[VTIME
] = 0; /* no matter how long that takes. */
1359 tty
.main
.c_cc
[VSWTCH
] = CDISABLE
; /* Turn off shell layering use
1363 #if defined (__mips__) || defined (HAVE_TCATTR)
1365 tty
.main
.c_cc
[VSUSP
] = CDISABLE
; /* Turn off mips handling of C-z. */
1368 tty
.main
.c_cc
[V_DSUSP
] = CDISABLE
; /* Turn off mips handling of C-y. */
1369 #endif /* V_DSUSP */
1370 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1371 tty
.main
.c_cc
[VDSUSP
] = CDISABLE
;
1374 tty
.main
.c_cc
[VLNEXT
] = CDISABLE
;
1377 tty
.main
.c_cc
[VREPRINT
] = CDISABLE
;
1378 #endif /* VREPRINT */
1380 tty
.main
.c_cc
[VWERASE
] = CDISABLE
;
1381 #endif /* VWERASE */
1383 tty
.main
.c_cc
[VDISCARD
] = CDISABLE
;
1384 #endif /* VDISCARD */
1386 if (tty_out
->flow_control
)
1389 tty
.main
.c_cc
[VSTART
] = '\021';
1392 tty
.main
.c_cc
[VSTOP
] = '\023';
1398 tty
.main
.c_cc
[VSTART
] = CDISABLE
;
1401 tty
.main
.c_cc
[VSTOP
] = CDISABLE
;
1404 #endif /* mips or HAVE_TCATTR */
1408 /* AIX enhanced edit loses NULs, so disable it. */
1409 tty
.main
.c_line
= 0;
1410 tty
.main
.c_iflag
&= ~ASCEDIT
;
1412 tty
.main
.c_cc
[VSTRT
] = CDISABLE
;
1413 tty
.main
.c_cc
[VSTOP
] = CDISABLE
;
1414 tty
.main
.c_cc
[VSUSP
] = CDISABLE
;
1415 tty
.main
.c_cc
[VDSUSP
] = CDISABLE
;
1416 #endif /* IBMR2AIX */
1417 if (tty_out
->flow_control
)
1420 tty
.main
.c_cc
[VSTART
] = '\021';
1423 tty
.main
.c_cc
[VSTOP
] = '\023';
1426 /* Also, PTY overloads NUL and BREAK.
1427 don't ignore break, but don't signal either, so it looks like NUL.
1428 This really serves a purpose only if running in an XTERM window
1429 or via TELNET or the like, but does no harm elsewhere. */
1430 tty
.main
.c_iflag
&= ~IGNBRK
;
1431 tty
.main
.c_iflag
&= ~BRKINT
;
1433 #else /* if not HAVE_TERMIO */
1435 tty
.main
.tt_char
|= TT$M_NOECHO
;
1437 tty
.main
.tt_char
|= TT$M_EIGHTBIT
;
1438 if (tty_out
->flow_control
)
1439 tty
.main
.tt_char
|= TT$M_TTSYNC
;
1441 tty
.main
.tt_char
&= ~TT$M_TTSYNC
;
1442 tty
.main
.tt2_char
|= TT2$M_PASTHRU
| TT2$M_XON
;
1443 #else /* not VMS (BSD, that is) */
1445 XSETINT (Vtty_erase_char
, tty
.main
.sg_erase
);
1446 tty
.main
.sg_flags
&= ~(ECHO
| CRMOD
| XTABS
);
1448 tty
.main
.sg_flags
|= ANYP
;
1449 tty
.main
.sg_flags
|= interrupt_input
? RAW
: CBREAK
;
1450 #endif /* not DOS_NT */
1451 #endif /* not VMS (BSD, that is) */
1452 #endif /* not HAVE_TERMIO */
1454 /* If going to use CBREAK mode, we must request C-g to interrupt
1455 and turn off start and stop chars, etc. If not going to use
1456 CBREAK mode, do this anyway so as to turn off local flow
1457 control for user coming over network on 4.2; in this case,
1458 only t_stopc and t_startc really matter. */
1461 /* Note: if not using CBREAK mode, it makes no difference how we
1463 tty
.tchars
= new_tchars
;
1464 tty
.tchars
.t_intrc
= quit_char
;
1465 if (tty_out
->flow_control
)
1467 tty
.tchars
.t_startc
= '\021';
1468 tty
.tchars
.t_stopc
= '\023';
1471 tty
.lmode
= LDECCTQ
| LLITOUT
| LPASS8
| LNOFLSH
| tty_out
->old_tty
.lmode
;
1473 #endif /* HAVE_TCHARS */
1474 #endif /* not HAVE_TERMIO */
1477 tty
.ltchars
= new_ltchars
;
1478 #endif /* HAVE_LTCHARS */
1479 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
1480 if (!tty_out
->term_initted
)
1481 internal_terminal_init ();
1485 EMACS_SET_TTY (fileno (tty_out
->input
), &tty
, 0);
1487 /* This code added to insure that, if flow-control is not to be used,
1488 we have an unlocked terminal at the start. */
1491 if (!tty_out
->flow_control
) ioctl (fileno (tty_out
->input
), TCXONC
, 1);
1494 if (!tty_out
->flow_control
) ioctl (fileno (tty_out
->input
), TIOCSTART
, 0);
1497 #if defined (HAVE_TERMIOS) || defined (HPUX)
1499 if (!tty_out
->flow_control
) tcflow (fileno (tty_out
->input
), TCOON
);
1504 /* Appears to do nothing when in PASTHRU mode.
1505 SYS$QIOW (0, fileno (tty_out->input), IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
1506 interrupt_signal, oob_chars, 0, 0, 0, 0);
1508 queue_kbd_input (0);
1512 #ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
1513 if (interrupt_input
)
1515 old_fcntl_owner
[fileno (tty_out
->input
)] =
1516 fcntl (fileno (tty_out
->input
), F_GETOWN
, 0);
1517 fcntl (fileno (tty_out
->input
), F_SETOWN
, getpid ());
1518 init_sigio (fileno (tty_out
->input
));
1520 if (gpm_tty
== tty_out
)
1522 /* Arrange for mouse events to give us SIGIO signals. */
1523 fcntl (gpm_fd
, F_SETOWN
, getpid ());
1524 fcntl (gpm_fd
, F_SETFL
, fcntl (gpm_fd
, F_GETFL
, 0) | O_NONBLOCK
);
1525 init_sigio (gpm_fd
);
1527 #endif /* HAVE_GPM */
1529 #endif /* F_GETOWN */
1530 #endif /* F_SETFL */
1532 #ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1536 /* This symbol is defined on recent USG systems.
1537 Someone says without this call USG won't really buffer the file
1538 even with a call to setbuf. */
1539 setvbuf (tty_out
->output
, (char *) _sobuf
, _IOFBF
, sizeof _sobuf
);
1541 setbuf (tty_out
->output
, (char *) _sobuf
);
1544 if (tty_out
->terminal
->set_terminal_modes_hook
)
1545 tty_out
->terminal
->set_terminal_modes_hook (tty_out
->terminal
);
1547 if (!tty_out
->term_initted
)
1549 Lisp_Object tail
, frame
;
1550 FOR_EACH_FRAME (tail
, frame
)
1552 /* XXX This needs to be revised. */
1553 if (FRAME_TERMCAP_P (XFRAME (frame
))
1554 && FRAME_TTY (XFRAME (frame
)) == tty_out
)
1555 init_frame_faces (XFRAME (frame
));
1559 if (tty_out
->term_initted
&& no_redraw_on_reenter
)
1561 /* XXX This seems wrong on multi-tty. */
1562 if (display_completed
)
1563 direct_output_forward_char (0);
1567 Lisp_Object tail
, frame
;
1569 FOR_EACH_FRAME (tail
, frame
)
1571 if (FRAME_TERMCAP_P (XFRAME (frame
))
1572 && FRAME_TTY (XFRAME (frame
)) == tty_out
)
1573 FRAME_GARBAGED_P (XFRAME (frame
)) = 1;
1577 tty_out
->term_initted
= 1;
1580 /* Return nonzero if safe to use tabs in output.
1581 At the time this is called, init_sys_modes has not been done yet. */
1584 tabs_safe_p (int fd
)
1586 struct emacs_tty etty
;
1588 EMACS_GET_TTY (fd
, &etty
);
1589 return EMACS_TTY_TABS_OK (&etty
);
1592 /* Get terminal size from system.
1593 Store number of lines into *HEIGHTP and width into *WIDTHP.
1594 We store 0 if there's no valid information. */
1597 get_tty_size (int fd
, int *widthp
, int *heightp
)
1603 struct winsize size
;
1605 if (ioctl (fd
, TIOCGWINSZ
, &size
) == -1)
1606 *widthp
= *heightp
= 0;
1609 *widthp
= size
.ws_col
;
1610 *heightp
= size
.ws_row
;
1616 /* SunOS - style. */
1617 struct ttysize size
;
1619 if (ioctl (fd
, TIOCGSIZE
, &size
) == -1)
1620 *widthp
= *heightp
= 0;
1623 *widthp
= size
.ts_cols
;
1624 *heightp
= size
.ts_lines
;
1630 /* Use a fresh channel since the current one may have stale info
1631 (for example, from prior to a suspend); and to avoid a dependency
1632 in the init sequence. */
1634 struct sensemode tty
;
1636 SYS$
ASSIGN (&input_dsc
, &chan
, 0, 0);
1637 SYS$
QIOW (0, chan
, IO$_SENSEMODE
, &tty
, 0, 0,
1638 &tty
.class, 12, 0, 0, 0, 0);
1640 *widthp
= tty
.scr_wid
;
1641 *heightp
= tty
.scr_len
;
1645 *widthp
= ScreenCols ();
1646 *heightp
= ScreenRows ();
1647 #else /* system doesn't know size */
1651 #endif /* not VMS */
1652 #endif /* not SunOS-style */
1653 #endif /* not BSD-style */
1656 /* Set the logical window size associated with descriptor FD
1657 to HEIGHT and WIDTH. This is used mainly with ptys. */
1660 set_window_size (fd
, height
, width
)
1661 int fd
, height
, width
;
1666 struct winsize size
;
1667 size
.ws_row
= height
;
1668 size
.ws_col
= width
;
1670 if (ioctl (fd
, TIOCSWINSZ
, &size
) == -1)
1671 return 0; /* error */
1678 /* SunOS - style. */
1679 struct ttysize size
;
1680 size
.ts_lines
= height
;
1681 size
.ts_cols
= width
;
1683 if (ioctl (fd
, TIOCGSIZE
, &size
) == -1)
1689 #endif /* not SunOS-style */
1690 #endif /* not BSD-style */
1695 /* Prepare all terminal devices for exiting Emacs. */
1698 reset_all_sys_modes (void)
1700 struct tty_display_info
*tty
;
1701 for (tty
= tty_list
; tty
; tty
= tty
->next
)
1702 reset_sys_modes (tty
);
1705 /* Prepare the terminal for closing it; move the cursor to the
1706 bottom of the frame, turn off interrupt-driven I/O, etc. */
1709 reset_sys_modes (tty_out
)
1710 struct tty_display_info
*tty_out
;
1717 if (!tty_out
->term_initted
)
1720 if (!tty_out
->output
)
1721 return; /* The tty is suspended. */
1723 /* Go to and clear the last line of the terminal. */
1725 cmgoto (tty_out
, FrameRows (tty_out
) - 1, 0);
1727 /* Code adapted from tty_clear_end_of_line. */
1728 if (tty_out
->TS_clr_line
)
1730 emacs_tputs (tty_out
, tty_out
->TS_clr_line
, 1, cmputc
);
1733 { /* have to do it the hard way */
1735 tty_turn_off_insert (tty_out
);
1737 for (i
= curX (tty_out
); i
< FrameCols (tty_out
) - 1; i
++)
1739 fputc (' ', tty_out
->output
);
1743 cmgoto (tty_out
, FrameRows (tty_out
) - 1, 0);
1744 fflush (tty_out
->output
);
1746 if (tty_out
->terminal
->reset_terminal_modes_hook
)
1747 tty_out
->terminal
->reset_terminal_modes_hook (tty_out
->terminal
);
1750 /* Avoid possible loss of output when changing terminal modes. */
1751 fsync (fileno (tty_out
->output
));
1755 #ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
1756 if (interrupt_input
)
1758 reset_sigio (fileno (tty_out
->input
));
1759 fcntl (fileno (tty_out
->input
), F_SETOWN
,
1760 old_fcntl_owner
[fileno (tty_out
->input
)]);
1762 #endif /* F_SETOWN */
1764 fcntl (fileno (tty_out
->input
), F_SETFL
,
1765 fcntl (fileno (tty_out
->input
), F_GETFL
, 0) & ~O_NDELAY
);
1767 #endif /* F_SETFL */
1769 if (tty_out
->old_tty
)
1770 while (EMACS_SET_TTY (fileno (tty_out
->input
),
1771 tty_out
->old_tty
, 0) < 0 && errno
== EINTR
)
1774 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1779 widen_foreground_group (fileno (tty_out
->input
));
1785 /* Set up the proper status flags for use of a pty. */
1791 /* I'm told that TOICREMOTE does not mean control chars
1792 "can't be sent" but rather that they don't have
1793 input-editing or signaling effects.
1794 That should be good, because we have other ways
1795 to do those things in Emacs.
1796 However, telnet mode seems not to work on 4.2.
1797 So TIOCREMOTE is turned off now. */
1799 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1800 will hang. In particular, the "timeout" feature (which
1801 causes a read to return if there is no data available)
1802 does this. Also it is known that telnet mode will hang
1803 in such a way that Emacs must be stopped (perhaps this
1804 is the same problem).
1806 If TIOCREMOTE is turned off, then there is a bug in
1807 hp-ux which sometimes loses data. Apparently the
1808 code which blocks the master process when the internal
1809 buffer fills up does not work. Other than this,
1810 though, everything else seems to work fine.
1812 Since the latter lossage is more benign, we may as well
1813 lose that way. -- cph */
1815 #if defined(UNIX98_PTYS)
1818 ioctl (fd
, FIONBIO
, &on
);
1823 #endif /* HAVE_PTYS */
1827 /* Assigning an input channel is done at the start of Emacs execution.
1828 This is called each time Emacs is resumed, also, but does nothing
1829 because input_chain is no longer zero. */
1836 if (fileno (CURTTY ()->input
)) == 0)
1838 status
= SYS$
ASSIGN (&input_dsc
, &fileno (CURTTY ()->input
)), 0, 0);
1844 /* Deassigning the input channel is done before exiting. */
1849 return SYS$
DASSGN (fileno (CURTTY ()->input
)));
1854 /* Request reading one character into the keyboard buffer.
1855 This is done as soon as the buffer becomes empty. */
1861 extern kbd_input_ast ();
1863 waiting_for_ast
= 0;
1865 status
= SYS$
QIO (0, fileno (CURTTY()->input
), IO$_READVBLK
,
1866 &input_iosb
, kbd_input_ast
, 1,
1867 &input_buffer
, 1, 0, terminator_mask
, 0, 0);
1872 /* Ast routine that is called when keyboard input comes in
1873 in accord with the SYS$QIO above. */
1878 register int c
= -1;
1879 int old_errno
= errno
;
1880 extern EMACS_TIME
*input_available_clear_time
;
1882 if (waiting_for_ast
)
1883 SYS$
SETEF (input_ef
);
1884 waiting_for_ast
= 0;
1887 if (input_count
== 25)
1889 printf ("Ast # %d,", input_count
);
1890 printf (" iosb = %x, %x, %x, %x",
1891 input_iosb
.offset
, input_iosb
.status
, input_iosb
.termlen
,
1894 if (input_iosb
.offset
)
1898 printf (", char = 0%o", c
);
1910 struct input_event e
;
1913 e
.kind
= ASCII_KEYSTROKE_EVENT
;
1914 XSETINT (e
.code
, c
);
1915 e
.frame_or_window
= selected_frame
;
1916 kbd_buffer_store_event (&e
);
1918 if (input_available_clear_time
)
1919 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
1923 /* Wait until there is something in kbd_buffer. */
1926 wait_for_kbd_input ()
1928 extern int have_process_input
, process_exited
;
1930 /* If already something, avoid doing system calls. */
1931 if (detect_input_pending ())
1935 /* Clear a flag, and tell ast routine above to set it. */
1936 SYS$
CLREF (input_ef
);
1937 waiting_for_ast
= 1;
1938 /* Check for timing error: ast happened while we were doing that. */
1939 if (!detect_input_pending ())
1941 /* No timing error: wait for flag to be set. */
1942 set_waiting_for_input (0);
1943 SYS$
WFLOR (input_ef
, input_eflist
);
1944 clear_waiting_for_input ();
1945 if (!detect_input_pending ())
1946 /* Check for subprocess input availability */
1948 int dsp
= have_process_input
|| process_exited
;
1950 SYS$
CLREF (process_ef
);
1951 if (have_process_input
)
1952 process_command_input ();
1957 update_mode_lines
++;
1958 prepare_menu_bars ();
1959 redisplay_preserve_echo_area (18);
1963 waiting_for_ast
= 0;
1966 /* Get rid of any pending QIO, when we are about to suspend
1967 or when we want to throw away pending input.
1968 We wait for a positive sign that the AST routine has run
1969 and therefore there is no I/O request queued when we return.
1970 SYS$SETAST is used to avoid a timing error. */
1976 printf ("At end_kbd_input.\n");
1980 if (LIB$
AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
1982 SYS$
CANCEL (fileno (CURTTY()->input
));
1987 /* Clear a flag, and tell ast routine above to set it. */
1988 SYS$
CLREF (input_ef
);
1989 waiting_for_ast
= 1;
1991 SYS$
CANCEL (fileno (CURTTY()->input
));
1993 SYS$
WAITFR (input_ef
);
1994 waiting_for_ast
= 0;
1997 /* Wait for either input available or time interval expiry. */
2000 input_wait_timeout (timeval
)
2001 int timeval
; /* Time to wait, in seconds */
2004 static int zero
= 0;
2005 static int large
= -10000000;
2007 LIB$
EMUL (&timeval
, &large
, &zero
, time
); /* Convert to VMS format */
2009 /* If already something, avoid doing system calls. */
2010 if (detect_input_pending ())
2014 /* Clear a flag, and tell ast routine above to set it. */
2015 SYS$
CLREF (input_ef
);
2016 waiting_for_ast
= 1;
2017 /* Check for timing error: ast happened while we were doing that. */
2018 if (!detect_input_pending ())
2020 /* No timing error: wait for flag to be set. */
2022 if (SYS$
SETIMR (timer_ef
, time
, 0, 1) & 1) /* Set timer */
2023 SYS$
WFLOR (timer_ef
, timer_eflist
); /* Wait for timer expiry or input */
2025 waiting_for_ast
= 0;
2028 /* The standard `sleep' routine works some other way
2029 and it stops working if you have ever quit out of it.
2030 This one continues to work. */
2036 static int zero
= 0;
2037 static int large
= -10000000;
2039 LIB$
EMUL (&timeval
, &large
, &zero
, time
); /* Convert to VMS format */
2042 if (SYS$
SETIMR (timer_ef
, time
, 0, 1) & 1) /* Set timer */
2043 SYS$
WAITFR (timer_ef
); /* Wait for timer expiry only */
2064 croak ("request sigio");
2072 croak ("unrequest sigio");
2077 /* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
2082 #ifndef SYSTEM_MALLOC
2089 /* Some systems that cannot dump also cannot implement these. */
2092 * Return the address of the start of the text segment prior to
2093 * doing an unexec. After unexec the return value is undefined.
2094 * See crt0.c for further explanation and _start.
2098 #if !(defined (__NetBSD__) && defined (__ELF__))
2099 #ifndef HAVE_TEXT_START
2104 return ((char *) TEXT_START
);
2106 extern int _start ();
2107 return ((char *) _start
);
2108 #endif /* TEXT_START */
2110 #endif /* not HAVE_TEXT_START */
2114 * Return the address of the start of the data segment prior to
2115 * doing an unexec. After unexec the return value is undefined.
2116 * See crt0.c for further information and definition of data_start.
2118 * Apparently, on BSD systems this is etext at startup. On
2119 * USG systems (swapping) this is highly mmu dependent and
2120 * is also dependent on whether or not the program is running
2121 * with shared text. Generally there is a (possibly large)
2122 * gap between end of text and start of data with shared text.
2124 * On Uniplus+ systems with shared text, data starts at a
2125 * fixed address. Each port (from a given oem) is generally
2126 * different, and the specific value of the start of data can
2127 * be obtained via the UniPlus+ specific "uvar" system call,
2128 * however the method outlined in crt0.c seems to be more portable.
2130 * Probably what will have to happen when a USG unexec is available,
2131 * at least on UniPlus, is temacs will have to be made unshared so
2132 * that text and data are contiguous. Then once loadup is complete,
2133 * unexec will produce a shared executable where the data can be
2134 * at the normal shared text boundary and the startofdata variable
2135 * will be patched by unexec to the correct value.
2139 #ifndef start_of_data
2144 return ((char *) DATA_START
);
2146 #ifdef ORDINARY_LINK
2148 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
2149 * data_start isn't defined. We take the address of environ, which
2150 * is known to live at or near the start of the system crt0.c, and
2151 * we don't sweat the handful of bytes that might lose.
2153 extern char **environ
;
2155 return ((char *) &environ
);
2157 extern int data_start
;
2158 return ((char *) &data_start
);
2159 #endif /* ORDINARY_LINK */
2160 #endif /* DATA_START */
2162 #endif /* start_of_data */
2163 #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
2165 /* init_system_name sets up the string for the Lisp function
2166 system-name to return. */
2168 extern Lisp_Object Vsystem_name
;
2172 #include <sys/socket.h>
2174 #endif /* HAVE_SOCKETS */
2175 #endif /* not VMS */
2178 #ifndef HAVE_H_ERRNO
2181 #endif /* TRY_AGAIN */
2188 if ((sp
= egetenv ("SYS$NODE")) == 0)
2189 Vsystem_name
= build_string ("vax-vms");
2190 else if ((end
= index (sp
, ':')) == 0)
2191 Vsystem_name
= build_string (sp
);
2193 Vsystem_name
= make_string (sp
, end
- sp
);
2195 #ifndef HAVE_GETHOSTNAME
2198 Vsystem_name
= build_string (uts
.nodename
);
2199 #else /* HAVE_GETHOSTNAME */
2200 unsigned int hostname_size
= 256;
2201 char *hostname
= (char *) alloca (hostname_size
);
2203 /* Try to get the host name; if the buffer is too short, try
2204 again. Apparently, the only indication gethostname gives of
2205 whether the buffer was large enough is the presence or absence
2206 of a '\0' in the string. Eech. */
2209 gethostname (hostname
, hostname_size
- 1);
2210 hostname
[hostname_size
- 1] = '\0';
2212 /* Was the buffer large enough for the '\0'? */
2213 if (strlen (hostname
) < hostname_size
- 1)
2216 hostname_size
<<= 1;
2217 hostname
= (char *) alloca (hostname_size
);
2220 /* Turn the hostname into the official, fully-qualified hostname.
2221 Don't do this if we're going to dump; this can confuse system
2222 libraries on some machines and make the dumped emacs core dump. */
2225 #endif /* not CANNOT_DUMP */
2226 if (! index (hostname
, '.'))
2229 #ifdef HAVE_GETADDRINFO
2230 struct addrinfo
*res
;
2231 struct addrinfo hints
;
2234 memset (&hints
, 0, sizeof(hints
));
2235 hints
.ai_socktype
= SOCK_STREAM
;
2236 hints
.ai_flags
= AI_CANONNAME
;
2238 for (count
= 0;; count
++)
2240 if ((ret
= getaddrinfo (hostname
, NULL
, &hints
, &res
)) == 0
2241 || ret
!= EAI_AGAIN
)
2246 Fsleep_for (make_number (1), Qnil
);
2251 struct addrinfo
*it
= res
;
2254 char *fqdn
= it
->ai_canonname
;
2255 if (fqdn
&& index (fqdn
, '.')
2256 && strcmp (fqdn
, "localhost.localdomain") != 0)
2262 hostname
= alloca (strlen (it
->ai_canonname
) + 1);
2263 strcpy (hostname
, it
->ai_canonname
);
2267 #else /* !HAVE_GETADDRINFO */
2269 for (count
= 0;; count
++)
2275 hp
= gethostbyname (hostname
);
2277 if (! (hp
== 0 && h_errno
== TRY_AGAIN
))
2284 Fsleep_for (make_number (1), Qnil
);
2289 char *fqdn
= (char *) hp
->h_name
;
2291 if (!index (fqdn
, '.'))
2293 /* We still don't have a fully qualified domain name.
2294 Try to find one in the list of alternate names */
2295 char **alias
= hp
->h_aliases
;
2297 && (!index (*alias
, '.')
2298 || !strcmp (*alias
, "localhost.localdomain")))
2305 #endif /* !HAVE_GETADDRINFO */
2307 #endif /* HAVE_SOCKETS */
2308 Vsystem_name
= build_string (hostname
);
2309 #endif /* HAVE_GETHOSTNAME */
2313 for (p
= SDATA (Vsystem_name
); *p
; p
++)
2314 if (*p
== ' ' || *p
== '\t')
2321 #if !defined (HAVE_SELECT)
2323 #include "sysselect.h"
2326 #if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
2327 /* Cause explanatory error message at compile time,
2328 since the select emulation is not good enough for X. */
2329 int *x
= &x_windows_lose_if_no_select_system_call
;
2332 /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2333 * Only checks read descriptors.
2335 /* How long to wait between checking fds in select */
2336 #define SELECT_PAUSE 1
2339 /* For longjmp'ing back to read_input_waiting. */
2341 jmp_buf read_alarm_throw
;
2343 /* Nonzero if the alarm signal should throw back to read_input_waiting.
2344 The read_socket_hook function sets this to 1 while it is waiting. */
2346 int read_alarm_should_throw
;
2352 signal (SIGALRM
, SIG_IGN
);
2353 SIGNAL_THREAD_CHECK (SIGALRM
);
2354 if (read_alarm_should_throw
)
2355 longjmp (read_alarm_throw
, 1);
2359 /* Only rfds are checked. */
2361 sys_select (nfds
, rfds
, wfds
, efds
, timeout
)
2363 SELECT_TYPE
*rfds
, *wfds
, *efds
;
2364 EMACS_TIME
*timeout
;
2366 /* XXX This needs to be updated for multi-tty support. Is there
2367 anybody who needs to emulate select these days? */
2372 extern int proc_buffered_char
[];
2373 #ifndef subprocesses
2374 int process_tick
= 0, update_tick
= 0;
2376 extern int process_tick
, update_tick
;
2380 #if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
2381 /* If we're using X, then the native select will work; we only need the
2382 emulation for non-X usage. */
2383 if (!NILP (Vinitial_window_system
))
2384 return select (nfds
, rfds
, wfds
, efds
, timeout
);
2386 timeoutval
= timeout
? EMACS_SECS (*timeout
) : 100000;
2387 local_timeout
= &timeoutval
;
2399 /* If we are looking only for the terminal, with no timeout,
2400 just read it and wait -- that's more efficient. */
2401 if (*local_timeout
== 100000 && process_tick
== update_tick
2402 && FD_ISSET (0, &orfds
))
2405 for (fd
= 1; fd
< nfds
; ++fd
)
2406 if (FD_ISSET (fd
, &orfds
))
2408 if (! detect_input_pending ())
2409 read_input_waiting ();
2415 /* Once a second, till the timer expires, check all the flagged read
2416 * descriptors to see if any input is available. If there is some then
2417 * set the corresponding bit in the return copy of rfds.
2421 register int to_check
, fd
;
2425 for (to_check
= nfds
, fd
= 0; --to_check
>= 0; fd
++)
2427 if (FD_ISSET (fd
, &orfds
))
2429 int avail
= 0, status
= 0;
2432 avail
= detect_input_pending (); /* Special keyboard handler */
2436 status
= ioctl (fd
, FIONREAD
, &avail
);
2437 #else /* no FIONREAD */
2438 /* Hoping it will return -1 if nothing available
2439 or 0 if all 0 chars requested are read. */
2440 if (proc_buffered_char
[fd
] >= 0)
2444 avail
= read (fd
, &buf
, 1);
2446 proc_buffered_char
[fd
] = buf
;
2448 #endif /* no FIONREAD */
2450 if (status
>= 0 && avail
> 0)
2458 if (*local_timeout
== 0 || ravail
!= 0 || process_tick
!= update_tick
)
2461 turn_on_atimers (0);
2462 signal (SIGALRM
, select_alarm
);
2464 alarm (SELECT_PAUSE
);
2466 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2467 while (select_alarmed
== 0 && *local_timeout
!= 0
2468 && process_tick
== update_tick
)
2470 /* If we are interested in terminal input,
2471 wait by reading the terminal.
2472 That makes instant wakeup for terminal input at least. */
2473 if (FD_ISSET (0, &orfds
))
2475 read_input_waiting ();
2476 if (detect_input_pending ())
2482 (*local_timeout
) -= SELECT_PAUSE
;
2484 /* Reset the old alarm if there was one. */
2485 turn_on_atimers (1);
2487 if (*local_timeout
== 0) /* Stop on timer being cleared */
2492 #endif /* not WINDOWSNT */
2494 /* Read keyboard input into the standard buffer,
2495 waiting for at least one character. */
2498 read_input_waiting ()
2500 /* XXX This needs to be updated for multi-tty support. Is there
2501 anybody who needs to emulate select these days? */
2503 extern int quit_char
;
2505 if (read_socket_hook
)
2507 struct input_event hold_quit
;
2509 EVENT_INIT (hold_quit
);
2510 hold_quit
.kind
= NO_EVENT
;
2512 read_alarm_should_throw
= 0;
2513 if (! setjmp (read_alarm_throw
))
2514 nread
= (*read_socket_hook
) (0, 1, &hold_quit
);
2518 if (hold_quit
.kind
!= NO_EVENT
)
2519 kbd_buffer_store_event (&hold_quit
);
2523 struct input_event e
;
2525 nread
= read (fileno (stdin
), buf
, 1);
2528 /* Scan the chars for C-g and store them in kbd_buffer. */
2529 e
.kind
= ASCII_KEYSTROKE_EVENT
;
2530 e
.frame_or_window
= selected_frame
;
2532 for (i
= 0; i
< nread
; i
++)
2534 /* Convert chars > 0177 to meta events if desired.
2535 We do this under the same conditions that read_avail_input does. */
2536 if (read_socket_hook
== 0)
2538 /* If the user says she has a meta key, then believe her. */
2539 if (meta_key
== 1 && (buf
[i
] & 0x80))
2540 e
.modifiers
= meta_modifier
;
2545 XSETINT (e
.code
, buf
[i
]);
2546 kbd_buffer_store_event (&e
);
2547 /* Don't look at input that follows a C-g too closely.
2548 This reduces lossage due to autorepeat on C-g. */
2549 if (buf
[i
] == quit_char
)
2555 #if !defined (HAVE_SELECT)
2556 #define select sys_select
2559 #endif /* not HAVE_SELECT */
2560 #endif /* not VMS */
2561 #endif /* not MSDOS */
2563 /* POSIX signals support - DJB */
2564 /* Anyone with POSIX signals should have ANSI C declarations */
2566 #ifdef POSIX_SIGNALS
2568 sigset_t empty_mask
, full_mask
;
2571 sys_signal (int signal_number
, signal_handler_t action
)
2573 struct sigaction new_action
, old_action
;
2574 sigemptyset (&new_action
.sa_mask
);
2575 new_action
.sa_handler
= action
;
2576 #if defined (SA_RESTART) && ! defined (BROKEN_SA_RESTART) && !defined(SYNC_INPUT)
2577 /* Emacs mostly works better with restartable system services. If this
2578 flag exists, we probably want to turn it on here.
2579 However, on some systems this resets the timeout of `select'
2580 which means that `select' never finishes if it keeps getting signals.
2581 BROKEN_SA_RESTART is defined on those systems. */
2582 /* It's not clear why the comment above says "mostly works better". --Stef
2583 When SYNC_INPUT is set, we don't want SA_RESTART because we need to poll
2584 for pending input so we need long-running syscalls to be interrupted
2585 after a signal that sets the interrupt_input_pending flag. */
2586 new_action
.sa_flags
= SA_RESTART
;
2588 new_action
.sa_flags
= 0;
2590 sigaction (signal_number
, &new_action
, &old_action
);
2591 return (old_action
.sa_handler
);
2595 /* If we're compiling with GCC, we don't need this function, since it
2596 can be written as a macro. */
2598 sys_sigmask (int sig
)
2601 sigemptyset (&mask
);
2602 sigaddset (&mask
, sig
);
2607 /* I'd like to have these guys return pointers to the mask storage in here,
2608 but there'd be trouble if the code was saving multiple masks. I'll be
2609 safe and pass the structure. It normally won't be more than 2 bytes
2613 sys_sigblock (sigset_t new_mask
)
2616 sigprocmask (SIG_BLOCK
, &new_mask
, &old_mask
);
2621 sys_sigunblock (sigset_t new_mask
)
2624 sigprocmask (SIG_UNBLOCK
, &new_mask
, &old_mask
);
2629 sys_sigsetmask (sigset_t new_mask
)
2632 sigprocmask (SIG_SETMASK
, &new_mask
, &old_mask
);
2636 #endif /* POSIX_SIGNALS */
2638 #if !defined HAVE_STRSIGNAL && !HAVE_DECL_SYS_SIGLIST
2639 static char *my_sys_siglist
[NSIG
];
2643 # define sys_siglist my_sys_siglist
2649 #ifdef POSIX_SIGNALS
2650 sigemptyset (&empty_mask
);
2651 sigfillset (&full_mask
);
2654 #if !defined HAVE_STRSIGNAL && !HAVE_DECL_SYS_SIGLIST
2658 sys_siglist
[SIGABRT
] = "Aborted";
2661 sys_siglist
[SIGAIO
] = "LAN I/O interrupt";
2664 sys_siglist
[SIGALRM
] = "Alarm clock";
2667 sys_siglist
[SIGBUS
] = "Bus error";
2670 sys_siglist
[SIGCLD
] = "Child status changed";
2673 sys_siglist
[SIGCHLD
] = "Child status changed";
2676 sys_siglist
[SIGCONT
] = "Continued";
2679 sys_siglist
[SIGDANGER
] = "Swap space dangerously low";
2682 sys_siglist
[SIGDGNOTIFY
] = "Notification message in queue";
2685 sys_siglist
[SIGEMT
] = "Emulation trap";
2688 sys_siglist
[SIGFPE
] = "Arithmetic exception";
2691 sys_siglist
[SIGFREEZE
] = "SIGFREEZE";
2694 sys_siglist
[SIGGRANT
] = "Monitor mode granted";
2697 sys_siglist
[SIGHUP
] = "Hangup";
2700 sys_siglist
[SIGILL
] = "Illegal instruction";
2703 sys_siglist
[SIGINT
] = "Interrupt";
2706 sys_siglist
[SIGIO
] = "I/O possible";
2709 sys_siglist
[SIGIOINT
] = "I/O intervention required";
2712 sys_siglist
[SIGIOT
] = "IOT trap";
2715 sys_siglist
[SIGKILL
] = "Killed";
2718 sys_siglist
[SIGLOST
] = "Resource lost";
2721 sys_siglist
[SIGLWP
] = "SIGLWP";
2724 sys_siglist
[SIGMSG
] = "Monitor mode data available";
2727 sys_siglist
[SIGWIND
] = "SIGPHONE";
2730 sys_siglist
[SIGPIPE
] = "Broken pipe";
2733 sys_siglist
[SIGPOLL
] = "Pollable event occurred";
2736 sys_siglist
[SIGPROF
] = "Profiling timer expired";
2739 sys_siglist
[SIGPTY
] = "PTY I/O interrupt";
2742 sys_siglist
[SIGPWR
] = "Power-fail restart";
2745 sys_siglist
[SIGQUIT
] = "Quit";
2748 sys_siglist
[SIGRETRACT
] = "Need to relinguish monitor mode";
2751 sys_siglist
[SIGSAK
] = "Secure attention";
2754 sys_siglist
[SIGSEGV
] = "Segmentation violation";
2757 sys_siglist
[SIGSOUND
] = "Sound completed";
2760 sys_siglist
[SIGSTOP
] = "Stopped (signal)";
2763 sys_siglist
[SIGSTP
] = "Stopped (user)";
2766 sys_siglist
[SIGSYS
] = "Bad argument to system call";
2769 sys_siglist
[SIGTERM
] = "Terminated";
2772 sys_siglist
[SIGTHAW
] = "SIGTHAW";
2775 sys_siglist
[SIGTRAP
] = "Trace/breakpoint trap";
2778 sys_siglist
[SIGTSTP
] = "Stopped (user)";
2781 sys_siglist
[SIGTTIN
] = "Stopped (tty input)";
2784 sys_siglist
[SIGTTOU
] = "Stopped (tty output)";
2787 sys_siglist
[SIGURG
] = "Urgent I/O condition";
2790 sys_siglist
[SIGUSR1
] = "User defined signal 1";
2793 sys_siglist
[SIGUSR2
] = "User defined signal 2";
2796 sys_siglist
[SIGVTALRM
] = "Virtual timer expired";
2799 sys_siglist
[SIGWAITING
] = "Process's LWPs are blocked";
2802 sys_siglist
[SIGWINCH
] = "Window size changed";
2805 sys_siglist
[SIGWIND
] = "SIGWIND";
2808 sys_siglist
[SIGXCPU
] = "CPU time limit exceeded";
2811 sys_siglist
[SIGXFSZ
] = "File size limit exceeded";
2814 #endif /* !defined HAVE_STRSIGNAL && !defined HAVE_DECL_SYS_SIGLIST */
2823 /* Figure out how many bits the system's random number generator uses.
2824 `random' and `lrand48' are assumed to return 31 usable bits.
2825 BSD `rand' returns a 31 bit value but the low order bits are unusable;
2826 so we'll shift it and treat it like the 15-bit USG `rand'. */
2830 # define RAND_BITS 31
2831 # else /* !HAVE_RANDOM */
2832 # ifdef HAVE_LRAND48
2833 # define RAND_BITS 31
2834 # define random lrand48
2835 # else /* !HAVE_LRAND48 */
2836 # define RAND_BITS 15
2837 # if RAND_MAX == 32767
2838 # define random rand
2839 # else /* RAND_MAX != 32767 */
2840 # if RAND_MAX == 2147483647
2841 # define random() (rand () >> 16)
2842 # else /* RAND_MAX != 2147483647 */
2844 # define random rand
2846 # define random() (rand () >> 16)
2848 # endif /* RAND_MAX != 2147483647 */
2849 # endif /* RAND_MAX != 32767 */
2850 # endif /* !HAVE_LRAND48 */
2851 # endif /* !HAVE_RANDOM */
2852 #endif /* !RAND_BITS */
2859 srandom ((unsigned int)arg
);
2861 # ifdef HAVE_LRAND48
2864 srand ((unsigned int)arg
);
2870 * Build a full Emacs-sized word out of whatever we've got.
2871 * This suffices even for a 64-bit architecture with a 15-bit rand.
2876 long val
= random ();
2877 #if VALBITS > RAND_BITS
2878 val
= (val
<< RAND_BITS
) ^ random ();
2879 #if VALBITS > 2*RAND_BITS
2880 val
= (val
<< RAND_BITS
) ^ random ();
2881 #if VALBITS > 3*RAND_BITS
2882 val
= (val
<< RAND_BITS
) ^ random ();
2883 #if VALBITS > 4*RAND_BITS
2884 val
= (val
<< RAND_BITS
) ^ random ();
2885 #endif /* need at least 5 */
2886 #endif /* need at least 4 */
2887 #endif /* need at least 3 */
2888 #endif /* need at least 2 */
2889 return val
& ((1L << VALBITS
) - 1);
2895 /* If any place else asks for the TERM variable,
2896 allow it to be overridden with the EMACS_TERM variable
2897 before attempting to translate the logical name TERM. As a last
2898 resort, ask for VAX C's special idea of the TERM variable. */
2905 static char buf
[256];
2906 static struct dsc$descriptor_s equiv
2907 = {sizeof (buf
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, buf
};
2908 static struct dsc$descriptor_s d_name
2909 = {0, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, 0};
2912 if (!strcmp (name
, "TERM"))
2914 val
= (char *) getenv ("EMACS_TERM");
2919 d_name
.dsc$w_length
= strlen (name
);
2920 d_name
.dsc$a_pointer
= name
;
2921 if (LIB$
SYS_TRNLOG (&d_name
, &eqlen
, &equiv
) == 1)
2923 char *str
= (char *) xmalloc (eqlen
+ 1);
2924 bcopy (buf
, str
, eqlen
);
2926 /* This is a storage leak, but a pain to fix. With luck,
2927 no one will ever notice. */
2930 return (char *) getenv (name
);
2935 /* Since VMS doesn't believe in core dumps, the only way to debug this beast is
2936 to force a call on the debugger from within the image. */
2940 reset_all_sys_modes ();
2941 LIB$
SIGNAL (SS$_DEBUG
);
2947 #ifdef LINK_CRTL_SHARE
2948 #ifdef SHARABLE_LIB_BUG
2949 /* Variables declared noshare and initialized in sharable libraries
2950 cannot be shared. The VMS linker incorrectly forces you to use a private
2951 version which is uninitialized... If not for this "feature", we
2952 could use the C library definition of sys_nerr and sys_errlist. */
2954 char *sys_errlist
[] =
2958 "no such file or directory",
2960 "interrupted system call",
2962 "no such device or address",
2963 "argument list too long",
2964 "exec format error",
2967 "no more processes",
2968 "not enough memory",
2969 "permission denied",
2971 "block device required",
2972 "mount devices busy",
2974 "cross-device link",
2979 "file table overflow",
2980 "too many open files",
2984 "no space left on device",
2986 "read-only file system",
2992 "vax/vms specific error code nontranslatable error"
2994 #endif /* SHARABLE_LIB_BUG */
2995 #endif /* LINK_CRTL_SHARE */
2998 #ifndef HAVE_STRERROR
3004 extern char *sys_errlist
[];
3005 extern int sys_nerr
;
3007 if (errnum
>= 0 && errnum
< sys_nerr
)
3008 return sys_errlist
[errnum
];
3009 return (char *) "Unknown error";
3011 #endif /* not WINDOWSNT */
3012 #endif /* ! HAVE_STRERROR */
3015 emacs_open (path
, oflag
, mode
)
3019 register int rtnval
;
3021 while ((rtnval
= open (path
, oflag
, mode
)) == -1
3022 && (errno
== EINTR
))
3032 register int rtnval
;
3034 #if defined (MAC_OSX) && defined (HAVE_CARBON)
3036 extern int mac_try_close_socket
P_ ((int));
3038 if (mac_try_close_socket (fd
))
3043 while ((rtnval
= close (fd
)) == -1
3044 && (errno
== EINTR
))
3047 /* If close is interrupted SunOS 4.1 may or may not have closed the
3048 file descriptor. If it did the second close will fail with
3049 errno = EBADF. That means we have succeeded. */
3050 if (rtnval
== -1 && did_retry
&& errno
== EBADF
)
3057 emacs_read (fildes
, buf
, nbyte
)
3062 register int rtnval
;
3064 while ((rtnval
= read (fildes
, buf
, nbyte
)) == -1
3065 && (errno
== EINTR
))
3071 emacs_write (fildes
, buf
, nbyte
)
3076 register int rtnval
, bytes_written
;
3082 rtnval
= write (fildes
, buf
, nbyte
);
3089 /* I originally used `QUIT' but that might causes files to
3090 be truncated if you hit C-g in the middle of it. --Stef */
3091 if (interrupt_input_pending
)
3092 handle_async_input ();
3093 if (pending_atimers
)
3094 do_pending_atimers ();
3099 return (bytes_written
? bytes_written
: -1);
3104 bytes_written
+= rtnval
;
3106 return (bytes_written
);
3111 * All of the following are for USG.
3113 * On USG systems the system calls are INTERRUPTIBLE by signals
3114 * that the user program has elected to catch. Thus the system call
3115 * must be retried in these cases. To handle this without massive
3116 * changes in the source code, we remap the standard system call names
3117 * to names for our own functions in sysdep.c that do the system call
3118 * with retries. Actually, for portability reasons, it is good
3119 * programming practice, as this example shows, to limit all actual
3120 * system calls to a single occurrence in the source. Sure, this
3121 * adds an extra level of function call overhead but it is almost
3122 * always negligible. Fred Fish, Unisoft Systems Inc.
3126 * Warning, this function may not duplicate 4.2 action properly
3127 * under error conditions.
3131 /* In 4.1, param.h fails to define this. */
3132 #define MAXPATHLEN 1024
3141 char *npath
, *spath
;
3142 extern char *getcwd ();
3144 BLOCK_INPUT
; /* getcwd uses malloc */
3145 spath
= npath
= getcwd ((char *) 0, MAXPATHLEN
);
3151 /* On Altos 3068, getcwd can return @hostname/dir, so discard
3152 up to first slash. Should be harmless on other systems. */
3153 while (*npath
&& *npath
!= '/')
3155 strcpy (pathname
, npath
);
3156 free (spath
); /* getcwd uses malloc */
3161 #endif /* HAVE_GETWD */
3164 * Emulate rename using unlink/link. Note that this is
3165 * only partially correct. Also, doesn't enforce restriction
3166 * that files be of same type (regular->regular, dir->dir, etc).
3175 if (access (from
, 0) == 0)
3178 if (link (from
, to
) == 0)
3179 if (unlink (from
) == 0)
3191 /* HPUX curses library references perror, but as far as we know
3192 it won't be called. Anyway this definition will do for now. */
3198 #endif /* not HAVE_PERROR */
3204 * Emulate BSD dup2. First close newd if it already exists.
3205 * Then, attempt to dup oldd. If not successful, call dup2 recursively
3206 * until we are, then close the unsuccessful ones.
3213 register int fd
, ret
;
3218 return fcntl (oldd
, F_DUPFD
, newd
);
3225 ret
= dup2 (old
,new);
3231 #endif /* not HAVE_DUP2 */
3234 * Gettimeofday. Simulate as much as possible. Only accurate
3235 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3236 * Only needed when subprocesses are defined.
3241 #ifndef HAVE_GETTIMEOFDAY
3246 gettimeofday (tp
, tzp
)
3248 struct timezone
*tzp
;
3250 extern long time ();
3252 tp
->tv_sec
= time ((long *)0);
3255 tzp
->tz_minuteswest
= -1;
3262 #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
3265 * This function will go away as soon as all the stubs fixed. (fnf)
3272 printf ("%s not yet implemented\r\n", badfunc
);
3273 reset_all_sys_modes ();
3279 /* Directory routines for systems that don't have them. */
3281 #ifdef SYSV_SYSTEM_DIR
3285 #if defined (BROKEN_CLOSEDIR) || !defined (HAVE_CLOSEDIR)
3289 register DIR *dirp
; /* stream from opendir */
3293 rtnval
= emacs_close (dirp
->dd_fd
);
3295 /* Some systems (like Solaris) allocate the buffer and the DIR all
3296 in one block. Why in the world are we freeing this ourselves
3298 #if ! defined (SOLARIS2)
3299 xfree ((char *) dirp
->dd_buf
); /* directory block defined in <dirent.h> */
3301 xfree ((char *) dirp
);
3305 #endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
3306 #endif /* SYSV_SYSTEM_DIR */
3308 #ifdef NONSYSTEM_DIR_LIBRARY
3312 char *filename
; /* name of directory */
3314 register DIR *dirp
; /* -> malloc'ed storage */
3315 register int fd
; /* file descriptor for read */
3316 struct stat sbuf
; /* result of fstat */
3318 fd
= emacs_open (filename
, O_RDONLY
, 0);
3323 if (fstat (fd
, &sbuf
) < 0
3324 || (sbuf
.st_mode
& S_IFMT
) != S_IFDIR
3325 || (dirp
= (DIR *) xmalloc (sizeof (DIR))) == 0)
3329 return 0; /* bad luck today */
3334 dirp
->dd_loc
= dirp
->dd_size
= 0; /* refill needed */
3341 register DIR *dirp
; /* stream from opendir */
3343 emacs_close (dirp
->dd_fd
);
3344 xfree ((char *) dirp
);
3352 ino_t od_ino
; /* inode */
3353 char od_name
[DIRSIZ
]; /* filename */
3355 #endif /* not VMS */
3357 struct direct dir_static
; /* simulated directory contents */
3362 register DIR *dirp
; /* stream from opendir */
3365 register struct olddir
*dp
; /* -> directory data */
3367 register struct dir$_name
*dp
; /* -> directory data */
3368 register struct dir$_version
*dv
; /* -> version data */
3373 if (dirp
->dd_loc
>= dirp
->dd_size
)
3374 dirp
->dd_loc
= dirp
->dd_size
= 0;
3376 if (dirp
->dd_size
== 0 /* refill buffer */
3377 && (dirp
->dd_size
= emacs_read (dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
)) <= 0)
3381 dp
= (struct olddir
*) &dirp
->dd_buf
[dirp
->dd_loc
];
3382 dirp
->dd_loc
+= sizeof (struct olddir
);
3384 if (dp
->od_ino
!= 0) /* not deleted entry */
3386 dir_static
.d_ino
= dp
->od_ino
;
3387 strncpy (dir_static
.d_name
, dp
->od_name
, DIRSIZ
);
3388 dir_static
.d_name
[DIRSIZ
] = '\0';
3389 dir_static
.d_namlen
= strlen (dir_static
.d_name
);
3390 dir_static
.d_reclen
= sizeof (struct direct
)
3392 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3393 return &dir_static
; /* -> simulated structure */
3396 dp
= (struct dir$_name
*) dirp
->dd_buf
;
3397 if (dirp
->dd_loc
== 0)
3398 dirp
->dd_loc
= (dp
->dir$b_namecount
&1) ? dp
->dir$b_namecount
+ 1
3399 : dp
->dir$b_namecount
;
3400 dv
= (struct dir$_version
*)&dp
->dir$t_name
[dirp
->dd_loc
];
3401 dir_static
.d_ino
= dv
->dir$w_fid_num
;
3402 dir_static
.d_namlen
= dp
->dir$b_namecount
;
3403 dir_static
.d_reclen
= sizeof (struct direct
)
3405 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3406 strncpy (dir_static
.d_name
, dp
->dir$t_name
, dp
->dir$b_namecount
);
3407 dir_static
.d_name
[dir_static
.d_namlen
] = '\0';
3408 dirp
->dd_loc
= dirp
->dd_size
; /* only one record at a time */
3415 /* readdirver is just like readdir except it returns all versions of a file
3416 as separate entries. */
3421 register DIR *dirp
; /* stream from opendir */
3423 register struct dir$_name
*dp
; /* -> directory data */
3424 register struct dir$_version
*dv
; /* -> version data */
3426 if (dirp
->dd_loc
>= dirp
->dd_size
- sizeof (struct dir$_name
))
3427 dirp
->dd_loc
= dirp
->dd_size
= 0;
3429 if (dirp
->dd_size
== 0 /* refill buffer */
3430 && (dirp
->dd_size
= sys_read (dirp
->dd_fd
, dirp
->dd_buf
, DIRBLKSIZ
)) <= 0)
3433 dp
= (struct dir$_name
*) dirp
->dd_buf
;
3434 if (dirp
->dd_loc
== 0)
3435 dirp
->dd_loc
= (dp
->dir$b_namecount
& 1) ? dp
->dir$b_namecount
+ 1
3436 : dp
->dir$b_namecount
;
3437 dv
= (struct dir$_version
*) &dp
->dir$t_name
[dirp
->dd_loc
];
3438 strncpy (dir_static
.d_name
, dp
->dir$t_name
, dp
->dir$b_namecount
);
3439 sprintf (&dir_static
.d_name
[dp
->dir$b_namecount
], ";%d", dv
->dir$w_version
);
3440 dir_static
.d_namlen
= strlen (dir_static
.d_name
);
3441 dir_static
.d_ino
= dv
->dir$w_fid_num
;
3442 dir_static
.d_reclen
= sizeof (struct direct
) - MAXNAMLEN
+ 3
3443 + dir_static
.d_namlen
- dir_static
.d_namlen
% 4;
3444 dirp
->dd_loc
= ((char *) (++dv
) - dp
->dir$t_name
);
3450 #endif /* NONSYSTEM_DIR_LIBRARY */
3454 set_file_times (filename
, atime
, mtime
)
3455 const char *filename
;
3456 EMACS_TIME atime
, mtime
;
3459 struct timeval tv
[2];
3462 return utimes (filename
, tv
);
3463 #else /* not HAVE_UTIMES */
3465 utb
.actime
= EMACS_SECS (atime
);
3466 utb
.modtime
= EMACS_SECS (mtime
);
3467 return utime (filename
, &utb
);
3468 #endif /* not HAVE_UTIMES */
3471 /* mkdir and rmdir functions, for systems which don't have them. */
3475 * Written by Robert Rother, Mariah Corporation, August 1985.
3477 * If you want it, it's yours. All I ask in return is that if you
3478 * figure out how to do this in a Bourne Shell script you send me
3480 * sdcsvax!rmr or rmr@uscd
3482 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
3483 * subroutine. 11Mar86; hoptoad!gnu
3485 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3486 * subroutine didn't return EEXIST. It does now.
3492 #ifdef MKDIR_PROTOTYPE
3496 mkdir (dpath
, dmode
)
3501 int cpid
, status
, fd
;
3502 struct stat statbuf
;
3504 if (stat (dpath
, &statbuf
) == 0)
3506 errno
= EEXIST
; /* Stat worked, so it already exists */
3510 /* If stat fails for a reason other than non-existence, return error */
3511 if (errno
!= ENOENT
)
3514 synch_process_alive
= 1;
3515 switch (cpid
= fork ())
3518 case -1: /* Error in fork */
3519 return (-1); /* Errno is set already */
3521 case 0: /* Child process */
3523 * Cheap hack to set mode of new directory. Since this
3524 * child process is going away anyway, we zap its umask.
3525 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3526 * directory. Does anybody care?
3528 status
= umask (0); /* Get current umask */
3529 status
= umask (status
| (0777 & ~dmode
)); /* Set for mkdir */
3530 fd
= emacs_open ("/dev/null", O_RDWR
, 0);
3537 execl ("/bin/mkdir", "mkdir", dpath
, (char *) 0);
3538 _exit (-1); /* Can't exec /bin/mkdir */
3540 default: /* Parent process */
3541 wait_for_termination (cpid
);
3544 if (synch_process_death
!= 0 || synch_process_retcode
!= 0
3545 || synch_process_termsig
!= 0)
3547 errno
= EIO
; /* We don't know why, but */
3548 return -1; /* /bin/mkdir failed */
3553 #endif /* not HAVE_MKDIR */
3560 int cpid
, status
, fd
;
3561 struct stat statbuf
;
3563 if (stat (dpath
, &statbuf
) != 0)
3565 /* Stat just set errno. We don't have to */
3569 synch_process_alive
= 1;
3570 switch (cpid
= fork ())
3573 case -1: /* Error in fork */
3574 return (-1); /* Errno is set already */
3576 case 0: /* Child process */
3577 fd
= emacs_open ("/dev/null", O_RDWR
, 0);
3584 execl ("/bin/rmdir", "rmdir", dpath
, (char *) 0);
3585 _exit (-1); /* Can't exec /bin/rmdir */
3587 default: /* Parent process */
3588 wait_for_termination (cpid
);
3591 if (synch_process_death
!= 0 || synch_process_retcode
!= 0
3592 || synch_process_termsig
!= 0)
3594 errno
= EIO
; /* We don't know why, but */
3595 return -1; /* /bin/rmdir failed */
3600 #endif /* !HAVE_RMDIR */
3604 /* Functions for VMS */
3610 /* Return as a string the VMS error string pertaining to STATUS.
3611 Reuses the same static buffer each time it is called. */
3615 int status
; /* VMS status code */
3619 static char buf
[257];
3621 bufadr
[0] = sizeof buf
- 1;
3622 bufadr
[1] = (int) buf
;
3623 if (! (SYS$
GETMSG (status
, &len
, bufadr
, 0x1, 0) & 1))
3624 return "untranslatable VMS error status";
3632 /* The following is necessary because 'access' emulation by VMS C (2.0) does
3633 * not work correctly. (It also doesn't work well in version 2.3.)
3638 #define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3639 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3643 unsigned short s_buflen
;
3644 unsigned short s_code
;
3646 unsigned short *s_retlenadr
;
3650 #define buflen s.s_buflen
3651 #define code s.s_code
3652 #define bufadr s.s_bufadr
3653 #define retlenadr s.s_retlenadr
3655 #define R_OK 4 /* test for read permission */
3656 #define W_OK 2 /* test for write permission */
3657 #define X_OK 1 /* test for execute (search) permission */
3658 #define F_OK 0 /* test for presence of file */
3661 sys_access (path
, mode
)
3665 static char *user
= NULL
;
3668 /* translate possible directory spec into .DIR file name, so brain-dead
3669 * access can treat the directory like a file. */
3670 if (directory_file_name (path
, dir_fn
))
3674 return access (path
, mode
);
3675 if (user
== NULL
&& (user
= (char *) getenv ("USER")) == NULL
)
3681 unsigned short int dummy
;
3683 static int constant
= ACL$C_FILE
;
3684 DESCRIPTOR (path_desc
, path
);
3685 DESCRIPTOR (user_desc
, user
);
3689 if ((mode
& X_OK
) && ((stat
= access (path
, mode
)) < 0 || mode
== X_OK
))
3692 acces
|= CHP$M_READ
;
3694 acces
|= CHP$M_WRITE
;
3695 itemlst
[0].buflen
= sizeof (int);
3696 itemlst
[0].code
= CHP$_FLAGS
;
3697 itemlst
[0].bufadr
= (char *) &flags
;
3698 itemlst
[0].retlenadr
= &dummy
;
3699 itemlst
[1].buflen
= sizeof (int);
3700 itemlst
[1].code
= CHP$_ACCESS
;
3701 itemlst
[1].bufadr
= (char *) &acces
;
3702 itemlst
[1].retlenadr
= &dummy
;
3703 itemlst
[2].end
= CHP$_END
;
3704 stat
= SYS$
CHECK_ACCESS (&constant
, &path_desc
, &user_desc
, itemlst
);
3705 return stat
== SS$_NORMAL
? 0 : -1;
3709 #else /* not VMS4_4 */
3712 #define ACE$M_WRITE 2
3713 #define ACE$C_KEYID 1
3715 static unsigned short memid
, grpid
;
3716 static unsigned int uic
;
3718 /* Called from init_sys_modes, so it happens not very often
3719 but at least each time Emacs is loaded. */
3721 sys_access_reinit ()
3727 sys_access (filename
, type
)
3733 int status
, size
, i
, typecode
, acl_controlled
;
3734 unsigned int *aclptr
, *aclend
, aclbuf
[60];
3735 union prvdef prvmask
;
3737 /* Get UIC and GRP values for protection checking. */
3740 status
= LIB$
GETJPI (&JPI$_UIC
, 0, 0, &uic
, 0, 0);
3743 memid
= uic
& 0xFFFF;
3747 if (type
!= 2) /* not checking write access */
3748 return access (filename
, type
);
3750 /* Check write protection. */
3752 #define CHECKPRIV(bit) (prvmask.bit)
3753 #define WRITABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
3755 /* Find privilege bits */
3756 status
= SYS$
SETPRV (0, 0, 0, prvmask
);
3758 error ("Unable to find privileges: %s", vmserrstr (status
));
3759 if (CHECKPRIV (PRV$V_BYPASS
))
3760 return 0; /* BYPASS enabled */
3762 fab
.fab$b_fac
= FAB$M_GET
;
3763 fab
.fab$l_fna
= filename
;
3764 fab
.fab$b_fns
= strlen (filename
);
3765 fab
.fab$l_xab
= &xab
;
3766 xab
= cc$rms_xabpro
;
3767 xab
.xab$l_aclbuf
= aclbuf
;
3768 xab
.xab$w_aclsiz
= sizeof (aclbuf
);
3769 status
= SYS$
OPEN (&fab
, 0, 0);
3772 SYS$
CLOSE (&fab
, 0, 0);
3773 /* Check system access */
3774 if (CHECKPRIV (PRV$V_SYSPRV
) && WRITABLE (XAB$V_SYS
))
3776 /* Check ACL entries, if any */
3778 if (xab
.xab$w_acllen
> 0)
3781 aclend
= &aclbuf
[xab
.xab$w_acllen
/ 4];
3782 while (*aclptr
&& aclptr
< aclend
)
3784 size
= (*aclptr
& 0xff) / 4;
3785 typecode
= (*aclptr
>> 8) & 0xff;
3786 if (typecode
== ACE$C_KEYID
)
3787 for (i
= size
- 1; i
> 1; i
--)
3788 if (aclptr
[i
] == uic
)
3791 if (aclptr
[1] & ACE$M_WRITE
)
3792 return 0; /* Write access through ACL */
3794 aclptr
= &aclptr
[size
];
3796 if (acl_controlled
) /* ACL specified, prohibits write access */
3799 /* No ACL entries specified, check normal protection */
3800 if (WRITABLE (XAB$V_WLD
)) /* World writable */
3802 if (WRITABLE (XAB$V_GRP
) &&
3803 (unsigned short) (xab
.xab$l_uic
>> 16) == grpid
)
3804 return 0; /* Group writable */
3805 if (WRITABLE (XAB$V_OWN
) &&
3806 (xab
.xab$l_uic
& 0xFFFF) == memid
)
3807 return 0; /* Owner writable */
3809 return -1; /* Not writable */
3811 #endif /* not VMS4_4 */
3814 static char vtbuf
[NAM$C_MAXRSS
+1];
3816 /* translate a vms file spec to a unix path */
3818 sys_translate_vms (vfile
)
3829 /* leading device or logical name is a root directory */
3830 if (p
= strchr (vfile
, ':'))
3839 if (*p
== '[' || *p
== '<')
3841 while (*++vfile
!= *p
+ 2)
3845 if (vfile
[-1] == *p
)
3868 static char utbuf
[NAM$C_MAXRSS
+1];
3870 /* translate a unix path to a VMS file spec */
3872 sys_translate_unix (ufile
)
3895 if (index (&ufile
[1], '/'))
3902 if (index (&ufile
[1], '/'))
3909 if (strncmp (ufile
, "./", 2) == 0)
3916 ufile
++; /* skip the dot */
3917 if (index (&ufile
[1], '/'))
3922 else if (strncmp (ufile
, "../", 3) == 0)
3930 ufile
+= 2; /* skip the dots */
3931 if (index (&ufile
[1], '/'))
3956 extern char *getcwd ();
3958 #define MAXPATHLEN 1024
3960 ptr
= xmalloc (MAXPATHLEN
);
3961 val
= getcwd (ptr
, MAXPATHLEN
);
3967 strcpy (pathname
, ptr
);
3976 long item_code
= JPI$_OWNER
;
3977 unsigned long parent_id
;
3980 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &parent_id
)) & 1) == 0)
3983 vaxc$errno
= status
;
3993 return (getgid () << 16) | getuid ();
3998 sys_read (fildes
, buf
, nbyte
)
4003 return read (fildes
, buf
, (nbyte
< MAXIOSIZE
? nbyte
: MAXIOSIZE
));
4007 * VAX/VMS VAX C RTL really loses. It insists that records
4008 * end with a newline (carriage return) character, and if they
4009 * don't it adds one (nice of it isn't it!)
4011 * Thus we do this stupidity below.
4016 sys_write (fildes
, buf
, nbytes
)
4019 unsigned int nbytes
;
4026 fstat (fildes
, &st
);
4032 /* Handle fixed-length files with carriage control. */
4033 if (st
.st_fab_rfm
== FAB$C_FIX
4034 && ((st
.st_fab_rat
& (FAB$M_FTN
| FAB$M_CR
)) != 0))
4036 len
= st
.st_fab_mrs
;
4037 retval
= write (fildes
, p
, min (len
, nbytes
));
4040 retval
++; /* This skips the implied carriage control */
4044 e
= p
+ min (MAXIOSIZE
, nbytes
) - 1;
4045 while (*e
!= '\n' && e
> p
) e
--;
4046 if (p
== e
) /* Ok.. so here we add a newline... sigh. */
4047 e
= p
+ min (MAXIOSIZE
, nbytes
) - 1;
4049 retval
= write (fildes
, p
, len
);
4060 /* Create file NEW copying its attributes from file OLD. If
4061 OLD is 0 or does not exist, create based on the value of
4064 /* Protection value the file should ultimately have.
4065 Set by create_copy_attrs, and use by rename_sansversions. */
4066 static unsigned short int fab_final_pro
;
4069 creat_copy_attrs (old
, new)
4072 struct FAB fab
= cc$rms_fab
;
4073 struct XABPRO xabpro
;
4074 char aclbuf
[256]; /* Choice of size is arbitrary. See below. */
4075 extern int vms_stmlf_recfm
;
4079 fab
.fab$b_fac
= FAB$M_GET
;
4080 fab
.fab$l_fna
= old
;
4081 fab
.fab$b_fns
= strlen (old
);
4082 fab
.fab$l_xab
= (char *) &xabpro
;
4083 xabpro
= cc$rms_xabpro
;
4084 xabpro
.xab$l_aclbuf
= aclbuf
;
4085 xabpro
.xab$w_aclsiz
= sizeof aclbuf
;
4086 /* Call $OPEN to fill in the fab & xabpro fields. */
4087 if (SYS$
OPEN (&fab
, 0, 0) & 1)
4089 SYS$
CLOSE (&fab
, 0, 0);
4090 fab
.fab$l_alq
= 0; /* zero the allocation quantity */
4091 if (xabpro
.xab$w_acllen
> 0)
4093 if (xabpro
.xab$w_acllen
> sizeof aclbuf
)
4094 /* If the acl buffer was too short, redo open with longer one.
4095 Wouldn't need to do this if there were some system imposed
4096 limit on the size of an ACL, but I can't find any such. */
4098 xabpro
.xab$l_aclbuf
= (char *) alloca (xabpro
.xab$w_acllen
);
4099 xabpro
.xab$w_aclsiz
= xabpro
.xab$w_acllen
;
4100 if (SYS$
OPEN (&fab
, 0, 0) & 1)
4101 SYS$
CLOSE (&fab
, 0, 0);
4107 xabpro
.xab$l_aclbuf
= 0;
4112 fab
.fab$l_fna
= new;
4113 fab
.fab$b_fns
= strlen (new);
4117 fab
.fab$b_rfm
= vms_stmlf_recfm
? FAB$C_STMLF
: FAB$C_VAR
;
4118 fab
.fab$b_rat
= FAB$M_CR
;
4121 /* Set the file protections such that we will be able to manipulate
4122 this file. Once we are done writing and renaming it, we will set
4123 the protections back. */
4125 fab_final_pro
= xabpro
.xab$w_pro
;
4127 SYS$
SETDFPROT (0, &fab_final_pro
);
4128 xabpro
.xab$w_pro
&= 0xff0f; /* set O:rewd for now. This is set back later. */
4130 /* Create the new file with either default attrs or attrs copied
4132 if (!(SYS$
CREATE (&fab
, 0, 0) & 1))
4134 SYS$
CLOSE (&fab
, 0, 0);
4135 /* As this is a "replacement" for creat, return a file descriptor
4136 opened for writing. */
4137 return open (new, O_WRONLY
);
4142 #include <varargs.h>
4145 #define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
4150 sys_creat (va_alist
)
4153 va_list list_incrementer
;
4156 int rfd
; /* related file descriptor */
4157 int fd
; /* Our new file descriptor */
4164 extern int vms_stmlf_recfm
;
4167 va_start (list_incrementer
);
4168 name
= va_arg (list_incrementer
, char *);
4169 mode
= va_arg (list_incrementer
, int);
4171 rfd
= va_arg (list_incrementer
, int);
4172 va_end (list_incrementer
);
4175 /* Use information from the related file descriptor to set record
4176 format of the newly created file. */
4177 fstat (rfd
, &st_buf
);
4178 switch (st_buf
.st_fab_rfm
)
4181 strcpy (rfm
, "rfm = fix");
4182 sprintf (mrs
, "mrs = %d", st_buf
.st_fab_mrs
);
4183 strcpy (rat
, "rat = ");
4184 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4186 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4187 strcat (rat
, "ftn");
4188 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4189 strcat (rat
, "prn");
4190 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4191 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4192 strcat (rat
, ", blk");
4194 strcat (rat
, "blk");
4195 return creat (name
, 0, rfm
, rat
, mrs
);
4198 strcpy (rfm
, "rfm = vfc");
4199 sprintf (fsz
, "fsz = %d", st_buf
.st_fab_fsz
);
4200 strcpy (rat
, "rat = ");
4201 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4203 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4204 strcat (rat
, "ftn");
4205 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4206 strcat (rat
, "prn");
4207 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4208 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4209 strcat (rat
, ", blk");
4211 strcat (rat
, "blk");
4212 return creat (name
, 0, rfm
, rat
, fsz
);
4215 strcpy (rfm
, "rfm = stm");
4219 strcpy (rfm
, "rfm = stmcr");
4223 strcpy (rfm
, "rfm = stmlf");
4227 strcpy (rfm
, "rfm = udf");
4231 strcpy (rfm
, "rfm = var");
4234 strcpy (rat
, "rat = ");
4235 if (st_buf
.st_fab_rat
& FAB$M_CR
)
4237 else if (st_buf
.st_fab_rat
& FAB$M_FTN
)
4238 strcat (rat
, "ftn");
4239 else if (st_buf
.st_fab_rat
& FAB$M_PRN
)
4240 strcat (rat
, "prn");
4241 if (st_buf
.st_fab_rat
& FAB$M_BLK
)
4242 if (st_buf
.st_fab_rat
& (FAB$M_CR
|FAB$M_FTN
|FAB$M_PRN
))
4243 strcat (rat
, ", blk");
4245 strcat (rat
, "blk");
4249 strcpy (rfm
, vms_stmlf_recfm
? "rfm = stmlf" : "rfm=var");
4250 strcpy (rat
, "rat=cr");
4252 /* Until the VAX C RTL fixes the many bugs with modes, always use
4253 mode 0 to get the user's default protection. */
4254 fd
= creat (name
, 0, rfm
, rat
);
4255 if (fd
< 0 && errno
== EEXIST
)
4257 if (unlink (name
) < 0)
4258 report_file_error ("delete", build_string (name
));
4259 fd
= creat (name
, 0, rfm
, rat
);
4265 /* fwrite to stdout is S L O W. Speed it up by using fputc...*/
4267 sys_fwrite (ptr
, size
, num
, fp
)
4268 register char * ptr
;
4271 register int tot
= num
* size
;
4279 * The VMS C library routine creat actually creates a new version of an
4280 * existing file rather than truncating the old version. There are times
4281 * when this is not the desired behavior, for instance, when writing an
4282 * auto save file (you only want one version), or when you don't have
4283 * write permission in the directory containing the file (but the file
4284 * itself is writable). Hence this routine, which is equivalent to
4285 * "close (creat (fn, 0));" on Unix if fn already exists.
4291 struct FAB xfab
= cc$rms_fab
;
4292 struct RAB xrab
= cc$rms_rab
;
4295 xfab
.fab$l_fop
= FAB$M_TEF
; /* free allocated but unused blocks on close */
4296 xfab
.fab$b_fac
= FAB$M_TRN
| FAB$M_GET
; /* allow truncate and get access */
4297 xfab
.fab$b_shr
= FAB$M_NIL
; /* allow no sharing - file must be locked */
4298 xfab
.fab$l_fna
= fn
;
4299 xfab
.fab$b_fns
= strlen (fn
);
4300 xfab
.fab$l_dna
= ";0"; /* default to latest version of the file */
4302 xrab
.rab$l_fab
= &xfab
;
4304 /* This gibberish opens the file, positions to the first record, and
4305 deletes all records from there until the end of file. */
4306 if ((SYS$
OPEN (&xfab
) & 01) == 01)
4308 if ((SYS$
CONNECT (&xrab
) & 01) == 01 &&
4309 (SYS$
FIND (&xrab
) & 01) == 01 &&
4310 (SYS$
TRUNCATE (&xrab
) & 01) == 01)
4321 /* Define this symbol to actually read SYSUAF.DAT. This requires either
4322 SYSPRV or a readable SYSUAF.DAT. */
4328 * Routine to read the VMS User Authorization File and return
4329 * a specific user's record.
4332 static struct UAF retuaf
;
4335 get_uaf_name (uname
)
4342 uaf_fab
= cc$rms_fab
;
4343 uaf_rab
= cc$rms_rab
;
4344 /* initialize fab fields */
4345 uaf_fab
.fab$l_fna
= "SYS$SYSTEM:SYSUAF.DAT";
4346 uaf_fab
.fab$b_fns
= 21;
4347 uaf_fab
.fab$b_fac
= FAB$M_GET
;
4348 uaf_fab
.fab$b_org
= FAB$C_IDX
;
4349 uaf_fab
.fab$b_shr
= FAB$M_GET
|FAB$M_PUT
|FAB$M_UPD
|FAB$M_DEL
;
4350 /* initialize rab fields */
4351 uaf_rab
.rab$l_fab
= &uaf_fab
;
4352 /* open the User Authorization File */
4353 status
= SYS$
OPEN (&uaf_fab
);
4357 vaxc$errno
= status
;
4360 status
= SYS$
CONNECT (&uaf_rab
);
4364 vaxc$errno
= status
;
4367 /* read the requested record - index is in uname */
4368 uaf_rab
.rab$l_kbf
= uname
;
4369 uaf_rab
.rab$b_ksz
= strlen (uname
);
4370 uaf_rab
.rab$b_rac
= RAB$C_KEY
;
4371 uaf_rab
.rab$l_ubf
= (char *)&retuaf
;
4372 uaf_rab
.rab$w_usz
= sizeof retuaf
;
4373 status
= SYS$
GET (&uaf_rab
);
4377 vaxc$errno
= status
;
4380 /* close the User Authorization File */
4381 status
= SYS$
DISCONNECT (&uaf_rab
);
4385 vaxc$errno
= status
;
4388 status
= SYS$
CLOSE (&uaf_fab
);
4392 vaxc$errno
= status
;
4406 uaf_fab
= cc$rms_fab
;
4407 uaf_rab
= cc$rms_rab
;
4408 /* initialize fab fields */
4409 uaf_fab
.fab$l_fna
= "SYS$SYSTEM:SYSUAF.DAT";
4410 uaf_fab
.fab$b_fns
= 21;
4411 uaf_fab
.fab$b_fac
= FAB$M_GET
;
4412 uaf_fab
.fab$b_org
= FAB$C_IDX
;
4413 uaf_fab
.fab$b_shr
= FAB$M_GET
|FAB$M_PUT
|FAB$M_UPD
|FAB$M_DEL
;
4414 /* initialize rab fields */
4415 uaf_rab
.rab$l_fab
= &uaf_fab
;
4416 /* open the User Authorization File */
4417 status
= SYS$
OPEN (&uaf_fab
);
4421 vaxc$errno
= status
;
4424 status
= SYS$
CONNECT (&uaf_rab
);
4428 vaxc$errno
= status
;
4431 /* read the requested record - index is in uic */
4432 uaf_rab
.rab$b_krf
= 1; /* 1st alternate key */
4433 uaf_rab
.rab$l_kbf
= (char *) &uic
;
4434 uaf_rab
.rab$b_ksz
= sizeof uic
;
4435 uaf_rab
.rab$b_rac
= RAB$C_KEY
;
4436 uaf_rab
.rab$l_ubf
= (char *)&retuaf
;
4437 uaf_rab
.rab$w_usz
= sizeof retuaf
;
4438 status
= SYS$
GET (&uaf_rab
);
4442 vaxc$errno
= status
;
4445 /* close the User Authorization File */
4446 status
= SYS$
DISCONNECT (&uaf_rab
);
4450 vaxc$errno
= status
;
4453 status
= SYS$
CLOSE (&uaf_fab
);
4457 vaxc$errno
= status
;
4463 static struct passwd retpw
;
4471 /* copy these out first because if the username is 32 chars, the next
4472 section will overwrite the first byte of the UIC */
4473 retpw
.pw_uid
= up
->uaf$w_mem
;
4474 retpw
.pw_gid
= up
->uaf$w_grp
;
4476 /* I suppose this is not the best style, to possibly overwrite one
4477 byte beyond the end of the field, but what the heck... */
4478 ptr
= &up
->uaf$t_username
[UAF$S_USERNAME
];
4479 while (ptr
[-1] == ' ')
4482 strcpy (retpw
.pw_name
, up
->uaf$t_username
);
4484 /* the rest of these are counted ascii strings */
4485 strncpy (retpw
.pw_gecos
, &up
->uaf$t_owner
[1], up
->uaf$t_owner
[0]);
4486 retpw
.pw_gecos
[up
->uaf$t_owner
[0]] = '\0';
4487 strncpy (retpw
.pw_dir
, &up
->uaf$t_defdev
[1], up
->uaf$t_defdev
[0]);
4488 retpw
.pw_dir
[up
->uaf$t_defdev
[0]] = '\0';
4489 strncat (retpw
.pw_dir
, &up
->uaf$t_defdir
[1], up
->uaf$t_defdir
[0]);
4490 retpw
.pw_dir
[up
->uaf$t_defdev
[0] + up
->uaf$t_defdir
[0]] = '\0';
4491 strncpy (retpw
.pw_shell
, &up
->uaf$t_defcli
[1], up
->uaf$t_defcli
[0]);
4492 retpw
.pw_shell
[up
->uaf$t_defcli
[0]] = '\0';
4496 #else /* not READ_SYSUAF */
4497 static struct passwd retpw
;
4498 #endif /* not READ_SYSUAF */
4509 unsigned char * full
;
4510 #endif /* READ_SYSUAF */
4515 if ('a' <= *ptr
&& *ptr
<= 'z')
4520 if (!(up
= get_uaf_name (name
)))
4522 return cnv_uaf_pw (up
);
4524 if (strcmp (name
, getenv ("USER")) == 0)
4526 retpw
.pw_uid
= getuid ();
4527 retpw
.pw_gid
= getgid ();
4528 strcpy (retpw
.pw_name
, name
);
4529 if (full
= egetenv ("FULLNAME"))
4530 strcpy (retpw
.pw_gecos
, full
);
4532 *retpw
.pw_gecos
= '\0';
4533 strcpy (retpw
.pw_dir
, egetenv ("HOME"));
4534 *retpw
.pw_shell
= '\0';
4539 #endif /* not READ_SYSUAF */
4549 if (!(up
= get_uaf_uic (uid
)))
4551 return cnv_uaf_pw (up
);
4553 if (uid
== sys_getuid ())
4554 return getpwnam (egetenv ("USER"));
4557 #endif /* not READ_SYSUAF */
4560 /* return total address space available to the current process. This is
4561 the sum of the current p0 size, p1 size and free page table entries
4567 unsigned long free_pages
;
4568 unsigned long frep0va
;
4569 unsigned long frep1va
;
4572 item_code
= JPI$_FREPTECNT
;
4573 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &free_pages
)) & 1) == 0)
4576 vaxc$errno
= status
;
4581 item_code
= JPI$_FREP0VA
;
4582 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &frep0va
)) & 1) == 0)
4585 vaxc$errno
= status
;
4588 item_code
= JPI$_FREP1VA
;
4589 if (((status
= LIB$
GETJPI (&item_code
, 0, 0, &frep1va
)) & 1) == 0)
4592 vaxc$errno
= status
;
4596 return free_pages
+ frep0va
+ (0x7fffffff - frep1va
);
4600 define_logical_name (varname
, string
)
4604 struct dsc$descriptor_s strdsc
=
4605 {strlen (string
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, string
};
4606 struct dsc$descriptor_s envdsc
=
4607 {strlen (varname
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, varname
};
4608 struct dsc$descriptor_s lnmdsc
=
4609 {7, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, "LNM$JOB"};
4611 return LIB$
SET_LOGICAL (&envdsc
, &strdsc
, &lnmdsc
, 0, 0);
4615 delete_logical_name (varname
)
4618 struct dsc$descriptor_s envdsc
=
4619 {strlen (varname
), DSC$K_DTYPE_T
, DSC$K_CLASS_S
, varname
};
4620 struct dsc$descriptor_s lnmdsc
=
4621 {7, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, "LNM$JOB"};
4623 return LIB$
DELETE_LOGICAL (&envdsc
, &lnmdsc
);
4641 error ("execvp system call not implemented");
4650 struct FAB from_fab
= cc$rms_fab
, to_fab
= cc$rms_fab
;
4651 struct NAM from_nam
= cc$rms_nam
, to_nam
= cc$rms_nam
;
4652 char from_esn
[NAM$C_MAXRSS
];
4653 char to_esn
[NAM$C_MAXRSS
];
4655 from_fab
.fab$l_fna
= from
;
4656 from_fab
.fab$b_fns
= strlen (from
);
4657 from_fab
.fab$l_nam
= &from_nam
;
4658 from_fab
.fab$l_fop
= FAB$M_NAM
;
4660 from_nam
.nam$l_esa
= from_esn
;
4661 from_nam
.nam$b_ess
= sizeof from_esn
;
4663 to_fab
.fab$l_fna
= to
;
4664 to_fab
.fab$b_fns
= strlen (to
);
4665 to_fab
.fab$l_nam
= &to_nam
;
4666 to_fab
.fab$l_fop
= FAB$M_NAM
;
4668 to_nam
.nam$l_esa
= to_esn
;
4669 to_nam
.nam$b_ess
= sizeof to_esn
;
4671 status
= SYS$
RENAME (&from_fab
, 0, 0, &to_fab
);
4677 if (status
== RMS$_DEV
)
4681 vaxc$errno
= status
;
4686 /* This function renames a file like `rename', but it strips
4687 the version number from the "to" filename, such that the "to" file is
4688 will always be a new version. It also sets the file protection once it is
4689 finished. The protection that we will use is stored in fab_final_pro,
4690 and was set when we did a creat_copy_attrs to create the file that we
4693 We could use the chmod function, but Eunichs uses 3 bits per user category
4694 to describe the protection, and VMS uses 4 (write and delete are separate
4695 bits). To maintain portability, the VMS implementation of `chmod' wires
4696 the W and D bits together. */
4699 static struct fibdef fib
; /* We need this initialized to zero */
4700 char vms_file_written
[NAM$C_MAXRSS
];
4703 rename_sans_version (from
,to
)
4710 struct FAB to_fab
= cc$rms_fab
;
4711 struct NAM to_nam
= cc$rms_nam
;
4712 struct dsc$descriptor fib_d
={sizeof (fib
),0,0,(char*) &fib
};
4713 struct dsc$descriptor fib_attr
[2]
4714 = {{sizeof (fab_final_pro
),ATR$C_FPRO
,0,(char*) &fab_final_pro
},{0,0,0,0}};
4715 char to_esn
[NAM$C_MAXRSS
];
4717 $
DESCRIPTOR (disk
,to_esn
);
4719 to_fab
.fab$l_fna
= to
;
4720 to_fab
.fab$b_fns
= strlen (to
);
4721 to_fab
.fab$l_nam
= &to_nam
;
4722 to_fab
.fab$l_fop
= FAB$M_NAM
;
4724 to_nam
.nam$l_esa
= to_esn
;
4725 to_nam
.nam$b_ess
= sizeof to_esn
;
4727 status
= SYS$
PARSE (&to_fab
, 0, 0); /* figure out the full file name */
4729 if (to_nam
.nam$l_fnb
&& NAM$M_EXP_VER
)
4730 *(to_nam
.nam$l_ver
) = '\0';
4732 stat
= rename (from
, to_esn
);
4736 strcpy (vms_file_written
, to_esn
);
4738 to_fab
.fab$l_fna
= vms_file_written
; /* this points to the versionless name */
4739 to_fab
.fab$b_fns
= strlen (vms_file_written
);
4741 /* Now set the file protection to the correct value */
4742 SYS$
OPEN (&to_fab
, 0, 0); /* This fills in the nam$w_fid fields */
4744 /* Copy these fields into the fib */
4745 fib
.fib$r_fid_overlay
.fib$w_fid
[0] = to_nam
.nam$w_fid
[0];
4746 fib
.fib$r_fid_overlay
.fib$w_fid
[1] = to_nam
.nam$w_fid
[1];
4747 fib
.fib$r_fid_overlay
.fib$w_fid
[2] = to_nam
.nam$w_fid
[2];
4749 SYS$
CLOSE (&to_fab
, 0, 0);
4751 stat
= SYS$
ASSIGN (&disk
, &chan
, 0, 0); /* open a channel to the disk */
4754 stat
= SYS$
QIOW (0, chan
, IO$_MODIFY
, iosb
, 0, 0, &fib_d
,
4755 0, 0, 0, &fib_attr
, 0);
4758 stat
= SYS$
DASSGN (chan
);
4761 strcpy (vms_file_written
, to_esn
); /* We will write this to the terminal*/
4772 unsigned short fid
[3];
4773 char esa
[NAM$C_MAXRSS
];
4776 fab
.fab$l_fop
= FAB$M_OFP
;
4777 fab
.fab$l_fna
= file
;
4778 fab
.fab$b_fns
= strlen (file
);
4779 fab
.fab$l_nam
= &nam
;
4782 nam
.nam$l_esa
= esa
;
4783 nam
.nam$b_ess
= NAM$C_MAXRSS
;
4785 status
= SYS$
PARSE (&fab
);
4786 if ((status
& 1) == 0)
4789 vaxc$errno
= status
;
4792 status
= SYS$
SEARCH (&fab
);
4793 if ((status
& 1) == 0)
4796 vaxc$errno
= status
;
4800 fid
[0] = nam
.nam$w_fid
[0];
4801 fid
[1] = nam
.nam$w_fid
[1];
4802 fid
[2] = nam
.nam$w_fid
[2];
4804 fab
.fab$l_fna
= new;
4805 fab
.fab$b_fns
= strlen (new);
4807 status
= SYS$
PARSE (&fab
);
4808 if ((status
& 1) == 0)
4811 vaxc$errno
= status
;
4815 nam
.nam$w_fid
[0] = fid
[0];
4816 nam
.nam$w_fid
[1] = fid
[1];
4817 nam
.nam$w_fid
[2] = fid
[2];
4819 nam
.nam$l_esa
= nam
.nam$l_name
;
4820 nam
.nam$b_esl
= nam
.nam$b_name
+ nam
.nam$b_type
+ nam
.nam$b_ver
;
4822 status
= SYS$
ENTER (&fab
);
4823 if ((status
& 1) == 0)
4826 vaxc$errno
= status
;
4837 printf ("%s not yet implemented\r\n", badfunc
);
4838 reset_all_sys_modes ();
4845 /* Arrange to return a range centered on zero. */
4846 return rand () - (1 << 30);
4863 register int length
;
4867 long max_str
= 65535;
4869 while (length
> max_str
) {
4870 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &max_str
, b
);
4875 (void) LIB$
MOVC5 (&zero
, &zero
, &zero
, &max_str
, b
);
4877 while (length
-- > 0)
4879 #endif /* not VMS */
4882 #endif /* no bzero */
4883 #endif /* BSTRING */
4885 #if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
4888 /* Saying `void' requires a declaration, above, where bcopy is used
4889 and that declaration causes pain for systems where bcopy is a macro. */
4890 bcopy (b1
, b2
, length
)
4893 register int length
;
4896 long max_str
= 65535;
4898 while (length
> max_str
) {
4899 (void) LIB$
MOVC3 (&max_str
, b1
, b2
);
4905 (void) LIB$
MOVC3 (&length
, b1
, b2
);
4907 while (length
-- > 0)
4909 #endif /* not VMS */
4911 #endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
4916 bcmp (b1
, b2
, length
) /* This could be a macro! */
4919 register int length
;
4922 struct dsc$descriptor_s src1
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b1
};
4923 struct dsc$descriptor_s src2
= {length
, DSC$K_DTYPE_T
, DSC$K_CLASS_S
, b2
};
4925 return STR$
COMPARE (&src1
, &src2
);
4927 while (length
-- > 0)
4932 #endif /* not VMS */
4934 #endif /* no bcmp */
4935 #endif /* not BSTRING */
4937 #ifndef HAVE_STRSIGNAL
4944 if (0 <= code
&& code
< NSIG
)
4947 signame
= sys_errlist
[code
];
4949 /* Cast to suppress warning if the table has const char *. */
4950 signame
= (char *) sys_siglist
[code
];
4956 #endif /* HAVE_STRSIGNAL */
4959 /* For make-serial-process */
4960 int serial_open (char *port
)
4964 fd
= emacs_open ((char*) port
,
4977 error ("Could not open %s: %s",
4978 port
, emacs_strerror (errno
));
4981 ioctl (fd
, TIOCEXCL
, (char *) 0);
4986 #endif /* TERMIOS */
4990 #if !defined (HAVE_CFMAKERAW)
4991 /* Workaround for targets which are missing cfmakeraw. */
4992 /* Pasted from man page. */
4993 static void cfmakeraw (struct termios
*termios_p
)
4995 termios_p
->c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
|IXON
);
4996 termios_p
->c_oflag
&= ~OPOST
;
4997 termios_p
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
4998 termios_p
->c_cflag
&= ~(CSIZE
|PARENB
);
4999 termios_p
->c_cflag
|= CS8
;
5001 #endif /* !defined (HAVE_CFMAKERAW */
5003 #if !defined (HAVE_CFSETSPEED)
5004 /* Workaround for targets which are missing cfsetspeed. */
5005 static int cfsetspeed (struct termios
*termios_p
, speed_t vitesse
)
5007 return (cfsetispeed (termios_p
, vitesse
)
5008 + cfsetospeed (termios_p
, vitesse
));
5012 /* For serial-process-configure */
5014 serial_configure (struct Lisp_Process
*p
,
5015 Lisp_Object contact
)
5017 Lisp_Object childp2
= Qnil
;
5018 Lisp_Object tem
= Qnil
;
5019 struct termios attr
;
5021 char summary
[4] = "???"; /* This usually becomes "8N1". */
5023 childp2
= Fcopy_sequence (p
->childp
);
5025 /* Read port attributes and prepare default configuration. */
5026 err
= tcgetattr (p
->outfd
, &attr
);
5028 error ("tcgetattr() failed: %s", emacs_strerror (errno
));
5030 #if defined (CLOCAL)
5031 attr
.c_cflag
|= CLOCAL
;
5034 attr
.c_cflag
| CREAD
;
5037 /* Configure speed. */
5038 if (!NILP (Fplist_member (contact
, QCspeed
)))
5039 tem
= Fplist_get (contact
, QCspeed
);
5041 tem
= Fplist_get (p
->childp
, QCspeed
);
5043 err
= cfsetspeed (&attr
, XINT (tem
));
5045 error ("cfsetspeed(%d) failed: %s", XINT (tem
), emacs_strerror (errno
));
5046 childp2
= Fplist_put (childp2
, QCspeed
, tem
);
5048 /* Configure bytesize. */
5049 if (!NILP (Fplist_member (contact
, QCbytesize
)))
5050 tem
= Fplist_get (contact
, QCbytesize
);
5052 tem
= Fplist_get (p
->childp
, QCbytesize
);
5054 tem
= make_number (8);
5056 if (XINT (tem
) != 7 && XINT (tem
) != 8)
5057 error (":bytesize must be nil (8), 7, or 8");
5058 summary
[0] = XINT(tem
) + '0';
5059 #if defined (CSIZE) && defined (CS7) && defined (CS8)
5060 attr
.c_cflag
&= ~CSIZE
;
5061 attr
.c_cflag
|= ((XINT (tem
) == 7) ? CS7
: CS8
);
5063 /* Don't error on bytesize 8, which should be set by cfmakeraw. */
5064 if (XINT (tem
) != 8)
5065 error ("Bytesize cannot be changed");
5067 childp2
= Fplist_put (childp2
, QCbytesize
, tem
);
5069 /* Configure parity. */
5070 if (!NILP (Fplist_member (contact
, QCparity
)))
5071 tem
= Fplist_get (contact
, QCparity
);
5073 tem
= Fplist_get (p
->childp
, QCparity
);
5074 if (!NILP (tem
) && !EQ (tem
, Qeven
) && !EQ (tem
, Qodd
))
5075 error (":parity must be nil (no parity), `even', or `odd'");
5076 #if defined (PARENB) && defined (PARODD) && defined (IGNPAR) && defined (INPCK)
5077 attr
.c_cflag
&= ~(PARENB
| PARODD
);
5078 attr
.c_iflag
&= ~(IGNPAR
| INPCK
);
5083 else if (EQ (tem
, Qeven
))
5086 attr
.c_cflag
|= PARENB
;
5087 attr
.c_iflag
|= (IGNPAR
| INPCK
);
5089 else if (EQ (tem
, Qodd
))
5092 attr
.c_cflag
|= (PARENB
| PARODD
);
5093 attr
.c_iflag
|= (IGNPAR
| INPCK
);
5096 /* Don't error on no parity, which should be set by cfmakeraw. */
5098 error ("Parity cannot be configured");
5100 childp2
= Fplist_put (childp2
, QCparity
, tem
);
5102 /* Configure stopbits. */
5103 if (!NILP (Fplist_member (contact
, QCstopbits
)))
5104 tem
= Fplist_get (contact
, QCstopbits
);
5106 tem
= Fplist_get (p
->childp
, QCstopbits
);
5108 tem
= make_number (1);
5110 if (XINT (tem
) != 1 && XINT (tem
) != 2)
5111 error (":stopbits must be nil (1 stopbit), 1, or 2");
5112 summary
[2] = XINT (tem
) + '0';
5113 #if defined (CSTOPB)
5114 attr
.c_cflag
&= ~CSTOPB
;
5115 if (XINT (tem
) == 2)
5116 attr
.c_cflag
|= CSTOPB
;
5118 /* Don't error on 1 stopbit, which should be set by cfmakeraw. */
5119 if (XINT (tem
) != 1)
5120 error ("Stopbits cannot be configured");
5122 childp2
= Fplist_put (childp2
, QCstopbits
, tem
);
5124 /* Configure flowcontrol. */
5125 if (!NILP (Fplist_member (contact
, QCflowcontrol
)))
5126 tem
= Fplist_get (contact
, QCflowcontrol
);
5128 tem
= Fplist_get (p
->childp
, QCflowcontrol
);
5129 if (!NILP (tem
) && !EQ (tem
, Qhw
) && !EQ (tem
, Qsw
))
5130 error (":flowcontrol must be nil (no flowcontrol), `hw', or `sw'");
5131 #if defined (CRTSCTS)
5132 attr
.c_cflag
&= ~CRTSCTS
;
5134 #if defined (CNEW_RTSCTS)
5135 attr
.c_cflag
&= ~CNEW_RTSCTS
;
5137 #if defined (IXON) && defined (IXOFF)
5138 attr
.c_iflag
&= ~(IXON
| IXOFF
);
5142 /* Already configured. */
5144 else if (EQ (tem
, Qhw
))
5146 #if defined (CRTSCTS)
5147 attr
.c_cflag
|= CRTSCTS
;
5148 #elif defined (CNEW_RTSCTS)
5149 attr
.c_cflag
|= CNEW_RTSCTS
;
5151 error ("Hardware flowcontrol (RTS/CTS) not supported");
5154 else if (EQ (tem
, Qsw
))
5156 #if defined (IXON) && defined (IXOFF)
5157 attr
.c_iflag
|= (IXON
| IXOFF
);
5159 error ("Software flowcontrol (XON/XOFF) not supported");
5162 childp2
= Fplist_put (childp2
, QCflowcontrol
, tem
);
5164 /* Activate configuration. */
5165 err
= tcsetattr (p
->outfd
, TCSANOW
, &attr
);
5167 error ("tcsetattr() failed: %s", emacs_strerror (errno
));
5169 childp2
= Fplist_put (childp2
, QCsummary
, build_string (summary
));
5170 p
->childp
= childp2
;
5173 #endif /* TERMIOS */
5175 /* arch-tag: edb43589-4e09-4544-b325-978b5b121dcf
5176 (do not change this comment) */