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