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