*** empty log message ***
[emacs.git] / src / sysdep.c
blob625b5d619aba5eac8aed9b5d3e0a29ff5222c0e5
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 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <signal.h>
27 #include <stdio.h>
28 #include <setjmp.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include "lisp.h"
33 /* Including stdlib.h isn't necessarily enough to get srandom
34 declared, e.g. without __USE_XOPEN_EXTENDED with glibc 2. */
35 #ifdef HAVE_RANDOM
36 #if 0 /* It turns out that defining _OSF_SOURCE in osf5-0.h gets
37 random prototyped as returning `int'. It looks to me as
38 though the best way to DTRT is to prefer the rand48 functions
39 (per libc.info). -- fx */
40 extern long int random P_ ((void));
41 #endif
42 #if 0 /* Don't prototype srandom; it takes an unsigned argument on
43 some systems, and an unsigned long on others, like FreeBSD
44 4.1. */
45 extern void srandom P_ ((unsigned int));
46 #endif
47 #endif
49 #include "blockinput.h"
51 #ifdef MAC_OS8
52 #include <sys/param.h>
54 #ifndef subprocesses
55 /* Nonzero means delete a process right away if it exits (process.c). */
56 static int delete_exited_processes;
57 #endif
58 #endif /* MAC_OS8 */
60 #ifdef WINDOWSNT
61 #define read sys_read
62 #define write sys_write
63 #include <windows.h>
64 #ifndef NULL
65 #define NULL 0
66 #endif
67 #endif /* not WINDOWSNT */
69 /* Does anyone other than VMS need this? */
70 #ifndef fwrite
71 #define sys_fwrite fwrite
72 #else
73 #undef fwrite
74 #endif
76 #include <sys/types.h>
77 #include <sys/stat.h>
78 #include <errno.h>
80 #ifdef HAVE_SETPGID
81 #if !defined (USG) || defined (BSD_PGRPS)
82 #undef setpgrp
83 #define setpgrp setpgid
84 #endif
85 #endif
87 /* Get SI_SRPC_DOMAIN, if it is available. */
88 #ifdef HAVE_SYS_SYSTEMINFO_H
89 #include <sys/systeminfo.h>
90 #endif
92 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
93 #include <dos.h>
94 #include "dosfns.h"
95 #include "msdos.h"
96 #include <sys/param.h>
98 #if __DJGPP__ > 1
99 extern int etext;
100 extern unsigned start __asm__ ("start");
101 #endif
102 #endif
104 #ifndef USE_CRT_DLL
105 #ifndef errno
106 extern int errno;
107 #endif
108 #endif
110 #ifdef VMS
111 #include <rms.h>
112 #include <ttdef.h>
113 #include <tt2def.h>
114 #include <iodef.h>
115 #include <ssdef.h>
116 #include <descrip.h>
117 #include <fibdef.h>
118 #include <atrdef.h>
119 #include <ctype.h>
120 #include <string.h>
121 #ifdef __GNUC__
122 #include <sys/file.h>
123 #else
124 #include <file.h>
125 #endif
126 #undef F_SETFL
127 #ifndef RAB$C_BID
128 #include <rab.h>
129 #endif
130 #define MAXIOSIZE (32 * PAGESIZE) /* Don't I/O more than 32 blocks at a time */
131 #endif /* VMS */
133 #ifndef VMS
134 #include <sys/file.h>
135 #endif /* not VMS */
137 #ifdef HAVE_FCNTL_H
138 #include <fcntl.h>
139 #endif
141 #ifndef MSDOS
142 #include <sys/ioctl.h>
143 #endif
145 #include "systty.h"
146 #include "syswait.h"
148 #ifdef BROKEN_TIOCGWINSZ
149 #undef TIOCGWINSZ
150 #undef TIOCSWINSZ
151 #endif
153 #if defined (USG) || defined (DGUX)
154 #include <sys/utsname.h>
155 #ifndef MEMORY_IN_STRING_H
156 #include <memory.h>
157 #endif
158 #if defined (TIOCGWINSZ) || defined (ISC4_0)
159 #ifdef NEED_SIOCTL
160 #include <sys/sioctl.h>
161 #endif
162 #ifdef NEED_PTEM_H
163 #include <sys/stream.h>
164 #include <sys/ptem.h>
165 #endif
166 #endif /* TIOCGWINSZ or ISC4_0 */
167 #endif /* USG or DGUX */
169 extern int quit_char;
171 #include "keyboard.h"
172 #include "frame.h"
173 #include "window.h"
174 #include "termhooks.h"
175 #include "termchar.h"
176 #include "termopts.h"
177 #include "dispextern.h"
178 #include "process.h"
180 #ifdef WINDOWSNT
181 #include <direct.h>
182 /* In process.h which conflicts with the local copy. */
183 #define _P_WAIT 0
184 int _CRTAPI1 _spawnlp (int, const char *, const char *, ...);
185 int _CRTAPI1 _getpid (void);
186 extern char *getwd (char *);
187 #endif
189 #ifdef NONSYSTEM_DIR_LIBRARY
190 #include "ndir.h"
191 #endif /* NONSYSTEM_DIR_LIBRARY */
193 #include "syssignal.h"
194 #include "systime.h"
195 #ifdef HAVE_UTIME_H
196 #include <utime.h>
197 #endif
199 #ifndef HAVE_UTIMES
200 #ifndef HAVE_STRUCT_UTIMBUF
201 /* We want to use utime rather than utimes, but we couldn't find the
202 structure declaration. We'll use the traditional one. */
203 struct utimbuf {
204 long actime;
205 long modtime;
207 #endif
208 #endif
210 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
211 #ifndef LPASS8
212 #define LPASS8 0
213 #endif
215 #ifdef BSD4_1
216 #define LNOFLSH 0100000
217 #endif
219 static int baud_convert[] =
220 #ifdef BAUD_CONVERT
221 BAUD_CONVERT;
222 #else
224 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
225 1800, 2400, 4800, 9600, 19200, 38400
227 #endif
229 #ifdef HAVE_SPEED_T
230 #include <termios.h>
231 #else
232 #if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
233 #else
234 #if defined (HAVE_TERMIOS_H) && defined (GNU_LINUX)
235 #include <termios.h>
236 #endif
237 #endif
238 #endif
240 int emacs_ospeed;
242 /* The file descriptor for Emacs's input terminal.
243 Under Unix, this is normally zero except when using X;
244 under VMS, we place the input channel number here. */
245 int input_fd;
247 void croak P_ ((char *)) NO_RETURN;
249 #ifdef AIXHFT
250 void hft_init ();
251 void hft_reset ();
252 #endif
254 /* Temporary used by `sigblock' when defined in terms of signprocmask. */
256 SIGMASKTYPE sigprocmask_set;
259 #if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME)
261 /* Return the current working directory. Returns NULL on errors.
262 Any other returned value must be freed with free. This is used
263 only when get_current_dir_name is not defined on the system. */
264 char*
265 get_current_dir_name ()
267 char *buf;
268 char *pwd;
269 struct stat dotstat, pwdstat;
270 /* If PWD is accurate, use it instead of calling getwd. PWD is
271 sometimes a nicer name, and using it may avoid a fatal error if a
272 parent directory is searchable but not readable. */
273 if ((pwd = getenv ("PWD")) != 0
274 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1])))
275 && stat (pwd, &pwdstat) == 0
276 && stat (".", &dotstat) == 0
277 && dotstat.st_ino == pwdstat.st_ino
278 && dotstat.st_dev == pwdstat.st_dev
279 #ifdef MAXPATHLEN
280 && strlen (pwd) < MAXPATHLEN
281 #endif
284 buf = (char *) malloc (strlen (pwd) + 1);
285 if (!buf)
286 return NULL;
287 strcpy (buf, pwd);
289 #ifdef HAVE_GETCWD
290 else
292 size_t buf_size = 1024;
293 buf = (char *) malloc (buf_size);
294 if (!buf)
295 return NULL;
296 for (;;)
298 if (getcwd (buf, buf_size) == buf)
299 break;
300 if (errno != ERANGE)
302 int tmp_errno = errno;
303 free (buf);
304 errno = tmp_errno;
305 return NULL;
307 buf_size *= 2;
308 buf = (char *) realloc (buf, buf_size);
309 if (!buf)
310 return NULL;
313 #else
314 else
316 /* We need MAXPATHLEN here. */
317 buf = (char *) malloc (MAXPATHLEN + 1);
318 if (!buf)
319 return NULL;
320 if (getwd (buf) == NULL)
322 int tmp_errno = errno;
323 free (buf);
324 errno = tmp_errno;
325 return NULL;
328 #endif
329 return buf;
331 #endif
334 /* Specify a different file descriptor for further input operations. */
336 void
337 change_input_fd (fd)
338 int fd;
340 input_fd = fd;
343 /* Discard pending input on descriptor input_fd. */
345 void
346 discard_tty_input ()
348 #ifndef WINDOWSNT
349 struct emacs_tty buf;
351 if (noninteractive)
352 return;
354 /* Discarding input is not safe when the input could contain
355 replies from the X server. So don't do it. */
356 if (read_socket_hook)
357 return;
359 #ifdef VMS
360 end_kbd_input ();
361 SYS$QIOW (0, input_fd, IO$_READVBLK|IO$M_PURGE, input_iosb, 0, 0,
362 &buf.main, 0, 0, terminator_mask, 0, 0);
363 queue_kbd_input ();
364 #else /* not VMS */
365 #ifdef APOLLO
367 int zero = 0;
368 ioctl (input_fd, TIOCFLUSH, &zero);
370 #else /* not Apollo */
371 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
372 while (dos_keyread () != -1)
374 #else /* not MSDOS */
375 EMACS_GET_TTY (input_fd, &buf);
376 EMACS_SET_TTY (input_fd, &buf, 0);
377 #endif /* not MSDOS */
378 #endif /* not Apollo */
379 #endif /* not VMS */
380 #endif /* not WINDOWSNT */
383 #ifdef SIGTSTP
385 /* Arrange for character C to be read as the next input from
386 the terminal. */
388 void
389 #ifdef PROTOTYPES
390 stuff_char (char c)
391 #else
392 stuff_char (c)
393 char c;
394 #endif
396 if (read_socket_hook)
397 return;
399 /* Should perhaps error if in batch mode */
400 #ifdef TIOCSTI
401 ioctl (input_fd, TIOCSTI, &c);
402 #else /* no TIOCSTI */
403 error ("Cannot stuff terminal input characters in this version of Unix");
404 #endif /* no TIOCSTI */
407 #endif /* SIGTSTP */
409 void
410 init_baud_rate ()
412 if (noninteractive)
413 emacs_ospeed = 0;
414 else
416 #ifdef INIT_BAUD_RATE
417 INIT_BAUD_RATE ();
418 #else
419 #ifdef DOS_NT
420 emacs_ospeed = 15;
421 #else /* not DOS_NT */
422 #ifdef VMS
423 struct sensemode sg;
425 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &sg, 0, 0,
426 &sg.class, 12, 0, 0, 0, 0 );
427 emacs_ospeed = sg.xmit_baud;
428 #else /* not VMS */
429 #ifdef HAVE_TERMIOS
430 struct termios sg;
432 sg.c_cflag = B9600;
433 tcgetattr (input_fd, &sg);
434 emacs_ospeed = cfgetospeed (&sg);
435 #if defined (USE_GETOBAUD) && defined (getobaud)
436 /* m88k-motorola-sysv3 needs this (ghazi@noc.rutgers.edu) 9/1/94. */
437 if (emacs_ospeed == 0)
438 emacs_ospeed = getobaud (sg.c_cflag);
439 #endif
440 #else /* neither VMS nor TERMIOS */
441 #ifdef HAVE_TERMIO
442 struct termio sg;
444 sg.c_cflag = B9600;
445 #ifdef HAVE_TCATTR
446 tcgetattr (input_fd, &sg);
447 #else
448 ioctl (input_fd, TCGETA, &sg);
449 #endif
450 emacs_ospeed = sg.c_cflag & CBAUD;
451 #else /* neither VMS nor TERMIOS nor TERMIO */
452 struct sgttyb sg;
454 sg.sg_ospeed = B9600;
455 if (ioctl (input_fd, TIOCGETP, &sg) < 0)
456 abort ();
457 emacs_ospeed = sg.sg_ospeed;
458 #endif /* not HAVE_TERMIO */
459 #endif /* not HAVE_TERMIOS */
460 #endif /* not VMS */
461 #endif /* not DOS_NT */
462 #endif /* not INIT_BAUD_RATE */
465 baud_rate = (emacs_ospeed < sizeof baud_convert / sizeof baud_convert[0]
466 ? baud_convert[emacs_ospeed] : 9600);
467 if (baud_rate == 0)
468 baud_rate = 1200;
471 /*ARGSUSED*/
472 void
473 set_exclusive_use (fd)
474 int fd;
476 #ifdef FIOCLEX
477 ioctl (fd, FIOCLEX, 0);
478 #endif
479 /* Ok to do nothing if this feature does not exist */
482 #ifndef subprocesses
484 wait_without_blocking ()
486 #ifdef BSD_SYSTEM
487 wait3 (0, WNOHANG | WUNTRACED, 0);
488 #else
489 croak ("wait_without_blocking");
490 #endif
491 synch_process_alive = 0;
494 #endif /* not subprocesses */
496 int wait_debugging; /* Set nonzero to make following function work under dbx
497 (at least for bsd). */
499 SIGTYPE
500 wait_for_termination_signal ()
503 /* Wait for subprocess with process id `pid' to terminate and
504 make sure it will get eliminated (not remain forever as a zombie) */
506 void
507 wait_for_termination (pid)
508 int pid;
510 while (1)
512 #ifdef subprocesses
513 #ifdef VMS
514 int status;
516 status = SYS$FORCEX (&pid, 0, 0);
517 break;
518 #else /* not VMS */
519 #if defined (BSD_SYSTEM) || (defined (HPUX) && !defined (HPUX_5))
520 /* Note that kill returns -1 even if the process is just a zombie now.
521 But inevitably a SIGCHLD interrupt should be generated
522 and child_sig will do wait3 and make the process go away. */
523 /* There is some indication that there is a bug involved with
524 termination of subprocesses, perhaps involving a kernel bug too,
525 but no idea what it is. Just as a hunch we signal SIGCHLD to see
526 if that causes the problem to go away or get worse. */
527 sigsetmask (sigmask (SIGCHLD));
528 if (0 > kill (pid, 0))
530 sigsetmask (SIGEMPTYMASK);
531 kill (getpid (), SIGCHLD);
532 break;
534 if (wait_debugging)
535 sleep (1);
536 else
537 sigpause (SIGEMPTYMASK);
538 #else /* not BSD_SYSTEM, and not HPUX version >= 6 */
539 #if defined (UNIPLUS)
540 if (0 > kill (pid, 0))
541 break;
542 wait (0);
543 #else /* neither BSD_SYSTEM nor UNIPLUS: random sysV */
544 #ifdef POSIX_SIGNALS /* would this work for GNU/Linux as well? */
545 sigblock (sigmask (SIGCHLD));
546 errno = 0;
547 if (kill (pid, 0) == -1 && errno == ESRCH)
549 sigunblock (sigmask (SIGCHLD));
550 break;
553 sigsuspend (&empty_mask);
554 #else /* not POSIX_SIGNALS */
555 #ifdef HAVE_SYSV_SIGPAUSE
556 sighold (SIGCHLD);
557 if (0 > kill (pid, 0))
559 sigrelse (SIGCHLD);
560 break;
562 sigpause (SIGCHLD);
563 #else /* not HAVE_SYSV_SIGPAUSE */
564 #ifdef WINDOWSNT
565 wait (0);
566 break;
567 #else /* not WINDOWSNT */
568 if (0 > kill (pid, 0))
569 break;
570 /* Using sleep instead of pause avoids timing error.
571 If the inferior dies just before the sleep,
572 we lose just one second. */
573 sleep (1);
574 #endif /* not WINDOWSNT */
575 #endif /* not HAVE_SYSV_SIGPAUSE */
576 #endif /* not POSIX_SIGNALS */
577 #endif /* not UNIPLUS */
578 #endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
579 #endif /* not VMS */
580 #else /* not subprocesses */
581 #if __DJGPP__ > 1
582 break;
583 #else /* not __DJGPP__ > 1 */
584 #ifndef BSD4_1
585 if (kill (pid, 0) < 0)
586 break;
587 wait (0);
588 #else /* BSD4_1 */
589 int status;
590 status = wait (0);
591 if (status == pid || status == -1)
592 break;
593 #endif /* BSD4_1 */
594 #endif /* not __DJGPP__ > 1*/
595 #endif /* not subprocesses */
599 #ifdef subprocesses
602 * flush any pending output
603 * (may flush input as well; it does not matter the way we use it)
606 void
607 flush_pending_output (channel)
608 int channel;
610 #ifdef HAVE_TERMIOS
611 /* If we try this, we get hit with SIGTTIN, because
612 the child's tty belongs to the child's pgrp. */
613 #else
614 #ifdef TCFLSH
615 ioctl (channel, TCFLSH, 1);
616 #else
617 #ifdef TIOCFLUSH
618 int zero = 0;
619 /* 3rd arg should be ignored
620 but some 4.2 kernels actually want the address of an int
621 and nonzero means something different. */
622 ioctl (channel, TIOCFLUSH, &zero);
623 #endif
624 #endif
625 #endif
628 #ifndef VMS
629 /* Set up the terminal at the other end of a pseudo-terminal that
630 we will be controlling an inferior through.
631 It should not echo or do line-editing, since that is done
632 in Emacs. No padding needed for insertion into an Emacs buffer. */
634 void
635 child_setup_tty (out)
636 int out;
638 #ifndef DOS_NT
639 struct emacs_tty s;
641 EMACS_GET_TTY (out, &s);
643 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
644 s.main.c_oflag |= OPOST; /* Enable output postprocessing */
645 s.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL on output */
646 #ifdef NLDLY
647 s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
648 /* No output delays */
649 #endif
650 s.main.c_lflag &= ~ECHO; /* Disable echo */
651 s.main.c_lflag |= ISIG; /* Enable signals */
652 #if 0 /* This causes bugs in (for instance) telnet to certain sites. */
653 s.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
654 #ifdef INLCR /* Just being cautious, since I can't check how
655 widespread INLCR is--rms. */
656 s.main.c_iflag &= ~INLCR; /* Disable map of NL to CR on input */
657 #endif
658 #endif
659 #ifdef IUCLC
660 s.main.c_iflag &= ~IUCLC; /* Disable downcasing on input. */
661 #endif
662 #ifdef ISTRIP
663 s.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
664 #endif
665 #ifdef OLCUC
666 s.main.c_oflag &= ~OLCUC; /* Disable upcasing on output. */
667 #endif
668 s.main.c_oflag &= ~TAB3; /* Disable tab expansion */
669 s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
670 #if 0
671 /* Said to be unnecessary: */
672 s.main.c_cc[VMIN] = 1; /* minimum number of characters to accept */
673 s.main.c_cc[VTIME] = 0; /* wait forever for at least 1 character */
674 #endif
676 s.main.c_lflag |= ICANON; /* Enable erase/kill and eof processing */
677 s.main.c_cc[VEOF] = 04; /* insure that EOF is Control-D */
678 s.main.c_cc[VERASE] = CDISABLE; /* disable erase processing */
679 s.main.c_cc[VKILL] = CDISABLE; /* disable kill processing */
681 #ifdef HPUX
682 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
683 #endif /* HPUX */
685 #ifdef SIGNALS_VIA_CHARACTERS
686 /* the QUIT and INTR character are used in process_send_signal
687 so set them here to something useful. */
688 if (s.main.c_cc[VQUIT] == CDISABLE)
689 s.main.c_cc[VQUIT] = '\\'&037; /* Control-\ */
690 if (s.main.c_cc[VINTR] == CDISABLE)
691 s.main.c_cc[VINTR] = 'C'&037; /* Control-C */
692 #endif /* not SIGNALS_VIA_CHARACTERS */
694 #ifdef AIX
695 /* AIX enhanced edit loses NULs, so disable it */
696 #ifndef IBMR2AIX
697 s.main.c_line = 0;
698 s.main.c_iflag &= ~ASCEDIT;
699 #endif
700 /* Also, PTY overloads NUL and BREAK.
701 don't ignore break, but don't signal either, so it looks like NUL. */
702 s.main.c_iflag &= ~IGNBRK;
703 s.main.c_iflag &= ~BRKINT;
704 /* rms: Formerly it set s.main.c_cc[VINTR] to 0377 here
705 unconditionally. Then a SIGNALS_VIA_CHARACTERS conditional
706 would force it to 0377. That looks like duplicated code. */
707 #ifndef SIGNALS_VIA_CHARACTERS
708 /* QUIT and INTR work better as signals, so disable character forms */
709 s.main.c_cc[VQUIT] = CDISABLE;
710 s.main.c_cc[VINTR] = CDISABLE;
711 s.main.c_lflag &= ~ISIG;
712 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
713 s.main.c_cc[VEOL] = CDISABLE;
714 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
715 #endif /* AIX */
717 #else /* not HAVE_TERMIO */
719 s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
720 | CBREAK | TANDEM);
721 s.main.sg_flags |= LPASS8;
722 s.main.sg_erase = 0377;
723 s.main.sg_kill = 0377;
724 s.lmode = LLITOUT | s.lmode; /* Don't strip 8th bit */
726 #endif /* not HAVE_TERMIO */
728 EMACS_SET_TTY (out, &s, 0);
730 #ifdef BSD4_1
731 if (interrupt_input)
732 reset_sigio ();
733 #endif /* BSD4_1 */
734 #ifdef RTU
736 int zero = 0;
737 ioctl (out, FIOASYNC, &zero);
739 #endif /* RTU */
740 #endif /* not DOS_NT */
742 #endif /* not VMS */
744 #endif /* subprocesses */
746 /* Record a signal code and the handler for it. */
747 struct save_signal
749 int code;
750 SIGTYPE (*handler) P_ ((int));
753 static void save_signal_handlers P_ ((struct save_signal *));
754 static void restore_signal_handlers P_ ((struct save_signal *));
756 /* Suspend the Emacs process; give terminal to its superior. */
758 void
759 sys_suspend ()
761 #ifdef VMS
762 /* "Foster" parentage allows emacs to return to a subprocess that attached
763 to the current emacs as a cheaper than starting a whole new process. This
764 is set up by KEPTEDITOR.COM. */
765 unsigned long parent_id, foster_parent_id;
766 char *fpid_string;
768 fpid_string = getenv ("EMACS_PARENT_PID");
769 if (fpid_string != NULL)
771 sscanf (fpid_string, "%x", &foster_parent_id);
772 if (foster_parent_id != 0)
773 parent_id = foster_parent_id;
774 else
775 parent_id = getppid ();
777 else
778 parent_id = getppid ();
780 xfree (fpid_string); /* On VMS, this was malloc'd */
782 if (parent_id && parent_id != 0xffffffff)
784 SIGTYPE (*oldsig)() = (int) signal (SIGINT, SIG_IGN);
785 int status = LIB$ATTACH (&parent_id) & 1;
786 signal (SIGINT, oldsig);
787 return status;
789 else
791 struct {
792 int l;
793 char *a;
794 } d_prompt;
795 d_prompt.l = sizeof ("Emacs: "); /* Our special prompt */
796 d_prompt.a = "Emacs: "; /* Just a reminder */
797 LIB$SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt, 0);
798 return 1;
800 return -1;
801 #else
802 #if defined (SIGTSTP) && !defined (MSDOS)
805 int pgrp = EMACS_GETPGRP (0);
806 EMACS_KILLPG (pgrp, SIGTSTP);
809 #else /* No SIGTSTP */
810 #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
811 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
812 kill (getpid (), SIGQUIT);
814 #else /* No SIGTSTP or USG_JOBCTRL */
816 /* On a system where suspending is not implemented,
817 instead fork a subshell and let it talk directly to the terminal
818 while we wait. */
819 sys_subshell ();
821 #endif /* no USG_JOBCTRL */
822 #endif /* no SIGTSTP */
823 #endif /* not VMS */
826 /* Fork a subshell. */
828 #ifndef MAC_OS8
829 void
830 sys_subshell ()
832 #ifndef VMS
833 #ifdef DOS_NT /* Demacs 1.1.2 91/10/20 Manabu Higashida */
834 int st;
835 char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS. */
836 #endif
837 int pid;
838 struct save_signal saved_handlers[5];
839 Lisp_Object dir;
840 unsigned char *str = 0;
841 int len;
843 saved_handlers[0].code = SIGINT;
844 saved_handlers[1].code = SIGQUIT;
845 saved_handlers[2].code = SIGTERM;
846 #ifdef SIGIO
847 saved_handlers[3].code = SIGIO;
848 saved_handlers[4].code = 0;
849 #else
850 saved_handlers[3].code = 0;
851 #endif
853 /* Mentioning current_buffer->buffer would mean including buffer.h,
854 which somehow wedges the hp compiler. So instead... */
856 dir = intern ("default-directory");
857 if (NILP (Fboundp (dir)))
858 goto xyzzy;
859 dir = Fsymbol_value (dir);
860 if (!STRINGP (dir))
861 goto xyzzy;
863 dir = expand_and_dir_to_file (Funhandled_file_name_directory (dir), Qnil);
864 str = (unsigned char *) alloca (SCHARS (dir) + 2);
865 len = SCHARS (dir);
866 bcopy (SDATA (dir), str, len);
867 if (str[len - 1] != '/') str[len++] = '/';
868 str[len] = 0;
869 xyzzy:
871 #ifdef DOS_NT
872 pid = 0;
873 #if __DJGPP__ > 1
874 save_signal_handlers (saved_handlers);
875 synch_process_alive = 1;
876 #endif /* __DJGPP__ > 1 */
877 #else
878 pid = vfork ();
879 if (pid == -1)
880 error ("Can't spawn subshell");
881 #endif
883 if (pid == 0)
885 char *sh = 0;
887 #ifdef DOS_NT /* MW, Aug 1993 */
888 getwd (oldwd);
889 if (sh == 0)
890 sh = (char *) egetenv ("SUSPEND"); /* KFS, 1994-12-14 */
891 #endif
892 if (sh == 0)
893 sh = (char *) egetenv ("SHELL");
894 if (sh == 0)
895 sh = "sh";
897 /* Use our buffer's default directory for the subshell. */
898 if (str)
899 chdir ((char *) str);
901 #ifdef subprocesses
902 close_process_descs (); /* Close Emacs's pipes/ptys */
903 #endif
905 #ifdef SET_EMACS_PRIORITY
907 extern EMACS_INT emacs_priority;
909 if (emacs_priority < 0)
910 nice (-emacs_priority);
912 #endif
914 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
916 char *epwd = getenv ("PWD");
917 char old_pwd[MAXPATHLEN+1+4];
919 /* If PWD is set, pass it with corrected value. */
920 if (epwd)
922 strcpy (old_pwd, epwd);
923 if (str[len - 1] == '/')
924 str[len - 1] = '\0';
925 setenv ("PWD", str, 1);
927 st = system (sh);
928 chdir (oldwd);
929 if (epwd)
930 putenv (old_pwd); /* restore previous value */
932 #if 0 /* This is also reported if last command executed in subshell failed, KFS */
933 if (st)
934 report_file_error ("Can't execute subshell", Fcons (build_string (sh), Qnil));
935 #endif
936 #else /* not MSDOS */
937 #ifdef WINDOWSNT
938 /* Waits for process completion */
939 pid = _spawnlp (_P_WAIT, sh, sh, NULL);
940 chdir (oldwd);
941 if (pid == -1)
942 write (1, "Can't execute subshell", 22);
943 #else /* not WINDOWSNT */
944 execlp (sh, sh, (char *) 0);
945 write (1, "Can't execute subshell", 22);
946 _exit (1);
947 #endif /* not WINDOWSNT */
948 #endif /* not MSDOS */
951 /* Do this now if we did not do it before. */
952 #if !defined (MSDOS) || __DJGPP__ == 1
953 save_signal_handlers (saved_handlers);
954 synch_process_alive = 1;
955 #endif
957 #ifndef DOS_NT
958 wait_for_termination (pid);
959 #endif
960 restore_signal_handlers (saved_handlers);
961 synch_process_alive = 0;
962 #endif /* !VMS */
964 #endif /* !MAC_OS8 */
966 static void
967 save_signal_handlers (saved_handlers)
968 struct save_signal *saved_handlers;
970 while (saved_handlers->code)
972 saved_handlers->handler
973 = (SIGTYPE (*) P_ ((int))) signal (saved_handlers->code, SIG_IGN);
974 saved_handlers++;
978 static void
979 restore_signal_handlers (saved_handlers)
980 struct save_signal *saved_handlers;
982 while (saved_handlers->code)
984 signal (saved_handlers->code, saved_handlers->handler);
985 saved_handlers++;
989 #ifdef F_SETFL
991 int old_fcntl_flags;
993 void
994 init_sigio (fd)
995 int fd;
997 #ifdef FASYNC
998 old_fcntl_flags = fcntl (fd, F_GETFL, 0) & ~FASYNC;
999 fcntl (fd, F_SETFL, old_fcntl_flags | FASYNC);
1000 #endif
1001 interrupts_deferred = 0;
1004 void
1005 reset_sigio ()
1007 unrequest_sigio ();
1010 #ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
1012 void
1013 request_sigio ()
1015 if (noninteractive || read_socket_hook)
1016 return;
1018 #ifdef SIGWINCH
1019 sigunblock (sigmask (SIGWINCH));
1020 #endif
1021 fcntl (input_fd, F_SETFL, old_fcntl_flags | FASYNC);
1023 interrupts_deferred = 0;
1026 void
1027 unrequest_sigio ()
1029 if (noninteractive || read_socket_hook)
1030 return;
1032 #ifdef SIGWINCH
1033 sigblock (sigmask (SIGWINCH));
1034 #endif
1035 fcntl (input_fd, F_SETFL, old_fcntl_flags);
1036 interrupts_deferred = 1;
1039 #else /* no FASYNC */
1040 #ifdef STRIDE /* Stride doesn't have FASYNC - use FIOASYNC */
1042 void
1043 request_sigio ()
1045 int on = 1;
1047 if (noninteractive || read_socket_hook)
1048 return;
1050 ioctl (input_fd, FIOASYNC, &on);
1051 interrupts_deferred = 0;
1054 void
1055 unrequest_sigio ()
1057 int off = 0;
1059 if (noninteractive || read_socket_hook)
1060 return;
1062 ioctl (input_fd, FIOASYNC, &off);
1063 interrupts_deferred = 1;
1066 #else /* not FASYNC, not STRIDE */
1068 #ifdef _CX_UX
1070 #include <termios.h>
1072 void
1073 request_sigio ()
1075 int on = 1;
1076 sigset_t st;
1078 if (noninteractive || read_socket_hook)
1079 return;
1081 sigemptyset (&st);
1082 sigaddset (&st, SIGIO);
1083 ioctl (input_fd, FIOASYNC, &on);
1084 interrupts_deferred = 0;
1085 sigprocmask (SIG_UNBLOCK, &st, (sigset_t *)0);
1088 void
1089 unrequest_sigio ()
1091 int off = 0;
1093 if (noninteractive || read_socket_hook)
1094 return;
1096 ioctl (input_fd, FIOASYNC, &off);
1097 interrupts_deferred = 1;
1100 #else /* ! _CX_UX */
1101 #ifndef MSDOS
1103 void
1104 request_sigio ()
1106 if (noninteractive || read_socket_hook)
1107 return;
1109 croak ("request_sigio");
1112 void
1113 unrequest_sigio ()
1115 if (noninteractive || read_socket_hook)
1116 return;
1118 croak ("unrequest_sigio");
1121 #endif /* MSDOS */
1122 #endif /* _CX_UX */
1123 #endif /* STRIDE */
1124 #endif /* FASYNC */
1125 #endif /* F_SETFL */
1127 /* Saving and restoring the process group of Emacs's terminal. */
1129 #ifdef BSD_PGRPS
1131 /* The process group of which Emacs was a member when it initially
1132 started.
1134 If Emacs was in its own process group (i.e. inherited_pgroup ==
1135 getpid ()), then we know we're running under a shell with job
1136 control (Emacs would never be run as part of a pipeline).
1137 Everything is fine.
1139 If Emacs was not in its own process group, then we know we're
1140 running under a shell (or a caller) that doesn't know how to
1141 separate itself from Emacs (like sh). Emacs must be in its own
1142 process group in order to receive SIGIO correctly. In this
1143 situation, we put ourselves in our own pgroup, forcibly set the
1144 tty's pgroup to our pgroup, and make sure to restore and reinstate
1145 the tty's pgroup just like any other terminal setting. If
1146 inherited_group was not the tty's pgroup, then we'll get a
1147 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
1148 it goes foreground in the future, which is what should happen. */
1149 int inherited_pgroup;
1151 /* Split off the foreground process group to Emacs alone.
1152 When we are in the foreground, but not started in our own process
1153 group, redirect the TTY to point to our own process group. We need
1154 to be in our own process group to receive SIGIO properly. */
1155 void
1156 narrow_foreground_group ()
1158 int me = getpid ();
1160 setpgrp (0, inherited_pgroup);
1161 if (inherited_pgroup != me)
1162 EMACS_SET_TTY_PGRP (input_fd, &me);
1163 setpgrp (0, me);
1166 /* Set the tty to our original foreground group. */
1167 void
1168 widen_foreground_group ()
1170 if (inherited_pgroup != getpid ())
1171 EMACS_SET_TTY_PGRP (input_fd, &inherited_pgroup);
1172 setpgrp (0, inherited_pgroup);
1175 #endif /* BSD_PGRPS */
1177 /* Getting and setting emacs_tty structures. */
1179 /* Set *TC to the parameters associated with the terminal FD.
1180 Return zero if all's well, or -1 if we ran into an error we
1181 couldn't deal with. */
1183 emacs_get_tty (fd, settings)
1184 int fd;
1185 struct emacs_tty *settings;
1187 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
1188 #ifdef HAVE_TCATTR
1189 /* We have those nifty POSIX tcmumbleattr functions. */
1190 bzero (&settings->main, sizeof (settings->main));
1191 if (tcgetattr (fd, &settings->main) < 0)
1192 return -1;
1194 #else
1195 #ifdef HAVE_TERMIO
1196 /* The SYSV-style interface? */
1197 if (ioctl (fd, TCGETA, &settings->main) < 0)
1198 return -1;
1200 #else
1201 #ifdef VMS
1202 /* Vehemently Monstrous System? :-) */
1203 if (! (SYS$QIOW (0, fd, IO$_SENSEMODE, settings, 0, 0,
1204 &settings->main.class, 12, 0, 0, 0, 0)
1205 & 1))
1206 return -1;
1208 #else
1209 #ifndef DOS_NT
1210 /* I give up - I hope you have the BSD ioctls. */
1211 if (ioctl (fd, TIOCGETP, &settings->main) < 0)
1212 return -1;
1213 #endif /* not DOS_NT */
1214 #endif
1215 #endif
1216 #endif
1218 /* Suivant - Do we have to get struct ltchars data? */
1219 #ifdef HAVE_LTCHARS
1220 if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
1221 return -1;
1222 #endif
1224 /* How about a struct tchars and a wordful of lmode bits? */
1225 #ifdef HAVE_TCHARS
1226 if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
1227 || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
1228 return -1;
1229 #endif
1231 /* We have survived the tempest. */
1232 return 0;
1236 /* Set the parameters of the tty on FD according to the contents of
1237 *SETTINGS. If FLUSHP is non-zero, we discard input.
1238 Return 0 if all went well, and -1 if anything failed. */
1241 emacs_set_tty (fd, settings, flushp)
1242 int fd;
1243 struct emacs_tty *settings;
1244 int flushp;
1246 /* Set the primary parameters - baud rate, character size, etcetera. */
1247 #ifdef HAVE_TCATTR
1248 int i;
1249 /* We have those nifty POSIX tcmumbleattr functions.
1250 William J. Smith <wjs@wiis.wang.com> writes:
1251 "POSIX 1003.1 defines tcsetattr to return success if it was
1252 able to perform any of the requested actions, even if some
1253 of the requested actions could not be performed.
1254 We must read settings back to ensure tty setup properly.
1255 AIX requires this to keep tty from hanging occasionally." */
1256 /* This make sure that we don't loop indefinitely in here. */
1257 for (i = 0 ; i < 10 ; i++)
1258 if (tcsetattr (fd, flushp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
1260 if (errno == EINTR)
1261 continue;
1262 else
1263 return -1;
1265 else
1267 struct termios new;
1269 bzero (&new, sizeof (new));
1270 /* Get the current settings, and see if they're what we asked for. */
1271 tcgetattr (fd, &new);
1272 /* We cannot use memcmp on the whole structure here because under
1273 * aix386 the termios structure has some reserved field that may
1274 * not be filled in.
1276 if ( new.c_iflag == settings->main.c_iflag
1277 && new.c_oflag == settings->main.c_oflag
1278 && new.c_cflag == settings->main.c_cflag
1279 && new.c_lflag == settings->main.c_lflag
1280 && memcmp (new.c_cc, settings->main.c_cc, NCCS) == 0)
1281 break;
1282 else
1283 continue;
1286 #else
1287 #ifdef HAVE_TERMIO
1288 /* The SYSV-style interface? */
1289 if (ioctl (fd, flushp ? TCSETAF : TCSETAW, &settings->main) < 0)
1290 return -1;
1292 #else
1293 #ifdef VMS
1294 /* Vehemently Monstrous System? :-) */
1295 if (! (SYS$QIOW (0, fd, IO$_SETMODE, &input_iosb, 0, 0,
1296 &settings->main.class, 12, 0, 0, 0, 0)
1297 & 1))
1298 return -1;
1300 #else
1301 #ifndef DOS_NT
1302 /* I give up - I hope you have the BSD ioctls. */
1303 if (ioctl (fd, (flushp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
1304 return -1;
1305 #endif /* not DOS_NT */
1307 #endif
1308 #endif
1309 #endif
1311 /* Suivant - Do we have to get struct ltchars data? */
1312 #ifdef HAVE_LTCHARS
1313 if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
1314 return -1;
1315 #endif
1317 /* How about a struct tchars and a wordful of lmode bits? */
1318 #ifdef HAVE_TCHARS
1319 if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
1320 || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
1321 return -1;
1322 #endif
1324 /* We have survived the tempest. */
1325 return 0;
1329 /* The initial tty mode bits */
1330 struct emacs_tty old_tty;
1332 /* 1 if we have been through init_sys_modes. */
1333 int term_initted;
1335 /* 1 if outer tty status has been recorded. */
1336 int old_tty_valid;
1338 #ifdef BSD4_1
1339 /* BSD 4.1 needs to keep track of the lmode bits in order to start
1340 sigio. */
1341 int lmode;
1342 #endif
1344 #ifndef F_SETOWN_BUG
1345 #ifdef F_SETOWN
1346 int old_fcntl_owner;
1347 #endif /* F_SETOWN */
1348 #endif /* F_SETOWN_BUG */
1350 /* This may also be defined in stdio,
1351 but if so, this does no harm,
1352 and using the same name avoids wasting the other one's space. */
1354 #ifdef nec_ews_svr4
1355 extern char *_sobuf ;
1356 #else
1357 #if defined (USG) || defined (DGUX)
1358 unsigned char _sobuf[BUFSIZ+8];
1359 #else
1360 char _sobuf[BUFSIZ];
1361 #endif
1362 #endif
1364 #ifdef HAVE_LTCHARS
1365 static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
1366 #endif
1367 #ifdef HAVE_TCHARS
1368 static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
1369 #endif
1371 void
1372 init_sys_modes ()
1374 struct emacs_tty tty;
1376 #ifdef VMS
1377 #if 0
1378 static int oob_chars[2] = {0, 1 << 7}; /* catch C-g's */
1379 extern int (*interrupt_signal) ();
1380 #endif
1381 #endif
1383 Vtty_erase_char = Qnil;
1385 if (noninteractive)
1386 return;
1388 #ifdef VMS
1389 if (!input_ef)
1390 input_ef = get_kbd_event_flag ();
1391 /* LIB$GET_EF (&input_ef); */
1392 SYS$CLREF (input_ef);
1393 waiting_for_ast = 0;
1394 if (!timer_ef)
1395 timer_ef = get_timer_event_flag ();
1396 /* LIB$GET_EF (&timer_ef); */
1397 SYS$CLREF (timer_ef);
1398 #if 0
1399 if (!process_ef)
1401 LIB$GET_EF (&process_ef);
1402 SYS$CLREF (process_ef);
1404 if (input_ef / 32 != process_ef / 32)
1405 croak ("Input and process event flags in different clusters.");
1406 #endif
1407 if (input_ef / 32 != timer_ef / 32)
1408 croak ("Input and timer event flags in different clusters.");
1409 #if 0
1410 input_eflist = ((unsigned) 1 << (input_ef % 32)) |
1411 ((unsigned) 1 << (process_ef % 32));
1412 #endif
1413 timer_eflist = ((unsigned) 1 << (input_ef % 32)) |
1414 ((unsigned) 1 << (timer_ef % 32));
1415 #ifndef VMS4_4
1416 sys_access_reinit ();
1417 #endif
1418 #endif /* not VMS */
1420 #ifdef BSD_PGRPS
1421 if (! read_socket_hook && EQ (Vwindow_system, Qnil))
1422 narrow_foreground_group ();
1423 #endif
1425 #ifdef HAVE_WINDOW_SYSTEM
1426 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1427 needs the initialization code below. */
1428 if (!read_socket_hook && EQ (Vwindow_system, Qnil))
1429 #endif
1431 EMACS_GET_TTY (input_fd, &old_tty);
1433 old_tty_valid = 1;
1435 tty = old_tty;
1437 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
1438 XSETINT (Vtty_erase_char, old_tty.main.c_cc[VERASE]);
1440 #ifdef DGUX
1441 /* This allows meta to be sent on 8th bit. */
1442 tty.main.c_iflag &= ~INPCK; /* don't check input for parity */
1443 #endif
1444 tty.main.c_iflag |= (IGNBRK); /* Ignore break condition */
1445 tty.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
1446 #ifdef INLCR /* I'm just being cautious,
1447 since I can't check how widespread INLCR is--rms. */
1448 tty.main.c_iflag &= ~INLCR; /* Disable map of NL to CR on input */
1449 #endif
1450 #ifdef ISTRIP
1451 tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
1452 #endif
1453 tty.main.c_lflag &= ~ECHO; /* Disable echo */
1454 tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */
1455 #ifdef IEXTEN
1456 tty.main.c_lflag &= ~IEXTEN; /* Disable other editing characters. */
1457 #endif
1458 tty.main.c_lflag |= ISIG; /* Enable signals */
1459 if (flow_control)
1461 tty.main.c_iflag |= IXON; /* Enable start/stop output control */
1462 #ifdef IXANY
1463 tty.main.c_iflag &= ~IXANY;
1464 #endif /* IXANY */
1466 else
1467 tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */
1468 tty.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL
1469 on output */
1470 tty.main.c_oflag &= ~TAB3; /* Disable tab expansion */
1471 #ifdef CS8
1472 if (meta_key)
1474 tty.main.c_cflag |= CS8; /* allow 8th bit on input */
1475 tty.main.c_cflag &= ~PARENB;/* Don't check parity */
1477 #endif
1478 tty.main.c_cc[VINTR] = quit_char; /* C-g (usually) gives SIGINT */
1479 /* Set up C-g for both SIGQUIT and SIGINT.
1480 We don't know which we will get, but we handle both alike
1481 so which one it really gives us does not matter. */
1482 tty.main.c_cc[VQUIT] = quit_char;
1483 tty.main.c_cc[VMIN] = 1; /* Input should wait for at least 1 char */
1484 tty.main.c_cc[VTIME] = 0; /* no matter how long that takes. */
1485 #ifdef VSWTCH
1486 tty.main.c_cc[VSWTCH] = CDISABLE; /* Turn off shell layering use
1487 of C-z */
1488 #endif /* VSWTCH */
1490 #if defined (mips) || defined (HAVE_TCATTR)
1491 #ifdef VSUSP
1492 tty.main.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z. */
1493 #endif /* VSUSP */
1494 #ifdef V_DSUSP
1495 tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y. */
1496 #endif /* V_DSUSP */
1497 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1498 tty.main.c_cc[VDSUSP] = CDISABLE;
1499 #endif /* VDSUSP */
1500 #ifdef VLNEXT
1501 tty.main.c_cc[VLNEXT] = CDISABLE;
1502 #endif /* VLNEXT */
1503 #ifdef VREPRINT
1504 tty.main.c_cc[VREPRINT] = CDISABLE;
1505 #endif /* VREPRINT */
1506 #ifdef VWERASE
1507 tty.main.c_cc[VWERASE] = CDISABLE;
1508 #endif /* VWERASE */
1509 #ifdef VDISCARD
1510 tty.main.c_cc[VDISCARD] = CDISABLE;
1511 #endif /* VDISCARD */
1513 if (flow_control)
1515 #ifdef VSTART
1516 tty.main.c_cc[VSTART] = '\021';
1517 #endif /* VSTART */
1518 #ifdef VSTOP
1519 tty.main.c_cc[VSTOP] = '\023';
1520 #endif /* VSTOP */
1522 else
1524 #ifdef VSTART
1525 tty.main.c_cc[VSTART] = CDISABLE;
1526 #endif /* VSTART */
1527 #ifdef VSTOP
1528 tty.main.c_cc[VSTOP] = CDISABLE;
1529 #endif /* VSTOP */
1531 #endif /* mips or HAVE_TCATTR */
1533 #ifdef SET_LINE_DISCIPLINE
1534 /* Need to explicitly request TERMIODISC line discipline or
1535 Ultrix's termios does not work correctly. */
1536 tty.main.c_line = SET_LINE_DISCIPLINE;
1537 #endif
1538 #ifdef AIX
1539 #ifndef IBMR2AIX
1540 /* AIX enhanced edit loses NULs, so disable it. */
1541 tty.main.c_line = 0;
1542 tty.main.c_iflag &= ~ASCEDIT;
1543 #else
1544 tty.main.c_cc[VSTRT] = CDISABLE;
1545 tty.main.c_cc[VSTOP] = CDISABLE;
1546 tty.main.c_cc[VSUSP] = CDISABLE;
1547 tty.main.c_cc[VDSUSP] = CDISABLE;
1548 #endif /* IBMR2AIX */
1549 if (flow_control)
1551 #ifdef VSTART
1552 tty.main.c_cc[VSTART] = '\021';
1553 #endif /* VSTART */
1554 #ifdef VSTOP
1555 tty.main.c_cc[VSTOP] = '\023';
1556 #endif /* VSTOP */
1558 /* Also, PTY overloads NUL and BREAK.
1559 don't ignore break, but don't signal either, so it looks like NUL.
1560 This really serves a purpose only if running in an XTERM window
1561 or via TELNET or the like, but does no harm elsewhere. */
1562 tty.main.c_iflag &= ~IGNBRK;
1563 tty.main.c_iflag &= ~BRKINT;
1564 #endif
1565 #else /* if not HAVE_TERMIO */
1566 #ifdef VMS
1567 tty.main.tt_char |= TT$M_NOECHO;
1568 if (meta_key)
1569 tty.main.tt_char |= TT$M_EIGHTBIT;
1570 if (flow_control)
1571 tty.main.tt_char |= TT$M_TTSYNC;
1572 else
1573 tty.main.tt_char &= ~TT$M_TTSYNC;
1574 tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
1575 #else /* not VMS (BSD, that is) */
1576 #ifndef DOS_NT
1577 XSETINT (Vtty_erase_char, tty.main.sg_erase);
1578 tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
1579 if (meta_key)
1580 tty.main.sg_flags |= ANYP;
1581 tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
1582 #endif /* not DOS_NT */
1583 #endif /* not VMS (BSD, that is) */
1584 #endif /* not HAVE_TERMIO */
1586 /* If going to use CBREAK mode, we must request C-g to interrupt
1587 and turn off start and stop chars, etc. If not going to use
1588 CBREAK mode, do this anyway so as to turn off local flow
1589 control for user coming over network on 4.2; in this case,
1590 only t_stopc and t_startc really matter. */
1591 #ifndef HAVE_TERMIO
1592 #ifdef HAVE_TCHARS
1593 /* Note: if not using CBREAK mode, it makes no difference how we
1594 set this */
1595 tty.tchars = new_tchars;
1596 tty.tchars.t_intrc = quit_char;
1597 if (flow_control)
1599 tty.tchars.t_startc = '\021';
1600 tty.tchars.t_stopc = '\023';
1603 tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | old_tty.lmode;
1604 #ifdef ultrix
1605 /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
1606 anything, and leaving it in breaks the meta key. Go figure. */
1607 tty.lmode &= ~LLITOUT;
1608 #endif
1610 #ifdef BSD4_1
1611 lmode = tty.lmode;
1612 #endif
1614 #endif /* HAVE_TCHARS */
1615 #endif /* not HAVE_TERMIO */
1617 #ifdef HAVE_LTCHARS
1618 tty.ltchars = new_ltchars;
1619 #endif /* HAVE_LTCHARS */
1620 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
1621 if (!term_initted)
1622 internal_terminal_init ();
1623 dos_ttraw ();
1624 #endif
1626 EMACS_SET_TTY (input_fd, &tty, 0);
1628 /* This code added to insure that, if flow-control is not to be used,
1629 we have an unlocked terminal at the start. */
1631 #ifdef TCXONC
1632 if (!flow_control) ioctl (input_fd, TCXONC, 1);
1633 #endif
1634 #ifndef APOLLO
1635 #ifdef TIOCSTART
1636 if (!flow_control) ioctl (input_fd, TIOCSTART, 0);
1637 #endif
1638 #endif
1640 #if defined (HAVE_TERMIOS) || defined (HPUX9)
1641 #ifdef TCOON
1642 if (!flow_control) tcflow (input_fd, TCOON);
1643 #endif
1644 #endif
1646 #ifdef AIXHFT
1647 hft_init ();
1648 #ifdef IBMR2AIX
1650 /* IBM's HFT device usually thinks a ^J should be LF/CR. We need it
1651 to be only LF. This is the way that is done. */
1652 struct termio tty;
1654 if (ioctl (1, HFTGETID, &tty) != -1)
1655 write (1, "\033[20l", 5);
1657 #endif
1658 #endif /* AIXHFT */
1660 #ifdef VMS
1661 /* Appears to do nothing when in PASTHRU mode.
1662 SYS$QIOW (0, input_fd, IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
1663 interrupt_signal, oob_chars, 0, 0, 0, 0);
1665 queue_kbd_input (0);
1666 #endif /* VMS */
1669 #ifdef F_SETFL
1670 #ifndef F_SETOWN_BUG
1671 #ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
1672 if (interrupt_input
1673 && ! read_socket_hook && EQ (Vwindow_system, Qnil))
1675 old_fcntl_owner = fcntl (input_fd, F_GETOWN, 0);
1676 fcntl (input_fd, F_SETOWN, getpid ());
1677 init_sigio (input_fd);
1679 #endif /* F_GETOWN */
1680 #endif /* F_SETOWN_BUG */
1681 #endif /* F_SETFL */
1683 #ifdef BSD4_1
1684 if (interrupt_input)
1685 init_sigio (input_fd);
1686 #endif
1688 #ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1689 #undef _IOFBF
1690 #endif
1691 #ifdef _IOFBF
1692 /* This symbol is defined on recent USG systems.
1693 Someone says without this call USG won't really buffer the file
1694 even with a call to setbuf. */
1695 setvbuf (stdout, (char *) _sobuf, _IOFBF, sizeof _sobuf);
1696 #else
1697 setbuf (stdout, (char *) _sobuf);
1698 #endif
1699 #ifdef HAVE_WINDOW_SYSTEM
1700 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1701 needs the initialization code below. */
1702 if (EQ (Vwindow_system, Qnil)
1703 #ifndef WINDOWSNT
1704 /* When running in tty mode on NT/Win95, we have a read_socket
1705 hook, but still need the rest of the initialization code below. */
1706 && (! read_socket_hook)
1707 #endif
1709 #endif
1710 set_terminal_modes ();
1712 if (!term_initted
1713 && FRAMEP (Vterminal_frame)
1714 && FRAME_TERMCAP_P (XFRAME (Vterminal_frame)))
1715 init_frame_faces (XFRAME (Vterminal_frame));
1717 if (term_initted && no_redraw_on_reenter)
1719 if (display_completed)
1720 direct_output_forward_char (0);
1722 else
1724 frame_garbaged = 1;
1725 if (FRAMEP (Vterminal_frame))
1726 FRAME_GARBAGED_P (XFRAME (Vterminal_frame)) = 1;
1729 term_initted = 1;
1732 /* Return nonzero if safe to use tabs in output.
1733 At the time this is called, init_sys_modes has not been done yet. */
1736 tabs_safe_p ()
1738 struct emacs_tty tty;
1740 EMACS_GET_TTY (input_fd, &tty);
1741 return EMACS_TTY_TABS_OK (&tty);
1744 /* Get terminal size from system.
1745 Store number of lines into *HEIGHTP and width into *WIDTHP.
1746 We store 0 if there's no valid information. */
1748 void
1749 get_frame_size (widthp, heightp)
1750 int *widthp, *heightp;
1753 #ifdef TIOCGWINSZ
1755 /* BSD-style. */
1756 struct winsize size;
1758 if (ioctl (input_fd, TIOCGWINSZ, &size) == -1)
1759 *widthp = *heightp = 0;
1760 else
1762 *widthp = size.ws_col;
1763 *heightp = size.ws_row;
1766 #else
1767 #ifdef TIOCGSIZE
1769 /* SunOS - style. */
1770 struct ttysize size;
1772 if (ioctl (input_fd, TIOCGSIZE, &size) == -1)
1773 *widthp = *heightp = 0;
1774 else
1776 *widthp = size.ts_cols;
1777 *heightp = size.ts_lines;
1780 #else
1781 #ifdef VMS
1783 /* Use a fresh channel since the current one may have stale info
1784 (for example, from prior to a suspend); and to avoid a dependency
1785 in the init sequence. */
1786 int chan;
1787 struct sensemode tty;
1789 SYS$ASSIGN (&input_dsc, &chan, 0, 0);
1790 SYS$QIOW (0, chan, IO$_SENSEMODE, &tty, 0, 0,
1791 &tty.class, 12, 0, 0, 0, 0);
1792 SYS$DASSGN (chan);
1793 *widthp = tty.scr_wid;
1794 *heightp = tty.scr_len;
1796 #else
1797 #ifdef MSDOS
1798 *widthp = ScreenCols ();
1799 *heightp = ScreenRows ();
1800 #else /* system doesn't know size */
1801 *widthp = 0;
1802 *heightp = 0;
1803 #endif
1805 #endif /* not VMS */
1806 #endif /* not SunOS-style */
1807 #endif /* not BSD-style */
1810 /* Set the logical window size associated with descriptor FD
1811 to HEIGHT and WIDTH. This is used mainly with ptys. */
1814 set_window_size (fd, height, width)
1815 int fd, height, width;
1817 #ifdef TIOCSWINSZ
1819 /* BSD-style. */
1820 struct winsize size;
1821 size.ws_row = height;
1822 size.ws_col = width;
1824 if (ioctl (fd, TIOCSWINSZ, &size) == -1)
1825 return 0; /* error */
1826 else
1827 return 1;
1829 #else
1830 #ifdef TIOCSSIZE
1832 /* SunOS - style. */
1833 struct ttysize size;
1834 size.ts_lines = height;
1835 size.ts_cols = width;
1837 if (ioctl (fd, TIOCGSIZE, &size) == -1)
1838 return 0;
1839 else
1840 return 1;
1841 #else
1842 return -1;
1843 #endif /* not SunOS-style */
1844 #endif /* not BSD-style */
1848 /* Prepare the terminal for exiting Emacs; move the cursor to the
1849 bottom of the frame, turn off interrupt-driven I/O, etc. */
1850 void
1851 reset_sys_modes ()
1853 struct frame *sf;
1855 if (noninteractive)
1857 fflush (stdout);
1858 return;
1860 if (!term_initted)
1861 return;
1862 #ifdef HAVE_WINDOW_SYSTEM
1863 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1864 needs the clean-up code below. */
1865 if (!EQ (Vwindow_system, Qnil)
1866 #ifndef WINDOWSNT
1867 /* When running in tty mode on NT/Win95, we have a read_socket
1868 hook, but still need the rest of the clean-up code below. */
1869 || read_socket_hook
1870 #endif
1872 return;
1873 #endif
1874 sf = SELECTED_FRAME ();
1875 cursor_to (FRAME_LINES (sf) - 1, 0);
1876 clear_end_of_line (FRAME_COLS (sf));
1877 /* clear_end_of_line may move the cursor */
1878 cursor_to (FRAME_LINES (sf) - 1, 0);
1879 #if defined (IBMR2AIX) && defined (AIXHFT)
1881 /* HFT devices normally use ^J as a LF/CR. We forced it to
1882 do the LF only. Now, we need to reset it. */
1883 struct termio tty;
1885 if (ioctl (1, HFTGETID, &tty) != -1)
1886 write (1, "\033[20h", 5);
1888 #endif
1890 reset_terminal_modes ();
1891 fflush (stdout);
1892 #ifdef BSD_SYSTEM
1893 #ifndef BSD4_1
1894 /* Avoid possible loss of output when changing terminal modes. */
1895 fsync (fileno (stdout));
1896 #endif
1897 #endif
1899 #ifdef F_SETFL
1900 #ifndef F_SETOWN_BUG
1901 #ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
1902 if (interrupt_input)
1904 reset_sigio ();
1905 fcntl (input_fd, F_SETOWN, old_fcntl_owner);
1907 #endif /* F_SETOWN */
1908 #endif /* F_SETOWN_BUG */
1909 #ifdef O_NDELAY
1910 fcntl (input_fd, F_SETFL, fcntl (input_fd, F_GETFL, 0) & ~O_NDELAY);
1911 #endif
1912 #endif /* F_SETFL */
1913 #ifdef BSD4_1
1914 if (interrupt_input)
1915 reset_sigio ();
1916 #endif /* BSD4_1 */
1918 if (old_tty_valid)
1919 while (EMACS_SET_TTY (input_fd, &old_tty, 0) < 0 && errno == EINTR)
1922 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1923 dos_ttcooked ();
1924 #endif
1926 #ifdef SET_LINE_DISCIPLINE
1927 /* Ultrix's termios *ignores* any line discipline except TERMIODISC.
1928 A different old line discipline is therefore not restored, yet.
1929 Restore the old line discipline by hand. */
1930 ioctl (0, TIOCSETD, &old_tty.main.c_line);
1931 #endif
1933 #ifdef AIXHFT
1934 hft_reset ();
1935 #endif
1937 #ifdef BSD_PGRPS
1938 widen_foreground_group ();
1939 #endif
1942 #ifdef HAVE_PTYS
1944 /* Set up the proper status flags for use of a pty. */
1946 void
1947 setup_pty (fd)
1948 int fd;
1950 /* I'm told that TOICREMOTE does not mean control chars
1951 "can't be sent" but rather that they don't have
1952 input-editing or signaling effects.
1953 That should be good, because we have other ways
1954 to do those things in Emacs.
1955 However, telnet mode seems not to work on 4.2.
1956 So TIOCREMOTE is turned off now. */
1958 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1959 will hang. In particular, the "timeout" feature (which
1960 causes a read to return if there is no data available)
1961 does this. Also it is known that telnet mode will hang
1962 in such a way that Emacs must be stopped (perhaps this
1963 is the same problem).
1965 If TIOCREMOTE is turned off, then there is a bug in
1966 hp-ux which sometimes loses data. Apparently the
1967 code which blocks the master process when the internal
1968 buffer fills up does not work. Other than this,
1969 though, everything else seems to work fine.
1971 Since the latter lossage is more benign, we may as well
1972 lose that way. -- cph */
1973 #ifdef FIONBIO
1974 #if defined(SYSV_PTYS) || defined(UNIX98_PTYS)
1976 int on = 1;
1977 ioctl (fd, FIONBIO, &on);
1979 #endif
1980 #endif
1981 #ifdef IBMRTAIX
1982 /* On AIX, the parent gets SIGHUP when a pty attached child dies. So, we */
1983 /* ignore SIGHUP once we've started a child on a pty. Note that this may */
1984 /* cause EMACS not to die when it should, i.e., when its own controlling */
1985 /* tty goes away. I've complained to the AIX developers, and they may */
1986 /* change this behavior, but I'm not going to hold my breath. */
1987 signal (SIGHUP, SIG_IGN);
1988 #endif
1990 #endif /* HAVE_PTYS */
1992 #ifdef VMS
1994 /* Assigning an input channel is done at the start of Emacs execution.
1995 This is called each time Emacs is resumed, also, but does nothing
1996 because input_chain is no longer zero. */
1998 void
1999 init_vms_input ()
2001 int status;
2003 if (input_fd == 0)
2005 status = SYS$ASSIGN (&input_dsc, &input_fd, 0, 0);
2006 if (! (status & 1))
2007 LIB$STOP (status);
2011 /* Deassigning the input channel is done before exiting. */
2013 void
2014 stop_vms_input ()
2016 return SYS$DASSGN (input_fd);
2019 short input_buffer;
2021 /* Request reading one character into the keyboard buffer.
2022 This is done as soon as the buffer becomes empty. */
2024 void
2025 queue_kbd_input ()
2027 int status;
2028 extern kbd_input_ast ();
2030 waiting_for_ast = 0;
2031 stop_input = 0;
2032 status = SYS$QIO (0, input_fd, IO$_READVBLK,
2033 &input_iosb, kbd_input_ast, 1,
2034 &input_buffer, 1, 0, terminator_mask, 0, 0);
2037 int input_count;
2039 /* Ast routine that is called when keyboard input comes in
2040 in accord with the SYS$QIO above. */
2042 void
2043 kbd_input_ast ()
2045 register int c = -1;
2046 int old_errno = errno;
2047 extern EMACS_TIME *input_available_clear_time;
2049 if (waiting_for_ast)
2050 SYS$SETEF (input_ef);
2051 waiting_for_ast = 0;
2052 input_count++;
2053 #ifdef ASTDEBUG
2054 if (input_count == 25)
2055 exit (1);
2056 printf ("Ast # %d,", input_count);
2057 printf (" iosb = %x, %x, %x, %x",
2058 input_iosb.offset, input_iosb.status, input_iosb.termlen,
2059 input_iosb.term);
2060 #endif
2061 if (input_iosb.offset)
2063 c = input_buffer;
2064 #ifdef ASTDEBUG
2065 printf (", char = 0%o", c);
2066 #endif
2068 #ifdef ASTDEBUG
2069 printf ("\n");
2070 fflush (stdout);
2071 sleep (1);
2072 #endif
2073 if (! stop_input)
2074 queue_kbd_input ();
2075 if (c >= 0)
2077 struct input_event e;
2078 EVENT_INIT (e);
2080 e.kind = ASCII_KEYSTROKE_EVENT;
2081 XSETINT (e.code, c);
2082 e.frame_or_window = selected_frame;
2083 kbd_buffer_store_event (&e);
2085 if (input_available_clear_time)
2086 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
2087 errno = old_errno;
2090 /* Wait until there is something in kbd_buffer. */
2092 void
2093 wait_for_kbd_input ()
2095 extern int have_process_input, process_exited;
2097 /* If already something, avoid doing system calls. */
2098 if (detect_input_pending ())
2100 return;
2102 /* Clear a flag, and tell ast routine above to set it. */
2103 SYS$CLREF (input_ef);
2104 waiting_for_ast = 1;
2105 /* Check for timing error: ast happened while we were doing that. */
2106 if (!detect_input_pending ())
2108 /* No timing error: wait for flag to be set. */
2109 set_waiting_for_input (0);
2110 SYS$WFLOR (input_ef, input_eflist);
2111 clear_waiting_for_input ();
2112 if (!detect_input_pending ())
2113 /* Check for subprocess input availability */
2115 int dsp = have_process_input || process_exited;
2117 SYS$CLREF (process_ef);
2118 if (have_process_input)
2119 process_command_input ();
2120 if (process_exited)
2121 process_exit ();
2122 if (dsp)
2124 update_mode_lines++;
2125 prepare_menu_bars ();
2126 redisplay_preserve_echo_area (18);
2130 waiting_for_ast = 0;
2133 /* Get rid of any pending QIO, when we are about to suspend
2134 or when we want to throw away pending input.
2135 We wait for a positive sign that the AST routine has run
2136 and therefore there is no I/O request queued when we return.
2137 SYS$SETAST is used to avoid a timing error. */
2139 void
2140 end_kbd_input ()
2142 #ifdef ASTDEBUG
2143 printf ("At end_kbd_input.\n");
2144 fflush (stdout);
2145 sleep (1);
2146 #endif
2147 if (LIB$AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
2149 SYS$CANCEL (input_fd);
2150 return;
2153 SYS$SETAST (0);
2154 /* Clear a flag, and tell ast routine above to set it. */
2155 SYS$CLREF (input_ef);
2156 waiting_for_ast = 1;
2157 stop_input = 1;
2158 SYS$CANCEL (input_fd);
2159 SYS$SETAST (1);
2160 SYS$WAITFR (input_ef);
2161 waiting_for_ast = 0;
2164 /* Wait for either input available or time interval expiry. */
2166 void
2167 input_wait_timeout (timeval)
2168 int timeval; /* Time to wait, in seconds */
2170 int time [2];
2171 static int zero = 0;
2172 static int large = -10000000;
2174 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
2176 /* If already something, avoid doing system calls. */
2177 if (detect_input_pending ())
2179 return;
2181 /* Clear a flag, and tell ast routine above to set it. */
2182 SYS$CLREF (input_ef);
2183 waiting_for_ast = 1;
2184 /* Check for timing error: ast happened while we were doing that. */
2185 if (!detect_input_pending ())
2187 /* No timing error: wait for flag to be set. */
2188 SYS$CANTIM (1, 0);
2189 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
2190 SYS$WFLOR (timer_ef, timer_eflist); /* Wait for timer expiry or input */
2192 waiting_for_ast = 0;
2195 /* The standard `sleep' routine works some other way
2196 and it stops working if you have ever quit out of it.
2197 This one continues to work. */
2199 sys_sleep (timeval)
2200 int timeval;
2202 int time [2];
2203 static int zero = 0;
2204 static int large = -10000000;
2206 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
2208 SYS$CANTIM (1, 0);
2209 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
2210 SYS$WAITFR (timer_ef); /* Wait for timer expiry only */
2213 void
2214 init_sigio (fd)
2215 int fd;
2217 request_sigio ();
2220 reset_sigio ()
2222 unrequest_sigio ();
2225 void
2226 request_sigio ()
2228 if (noninteractive)
2229 return;
2230 croak ("request sigio");
2233 void
2234 unrequest_sigio ()
2236 if (noninteractive)
2237 return;
2238 croak ("unrequest sigio");
2241 #endif /* VMS */
2243 /* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
2244 #ifndef CANNOT_DUMP
2245 #define NEED_STARTS
2246 #endif
2248 #ifndef SYSTEM_MALLOC
2249 #ifndef NEED_STARTS
2250 #define NEED_STARTS
2251 #endif
2252 #endif
2254 #ifdef NEED_STARTS
2255 /* Some systems that cannot dump also cannot implement these. */
2258 * Return the address of the start of the text segment prior to
2259 * doing an unexec. After unexec the return value is undefined.
2260 * See crt0.c for further explanation and _start.
2264 #if !(defined (__NetBSD__) && defined (__ELF__))
2265 #ifndef HAVE_TEXT_START
2266 char *
2267 start_of_text ()
2269 #ifdef TEXT_START
2270 return ((char *) TEXT_START);
2271 #else
2272 #ifdef GOULD
2273 extern csrt ();
2274 return ((char *) csrt);
2275 #else /* not GOULD */
2276 extern int _start ();
2277 return ((char *) _start);
2278 #endif /* GOULD */
2279 #endif /* TEXT_START */
2281 #endif /* not HAVE_TEXT_START */
2282 #endif
2285 * Return the address of the start of the data segment prior to
2286 * doing an unexec. After unexec the return value is undefined.
2287 * See crt0.c for further information and definition of data_start.
2289 * Apparently, on BSD systems this is etext at startup. On
2290 * USG systems (swapping) this is highly mmu dependent and
2291 * is also dependent on whether or not the program is running
2292 * with shared text. Generally there is a (possibly large)
2293 * gap between end of text and start of data with shared text.
2295 * On Uniplus+ systems with shared text, data starts at a
2296 * fixed address. Each port (from a given oem) is generally
2297 * different, and the specific value of the start of data can
2298 * be obtained via the UniPlus+ specific "uvar" system call,
2299 * however the method outlined in crt0.c seems to be more portable.
2301 * Probably what will have to happen when a USG unexec is available,
2302 * at least on UniPlus, is temacs will have to be made unshared so
2303 * that text and data are contiguous. Then once loadup is complete,
2304 * unexec will produce a shared executable where the data can be
2305 * at the normal shared text boundary and the startofdata variable
2306 * will be patched by unexec to the correct value.
2310 #ifndef start_of_data
2311 char *
2312 start_of_data ()
2314 #ifdef DATA_START
2315 return ((char *) DATA_START);
2316 #else
2317 #ifdef ORDINARY_LINK
2319 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
2320 * data_start isn't defined. We take the address of environ, which
2321 * is known to live at or near the start of the system crt0.c, and
2322 * we don't sweat the handful of bytes that might lose.
2324 extern char **environ;
2326 return ((char *) &environ);
2327 #else
2328 extern int data_start;
2329 return ((char *) &data_start);
2330 #endif /* ORDINARY_LINK */
2331 #endif /* DATA_START */
2333 #endif /* start_of_data */
2334 #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
2336 /* init_system_name sets up the string for the Lisp function
2337 system-name to return. */
2339 #ifdef BSD4_1
2340 #include <whoami.h>
2341 #endif
2343 extern Lisp_Object Vsystem_name;
2345 #ifndef BSD4_1
2346 #ifndef VMS
2347 #ifdef HAVE_SOCKETS
2348 #include <sys/socket.h>
2349 #include <netdb.h>
2350 #endif /* HAVE_SOCKETS */
2351 #endif /* not VMS */
2352 #endif /* not BSD4_1 */
2354 #ifdef TRY_AGAIN
2355 #ifndef HAVE_H_ERRNO
2356 extern int h_errno;
2357 #endif
2358 #endif /* TRY_AGAIN */
2360 void
2361 init_system_name ()
2363 #ifdef BSD4_1
2364 Vsystem_name = build_string (sysname);
2365 #else
2366 #ifdef VMS
2367 char *sp, *end;
2368 if ((sp = egetenv ("SYS$NODE")) == 0)
2369 Vsystem_name = build_string ("vax-vms");
2370 else if ((end = index (sp, ':')) == 0)
2371 Vsystem_name = build_string (sp);
2372 else
2373 Vsystem_name = make_string (sp, end - sp);
2374 #else
2375 #ifndef HAVE_GETHOSTNAME
2376 struct utsname uts;
2377 uname (&uts);
2378 Vsystem_name = build_string (uts.nodename);
2379 #else /* HAVE_GETHOSTNAME */
2380 unsigned int hostname_size = 256;
2381 char *hostname = (char *) alloca (hostname_size);
2383 /* Try to get the host name; if the buffer is too short, try
2384 again. Apparently, the only indication gethostname gives of
2385 whether the buffer was large enough is the presence or absence
2386 of a '\0' in the string. Eech. */
2387 for (;;)
2389 gethostname (hostname, hostname_size - 1);
2390 hostname[hostname_size - 1] = '\0';
2392 /* Was the buffer large enough for the '\0'? */
2393 if (strlen (hostname) < hostname_size - 1)
2394 break;
2396 hostname_size <<= 1;
2397 hostname = (char *) alloca (hostname_size);
2399 #ifdef HAVE_SOCKETS
2400 /* Turn the hostname into the official, fully-qualified hostname.
2401 Don't do this if we're going to dump; this can confuse system
2402 libraries on some machines and make the dumped emacs core dump. */
2403 #ifndef CANNOT_DUMP
2404 if (initialized)
2405 #endif /* not CANNOT_DUMP */
2406 if (! index (hostname, '.'))
2408 struct hostent *hp;
2409 int count;
2410 for (count = 0;; count++)
2412 #ifdef TRY_AGAIN
2413 h_errno = 0;
2414 #endif
2415 hp = gethostbyname (hostname);
2416 #ifdef TRY_AGAIN
2417 if (! (hp == 0 && h_errno == TRY_AGAIN))
2418 #endif
2419 break;
2420 if (count >= 5)
2421 break;
2422 Fsleep_for (make_number (1), Qnil);
2424 if (hp)
2426 char *fqdn = (char *) hp->h_name;
2427 #if 0
2428 char *p;
2429 #endif
2431 if (!index (fqdn, '.'))
2433 /* We still don't have a fully qualified domain name.
2434 Try to find one in the list of alternate names */
2435 char **alias = hp->h_aliases;
2436 while (*alias && !index (*alias, '.'))
2437 alias++;
2438 if (*alias)
2439 fqdn = *alias;
2441 hostname = fqdn;
2442 #if 0
2443 /* Convert the host name to lower case. */
2444 /* Using ctype.h here would introduce a possible locale
2445 dependence that is probably wrong for hostnames. */
2446 p = hostname;
2447 while (*p)
2449 if (*p >= 'A' && *p <= 'Z')
2450 *p += 'a' - 'A';
2451 p++;
2453 #endif
2456 #endif /* HAVE_SOCKETS */
2457 /* We used to try using getdomainname here,
2458 but NIIBE Yutaka <gniibe@etl.go.jp> says that
2459 getdomainname gets the NIS/YP domain which often is not the same
2460 as in Internet domain name. */
2461 #if 0 /* Turned off because sysinfo is not really likely to return the
2462 correct Internet domain. */
2463 #if (HAVE_SYSINFO && defined (SI_SRPC_DOMAIN))
2464 if (! index (hostname, '.'))
2466 /* The hostname is not fully qualified. Append the domain name. */
2468 int hostlen = strlen (hostname);
2469 int domain_size = 256;
2471 for (;;)
2473 char *domain = (char *) alloca (domain_size + 1);
2474 char *fqdn = (char *) alloca (hostlen + 1 + domain_size + 1);
2475 int sys_domain_size = sysinfo (SI_SRPC_DOMAIN, domain, domain_size);
2476 if (sys_domain_size <= 0)
2477 break;
2478 if (domain_size < sys_domain_size)
2480 domain_size = sys_domain_size;
2481 continue;
2483 strcpy (fqdn, hostname);
2484 if (domain[0] == '.')
2485 strcpy (fqdn + hostlen, domain);
2486 else if (domain[0] != 0)
2488 fqdn[hostlen] = '.';
2489 strcpy (fqdn + hostlen + 1, domain);
2491 hostname = fqdn;
2492 break;
2495 #endif /* HAVE_SYSINFO && defined (SI_SRPC_DOMAIN) */
2496 #endif /* 0 */
2497 Vsystem_name = build_string (hostname);
2498 #endif /* HAVE_GETHOSTNAME */
2499 #endif /* VMS */
2500 #endif /* BSD4_1 */
2502 unsigned char *p;
2503 for (p = SDATA (Vsystem_name); *p; p++)
2504 if (*p == ' ' || *p == '\t')
2505 *p = '-';
2509 #ifndef MSDOS
2510 #ifndef VMS
2511 #if !defined (HAVE_SELECT) || defined (BROKEN_SELECT_NON_X)
2513 #include "sysselect.h"
2514 #undef select
2516 #if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
2517 /* Cause explanatory error message at compile time,
2518 since the select emulation is not good enough for X. */
2519 int *x = &x_windows_lose_if_no_select_system_call;
2520 #endif
2522 /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2523 * Only checks read descriptors.
2525 /* How long to wait between checking fds in select */
2526 #define SELECT_PAUSE 1
2527 int select_alarmed;
2529 /* For longjmp'ing back to read_input_waiting. */
2531 jmp_buf read_alarm_throw;
2533 /* Nonzero if the alarm signal should throw back to read_input_waiting.
2534 The read_socket_hook function sets this to 1 while it is waiting. */
2536 int read_alarm_should_throw;
2538 SIGTYPE
2539 select_alarm ()
2541 select_alarmed = 1;
2542 #ifdef BSD4_1
2543 sigrelse (SIGALRM);
2544 #else /* not BSD4_1 */
2545 signal (SIGALRM, SIG_IGN);
2546 #endif /* not BSD4_1 */
2547 SIGNAL_THREAD_CHECK (SIGALRM);
2548 if (read_alarm_should_throw)
2549 longjmp (read_alarm_throw, 1);
2552 #ifndef WINDOWSNT
2553 /* Only rfds are checked. */
2555 sys_select (nfds, rfds, wfds, efds, timeout)
2556 int nfds;
2557 SELECT_TYPE *rfds, *wfds, *efds;
2558 EMACS_TIME *timeout;
2560 int ravail = 0;
2561 SELECT_TYPE orfds;
2562 int timeoutval;
2563 int *local_timeout;
2564 extern int proc_buffered_char[];
2565 #ifndef subprocesses
2566 int process_tick = 0, update_tick = 0;
2567 #else
2568 extern int process_tick, update_tick;
2569 #endif
2570 unsigned char buf;
2572 #if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
2573 /* If we're using X, then the native select will work; we only need the
2574 emulation for non-X usage. */
2575 if (!NILP (Vwindow_system))
2576 return select (nfds, rfds, wfds, efds, timeout);
2577 #endif
2578 timeoutval = timeout ? EMACS_SECS (*timeout) : 100000;
2579 local_timeout = &timeoutval;
2580 FD_ZERO (&orfds);
2581 if (rfds)
2583 orfds = *rfds;
2584 FD_ZERO (rfds);
2586 if (wfds)
2587 FD_ZERO (wfds);
2588 if (efds)
2589 FD_ZERO (efds);
2591 /* If we are looking only for the terminal, with no timeout,
2592 just read it and wait -- that's more efficient. */
2593 if (*local_timeout == 100000 && process_tick == update_tick
2594 && FD_ISSET (0, &orfds))
2596 int fd;
2597 for (fd = 1; fd < nfds; ++fd)
2598 if (FD_ISSET (fd, &orfds))
2599 goto hardway;
2600 if (! detect_input_pending ())
2601 read_input_waiting ();
2602 FD_SET (0, rfds);
2603 return 1;
2606 hardway:
2607 /* Once a second, till the timer expires, check all the flagged read
2608 * descriptors to see if any input is available. If there is some then
2609 * set the corresponding bit in the return copy of rfds.
2611 while (1)
2613 register int to_check, fd;
2615 if (rfds)
2617 for (to_check = nfds, fd = 0; --to_check >= 0; fd++)
2619 if (FD_ISSET (fd, &orfds))
2621 int avail = 0, status = 0;
2623 if (fd == 0)
2624 avail = detect_input_pending (); /* Special keyboard handler */
2625 else
2627 #ifdef FIONREAD
2628 status = ioctl (fd, FIONREAD, &avail);
2629 #else /* no FIONREAD */
2630 /* Hoping it will return -1 if nothing available
2631 or 0 if all 0 chars requested are read. */
2632 if (proc_buffered_char[fd] >= 0)
2633 avail = 1;
2634 else
2636 avail = read (fd, &buf, 1);
2637 if (avail > 0)
2638 proc_buffered_char[fd] = buf;
2640 #endif /* no FIONREAD */
2642 if (status >= 0 && avail > 0)
2644 FD_SET (fd, rfds);
2645 ravail++;
2650 if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
2651 break;
2653 turn_on_atimers (0);
2654 signal (SIGALRM, select_alarm);
2655 select_alarmed = 0;
2656 alarm (SELECT_PAUSE);
2658 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2659 while (select_alarmed == 0 && *local_timeout != 0
2660 && process_tick == update_tick)
2662 /* If we are interested in terminal input,
2663 wait by reading the terminal.
2664 That makes instant wakeup for terminal input at least. */
2665 if (FD_ISSET (0, &orfds))
2667 read_input_waiting ();
2668 if (detect_input_pending ())
2669 select_alarmed = 1;
2671 else
2672 pause ();
2674 (*local_timeout) -= SELECT_PAUSE;
2676 /* Reset the old alarm if there was one. */
2677 turn_on_atimers (1);
2679 if (*local_timeout == 0) /* Stop on timer being cleared */
2680 break;
2682 return ravail;
2684 #endif /* not WINDOWSNT */
2686 /* Read keyboard input into the standard buffer,
2687 waiting for at least one character. */
2689 void
2690 read_input_waiting ()
2692 int nread, i;
2693 extern int quit_char;
2695 if (read_socket_hook)
2697 struct input_event hold_quit;
2699 EVENT_INIT (hold_quit);
2700 hold_quit.kind = NO_EVENT;
2702 read_alarm_should_throw = 0;
2703 if (! setjmp (read_alarm_throw))
2704 nread = (*read_socket_hook) (0, 1, &hold_quit);
2705 else
2706 nread = -1;
2708 if (hold_quit.kind != NO_EVENT)
2709 kbd_buffer_store_event (&hold_quit);
2711 else
2713 struct input_event e;
2714 char buf[3];
2715 nread = read (fileno (stdin), buf, 1);
2716 EVENT_INIT (e);
2718 /* Scan the chars for C-g and store them in kbd_buffer. */
2719 e.kind = ASCII_KEYSTROKE_EVENT;
2720 e.frame_or_window = selected_frame;
2721 e.modifiers = 0;
2722 for (i = 0; i < nread; i++)
2724 /* Convert chars > 0177 to meta events if desired.
2725 We do this under the same conditions that read_avail_input does. */
2726 if (read_socket_hook == 0)
2728 /* If the user says she has a meta key, then believe her. */
2729 if (meta_key == 1 && (buf[i] & 0x80))
2730 e.modifiers = meta_modifier;
2731 if (meta_key != 2)
2732 buf[i] &= ~0x80;
2735 XSETINT (e.code, buf[i]);
2736 kbd_buffer_store_event (&e);
2737 /* Don't look at input that follows a C-g too closely.
2738 This reduces lossage due to autorepeat on C-g. */
2739 if (buf[i] == quit_char)
2740 break;
2745 #endif /* not HAVE_SELECT */
2746 #endif /* not VMS */
2747 #endif /* not MSDOS */
2749 #ifdef BSD4_1
2750 void
2751 init_sigio (fd)
2752 int fd;
2754 if (noninteractive)
2755 return;
2756 lmode = LINTRUP | lmode;
2757 ioctl (fd, TIOCLSET, &lmode);
2760 void
2761 reset_sigio ()
2763 if (noninteractive)
2764 return;
2765 lmode = ~LINTRUP & lmode;
2766 ioctl (0, TIOCLSET, &lmode);
2769 void
2770 request_sigio ()
2772 if (noninteractive)
2773 return;
2774 sigrelse (SIGTINT);
2776 interrupts_deferred = 0;
2779 void
2780 unrequest_sigio ()
2782 if (noninteractive)
2783 return;
2784 sighold (SIGTINT);
2786 interrupts_deferred = 1;
2789 /* still inside #ifdef BSD4_1 */
2790 #ifdef subprocesses
2792 int sigheld; /* Mask of held signals */
2794 void
2795 sigholdx (signum)
2796 int signum;
2798 sigheld |= sigbit (signum);
2799 sighold (signum);
2802 void
2803 sigisheld (signum)
2804 int signum;
2806 sigheld |= sigbit (signum);
2809 void
2810 sigunhold (signum)
2811 int signum;
2813 sigheld &= ~sigbit (signum);
2814 sigrelse (signum);
2817 void
2818 sigfree () /* Free all held signals */
2820 int i;
2821 for (i = 0; i < NSIG; i++)
2822 if (sigheld & sigbit (i))
2823 sigrelse (i);
2824 sigheld = 0;
2828 sigbit (i)
2830 return 1 << (i - 1);
2832 #endif /* subprocesses */
2833 #endif /* BSD4_1 */
2835 /* POSIX signals support - DJB */
2836 /* Anyone with POSIX signals should have ANSI C declarations */
2838 #ifdef POSIX_SIGNALS
2840 sigset_t empty_mask, full_mask;
2842 signal_handler_t
2843 sys_signal (int signal_number, signal_handler_t action)
2845 struct sigaction new_action, old_action;
2846 sigemptyset (&new_action.sa_mask);
2847 new_action.sa_handler = action;
2848 #if defined (SA_RESTART) && ! defined (BROKEN_SA_RESTART) && !defined(SYNC_INPUT)
2849 /* Emacs mostly works better with restartable system services. If this
2850 flag exists, we probably want to turn it on here.
2851 However, on some systems this resets the timeout of `select'
2852 which means that `select' never finishes if it keeps getting signals.
2853 BROKEN_SA_RESTART is defined on those systems. */
2854 /* It's not clear why the comment above says "mostly works better". --Stef
2855 When SYNC_INPUT is set, we don't want SA_RESTART because we need to poll
2856 for pending input so we need long-running syscalls to be interrupted
2857 after a signal that sets the interrupt_input_pending flag. */
2858 new_action.sa_flags = SA_RESTART;
2859 #else
2860 new_action.sa_flags = 0;
2861 #endif
2862 sigaction (signal_number, &new_action, &old_action);
2863 return (old_action.sa_handler);
2866 #ifndef __GNUC__
2867 /* If we're compiling with GCC, we don't need this function, since it
2868 can be written as a macro. */
2869 sigset_t
2870 sys_sigmask (int sig)
2872 sigset_t mask;
2873 sigemptyset (&mask);
2874 sigaddset (&mask, sig);
2875 return mask;
2877 #endif
2879 /* I'd like to have these guys return pointers to the mask storage in here,
2880 but there'd be trouble if the code was saving multiple masks. I'll be
2881 safe and pass the structure. It normally won't be more than 2 bytes
2882 anyhow. - DJB */
2884 sigset_t
2885 sys_sigblock (sigset_t new_mask)
2887 sigset_t old_mask;
2888 sigprocmask (SIG_BLOCK, &new_mask, &old_mask);
2889 return (old_mask);
2892 sigset_t
2893 sys_sigunblock (sigset_t new_mask)
2895 sigset_t old_mask;
2896 sigprocmask (SIG_UNBLOCK, &new_mask, &old_mask);
2897 return (old_mask);
2900 sigset_t
2901 sys_sigsetmask (sigset_t new_mask)
2903 sigset_t old_mask;
2904 sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
2905 return (old_mask);
2908 #endif /* POSIX_SIGNALS */
2910 #if !defined HAVE_STRSIGNAL && !HAVE_DECL_SYS_SIGLIST
2911 static char *my_sys_siglist[NSIG];
2912 # ifdef sys_siglist
2913 # undef sys_siglist
2914 # endif
2915 # define sys_siglist my_sys_siglist
2916 #endif
2918 void
2919 init_signals ()
2921 #ifdef POSIX_SIGNALS
2922 sigemptyset (&empty_mask);
2923 sigfillset (&full_mask);
2924 #endif
2926 #if !defined HAVE_STRSIGNAL && !HAVE_DECL_SYS_SIGLIST
2927 if (! initialized)
2929 # ifdef SIGABRT
2930 sys_siglist[SIGABRT] = "Aborted";
2931 # endif
2932 # ifdef SIGAIO
2933 sys_siglist[SIGAIO] = "LAN I/O interrupt";
2934 # endif
2935 # ifdef SIGALRM
2936 sys_siglist[SIGALRM] = "Alarm clock";
2937 # endif
2938 # ifdef SIGBUS
2939 sys_siglist[SIGBUS] = "Bus error";
2940 # endif
2941 # ifdef SIGCLD
2942 sys_siglist[SIGCLD] = "Child status changed";
2943 # endif
2944 # ifdef SIGCHLD
2945 sys_siglist[SIGCHLD] = "Child status changed";
2946 # endif
2947 # ifdef SIGCONT
2948 sys_siglist[SIGCONT] = "Continued";
2949 # endif
2950 # ifdef SIGDANGER
2951 sys_siglist[SIGDANGER] = "Swap space dangerously low";
2952 # endif
2953 # ifdef SIGDGNOTIFY
2954 sys_siglist[SIGDGNOTIFY] = "Notification message in queue";
2955 # endif
2956 # ifdef SIGEMT
2957 sys_siglist[SIGEMT] = "Emulation trap";
2958 # endif
2959 # ifdef SIGFPE
2960 sys_siglist[SIGFPE] = "Arithmetic exception";
2961 # endif
2962 # ifdef SIGFREEZE
2963 sys_siglist[SIGFREEZE] = "SIGFREEZE";
2964 # endif
2965 # ifdef SIGGRANT
2966 sys_siglist[SIGGRANT] = "Monitor mode granted";
2967 # endif
2968 # ifdef SIGHUP
2969 sys_siglist[SIGHUP] = "Hangup";
2970 # endif
2971 # ifdef SIGILL
2972 sys_siglist[SIGILL] = "Illegal instruction";
2973 # endif
2974 # ifdef SIGINT
2975 sys_siglist[SIGINT] = "Interrupt";
2976 # endif
2977 # ifdef SIGIO
2978 sys_siglist[SIGIO] = "I/O possible";
2979 # endif
2980 # ifdef SIGIOINT
2981 sys_siglist[SIGIOINT] = "I/O intervention required";
2982 # endif
2983 # ifdef SIGIOT
2984 sys_siglist[SIGIOT] = "IOT trap";
2985 # endif
2986 # ifdef SIGKILL
2987 sys_siglist[SIGKILL] = "Killed";
2988 # endif
2989 # ifdef SIGLOST
2990 sys_siglist[SIGLOST] = "Resource lost";
2991 # endif
2992 # ifdef SIGLWP
2993 sys_siglist[SIGLWP] = "SIGLWP";
2994 # endif
2995 # ifdef SIGMSG
2996 sys_siglist[SIGMSG] = "Monitor mode data available";
2997 # endif
2998 # ifdef SIGPHONE
2999 sys_siglist[SIGWIND] = "SIGPHONE";
3000 # endif
3001 # ifdef SIGPIPE
3002 sys_siglist[SIGPIPE] = "Broken pipe";
3003 # endif
3004 # ifdef SIGPOLL
3005 sys_siglist[SIGPOLL] = "Pollable event occurred";
3006 # endif
3007 # ifdef SIGPROF
3008 sys_siglist[SIGPROF] = "Profiling timer expired";
3009 # endif
3010 # ifdef SIGPTY
3011 sys_siglist[SIGPTY] = "PTY I/O interrupt";
3012 # endif
3013 # ifdef SIGPWR
3014 sys_siglist[SIGPWR] = "Power-fail restart";
3015 # endif
3016 # ifdef SIGQUIT
3017 sys_siglist[SIGQUIT] = "Quit";
3018 # endif
3019 # ifdef SIGRETRACT
3020 sys_siglist[SIGRETRACT] = "Need to relinguish monitor mode";
3021 # endif
3022 # ifdef SIGSAK
3023 sys_siglist[SIGSAK] = "Secure attention";
3024 # endif
3025 # ifdef SIGSEGV
3026 sys_siglist[SIGSEGV] = "Segmentation violation";
3027 # endif
3028 # ifdef SIGSOUND
3029 sys_siglist[SIGSOUND] = "Sound completed";
3030 # endif
3031 # ifdef SIGSTOP
3032 sys_siglist[SIGSTOP] = "Stopped (signal)";
3033 # endif
3034 # ifdef SIGSTP
3035 sys_siglist[SIGSTP] = "Stopped (user)";
3036 # endif
3037 # ifdef SIGSYS
3038 sys_siglist[SIGSYS] = "Bad argument to system call";
3039 # endif
3040 # ifdef SIGTERM
3041 sys_siglist[SIGTERM] = "Terminated";
3042 # endif
3043 # ifdef SIGTHAW
3044 sys_siglist[SIGTHAW] = "SIGTHAW";
3045 # endif
3046 # ifdef SIGTRAP
3047 sys_siglist[SIGTRAP] = "Trace/breakpoint trap";
3048 # endif
3049 # ifdef SIGTSTP
3050 sys_siglist[SIGTSTP] = "Stopped (user)";
3051 # endif
3052 # ifdef SIGTTIN
3053 sys_siglist[SIGTTIN] = "Stopped (tty input)";
3054 # endif
3055 # ifdef SIGTTOU
3056 sys_siglist[SIGTTOU] = "Stopped (tty output)";
3057 # endif
3058 # ifdef SIGURG
3059 sys_siglist[SIGURG] = "Urgent I/O condition";
3060 # endif
3061 # ifdef SIGUSR1
3062 sys_siglist[SIGUSR1] = "User defined signal 1";
3063 # endif
3064 # ifdef SIGUSR2
3065 sys_siglist[SIGUSR2] = "User defined signal 2";
3066 # endif
3067 # ifdef SIGVTALRM
3068 sys_siglist[SIGVTALRM] = "Virtual timer expired";
3069 # endif
3070 # ifdef SIGWAITING
3071 sys_siglist[SIGWAITING] = "Process's LWPs are blocked";
3072 # endif
3073 # ifdef SIGWINCH
3074 sys_siglist[SIGWINCH] = "Window size changed";
3075 # endif
3076 # ifdef SIGWIND
3077 sys_siglist[SIGWIND] = "SIGWIND";
3078 # endif
3079 # ifdef SIGXCPU
3080 sys_siglist[SIGXCPU] = "CPU time limit exceeded";
3081 # endif
3082 # ifdef SIGXFSZ
3083 sys_siglist[SIGXFSZ] = "File size limit exceeded";
3084 # endif
3086 #endif /* !defined HAVE_STRSIGNAL && !defined HAVE_DECL_SYS_SIGLIST */
3089 #ifndef HAVE_RANDOM
3090 #ifdef random
3091 #define HAVE_RANDOM
3092 #endif
3093 #endif
3095 /* Figure out how many bits the system's random number generator uses.
3096 `random' and `lrand48' are assumed to return 31 usable bits.
3097 BSD `rand' returns a 31 bit value but the low order bits are unusable;
3098 so we'll shift it and treat it like the 15-bit USG `rand'. */
3100 #ifndef RAND_BITS
3101 # ifdef HAVE_RANDOM
3102 # define RAND_BITS 31
3103 # else /* !HAVE_RANDOM */
3104 # ifdef HAVE_LRAND48
3105 # define RAND_BITS 31
3106 # define random lrand48
3107 # else /* !HAVE_LRAND48 */
3108 # define RAND_BITS 15
3109 # if RAND_MAX == 32767
3110 # define random rand
3111 # else /* RAND_MAX != 32767 */
3112 # if RAND_MAX == 2147483647
3113 # define random() (rand () >> 16)
3114 # else /* RAND_MAX != 2147483647 */
3115 # ifdef USG
3116 # define random rand
3117 # else
3118 # define random() (rand () >> 16)
3119 # endif /* !USG */
3120 # endif /* RAND_MAX != 2147483647 */
3121 # endif /* RAND_MAX != 32767 */
3122 # endif /* !HAVE_LRAND48 */
3123 # endif /* !HAVE_RANDOM */
3124 #endif /* !RAND_BITS */
3126 void
3127 seed_random (arg)
3128 long arg;
3130 #ifdef HAVE_RANDOM
3131 srandom ((unsigned int)arg);
3132 #else
3133 # ifdef HAVE_LRAND48
3134 srand48 (arg);
3135 # else
3136 srand ((unsigned int)arg);
3137 # endif
3138 #endif
3142 * Build a full Emacs-sized word out of whatever we've got.
3143 * This suffices even for a 64-bit architecture with a 15-bit rand.
3145 long
3146 get_random ()
3148 long val = random ();
3149 #if VALBITS > RAND_BITS
3150 val = (val << RAND_BITS) ^ random ();
3151 #if VALBITS > 2*RAND_BITS
3152 val = (val << RAND_BITS) ^ random ();
3153 #if VALBITS > 3*RAND_BITS
3154 val = (val << RAND_BITS) ^ random ();
3155 #if VALBITS > 4*RAND_BITS
3156 val = (val << RAND_BITS) ^ random ();
3157 #endif /* need at least 5 */
3158 #endif /* need at least 4 */
3159 #endif /* need at least 3 */
3160 #endif /* need at least 2 */
3161 return val & ((1L << VALBITS) - 1);
3164 #ifdef WRONG_NAME_INSQUE
3166 insque (q,p)
3167 caddr_t q,p;
3169 _insque (q,p);
3172 #endif
3174 #ifdef VMS
3176 #ifdef getenv
3177 /* If any place else asks for the TERM variable,
3178 allow it to be overridden with the EMACS_TERM variable
3179 before attempting to translate the logical name TERM. As a last
3180 resort, ask for VAX C's special idea of the TERM variable. */
3181 #undef getenv
3182 char *
3183 sys_getenv (name)
3184 char *name;
3186 register char *val;
3187 static char buf[256];
3188 static struct dsc$descriptor_s equiv
3189 = {sizeof (buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf};
3190 static struct dsc$descriptor_s d_name
3191 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
3192 short eqlen;
3194 if (!strcmp (name, "TERM"))
3196 val = (char *) getenv ("EMACS_TERM");
3197 if (val)
3198 return val;
3201 d_name.dsc$w_length = strlen (name);
3202 d_name.dsc$a_pointer = name;
3203 if (LIB$SYS_TRNLOG (&d_name, &eqlen, &equiv) == 1)
3205 char *str = (char *) xmalloc (eqlen + 1);
3206 bcopy (buf, str, eqlen);
3207 str[eqlen] = '\0';
3208 /* This is a storage leak, but a pain to fix. With luck,
3209 no one will ever notice. */
3210 return str;
3212 return (char *) getenv (name);
3214 #endif /* getenv */
3216 #ifdef abort
3217 /* Since VMS doesn't believe in core dumps, the only way to debug this beast is
3218 to force a call on the debugger from within the image. */
3219 #undef abort
3220 sys_abort ()
3222 reset_sys_modes ();
3223 LIB$SIGNAL (SS$_DEBUG);
3225 #endif /* abort */
3226 #endif /* VMS */
3228 #ifdef VMS
3229 #ifdef LINK_CRTL_SHARE
3230 #ifdef SHARABLE_LIB_BUG
3231 /* Variables declared noshare and initialized in sharable libraries
3232 cannot be shared. The VMS linker incorrectly forces you to use a private
3233 version which is uninitialized... If not for this "feature", we
3234 could use the C library definition of sys_nerr and sys_errlist. */
3235 int sys_nerr = 35;
3236 char *sys_errlist[] =
3238 "error 0",
3239 "not owner",
3240 "no such file or directory",
3241 "no such process",
3242 "interrupted system call",
3243 "i/o error",
3244 "no such device or address",
3245 "argument list too long",
3246 "exec format error",
3247 "bad file number",
3248 "no child process",
3249 "no more processes",
3250 "not enough memory",
3251 "permission denied",
3252 "bad address",
3253 "block device required",
3254 "mount devices busy",
3255 "file exists",
3256 "cross-device link",
3257 "no such device",
3258 "not a directory",
3259 "is a directory",
3260 "invalid argument",
3261 "file table overflow",
3262 "too many open files",
3263 "not a typewriter",
3264 "text file busy",
3265 "file too big",
3266 "no space left on device",
3267 "illegal seek",
3268 "read-only file system",
3269 "too many links",
3270 "broken pipe",
3271 "math argument",
3272 "result too large",
3273 "I/O stream empty",
3274 "vax/vms specific error code nontranslatable error"
3276 #endif /* SHARABLE_LIB_BUG */
3277 #endif /* LINK_CRTL_SHARE */
3278 #endif /* VMS */
3280 #ifndef HAVE_STRERROR
3281 #ifndef WINDOWSNT
3282 char *
3283 strerror (errnum)
3284 int errnum;
3286 extern char *sys_errlist[];
3287 extern int sys_nerr;
3289 if (errnum >= 0 && errnum < sys_nerr)
3290 return sys_errlist[errnum];
3291 return (char *) "Unknown error";
3293 #endif /* not WINDOWSNT */
3294 #endif /* ! HAVE_STRERROR */
3297 emacs_open (path, oflag, mode)
3298 const char *path;
3299 int oflag, mode;
3301 register int rtnval;
3303 #ifdef BSD4_1
3304 if (oflag & O_CREAT)
3305 return creat (path, mode);
3306 #endif
3308 while ((rtnval = open (path, oflag, mode)) == -1
3309 && (errno == EINTR))
3310 QUIT;
3311 return (rtnval);
3315 emacs_close (fd)
3316 int fd;
3318 int did_retry = 0;
3319 register int rtnval;
3321 while ((rtnval = close (fd)) == -1
3322 && (errno == EINTR))
3323 did_retry = 1;
3325 /* If close is interrupted SunOS 4.1 may or may not have closed the
3326 file descriptor. If it did the second close will fail with
3327 errno = EBADF. That means we have succeeded. */
3328 if (rtnval == -1 && did_retry && errno == EBADF)
3329 return 0;
3331 return rtnval;
3335 emacs_read (fildes, buf, nbyte)
3336 int fildes;
3337 char *buf;
3338 unsigned int nbyte;
3340 register int rtnval;
3342 while ((rtnval = read (fildes, buf, nbyte)) == -1
3343 && (errno == EINTR))
3344 QUIT;
3345 return (rtnval);
3349 emacs_write (fildes, buf, nbyte)
3350 int fildes;
3351 const char *buf;
3352 unsigned int nbyte;
3354 register int rtnval, bytes_written;
3356 bytes_written = 0;
3358 while (nbyte > 0)
3360 rtnval = write (fildes, buf, nbyte);
3362 if (rtnval == -1)
3364 if (errno == EINTR)
3366 #ifdef SYNC_INPUT
3367 /* I originally used `QUIT' but that might causes files to
3368 be truncated if you hit C-g in the middle of it. --Stef */
3369 if (interrupt_input_pending)
3370 handle_async_input ();
3371 #endif
3372 continue;
3374 else
3375 return (bytes_written ? bytes_written : -1);
3378 buf += rtnval;
3379 nbyte -= rtnval;
3380 bytes_written += rtnval;
3382 return (bytes_written);
3385 #ifdef USG
3387 * All of the following are for USG.
3389 * On USG systems the system calls are INTERRUPTIBLE by signals
3390 * that the user program has elected to catch. Thus the system call
3391 * must be retried in these cases. To handle this without massive
3392 * changes in the source code, we remap the standard system call names
3393 * to names for our own functions in sysdep.c that do the system call
3394 * with retries. Actually, for portability reasons, it is good
3395 * programming practice, as this example shows, to limit all actual
3396 * system calls to a single occurrence in the source. Sure, this
3397 * adds an extra level of function call overhead but it is almost
3398 * always negligible. Fred Fish, Unisoft Systems Inc.
3402 * Warning, this function may not duplicate 4.2 action properly
3403 * under error conditions.
3406 #ifndef MAXPATHLEN
3407 /* In 4.1, param.h fails to define this. */
3408 #define MAXPATHLEN 1024
3409 #endif
3411 #ifndef HAVE_GETWD
3413 char *
3414 getwd (pathname)
3415 char *pathname;
3417 char *npath, *spath;
3418 extern char *getcwd ();
3420 BLOCK_INPUT; /* getcwd uses malloc */
3421 spath = npath = getcwd ((char *) 0, MAXPATHLEN);
3422 if (spath == 0)
3424 UNBLOCK_INPUT;
3425 return spath;
3427 /* On Altos 3068, getcwd can return @hostname/dir, so discard
3428 up to first slash. Should be harmless on other systems. */
3429 while (*npath && *npath != '/')
3430 npath++;
3431 strcpy (pathname, npath);
3432 free (spath); /* getcwd uses malloc */
3433 UNBLOCK_INPUT;
3434 return pathname;
3437 #endif /* HAVE_GETWD */
3440 * Emulate rename using unlink/link. Note that this is
3441 * only partially correct. Also, doesn't enforce restriction
3442 * that files be of same type (regular->regular, dir->dir, etc).
3445 #ifndef HAVE_RENAME
3447 rename (from, to)
3448 const char *from;
3449 const char *to;
3451 if (access (from, 0) == 0)
3453 unlink (to);
3454 if (link (from, to) == 0)
3455 if (unlink (from) == 0)
3456 return (0);
3458 return (-1);
3461 #endif
3464 #ifdef HPUX
3465 #ifndef HAVE_PERROR
3467 /* HPUX curses library references perror, but as far as we know
3468 it won't be called. Anyway this definition will do for now. */
3470 perror ()
3474 #endif /* not HAVE_PERROR */
3475 #endif /* HPUX */
3477 #ifndef HAVE_DUP2
3480 * Emulate BSD dup2. First close newd if it already exists.
3481 * Then, attempt to dup oldd. If not successful, call dup2 recursively
3482 * until we are, then close the unsuccessful ones.
3485 dup2 (oldd, newd)
3486 int oldd;
3487 int newd;
3489 register int fd, ret;
3491 emacs_close (newd);
3493 #ifdef F_DUPFD
3494 return fcntl (oldd, F_DUPFD, newd);
3495 #else
3496 fd = dup (old);
3497 if (fd == -1)
3498 return -1;
3499 if (fd == new)
3500 return new;
3501 ret = dup2 (old,new);
3502 emacs_close (fd);
3503 return ret;
3504 #endif
3507 #endif /* not HAVE_DUP2 */
3510 * Gettimeofday. Simulate as much as possible. Only accurate
3511 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3512 * Only needed when subprocesses are defined.
3515 #ifdef subprocesses
3516 #ifndef VMS
3517 #ifndef HAVE_GETTIMEOFDAY
3518 #ifdef HAVE_TIMEVAL
3520 /* ARGSUSED */
3522 gettimeofday (tp, tzp)
3523 struct timeval *tp;
3524 struct timezone *tzp;
3526 extern long time ();
3528 tp->tv_sec = time ((long *)0);
3529 tp->tv_usec = 0;
3530 if (tzp != 0)
3531 tzp->tz_minuteswest = -1;
3532 return 0;
3535 #endif
3536 #endif
3537 #endif
3538 #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
3541 * This function will go away as soon as all the stubs fixed. (fnf)
3544 void
3545 croak (badfunc)
3546 char *badfunc;
3548 printf ("%s not yet implemented\r\n", badfunc);
3549 reset_sys_modes ();
3550 exit (1);
3553 #endif /* USG */
3555 /* Directory routines for systems that don't have them. */
3557 #ifdef SYSV_SYSTEM_DIR
3559 #include <dirent.h>
3561 #if defined (BROKEN_CLOSEDIR) || !defined (HAVE_CLOSEDIR)
3564 closedir (dirp)
3565 register DIR *dirp; /* stream from opendir */
3567 int rtnval;
3569 rtnval = emacs_close (dirp->dd_fd);
3571 /* Some systems (like Solaris) allocate the buffer and the DIR all
3572 in one block. Why in the world are we freeing this ourselves
3573 anyway? */
3574 #if ! (defined (sun) && defined (USG5_4))
3575 xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
3576 #endif
3577 xfree ((char *) dirp);
3579 return rtnval;
3581 #endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
3582 #endif /* SYSV_SYSTEM_DIR */
3584 #ifdef NONSYSTEM_DIR_LIBRARY
3586 DIR *
3587 opendir (filename)
3588 char *filename; /* name of directory */
3590 register DIR *dirp; /* -> malloc'ed storage */
3591 register int fd; /* file descriptor for read */
3592 struct stat sbuf; /* result of fstat */
3594 fd = emacs_open (filename, O_RDONLY, 0);
3595 if (fd < 0)
3596 return 0;
3598 BLOCK_INPUT;
3599 if (fstat (fd, &sbuf) < 0
3600 || (sbuf.st_mode & S_IFMT) != S_IFDIR
3601 || (dirp = (DIR *) xmalloc (sizeof (DIR))) == 0)
3603 emacs_close (fd);
3604 UNBLOCK_INPUT;
3605 return 0; /* bad luck today */
3607 UNBLOCK_INPUT;
3609 dirp->dd_fd = fd;
3610 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
3612 return dirp;
3615 void
3616 closedir (dirp)
3617 register DIR *dirp; /* stream from opendir */
3619 emacs_close (dirp->dd_fd);
3620 xfree ((char *) dirp);
3624 #ifndef VMS
3625 #define DIRSIZ 14
3626 struct olddir
3628 ino_t od_ino; /* inode */
3629 char od_name[DIRSIZ]; /* filename */
3631 #endif /* not VMS */
3633 struct direct dir_static; /* simulated directory contents */
3635 /* ARGUSED */
3636 struct direct *
3637 readdir (dirp)
3638 register DIR *dirp; /* stream from opendir */
3640 #ifndef VMS
3641 register struct olddir *dp; /* -> directory data */
3642 #else /* VMS */
3643 register struct dir$_name *dp; /* -> directory data */
3644 register struct dir$_version *dv; /* -> version data */
3645 #endif /* VMS */
3647 for (; ;)
3649 if (dirp->dd_loc >= dirp->dd_size)
3650 dirp->dd_loc = dirp->dd_size = 0;
3652 if (dirp->dd_size == 0 /* refill buffer */
3653 && (dirp->dd_size = emacs_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3654 return 0;
3656 #ifndef VMS
3657 dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
3658 dirp->dd_loc += sizeof (struct olddir);
3660 if (dp->od_ino != 0) /* not deleted entry */
3662 dir_static.d_ino = dp->od_ino;
3663 strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
3664 dir_static.d_name[DIRSIZ] = '\0';
3665 dir_static.d_namlen = strlen (dir_static.d_name);
3666 dir_static.d_reclen = sizeof (struct direct)
3667 - MAXNAMLEN + 3
3668 + dir_static.d_namlen - dir_static.d_namlen % 4;
3669 return &dir_static; /* -> simulated structure */
3671 #else /* VMS */
3672 dp = (struct dir$_name *) dirp->dd_buf;
3673 if (dirp->dd_loc == 0)
3674 dirp->dd_loc = (dp->dir$b_namecount&1) ? dp->dir$b_namecount + 1
3675 : dp->dir$b_namecount;
3676 dv = (struct dir$_version *)&dp->dir$t_name[dirp->dd_loc];
3677 dir_static.d_ino = dv->dir$w_fid_num;
3678 dir_static.d_namlen = dp->dir$b_namecount;
3679 dir_static.d_reclen = sizeof (struct direct)
3680 - MAXNAMLEN + 3
3681 + dir_static.d_namlen - dir_static.d_namlen % 4;
3682 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3683 dir_static.d_name[dir_static.d_namlen] = '\0';
3684 dirp->dd_loc = dirp->dd_size; /* only one record at a time */
3685 return &dir_static;
3686 #endif /* VMS */
3690 #ifdef VMS
3691 /* readdirver is just like readdir except it returns all versions of a file
3692 as separate entries. */
3694 /* ARGUSED */
3695 struct direct *
3696 readdirver (dirp)
3697 register DIR *dirp; /* stream from opendir */
3699 register struct dir$_name *dp; /* -> directory data */
3700 register struct dir$_version *dv; /* -> version data */
3702 if (dirp->dd_loc >= dirp->dd_size - sizeof (struct dir$_name))
3703 dirp->dd_loc = dirp->dd_size = 0;
3705 if (dirp->dd_size == 0 /* refill buffer */
3706 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3707 return 0;
3709 dp = (struct dir$_name *) dirp->dd_buf;
3710 if (dirp->dd_loc == 0)
3711 dirp->dd_loc = (dp->dir$b_namecount & 1) ? dp->dir$b_namecount + 1
3712 : dp->dir$b_namecount;
3713 dv = (struct dir$_version *) &dp->dir$t_name[dirp->dd_loc];
3714 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3715 sprintf (&dir_static.d_name[dp->dir$b_namecount], ";%d", dv->dir$w_version);
3716 dir_static.d_namlen = strlen (dir_static.d_name);
3717 dir_static.d_ino = dv->dir$w_fid_num;
3718 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3
3719 + dir_static.d_namlen - dir_static.d_namlen % 4;
3720 dirp->dd_loc = ((char *) (++dv) - dp->dir$t_name);
3721 return &dir_static;
3724 #endif /* VMS */
3726 #endif /* NONSYSTEM_DIR_LIBRARY */
3730 set_file_times (filename, atime, mtime)
3731 const char *filename;
3732 EMACS_TIME atime, mtime;
3734 #ifdef HAVE_UTIMES
3735 struct timeval tv[2];
3736 tv[0] = atime;
3737 tv[1] = mtime;
3738 return utimes (filename, tv);
3739 #else /* not HAVE_UTIMES */
3740 struct utimbuf utb;
3741 utb.actime = EMACS_SECS (atime);
3742 utb.modtime = EMACS_SECS (mtime);
3743 return utime (filename, &utb);
3744 #endif /* not HAVE_UTIMES */
3747 /* mkdir and rmdir functions, for systems which don't have them. */
3749 #ifndef HAVE_MKDIR
3751 * Written by Robert Rother, Mariah Corporation, August 1985.
3753 * If you want it, it's yours. All I ask in return is that if you
3754 * figure out how to do this in a Bourne Shell script you send me
3755 * a copy.
3756 * sdcsvax!rmr or rmr@uscd
3758 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
3759 * subroutine. 11Mar86; hoptoad!gnu
3761 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3762 * subroutine didn't return EEXIST. It does now.
3766 * Make a directory.
3768 #ifdef MKDIR_PROTOTYPE
3769 MKDIR_PROTOTYPE
3770 #else
3772 mkdir (dpath, dmode)
3773 char *dpath;
3774 int dmode;
3775 #endif
3777 int cpid, status, fd;
3778 struct stat statbuf;
3780 if (stat (dpath, &statbuf) == 0)
3782 errno = EEXIST; /* Stat worked, so it already exists */
3783 return -1;
3786 /* If stat fails for a reason other than non-existence, return error */
3787 if (errno != ENOENT)
3788 return -1;
3790 synch_process_alive = 1;
3791 switch (cpid = fork ())
3794 case -1: /* Error in fork */
3795 return (-1); /* Errno is set already */
3797 case 0: /* Child process */
3799 * Cheap hack to set mode of new directory. Since this
3800 * child process is going away anyway, we zap its umask.
3801 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3802 * directory. Does anybody care?
3804 status = umask (0); /* Get current umask */
3805 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */
3806 fd = emacs_open ("/dev/null", O_RDWR, 0);
3807 if (fd >= 0)
3809 dup2 (fd, 0);
3810 dup2 (fd, 1);
3811 dup2 (fd, 2);
3813 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
3814 _exit (-1); /* Can't exec /bin/mkdir */
3816 default: /* Parent process */
3817 wait_for_termination (cpid);
3820 if (synch_process_death != 0 || synch_process_retcode != 0
3821 || synch_process_termsig != 0)
3823 errno = EIO; /* We don't know why, but */
3824 return -1; /* /bin/mkdir failed */
3827 return 0;
3829 #endif /* not HAVE_MKDIR */
3831 #ifndef HAVE_RMDIR
3833 rmdir (dpath)
3834 char *dpath;
3836 int cpid, status, fd;
3837 struct stat statbuf;
3839 if (stat (dpath, &statbuf) != 0)
3841 /* Stat just set errno. We don't have to */
3842 return -1;
3845 synch_process_alive = 1;
3846 switch (cpid = fork ())
3849 case -1: /* Error in fork */
3850 return (-1); /* Errno is set already */
3852 case 0: /* Child process */
3853 fd = emacs_open ("/dev/null", O_RDWR, 0);
3854 if (fd >= 0)
3856 dup2 (fd, 0);
3857 dup2 (fd, 1);
3858 dup2 (fd, 2);
3860 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
3861 _exit (-1); /* Can't exec /bin/rmdir */
3863 default: /* Parent process */
3864 wait_for_termination (cpid);
3867 if (synch_process_death != 0 || synch_process_retcode != 0
3868 || synch_process_termsig != 0)
3870 errno = EIO; /* We don't know why, but */
3871 return -1; /* /bin/rmdir failed */
3874 return 0;
3876 #endif /* !HAVE_RMDIR */
3880 /* Functions for VMS */
3881 #ifdef VMS
3882 #include <acldef.h>
3883 #include <chpdef.h>
3884 #include <jpidef.h>
3886 /* Return as a string the VMS error string pertaining to STATUS.
3887 Reuses the same static buffer each time it is called. */
3889 char *
3890 vmserrstr (status)
3891 int status; /* VMS status code */
3893 int bufadr[2];
3894 short len;
3895 static char buf[257];
3897 bufadr[0] = sizeof buf - 1;
3898 bufadr[1] = (int) buf;
3899 if (! (SYS$GETMSG (status, &len, bufadr, 0x1, 0) & 1))
3900 return "untranslatable VMS error status";
3901 buf[len] = '\0';
3902 return buf;
3905 #ifdef access
3906 #undef access
3908 /* The following is necessary because 'access' emulation by VMS C (2.0) does
3909 * not work correctly. (It also doesn't work well in version 2.3.)
3912 #ifdef VMS4_4
3914 #define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3915 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3917 typedef union {
3918 struct {
3919 unsigned short s_buflen;
3920 unsigned short s_code;
3921 char *s_bufadr;
3922 unsigned short *s_retlenadr;
3923 } s;
3924 int end;
3925 } item;
3926 #define buflen s.s_buflen
3927 #define code s.s_code
3928 #define bufadr s.s_bufadr
3929 #define retlenadr s.s_retlenadr
3931 #define R_OK 4 /* test for read permission */
3932 #define W_OK 2 /* test for write permission */
3933 #define X_OK 1 /* test for execute (search) permission */
3934 #define F_OK 0 /* test for presence of file */
3937 sys_access (path, mode)
3938 char *path;
3939 int mode;
3941 static char *user = NULL;
3942 char dir_fn[512];
3944 /* translate possible directory spec into .DIR file name, so brain-dead
3945 * access can treat the directory like a file. */
3946 if (directory_file_name (path, dir_fn))
3947 path = dir_fn;
3949 if (mode == F_OK)
3950 return access (path, mode);
3951 if (user == NULL && (user = (char *) getenv ("USER")) == NULL)
3952 return -1;
3954 int stat;
3955 int flags;
3956 int acces;
3957 unsigned short int dummy;
3958 item itemlst[3];
3959 static int constant = ACL$C_FILE;
3960 DESCRIPTOR (path_desc, path);
3961 DESCRIPTOR (user_desc, user);
3963 flags = 0;
3964 acces = 0;
3965 if ((mode & X_OK) && ((stat = access (path, mode)) < 0 || mode == X_OK))
3966 return stat;
3967 if (mode & R_OK)
3968 acces |= CHP$M_READ;
3969 if (mode & W_OK)
3970 acces |= CHP$M_WRITE;
3971 itemlst[0].buflen = sizeof (int);
3972 itemlst[0].code = CHP$_FLAGS;
3973 itemlst[0].bufadr = (char *) &flags;
3974 itemlst[0].retlenadr = &dummy;
3975 itemlst[1].buflen = sizeof (int);
3976 itemlst[1].code = CHP$_ACCESS;
3977 itemlst[1].bufadr = (char *) &acces;
3978 itemlst[1].retlenadr = &dummy;
3979 itemlst[2].end = CHP$_END;
3980 stat = SYS$CHECK_ACCESS (&constant, &path_desc, &user_desc, itemlst);
3981 return stat == SS$_NORMAL ? 0 : -1;
3985 #else /* not VMS4_4 */
3987 #include <prvdef.h>
3988 #define ACE$M_WRITE 2
3989 #define ACE$C_KEYID 1
3991 static unsigned short memid, grpid;
3992 static unsigned int uic;
3994 /* Called from init_sys_modes, so it happens not very often
3995 but at least each time Emacs is loaded. */
3996 void
3997 sys_access_reinit ()
3999 uic = 0;
4003 sys_access (filename, type)
4004 char * filename;
4005 int type;
4007 struct FAB fab;
4008 struct XABPRO xab;
4009 int status, size, i, typecode, acl_controlled;
4010 unsigned int *aclptr, *aclend, aclbuf[60];
4011 union prvdef prvmask;
4013 /* Get UIC and GRP values for protection checking. */
4014 if (uic == 0)
4016 status = LIB$GETJPI (&JPI$_UIC, 0, 0, &uic, 0, 0);
4017 if (! (status & 1))
4018 return -1;
4019 memid = uic & 0xFFFF;
4020 grpid = uic >> 16;
4023 if (type != 2) /* not checking write access */
4024 return access (filename, type);
4026 /* Check write protection. */
4028 #define CHECKPRIV(bit) (prvmask.bit)
4029 #define WRITABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
4031 /* Find privilege bits */
4032 status = SYS$SETPRV (0, 0, 0, prvmask);
4033 if (! (status & 1))
4034 error ("Unable to find privileges: %s", vmserrstr (status));
4035 if (CHECKPRIV (PRV$V_BYPASS))
4036 return 0; /* BYPASS enabled */
4037 fab = cc$rms_fab;
4038 fab.fab$b_fac = FAB$M_GET;
4039 fab.fab$l_fna = filename;
4040 fab.fab$b_fns = strlen (filename);
4041 fab.fab$l_xab = &xab;
4042 xab = cc$rms_xabpro;
4043 xab.xab$l_aclbuf = aclbuf;
4044 xab.xab$w_aclsiz = sizeof (aclbuf);
4045 status = SYS$OPEN (&fab, 0, 0);
4046 if (! (status & 1))
4047 return -1;
4048 SYS$CLOSE (&fab, 0, 0);
4049 /* Check system access */
4050 if (CHECKPRIV (PRV$V_SYSPRV) && WRITABLE (XAB$V_SYS))
4051 return 0;
4052 /* Check ACL entries, if any */
4053 acl_controlled = 0;
4054 if (xab.xab$w_acllen > 0)
4056 aclptr = aclbuf;
4057 aclend = &aclbuf[xab.xab$w_acllen / 4];
4058 while (*aclptr && aclptr < aclend)
4060 size = (*aclptr & 0xff) / 4;
4061 typecode = (*aclptr >> 8) & 0xff;
4062 if (typecode == ACE$C_KEYID)
4063 for (i = size - 1; i > 1; i--)
4064 if (aclptr[i] == uic)
4066 acl_controlled = 1;
4067 if (aclptr[1] & ACE$M_WRITE)
4068 return 0; /* Write access through ACL */
4070 aclptr = &aclptr[size];
4072 if (acl_controlled) /* ACL specified, prohibits write access */
4073 return -1;
4075 /* No ACL entries specified, check normal protection */
4076 if (WRITABLE (XAB$V_WLD)) /* World writable */
4077 return 0;
4078 if (WRITABLE (XAB$V_GRP) &&
4079 (unsigned short) (xab.xab$l_uic >> 16) == grpid)
4080 return 0; /* Group writable */
4081 if (WRITABLE (XAB$V_OWN) &&
4082 (xab.xab$l_uic & 0xFFFF) == memid)
4083 return 0; /* Owner writable */
4085 return -1; /* Not writable */
4087 #endif /* not VMS4_4 */
4088 #endif /* access */
4090 static char vtbuf[NAM$C_MAXRSS+1];
4092 /* translate a vms file spec to a unix path */
4093 char *
4094 sys_translate_vms (vfile)
4095 char * vfile;
4097 char * p;
4098 char * targ;
4100 if (!vfile)
4101 return 0;
4103 targ = vtbuf;
4105 /* leading device or logical name is a root directory */
4106 if (p = strchr (vfile, ':'))
4108 *targ++ = '/';
4109 while (vfile < p)
4110 *targ++ = *vfile++;
4111 vfile++;
4112 *targ++ = '/';
4114 p = vfile;
4115 if (*p == '[' || *p == '<')
4117 while (*++vfile != *p + 2)
4118 switch (*vfile)
4120 case '.':
4121 if (vfile[-1] == *p)
4122 *targ++ = '.';
4123 *targ++ = '/';
4124 break;
4126 case '-':
4127 *targ++ = '.';
4128 *targ++ = '.';
4129 break;
4131 default:
4132 *targ++ = *vfile;
4133 break;
4135 vfile++;
4136 *targ++ = '/';
4138 while (*vfile)
4139 *targ++ = *vfile++;
4141 return vtbuf;
4144 static char utbuf[NAM$C_MAXRSS+1];
4146 /* translate a unix path to a VMS file spec */
4147 char *
4148 sys_translate_unix (ufile)
4149 char * ufile;
4151 int slash_seen = 0;
4152 char *p;
4153 char * targ;
4155 if (!ufile)
4156 return 0;
4158 targ = utbuf;
4160 if (*ufile == '/')
4162 ufile++;
4165 while (*ufile)
4167 switch (*ufile)
4169 case '/':
4170 if (slash_seen)
4171 if (index (&ufile[1], '/'))
4172 *targ++ = '.';
4173 else
4174 *targ++ = ']';
4175 else
4177 *targ++ = ':';
4178 if (index (&ufile[1], '/'))
4179 *targ++ = '[';
4180 slash_seen = 1;
4182 break;
4184 case '.':
4185 if (strncmp (ufile, "./", 2) == 0)
4187 if (!slash_seen)
4189 *targ++ = '[';
4190 slash_seen = 1;
4192 ufile++; /* skip the dot */
4193 if (index (&ufile[1], '/'))
4194 *targ++ = '.';
4195 else
4196 *targ++ = ']';
4198 else if (strncmp (ufile, "../", 3) == 0)
4200 if (!slash_seen)
4202 *targ++ = '[';
4203 slash_seen = 1;
4205 *targ++ = '-';
4206 ufile += 2; /* skip the dots */
4207 if (index (&ufile[1], '/'))
4208 *targ++ = '.';
4209 else
4210 *targ++ = ']';
4212 else
4213 *targ++ = *ufile;
4214 break;
4216 default:
4217 *targ++ = *ufile;
4218 break;
4220 ufile++;
4222 *targ = '\0';
4224 return utbuf;
4227 char *
4228 getwd (pathname)
4229 char *pathname;
4231 char *ptr, *val;
4232 extern char *getcwd ();
4234 #define MAXPATHLEN 1024
4236 ptr = xmalloc (MAXPATHLEN);
4237 val = getcwd (ptr, MAXPATHLEN);
4238 if (val == 0)
4240 xfree (ptr);
4241 return val;
4243 strcpy (pathname, ptr);
4244 xfree (ptr);
4246 return pathname;
4250 getppid ()
4252 long item_code = JPI$_OWNER;
4253 unsigned long parent_id;
4254 int status;
4256 if (((status = LIB$GETJPI (&item_code, 0, 0, &parent_id)) & 1) == 0)
4258 errno = EVMSERR;
4259 vaxc$errno = status;
4260 return -1;
4262 return parent_id;
4265 #undef getuid
4266 unsigned
4267 sys_getuid ()
4269 return (getgid () << 16) | getuid ();
4272 #undef read
4274 sys_read (fildes, buf, nbyte)
4275 int fildes;
4276 char *buf;
4277 unsigned int nbyte;
4279 return read (fildes, buf, (nbyte < MAXIOSIZE ? nbyte : MAXIOSIZE));
4282 #if 0
4284 sys_write (fildes, buf, nbyte)
4285 int fildes;
4286 char *buf;
4287 unsigned int nbyte;
4289 register int nwrote, rtnval = 0;
4291 while (nbyte > MAXIOSIZE && (nwrote = write (fildes, buf, MAXIOSIZE)) > 0) {
4292 nbyte -= nwrote;
4293 buf += nwrote;
4294 rtnval += nwrote;
4296 if (nwrote < 0)
4297 return rtnval ? rtnval : -1;
4298 if ((nwrote = write (fildes, buf, nbyte)) < 0)
4299 return rtnval ? rtnval : -1;
4300 return (rtnval + nwrote);
4302 #endif /* 0 */
4305 * VAX/VMS VAX C RTL really loses. It insists that records
4306 * end with a newline (carriage return) character, and if they
4307 * don't it adds one (nice of it isn't it!)
4309 * Thus we do this stupidity below.
4312 #undef write
4314 sys_write (fildes, buf, nbytes)
4315 int fildes;
4316 char *buf;
4317 unsigned int nbytes;
4319 register char *p;
4320 register char *e;
4321 int sum = 0;
4322 struct stat st;
4324 fstat (fildes, &st);
4325 p = buf;
4326 while (nbytes > 0)
4328 int len, retval;
4330 /* Handle fixed-length files with carriage control. */
4331 if (st.st_fab_rfm == FAB$C_FIX
4332 && ((st.st_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
4334 len = st.st_fab_mrs;
4335 retval = write (fildes, p, min (len, nbytes));
4336 if (retval != len)
4337 return -1;
4338 retval++; /* This skips the implied carriage control */
4340 else
4342 e = p + min (MAXIOSIZE, nbytes) - 1;
4343 while (*e != '\n' && e > p) e--;
4344 if (p == e) /* Ok.. so here we add a newline... sigh. */
4345 e = p + min (MAXIOSIZE, nbytes) - 1;
4346 len = e + 1 - p;
4347 retval = write (fildes, p, len);
4348 if (retval != len)
4349 return -1;
4351 p += retval;
4352 sum += retval;
4353 nbytes -= retval;
4355 return sum;
4358 /* Create file NEW copying its attributes from file OLD. If
4359 OLD is 0 or does not exist, create based on the value of
4360 vms_stmlf_recfm. */
4362 /* Protection value the file should ultimately have.
4363 Set by create_copy_attrs, and use by rename_sansversions. */
4364 static unsigned short int fab_final_pro;
4367 creat_copy_attrs (old, new)
4368 char *old, *new;
4370 struct FAB fab = cc$rms_fab;
4371 struct XABPRO xabpro;
4372 char aclbuf[256]; /* Choice of size is arbitrary. See below. */
4373 extern int vms_stmlf_recfm;
4375 if (old)
4377 fab.fab$b_fac = FAB$M_GET;
4378 fab.fab$l_fna = old;
4379 fab.fab$b_fns = strlen (old);
4380 fab.fab$l_xab = (char *) &xabpro;
4381 xabpro = cc$rms_xabpro;
4382 xabpro.xab$l_aclbuf = aclbuf;
4383 xabpro.xab$w_aclsiz = sizeof aclbuf;
4384 /* Call $OPEN to fill in the fab & xabpro fields. */
4385 if (SYS$OPEN (&fab, 0, 0) & 1)
4387 SYS$CLOSE (&fab, 0, 0);
4388 fab.fab$l_alq = 0; /* zero the allocation quantity */
4389 if (xabpro.xab$w_acllen > 0)
4391 if (xabpro.xab$w_acllen > sizeof aclbuf)
4392 /* If the acl buffer was too short, redo open with longer one.
4393 Wouldn't need to do this if there were some system imposed
4394 limit on the size of an ACL, but I can't find any such. */
4396 xabpro.xab$l_aclbuf = (char *) alloca (xabpro.xab$w_acllen);
4397 xabpro.xab$w_aclsiz = xabpro.xab$w_acllen;
4398 if (SYS$OPEN (&fab, 0, 0) & 1)
4399 SYS$CLOSE (&fab, 0, 0);
4400 else
4401 old = 0;
4404 else
4405 xabpro.xab$l_aclbuf = 0;
4407 else
4408 old = 0;
4410 fab.fab$l_fna = new;
4411 fab.fab$b_fns = strlen (new);
4412 if (!old)
4414 fab.fab$l_xab = 0;
4415 fab.fab$b_rfm = vms_stmlf_recfm ? FAB$C_STMLF : FAB$C_VAR;
4416 fab.fab$b_rat = FAB$M_CR;
4419 /* Set the file protections such that we will be able to manipulate
4420 this file. Once we are done writing and renaming it, we will set
4421 the protections back. */
4422 if (old)
4423 fab_final_pro = xabpro.xab$w_pro;
4424 else
4425 SYS$SETDFPROT (0, &fab_final_pro);
4426 xabpro.xab$w_pro &= 0xff0f; /* set O:rewd for now. This is set back later. */
4428 /* Create the new file with either default attrs or attrs copied
4429 from old file. */
4430 if (!(SYS$CREATE (&fab, 0, 0) & 1))
4431 return -1;
4432 SYS$CLOSE (&fab, 0, 0);
4433 /* As this is a "replacement" for creat, return a file descriptor
4434 opened for writing. */
4435 return open (new, O_WRONLY);
4438 #ifdef creat
4439 #undef creat
4440 #include <varargs.h>
4441 #ifdef __GNUC__
4442 #ifndef va_count
4443 #define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
4444 #endif
4445 #endif
4448 sys_creat (va_alist)
4449 va_dcl
4451 va_list list_incrementer;
4452 char *name;
4453 int mode;
4454 int rfd; /* related file descriptor */
4455 int fd; /* Our new file descriptor */
4456 int count;
4457 struct stat st_buf;
4458 char rfm[12];
4459 char rat[15];
4460 char mrs[13];
4461 char fsz[13];
4462 extern int vms_stmlf_recfm;
4464 va_count (count);
4465 va_start (list_incrementer);
4466 name = va_arg (list_incrementer, char *);
4467 mode = va_arg (list_incrementer, int);
4468 if (count > 2)
4469 rfd = va_arg (list_incrementer, int);
4470 va_end (list_incrementer);
4471 if (count > 2)
4473 /* Use information from the related file descriptor to set record
4474 format of the newly created file. */
4475 fstat (rfd, &st_buf);
4476 switch (st_buf.st_fab_rfm)
4478 case FAB$C_FIX:
4479 strcpy (rfm, "rfm = fix");
4480 sprintf (mrs, "mrs = %d", st_buf.st_fab_mrs);
4481 strcpy (rat, "rat = ");
4482 if (st_buf.st_fab_rat & FAB$M_CR)
4483 strcat (rat, "cr");
4484 else if (st_buf.st_fab_rat & FAB$M_FTN)
4485 strcat (rat, "ftn");
4486 else if (st_buf.st_fab_rat & FAB$M_PRN)
4487 strcat (rat, "prn");
4488 if (st_buf.st_fab_rat & FAB$M_BLK)
4489 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4490 strcat (rat, ", blk");
4491 else
4492 strcat (rat, "blk");
4493 return creat (name, 0, rfm, rat, mrs);
4495 case FAB$C_VFC:
4496 strcpy (rfm, "rfm = vfc");
4497 sprintf (fsz, "fsz = %d", st_buf.st_fab_fsz);
4498 strcpy (rat, "rat = ");
4499 if (st_buf.st_fab_rat & FAB$M_CR)
4500 strcat (rat, "cr");
4501 else if (st_buf.st_fab_rat & FAB$M_FTN)
4502 strcat (rat, "ftn");
4503 else if (st_buf.st_fab_rat & FAB$M_PRN)
4504 strcat (rat, "prn");
4505 if (st_buf.st_fab_rat & FAB$M_BLK)
4506 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4507 strcat (rat, ", blk");
4508 else
4509 strcat (rat, "blk");
4510 return creat (name, 0, rfm, rat, fsz);
4512 case FAB$C_STM:
4513 strcpy (rfm, "rfm = stm");
4514 break;
4516 case FAB$C_STMCR:
4517 strcpy (rfm, "rfm = stmcr");
4518 break;
4520 case FAB$C_STMLF:
4521 strcpy (rfm, "rfm = stmlf");
4522 break;
4524 case FAB$C_UDF:
4525 strcpy (rfm, "rfm = udf");
4526 break;
4528 case FAB$C_VAR:
4529 strcpy (rfm, "rfm = var");
4530 break;
4532 strcpy (rat, "rat = ");
4533 if (st_buf.st_fab_rat & FAB$M_CR)
4534 strcat (rat, "cr");
4535 else if (st_buf.st_fab_rat & FAB$M_FTN)
4536 strcat (rat, "ftn");
4537 else if (st_buf.st_fab_rat & FAB$M_PRN)
4538 strcat (rat, "prn");
4539 if (st_buf.st_fab_rat & FAB$M_BLK)
4540 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4541 strcat (rat, ", blk");
4542 else
4543 strcat (rat, "blk");
4545 else
4547 strcpy (rfm, vms_stmlf_recfm ? "rfm = stmlf" : "rfm=var");
4548 strcpy (rat, "rat=cr");
4550 /* Until the VAX C RTL fixes the many bugs with modes, always use
4551 mode 0 to get the user's default protection. */
4552 fd = creat (name, 0, rfm, rat);
4553 if (fd < 0 && errno == EEXIST)
4555 if (unlink (name) < 0)
4556 report_file_error ("delete", build_string (name));
4557 fd = creat (name, 0, rfm, rat);
4559 return fd;
4561 #endif /* creat */
4563 /* fwrite to stdout is S L O W. Speed it up by using fputc...*/
4565 sys_fwrite (ptr, size, num, fp)
4566 register char * ptr;
4567 FILE * fp;
4569 register int tot = num * size;
4571 while (tot--)
4572 fputc (*ptr++, fp);
4573 return num;
4577 * The VMS C library routine creat actually creates a new version of an
4578 * existing file rather than truncating the old version. There are times
4579 * when this is not the desired behavior, for instance, when writing an
4580 * auto save file (you only want one version), or when you don't have
4581 * write permission in the directory containing the file (but the file
4582 * itself is writable). Hence this routine, which is equivalent to
4583 * "close (creat (fn, 0));" on Unix if fn already exists.
4586 vms_truncate (fn)
4587 char *fn;
4589 struct FAB xfab = cc$rms_fab;
4590 struct RAB xrab = cc$rms_rab;
4591 int status;
4593 xfab.fab$l_fop = FAB$M_TEF; /* free allocated but unused blocks on close */
4594 xfab.fab$b_fac = FAB$M_TRN | FAB$M_GET; /* allow truncate and get access */
4595 xfab.fab$b_shr = FAB$M_NIL; /* allow no sharing - file must be locked */
4596 xfab.fab$l_fna = fn;
4597 xfab.fab$b_fns = strlen (fn);
4598 xfab.fab$l_dna = ";0"; /* default to latest version of the file */
4599 xfab.fab$b_dns = 2;
4600 xrab.rab$l_fab = &xfab;
4602 /* This gibberish opens the file, positions to the first record, and
4603 deletes all records from there until the end of file. */
4604 if ((SYS$OPEN (&xfab) & 01) == 01)
4606 if ((SYS$CONNECT (&xrab) & 01) == 01 &&
4607 (SYS$FIND (&xrab) & 01) == 01 &&
4608 (SYS$TRUNCATE (&xrab) & 01) == 01)
4609 status = 0;
4610 else
4611 status = -1;
4613 else
4614 status = -1;
4615 SYS$CLOSE (&xfab);
4616 return status;
4619 /* Define this symbol to actually read SYSUAF.DAT. This requires either
4620 SYSPRV or a readable SYSUAF.DAT. */
4622 #ifdef READ_SYSUAF
4624 * getuaf.c
4626 * Routine to read the VMS User Authorization File and return
4627 * a specific user's record.
4630 static struct UAF retuaf;
4632 struct UAF *
4633 get_uaf_name (uname)
4634 char * uname;
4636 register status;
4637 struct FAB uaf_fab;
4638 struct RAB uaf_rab;
4640 uaf_fab = cc$rms_fab;
4641 uaf_rab = cc$rms_rab;
4642 /* initialize fab fields */
4643 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4644 uaf_fab.fab$b_fns = 21;
4645 uaf_fab.fab$b_fac = FAB$M_GET;
4646 uaf_fab.fab$b_org = FAB$C_IDX;
4647 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4648 /* initialize rab fields */
4649 uaf_rab.rab$l_fab = &uaf_fab;
4650 /* open the User Authorization File */
4651 status = SYS$OPEN (&uaf_fab);
4652 if (!(status&1))
4654 errno = EVMSERR;
4655 vaxc$errno = status;
4656 return 0;
4658 status = SYS$CONNECT (&uaf_rab);
4659 if (!(status&1))
4661 errno = EVMSERR;
4662 vaxc$errno = status;
4663 return 0;
4665 /* read the requested record - index is in uname */
4666 uaf_rab.rab$l_kbf = uname;
4667 uaf_rab.rab$b_ksz = strlen (uname);
4668 uaf_rab.rab$b_rac = RAB$C_KEY;
4669 uaf_rab.rab$l_ubf = (char *)&retuaf;
4670 uaf_rab.rab$w_usz = sizeof retuaf;
4671 status = SYS$GET (&uaf_rab);
4672 if (!(status&1))
4674 errno = EVMSERR;
4675 vaxc$errno = status;
4676 return 0;
4678 /* close the User Authorization File */
4679 status = SYS$DISCONNECT (&uaf_rab);
4680 if (!(status&1))
4682 errno = EVMSERR;
4683 vaxc$errno = status;
4684 return 0;
4686 status = SYS$CLOSE (&uaf_fab);
4687 if (!(status&1))
4689 errno = EVMSERR;
4690 vaxc$errno = status;
4691 return 0;
4693 return &retuaf;
4696 struct UAF *
4697 get_uaf_uic (uic)
4698 unsigned long uic;
4700 register status;
4701 struct FAB uaf_fab;
4702 struct RAB uaf_rab;
4704 uaf_fab = cc$rms_fab;
4705 uaf_rab = cc$rms_rab;
4706 /* initialize fab fields */
4707 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4708 uaf_fab.fab$b_fns = 21;
4709 uaf_fab.fab$b_fac = FAB$M_GET;
4710 uaf_fab.fab$b_org = FAB$C_IDX;
4711 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4712 /* initialize rab fields */
4713 uaf_rab.rab$l_fab = &uaf_fab;
4714 /* open the User Authorization File */
4715 status = SYS$OPEN (&uaf_fab);
4716 if (!(status&1))
4718 errno = EVMSERR;
4719 vaxc$errno = status;
4720 return 0;
4722 status = SYS$CONNECT (&uaf_rab);
4723 if (!(status&1))
4725 errno = EVMSERR;
4726 vaxc$errno = status;
4727 return 0;
4729 /* read the requested record - index is in uic */
4730 uaf_rab.rab$b_krf = 1; /* 1st alternate key */
4731 uaf_rab.rab$l_kbf = (char *) &uic;
4732 uaf_rab.rab$b_ksz = sizeof uic;
4733 uaf_rab.rab$b_rac = RAB$C_KEY;
4734 uaf_rab.rab$l_ubf = (char *)&retuaf;
4735 uaf_rab.rab$w_usz = sizeof retuaf;
4736 status = SYS$GET (&uaf_rab);
4737 if (!(status&1))
4739 errno = EVMSERR;
4740 vaxc$errno = status;
4741 return 0;
4743 /* close the User Authorization File */
4744 status = SYS$DISCONNECT (&uaf_rab);
4745 if (!(status&1))
4747 errno = EVMSERR;
4748 vaxc$errno = status;
4749 return 0;
4751 status = SYS$CLOSE (&uaf_fab);
4752 if (!(status&1))
4754 errno = EVMSERR;
4755 vaxc$errno = status;
4756 return 0;
4758 return &retuaf;
4761 static struct passwd retpw;
4763 struct passwd *
4764 cnv_uaf_pw (up)
4765 struct UAF * up;
4767 char * ptr;
4769 /* copy these out first because if the username is 32 chars, the next
4770 section will overwrite the first byte of the UIC */
4771 retpw.pw_uid = up->uaf$w_mem;
4772 retpw.pw_gid = up->uaf$w_grp;
4774 /* I suppose this is not the best style, to possibly overwrite one
4775 byte beyond the end of the field, but what the heck... */
4776 ptr = &up->uaf$t_username[UAF$S_USERNAME];
4777 while (ptr[-1] == ' ')
4778 ptr--;
4779 *ptr = '\0';
4780 strcpy (retpw.pw_name, up->uaf$t_username);
4782 /* the rest of these are counted ascii strings */
4783 strncpy (retpw.pw_gecos, &up->uaf$t_owner[1], up->uaf$t_owner[0]);
4784 retpw.pw_gecos[up->uaf$t_owner[0]] = '\0';
4785 strncpy (retpw.pw_dir, &up->uaf$t_defdev[1], up->uaf$t_defdev[0]);
4786 retpw.pw_dir[up->uaf$t_defdev[0]] = '\0';
4787 strncat (retpw.pw_dir, &up->uaf$t_defdir[1], up->uaf$t_defdir[0]);
4788 retpw.pw_dir[up->uaf$t_defdev[0] + up->uaf$t_defdir[0]] = '\0';
4789 strncpy (retpw.pw_shell, &up->uaf$t_defcli[1], up->uaf$t_defcli[0]);
4790 retpw.pw_shell[up->uaf$t_defcli[0]] = '\0';
4792 return &retpw;
4794 #else /* not READ_SYSUAF */
4795 static struct passwd retpw;
4796 #endif /* not READ_SYSUAF */
4798 struct passwd *
4799 getpwnam (name)
4800 char * name;
4802 #ifdef READ_SYSUAF
4803 struct UAF *up;
4804 #else
4805 char * user;
4806 char * dir;
4807 unsigned char * full;
4808 #endif /* READ_SYSUAF */
4809 char *ptr = name;
4811 while (*ptr)
4813 if ('a' <= *ptr && *ptr <= 'z')
4814 *ptr -= 040;
4815 ptr++;
4817 #ifdef READ_SYSUAF
4818 if (!(up = get_uaf_name (name)))
4819 return 0;
4820 return cnv_uaf_pw (up);
4821 #else
4822 if (strcmp (name, getenv ("USER")) == 0)
4824 retpw.pw_uid = getuid ();
4825 retpw.pw_gid = getgid ();
4826 strcpy (retpw.pw_name, name);
4827 if (full = egetenv ("FULLNAME"))
4828 strcpy (retpw.pw_gecos, full);
4829 else
4830 *retpw.pw_gecos = '\0';
4831 strcpy (retpw.pw_dir, egetenv ("HOME"));
4832 *retpw.pw_shell = '\0';
4833 return &retpw;
4835 else
4836 return 0;
4837 #endif /* not READ_SYSUAF */
4840 struct passwd *
4841 getpwuid (uid)
4842 unsigned long uid;
4844 #ifdef READ_SYSUAF
4845 struct UAF * up;
4847 if (!(up = get_uaf_uic (uid)))
4848 return 0;
4849 return cnv_uaf_pw (up);
4850 #else
4851 if (uid == sys_getuid ())
4852 return getpwnam (egetenv ("USER"));
4853 else
4854 return 0;
4855 #endif /* not READ_SYSUAF */
4858 /* return total address space available to the current process. This is
4859 the sum of the current p0 size, p1 size and free page table entries
4860 available. */
4862 vlimit ()
4864 int item_code;
4865 unsigned long free_pages;
4866 unsigned long frep0va;
4867 unsigned long frep1va;
4868 register status;
4870 item_code = JPI$_FREPTECNT;
4871 if (((status = LIB$GETJPI (&item_code, 0, 0, &free_pages)) & 1) == 0)
4873 errno = EVMSERR;
4874 vaxc$errno = status;
4875 return -1;
4877 free_pages *= 512;
4879 item_code = JPI$_FREP0VA;
4880 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep0va)) & 1) == 0)
4882 errno = EVMSERR;
4883 vaxc$errno = status;
4884 return -1;
4886 item_code = JPI$_FREP1VA;
4887 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep1va)) & 1) == 0)
4889 errno = EVMSERR;
4890 vaxc$errno = status;
4891 return -1;
4894 return free_pages + frep0va + (0x7fffffff - frep1va);
4898 define_logical_name (varname, string)
4899 char *varname;
4900 char *string;
4902 struct dsc$descriptor_s strdsc =
4903 {strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string};
4904 struct dsc$descriptor_s envdsc =
4905 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4906 struct dsc$descriptor_s lnmdsc =
4907 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4909 return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
4913 delete_logical_name (varname)
4914 char *varname;
4916 struct dsc$descriptor_s envdsc =
4917 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4918 struct dsc$descriptor_s lnmdsc =
4919 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4921 return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
4925 ulimit ()
4927 return 0;
4931 setpgrp ()
4933 return 0;
4937 execvp ()
4939 error ("execvp system call not implemented");
4940 return -1;
4944 rename (from, to)
4945 char *from, *to;
4947 int status;
4948 struct FAB from_fab = cc$rms_fab, to_fab = cc$rms_fab;
4949 struct NAM from_nam = cc$rms_nam, to_nam = cc$rms_nam;
4950 char from_esn[NAM$C_MAXRSS];
4951 char to_esn[NAM$C_MAXRSS];
4953 from_fab.fab$l_fna = from;
4954 from_fab.fab$b_fns = strlen (from);
4955 from_fab.fab$l_nam = &from_nam;
4956 from_fab.fab$l_fop = FAB$M_NAM;
4958 from_nam.nam$l_esa = from_esn;
4959 from_nam.nam$b_ess = sizeof from_esn;
4961 to_fab.fab$l_fna = to;
4962 to_fab.fab$b_fns = strlen (to);
4963 to_fab.fab$l_nam = &to_nam;
4964 to_fab.fab$l_fop = FAB$M_NAM;
4966 to_nam.nam$l_esa = to_esn;
4967 to_nam.nam$b_ess = sizeof to_esn;
4969 status = SYS$RENAME (&from_fab, 0, 0, &to_fab);
4971 if (status & 1)
4972 return 0;
4973 else
4975 if (status == RMS$_DEV)
4976 errno = EXDEV;
4977 else
4978 errno = EVMSERR;
4979 vaxc$errno = status;
4980 return -1;
4984 /* This function renames a file like `rename', but it strips
4985 the version number from the "to" filename, such that the "to" file is
4986 will always be a new version. It also sets the file protection once it is
4987 finished. The protection that we will use is stored in fab_final_pro,
4988 and was set when we did a creat_copy_attrs to create the file that we
4989 are renaming.
4991 We could use the chmod function, but Eunichs uses 3 bits per user category
4992 to describe the protection, and VMS uses 4 (write and delete are separate
4993 bits). To maintain portability, the VMS implementation of `chmod' wires
4994 the W and D bits together. */
4997 static struct fibdef fib; /* We need this initialized to zero */
4998 char vms_file_written[NAM$C_MAXRSS];
5001 rename_sans_version (from,to)
5002 char *from, *to;
5004 short int chan;
5005 int stat;
5006 short int iosb[4];
5007 int status;
5008 struct FAB to_fab = cc$rms_fab;
5009 struct NAM to_nam = cc$rms_nam;
5010 struct dsc$descriptor fib_d ={sizeof (fib),0,0,(char*) &fib};
5011 struct dsc$descriptor fib_attr[2]
5012 = {{sizeof (fab_final_pro),ATR$C_FPRO,0,(char*) &fab_final_pro},{0,0,0,0}};
5013 char to_esn[NAM$C_MAXRSS];
5015 $DESCRIPTOR (disk,to_esn);
5017 to_fab.fab$l_fna = to;
5018 to_fab.fab$b_fns = strlen (to);
5019 to_fab.fab$l_nam = &to_nam;
5020 to_fab.fab$l_fop = FAB$M_NAM;
5022 to_nam.nam$l_esa = to_esn;
5023 to_nam.nam$b_ess = sizeof to_esn;
5025 status = SYS$PARSE (&to_fab, 0, 0); /* figure out the full file name */
5027 if (to_nam.nam$l_fnb && NAM$M_EXP_VER)
5028 *(to_nam.nam$l_ver) = '\0';
5030 stat = rename (from, to_esn);
5031 if (stat < 0)
5032 return stat;
5034 strcpy (vms_file_written, to_esn);
5036 to_fab.fab$l_fna = vms_file_written; /* this points to the versionless name */
5037 to_fab.fab$b_fns = strlen (vms_file_written);
5039 /* Now set the file protection to the correct value */
5040 SYS$OPEN (&to_fab, 0, 0); /* This fills in the nam$w_fid fields */
5042 /* Copy these fields into the fib */
5043 fib.fib$r_fid_overlay.fib$w_fid[0] = to_nam.nam$w_fid[0];
5044 fib.fib$r_fid_overlay.fib$w_fid[1] = to_nam.nam$w_fid[1];
5045 fib.fib$r_fid_overlay.fib$w_fid[2] = to_nam.nam$w_fid[2];
5047 SYS$CLOSE (&to_fab, 0, 0);
5049 stat = SYS$ASSIGN (&disk, &chan, 0, 0); /* open a channel to the disk */
5050 if (!stat)
5051 LIB$SIGNAL (stat);
5052 stat = SYS$QIOW (0, chan, IO$_MODIFY, iosb, 0, 0, &fib_d,
5053 0, 0, 0, &fib_attr, 0);
5054 if (!stat)
5055 LIB$SIGNAL (stat);
5056 stat = SYS$DASSGN (chan);
5057 if (!stat)
5058 LIB$SIGNAL (stat);
5059 strcpy (vms_file_written, to_esn); /* We will write this to the terminal*/
5060 return 0;
5064 link (file, new)
5065 char * file, * new;
5067 register status;
5068 struct FAB fab;
5069 struct NAM nam;
5070 unsigned short fid[3];
5071 char esa[NAM$C_MAXRSS];
5073 fab = cc$rms_fab;
5074 fab.fab$l_fop = FAB$M_OFP;
5075 fab.fab$l_fna = file;
5076 fab.fab$b_fns = strlen (file);
5077 fab.fab$l_nam = &nam;
5079 nam = cc$rms_nam;
5080 nam.nam$l_esa = esa;
5081 nam.nam$b_ess = NAM$C_MAXRSS;
5083 status = SYS$PARSE (&fab);
5084 if ((status & 1) == 0)
5086 errno = EVMSERR;
5087 vaxc$errno = status;
5088 return -1;
5090 status = SYS$SEARCH (&fab);
5091 if ((status & 1) == 0)
5093 errno = EVMSERR;
5094 vaxc$errno = status;
5095 return -1;
5098 fid[0] = nam.nam$w_fid[0];
5099 fid[1] = nam.nam$w_fid[1];
5100 fid[2] = nam.nam$w_fid[2];
5102 fab.fab$l_fna = new;
5103 fab.fab$b_fns = strlen (new);
5105 status = SYS$PARSE (&fab);
5106 if ((status & 1) == 0)
5108 errno = EVMSERR;
5109 vaxc$errno = status;
5110 return -1;
5113 nam.nam$w_fid[0] = fid[0];
5114 nam.nam$w_fid[1] = fid[1];
5115 nam.nam$w_fid[2] = fid[2];
5117 nam.nam$l_esa = nam.nam$l_name;
5118 nam.nam$b_esl = nam.nam$b_name + nam.nam$b_type + nam.nam$b_ver;
5120 status = SYS$ENTER (&fab);
5121 if ((status & 1) == 0)
5123 errno = EVMSERR;
5124 vaxc$errno = status;
5125 return -1;
5128 return 0;
5131 void
5132 croak (badfunc)
5133 char *badfunc;
5135 printf ("%s not yet implemented\r\n", badfunc);
5136 reset_sys_modes ();
5137 exit (1);
5140 long
5141 random ()
5143 /* Arrange to return a range centered on zero. */
5144 return rand () - (1 << 30);
5147 void
5148 srandom (seed)
5150 srand (seed);
5152 #endif /* VMS */
5154 #ifdef AIXHFT
5156 /* Called from init_sys_modes. */
5157 void
5158 hft_init ()
5160 int junk;
5162 /* If we're not on an HFT we shouldn't do any of this. We determine
5163 if we are on an HFT by trying to get an HFT error code. If this
5164 call fails, we're not on an HFT. */
5165 #ifdef IBMR2AIX
5166 if (ioctl (0, HFQERROR, &junk) < 0)
5167 return;
5168 #else /* not IBMR2AIX */
5169 if (ioctl (0, HFQEIO, 0) < 0)
5170 return;
5171 #endif /* not IBMR2AIX */
5173 /* On AIX the default hft keyboard mapping uses backspace rather than delete
5174 as the rubout key's ASCII code. Here this is changed. The bug is that
5175 there's no way to determine the old mapping, so in reset_sys_modes
5176 we need to assume that the normal map had been present. Of course, this
5177 code also doesn't help if on a terminal emulator which doesn't understand
5178 HFT VTD's. */
5180 struct hfbuf buf;
5181 struct hfkeymap keymap;
5183 buf.hf_bufp = (char *)&keymap;
5184 buf.hf_buflen = sizeof (keymap);
5185 keymap.hf_nkeys = 2;
5186 keymap.hfkey[0].hf_kpos = 15;
5187 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
5188 #ifdef IBMR2AIX
5189 keymap.hfkey[0].hf_keyidh = '<';
5190 #else /* not IBMR2AIX */
5191 keymap.hfkey[0].hf_page = '<';
5192 #endif /* not IBMR2AIX */
5193 keymap.hfkey[0].hf_char = 127;
5194 keymap.hfkey[1].hf_kpos = 15;
5195 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
5196 #ifdef IBMR2AIX
5197 keymap.hfkey[1].hf_keyidh = '<';
5198 #else /* not IBMR2AIX */
5199 keymap.hfkey[1].hf_page = '<';
5200 #endif /* not IBMR2AIX */
5201 keymap.hfkey[1].hf_char = 127;
5202 hftctl (0, HFSKBD, &buf);
5204 /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
5205 at times. */
5206 line_ins_del_ok = char_ins_del_ok = 0;
5209 /* Reset the rubout key to backspace. */
5211 void
5212 hft_reset ()
5214 struct hfbuf buf;
5215 struct hfkeymap keymap;
5216 int junk;
5218 #ifdef IBMR2AIX
5219 if (ioctl (0, HFQERROR, &junk) < 0)
5220 return;
5221 #else /* not IBMR2AIX */
5222 if (ioctl (0, HFQEIO, 0) < 0)
5223 return;
5224 #endif /* not IBMR2AIX */
5226 buf.hf_bufp = (char *)&keymap;
5227 buf.hf_buflen = sizeof (keymap);
5228 keymap.hf_nkeys = 2;
5229 keymap.hfkey[0].hf_kpos = 15;
5230 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
5231 #ifdef IBMR2AIX
5232 keymap.hfkey[0].hf_keyidh = '<';
5233 #else /* not IBMR2AIX */
5234 keymap.hfkey[0].hf_page = '<';
5235 #endif /* not IBMR2AIX */
5236 keymap.hfkey[0].hf_char = 8;
5237 keymap.hfkey[1].hf_kpos = 15;
5238 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
5239 #ifdef IBMR2AIX
5240 keymap.hfkey[1].hf_keyidh = '<';
5241 #else /* not IBMR2AIX */
5242 keymap.hfkey[1].hf_page = '<';
5243 #endif /* not IBMR2AIX */
5244 keymap.hfkey[1].hf_char = 8;
5245 hftctl (0, HFSKBD, &buf);
5248 #endif /* AIXHFT */
5250 #ifdef USE_DL_STUBS
5252 /* These are included on Sunos 4.1 when we do not use shared libraries.
5253 X11 libraries may refer to these functions but (we hope) do not
5254 actually call them. */
5256 void *
5257 dlopen ()
5259 return 0;
5262 void *
5263 dlsym ()
5265 return 0;
5269 dlclose ()
5271 return -1;
5274 #endif /* USE_DL_STUBS */
5276 #ifndef BSTRING
5278 #ifndef bzero
5280 void
5281 bzero (b, length)
5282 register char *b;
5283 register int length;
5285 #ifdef VMS
5286 short zero = 0;
5287 long max_str = 65535;
5289 while (length > max_str) {
5290 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
5291 length -= max_str;
5292 b += max_str;
5294 max_str = length;
5295 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
5296 #else
5297 while (length-- > 0)
5298 *b++ = 0;
5299 #endif /* not VMS */
5302 #endif /* no bzero */
5303 #endif /* BSTRING */
5305 #if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
5306 #undef bcopy
5308 /* Saying `void' requires a declaration, above, where bcopy is used
5309 and that declaration causes pain for systems where bcopy is a macro. */
5310 bcopy (b1, b2, length)
5311 register char *b1;
5312 register char *b2;
5313 register int length;
5315 #ifdef VMS
5316 long max_str = 65535;
5318 while (length > max_str) {
5319 (void) LIB$MOVC3 (&max_str, b1, b2);
5320 length -= max_str;
5321 b1 += max_str;
5322 b2 += max_str;
5324 max_str = length;
5325 (void) LIB$MOVC3 (&length, b1, b2);
5326 #else
5327 while (length-- > 0)
5328 *b2++ = *b1++;
5329 #endif /* not VMS */
5331 #endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
5333 #ifndef BSTRING
5334 #ifndef bcmp
5336 bcmp (b1, b2, length) /* This could be a macro! */
5337 register char *b1;
5338 register char *b2;
5339 register int length;
5341 #ifdef VMS
5342 struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
5343 struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
5345 return STR$COMPARE (&src1, &src2);
5346 #else
5347 while (length-- > 0)
5348 if (*b1++ != *b2++)
5349 return 1;
5351 return 0;
5352 #endif /* not VMS */
5354 #endif /* no bcmp */
5355 #endif /* not BSTRING */
5357 #ifndef HAVE_STRSIGNAL
5358 char *
5359 strsignal (code)
5360 int code;
5362 char *signame = 0;
5364 if (0 <= code && code < NSIG)
5366 #ifdef VMS
5367 signame = sys_errlist[code];
5368 #else
5369 /* Cast to suppress warning if the table has const char *. */
5370 signame = (char *) sys_siglist[code];
5371 #endif
5374 return signame;
5376 #endif /* HAVE_STRSIGNAL */
5378 /* arch-tag: edb43589-4e09-4544-b325-978b5b121dcf
5379 (do not change this comment) */