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