(Info-search): Print the default as part of the prompt.
[emacs.git] / src / sysdep.c
blob0e1b364be72e1a5a481511f44d76d5c1ac26671c
1 /* Interfaces to system-dependent kernel and library entries.
2 Copyright (C) 1985, 86,87,88,93,94,95, 1999, 2000, 2001
3 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include <signal.h>
25 #include <setjmp.h>
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #include "lisp.h"
30 /* Including stdlib.h isn't necessarily enough to get srandom
31 declared, e.g. without __USE_XOPEN_EXTENDED with glibc 2. */
32 #ifdef HAVE_RANDOM
33 extern long int random P_ ((void));
34 #if 0 /* Don't prototype srandom; it takes an unsigned argument on
35 some systems, and an unsigned long on others, like FreeBSD
36 4.1. */
37 extern void srandom P_ ((unsigned int));
38 #endif
39 #endif
41 #include "blockinput.h"
42 #undef NULL
44 #ifdef macintosh
45 /* It is essential to include stdlib.h so that this file picks up
46 the correct definitions of rand, srand, and RAND_MAX.
47 Otherwise random numbers will not work correctly. */
48 #include <stdlib.h>
50 #ifndef subprocesses
51 /* Nonzero means delete a process right away if it exits (process.c). */
52 static int delete_exited_processes;
53 #endif
54 #endif /* macintosh */
56 #define min(x,y) ((x) > (y) ? (y) : (x))
58 #ifdef WINDOWSNT
59 #define read sys_read
60 #define write sys_write
61 #include <windows.h>
62 #ifndef NULL
63 #define NULL 0
64 #endif
65 #endif /* not WINDOWSNT */
67 /* Does anyone other than VMS need this? */
68 #ifndef fwrite
69 #define sys_fwrite fwrite
70 #else
71 #undef fwrite
72 #endif
74 #include <stdio.h>
75 #include <sys/types.h>
76 #include <sys/stat.h>
77 #include <errno.h>
79 /* Get _POSIX_VDISABLE, if it is available. */
80 #ifdef HAVE_UNISTD_H
81 #include <unistd.h>
82 #endif
84 #ifdef HAVE_STDLIB_H
85 #include <stdlib.h>
86 #endif
88 #ifdef HAVE_SETPGID
89 #if !defined (USG) || defined (BSD_PGRPS)
90 #undef setpgrp
91 #define setpgrp setpgid
92 #endif
93 #endif
95 /* Get SI_SRPC_DOMAIN, if it is available. */
96 #ifdef HAVE_SYS_SYSTEMINFO_H
97 #include <sys/systeminfo.h>
98 #endif
100 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
101 #include <dos.h>
102 #include "dosfns.h"
103 #include "msdos.h"
104 #include <sys/param.h>
106 #if __DJGPP__ > 1
107 extern int etext;
108 extern unsigned start __asm__ ("start");
109 #endif
110 #endif
112 #ifndef USE_CRT_DLL
113 #ifndef errno
114 extern int errno;
115 #endif
116 #endif
118 #ifdef VMS
119 #include <rms.h>
120 #include <ttdef.h>
121 #include <tt2def.h>
122 #include <iodef.h>
123 #include <ssdef.h>
124 #include <descrip.h>
125 #include <fibdef.h>
126 #include <atrdef.h>
127 #include <ctype.h>
128 #include <string.h>
129 #ifdef __GNUC__
130 #include <sys/file.h>
131 #else
132 #include <file.h>
133 #endif
134 #undef F_SETFL
135 #ifndef RAB$C_BID
136 #include <rab.h>
137 #endif
138 #define MAXIOSIZE (32 * PAGESIZE) /* Don't I/O more than 32 blocks at a time */
139 #endif /* VMS */
141 #ifndef BSD4_1
142 #ifdef BSD_SYSTEM /* avoid writing defined (BSD_SYSTEM) || defined (USG)
143 because the vms compiler doesn't grok `defined' */
144 #include <fcntl.h>
145 #endif
146 #ifdef USG
147 #ifndef USG5
148 #include <fcntl.h>
149 #endif
150 #endif
151 #endif /* not 4.1 bsd */
153 #ifndef MSDOS
154 #include <sys/ioctl.h>
155 #endif
157 #include "systty.h"
158 #include "syswait.h"
160 #ifdef BROKEN_TIOCGWINSZ
161 #undef TIOCGWINSZ
162 #undef TIOCSWINSZ
163 #endif
165 #if defined (USG) || defined (DGUX)
166 #include <sys/utsname.h>
167 #ifndef MEMORY_IN_STRING_H
168 #include <memory.h>
169 #endif
170 #if defined (TIOCGWINSZ) || defined (ISC4_0)
171 #ifdef NEED_SIOCTL
172 #include <sys/sioctl.h>
173 #endif
174 #ifdef NEED_PTEM_H
175 #include <sys/stream.h>
176 #include <sys/ptem.h>
177 #endif
178 #endif /* TIOCGWINSZ or ISC4_0 */
179 #endif /* USG or DGUX */
181 extern int quit_char;
183 #include "keyboard.h"
184 #include "frame.h"
185 #include "window.h"
186 #include "termhooks.h"
187 #include "termchar.h"
188 #include "termopts.h"
189 #include "dispextern.h"
190 #include "process.h"
192 #ifdef WINDOWSNT
193 #include <direct.h>
194 /* In process.h which conflicts with the local copy. */
195 #define _P_WAIT 0
196 int _CRTAPI1 _spawnlp (int, const char *, const char *, ...);
197 int _CRTAPI1 _getpid (void);
198 #endif
200 #ifdef NONSYSTEM_DIR_LIBRARY
201 #include "ndir.h"
202 #endif /* NONSYSTEM_DIR_LIBRARY */
204 #include "syssignal.h"
205 #include "systime.h"
206 #ifdef HAVE_UTIME_H
207 #include <utime.h>
208 #endif
210 #ifndef HAVE_UTIMES
211 #ifndef HAVE_STRUCT_UTIMBUF
212 /* We want to use utime rather than utimes, but we couldn't find the
213 structure declaration. We'll use the traditional one. */
214 struct utimbuf {
215 long actime;
216 long modtime;
218 #endif
219 #endif
221 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
222 #ifndef LPASS8
223 #define LPASS8 0
224 #endif
226 #ifdef BSD4_1
227 #define LNOFLSH 0100000
228 #endif
230 static int baud_convert[] =
231 #ifdef BAUD_CONVERT
232 BAUD_CONVERT;
233 #else
235 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
236 1800, 2400, 4800, 9600, 19200, 38400
238 #endif
240 #ifdef HAVE_SPEED_T
241 #include <termios.h>
242 #else
243 #if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
244 #else
245 #if defined (HAVE_TERMIOS_H) && defined (LINUX)
246 #include <termios.h>
247 #endif
248 #endif
249 #endif
251 int emacs_ospeed;
253 /* The file descriptor for Emacs's input terminal.
254 Under Unix, this is normally zero except when using X;
255 under VMS, we place the input channel number here. */
256 int input_fd;
258 void croak P_ ((char *));
260 #ifdef AIXHFT
261 void hft_init ();
262 void hft_reset ();
263 #endif
265 /* Temporary used by `sigblock' when defined in terms of signprocmask. */
267 SIGMASKTYPE sigprocmask_set;
270 /* Specify a different file descriptor for further input operations. */
272 void
273 change_input_fd (fd)
274 int fd;
276 input_fd = fd;
279 /* Discard pending input on descriptor input_fd. */
281 void
282 discard_tty_input ()
284 #ifndef WINDOWSNT
285 struct emacs_tty buf;
287 if (noninteractive)
288 return;
290 /* Discarding input is not safe when the input could contain
291 replies from the X server. So don't do it. */
292 if (read_socket_hook)
293 return;
295 #ifdef VMS
296 end_kbd_input ();
297 SYS$QIOW (0, input_fd, IO$_READVBLK|IO$M_PURGE, input_iosb, 0, 0,
298 &buf.main, 0, 0, terminator_mask, 0, 0);
299 queue_kbd_input ();
300 #else /* not VMS */
301 #ifdef APOLLO
303 int zero = 0;
304 ioctl (input_fd, TIOCFLUSH, &zero);
306 #else /* not Apollo */
307 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
308 while (dos_keyread () != -1)
310 #else /* not MSDOS */
311 EMACS_GET_TTY (input_fd, &buf);
312 EMACS_SET_TTY (input_fd, &buf, 0);
313 #endif /* not MSDOS */
314 #endif /* not Apollo */
315 #endif /* not VMS */
316 #endif /* not WINDOWSNT */
319 #ifdef SIGTSTP
321 /* Arrange for character C to be read as the next input from
322 the terminal. */
324 void
325 stuff_char (c)
326 char c;
328 if (read_socket_hook)
329 return;
331 /* Should perhaps error if in batch mode */
332 #ifdef TIOCSTI
333 ioctl (input_fd, TIOCSTI, &c);
334 #else /* no TIOCSTI */
335 error ("Cannot stuff terminal input characters in this version of Unix");
336 #endif /* no TIOCSTI */
339 #endif /* SIGTSTP */
341 void
342 init_baud_rate ()
344 if (noninteractive)
345 emacs_ospeed = 0;
346 else
348 #ifdef INIT_BAUD_RATE
349 INIT_BAUD_RATE ();
350 #else
351 #ifdef DOS_NT
352 emacs_ospeed = 15;
353 #else /* not DOS_NT */
354 #ifdef VMS
355 struct sensemode sg;
357 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &sg, 0, 0,
358 &sg.class, 12, 0, 0, 0, 0 );
359 emacs_ospeed = sg.xmit_baud;
360 #else /* not VMS */
361 #ifdef HAVE_TERMIOS
362 struct termios sg;
364 sg.c_cflag = B9600;
365 tcgetattr (input_fd, &sg);
366 emacs_ospeed = cfgetospeed (&sg);
367 #if defined (USE_GETOBAUD) && defined (getobaud)
368 /* m88k-motorola-sysv3 needs this (ghazi@noc.rutgers.edu) 9/1/94. */
369 if (emacs_ospeed == 0)
370 emacs_ospeed = getobaud (sg.c_cflag);
371 #endif
372 #else /* neither VMS nor TERMIOS */
373 #ifdef HAVE_TERMIO
374 struct termio sg;
376 sg.c_cflag = B9600;
377 #ifdef HAVE_TCATTR
378 tcgetattr (input_fd, &sg);
379 #else
380 ioctl (input_fd, TCGETA, &sg);
381 #endif
382 emacs_ospeed = sg.c_cflag & CBAUD;
383 #else /* neither VMS nor TERMIOS nor TERMIO */
384 struct sgttyb sg;
386 sg.sg_ospeed = B9600;
387 if (ioctl (input_fd, TIOCGETP, &sg) < 0)
388 abort ();
389 emacs_ospeed = sg.sg_ospeed;
390 #endif /* not HAVE_TERMIO */
391 #endif /* not HAVE_TERMIOS */
392 #endif /* not VMS */
393 #endif /* not DOS_NT */
394 #endif /* not INIT_BAUD_RATE */
397 baud_rate = (emacs_ospeed < sizeof baud_convert / sizeof baud_convert[0]
398 ? baud_convert[emacs_ospeed] : 9600);
399 if (baud_rate == 0)
400 baud_rate = 1200;
403 /*ARGSUSED*/
404 void
405 set_exclusive_use (fd)
406 int fd;
408 #ifdef FIOCLEX
409 ioctl (fd, FIOCLEX, 0);
410 #endif
411 /* Ok to do nothing if this feature does not exist */
414 #ifndef subprocesses
416 wait_without_blocking ()
418 #ifdef BSD_SYSTEM
419 wait3 (0, WNOHANG | WUNTRACED, 0);
420 #else
421 croak ("wait_without_blocking");
422 #endif
423 synch_process_alive = 0;
426 #endif /* not subprocesses */
428 int wait_debugging; /* Set nonzero to make following function work under dbx
429 (at least for bsd). */
431 SIGTYPE
432 wait_for_termination_signal ()
435 /* Wait for subprocess with process id `pid' to terminate and
436 make sure it will get eliminated (not remain forever as a zombie) */
438 void
439 wait_for_termination (pid)
440 int pid;
442 while (1)
444 #ifdef subprocesses
445 #ifdef VMS
446 int status;
448 status = SYS$FORCEX (&pid, 0, 0);
449 break;
450 #else /* not VMS */
451 #if defined (BSD_SYSTEM) || (defined (HPUX) && !defined (HPUX_5))
452 /* Note that kill returns -1 even if the process is just a zombie now.
453 But inevitably a SIGCHLD interrupt should be generated
454 and child_sig will do wait3 and make the process go away. */
455 /* There is some indication that there is a bug involved with
456 termination of subprocesses, perhaps involving a kernel bug too,
457 but no idea what it is. Just as a hunch we signal SIGCHLD to see
458 if that causes the problem to go away or get worse. */
459 sigsetmask (sigmask (SIGCHLD));
460 if (0 > kill (pid, 0))
462 sigsetmask (SIGEMPTYMASK);
463 kill (getpid (), SIGCHLD);
464 break;
466 if (wait_debugging)
467 sleep (1);
468 else
469 sigpause (SIGEMPTYMASK);
470 #else /* not BSD_SYSTEM, and not HPUX version >= 6 */
471 #if defined (UNIPLUS)
472 if (0 > kill (pid, 0))
473 break;
474 wait (0);
475 #else /* neither BSD_SYSTEM nor UNIPLUS: random sysV */
476 #ifdef POSIX_SIGNALS /* would this work for LINUX as well? */
477 sigblock (sigmask (SIGCHLD));
478 if (0 > kill (pid, 0))
480 sigunblock (sigmask (SIGCHLD));
481 break;
483 sigpause (SIGEMPTYMASK);
484 #else /* not POSIX_SIGNALS */
485 #ifdef HAVE_SYSV_SIGPAUSE
486 sighold (SIGCHLD);
487 if (0 > kill (pid, 0))
489 sigrelse (SIGCHLD);
490 break;
492 sigpause (SIGCHLD);
493 #else /* not HAVE_SYSV_SIGPAUSE */
494 #ifdef WINDOWSNT
495 wait (0);
496 break;
497 #else /* not WINDOWSNT */
498 if (0 > kill (pid, 0))
499 break;
500 /* Using sleep instead of pause avoids timing error.
501 If the inferior dies just before the sleep,
502 we lose just one second. */
503 sleep (1);
504 #endif /* not WINDOWSNT */
505 #endif /* not HAVE_SYSV_SIGPAUSE */
506 #endif /* not POSIX_SIGNALS */
507 #endif /* not UNIPLUS */
508 #endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
509 #endif /* not VMS */
510 #else /* not subprocesses */
511 #if __DJGPP__ > 1
512 break;
513 #else /* not __DJGPP__ > 1 */
514 #ifndef BSD4_1
515 if (kill (pid, 0) < 0)
516 break;
517 wait (0);
518 #else /* BSD4_1 */
519 int status;
520 status = wait (0);
521 if (status == pid || status == -1)
522 break;
523 #endif /* BSD4_1 */
524 #endif /* not __DJGPP__ > 1*/
525 #endif /* not subprocesses */
529 #ifdef subprocesses
532 * flush any pending output
533 * (may flush input as well; it does not matter the way we use it)
536 void
537 flush_pending_output (channel)
538 int channel;
540 #ifdef HAVE_TERMIOS
541 /* If we try this, we get hit with SIGTTIN, because
542 the child's tty belongs to the child's pgrp. */
543 #else
544 #ifdef TCFLSH
545 ioctl (channel, TCFLSH, 1);
546 #else
547 #ifdef TIOCFLUSH
548 int zero = 0;
549 /* 3rd arg should be ignored
550 but some 4.2 kernels actually want the address of an int
551 and nonzero means something different. */
552 ioctl (channel, TIOCFLUSH, &zero);
553 #endif
554 #endif
555 #endif
558 #ifndef VMS
559 /* Set up the terminal at the other end of a pseudo-terminal that
560 we will be controlling an inferior through.
561 It should not echo or do line-editing, since that is done
562 in Emacs. No padding needed for insertion into an Emacs buffer. */
564 void
565 child_setup_tty (out)
566 int out;
568 #ifndef DOS_NT
569 struct emacs_tty s;
571 EMACS_GET_TTY (out, &s);
573 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
574 s.main.c_oflag |= OPOST; /* Enable output postprocessing */
575 s.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL on output */
576 #ifdef NLDLY
577 s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
578 /* No output delays */
579 #endif
580 s.main.c_lflag &= ~ECHO; /* Disable echo */
581 s.main.c_lflag |= ISIG; /* Enable signals */
582 #ifdef IUCLC
583 s.main.c_iflag &= ~IUCLC; /* Disable downcasing on input. */
584 #endif
585 #ifdef ISTRIP
586 s.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
587 #endif
588 #ifdef OLCUC
589 s.main.c_oflag &= ~OLCUC; /* Disable upcasing on output. */
590 #endif
591 s.main.c_oflag &= ~TAB3; /* Disable tab expansion */
592 s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
593 #if 0
594 /* Said to be unnecessary: */
595 s.main.c_cc[VMIN] = 1; /* minimum number of characters to accept */
596 s.main.c_cc[VTIME] = 0; /* wait forever for at least 1 character */
597 #endif
599 s.main.c_lflag |= ICANON; /* Enable erase/kill and eof processing */
600 s.main.c_cc[VEOF] = 04; /* insure that EOF is Control-D */
601 s.main.c_cc[VERASE] = CDISABLE; /* disable erase processing */
602 s.main.c_cc[VKILL] = CDISABLE; /* disable kill processing */
604 #ifdef HPUX
605 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
606 #endif /* HPUX */
608 #ifdef AIX
609 /* AIX enhanced edit loses NULs, so disable it */
610 #ifndef IBMR2AIX
611 s.main.c_line = 0;
612 s.main.c_iflag &= ~ASCEDIT;
613 #endif
614 /* Also, PTY overloads NUL and BREAK.
615 don't ignore break, but don't signal either, so it looks like NUL. */
616 s.main.c_iflag &= ~IGNBRK;
617 s.main.c_iflag &= ~BRKINT;
618 /* QUIT and INTR work better as signals, so disable character forms */
619 s.main.c_cc[VINTR] = 0377;
620 #ifdef SIGNALS_VIA_CHARACTERS
621 /* the QUIT and INTR character are used in process_send_signal
622 so set them here to something useful. */
623 if (s.main.c_cc[VQUIT] == 0377)
624 s.main.c_cc[VQUIT] = '\\'&037; /* Control-\ */
625 if (s.main.c_cc[VINTR] == 0377)
626 s.main.c_cc[VINTR] = 'C'&037; /* Control-C */
627 #else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
628 /* QUIT and INTR work better as signals, so disable character forms */
629 s.main.c_cc[VQUIT] = 0377;
630 s.main.c_cc[VINTR] = 0377;
631 s.main.c_lflag &= ~ISIG;
632 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
633 s.main.c_cc[VEOL] = 0377;
634 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
635 #endif /* AIX */
637 #else /* not HAVE_TERMIO */
639 s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
640 | CBREAK | TANDEM);
641 s.main.sg_flags |= LPASS8;
642 s.main.sg_erase = 0377;
643 s.main.sg_kill = 0377;
644 s.lmode = LLITOUT | s.lmode; /* Don't strip 8th bit */
646 #endif /* not HAVE_TERMIO */
648 EMACS_SET_TTY (out, &s, 0);
650 #ifdef BSD4_1
651 if (interrupt_input)
652 reset_sigio ();
653 #endif /* BSD4_1 */
654 #ifdef RTU
656 int zero = 0;
657 ioctl (out, FIOASYNC, &zero);
659 #endif /* RTU */
660 #endif /* not DOS_NT */
662 #endif /* not VMS */
664 #endif /* subprocesses */
666 /* Record a signal code and the handler for it. */
667 struct save_signal
669 int code;
670 SIGTYPE (*handler) P_ ((int));
673 static void save_signal_handlers P_ ((struct save_signal *));
674 static void restore_signal_handlers P_ ((struct save_signal *));
676 /* Suspend the Emacs process; give terminal to its superior. */
678 void
679 sys_suspend ()
681 #ifdef VMS
682 /* "Foster" parentage allows emacs to return to a subprocess that attached
683 to the current emacs as a cheaper than starting a whole new process. This
684 is set up by KEPTEDITOR.COM. */
685 unsigned long parent_id, foster_parent_id;
686 char *fpid_string;
688 fpid_string = getenv ("EMACS_PARENT_PID");
689 if (fpid_string != NULL)
691 sscanf (fpid_string, "%x", &foster_parent_id);
692 if (foster_parent_id != 0)
693 parent_id = foster_parent_id;
694 else
695 parent_id = getppid ();
697 else
698 parent_id = getppid ();
700 xfree (fpid_string); /* On VMS, this was malloc'd */
702 if (parent_id && parent_id != 0xffffffff)
704 SIGTYPE (*oldsig)() = (int) signal (SIGINT, SIG_IGN);
705 int status = LIB$ATTACH (&parent_id) & 1;
706 signal (SIGINT, oldsig);
707 return status;
709 else
711 struct {
712 int l;
713 char *a;
714 } d_prompt;
715 d_prompt.l = sizeof ("Emacs: "); /* Our special prompt */
716 d_prompt.a = "Emacs: "; /* Just a reminder */
717 LIB$SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt, 0);
718 return 1;
720 return -1;
721 #else
722 #if defined (SIGTSTP) && !defined (MSDOS)
725 int pgrp = EMACS_GETPGRP (0);
726 EMACS_KILLPG (pgrp, SIGTSTP);
729 #else /* No SIGTSTP */
730 #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
731 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
732 kill (getpid (), SIGQUIT);
734 #else /* No SIGTSTP or USG_JOBCTRL */
736 /* On a system where suspending is not implemented,
737 instead fork a subshell and let it talk directly to the terminal
738 while we wait. */
739 sys_subshell ();
741 #endif /* no USG_JOBCTRL */
742 #endif /* no SIGTSTP */
743 #endif /* not VMS */
746 /* Fork a subshell. */
748 #ifndef macintosh
749 void
750 sys_subshell ()
752 #ifndef VMS
753 #ifdef DOS_NT /* Demacs 1.1.2 91/10/20 Manabu Higashida */
754 int st;
755 char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS. */
756 #endif
757 int pid;
758 struct save_signal saved_handlers[5];
759 Lisp_Object dir;
760 unsigned char *str = 0;
761 int len;
763 saved_handlers[0].code = SIGINT;
764 saved_handlers[1].code = SIGQUIT;
765 saved_handlers[2].code = SIGTERM;
766 #ifdef SIGIO
767 saved_handlers[3].code = SIGIO;
768 saved_handlers[4].code = 0;
769 #else
770 saved_handlers[3].code = 0;
771 #endif
773 /* Mentioning current_buffer->buffer would mean including buffer.h,
774 which somehow wedges the hp compiler. So instead... */
776 dir = intern ("default-directory");
777 if (NILP (Fboundp (dir)))
778 goto xyzzy;
779 dir = Fsymbol_value (dir);
780 if (!STRINGP (dir))
781 goto xyzzy;
783 dir = expand_and_dir_to_file (Funhandled_file_name_directory (dir), Qnil);
784 str = (unsigned char *) alloca (XSTRING (dir)->size + 2);
785 len = XSTRING (dir)->size;
786 bcopy (XSTRING (dir)->data, str, len);
787 if (str[len - 1] != '/') str[len++] = '/';
788 str[len] = 0;
789 xyzzy:
791 #ifdef DOS_NT
792 pid = 0;
793 #if __DJGPP__ > 1
794 save_signal_handlers (saved_handlers);
795 synch_process_alive = 1;
796 #endif /* __DJGPP__ > 1 */
797 #else
798 pid = vfork ();
799 if (pid == -1)
800 error ("Can't spawn subshell");
801 #endif
803 if (pid == 0)
805 char *sh = 0;
807 #ifdef DOS_NT /* MW, Aug 1993 */
808 getwd (oldwd);
809 if (sh == 0)
810 sh = (char *) egetenv ("SUSPEND"); /* KFS, 1994-12-14 */
811 #endif
812 if (sh == 0)
813 sh = (char *) egetenv ("SHELL");
814 if (sh == 0)
815 sh = "sh";
817 /* Use our buffer's default directory for the subshell. */
818 if (str)
819 chdir ((char *) str);
821 #ifdef subprocesses
822 close_process_descs (); /* Close Emacs's pipes/ptys */
823 #endif
825 #ifdef SET_EMACS_PRIORITY
827 extern int emacs_priority;
829 if (emacs_priority < 0)
830 nice (-emacs_priority);
832 #endif
834 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
835 st = system (sh);
836 chdir (oldwd);
837 #if 0 /* This is also reported if last command executed in subshell failed, KFS */
838 if (st)
839 report_file_error ("Can't execute subshell", Fcons (build_string (sh), Qnil));
840 #endif
841 #else /* not MSDOS */
842 #ifdef WINDOWSNT
843 /* Waits for process completion */
844 pid = _spawnlp (_P_WAIT, sh, sh, NULL);
845 chdir (oldwd);
846 if (pid == -1)
847 write (1, "Can't execute subshell", 22);
848 #else /* not WINDOWSNT */
849 execlp (sh, sh, 0);
850 write (1, "Can't execute subshell", 22);
851 _exit (1);
852 #endif /* not WINDOWSNT */
853 #endif /* not MSDOS */
856 /* Do this now if we did not do it before. */
857 #if !defined (MSDOS) || __DJGPP__ == 1
858 save_signal_handlers (saved_handlers);
859 synch_process_alive = 1;
860 #endif
862 #ifndef DOS_NT
863 wait_for_termination (pid);
864 #endif
865 restore_signal_handlers (saved_handlers);
866 synch_process_alive = 0;
867 #endif /* !VMS */
869 #endif /* !macintosh */
871 static void
872 save_signal_handlers (saved_handlers)
873 struct save_signal *saved_handlers;
875 while (saved_handlers->code)
877 saved_handlers->handler
878 = (SIGTYPE (*) P_ ((int))) signal (saved_handlers->code, SIG_IGN);
879 saved_handlers++;
883 static void
884 restore_signal_handlers (saved_handlers)
885 struct save_signal *saved_handlers;
887 while (saved_handlers->code)
889 signal (saved_handlers->code, saved_handlers->handler);
890 saved_handlers++;
894 #ifdef F_SETFL
896 int old_fcntl_flags;
898 void
899 init_sigio (fd)
900 int fd;
902 #ifdef FASYNC
903 old_fcntl_flags = fcntl (fd, F_GETFL, 0) & ~FASYNC;
904 fcntl (fd, F_SETFL, old_fcntl_flags | FASYNC);
905 #endif
906 interrupts_deferred = 0;
909 void
910 reset_sigio ()
912 unrequest_sigio ();
915 #ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
917 void
918 request_sigio ()
920 if (read_socket_hook)
921 return;
923 #ifdef SIGWINCH
924 sigunblock (sigmask (SIGWINCH));
925 #endif
926 fcntl (input_fd, F_SETFL, old_fcntl_flags | FASYNC);
928 interrupts_deferred = 0;
931 void
932 unrequest_sigio ()
934 if (read_socket_hook)
935 return;
937 #ifdef SIGWINCH
938 sigblock (sigmask (SIGWINCH));
939 #endif
940 fcntl (input_fd, F_SETFL, old_fcntl_flags);
941 interrupts_deferred = 1;
944 #else /* no FASYNC */
945 #ifdef STRIDE /* Stride doesn't have FASYNC - use FIOASYNC */
947 void
948 request_sigio ()
950 int on = 1;
952 if (read_socket_hook)
953 return;
955 ioctl (input_fd, FIOASYNC, &on);
956 interrupts_deferred = 0;
959 void
960 unrequest_sigio ()
962 int off = 0;
964 if (read_socket_hook)
965 return;
967 ioctl (input_fd, FIOASYNC, &off);
968 interrupts_deferred = 1;
971 #else /* not FASYNC, not STRIDE */
973 #ifdef _CX_UX
975 #include <termios.h>
977 void
978 request_sigio ()
980 int on = 1;
981 sigset_t st;
983 if (read_socket_hook)
984 return;
986 sigemptyset (&st);
987 sigaddset (&st, SIGIO);
988 ioctl (input_fd, FIOASYNC, &on);
989 interrupts_deferred = 0;
990 sigprocmask (SIG_UNBLOCK, &st, (sigset_t *)0);
993 void
994 unrequest_sigio ()
996 int off = 0;
998 if (read_socket_hook)
999 return;
1001 ioctl (input_fd, FIOASYNC, &off);
1002 interrupts_deferred = 1;
1005 #else /* ! _CX_UX */
1007 void
1008 request_sigio ()
1010 if (read_socket_hook)
1011 return;
1013 croak ("request_sigio");
1016 void
1017 unrequest_sigio ()
1019 if (read_socket_hook)
1020 return;
1022 croak ("unrequest_sigio");
1025 #endif /* _CX_UX */
1026 #endif /* STRIDE */
1027 #endif /* FASYNC */
1028 #endif /* F_SETFL */
1030 /* Saving and restoring the process group of Emacs's terminal. */
1032 #ifdef BSD_PGRPS
1034 /* The process group of which Emacs was a member when it initially
1035 started.
1037 If Emacs was in its own process group (i.e. inherited_pgroup ==
1038 getpid ()), then we know we're running under a shell with job
1039 control (Emacs would never be run as part of a pipeline).
1040 Everything is fine.
1042 If Emacs was not in its own process group, then we know we're
1043 running under a shell (or a caller) that doesn't know how to
1044 separate itself from Emacs (like sh). Emacs must be in its own
1045 process group in order to receive SIGIO correctly. In this
1046 situation, we put ourselves in our own pgroup, forcibly set the
1047 tty's pgroup to our pgroup, and make sure to restore and reinstate
1048 the tty's pgroup just like any other terminal setting. If
1049 inherited_group was not the tty's pgroup, then we'll get a
1050 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
1051 it goes foreground in the future, which is what should happen. */
1052 int inherited_pgroup;
1054 /* Split off the foreground process group to Emacs alone.
1055 When we are in the foreground, but not started in our own process
1056 group, redirect the TTY to point to our own process group. We need
1057 to be in our own process group to receive SIGIO properly. */
1058 void
1059 narrow_foreground_group ()
1061 int me = getpid ();
1063 setpgrp (0, inherited_pgroup);
1064 if (inherited_pgroup != me)
1065 EMACS_SET_TTY_PGRP (input_fd, &me);
1066 setpgrp (0, me);
1069 /* Set the tty to our original foreground group. */
1070 void
1071 widen_foreground_group ()
1073 if (inherited_pgroup != getpid ())
1074 EMACS_SET_TTY_PGRP (input_fd, &inherited_pgroup);
1075 setpgrp (0, inherited_pgroup);
1078 #endif /* BSD_PGRPS */
1080 /* Getting and setting emacs_tty structures. */
1082 /* Set *TC to the parameters associated with the terminal FD.
1083 Return zero if all's well, or -1 if we ran into an error we
1084 couldn't deal with. */
1086 emacs_get_tty (fd, settings)
1087 int fd;
1088 struct emacs_tty *settings;
1090 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
1091 #ifdef HAVE_TCATTR
1092 /* We have those nifty POSIX tcmumbleattr functions. */
1093 bzero (&settings->main, sizeof (settings->main));
1094 if (tcgetattr (fd, &settings->main) < 0)
1095 return -1;
1097 #else
1098 #ifdef HAVE_TERMIO
1099 /* The SYSV-style interface? */
1100 if (ioctl (fd, TCGETA, &settings->main) < 0)
1101 return -1;
1103 #else
1104 #ifdef VMS
1105 /* Vehemently Monstrous System? :-) */
1106 if (! (SYS$QIOW (0, fd, IO$_SENSEMODE, settings, 0, 0,
1107 &settings->main.class, 12, 0, 0, 0, 0)
1108 & 1))
1109 return -1;
1111 #else
1112 #ifndef DOS_NT
1113 /* I give up - I hope you have the BSD ioctls. */
1114 if (ioctl (fd, TIOCGETP, &settings->main) < 0)
1115 return -1;
1116 #endif /* not DOS_NT */
1117 #endif
1118 #endif
1119 #endif
1121 /* Suivant - Do we have to get struct ltchars data? */
1122 #ifdef HAVE_LTCHARS
1123 if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
1124 return -1;
1125 #endif
1127 /* How about a struct tchars and a wordful of lmode bits? */
1128 #ifdef HAVE_TCHARS
1129 if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
1130 || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
1131 return -1;
1132 #endif
1134 /* We have survived the tempest. */
1135 return 0;
1139 /* Set the parameters of the tty on FD according to the contents of
1140 *SETTINGS. If FLUSHP is non-zero, we discard input.
1141 Return 0 if all went well, and -1 if anything failed. */
1144 emacs_set_tty (fd, settings, flushp)
1145 int fd;
1146 struct emacs_tty *settings;
1147 int flushp;
1149 /* Set the primary parameters - baud rate, character size, etcetera. */
1150 #ifdef HAVE_TCATTR
1151 int i;
1152 /* We have those nifty POSIX tcmumbleattr functions.
1153 William J. Smith <wjs@wiis.wang.com> writes:
1154 "POSIX 1003.1 defines tcsetattr to return success if it was
1155 able to perform any of the requested actions, even if some
1156 of the requested actions could not be performed.
1157 We must read settings back to ensure tty setup properly.
1158 AIX requires this to keep tty from hanging occasionally." */
1159 /* This make sure that we don't loop indefinitely in here. */
1160 for (i = 0 ; i < 10 ; i++)
1161 if (tcsetattr (fd, flushp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
1163 if (errno == EINTR)
1164 continue;
1165 else
1166 return -1;
1168 else
1170 struct termios new;
1172 bzero (&new, sizeof (new));
1173 /* Get the current settings, and see if they're what we asked for. */
1174 tcgetattr (fd, &new);
1175 /* We cannot use memcmp on the whole structure here because under
1176 * aix386 the termios structure has some reserved field that may
1177 * not be filled in.
1179 if ( new.c_iflag == settings->main.c_iflag
1180 && new.c_oflag == settings->main.c_oflag
1181 && new.c_cflag == settings->main.c_cflag
1182 && new.c_lflag == settings->main.c_lflag
1183 && memcmp (new.c_cc, settings->main.c_cc, NCCS) == 0)
1184 break;
1185 else
1186 continue;
1189 #else
1190 #ifdef HAVE_TERMIO
1191 /* The SYSV-style interface? */
1192 if (ioctl (fd, flushp ? TCSETAF : TCSETAW, &settings->main) < 0)
1193 return -1;
1195 #else
1196 #ifdef VMS
1197 /* Vehemently Monstrous System? :-) */
1198 if (! (SYS$QIOW (0, fd, IO$_SETMODE, &input_iosb, 0, 0,
1199 &settings->main.class, 12, 0, 0, 0, 0)
1200 & 1))
1201 return -1;
1203 #else
1204 #ifndef DOS_NT
1205 /* I give up - I hope you have the BSD ioctls. */
1206 if (ioctl (fd, (flushp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
1207 return -1;
1208 #endif /* not DOS_NT */
1210 #endif
1211 #endif
1212 #endif
1214 /* Suivant - Do we have to get struct ltchars data? */
1215 #ifdef HAVE_LTCHARS
1216 if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
1217 return -1;
1218 #endif
1220 /* How about a struct tchars and a wordful of lmode bits? */
1221 #ifdef HAVE_TCHARS
1222 if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
1223 || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
1224 return -1;
1225 #endif
1227 /* We have survived the tempest. */
1228 return 0;
1232 /* The initial tty mode bits */
1233 struct emacs_tty old_tty;
1235 /* 1 if we have been through init_sys_modes. */
1236 int term_initted;
1238 /* 1 if outer tty status has been recorded. */
1239 int old_tty_valid;
1241 #ifdef BSD4_1
1242 /* BSD 4.1 needs to keep track of the lmode bits in order to start
1243 sigio. */
1244 int lmode;
1245 #endif
1247 #ifndef F_SETOWN_BUG
1248 #ifdef F_SETOWN
1249 int old_fcntl_owner;
1250 #endif /* F_SETOWN */
1251 #endif /* F_SETOWN_BUG */
1253 /* This may also be defined in stdio,
1254 but if so, this does no harm,
1255 and using the same name avoids wasting the other one's space. */
1257 #ifdef nec_ews_svr4
1258 extern char *_sobuf ;
1259 #else
1260 #if defined (USG) || defined (DGUX)
1261 unsigned char _sobuf[BUFSIZ+8];
1262 #else
1263 char _sobuf[BUFSIZ];
1264 #endif
1265 #endif
1267 #ifdef HAVE_LTCHARS
1268 static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
1269 #endif
1270 #ifdef HAVE_TCHARS
1271 static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
1272 #endif
1274 void
1275 init_sys_modes ()
1277 struct emacs_tty tty;
1279 #ifdef macintosh
1280 /* cus-start.el complains if delete-exited-processes is not defined */
1281 #ifndef subprocesses
1282 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
1283 "*Non-nil means delete processes immediately when they exit.\n\
1284 nil means don't delete them until `list-processes' is run.");
1285 delete_exited_processes = 0;
1286 #endif
1287 #endif /* not macintosh */
1289 #ifdef VMS
1290 #if 0
1291 static int oob_chars[2] = {0, 1 << 7}; /* catch C-g's */
1292 extern int (*interrupt_signal) ();
1293 #endif
1294 #endif
1296 Vtty_erase_char = Qnil;
1298 if (noninteractive)
1299 return;
1301 #ifdef VMS
1302 if (!input_ef)
1303 input_ef = get_kbd_event_flag ();
1304 /* LIB$GET_EF (&input_ef); */
1305 SYS$CLREF (input_ef);
1306 waiting_for_ast = 0;
1307 if (!timer_ef)
1308 timer_ef = get_timer_event_flag ();
1309 /* LIB$GET_EF (&timer_ef); */
1310 SYS$CLREF (timer_ef);
1311 #if 0
1312 if (!process_ef)
1314 LIB$GET_EF (&process_ef);
1315 SYS$CLREF (process_ef);
1317 if (input_ef / 32 != process_ef / 32)
1318 croak ("Input and process event flags in different clusters.");
1319 #endif
1320 if (input_ef / 32 != timer_ef / 32)
1321 croak ("Input and timer event flags in different clusters.");
1322 #if 0
1323 input_eflist = ((unsigned) 1 << (input_ef % 32)) |
1324 ((unsigned) 1 << (process_ef % 32));
1325 #endif
1326 timer_eflist = ((unsigned) 1 << (input_ef % 32)) |
1327 ((unsigned) 1 << (timer_ef % 32));
1328 #ifndef VMS4_4
1329 sys_access_reinit ();
1330 #endif
1331 #endif /* not VMS */
1333 #ifdef BSD_PGRPS
1334 if (! read_socket_hook && EQ (Vwindow_system, Qnil))
1335 narrow_foreground_group ();
1336 #endif
1338 #ifdef HAVE_WINDOW_SYSTEM
1339 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1340 needs the initialization code below. */
1341 if (!read_socket_hook && EQ (Vwindow_system, Qnil))
1342 #endif
1344 EMACS_GET_TTY (input_fd, &old_tty);
1346 old_tty_valid = 1;
1348 tty = old_tty;
1350 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
1351 XSETINT (Vtty_erase_char, old_tty.main.c_cc[VERASE]);
1353 #ifdef DGUX
1354 /* This allows meta to be sent on 8th bit. */
1355 tty.main.c_iflag &= ~INPCK; /* don't check input for parity */
1356 #endif
1357 tty.main.c_iflag |= (IGNBRK); /* Ignore break condition */
1358 tty.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
1359 #ifdef INLCR /* I'm just being cautious,
1360 since I can't check how widespread INLCR is--rms. */
1361 tty.main.c_iflag &= ~INLCR; /* Disable map of NL to CR on input */
1362 #endif
1363 #ifdef ISTRIP
1364 tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
1365 #endif
1366 tty.main.c_lflag &= ~ECHO; /* Disable echo */
1367 tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */
1368 #ifdef IEXTEN
1369 tty.main.c_lflag &= ~IEXTEN; /* Disable other editing characters. */
1370 #endif
1371 tty.main.c_lflag |= ISIG; /* Enable signals */
1372 if (flow_control)
1374 tty.main.c_iflag |= IXON; /* Enable start/stop output control */
1375 #ifdef IXANY
1376 tty.main.c_iflag &= ~IXANY;
1377 #endif /* IXANY */
1379 else
1380 tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */
1381 tty.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL
1382 on output */
1383 tty.main.c_oflag &= ~TAB3; /* Disable tab expansion */
1384 #ifdef CS8
1385 if (meta_key)
1387 tty.main.c_cflag |= CS8; /* allow 8th bit on input */
1388 tty.main.c_cflag &= ~PARENB;/* Don't check parity */
1390 #endif
1391 tty.main.c_cc[VINTR] = quit_char; /* C-g (usually) gives SIGINT */
1392 /* Set up C-g for both SIGQUIT and SIGINT.
1393 We don't know which we will get, but we handle both alike
1394 so which one it really gives us does not matter. */
1395 tty.main.c_cc[VQUIT] = quit_char;
1396 tty.main.c_cc[VMIN] = 1; /* Input should wait for at least 1 char */
1397 tty.main.c_cc[VTIME] = 0; /* no matter how long that takes. */
1398 #ifdef VSWTCH
1399 tty.main.c_cc[VSWTCH] = CDISABLE; /* Turn off shell layering use
1400 of C-z */
1401 #endif /* VSWTCH */
1403 #if defined (mips) || defined (HAVE_TCATTR)
1404 #ifdef VSUSP
1405 tty.main.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z. */
1406 #endif /* VSUSP */
1407 #ifdef V_DSUSP
1408 tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y. */
1409 #endif /* V_DSUSP */
1410 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1411 tty.main.c_cc[VDSUSP] = CDISABLE;
1412 #endif /* VDSUSP */
1413 #ifdef VLNEXT
1414 tty.main.c_cc[VLNEXT] = CDISABLE;
1415 #endif /* VLNEXT */
1416 #ifdef VREPRINT
1417 tty.main.c_cc[VREPRINT] = CDISABLE;
1418 #endif /* VREPRINT */
1419 #ifdef VWERASE
1420 tty.main.c_cc[VWERASE] = CDISABLE;
1421 #endif /* VWERASE */
1422 #ifdef VDISCARD
1423 tty.main.c_cc[VDISCARD] = CDISABLE;
1424 #endif /* VDISCARD */
1426 if (flow_control)
1428 #ifdef VSTART
1429 tty.main.c_cc[VSTART] = '\021';
1430 #endif /* VSTART */
1431 #ifdef VSTOP
1432 tty.main.c_cc[VSTOP] = '\023';
1433 #endif /* VSTOP */
1435 else
1437 #ifdef VSTART
1438 tty.main.c_cc[VSTART] = CDISABLE;
1439 #endif /* VSTART */
1440 #ifdef VSTOP
1441 tty.main.c_cc[VSTOP] = CDISABLE;
1442 #endif /* VSTOP */
1444 #endif /* mips or HAVE_TCATTR */
1446 #ifdef SET_LINE_DISCIPLINE
1447 /* Need to explicitly request TERMIODISC line discipline or
1448 Ultrix's termios does not work correctly. */
1449 tty.main.c_line = SET_LINE_DISCIPLINE;
1450 #endif
1451 #ifdef AIX
1452 #ifndef IBMR2AIX
1453 /* AIX enhanced edit loses NULs, so disable it. */
1454 tty.main.c_line = 0;
1455 tty.main.c_iflag &= ~ASCEDIT;
1456 #else
1457 tty.main.c_cc[VSTRT] = 255;
1458 tty.main.c_cc[VSTOP] = 255;
1459 tty.main.c_cc[VSUSP] = 255;
1460 tty.main.c_cc[VDSUSP] = 255;
1461 #endif /* IBMR2AIX */
1462 if (flow_control)
1464 #ifdef VSTART
1465 tty.main.c_cc[VSTART] = '\021';
1466 #endif /* VSTART */
1467 #ifdef VSTOP
1468 tty.main.c_cc[VSTOP] = '\023';
1469 #endif /* VSTOP */
1471 /* Also, PTY overloads NUL and BREAK.
1472 don't ignore break, but don't signal either, so it looks like NUL.
1473 This really serves a purpose only if running in an XTERM window
1474 or via TELNET or the like, but does no harm elsewhere. */
1475 tty.main.c_iflag &= ~IGNBRK;
1476 tty.main.c_iflag &= ~BRKINT;
1477 #endif
1478 #else /* if not HAVE_TERMIO */
1479 #ifdef VMS
1480 tty.main.tt_char |= TT$M_NOECHO;
1481 if (meta_key)
1482 tty.main.tt_char |= TT$M_EIGHTBIT;
1483 if (flow_control)
1484 tty.main.tt_char |= TT$M_TTSYNC;
1485 else
1486 tty.main.tt_char &= ~TT$M_TTSYNC;
1487 tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
1488 #else /* not VMS (BSD, that is) */
1489 #ifndef DOS_NT
1490 XSETINT (Vtty_erase_char, tty.main.sg_erase);
1491 tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
1492 if (meta_key)
1493 tty.main.sg_flags |= ANYP;
1494 tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
1495 #endif /* not DOS_NT */
1496 #endif /* not VMS (BSD, that is) */
1497 #endif /* not HAVE_TERMIO */
1499 /* If going to use CBREAK mode, we must request C-g to interrupt
1500 and turn off start and stop chars, etc. If not going to use
1501 CBREAK mode, do this anyway so as to turn off local flow
1502 control for user coming over network on 4.2; in this case,
1503 only t_stopc and t_startc really matter. */
1504 #ifndef HAVE_TERMIO
1505 #ifdef HAVE_TCHARS
1506 /* Note: if not using CBREAK mode, it makes no difference how we
1507 set this */
1508 tty.tchars = new_tchars;
1509 tty.tchars.t_intrc = quit_char;
1510 if (flow_control)
1512 tty.tchars.t_startc = '\021';
1513 tty.tchars.t_stopc = '\023';
1516 tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | old_tty.lmode;
1517 #ifdef ultrix
1518 /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
1519 anything, and leaving it in breaks the meta key. Go figure. */
1520 tty.lmode &= ~LLITOUT;
1521 #endif
1523 #ifdef BSD4_1
1524 lmode = tty.lmode;
1525 #endif
1527 #endif /* HAVE_TCHARS */
1528 #endif /* not HAVE_TERMIO */
1530 #ifdef HAVE_LTCHARS
1531 tty.ltchars = new_ltchars;
1532 #endif /* HAVE_LTCHARS */
1533 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
1534 if (!term_initted)
1535 internal_terminal_init ();
1536 dos_ttraw ();
1537 #endif
1539 EMACS_SET_TTY (input_fd, &tty, 0);
1541 /* This code added to insure that, if flow-control is not to be used,
1542 we have an unlocked terminal at the start. */
1544 #ifdef TCXONC
1545 if (!flow_control) ioctl (input_fd, TCXONC, 1);
1546 #endif
1547 #ifndef APOLLO
1548 #ifdef TIOCSTART
1549 if (!flow_control) ioctl (input_fd, TIOCSTART, 0);
1550 #endif
1551 #endif
1553 #if defined (HAVE_TERMIOS) || defined (HPUX9)
1554 #ifdef TCOON
1555 if (!flow_control) tcflow (input_fd, TCOON);
1556 #endif
1557 #endif
1559 #ifdef AIXHFT
1560 hft_init ();
1561 #ifdef IBMR2AIX
1563 /* IBM's HFT device usually thinks a ^J should be LF/CR. We need it
1564 to be only LF. This is the way that is done. */
1565 struct termio tty;
1567 if (ioctl (1, HFTGETID, &tty) != -1)
1568 write (1, "\033[20l", 5);
1570 #endif
1571 #endif /* AIXHFT */
1573 #ifdef VMS
1574 /* Appears to do nothing when in PASTHRU mode.
1575 SYS$QIOW (0, input_fd, IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
1576 interrupt_signal, oob_chars, 0, 0, 0, 0);
1578 queue_kbd_input (0);
1579 #endif /* VMS */
1582 #ifdef F_SETFL
1583 #ifndef F_SETOWN_BUG
1584 #ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
1585 if (interrupt_input
1586 && ! read_socket_hook && EQ (Vwindow_system, Qnil))
1588 old_fcntl_owner = fcntl (input_fd, F_GETOWN, 0);
1589 fcntl (input_fd, F_SETOWN, getpid ());
1590 init_sigio (input_fd);
1592 #endif /* F_GETOWN */
1593 #endif /* F_SETOWN_BUG */
1594 #endif /* F_SETFL */
1596 #ifdef BSD4_1
1597 if (interrupt_input)
1598 init_sigio (input_fd);
1599 #endif
1601 #ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1602 #undef _IOFBF
1603 #endif
1604 #ifdef _IOFBF
1605 /* This symbol is defined on recent USG systems.
1606 Someone says without this call USG won't really buffer the file
1607 even with a call to setbuf. */
1608 setvbuf (stdout, (char *) _sobuf, _IOFBF, sizeof _sobuf);
1609 #else
1610 setbuf (stdout, (char *) _sobuf);
1611 #endif
1612 #ifdef HAVE_WINDOW_SYSTEM
1613 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1614 needs the initialization code below. */
1615 if (EQ (Vwindow_system, Qnil)
1616 #ifndef WINDOWSNT
1617 /* When running in tty mode on NT/Win95, we have a read_socket
1618 hook, but still need the rest of the initialization code below. */
1619 && (! read_socket_hook)
1620 #endif
1622 #endif
1623 set_terminal_modes ();
1625 if (!term_initted
1626 && FRAMEP (Vterminal_frame)
1627 && FRAME_TERMCAP_P (XFRAME (Vterminal_frame)))
1628 init_frame_faces (XFRAME (Vterminal_frame));
1630 if (term_initted && no_redraw_on_reenter)
1632 if (display_completed)
1633 direct_output_forward_char (0);
1635 else
1637 frame_garbaged = 1;
1638 if (FRAMEP (Vterminal_frame))
1639 FRAME_GARBAGED_P (XFRAME (Vterminal_frame)) = 1;
1642 term_initted = 1;
1645 /* Return nonzero if safe to use tabs in output.
1646 At the time this is called, init_sys_modes has not been done yet. */
1649 tabs_safe_p ()
1651 struct emacs_tty tty;
1653 EMACS_GET_TTY (input_fd, &tty);
1654 return EMACS_TTY_TABS_OK (&tty);
1657 /* Get terminal size from system.
1658 Store number of lines into *HEIGHTP and width into *WIDTHP.
1659 We store 0 if there's no valid information. */
1661 void
1662 get_frame_size (widthp, heightp)
1663 int *widthp, *heightp;
1666 #ifdef TIOCGWINSZ
1668 /* BSD-style. */
1669 struct winsize size;
1671 if (ioctl (input_fd, TIOCGWINSZ, &size) == -1)
1672 *widthp = *heightp = 0;
1673 else
1675 *widthp = size.ws_col;
1676 *heightp = size.ws_row;
1679 #else
1680 #ifdef TIOCGSIZE
1682 /* SunOS - style. */
1683 struct ttysize size;
1685 if (ioctl (input_fd, TIOCGSIZE, &size) == -1)
1686 *widthp = *heightp = 0;
1687 else
1689 *widthp = size.ts_cols;
1690 *heightp = size.ts_lines;
1693 #else
1694 #ifdef VMS
1696 struct sensemode tty;
1698 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &tty, 0, 0,
1699 &tty.class, 12, 0, 0, 0, 0);
1700 *widthp = tty.scr_wid;
1701 *heightp = tty.scr_len;
1703 #else
1704 #ifdef MSDOS
1705 *widthp = ScreenCols ();
1706 *heightp = ScreenRows ();
1707 #else /* system doesn't know size */
1708 *widthp = 0;
1709 *heightp = 0;
1710 #endif
1712 #endif /* not VMS */
1713 #endif /* not SunOS-style */
1714 #endif /* not BSD-style */
1717 /* Set the logical window size associated with descriptor FD
1718 to HEIGHT and WIDTH. This is used mainly with ptys. */
1721 set_window_size (fd, height, width)
1722 int fd, height, width;
1724 #ifdef TIOCSWINSZ
1726 /* BSD-style. */
1727 struct winsize size;
1728 size.ws_row = height;
1729 size.ws_col = width;
1731 if (ioctl (fd, TIOCSWINSZ, &size) == -1)
1732 return 0; /* error */
1733 else
1734 return 1;
1736 #else
1737 #ifdef TIOCSSIZE
1739 /* SunOS - style. */
1740 struct ttysize size;
1741 size.ts_lines = height;
1742 size.ts_cols = width;
1744 if (ioctl (fd, TIOCGSIZE, &size) == -1)
1745 return 0;
1746 else
1747 return 1;
1748 #else
1749 return -1;
1750 #endif /* not SunOS-style */
1751 #endif /* not BSD-style */
1755 /* Prepare the terminal for exiting Emacs; move the cursor to the
1756 bottom of the frame, turn off interrupt-driven I/O, etc. */
1757 void
1758 reset_sys_modes ()
1760 struct frame *sf;
1762 if (noninteractive)
1764 fflush (stdout);
1765 return;
1767 if (!term_initted)
1768 return;
1769 #ifdef HAVE_WINDOW_SYSTEM
1770 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1771 needs the clean-up code below. */
1772 if (!EQ (Vwindow_system, Qnil)
1773 #ifndef WINDOWSNT
1774 /* When running in tty mode on NT/Win95, we have a read_socket
1775 hook, but still need the rest of the clean-up code below. */
1776 || read_socket_hook
1777 #endif
1779 return;
1780 #endif
1781 sf = SELECTED_FRAME ();
1782 cursor_to (FRAME_HEIGHT (sf) - 1, 0);
1783 clear_end_of_line (FRAME_WIDTH (sf));
1784 /* clear_end_of_line may move the cursor */
1785 cursor_to (FRAME_HEIGHT (sf) - 1, 0);
1786 #if defined (IBMR2AIX) && defined (AIXHFT)
1788 /* HFT devices normally use ^J as a LF/CR. We forced it to
1789 do the LF only. Now, we need to reset it. */
1790 struct termio tty;
1792 if (ioctl (1, HFTGETID, &tty) != -1)
1793 write (1, "\033[20h", 5);
1795 #endif
1797 reset_terminal_modes ();
1798 fflush (stdout);
1799 #ifdef BSD_SYSTEM
1800 #ifndef BSD4_1
1801 /* Avoid possible loss of output when changing terminal modes. */
1802 fsync (fileno (stdout));
1803 #endif
1804 #endif
1806 #ifdef F_SETFL
1807 #ifndef F_SETOWN_BUG
1808 #ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
1809 if (interrupt_input)
1811 reset_sigio ();
1812 fcntl (input_fd, F_SETOWN, old_fcntl_owner);
1814 #endif /* F_SETOWN */
1815 #endif /* F_SETOWN_BUG */
1816 #ifdef O_NDELAY
1817 fcntl (input_fd, F_SETFL, fcntl (input_fd, F_GETFL, 0) & ~O_NDELAY);
1818 #endif
1819 #endif /* F_SETFL */
1820 #ifdef BSD4_1
1821 if (interrupt_input)
1822 reset_sigio ();
1823 #endif /* BSD4_1 */
1825 if (old_tty_valid)
1826 while (EMACS_SET_TTY (input_fd, &old_tty, 0) < 0 && errno == EINTR)
1829 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1830 dos_ttcooked ();
1831 #endif
1833 #ifdef SET_LINE_DISCIPLINE
1834 /* Ultrix's termios *ignores* any line discipline except TERMIODISC.
1835 A different old line discipline is therefore not restored, yet.
1836 Restore the old line discipline by hand. */
1837 ioctl (0, TIOCSETD, &old_tty.main.c_line);
1838 #endif
1840 #ifdef AIXHFT
1841 hft_reset ();
1842 #endif
1844 #ifdef BSD_PGRPS
1845 widen_foreground_group ();
1846 #endif
1849 #ifdef HAVE_PTYS
1851 /* Set up the proper status flags for use of a pty. */
1853 void
1854 setup_pty (fd)
1855 int fd;
1857 /* I'm told that TOICREMOTE does not mean control chars
1858 "can't be sent" but rather that they don't have
1859 input-editing or signaling effects.
1860 That should be good, because we have other ways
1861 to do those things in Emacs.
1862 However, telnet mode seems not to work on 4.2.
1863 So TIOCREMOTE is turned off now. */
1865 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1866 will hang. In particular, the "timeout" feature (which
1867 causes a read to return if there is no data available)
1868 does this. Also it is known that telnet mode will hang
1869 in such a way that Emacs must be stopped (perhaps this
1870 is the same problem).
1872 If TIOCREMOTE is turned off, then there is a bug in
1873 hp-ux which sometimes loses data. Apparently the
1874 code which blocks the master process when the internal
1875 buffer fills up does not work. Other than this,
1876 though, everything else seems to work fine.
1878 Since the latter lossage is more benign, we may as well
1879 lose that way. -- cph */
1880 #ifdef FIONBIO
1881 #if defined(SYSV_PTYS) || defined(UNIX98_PTYS)
1883 int on = 1;
1884 ioctl (fd, FIONBIO, &on);
1886 #endif
1887 #endif
1888 #ifdef IBMRTAIX
1889 /* On AIX, the parent gets SIGHUP when a pty attached child dies. So, we */
1890 /* ignore SIGHUP once we've started a child on a pty. Note that this may */
1891 /* cause EMACS not to die when it should, i.e., when its own controlling */
1892 /* tty goes away. I've complained to the AIX developers, and they may */
1893 /* change this behavior, but I'm not going to hold my breath. */
1894 signal (SIGHUP, SIG_IGN);
1895 #endif
1897 #endif /* HAVE_PTYS */
1899 #ifdef VMS
1901 /* Assigning an input channel is done at the start of Emacs execution.
1902 This is called each time Emacs is resumed, also, but does nothing
1903 because input_chain is no longer zero. */
1905 void
1906 init_vms_input ()
1908 int status;
1910 if (input_fd == 0)
1912 status = SYS$ASSIGN (&input_dsc, &input_fd, 0, 0);
1913 if (! (status & 1))
1914 LIB$STOP (status);
1918 /* Deassigning the input channel is done before exiting. */
1920 void
1921 stop_vms_input ()
1923 return SYS$DASSGN (input_fd);
1926 short input_buffer;
1928 /* Request reading one character into the keyboard buffer.
1929 This is done as soon as the buffer becomes empty. */
1931 void
1932 queue_kbd_input ()
1934 int status;
1935 extern kbd_input_ast ();
1937 waiting_for_ast = 0;
1938 stop_input = 0;
1939 status = SYS$QIO (0, input_fd, IO$_READVBLK,
1940 &input_iosb, kbd_input_ast, 1,
1941 &input_buffer, 1, 0, terminator_mask, 0, 0);
1944 int input_count;
1946 /* Ast routine that is called when keyboard input comes in
1947 in accord with the SYS$QIO above. */
1949 void
1950 kbd_input_ast ()
1952 register int c = -1;
1953 int old_errno = errno;
1954 extern EMACS_TIME *input_available_clear_time;
1956 if (waiting_for_ast)
1957 SYS$SETEF (input_ef);
1958 waiting_for_ast = 0;
1959 input_count++;
1960 #ifdef ASTDEBUG
1961 if (input_count == 25)
1962 exit (1);
1963 printf ("Ast # %d,", input_count);
1964 printf (" iosb = %x, %x, %x, %x",
1965 input_iosb.offset, input_iosb.status, input_iosb.termlen,
1966 input_iosb.term);
1967 #endif
1968 if (input_iosb.offset)
1970 c = input_buffer;
1971 #ifdef ASTDEBUG
1972 printf (", char = 0%o", c);
1973 #endif
1975 #ifdef ASTDEBUG
1976 printf ("\n");
1977 fflush (stdout);
1978 sleep (1);
1979 #endif
1980 if (! stop_input)
1981 queue_kbd_input ();
1982 if (c >= 0)
1984 struct input_event e;
1985 e.kind = ascii_keystroke;
1986 XSETINT (e.code, c);
1987 e.frame_or_window = selected_frame;
1988 kbd_buffer_store_event (&e);
1990 if (input_available_clear_time)
1991 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
1992 errno = old_errno;
1995 /* Wait until there is something in kbd_buffer. */
1997 void
1998 wait_for_kbd_input ()
2000 extern int have_process_input, process_exited;
2002 /* If already something, avoid doing system calls. */
2003 if (detect_input_pending ())
2005 return;
2007 /* Clear a flag, and tell ast routine above to set it. */
2008 SYS$CLREF (input_ef);
2009 waiting_for_ast = 1;
2010 /* Check for timing error: ast happened while we were doing that. */
2011 if (!detect_input_pending ())
2013 /* No timing error: wait for flag to be set. */
2014 set_waiting_for_input (0);
2015 SYS$WFLOR (input_ef, input_eflist);
2016 clear_waiting_for_input (0);
2017 if (!detect_input_pending ())
2018 /* Check for subprocess input availability */
2020 int dsp = have_process_input || process_exited;
2022 SYS$CLREF (process_ef);
2023 if (have_process_input)
2024 process_command_input ();
2025 if (process_exited)
2026 process_exit ();
2027 if (dsp)
2029 update_mode_lines++;
2030 prepare_menu_bars ();
2031 redisplay_preserve_echo_area ();
2035 waiting_for_ast = 0;
2038 /* Get rid of any pending QIO, when we are about to suspend
2039 or when we want to throw away pending input.
2040 We wait for a positive sign that the AST routine has run
2041 and therefore there is no I/O request queued when we return.
2042 SYS$SETAST is used to avoid a timing error. */
2044 void
2045 end_kbd_input ()
2047 #ifdef ASTDEBUG
2048 printf ("At end_kbd_input.\n");
2049 fflush (stdout);
2050 sleep (1);
2051 #endif
2052 if (LIB$AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
2054 SYS$CANCEL (input_fd);
2055 return;
2058 SYS$SETAST (0);
2059 /* Clear a flag, and tell ast routine above to set it. */
2060 SYS$CLREF (input_ef);
2061 waiting_for_ast = 1;
2062 stop_input = 1;
2063 SYS$CANCEL (input_fd);
2064 SYS$SETAST (1);
2065 SYS$WAITFR (input_ef);
2066 waiting_for_ast = 0;
2069 /* Wait for either input available or time interval expiry. */
2071 void
2072 input_wait_timeout (timeval)
2073 int timeval; /* Time to wait, in seconds */
2075 int time [2];
2076 static int zero = 0;
2077 static int large = -10000000;
2079 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
2081 /* If already something, avoid doing system calls. */
2082 if (detect_input_pending ())
2084 return;
2086 /* Clear a flag, and tell ast routine above to set it. */
2087 SYS$CLREF (input_ef);
2088 waiting_for_ast = 1;
2089 /* Check for timing error: ast happened while we were doing that. */
2090 if (!detect_input_pending ())
2092 /* No timing error: wait for flag to be set. */
2093 SYS$CANTIM (1, 0);
2094 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
2095 SYS$WFLOR (timer_ef, timer_eflist); /* Wait for timer expiry or input */
2097 waiting_for_ast = 0;
2100 /* The standard `sleep' routine works some other way
2101 and it stops working if you have ever quit out of it.
2102 This one continues to work. */
2104 sys_sleep (timeval)
2105 int timeval;
2107 int time [2];
2108 static int zero = 0;
2109 static int large = -10000000;
2111 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
2113 SYS$CANTIM (1, 0);
2114 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
2115 SYS$WAITFR (timer_ef); /* Wait for timer expiry only */
2118 void
2119 init_sigio (fd)
2120 int fd;
2122 request_sigio ();
2125 reset_sigio ()
2127 unrequest_sigio ();
2130 void
2131 request_sigio ()
2133 croak ("request sigio");
2136 void
2137 unrequest_sigio ()
2139 croak ("unrequest sigio");
2142 #endif /* VMS */
2144 /* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
2145 #ifndef CANNOT_DUMP
2146 #define NEED_STARTS
2147 #endif
2149 #ifndef SYSTEM_MALLOC
2150 #ifndef NEED_STARTS
2151 #define NEED_STARTS
2152 #endif
2153 #endif
2155 #ifdef NEED_STARTS
2156 /* Some systems that cannot dump also cannot implement these. */
2159 * Return the address of the start of the text segment prior to
2160 * doing an unexec. After unexec the return value is undefined.
2161 * See crt0.c for further explanation and _start.
2165 #if !(defined (__NetBSD__) && defined (__ELF__))
2166 #ifndef HAVE_TEXT_START
2167 char *
2168 start_of_text ()
2170 #ifdef TEXT_START
2171 return ((char *) TEXT_START);
2172 #else
2173 #ifdef GOULD
2174 extern csrt ();
2175 return ((char *) csrt);
2176 #else /* not GOULD */
2177 extern int _start ();
2178 return ((char *) _start);
2179 #endif /* GOULD */
2180 #endif /* TEXT_START */
2182 #endif /* not HAVE_TEXT_START */
2183 #endif
2186 * Return the address of the start of the data segment prior to
2187 * doing an unexec. After unexec the return value is undefined.
2188 * See crt0.c for further information and definition of data_start.
2190 * Apparently, on BSD systems this is etext at startup. On
2191 * USG systems (swapping) this is highly mmu dependent and
2192 * is also dependent on whether or not the program is running
2193 * with shared text. Generally there is a (possibly large)
2194 * gap between end of text and start of data with shared text.
2196 * On Uniplus+ systems with shared text, data starts at a
2197 * fixed address. Each port (from a given oem) is generally
2198 * different, and the specific value of the start of data can
2199 * be obtained via the UniPlus+ specific "uvar" system call,
2200 * however the method outlined in crt0.c seems to be more portable.
2202 * Probably what will have to happen when a USG unexec is available,
2203 * at least on UniPlus, is temacs will have to be made unshared so
2204 * that text and data are contiguous. Then once loadup is complete,
2205 * unexec will produce a shared executable where the data can be
2206 * at the normal shared text boundary and the startofdata variable
2207 * will be patched by unexec to the correct value.
2211 char *
2212 start_of_data ()
2214 #ifdef DATA_START
2215 return ((char *) DATA_START);
2216 #else
2217 #ifdef ORDINARY_LINK
2219 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
2220 * data_start isn't defined. We take the address of environ, which
2221 * is known to live at or near the start of the system crt0.c, and
2222 * we don't sweat the handful of bytes that might lose.
2224 extern char **environ;
2226 return ((char *) &environ);
2227 #else
2228 extern int data_start;
2229 return ((char *) &data_start);
2230 #endif /* ORDINARY_LINK */
2231 #endif /* DATA_START */
2233 #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
2235 #ifndef CANNOT_DUMP
2236 /* Some systems that cannot dump also cannot implement these. */
2239 * Return the address of the end of the text segment prior to
2240 * doing an unexec. After unexec the return value is undefined.
2243 char *
2244 end_of_text ()
2246 #ifdef TEXT_END
2247 return ((char *) TEXT_END);
2248 #else
2249 extern int etext;
2250 return ((char *) &etext);
2251 #endif
2255 * Return the address of the end of the data segment prior to
2256 * doing an unexec. After unexec the return value is undefined.
2259 char *
2260 end_of_data ()
2262 #ifdef DATA_END
2263 return ((char *) DATA_END);
2264 #else
2265 extern int edata;
2266 return ((char *) &edata);
2267 #endif
2270 #endif /* not CANNOT_DUMP */
2272 /* init_system_name sets up the string for the Lisp function
2273 system-name to return. */
2275 #ifdef BSD4_1
2276 #include <whoami.h>
2277 #endif
2279 extern Lisp_Object Vsystem_name;
2281 #ifndef BSD4_1
2282 #ifndef VMS
2283 #ifdef HAVE_SOCKETS
2284 #include <sys/socket.h>
2285 #include <netdb.h>
2286 #endif /* HAVE_SOCKETS */
2287 #endif /* not VMS */
2288 #endif /* not BSD4_1 */
2290 #ifdef TRY_AGAIN
2291 #ifndef HAVE_H_ERRNO
2292 extern int h_errno;
2293 #endif
2294 #endif /* TRY_AGAIN */
2296 void
2297 init_system_name ()
2299 #ifdef BSD4_1
2300 Vsystem_name = build_string (sysname);
2301 #else
2302 #ifdef VMS
2303 char *sp, *end;
2304 if ((sp = egetenv ("SYS$NODE")) == 0)
2305 Vsystem_name = build_string ("vax-vms");
2306 else if ((end = index (sp, ':')) == 0)
2307 Vsystem_name = build_string (sp);
2308 else
2309 Vsystem_name = make_string (sp, end - sp);
2310 #else
2311 #ifndef HAVE_GETHOSTNAME
2312 struct utsname uts;
2313 uname (&uts);
2314 Vsystem_name = build_string (uts.nodename);
2315 #else /* HAVE_GETHOSTNAME */
2316 unsigned int hostname_size = 256;
2317 char *hostname = (char *) alloca (hostname_size);
2319 /* Try to get the host name; if the buffer is too short, try
2320 again. Apparently, the only indication gethostname gives of
2321 whether the buffer was large enough is the presence or absence
2322 of a '\0' in the string. Eech. */
2323 for (;;)
2325 gethostname (hostname, hostname_size - 1);
2326 hostname[hostname_size - 1] = '\0';
2328 /* Was the buffer large enough for the '\0'? */
2329 if (strlen (hostname) < hostname_size - 1)
2330 break;
2332 hostname_size <<= 1;
2333 hostname = (char *) alloca (hostname_size);
2335 #ifdef HAVE_SOCKETS
2336 /* Turn the hostname into the official, fully-qualified hostname.
2337 Don't do this if we're going to dump; this can confuse system
2338 libraries on some machines and make the dumped emacs core dump. */
2339 #ifndef CANNOT_DUMP
2340 if (initialized)
2341 #endif /* not CANNOT_DUMP */
2342 if (! index (hostname, '.'))
2344 struct hostent *hp;
2345 int count;
2346 for (count = 0;; count++)
2348 #ifdef TRY_AGAIN
2349 h_errno = 0;
2350 #endif
2351 hp = gethostbyname (hostname);
2352 #ifdef TRY_AGAIN
2353 if (! (hp == 0 && h_errno == TRY_AGAIN))
2354 #endif
2355 break;
2356 if (count >= 5)
2357 break;
2358 Fsleep_for (make_number (1), Qnil);
2360 if (hp)
2362 char *fqdn = (char *) hp->h_name;
2363 char *p;
2365 if (!index (fqdn, '.'))
2367 /* We still don't have a fully qualified domain name.
2368 Try to find one in the list of alternate names */
2369 char **alias = hp->h_aliases;
2370 while (*alias && !index (*alias, '.'))
2371 alias++;
2372 if (*alias)
2373 fqdn = *alias;
2375 hostname = fqdn;
2376 #if 0
2377 /* Convert the host name to lower case. */
2378 /* Using ctype.h here would introduce a possible locale
2379 dependence that is probably wrong for hostnames. */
2380 p = hostname;
2381 while (*p)
2383 if (*p >= 'A' && *p <= 'Z')
2384 *p += 'a' - 'A';
2385 p++;
2387 #endif
2390 #endif /* HAVE_SOCKETS */
2391 /* We used to try using getdomainname here,
2392 but NIIBE Yutaka <gniibe@etl.go.jp> says that
2393 getdomainname gets the NIS/YP domain which often is not the same
2394 as in Internet domain name. */
2395 #if 0 /* Turned off because sysinfo is not really likely to return the
2396 correct Internet domain. */
2397 #if (HAVE_SYSINFO && defined (SI_SRPC_DOMAIN))
2398 if (! index (hostname, '.'))
2400 /* The hostname is not fully qualified. Append the domain name. */
2402 int hostlen = strlen (hostname);
2403 int domain_size = 256;
2405 for (;;)
2407 char *domain = (char *) alloca (domain_size + 1);
2408 char *fqdn = (char *) alloca (hostlen + 1 + domain_size + 1);
2409 int sys_domain_size = sysinfo (SI_SRPC_DOMAIN, domain, domain_size);
2410 if (sys_domain_size <= 0)
2411 break;
2412 if (domain_size < sys_domain_size)
2414 domain_size = sys_domain_size;
2415 continue;
2417 strcpy (fqdn, hostname);
2418 if (domain[0] == '.')
2419 strcpy (fqdn + hostlen, domain);
2420 else if (domain[0] != 0)
2422 fqdn[hostlen] = '.';
2423 strcpy (fqdn + hostlen + 1, domain);
2425 hostname = fqdn;
2426 break;
2429 #endif /* HAVE_SYSINFO && defined (SI_SRPC_DOMAIN) */
2430 #endif /* 0 */
2431 Vsystem_name = build_string (hostname);
2432 #endif /* HAVE_GETHOSTNAME */
2433 #endif /* VMS */
2434 #endif /* BSD4_1 */
2436 unsigned char *p;
2437 for (p = XSTRING (Vsystem_name)->data; *p; p++)
2438 if (*p == ' ' || *p == '\t')
2439 *p = '-';
2443 #ifndef MSDOS
2444 #ifndef VMS
2445 #if !defined (HAVE_SELECT) || defined (BROKEN_SELECT_NON_X)
2447 #include "sysselect.h"
2448 #undef select
2450 #if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
2451 /* Cause explanatory error message at compile time,
2452 since the select emulation is not good enough for X. */
2453 int *x = &x_windows_lose_if_no_select_system_call;
2454 #endif
2456 /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2457 * Only checks read descriptors.
2459 /* How long to wait between checking fds in select */
2460 #define SELECT_PAUSE 1
2461 int select_alarmed;
2463 /* For longjmp'ing back to read_input_waiting. */
2465 jmp_buf read_alarm_throw;
2467 /* Nonzero if the alarm signal should throw back to read_input_waiting.
2468 The read_socket_hook function sets this to 1 while it is waiting. */
2470 int read_alarm_should_throw;
2472 SIGTYPE
2473 select_alarm ()
2475 select_alarmed = 1;
2476 #ifdef BSD4_1
2477 sigrelse (SIGALRM);
2478 #else /* not BSD4_1 */
2479 signal (SIGALRM, SIG_IGN);
2480 #endif /* not BSD4_1 */
2481 if (read_alarm_should_throw)
2482 longjmp (read_alarm_throw, 1);
2485 #ifndef WINDOWSNT
2486 /* Only rfds are checked. */
2488 sys_select (nfds, rfds, wfds, efds, timeout)
2489 int nfds;
2490 SELECT_TYPE *rfds, *wfds, *efds;
2491 EMACS_TIME *timeout;
2493 int ravail = 0;
2494 SELECT_TYPE orfds;
2495 int timeoutval;
2496 int *local_timeout;
2497 extern int proc_buffered_char[];
2498 #ifndef subprocesses
2499 int process_tick = 0, update_tick = 0;
2500 #else
2501 extern int process_tick, update_tick;
2502 #endif
2503 unsigned char buf;
2505 #if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
2506 /* If we're using X, then the native select will work; we only need the
2507 emulation for non-X usage. */
2508 if (!NILP (Vwindow_system))
2509 return select (nfds, rfds, wfds, efds, timeout);
2510 #endif
2511 timeoutval = timeout ? EMACS_SECS (*timeout) : 100000;
2512 local_timeout = &timeoutval;
2513 FD_ZERO (&orfds);
2514 if (rfds)
2516 orfds = *rfds;
2517 FD_ZERO (rfds);
2519 if (wfds)
2520 FD_ZERO (wfds);
2521 if (efds)
2522 FD_ZERO (efds);
2524 /* If we are looking only for the terminal, with no timeout,
2525 just read it and wait -- that's more efficient. */
2526 if (*local_timeout == 100000 && process_tick == update_tick
2527 && FD_ISSET (0, &orfds))
2529 int fd;
2530 for (fd = 1; fd < nfds; ++fd)
2531 if (FD_ISSET (fd, &orfds))
2532 goto hardway;
2533 if (! detect_input_pending ())
2534 read_input_waiting ();
2535 FD_SET (0, rfds);
2536 return 1;
2539 hardway:
2540 /* Once a second, till the timer expires, check all the flagged read
2541 * descriptors to see if any input is available. If there is some then
2542 * set the corresponding bit in the return copy of rfds.
2544 while (1)
2546 register int to_check, fd;
2548 if (rfds)
2550 for (to_check = nfds, fd = 0; --to_check >= 0; fd++)
2552 if (FD_ISSET (fd, &orfds))
2554 int avail = 0, status = 0;
2556 if (fd == 0)
2557 avail = detect_input_pending (); /* Special keyboard handler */
2558 else
2560 #ifdef FIONREAD
2561 status = ioctl (fd, FIONREAD, &avail);
2562 #else /* no FIONREAD */
2563 /* Hoping it will return -1 if nothing available
2564 or 0 if all 0 chars requested are read. */
2565 if (proc_buffered_char[fd] >= 0)
2566 avail = 1;
2567 else
2569 avail = read (fd, &buf, 1);
2570 if (avail > 0)
2571 proc_buffered_char[fd] = buf;
2573 #endif /* no FIONREAD */
2575 if (status >= 0 && avail > 0)
2577 FD_SET (fd, rfds);
2578 ravail++;
2583 if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
2584 break;
2586 turn_on_atimers (0);
2587 signal (SIGALRM, select_alarm);
2588 select_alarmed = 0;
2589 alarm (SELECT_PAUSE);
2591 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2592 while (select_alarmed == 0 && *local_timeout != 0
2593 && process_tick == update_tick)
2595 /* If we are interested in terminal input,
2596 wait by reading the terminal.
2597 That makes instant wakeup for terminal input at least. */
2598 if (FD_ISSET (0, &orfds))
2600 read_input_waiting ();
2601 if (detect_input_pending ())
2602 select_alarmed = 1;
2604 else
2605 pause ();
2607 (*local_timeout) -= SELECT_PAUSE;
2609 /* Reset the old alarm if there was one. */
2610 turn_on_atimers (1);
2612 if (*local_timeout == 0) /* Stop on timer being cleared */
2613 break;
2615 return ravail;
2617 #endif /* not WINDOWSNT */
2619 /* Read keyboard input into the standard buffer,
2620 waiting for at least one character. */
2622 /* Make all keyboard buffers much bigger when using a window system. */
2623 #ifdef HAVE_WINDOW_SYSTEM
2624 #define BUFFER_SIZE_FACTOR 16
2625 #else
2626 #define BUFFER_SIZE_FACTOR 1
2627 #endif
2629 void
2630 read_input_waiting ()
2632 struct input_event e;
2633 int nread, i;
2634 extern int quit_char;
2636 if (read_socket_hook)
2638 struct input_event buf[256];
2640 read_alarm_should_throw = 0;
2641 if (! setjmp (read_alarm_throw))
2642 nread = (*read_socket_hook) (0, buf, 256, 1);
2643 else
2644 nread = -1;
2646 /* Scan the chars for C-g and store them in kbd_buffer. */
2647 for (i = 0; i < nread; i++)
2649 kbd_buffer_store_event (&buf[i]);
2650 /* Don't look at input that follows a C-g too closely.
2651 This reduces lossage due to autorepeat on C-g. */
2652 if (buf[i].kind == ascii_keystroke
2653 && buf[i].code == quit_char)
2654 break;
2657 else
2659 char buf[3];
2660 nread = read (fileno (stdin), buf, 1);
2662 /* Scan the chars for C-g and store them in kbd_buffer. */
2663 e.kind = ascii_keystroke;
2664 e.frame_or_window = selected_frame;
2665 e.modifiers = 0;
2666 for (i = 0; i < nread; i++)
2668 /* Convert chars > 0177 to meta events if desired.
2669 We do this under the same conditions that read_avail_input does. */
2670 if (read_socket_hook == 0)
2672 /* If the user says she has a meta key, then believe her. */
2673 if (meta_key == 1 && (buf[i] & 0x80))
2674 e.modifiers = meta_modifier;
2675 if (meta_key != 2)
2676 buf[i] &= ~0x80;
2679 XSETINT (e.code, buf[i]);
2680 kbd_buffer_store_event (&e);
2681 /* Don't look at input that follows a C-g too closely.
2682 This reduces lossage due to autorepeat on C-g. */
2683 if (buf[i] == quit_char)
2684 break;
2689 #endif /* not HAVE_SELECT */
2690 #endif /* not VMS */
2691 #endif /* not MSDOS */
2693 #ifdef BSD4_1
2694 void
2695 init_sigio (fd)
2696 int fd;
2698 if (noninteractive)
2699 return;
2700 lmode = LINTRUP | lmode;
2701 ioctl (fd, TIOCLSET, &lmode);
2704 void
2705 reset_sigio ()
2707 if (noninteractive)
2708 return;
2709 lmode = ~LINTRUP & lmode;
2710 ioctl (0, TIOCLSET, &lmode);
2713 void
2714 request_sigio ()
2716 sigrelse (SIGTINT);
2718 interrupts_deferred = 0;
2721 void
2722 unrequest_sigio ()
2724 sighold (SIGTINT);
2726 interrupts_deferred = 1;
2729 /* still inside #ifdef BSD4_1 */
2730 #ifdef subprocesses
2732 int sigheld; /* Mask of held signals */
2734 void
2735 sigholdx (signum)
2736 int signum;
2738 sigheld |= sigbit (signum);
2739 sighold (signum);
2742 void
2743 sigisheld (signum)
2744 int signum;
2746 sigheld |= sigbit (signum);
2749 void
2750 sigunhold (signum)
2751 int signum;
2753 sigheld &= ~sigbit (signum);
2754 sigrelse (signum);
2757 void
2758 sigfree () /* Free all held signals */
2760 int i;
2761 for (i = 0; i < NSIG; i++)
2762 if (sigheld & sigbit (i))
2763 sigrelse (i);
2764 sigheld = 0;
2768 sigbit (i)
2770 return 1 << (i - 1);
2772 #endif /* subprocesses */
2773 #endif /* BSD4_1 */
2775 /* POSIX signals support - DJB */
2776 /* Anyone with POSIX signals should have ANSI C declarations */
2778 #ifdef POSIX_SIGNALS
2780 sigset_t empty_mask, full_mask;
2782 signal_handler_t
2783 sys_signal (int signal_number, signal_handler_t action)
2785 struct sigaction new_action, old_action;
2786 sigemptyset (&new_action.sa_mask);
2787 new_action.sa_handler = action;
2788 #ifdef SA_RESTART
2789 /* Emacs mostly works better with restartable system services. If this
2790 * flag exists, we probably want to turn it on here.
2792 new_action.sa_flags = SA_RESTART;
2793 #else
2794 new_action.sa_flags = 0;
2795 #endif
2796 sigaction (signal_number, &new_action, &old_action);
2797 return (old_action.sa_handler);
2800 #ifndef __GNUC__
2801 /* If we're compiling with GCC, we don't need this function, since it
2802 can be written as a macro. */
2803 sigset_t
2804 sys_sigmask (int sig)
2806 sigset_t mask;
2807 sigemptyset (&mask);
2808 sigaddset (&mask, sig);
2809 return mask;
2811 #endif
2813 /* I'd like to have these guys return pointers to the mask storage in here,
2814 but there'd be trouble if the code was saving multiple masks. I'll be
2815 safe and pass the structure. It normally won't be more than 2 bytes
2816 anyhow. - DJB */
2818 sigset_t
2819 sys_sigblock (sigset_t new_mask)
2821 sigset_t old_mask;
2822 sigprocmask (SIG_BLOCK, &new_mask, &old_mask);
2823 return (old_mask);
2826 sigset_t
2827 sys_sigunblock (sigset_t new_mask)
2829 sigset_t old_mask;
2830 sigprocmask (SIG_UNBLOCK, &new_mask, &old_mask);
2831 return (old_mask);
2834 sigset_t
2835 sys_sigsetmask (sigset_t new_mask)
2837 sigset_t old_mask;
2838 sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
2839 return (old_mask);
2842 #endif /* POSIX_SIGNALS */
2844 #if !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED
2845 static char *my_sys_siglist[NSIG];
2846 # ifdef sys_siglist
2847 # undef sys_siglist
2848 # endif
2849 # define sys_siglist my_sys_siglist
2850 #endif
2852 void
2853 init_signals ()
2855 #ifdef POSIX_SIGNALS
2856 sigemptyset (&empty_mask);
2857 sigfillset (&full_mask);
2858 #endif
2860 #if !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED
2861 if (! initialized)
2863 # ifdef SIGABRT
2864 sys_siglist[SIGABRT] = "Aborted";
2865 # endif
2866 # ifdef SIGAIO
2867 sys_siglist[SIGAIO] = "LAN I/O interrupt";
2868 # endif
2869 # ifdef SIGALRM
2870 sys_siglist[SIGALRM] = "Alarm clock";
2871 # endif
2872 # ifdef SIGBUS
2873 sys_siglist[SIGBUS] = "Bus error";
2874 # endif
2875 # ifdef SIGCLD
2876 sys_siglist[SIGCLD] = "Child status changed";
2877 # endif
2878 # ifdef SIGCHLD
2879 sys_siglist[SIGCHLD] = "Child status changed";
2880 # endif
2881 # ifdef SIGCONT
2882 sys_siglist[SIGCONT] = "Continued";
2883 # endif
2884 # ifdef SIGDANGER
2885 sys_siglist[SIGDANGER] = "Swap space dangerously low";
2886 # endif
2887 # ifdef SIGDGNOTIFY
2888 sys_siglist[SIGDGNOTIFY] = "Notification message in queue";
2889 # endif
2890 # ifdef SIGEMT
2891 sys_siglist[SIGEMT] = "Emulation trap";
2892 # endif
2893 # ifdef SIGFPE
2894 sys_siglist[SIGFPE] = "Arithmetic exception";
2895 # endif
2896 # ifdef SIGFREEZE
2897 sys_siglist[SIGFREEZE] = "SIGFREEZE";
2898 # endif
2899 # ifdef SIGGRANT
2900 sys_siglist[SIGGRANT] = "Monitor mode granted";
2901 # endif
2902 # ifdef SIGHUP
2903 sys_siglist[SIGHUP] = "Hangup";
2904 # endif
2905 # ifdef SIGILL
2906 sys_siglist[SIGILL] = "Illegal instruction";
2907 # endif
2908 # ifdef SIGINT
2909 sys_siglist[SIGINT] = "Interrupt";
2910 # endif
2911 # ifdef SIGIO
2912 sys_siglist[SIGIO] = "I/O possible";
2913 # endif
2914 # ifdef SIGIOINT
2915 sys_siglist[SIGIOINT] = "I/O intervention required";
2916 # endif
2917 # ifdef SIGIOT
2918 sys_siglist[SIGIOT] = "IOT trap";
2919 # endif
2920 # ifdef SIGKILL
2921 sys_siglist[SIGKILL] = "Killed";
2922 # endif
2923 # ifdef SIGLOST
2924 sys_siglist[SIGLOST] = "Resource lost";
2925 # endif
2926 # ifdef SIGLWP
2927 sys_siglist[SIGLWP] = "SIGLWP";
2928 # endif
2929 # ifdef SIGMSG
2930 sys_siglist[SIGMSG] = "Monitor mode data available";
2931 # endif
2932 # ifdef SIGPHONE
2933 sys_siglist[SIGWIND] = "SIGPHONE";
2934 # endif
2935 # ifdef SIGPIPE
2936 sys_siglist[SIGPIPE] = "Broken pipe";
2937 # endif
2938 # ifdef SIGPOLL
2939 sys_siglist[SIGPOLL] = "Pollable event occurred";
2940 # endif
2941 # ifdef SIGPROF
2942 sys_siglist[SIGPROF] = "Profiling timer expired";
2943 # endif
2944 # ifdef SIGPTY
2945 sys_siglist[SIGPTY] = "PTY I/O interrupt";
2946 # endif
2947 # ifdef SIGPWR
2948 sys_siglist[SIGPWR] = "Power-fail restart";
2949 # endif
2950 # ifdef SIGQUIT
2951 sys_siglist[SIGQUIT] = "Quit";
2952 # endif
2953 # ifdef SIGRETRACT
2954 sys_siglist[SIGRETRACT] = "Need to relinguish monitor mode";
2955 # endif
2956 # ifdef SIGSAK
2957 sys_siglist[SIGSAK] = "Secure attention";
2958 # endif
2959 # ifdef SIGSEGV
2960 sys_siglist[SIGSEGV] = "Segmentation violation";
2961 # endif
2962 # ifdef SIGSOUND
2963 sys_siglist[SIGSOUND] = "Sound completed";
2964 # endif
2965 # ifdef SIGSTOP
2966 sys_siglist[SIGSTOP] = "Stopped (signal)";
2967 # endif
2968 # ifdef SIGSTP
2969 sys_siglist[SIGSTP] = "Stopped (user)";
2970 # endif
2971 # ifdef SIGSYS
2972 sys_siglist[SIGSYS] = "Bad argument to system call";
2973 # endif
2974 # ifdef SIGTERM
2975 sys_siglist[SIGTERM] = "Terminated";
2976 # endif
2977 # ifdef SIGTHAW
2978 sys_siglist[SIGTHAW] = "SIGTHAW";
2979 # endif
2980 # ifdef SIGTRAP
2981 sys_siglist[SIGTRAP] = "Trace/breakpoint trap";
2982 # endif
2983 # ifdef SIGTSTP
2984 sys_siglist[SIGTSTP] = "Stopped (user)";
2985 # endif
2986 # ifdef SIGTTIN
2987 sys_siglist[SIGTTIN] = "Stopped (tty input)";
2988 # endif
2989 # ifdef SIGTTOU
2990 sys_siglist[SIGTTOU] = "Stopped (tty output)";
2991 # endif
2992 # ifdef SIGURG
2993 sys_siglist[SIGURG] = "Urgent I/O condition";
2994 # endif
2995 # ifdef SIGUSR1
2996 sys_siglist[SIGUSR1] = "User defined signal 1";
2997 # endif
2998 # ifdef SIGUSR2
2999 sys_siglist[SIGUSR2] = "User defined signal 2";
3000 # endif
3001 # ifdef SIGVTALRM
3002 sys_siglist[SIGVTALRM] = "Virtual timer expired";
3003 # endif
3004 # ifdef SIGWAITING
3005 sys_siglist[SIGWAITING] = "Process's LWPs are blocked";
3006 # endif
3007 # ifdef SIGWINCH
3008 sys_siglist[SIGWINCH] = "Window size changed";
3009 # endif
3010 # ifdef SIGWIND
3011 sys_siglist[SIGWIND] = "SIGWIND";
3012 # endif
3013 # ifdef SIGXCPU
3014 sys_siglist[SIGXCPU] = "CPU time limit exceeded";
3015 # endif
3016 # ifdef SIGXFSZ
3017 sys_siglist[SIGXFSZ] = "File size limit exceeded";
3018 # endif
3020 #endif /* !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED */
3023 #ifndef HAVE_RANDOM
3024 #ifdef random
3025 #define HAVE_RANDOM
3026 #endif
3027 #endif
3029 /* Figure out how many bits the system's random number generator uses.
3030 `random' and `lrand48' are assumed to return 31 usable bits.
3031 BSD `rand' returns a 31 bit value but the low order bits are unusable;
3032 so we'll shift it and treat it like the 15-bit USG `rand'. */
3034 #ifndef RAND_BITS
3035 # ifdef HAVE_RANDOM
3036 # define RAND_BITS 31
3037 # else /* !HAVE_RANDOM */
3038 # ifdef HAVE_LRAND48
3039 # define RAND_BITS 31
3040 # define random lrand48
3041 # else /* !HAVE_LRAND48 */
3042 # define RAND_BITS 15
3043 # if RAND_MAX == 32767
3044 # define random rand
3045 # else /* RAND_MAX != 32767 */
3046 # if RAND_MAX == 2147483647
3047 # define random() (rand () >> 16)
3048 # else /* RAND_MAX != 2147483647 */
3049 # ifdef USG
3050 # define random rand
3051 # else
3052 # define random() (rand () >> 16)
3053 # endif /* !USG */
3054 # endif /* RAND_MAX != 2147483647 */
3055 # endif /* RAND_MAX != 32767 */
3056 # endif /* !HAVE_LRAND48 */
3057 # endif /* !HAVE_RANDOM */
3058 #endif /* !RAND_BITS */
3060 void
3061 seed_random (arg)
3062 long arg;
3064 #ifdef HAVE_RANDOM
3065 srandom ((unsigned int)arg);
3066 #else
3067 # ifdef HAVE_LRAND48
3068 srand48 (arg);
3069 # else
3070 srand ((unsigned int)arg);
3071 # endif
3072 #endif
3076 * Build a full Emacs-sized word out of whatever we've got.
3077 * This suffices even for a 64-bit architecture with a 15-bit rand.
3079 long
3080 get_random ()
3082 long val = random ();
3083 #if VALBITS > RAND_BITS
3084 val = (val << RAND_BITS) ^ random ();
3085 #if VALBITS > 2*RAND_BITS
3086 val = (val << RAND_BITS) ^ random ();
3087 #if VALBITS > 3*RAND_BITS
3088 val = (val << RAND_BITS) ^ random ();
3089 #if VALBITS > 4*RAND_BITS
3090 val = (val << RAND_BITS) ^ random ();
3091 #endif /* need at least 5 */
3092 #endif /* need at least 4 */
3093 #endif /* need at least 3 */
3094 #endif /* need at least 2 */
3095 return val & ((1L << VALBITS) - 1);
3098 #ifdef WRONG_NAME_INSQUE
3100 insque (q,p)
3101 caddr_t q,p;
3103 _insque (q,p);
3106 #endif
3108 #ifdef VMS
3110 #ifdef getenv
3111 /* If any place else asks for the TERM variable,
3112 allow it to be overridden with the EMACS_TERM variable
3113 before attempting to translate the logical name TERM. As a last
3114 resort, ask for VAX C's special idea of the TERM variable. */
3115 #undef getenv
3116 char *
3117 sys_getenv (name)
3118 char *name;
3120 register char *val;
3121 static char buf[256];
3122 static struct dsc$descriptor_s equiv
3123 = {sizeof (buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf};
3124 static struct dsc$descriptor_s d_name
3125 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
3126 short eqlen;
3128 if (!strcmp (name, "TERM"))
3130 val = (char *) getenv ("EMACS_TERM");
3131 if (val)
3132 return val;
3135 d_name.dsc$w_length = strlen (name);
3136 d_name.dsc$a_pointer = name;
3137 if (LIB$SYS_TRNLOG (&d_name, &eqlen, &equiv) == 1)
3139 char *str = (char *) xmalloc (eqlen + 1);
3140 bcopy (buf, str, eqlen);
3141 str[eqlen] = '\0';
3142 /* This is a storage leak, but a pain to fix. With luck,
3143 no one will ever notice. */
3144 return str;
3146 return (char *) getenv (name);
3148 #endif /* getenv */
3150 #ifdef abort
3151 /* Since VMS doesn't believe in core dumps, the only way to debug this beast is
3152 to force a call on the debugger from within the image. */
3153 #undef abort
3154 sys_abort ()
3156 reset_sys_modes ();
3157 LIB$SIGNAL (SS$_DEBUG);
3159 #endif /* abort */
3160 #endif /* VMS */
3162 #ifdef VMS
3163 #ifdef LINK_CRTL_SHARE
3164 #ifdef SHARABLE_LIB_BUG
3165 /* Variables declared noshare and initialized in sharable libraries
3166 cannot be shared. The VMS linker incorrectly forces you to use a private
3167 version which is uninitialized... If not for this "feature", we
3168 could use the C library definition of sys_nerr and sys_errlist. */
3169 int sys_nerr = 35;
3170 char *sys_errlist[] =
3172 "error 0",
3173 "not owner",
3174 "no such file or directory",
3175 "no such process",
3176 "interrupted system call",
3177 "i/o error",
3178 "no such device or address",
3179 "argument list too long",
3180 "exec format error",
3181 "bad file number",
3182 "no child process",
3183 "no more processes",
3184 "not enough memory",
3185 "permission denied",
3186 "bad address",
3187 "block device required",
3188 "mount devices busy",
3189 "file exists",
3190 "cross-device link",
3191 "no such device",
3192 "not a directory",
3193 "is a directory",
3194 "invalid argument",
3195 "file table overflow",
3196 "too many open files",
3197 "not a typewriter",
3198 "text file busy",
3199 "file too big",
3200 "no space left on device",
3201 "illegal seek",
3202 "read-only file system",
3203 "too many links",
3204 "broken pipe",
3205 "math argument",
3206 "result too large",
3207 "I/O stream empty",
3208 "vax/vms specific error code nontranslatable error"
3210 #endif /* SHARABLE_LIB_BUG */
3211 #endif /* LINK_CRTL_SHARE */
3212 #endif /* VMS */
3214 #ifndef HAVE_STRERROR
3215 #ifndef WINDOWSNT
3216 char *
3217 strerror (errnum)
3218 int errnum;
3220 extern char *sys_errlist[];
3221 extern int sys_nerr;
3223 if (errnum >= 0 && errnum < sys_nerr)
3224 return sys_errlist[errnum];
3225 return (char *) "Unknown error";
3227 #endif /* not WINDOWSNT */
3228 #endif /* ! HAVE_STRERROR */
3231 emacs_open (path, oflag, mode)
3232 char *path;
3233 int oflag, mode;
3235 register int rtnval;
3237 #ifdef BSD4_1
3238 if (oflag & O_CREAT)
3239 return creat (path, mode);
3240 #endif
3242 while ((rtnval = open (path, oflag, mode)) == -1
3243 && (errno == EINTR));
3244 return (rtnval);
3248 emacs_close (fd)
3249 int fd;
3251 int did_retry = 0;
3252 register int rtnval;
3254 while ((rtnval = close (fd)) == -1
3255 && (errno == EINTR))
3256 did_retry = 1;
3258 /* If close is interrupted SunOS 4.1 may or may not have closed the
3259 file descriptor. If it did the second close will fail with
3260 errno = EBADF. That means we have succeeded. */
3261 if (rtnval == -1 && did_retry && errno == EBADF)
3262 return 0;
3264 return rtnval;
3268 emacs_read (fildes, buf, nbyte)
3269 int fildes;
3270 char *buf;
3271 unsigned int nbyte;
3273 register int rtnval;
3275 while ((rtnval = read (fildes, buf, nbyte)) == -1
3276 && (errno == EINTR));
3277 return (rtnval);
3281 emacs_write (fildes, buf, nbyte)
3282 int fildes;
3283 char *buf;
3284 unsigned int nbyte;
3286 register int rtnval, bytes_written;
3288 bytes_written = 0;
3290 while (nbyte > 0)
3292 rtnval = write (fildes, buf, nbyte);
3294 if (rtnval == -1)
3296 if (errno == EINTR)
3297 continue;
3298 else
3299 return (bytes_written ? bytes_written : -1);
3302 buf += rtnval;
3303 nbyte -= rtnval;
3304 bytes_written += rtnval;
3306 return (bytes_written);
3309 #ifdef USG
3311 * All of the following are for USG.
3313 * On USG systems the system calls are INTERRUPTIBLE by signals
3314 * that the user program has elected to catch. Thus the system call
3315 * must be retried in these cases. To handle this without massive
3316 * changes in the source code, we remap the standard system call names
3317 * to names for our own functions in sysdep.c that do the system call
3318 * with retries. Actually, for portability reasons, it is good
3319 * programming practice, as this example shows, to limit all actual
3320 * system calls to a single occurrence in the source. Sure, this
3321 * adds an extra level of function call overhead but it is almost
3322 * always negligible. Fred Fish, Unisoft Systems Inc.
3326 * Warning, this function may not duplicate 4.2 action properly
3327 * under error conditions.
3330 #ifndef MAXPATHLEN
3331 /* In 4.1, param.h fails to define this. */
3332 #define MAXPATHLEN 1024
3333 #endif
3335 #ifndef HAVE_GETWD
3337 char *
3338 getwd (pathname)
3339 char *pathname;
3341 char *npath, *spath;
3342 extern char *getcwd ();
3344 BLOCK_INPUT; /* getcwd uses malloc */
3345 spath = npath = getcwd ((char *) 0, MAXPATHLEN);
3346 if (spath == 0)
3347 return spath;
3348 /* On Altos 3068, getcwd can return @hostname/dir, so discard
3349 up to first slash. Should be harmless on other systems. */
3350 while (*npath && *npath != '/')
3351 npath++;
3352 strcpy (pathname, npath);
3353 free (spath); /* getcwd uses malloc */
3354 UNBLOCK_INPUT;
3355 return pathname;
3358 #endif /* HAVE_GETWD */
3361 * Emulate rename using unlink/link. Note that this is
3362 * only partially correct. Also, doesn't enforce restriction
3363 * that files be of same type (regular->regular, dir->dir, etc).
3366 #ifndef HAVE_RENAME
3368 rename (from, to)
3369 const char *from;
3370 const char *to;
3372 if (access (from, 0) == 0)
3374 unlink (to);
3375 if (link (from, to) == 0)
3376 if (unlink (from) == 0)
3377 return (0);
3379 return (-1);
3382 #endif
3385 #ifdef HPUX
3386 #ifndef HAVE_PERROR
3388 /* HPUX curses library references perror, but as far as we know
3389 it won't be called. Anyway this definition will do for now. */
3391 perror ()
3395 #endif /* not HAVE_PERROR */
3396 #endif /* HPUX */
3398 #ifndef HAVE_DUP2
3401 * Emulate BSD dup2. First close newd if it already exists.
3402 * Then, attempt to dup oldd. If not successful, call dup2 recursively
3403 * until we are, then close the unsuccessful ones.
3406 dup2 (oldd, newd)
3407 int oldd;
3408 int newd;
3410 register int fd, ret;
3412 emacs_close (newd);
3414 #ifdef F_DUPFD
3415 return fcntl (oldd, F_DUPFD, newd);
3416 #else
3417 fd = dup (old);
3418 if (fd == -1)
3419 return -1;
3420 if (fd == new)
3421 return new;
3422 ret = dup2 (old,new);
3423 emacs_close (fd);
3424 return ret;
3425 #endif
3428 #endif /* not HAVE_DUP2 */
3431 * Gettimeofday. Simulate as much as possible. Only accurate
3432 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3433 * Only needed when subprocesses are defined.
3436 #ifdef subprocesses
3437 #ifndef VMS
3438 #ifndef HAVE_GETTIMEOFDAY
3439 #ifdef HAVE_TIMEVAL
3441 /* ARGSUSED */
3443 gettimeofday (tp, tzp)
3444 struct timeval *tp;
3445 struct timezone *tzp;
3447 extern long time ();
3449 tp->tv_sec = time ((long *)0);
3450 tp->tv_usec = 0;
3451 if (tzp != 0)
3452 tzp->tz_minuteswest = -1;
3453 return 0;
3456 #endif
3457 #endif
3458 #endif
3459 #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
3462 * This function will go away as soon as all the stubs fixed. (fnf)
3465 void
3466 croak (badfunc)
3467 char *badfunc;
3469 printf ("%s not yet implemented\r\n", badfunc);
3470 reset_sys_modes ();
3471 exit (1);
3474 #endif /* USG */
3476 /* Directory routines for systems that don't have them. */
3478 #ifdef SYSV_SYSTEM_DIR
3480 #include <dirent.h>
3482 #if defined (BROKEN_CLOSEDIR) || !defined (HAVE_CLOSEDIR)
3485 closedir (dirp)
3486 register DIR *dirp; /* stream from opendir */
3488 int rtnval;
3490 rtnval = emacs_close (dirp->dd_fd);
3492 /* Some systems (like Solaris) allocate the buffer and the DIR all
3493 in one block. Why in the world are we freeing this ourselves
3494 anyway? */
3495 #if ! (defined (sun) && defined (USG5_4))
3496 xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
3497 #endif
3498 xfree ((char *) dirp);
3500 return rtnval;
3502 #endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
3503 #endif /* SYSV_SYSTEM_DIR */
3505 #ifdef NONSYSTEM_DIR_LIBRARY
3507 DIR *
3508 opendir (filename)
3509 char *filename; /* name of directory */
3511 register DIR *dirp; /* -> malloc'ed storage */
3512 register int fd; /* file descriptor for read */
3513 struct stat sbuf; /* result of fstat */
3515 fd = emacs_open (filename, O_RDONLY, 0);
3516 if (fd < 0)
3517 return 0;
3519 BLOCK_INPUT;
3520 if (fstat (fd, &sbuf) < 0
3521 || (sbuf.st_mode & S_IFMT) != S_IFDIR
3522 || (dirp = (DIR *) xmalloc (sizeof (DIR))) == 0)
3524 emacs_close (fd);
3525 UNBLOCK_INPUT;
3526 return 0; /* bad luck today */
3528 UNBLOCK_INPUT;
3530 dirp->dd_fd = fd;
3531 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
3533 return dirp;
3536 void
3537 closedir (dirp)
3538 register DIR *dirp; /* stream from opendir */
3540 emacs_close (dirp->dd_fd);
3541 xfree ((char *) dirp);
3545 #ifndef VMS
3546 #define DIRSIZ 14
3547 struct olddir
3549 ino_t od_ino; /* inode */
3550 char od_name[DIRSIZ]; /* filename */
3552 #endif /* not VMS */
3554 struct direct dir_static; /* simulated directory contents */
3556 /* ARGUSED */
3557 struct direct *
3558 readdir (dirp)
3559 register DIR *dirp; /* stream from opendir */
3561 #ifndef VMS
3562 register struct olddir *dp; /* -> directory data */
3563 #else /* VMS */
3564 register struct dir$_name *dp; /* -> directory data */
3565 register struct dir$_version *dv; /* -> version data */
3566 #endif /* VMS */
3568 for (; ;)
3570 if (dirp->dd_loc >= dirp->dd_size)
3571 dirp->dd_loc = dirp->dd_size = 0;
3573 if (dirp->dd_size == 0 /* refill buffer */
3574 && (dirp->dd_size = emacs_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3575 return 0;
3577 #ifndef VMS
3578 dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
3579 dirp->dd_loc += sizeof (struct olddir);
3581 if (dp->od_ino != 0) /* not deleted entry */
3583 dir_static.d_ino = dp->od_ino;
3584 strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
3585 dir_static.d_name[DIRSIZ] = '\0';
3586 dir_static.d_namlen = strlen (dir_static.d_name);
3587 dir_static.d_reclen = sizeof (struct direct)
3588 - MAXNAMLEN + 3
3589 + dir_static.d_namlen - dir_static.d_namlen % 4;
3590 return &dir_static; /* -> simulated structure */
3592 #else /* VMS */
3593 dp = (struct dir$_name *) dirp->dd_buf;
3594 if (dirp->dd_loc == 0)
3595 dirp->dd_loc = (dp->dir$b_namecount&1) ? dp->dir$b_namecount + 1
3596 : dp->dir$b_namecount;
3597 dv = (struct dir$_version *)&dp->dir$t_name[dirp->dd_loc];
3598 dir_static.d_ino = dv->dir$w_fid_num;
3599 dir_static.d_namlen = dp->dir$b_namecount;
3600 dir_static.d_reclen = sizeof (struct direct)
3601 - MAXNAMLEN + 3
3602 + dir_static.d_namlen - dir_static.d_namlen % 4;
3603 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3604 dir_static.d_name[dir_static.d_namlen] = '\0';
3605 dirp->dd_loc = dirp->dd_size; /* only one record at a time */
3606 return &dir_static;
3607 #endif /* VMS */
3611 #ifdef VMS
3612 /* readdirver is just like readdir except it returns all versions of a file
3613 as separate entries. */
3615 /* ARGUSED */
3616 struct direct *
3617 readdirver (dirp)
3618 register DIR *dirp; /* stream from opendir */
3620 register struct dir$_name *dp; /* -> directory data */
3621 register struct dir$_version *dv; /* -> version data */
3623 if (dirp->dd_loc >= dirp->dd_size - sizeof (struct dir$_name))
3624 dirp->dd_loc = dirp->dd_size = 0;
3626 if (dirp->dd_size == 0 /* refill buffer */
3627 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3628 return 0;
3630 dp = (struct dir$_name *) dirp->dd_buf;
3631 if (dirp->dd_loc == 0)
3632 dirp->dd_loc = (dp->dir$b_namecount & 1) ? dp->dir$b_namecount + 1
3633 : dp->dir$b_namecount;
3634 dv = (struct dir$_version *) &dp->dir$t_name[dirp->dd_loc];
3635 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3636 sprintf (&dir_static.d_name[dp->dir$b_namecount], ";%d", dv->dir$w_version);
3637 dir_static.d_namlen = strlen (dir_static.d_name);
3638 dir_static.d_ino = dv->dir$w_fid_num;
3639 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3
3640 + dir_static.d_namlen - dir_static.d_namlen % 4;
3641 dirp->dd_loc = ((char *) (++dv) - dp->dir$t_name);
3642 return &dir_static;
3645 #endif /* VMS */
3647 #endif /* NONSYSTEM_DIR_LIBRARY */
3651 set_file_times (filename, atime, mtime)
3652 char *filename;
3653 EMACS_TIME atime, mtime;
3655 #ifdef HAVE_UTIMES
3656 struct timeval tv[2];
3657 tv[0] = atime;
3658 tv[1] = mtime;
3659 return utimes (filename, tv);
3660 #else /* not HAVE_UTIMES */
3661 struct utimbuf utb;
3662 utb.actime = EMACS_SECS (atime);
3663 utb.modtime = EMACS_SECS (mtime);
3664 return utime (filename, &utb);
3665 #endif /* not HAVE_UTIMES */
3668 /* mkdir and rmdir functions, for systems which don't have them. */
3670 #ifndef HAVE_MKDIR
3672 * Written by Robert Rother, Mariah Corporation, August 1985.
3674 * If you want it, it's yours. All I ask in return is that if you
3675 * figure out how to do this in a Bourne Shell script you send me
3676 * a copy.
3677 * sdcsvax!rmr or rmr@uscd
3679 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
3680 * subroutine. 11Mar86; hoptoad!gnu
3682 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3683 * subroutine didn't return EEXIST. It does now.
3687 * Make a directory.
3689 #ifdef MKDIR_PROTOTYPE
3690 MKDIR_PROTOTYPE
3691 #else
3693 mkdir (dpath, dmode)
3694 char *dpath;
3695 int dmode;
3696 #endif
3698 int cpid, status, fd;
3699 struct stat statbuf;
3701 if (stat (dpath, &statbuf) == 0)
3703 errno = EEXIST; /* Stat worked, so it already exists */
3704 return -1;
3707 /* If stat fails for a reason other than non-existence, return error */
3708 if (errno != ENOENT)
3709 return -1;
3711 synch_process_alive = 1;
3712 switch (cpid = fork ())
3715 case -1: /* Error in fork */
3716 return (-1); /* Errno is set already */
3718 case 0: /* Child process */
3720 * Cheap hack to set mode of new directory. Since this
3721 * child process is going away anyway, we zap its umask.
3722 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3723 * directory. Does anybody care?
3725 status = umask (0); /* Get current umask */
3726 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */
3727 fd = emacs_open ("/dev/null", O_RDWR, 0);
3728 if (fd >= 0)
3730 dup2 (fd, 0);
3731 dup2 (fd, 1);
3732 dup2 (fd, 2);
3734 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
3735 _exit (-1); /* Can't exec /bin/mkdir */
3737 default: /* Parent process */
3738 wait_for_termination (cpid);
3741 if (synch_process_death != 0 || synch_process_retcode != 0)
3743 errno = EIO; /* We don't know why, but */
3744 return -1; /* /bin/mkdir failed */
3747 return 0;
3749 #endif /* not HAVE_MKDIR */
3751 #ifndef HAVE_RMDIR
3753 rmdir (dpath)
3754 char *dpath;
3756 int cpid, status, fd;
3757 struct stat statbuf;
3759 if (stat (dpath, &statbuf) != 0)
3761 /* Stat just set errno. We don't have to */
3762 return -1;
3765 synch_process_alive = 1;
3766 switch (cpid = fork ())
3769 case -1: /* Error in fork */
3770 return (-1); /* Errno is set already */
3772 case 0: /* Child process */
3773 fd = emacs_open ("/dev/null", O_RDWR, 0);
3774 if (fd >= 0)
3776 dup2 (fd, 0);
3777 dup2 (fd, 1);
3778 dup2 (fd, 2);
3780 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
3781 _exit (-1); /* Can't exec /bin/rmdir */
3783 default: /* Parent process */
3784 wait_for_termination (cpid);
3787 if (synch_process_death != 0 || synch_process_retcode != 0)
3789 errno = EIO; /* We don't know why, but */
3790 return -1; /* /bin/rmdir failed */
3793 return 0;
3795 #endif /* !HAVE_RMDIR */
3799 /* Functions for VMS */
3800 #ifdef VMS
3801 #include "vms-pwd.h"
3802 #include <acldef.h>
3803 #include <chpdef.h>
3804 #include <jpidef.h>
3806 /* Return as a string the VMS error string pertaining to STATUS.
3807 Reuses the same static buffer each time it is called. */
3809 char *
3810 vmserrstr (status)
3811 int status; /* VMS status code */
3813 int bufadr[2];
3814 short len;
3815 static char buf[257];
3817 bufadr[0] = sizeof buf - 1;
3818 bufadr[1] = (int) buf;
3819 if (! (SYS$GETMSG (status, &len, bufadr, 0x1, 0) & 1))
3820 return "untranslatable VMS error status";
3821 buf[len] = '\0';
3822 return buf;
3825 #ifdef access
3826 #undef access
3828 /* The following is necessary because 'access' emulation by VMS C (2.0) does
3829 * not work correctly. (It also doesn't work well in version 2.3.)
3832 #ifdef VMS4_4
3834 #define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3835 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3837 typedef union {
3838 struct {
3839 unsigned short s_buflen;
3840 unsigned short s_code;
3841 char *s_bufadr;
3842 unsigned short *s_retlenadr;
3843 } s;
3844 int end;
3845 } item;
3846 #define buflen s.s_buflen
3847 #define code s.s_code
3848 #define bufadr s.s_bufadr
3849 #define retlenadr s.s_retlenadr
3851 #define R_OK 4 /* test for read permission */
3852 #define W_OK 2 /* test for write permission */
3853 #define X_OK 1 /* test for execute (search) permission */
3854 #define F_OK 0 /* test for presence of file */
3857 sys_access (path, mode)
3858 char *path;
3859 int mode;
3861 static char *user = NULL;
3862 char dir_fn[512];
3864 /* translate possible directory spec into .DIR file name, so brain-dead
3865 * access can treat the directory like a file. */
3866 if (directory_file_name (path, dir_fn))
3867 path = dir_fn;
3869 if (mode == F_OK)
3870 return access (path, mode);
3871 if (user == NULL && (user = (char *) getenv ("USER")) == NULL)
3872 return -1;
3874 int stat;
3875 int flags;
3876 int acces;
3877 unsigned short int dummy;
3878 item itemlst[3];
3879 static int constant = ACL$C_FILE;
3880 DESCRIPTOR (path_desc, path);
3881 DESCRIPTOR (user_desc, user);
3883 flags = 0;
3884 acces = 0;
3885 if ((mode & X_OK) && ((stat = access (path, mode)) < 0 || mode == X_OK))
3886 return stat;
3887 if (mode & R_OK)
3888 acces |= CHP$M_READ;
3889 if (mode & W_OK)
3890 acces |= CHP$M_WRITE;
3891 itemlst[0].buflen = sizeof (int);
3892 itemlst[0].code = CHP$_FLAGS;
3893 itemlst[0].bufadr = (char *) &flags;
3894 itemlst[0].retlenadr = &dummy;
3895 itemlst[1].buflen = sizeof (int);
3896 itemlst[1].code = CHP$_ACCESS;
3897 itemlst[1].bufadr = (char *) &acces;
3898 itemlst[1].retlenadr = &dummy;
3899 itemlst[2].end = CHP$_END;
3900 stat = SYS$CHECK_ACCESS (&constant, &path_desc, &user_desc, itemlst);
3901 return stat == SS$_NORMAL ? 0 : -1;
3905 #else /* not VMS4_4 */
3907 #include <prvdef.h>
3908 #define ACE$M_WRITE 2
3909 #define ACE$C_KEYID 1
3911 static unsigned short memid, grpid;
3912 static unsigned int uic;
3914 /* Called from init_sys_modes, so it happens not very often
3915 but at least each time Emacs is loaded. */
3916 void
3917 sys_access_reinit ()
3919 uic = 0;
3923 sys_access (filename, type)
3924 char * filename;
3925 int type;
3927 struct FAB fab;
3928 struct XABPRO xab;
3929 int status, size, i, typecode, acl_controlled;
3930 unsigned int *aclptr, *aclend, aclbuf[60];
3931 union prvdef prvmask;
3933 /* Get UIC and GRP values for protection checking. */
3934 if (uic == 0)
3936 status = LIB$GETJPI (&JPI$_UIC, 0, 0, &uic, 0, 0);
3937 if (! (status & 1))
3938 return -1;
3939 memid = uic & 0xFFFF;
3940 grpid = uic >> 16;
3943 if (type != 2) /* not checking write access */
3944 return access (filename, type);
3946 /* Check write protection. */
3948 #define CHECKPRIV(bit) (prvmask.bit)
3949 #define WRITABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
3951 /* Find privilege bits */
3952 status = SYS$SETPRV (0, 0, 0, prvmask);
3953 if (! (status & 1))
3954 error ("Unable to find privileges: %s", vmserrstr (status));
3955 if (CHECKPRIV (PRV$V_BYPASS))
3956 return 0; /* BYPASS enabled */
3957 fab = cc$rms_fab;
3958 fab.fab$b_fac = FAB$M_GET;
3959 fab.fab$l_fna = filename;
3960 fab.fab$b_fns = strlen (filename);
3961 fab.fab$l_xab = &xab;
3962 xab = cc$rms_xabpro;
3963 xab.xab$l_aclbuf = aclbuf;
3964 xab.xab$w_aclsiz = sizeof (aclbuf);
3965 status = SYS$OPEN (&fab, 0, 0);
3966 if (! (status & 1))
3967 return -1;
3968 SYS$CLOSE (&fab, 0, 0);
3969 /* Check system access */
3970 if (CHECKPRIV (PRV$V_SYSPRV) && WRITABLE (XAB$V_SYS))
3971 return 0;
3972 /* Check ACL entries, if any */
3973 acl_controlled = 0;
3974 if (xab.xab$w_acllen > 0)
3976 aclptr = aclbuf;
3977 aclend = &aclbuf[xab.xab$w_acllen / 4];
3978 while (*aclptr && aclptr < aclend)
3980 size = (*aclptr & 0xff) / 4;
3981 typecode = (*aclptr >> 8) & 0xff;
3982 if (typecode == ACE$C_KEYID)
3983 for (i = size - 1; i > 1; i--)
3984 if (aclptr[i] == uic)
3986 acl_controlled = 1;
3987 if (aclptr[1] & ACE$M_WRITE)
3988 return 0; /* Write access through ACL */
3990 aclptr = &aclptr[size];
3992 if (acl_controlled) /* ACL specified, prohibits write access */
3993 return -1;
3995 /* No ACL entries specified, check normal protection */
3996 if (WRITABLE (XAB$V_WLD)) /* World writable */
3997 return 0;
3998 if (WRITABLE (XAB$V_GRP) &&
3999 (unsigned short) (xab.xab$l_uic >> 16) == grpid)
4000 return 0; /* Group writable */
4001 if (WRITABLE (XAB$V_OWN) &&
4002 (xab.xab$l_uic & 0xFFFF) == memid)
4003 return 0; /* Owner writable */
4005 return -1; /* Not writable */
4007 #endif /* not VMS4_4 */
4008 #endif /* access */
4010 static char vtbuf[NAM$C_MAXRSS+1];
4012 /* translate a vms file spec to a unix path */
4013 char *
4014 sys_translate_vms (vfile)
4015 char * vfile;
4017 char * p;
4018 char * targ;
4020 if (!vfile)
4021 return 0;
4023 targ = vtbuf;
4025 /* leading device or logical name is a root directory */
4026 if (p = strchr (vfile, ':'))
4028 *targ++ = '/';
4029 while (vfile < p)
4030 *targ++ = *vfile++;
4031 vfile++;
4032 *targ++ = '/';
4034 p = vfile;
4035 if (*p == '[' || *p == '<')
4037 while (*++vfile != *p + 2)
4038 switch (*vfile)
4040 case '.':
4041 if (vfile[-1] == *p)
4042 *targ++ = '.';
4043 *targ++ = '/';
4044 break;
4046 case '-':
4047 *targ++ = '.';
4048 *targ++ = '.';
4049 break;
4051 default:
4052 *targ++ = *vfile;
4053 break;
4055 vfile++;
4056 *targ++ = '/';
4058 while (*vfile)
4059 *targ++ = *vfile++;
4061 return vtbuf;
4064 static char utbuf[NAM$C_MAXRSS+1];
4066 /* translate a unix path to a VMS file spec */
4067 char *
4068 sys_translate_unix (ufile)
4069 char * ufile;
4071 int slash_seen = 0;
4072 char *p;
4073 char * targ;
4075 if (!ufile)
4076 return 0;
4078 targ = utbuf;
4080 if (*ufile == '/')
4082 ufile++;
4085 while (*ufile)
4087 switch (*ufile)
4089 case '/':
4090 if (slash_seen)
4091 if (index (&ufile[1], '/'))
4092 *targ++ = '.';
4093 else
4094 *targ++ = ']';
4095 else
4097 *targ++ = ':';
4098 if (index (&ufile[1], '/'))
4099 *targ++ = '[';
4100 slash_seen = 1;
4102 break;
4104 case '.':
4105 if (strncmp (ufile, "./", 2) == 0)
4107 if (!slash_seen)
4109 *targ++ = '[';
4110 slash_seen = 1;
4112 ufile++; /* skip the dot */
4113 if (index (&ufile[1], '/'))
4114 *targ++ = '.';
4115 else
4116 *targ++ = ']';
4118 else if (strncmp (ufile, "../", 3) == 0)
4120 if (!slash_seen)
4122 *targ++ = '[';
4123 slash_seen = 1;
4125 *targ++ = '-';
4126 ufile += 2; /* skip the dots */
4127 if (index (&ufile[1], '/'))
4128 *targ++ = '.';
4129 else
4130 *targ++ = ']';
4132 else
4133 *targ++ = *ufile;
4134 break;
4136 default:
4137 *targ++ = *ufile;
4138 break;
4140 ufile++;
4142 *targ = '\0';
4144 return utbuf;
4147 char *
4148 getwd (pathname)
4149 char *pathname;
4151 char *ptr, *val;
4152 extern char *getcwd ();
4154 #define MAXPATHLEN 1024
4156 ptr = xmalloc (MAXPATHLEN);
4157 val = getcwd (ptr, MAXPATHLEN);
4158 if (val == 0)
4160 xfree (ptr);
4161 return val;
4163 strcpy (pathname, ptr);
4164 xfree (ptr);
4166 return pathname;
4170 getppid ()
4172 long item_code = JPI$_OWNER;
4173 unsigned long parent_id;
4174 int status;
4176 if (((status = LIB$GETJPI (&item_code, 0, 0, &parent_id)) & 1) == 0)
4178 errno = EVMSERR;
4179 vaxc$errno = status;
4180 return -1;
4182 return parent_id;
4185 #undef getuid
4186 unsigned
4187 sys_getuid ()
4189 return (getgid () << 16) | getuid ();
4192 #undef read
4194 sys_read (fildes, buf, nbyte)
4195 int fildes;
4196 char *buf;
4197 unsigned int nbyte;
4199 return read (fildes, buf, (nbyte < MAXIOSIZE ? nbyte : MAXIOSIZE));
4202 #if 0
4204 sys_write (fildes, buf, nbyte)
4205 int fildes;
4206 char *buf;
4207 unsigned int nbyte;
4209 register int nwrote, rtnval = 0;
4211 while (nbyte > MAXIOSIZE && (nwrote = write (fildes, buf, MAXIOSIZE)) > 0) {
4212 nbyte -= nwrote;
4213 buf += nwrote;
4214 rtnval += nwrote;
4216 if (nwrote < 0)
4217 return rtnval ? rtnval : -1;
4218 if ((nwrote = write (fildes, buf, nbyte)) < 0)
4219 return rtnval ? rtnval : -1;
4220 return (rtnval + nwrote);
4222 #endif /* 0 */
4225 * VAX/VMS VAX C RTL really loses. It insists that records
4226 * end with a newline (carriage return) character, and if they
4227 * don't it adds one (nice of it isn't it!)
4229 * Thus we do this stupidity below.
4232 #undef write
4234 sys_write (fildes, buf, nbytes)
4235 int fildes;
4236 char *buf;
4237 unsigned int nbytes;
4239 register char *p;
4240 register char *e;
4241 int sum = 0;
4242 struct stat st;
4244 fstat (fildes, &st);
4245 p = buf;
4246 while (nbytes > 0)
4248 int len, retval;
4250 /* Handle fixed-length files with carriage control. */
4251 if (st.st_fab_rfm == FAB$C_FIX
4252 && ((st.st_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
4254 len = st.st_fab_mrs;
4255 retval = write (fildes, p, min (len, nbytes));
4256 if (retval != len)
4257 return -1;
4258 retval++; /* This skips the implied carriage control */
4260 else
4262 e = p + min (MAXIOSIZE, nbytes) - 1;
4263 while (*e != '\n' && e > p) e--;
4264 if (p == e) /* Ok.. so here we add a newline... sigh. */
4265 e = p + min (MAXIOSIZE, nbytes) - 1;
4266 len = e + 1 - p;
4267 retval = write (fildes, p, len);
4268 if (retval != len)
4269 return -1;
4271 p += retval;
4272 sum += retval;
4273 nbytes -= retval;
4275 return sum;
4278 /* Create file NEW copying its attributes from file OLD. If
4279 OLD is 0 or does not exist, create based on the value of
4280 vms_stmlf_recfm. */
4282 /* Protection value the file should ultimately have.
4283 Set by create_copy_attrs, and use by rename_sansversions. */
4284 static unsigned short int fab_final_pro;
4287 creat_copy_attrs (old, new)
4288 char *old, *new;
4290 struct FAB fab = cc$rms_fab;
4291 struct XABPRO xabpro;
4292 char aclbuf[256]; /* Choice of size is arbitrary. See below. */
4293 extern int vms_stmlf_recfm;
4295 if (old)
4297 fab.fab$b_fac = FAB$M_GET;
4298 fab.fab$l_fna = old;
4299 fab.fab$b_fns = strlen (old);
4300 fab.fab$l_xab = (char *) &xabpro;
4301 xabpro = cc$rms_xabpro;
4302 xabpro.xab$l_aclbuf = aclbuf;
4303 xabpro.xab$w_aclsiz = sizeof aclbuf;
4304 /* Call $OPEN to fill in the fab & xabpro fields. */
4305 if (SYS$OPEN (&fab, 0, 0) & 1)
4307 SYS$CLOSE (&fab, 0, 0);
4308 fab.fab$l_alq = 0; /* zero the allocation quantity */
4309 if (xabpro.xab$w_acllen > 0)
4311 if (xabpro.xab$w_acllen > sizeof aclbuf)
4312 /* If the acl buffer was too short, redo open with longer one.
4313 Wouldn't need to do this if there were some system imposed
4314 limit on the size of an ACL, but I can't find any such. */
4316 xabpro.xab$l_aclbuf = (char *) alloca (xabpro.xab$w_acllen);
4317 xabpro.xab$w_aclsiz = xabpro.xab$w_acllen;
4318 if (SYS$OPEN (&fab, 0, 0) & 1)
4319 SYS$CLOSE (&fab, 0, 0);
4320 else
4321 old = 0;
4324 else
4325 xabpro.xab$l_aclbuf = 0;
4327 else
4328 old = 0;
4330 fab.fab$l_fna = new;
4331 fab.fab$b_fns = strlen (new);
4332 if (!old)
4334 fab.fab$l_xab = 0;
4335 fab.fab$b_rfm = vms_stmlf_recfm ? FAB$C_STMLF : FAB$C_VAR;
4336 fab.fab$b_rat = FAB$M_CR;
4339 /* Set the file protections such that we will be able to manipulate
4340 this file. Once we are done writing and renaming it, we will set
4341 the protections back. */
4342 if (old)
4343 fab_final_pro = xabpro.xab$w_pro;
4344 else
4345 SYS$SETDFPROT (0, &fab_final_pro);
4346 xabpro.xab$w_pro &= 0xff0f; /* set O:rewd for now. This is set back later. */
4348 /* Create the new file with either default attrs or attrs copied
4349 from old file. */
4350 if (!(SYS$CREATE (&fab, 0, 0) & 1))
4351 return -1;
4352 SYS$CLOSE (&fab, 0, 0);
4353 /* As this is a "replacement" for creat, return a file descriptor
4354 opened for writing. */
4355 return open (new, O_WRONLY);
4358 #ifdef creat
4359 #undef creat
4360 #include <varargs.h>
4361 #ifdef __GNUC__
4362 #ifndef va_count
4363 #define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
4364 #endif
4365 #endif
4368 sys_creat (va_alist)
4369 va_dcl
4371 va_list list_incrementer;
4372 char *name;
4373 int mode;
4374 int rfd; /* related file descriptor */
4375 int fd; /* Our new file descriptor */
4376 int count;
4377 struct stat st_buf;
4378 char rfm[12];
4379 char rat[15];
4380 char mrs[13];
4381 char fsz[13];
4382 extern int vms_stmlf_recfm;
4384 va_count (count);
4385 va_start (list_incrementer);
4386 name = va_arg (list_incrementer, char *);
4387 mode = va_arg (list_incrementer, int);
4388 if (count > 2)
4389 rfd = va_arg (list_incrementer, int);
4390 va_end (list_incrementer);
4391 if (count > 2)
4393 /* Use information from the related file descriptor to set record
4394 format of the newly created file. */
4395 fstat (rfd, &st_buf);
4396 switch (st_buf.st_fab_rfm)
4398 case FAB$C_FIX:
4399 strcpy (rfm, "rfm = fix");
4400 sprintf (mrs, "mrs = %d", st_buf.st_fab_mrs);
4401 strcpy (rat, "rat = ");
4402 if (st_buf.st_fab_rat & FAB$M_CR)
4403 strcat (rat, "cr");
4404 else if (st_buf.st_fab_rat & FAB$M_FTN)
4405 strcat (rat, "ftn");
4406 else if (st_buf.st_fab_rat & FAB$M_PRN)
4407 strcat (rat, "prn");
4408 if (st_buf.st_fab_rat & FAB$M_BLK)
4409 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4410 strcat (rat, ", blk");
4411 else
4412 strcat (rat, "blk");
4413 return creat (name, 0, rfm, rat, mrs);
4415 case FAB$C_VFC:
4416 strcpy (rfm, "rfm = vfc");
4417 sprintf (fsz, "fsz = %d", st_buf.st_fab_fsz);
4418 strcpy (rat, "rat = ");
4419 if (st_buf.st_fab_rat & FAB$M_CR)
4420 strcat (rat, "cr");
4421 else if (st_buf.st_fab_rat & FAB$M_FTN)
4422 strcat (rat, "ftn");
4423 else if (st_buf.st_fab_rat & FAB$M_PRN)
4424 strcat (rat, "prn");
4425 if (st_buf.st_fab_rat & FAB$M_BLK)
4426 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4427 strcat (rat, ", blk");
4428 else
4429 strcat (rat, "blk");
4430 return creat (name, 0, rfm, rat, fsz);
4432 case FAB$C_STM:
4433 strcpy (rfm, "rfm = stm");
4434 break;
4436 case FAB$C_STMCR:
4437 strcpy (rfm, "rfm = stmcr");
4438 break;
4440 case FAB$C_STMLF:
4441 strcpy (rfm, "rfm = stmlf");
4442 break;
4444 case FAB$C_UDF:
4445 strcpy (rfm, "rfm = udf");
4446 break;
4448 case FAB$C_VAR:
4449 strcpy (rfm, "rfm = var");
4450 break;
4452 strcpy (rat, "rat = ");
4453 if (st_buf.st_fab_rat & FAB$M_CR)
4454 strcat (rat, "cr");
4455 else if (st_buf.st_fab_rat & FAB$M_FTN)
4456 strcat (rat, "ftn");
4457 else if (st_buf.st_fab_rat & FAB$M_PRN)
4458 strcat (rat, "prn");
4459 if (st_buf.st_fab_rat & FAB$M_BLK)
4460 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4461 strcat (rat, ", blk");
4462 else
4463 strcat (rat, "blk");
4465 else
4467 strcpy (rfm, vms_stmlf_recfm ? "rfm = stmlf" : "rfm=var");
4468 strcpy (rat, "rat=cr");
4470 /* Until the VAX C RTL fixes the many bugs with modes, always use
4471 mode 0 to get the user's default protection. */
4472 fd = creat (name, 0, rfm, rat);
4473 if (fd < 0 && errno == EEXIST)
4475 if (unlink (name) < 0)
4476 report_file_error ("delete", build_string (name));
4477 fd = creat (name, 0, rfm, rat);
4479 return fd;
4481 #endif /* creat */
4483 /* fwrite to stdout is S L O W. Speed it up by using fputc...*/
4485 sys_fwrite (ptr, size, num, fp)
4486 register char * ptr;
4487 FILE * fp;
4489 register int tot = num * size;
4491 while (tot--)
4492 fputc (*ptr++, fp);
4493 return num;
4497 * The VMS C library routine creat actually creates a new version of an
4498 * existing file rather than truncating the old version. There are times
4499 * when this is not the desired behavior, for instance, when writing an
4500 * auto save file (you only want one version), or when you don't have
4501 * write permission in the directory containing the file (but the file
4502 * itself is writable). Hence this routine, which is equivalent to
4503 * "close (creat (fn, 0));" on Unix if fn already exists.
4506 vms_truncate (fn)
4507 char *fn;
4509 struct FAB xfab = cc$rms_fab;
4510 struct RAB xrab = cc$rms_rab;
4511 int status;
4513 xfab.fab$l_fop = FAB$M_TEF; /* free allocated but unused blocks on close */
4514 xfab.fab$b_fac = FAB$M_TRN | FAB$M_GET; /* allow truncate and get access */
4515 xfab.fab$b_shr = FAB$M_NIL; /* allow no sharing - file must be locked */
4516 xfab.fab$l_fna = fn;
4517 xfab.fab$b_fns = strlen (fn);
4518 xfab.fab$l_dna = ";0"; /* default to latest version of the file */
4519 xfab.fab$b_dns = 2;
4520 xrab.rab$l_fab = &xfab;
4522 /* This gibberish opens the file, positions to the first record, and
4523 deletes all records from there until the end of file. */
4524 if ((SYS$OPEN (&xfab) & 01) == 01)
4526 if ((SYS$CONNECT (&xrab) & 01) == 01 &&
4527 (SYS$FIND (&xrab) & 01) == 01 &&
4528 (SYS$TRUNCATE (&xrab) & 01) == 01)
4529 status = 0;
4530 else
4531 status = -1;
4533 else
4534 status = -1;
4535 SYS$CLOSE (&xfab);
4536 return status;
4539 /* Define this symbol to actually read SYSUAF.DAT. This requires either
4540 SYSPRV or a readable SYSUAF.DAT. */
4542 #ifdef READ_SYSUAF
4544 * getuaf.c
4546 * Routine to read the VMS User Authorization File and return
4547 * a specific user's record.
4550 static struct UAF retuaf;
4552 struct UAF *
4553 get_uaf_name (uname)
4554 char * uname;
4556 register status;
4557 struct FAB uaf_fab;
4558 struct RAB uaf_rab;
4560 uaf_fab = cc$rms_fab;
4561 uaf_rab = cc$rms_rab;
4562 /* initialize fab fields */
4563 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4564 uaf_fab.fab$b_fns = 21;
4565 uaf_fab.fab$b_fac = FAB$M_GET;
4566 uaf_fab.fab$b_org = FAB$C_IDX;
4567 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4568 /* initialize rab fields */
4569 uaf_rab.rab$l_fab = &uaf_fab;
4570 /* open the User Authorization File */
4571 status = SYS$OPEN (&uaf_fab);
4572 if (!(status&1))
4574 errno = EVMSERR;
4575 vaxc$errno = status;
4576 return 0;
4578 status = SYS$CONNECT (&uaf_rab);
4579 if (!(status&1))
4581 errno = EVMSERR;
4582 vaxc$errno = status;
4583 return 0;
4585 /* read the requested record - index is in uname */
4586 uaf_rab.rab$l_kbf = uname;
4587 uaf_rab.rab$b_ksz = strlen (uname);
4588 uaf_rab.rab$b_rac = RAB$C_KEY;
4589 uaf_rab.rab$l_ubf = (char *)&retuaf;
4590 uaf_rab.rab$w_usz = sizeof retuaf;
4591 status = SYS$GET (&uaf_rab);
4592 if (!(status&1))
4594 errno = EVMSERR;
4595 vaxc$errno = status;
4596 return 0;
4598 /* close the User Authorization File */
4599 status = SYS$DISCONNECT (&uaf_rab);
4600 if (!(status&1))
4602 errno = EVMSERR;
4603 vaxc$errno = status;
4604 return 0;
4606 status = SYS$CLOSE (&uaf_fab);
4607 if (!(status&1))
4609 errno = EVMSERR;
4610 vaxc$errno = status;
4611 return 0;
4613 return &retuaf;
4616 struct UAF *
4617 get_uaf_uic (uic)
4618 unsigned long uic;
4620 register status;
4621 struct FAB uaf_fab;
4622 struct RAB uaf_rab;
4624 uaf_fab = cc$rms_fab;
4625 uaf_rab = cc$rms_rab;
4626 /* initialize fab fields */
4627 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4628 uaf_fab.fab$b_fns = 21;
4629 uaf_fab.fab$b_fac = FAB$M_GET;
4630 uaf_fab.fab$b_org = FAB$C_IDX;
4631 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4632 /* initialize rab fields */
4633 uaf_rab.rab$l_fab = &uaf_fab;
4634 /* open the User Authorization File */
4635 status = SYS$OPEN (&uaf_fab);
4636 if (!(status&1))
4638 errno = EVMSERR;
4639 vaxc$errno = status;
4640 return 0;
4642 status = SYS$CONNECT (&uaf_rab);
4643 if (!(status&1))
4645 errno = EVMSERR;
4646 vaxc$errno = status;
4647 return 0;
4649 /* read the requested record - index is in uic */
4650 uaf_rab.rab$b_krf = 1; /* 1st alternate key */
4651 uaf_rab.rab$l_kbf = (char *) &uic;
4652 uaf_rab.rab$b_ksz = sizeof uic;
4653 uaf_rab.rab$b_rac = RAB$C_KEY;
4654 uaf_rab.rab$l_ubf = (char *)&retuaf;
4655 uaf_rab.rab$w_usz = sizeof retuaf;
4656 status = SYS$GET (&uaf_rab);
4657 if (!(status&1))
4659 errno = EVMSERR;
4660 vaxc$errno = status;
4661 return 0;
4663 /* close the User Authorization File */
4664 status = SYS$DISCONNECT (&uaf_rab);
4665 if (!(status&1))
4667 errno = EVMSERR;
4668 vaxc$errno = status;
4669 return 0;
4671 status = SYS$CLOSE (&uaf_fab);
4672 if (!(status&1))
4674 errno = EVMSERR;
4675 vaxc$errno = status;
4676 return 0;
4678 return &retuaf;
4681 static struct passwd retpw;
4683 struct passwd *
4684 cnv_uaf_pw (up)
4685 struct UAF * up;
4687 char * ptr;
4689 /* copy these out first because if the username is 32 chars, the next
4690 section will overwrite the first byte of the UIC */
4691 retpw.pw_uid = up->uaf$w_mem;
4692 retpw.pw_gid = up->uaf$w_grp;
4694 /* I suppose this is not the best style, to possibly overwrite one
4695 byte beyond the end of the field, but what the heck... */
4696 ptr = &up->uaf$t_username[UAF$S_USERNAME];
4697 while (ptr[-1] == ' ')
4698 ptr--;
4699 *ptr = '\0';
4700 strcpy (retpw.pw_name, up->uaf$t_username);
4702 /* the rest of these are counted ascii strings */
4703 strncpy (retpw.pw_gecos, &up->uaf$t_owner[1], up->uaf$t_owner[0]);
4704 retpw.pw_gecos[up->uaf$t_owner[0]] = '\0';
4705 strncpy (retpw.pw_dir, &up->uaf$t_defdev[1], up->uaf$t_defdev[0]);
4706 retpw.pw_dir[up->uaf$t_defdev[0]] = '\0';
4707 strncat (retpw.pw_dir, &up->uaf$t_defdir[1], up->uaf$t_defdir[0]);
4708 retpw.pw_dir[up->uaf$t_defdev[0] + up->uaf$t_defdir[0]] = '\0';
4709 strncpy (retpw.pw_shell, &up->uaf$t_defcli[1], up->uaf$t_defcli[0]);
4710 retpw.pw_shell[up->uaf$t_defcli[0]] = '\0';
4712 return &retpw;
4714 #else /* not READ_SYSUAF */
4715 static struct passwd retpw;
4716 #endif /* not READ_SYSUAF */
4718 struct passwd *
4719 getpwnam (name)
4720 char * name;
4722 #ifdef READ_SYSUAF
4723 struct UAF *up;
4724 #else
4725 char * user;
4726 char * dir;
4727 unsigned char * full;
4728 #endif /* READ_SYSUAF */
4729 char *ptr = name;
4731 while (*ptr)
4733 if ('a' <= *ptr && *ptr <= 'z')
4734 *ptr -= 040;
4735 ptr++;
4737 #ifdef READ_SYSUAF
4738 if (!(up = get_uaf_name (name)))
4739 return 0;
4740 return cnv_uaf_pw (up);
4741 #else
4742 if (strcmp (name, getenv ("USER")) == 0)
4744 retpw.pw_uid = getuid ();
4745 retpw.pw_gid = getgid ();
4746 strcpy (retpw.pw_name, name);
4747 if (full = egetenv ("FULLNAME"))
4748 strcpy (retpw.pw_gecos, full);
4749 else
4750 *retpw.pw_gecos = '\0';
4751 strcpy (retpw.pw_dir, egetenv ("HOME"));
4752 *retpw.pw_shell = '\0';
4753 return &retpw;
4755 else
4756 return 0;
4757 #endif /* not READ_SYSUAF */
4760 struct passwd *
4761 getpwuid (uid)
4762 unsigned long uid;
4764 #ifdef READ_SYSUAF
4765 struct UAF * up;
4767 if (!(up = get_uaf_uic (uid)))
4768 return 0;
4769 return cnv_uaf_pw (up);
4770 #else
4771 if (uid == sys_getuid ())
4772 return getpwnam (egetenv ("USER"));
4773 else
4774 return 0;
4775 #endif /* not READ_SYSUAF */
4778 /* return total address space available to the current process. This is
4779 the sum of the current p0 size, p1 size and free page table entries
4780 available. */
4782 vlimit ()
4784 int item_code;
4785 unsigned long free_pages;
4786 unsigned long frep0va;
4787 unsigned long frep1va;
4788 register status;
4790 item_code = JPI$_FREPTECNT;
4791 if (((status = LIB$GETJPI (&item_code, 0, 0, &free_pages)) & 1) == 0)
4793 errno = EVMSERR;
4794 vaxc$errno = status;
4795 return -1;
4797 free_pages *= 512;
4799 item_code = JPI$_FREP0VA;
4800 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep0va)) & 1) == 0)
4802 errno = EVMSERR;
4803 vaxc$errno = status;
4804 return -1;
4806 item_code = JPI$_FREP1VA;
4807 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep1va)) & 1) == 0)
4809 errno = EVMSERR;
4810 vaxc$errno = status;
4811 return -1;
4814 return free_pages + frep0va + (0x7fffffff - frep1va);
4818 define_logical_name (varname, string)
4819 char *varname;
4820 char *string;
4822 struct dsc$descriptor_s strdsc =
4823 {strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string};
4824 struct dsc$descriptor_s envdsc =
4825 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4826 struct dsc$descriptor_s lnmdsc =
4827 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4829 return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
4833 delete_logical_name (varname)
4834 char *varname;
4836 struct dsc$descriptor_s envdsc =
4837 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4838 struct dsc$descriptor_s lnmdsc =
4839 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4841 return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
4845 ulimit ()
4847 return 0;
4851 setpgrp ()
4853 return 0;
4857 execvp ()
4859 error ("execvp system call not implemented");
4860 return -1;
4864 rename (from, to)
4865 char *from, *to;
4867 int status;
4868 struct FAB from_fab = cc$rms_fab, to_fab = cc$rms_fab;
4869 struct NAM from_nam = cc$rms_nam, to_nam = cc$rms_nam;
4870 char from_esn[NAM$C_MAXRSS];
4871 char to_esn[NAM$C_MAXRSS];
4873 from_fab.fab$l_fna = from;
4874 from_fab.fab$b_fns = strlen (from);
4875 from_fab.fab$l_nam = &from_nam;
4876 from_fab.fab$l_fop = FAB$M_NAM;
4878 from_nam.nam$l_esa = from_esn;
4879 from_nam.nam$b_ess = sizeof from_esn;
4881 to_fab.fab$l_fna = to;
4882 to_fab.fab$b_fns = strlen (to);
4883 to_fab.fab$l_nam = &to_nam;
4884 to_fab.fab$l_fop = FAB$M_NAM;
4886 to_nam.nam$l_esa = to_esn;
4887 to_nam.nam$b_ess = sizeof to_esn;
4889 status = SYS$RENAME (&from_fab, 0, 0, &to_fab);
4891 if (status & 1)
4892 return 0;
4893 else
4895 if (status == RMS$_DEV)
4896 errno = EXDEV;
4897 else
4898 errno = EVMSERR;
4899 vaxc$errno = status;
4900 return -1;
4904 /* This function renames a file like `rename', but it strips
4905 the version number from the "to" filename, such that the "to" file is
4906 will always be a new version. It also sets the file protection once it is
4907 finished. The protection that we will use is stored in fab_final_pro,
4908 and was set when we did a creat_copy_attrs to create the file that we
4909 are renaming.
4911 We could use the chmod function, but Eunichs uses 3 bits per user category
4912 to describe the protection, and VMS uses 4 (write and delete are separate
4913 bits). To maintain portability, the VMS implementation of `chmod' wires
4914 the W and D bits together. */
4917 static struct fibdef fib; /* We need this initialized to zero */
4918 char vms_file_written[NAM$C_MAXRSS];
4921 rename_sans_version (from,to)
4922 char *from, *to;
4924 short int chan;
4925 int stat;
4926 short int iosb[4];
4927 int status;
4928 struct FAB to_fab = cc$rms_fab;
4929 struct NAM to_nam = cc$rms_nam;
4930 struct dsc$descriptor fib_d ={sizeof (fib),0,0,(char*) &fib};
4931 struct dsc$descriptor fib_attr[2]
4932 = {{sizeof (fab_final_pro),ATR$C_FPRO,0,(char*) &fab_final_pro},{0,0,0,0}};
4933 char to_esn[NAM$C_MAXRSS];
4935 $DESCRIPTOR (disk,to_esn);
4937 to_fab.fab$l_fna = to;
4938 to_fab.fab$b_fns = strlen (to);
4939 to_fab.fab$l_nam = &to_nam;
4940 to_fab.fab$l_fop = FAB$M_NAM;
4942 to_nam.nam$l_esa = to_esn;
4943 to_nam.nam$b_ess = sizeof to_esn;
4945 status = SYS$PARSE (&to_fab, 0, 0); /* figure out the full file name */
4947 if (to_nam.nam$l_fnb && NAM$M_EXP_VER)
4948 *(to_nam.nam$l_ver) = '\0';
4950 stat = rename (from, to_esn);
4951 if (stat < 0)
4952 return stat;
4954 strcpy (vms_file_written, to_esn);
4956 to_fab.fab$l_fna = vms_file_written; /* this points to the versionless name */
4957 to_fab.fab$b_fns = strlen (vms_file_written);
4959 /* Now set the file protection to the correct value */
4960 SYS$OPEN (&to_fab, 0, 0); /* This fills in the nam$w_fid fields */
4962 /* Copy these fields into the fib */
4963 fib.fib$r_fid_overlay.fib$w_fid[0] = to_nam.nam$w_fid[0];
4964 fib.fib$r_fid_overlay.fib$w_fid[1] = to_nam.nam$w_fid[1];
4965 fib.fib$r_fid_overlay.fib$w_fid[2] = to_nam.nam$w_fid[2];
4967 SYS$CLOSE (&to_fab, 0, 0);
4969 stat = SYS$ASSIGN (&disk, &chan, 0, 0); /* open a channel to the disk */
4970 if (!stat)
4971 LIB$SIGNAL (stat);
4972 stat = SYS$QIOW (0, chan, IO$_MODIFY, iosb, 0, 0, &fib_d,
4973 0, 0, 0, &fib_attr, 0);
4974 if (!stat)
4975 LIB$SIGNAL (stat);
4976 stat = SYS$DASSGN (chan);
4977 if (!stat)
4978 LIB$SIGNAL (stat);
4979 strcpy (vms_file_written, to_esn); /* We will write this to the terminal*/
4980 return 0;
4984 link (file, new)
4985 char * file, * new;
4987 register status;
4988 struct FAB fab;
4989 struct NAM nam;
4990 unsigned short fid[3];
4991 char esa[NAM$C_MAXRSS];
4993 fab = cc$rms_fab;
4994 fab.fab$l_fop = FAB$M_OFP;
4995 fab.fab$l_fna = file;
4996 fab.fab$b_fns = strlen (file);
4997 fab.fab$l_nam = &nam;
4999 nam = cc$rms_nam;
5000 nam.nam$l_esa = esa;
5001 nam.nam$b_ess = NAM$C_MAXRSS;
5003 status = SYS$PARSE (&fab);
5004 if ((status & 1) == 0)
5006 errno = EVMSERR;
5007 vaxc$errno = status;
5008 return -1;
5010 status = SYS$SEARCH (&fab);
5011 if ((status & 1) == 0)
5013 errno = EVMSERR;
5014 vaxc$errno = status;
5015 return -1;
5018 fid[0] = nam.nam$w_fid[0];
5019 fid[1] = nam.nam$w_fid[1];
5020 fid[2] = nam.nam$w_fid[2];
5022 fab.fab$l_fna = new;
5023 fab.fab$b_fns = strlen (new);
5025 status = SYS$PARSE (&fab);
5026 if ((status & 1) == 0)
5028 errno = EVMSERR;
5029 vaxc$errno = status;
5030 return -1;
5033 nam.nam$w_fid[0] = fid[0];
5034 nam.nam$w_fid[1] = fid[1];
5035 nam.nam$w_fid[2] = fid[2];
5037 nam.nam$l_esa = nam.nam$l_name;
5038 nam.nam$b_esl = nam.nam$b_name + nam.nam$b_type + nam.nam$b_ver;
5040 status = SYS$ENTER (&fab);
5041 if ((status & 1) == 0)
5043 errno = EVMSERR;
5044 vaxc$errno = status;
5045 return -1;
5048 return 0;
5051 void
5052 croak (badfunc)
5053 char *badfunc;
5055 printf ("%s not yet implemented\r\n", badfunc);
5056 reset_sys_modes ();
5057 exit (1);
5060 long
5061 random ()
5063 /* Arrange to return a range centered on zero. */
5064 return rand () - (1 << 30);
5067 void
5068 srandom (seed)
5070 srand (seed);
5072 #endif /* VMS */
5074 #ifdef AIXHFT
5076 /* Called from init_sys_modes. */
5077 void
5078 hft_init ()
5080 int junk;
5082 /* If we're not on an HFT we shouldn't do any of this. We determine
5083 if we are on an HFT by trying to get an HFT error code. If this
5084 call fails, we're not on an HFT. */
5085 #ifdef IBMR2AIX
5086 if (ioctl (0, HFQERROR, &junk) < 0)
5087 return;
5088 #else /* not IBMR2AIX */
5089 if (ioctl (0, HFQEIO, 0) < 0)
5090 return;
5091 #endif /* not IBMR2AIX */
5093 /* On AIX the default hft keyboard mapping uses backspace rather than delete
5094 as the rubout key's ASCII code. Here this is changed. The bug is that
5095 there's no way to determine the old mapping, so in reset_sys_modes
5096 we need to assume that the normal map had been present. Of course, this
5097 code also doesn't help if on a terminal emulator which doesn't understand
5098 HFT VTD's. */
5100 struct hfbuf buf;
5101 struct hfkeymap keymap;
5103 buf.hf_bufp = (char *)&keymap;
5104 buf.hf_buflen = sizeof (keymap);
5105 keymap.hf_nkeys = 2;
5106 keymap.hfkey[0].hf_kpos = 15;
5107 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
5108 #ifdef IBMR2AIX
5109 keymap.hfkey[0].hf_keyidh = '<';
5110 #else /* not IBMR2AIX */
5111 keymap.hfkey[0].hf_page = '<';
5112 #endif /* not IBMR2AIX */
5113 keymap.hfkey[0].hf_char = 127;
5114 keymap.hfkey[1].hf_kpos = 15;
5115 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
5116 #ifdef IBMR2AIX
5117 keymap.hfkey[1].hf_keyidh = '<';
5118 #else /* not IBMR2AIX */
5119 keymap.hfkey[1].hf_page = '<';
5120 #endif /* not IBMR2AIX */
5121 keymap.hfkey[1].hf_char = 127;
5122 hftctl (0, HFSKBD, &buf);
5124 /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
5125 at times. */
5126 line_ins_del_ok = char_ins_del_ok = 0;
5129 /* Reset the rubout key to backspace. */
5131 void
5132 hft_reset ()
5134 struct hfbuf buf;
5135 struct hfkeymap keymap;
5136 int junk;
5138 #ifdef IBMR2AIX
5139 if (ioctl (0, HFQERROR, &junk) < 0)
5140 return;
5141 #else /* not IBMR2AIX */
5142 if (ioctl (0, HFQEIO, 0) < 0)
5143 return;
5144 #endif /* not IBMR2AIX */
5146 buf.hf_bufp = (char *)&keymap;
5147 buf.hf_buflen = sizeof (keymap);
5148 keymap.hf_nkeys = 2;
5149 keymap.hfkey[0].hf_kpos = 15;
5150 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
5151 #ifdef IBMR2AIX
5152 keymap.hfkey[0].hf_keyidh = '<';
5153 #else /* not IBMR2AIX */
5154 keymap.hfkey[0].hf_page = '<';
5155 #endif /* not IBMR2AIX */
5156 keymap.hfkey[0].hf_char = 8;
5157 keymap.hfkey[1].hf_kpos = 15;
5158 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
5159 #ifdef IBMR2AIX
5160 keymap.hfkey[1].hf_keyidh = '<';
5161 #else /* not IBMR2AIX */
5162 keymap.hfkey[1].hf_page = '<';
5163 #endif /* not IBMR2AIX */
5164 keymap.hfkey[1].hf_char = 8;
5165 hftctl (0, HFSKBD, &buf);
5168 #endif /* AIXHFT */
5170 #ifdef USE_DL_STUBS
5172 /* These are included on Sunos 4.1 when we do not use shared libraries.
5173 X11 libraries may refer to these functions but (we hope) do not
5174 actually call them. */
5176 void *
5177 dlopen ()
5179 return 0;
5182 void *
5183 dlsym ()
5185 return 0;
5189 dlclose ()
5191 return -1;
5194 #endif /* USE_DL_STUBS */
5196 #ifndef BSTRING
5198 #ifndef bzero
5200 void
5201 bzero (b, length)
5202 register char *b;
5203 register int length;
5205 #ifdef VMS
5206 short zero = 0;
5207 long max_str = 65535;
5209 while (length > max_str) {
5210 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
5211 length -= max_str;
5212 b += max_str;
5214 max_str = length;
5215 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
5216 #else
5217 while (length-- > 0)
5218 *b++ = 0;
5219 #endif /* not VMS */
5222 #endif /* no bzero */
5223 #endif /* BSTRING */
5225 #if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
5226 #undef bcopy
5228 /* Saying `void' requires a declaration, above, where bcopy is used
5229 and that declaration causes pain for systems where bcopy is a macro. */
5230 bcopy (b1, b2, length)
5231 register char *b1;
5232 register char *b2;
5233 register int length;
5235 #ifdef VMS
5236 long max_str = 65535;
5238 while (length > max_str) {
5239 (void) LIB$MOVC3 (&max_str, b1, b2);
5240 length -= max_str;
5241 b1 += max_str;
5242 b2 += max_str;
5244 max_str = length;
5245 (void) LIB$MOVC3 (&length, b1, b2);
5246 #else
5247 while (length-- > 0)
5248 *b2++ = *b1++;
5249 #endif /* not VMS */
5251 #endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
5253 #ifndef BSTRING
5254 #ifndef bcmp
5256 bcmp (b1, b2, length) /* This could be a macro! */
5257 register char *b1;
5258 register char *b2;
5259 register int length;
5261 #ifdef VMS
5262 struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
5263 struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
5265 return STR$COMPARE (&src1, &src2);
5266 #else
5267 while (length-- > 0)
5268 if (*b1++ != *b2++)
5269 return 1;
5271 return 0;
5272 #endif /* not VMS */
5274 #endif /* no bcmp */
5275 #endif /* not BSTRING */
5277 #ifndef HAVE_STRSIGNAL
5278 char *
5279 strsignal (code)
5280 int code;
5282 char *signame = 0;
5284 if (0 <= code && code < NSIG)
5286 #ifdef VMS
5287 signame = sys_errlist[code];
5288 #else
5289 /* Cast to suppress warning if the table has const char *. */
5290 signame = (char *) sys_siglist[code];
5291 #endif
5294 return signame;
5296 #endif /* HAVE_STRSIGNAL */