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