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