regenerataed for Emacs 21.2.94
[emacs.git] / src / sysdep.c
blob0e8cc12f0fdff776647d846718fbd0a73073fafe
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 #if 0 /* It turns out that defining _OSF_SOURCE in osf5-0.h gets
34 random prototyped as returning `int'. It looks to me as
35 though the best way to DTRT is to prefer the rand48 functions
36 (per libc.info). -- fx */
37 extern long int random P_ ((void));
38 #endif
39 #if 0 /* Don't prototype srandom; it takes an unsigned argument on
40 some systems, and an unsigned long on others, like FreeBSD
41 4.1. */
42 extern void srandom P_ ((unsigned int));
43 #endif
44 #endif
46 #include "blockinput.h"
47 #undef NULL
49 #ifdef macintosh
50 /* It is essential to include stdlib.h so that this file picks up
51 the correct definitions of rand, srand, and RAND_MAX.
52 Otherwise random numbers will not work correctly. */
53 #include <stdlib.h>
55 #ifndef subprocesses
56 /* Nonzero means delete a process right away if it exits (process.c). */
57 static int delete_exited_processes;
58 #endif
59 #endif /* macintosh */
61 #define min(x,y) ((x) > (y) ? (y) : (x))
63 #ifdef WINDOWSNT
64 #define read sys_read
65 #define write sys_write
66 #include <windows.h>
67 #ifndef NULL
68 #define NULL 0
69 #endif
70 #endif /* not WINDOWSNT */
72 /* Does anyone other than VMS need this? */
73 #ifndef fwrite
74 #define sys_fwrite fwrite
75 #else
76 #undef fwrite
77 #endif
79 #include <stdio.h>
80 #include <sys/types.h>
81 #include <sys/stat.h>
82 #include <errno.h>
84 /* Get _POSIX_VDISABLE, if it is available. */
85 #ifdef HAVE_UNISTD_H
86 #include <unistd.h>
87 #endif
89 #ifdef HAVE_STDLIB_H
90 #include <stdlib.h>
91 #endif
93 #ifdef HAVE_SETPGID
94 #if !defined (USG) || defined (BSD_PGRPS)
95 #undef setpgrp
96 #define setpgrp setpgid
97 #endif
98 #endif
100 /* Get SI_SRPC_DOMAIN, if it is available. */
101 #ifdef HAVE_SYS_SYSTEMINFO_H
102 #include <sys/systeminfo.h>
103 #endif
105 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
106 #include <dos.h>
107 #include "dosfns.h"
108 #include "msdos.h"
109 #include <sys/param.h>
111 #if __DJGPP__ > 1
112 extern int etext;
113 extern unsigned start __asm__ ("start");
114 #endif
115 #endif
117 #ifndef USE_CRT_DLL
118 #ifndef errno
119 extern int errno;
120 #endif
121 #endif
123 #ifdef VMS
124 #include <rms.h>
125 #include <ttdef.h>
126 #include <tt2def.h>
127 #include <iodef.h>
128 #include <ssdef.h>
129 #include <descrip.h>
130 #include <fibdef.h>
131 #include <atrdef.h>
132 #include <ctype.h>
133 #include <string.h>
134 #ifdef __GNUC__
135 #include <sys/file.h>
136 #else
137 #include <file.h>
138 #endif
139 #undef F_SETFL
140 #ifndef RAB$C_BID
141 #include <rab.h>
142 #endif
143 #define MAXIOSIZE (32 * PAGESIZE) /* Don't I/O more than 32 blocks at a time */
144 #endif /* VMS */
146 #ifndef VMS
147 #include <sys/file.h>
148 #endif /* not VMS */
150 #ifndef BSD4_1
151 #ifdef BSD_SYSTEM /* avoid writing defined (BSD_SYSTEM) || defined (USG)
152 because the vms compiler doesn't grok `defined' */
153 #include <fcntl.h>
154 #endif
155 #ifdef USG
156 #ifndef USG5
157 #include <fcntl.h>
158 #endif
159 #endif
160 #endif /* not 4.1 bsd */
162 #ifndef MSDOS
163 #include <sys/ioctl.h>
164 #endif
166 #include "systty.h"
167 #include "syswait.h"
169 #ifdef BROKEN_TIOCGWINSZ
170 #undef TIOCGWINSZ
171 #undef TIOCSWINSZ
172 #endif
174 #if defined (USG) || defined (DGUX)
175 #include <sys/utsname.h>
176 #ifndef MEMORY_IN_STRING_H
177 #include <memory.h>
178 #endif
179 #if defined (TIOCGWINSZ) || defined (ISC4_0)
180 #ifdef NEED_SIOCTL
181 #include <sys/sioctl.h>
182 #endif
183 #ifdef NEED_PTEM_H
184 #include <sys/stream.h>
185 #include <sys/ptem.h>
186 #endif
187 #endif /* TIOCGWINSZ or ISC4_0 */
188 #endif /* USG or DGUX */
190 extern int quit_char;
192 #include "keyboard.h"
193 #include "frame.h"
194 #include "window.h"
195 #include "termhooks.h"
196 #include "termchar.h"
197 #include "termopts.h"
198 #include "dispextern.h"
199 #include "process.h"
201 #ifdef WINDOWSNT
202 #include <direct.h>
203 /* In process.h which conflicts with the local copy. */
204 #define _P_WAIT 0
205 int _CRTAPI1 _spawnlp (int, const char *, const char *, ...);
206 int _CRTAPI1 _getpid (void);
207 #endif
209 #ifdef NONSYSTEM_DIR_LIBRARY
210 #include "ndir.h"
211 #endif /* NONSYSTEM_DIR_LIBRARY */
213 #include "syssignal.h"
214 #include "systime.h"
215 #ifdef HAVE_UTIME_H
216 #include <utime.h>
217 #endif
219 #ifndef HAVE_UTIMES
220 #ifndef HAVE_STRUCT_UTIMBUF
221 /* We want to use utime rather than utimes, but we couldn't find the
222 structure declaration. We'll use the traditional one. */
223 struct utimbuf {
224 long actime;
225 long modtime;
227 #endif
228 #endif
230 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
231 #ifndef LPASS8
232 #define LPASS8 0
233 #endif
235 #ifdef BSD4_1
236 #define LNOFLSH 0100000
237 #endif
239 static int baud_convert[] =
240 #ifdef BAUD_CONVERT
241 BAUD_CONVERT;
242 #else
244 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
245 1800, 2400, 4800, 9600, 19200, 38400
247 #endif
249 #ifdef HAVE_SPEED_T
250 #include <termios.h>
251 #else
252 #if defined (HAVE_LIBNCURSES) && ! defined (NCURSES_OSPEED_T)
253 #else
254 #if defined (HAVE_TERMIOS_H) && defined (LINUX)
255 #include <termios.h>
256 #endif
257 #endif
258 #endif
260 int emacs_ospeed;
262 /* The file descriptor for Emacs's input terminal.
263 Under Unix, this is normally zero except when using X;
264 under VMS, we place the input channel number here. */
265 int input_fd;
267 void croak P_ ((char *));
269 #ifdef AIXHFT
270 void hft_init ();
271 void hft_reset ();
272 #endif
274 /* Temporary used by `sigblock' when defined in terms of signprocmask. */
276 SIGMASKTYPE sigprocmask_set;
279 /* Specify a different file descriptor for further input operations. */
281 void
282 change_input_fd (fd)
283 int fd;
285 input_fd = fd;
288 /* Discard pending input on descriptor input_fd. */
290 void
291 discard_tty_input ()
293 #ifndef WINDOWSNT
294 struct emacs_tty buf;
296 if (noninteractive)
297 return;
299 /* Discarding input is not safe when the input could contain
300 replies from the X server. So don't do it. */
301 if (read_socket_hook)
302 return;
304 #ifdef VMS
305 end_kbd_input ();
306 SYS$QIOW (0, input_fd, IO$_READVBLK|IO$M_PURGE, input_iosb, 0, 0,
307 &buf.main, 0, 0, terminator_mask, 0, 0);
308 queue_kbd_input ();
309 #else /* not VMS */
310 #ifdef APOLLO
312 int zero = 0;
313 ioctl (input_fd, TIOCFLUSH, &zero);
315 #else /* not Apollo */
316 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
317 while (dos_keyread () != -1)
319 #else /* not MSDOS */
320 EMACS_GET_TTY (input_fd, &buf);
321 EMACS_SET_TTY (input_fd, &buf, 0);
322 #endif /* not MSDOS */
323 #endif /* not Apollo */
324 #endif /* not VMS */
325 #endif /* not WINDOWSNT */
328 #ifdef SIGTSTP
330 /* Arrange for character C to be read as the next input from
331 the terminal. */
333 void
334 stuff_char (c)
335 char c;
337 if (read_socket_hook)
338 return;
340 /* Should perhaps error if in batch mode */
341 #ifdef TIOCSTI
342 ioctl (input_fd, TIOCSTI, &c);
343 #else /* no TIOCSTI */
344 error ("Cannot stuff terminal input characters in this version of Unix");
345 #endif /* no TIOCSTI */
348 #endif /* SIGTSTP */
350 void
351 init_baud_rate ()
353 if (noninteractive)
354 emacs_ospeed = 0;
355 else
357 #ifdef INIT_BAUD_RATE
358 INIT_BAUD_RATE ();
359 #else
360 #ifdef DOS_NT
361 emacs_ospeed = 15;
362 #else /* not DOS_NT */
363 #ifdef VMS
364 struct sensemode sg;
366 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &sg, 0, 0,
367 &sg.class, 12, 0, 0, 0, 0 );
368 emacs_ospeed = sg.xmit_baud;
369 #else /* not VMS */
370 #ifdef HAVE_TERMIOS
371 struct termios sg;
373 sg.c_cflag = B9600;
374 tcgetattr (input_fd, &sg);
375 emacs_ospeed = cfgetospeed (&sg);
376 #if defined (USE_GETOBAUD) && defined (getobaud)
377 /* m88k-motorola-sysv3 needs this (ghazi@noc.rutgers.edu) 9/1/94. */
378 if (emacs_ospeed == 0)
379 emacs_ospeed = getobaud (sg.c_cflag);
380 #endif
381 #else /* neither VMS nor TERMIOS */
382 #ifdef HAVE_TERMIO
383 struct termio sg;
385 sg.c_cflag = B9600;
386 #ifdef HAVE_TCATTR
387 tcgetattr (input_fd, &sg);
388 #else
389 ioctl (input_fd, TCGETA, &sg);
390 #endif
391 emacs_ospeed = sg.c_cflag & CBAUD;
392 #else /* neither VMS nor TERMIOS nor TERMIO */
393 struct sgttyb sg;
395 sg.sg_ospeed = B9600;
396 if (ioctl (input_fd, TIOCGETP, &sg) < 0)
397 abort ();
398 emacs_ospeed = sg.sg_ospeed;
399 #endif /* not HAVE_TERMIO */
400 #endif /* not HAVE_TERMIOS */
401 #endif /* not VMS */
402 #endif /* not DOS_NT */
403 #endif /* not INIT_BAUD_RATE */
406 baud_rate = (emacs_ospeed < sizeof baud_convert / sizeof baud_convert[0]
407 ? baud_convert[emacs_ospeed] : 9600);
408 if (baud_rate == 0)
409 baud_rate = 1200;
412 /*ARGSUSED*/
413 void
414 set_exclusive_use (fd)
415 int fd;
417 #ifdef FIOCLEX
418 ioctl (fd, FIOCLEX, 0);
419 #endif
420 /* Ok to do nothing if this feature does not exist */
423 #ifndef subprocesses
425 wait_without_blocking ()
427 #ifdef BSD_SYSTEM
428 wait3 (0, WNOHANG | WUNTRACED, 0);
429 #else
430 croak ("wait_without_blocking");
431 #endif
432 synch_process_alive = 0;
435 #endif /* not subprocesses */
437 int wait_debugging; /* Set nonzero to make following function work under dbx
438 (at least for bsd). */
440 SIGTYPE
441 wait_for_termination_signal ()
444 /* Wait for subprocess with process id `pid' to terminate and
445 make sure it will get eliminated (not remain forever as a zombie) */
447 void
448 wait_for_termination (pid)
449 int pid;
451 while (1)
453 #ifdef subprocesses
454 #ifdef VMS
455 int status;
457 status = SYS$FORCEX (&pid, 0, 0);
458 break;
459 #else /* not VMS */
460 #if defined (BSD_SYSTEM) || (defined (HPUX) && !defined (HPUX_5))
461 /* Note that kill returns -1 even if the process is just a zombie now.
462 But inevitably a SIGCHLD interrupt should be generated
463 and child_sig will do wait3 and make the process go away. */
464 /* There is some indication that there is a bug involved with
465 termination of subprocesses, perhaps involving a kernel bug too,
466 but no idea what it is. Just as a hunch we signal SIGCHLD to see
467 if that causes the problem to go away or get worse. */
468 sigsetmask (sigmask (SIGCHLD));
469 if (0 > kill (pid, 0))
471 sigsetmask (SIGEMPTYMASK);
472 kill (getpid (), SIGCHLD);
473 break;
475 if (wait_debugging)
476 sleep (1);
477 else
478 sigpause (SIGEMPTYMASK);
479 #else /* not BSD_SYSTEM, and not HPUX version >= 6 */
480 #if defined (UNIPLUS)
481 if (0 > kill (pid, 0))
482 break;
483 wait (0);
484 #else /* neither BSD_SYSTEM nor UNIPLUS: random sysV */
485 #ifdef POSIX_SIGNALS /* would this work for LINUX as well? */
486 sigblock (sigmask (SIGCHLD));
487 errno = 0;
488 if (kill (pid, 0) == -1 && errno == ESRCH)
490 sigunblock (sigmask (SIGCHLD));
491 break;
494 /* FIXME: Since sigpause is not POSIX and its use is deprecated,
495 this should probably be `sigsuspend (&empty_mask)', which is
496 POSIX. I'm not making that change right away because the
497 release is nearing. 2001-09-20 gerd. */
498 sigpause (SIGEMPTYMASK);
499 #else /* not POSIX_SIGNALS */
500 #ifdef HAVE_SYSV_SIGPAUSE
501 sighold (SIGCHLD);
502 if (0 > kill (pid, 0))
504 sigrelse (SIGCHLD);
505 break;
507 sigpause (SIGCHLD);
508 #else /* not HAVE_SYSV_SIGPAUSE */
509 #ifdef WINDOWSNT
510 wait (0);
511 break;
512 #else /* not WINDOWSNT */
513 if (0 > kill (pid, 0))
514 break;
515 /* Using sleep instead of pause avoids timing error.
516 If the inferior dies just before the sleep,
517 we lose just one second. */
518 sleep (1);
519 #endif /* not WINDOWSNT */
520 #endif /* not HAVE_SYSV_SIGPAUSE */
521 #endif /* not POSIX_SIGNALS */
522 #endif /* not UNIPLUS */
523 #endif /* not BSD_SYSTEM, and not HPUX version >= 6 */
524 #endif /* not VMS */
525 #else /* not subprocesses */
526 #if __DJGPP__ > 1
527 break;
528 #else /* not __DJGPP__ > 1 */
529 #ifndef BSD4_1
530 if (kill (pid, 0) < 0)
531 break;
532 wait (0);
533 #else /* BSD4_1 */
534 int status;
535 status = wait (0);
536 if (status == pid || status == -1)
537 break;
538 #endif /* BSD4_1 */
539 #endif /* not __DJGPP__ > 1*/
540 #endif /* not subprocesses */
544 #ifdef subprocesses
547 * flush any pending output
548 * (may flush input as well; it does not matter the way we use it)
551 void
552 flush_pending_output (channel)
553 int channel;
555 #ifdef HAVE_TERMIOS
556 /* If we try this, we get hit with SIGTTIN, because
557 the child's tty belongs to the child's pgrp. */
558 #else
559 #ifdef TCFLSH
560 ioctl (channel, TCFLSH, 1);
561 #else
562 #ifdef TIOCFLUSH
563 int zero = 0;
564 /* 3rd arg should be ignored
565 but some 4.2 kernels actually want the address of an int
566 and nonzero means something different. */
567 ioctl (channel, TIOCFLUSH, &zero);
568 #endif
569 #endif
570 #endif
573 #ifndef VMS
574 /* Set up the terminal at the other end of a pseudo-terminal that
575 we will be controlling an inferior through.
576 It should not echo or do line-editing, since that is done
577 in Emacs. No padding needed for insertion into an Emacs buffer. */
579 void
580 child_setup_tty (out)
581 int out;
583 #ifndef DOS_NT
584 struct emacs_tty s;
586 EMACS_GET_TTY (out, &s);
588 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
589 s.main.c_oflag |= OPOST; /* Enable output postprocessing */
590 s.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL on output */
591 #ifdef NLDLY
592 s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
593 /* No output delays */
594 #endif
595 s.main.c_lflag &= ~ECHO; /* Disable echo */
596 s.main.c_lflag |= ISIG; /* Enable signals */
597 #if 0 /* This causes bugs in (for instance) telnet to certain sites. */
598 s.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
599 #ifdef INLCR /* Just being cautious, since I can't check how
600 widespread INLCR is--rms. */
601 s.main.c_iflag &= ~INLCR; /* Disable map of NL to CR on input */
602 #endif
603 #endif
604 #ifdef IUCLC
605 s.main.c_iflag &= ~IUCLC; /* Disable downcasing on input. */
606 #endif
607 #ifdef ISTRIP
608 s.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
609 #endif
610 #ifdef OLCUC
611 s.main.c_oflag &= ~OLCUC; /* Disable upcasing on output. */
612 #endif
613 s.main.c_oflag &= ~TAB3; /* Disable tab expansion */
614 s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
615 #if 0
616 /* Said to be unnecessary: */
617 s.main.c_cc[VMIN] = 1; /* minimum number of characters to accept */
618 s.main.c_cc[VTIME] = 0; /* wait forever for at least 1 character */
619 #endif
621 s.main.c_lflag |= ICANON; /* Enable erase/kill and eof processing */
622 s.main.c_cc[VEOF] = 04; /* insure that EOF is Control-D */
623 s.main.c_cc[VERASE] = CDISABLE; /* disable erase processing */
624 s.main.c_cc[VKILL] = CDISABLE; /* disable kill processing */
626 #ifdef HPUX
627 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
628 #endif /* HPUX */
630 #ifdef AIX
631 /* AIX enhanced edit loses NULs, so disable it */
632 #ifndef IBMR2AIX
633 s.main.c_line = 0;
634 s.main.c_iflag &= ~ASCEDIT;
635 #endif
636 /* Also, PTY overloads NUL and BREAK.
637 don't ignore break, but don't signal either, so it looks like NUL. */
638 s.main.c_iflag &= ~IGNBRK;
639 s.main.c_iflag &= ~BRKINT;
640 /* QUIT and INTR work better as signals, so disable character forms */
641 s.main.c_cc[VINTR] = 0377;
642 #ifdef SIGNALS_VIA_CHARACTERS
643 /* the QUIT and INTR character are used in process_send_signal
644 so set them here to something useful. */
645 if (s.main.c_cc[VQUIT] == 0377)
646 s.main.c_cc[VQUIT] = '\\'&037; /* Control-\ */
647 if (s.main.c_cc[VINTR] == 0377)
648 s.main.c_cc[VINTR] = 'C'&037; /* Control-C */
649 #else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
650 /* QUIT and INTR work better as signals, so disable character forms */
651 s.main.c_cc[VQUIT] = 0377;
652 s.main.c_cc[VINTR] = 0377;
653 s.main.c_lflag &= ~ISIG;
654 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
655 s.main.c_cc[VEOL] = 0377;
656 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
657 #endif /* AIX */
659 #else /* not HAVE_TERMIO */
661 s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
662 | CBREAK | TANDEM);
663 s.main.sg_flags |= LPASS8;
664 s.main.sg_erase = 0377;
665 s.main.sg_kill = 0377;
666 s.lmode = LLITOUT | s.lmode; /* Don't strip 8th bit */
668 #endif /* not HAVE_TERMIO */
670 EMACS_SET_TTY (out, &s, 0);
672 #ifdef BSD4_1
673 if (interrupt_input)
674 reset_sigio ();
675 #endif /* BSD4_1 */
676 #ifdef RTU
678 int zero = 0;
679 ioctl (out, FIOASYNC, &zero);
681 #endif /* RTU */
682 #endif /* not DOS_NT */
684 #endif /* not VMS */
686 #endif /* subprocesses */
688 /* Record a signal code and the handler for it. */
689 struct save_signal
691 int code;
692 SIGTYPE (*handler) P_ ((int));
695 static void save_signal_handlers P_ ((struct save_signal *));
696 static void restore_signal_handlers P_ ((struct save_signal *));
698 /* Suspend the Emacs process; give terminal to its superior. */
700 void
701 sys_suspend ()
703 #ifdef VMS
704 /* "Foster" parentage allows emacs to return to a subprocess that attached
705 to the current emacs as a cheaper than starting a whole new process. This
706 is set up by KEPTEDITOR.COM. */
707 unsigned long parent_id, foster_parent_id;
708 char *fpid_string;
710 fpid_string = getenv ("EMACS_PARENT_PID");
711 if (fpid_string != NULL)
713 sscanf (fpid_string, "%x", &foster_parent_id);
714 if (foster_parent_id != 0)
715 parent_id = foster_parent_id;
716 else
717 parent_id = getppid ();
719 else
720 parent_id = getppid ();
722 xfree (fpid_string); /* On VMS, this was malloc'd */
724 if (parent_id && parent_id != 0xffffffff)
726 SIGTYPE (*oldsig)() = (int) signal (SIGINT, SIG_IGN);
727 int status = LIB$ATTACH (&parent_id) & 1;
728 signal (SIGINT, oldsig);
729 return status;
731 else
733 struct {
734 int l;
735 char *a;
736 } d_prompt;
737 d_prompt.l = sizeof ("Emacs: "); /* Our special prompt */
738 d_prompt.a = "Emacs: "; /* Just a reminder */
739 LIB$SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt, 0);
740 return 1;
742 return -1;
743 #else
744 #if defined (SIGTSTP) && !defined (MSDOS)
747 int pgrp = EMACS_GETPGRP (0);
748 EMACS_KILLPG (pgrp, SIGTSTP);
751 #else /* No SIGTSTP */
752 #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
753 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
754 kill (getpid (), SIGQUIT);
756 #else /* No SIGTSTP or USG_JOBCTRL */
758 /* On a system where suspending is not implemented,
759 instead fork a subshell and let it talk directly to the terminal
760 while we wait. */
761 sys_subshell ();
763 #endif /* no USG_JOBCTRL */
764 #endif /* no SIGTSTP */
765 #endif /* not VMS */
768 /* Fork a subshell. */
770 #ifndef macintosh
771 void
772 sys_subshell ()
774 #ifndef VMS
775 #ifdef DOS_NT /* Demacs 1.1.2 91/10/20 Manabu Higashida */
776 int st;
777 char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS. */
778 #endif
779 int pid;
780 struct save_signal saved_handlers[5];
781 Lisp_Object dir;
782 unsigned char *str = 0;
783 int len;
785 saved_handlers[0].code = SIGINT;
786 saved_handlers[1].code = SIGQUIT;
787 saved_handlers[2].code = SIGTERM;
788 #ifdef SIGIO
789 saved_handlers[3].code = SIGIO;
790 saved_handlers[4].code = 0;
791 #else
792 saved_handlers[3].code = 0;
793 #endif
795 /* Mentioning current_buffer->buffer would mean including buffer.h,
796 which somehow wedges the hp compiler. So instead... */
798 dir = intern ("default-directory");
799 if (NILP (Fboundp (dir)))
800 goto xyzzy;
801 dir = Fsymbol_value (dir);
802 if (!STRINGP (dir))
803 goto xyzzy;
805 dir = expand_and_dir_to_file (Funhandled_file_name_directory (dir), Qnil);
806 str = (unsigned char *) alloca (XSTRING (dir)->size + 2);
807 len = XSTRING (dir)->size;
808 bcopy (XSTRING (dir)->data, str, len);
809 if (str[len - 1] != '/') str[len++] = '/';
810 str[len] = 0;
811 xyzzy:
813 #ifdef DOS_NT
814 pid = 0;
815 #if __DJGPP__ > 1
816 save_signal_handlers (saved_handlers);
817 synch_process_alive = 1;
818 #endif /* __DJGPP__ > 1 */
819 #else
820 pid = vfork ();
821 if (pid == -1)
822 error ("Can't spawn subshell");
823 #endif
825 if (pid == 0)
827 char *sh = 0;
829 #ifdef DOS_NT /* MW, Aug 1993 */
830 getwd (oldwd);
831 if (sh == 0)
832 sh = (char *) egetenv ("SUSPEND"); /* KFS, 1994-12-14 */
833 #endif
834 if (sh == 0)
835 sh = (char *) egetenv ("SHELL");
836 if (sh == 0)
837 sh = "sh";
839 /* Use our buffer's default directory for the subshell. */
840 if (str)
841 chdir ((char *) str);
843 #ifdef subprocesses
844 close_process_descs (); /* Close Emacs's pipes/ptys */
845 #endif
847 #ifdef SET_EMACS_PRIORITY
849 extern int emacs_priority;
851 if (emacs_priority < 0)
852 nice (-emacs_priority);
854 #endif
856 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
857 st = system (sh);
858 chdir (oldwd);
859 #if 0 /* This is also reported if last command executed in subshell failed, KFS */
860 if (st)
861 report_file_error ("Can't execute subshell", Fcons (build_string (sh), Qnil));
862 #endif
863 #else /* not MSDOS */
864 #ifdef WINDOWSNT
865 /* Waits for process completion */
866 pid = _spawnlp (_P_WAIT, sh, sh, NULL);
867 chdir (oldwd);
868 if (pid == -1)
869 write (1, "Can't execute subshell", 22);
870 #else /* not WINDOWSNT */
871 execlp (sh, sh, 0);
872 write (1, "Can't execute subshell", 22);
873 _exit (1);
874 #endif /* not WINDOWSNT */
875 #endif /* not MSDOS */
878 /* Do this now if we did not do it before. */
879 #if !defined (MSDOS) || __DJGPP__ == 1
880 save_signal_handlers (saved_handlers);
881 synch_process_alive = 1;
882 #endif
884 #ifndef DOS_NT
885 wait_for_termination (pid);
886 #endif
887 restore_signal_handlers (saved_handlers);
888 synch_process_alive = 0;
889 #endif /* !VMS */
891 #endif /* !macintosh */
893 static void
894 save_signal_handlers (saved_handlers)
895 struct save_signal *saved_handlers;
897 while (saved_handlers->code)
899 saved_handlers->handler
900 = (SIGTYPE (*) P_ ((int))) signal (saved_handlers->code, SIG_IGN);
901 saved_handlers++;
905 static void
906 restore_signal_handlers (saved_handlers)
907 struct save_signal *saved_handlers;
909 while (saved_handlers->code)
911 signal (saved_handlers->code, saved_handlers->handler);
912 saved_handlers++;
916 #ifdef F_SETFL
918 int old_fcntl_flags;
920 void
921 init_sigio (fd)
922 int fd;
924 #ifdef FASYNC
925 old_fcntl_flags = fcntl (fd, F_GETFL, 0) & ~FASYNC;
926 fcntl (fd, F_SETFL, old_fcntl_flags | FASYNC);
927 #endif
928 interrupts_deferred = 0;
931 void
932 reset_sigio ()
934 unrequest_sigio ();
937 #ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
939 void
940 request_sigio ()
942 if (read_socket_hook)
943 return;
945 #ifdef SIGWINCH
946 sigunblock (sigmask (SIGWINCH));
947 #endif
948 fcntl (input_fd, F_SETFL, old_fcntl_flags | FASYNC);
950 interrupts_deferred = 0;
953 void
954 unrequest_sigio ()
956 if (read_socket_hook)
957 return;
959 #ifdef SIGWINCH
960 sigblock (sigmask (SIGWINCH));
961 #endif
962 fcntl (input_fd, F_SETFL, old_fcntl_flags);
963 interrupts_deferred = 1;
966 #else /* no FASYNC */
967 #ifdef STRIDE /* Stride doesn't have FASYNC - use FIOASYNC */
969 void
970 request_sigio ()
972 int on = 1;
974 if (read_socket_hook)
975 return;
977 ioctl (input_fd, FIOASYNC, &on);
978 interrupts_deferred = 0;
981 void
982 unrequest_sigio ()
984 int off = 0;
986 if (read_socket_hook)
987 return;
989 ioctl (input_fd, FIOASYNC, &off);
990 interrupts_deferred = 1;
993 #else /* not FASYNC, not STRIDE */
995 #ifdef _CX_UX
997 #include <termios.h>
999 void
1000 request_sigio ()
1002 int on = 1;
1003 sigset_t st;
1005 if (read_socket_hook)
1006 return;
1008 sigemptyset (&st);
1009 sigaddset (&st, SIGIO);
1010 ioctl (input_fd, FIOASYNC, &on);
1011 interrupts_deferred = 0;
1012 sigprocmask (SIG_UNBLOCK, &st, (sigset_t *)0);
1015 void
1016 unrequest_sigio ()
1018 int off = 0;
1020 if (read_socket_hook)
1021 return;
1023 ioctl (input_fd, FIOASYNC, &off);
1024 interrupts_deferred = 1;
1027 #else /* ! _CX_UX */
1028 #ifndef MSDOS
1030 void
1031 request_sigio ()
1033 if (read_socket_hook)
1034 return;
1036 croak ("request_sigio");
1039 void
1040 unrequest_sigio ()
1042 if (read_socket_hook)
1043 return;
1045 croak ("unrequest_sigio");
1048 #endif /* MSDOS */
1049 #endif /* _CX_UX */
1050 #endif /* STRIDE */
1051 #endif /* FASYNC */
1052 #endif /* F_SETFL */
1054 /* Saving and restoring the process group of Emacs's terminal. */
1056 #ifdef BSD_PGRPS
1058 /* The process group of which Emacs was a member when it initially
1059 started.
1061 If Emacs was in its own process group (i.e. inherited_pgroup ==
1062 getpid ()), then we know we're running under a shell with job
1063 control (Emacs would never be run as part of a pipeline).
1064 Everything is fine.
1066 If Emacs was not in its own process group, then we know we're
1067 running under a shell (or a caller) that doesn't know how to
1068 separate itself from Emacs (like sh). Emacs must be in its own
1069 process group in order to receive SIGIO correctly. In this
1070 situation, we put ourselves in our own pgroup, forcibly set the
1071 tty's pgroup to our pgroup, and make sure to restore and reinstate
1072 the tty's pgroup just like any other terminal setting. If
1073 inherited_group was not the tty's pgroup, then we'll get a
1074 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
1075 it goes foreground in the future, which is what should happen. */
1076 int inherited_pgroup;
1078 /* Split off the foreground process group to Emacs alone.
1079 When we are in the foreground, but not started in our own process
1080 group, redirect the TTY to point to our own process group. We need
1081 to be in our own process group to receive SIGIO properly. */
1082 void
1083 narrow_foreground_group ()
1085 int me = getpid ();
1087 setpgrp (0, inherited_pgroup);
1088 if (inherited_pgroup != me)
1089 EMACS_SET_TTY_PGRP (input_fd, &me);
1090 setpgrp (0, me);
1093 /* Set the tty to our original foreground group. */
1094 void
1095 widen_foreground_group ()
1097 if (inherited_pgroup != getpid ())
1098 EMACS_SET_TTY_PGRP (input_fd, &inherited_pgroup);
1099 setpgrp (0, inherited_pgroup);
1102 #endif /* BSD_PGRPS */
1104 /* Getting and setting emacs_tty structures. */
1106 /* Set *TC to the parameters associated with the terminal FD.
1107 Return zero if all's well, or -1 if we ran into an error we
1108 couldn't deal with. */
1110 emacs_get_tty (fd, settings)
1111 int fd;
1112 struct emacs_tty *settings;
1114 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
1115 #ifdef HAVE_TCATTR
1116 /* We have those nifty POSIX tcmumbleattr functions. */
1117 bzero (&settings->main, sizeof (settings->main));
1118 if (tcgetattr (fd, &settings->main) < 0)
1119 return -1;
1121 #else
1122 #ifdef HAVE_TERMIO
1123 /* The SYSV-style interface? */
1124 if (ioctl (fd, TCGETA, &settings->main) < 0)
1125 return -1;
1127 #else
1128 #ifdef VMS
1129 /* Vehemently Monstrous System? :-) */
1130 if (! (SYS$QIOW (0, fd, IO$_SENSEMODE, settings, 0, 0,
1131 &settings->main.class, 12, 0, 0, 0, 0)
1132 & 1))
1133 return -1;
1135 #else
1136 #ifndef DOS_NT
1137 /* I give up - I hope you have the BSD ioctls. */
1138 if (ioctl (fd, TIOCGETP, &settings->main) < 0)
1139 return -1;
1140 #endif /* not DOS_NT */
1141 #endif
1142 #endif
1143 #endif
1145 /* Suivant - Do we have to get struct ltchars data? */
1146 #ifdef HAVE_LTCHARS
1147 if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
1148 return -1;
1149 #endif
1151 /* How about a struct tchars and a wordful of lmode bits? */
1152 #ifdef HAVE_TCHARS
1153 if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
1154 || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
1155 return -1;
1156 #endif
1158 /* We have survived the tempest. */
1159 return 0;
1163 /* Set the parameters of the tty on FD according to the contents of
1164 *SETTINGS. If FLUSHP is non-zero, we discard input.
1165 Return 0 if all went well, and -1 if anything failed. */
1168 emacs_set_tty (fd, settings, flushp)
1169 int fd;
1170 struct emacs_tty *settings;
1171 int flushp;
1173 /* Set the primary parameters - baud rate, character size, etcetera. */
1174 #ifdef HAVE_TCATTR
1175 int i;
1176 /* We have those nifty POSIX tcmumbleattr functions.
1177 William J. Smith <wjs@wiis.wang.com> writes:
1178 "POSIX 1003.1 defines tcsetattr to return success if it was
1179 able to perform any of the requested actions, even if some
1180 of the requested actions could not be performed.
1181 We must read settings back to ensure tty setup properly.
1182 AIX requires this to keep tty from hanging occasionally." */
1183 /* This make sure that we don't loop indefinitely in here. */
1184 for (i = 0 ; i < 10 ; i++)
1185 if (tcsetattr (fd, flushp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
1187 if (errno == EINTR)
1188 continue;
1189 else
1190 return -1;
1192 else
1194 struct termios new;
1196 bzero (&new, sizeof (new));
1197 /* Get the current settings, and see if they're what we asked for. */
1198 tcgetattr (fd, &new);
1199 /* We cannot use memcmp on the whole structure here because under
1200 * aix386 the termios structure has some reserved field that may
1201 * not be filled in.
1203 if ( new.c_iflag == settings->main.c_iflag
1204 && new.c_oflag == settings->main.c_oflag
1205 && new.c_cflag == settings->main.c_cflag
1206 && new.c_lflag == settings->main.c_lflag
1207 && memcmp (new.c_cc, settings->main.c_cc, NCCS) == 0)
1208 break;
1209 else
1210 continue;
1213 #else
1214 #ifdef HAVE_TERMIO
1215 /* The SYSV-style interface? */
1216 if (ioctl (fd, flushp ? TCSETAF : TCSETAW, &settings->main) < 0)
1217 return -1;
1219 #else
1220 #ifdef VMS
1221 /* Vehemently Monstrous System? :-) */
1222 if (! (SYS$QIOW (0, fd, IO$_SETMODE, &input_iosb, 0, 0,
1223 &settings->main.class, 12, 0, 0, 0, 0)
1224 & 1))
1225 return -1;
1227 #else
1228 #ifndef DOS_NT
1229 /* I give up - I hope you have the BSD ioctls. */
1230 if (ioctl (fd, (flushp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
1231 return -1;
1232 #endif /* not DOS_NT */
1234 #endif
1235 #endif
1236 #endif
1238 /* Suivant - Do we have to get struct ltchars data? */
1239 #ifdef HAVE_LTCHARS
1240 if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
1241 return -1;
1242 #endif
1244 /* How about a struct tchars and a wordful of lmode bits? */
1245 #ifdef HAVE_TCHARS
1246 if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
1247 || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
1248 return -1;
1249 #endif
1251 /* We have survived the tempest. */
1252 return 0;
1256 /* The initial tty mode bits */
1257 struct emacs_tty old_tty;
1259 /* 1 if we have been through init_sys_modes. */
1260 int term_initted;
1262 /* 1 if outer tty status has been recorded. */
1263 int old_tty_valid;
1265 #ifdef BSD4_1
1266 /* BSD 4.1 needs to keep track of the lmode bits in order to start
1267 sigio. */
1268 int lmode;
1269 #endif
1271 #ifndef F_SETOWN_BUG
1272 #ifdef F_SETOWN
1273 int old_fcntl_owner;
1274 #endif /* F_SETOWN */
1275 #endif /* F_SETOWN_BUG */
1277 /* This may also be defined in stdio,
1278 but if so, this does no harm,
1279 and using the same name avoids wasting the other one's space. */
1281 #ifdef nec_ews_svr4
1282 extern char *_sobuf ;
1283 #else
1284 #if defined (USG) || defined (DGUX)
1285 unsigned char _sobuf[BUFSIZ+8];
1286 #else
1287 char _sobuf[BUFSIZ];
1288 #endif
1289 #endif
1291 #ifdef HAVE_LTCHARS
1292 static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
1293 #endif
1294 #ifdef HAVE_TCHARS
1295 static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
1296 #endif
1298 void
1299 init_sys_modes ()
1301 struct emacs_tty tty;
1303 #ifdef macintosh
1304 /* cus-start.el complains if delete-exited-processes is not defined */
1305 #ifndef subprocesses
1306 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
1307 "*Non-nil means delete processes immediately when they exit.\n\
1308 nil means don't delete them until `list-processes' is run.");
1309 delete_exited_processes = 0;
1310 #endif
1311 #endif /* not macintosh */
1313 #ifdef VMS
1314 #if 0
1315 static int oob_chars[2] = {0, 1 << 7}; /* catch C-g's */
1316 extern int (*interrupt_signal) ();
1317 #endif
1318 #endif
1320 Vtty_erase_char = Qnil;
1322 if (noninteractive)
1323 return;
1325 #ifdef VMS
1326 if (!input_ef)
1327 input_ef = get_kbd_event_flag ();
1328 /* LIB$GET_EF (&input_ef); */
1329 SYS$CLREF (input_ef);
1330 waiting_for_ast = 0;
1331 if (!timer_ef)
1332 timer_ef = get_timer_event_flag ();
1333 /* LIB$GET_EF (&timer_ef); */
1334 SYS$CLREF (timer_ef);
1335 #if 0
1336 if (!process_ef)
1338 LIB$GET_EF (&process_ef);
1339 SYS$CLREF (process_ef);
1341 if (input_ef / 32 != process_ef / 32)
1342 croak ("Input and process event flags in different clusters.");
1343 #endif
1344 if (input_ef / 32 != timer_ef / 32)
1345 croak ("Input and timer event flags in different clusters.");
1346 #if 0
1347 input_eflist = ((unsigned) 1 << (input_ef % 32)) |
1348 ((unsigned) 1 << (process_ef % 32));
1349 #endif
1350 timer_eflist = ((unsigned) 1 << (input_ef % 32)) |
1351 ((unsigned) 1 << (timer_ef % 32));
1352 #ifndef VMS4_4
1353 sys_access_reinit ();
1354 #endif
1355 #endif /* not VMS */
1357 #ifdef BSD_PGRPS
1358 if (! read_socket_hook && EQ (Vwindow_system, Qnil))
1359 narrow_foreground_group ();
1360 #endif
1362 #ifdef HAVE_WINDOW_SYSTEM
1363 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1364 needs the initialization code below. */
1365 if (!read_socket_hook && EQ (Vwindow_system, Qnil))
1366 #endif
1368 EMACS_GET_TTY (input_fd, &old_tty);
1370 old_tty_valid = 1;
1372 tty = old_tty;
1374 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
1375 XSETINT (Vtty_erase_char, old_tty.main.c_cc[VERASE]);
1377 #ifdef DGUX
1378 /* This allows meta to be sent on 8th bit. */
1379 tty.main.c_iflag &= ~INPCK; /* don't check input for parity */
1380 #endif
1381 tty.main.c_iflag |= (IGNBRK); /* Ignore break condition */
1382 tty.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
1383 #ifdef INLCR /* I'm just being cautious,
1384 since I can't check how widespread INLCR is--rms. */
1385 tty.main.c_iflag &= ~INLCR; /* Disable map of NL to CR on input */
1386 #endif
1387 #ifdef ISTRIP
1388 tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
1389 #endif
1390 tty.main.c_lflag &= ~ECHO; /* Disable echo */
1391 tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */
1392 #ifdef IEXTEN
1393 tty.main.c_lflag &= ~IEXTEN; /* Disable other editing characters. */
1394 #endif
1395 tty.main.c_lflag |= ISIG; /* Enable signals */
1396 if (flow_control)
1398 tty.main.c_iflag |= IXON; /* Enable start/stop output control */
1399 #ifdef IXANY
1400 tty.main.c_iflag &= ~IXANY;
1401 #endif /* IXANY */
1403 else
1404 tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */
1405 tty.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL
1406 on output */
1407 tty.main.c_oflag &= ~TAB3; /* Disable tab expansion */
1408 #ifdef CS8
1409 if (meta_key)
1411 tty.main.c_cflag |= CS8; /* allow 8th bit on input */
1412 tty.main.c_cflag &= ~PARENB;/* Don't check parity */
1414 #endif
1415 tty.main.c_cc[VINTR] = quit_char; /* C-g (usually) gives SIGINT */
1416 /* Set up C-g for both SIGQUIT and SIGINT.
1417 We don't know which we will get, but we handle both alike
1418 so which one it really gives us does not matter. */
1419 tty.main.c_cc[VQUIT] = quit_char;
1420 tty.main.c_cc[VMIN] = 1; /* Input should wait for at least 1 char */
1421 tty.main.c_cc[VTIME] = 0; /* no matter how long that takes. */
1422 #ifdef VSWTCH
1423 tty.main.c_cc[VSWTCH] = CDISABLE; /* Turn off shell layering use
1424 of C-z */
1425 #endif /* VSWTCH */
1427 #if defined (mips) || defined (HAVE_TCATTR)
1428 #ifdef VSUSP
1429 tty.main.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z. */
1430 #endif /* VSUSP */
1431 #ifdef V_DSUSP
1432 tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y. */
1433 #endif /* V_DSUSP */
1434 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1435 tty.main.c_cc[VDSUSP] = CDISABLE;
1436 #endif /* VDSUSP */
1437 #ifdef VLNEXT
1438 tty.main.c_cc[VLNEXT] = CDISABLE;
1439 #endif /* VLNEXT */
1440 #ifdef VREPRINT
1441 tty.main.c_cc[VREPRINT] = CDISABLE;
1442 #endif /* VREPRINT */
1443 #ifdef VWERASE
1444 tty.main.c_cc[VWERASE] = CDISABLE;
1445 #endif /* VWERASE */
1446 #ifdef VDISCARD
1447 tty.main.c_cc[VDISCARD] = CDISABLE;
1448 #endif /* VDISCARD */
1450 if (flow_control)
1452 #ifdef VSTART
1453 tty.main.c_cc[VSTART] = '\021';
1454 #endif /* VSTART */
1455 #ifdef VSTOP
1456 tty.main.c_cc[VSTOP] = '\023';
1457 #endif /* VSTOP */
1459 else
1461 #ifdef VSTART
1462 tty.main.c_cc[VSTART] = CDISABLE;
1463 #endif /* VSTART */
1464 #ifdef VSTOP
1465 tty.main.c_cc[VSTOP] = CDISABLE;
1466 #endif /* VSTOP */
1468 #endif /* mips or HAVE_TCATTR */
1470 #ifdef SET_LINE_DISCIPLINE
1471 /* Need to explicitly request TERMIODISC line discipline or
1472 Ultrix's termios does not work correctly. */
1473 tty.main.c_line = SET_LINE_DISCIPLINE;
1474 #endif
1475 #ifdef AIX
1476 #ifndef IBMR2AIX
1477 /* AIX enhanced edit loses NULs, so disable it. */
1478 tty.main.c_line = 0;
1479 tty.main.c_iflag &= ~ASCEDIT;
1480 #else
1481 tty.main.c_cc[VSTRT] = 255;
1482 tty.main.c_cc[VSTOP] = 255;
1483 tty.main.c_cc[VSUSP] = 255;
1484 tty.main.c_cc[VDSUSP] = 255;
1485 #endif /* IBMR2AIX */
1486 if (flow_control)
1488 #ifdef VSTART
1489 tty.main.c_cc[VSTART] = '\021';
1490 #endif /* VSTART */
1491 #ifdef VSTOP
1492 tty.main.c_cc[VSTOP] = '\023';
1493 #endif /* VSTOP */
1495 /* Also, PTY overloads NUL and BREAK.
1496 don't ignore break, but don't signal either, so it looks like NUL.
1497 This really serves a purpose only if running in an XTERM window
1498 or via TELNET or the like, but does no harm elsewhere. */
1499 tty.main.c_iflag &= ~IGNBRK;
1500 tty.main.c_iflag &= ~BRKINT;
1501 #endif
1502 #else /* if not HAVE_TERMIO */
1503 #ifdef VMS
1504 tty.main.tt_char |= TT$M_NOECHO;
1505 if (meta_key)
1506 tty.main.tt_char |= TT$M_EIGHTBIT;
1507 if (flow_control)
1508 tty.main.tt_char |= TT$M_TTSYNC;
1509 else
1510 tty.main.tt_char &= ~TT$M_TTSYNC;
1511 tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
1512 #else /* not VMS (BSD, that is) */
1513 #ifndef DOS_NT
1514 XSETINT (Vtty_erase_char, tty.main.sg_erase);
1515 tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
1516 if (meta_key)
1517 tty.main.sg_flags |= ANYP;
1518 tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
1519 #endif /* not DOS_NT */
1520 #endif /* not VMS (BSD, that is) */
1521 #endif /* not HAVE_TERMIO */
1523 /* If going to use CBREAK mode, we must request C-g to interrupt
1524 and turn off start and stop chars, etc. If not going to use
1525 CBREAK mode, do this anyway so as to turn off local flow
1526 control for user coming over network on 4.2; in this case,
1527 only t_stopc and t_startc really matter. */
1528 #ifndef HAVE_TERMIO
1529 #ifdef HAVE_TCHARS
1530 /* Note: if not using CBREAK mode, it makes no difference how we
1531 set this */
1532 tty.tchars = new_tchars;
1533 tty.tchars.t_intrc = quit_char;
1534 if (flow_control)
1536 tty.tchars.t_startc = '\021';
1537 tty.tchars.t_stopc = '\023';
1540 tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | old_tty.lmode;
1541 #ifdef ultrix
1542 /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
1543 anything, and leaving it in breaks the meta key. Go figure. */
1544 tty.lmode &= ~LLITOUT;
1545 #endif
1547 #ifdef BSD4_1
1548 lmode = tty.lmode;
1549 #endif
1551 #endif /* HAVE_TCHARS */
1552 #endif /* not HAVE_TERMIO */
1554 #ifdef HAVE_LTCHARS
1555 tty.ltchars = new_ltchars;
1556 #endif /* HAVE_LTCHARS */
1557 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
1558 if (!term_initted)
1559 internal_terminal_init ();
1560 dos_ttraw ();
1561 #endif
1563 EMACS_SET_TTY (input_fd, &tty, 0);
1565 /* This code added to insure that, if flow-control is not to be used,
1566 we have an unlocked terminal at the start. */
1568 #ifdef TCXONC
1569 if (!flow_control) ioctl (input_fd, TCXONC, 1);
1570 #endif
1571 #ifndef APOLLO
1572 #ifdef TIOCSTART
1573 if (!flow_control) ioctl (input_fd, TIOCSTART, 0);
1574 #endif
1575 #endif
1577 #if defined (HAVE_TERMIOS) || defined (HPUX9)
1578 #ifdef TCOON
1579 if (!flow_control) tcflow (input_fd, TCOON);
1580 #endif
1581 #endif
1583 #ifdef AIXHFT
1584 hft_init ();
1585 #ifdef IBMR2AIX
1587 /* IBM's HFT device usually thinks a ^J should be LF/CR. We need it
1588 to be only LF. This is the way that is done. */
1589 struct termio tty;
1591 if (ioctl (1, HFTGETID, &tty) != -1)
1592 write (1, "\033[20l", 5);
1594 #endif
1595 #endif /* AIXHFT */
1597 #ifdef VMS
1598 /* Appears to do nothing when in PASTHRU mode.
1599 SYS$QIOW (0, input_fd, IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
1600 interrupt_signal, oob_chars, 0, 0, 0, 0);
1602 queue_kbd_input (0);
1603 #endif /* VMS */
1606 #ifdef F_SETFL
1607 #ifndef F_SETOWN_BUG
1608 #ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
1609 if (interrupt_input
1610 && ! read_socket_hook && EQ (Vwindow_system, Qnil))
1612 old_fcntl_owner = fcntl (input_fd, F_GETOWN, 0);
1613 fcntl (input_fd, F_SETOWN, getpid ());
1614 init_sigio (input_fd);
1616 #endif /* F_GETOWN */
1617 #endif /* F_SETOWN_BUG */
1618 #endif /* F_SETFL */
1620 #ifdef BSD4_1
1621 if (interrupt_input)
1622 init_sigio (input_fd);
1623 #endif
1625 #ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1626 #undef _IOFBF
1627 #endif
1628 #ifdef _IOFBF
1629 /* This symbol is defined on recent USG systems.
1630 Someone says without this call USG won't really buffer the file
1631 even with a call to setbuf. */
1632 setvbuf (stdout, (char *) _sobuf, _IOFBF, sizeof _sobuf);
1633 #else
1634 setbuf (stdout, (char *) _sobuf);
1635 #endif
1636 #ifdef HAVE_WINDOW_SYSTEM
1637 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1638 needs the initialization code below. */
1639 if (EQ (Vwindow_system, Qnil)
1640 #ifndef WINDOWSNT
1641 /* When running in tty mode on NT/Win95, we have a read_socket
1642 hook, but still need the rest of the initialization code below. */
1643 && (! read_socket_hook)
1644 #endif
1646 #endif
1647 set_terminal_modes ();
1649 if (!term_initted
1650 && FRAMEP (Vterminal_frame)
1651 && FRAME_TERMCAP_P (XFRAME (Vterminal_frame)))
1652 init_frame_faces (XFRAME (Vterminal_frame));
1654 if (term_initted && no_redraw_on_reenter)
1656 if (display_completed)
1657 direct_output_forward_char (0);
1659 else
1661 frame_garbaged = 1;
1662 if (FRAMEP (Vterminal_frame))
1663 FRAME_GARBAGED_P (XFRAME (Vterminal_frame)) = 1;
1666 term_initted = 1;
1669 /* Return nonzero if safe to use tabs in output.
1670 At the time this is called, init_sys_modes has not been done yet. */
1673 tabs_safe_p ()
1675 struct emacs_tty tty;
1677 EMACS_GET_TTY (input_fd, &tty);
1678 return EMACS_TTY_TABS_OK (&tty);
1681 /* Get terminal size from system.
1682 Store number of lines into *HEIGHTP and width into *WIDTHP.
1683 We store 0 if there's no valid information. */
1685 void
1686 get_frame_size (widthp, heightp)
1687 int *widthp, *heightp;
1690 #ifdef TIOCGWINSZ
1692 /* BSD-style. */
1693 struct winsize size;
1695 if (ioctl (input_fd, TIOCGWINSZ, &size) == -1)
1696 *widthp = *heightp = 0;
1697 else
1699 *widthp = size.ws_col;
1700 *heightp = size.ws_row;
1703 #else
1704 #ifdef TIOCGSIZE
1706 /* SunOS - style. */
1707 struct ttysize size;
1709 if (ioctl (input_fd, TIOCGSIZE, &size) == -1)
1710 *widthp = *heightp = 0;
1711 else
1713 *widthp = size.ts_cols;
1714 *heightp = size.ts_lines;
1717 #else
1718 #ifdef VMS
1720 struct sensemode tty;
1722 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &tty, 0, 0,
1723 &tty.class, 12, 0, 0, 0, 0);
1724 *widthp = tty.scr_wid;
1725 *heightp = tty.scr_len;
1727 #else
1728 #ifdef MSDOS
1729 *widthp = ScreenCols ();
1730 *heightp = ScreenRows ();
1731 #else /* system doesn't know size */
1732 *widthp = 0;
1733 *heightp = 0;
1734 #endif
1736 #endif /* not VMS */
1737 #endif /* not SunOS-style */
1738 #endif /* not BSD-style */
1741 /* Set the logical window size associated with descriptor FD
1742 to HEIGHT and WIDTH. This is used mainly with ptys. */
1745 set_window_size (fd, height, width)
1746 int fd, height, width;
1748 #ifdef TIOCSWINSZ
1750 /* BSD-style. */
1751 struct winsize size;
1752 size.ws_row = height;
1753 size.ws_col = width;
1755 if (ioctl (fd, TIOCSWINSZ, &size) == -1)
1756 return 0; /* error */
1757 else
1758 return 1;
1760 #else
1761 #ifdef TIOCSSIZE
1763 /* SunOS - style. */
1764 struct ttysize size;
1765 size.ts_lines = height;
1766 size.ts_cols = width;
1768 if (ioctl (fd, TIOCGSIZE, &size) == -1)
1769 return 0;
1770 else
1771 return 1;
1772 #else
1773 return -1;
1774 #endif /* not SunOS-style */
1775 #endif /* not BSD-style */
1779 /* Prepare the terminal for exiting Emacs; move the cursor to the
1780 bottom of the frame, turn off interrupt-driven I/O, etc. */
1781 void
1782 reset_sys_modes ()
1784 struct frame *sf;
1786 if (noninteractive)
1788 fflush (stdout);
1789 return;
1791 if (!term_initted)
1792 return;
1793 #ifdef HAVE_WINDOW_SYSTEM
1794 /* Emacs' window system on MSDOG uses the `internal terminal' and therefore
1795 needs the clean-up code below. */
1796 if (!EQ (Vwindow_system, Qnil)
1797 #ifndef WINDOWSNT
1798 /* When running in tty mode on NT/Win95, we have a read_socket
1799 hook, but still need the rest of the clean-up code below. */
1800 || read_socket_hook
1801 #endif
1803 return;
1804 #endif
1805 sf = SELECTED_FRAME ();
1806 cursor_to (FRAME_HEIGHT (sf) - 1, 0);
1807 clear_end_of_line (FRAME_WIDTH (sf));
1808 /* clear_end_of_line may move the cursor */
1809 cursor_to (FRAME_HEIGHT (sf) - 1, 0);
1810 #if defined (IBMR2AIX) && defined (AIXHFT)
1812 /* HFT devices normally use ^J as a LF/CR. We forced it to
1813 do the LF only. Now, we need to reset it. */
1814 struct termio tty;
1816 if (ioctl (1, HFTGETID, &tty) != -1)
1817 write (1, "\033[20h", 5);
1819 #endif
1821 reset_terminal_modes ();
1822 fflush (stdout);
1823 #ifdef BSD_SYSTEM
1824 #ifndef BSD4_1
1825 /* Avoid possible loss of output when changing terminal modes. */
1826 fsync (fileno (stdout));
1827 #endif
1828 #endif
1830 #ifdef F_SETFL
1831 #ifndef F_SETOWN_BUG
1832 #ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
1833 if (interrupt_input)
1835 reset_sigio ();
1836 fcntl (input_fd, F_SETOWN, old_fcntl_owner);
1838 #endif /* F_SETOWN */
1839 #endif /* F_SETOWN_BUG */
1840 #ifdef O_NDELAY
1841 fcntl (input_fd, F_SETFL, fcntl (input_fd, F_GETFL, 0) & ~O_NDELAY);
1842 #endif
1843 #endif /* F_SETFL */
1844 #ifdef BSD4_1
1845 if (interrupt_input)
1846 reset_sigio ();
1847 #endif /* BSD4_1 */
1849 if (old_tty_valid)
1850 while (EMACS_SET_TTY (input_fd, &old_tty, 0) < 0 && errno == EINTR)
1853 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1854 dos_ttcooked ();
1855 #endif
1857 #ifdef SET_LINE_DISCIPLINE
1858 /* Ultrix's termios *ignores* any line discipline except TERMIODISC.
1859 A different old line discipline is therefore not restored, yet.
1860 Restore the old line discipline by hand. */
1861 ioctl (0, TIOCSETD, &old_tty.main.c_line);
1862 #endif
1864 #ifdef AIXHFT
1865 hft_reset ();
1866 #endif
1868 #ifdef BSD_PGRPS
1869 widen_foreground_group ();
1870 #endif
1873 #ifdef HAVE_PTYS
1875 /* Set up the proper status flags for use of a pty. */
1877 void
1878 setup_pty (fd)
1879 int fd;
1881 /* I'm told that TOICREMOTE does not mean control chars
1882 "can't be sent" but rather that they don't have
1883 input-editing or signaling effects.
1884 That should be good, because we have other ways
1885 to do those things in Emacs.
1886 However, telnet mode seems not to work on 4.2.
1887 So TIOCREMOTE is turned off now. */
1889 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1890 will hang. In particular, the "timeout" feature (which
1891 causes a read to return if there is no data available)
1892 does this. Also it is known that telnet mode will hang
1893 in such a way that Emacs must be stopped (perhaps this
1894 is the same problem).
1896 If TIOCREMOTE is turned off, then there is a bug in
1897 hp-ux which sometimes loses data. Apparently the
1898 code which blocks the master process when the internal
1899 buffer fills up does not work. Other than this,
1900 though, everything else seems to work fine.
1902 Since the latter lossage is more benign, we may as well
1903 lose that way. -- cph */
1904 #ifdef FIONBIO
1905 #if defined(SYSV_PTYS) || defined(UNIX98_PTYS)
1907 int on = 1;
1908 ioctl (fd, FIONBIO, &on);
1910 #endif
1911 #endif
1912 #ifdef IBMRTAIX
1913 /* On AIX, the parent gets SIGHUP when a pty attached child dies. So, we */
1914 /* ignore SIGHUP once we've started a child on a pty. Note that this may */
1915 /* cause EMACS not to die when it should, i.e., when its own controlling */
1916 /* tty goes away. I've complained to the AIX developers, and they may */
1917 /* change this behavior, but I'm not going to hold my breath. */
1918 signal (SIGHUP, SIG_IGN);
1919 #endif
1921 #endif /* HAVE_PTYS */
1923 #ifdef VMS
1925 /* Assigning an input channel is done at the start of Emacs execution.
1926 This is called each time Emacs is resumed, also, but does nothing
1927 because input_chain is no longer zero. */
1929 void
1930 init_vms_input ()
1932 int status;
1934 if (input_fd == 0)
1936 status = SYS$ASSIGN (&input_dsc, &input_fd, 0, 0);
1937 if (! (status & 1))
1938 LIB$STOP (status);
1942 /* Deassigning the input channel is done before exiting. */
1944 void
1945 stop_vms_input ()
1947 return SYS$DASSGN (input_fd);
1950 short input_buffer;
1952 /* Request reading one character into the keyboard buffer.
1953 This is done as soon as the buffer becomes empty. */
1955 void
1956 queue_kbd_input ()
1958 int status;
1959 extern kbd_input_ast ();
1961 waiting_for_ast = 0;
1962 stop_input = 0;
1963 status = SYS$QIO (0, input_fd, IO$_READVBLK,
1964 &input_iosb, kbd_input_ast, 1,
1965 &input_buffer, 1, 0, terminator_mask, 0, 0);
1968 int input_count;
1970 /* Ast routine that is called when keyboard input comes in
1971 in accord with the SYS$QIO above. */
1973 void
1974 kbd_input_ast ()
1976 register int c = -1;
1977 int old_errno = errno;
1978 extern EMACS_TIME *input_available_clear_time;
1980 if (waiting_for_ast)
1981 SYS$SETEF (input_ef);
1982 waiting_for_ast = 0;
1983 input_count++;
1984 #ifdef ASTDEBUG
1985 if (input_count == 25)
1986 exit (1);
1987 printf ("Ast # %d,", input_count);
1988 printf (" iosb = %x, %x, %x, %x",
1989 input_iosb.offset, input_iosb.status, input_iosb.termlen,
1990 input_iosb.term);
1991 #endif
1992 if (input_iosb.offset)
1994 c = input_buffer;
1995 #ifdef ASTDEBUG
1996 printf (", char = 0%o", c);
1997 #endif
1999 #ifdef ASTDEBUG
2000 printf ("\n");
2001 fflush (stdout);
2002 sleep (1);
2003 #endif
2004 if (! stop_input)
2005 queue_kbd_input ();
2006 if (c >= 0)
2008 struct input_event e;
2009 e.kind = ascii_keystroke;
2010 XSETINT (e.code, c);
2011 e.frame_or_window = selected_frame;
2012 kbd_buffer_store_event (&e);
2014 if (input_available_clear_time)
2015 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
2016 errno = old_errno;
2019 /* Wait until there is something in kbd_buffer. */
2021 void
2022 wait_for_kbd_input ()
2024 extern int have_process_input, process_exited;
2026 /* If already something, avoid doing system calls. */
2027 if (detect_input_pending ())
2029 return;
2031 /* Clear a flag, and tell ast routine above to set it. */
2032 SYS$CLREF (input_ef);
2033 waiting_for_ast = 1;
2034 /* Check for timing error: ast happened while we were doing that. */
2035 if (!detect_input_pending ())
2037 /* No timing error: wait for flag to be set. */
2038 set_waiting_for_input (0);
2039 SYS$WFLOR (input_ef, input_eflist);
2040 clear_waiting_for_input (0);
2041 if (!detect_input_pending ())
2042 /* Check for subprocess input availability */
2044 int dsp = have_process_input || process_exited;
2046 SYS$CLREF (process_ef);
2047 if (have_process_input)
2048 process_command_input ();
2049 if (process_exited)
2050 process_exit ();
2051 if (dsp)
2053 update_mode_lines++;
2054 prepare_menu_bars ();
2055 redisplay_preserve_echo_area (18);
2059 waiting_for_ast = 0;
2062 /* Get rid of any pending QIO, when we are about to suspend
2063 or when we want to throw away pending input.
2064 We wait for a positive sign that the AST routine has run
2065 and therefore there is no I/O request queued when we return.
2066 SYS$SETAST is used to avoid a timing error. */
2068 void
2069 end_kbd_input ()
2071 #ifdef ASTDEBUG
2072 printf ("At end_kbd_input.\n");
2073 fflush (stdout);
2074 sleep (1);
2075 #endif
2076 if (LIB$AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
2078 SYS$CANCEL (input_fd);
2079 return;
2082 SYS$SETAST (0);
2083 /* Clear a flag, and tell ast routine above to set it. */
2084 SYS$CLREF (input_ef);
2085 waiting_for_ast = 1;
2086 stop_input = 1;
2087 SYS$CANCEL (input_fd);
2088 SYS$SETAST (1);
2089 SYS$WAITFR (input_ef);
2090 waiting_for_ast = 0;
2093 /* Wait for either input available or time interval expiry. */
2095 void
2096 input_wait_timeout (timeval)
2097 int timeval; /* Time to wait, in seconds */
2099 int time [2];
2100 static int zero = 0;
2101 static int large = -10000000;
2103 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
2105 /* If already something, avoid doing system calls. */
2106 if (detect_input_pending ())
2108 return;
2110 /* Clear a flag, and tell ast routine above to set it. */
2111 SYS$CLREF (input_ef);
2112 waiting_for_ast = 1;
2113 /* Check for timing error: ast happened while we were doing that. */
2114 if (!detect_input_pending ())
2116 /* No timing error: wait for flag to be set. */
2117 SYS$CANTIM (1, 0);
2118 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
2119 SYS$WFLOR (timer_ef, timer_eflist); /* Wait for timer expiry or input */
2121 waiting_for_ast = 0;
2124 /* The standard `sleep' routine works some other way
2125 and it stops working if you have ever quit out of it.
2126 This one continues to work. */
2128 sys_sleep (timeval)
2129 int timeval;
2131 int time [2];
2132 static int zero = 0;
2133 static int large = -10000000;
2135 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
2137 SYS$CANTIM (1, 0);
2138 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
2139 SYS$WAITFR (timer_ef); /* Wait for timer expiry only */
2142 void
2143 init_sigio (fd)
2144 int fd;
2146 request_sigio ();
2149 reset_sigio ()
2151 unrequest_sigio ();
2154 void
2155 request_sigio ()
2157 croak ("request sigio");
2160 void
2161 unrequest_sigio ()
2163 croak ("unrequest sigio");
2166 #endif /* VMS */
2168 /* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
2169 #ifndef CANNOT_DUMP
2170 #define NEED_STARTS
2171 #endif
2173 #ifndef SYSTEM_MALLOC
2174 #ifndef NEED_STARTS
2175 #define NEED_STARTS
2176 #endif
2177 #endif
2179 #ifdef NEED_STARTS
2180 /* Some systems that cannot dump also cannot implement these. */
2183 * Return the address of the start of the text segment prior to
2184 * doing an unexec. After unexec the return value is undefined.
2185 * See crt0.c for further explanation and _start.
2189 #if !(defined (__NetBSD__) && defined (__ELF__))
2190 #ifndef HAVE_TEXT_START
2191 char *
2192 start_of_text ()
2194 #ifdef TEXT_START
2195 return ((char *) TEXT_START);
2196 #else
2197 #ifdef GOULD
2198 extern csrt ();
2199 return ((char *) csrt);
2200 #else /* not GOULD */
2201 extern int _start ();
2202 return ((char *) _start);
2203 #endif /* GOULD */
2204 #endif /* TEXT_START */
2206 #endif /* not HAVE_TEXT_START */
2207 #endif
2210 * Return the address of the start of the data segment prior to
2211 * doing an unexec. After unexec the return value is undefined.
2212 * See crt0.c for further information and definition of data_start.
2214 * Apparently, on BSD systems this is etext at startup. On
2215 * USG systems (swapping) this is highly mmu dependent and
2216 * is also dependent on whether or not the program is running
2217 * with shared text. Generally there is a (possibly large)
2218 * gap between end of text and start of data with shared text.
2220 * On Uniplus+ systems with shared text, data starts at a
2221 * fixed address. Each port (from a given oem) is generally
2222 * different, and the specific value of the start of data can
2223 * be obtained via the UniPlus+ specific "uvar" system call,
2224 * however the method outlined in crt0.c seems to be more portable.
2226 * Probably what will have to happen when a USG unexec is available,
2227 * at least on UniPlus, is temacs will have to be made unshared so
2228 * that text and data are contiguous. Then once loadup is complete,
2229 * unexec will produce a shared executable where the data can be
2230 * at the normal shared text boundary and the startofdata variable
2231 * will be patched by unexec to the correct value.
2235 char *
2236 start_of_data ()
2238 #ifdef DATA_START
2239 return ((char *) DATA_START);
2240 #else
2241 #ifdef ORDINARY_LINK
2243 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
2244 * data_start isn't defined. We take the address of environ, which
2245 * is known to live at or near the start of the system crt0.c, and
2246 * we don't sweat the handful of bytes that might lose.
2248 extern char **environ;
2250 return ((char *) &environ);
2251 #else
2252 extern int data_start;
2253 return ((char *) &data_start);
2254 #endif /* ORDINARY_LINK */
2255 #endif /* DATA_START */
2257 #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
2259 #ifndef CANNOT_DUMP
2260 /* Some systems that cannot dump also cannot implement these. */
2263 * Return the address of the end of the text segment prior to
2264 * doing an unexec. After unexec the return value is undefined.
2267 char *
2268 end_of_text ()
2270 #ifdef TEXT_END
2271 return ((char *) TEXT_END);
2272 #else
2273 extern int etext;
2274 return ((char *) &etext);
2275 #endif
2279 * Return the address of the end of the data segment prior to
2280 * doing an unexec. After unexec the return value is undefined.
2283 char *
2284 end_of_data ()
2286 #ifdef DATA_END
2287 return ((char *) DATA_END);
2288 #else
2289 extern int edata;
2290 return ((char *) &edata);
2291 #endif
2294 #endif /* not CANNOT_DUMP */
2296 /* init_system_name sets up the string for the Lisp function
2297 system-name to return. */
2299 #ifdef BSD4_1
2300 #include <whoami.h>
2301 #endif
2303 extern Lisp_Object Vsystem_name;
2305 #ifndef BSD4_1
2306 #ifndef VMS
2307 #ifdef HAVE_SOCKETS
2308 #include <sys/socket.h>
2309 #include <netdb.h>
2310 #endif /* HAVE_SOCKETS */
2311 #endif /* not VMS */
2312 #endif /* not BSD4_1 */
2314 #ifdef TRY_AGAIN
2315 #ifndef HAVE_H_ERRNO
2316 extern int h_errno;
2317 #endif
2318 #endif /* TRY_AGAIN */
2320 void
2321 init_system_name ()
2323 #ifdef BSD4_1
2324 Vsystem_name = build_string (sysname);
2325 #else
2326 #ifdef VMS
2327 char *sp, *end;
2328 if ((sp = egetenv ("SYS$NODE")) == 0)
2329 Vsystem_name = build_string ("vax-vms");
2330 else if ((end = index (sp, ':')) == 0)
2331 Vsystem_name = build_string (sp);
2332 else
2333 Vsystem_name = make_string (sp, end - sp);
2334 #else
2335 #ifndef HAVE_GETHOSTNAME
2336 struct utsname uts;
2337 uname (&uts);
2338 Vsystem_name = build_string (uts.nodename);
2339 #else /* HAVE_GETHOSTNAME */
2340 unsigned int hostname_size = 256;
2341 char *hostname = (char *) alloca (hostname_size);
2343 /* Try to get the host name; if the buffer is too short, try
2344 again. Apparently, the only indication gethostname gives of
2345 whether the buffer was large enough is the presence or absence
2346 of a '\0' in the string. Eech. */
2347 for (;;)
2349 gethostname (hostname, hostname_size - 1);
2350 hostname[hostname_size - 1] = '\0';
2352 /* Was the buffer large enough for the '\0'? */
2353 if (strlen (hostname) < hostname_size - 1)
2354 break;
2356 hostname_size <<= 1;
2357 hostname = (char *) alloca (hostname_size);
2359 #ifdef HAVE_SOCKETS
2360 /* Turn the hostname into the official, fully-qualified hostname.
2361 Don't do this if we're going to dump; this can confuse system
2362 libraries on some machines and make the dumped emacs core dump. */
2363 #ifndef CANNOT_DUMP
2364 if (initialized)
2365 #endif /* not CANNOT_DUMP */
2366 if (! index (hostname, '.'))
2368 struct hostent *hp;
2369 int count;
2370 for (count = 0;; count++)
2372 #ifdef TRY_AGAIN
2373 h_errno = 0;
2374 #endif
2375 hp = gethostbyname (hostname);
2376 #ifdef TRY_AGAIN
2377 if (! (hp == 0 && h_errno == TRY_AGAIN))
2378 #endif
2379 break;
2380 if (count >= 5)
2381 break;
2382 Fsleep_for (make_number (1), Qnil);
2384 if (hp)
2386 char *fqdn = (char *) hp->h_name;
2387 char *p;
2389 if (!index (fqdn, '.'))
2391 /* We still don't have a fully qualified domain name.
2392 Try to find one in the list of alternate names */
2393 char **alias = hp->h_aliases;
2394 while (*alias && !index (*alias, '.'))
2395 alias++;
2396 if (*alias)
2397 fqdn = *alias;
2399 hostname = fqdn;
2400 #if 0
2401 /* Convert the host name to lower case. */
2402 /* Using ctype.h here would introduce a possible locale
2403 dependence that is probably wrong for hostnames. */
2404 p = hostname;
2405 while (*p)
2407 if (*p >= 'A' && *p <= 'Z')
2408 *p += 'a' - 'A';
2409 p++;
2411 #endif
2414 #endif /* HAVE_SOCKETS */
2415 /* We used to try using getdomainname here,
2416 but NIIBE Yutaka <gniibe@etl.go.jp> says that
2417 getdomainname gets the NIS/YP domain which often is not the same
2418 as in Internet domain name. */
2419 #if 0 /* Turned off because sysinfo is not really likely to return the
2420 correct Internet domain. */
2421 #if (HAVE_SYSINFO && defined (SI_SRPC_DOMAIN))
2422 if (! index (hostname, '.'))
2424 /* The hostname is not fully qualified. Append the domain name. */
2426 int hostlen = strlen (hostname);
2427 int domain_size = 256;
2429 for (;;)
2431 char *domain = (char *) alloca (domain_size + 1);
2432 char *fqdn = (char *) alloca (hostlen + 1 + domain_size + 1);
2433 int sys_domain_size = sysinfo (SI_SRPC_DOMAIN, domain, domain_size);
2434 if (sys_domain_size <= 0)
2435 break;
2436 if (domain_size < sys_domain_size)
2438 domain_size = sys_domain_size;
2439 continue;
2441 strcpy (fqdn, hostname);
2442 if (domain[0] == '.')
2443 strcpy (fqdn + hostlen, domain);
2444 else if (domain[0] != 0)
2446 fqdn[hostlen] = '.';
2447 strcpy (fqdn + hostlen + 1, domain);
2449 hostname = fqdn;
2450 break;
2453 #endif /* HAVE_SYSINFO && defined (SI_SRPC_DOMAIN) */
2454 #endif /* 0 */
2455 Vsystem_name = build_string (hostname);
2456 #endif /* HAVE_GETHOSTNAME */
2457 #endif /* VMS */
2458 #endif /* BSD4_1 */
2460 unsigned char *p;
2461 for (p = XSTRING (Vsystem_name)->data; *p; p++)
2462 if (*p == ' ' || *p == '\t')
2463 *p = '-';
2467 #ifndef MSDOS
2468 #ifndef VMS
2469 #if !defined (HAVE_SELECT) || defined (BROKEN_SELECT_NON_X)
2471 #include "sysselect.h"
2472 #undef select
2474 #if defined (HAVE_X_WINDOWS) && !defined (HAVE_SELECT)
2475 /* Cause explanatory error message at compile time,
2476 since the select emulation is not good enough for X. */
2477 int *x = &x_windows_lose_if_no_select_system_call;
2478 #endif
2480 /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2481 * Only checks read descriptors.
2483 /* How long to wait between checking fds in select */
2484 #define SELECT_PAUSE 1
2485 int select_alarmed;
2487 /* For longjmp'ing back to read_input_waiting. */
2489 jmp_buf read_alarm_throw;
2491 /* Nonzero if the alarm signal should throw back to read_input_waiting.
2492 The read_socket_hook function sets this to 1 while it is waiting. */
2494 int read_alarm_should_throw;
2496 SIGTYPE
2497 select_alarm ()
2499 select_alarmed = 1;
2500 #ifdef BSD4_1
2501 sigrelse (SIGALRM);
2502 #else /* not BSD4_1 */
2503 signal (SIGALRM, SIG_IGN);
2504 #endif /* not BSD4_1 */
2505 if (read_alarm_should_throw)
2506 longjmp (read_alarm_throw, 1);
2509 #ifndef WINDOWSNT
2510 /* Only rfds are checked. */
2512 sys_select (nfds, rfds, wfds, efds, timeout)
2513 int nfds;
2514 SELECT_TYPE *rfds, *wfds, *efds;
2515 EMACS_TIME *timeout;
2517 int ravail = 0;
2518 SELECT_TYPE orfds;
2519 int timeoutval;
2520 int *local_timeout;
2521 extern int proc_buffered_char[];
2522 #ifndef subprocesses
2523 int process_tick = 0, update_tick = 0;
2524 #else
2525 extern int process_tick, update_tick;
2526 #endif
2527 unsigned char buf;
2529 #if defined (HAVE_SELECT) && defined (HAVE_X_WINDOWS)
2530 /* If we're using X, then the native select will work; we only need the
2531 emulation for non-X usage. */
2532 if (!NILP (Vwindow_system))
2533 return select (nfds, rfds, wfds, efds, timeout);
2534 #endif
2535 timeoutval = timeout ? EMACS_SECS (*timeout) : 100000;
2536 local_timeout = &timeoutval;
2537 FD_ZERO (&orfds);
2538 if (rfds)
2540 orfds = *rfds;
2541 FD_ZERO (rfds);
2543 if (wfds)
2544 FD_ZERO (wfds);
2545 if (efds)
2546 FD_ZERO (efds);
2548 /* If we are looking only for the terminal, with no timeout,
2549 just read it and wait -- that's more efficient. */
2550 if (*local_timeout == 100000 && process_tick == update_tick
2551 && FD_ISSET (0, &orfds))
2553 int fd;
2554 for (fd = 1; fd < nfds; ++fd)
2555 if (FD_ISSET (fd, &orfds))
2556 goto hardway;
2557 if (! detect_input_pending ())
2558 read_input_waiting ();
2559 FD_SET (0, rfds);
2560 return 1;
2563 hardway:
2564 /* Once a second, till the timer expires, check all the flagged read
2565 * descriptors to see if any input is available. If there is some then
2566 * set the corresponding bit in the return copy of rfds.
2568 while (1)
2570 register int to_check, fd;
2572 if (rfds)
2574 for (to_check = nfds, fd = 0; --to_check >= 0; fd++)
2576 if (FD_ISSET (fd, &orfds))
2578 int avail = 0, status = 0;
2580 if (fd == 0)
2581 avail = detect_input_pending (); /* Special keyboard handler */
2582 else
2584 #ifdef FIONREAD
2585 status = ioctl (fd, FIONREAD, &avail);
2586 #else /* no FIONREAD */
2587 /* Hoping it will return -1 if nothing available
2588 or 0 if all 0 chars requested are read. */
2589 if (proc_buffered_char[fd] >= 0)
2590 avail = 1;
2591 else
2593 avail = read (fd, &buf, 1);
2594 if (avail > 0)
2595 proc_buffered_char[fd] = buf;
2597 #endif /* no FIONREAD */
2599 if (status >= 0 && avail > 0)
2601 FD_SET (fd, rfds);
2602 ravail++;
2607 if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
2608 break;
2610 turn_on_atimers (0);
2611 signal (SIGALRM, select_alarm);
2612 select_alarmed = 0;
2613 alarm (SELECT_PAUSE);
2615 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2616 while (select_alarmed == 0 && *local_timeout != 0
2617 && process_tick == update_tick)
2619 /* If we are interested in terminal input,
2620 wait by reading the terminal.
2621 That makes instant wakeup for terminal input at least. */
2622 if (FD_ISSET (0, &orfds))
2624 read_input_waiting ();
2625 if (detect_input_pending ())
2626 select_alarmed = 1;
2628 else
2629 pause ();
2631 (*local_timeout) -= SELECT_PAUSE;
2633 /* Reset the old alarm if there was one. */
2634 turn_on_atimers (1);
2636 if (*local_timeout == 0) /* Stop on timer being cleared */
2637 break;
2639 return ravail;
2641 #endif /* not WINDOWSNT */
2643 /* Read keyboard input into the standard buffer,
2644 waiting for at least one character. */
2646 /* Make all keyboard buffers much bigger when using a window system. */
2647 #ifdef HAVE_WINDOW_SYSTEM
2648 #define BUFFER_SIZE_FACTOR 16
2649 #else
2650 #define BUFFER_SIZE_FACTOR 1
2651 #endif
2653 void
2654 read_input_waiting ()
2656 struct input_event e;
2657 int nread, i;
2658 extern int quit_char;
2660 if (read_socket_hook)
2662 struct input_event buf[256];
2664 read_alarm_should_throw = 0;
2665 if (! setjmp (read_alarm_throw))
2666 nread = (*read_socket_hook) (0, buf, 256, 1);
2667 else
2668 nread = -1;
2670 /* Scan the chars for C-g and store them in kbd_buffer. */
2671 for (i = 0; i < nread; i++)
2673 kbd_buffer_store_event (&buf[i]);
2674 /* Don't look at input that follows a C-g too closely.
2675 This reduces lossage due to autorepeat on C-g. */
2676 if (buf[i].kind == ascii_keystroke
2677 && buf[i].code == quit_char)
2678 break;
2681 else
2683 char buf[3];
2684 nread = read (fileno (stdin), buf, 1);
2686 /* Scan the chars for C-g and store them in kbd_buffer. */
2687 e.kind = ascii_keystroke;
2688 e.frame_or_window = selected_frame;
2689 e.modifiers = 0;
2690 for (i = 0; i < nread; i++)
2692 /* Convert chars > 0177 to meta events if desired.
2693 We do this under the same conditions that read_avail_input does. */
2694 if (read_socket_hook == 0)
2696 /* If the user says she has a meta key, then believe her. */
2697 if (meta_key == 1 && (buf[i] & 0x80))
2698 e.modifiers = meta_modifier;
2699 if (meta_key != 2)
2700 buf[i] &= ~0x80;
2703 XSETINT (e.code, buf[i]);
2704 kbd_buffer_store_event (&e);
2705 /* Don't look at input that follows a C-g too closely.
2706 This reduces lossage due to autorepeat on C-g. */
2707 if (buf[i] == quit_char)
2708 break;
2713 #endif /* not HAVE_SELECT */
2714 #endif /* not VMS */
2715 #endif /* not MSDOS */
2717 #ifdef BSD4_1
2718 void
2719 init_sigio (fd)
2720 int fd;
2722 if (noninteractive)
2723 return;
2724 lmode = LINTRUP | lmode;
2725 ioctl (fd, TIOCLSET, &lmode);
2728 void
2729 reset_sigio ()
2731 if (noninteractive)
2732 return;
2733 lmode = ~LINTRUP & lmode;
2734 ioctl (0, TIOCLSET, &lmode);
2737 void
2738 request_sigio ()
2740 sigrelse (SIGTINT);
2742 interrupts_deferred = 0;
2745 void
2746 unrequest_sigio ()
2748 sighold (SIGTINT);
2750 interrupts_deferred = 1;
2753 /* still inside #ifdef BSD4_1 */
2754 #ifdef subprocesses
2756 int sigheld; /* Mask of held signals */
2758 void
2759 sigholdx (signum)
2760 int signum;
2762 sigheld |= sigbit (signum);
2763 sighold (signum);
2766 void
2767 sigisheld (signum)
2768 int signum;
2770 sigheld |= sigbit (signum);
2773 void
2774 sigunhold (signum)
2775 int signum;
2777 sigheld &= ~sigbit (signum);
2778 sigrelse (signum);
2781 void
2782 sigfree () /* Free all held signals */
2784 int i;
2785 for (i = 0; i < NSIG; i++)
2786 if (sigheld & sigbit (i))
2787 sigrelse (i);
2788 sigheld = 0;
2792 sigbit (i)
2794 return 1 << (i - 1);
2796 #endif /* subprocesses */
2797 #endif /* BSD4_1 */
2799 /* POSIX signals support - DJB */
2800 /* Anyone with POSIX signals should have ANSI C declarations */
2802 #ifdef POSIX_SIGNALS
2804 sigset_t empty_mask, full_mask;
2806 signal_handler_t
2807 sys_signal (int signal_number, signal_handler_t action)
2809 struct sigaction new_action, old_action;
2810 sigemptyset (&new_action.sa_mask);
2811 new_action.sa_handler = action;
2812 #if defined (SA_RESTART) && ! defined (BROKEN_SA_RESTART)
2813 /* Emacs mostly works better with restartable system services. If this
2814 flag exists, we probably want to turn it on here.
2815 However, on some systems this resets the timeout of `select'
2816 which means that `select' never finishes if it keeps getting signals.
2817 BROKEN_SA_RESTART is defined on those systems. */
2818 new_action.sa_flags = SA_RESTART;
2819 #else
2820 new_action.sa_flags = 0;
2821 #endif
2822 sigaction (signal_number, &new_action, &old_action);
2823 return (old_action.sa_handler);
2826 #ifndef __GNUC__
2827 /* If we're compiling with GCC, we don't need this function, since it
2828 can be written as a macro. */
2829 sigset_t
2830 sys_sigmask (int sig)
2832 sigset_t mask;
2833 sigemptyset (&mask);
2834 sigaddset (&mask, sig);
2835 return mask;
2837 #endif
2839 /* I'd like to have these guys return pointers to the mask storage in here,
2840 but there'd be trouble if the code was saving multiple masks. I'll be
2841 safe and pass the structure. It normally won't be more than 2 bytes
2842 anyhow. - DJB */
2844 sigset_t
2845 sys_sigblock (sigset_t new_mask)
2847 sigset_t old_mask;
2848 sigprocmask (SIG_BLOCK, &new_mask, &old_mask);
2849 return (old_mask);
2852 sigset_t
2853 sys_sigunblock (sigset_t new_mask)
2855 sigset_t old_mask;
2856 sigprocmask (SIG_UNBLOCK, &new_mask, &old_mask);
2857 return (old_mask);
2860 sigset_t
2861 sys_sigsetmask (sigset_t new_mask)
2863 sigset_t old_mask;
2864 sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
2865 return (old_mask);
2868 #endif /* POSIX_SIGNALS */
2870 #if !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED
2871 static char *my_sys_siglist[NSIG];
2872 # ifdef sys_siglist
2873 # undef sys_siglist
2874 # endif
2875 # define sys_siglist my_sys_siglist
2876 #endif
2878 void
2879 init_signals ()
2881 #ifdef POSIX_SIGNALS
2882 sigemptyset (&empty_mask);
2883 sigfillset (&full_mask);
2884 #endif
2886 #if !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED
2887 if (! initialized)
2889 # ifdef SIGABRT
2890 sys_siglist[SIGABRT] = "Aborted";
2891 # endif
2892 # ifdef SIGAIO
2893 sys_siglist[SIGAIO] = "LAN I/O interrupt";
2894 # endif
2895 # ifdef SIGALRM
2896 sys_siglist[SIGALRM] = "Alarm clock";
2897 # endif
2898 # ifdef SIGBUS
2899 sys_siglist[SIGBUS] = "Bus error";
2900 # endif
2901 # ifdef SIGCLD
2902 sys_siglist[SIGCLD] = "Child status changed";
2903 # endif
2904 # ifdef SIGCHLD
2905 sys_siglist[SIGCHLD] = "Child status changed";
2906 # endif
2907 # ifdef SIGCONT
2908 sys_siglist[SIGCONT] = "Continued";
2909 # endif
2910 # ifdef SIGDANGER
2911 sys_siglist[SIGDANGER] = "Swap space dangerously low";
2912 # endif
2913 # ifdef SIGDGNOTIFY
2914 sys_siglist[SIGDGNOTIFY] = "Notification message in queue";
2915 # endif
2916 # ifdef SIGEMT
2917 sys_siglist[SIGEMT] = "Emulation trap";
2918 # endif
2919 # ifdef SIGFPE
2920 sys_siglist[SIGFPE] = "Arithmetic exception";
2921 # endif
2922 # ifdef SIGFREEZE
2923 sys_siglist[SIGFREEZE] = "SIGFREEZE";
2924 # endif
2925 # ifdef SIGGRANT
2926 sys_siglist[SIGGRANT] = "Monitor mode granted";
2927 # endif
2928 # ifdef SIGHUP
2929 sys_siglist[SIGHUP] = "Hangup";
2930 # endif
2931 # ifdef SIGILL
2932 sys_siglist[SIGILL] = "Illegal instruction";
2933 # endif
2934 # ifdef SIGINT
2935 sys_siglist[SIGINT] = "Interrupt";
2936 # endif
2937 # ifdef SIGIO
2938 sys_siglist[SIGIO] = "I/O possible";
2939 # endif
2940 # ifdef SIGIOINT
2941 sys_siglist[SIGIOINT] = "I/O intervention required";
2942 # endif
2943 # ifdef SIGIOT
2944 sys_siglist[SIGIOT] = "IOT trap";
2945 # endif
2946 # ifdef SIGKILL
2947 sys_siglist[SIGKILL] = "Killed";
2948 # endif
2949 # ifdef SIGLOST
2950 sys_siglist[SIGLOST] = "Resource lost";
2951 # endif
2952 # ifdef SIGLWP
2953 sys_siglist[SIGLWP] = "SIGLWP";
2954 # endif
2955 # ifdef SIGMSG
2956 sys_siglist[SIGMSG] = "Monitor mode data available";
2957 # endif
2958 # ifdef SIGPHONE
2959 sys_siglist[SIGWIND] = "SIGPHONE";
2960 # endif
2961 # ifdef SIGPIPE
2962 sys_siglist[SIGPIPE] = "Broken pipe";
2963 # endif
2964 # ifdef SIGPOLL
2965 sys_siglist[SIGPOLL] = "Pollable event occurred";
2966 # endif
2967 # ifdef SIGPROF
2968 sys_siglist[SIGPROF] = "Profiling timer expired";
2969 # endif
2970 # ifdef SIGPTY
2971 sys_siglist[SIGPTY] = "PTY I/O interrupt";
2972 # endif
2973 # ifdef SIGPWR
2974 sys_siglist[SIGPWR] = "Power-fail restart";
2975 # endif
2976 # ifdef SIGQUIT
2977 sys_siglist[SIGQUIT] = "Quit";
2978 # endif
2979 # ifdef SIGRETRACT
2980 sys_siglist[SIGRETRACT] = "Need to relinguish monitor mode";
2981 # endif
2982 # ifdef SIGSAK
2983 sys_siglist[SIGSAK] = "Secure attention";
2984 # endif
2985 # ifdef SIGSEGV
2986 sys_siglist[SIGSEGV] = "Segmentation violation";
2987 # endif
2988 # ifdef SIGSOUND
2989 sys_siglist[SIGSOUND] = "Sound completed";
2990 # endif
2991 # ifdef SIGSTOP
2992 sys_siglist[SIGSTOP] = "Stopped (signal)";
2993 # endif
2994 # ifdef SIGSTP
2995 sys_siglist[SIGSTP] = "Stopped (user)";
2996 # endif
2997 # ifdef SIGSYS
2998 sys_siglist[SIGSYS] = "Bad argument to system call";
2999 # endif
3000 # ifdef SIGTERM
3001 sys_siglist[SIGTERM] = "Terminated";
3002 # endif
3003 # ifdef SIGTHAW
3004 sys_siglist[SIGTHAW] = "SIGTHAW";
3005 # endif
3006 # ifdef SIGTRAP
3007 sys_siglist[SIGTRAP] = "Trace/breakpoint trap";
3008 # endif
3009 # ifdef SIGTSTP
3010 sys_siglist[SIGTSTP] = "Stopped (user)";
3011 # endif
3012 # ifdef SIGTTIN
3013 sys_siglist[SIGTTIN] = "Stopped (tty input)";
3014 # endif
3015 # ifdef SIGTTOU
3016 sys_siglist[SIGTTOU] = "Stopped (tty output)";
3017 # endif
3018 # ifdef SIGURG
3019 sys_siglist[SIGURG] = "Urgent I/O condition";
3020 # endif
3021 # ifdef SIGUSR1
3022 sys_siglist[SIGUSR1] = "User defined signal 1";
3023 # endif
3024 # ifdef SIGUSR2
3025 sys_siglist[SIGUSR2] = "User defined signal 2";
3026 # endif
3027 # ifdef SIGVTALRM
3028 sys_siglist[SIGVTALRM] = "Virtual timer expired";
3029 # endif
3030 # ifdef SIGWAITING
3031 sys_siglist[SIGWAITING] = "Process's LWPs are blocked";
3032 # endif
3033 # ifdef SIGWINCH
3034 sys_siglist[SIGWINCH] = "Window size changed";
3035 # endif
3036 # ifdef SIGWIND
3037 sys_siglist[SIGWIND] = "SIGWIND";
3038 # endif
3039 # ifdef SIGXCPU
3040 sys_siglist[SIGXCPU] = "CPU time limit exceeded";
3041 # endif
3042 # ifdef SIGXFSZ
3043 sys_siglist[SIGXFSZ] = "File size limit exceeded";
3044 # endif
3046 #endif /* !defined HAVE_STRSIGNAL && !defined SYS_SIGLIST_DECLARED */
3049 #ifndef HAVE_RANDOM
3050 #ifdef random
3051 #define HAVE_RANDOM
3052 #endif
3053 #endif
3055 /* Figure out how many bits the system's random number generator uses.
3056 `random' and `lrand48' are assumed to return 31 usable bits.
3057 BSD `rand' returns a 31 bit value but the low order bits are unusable;
3058 so we'll shift it and treat it like the 15-bit USG `rand'. */
3060 #ifndef RAND_BITS
3061 # ifdef HAVE_RANDOM
3062 # define RAND_BITS 31
3063 # else /* !HAVE_RANDOM */
3064 # ifdef HAVE_LRAND48
3065 # define RAND_BITS 31
3066 # define random lrand48
3067 # else /* !HAVE_LRAND48 */
3068 # define RAND_BITS 15
3069 # if RAND_MAX == 32767
3070 # define random rand
3071 # else /* RAND_MAX != 32767 */
3072 # if RAND_MAX == 2147483647
3073 # define random() (rand () >> 16)
3074 # else /* RAND_MAX != 2147483647 */
3075 # ifdef USG
3076 # define random rand
3077 # else
3078 # define random() (rand () >> 16)
3079 # endif /* !USG */
3080 # endif /* RAND_MAX != 2147483647 */
3081 # endif /* RAND_MAX != 32767 */
3082 # endif /* !HAVE_LRAND48 */
3083 # endif /* !HAVE_RANDOM */
3084 #endif /* !RAND_BITS */
3086 void
3087 seed_random (arg)
3088 long arg;
3090 #ifdef HAVE_RANDOM
3091 srandom ((unsigned int)arg);
3092 #else
3093 # ifdef HAVE_LRAND48
3094 srand48 (arg);
3095 # else
3096 srand ((unsigned int)arg);
3097 # endif
3098 #endif
3102 * Build a full Emacs-sized word out of whatever we've got.
3103 * This suffices even for a 64-bit architecture with a 15-bit rand.
3105 long
3106 get_random ()
3108 long val = random ();
3109 #if VALBITS > RAND_BITS
3110 val = (val << RAND_BITS) ^ random ();
3111 #if VALBITS > 2*RAND_BITS
3112 val = (val << RAND_BITS) ^ random ();
3113 #if VALBITS > 3*RAND_BITS
3114 val = (val << RAND_BITS) ^ random ();
3115 #if VALBITS > 4*RAND_BITS
3116 val = (val << RAND_BITS) ^ random ();
3117 #endif /* need at least 5 */
3118 #endif /* need at least 4 */
3119 #endif /* need at least 3 */
3120 #endif /* need at least 2 */
3121 return val & ((1L << VALBITS) - 1);
3124 #ifdef WRONG_NAME_INSQUE
3126 insque (q,p)
3127 caddr_t q,p;
3129 _insque (q,p);
3132 #endif
3134 #ifdef VMS
3136 #ifdef getenv
3137 /* If any place else asks for the TERM variable,
3138 allow it to be overridden with the EMACS_TERM variable
3139 before attempting to translate the logical name TERM. As a last
3140 resort, ask for VAX C's special idea of the TERM variable. */
3141 #undef getenv
3142 char *
3143 sys_getenv (name)
3144 char *name;
3146 register char *val;
3147 static char buf[256];
3148 static struct dsc$descriptor_s equiv
3149 = {sizeof (buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf};
3150 static struct dsc$descriptor_s d_name
3151 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
3152 short eqlen;
3154 if (!strcmp (name, "TERM"))
3156 val = (char *) getenv ("EMACS_TERM");
3157 if (val)
3158 return val;
3161 d_name.dsc$w_length = strlen (name);
3162 d_name.dsc$a_pointer = name;
3163 if (LIB$SYS_TRNLOG (&d_name, &eqlen, &equiv) == 1)
3165 char *str = (char *) xmalloc (eqlen + 1);
3166 bcopy (buf, str, eqlen);
3167 str[eqlen] = '\0';
3168 /* This is a storage leak, but a pain to fix. With luck,
3169 no one will ever notice. */
3170 return str;
3172 return (char *) getenv (name);
3174 #endif /* getenv */
3176 #ifdef abort
3177 /* Since VMS doesn't believe in core dumps, the only way to debug this beast is
3178 to force a call on the debugger from within the image. */
3179 #undef abort
3180 sys_abort ()
3182 reset_sys_modes ();
3183 LIB$SIGNAL (SS$_DEBUG);
3185 #endif /* abort */
3186 #endif /* VMS */
3188 #ifdef VMS
3189 #ifdef LINK_CRTL_SHARE
3190 #ifdef SHARABLE_LIB_BUG
3191 /* Variables declared noshare and initialized in sharable libraries
3192 cannot be shared. The VMS linker incorrectly forces you to use a private
3193 version which is uninitialized... If not for this "feature", we
3194 could use the C library definition of sys_nerr and sys_errlist. */
3195 int sys_nerr = 35;
3196 char *sys_errlist[] =
3198 "error 0",
3199 "not owner",
3200 "no such file or directory",
3201 "no such process",
3202 "interrupted system call",
3203 "i/o error",
3204 "no such device or address",
3205 "argument list too long",
3206 "exec format error",
3207 "bad file number",
3208 "no child process",
3209 "no more processes",
3210 "not enough memory",
3211 "permission denied",
3212 "bad address",
3213 "block device required",
3214 "mount devices busy",
3215 "file exists",
3216 "cross-device link",
3217 "no such device",
3218 "not a directory",
3219 "is a directory",
3220 "invalid argument",
3221 "file table overflow",
3222 "too many open files",
3223 "not a typewriter",
3224 "text file busy",
3225 "file too big",
3226 "no space left on device",
3227 "illegal seek",
3228 "read-only file system",
3229 "too many links",
3230 "broken pipe",
3231 "math argument",
3232 "result too large",
3233 "I/O stream empty",
3234 "vax/vms specific error code nontranslatable error"
3236 #endif /* SHARABLE_LIB_BUG */
3237 #endif /* LINK_CRTL_SHARE */
3238 #endif /* VMS */
3240 #ifndef HAVE_STRERROR
3241 #ifndef WINDOWSNT
3242 char *
3243 strerror (errnum)
3244 int errnum;
3246 extern char *sys_errlist[];
3247 extern int sys_nerr;
3249 if (errnum >= 0 && errnum < sys_nerr)
3250 return sys_errlist[errnum];
3251 return (char *) "Unknown error";
3253 #endif /* not WINDOWSNT */
3254 #endif /* ! HAVE_STRERROR */
3257 emacs_open (path, oflag, mode)
3258 char *path;
3259 int oflag, mode;
3261 register int rtnval;
3263 #ifdef BSD4_1
3264 if (oflag & O_CREAT)
3265 return creat (path, mode);
3266 #endif
3268 while ((rtnval = open (path, oflag, mode)) == -1
3269 && (errno == EINTR));
3270 return (rtnval);
3274 emacs_close (fd)
3275 int fd;
3277 int did_retry = 0;
3278 register int rtnval;
3280 while ((rtnval = close (fd)) == -1
3281 && (errno == EINTR))
3282 did_retry = 1;
3284 /* If close is interrupted SunOS 4.1 may or may not have closed the
3285 file descriptor. If it did the second close will fail with
3286 errno = EBADF. That means we have succeeded. */
3287 if (rtnval == -1 && did_retry && errno == EBADF)
3288 return 0;
3290 return rtnval;
3294 emacs_read (fildes, buf, nbyte)
3295 int fildes;
3296 char *buf;
3297 unsigned int nbyte;
3299 register int rtnval;
3301 while ((rtnval = read (fildes, buf, nbyte)) == -1
3302 && (errno == EINTR));
3303 return (rtnval);
3307 emacs_write (fildes, buf, nbyte)
3308 int fildes;
3309 char *buf;
3310 unsigned int nbyte;
3312 register int rtnval, bytes_written;
3314 bytes_written = 0;
3316 while (nbyte > 0)
3318 rtnval = write (fildes, buf, nbyte);
3320 if (rtnval == -1)
3322 if (errno == EINTR)
3323 continue;
3324 else
3325 return (bytes_written ? bytes_written : -1);
3328 buf += rtnval;
3329 nbyte -= rtnval;
3330 bytes_written += rtnval;
3332 return (bytes_written);
3335 #ifdef USG
3337 * All of the following are for USG.
3339 * On USG systems the system calls are INTERRUPTIBLE by signals
3340 * that the user program has elected to catch. Thus the system call
3341 * must be retried in these cases. To handle this without massive
3342 * changes in the source code, we remap the standard system call names
3343 * to names for our own functions in sysdep.c that do the system call
3344 * with retries. Actually, for portability reasons, it is good
3345 * programming practice, as this example shows, to limit all actual
3346 * system calls to a single occurrence in the source. Sure, this
3347 * adds an extra level of function call overhead but it is almost
3348 * always negligible. Fred Fish, Unisoft Systems Inc.
3352 * Warning, this function may not duplicate 4.2 action properly
3353 * under error conditions.
3356 #ifndef MAXPATHLEN
3357 /* In 4.1, param.h fails to define this. */
3358 #define MAXPATHLEN 1024
3359 #endif
3361 #ifndef HAVE_GETWD
3363 char *
3364 getwd (pathname)
3365 char *pathname;
3367 char *npath, *spath;
3368 extern char *getcwd ();
3370 BLOCK_INPUT; /* getcwd uses malloc */
3371 spath = npath = getcwd ((char *) 0, MAXPATHLEN);
3372 if (spath == 0)
3374 UNBLOCK_INPUT;
3375 return spath;
3377 /* On Altos 3068, getcwd can return @hostname/dir, so discard
3378 up to first slash. Should be harmless on other systems. */
3379 while (*npath && *npath != '/')
3380 npath++;
3381 strcpy (pathname, npath);
3382 free (spath); /* getcwd uses malloc */
3383 UNBLOCK_INPUT;
3384 return pathname;
3387 #endif /* HAVE_GETWD */
3390 * Emulate rename using unlink/link. Note that this is
3391 * only partially correct. Also, doesn't enforce restriction
3392 * that files be of same type (regular->regular, dir->dir, etc).
3395 #ifndef HAVE_RENAME
3397 rename (from, to)
3398 const char *from;
3399 const char *to;
3401 if (access (from, 0) == 0)
3403 unlink (to);
3404 if (link (from, to) == 0)
3405 if (unlink (from) == 0)
3406 return (0);
3408 return (-1);
3411 #endif
3414 #ifdef HPUX
3415 #ifndef HAVE_PERROR
3417 /* HPUX curses library references perror, but as far as we know
3418 it won't be called. Anyway this definition will do for now. */
3420 perror ()
3424 #endif /* not HAVE_PERROR */
3425 #endif /* HPUX */
3427 #ifndef HAVE_DUP2
3430 * Emulate BSD dup2. First close newd if it already exists.
3431 * Then, attempt to dup oldd. If not successful, call dup2 recursively
3432 * until we are, then close the unsuccessful ones.
3435 dup2 (oldd, newd)
3436 int oldd;
3437 int newd;
3439 register int fd, ret;
3441 emacs_close (newd);
3443 #ifdef F_DUPFD
3444 return fcntl (oldd, F_DUPFD, newd);
3445 #else
3446 fd = dup (old);
3447 if (fd == -1)
3448 return -1;
3449 if (fd == new)
3450 return new;
3451 ret = dup2 (old,new);
3452 emacs_close (fd);
3453 return ret;
3454 #endif
3457 #endif /* not HAVE_DUP2 */
3460 * Gettimeofday. Simulate as much as possible. Only accurate
3461 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3462 * Only needed when subprocesses are defined.
3465 #ifdef subprocesses
3466 #ifndef VMS
3467 #ifndef HAVE_GETTIMEOFDAY
3468 #ifdef HAVE_TIMEVAL
3470 /* ARGSUSED */
3472 gettimeofday (tp, tzp)
3473 struct timeval *tp;
3474 struct timezone *tzp;
3476 extern long time ();
3478 tp->tv_sec = time ((long *)0);
3479 tp->tv_usec = 0;
3480 if (tzp != 0)
3481 tzp->tz_minuteswest = -1;
3482 return 0;
3485 #endif
3486 #endif
3487 #endif
3488 #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
3491 * This function will go away as soon as all the stubs fixed. (fnf)
3494 void
3495 croak (badfunc)
3496 char *badfunc;
3498 printf ("%s not yet implemented\r\n", badfunc);
3499 reset_sys_modes ();
3500 exit (1);
3503 #endif /* USG */
3505 /* Directory routines for systems that don't have them. */
3507 #ifdef SYSV_SYSTEM_DIR
3509 #include <dirent.h>
3511 #if defined (BROKEN_CLOSEDIR) || !defined (HAVE_CLOSEDIR)
3514 closedir (dirp)
3515 register DIR *dirp; /* stream from opendir */
3517 int rtnval;
3519 rtnval = emacs_close (dirp->dd_fd);
3521 /* Some systems (like Solaris) allocate the buffer and the DIR all
3522 in one block. Why in the world are we freeing this ourselves
3523 anyway? */
3524 #if ! (defined (sun) && defined (USG5_4))
3525 xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
3526 #endif
3527 xfree ((char *) dirp);
3529 return rtnval;
3531 #endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
3532 #endif /* SYSV_SYSTEM_DIR */
3534 #ifdef NONSYSTEM_DIR_LIBRARY
3536 DIR *
3537 opendir (filename)
3538 char *filename; /* name of directory */
3540 register DIR *dirp; /* -> malloc'ed storage */
3541 register int fd; /* file descriptor for read */
3542 struct stat sbuf; /* result of fstat */
3544 fd = emacs_open (filename, O_RDONLY, 0);
3545 if (fd < 0)
3546 return 0;
3548 BLOCK_INPUT;
3549 if (fstat (fd, &sbuf) < 0
3550 || (sbuf.st_mode & S_IFMT) != S_IFDIR
3551 || (dirp = (DIR *) xmalloc (sizeof (DIR))) == 0)
3553 emacs_close (fd);
3554 UNBLOCK_INPUT;
3555 return 0; /* bad luck today */
3557 UNBLOCK_INPUT;
3559 dirp->dd_fd = fd;
3560 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
3562 return dirp;
3565 void
3566 closedir (dirp)
3567 register DIR *dirp; /* stream from opendir */
3569 emacs_close (dirp->dd_fd);
3570 xfree ((char *) dirp);
3574 #ifndef VMS
3575 #define DIRSIZ 14
3576 struct olddir
3578 ino_t od_ino; /* inode */
3579 char od_name[DIRSIZ]; /* filename */
3581 #endif /* not VMS */
3583 struct direct dir_static; /* simulated directory contents */
3585 /* ARGUSED */
3586 struct direct *
3587 readdir (dirp)
3588 register DIR *dirp; /* stream from opendir */
3590 #ifndef VMS
3591 register struct olddir *dp; /* -> directory data */
3592 #else /* VMS */
3593 register struct dir$_name *dp; /* -> directory data */
3594 register struct dir$_version *dv; /* -> version data */
3595 #endif /* VMS */
3597 for (; ;)
3599 if (dirp->dd_loc >= dirp->dd_size)
3600 dirp->dd_loc = dirp->dd_size = 0;
3602 if (dirp->dd_size == 0 /* refill buffer */
3603 && (dirp->dd_size = emacs_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3604 return 0;
3606 #ifndef VMS
3607 dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
3608 dirp->dd_loc += sizeof (struct olddir);
3610 if (dp->od_ino != 0) /* not deleted entry */
3612 dir_static.d_ino = dp->od_ino;
3613 strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
3614 dir_static.d_name[DIRSIZ] = '\0';
3615 dir_static.d_namlen = strlen (dir_static.d_name);
3616 dir_static.d_reclen = sizeof (struct direct)
3617 - MAXNAMLEN + 3
3618 + dir_static.d_namlen - dir_static.d_namlen % 4;
3619 return &dir_static; /* -> simulated structure */
3621 #else /* VMS */
3622 dp = (struct dir$_name *) dirp->dd_buf;
3623 if (dirp->dd_loc == 0)
3624 dirp->dd_loc = (dp->dir$b_namecount&1) ? dp->dir$b_namecount + 1
3625 : dp->dir$b_namecount;
3626 dv = (struct dir$_version *)&dp->dir$t_name[dirp->dd_loc];
3627 dir_static.d_ino = dv->dir$w_fid_num;
3628 dir_static.d_namlen = dp->dir$b_namecount;
3629 dir_static.d_reclen = sizeof (struct direct)
3630 - MAXNAMLEN + 3
3631 + dir_static.d_namlen - dir_static.d_namlen % 4;
3632 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3633 dir_static.d_name[dir_static.d_namlen] = '\0';
3634 dirp->dd_loc = dirp->dd_size; /* only one record at a time */
3635 return &dir_static;
3636 #endif /* VMS */
3640 #ifdef VMS
3641 /* readdirver is just like readdir except it returns all versions of a file
3642 as separate entries. */
3644 /* ARGUSED */
3645 struct direct *
3646 readdirver (dirp)
3647 register DIR *dirp; /* stream from opendir */
3649 register struct dir$_name *dp; /* -> directory data */
3650 register struct dir$_version *dv; /* -> version data */
3652 if (dirp->dd_loc >= dirp->dd_size - sizeof (struct dir$_name))
3653 dirp->dd_loc = dirp->dd_size = 0;
3655 if (dirp->dd_size == 0 /* refill buffer */
3656 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3657 return 0;
3659 dp = (struct dir$_name *) dirp->dd_buf;
3660 if (dirp->dd_loc == 0)
3661 dirp->dd_loc = (dp->dir$b_namecount & 1) ? dp->dir$b_namecount + 1
3662 : dp->dir$b_namecount;
3663 dv = (struct dir$_version *) &dp->dir$t_name[dirp->dd_loc];
3664 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3665 sprintf (&dir_static.d_name[dp->dir$b_namecount], ";%d", dv->dir$w_version);
3666 dir_static.d_namlen = strlen (dir_static.d_name);
3667 dir_static.d_ino = dv->dir$w_fid_num;
3668 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3
3669 + dir_static.d_namlen - dir_static.d_namlen % 4;
3670 dirp->dd_loc = ((char *) (++dv) - dp->dir$t_name);
3671 return &dir_static;
3674 #endif /* VMS */
3676 #endif /* NONSYSTEM_DIR_LIBRARY */
3680 set_file_times (filename, atime, mtime)
3681 char *filename;
3682 EMACS_TIME atime, mtime;
3684 #ifdef HAVE_UTIMES
3685 struct timeval tv[2];
3686 tv[0] = atime;
3687 tv[1] = mtime;
3688 return utimes (filename, tv);
3689 #else /* not HAVE_UTIMES */
3690 struct utimbuf utb;
3691 utb.actime = EMACS_SECS (atime);
3692 utb.modtime = EMACS_SECS (mtime);
3693 return utime (filename, &utb);
3694 #endif /* not HAVE_UTIMES */
3697 /* mkdir and rmdir functions, for systems which don't have them. */
3699 #ifndef HAVE_MKDIR
3701 * Written by Robert Rother, Mariah Corporation, August 1985.
3703 * If you want it, it's yours. All I ask in return is that if you
3704 * figure out how to do this in a Bourne Shell script you send me
3705 * a copy.
3706 * sdcsvax!rmr or rmr@uscd
3708 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
3709 * subroutine. 11Mar86; hoptoad!gnu
3711 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3712 * subroutine didn't return EEXIST. It does now.
3716 * Make a directory.
3718 #ifdef MKDIR_PROTOTYPE
3719 MKDIR_PROTOTYPE
3720 #else
3722 mkdir (dpath, dmode)
3723 char *dpath;
3724 int dmode;
3725 #endif
3727 int cpid, status, fd;
3728 struct stat statbuf;
3730 if (stat (dpath, &statbuf) == 0)
3732 errno = EEXIST; /* Stat worked, so it already exists */
3733 return -1;
3736 /* If stat fails for a reason other than non-existence, return error */
3737 if (errno != ENOENT)
3738 return -1;
3740 synch_process_alive = 1;
3741 switch (cpid = fork ())
3744 case -1: /* Error in fork */
3745 return (-1); /* Errno is set already */
3747 case 0: /* Child process */
3749 * Cheap hack to set mode of new directory. Since this
3750 * child process is going away anyway, we zap its umask.
3751 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3752 * directory. Does anybody care?
3754 status = umask (0); /* Get current umask */
3755 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */
3756 fd = emacs_open ("/dev/null", O_RDWR, 0);
3757 if (fd >= 0)
3759 dup2 (fd, 0);
3760 dup2 (fd, 1);
3761 dup2 (fd, 2);
3763 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
3764 _exit (-1); /* Can't exec /bin/mkdir */
3766 default: /* Parent process */
3767 wait_for_termination (cpid);
3770 if (synch_process_death != 0 || synch_process_retcode != 0)
3772 errno = EIO; /* We don't know why, but */
3773 return -1; /* /bin/mkdir failed */
3776 return 0;
3778 #endif /* not HAVE_MKDIR */
3780 #ifndef HAVE_RMDIR
3782 rmdir (dpath)
3783 char *dpath;
3785 int cpid, status, fd;
3786 struct stat statbuf;
3788 if (stat (dpath, &statbuf) != 0)
3790 /* Stat just set errno. We don't have to */
3791 return -1;
3794 synch_process_alive = 1;
3795 switch (cpid = fork ())
3798 case -1: /* Error in fork */
3799 return (-1); /* Errno is set already */
3801 case 0: /* Child process */
3802 fd = emacs_open ("/dev/null", O_RDWR, 0);
3803 if (fd >= 0)
3805 dup2 (fd, 0);
3806 dup2 (fd, 1);
3807 dup2 (fd, 2);
3809 execl ("/bin/rmdir", "rmdir", dpath, (char *) 0);
3810 _exit (-1); /* Can't exec /bin/rmdir */
3812 default: /* Parent process */
3813 wait_for_termination (cpid);
3816 if (synch_process_death != 0 || synch_process_retcode != 0)
3818 errno = EIO; /* We don't know why, but */
3819 return -1; /* /bin/rmdir failed */
3822 return 0;
3824 #endif /* !HAVE_RMDIR */
3828 /* Functions for VMS */
3829 #ifdef VMS
3830 #include "vms-pwd.h"
3831 #include <acldef.h>
3832 #include <chpdef.h>
3833 #include <jpidef.h>
3835 /* Return as a string the VMS error string pertaining to STATUS.
3836 Reuses the same static buffer each time it is called. */
3838 char *
3839 vmserrstr (status)
3840 int status; /* VMS status code */
3842 int bufadr[2];
3843 short len;
3844 static char buf[257];
3846 bufadr[0] = sizeof buf - 1;
3847 bufadr[1] = (int) buf;
3848 if (! (SYS$GETMSG (status, &len, bufadr, 0x1, 0) & 1))
3849 return "untranslatable VMS error status";
3850 buf[len] = '\0';
3851 return buf;
3854 #ifdef access
3855 #undef access
3857 /* The following is necessary because 'access' emulation by VMS C (2.0) does
3858 * not work correctly. (It also doesn't work well in version 2.3.)
3861 #ifdef VMS4_4
3863 #define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3864 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3866 typedef union {
3867 struct {
3868 unsigned short s_buflen;
3869 unsigned short s_code;
3870 char *s_bufadr;
3871 unsigned short *s_retlenadr;
3872 } s;
3873 int end;
3874 } item;
3875 #define buflen s.s_buflen
3876 #define code s.s_code
3877 #define bufadr s.s_bufadr
3878 #define retlenadr s.s_retlenadr
3880 #define R_OK 4 /* test for read permission */
3881 #define W_OK 2 /* test for write permission */
3882 #define X_OK 1 /* test for execute (search) permission */
3883 #define F_OK 0 /* test for presence of file */
3886 sys_access (path, mode)
3887 char *path;
3888 int mode;
3890 static char *user = NULL;
3891 char dir_fn[512];
3893 /* translate possible directory spec into .DIR file name, so brain-dead
3894 * access can treat the directory like a file. */
3895 if (directory_file_name (path, dir_fn))
3896 path = dir_fn;
3898 if (mode == F_OK)
3899 return access (path, mode);
3900 if (user == NULL && (user = (char *) getenv ("USER")) == NULL)
3901 return -1;
3903 int stat;
3904 int flags;
3905 int acces;
3906 unsigned short int dummy;
3907 item itemlst[3];
3908 static int constant = ACL$C_FILE;
3909 DESCRIPTOR (path_desc, path);
3910 DESCRIPTOR (user_desc, user);
3912 flags = 0;
3913 acces = 0;
3914 if ((mode & X_OK) && ((stat = access (path, mode)) < 0 || mode == X_OK))
3915 return stat;
3916 if (mode & R_OK)
3917 acces |= CHP$M_READ;
3918 if (mode & W_OK)
3919 acces |= CHP$M_WRITE;
3920 itemlst[0].buflen = sizeof (int);
3921 itemlst[0].code = CHP$_FLAGS;
3922 itemlst[0].bufadr = (char *) &flags;
3923 itemlst[0].retlenadr = &dummy;
3924 itemlst[1].buflen = sizeof (int);
3925 itemlst[1].code = CHP$_ACCESS;
3926 itemlst[1].bufadr = (char *) &acces;
3927 itemlst[1].retlenadr = &dummy;
3928 itemlst[2].end = CHP$_END;
3929 stat = SYS$CHECK_ACCESS (&constant, &path_desc, &user_desc, itemlst);
3930 return stat == SS$_NORMAL ? 0 : -1;
3934 #else /* not VMS4_4 */
3936 #include <prvdef.h>
3937 #define ACE$M_WRITE 2
3938 #define ACE$C_KEYID 1
3940 static unsigned short memid, grpid;
3941 static unsigned int uic;
3943 /* Called from init_sys_modes, so it happens not very often
3944 but at least each time Emacs is loaded. */
3945 void
3946 sys_access_reinit ()
3948 uic = 0;
3952 sys_access (filename, type)
3953 char * filename;
3954 int type;
3956 struct FAB fab;
3957 struct XABPRO xab;
3958 int status, size, i, typecode, acl_controlled;
3959 unsigned int *aclptr, *aclend, aclbuf[60];
3960 union prvdef prvmask;
3962 /* Get UIC and GRP values for protection checking. */
3963 if (uic == 0)
3965 status = LIB$GETJPI (&JPI$_UIC, 0, 0, &uic, 0, 0);
3966 if (! (status & 1))
3967 return -1;
3968 memid = uic & 0xFFFF;
3969 grpid = uic >> 16;
3972 if (type != 2) /* not checking write access */
3973 return access (filename, type);
3975 /* Check write protection. */
3977 #define CHECKPRIV(bit) (prvmask.bit)
3978 #define WRITABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
3980 /* Find privilege bits */
3981 status = SYS$SETPRV (0, 0, 0, prvmask);
3982 if (! (status & 1))
3983 error ("Unable to find privileges: %s", vmserrstr (status));
3984 if (CHECKPRIV (PRV$V_BYPASS))
3985 return 0; /* BYPASS enabled */
3986 fab = cc$rms_fab;
3987 fab.fab$b_fac = FAB$M_GET;
3988 fab.fab$l_fna = filename;
3989 fab.fab$b_fns = strlen (filename);
3990 fab.fab$l_xab = &xab;
3991 xab = cc$rms_xabpro;
3992 xab.xab$l_aclbuf = aclbuf;
3993 xab.xab$w_aclsiz = sizeof (aclbuf);
3994 status = SYS$OPEN (&fab, 0, 0);
3995 if (! (status & 1))
3996 return -1;
3997 SYS$CLOSE (&fab, 0, 0);
3998 /* Check system access */
3999 if (CHECKPRIV (PRV$V_SYSPRV) && WRITABLE (XAB$V_SYS))
4000 return 0;
4001 /* Check ACL entries, if any */
4002 acl_controlled = 0;
4003 if (xab.xab$w_acllen > 0)
4005 aclptr = aclbuf;
4006 aclend = &aclbuf[xab.xab$w_acllen / 4];
4007 while (*aclptr && aclptr < aclend)
4009 size = (*aclptr & 0xff) / 4;
4010 typecode = (*aclptr >> 8) & 0xff;
4011 if (typecode == ACE$C_KEYID)
4012 for (i = size - 1; i > 1; i--)
4013 if (aclptr[i] == uic)
4015 acl_controlled = 1;
4016 if (aclptr[1] & ACE$M_WRITE)
4017 return 0; /* Write access through ACL */
4019 aclptr = &aclptr[size];
4021 if (acl_controlled) /* ACL specified, prohibits write access */
4022 return -1;
4024 /* No ACL entries specified, check normal protection */
4025 if (WRITABLE (XAB$V_WLD)) /* World writable */
4026 return 0;
4027 if (WRITABLE (XAB$V_GRP) &&
4028 (unsigned short) (xab.xab$l_uic >> 16) == grpid)
4029 return 0; /* Group writable */
4030 if (WRITABLE (XAB$V_OWN) &&
4031 (xab.xab$l_uic & 0xFFFF) == memid)
4032 return 0; /* Owner writable */
4034 return -1; /* Not writable */
4036 #endif /* not VMS4_4 */
4037 #endif /* access */
4039 static char vtbuf[NAM$C_MAXRSS+1];
4041 /* translate a vms file spec to a unix path */
4042 char *
4043 sys_translate_vms (vfile)
4044 char * vfile;
4046 char * p;
4047 char * targ;
4049 if (!vfile)
4050 return 0;
4052 targ = vtbuf;
4054 /* leading device or logical name is a root directory */
4055 if (p = strchr (vfile, ':'))
4057 *targ++ = '/';
4058 while (vfile < p)
4059 *targ++ = *vfile++;
4060 vfile++;
4061 *targ++ = '/';
4063 p = vfile;
4064 if (*p == '[' || *p == '<')
4066 while (*++vfile != *p + 2)
4067 switch (*vfile)
4069 case '.':
4070 if (vfile[-1] == *p)
4071 *targ++ = '.';
4072 *targ++ = '/';
4073 break;
4075 case '-':
4076 *targ++ = '.';
4077 *targ++ = '.';
4078 break;
4080 default:
4081 *targ++ = *vfile;
4082 break;
4084 vfile++;
4085 *targ++ = '/';
4087 while (*vfile)
4088 *targ++ = *vfile++;
4090 return vtbuf;
4093 static char utbuf[NAM$C_MAXRSS+1];
4095 /* translate a unix path to a VMS file spec */
4096 char *
4097 sys_translate_unix (ufile)
4098 char * ufile;
4100 int slash_seen = 0;
4101 char *p;
4102 char * targ;
4104 if (!ufile)
4105 return 0;
4107 targ = utbuf;
4109 if (*ufile == '/')
4111 ufile++;
4114 while (*ufile)
4116 switch (*ufile)
4118 case '/':
4119 if (slash_seen)
4120 if (index (&ufile[1], '/'))
4121 *targ++ = '.';
4122 else
4123 *targ++ = ']';
4124 else
4126 *targ++ = ':';
4127 if (index (&ufile[1], '/'))
4128 *targ++ = '[';
4129 slash_seen = 1;
4131 break;
4133 case '.':
4134 if (strncmp (ufile, "./", 2) == 0)
4136 if (!slash_seen)
4138 *targ++ = '[';
4139 slash_seen = 1;
4141 ufile++; /* skip the dot */
4142 if (index (&ufile[1], '/'))
4143 *targ++ = '.';
4144 else
4145 *targ++ = ']';
4147 else if (strncmp (ufile, "../", 3) == 0)
4149 if (!slash_seen)
4151 *targ++ = '[';
4152 slash_seen = 1;
4154 *targ++ = '-';
4155 ufile += 2; /* skip the dots */
4156 if (index (&ufile[1], '/'))
4157 *targ++ = '.';
4158 else
4159 *targ++ = ']';
4161 else
4162 *targ++ = *ufile;
4163 break;
4165 default:
4166 *targ++ = *ufile;
4167 break;
4169 ufile++;
4171 *targ = '\0';
4173 return utbuf;
4176 char *
4177 getwd (pathname)
4178 char *pathname;
4180 char *ptr, *val;
4181 extern char *getcwd ();
4183 #define MAXPATHLEN 1024
4185 ptr = xmalloc (MAXPATHLEN);
4186 val = getcwd (ptr, MAXPATHLEN);
4187 if (val == 0)
4189 xfree (ptr);
4190 return val;
4192 strcpy (pathname, ptr);
4193 xfree (ptr);
4195 return pathname;
4199 getppid ()
4201 long item_code = JPI$_OWNER;
4202 unsigned long parent_id;
4203 int status;
4205 if (((status = LIB$GETJPI (&item_code, 0, 0, &parent_id)) & 1) == 0)
4207 errno = EVMSERR;
4208 vaxc$errno = status;
4209 return -1;
4211 return parent_id;
4214 #undef getuid
4215 unsigned
4216 sys_getuid ()
4218 return (getgid () << 16) | getuid ();
4221 #undef read
4223 sys_read (fildes, buf, nbyte)
4224 int fildes;
4225 char *buf;
4226 unsigned int nbyte;
4228 return read (fildes, buf, (nbyte < MAXIOSIZE ? nbyte : MAXIOSIZE));
4231 #if 0
4233 sys_write (fildes, buf, nbyte)
4234 int fildes;
4235 char *buf;
4236 unsigned int nbyte;
4238 register int nwrote, rtnval = 0;
4240 while (nbyte > MAXIOSIZE && (nwrote = write (fildes, buf, MAXIOSIZE)) > 0) {
4241 nbyte -= nwrote;
4242 buf += nwrote;
4243 rtnval += nwrote;
4245 if (nwrote < 0)
4246 return rtnval ? rtnval : -1;
4247 if ((nwrote = write (fildes, buf, nbyte)) < 0)
4248 return rtnval ? rtnval : -1;
4249 return (rtnval + nwrote);
4251 #endif /* 0 */
4254 * VAX/VMS VAX C RTL really loses. It insists that records
4255 * end with a newline (carriage return) character, and if they
4256 * don't it adds one (nice of it isn't it!)
4258 * Thus we do this stupidity below.
4261 #undef write
4263 sys_write (fildes, buf, nbytes)
4264 int fildes;
4265 char *buf;
4266 unsigned int nbytes;
4268 register char *p;
4269 register char *e;
4270 int sum = 0;
4271 struct stat st;
4273 fstat (fildes, &st);
4274 p = buf;
4275 while (nbytes > 0)
4277 int len, retval;
4279 /* Handle fixed-length files with carriage control. */
4280 if (st.st_fab_rfm == FAB$C_FIX
4281 && ((st.st_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
4283 len = st.st_fab_mrs;
4284 retval = write (fildes, p, min (len, nbytes));
4285 if (retval != len)
4286 return -1;
4287 retval++; /* This skips the implied carriage control */
4289 else
4291 e = p + min (MAXIOSIZE, nbytes) - 1;
4292 while (*e != '\n' && e > p) e--;
4293 if (p == e) /* Ok.. so here we add a newline... sigh. */
4294 e = p + min (MAXIOSIZE, nbytes) - 1;
4295 len = e + 1 - p;
4296 retval = write (fildes, p, len);
4297 if (retval != len)
4298 return -1;
4300 p += retval;
4301 sum += retval;
4302 nbytes -= retval;
4304 return sum;
4307 /* Create file NEW copying its attributes from file OLD. If
4308 OLD is 0 or does not exist, create based on the value of
4309 vms_stmlf_recfm. */
4311 /* Protection value the file should ultimately have.
4312 Set by create_copy_attrs, and use by rename_sansversions. */
4313 static unsigned short int fab_final_pro;
4316 creat_copy_attrs (old, new)
4317 char *old, *new;
4319 struct FAB fab = cc$rms_fab;
4320 struct XABPRO xabpro;
4321 char aclbuf[256]; /* Choice of size is arbitrary. See below. */
4322 extern int vms_stmlf_recfm;
4324 if (old)
4326 fab.fab$b_fac = FAB$M_GET;
4327 fab.fab$l_fna = old;
4328 fab.fab$b_fns = strlen (old);
4329 fab.fab$l_xab = (char *) &xabpro;
4330 xabpro = cc$rms_xabpro;
4331 xabpro.xab$l_aclbuf = aclbuf;
4332 xabpro.xab$w_aclsiz = sizeof aclbuf;
4333 /* Call $OPEN to fill in the fab & xabpro fields. */
4334 if (SYS$OPEN (&fab, 0, 0) & 1)
4336 SYS$CLOSE (&fab, 0, 0);
4337 fab.fab$l_alq = 0; /* zero the allocation quantity */
4338 if (xabpro.xab$w_acllen > 0)
4340 if (xabpro.xab$w_acllen > sizeof aclbuf)
4341 /* If the acl buffer was too short, redo open with longer one.
4342 Wouldn't need to do this if there were some system imposed
4343 limit on the size of an ACL, but I can't find any such. */
4345 xabpro.xab$l_aclbuf = (char *) alloca (xabpro.xab$w_acllen);
4346 xabpro.xab$w_aclsiz = xabpro.xab$w_acllen;
4347 if (SYS$OPEN (&fab, 0, 0) & 1)
4348 SYS$CLOSE (&fab, 0, 0);
4349 else
4350 old = 0;
4353 else
4354 xabpro.xab$l_aclbuf = 0;
4356 else
4357 old = 0;
4359 fab.fab$l_fna = new;
4360 fab.fab$b_fns = strlen (new);
4361 if (!old)
4363 fab.fab$l_xab = 0;
4364 fab.fab$b_rfm = vms_stmlf_recfm ? FAB$C_STMLF : FAB$C_VAR;
4365 fab.fab$b_rat = FAB$M_CR;
4368 /* Set the file protections such that we will be able to manipulate
4369 this file. Once we are done writing and renaming it, we will set
4370 the protections back. */
4371 if (old)
4372 fab_final_pro = xabpro.xab$w_pro;
4373 else
4374 SYS$SETDFPROT (0, &fab_final_pro);
4375 xabpro.xab$w_pro &= 0xff0f; /* set O:rewd for now. This is set back later. */
4377 /* Create the new file with either default attrs or attrs copied
4378 from old file. */
4379 if (!(SYS$CREATE (&fab, 0, 0) & 1))
4380 return -1;
4381 SYS$CLOSE (&fab, 0, 0);
4382 /* As this is a "replacement" for creat, return a file descriptor
4383 opened for writing. */
4384 return open (new, O_WRONLY);
4387 #ifdef creat
4388 #undef creat
4389 #include <varargs.h>
4390 #ifdef __GNUC__
4391 #ifndef va_count
4392 #define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
4393 #endif
4394 #endif
4397 sys_creat (va_alist)
4398 va_dcl
4400 va_list list_incrementer;
4401 char *name;
4402 int mode;
4403 int rfd; /* related file descriptor */
4404 int fd; /* Our new file descriptor */
4405 int count;
4406 struct stat st_buf;
4407 char rfm[12];
4408 char rat[15];
4409 char mrs[13];
4410 char fsz[13];
4411 extern int vms_stmlf_recfm;
4413 va_count (count);
4414 va_start (list_incrementer);
4415 name = va_arg (list_incrementer, char *);
4416 mode = va_arg (list_incrementer, int);
4417 if (count > 2)
4418 rfd = va_arg (list_incrementer, int);
4419 va_end (list_incrementer);
4420 if (count > 2)
4422 /* Use information from the related file descriptor to set record
4423 format of the newly created file. */
4424 fstat (rfd, &st_buf);
4425 switch (st_buf.st_fab_rfm)
4427 case FAB$C_FIX:
4428 strcpy (rfm, "rfm = fix");
4429 sprintf (mrs, "mrs = %d", st_buf.st_fab_mrs);
4430 strcpy (rat, "rat = ");
4431 if (st_buf.st_fab_rat & FAB$M_CR)
4432 strcat (rat, "cr");
4433 else if (st_buf.st_fab_rat & FAB$M_FTN)
4434 strcat (rat, "ftn");
4435 else if (st_buf.st_fab_rat & FAB$M_PRN)
4436 strcat (rat, "prn");
4437 if (st_buf.st_fab_rat & FAB$M_BLK)
4438 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4439 strcat (rat, ", blk");
4440 else
4441 strcat (rat, "blk");
4442 return creat (name, 0, rfm, rat, mrs);
4444 case FAB$C_VFC:
4445 strcpy (rfm, "rfm = vfc");
4446 sprintf (fsz, "fsz = %d", st_buf.st_fab_fsz);
4447 strcpy (rat, "rat = ");
4448 if (st_buf.st_fab_rat & FAB$M_CR)
4449 strcat (rat, "cr");
4450 else if (st_buf.st_fab_rat & FAB$M_FTN)
4451 strcat (rat, "ftn");
4452 else if (st_buf.st_fab_rat & FAB$M_PRN)
4453 strcat (rat, "prn");
4454 if (st_buf.st_fab_rat & FAB$M_BLK)
4455 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4456 strcat (rat, ", blk");
4457 else
4458 strcat (rat, "blk");
4459 return creat (name, 0, rfm, rat, fsz);
4461 case FAB$C_STM:
4462 strcpy (rfm, "rfm = stm");
4463 break;
4465 case FAB$C_STMCR:
4466 strcpy (rfm, "rfm = stmcr");
4467 break;
4469 case FAB$C_STMLF:
4470 strcpy (rfm, "rfm = stmlf");
4471 break;
4473 case FAB$C_UDF:
4474 strcpy (rfm, "rfm = udf");
4475 break;
4477 case FAB$C_VAR:
4478 strcpy (rfm, "rfm = var");
4479 break;
4481 strcpy (rat, "rat = ");
4482 if (st_buf.st_fab_rat & FAB$M_CR)
4483 strcat (rat, "cr");
4484 else if (st_buf.st_fab_rat & FAB$M_FTN)
4485 strcat (rat, "ftn");
4486 else if (st_buf.st_fab_rat & FAB$M_PRN)
4487 strcat (rat, "prn");
4488 if (st_buf.st_fab_rat & FAB$M_BLK)
4489 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4490 strcat (rat, ", blk");
4491 else
4492 strcat (rat, "blk");
4494 else
4496 strcpy (rfm, vms_stmlf_recfm ? "rfm = stmlf" : "rfm=var");
4497 strcpy (rat, "rat=cr");
4499 /* Until the VAX C RTL fixes the many bugs with modes, always use
4500 mode 0 to get the user's default protection. */
4501 fd = creat (name, 0, rfm, rat);
4502 if (fd < 0 && errno == EEXIST)
4504 if (unlink (name) < 0)
4505 report_file_error ("delete", build_string (name));
4506 fd = creat (name, 0, rfm, rat);
4508 return fd;
4510 #endif /* creat */
4512 /* fwrite to stdout is S L O W. Speed it up by using fputc...*/
4514 sys_fwrite (ptr, size, num, fp)
4515 register char * ptr;
4516 FILE * fp;
4518 register int tot = num * size;
4520 while (tot--)
4521 fputc (*ptr++, fp);
4522 return num;
4526 * The VMS C library routine creat actually creates a new version of an
4527 * existing file rather than truncating the old version. There are times
4528 * when this is not the desired behavior, for instance, when writing an
4529 * auto save file (you only want one version), or when you don't have
4530 * write permission in the directory containing the file (but the file
4531 * itself is writable). Hence this routine, which is equivalent to
4532 * "close (creat (fn, 0));" on Unix if fn already exists.
4535 vms_truncate (fn)
4536 char *fn;
4538 struct FAB xfab = cc$rms_fab;
4539 struct RAB xrab = cc$rms_rab;
4540 int status;
4542 xfab.fab$l_fop = FAB$M_TEF; /* free allocated but unused blocks on close */
4543 xfab.fab$b_fac = FAB$M_TRN | FAB$M_GET; /* allow truncate and get access */
4544 xfab.fab$b_shr = FAB$M_NIL; /* allow no sharing - file must be locked */
4545 xfab.fab$l_fna = fn;
4546 xfab.fab$b_fns = strlen (fn);
4547 xfab.fab$l_dna = ";0"; /* default to latest version of the file */
4548 xfab.fab$b_dns = 2;
4549 xrab.rab$l_fab = &xfab;
4551 /* This gibberish opens the file, positions to the first record, and
4552 deletes all records from there until the end of file. */
4553 if ((SYS$OPEN (&xfab) & 01) == 01)
4555 if ((SYS$CONNECT (&xrab) & 01) == 01 &&
4556 (SYS$FIND (&xrab) & 01) == 01 &&
4557 (SYS$TRUNCATE (&xrab) & 01) == 01)
4558 status = 0;
4559 else
4560 status = -1;
4562 else
4563 status = -1;
4564 SYS$CLOSE (&xfab);
4565 return status;
4568 /* Define this symbol to actually read SYSUAF.DAT. This requires either
4569 SYSPRV or a readable SYSUAF.DAT. */
4571 #ifdef READ_SYSUAF
4573 * getuaf.c
4575 * Routine to read the VMS User Authorization File and return
4576 * a specific user's record.
4579 static struct UAF retuaf;
4581 struct UAF *
4582 get_uaf_name (uname)
4583 char * uname;
4585 register status;
4586 struct FAB uaf_fab;
4587 struct RAB uaf_rab;
4589 uaf_fab = cc$rms_fab;
4590 uaf_rab = cc$rms_rab;
4591 /* initialize fab fields */
4592 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4593 uaf_fab.fab$b_fns = 21;
4594 uaf_fab.fab$b_fac = FAB$M_GET;
4595 uaf_fab.fab$b_org = FAB$C_IDX;
4596 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4597 /* initialize rab fields */
4598 uaf_rab.rab$l_fab = &uaf_fab;
4599 /* open the User Authorization File */
4600 status = SYS$OPEN (&uaf_fab);
4601 if (!(status&1))
4603 errno = EVMSERR;
4604 vaxc$errno = status;
4605 return 0;
4607 status = SYS$CONNECT (&uaf_rab);
4608 if (!(status&1))
4610 errno = EVMSERR;
4611 vaxc$errno = status;
4612 return 0;
4614 /* read the requested record - index is in uname */
4615 uaf_rab.rab$l_kbf = uname;
4616 uaf_rab.rab$b_ksz = strlen (uname);
4617 uaf_rab.rab$b_rac = RAB$C_KEY;
4618 uaf_rab.rab$l_ubf = (char *)&retuaf;
4619 uaf_rab.rab$w_usz = sizeof retuaf;
4620 status = SYS$GET (&uaf_rab);
4621 if (!(status&1))
4623 errno = EVMSERR;
4624 vaxc$errno = status;
4625 return 0;
4627 /* close the User Authorization File */
4628 status = SYS$DISCONNECT (&uaf_rab);
4629 if (!(status&1))
4631 errno = EVMSERR;
4632 vaxc$errno = status;
4633 return 0;
4635 status = SYS$CLOSE (&uaf_fab);
4636 if (!(status&1))
4638 errno = EVMSERR;
4639 vaxc$errno = status;
4640 return 0;
4642 return &retuaf;
4645 struct UAF *
4646 get_uaf_uic (uic)
4647 unsigned long uic;
4649 register status;
4650 struct FAB uaf_fab;
4651 struct RAB uaf_rab;
4653 uaf_fab = cc$rms_fab;
4654 uaf_rab = cc$rms_rab;
4655 /* initialize fab fields */
4656 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4657 uaf_fab.fab$b_fns = 21;
4658 uaf_fab.fab$b_fac = FAB$M_GET;
4659 uaf_fab.fab$b_org = FAB$C_IDX;
4660 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4661 /* initialize rab fields */
4662 uaf_rab.rab$l_fab = &uaf_fab;
4663 /* open the User Authorization File */
4664 status = SYS$OPEN (&uaf_fab);
4665 if (!(status&1))
4667 errno = EVMSERR;
4668 vaxc$errno = status;
4669 return 0;
4671 status = SYS$CONNECT (&uaf_rab);
4672 if (!(status&1))
4674 errno = EVMSERR;
4675 vaxc$errno = status;
4676 return 0;
4678 /* read the requested record - index is in uic */
4679 uaf_rab.rab$b_krf = 1; /* 1st alternate key */
4680 uaf_rab.rab$l_kbf = (char *) &uic;
4681 uaf_rab.rab$b_ksz = sizeof uic;
4682 uaf_rab.rab$b_rac = RAB$C_KEY;
4683 uaf_rab.rab$l_ubf = (char *)&retuaf;
4684 uaf_rab.rab$w_usz = sizeof retuaf;
4685 status = SYS$GET (&uaf_rab);
4686 if (!(status&1))
4688 errno = EVMSERR;
4689 vaxc$errno = status;
4690 return 0;
4692 /* close the User Authorization File */
4693 status = SYS$DISCONNECT (&uaf_rab);
4694 if (!(status&1))
4696 errno = EVMSERR;
4697 vaxc$errno = status;
4698 return 0;
4700 status = SYS$CLOSE (&uaf_fab);
4701 if (!(status&1))
4703 errno = EVMSERR;
4704 vaxc$errno = status;
4705 return 0;
4707 return &retuaf;
4710 static struct passwd retpw;
4712 struct passwd *
4713 cnv_uaf_pw (up)
4714 struct UAF * up;
4716 char * ptr;
4718 /* copy these out first because if the username is 32 chars, the next
4719 section will overwrite the first byte of the UIC */
4720 retpw.pw_uid = up->uaf$w_mem;
4721 retpw.pw_gid = up->uaf$w_grp;
4723 /* I suppose this is not the best style, to possibly overwrite one
4724 byte beyond the end of the field, but what the heck... */
4725 ptr = &up->uaf$t_username[UAF$S_USERNAME];
4726 while (ptr[-1] == ' ')
4727 ptr--;
4728 *ptr = '\0';
4729 strcpy (retpw.pw_name, up->uaf$t_username);
4731 /* the rest of these are counted ascii strings */
4732 strncpy (retpw.pw_gecos, &up->uaf$t_owner[1], up->uaf$t_owner[0]);
4733 retpw.pw_gecos[up->uaf$t_owner[0]] = '\0';
4734 strncpy (retpw.pw_dir, &up->uaf$t_defdev[1], up->uaf$t_defdev[0]);
4735 retpw.pw_dir[up->uaf$t_defdev[0]] = '\0';
4736 strncat (retpw.pw_dir, &up->uaf$t_defdir[1], up->uaf$t_defdir[0]);
4737 retpw.pw_dir[up->uaf$t_defdev[0] + up->uaf$t_defdir[0]] = '\0';
4738 strncpy (retpw.pw_shell, &up->uaf$t_defcli[1], up->uaf$t_defcli[0]);
4739 retpw.pw_shell[up->uaf$t_defcli[0]] = '\0';
4741 return &retpw;
4743 #else /* not READ_SYSUAF */
4744 static struct passwd retpw;
4745 #endif /* not READ_SYSUAF */
4747 struct passwd *
4748 getpwnam (name)
4749 char * name;
4751 #ifdef READ_SYSUAF
4752 struct UAF *up;
4753 #else
4754 char * user;
4755 char * dir;
4756 unsigned char * full;
4757 #endif /* READ_SYSUAF */
4758 char *ptr = name;
4760 while (*ptr)
4762 if ('a' <= *ptr && *ptr <= 'z')
4763 *ptr -= 040;
4764 ptr++;
4766 #ifdef READ_SYSUAF
4767 if (!(up = get_uaf_name (name)))
4768 return 0;
4769 return cnv_uaf_pw (up);
4770 #else
4771 if (strcmp (name, getenv ("USER")) == 0)
4773 retpw.pw_uid = getuid ();
4774 retpw.pw_gid = getgid ();
4775 strcpy (retpw.pw_name, name);
4776 if (full = egetenv ("FULLNAME"))
4777 strcpy (retpw.pw_gecos, full);
4778 else
4779 *retpw.pw_gecos = '\0';
4780 strcpy (retpw.pw_dir, egetenv ("HOME"));
4781 *retpw.pw_shell = '\0';
4782 return &retpw;
4784 else
4785 return 0;
4786 #endif /* not READ_SYSUAF */
4789 struct passwd *
4790 getpwuid (uid)
4791 unsigned long uid;
4793 #ifdef READ_SYSUAF
4794 struct UAF * up;
4796 if (!(up = get_uaf_uic (uid)))
4797 return 0;
4798 return cnv_uaf_pw (up);
4799 #else
4800 if (uid == sys_getuid ())
4801 return getpwnam (egetenv ("USER"));
4802 else
4803 return 0;
4804 #endif /* not READ_SYSUAF */
4807 /* return total address space available to the current process. This is
4808 the sum of the current p0 size, p1 size and free page table entries
4809 available. */
4811 vlimit ()
4813 int item_code;
4814 unsigned long free_pages;
4815 unsigned long frep0va;
4816 unsigned long frep1va;
4817 register status;
4819 item_code = JPI$_FREPTECNT;
4820 if (((status = LIB$GETJPI (&item_code, 0, 0, &free_pages)) & 1) == 0)
4822 errno = EVMSERR;
4823 vaxc$errno = status;
4824 return -1;
4826 free_pages *= 512;
4828 item_code = JPI$_FREP0VA;
4829 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep0va)) & 1) == 0)
4831 errno = EVMSERR;
4832 vaxc$errno = status;
4833 return -1;
4835 item_code = JPI$_FREP1VA;
4836 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep1va)) & 1) == 0)
4838 errno = EVMSERR;
4839 vaxc$errno = status;
4840 return -1;
4843 return free_pages + frep0va + (0x7fffffff - frep1va);
4847 define_logical_name (varname, string)
4848 char *varname;
4849 char *string;
4851 struct dsc$descriptor_s strdsc =
4852 {strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string};
4853 struct dsc$descriptor_s envdsc =
4854 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4855 struct dsc$descriptor_s lnmdsc =
4856 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4858 return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
4862 delete_logical_name (varname)
4863 char *varname;
4865 struct dsc$descriptor_s envdsc =
4866 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4867 struct dsc$descriptor_s lnmdsc =
4868 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4870 return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
4874 ulimit ()
4876 return 0;
4880 setpgrp ()
4882 return 0;
4886 execvp ()
4888 error ("execvp system call not implemented");
4889 return -1;
4893 rename (from, to)
4894 char *from, *to;
4896 int status;
4897 struct FAB from_fab = cc$rms_fab, to_fab = cc$rms_fab;
4898 struct NAM from_nam = cc$rms_nam, to_nam = cc$rms_nam;
4899 char from_esn[NAM$C_MAXRSS];
4900 char to_esn[NAM$C_MAXRSS];
4902 from_fab.fab$l_fna = from;
4903 from_fab.fab$b_fns = strlen (from);
4904 from_fab.fab$l_nam = &from_nam;
4905 from_fab.fab$l_fop = FAB$M_NAM;
4907 from_nam.nam$l_esa = from_esn;
4908 from_nam.nam$b_ess = sizeof from_esn;
4910 to_fab.fab$l_fna = to;
4911 to_fab.fab$b_fns = strlen (to);
4912 to_fab.fab$l_nam = &to_nam;
4913 to_fab.fab$l_fop = FAB$M_NAM;
4915 to_nam.nam$l_esa = to_esn;
4916 to_nam.nam$b_ess = sizeof to_esn;
4918 status = SYS$RENAME (&from_fab, 0, 0, &to_fab);
4920 if (status & 1)
4921 return 0;
4922 else
4924 if (status == RMS$_DEV)
4925 errno = EXDEV;
4926 else
4927 errno = EVMSERR;
4928 vaxc$errno = status;
4929 return -1;
4933 /* This function renames a file like `rename', but it strips
4934 the version number from the "to" filename, such that the "to" file is
4935 will always be a new version. It also sets the file protection once it is
4936 finished. The protection that we will use is stored in fab_final_pro,
4937 and was set when we did a creat_copy_attrs to create the file that we
4938 are renaming.
4940 We could use the chmod function, but Eunichs uses 3 bits per user category
4941 to describe the protection, and VMS uses 4 (write and delete are separate
4942 bits). To maintain portability, the VMS implementation of `chmod' wires
4943 the W and D bits together. */
4946 static struct fibdef fib; /* We need this initialized to zero */
4947 char vms_file_written[NAM$C_MAXRSS];
4950 rename_sans_version (from,to)
4951 char *from, *to;
4953 short int chan;
4954 int stat;
4955 short int iosb[4];
4956 int status;
4957 struct FAB to_fab = cc$rms_fab;
4958 struct NAM to_nam = cc$rms_nam;
4959 struct dsc$descriptor fib_d ={sizeof (fib),0,0,(char*) &fib};
4960 struct dsc$descriptor fib_attr[2]
4961 = {{sizeof (fab_final_pro),ATR$C_FPRO,0,(char*) &fab_final_pro},{0,0,0,0}};
4962 char to_esn[NAM$C_MAXRSS];
4964 $DESCRIPTOR (disk,to_esn);
4966 to_fab.fab$l_fna = to;
4967 to_fab.fab$b_fns = strlen (to);
4968 to_fab.fab$l_nam = &to_nam;
4969 to_fab.fab$l_fop = FAB$M_NAM;
4971 to_nam.nam$l_esa = to_esn;
4972 to_nam.nam$b_ess = sizeof to_esn;
4974 status = SYS$PARSE (&to_fab, 0, 0); /* figure out the full file name */
4976 if (to_nam.nam$l_fnb && NAM$M_EXP_VER)
4977 *(to_nam.nam$l_ver) = '\0';
4979 stat = rename (from, to_esn);
4980 if (stat < 0)
4981 return stat;
4983 strcpy (vms_file_written, to_esn);
4985 to_fab.fab$l_fna = vms_file_written; /* this points to the versionless name */
4986 to_fab.fab$b_fns = strlen (vms_file_written);
4988 /* Now set the file protection to the correct value */
4989 SYS$OPEN (&to_fab, 0, 0); /* This fills in the nam$w_fid fields */
4991 /* Copy these fields into the fib */
4992 fib.fib$r_fid_overlay.fib$w_fid[0] = to_nam.nam$w_fid[0];
4993 fib.fib$r_fid_overlay.fib$w_fid[1] = to_nam.nam$w_fid[1];
4994 fib.fib$r_fid_overlay.fib$w_fid[2] = to_nam.nam$w_fid[2];
4996 SYS$CLOSE (&to_fab, 0, 0);
4998 stat = SYS$ASSIGN (&disk, &chan, 0, 0); /* open a channel to the disk */
4999 if (!stat)
5000 LIB$SIGNAL (stat);
5001 stat = SYS$QIOW (0, chan, IO$_MODIFY, iosb, 0, 0, &fib_d,
5002 0, 0, 0, &fib_attr, 0);
5003 if (!stat)
5004 LIB$SIGNAL (stat);
5005 stat = SYS$DASSGN (chan);
5006 if (!stat)
5007 LIB$SIGNAL (stat);
5008 strcpy (vms_file_written, to_esn); /* We will write this to the terminal*/
5009 return 0;
5013 link (file, new)
5014 char * file, * new;
5016 register status;
5017 struct FAB fab;
5018 struct NAM nam;
5019 unsigned short fid[3];
5020 char esa[NAM$C_MAXRSS];
5022 fab = cc$rms_fab;
5023 fab.fab$l_fop = FAB$M_OFP;
5024 fab.fab$l_fna = file;
5025 fab.fab$b_fns = strlen (file);
5026 fab.fab$l_nam = &nam;
5028 nam = cc$rms_nam;
5029 nam.nam$l_esa = esa;
5030 nam.nam$b_ess = NAM$C_MAXRSS;
5032 status = SYS$PARSE (&fab);
5033 if ((status & 1) == 0)
5035 errno = EVMSERR;
5036 vaxc$errno = status;
5037 return -1;
5039 status = SYS$SEARCH (&fab);
5040 if ((status & 1) == 0)
5042 errno = EVMSERR;
5043 vaxc$errno = status;
5044 return -1;
5047 fid[0] = nam.nam$w_fid[0];
5048 fid[1] = nam.nam$w_fid[1];
5049 fid[2] = nam.nam$w_fid[2];
5051 fab.fab$l_fna = new;
5052 fab.fab$b_fns = strlen (new);
5054 status = SYS$PARSE (&fab);
5055 if ((status & 1) == 0)
5057 errno = EVMSERR;
5058 vaxc$errno = status;
5059 return -1;
5062 nam.nam$w_fid[0] = fid[0];
5063 nam.nam$w_fid[1] = fid[1];
5064 nam.nam$w_fid[2] = fid[2];
5066 nam.nam$l_esa = nam.nam$l_name;
5067 nam.nam$b_esl = nam.nam$b_name + nam.nam$b_type + nam.nam$b_ver;
5069 status = SYS$ENTER (&fab);
5070 if ((status & 1) == 0)
5072 errno = EVMSERR;
5073 vaxc$errno = status;
5074 return -1;
5077 return 0;
5080 void
5081 croak (badfunc)
5082 char *badfunc;
5084 printf ("%s not yet implemented\r\n", badfunc);
5085 reset_sys_modes ();
5086 exit (1);
5089 long
5090 random ()
5092 /* Arrange to return a range centered on zero. */
5093 return rand () - (1 << 30);
5096 void
5097 srandom (seed)
5099 srand (seed);
5101 #endif /* VMS */
5103 #ifdef AIXHFT
5105 /* Called from init_sys_modes. */
5106 void
5107 hft_init ()
5109 int junk;
5111 /* If we're not on an HFT we shouldn't do any of this. We determine
5112 if we are on an HFT by trying to get an HFT error code. If this
5113 call fails, we're not on an HFT. */
5114 #ifdef IBMR2AIX
5115 if (ioctl (0, HFQERROR, &junk) < 0)
5116 return;
5117 #else /* not IBMR2AIX */
5118 if (ioctl (0, HFQEIO, 0) < 0)
5119 return;
5120 #endif /* not IBMR2AIX */
5122 /* On AIX the default hft keyboard mapping uses backspace rather than delete
5123 as the rubout key's ASCII code. Here this is changed. The bug is that
5124 there's no way to determine the old mapping, so in reset_sys_modes
5125 we need to assume that the normal map had been present. Of course, this
5126 code also doesn't help if on a terminal emulator which doesn't understand
5127 HFT VTD's. */
5129 struct hfbuf buf;
5130 struct hfkeymap keymap;
5132 buf.hf_bufp = (char *)&keymap;
5133 buf.hf_buflen = sizeof (keymap);
5134 keymap.hf_nkeys = 2;
5135 keymap.hfkey[0].hf_kpos = 15;
5136 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
5137 #ifdef IBMR2AIX
5138 keymap.hfkey[0].hf_keyidh = '<';
5139 #else /* not IBMR2AIX */
5140 keymap.hfkey[0].hf_page = '<';
5141 #endif /* not IBMR2AIX */
5142 keymap.hfkey[0].hf_char = 127;
5143 keymap.hfkey[1].hf_kpos = 15;
5144 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
5145 #ifdef IBMR2AIX
5146 keymap.hfkey[1].hf_keyidh = '<';
5147 #else /* not IBMR2AIX */
5148 keymap.hfkey[1].hf_page = '<';
5149 #endif /* not IBMR2AIX */
5150 keymap.hfkey[1].hf_char = 127;
5151 hftctl (0, HFSKBD, &buf);
5153 /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
5154 at times. */
5155 line_ins_del_ok = char_ins_del_ok = 0;
5158 /* Reset the rubout key to backspace. */
5160 void
5161 hft_reset ()
5163 struct hfbuf buf;
5164 struct hfkeymap keymap;
5165 int junk;
5167 #ifdef IBMR2AIX
5168 if (ioctl (0, HFQERROR, &junk) < 0)
5169 return;
5170 #else /* not IBMR2AIX */
5171 if (ioctl (0, HFQEIO, 0) < 0)
5172 return;
5173 #endif /* not IBMR2AIX */
5175 buf.hf_bufp = (char *)&keymap;
5176 buf.hf_buflen = sizeof (keymap);
5177 keymap.hf_nkeys = 2;
5178 keymap.hfkey[0].hf_kpos = 15;
5179 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
5180 #ifdef IBMR2AIX
5181 keymap.hfkey[0].hf_keyidh = '<';
5182 #else /* not IBMR2AIX */
5183 keymap.hfkey[0].hf_page = '<';
5184 #endif /* not IBMR2AIX */
5185 keymap.hfkey[0].hf_char = 8;
5186 keymap.hfkey[1].hf_kpos = 15;
5187 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
5188 #ifdef IBMR2AIX
5189 keymap.hfkey[1].hf_keyidh = '<';
5190 #else /* not IBMR2AIX */
5191 keymap.hfkey[1].hf_page = '<';
5192 #endif /* not IBMR2AIX */
5193 keymap.hfkey[1].hf_char = 8;
5194 hftctl (0, HFSKBD, &buf);
5197 #endif /* AIXHFT */
5199 #ifdef USE_DL_STUBS
5201 /* These are included on Sunos 4.1 when we do not use shared libraries.
5202 X11 libraries may refer to these functions but (we hope) do not
5203 actually call them. */
5205 void *
5206 dlopen ()
5208 return 0;
5211 void *
5212 dlsym ()
5214 return 0;
5218 dlclose ()
5220 return -1;
5223 #endif /* USE_DL_STUBS */
5225 #ifndef BSTRING
5227 #ifndef bzero
5229 void
5230 bzero (b, length)
5231 register char *b;
5232 register int length;
5234 #ifdef VMS
5235 short zero = 0;
5236 long max_str = 65535;
5238 while (length > max_str) {
5239 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
5240 length -= max_str;
5241 b += max_str;
5243 max_str = length;
5244 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
5245 #else
5246 while (length-- > 0)
5247 *b++ = 0;
5248 #endif /* not VMS */
5251 #endif /* no bzero */
5252 #endif /* BSTRING */
5254 #if (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY)
5255 #undef bcopy
5257 /* Saying `void' requires a declaration, above, where bcopy is used
5258 and that declaration causes pain for systems where bcopy is a macro. */
5259 bcopy (b1, b2, length)
5260 register char *b1;
5261 register char *b2;
5262 register int length;
5264 #ifdef VMS
5265 long max_str = 65535;
5267 while (length > max_str) {
5268 (void) LIB$MOVC3 (&max_str, b1, b2);
5269 length -= max_str;
5270 b1 += max_str;
5271 b2 += max_str;
5273 max_str = length;
5274 (void) LIB$MOVC3 (&length, b1, b2);
5275 #else
5276 while (length-- > 0)
5277 *b2++ = *b1++;
5278 #endif /* not VMS */
5280 #endif /* (!defined (BSTRING) && !defined (bcopy)) || defined (NEED_BCOPY) */
5282 #ifndef BSTRING
5283 #ifndef bcmp
5285 bcmp (b1, b2, length) /* This could be a macro! */
5286 register char *b1;
5287 register char *b2;
5288 register int length;
5290 #ifdef VMS
5291 struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
5292 struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
5294 return STR$COMPARE (&src1, &src2);
5295 #else
5296 while (length-- > 0)
5297 if (*b1++ != *b2++)
5298 return 1;
5300 return 0;
5301 #endif /* not VMS */
5303 #endif /* no bcmp */
5304 #endif /* not BSTRING */
5306 #ifndef HAVE_STRSIGNAL
5307 char *
5308 strsignal (code)
5309 int code;
5311 char *signame = 0;
5313 if (0 <= code && code < NSIG)
5315 #ifdef VMS
5316 signame = sys_errlist[code];
5317 #else
5318 /* Cast to suppress warning if the table has const char *. */
5319 signame = (char *) sys_siglist[code];
5320 #endif
5323 return signame;
5325 #endif /* HAVE_STRSIGNAL */