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