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