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