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