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