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