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