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