vc-parse-buffer: arrange for old properties to get cleared when their
[emacs.git] / src / sysdep.c
blob42b6ce14d65da46ce89b1372ac54472b2965f420
1 /* Interfaces to system-dependent kernel and library entries.
2 Copyright (C) 1985, 86, 87, 88, 93, 94 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <signal.h>
22 #include <setjmp.h>
24 #include <config.h>
25 #include "lisp.h"
26 #include "blockinput.h"
27 #undef NULL
29 #define min(x,y) ((x) > (y) ? (y) : (x))
31 /* In this file, open, read and write refer to the system calls,
32 not our sugared interfaces sys_open, sys_read and sys_write.
33 Contrariwise, for systems where we use the system calls directly,
34 define sys_read, etc. here as aliases for them. */
35 #ifndef read
36 #define sys_read read
37 #define sys_write write
38 #endif /* `read' is not a macro */
40 #undef read
41 #undef write
43 #ifndef close
44 #define sys_close close
45 #else
46 #undef close
47 #endif
49 #ifndef open
50 #define sys_open open
51 #else /* `open' is a macro */
52 #undef open
53 #endif /* `open' is a macro */
55 /* Does anyone other than VMS need this? */
56 #ifndef fwrite
57 #define sys_fwrite fwrite
58 #else
59 #undef fwrite
60 #endif
62 #include <stdio.h>
63 #include <sys/types.h>
64 #include <sys/stat.h>
65 #include <errno.h>
67 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
68 #include <dos.h>
69 #include "dosfns.h"
70 #include "msdos.h"
71 #include <sys/param.h>
72 #endif
74 extern int errno;
76 #ifdef VMS
77 #include <rms.h>
78 #include <ttdef.h>
79 #include <tt2def.h>
80 #include <iodef.h>
81 #include <ssdef.h>
82 #include <descrip.h>
83 #include <fibdef.h>
84 #include <atrdef.h>
85 #include <ctype.h>
86 #include <string.h>
87 #ifdef __GNUC__
88 #include <sys/file.h>
89 #else
90 #include <file.h>
91 #endif
92 #undef F_SETFL
93 #ifndef RAB$C_BID
94 #include <rab.h>
95 #endif
96 #define MAXIOSIZE ( 32 * PAGESIZE ) /* Don't I/O more than 32 blocks at a time */
97 #endif /* VMS */
99 #ifndef BSD4_1
100 #ifdef BSD /* this is done this way to avoid defined (BSD) || defined (USG)
101 because the vms compiler doesn't grok `defined' */
102 #include <fcntl.h>
103 #endif
104 #ifdef USG
105 #ifndef USG5
106 #include <fcntl.h>
107 #endif
108 #endif
109 #endif /* not 4.1 bsd */
111 #ifdef BROKEN_FASYNC
112 /* On some systems (DGUX comes to mind real fast) FASYNC causes
113 background writes to the terminal to stop all processes in the
114 process group when invoked under the csh (and probably any shell
115 with job control). This stops Emacs dead in its tracks when coming
116 up under X11. */
117 #undef FASYNC
118 #endif
120 #ifndef MSDOS
121 #include <sys/ioctl.h>
122 #endif
123 #include "systty.h"
124 #include "syswait.h"
126 #ifdef BROKEN_TIOCGWINSZ
127 #undef TIOCGWINSZ
128 #endif
130 #ifdef USG
131 #include <sys/utsname.h>
132 #include <string.h>
133 #ifndef MEMORY_IN_STRING_H
134 #include <memory.h>
135 #endif
136 #ifdef TIOCGWINSZ
137 #ifdef NEED_SIOCTL
138 #include <sys/sioctl.h>
139 #endif
140 #ifdef NEED_PTEM_H
141 #include <sys/stream.h>
142 #include <sys/ptem.h>
143 #endif
144 #endif /* TIOCGWINSZ */
145 #endif /* USG */
147 extern int quit_char;
149 #include "frame.h"
150 #include "window.h"
151 #include "termhooks.h"
152 #include "termchar.h"
153 #include "termopts.h"
154 #include "dispextern.h"
155 #include "process.h"
157 #ifdef NONSYSTEM_DIR_LIBRARY
158 #include "ndir.h"
159 #endif /* NONSYSTEM_DIR_LIBRARY */
161 #include "syssignal.h"
162 #include "systime.h"
164 /* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
165 #ifndef LPASS8
166 #define LPASS8 0
167 #endif
169 #ifdef BSD4_1
170 #define LNOFLSH 0100000
171 #endif
173 static int baud_convert[] =
174 #ifdef BAUD_CONVERT
175 BAUD_CONVERT;
176 #else
178 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
179 1800, 2400, 4800, 9600, 19200, 38400
181 #endif
183 extern short ospeed;
185 /* The file descriptor for Emacs's input terminal.
186 Under Unix, this is normaly zero except when using X;
187 under VMS, we place the input channel number here.
188 This allows us to write more code that works for both VMS and Unix. */
189 static int input_fd;
191 /* Specify a different file descriptor for further input operations. */
193 void
194 change_input_fd (fd)
195 int fd;
197 input_fd = fd;
200 /* Discard pending input on descriptor input_fd. */
202 discard_tty_input ()
204 struct emacs_tty buf;
206 if (noninteractive)
207 return;
209 /* Discarding input is not safe when the input could contain
210 replies from the X server. So don't do it. */
211 if (read_socket_hook)
212 return;
214 #ifdef VMS
215 end_kbd_input ();
216 SYS$QIOW (0, input_fd, IO$_READVBLK|IO$M_PURGE, input_iosb, 0, 0,
217 &buf.main, 0, 0, terminator_mask, 0, 0);
218 queue_kbd_input ();
219 #else /* not VMS */
220 #ifdef APOLLO
222 int zero = 0;
223 ioctl (input_fd, TIOCFLUSH, &zero);
225 #else /* not Apollo */
226 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
227 while (dos_keyread () != -1)
229 #else /* not MSDOS */
230 EMACS_GET_TTY (input_fd, &buf);
231 EMACS_SET_TTY (input_fd, &buf, 0);
232 #endif /* not MSDOS */
233 #endif /* not Apollo */
234 #endif /* not VMS */
237 #ifdef SIGTSTP
239 /* Arrange for character C to be read as the next input from
240 the terminal. */
242 stuff_char (c)
243 char c;
245 /* Should perhaps error if in batch mode */
246 #ifdef TIOCSTI
247 ioctl (input_fd, TIOCSTI, &c);
248 #else /* no TIOCSTI */
249 error ("Cannot stuff terminal input characters in this version of Unix");
250 #endif /* no TIOCSTI */
253 #endif /* SIGTSTP */
255 init_baud_rate ()
257 if (noninteractive)
258 ospeed = 0;
259 else
261 #ifdef MSDOS
262 ospeed = 15;
263 #else
264 #ifdef VMS
265 struct sensemode sg;
267 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &sg, 0, 0,
268 &sg.class, 12, 0, 0, 0, 0 );
269 ospeed = sg.xmit_baud;
270 #else /* not VMS */
271 #ifdef HAVE_TERMIOS
272 struct termios sg;
274 sg.c_cflag = B9600;
275 tcgetattr (input_fd, &sg);
276 ospeed = cfgetospeed (&sg);
277 #else /* neither VMS nor TERMIOS */
278 #ifdef HAVE_TERMIO
279 struct termio sg;
281 sg.c_cflag = B9600;
282 #ifdef HAVE_TCATTR
283 tcgetattr (input_fd, &sg);
284 #else
285 ioctl (input_fd, TCGETA, &sg);
286 #endif
287 ospeed = sg.c_cflag & CBAUD;
288 #else /* neither VMS nor TERMIOS nor TERMIO */
289 struct sgttyb sg;
291 sg.sg_ospeed = B9600;
292 if (ioctl (input_fd, TIOCGETP, &sg) < 0)
293 abort ();
294 ospeed = sg.sg_ospeed;
295 #endif /* not HAVE_TERMIO */
296 #endif /* not HAVE_TERMIOS */
297 #endif /* not VMS */
298 #endif /* not MSDOS */
301 baud_rate = (ospeed < sizeof baud_convert / sizeof baud_convert[0]
302 ? baud_convert[ospeed] : 9600);
303 if (baud_rate == 0)
304 baud_rate = 1200;
307 /*ARGSUSED*/
308 set_exclusive_use (fd)
309 int fd;
311 #ifdef FIOCLEX
312 ioctl (fd, FIOCLEX, 0);
313 #endif
314 /* Ok to do nothing if this feature does not exist */
317 #ifndef subprocesses
319 wait_without_blocking ()
321 #ifdef BSD
322 wait3 (0, WNOHANG | WUNTRACED, 0);
323 #else
324 croak ("wait_without_blocking");
325 #endif
326 synch_process_alive = 0;
329 #endif /* not subprocesses */
331 int wait_debugging; /* Set nonzero to make following function work under dbx
332 (at least for bsd). */
334 SIGTYPE
335 wait_for_termination_signal ()
338 /* Wait for subprocess with process id `pid' to terminate and
339 make sure it will get eliminated (not remain forever as a zombie) */
341 wait_for_termination (pid)
342 int pid;
344 while (1)
346 #ifdef subprocesses
347 #ifdef VMS
348 int status;
350 status = SYS$FORCEX (&pid, 0, 0);
351 break;
352 #else /* not VMS */
353 #if defined (BSD) || (defined (HPUX) && !defined (HPUX_5))
354 /* Note that kill returns -1 even if the process is just a zombie now.
355 But inevitably a SIGCHLD interrupt should be generated
356 and child_sig will do wait3 and make the process go away. */
357 /* There is some indication that there is a bug involved with
358 termination of subprocesses, perhaps involving a kernel bug too,
359 but no idea what it is. Just as a hunch we signal SIGCHLD to see
360 if that causes the problem to go away or get worse. */
361 sigsetmask (sigmask (SIGCHLD));
362 if (0 > kill (pid, 0))
364 sigsetmask (SIGEMPTYMASK);
365 kill (getpid (), SIGCHLD);
366 break;
368 if (wait_debugging)
369 sleep (1);
370 else
371 sigpause (SIGEMPTYMASK);
372 #else /* not BSD, and not HPUX version >= 6 */
373 #if defined (UNIPLUS)
374 if (0 > kill (pid, 0))
375 break;
376 wait (0);
377 #else /* neither BSD nor UNIPLUS: random sysV */
378 #ifdef POSIX_SIGNALS /* would this work for LINUX as well? */
379 sigblock (sigmask (SIGCHLD));
380 if (0 > kill (pid, 0))
382 sigunblock (sigmask (SIGCHLD));
383 break;
385 sigpause (SIGEMPTYMASK);
386 #else /* not POSIX_SIGNALS */
387 #ifdef HAVE_SYSV_SIGPAUSE
388 sighold (SIGCHLD);
389 if (0 > kill (pid, 0))
391 sigrelse (SIGCHLD);
392 break;
394 sigpause (SIGCHLD);
395 #else /* not HAVE_SYSV_SIGPAUSE */
396 if (0 > kill (pid, 0))
397 break;
398 /* Using sleep instead of pause avoids timing error.
399 If the inferior dies just before the sleep,
400 we lose just one second. */
401 sleep (1);
402 #endif /* not HAVE_SYSV_SIGPAUSE */
403 #endif /* not POSIX_SIGNALS */
404 #endif /* not UNIPLUS */
405 #endif /* not BSD, and not HPUX version >= 6 */
406 #endif /* not VMS */
407 #else /* not subprocesses */
408 #ifndef BSD4_1
409 if (kill (pid, 0) < 0)
410 break;
411 wait (0);
412 #else /* BSD4_1 */
413 int status;
414 status = wait (0);
415 if (status == pid || status == -1)
416 break;
417 #endif /* BSD4_1 */
418 #endif /* not subprocesses */
422 #ifdef subprocesses
425 * flush any pending output
426 * (may flush input as well; it does not matter the way we use it)
429 flush_pending_output (channel)
430 int channel;
432 #ifdef HAVE_TERMIOS
433 /* If we try this, we get hit with SIGTTIN, because
434 the child's tty belongs to the child's pgrp. */
435 #else
436 #ifdef TCFLSH
437 ioctl (channel, TCFLSH, 1);
438 #else
439 #ifdef TIOCFLUSH
440 int zero = 0;
441 /* 3rd arg should be ignored
442 but some 4.2 kernels actually want the address of an int
443 and nonzero means something different. */
444 ioctl (channel, TIOCFLUSH, &zero);
445 #endif
446 #endif
447 #endif
450 #ifndef VMS
451 /* Set up the terminal at the other end of a pseudo-terminal that
452 we will be controlling an inferior through.
453 It should not echo or do line-editing, since that is done
454 in Emacs. No padding needed for insertion into an Emacs buffer. */
456 child_setup_tty (out)
457 int out;
459 #ifndef MSDOS
460 struct emacs_tty s;
462 EMACS_GET_TTY (out, &s);
464 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
465 s.main.c_oflag |= OPOST; /* Enable output postprocessing */
466 s.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL on output */
467 s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
468 /* No output delays */
469 s.main.c_lflag &= ~ECHO; /* Disable echo */
470 s.main.c_lflag |= ISIG; /* Enable signals */
471 s.main.c_iflag &= ~IUCLC; /* Disable map of upper case to lower on
472 input */
473 s.main.c_oflag &= ~OLCUC; /* Disable map of lower case to upper on
474 output */
475 s.main.c_cflag = (s.main.c_cflag & ~CSIZE) | CS8; /* Don't strip 8th bit */
476 #if 0
477 /* Said to be unnecessary: */
478 s.main.c_cc[VMIN] = 1; /* minimum number of characters to accept */
479 s.main.c_cc[VTIME] = 0; /* wait forever for at least 1 character */
480 #endif
482 s.main.c_lflag |= ICANON; /* Enable erase/kill and eof processing */
483 s.main.c_cc[VEOF] = 04; /* insure that EOF is Control-D */
484 s.main.c_cc[VERASE] = 0377; /* disable erase processing */
485 s.main.c_cc[VKILL] = 0377; /* disable kill processing */
487 #ifdef HPUX
488 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
489 #endif /* HPUX */
491 #ifdef AIX
492 /* AIX enhanced edit loses NULs, so disable it */
493 #ifndef IBMR2AIX
494 s.main.c_line = 0;
495 s.main.c_iflag &= ~ASCEDIT;
496 #endif
497 /* Also, PTY overloads NUL and BREAK.
498 don't ignore break, but don't signal either, so it looks like NUL. */
499 s.main.c_iflag &= ~IGNBRK;
500 s.main.c_iflag &= ~BRKINT;
501 /* QUIT and INTR work better as signals, so disable character forms */
502 s.main.c_cc[VINTR] = 0377;
503 #ifdef SIGNALS_VIA_CHARACTERS
504 /* the QUIT and INTR character are used in process_send_signal
505 so set them here to something useful. */
506 if (s.main.c_cc[VQUIT] == 0377)
507 s.main.c_cc[VQUIT] = '\\'&037; /* Control-\ */
508 if (s.main.c_cc[VINTR] == 0377)
509 s.main.c_cc[VINTR] = 'C'&037; /* Control-C */
510 #else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
511 /* QUIT and INTR work better as signals, so disable character forms */
512 s.main.c_cc[VQUIT] = 0377;
513 s.main.c_cc[VINTR] = 0377;
514 s.main.c_lflag &= ~ISIG;
515 #endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
516 s.main.c_cc[VEOL] = 0377;
517 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
518 #endif /* AIX */
520 #else /* not HAVE_TERMIO */
522 s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
523 | CBREAK | TANDEM);
524 s.main.sg_flags |= LPASS8;
525 s.main.sg_erase = 0377;
526 s.main.sg_kill = 0377;
527 s.lmode = LLITOUT | s.lmode; /* Don't strip 8th bit */
529 #endif /* not HAVE_TERMIO */
531 EMACS_SET_TTY (out, &s, 0);
533 #ifdef BSD4_1
534 if (interrupt_input)
535 reset_sigio ();
536 #endif /* BSD4_1 */
537 #ifdef RTU
539 int zero = 0;
540 ioctl (out, FIOASYNC, &zero);
542 #endif /* RTU */
543 #endif /* not MSDOS */
545 #endif /* not VMS */
547 #endif /* subprocesses */
549 /*ARGSUSED*/
550 setpgrp_of_tty (pid)
551 int pid;
553 EMACS_SET_TTY_PGRP (input_fd, &pid);
556 /* Record a signal code and the handler for it. */
557 struct save_signal
559 int code;
560 SIGTYPE (*handler) ();
563 /* Suspend the Emacs process; give terminal to its superior. */
565 sys_suspend ()
567 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
568 int st;
569 char oldwd[MAXPATHLEN+1]; /* Fixed length is safe on MSDOS. */
570 #endif
571 #ifdef VMS
572 /* "Foster" parentage allows emacs to return to a subprocess that attached
573 to the current emacs as a cheaper than starting a whole new process. This
574 is set up by KEPTEDITOR.COM. */
575 unsigned long parent_id, foster_parent_id;
576 char *fpid_string;
578 fpid_string = getenv ("EMACS_PARENT_PID");
579 if (fpid_string != NULL)
581 sscanf (fpid_string, "%x", &foster_parent_id);
582 if (foster_parent_id != 0)
583 parent_id = foster_parent_id;
584 else
585 parent_id = getppid ();
587 else
588 parent_id = getppid ();
590 xfree (fpid_string); /* On VMS, this was malloc'd */
592 if (parent_id && parent_id != 0xffffffff)
594 SIGTYPE (*oldsig)() = (int) signal (SIGINT, SIG_IGN);
595 int status = LIB$ATTACH (&parent_id) & 1;
596 signal (SIGINT, oldsig);
597 return status;
599 else
601 struct {
602 int l;
603 char *a;
604 } d_prompt;
605 d_prompt.l = sizeof ("Emacs: "); /* Our special prompt */
606 d_prompt.a = "Emacs: "; /* Just a reminder */
607 LIB$SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt, 0);
608 return 1;
610 return -1;
611 #else
612 #if defined(SIGTSTP) && !defined(MSDOS)
615 int pgrp = EMACS_GETPGRP (0);
616 EMACS_KILLPG (pgrp, SIGTSTP);
619 #else /* No SIGTSTP */
620 #ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
621 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
622 kill (getpid (), SIGQUIT);
624 #else /* No SIGTSTP or USG_JOBCTRL */
626 /* On a system where suspending is not implemented,
627 instead fork a subshell and let it talk directly to the terminal
628 while we wait. */
629 int pid = fork ();
630 struct save_signal saved_handlers[5];
632 saved_handlers[0].code = SIGINT;
633 saved_handlers[1].code = SIGQUIT;
634 saved_handlers[2].code = SIGTERM;
635 #ifdef SIGIO
636 saved_handlers[3].code = SIGIO;
637 saved_handlers[4].code = 0;
638 #else
639 saved_handlers[3].code = 0;
640 #endif
642 if (pid == -1)
643 error ("Can't spawn subshell");
644 if (pid == 0)
646 char *sh;
648 #ifdef MSDOS /* MW, Aug 1993 */
649 getwd (oldwd);
650 #endif
651 sh = (char *) egetenv ("SHELL");
652 if (sh == 0)
653 sh = "sh";
655 /* Use our buffer's default directory for the subshell. */
657 Lisp_Object dir;
658 unsigned char *str;
659 int len;
661 /* mentioning current_buffer->buffer would mean including buffer.h,
662 which somehow wedges the hp compiler. So instead... */
664 dir = intern ("default-directory");
665 /* Can't use NILP */
666 if (XFASTINT (Fboundp (dir)) == XFASTINT (Qnil))
667 goto xyzzy;
668 dir = Fsymbol_value (dir);
669 if (XTYPE (dir) != Lisp_String)
670 goto xyzzy;
672 str = (unsigned char *) alloca (XSTRING (dir)->size + 2);
673 len = XSTRING (dir)->size;
674 bcopy (XSTRING (dir)->data, str, len);
675 if (str[len - 1] != '/') str[len++] = '/';
676 str[len] = 0;
677 chdir (str);
679 xyzzy:
680 #ifdef subprocesses
681 close_process_descs (); /* Close Emacs's pipes/ptys */
682 #endif
684 #ifdef PRIO_PROCESS
686 extern int emacs_priority;
688 if (emacs_priority)
689 nice (-emacs_priority);
691 #endif
693 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
694 st = system (sh);
695 chdir (oldwd);
696 if (st)
697 report_file_error ("Can't execute subshell", Fcons (build_string (sh), Qnil));
698 #else /* not MSDOS */
699 execlp (sh, sh, 0);
700 write (1, "Can't execute subshell", 22);
701 _exit (1);
702 #endif /* not MSDOS */
705 save_signal_handlers (saved_handlers);
706 synch_process_alive = 1;
707 wait_for_termination (pid);
708 restore_signal_handlers (saved_handlers);
710 #endif /* no USG_JOBCTRL */
711 #endif /* no SIGTSTP */
712 #endif /* not VMS */
715 save_signal_handlers (saved_handlers)
716 struct save_signal *saved_handlers;
718 while (saved_handlers->code)
720 saved_handlers->handler
721 = (SIGTYPE (*) ()) signal (saved_handlers->code, SIG_IGN);
722 saved_handlers++;
726 restore_signal_handlers (saved_handlers)
727 struct save_signal *saved_handlers;
729 while (saved_handlers->code)
731 signal (saved_handlers->code, saved_handlers->handler);
732 saved_handlers++;
736 #ifdef F_SETFL
738 int old_fcntl_flags;
740 init_sigio ()
742 #ifdef FASYNC
743 old_fcntl_flags = fcntl (input_fd, F_GETFL, 0) & ~FASYNC;
744 #endif
745 request_sigio ();
748 reset_sigio ()
750 unrequest_sigio ();
753 #ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
755 request_sigio ()
757 #ifdef SIGWINCH
758 sigunblock (sigmask (SIGWINCH));
759 #endif
760 fcntl (input_fd, F_SETFL, old_fcntl_flags | FASYNC);
762 interrupts_deferred = 0;
765 unrequest_sigio ()
767 #ifdef SIGWINCH
768 sigblock (sigmask (SIGWINCH));
769 #endif
770 fcntl (input_fd, F_SETFL, old_fcntl_flags);
771 interrupts_deferred = 1;
774 #else /* no FASYNC */
775 #ifdef STRIDE /* Stride doesn't have FASYNC - use FIOASYNC */
777 request_sigio ()
779 int on = 1;
780 ioctl (input_fd, FIOASYNC, &on);
781 interrupts_deferred = 0;
784 unrequest_sigio ()
786 int off = 0;
788 ioctl (input_fd, FIOASYNC, &off);
789 interrupts_deferred = 1;
792 #else /* not FASYNC, not STRIDE */
794 #ifdef _CX_UX
796 #include <termios.h>
798 request_sigio ()
800 int on = 1;
801 sigset_t st;
803 sigemptyset(&st);
804 sigaddset(&st, SIGIO);
805 ioctl (input_fd, FIOASYNC, &on);
806 interrupts_deferred = 0;
807 sigprocmask(SIG_UNBLOCK, &st, (sigset_t *)0);
810 unrequest_sigio ()
812 int off = 0;
814 ioctl (input_fd, FIOASYNC, &off);
815 interrupts_deferred = 1;
818 #else /* ! _CX_UX */
820 request_sigio ()
822 croak ("request_sigio");
825 unrequest_sigio ()
827 croak ("unrequest_sigio");
830 #endif /* _CX_UX */
831 #endif /* STRIDE */
832 #endif /* FASYNC */
833 #endif /* F_SETFL */
835 /* Saving and restoring the process group of Emacs's terminal. */
837 #ifdef BSD
839 /* The process group of which Emacs was a member when it initially
840 started.
842 If Emacs was in its own process group (i.e. inherited_pgroup ==
843 getpid ()), then we know we're running under a shell with job
844 control (Emacs would never be run as part of a pipeline).
845 Everything is fine.
847 If Emacs was not in its own process group, then we know we're
848 running under a shell (or a caller) that doesn't know how to
849 separate itself from Emacs (like sh). Emacs must be in its own
850 process group in order to receive SIGIO correctly. In this
851 situation, we put ourselves in our own pgroup, forcibly set the
852 tty's pgroup to our pgroup, and make sure to restore and reinstate
853 the tty's pgroup just like any other terminal setting. If
854 inherited_group was not the tty's pgroup, then we'll get a
855 SIGTTmumble when we try to change the tty's pgroup, and a CONT if
856 it goes foreground in the future, which is what should happen. */
857 int inherited_pgroup;
859 /* Split off the foreground process group to Emacs alone.
860 When we are in the foreground, but not started in our own process
861 group, redirect the TTY to point to our own process group. We need
862 to be in our own process group to receive SIGIO properly. */
863 narrow_foreground_group ()
865 int me = getpid ();
867 setpgrp (0, inherited_pgroup);
868 if (inherited_pgroup != me)
869 EMACS_SET_TTY_PGRP (input_fd, &me);
870 setpgrp (0, me);
873 /* Set the tty to our original foreground group. */
874 widen_foreground_group ()
876 if (inherited_pgroup != getpid ())
877 EMACS_SET_TTY_PGRP (input_fd, &inherited_pgroup);
878 setpgrp (0, inherited_pgroup);
881 #endif
883 /* Getting and setting emacs_tty structures. */
885 /* Set *TC to the parameters associated with the terminal FD.
886 Return zero if all's well, or -1 if we ran into an error we
887 couldn't deal with. */
889 emacs_get_tty (fd, settings)
890 int fd;
891 struct emacs_tty *settings;
893 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
894 #ifdef HAVE_TCATTR
895 /* We have those nifty POSIX tcmumbleattr functions. */
896 if (tcgetattr (fd, &settings->main) < 0)
897 return -1;
899 #else
900 #ifdef HAVE_TERMIO
901 /* The SYSV-style interface? */
902 if (ioctl (fd, TCGETA, &settings->main) < 0)
903 return -1;
905 #else
906 #ifdef VMS
907 /* Vehemently Monstrous System? :-) */
908 if (! (SYS$QIOW (0, fd, IO$_SENSEMODE, settings, 0, 0,
909 &settings->main.class, 12, 0, 0, 0, 0)
910 & 1))
911 return -1;
913 #else
914 #ifndef MSDOS
915 /* I give up - I hope you have the BSD ioctls. */
916 if (ioctl (fd, TIOCGETP, &settings->main) < 0)
917 return -1;
918 #endif /* not MSDOS */
919 #endif
920 #endif
921 #endif
923 /* Suivant - Do we have to get struct ltchars data? */
924 #ifdef HAVE_LTCHARS
925 if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
926 return -1;
927 #endif
929 /* How about a struct tchars and a wordful of lmode bits? */
930 #ifdef HAVE_TCHARS
931 if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
932 || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
933 return -1;
934 #endif
936 /* We have survived the tempest. */
937 return 0;
941 /* Set the parameters of the tty on FD according to the contents of
942 *SETTINGS. If WAITP is non-zero, we wait for all queued output to
943 be written before making the change; otherwise, we forget any
944 queued input and make the change immediately.
945 Return 0 if all went well, and -1 if anything failed. */
947 emacs_set_tty (fd, settings, waitp)
948 int fd;
949 struct emacs_tty *settings;
950 int waitp;
952 /* Set the primary parameters - baud rate, character size, etcetera. */
953 #ifdef HAVE_TCATTR
954 int i;
955 /* We have those nifty POSIX tcmumbleattr functions.
956 William J. Smith <wjs@wiis.wang.com> writes:
957 "POSIX 1003.1 defines tcsetattr() to return success if it was
958 able to perform any of the requested actions, even if some
959 of the requested actions could not be performed.
960 We must read settings back to ensure tty setup properly.
961 AIX requires this to keep tty from hanging occasionally." */
962 /* This make sure that we don't loop indefinitely in here. */
963 for (i = 0 ; i < 10 ; i++)
964 if (tcsetattr (fd, waitp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
966 if (errno == EINTR)
967 continue;
968 else
969 return -1;
971 else
973 struct termios new;
975 /* Get the current settings, and see if they're what we asked for. */
976 tcgetattr (fd, &new);
977 /* We cannot use memcmp on the whole structure here because under
978 * aix386 the termios structure has some reserved field that may
979 * not be filled in.
981 if ( new.c_iflag == settings->main.c_iflag
982 && new.c_oflag == settings->main.c_oflag
983 && new.c_cflag == settings->main.c_cflag
984 && new.c_lflag == settings->main.c_lflag
985 && memcmp(new.c_cc, settings->main.c_cc, NCCS) == 0)
986 break;
987 else
988 continue;
991 #else
992 #ifdef HAVE_TERMIO
993 /* The SYSV-style interface? */
994 if (ioctl (fd, waitp ? TCSETAW : TCSETAF, &settings->main) < 0)
995 return -1;
997 #else
998 #ifdef VMS
999 /* Vehemently Monstrous System? :-) */
1000 if (! (SYS$QIOW (0, fd, IO$_SETMODE, &input_iosb, 0, 0,
1001 &settings->main.class, 12, 0, 0, 0, 0)
1002 & 1))
1003 return -1;
1005 #else
1006 #ifndef MSDOS
1007 /* I give up - I hope you have the BSD ioctls. */
1008 if (ioctl (fd, (waitp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
1009 return -1;
1010 #endif /* not MSDOS */
1012 #endif
1013 #endif
1014 #endif
1016 /* Suivant - Do we have to get struct ltchars data? */
1017 #ifdef HAVE_LTCHARS
1018 if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
1019 return -1;
1020 #endif
1022 /* How about a struct tchars and a wordful of lmode bits? */
1023 #ifdef HAVE_TCHARS
1024 if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
1025 || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
1026 return -1;
1027 #endif
1029 /* We have survived the tempest. */
1030 return 0;
1034 /* The initial tty mode bits */
1035 struct emacs_tty old_tty;
1037 int term_initted; /* 1 if outer tty status has been recorded */
1039 #ifdef BSD4_1
1040 /* BSD 4.1 needs to keep track of the lmode bits in order to start
1041 sigio. */
1042 int lmode;
1043 #endif
1045 #ifndef F_SETOWN_BUG
1046 #ifdef F_SETOWN
1047 int old_fcntl_owner;
1048 #endif /* F_SETOWN */
1049 #endif /* F_SETOWN_BUG */
1051 /* This may also be defined in stdio,
1052 but if so, this does no harm,
1053 and using the same name avoids wasting the other one's space. */
1055 #if defined (USG) || defined (DGUX)
1056 unsigned char _sobuf[BUFSIZ+8];
1057 #else
1058 char _sobuf[BUFSIZ];
1059 #endif
1061 #ifdef HAVE_LTCHARS
1062 static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
1063 #endif
1064 #ifdef HAVE_TCHARS
1065 static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
1066 #endif
1068 init_sys_modes ()
1070 struct emacs_tty tty;
1072 #ifdef VMS
1073 #if 0
1074 static int oob_chars[2] = {0, 1 << 7}; /* catch C-g's */
1075 extern int (*interrupt_signal) ();
1076 #endif
1077 #endif
1079 if (noninteractive)
1080 return;
1082 #ifdef VMS
1083 if (!input_ef)
1084 input_ef = get_kbd_event_flag ();
1085 /* LIB$GET_EF (&input_ef); */
1086 SYS$CLREF (input_ef);
1087 waiting_for_ast = 0;
1088 if (!timer_ef)
1089 timer_ef = get_timer_event_flag ();
1090 /* LIB$GET_EF (&timer_ef); */
1091 SYS$CLREF (timer_ef);
1092 #if 0
1093 if (!process_ef)
1095 LIB$GET_EF (&process_ef);
1096 SYS$CLREF (process_ef);
1098 if (input_ef / 32 != process_ef / 32)
1099 croak ("Input and process event flags in different clusters.");
1100 #endif
1101 if (input_ef / 32 != timer_ef / 32)
1102 croak ("Input and timer event flags in different clusters.");
1103 #if 0
1104 input_eflist = ((unsigned) 1 << (input_ef % 32)) |
1105 ((unsigned) 1 << (process_ef % 32));
1106 #endif
1107 timer_eflist = ((unsigned) 1 << (input_ef % 32)) |
1108 ((unsigned) 1 << (timer_ef % 32));
1109 #ifndef VMS4_4
1110 sys_access_reinit ();
1111 #endif
1112 #endif /* not VMS */
1114 #ifdef BSD
1115 if (! read_socket_hook && EQ (Vwindow_system, Qnil))
1116 narrow_foreground_group ();
1117 #endif
1119 EMACS_GET_TTY (input_fd, &old_tty);
1121 if (!read_socket_hook && EQ (Vwindow_system, Qnil))
1123 tty = old_tty;
1125 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
1126 tty.main.c_iflag |= (IGNBRK); /* Ignore break condition */
1127 tty.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
1128 #ifdef ISTRIP
1129 tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
1130 #endif
1131 tty.main.c_lflag &= ~ECHO; /* Disable echo */
1132 tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */
1133 #ifdef IEXTEN
1134 tty.main.c_iflag &= ~IEXTEN; /* Disable other editing characters. */
1135 #endif
1136 tty.main.c_lflag |= ISIG; /* Enable signals */
1137 if (flow_control)
1139 tty.main.c_iflag |= IXON; /* Enable start/stop output control */
1140 #ifdef IXANY
1141 tty.main.c_iflag &= ~IXANY;
1142 #endif /* IXANY */
1144 else
1145 tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */
1146 tty.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL
1147 on output */
1148 tty.main.c_oflag &= ~TAB3; /* Disable tab expansion */
1149 #ifdef CS8
1150 if (meta_key)
1152 tty.main.c_cflag |= CS8; /* allow 8th bit on input */
1153 tty.main.c_cflag &= ~PARENB;/* Don't check parity */
1155 #endif
1156 tty.main.c_cc[VINTR] = quit_char; /* C-g (usually) gives SIGINT */
1157 /* Set up C-g for both SIGQUIT and SIGINT.
1158 We don't know which we will get, but we handle both alike
1159 so which one it really gives us does not matter. */
1160 tty.main.c_cc[VQUIT] = quit_char;
1161 tty.main.c_cc[VMIN] = 1; /* Input should wait for at least 1 char */
1162 tty.main.c_cc[VTIME] = 0; /* no matter how long that takes. */
1163 #ifdef VSWTCH
1164 tty.main.c_cc[VSWTCH] = CDISABLE; /* Turn off shell layering use
1165 of C-z */
1166 #endif /* VSWTCH */
1167 #if defined (mips) || defined (HAVE_TCATTR)
1168 #ifdef VSUSP
1169 tty.main.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z. */
1170 #endif /* VSUSP */
1171 #ifdef V_DSUSP
1172 tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y. */
1173 #endif /* V_DSUSP */
1174 #ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1175 tty.main.c_cc[VDSUSP] = CDISABLE;
1176 #endif /* VDSUSP */
1177 #ifdef VLNEXT
1178 tty.main.c_cc[VLNEXT] = CDISABLE;
1179 #endif /* VLNEXT */
1180 #ifdef VREPRINT
1181 tty.main.c_cc[VREPRINT] = CDISABLE;
1182 #endif /* VREPRINT */
1183 #ifdef VWERASE
1184 tty.main.c_cc[VWERASE] = CDISABLE;
1185 #endif /* VWERASE */
1186 #ifdef VDISCARD
1187 tty.main.c_cc[VDISCARD] = CDISABLE;
1188 #endif /* VDISCARD */
1189 #endif /* mips or HAVE_TCATTR */
1190 #ifdef AIX
1191 #ifndef IBMR2AIX
1192 /* AIX enhanced edit loses NULs, so disable it */
1193 tty.main.c_line = 0;
1194 tty.main.c_iflag &= ~ASCEDIT;
1195 #else
1196 tty.main.c_cc[VSTRT] = 255;
1197 tty.main.c_cc[VSTOP] = 255;
1198 tty.main.c_cc[VSUSP] = 255;
1199 tty.main.c_cc[VDSUSP] = 255;
1200 #endif /* IBMR2AIX */
1201 /* Also, PTY overloads NUL and BREAK.
1202 don't ignore break, but don't signal either, so it looks like NUL.
1203 This really serves a purpose only if running in an XTERM window
1204 or via TELNET or the like, but does no harm elsewhere. */
1205 tty.main.c_iflag &= ~IGNBRK;
1206 tty.main.c_iflag &= ~BRKINT;
1207 #endif
1208 #else /* if not HAVE_TERMIO */
1209 #ifdef VMS
1210 tty.main.tt_char |= TT$M_NOECHO;
1211 if (meta_key)
1212 tty.main.tt_char |= TT$M_EIGHTBIT;
1213 if (flow_control)
1214 tty.main.tt_char |= TT$M_TTSYNC;
1215 else
1216 tty.main.tt_char &= ~TT$M_TTSYNC;
1217 tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
1218 #else /* not VMS (BSD, that is) */
1219 #ifndef MSDOS
1220 tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
1221 if (meta_key)
1222 tty.main.sg_flags |= ANYP;
1223 tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
1224 #endif
1225 #endif /* not VMS (BSD, that is) */
1226 #endif /* not HAVE_TERMIO */
1228 /* If going to use CBREAK mode, we must request C-g to interrupt
1229 and turn off start and stop chars, etc. If not going to use
1230 CBREAK mode, do this anyway so as to turn off local flow
1231 control for user coming over network on 4.2; in this case,
1232 only t_stopc and t_startc really matter. */
1233 #ifndef HAVE_TERMIO
1234 #ifdef HAVE_TCHARS
1235 /* Note: if not using CBREAK mode, it makes no difference how we
1236 set this */
1237 tty.tchars = new_tchars;
1238 tty.tchars.t_intrc = quit_char;
1239 if (flow_control)
1241 tty.tchars.t_startc = '\021';
1242 tty.tchars.t_stopc = '\023';
1245 tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | old_tty.lmode;
1246 #ifdef ultrix
1247 /* Under Ultrix 4.2a, leaving this out doesn't seem to hurt
1248 anything, and leaving it in breaks the meta key. Go figure. */
1249 tty.lmode &= ~LLITOUT;
1250 #endif
1252 #ifdef BSD4_1
1253 lmode = tty.lmode;
1254 #endif
1256 #endif /* HAVE_TCHARS */
1257 #endif /* not HAVE_TERMIO */
1259 #ifdef HAVE_LTCHARS
1260 tty.ltchars = new_ltchars;
1261 #endif /* HAVE_LTCHARS */
1262 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida, MW Aug 1993 */
1263 internal_terminal_init ();
1264 dos_ttraw ();
1265 #endif
1267 EMACS_SET_TTY (input_fd, &tty, 0);
1269 /* This code added to insure that, if flow-control is not to be used,
1270 we have an unlocked terminal at the start. */
1272 #ifdef TCXONC
1273 if (!flow_control) ioctl (input_fd, TCXONC, 1);
1274 #endif
1275 #ifndef APOLLO
1276 #ifdef TIOCSTART
1277 if (!flow_control) ioctl (input_fd, TIOCSTART, 0);
1278 #endif
1279 #endif
1281 #ifdef AIX
1282 hft_init ();
1283 #ifdef IBMR2AIX
1285 /* IBM's HFT device usually thinks a ^J should be LF/CR. We need it
1286 to be only LF. This is the way that is done. */
1287 struct termio tty;
1289 if (ioctl (1, HFTGETID, &tty) != -1)
1290 write (1, "\033[20l", 5);
1292 #endif
1293 #endif
1295 #ifdef VMS
1296 /* Appears to do nothing when in PASTHRU mode.
1297 SYS$QIOW (0, input_fd, IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
1298 interrupt_signal, oob_chars, 0, 0, 0, 0);
1300 queue_kbd_input (0);
1301 #endif /* VMS */
1304 #ifdef F_SETFL
1305 #ifndef F_SETOWN_BUG
1306 #ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
1307 if (interrupt_input)
1309 old_fcntl_owner = fcntl (input_fd, F_GETOWN, 0);
1310 fcntl (input_fd, F_SETOWN, getpid ());
1311 init_sigio ();
1313 #endif /* F_GETOWN */
1314 #endif /* F_SETOWN_BUG */
1315 #endif /* F_SETFL */
1317 #ifdef BSD4_1
1318 if (interrupt_input)
1319 init_sigio ();
1320 #endif
1322 #ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1323 #undef _IOFBF
1324 #endif
1325 #ifdef _IOFBF
1326 /* This symbol is defined on recent USG systems.
1327 Someone says without this call USG won't really buffer the file
1328 even with a call to setbuf. */
1329 setvbuf (stdout, _sobuf, _IOFBF, sizeof _sobuf);
1330 #else
1331 setbuf (stdout, _sobuf);
1332 #endif
1333 set_terminal_modes ();
1334 if (term_initted && no_redraw_on_reenter)
1336 if (display_completed)
1337 direct_output_forward_char (0);
1339 else
1341 frame_garbaged = 1;
1342 #ifdef MULTI_FRAME
1343 if (FRAMEP (Vterminal_frame))
1344 FRAME_GARBAGED_P (XFRAME (Vterminal_frame)) = 1;
1345 #endif
1348 term_initted = 1;
1351 /* Return nonzero if safe to use tabs in output.
1352 At the time this is called, init_sys_modes has not been done yet. */
1354 tabs_safe_p ()
1356 struct emacs_tty tty;
1358 EMACS_GET_TTY (input_fd, &tty);
1359 return EMACS_TTY_TABS_OK (&tty);
1362 /* Get terminal size from system.
1363 Store number of lines into *heightp and width into *widthp.
1364 If zero or a negative number is stored, the value is not valid. */
1366 get_frame_size (widthp, heightp)
1367 int *widthp, *heightp;
1370 #ifdef TIOCGWINSZ
1372 /* BSD-style. */
1373 struct winsize size;
1375 if (ioctl (input_fd, TIOCGWINSZ, &size) == -1)
1376 *widthp = *heightp = 0;
1377 else
1379 *widthp = size.ws_col;
1380 *heightp = size.ws_row;
1383 #else
1384 #ifdef TIOCGSIZE
1386 /* SunOS - style. */
1387 struct ttysize size;
1389 if (ioctl (input_fd, TIOCGSIZE, &size) == -1)
1390 *widthp = *heightp = 0;
1391 else
1393 *widthp = size.ts_cols;
1394 *heightp = size.ts_lines;
1397 #else
1398 #ifdef VMS
1400 struct sensemode tty;
1402 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &tty, 0, 0,
1403 &tty.class, 12, 0, 0, 0, 0);
1404 *widthp = tty.scr_wid;
1405 *heightp = tty.scr_len;
1407 #else
1408 #ifdef MSDOS
1409 *widthp = ScreenCols ();
1410 *heightp = ScreenRows ();
1411 #else /* system doesn't know size */
1412 *widthp = 0;
1413 *heightp = 0;
1414 #endif
1416 #endif /* not VMS */
1417 #endif /* not SunOS-style */
1418 #endif /* not BSD-style */
1422 /* Prepare the terminal for exiting Emacs; move the cursor to the
1423 bottom of the frame, turn off interrupt-driven I/O, etc. */
1424 reset_sys_modes ()
1426 if (noninteractive)
1428 fflush (stdout);
1429 return;
1431 if (!term_initted)
1432 return;
1433 if (read_socket_hook || !EQ (Vwindow_system, Qnil))
1434 return;
1435 cursor_to (FRAME_HEIGHT (selected_frame) - 1, 0);
1436 clear_end_of_line (FRAME_WIDTH (selected_frame));
1437 /* clear_end_of_line may move the cursor */
1438 cursor_to (FRAME_HEIGHT (selected_frame) - 1, 0);
1439 #ifdef IBMR2AIX
1441 /* HFT devices normally use ^J as a LF/CR. We forced it to
1442 do the LF only. Now, we need to reset it. */
1443 struct termio tty;
1445 if (ioctl (1, HFTGETID, &tty) != -1)
1446 write (1, "\033[20h", 5);
1448 #endif
1450 reset_terminal_modes ();
1451 fflush (stdout);
1452 #ifdef BSD
1453 #ifndef BSD4_1
1454 /* Avoid possible loss of output when changing terminal modes. */
1455 fsync (fileno (stdout));
1456 #endif
1457 #endif
1459 #ifdef F_SETFL
1460 #ifndef F_SETOWN_BUG
1461 #ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
1462 if (interrupt_input)
1464 reset_sigio ();
1465 fcntl (input_fd, F_SETOWN, old_fcntl_owner);
1467 #endif /* F_SETOWN */
1468 #endif /* F_SETOWN_BUG */
1469 #endif /* F_SETFL */
1470 #ifdef BSD4_1
1471 if (interrupt_input)
1472 reset_sigio ();
1473 #endif /* BSD4_1 */
1475 while (EMACS_SET_TTY (input_fd, &old_tty, 0) < 0 && errno == EINTR)
1478 #ifdef MSDOS /* Demacs 1.1.2 91/10/20 Manabu Higashida */
1479 dos_ttcooked ();
1480 #endif
1482 #ifdef AIX
1483 hft_reset ();
1484 #endif
1486 #ifdef BSD
1487 widen_foreground_group ();
1488 #endif
1491 #ifdef HAVE_PTYS
1493 /* Set up the proper status flags for use of a pty. */
1495 setup_pty (fd)
1496 int fd;
1498 /* I'm told that TOICREMOTE does not mean control chars
1499 "can't be sent" but rather that they don't have
1500 input-editing or signaling effects.
1501 That should be good, because we have other ways
1502 to do those things in Emacs.
1503 However, telnet mode seems not to work on 4.2.
1504 So TIOCREMOTE is turned off now. */
1506 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1507 will hang. In particular, the "timeout" feature (which
1508 causes a read to return if there is no data available)
1509 does this. Also it is known that telnet mode will hang
1510 in such a way that Emacs must be stopped (perhaps this
1511 is the same problem).
1513 If TIOCREMOTE is turned off, then there is a bug in
1514 hp-ux which sometimes loses data. Apparently the
1515 code which blocks the master process when the internal
1516 buffer fills up does not work. Other than this,
1517 though, everything else seems to work fine.
1519 Since the latter lossage is more benign, we may as well
1520 lose that way. -- cph */
1521 #ifdef FIONBIO
1522 #ifdef SYSV_PTYS
1524 int on = 1;
1525 ioctl (fd, FIONBIO, &on);
1527 #endif
1528 #endif
1529 #ifdef IBMRTAIX
1530 /* On AIX, the parent gets SIGHUP when a pty attached child dies. So, we */
1531 /* ignore SIGHUP once we've started a child on a pty. Note that this may */
1532 /* cause EMACS not to die when it should, i.e., when its own controlling */
1533 /* tty goes away. I've complained to the AIX developers, and they may */
1534 /* change this behavior, but I'm not going to hold my breath. */
1535 signal (SIGHUP, SIG_IGN);
1536 #endif
1538 #endif /* HAVE_PTYS */
1540 #ifdef VMS
1542 /* Assigning an input channel is done at the start of Emacs execution.
1543 This is called each time Emacs is resumed, also, but does nothing
1544 because input_chain is no longer zero. */
1546 init_vms_input ()
1548 int status;
1550 if (input_fd == 0)
1552 status = SYS$ASSIGN (&input_dsc, &input_fd, 0, 0);
1553 if (! (status & 1))
1554 LIB$STOP (status);
1558 /* Deassigning the input channel is done before exiting. */
1560 stop_vms_input ()
1562 return SYS$DASSGN (input_fd);
1565 short input_buffer;
1567 /* Request reading one character into the keyboard buffer.
1568 This is done as soon as the buffer becomes empty. */
1570 queue_kbd_input ()
1572 int status;
1573 extern kbd_input_ast ();
1575 waiting_for_ast = 0;
1576 stop_input = 0;
1577 status = SYS$QIO (0, input_fd, IO$_READVBLK,
1578 &input_iosb, kbd_input_ast, 1,
1579 &input_buffer, 1, 0, terminator_mask, 0, 0);
1582 int input_count;
1584 /* Ast routine that is called when keyboard input comes in
1585 in accord with the SYS$QIO above. */
1587 kbd_input_ast ()
1589 register int c = -1;
1590 int old_errno = errno;
1591 extern EMACS_TIME *input_available_clear_time;
1593 if (waiting_for_ast)
1594 SYS$SETEF (input_ef);
1595 waiting_for_ast = 0;
1596 input_count++;
1597 #ifdef ASTDEBUG
1598 if (input_count == 25)
1599 exit (1);
1600 printf ("Ast # %d,", input_count);
1601 printf (" iosb = %x, %x, %x, %x",
1602 input_iosb.offset, input_iosb.status, input_iosb.termlen,
1603 input_iosb.term);
1604 #endif
1605 if (input_iosb.offset)
1607 c = input_buffer;
1608 #ifdef ASTDEBUG
1609 printf (", char = 0%o", c);
1610 #endif
1612 #ifdef ASTDEBUG
1613 printf ("\n");
1614 fflush (stdout);
1615 sleep (1);
1616 #endif
1617 if (! stop_input)
1618 queue_kbd_input ();
1619 if (c >= 0)
1621 struct input_event e;
1622 e.kind = ascii_keystroke;
1623 XSET (e.code, Lisp_Int, c);
1624 #ifdef MULTI_FRAME
1625 XSET(e.frame_or_window, Lisp_Frame, selected_frame);
1626 #else
1627 e.frame_or_window = Qnil;
1628 #endif
1629 kbd_buffer_store_event (&e);
1631 if (input_available_clear_time)
1632 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
1633 errno = old_errno;
1636 /* Wait until there is something in kbd_buffer. */
1638 wait_for_kbd_input ()
1640 extern int have_process_input, process_exited;
1642 /* If already something, avoid doing system calls. */
1643 if (detect_input_pending ())
1645 return;
1647 /* Clear a flag, and tell ast routine above to set it. */
1648 SYS$CLREF (input_ef);
1649 waiting_for_ast = 1;
1650 /* Check for timing error: ast happened while we were doing that. */
1651 if (!detect_input_pending ())
1653 /* No timing error: wait for flag to be set. */
1654 set_waiting_for_input (0);
1655 SYS$WFLOR (input_ef, input_eflist);
1656 clear_waiting_for_input (0);
1657 if (!detect_input_pending ())
1658 /* Check for subprocess input availability */
1660 int dsp = have_process_input || process_exited;
1662 SYS$CLREF (process_ef);
1663 if (have_process_input)
1664 process_command_input ();
1665 if (process_exited)
1666 process_exit ();
1667 if (dsp)
1669 update_mode_lines++;
1670 prepare_menu_bars ();
1671 redisplay_preserve_echo_area ();
1675 waiting_for_ast = 0;
1678 /* Get rid of any pending QIO, when we are about to suspend
1679 or when we want to throw away pending input.
1680 We wait for a positive sign that the AST routine has run
1681 and therefore there is no I/O request queued when we return.
1682 SYS$SETAST is used to avoid a timing error. */
1684 end_kbd_input ()
1686 #ifdef ASTDEBUG
1687 printf ("At end_kbd_input.\n");
1688 fflush (stdout);
1689 sleep (1);
1690 #endif
1691 if (LIB$AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
1693 SYS$CANCEL (input_fd);
1694 return;
1697 SYS$SETAST (0);
1698 /* Clear a flag, and tell ast routine above to set it. */
1699 SYS$CLREF (input_ef);
1700 waiting_for_ast = 1;
1701 stop_input = 1;
1702 SYS$CANCEL (input_fd);
1703 SYS$SETAST (1);
1704 SYS$WAITFR (input_ef);
1705 waiting_for_ast = 0;
1708 /* Wait for either input available or time interval expiry. */
1710 input_wait_timeout (timeval)
1711 int timeval; /* Time to wait, in seconds */
1713 int time [2];
1714 static int zero = 0;
1715 static int large = -10000000;
1717 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
1719 /* If already something, avoid doing system calls. */
1720 if (detect_input_pending ())
1722 return;
1724 /* Clear a flag, and tell ast routine above to set it. */
1725 SYS$CLREF (input_ef);
1726 waiting_for_ast = 1;
1727 /* Check for timing error: ast happened while we were doing that. */
1728 if (!detect_input_pending ())
1730 /* No timing error: wait for flag to be set. */
1731 SYS$CANTIM (1, 0);
1732 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
1733 SYS$WFLOR (timer_ef, timer_eflist); /* Wait for timer expiry or input */
1735 waiting_for_ast = 0;
1738 /* The standard `sleep' routine works some other way
1739 and it stops working if you have ever quit out of it.
1740 This one continues to work. */
1742 sys_sleep (timeval)
1743 int timeval;
1745 int time [2];
1746 static int zero = 0;
1747 static int large = -10000000;
1749 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
1751 SYS$CANTIM (1, 0);
1752 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
1753 SYS$WAITFR (timer_ef); /* Wait for timer expiry only */
1756 init_sigio ()
1758 request_sigio ();
1761 reset_sigio ()
1763 unrequest_sigio ();
1766 request_sigio ()
1768 croak ("request sigio");
1771 unrequest_sigio ()
1773 croak ("unrequest sigio");
1776 #endif /* VMS */
1778 /* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
1779 #ifndef CANNOT_DUMP
1780 #define NEED_STARTS
1781 #endif
1783 #ifndef SYSTEM_MALLOC
1784 #ifndef NEED_STARTS
1785 #define NEED_STARTS
1786 #endif
1787 #endif
1789 #ifdef NEED_STARTS
1790 /* Some systems that cannot dump also cannot implement these. */
1793 * Return the address of the start of the text segment prior to
1794 * doing an unexec. After unexec the return value is undefined.
1795 * See crt0.c for further explanation and _start.
1799 #ifndef CANNOT_UNEXEC
1800 char *
1801 start_of_text ()
1803 #ifdef TEXT_START
1804 return ((char *) TEXT_START);
1805 #else
1806 #ifdef GOULD
1807 extern csrt ();
1808 return ((char *) csrt);
1809 #else /* not GOULD */
1810 extern int _start ();
1811 return ((char *) _start);
1812 #endif /* GOULD */
1813 #endif /* TEXT_START */
1815 #endif /* not CANNOT_UNEXEC */
1818 * Return the address of the start of the data segment prior to
1819 * doing an unexec. After unexec the return value is undefined.
1820 * See crt0.c for further information and definition of data_start.
1822 * Apparently, on BSD systems this is etext at startup. On
1823 * USG systems (swapping) this is highly mmu dependent and
1824 * is also dependent on whether or not the program is running
1825 * with shared text. Generally there is a (possibly large)
1826 * gap between end of text and start of data with shared text.
1828 * On Uniplus+ systems with shared text, data starts at a
1829 * fixed address. Each port (from a given oem) is generally
1830 * different, and the specific value of the start of data can
1831 * be obtained via the UniPlus+ specific "uvar" system call,
1832 * however the method outlined in crt0.c seems to be more portable.
1834 * Probably what will have to happen when a USG unexec is available,
1835 * at least on UniPlus, is temacs will have to be made unshared so
1836 * that text and data are contiguous. Then once loadup is complete,
1837 * unexec will produce a shared executable where the data can be
1838 * at the normal shared text boundry and the startofdata variable
1839 * will be patched by unexec to the correct value.
1843 char *
1844 start_of_data ()
1846 #ifdef DATA_START
1847 return ((char *) DATA_START);
1848 #else
1849 #ifdef ORDINARY_LINK
1851 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
1852 * data_start isn't defined. We take the address of environ, which
1853 * is known to live at or near the start of the system crt0.c, and
1854 * we don't sweat the handful of bytes that might lose.
1856 extern char **environ;
1858 return((char *) &environ);
1859 #else
1860 extern int data_start;
1861 return ((char *) &data_start);
1862 #endif /* ORDINARY_LINK */
1863 #endif /* DATA_START */
1865 #endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
1867 #ifndef CANNOT_DUMP
1868 /* Some systems that cannot dump also cannot implement these. */
1871 * Return the address of the end of the text segment prior to
1872 * doing an unexec. After unexec the return value is undefined.
1875 char *
1876 end_of_text ()
1878 #ifdef TEXT_END
1879 return ((char *) TEXT_END);
1880 #else
1881 extern int etext;
1882 return ((char *) &etext);
1883 #endif
1887 * Return the address of the end of the data segment prior to
1888 * doing an unexec. After unexec the return value is undefined.
1891 char *
1892 end_of_data ()
1894 #ifdef DATA_END
1895 return ((char *) DATA_END);
1896 #else
1897 extern int edata;
1898 return ((char *) &edata);
1899 #endif
1902 #endif /* not CANNOT_DUMP */
1904 /* Get_system_name returns as its value
1905 a string for the Lisp function system-name to return. */
1907 #ifdef BSD4_1
1908 #include <whoami.h>
1909 #endif
1911 /* Can't have this within the function since `static' is #defined to
1912 nothing for some USG systems. */
1913 #ifdef USG
1914 #ifdef HAVE_GETHOSTNAME
1915 static char get_system_name_name[256];
1916 #else /* not HAVE_GETHOSTNAME */
1917 static struct utsname get_system_name_name;
1918 #endif /* not HAVE_GETHOSTNAME */
1919 #endif /* USG */
1921 #ifndef BSD4_1
1922 #ifndef USG
1923 #ifndef VMS
1924 #ifdef HAVE_SOCKETS
1925 #include <sys/socket.h>
1926 #include <netdb.h>
1927 #endif /* HAVE_SOCKETS */
1928 #endif /* not VMS */
1929 #endif /* not USG */
1930 #endif /* not BSD4_1 */
1932 char *
1933 get_system_name ()
1935 #ifdef USG
1936 #ifdef HAVE_GETHOSTNAME
1937 gethostname (get_system_name_name, sizeof (get_system_name_name));
1938 return get_system_name_name;
1939 #else /* not HAVE_GETHOSTNAME */
1940 uname (&get_system_name_name);
1941 return (get_system_name_name.nodename);
1942 #endif /* not HAVE_GETHOSTNAME */
1943 #else /* Not USG */
1944 #ifdef BSD4_1
1945 return sysname;
1946 #else /* not USG, not 4.1 */
1947 static char system_name_saved[32];
1948 #ifdef VMS
1949 char *sp;
1950 if ((sp = egetenv ("SYS$NODE")) == 0)
1951 sp = "vax-vms";
1952 else
1954 char *end;
1956 if ((end = index (sp, ':')) != 0)
1957 *end = '\0';
1959 strcpy (system_name_saved, sp);
1960 #else /* not VMS */
1961 gethostname (system_name_saved, sizeof (system_name_saved));
1962 #ifdef HAVE_SOCKETS
1963 /* Turn the hostname into the official, fully-qualified hostname.
1964 Don't do this if we're going to dump; this can confuse system
1965 libraries on some machines and make the dumped emacs core dump. */
1966 #ifndef CANNOT_DUMP
1967 if (initialized)
1968 #endif /* not CANNOT_DUMP */
1970 struct hostent *hp;
1971 hp = gethostbyname (system_name_saved);
1972 if (hp && strlen (hp->h_name) < sizeof(system_name_saved))
1973 strcpy (system_name_saved, hp->h_name);
1974 if (hp && !index (system_name_saved, '.'))
1976 /* We still don't have a fully qualified domain name.
1977 Try to find one in the list of alternate names */
1978 char **alias = hp->h_aliases;
1979 while (*alias && !index (*alias, '.'))
1980 alias++;
1981 if (*alias && strlen (*alias) < sizeof (system_name_saved))
1982 strcpy (system_name_saved, *alias);
1985 #endif /* HAVE_SOCKETS */
1986 #endif /* not VMS */
1987 return system_name_saved;
1988 #endif /* not USG, not 4.1 */
1989 #endif /* not USG */
1992 #ifdef VMS
1993 #ifndef HAVE_GETHOSTNAME
1994 void gethostname(buf, len)
1995 char *buf;
1996 int len;
1998 char *s;
1999 s = getenv ("SYS$NODE");
2000 if (s == NULL)
2001 buf[0] = '\0';
2002 else {
2003 strncpy (buf, s, len - 2);
2004 buf[len - 1] = '\0';
2005 } /* else */
2006 } /* static void gethostname */
2007 #endif /* ! HAVE_GETHOSTNAME */
2008 #endif /* VMS */
2011 #ifndef VMS
2012 #ifndef HAVE_SELECT
2014 #ifdef HAVE_X_WINDOWS
2015 /* Cause explanatory error message at compile time,
2016 since the select emulation is not good enough for X. */
2017 int *x = &x_windows_lose_if_no_select_system_call;
2018 #endif
2020 /* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
2021 * Only checks read descriptors.
2023 /* How long to wait between checking fds in select */
2024 #define SELECT_PAUSE 1
2025 int select_alarmed;
2027 /* For longjmp'ing back to read_input_waiting. */
2029 jmp_buf read_alarm_throw;
2031 /* Nonzero if the alarm signal should throw back to read_input_waiting.
2032 The read_socket_hook function sets this to 1 while it is waiting. */
2034 int read_alarm_should_throw;
2036 SIGTYPE
2037 select_alarm ()
2039 select_alarmed = 1;
2040 #ifdef BSD4_1
2041 sigrelse (SIGALRM);
2042 #else /* not BSD4_1 */
2043 signal (SIGALRM, SIG_IGN);
2044 #endif /* not BSD4_1 */
2045 if (read_alarm_should_throw)
2046 longjmp (read_alarm_throw, 1);
2049 /* Only rfds are checked. */
2051 select (nfds, rfds, wfds, efds, timeout)
2052 int nfds;
2053 int *rfds, *wfds, *efds, *timeout;
2055 int ravail = 0, orfds = 0, old_alarm;
2056 int timeoutval = timeout ? *timeout : 100000;
2057 int *local_timeout = &timeoutval;
2058 extern int proc_buffered_char[];
2059 #ifndef subprocesses
2060 int process_tick = 0, update_tick = 0;
2061 #else
2062 extern int process_tick, update_tick;
2063 #endif
2064 SIGTYPE (*old_trap) ();
2065 unsigned char buf;
2067 if (rfds)
2069 orfds = *rfds;
2070 *rfds = 0;
2072 if (wfds)
2073 *wfds = 0;
2074 if (efds)
2075 *efds = 0;
2077 /* If we are looking only for the terminal, with no timeout,
2078 just read it and wait -- that's more efficient. */
2079 if (orfds == 1 && *local_timeout == 100000 && process_tick == update_tick)
2081 if (! detect_input_pending ())
2082 read_input_waiting ();
2083 *rfds = 1;
2084 return 1;
2087 /* Once a second, till the timer expires, check all the flagged read
2088 * descriptors to see if any input is available. If there is some then
2089 * set the corresponding bit in the return copy of rfds.
2091 while (1)
2093 register int to_check, bit, fd;
2095 if (rfds)
2097 for (to_check = nfds, bit = 1, fd = 0; --to_check >= 0; bit <<= 1, fd++)
2099 if (orfds & bit)
2101 int avail = 0, status = 0;
2103 if (bit == 1)
2104 avail = detect_input_pending (); /* Special keyboard handler */
2105 else
2107 #ifdef FIONREAD
2108 status = ioctl (fd, FIONREAD, &avail);
2109 #else /* no FIONREAD */
2110 #ifdef MSDOS
2111 abort (); /* I don't think we need it. */
2112 #else /* not MSDOS */
2113 /* Hoping it will return -1 if nothing available
2114 or 0 if all 0 chars requested are read. */
2115 if (proc_buffered_char[fd] >= 0)
2116 avail = 1;
2117 else
2119 avail = read (fd, &buf, 1);
2120 if (avail > 0)
2121 proc_buffered_char[fd] = buf;
2123 #endif /* not MSDOS */
2124 #endif /* no FIONREAD */
2126 if (status >= 0 && avail > 0)
2128 (*rfds) |= bit;
2129 ravail++;
2134 if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
2135 break;
2136 old_alarm = alarm (0);
2137 old_trap = signal (SIGALRM, select_alarm);
2138 select_alarmed = 0;
2139 alarm (SELECT_PAUSE);
2140 /* Wait for a SIGALRM (or maybe a SIGTINT) */
2141 while (select_alarmed == 0 && *local_timeout != 0
2142 && process_tick == update_tick)
2144 #ifdef MSDOS
2145 sleep_or_kbd_hit (SELECT_PAUSE, (orfds & 1) != 0);
2146 select_alarm ();
2147 #else /* not MSDOS */
2148 /* If we are interested in terminal input,
2149 wait by reading the terminal.
2150 That makes instant wakeup for terminal input at least. */
2151 if (orfds & 1)
2153 read_input_waiting ();
2154 if (detect_input_pending ())
2155 select_alarmed = 1;
2157 else
2158 pause ();
2159 #endif /* not MSDOS */
2161 (*local_timeout) -= SELECT_PAUSE;
2162 /* Reset the old alarm if there was one */
2163 alarm (0);
2164 signal (SIGALRM, old_trap);
2165 if (old_alarm != 0)
2167 /* Reset or forge an interrupt for the original handler. */
2168 old_alarm -= SELECT_PAUSE;
2169 if (old_alarm <= 0)
2170 kill (getpid (), SIGALRM); /* Fake an alarm with the orig' handler */
2171 else
2172 alarm (old_alarm);
2174 if (*local_timeout == 0) /* Stop on timer being cleared */
2175 break;
2177 return ravail;
2180 /* Read keyboard input into the standard buffer,
2181 waiting for at least one character. */
2183 /* Make all keyboard buffers much bigger when using X windows. */
2184 #ifdef HAVE_X_WINDOWS
2185 #define BUFFER_SIZE_FACTOR 16
2186 #else
2187 #define BUFFER_SIZE_FACTOR 1
2188 #endif
2190 read_input_waiting ()
2192 struct input_event e;
2193 int nread, i;
2194 extern int quit_char;
2196 if (read_socket_hook)
2198 struct input_event buf[256];
2200 read_alarm_should_throw = 0;
2201 if (! setjmp (read_alarm_throw))
2202 nread = (*read_socket_hook) (0, buf, 256, 1, 0);
2203 else
2204 nread = -1;
2206 /* Scan the chars for C-g and store them in kbd_buffer. */
2207 for (i = 0; i < nread; i++)
2209 kbd_buffer_store_event (&buf[i]);
2210 /* Don't look at input that follows a C-g too closely.
2211 This reduces lossage due to autorepeat on C-g. */
2212 if (buf[i].kind == ascii_keystroke
2213 && XINT(buf[i].code) == quit_char)
2214 break;
2217 else
2219 char buf[3];
2220 nread = read (fileno (stdin), buf, 1);
2222 /* Scan the chars for C-g and store them in kbd_buffer. */
2223 e.kind = ascii_keystroke;
2224 e.frame_or_window = selected_frame;
2225 e.modifiers = 0;
2226 for (i = 0; i < nread; i++)
2228 /* Convert chars > 0177 to meta events if desired.
2229 We do this under the same conditions that read_avail_input does. */
2230 if (read_socket_hook == 0)
2232 /* If the user says she has a meta key, then believe her. */
2233 if (meta_key == 1 && (buf[i] & 0x80))
2234 e.modifiers = meta_modifier;
2235 if (meta_key != 2)
2236 buf[i] &= ~0x80;
2239 XSET (e.code, Lisp_Int, buf[i]);
2240 kbd_buffer_store_event (&e);
2241 /* Don't look at input that follows a C-g too closely.
2242 This reduces lossage due to autorepeat on C-g. */
2243 if (buf[i] == quit_char)
2244 break;
2249 #endif /* not HAVE_SELECT */
2250 #endif /* not VMS */
2252 #ifdef BSD4_1
2254 * Partially emulate 4.2 open call.
2255 * open is defined as this in 4.1.
2257 * - added by Michael Bloom @ Citicorp/TTI
2262 sys_open (path, oflag, mode)
2263 char *path;
2264 int oflag, mode;
2266 if (oflag & O_CREAT)
2267 return creat (path, mode);
2268 else
2269 return open (path, oflag);
2272 init_sigio ()
2274 if (noninteractive)
2275 return;
2276 lmode = LINTRUP | lmode;
2277 ioctl (0, TIOCLSET, &lmode);
2280 reset_sigio ()
2282 if (noninteractive)
2283 return;
2284 lmode = ~LINTRUP & lmode;
2285 ioctl (0, TIOCLSET, &lmode);
2288 request_sigio ()
2290 sigrelse (SIGTINT);
2292 interrupts_deferred = 0;
2295 unrequest_sigio ()
2297 sighold (SIGTINT);
2299 interrupts_deferred = 1;
2302 /* still inside #ifdef BSD4_1 */
2303 #ifdef subprocesses
2305 int sigheld; /* Mask of held signals */
2307 sigholdx (signum)
2308 int signum;
2310 sigheld |= sigbit (signum);
2311 sighold (signum);
2314 sigisheld (signum)
2315 int signum;
2317 sigheld |= sigbit (signum);
2320 sigunhold (signum)
2321 int signum;
2323 sigheld &= ~sigbit (signum);
2324 sigrelse (signum);
2327 sigfree () /* Free all held signals */
2329 int i;
2330 for (i = 0; i < NSIG; i++)
2331 if (sigheld & sigbit (i))
2332 sigrelse (i);
2333 sigheld = 0;
2336 sigbit (i)
2338 return 1 << (i - 1);
2340 #endif /* subprocesses */
2341 #endif /* BSD4_1 */
2343 /* POSIX signals support - DJB */
2344 /* Anyone with POSIX signals should have ANSI C declarations */
2346 #ifdef POSIX_SIGNALS
2348 sigset_t old_mask, empty_mask, full_mask, temp_mask;
2349 static struct sigaction new_action, old_action;
2351 init_signals ()
2353 sigemptyset (&empty_mask);
2354 sigfillset (&full_mask);
2357 signal_handler_t
2358 sys_signal (int signal_number, signal_handler_t action)
2360 #ifdef DGUX
2361 /* This gets us restartable system calls for efficiency.
2362 The "else" code will works as well. */
2363 return (berk_signal (signal_number, action));
2364 #else
2365 sigemptyset (&new_action.sa_mask);
2366 new_action.sa_handler = action;
2367 #ifdef SA_RESTART
2368 /* Emacs mostly works better with restartable system services. If this
2369 * flag exists, we probably want to turn it on here.
2371 new_action.sa_flags = SA_RESTART;
2372 #else
2373 new_action.sa_flags = 0;
2374 #endif
2375 sigaction (signal_number, &new_action, &old_action);
2376 return (old_action.sa_handler);
2377 #endif /* DGUX */
2380 #ifndef __GNUC__
2381 /* If we're compiling with GCC, we don't need this function, since it
2382 can be written as a macro. */
2383 sigset_t
2384 sys_sigmask (int sig)
2386 sigset_t mask;
2387 sigemptyset (&mask);
2388 sigaddset (&mask, sig);
2389 return mask;
2391 #endif
2394 sys_sigpause (sigset_t new_mask)
2396 /* pause emulating berk sigpause... */
2397 sigsuspend (&new_mask);
2398 return (EINTR);
2401 /* I'd like to have these guys return pointers to the mask storage in here,
2402 but there'd be trouble if the code was saving multiple masks. I'll be
2403 safe and pass the structure. It normally won't be more than 2 bytes
2404 anyhow. - DJB */
2406 sigset_t
2407 sys_sigblock (sigset_t new_mask)
2409 sigset_t old_mask;
2410 sigprocmask (SIG_BLOCK, &new_mask, &old_mask);
2411 return (old_mask);
2414 sigset_t
2415 sys_sigunblock (sigset_t new_mask)
2417 sigset_t old_mask;
2418 sigprocmask (SIG_UNBLOCK, &new_mask, &old_mask);
2419 return (old_mask);
2422 sigset_t
2423 sys_sigsetmask (sigset_t new_mask)
2425 sigset_t old_mask;
2426 sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
2427 return (old_mask);
2430 #endif /* POSIX_SIGNALS */
2432 #ifndef BSTRING
2434 #ifndef bzero
2436 void
2437 bzero (b, length)
2438 register char *b;
2439 register int length;
2441 #ifdef VMS
2442 short zero = 0;
2443 long max_str = 65535;
2445 while (length > max_str) {
2446 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
2447 length -= max_str;
2448 b += max_str;
2450 max_str = length;
2451 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
2452 #else
2453 while (length-- > 0)
2454 *b++ = 0;
2455 #endif /* not VMS */
2458 #endif /* no bzero */
2460 #ifndef bcopy
2461 /* Saying `void' requires a declaration, above, where bcopy is used
2462 and that declaration causes pain for systems where bcopy is a macro. */
2463 bcopy (b1, b2, length)
2464 register char *b1;
2465 register char *b2;
2466 register int length;
2468 #ifdef VMS
2469 long max_str = 65535;
2471 while (length > max_str) {
2472 (void) LIB$MOVC3 (&max_str, b1, b2);
2473 length -= max_str;
2474 b1 += max_str;
2475 b2 += max_str;
2477 max_str = length;
2478 (void) LIB$MOVC3 (&length, b1, b2);
2479 #else
2480 while (length-- > 0)
2481 *b2++ = *b1++;
2482 #endif /* not VMS */
2484 #endif /* no bcopy */
2486 #ifndef bcmp
2488 bcmp (b1, b2, length) /* This could be a macro! */
2489 register char *b1;
2490 register char *b2;
2491 register int length;
2493 #ifdef VMS
2494 struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
2495 struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
2497 return STR$COMPARE (&src1, &src2);
2498 #else
2499 while (length-- > 0)
2500 if (*b1++ != *b2++)
2501 return 1;
2503 return 0;
2504 #endif /* not VMS */
2506 #endif /* no bcmp */
2508 #endif /* not BSTRING */
2510 #ifndef HAVE_RANDOM
2511 #ifdef USG
2513 * The BSD random returns numbers in the range of
2514 * 0 to 2e31 - 1. The USG rand returns numbers in the
2515 * range of 0 to 2e15 - 1. This is probably not significant
2516 * in this usage.
2519 long
2520 random ()
2522 /* Arrange to return a range centered on zero. */
2523 return (rand () << 15) + rand () - (1 << 29);
2526 srandom (arg)
2527 int arg;
2529 srand (arg);
2532 #endif /* USG */
2534 #ifdef BSD4_1
2535 long random ()
2537 /* Arrange to return a range centered on zero. */
2538 return (rand () << 15) + rand () - (1 << 29);
2541 srandom (arg)
2542 int arg;
2544 srand (arg);
2546 #endif /* BSD4_1 */
2547 #endif
2549 #ifdef WRONG_NAME_INSQUE
2551 insque (q,p)
2552 caddr_t q,p;
2554 _insque (q,p);
2557 #endif
2559 #ifdef VMS
2561 #ifdef getenv
2562 /* If any place else asks for the TERM variable,
2563 allow it to be overridden with the EMACS_TERM variable
2564 before attempting to translate the logical name TERM. As a last
2565 resort, ask for VAX C's special idea of the TERM variable. */
2566 #undef getenv
2567 char *
2568 sys_getenv (name)
2569 char *name;
2571 register char *val;
2572 static char buf[256];
2573 static struct dsc$descriptor_s equiv
2574 = {sizeof (buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf};
2575 static struct dsc$descriptor_s d_name
2576 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2577 short eqlen;
2579 if (!strcmp (name, "TERM"))
2581 val = (char *) getenv ("EMACS_TERM");
2582 if (val)
2583 return val;
2586 d_name.dsc$w_length = strlen (name);
2587 d_name.dsc$a_pointer = name;
2588 if (LIB$SYS_TRNLOG (&d_name, &eqlen, &equiv) == 1)
2590 char *str = (char *) xmalloc (eqlen + 1);
2591 bcopy (buf, str, eqlen);
2592 str[eqlen] = '\0';
2593 /* This is a storage leak, but a pain to fix. With luck,
2594 no one will ever notice. */
2595 return str;
2597 return (char *) getenv (name);
2599 #endif /* getenv */
2601 #ifdef abort
2602 /* Since VMS doesn't believe in core dumps, the only way to debug this beast is
2603 to force a call on the debugger from within the image. */
2604 #undef abort
2605 sys_abort ()
2607 reset_sys_modes ();
2608 LIB$SIGNAL (SS$_DEBUG);
2610 #endif /* abort */
2611 #endif /* VMS */
2613 #ifdef VMS
2614 #ifdef LINK_CRTL_SHARE
2615 #ifdef SHAREABLE_LIB_BUG
2616 /* Variables declared noshare and initialized in sharable libraries
2617 cannot be shared. The VMS linker incorrectly forces you to use a private
2618 version which is uninitialized... If not for this "feature", we
2619 could use the C library definition of sys_nerr and sys_errlist. */
2620 int sys_nerr = 35;
2621 char *sys_errlist[] =
2623 "error 0",
2624 "not owner",
2625 "no such file or directory",
2626 "no such process",
2627 "interrupted system call",
2628 "i/o error",
2629 "no such device or address",
2630 "argument list too long",
2631 "exec format error",
2632 "bad file number",
2633 "no child process",
2634 "no more processes",
2635 "not enough memory",
2636 "permission denied",
2637 "bad address",
2638 "block device required",
2639 "mount devices busy",
2640 "file exists",
2641 "cross-device link",
2642 "no such device",
2643 "not a directory",
2644 "is a directory",
2645 "invalid argument",
2646 "file table overflow",
2647 "too many open files",
2648 "not a typewriter",
2649 "text file busy",
2650 "file too big",
2651 "no space left on device",
2652 "illegal seek",
2653 "read-only file system",
2654 "too many links",
2655 "broken pipe",
2656 "math argument",
2657 "result too large",
2658 "I/O stream empty",
2659 "vax/vms specific error code nontranslatable error"
2661 #endif /* SHAREABLE_LIB_BUG */
2662 #endif /* LINK_CRTL_SHARE */
2663 #endif /* VMS */
2665 #ifndef HAVE_STRERROR
2666 char *
2667 strerror (errnum)
2668 int errnum;
2670 extern char *sys_errlist[];
2671 extern int sys_nerr;
2673 if (errnum >= 0 && errnum < sys_nerr)
2674 return sys_errlist[errnum];
2675 return (char *) "Unknown error";
2678 #endif /* ! HAVE_STRERROR */
2680 #ifdef INTERRUPTIBLE_OPEN
2683 /* VARARGS 2 */
2684 sys_open (path, oflag, mode)
2685 char *path;
2686 int oflag, mode;
2688 register int rtnval;
2690 while ((rtnval = open (path, oflag, mode)) == -1
2691 && (errno == EINTR));
2692 return (rtnval);
2695 #endif /* INTERRUPTIBLE_OPEN */
2697 #ifdef INTERRUPTIBLE_CLOSE
2699 sys_close (fd)
2700 int fd;
2702 register int rtnval;
2704 while ((rtnval = close (fd)) == -1
2705 && (errno == EINTR));
2706 return rtnval;
2709 #endif /* INTERRUPTIBLE_CLOSE */
2711 #ifdef INTERRUPTIBLE_IO
2714 sys_read (fildes, buf, nbyte)
2715 int fildes;
2716 char *buf;
2717 unsigned int nbyte;
2719 register int rtnval;
2721 while ((rtnval = read (fildes, buf, nbyte)) == -1
2722 && (errno == EINTR));
2723 return (rtnval);
2727 sys_write (fildes, buf, nbyte)
2728 int fildes;
2729 char *buf;
2730 unsigned int nbyte;
2732 register int rtnval, bytes_written;
2734 bytes_written = 0;
2736 while (nbyte > 0)
2738 rtnval = write (fildes, buf, nbyte);
2740 if (rtnval == -1)
2742 if (errno == EINTR)
2743 continue;
2744 else
2745 return (-1);
2748 buf += rtnval;
2749 nbyte -= rtnval;
2750 bytes_written += rtnval;
2752 return (bytes_written);
2755 #endif /* INTERRUPTIBLE_IO */
2757 #ifndef HAVE_VFORK
2760 * Substitute fork for vfork on USG flavors.
2763 vfork ()
2765 return (fork ());
2768 #endif /* not HAVE_VFORK */
2770 #ifdef USG
2772 * All of the following are for USG.
2774 * On USG systems the system calls are INTERRUPTIBLE by signals
2775 * that the user program has elected to catch. Thus the system call
2776 * must be retried in these cases. To handle this without massive
2777 * changes in the source code, we remap the standard system call names
2778 * to names for our own functions in sysdep.c that do the system call
2779 * with retries. Actually, for portability reasons, it is good
2780 * programming practice, as this example shows, to limit all actual
2781 * system calls to a single occurrence in the source. Sure, this
2782 * adds an extra level of function call overhead but it is almost
2783 * always negligible. Fred Fish, Unisoft Systems Inc.
2786 #ifndef HAVE_SYS_SIGLIST
2787 char *sys_siglist[NSIG + 1] =
2789 #ifdef AIX
2790 /* AIX has changed the signals a bit */
2791 "bogus signal", /* 0 */
2792 "hangup", /* 1 SIGHUP */
2793 "interrupt", /* 2 SIGINT */
2794 "quit", /* 3 SIGQUIT */
2795 "illegal instruction", /* 4 SIGILL */
2796 "trace trap", /* 5 SIGTRAP */
2797 "IOT instruction", /* 6 SIGIOT */
2798 "crash likely", /* 7 SIGDANGER */
2799 "floating point exception", /* 8 SIGFPE */
2800 "kill", /* 9 SIGKILL */
2801 "bus error", /* 10 SIGBUS */
2802 "segmentation violation", /* 11 SIGSEGV */
2803 "bad argument to system call", /* 12 SIGSYS */
2804 "write on a pipe with no one to read it", /* 13 SIGPIPE */
2805 "alarm clock", /* 14 SIGALRM */
2806 "software termination signum", /* 15 SIGTERM */
2807 "user defined signal 1", /* 16 SIGUSR1 */
2808 "user defined signal 2", /* 17 SIGUSR2 */
2809 "death of a child", /* 18 SIGCLD */
2810 "power-fail restart", /* 19 SIGPWR */
2811 "bogus signal", /* 20 */
2812 "bogus signal", /* 21 */
2813 "bogus signal", /* 22 */
2814 "bogus signal", /* 23 */
2815 "bogus signal", /* 24 */
2816 "LAN I/O interrupt", /* 25 SIGAIO */
2817 "PTY I/O interrupt", /* 26 SIGPTY */
2818 "I/O intervention required", /* 27 SIGIOINT */
2819 "HFT grant", /* 28 SIGGRANT */
2820 "HFT retract", /* 29 SIGRETRACT */
2821 "HFT sound done", /* 30 SIGSOUND */
2822 "HFT input ready", /* 31 SIGMSG */
2823 #else /* not AIX */
2824 "bogus signal", /* 0 */
2825 "hangup", /* 1 SIGHUP */
2826 "interrupt", /* 2 SIGINT */
2827 "quit", /* 3 SIGQUIT */
2828 "illegal instruction", /* 4 SIGILL */
2829 "trace trap", /* 5 SIGTRAP */
2830 "IOT instruction", /* 6 SIGIOT */
2831 "EMT instruction", /* 7 SIGEMT */
2832 "floating point exception", /* 8 SIGFPE */
2833 "kill", /* 9 SIGKILL */
2834 "bus error", /* 10 SIGBUS */
2835 "segmentation violation", /* 11 SIGSEGV */
2836 "bad argument to system call", /* 12 SIGSYS */
2837 "write on a pipe with no one to read it", /* 13 SIGPIPE */
2838 "alarm clock", /* 14 SIGALRM */
2839 "software termination signum", /* 15 SIGTERM */
2840 "user defined signal 1", /* 16 SIGUSR1 */
2841 "user defined signal 2", /* 17 SIGUSR2 */
2842 "death of a child", /* 18 SIGCLD */
2843 "power-fail restart", /* 19 SIGPWR */
2844 #ifdef sun
2845 "window size change", /* 20 SIGWINCH */
2846 "urgent socket condition", /* 21 SIGURG */
2847 "pollable event occured", /* 22 SIGPOLL */
2848 "stop (cannot be caught or ignored)", /* 23 SIGSTOP */
2849 "user stop requested from tty", /* 24 SIGTSTP */
2850 "stopped process has been continued", /* 25 SIGCONT */
2851 "background tty read attempted", /* 26 SIGTTIN */
2852 "background tty write attempted", /* 27 SIGTTOU */
2853 "virtual timer expired", /* 28 SIGVTALRM */
2854 "profiling timer expired", /* 29 SIGPROF */
2855 "exceeded cpu limit", /* 30 SIGXCPU */
2856 "exceeded file size limit", /* 31 SIGXFSZ */
2857 "process's lwps are blocked", /* 32 SIGWAITING */
2858 "special signal used by thread library", /* 33 SIGLWP */
2859 "Special Signal Used By CPR", /* 34 SIGFREEZE */
2860 "Special Signal Used By CPR", /* 35 SIGTHAW */
2861 #endif /* sun */
2862 #endif /* not AIX */
2865 #endif /* HAVE_SYS_SIGLIST */
2868 * Warning, this function may not duplicate 4.2 action properly
2869 * under error conditions.
2872 #ifndef MAXPATHLEN
2873 /* In 4.1, param.h fails to define this. */
2874 #define MAXPATHLEN 1024
2875 #endif
2877 #ifndef HAVE_GETWD
2879 char *
2880 getwd (pathname)
2881 char *pathname;
2883 char *npath, *spath;
2884 extern char *getcwd ();
2886 BLOCK_INPUT; /* getcwd uses malloc */
2887 spath = npath = getcwd ((char *) 0, MAXPATHLEN);
2888 if (spath == 0)
2889 return spath;
2890 /* On Altos 3068, getcwd can return @hostname/dir, so discard
2891 up to first slash. Should be harmless on other systems. */
2892 while (*npath && *npath != '/')
2893 npath++;
2894 strcpy (pathname, npath);
2895 free (spath); /* getcwd uses malloc */
2896 UNBLOCK_INPUT;
2897 return pathname;
2900 #endif /* HAVE_GETWD */
2903 * Emulate rename using unlink/link. Note that this is
2904 * only partially correct. Also, doesn't enforce restriction
2905 * that files be of same type (regular->regular, dir->dir, etc).
2908 #ifndef HAVE_RENAME
2910 rename (from, to)
2911 const char *from;
2912 const char *to;
2914 if (access (from, 0) == 0)
2916 unlink (to);
2917 if (link (from, to) == 0)
2918 if (unlink (from) == 0)
2919 return (0);
2921 return (-1);
2924 #endif
2926 #ifdef MISSING_UTIMES
2928 /* HPUX (among others) sets HAVE_TIMEVAL but does not implement utimes. */
2930 utimes ()
2933 #endif
2935 #ifdef IRIS_UTIME
2937 /* The IRIS (3.5) has timevals, but uses sys V utime, and doesn't have the
2938 utimbuf structure defined anywhere but in the man page. */
2940 struct utimbuf
2942 long actime;
2943 long modtime;
2946 utimes (name, tvp)
2947 char *name;
2948 struct timeval tvp[];
2950 struct utimbuf utb;
2951 utb.actime = tvp[0].tv_sec;
2952 utb.modtime = tvp[1].tv_sec;
2953 utime (name, &utb);
2955 #endif /* IRIS_UTIME */
2958 #ifdef HPUX
2959 #ifndef HAVE_PERROR
2961 /* HPUX curses library references perror, but as far as we know
2962 it won't be called. Anyway this definition will do for now. */
2964 perror ()
2968 #endif /* not HAVE_PERROR */
2969 #endif /* HPUX */
2971 #ifndef HAVE_DUP2
2974 * Emulate BSD dup2. First close newd if it already exists.
2975 * Then, attempt to dup oldd. If not successful, call dup2 recursively
2976 * until we are, then close the unsuccessful ones.
2979 dup2 (oldd, newd)
2980 int oldd;
2981 int newd;
2983 register int fd, ret;
2985 sys_close (newd);
2987 #ifdef F_DUPFD
2988 fd = fcntl (oldd, F_DUPFD, newd);
2989 if (fd != newd)
2990 error ("can't dup2 (%i,%i) : %s", oldd, newd, strerror (errno));
2991 #else
2992 fd = dup (old);
2993 if (fd == -1)
2994 return -1;
2995 if (fd == new)
2996 return new;
2997 ret = dup2 (old,new);
2998 sys_close (fd);
2999 return ret;
3000 #endif
3003 #endif /* not HAVE_DUP2 */
3006 * Gettimeofday. Simulate as much as possible. Only accurate
3007 * to nearest second. Emacs doesn't use tzp so ignore it for now.
3008 * Only needed when subprocesses are defined.
3011 #ifdef subprocesses
3012 #ifndef VMS
3013 #ifndef HAVE_GETTIMEOFDAY
3014 #ifdef HAVE_TIMEVAL
3016 /* ARGSUSED */
3017 gettimeofday (tp, tzp)
3018 struct timeval *tp;
3019 struct timezone *tzp;
3021 extern long time ();
3023 tp->tv_sec = time ((long *)0);
3024 tp->tv_usec = 0;
3025 if (tzp != 0)
3026 tzp->tz_minuteswest = -1;
3029 #endif
3030 #endif
3031 #endif
3032 #endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
3035 * This function will go away as soon as all the stubs fixed. (fnf)
3038 croak (badfunc)
3039 char *badfunc;
3041 printf ("%s not yet implemented\r\n", badfunc);
3042 reset_sys_modes ();
3043 exit (1);
3046 #endif /* USG */
3048 #ifdef DGUX
3050 char *sys_siglist[NSIG + 1] =
3052 "null signal", /* 0 SIGNULL */
3053 "hangup", /* 1 SIGHUP */
3054 "interrupt", /* 2 SIGINT */
3055 "quit", /* 3 SIGQUIT */
3056 "illegal instruction", /* 4 SIGILL */
3057 "trace trap", /* 5 SIGTRAP */
3058 "abort termination", /* 6 SIGABRT */
3059 "SIGEMT", /* 7 SIGEMT */
3060 "floating point exception", /* 8 SIGFPE */
3061 "kill", /* 9 SIGKILL */
3062 "bus error", /* 10 SIGBUS */
3063 "segmentation violation", /* 11 SIGSEGV */
3064 "bad argument to system call", /* 12 SIGSYS */
3065 "write on a pipe with no reader", /* 13 SIGPIPE */
3066 "alarm clock", /* 14 SIGALRM */
3067 "software termination signal", /* 15 SIGTERM */
3068 "user defined signal 1", /* 16 SIGUSR1 */
3069 "user defined signal 2", /* 17 SIGUSR2 */
3070 "child stopped or terminated", /* 18 SIGCLD */
3071 "power-fail restart", /* 19 SIGPWR */
3072 "window size changed", /* 20 SIGWINCH */
3073 "undefined", /* 21 */
3074 "pollable event occurred", /* 22 SIGPOLL */
3075 "sendable stop signal not from tty", /* 23 SIGSTOP */
3076 "stop signal from tty", /* 24 SIGSTP */
3077 "continue a stopped process", /* 25 SIGCONT */
3078 "attempted background tty read", /* 26 SIGTTIN */
3079 "attempted background tty write", /* 27 SIGTTOU */
3080 "undefined", /* 28 */
3081 "undefined", /* 29 */
3082 "undefined", /* 30 */
3083 "undefined", /* 31 */
3084 "undefined", /* 32 */
3085 "socket (TCP/IP) urgent data arrival", /* 33 SIGURG */
3086 "I/O is possible", /* 34 SIGIO */
3087 "exceeded cpu time limit", /* 35 SIGXCPU */
3088 "exceeded file size limit", /* 36 SIGXFSZ */
3089 "virtual time alarm", /* 37 SIGVTALRM */
3090 "profiling time alarm", /* 38 SIGPROF */
3091 "undefined", /* 39 */
3092 "file record locks revoked", /* 40 SIGLOST */
3093 "undefined", /* 41 */
3094 "undefined", /* 42 */
3095 "undefined", /* 43 */
3096 "undefined", /* 44 */
3097 "undefined", /* 45 */
3098 "undefined", /* 46 */
3099 "undefined", /* 47 */
3100 "undefined", /* 48 */
3101 "undefined", /* 49 */
3102 "undefined", /* 50 */
3103 "undefined", /* 51 */
3104 "undefined", /* 52 */
3105 "undefined", /* 53 */
3106 "undefined", /* 54 */
3107 "undefined", /* 55 */
3108 "undefined", /* 56 */
3109 "undefined", /* 57 */
3110 "undefined", /* 58 */
3111 "undefined", /* 59 */
3112 "undefined", /* 60 */
3113 "undefined", /* 61 */
3114 "undefined", /* 62 */
3115 "undefined", /* 63 */
3116 "notification message in mess. queue", /* 64 SIGDGNOTIFY */
3120 #endif /* DGUX */
3122 /* Directory routines for systems that don't have them. */
3124 #ifdef SYSV_SYSTEM_DIR
3126 #include <dirent.h>
3128 #if defined(BROKEN_CLOSEDIR) || !defined(HAVE_CLOSEDIR)
3131 closedir (dirp)
3132 register DIR *dirp; /* stream from opendir */
3134 int rtnval;
3136 rtnval = sys_close (dirp->dd_fd);
3138 /* Some systems (like Solaris) allocate the buffer and the DIR all
3139 in one block. Why in the world are we freeing this ourselves
3140 anyway? */
3141 #if ! (defined (sun) && defined (USG5_4))
3142 xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
3143 #endif
3144 xfree ((char *) dirp);
3146 return rtnval;
3148 #endif /* BROKEN_CLOSEDIR or not HAVE_CLOSEDIR */
3149 #endif /* SYSV_SYSTEM_DIR */
3151 #ifdef NONSYSTEM_DIR_LIBRARY
3153 DIR *
3154 opendir (filename)
3155 char *filename; /* name of directory */
3157 register DIR *dirp; /* -> malloc'ed storage */
3158 register int fd; /* file descriptor for read */
3159 struct stat sbuf; /* result of fstat */
3161 fd = sys_open (filename, 0);
3162 if (fd < 0)
3163 return 0;
3165 BLOCK_INPUT;
3166 if (fstat (fd, &sbuf) < 0
3167 || (sbuf.st_mode & S_IFMT) != S_IFDIR
3168 || (dirp = (DIR *) malloc (sizeof (DIR))) == 0)
3170 sys_close (fd);
3171 UNBLOCK_INPUT;
3172 return 0; /* bad luck today */
3174 UNBLOCK_INPUT;
3176 dirp->dd_fd = fd;
3177 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
3179 return dirp;
3182 void
3183 closedir (dirp)
3184 register DIR *dirp; /* stream from opendir */
3186 sys_close (dirp->dd_fd);
3187 xfree ((char *) dirp);
3191 #ifndef VMS
3192 #define DIRSIZ 14
3193 struct olddir
3195 ino_t od_ino; /* inode */
3196 char od_name[DIRSIZ]; /* filename */
3198 #endif /* not VMS */
3200 struct direct dir_static; /* simulated directory contents */
3202 /* ARGUSED */
3203 struct direct *
3204 readdir (dirp)
3205 register DIR *dirp; /* stream from opendir */
3207 #ifndef VMS
3208 register struct olddir *dp; /* -> directory data */
3209 #else /* VMS */
3210 register struct dir$_name *dp; /* -> directory data */
3211 register struct dir$_version *dv; /* -> version data */
3212 #endif /* VMS */
3214 for (; ;)
3216 if (dirp->dd_loc >= dirp->dd_size)
3217 dirp->dd_loc = dirp->dd_size = 0;
3219 if (dirp->dd_size == 0 /* refill buffer */
3220 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3221 return 0;
3223 #ifndef VMS
3224 dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
3225 dirp->dd_loc += sizeof (struct olddir);
3227 if (dp->od_ino != 0) /* not deleted entry */
3229 dir_static.d_ino = dp->od_ino;
3230 strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
3231 dir_static.d_name[DIRSIZ] = '\0';
3232 dir_static.d_namlen = strlen (dir_static.d_name);
3233 dir_static.d_reclen = sizeof (struct direct)
3234 - MAXNAMLEN + 3
3235 + dir_static.d_namlen - dir_static.d_namlen % 4;
3236 return &dir_static; /* -> simulated structure */
3238 #else /* VMS */
3239 dp = (struct dir$_name *) dirp->dd_buf;
3240 if (dirp->dd_loc == 0)
3241 dirp->dd_loc = (dp->dir$b_namecount&1) ? dp->dir$b_namecount + 1
3242 : dp->dir$b_namecount;
3243 dv = (struct dir$_version *)&dp->dir$t_name[dirp->dd_loc];
3244 dir_static.d_ino = dv->dir$w_fid_num;
3245 dir_static.d_namlen = dp->dir$b_namecount;
3246 dir_static.d_reclen = sizeof (struct direct)
3247 - MAXNAMLEN + 3
3248 + dir_static.d_namlen - dir_static.d_namlen % 4;
3249 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3250 dir_static.d_name[dir_static.d_namlen] = '\0';
3251 dirp->dd_loc = dirp->dd_size; /* only one record at a time */
3252 return &dir_static;
3253 #endif /* VMS */
3257 #ifdef VMS
3258 /* readdirver is just like readdir except it returns all versions of a file
3259 as separate entries. */
3261 /* ARGUSED */
3262 struct direct *
3263 readdirver (dirp)
3264 register DIR *dirp; /* stream from opendir */
3266 register struct dir$_name *dp; /* -> directory data */
3267 register struct dir$_version *dv; /* -> version data */
3269 if (dirp->dd_loc >= dirp->dd_size - sizeof (struct dir$_name))
3270 dirp->dd_loc = dirp->dd_size = 0;
3272 if (dirp->dd_size == 0 /* refill buffer */
3273 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
3274 return 0;
3276 dp = (struct dir$_name *) dirp->dd_buf;
3277 if (dirp->dd_loc == 0)
3278 dirp->dd_loc = (dp->dir$b_namecount & 1) ? dp->dir$b_namecount + 1
3279 : dp->dir$b_namecount;
3280 dv = (struct dir$_version *) &dp->dir$t_name[dirp->dd_loc];
3281 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3282 sprintf (&dir_static.d_name[dp->dir$b_namecount], ";%d", dv->dir$w_version);
3283 dir_static.d_namlen = strlen (dir_static.d_name);
3284 dir_static.d_ino = dv->dir$w_fid_num;
3285 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3
3286 + dir_static.d_namlen - dir_static.d_namlen % 4;
3287 dirp->dd_loc = ((char *) (++dv) - dp->dir$t_name);
3288 return &dir_static;
3291 #endif /* VMS */
3293 #endif /* NONSYSTEM_DIR_LIBRARY */
3296 /* mkdir and rmdir functions, for systems which don't have them. */
3298 #ifndef HAVE_MKDIR
3300 * Written by Robert Rother, Mariah Corporation, August 1985.
3302 * If you want it, it's yours. All I ask in return is that if you
3303 * figure out how to do this in a Bourne Shell script you send me
3304 * a copy.
3305 * sdcsvax!rmr or rmr@uscd
3307 * Severely hacked over by John Gilmore to make a 4.2BSD compatible
3308 * subroutine. 11Mar86; hoptoad!gnu
3310 * Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
3311 * subroutine didn't return EEXIST. It does now.
3315 * Make a directory.
3317 #ifdef MKDIR_PROTOTYPE
3318 MKDIR_PROTOTYPE
3319 #else
3321 mkdir (dpath, dmode)
3322 char *dpath;
3323 int dmode;
3324 #endif
3326 int cpid, status, fd;
3327 struct stat statbuf;
3329 if (stat (dpath, &statbuf) == 0)
3331 errno = EEXIST; /* Stat worked, so it already exists */
3332 return -1;
3335 /* If stat fails for a reason other than non-existence, return error */
3336 if (errno != ENOENT)
3337 return -1;
3339 synch_process_alive = 1;
3340 switch (cpid = fork ())
3343 case -1: /* Error in fork */
3344 return (-1); /* Errno is set already */
3346 case 0: /* Child process */
3348 * Cheap hack to set mode of new directory. Since this
3349 * child process is going away anyway, we zap its umask.
3350 * FIXME, this won't suffice to set SUID, SGID, etc. on this
3351 * directory. Does anybody care?
3353 status = umask (0); /* Get current umask */
3354 status = umask (status | (0777 & ~dmode)); /* Set for mkdir */
3355 fd = sys_open("/dev/null", 2);
3356 if (fd >= 0)
3358 dup2 (fd, 0);
3359 dup2 (fd, 1);
3360 dup2 (fd, 2);
3362 execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
3363 _exit (-1); /* Can't exec /bin/mkdir */
3365 default: /* Parent process */
3366 wait_for_termination (cpid);
3369 if (synch_process_death != 0 || synch_process_retcode != 0)
3371 errno = EIO; /* We don't know why, but */
3372 return -1; /* /bin/mkdir failed */
3375 return 0;
3377 #endif /* not HAVE_MKDIR */
3379 #ifndef HAVE_RMDIR
3381 rmdir (dpath)
3382 char *dpath;
3384 int cpid, status, fd;
3385 struct stat statbuf;
3387 if (stat (dpath, &statbuf) != 0)
3389 /* Stat just set errno. We don't have to */
3390 return -1;
3393 synch_process_alive = 1;
3394 switch (cpid = fork ())
3397 case -1: /* Error in fork */
3398 return (-1); /* Errno is set already */
3400 case 0: /* Child process */
3401 fd = sys_open("/dev/null", 2);
3402 if (fd >= 0)
3404 dup2 (fd, 0);
3405 dup2 (fd, 1);
3406 dup2 (fd, 2);
3408 wait_for_termination (cpid);
3409 if (synch_process_death != 0 || synch_process_retcode != 0)
3410 return -1; /* /bin/rmdir failed */
3411 default: /* Parent process */
3412 while (cpid != wait (&status)); /* Wait for kid to finish */
3415 if (WIFSIGNALED (status) || WEXITSTATUS (status) != 0)
3417 errno = EIO; /* We don't know why, but */
3418 return -1; /* /bin/mkdir failed */
3421 return 0;
3423 #endif /* !HAVE_RMDIR */
3427 /* Functions for VMS */
3428 #ifdef VMS
3429 #include "vms-pwd.h"
3430 #include <acldef.h>
3431 #include <chpdef.h>
3432 #include <jpidef.h>
3434 /* Return as a string the VMS error string pertaining to STATUS.
3435 Reuses the same static buffer each time it is called. */
3437 char *
3438 vmserrstr (status)
3439 int status; /* VMS status code */
3441 int bufadr[2];
3442 short len;
3443 static char buf[257];
3445 bufadr[0] = sizeof buf - 1;
3446 bufadr[1] = (int) buf;
3447 if (! (SYS$GETMSG (status, &len, bufadr, 0x1, 0) & 1))
3448 return "untranslatable VMS error status";
3449 buf[len] = '\0';
3450 return buf;
3453 #ifdef access
3454 #undef access
3456 /* The following is necessary because 'access' emulation by VMS C (2.0) does
3457 * not work correctly. (It also doesn't work well in version 2.3.)
3460 #ifdef VMS4_4
3462 #define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3463 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3465 typedef union {
3466 struct {
3467 unsigned short s_buflen;
3468 unsigned short s_code;
3469 char *s_bufadr;
3470 unsigned short *s_retlenadr;
3471 } s;
3472 int end;
3473 } item;
3474 #define buflen s.s_buflen
3475 #define code s.s_code
3476 #define bufadr s.s_bufadr
3477 #define retlenadr s.s_retlenadr
3479 #define R_OK 4 /* test for read permission */
3480 #define W_OK 2 /* test for write permission */
3481 #define X_OK 1 /* test for execute (search) permission */
3482 #define F_OK 0 /* test for presence of file */
3485 sys_access (path, mode)
3486 char *path;
3487 int mode;
3489 static char *user = NULL;
3490 char dir_fn[512];
3492 /* translate possible directory spec into .DIR file name, so brain-dead
3493 * access can treat the directory like a file. */
3494 if (directory_file_name (path, dir_fn))
3495 path = dir_fn;
3497 if (mode == F_OK)
3498 return access (path, mode);
3499 if (user == NULL && (user = (char *) getenv ("USER")) == NULL)
3500 return -1;
3502 int stat;
3503 int flags;
3504 int acces;
3505 unsigned short int dummy;
3506 item itemlst[3];
3507 static int constant = ACL$C_FILE;
3508 DESCRIPTOR (path_desc, path);
3509 DESCRIPTOR (user_desc, user);
3511 flags = 0;
3512 acces = 0;
3513 if ((mode & X_OK) && ((stat = access (path, mode)) < 0 || mode == X_OK))
3514 return stat;
3515 if (mode & R_OK)
3516 acces |= CHP$M_READ;
3517 if (mode & W_OK)
3518 acces |= CHP$M_WRITE;
3519 itemlst[0].buflen = sizeof (int);
3520 itemlst[0].code = CHP$_FLAGS;
3521 itemlst[0].bufadr = (char *) &flags;
3522 itemlst[0].retlenadr = &dummy;
3523 itemlst[1].buflen = sizeof (int);
3524 itemlst[1].code = CHP$_ACCESS;
3525 itemlst[1].bufadr = (char *) &acces;
3526 itemlst[1].retlenadr = &dummy;
3527 itemlst[2].end = CHP$_END;
3528 stat = SYS$CHECK_ACCESS (&constant, &path_desc, &user_desc, itemlst);
3529 return stat == SS$_NORMAL ? 0 : -1;
3533 #else /* not VMS4_4 */
3535 #include <prvdef.h>
3536 #define ACE$M_WRITE 2
3537 #define ACE$C_KEYID 1
3539 static unsigned short memid, grpid;
3540 static unsigned int uic;
3542 /* Called from init_sys_modes, so it happens not very often
3543 but at least each time Emacs is loaded. */
3544 sys_access_reinit ()
3546 uic = 0;
3550 sys_access (filename, type)
3551 char * filename;
3552 int type;
3554 struct FAB fab;
3555 struct XABPRO xab;
3556 int status, size, i, typecode, acl_controlled;
3557 unsigned int *aclptr, *aclend, aclbuf[60];
3558 union prvdef prvmask;
3560 /* Get UIC and GRP values for protection checking. */
3561 if (uic == 0)
3563 status = LIB$GETJPI (&JPI$_UIC, 0, 0, &uic, 0, 0);
3564 if (! (status & 1))
3565 return -1;
3566 memid = uic & 0xFFFF;
3567 grpid = uic >> 16;
3570 if (type != 2) /* not checking write access */
3571 return access (filename, type);
3573 /* Check write protection. */
3575 #define CHECKPRIV(bit) (prvmask.bit)
3576 #define WRITEABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
3578 /* Find privilege bits */
3579 status = SYS$SETPRV (0, 0, 0, prvmask);
3580 if (! (status & 1))
3581 error ("Unable to find privileges: %s", vmserrstr (status));
3582 if (CHECKPRIV (PRV$V_BYPASS))
3583 return 0; /* BYPASS enabled */
3584 fab = cc$rms_fab;
3585 fab.fab$b_fac = FAB$M_GET;
3586 fab.fab$l_fna = filename;
3587 fab.fab$b_fns = strlen (filename);
3588 fab.fab$l_xab = &xab;
3589 xab = cc$rms_xabpro;
3590 xab.xab$l_aclbuf = aclbuf;
3591 xab.xab$w_aclsiz = sizeof (aclbuf);
3592 status = SYS$OPEN (&fab, 0, 0);
3593 if (! (status & 1))
3594 return -1;
3595 SYS$CLOSE (&fab, 0, 0);
3596 /* Check system access */
3597 if (CHECKPRIV (PRV$V_SYSPRV) && WRITEABLE (XAB$V_SYS))
3598 return 0;
3599 /* Check ACL entries, if any */
3600 acl_controlled = 0;
3601 if (xab.xab$w_acllen > 0)
3603 aclptr = aclbuf;
3604 aclend = &aclbuf[xab.xab$w_acllen / 4];
3605 while (*aclptr && aclptr < aclend)
3607 size = (*aclptr & 0xff) / 4;
3608 typecode = (*aclptr >> 8) & 0xff;
3609 if (typecode == ACE$C_KEYID)
3610 for (i = size - 1; i > 1; i--)
3611 if (aclptr[i] == uic)
3613 acl_controlled = 1;
3614 if (aclptr[1] & ACE$M_WRITE)
3615 return 0; /* Write access through ACL */
3617 aclptr = &aclptr[size];
3619 if (acl_controlled) /* ACL specified, prohibits write access */
3620 return -1;
3622 /* No ACL entries specified, check normal protection */
3623 if (WRITEABLE (XAB$V_WLD)) /* World writeable */
3624 return 0;
3625 if (WRITEABLE (XAB$V_GRP) &&
3626 (unsigned short) (xab.xab$l_uic >> 16) == grpid)
3627 return 0; /* Group writeable */
3628 if (WRITEABLE (XAB$V_OWN) &&
3629 (xab.xab$l_uic & 0xFFFF) == memid)
3630 return 0; /* Owner writeable */
3632 return -1; /* Not writeable */
3634 #endif /* not VMS4_4 */
3635 #endif /* access */
3637 static char vtbuf[NAM$C_MAXRSS+1];
3639 /* translate a vms file spec to a unix path */
3640 char *
3641 sys_translate_vms (vfile)
3642 char * vfile;
3644 char * p;
3645 char * targ;
3647 if (!vfile)
3648 return 0;
3650 targ = vtbuf;
3652 /* leading device or logical name is a root directory */
3653 if (p = strchr (vfile, ':'))
3655 *targ++ = '/';
3656 while (vfile < p)
3657 *targ++ = *vfile++;
3658 vfile++;
3659 *targ++ = '/';
3661 p = vfile;
3662 if (*p == '[' || *p == '<')
3664 while (*++vfile != *p + 2)
3665 switch (*vfile)
3667 case '.':
3668 if (vfile[-1] == *p)
3669 *targ++ = '.';
3670 *targ++ = '/';
3671 break;
3673 case '-':
3674 *targ++ = '.';
3675 *targ++ = '.';
3676 break;
3678 default:
3679 *targ++ = *vfile;
3680 break;
3682 vfile++;
3683 *targ++ = '/';
3685 while (*vfile)
3686 *targ++ = *vfile++;
3688 return vtbuf;
3691 static char utbuf[NAM$C_MAXRSS+1];
3693 /* translate a unix path to a VMS file spec */
3694 char *
3695 sys_translate_unix (ufile)
3696 char * ufile;
3698 int slash_seen = 0;
3699 char *p;
3700 char * targ;
3702 if (!ufile)
3703 return 0;
3705 targ = utbuf;
3707 if (*ufile == '/')
3709 ufile++;
3712 while (*ufile)
3714 switch (*ufile)
3716 case '/':
3717 if (slash_seen)
3718 if (index (&ufile[1], '/'))
3719 *targ++ = '.';
3720 else
3721 *targ++ = ']';
3722 else
3724 *targ++ = ':';
3725 if (index (&ufile[1], '/'))
3726 *targ++ = '[';
3727 slash_seen = 1;
3729 break;
3731 case '.':
3732 if (strncmp (ufile, "./", 2) == 0)
3734 if (!slash_seen)
3736 *targ++ = '[';
3737 slash_seen = 1;
3739 ufile++; /* skip the dot */
3740 if (index (&ufile[1], '/'))
3741 *targ++ = '.';
3742 else
3743 *targ++ = ']';
3745 else if (strncmp (ufile, "../", 3) == 0)
3747 if (!slash_seen)
3749 *targ++ = '[';
3750 slash_seen = 1;
3752 *targ++ = '-';
3753 ufile += 2; /* skip the dots */
3754 if (index (&ufile[1], '/'))
3755 *targ++ = '.';
3756 else
3757 *targ++ = ']';
3759 else
3760 *targ++ = *ufile;
3761 break;
3763 default:
3764 *targ++ = *ufile;
3765 break;
3767 ufile++;
3769 *targ = '\0';
3771 return utbuf;
3774 char *
3775 getwd (pathname)
3776 char *pathname;
3778 char *ptr, *val;
3779 extern char *getcwd ();
3781 #define MAXPATHLEN 1024
3783 ptr = xmalloc (MAXPATHLEN);
3784 val = getcwd (ptr, MAXPATHLEN);
3785 if (val == 0)
3787 xfree (ptr);
3788 return val;
3790 strcpy (pathname, ptr);
3791 xfree (ptr);
3793 return pathname;
3796 getppid ()
3798 long item_code = JPI$_OWNER;
3799 unsigned long parent_id;
3800 int status;
3802 if (((status = LIB$GETJPI (&item_code, 0, 0, &parent_id)) & 1) == 0)
3804 errno = EVMSERR;
3805 vaxc$errno = status;
3806 return -1;
3808 return parent_id;
3811 #undef getuid
3812 unsigned
3813 sys_getuid ()
3815 return (getgid () << 16) | getuid ();
3819 sys_read (fildes, buf, nbyte)
3820 int fildes;
3821 char *buf;
3822 unsigned int nbyte;
3824 return read (fildes, buf, (nbyte < MAXIOSIZE ? nbyte : MAXIOSIZE));
3827 #if 0
3829 sys_write (fildes, buf, nbyte)
3830 int fildes;
3831 char *buf;
3832 unsigned int nbyte;
3834 register int nwrote, rtnval = 0;
3836 while (nbyte > MAXIOSIZE && (nwrote = write (fildes, buf, MAXIOSIZE)) > 0) {
3837 nbyte -= nwrote;
3838 buf += nwrote;
3839 rtnval += nwrote;
3841 if (nwrote < 0)
3842 return rtnval ? rtnval : -1;
3843 if ((nwrote = write (fildes, buf, nbyte)) < 0)
3844 return rtnval ? rtnval : -1;
3845 return (rtnval + nwrote);
3847 #endif /* 0 */
3850 * VAX/VMS VAX C RTL really loses. It insists that records
3851 * end with a newline (carriage return) character, and if they
3852 * don't it adds one (nice of it isn't it!)
3854 * Thus we do this stupidity below.
3858 sys_write (fildes, buf, nbytes)
3859 int fildes;
3860 char *buf;
3861 unsigned int nbytes;
3863 register char *p;
3864 register char *e;
3865 int sum = 0;
3866 struct stat st;
3868 fstat (fildes, &st);
3869 p = buf;
3870 while (nbytes > 0)
3872 int len, retval;
3874 /* Handle fixed-length files with carriage control. */
3875 if (st.st_fab_rfm == FAB$C_FIX
3876 && ((st.st_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
3878 len = st.st_fab_mrs;
3879 retval = write (fildes, p, min (len, nbytes));
3880 if (retval != len)
3881 return -1;
3882 retval++; /* This skips the implied carriage control */
3884 else
3886 e = p + min (MAXIOSIZE, nbytes) - 1;
3887 while (*e != '\n' && e > p) e--;
3888 if (p == e) /* Ok.. so here we add a newline... sigh. */
3889 e = p + min (MAXIOSIZE, nbytes) - 1;
3890 len = e + 1 - p;
3891 retval = write (fildes, p, len);
3892 if (retval != len)
3893 return -1;
3895 p += retval;
3896 sum += retval;
3897 nbytes -= retval;
3899 return sum;
3902 /* Create file NEW copying its attributes from file OLD. If
3903 OLD is 0 or does not exist, create based on the value of
3904 vms_stmlf_recfm. */
3906 /* Protection value the file should ultimately have.
3907 Set by create_copy_attrs, and use by rename_sansversions. */
3908 static unsigned short int fab_final_pro;
3911 creat_copy_attrs (old, new)
3912 char *old, *new;
3914 struct FAB fab = cc$rms_fab;
3915 struct XABPRO xabpro;
3916 char aclbuf[256]; /* Choice of size is arbitrary. See below. */
3917 extern int vms_stmlf_recfm;
3919 if (old)
3921 fab.fab$b_fac = FAB$M_GET;
3922 fab.fab$l_fna = old;
3923 fab.fab$b_fns = strlen (old);
3924 fab.fab$l_xab = (char *) &xabpro;
3925 xabpro = cc$rms_xabpro;
3926 xabpro.xab$l_aclbuf = aclbuf;
3927 xabpro.xab$w_aclsiz = sizeof aclbuf;
3928 /* Call $OPEN to fill in the fab & xabpro fields. */
3929 if (SYS$OPEN (&fab, 0, 0) & 1)
3931 SYS$CLOSE (&fab, 0, 0);
3932 fab.fab$l_alq = 0; /* zero the allocation quantity */
3933 if (xabpro.xab$w_acllen > 0)
3935 if (xabpro.xab$w_acllen > sizeof aclbuf)
3936 /* If the acl buffer was too short, redo open with longer one.
3937 Wouldn't need to do this if there were some system imposed
3938 limit on the size of an ACL, but I can't find any such. */
3940 xabpro.xab$l_aclbuf = (char *) alloca (xabpro.xab$w_acllen);
3941 xabpro.xab$w_aclsiz = xabpro.xab$w_acllen;
3942 if (SYS$OPEN (&fab, 0, 0) & 1)
3943 SYS$CLOSE (&fab, 0, 0);
3944 else
3945 old = 0;
3948 else
3949 xabpro.xab$l_aclbuf = 0;
3951 else
3952 old = 0;
3954 fab.fab$l_fna = new;
3955 fab.fab$b_fns = strlen (new);
3956 if (!old)
3958 fab.fab$l_xab = 0;
3959 fab.fab$b_rfm = vms_stmlf_recfm ? FAB$C_STMLF : FAB$C_VAR;
3960 fab.fab$b_rat = FAB$M_CR;
3963 /* Set the file protections such that we will be able to manipulate
3964 this file. Once we are done writing and renaming it, we will set
3965 the protections back. */
3966 if (old)
3967 fab_final_pro = xabpro.xab$w_pro;
3968 else
3969 SYS$SETDFPROT (0, &fab_final_pro);
3970 xabpro.xab$w_pro &= 0xff0f; /* set O:rewd for now. This is set back later. */
3972 /* Create the new file with either default attrs or attrs copied
3973 from old file. */
3974 if (!(SYS$CREATE (&fab, 0, 0) & 1))
3975 return -1;
3976 SYS$CLOSE (&fab, 0, 0);
3977 /* As this is a "replacement" for creat, return a file descriptor
3978 opened for writing. */
3979 return open (new, O_WRONLY);
3982 #ifdef creat
3983 #undef creat
3984 #include <varargs.h>
3985 #ifdef __GNUC__
3986 #ifndef va_count
3987 #define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
3988 #endif
3989 #endif
3991 sys_creat (va_alist)
3992 va_dcl
3994 va_list list_incrementer;
3995 char *name;
3996 int mode;
3997 int rfd; /* related file descriptor */
3998 int fd; /* Our new file descriptor */
3999 int count;
4000 struct stat st_buf;
4001 char rfm[12];
4002 char rat[15];
4003 char mrs[13];
4004 char fsz[13];
4005 extern int vms_stmlf_recfm;
4007 va_count (count);
4008 va_start (list_incrementer);
4009 name = va_arg (list_incrementer, char *);
4010 mode = va_arg (list_incrementer, int);
4011 if (count > 2)
4012 rfd = va_arg (list_incrementer, int);
4013 va_end (list_incrementer);
4014 if (count > 2)
4016 /* Use information from the related file descriptor to set record
4017 format of the newly created file. */
4018 fstat (rfd, &st_buf);
4019 switch (st_buf.st_fab_rfm)
4021 case FAB$C_FIX:
4022 strcpy (rfm, "rfm = fix");
4023 sprintf (mrs, "mrs = %d", st_buf.st_fab_mrs);
4024 strcpy (rat, "rat = ");
4025 if (st_buf.st_fab_rat & FAB$M_CR)
4026 strcat (rat, "cr");
4027 else if (st_buf.st_fab_rat & FAB$M_FTN)
4028 strcat (rat, "ftn");
4029 else if (st_buf.st_fab_rat & FAB$M_PRN)
4030 strcat (rat, "prn");
4031 if (st_buf.st_fab_rat & FAB$M_BLK)
4032 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4033 strcat (rat, ", blk");
4034 else
4035 strcat (rat, "blk");
4036 return creat (name, 0, rfm, rat, mrs);
4038 case FAB$C_VFC:
4039 strcpy (rfm, "rfm = vfc");
4040 sprintf (fsz, "fsz = %d", st_buf.st_fab_fsz);
4041 strcpy (rat, "rat = ");
4042 if (st_buf.st_fab_rat & FAB$M_CR)
4043 strcat (rat, "cr");
4044 else if (st_buf.st_fab_rat & FAB$M_FTN)
4045 strcat (rat, "ftn");
4046 else if (st_buf.st_fab_rat & FAB$M_PRN)
4047 strcat (rat, "prn");
4048 if (st_buf.st_fab_rat & FAB$M_BLK)
4049 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4050 strcat (rat, ", blk");
4051 else
4052 strcat (rat, "blk");
4053 return creat (name, 0, rfm, rat, fsz);
4055 case FAB$C_STM:
4056 strcpy (rfm, "rfm = stm");
4057 break;
4059 case FAB$C_STMCR:
4060 strcpy (rfm, "rfm = stmcr");
4061 break;
4063 case FAB$C_STMLF:
4064 strcpy (rfm, "rfm = stmlf");
4065 break;
4067 case FAB$C_UDF:
4068 strcpy (rfm, "rfm = udf");
4069 break;
4071 case FAB$C_VAR:
4072 strcpy (rfm, "rfm = var");
4073 break;
4075 strcpy (rat, "rat = ");
4076 if (st_buf.st_fab_rat & FAB$M_CR)
4077 strcat (rat, "cr");
4078 else if (st_buf.st_fab_rat & FAB$M_FTN)
4079 strcat (rat, "ftn");
4080 else if (st_buf.st_fab_rat & FAB$M_PRN)
4081 strcat (rat, "prn");
4082 if (st_buf.st_fab_rat & FAB$M_BLK)
4083 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
4084 strcat (rat, ", blk");
4085 else
4086 strcat (rat, "blk");
4088 else
4090 strcpy (rfm, vms_stmlf_recfm ? "rfm = stmlf" : "rfm=var");
4091 strcpy (rat, "rat=cr");
4093 /* Until the VAX C RTL fixes the many bugs with modes, always use
4094 mode 0 to get the user's default protection. */
4095 fd = creat (name, 0, rfm, rat);
4096 if (fd < 0 && errno == EEXIST)
4098 if (unlink (name) < 0)
4099 report_file_error ("delete", build_string (name));
4100 fd = creat (name, 0, rfm, rat);
4102 return fd;
4104 #endif /* creat */
4106 /* fwrite to stdout is S L O W. Speed it up by using fputc...*/
4107 sys_fwrite (ptr, size, num, fp)
4108 register char * ptr;
4109 FILE * fp;
4111 register int tot = num * size;
4113 while (tot--)
4114 fputc (*ptr++, fp);
4118 * The VMS C library routine creat actually creates a new version of an
4119 * existing file rather than truncating the old version. There are times
4120 * when this is not the desired behavior, for instance, when writing an
4121 * auto save file (you only want one version), or when you don't have
4122 * write permission in the directory containing the file (but the file
4123 * itself is writable). Hence this routine, which is equivalent to
4124 * "close (creat (fn, 0));" on Unix if fn already exists.
4127 vms_truncate (fn)
4128 char *fn;
4130 struct FAB xfab = cc$rms_fab;
4131 struct RAB xrab = cc$rms_rab;
4132 int status;
4134 xfab.fab$l_fop = FAB$M_TEF; /* free allocated but unused blocks on close */
4135 xfab.fab$b_fac = FAB$M_TRN | FAB$M_GET; /* allow truncate and get access */
4136 xfab.fab$b_shr = FAB$M_NIL; /* allow no sharing - file must be locked */
4137 xfab.fab$l_fna = fn;
4138 xfab.fab$b_fns = strlen (fn);
4139 xfab.fab$l_dna = ";0"; /* default to latest version of the file */
4140 xfab.fab$b_dns = 2;
4141 xrab.rab$l_fab = &xfab;
4143 /* This gibberish opens the file, positions to the first record, and
4144 deletes all records from there until the end of file. */
4145 if ((SYS$OPEN (&xfab) & 01) == 01)
4147 if ((SYS$CONNECT (&xrab) & 01) == 01 &&
4148 (SYS$FIND (&xrab) & 01) == 01 &&
4149 (SYS$TRUNCATE (&xrab) & 01) == 01)
4150 status = 0;
4151 else
4152 status = -1;
4154 else
4155 status = -1;
4156 SYS$CLOSE (&xfab);
4157 return status;
4160 /* Define this symbol to actually read SYSUAF.DAT. This requires either
4161 SYSPRV or a readable SYSUAF.DAT. */
4163 #ifdef READ_SYSUAF
4165 * getuaf.c
4167 * Routine to read the VMS User Authorization File and return
4168 * a specific user's record.
4171 static struct UAF retuaf;
4173 struct UAF *
4174 get_uaf_name (uname)
4175 char * uname;
4177 register status;
4178 struct FAB uaf_fab;
4179 struct RAB uaf_rab;
4181 uaf_fab = cc$rms_fab;
4182 uaf_rab = cc$rms_rab;
4183 /* initialize fab fields */
4184 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4185 uaf_fab.fab$b_fns = 21;
4186 uaf_fab.fab$b_fac = FAB$M_GET;
4187 uaf_fab.fab$b_org = FAB$C_IDX;
4188 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4189 /* initialize rab fields */
4190 uaf_rab.rab$l_fab = &uaf_fab;
4191 /* open the User Authorization File */
4192 status = SYS$OPEN (&uaf_fab);
4193 if (!(status&1))
4195 errno = EVMSERR;
4196 vaxc$errno = status;
4197 return 0;
4199 status = SYS$CONNECT (&uaf_rab);
4200 if (!(status&1))
4202 errno = EVMSERR;
4203 vaxc$errno = status;
4204 return 0;
4206 /* read the requested record - index is in uname */
4207 uaf_rab.rab$l_kbf = uname;
4208 uaf_rab.rab$b_ksz = strlen (uname);
4209 uaf_rab.rab$b_rac = RAB$C_KEY;
4210 uaf_rab.rab$l_ubf = (char *)&retuaf;
4211 uaf_rab.rab$w_usz = sizeof retuaf;
4212 status = SYS$GET (&uaf_rab);
4213 if (!(status&1))
4215 errno = EVMSERR;
4216 vaxc$errno = status;
4217 return 0;
4219 /* close the User Authorization File */
4220 status = SYS$DISCONNECT (&uaf_rab);
4221 if (!(status&1))
4223 errno = EVMSERR;
4224 vaxc$errno = status;
4225 return 0;
4227 status = SYS$CLOSE (&uaf_fab);
4228 if (!(status&1))
4230 errno = EVMSERR;
4231 vaxc$errno = status;
4232 return 0;
4234 return &retuaf;
4237 struct UAF *
4238 get_uaf_uic (uic)
4239 unsigned long uic;
4241 register status;
4242 struct FAB uaf_fab;
4243 struct RAB uaf_rab;
4245 uaf_fab = cc$rms_fab;
4246 uaf_rab = cc$rms_rab;
4247 /* initialize fab fields */
4248 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
4249 uaf_fab.fab$b_fns = 21;
4250 uaf_fab.fab$b_fac = FAB$M_GET;
4251 uaf_fab.fab$b_org = FAB$C_IDX;
4252 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
4253 /* initialize rab fields */
4254 uaf_rab.rab$l_fab = &uaf_fab;
4255 /* open the User Authorization File */
4256 status = SYS$OPEN (&uaf_fab);
4257 if (!(status&1))
4259 errno = EVMSERR;
4260 vaxc$errno = status;
4261 return 0;
4263 status = SYS$CONNECT (&uaf_rab);
4264 if (!(status&1))
4266 errno = EVMSERR;
4267 vaxc$errno = status;
4268 return 0;
4270 /* read the requested record - index is in uic */
4271 uaf_rab.rab$b_krf = 1; /* 1st alternate key */
4272 uaf_rab.rab$l_kbf = (char *) &uic;
4273 uaf_rab.rab$b_ksz = sizeof uic;
4274 uaf_rab.rab$b_rac = RAB$C_KEY;
4275 uaf_rab.rab$l_ubf = (char *)&retuaf;
4276 uaf_rab.rab$w_usz = sizeof retuaf;
4277 status = SYS$GET (&uaf_rab);
4278 if (!(status&1))
4280 errno = EVMSERR;
4281 vaxc$errno = status;
4282 return 0;
4284 /* close the User Authorization File */
4285 status = SYS$DISCONNECT (&uaf_rab);
4286 if (!(status&1))
4288 errno = EVMSERR;
4289 vaxc$errno = status;
4290 return 0;
4292 status = SYS$CLOSE (&uaf_fab);
4293 if (!(status&1))
4295 errno = EVMSERR;
4296 vaxc$errno = status;
4297 return 0;
4299 return &retuaf;
4302 static struct passwd retpw;
4304 struct passwd *
4305 cnv_uaf_pw (up)
4306 struct UAF * up;
4308 char * ptr;
4310 /* copy these out first because if the username is 32 chars, the next
4311 section will overwrite the first byte of the UIC */
4312 retpw.pw_uid = up->uaf$w_mem;
4313 retpw.pw_gid = up->uaf$w_grp;
4315 /* I suppose this is not the best sytle, to possibly overwrite one
4316 byte beyond the end of the field, but what the heck... */
4317 ptr = &up->uaf$t_username[UAF$S_USERNAME];
4318 while (ptr[-1] == ' ')
4319 ptr--;
4320 *ptr = '\0';
4321 strcpy (retpw.pw_name, up->uaf$t_username);
4323 /* the rest of these are counted ascii strings */
4324 strncpy (retpw.pw_gecos, &up->uaf$t_owner[1], up->uaf$t_owner[0]);
4325 retpw.pw_gecos[up->uaf$t_owner[0]] = '\0';
4326 strncpy (retpw.pw_dir, &up->uaf$t_defdev[1], up->uaf$t_defdev[0]);
4327 retpw.pw_dir[up->uaf$t_defdev[0]] = '\0';
4328 strncat (retpw.pw_dir, &up->uaf$t_defdir[1], up->uaf$t_defdir[0]);
4329 retpw.pw_dir[up->uaf$t_defdev[0] + up->uaf$t_defdir[0]] = '\0';
4330 strncpy (retpw.pw_shell, &up->uaf$t_defcli[1], up->uaf$t_defcli[0]);
4331 retpw.pw_shell[up->uaf$t_defcli[0]] = '\0';
4333 return &retpw;
4335 #else /* not READ_SYSUAF */
4336 static struct passwd retpw;
4337 #endif /* not READ_SYSUAF */
4339 struct passwd *
4340 getpwnam (name)
4341 char * name;
4343 #ifdef READ_SYSUAF
4344 struct UAF *up;
4345 #else
4346 char * user;
4347 char * dir;
4348 unsigned char * full;
4349 #endif /* READ_SYSUAF */
4350 char *ptr = name;
4352 while (*ptr)
4354 if ('a' <= *ptr && *ptr <= 'z')
4355 *ptr -= 040;
4356 ptr++;
4358 #ifdef READ_SYSUAF
4359 if (!(up = get_uaf_name (name)))
4360 return 0;
4361 return cnv_uaf_pw (up);
4362 #else
4363 if (strcmp (name, getenv ("USER")) == 0)
4365 retpw.pw_uid = getuid ();
4366 retpw.pw_gid = getgid ();
4367 strcpy (retpw.pw_name, name);
4368 if (full = egetenv ("FULLNAME"))
4369 strcpy (retpw.pw_gecos, full);
4370 else
4371 *retpw.pw_gecos = '\0';
4372 strcpy (retpw.pw_dir, egetenv ("HOME"));
4373 *retpw.pw_shell = '\0';
4374 return &retpw;
4376 else
4377 return 0;
4378 #endif /* not READ_SYSUAF */
4381 struct passwd *
4382 getpwuid (uid)
4383 unsigned long uid;
4385 #ifdef READ_SYSUAF
4386 struct UAF * up;
4388 if (!(up = get_uaf_uic (uid)))
4389 return 0;
4390 return cnv_uaf_pw (up);
4391 #else
4392 if (uid == sys_getuid ())
4393 return getpwnam (egetenv ("USER"));
4394 else
4395 return 0;
4396 #endif /* not READ_SYSUAF */
4399 /* return total address space available to the current process. This is
4400 the sum of the current p0 size, p1 size and free page table entries
4401 available. */
4402 vlimit ()
4404 int item_code;
4405 unsigned long free_pages;
4406 unsigned long frep0va;
4407 unsigned long frep1va;
4408 register status;
4410 item_code = JPI$_FREPTECNT;
4411 if (((status = LIB$GETJPI (&item_code, 0, 0, &free_pages)) & 1) == 0)
4413 errno = EVMSERR;
4414 vaxc$errno = status;
4415 return -1;
4417 free_pages *= 512;
4419 item_code = JPI$_FREP0VA;
4420 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep0va)) & 1) == 0)
4422 errno = EVMSERR;
4423 vaxc$errno = status;
4424 return -1;
4426 item_code = JPI$_FREP1VA;
4427 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep1va)) & 1) == 0)
4429 errno = EVMSERR;
4430 vaxc$errno = status;
4431 return -1;
4434 return free_pages + frep0va + (0x7fffffff - frep1va);
4437 define_logical_name (varname, string)
4438 char *varname;
4439 char *string;
4441 struct dsc$descriptor_s strdsc =
4442 {strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string};
4443 struct dsc$descriptor_s envdsc =
4444 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4445 struct dsc$descriptor_s lnmdsc =
4446 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4448 return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
4451 delete_logical_name (varname)
4452 char *varname;
4454 struct dsc$descriptor_s envdsc =
4455 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4456 struct dsc$descriptor_s lnmdsc =
4457 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4459 return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
4462 ulimit ()
4465 setpgrp ()
4468 execvp ()
4470 error ("execvp system call not implemented");
4474 rename (from, to)
4475 char *from, *to;
4477 int status;
4478 struct FAB from_fab = cc$rms_fab, to_fab = cc$rms_fab;
4479 struct NAM from_nam = cc$rms_nam, to_nam = cc$rms_nam;
4480 char from_esn[NAM$C_MAXRSS];
4481 char to_esn[NAM$C_MAXRSS];
4483 from_fab.fab$l_fna = from;
4484 from_fab.fab$b_fns = strlen (from);
4485 from_fab.fab$l_nam = &from_nam;
4486 from_fab.fab$l_fop = FAB$M_NAM;
4488 from_nam.nam$l_esa = from_esn;
4489 from_nam.nam$b_ess = sizeof from_esn;
4491 to_fab.fab$l_fna = to;
4492 to_fab.fab$b_fns = strlen (to);
4493 to_fab.fab$l_nam = &to_nam;
4494 to_fab.fab$l_fop = FAB$M_NAM;
4496 to_nam.nam$l_esa = to_esn;
4497 to_nam.nam$b_ess = sizeof to_esn;
4499 status = SYS$RENAME (&from_fab, 0, 0, &to_fab);
4501 if (status & 1)
4502 return 0;
4503 else
4505 if (status == RMS$_DEV)
4506 errno = EXDEV;
4507 else
4508 errno = EVMSERR;
4509 vaxc$errno = status;
4510 return -1;
4514 /* This function renames a file like `rename', but it strips
4515 the version number from the "to" filename, such that the "to" file is
4516 will always be a new version. It also sets the file protection once it is
4517 finished. The protection that we will use is stored in fab_final_pro,
4518 and was set when we did a creat_copy_attrs to create the file that we
4519 are renaming.
4521 We could use the chmod function, but Eunichs uses 3 bits per user category
4522 to describe the protection, and VMS uses 4 (write and delete are separate
4523 bits). To maintain portability, the VMS implementation of `chmod' wires
4524 the W and D bits together. */
4527 static struct fibdef fib; /* We need this initialized to zero */
4528 char vms_file_written[NAM$C_MAXRSS];
4531 rename_sans_version (from,to)
4532 char *from, *to;
4534 short int chan;
4535 int stat;
4536 short int iosb[4];
4537 int status;
4538 struct FAB to_fab = cc$rms_fab;
4539 struct NAM to_nam = cc$rms_nam;
4540 struct dsc$descriptor fib_d ={sizeof (fib),0,0,(char*) &fib};
4541 struct dsc$descriptor fib_attr[2]
4542 = {{sizeof (fab_final_pro),ATR$C_FPRO,0,(char*) &fab_final_pro},{0,0,0,0}};
4543 char to_esn[NAM$C_MAXRSS];
4545 $DESCRIPTOR (disk,to_esn);
4547 to_fab.fab$l_fna = to;
4548 to_fab.fab$b_fns = strlen (to);
4549 to_fab.fab$l_nam = &to_nam;
4550 to_fab.fab$l_fop = FAB$M_NAM;
4552 to_nam.nam$l_esa = to_esn;
4553 to_nam.nam$b_ess = sizeof to_esn;
4555 status = SYS$PARSE (&to_fab, 0, 0); /* figure out the full file name */
4557 if (to_nam.nam$l_fnb && NAM$M_EXP_VER)
4558 *(to_nam.nam$l_ver) = '\0';
4560 stat = rename (from, to_esn);
4561 if (stat < 0)
4562 return stat;
4564 strcpy (vms_file_written, to_esn);
4566 to_fab.fab$l_fna = vms_file_written; /* this points to the versionless name */
4567 to_fab.fab$b_fns = strlen (vms_file_written);
4569 /* Now set the file protection to the correct value */
4570 SYS$OPEN (&to_fab, 0, 0); /* This fills in the nam$w_fid fields */
4572 /* Copy these fields into the fib */
4573 fib.fib$r_fid_overlay.fib$w_fid[0] = to_nam.nam$w_fid[0];
4574 fib.fib$r_fid_overlay.fib$w_fid[1] = to_nam.nam$w_fid[1];
4575 fib.fib$r_fid_overlay.fib$w_fid[2] = to_nam.nam$w_fid[2];
4577 SYS$CLOSE (&to_fab, 0, 0);
4579 stat = SYS$ASSIGN (&disk, &chan, 0, 0); /* open a channel to the disk */
4580 if (!stat)
4581 LIB$SIGNAL (stat);
4582 stat = SYS$QIOW (0, chan, IO$_MODIFY, iosb, 0, 0, &fib_d,
4583 0, 0, 0, &fib_attr, 0);
4584 if (!stat)
4585 LIB$SIGNAL (stat);
4586 stat = SYS$DASSGN (chan);
4587 if (!stat)
4588 LIB$SIGNAL (stat);
4589 strcpy (vms_file_written, to_esn); /* We will write this to the terminal*/
4590 return 0;
4593 link (file, new)
4594 char * file, * new;
4596 register status;
4597 struct FAB fab;
4598 struct NAM nam;
4599 unsigned short fid[3];
4600 char esa[NAM$C_MAXRSS];
4602 fab = cc$rms_fab;
4603 fab.fab$l_fop = FAB$M_OFP;
4604 fab.fab$l_fna = file;
4605 fab.fab$b_fns = strlen (file);
4606 fab.fab$l_nam = &nam;
4608 nam = cc$rms_nam;
4609 nam.nam$l_esa = esa;
4610 nam.nam$b_ess = NAM$C_MAXRSS;
4612 status = SYS$PARSE (&fab);
4613 if ((status & 1) == 0)
4615 errno = EVMSERR;
4616 vaxc$errno = status;
4617 return -1;
4619 status = SYS$SEARCH (&fab);
4620 if ((status & 1) == 0)
4622 errno = EVMSERR;
4623 vaxc$errno = status;
4624 return -1;
4627 fid[0] = nam.nam$w_fid[0];
4628 fid[1] = nam.nam$w_fid[1];
4629 fid[2] = nam.nam$w_fid[2];
4631 fab.fab$l_fna = new;
4632 fab.fab$b_fns = strlen (new);
4634 status = SYS$PARSE (&fab);
4635 if ((status & 1) == 0)
4637 errno = EVMSERR;
4638 vaxc$errno = status;
4639 return -1;
4642 nam.nam$w_fid[0] = fid[0];
4643 nam.nam$w_fid[1] = fid[1];
4644 nam.nam$w_fid[2] = fid[2];
4646 nam.nam$l_esa = nam.nam$l_name;
4647 nam.nam$b_esl = nam.nam$b_name + nam.nam$b_type + nam.nam$b_ver;
4649 status = SYS$ENTER (&fab);
4650 if ((status & 1) == 0)
4652 errno = EVMSERR;
4653 vaxc$errno = status;
4654 return -1;
4657 return 0;
4660 croak (badfunc)
4661 char *badfunc;
4663 printf ("%s not yet implemented\r\n", badfunc);
4664 reset_sys_modes ();
4665 exit (1);
4668 long
4669 random ()
4671 /* Arrange to return a range centered on zero. */
4672 return rand () - (1 << 30);
4675 srandom (seed)
4677 srand (seed);
4679 #endif /* VMS */
4681 #ifdef AIX
4683 /* Called from init_sys_modes. */
4684 hft_init ()
4686 int junk;
4688 /* If we're not on an HFT we shouldn't do any of this. We determine
4689 if we are on an HFT by trying to get an HFT error code. If this
4690 call fails, we're not on an HFT. */
4691 #ifdef IBMR2AIX
4692 if (ioctl (0, HFQERROR, &junk) < 0)
4693 return;
4694 #else /* not IBMR2AIX */
4695 if (ioctl (0, HFQEIO, 0) < 0)
4696 return;
4697 #endif /* not IBMR2AIX */
4699 /* On AIX the default hft keyboard mapping uses backspace rather than delete
4700 as the rubout key's ASCII code. Here this is changed. The bug is that
4701 there's no way to determine the old mapping, so in reset_sys_modes
4702 we need to assume that the normal map had been present. Of course, this
4703 code also doesn't help if on a terminal emulator which doesn't understand
4704 HFT VTD's. */
4706 struct hfbuf buf;
4707 struct hfkeymap keymap;
4709 buf.hf_bufp = (char *)&keymap;
4710 buf.hf_buflen = sizeof (keymap);
4711 keymap.hf_nkeys = 2;
4712 keymap.hfkey[0].hf_kpos = 15;
4713 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
4714 #ifdef IBMR2AIX
4715 keymap.hfkey[0].hf_keyidh = '<';
4716 #else /* not IBMR2AIX */
4717 keymap.hfkey[0].hf_page = '<';
4718 #endif /* not IBMR2AIX */
4719 keymap.hfkey[0].hf_char = 127;
4720 keymap.hfkey[1].hf_kpos = 15;
4721 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
4722 #ifdef IBMR2AIX
4723 keymap.hfkey[1].hf_keyidh = '<';
4724 #else /* not IBMR2AIX */
4725 keymap.hfkey[1].hf_page = '<';
4726 #endif /* not IBMR2AIX */
4727 keymap.hfkey[1].hf_char = 127;
4728 hftctl (0, HFSKBD, &buf);
4730 /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
4731 at times. */
4732 line_ins_del_ok = char_ins_del_ok = 0;
4735 /* Reset the rubout key to backspace. */
4737 hft_reset ()
4739 struct hfbuf buf;
4740 struct hfkeymap keymap;
4741 int junk;
4743 #ifdef IBMR2AIX
4744 if (ioctl (0, HFQERROR, &junk) < 0)
4745 return;
4746 #else /* not IBMR2AIX */
4747 if (ioctl (0, HFQEIO, 0) < 0)
4748 return;
4749 #endif /* not IBMR2AIX */
4751 buf.hf_bufp = (char *)&keymap;
4752 buf.hf_buflen = sizeof (keymap);
4753 keymap.hf_nkeys = 2;
4754 keymap.hfkey[0].hf_kpos = 15;
4755 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
4756 #ifdef IBMR2AIX
4757 keymap.hfkey[0].hf_keyidh = '<';
4758 #else /* not IBMR2AIX */
4759 keymap.hfkey[0].hf_page = '<';
4760 #endif /* not IBMR2AIX */
4761 keymap.hfkey[0].hf_char = 8;
4762 keymap.hfkey[1].hf_kpos = 15;
4763 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
4764 #ifdef IBMR2AIX
4765 keymap.hfkey[1].hf_keyidh = '<';
4766 #else /* not IBMR2AIX */
4767 keymap.hfkey[1].hf_page = '<';
4768 #endif /* not IBMR2AIX */
4769 keymap.hfkey[1].hf_char = 8;
4770 hftctl (0, HFSKBD, &buf);
4773 #endif /* AIX */