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