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