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