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