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