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