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