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