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