* configure.in: Use AC_LONG_FILE_NAMES.
[bpt/emacs.git] / src / sysdep.c
CommitLineData
86a5659e 1/* Interfaces to system-dependent kernel and library entries.
c6c5df7f 2 Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
86a5659e
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <signal.h>
22#include <setjmp.h>
23
24#include "config.h"
25#include "lisp.h"
9ac0d9e0 26#include "blockinput.h"
86a5659e
JB
27#undef NULL
28
29#define min(x,y) ((x) > (y) ? (y) : (x))
30
31/* In this file, open, read and write refer to the system calls,
32 not our sugared interfaces sys_open, sys_read and sys_write.
33 Contrariwise, for systems where we use the system calls directly,
34 define sys_read, etc. here as aliases for them. */
35#ifndef read
36#define sys_read read
37#define sys_write write
38#endif /* `read' is not a macro */
39
40#undef read
41#undef write
42
43#ifndef close
44#define sys_close close
45#else
46#undef close
47#endif
48
49#ifndef open
50#define sys_open open
51#else /* `open' is a macro */
52#undef open
53#endif /* `open' is a macro */
54
986ffb24
JB
55/* Does anyone other than VMS need this? */
56#ifndef fwrite
57#define sys_fwrite fwrite
58#else
59#undef fwrite
60#endif
61
86a5659e
JB
62#include <stdio.h>
63#include <sys/types.h>
64#include <sys/stat.h>
65#include <errno.h>
66
67extern int errno;
68#ifndef VMS
69extern char *sys_errlist[];
70#endif
71
72#ifdef VMS
73#include <rms.h>
74#include <ttdef.h>
75#include <tt2def.h>
76#include <iodef.h>
77#include <ssdef.h>
78#include <descrip.h>
79#include <fibdef.h>
80#include <atrdef.h>
81#include <ctype.h>
82#include <string.h>
83#ifdef __GNUC__
84#include <sys/file.h>
85#else
86#include <file.h>
87#endif
88#undef F_SETFL
89#ifndef RAB$C_BID
90#include <rab.h>
91#endif
92#define MAXIOSIZE ( 32 * PAGESIZE ) /* Don't I/O more than 32 blocks at a time */
93#endif /* VMS */
94
95#ifndef BSD4_1
96#ifdef BSD /* this is done this way to avoid defined (BSD) || defined (USG)
97 because the vms compiler doesn't grok `defined' */
98#include <fcntl.h>
99#endif
100#ifdef USG
34567704 101#ifndef USG5
86a5659e
JB
102#include <fcntl.h>
103#endif
34567704 104#endif
86a5659e
JB
105#endif /* not 4.1 bsd */
106
107/* Get DGUX definition for FASYNC - DJB */
108#ifdef DGUX
109#include <sys/file.h>
110#endif /* DGUX */
111
112#include <sys/ioctl.h>
e04a4e0d 113#include "systty.h"
86a5659e
JB
114
115#ifdef BSD
116#ifdef BSD4_1
117#include <wait.h>
118#else /* not 4.1 */
119#include <sys/wait.h>
120#endif /* not 4.1 */
121#endif /* BSD */
122
86a5659e
JB
123#ifdef BROKEN_TIOCGWINSZ
124#undef TIOCGWINSZ
125#endif
126
86a5659e
JB
127#ifdef USG
128#include <sys/utsname.h>
129#include <string.h>
130#ifndef MEMORY_IN_STRING_H
131#include <memory.h>
132#endif
133#ifdef TIOCGWINSZ
134#ifdef NEED_SIOCTL
135#include <sys/sioctl.h>
136#endif
137#ifdef NEED_PTEM_H
138#include <sys/stream.h>
139#include <sys/ptem.h>
140#endif
141#endif /* TIOCGWINSZ */
86a5659e
JB
142#endif /* USG */
143
86a5659e
JB
144extern int quit_char;
145
0137dbf7 146#include "frame.h"
86a5659e
JB
147#include "window.h"
148#include "termhooks.h"
149#include "termchar.h"
150#include "termopts.h"
151#include "dispextern.h"
152#include "process.h"
153
154#ifdef NONSYSTEM_DIR_LIBRARY
155#include "ndir.h"
156#endif /* NONSYSTEM_DIR_LIBRARY */
157
91bac16a
JB
158#include "syssignal.h"
159#include "systime.h"
86a5659e
JB
160
161static int baud_convert[] =
162#ifdef BAUD_CONVERT
163 BAUD_CONVERT;
164#else
165 {
166 0, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
167 1800, 2400, 4800, 9600, 19200, 38400
168 };
169#endif
170
171extern short ospeed;
172
91bac16a
JB
173/* The file descriptor for Emacs's input terminal.
174 Under Unix, this is always left zero;
175 under VMS, we place the input channel number here.
176 This allows us to write more code that works for both VMS and Unix. */
177static int input_fd;
178
86a5659e
JB
179discard_tty_input ()
180{
91bac16a 181 struct emacs_tty buf;
86a5659e
JB
182
183 if (noninteractive)
184 return;
185
186 /* Discarding input is not safe when the input could contain
187 replies from the X server. So don't do it. */
188 if (read_socket_hook)
189 return;
190
191#ifdef VMS
192 end_kbd_input ();
91bac16a
JB
193 SYS$QIOW (0, input_fd, IO$_READVBLK|IO$M_PURGE, input_iosb, 0, 0,
194 &buf.main, 0, 0, terminator_mask, 0, 0);
86a5659e
JB
195 queue_kbd_input ();
196#else /* not VMS */
197#ifdef APOLLO
198 {
199 int zero = 0;
200 ioctl (0, TIOCFLUSH, &zero);
201 }
202#else /* not Apollo */
91bac16a
JB
203 EMACS_GET_TTY (input_fd, &buf);
204 EMACS_SET_TTY (input_fd, &buf, 0);
86a5659e
JB
205#endif /* not Apollo */
206#endif /* not VMS */
207}
208
209#ifdef SIGTSTP
210
211stuff_char (c)
212 char c;
213{
214/* Should perhaps error if in batch mode */
215#ifdef TIOCSTI
216 ioctl (0, TIOCSTI, &c);
217#else /* no TIOCSTI */
218 error ("Cannot stuff terminal input characters in this version of Unix.");
219#endif /* no TIOCSTI */
220}
221
222#endif /* SIGTSTP */
223
224init_baud_rate ()
225{
86a5659e
JB
226 if (noninteractive)
227 ospeed = 0;
228 else
229 {
230#ifdef VMS
91bac16a
JB
231 struct sensemode sg;
232
233 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &sg, 0, 0,
86a5659e 234 &sg.class, 12, 0, 0, 0, 0 );
91bac16a
JB
235 ospeed = sg.xmit_baud;
236#else /* not VMS */
e04a4e0d
JB
237#ifdef HAVE_TERMIOS
238 struct termios sg;
91bac16a
JB
239
240 sg.c_cflag = (sg.c_cflag & ~CBAUD) | B9600;
241 tcgetattr (0, &sg);
d7272cff 242 ospeed = cfgetospeed (&sg);
e04a4e0d
JB
243#else /* neither VMS nor TERMIOS */
244#ifdef HAVE_TERMIO
245 struct termio sg;
91bac16a
JB
246
247 sg.c_cflag = (sg.c_cflag & ~CBAUD) | B9600;
e04a4e0d 248#ifdef HAVE_TCATTR
86a5659e 249 tcgetattr (0, &sg);
e04a4e0d 250#else
6c65530f 251 ioctl (input_fd, TCGETA, &sg);
e04a4e0d 252#endif
91bac16a 253 ospeed = sg.c_cflag & CBAUD;
e04a4e0d 254#else /* neither VMS nor TERMIOS nor TERMIO */
91bac16a
JB
255 struct sgttyb sg;
256
257 sg.sg_ospeed = B9600;
d7272cff
RS
258 if (ioctl (0, TIOCGETP, &sg) < 0)
259 abort ();
91bac16a 260 ospeed = sg.sg_ospeed;
91bac16a 261#endif /* not HAVE_TERMIO */
e04a4e0d 262#endif /* not HAVE_TERMIOS */
86a5659e 263#endif /* not VMS */
86a5659e
JB
264 }
265
266 baud_rate = (ospeed < sizeof baud_convert / sizeof baud_convert[0]
267 ? baud_convert[ospeed] : 9600);
268 if (baud_rate == 0)
269 baud_rate = 1200;
270}
271
272/*ARGSUSED*/
273set_exclusive_use (fd)
274 int fd;
275{
276#ifdef FIOCLEX
277 ioctl (fd, FIOCLEX, 0);
278#endif
279 /* Ok to do nothing if this feature does not exist */
280}
281
282#ifndef subprocesses
283
284wait_without_blocking ()
285{
286#ifdef BSD
287 wait3 (0, WNOHANG | WUNTRACED, 0);
288#else
289 croak ("wait_without_blocking");
290#endif
291 synch_process_alive = 0;
292}
293
294#endif /* not subprocesses */
295
296int wait_debugging; /* Set nonzero to make following function work under dbx
297 (at least for bsd). */
298
299SIGTYPE
300wait_for_termination_signal ()
301{}
302
303/* Wait for subprocess with process id `pid' to terminate and
304 make sure it will get eliminated (not remain forever as a zombie) */
305
306wait_for_termination (pid)
307 int pid;
308{
309 while (1)
310 {
311#ifdef subprocesses
312#ifdef VMS
313 int status;
314
986ffb24 315 status = SYS$FORCEX (&pid, 0, 0);
86a5659e
JB
316 break;
317#else /* not VMS */
05695621 318#if defined (BSD) || (defined (HPUX) && !defined (HPUX_5))
4c8975ad
RS
319 /* Note that kill returns -1 even if the process is just a zombie now.
320 But inevitably a SIGCHLD interrupt should be generated
321 and child_sig will do wait3 and make the process go away. */
322 /* There is some indication that there is a bug involved with
323 termination of subprocesses, perhaps involving a kernel bug too,
324 but no idea what it is. Just as a hunch we signal SIGCHLD to see
325 if that causes the problem to go away or get worse. */
326 sigsetmask (sigmask (SIGCHLD));
327 if (0 > kill (pid, 0))
328 {
329 sigsetmask (SIGEMPTYMASK);
330 kill (getpid (), SIGCHLD);
331 break;
332 }
333 if (wait_debugging)
334 sleep (1);
335 else
336 sigpause (SIGEMPTYMASK);
05695621
RS
337#else /* not BSD, and not HPUX version >= 6 */
338#if defined (UNIPLUS)
4c8975ad
RS
339 if (0 > kill (pid, 0))
340 break;
341 wait (0);
05695621 342#else /* neither BSD nor UNIPLUS: random sysV */
9ab714c7
RS
343#ifdef POSIX_SIGNALS /* would this work for LINUX as well? */
344 sigblock (sigmask (SIGCHLD));
345 if (0 > kill (pid, 0))
346 {
347 sigunblock (sigmask (SIGCHLD));
348 break;
349 }
92c995de 350 sigpause (SIGEMPTYMASK);
9ab714c7 351#else /* not POSIX_SIGNALS */
4c8975ad
RS
352#ifdef HAVE_SYSV_SIGPAUSE
353 sighold (SIGCHLD);
354 if (0 > kill (pid, 0))
355 {
356 sigrelse (SIGCHLD);
357 break;
358 }
359 sigpause (SIGCHLD);
360#else /* not HAVE_SYSV_SIGPAUSE */
361 if (0 > kill (pid, 0))
86a5659e 362 break;
4c8975ad
RS
363 /* Using sleep instead of pause avoids timing error.
364 If the inferior dies just before the sleep,
365 we lose just one second. */
366 sleep (1);
367#endif /* not HAVE_SYSV_SIGPAUSE */
9ab714c7 368#endif /* not POSIX_SIGNALS */
4c8975ad
RS
369#endif /* not UNIPLUS */
370#endif /* not BSD, and not HPUX version >= 6 */
86a5659e
JB
371#endif /* not VMS */
372#else /* not subprocesses */
373#ifndef BSD4_1
374 if (kill (pid, 0) < 0)
375 break;
376 wait (0);
377#else /* BSD4_1 */
378 int status;
379 status = wait (0);
380 if (status == pid || status == -1)
381 break;
382#endif /* BSD4_1 */
383#endif /* not subprocesses */
384 }
385}
386
387#ifdef subprocesses
388
389/*
390 * flush any pending output
391 * (may flush input as well; it does not matter the way we use it)
392 */
393
394flush_pending_output (channel)
395 int channel;
396{
397#ifdef HAVE_TERMIOS
398 /* If we try this, we get hit with SIGTTIN, because
399 the child's tty belongs to the child's pgrp. */
400#else
401#ifdef TCFLSH
402 ioctl (channel, TCFLSH, 1);
403#else
404#ifdef TIOCFLUSH
405 int zero = 0;
406 /* 3rd arg should be ignored
407 but some 4.2 kernels actually want the address of an int
408 and nonzero means something different. */
409 ioctl (channel, TIOCFLUSH, &zero);
410#endif
411#endif
412#endif
413}
414
415#ifndef VMS
416/* Set up the terminal at the other end of a pseudo-terminal that
417 we will be controlling an inferior through.
418 It should not echo or do line-editing, since that is done
419 in Emacs. No padding needed for insertion into an Emacs buffer. */
420
421child_setup_tty (out)
422 int out;
423{
91bac16a
JB
424 struct emacs_tty s;
425
426 EMACS_GET_TTY (out, &s);
86a5659e 427
31be8d24 428#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
91bac16a
JB
429 s.main.c_oflag |= OPOST; /* Enable output postprocessing */
430 s.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL on output */
431 s.main.c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY);
432 /* No output delays */
433 s.main.c_lflag &= ~ECHO; /* Disable echo */
434 s.main.c_lflag |= ISIG; /* Enable signals */
435 s.main.c_iflag &= ~IUCLC; /* Disable map of upper case to lower on
436 input */
437 s.main.c_oflag &= ~OLCUC; /* Disable map of lower case to upper on
438 output */
439#if 0
eb8c3be9 440 /* Said to be unnecessary: */
91bac16a
JB
441 s.main.c_cc[VMIN] = 1; /* minimum number of characters to accept */
442 s.main.c_cc[VTIME] = 0; /* wait forever for at least 1 character */
443#endif
444
445 s.main.c_lflag |= ICANON; /* Enable erase/kill and eof processing */
446 s.main.c_cc[VEOF] = 04; /* insure that EOF is Control-D */
447 s.main.c_cc[VERASE] = 0377; /* disable erase processing */
448 s.main.c_cc[VKILL] = 0377; /* disable kill processing */
449
86a5659e 450#ifdef HPUX
91bac16a 451 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
86a5659e 452#endif /* HPUX */
91bac16a 453
86a5659e
JB
454#ifdef AIX
455/* AIX enhanced edit loses NULs, so disable it */
456#ifndef IBMR2AIX
91bac16a
JB
457 s.main.c_line = 0;
458 s.main.c_iflag &= ~ASCEDIT;
86a5659e
JB
459#endif
460 /* Also, PTY overloads NUL and BREAK.
461 don't ignore break, but don't signal either, so it looks like NUL. */
91bac16a
JB
462 s.main.c_iflag &= ~IGNBRK;
463 s.main.c_iflag &= ~BRKINT;
464 /* QUIT and INTR work better as signals, so disable character forms */
465 s.main.c_cc[VQUIT] = 0377;
466 s.main.c_cc[VINTR] = 0377;
e6cc3307
RS
467#ifdef SIGNALS_VIA_CHARACTERS
468 /* the QUIT and INTR character are used in process_send_signal
469 so set them here to something useful. */
470 if (s.main.c_cc[VQUIT] == 0377)
471 s.main.c_cc[VQUIT] = '\\'&037; /* Control-\ */
472 if (s.main.c_cc[VINTR] == 0377)
473 s.main.c_cc[VINTR] = 'C'&037; /* Control-C */
474#else /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
475 /* QUIT and INTR work better as signals, so disable character forms */
476 s.main.c_cc[VQUIT] = 0377;
477 s.main.c_cc[VINTR] = 0377;
91bac16a 478 s.main.c_lflag &= ~ISIG;
e6cc3307
RS
479#endif /* no TIOCGPGRP or no TIOCGLTC or no TIOCGETC */
480 s.main.c_cc[VEOL] = 0377;
91bac16a 481 s.main.c_cflag = (s.main.c_cflag & ~CBAUD) | B9600; /* baud rate sanity */
86a5659e
JB
482#endif /* AIX */
483
484#else /* not HAVE_TERMIO */
91bac16a
JB
485
486 s.main.sg_flags &= ~(ECHO | CRMOD | ANYP | ALLDELAY | RAW | LCASE
487 | CBREAK | TANDEM);
488 s.main.sg_erase = 0377;
489 s.main.sg_kill = 0377;
490
86a5659e
JB
491#endif /* not HAVE_TERMIO */
492
91bac16a 493 EMACS_SET_TTY (out, &s, 0);
86a5659e
JB
494
495#ifdef BSD4_1
496 if (interrupt_input)
497 reset_sigio ();
498#endif /* BSD4_1 */
499#ifdef RTU
500 {
501 int zero = 0;
502 ioctl (out, FIOASYNC, &zero);
503 }
504#endif /* RTU */
505}
506#endif /* not VMS */
507
508#endif /* subprocesses */
509
510/*ARGSUSED*/
511setpgrp_of_tty (pid)
512 int pid;
513{
ffd56f97 514 EMACS_SET_TTY_PGRP (input_fd, &pid);
86a5659e
JB
515}
516
517/* Record a signal code and the handler for it. */
518struct save_signal
519{
520 int code;
521 SIGTYPE (*handler) ();
522};
523
524/* Suspend the Emacs process; give terminal to its superior. */
525
526sys_suspend ()
527{
528#ifdef VMS
88191e36
RS
529 /* "Foster" parentage allows emacs to return to a subprocess that attached
530 to the current emacs as a cheaper than starting a whole new process. This
531 is set up by KEPTEDITOR.COM. */
532 unsigned long parent_id, foster_parent_id;
533 char *fpid_string;
534
535 fpid_string = getenv ("EMACS_PARENT_PID");
536 if (fpid_string != NULL)
537 {
538 sscanf (fpid_string, "%x", &foster_parent_id);
539 if (foster_parent_id != 0)
540 parent_id = foster_parent_id;
541 else
542 parent_id = getppid ();
543 }
544 else
545 parent_id = getppid ();
546
9ac0d9e0 547 xfree (fpid_string); /* On VMS, this was malloc'd */
86a5659e 548
86a5659e
JB
549 if (parent_id && parent_id != 0xffffffff)
550 {
551 SIGTYPE (*oldsig)() = (int) signal (SIGINT, SIG_IGN);
552 int status = LIB$ATTACH (&parent_id) & 1;
553 signal (SIGINT, oldsig);
554 return status;
555 }
556 else
557 {
558 struct {
559 int l;
560 char *a;
561 } d_prompt;
562 d_prompt.l = sizeof ("Emacs: "); /* Our special prompt */
563 d_prompt.a = "Emacs: "; /* Just a reminder */
986ffb24 564 LIB$SPAWN (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, &d_prompt, 0);
86a5659e
JB
565 return 1;
566 }
567 return -1;
568#else
569#ifdef SIGTSTP
570
92c995de
RS
571#ifdef GETPGRP_NO_ARG
572 EMACS_KILLPG (getpgrp (), SIGTSTP);
573#else
91bac16a 574 EMACS_KILLPG (getpgrp (0), SIGTSTP);
92c995de 575#endif
86a5659e
JB
576
577#else /* No SIGTSTP */
578#ifdef USG_JOBCTRL /* If you don't know what this is don't mess with it */
579 ptrace (0, 0, 0, 0); /* set for ptrace - caught by csh */
580 kill (getpid (), SIGQUIT);
581
582#else /* No SIGTSTP or USG_JOBCTRL */
583
584/* On a system where suspending is not implemented,
585 instead fork a subshell and let it talk directly to the terminal
586 while we wait. */
587 int pid = fork ();
588 struct save_signal saved_handlers[5];
589
590 saved_handlers[0].code = SIGINT;
591 saved_handlers[1].code = SIGQUIT;
592 saved_handlers[2].code = SIGTERM;
593#ifdef SIGIO
594 saved_handlers[3].code = SIGIO;
595 saved_handlers[4].code = 0;
596#else
597 saved_handlers[3].code = 0;
598#endif
599
600 if (pid == -1)
601 error ("Can't spawn subshell");
602 if (pid == 0)
603 {
604 char *sh;
605
606 sh = (char *) egetenv ("SHELL");
607 if (sh == 0)
608 sh = "sh";
609 /* Use our buffer's default directory for the subshell. */
610 {
611 Lisp_Object dir;
612 unsigned char *str;
613 int len;
614
615 /* mentioning current_buffer->buffer would mean including buffer.h,
616 which somehow wedges the hp compiler. So instead... */
617
618 dir = intern ("default-directory");
619 /* Can't use NULL */
620 if (XFASTINT (Fboundp (dir)) == XFASTINT (Qnil))
621 goto xyzzy;
622 dir = Fsymbol_value (dir);
623 if (XTYPE (dir) != Lisp_String)
624 goto xyzzy;
625
626 str = (unsigned char *) alloca (XSTRING (dir)->size + 2);
627 len = XSTRING (dir)->size;
628 bcopy (XSTRING (dir)->data, str, len);
629 if (str[len - 1] != '/') str[len++] = '/';
630 str[len] = 0;
631 chdir (str);
632 }
633 xyzzy:
634#ifdef subprocesses
635 close_process_descs (); /* Close Emacs's pipes/ptys */
636#endif
1593c2fe
JB
637
638#ifdef PRIO_PROCESS
639 {
640 extern int emacs_priority;
641
642 if (emacs_priority)
643 nice (-emacs_priority);
644 }
645#endif
646
86a5659e
JB
647 execlp (sh, sh, 0);
648 write (1, "Can't execute subshell", 22);
649 _exit (1);
650 }
651
652 save_signal_handlers (saved_handlers);
ffafc793 653 synch_process_alive = 1;
86a5659e
JB
654 wait_for_termination (pid);
655 restore_signal_handlers (saved_handlers);
656
657#endif /* no USG_JOBCTRL */
658#endif /* no SIGTSTP */
659#endif /* not VMS */
660}
661
662save_signal_handlers (saved_handlers)
663 struct save_signal *saved_handlers;
664{
665 while (saved_handlers->code)
666 {
508b171c
JA
667 saved_handlers->handler
668 = (SIGTYPE (*) ()) signal (saved_handlers->code, SIG_IGN);
86a5659e
JB
669 saved_handlers++;
670 }
671}
672
673restore_signal_handlers (saved_handlers)
674 struct save_signal *saved_handlers;
675{
676 while (saved_handlers->code)
677 {
678 signal (saved_handlers->code, saved_handlers->handler);
679 saved_handlers++;
680 }
681}
682\f
683#ifdef F_SETFL
684
685int old_fcntl_flags;
686
687init_sigio ()
688{
689#ifdef FASYNC
690 old_fcntl_flags = fcntl (0, F_GETFL, 0) & ~FASYNC;
691#endif
692 request_sigio ();
693}
694
695reset_sigio ()
696{
697 unrequest_sigio ();
698}
699
eb8c3be9 700#ifdef FASYNC /* F_SETFL does not imply existence of FASYNC */
86a5659e
JB
701
702request_sigio ()
703{
704#ifdef SIGWINCH
e065a56e 705 sigunblock (sigmask (SIGWINCH));
86a5659e
JB
706#endif
707 fcntl (0, F_SETFL, old_fcntl_flags | FASYNC);
708
709 interrupts_deferred = 0;
710}
711
712unrequest_sigio ()
713{
714#ifdef SIGWINCH
e065a56e 715 sigblock (sigmask (SIGWINCH));
86a5659e
JB
716#endif
717 fcntl (0, F_SETFL, old_fcntl_flags);
718 interrupts_deferred = 1;
719}
720
721#else /* no FASYNC */
722#ifdef STRIDE /* Stride doesn't have FASYNC - use FIOASYNC */
723
724request_sigio ()
725{
726 int on = 1;
727 ioctl (0, FIOASYNC, &on);
728 interrupts_deferred = 0;
729}
730
731unrequest_sigio ()
732{
733 int off = 0;
734
735 ioctl (0, FIOASYNC, &off);
736 interrupts_deferred = 1;
737}
738
739#else /* not FASYNC, not STRIDE */
740
741request_sigio ()
742{
743 croak ("request_sigio");
744}
745
746unrequest_sigio ()
747{
748 croak ("unrequest_sigio");
749}
750
751#endif /* STRIDE */
752#endif /* FASYNC */
753#endif /* F_SETFL */
754\f
68936329
JB
755/* Getting and setting emacs_tty structures. */
756
757/* Set *TC to the parameters associated with the terminal FD.
758 Return zero if all's well, or -1 if we ran into an error we
759 couldn't deal with. */
760int
761emacs_get_tty (fd, settings)
762 int fd;
763 struct emacs_tty *settings;
764{
765 /* Retrieve the primary parameters - baud rate, character size, etcetera. */
766#ifdef HAVE_TCATTR
767 /* We have those nifty POSIX tcmumbleattr functions. */
768 if (tcgetattr (fd, &settings->main) < 0)
769 return -1;
770
771#else
772#ifdef HAVE_TERMIO
773 /* The SYSV-style interface? */
774 if (ioctl (fd, TCGETA, &settings->main) < 0)
775 return -1;
776
777#else
778#ifdef VMS
779 /* Vehemently Monstrous System? :-) */
780 if (! (SYS$QIOW (0, fd, IO$_SENSEMODE, settings, 0, 0,
781 &settings->main.class, 12, 0, 0, 0, 0)
782 & 1))
783 return -1;
784
785#else
786 /* I give up - I hope you have the BSD ioctls. */
787 if (ioctl (fd, TIOCGETP, &settings->main) < 0)
788 return -1;
789
790#endif
791#endif
792#endif
793
794 /* Suivant - Do we have to get struct ltchars data? */
50b8cf60 795#ifdef HAVE_LTCHARS
68936329
JB
796 if (ioctl (fd, TIOCGLTC, &settings->ltchars) < 0)
797 return -1;
798#endif
799
800 /* How about a struct tchars and a wordful of lmode bits? */
50b8cf60 801#ifdef HAVE_TCHARS
68936329
JB
802 if (ioctl (fd, TIOCGETC, &settings->tchars) < 0
803 || ioctl (fd, TIOCLGET, &settings->lmode) < 0)
804 return -1;
805#endif
806
807 /* We have survived the tempest. */
808 return 0;
809}
810
811
812/* Set the parameters of the tty on FD according to the contents of
813 *SETTINGS. If WAITP is non-zero, we wait for all queued output to
814 be written before making the change; otherwise, we forget any
815 queued input and make the change immediately.
816 Return 0 if all went well, and -1 if anything failed. */
817int
818emacs_set_tty (fd, settings, waitp)
819 int fd;
820 struct emacs_tty *settings;
821 int waitp;
822{
823 /* Set the primary parameters - baud rate, character size, etcetera. */
824#ifdef HAVE_TCATTR
e6cc3307 825 int i;
68936329
JB
826 /* We have those nifty POSIX tcmumbleattr functions.
827 William J. Smith <wjs@wiis.wang.com> writes:
828 "POSIX 1003.1 defines tcsetattr() to return success if it was
829 able to perform any of the requested actions, even if some
830 of the requested actions could not be performed.
831 We must read settings back to ensure tty setup properly.
832 AIX requires this to keep tty from hanging occasionally." */
eb8c3be9 833 /* This make sure that we don't loop indefinitely in here. */
e6cc3307 834 for (i = 0 ; i < 10 ; i++)
68936329
JB
835 if (tcsetattr (fd, waitp ? TCSAFLUSH : TCSADRAIN, &settings->main) < 0)
836 {
837 if (errno == EINTR)
838 continue;
839 else
840 return -1;
841 }
842 else
843 {
844 struct termios new;
845
846 /* Get the current settings, and see if they're what we asked for. */
847 tcgetattr (fd, &new);
e6cc3307
RS
848 /* We cannot use memcmp on the whole structure here because under
849 * aix386 the termios structure has some reserved field that may
850 * not be filled in.
851 */
852 if ( new.c_iflag == settings->main.c_iflag
853 && new.c_oflag == settings->main.c_oflag
854 && new.c_cflag == settings->main.c_cflag
855 && new.c_lflag == settings->main.c_lflag
856 && memcmp(new.c_cc, settings->main.c_cc, NCCS) == 0)
68936329 857 break;
e6cc3307
RS
858 else
859 continue;
68936329
JB
860 }
861
862#else
863#ifdef HAVE_TERMIO
864 /* The SYSV-style interface? */
865 if (ioctl (fd, waitp ? TCSETAW : TCSETAF, &settings->main) < 0)
866 return -1;
867
868#else
869#ifdef VMS
870 /* Vehemently Monstrous System? :-) */
871 if (! (SYS$QIOW (0, fd, IO$_SETMODE, &input_iosb, 0, 0,
872 &settings->main.class, 12, 0, 0, 0, 0)
873 & 1))
874 return -1;
875
876#else
877 /* I give up - I hope you have the BSD ioctls. */
878 if (ioctl (fd, (waitp) ? TIOCSETP : TIOCSETN, &settings->main) < 0)
879 return -1;
880
881#endif
882#endif
883#endif
884
885 /* Suivant - Do we have to get struct ltchars data? */
50b8cf60 886#ifdef HAVE_LTCHARS
68936329
JB
887 if (ioctl (fd, TIOCSLTC, &settings->ltchars) < 0)
888 return -1;
889#endif
890
891 /* How about a struct tchars and a wordful of lmode bits? */
50b8cf60 892#ifdef HAVE_TCHARS
68936329
JB
893 if (ioctl (fd, TIOCSETC, &settings->tchars) < 0
894 || ioctl (fd, TIOCLSET, &settings->lmode) < 0)
895 return -1;
896#endif
897
898 /* We have survived the tempest. */
899 return 0;
900}
901
902\f
91bac16a
JB
903/* The initial tty mode bits */
904struct emacs_tty old_tty;
86a5659e
JB
905
906int term_initted; /* 1 if outer tty status has been recorded */
907
91bac16a
JB
908#ifdef BSD4_1
909/* BSD 4.1 needs to keep track of the lmode bits in order to start
910 sigio. */
911int lmode;
912#endif
913
86a5659e
JB
914#ifdef F_SETOWN
915int old_fcntl_owner;
916#endif /* F_SETOWN */
917
86a5659e
JB
918/* This may also be defined in stdio,
919 but if so, this does no harm,
920 and using the same name avoids wasting the other one's space. */
921
922#if defined (USG) || defined (DGUX)
923unsigned char _sobuf[BUFSIZ+8];
924#else
925char _sobuf[BUFSIZ];
926#endif
927
50b8cf60 928#ifdef HAVE_LTCHARS
86a5659e
JB
929static struct ltchars new_ltchars = {-1,-1,-1,-1,-1,-1};
930#endif
50b8cf60 931#ifdef HAVE_TCHARS
86a5659e
JB
932 static struct tchars new_tchars = {-1,-1,-1,-1,-1,-1};
933#endif
934
935init_sys_modes ()
936{
91bac16a
JB
937 struct emacs_tty tty;
938
86a5659e
JB
939#ifdef VMS
940#if 0
941 static int oob_chars[2] = {0, 1 << 7}; /* catch C-g's */
942 extern int (*interrupt_signal) ();
943#endif
944#endif
945
946 if (noninteractive)
947 return;
948
949#ifdef VMS
950 if (!input_ef)
951 input_ef = get_kbd_event_flag ();
952 /* LIB$GET_EF (&input_ef); */
953 SYS$CLREF (input_ef);
954 waiting_for_ast = 0;
955 if (!timer_ef)
956 timer_ef = get_timer_event_flag ();
957 /* LIB$GET_EF (&timer_ef); */
958 SYS$CLREF (timer_ef);
210b2b4f 959#if 0
86a5659e
JB
960 if (!process_ef)
961 {
962 LIB$GET_EF (&process_ef);
963 SYS$CLREF (process_ef);
964 }
965 if (input_ef / 32 != process_ef / 32)
966 croak ("Input and process event flags in different clusters.");
210b2b4f 967#endif
86a5659e 968 if (input_ef / 32 != timer_ef / 32)
210b2b4f
JB
969 croak ("Input and timer event flags in different clusters.");
970#if 0
86a5659e
JB
971 input_eflist = ((unsigned) 1 << (input_ef % 32)) |
972 ((unsigned) 1 << (process_ef % 32));
210b2b4f 973#endif
86a5659e
JB
974 timer_eflist = ((unsigned) 1 << (input_ef % 32)) |
975 ((unsigned) 1 << (timer_ef % 32));
86a5659e
JB
976#ifndef VMS4_4
977 sys_access_reinit ();
978#endif
86a5659e 979#endif /* not VMS */
91bac16a
JB
980
981 EMACS_GET_TTY (input_fd, &old_tty);
982
86a5659e
JB
983 if (!read_socket_hook && EQ (Vwindow_system, Qnil))
984 {
91bac16a 985 tty = old_tty;
86a5659e 986
31be8d24 987#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
91bac16a
JB
988 tty.main.c_iflag |= (IGNBRK); /* Ignore break condition */
989 tty.main.c_iflag &= ~ICRNL; /* Disable map of CR to NL on input */
86a5659e 990#ifdef ISTRIP
91bac16a 991 tty.main.c_iflag &= ~ISTRIP; /* don't strip 8th bit on input */
86a5659e 992#endif
91bac16a
JB
993 tty.main.c_lflag &= ~ECHO; /* Disable echo */
994 tty.main.c_lflag &= ~ICANON; /* Disable erase/kill processing */
e2b40c23
RS
995#ifdef IEXTEN
996 tty.main.c_iflag &= ~IEXTEN; /* Disable other editing characters. */
997#endif
91bac16a 998 tty.main.c_lflag |= ISIG; /* Enable signals */
86a5659e
JB
999 if (flow_control)
1000 {
91bac16a 1001 tty.main.c_iflag |= IXON; /* Enable start/stop output control */
86a5659e 1002#ifdef IXANY
91bac16a 1003 tty.main.c_iflag &= ~IXANY;
86a5659e
JB
1004#endif /* IXANY */
1005 }
1006 else
91bac16a
JB
1007 tty.main.c_iflag &= ~IXON; /* Disable start/stop output control */
1008 tty.main.c_oflag &= ~ONLCR; /* Disable map of NL to CR-NL
1009 on output */
1010 tty.main.c_oflag &= ~TAB3; /* Disable tab expansion */
86a5659e
JB
1011#ifdef CS8
1012 if (meta_key)
1013 {
91bac16a
JB
1014 tty.main.c_cflag |= CS8; /* allow 8th bit on input */
1015 tty.main.c_cflag &= ~PARENB;/* Don't check parity */
86a5659e
JB
1016 }
1017#endif
91bac16a 1018 tty.main.c_cc[VINTR] = quit_char; /* C-g (usually) gives SIGINT */
86a5659e
JB
1019 /* Set up C-g for both SIGQUIT and SIGINT.
1020 We don't know which we will get, but we handle both alike
1021 so which one it really gives us does not matter. */
91bac16a
JB
1022 tty.main.c_cc[VQUIT] = quit_char;
1023 tty.main.c_cc[VMIN] = 1; /* Input should wait for at least 1 char */
1024 tty.main.c_cc[VTIME] = 0; /* no matter how long that takes. */
86a5659e 1025#ifdef VSWTCH
e2b40c23 1026 tty.main.c_cc[VSWTCH] = CDISABLE; /* Turn off shell layering use
91bac16a 1027 of C-z */
86a5659e
JB
1028#endif /* VSWTCH */
1029#if defined (mips) || defined (HAVE_TCATTR)
86a5659e 1030#ifdef VSUSP
e2b40c23 1031 tty.main.c_cc[VSUSP] = CDISABLE; /* Turn off mips handling of C-z. */
86a5659e
JB
1032#endif /* VSUSP */
1033#ifdef V_DSUSP
e2b40c23 1034 tty.main.c_cc[V_DSUSP] = CDISABLE; /* Turn off mips handling of C-y. */
86a5659e 1035#endif /* V_DSUSP */
e2b40c23
RS
1036#ifdef VDSUSP /* Some systems have VDSUSP, some have V_DSUSP. */
1037 tty.main.c_cc[VDSUSP] = CDISABLE;
1038#endif /* VDSUSP */
92c995de
RS
1039#ifdef VLNEXT
1040 tty.main.c_cc[VLNEXT] = CDISABLE;
1041#endif /* VLNEXT */
1042#ifdef VREPRINT
1043 tty.main.c_cc[VREPRINT] = CDISABLE;
1044#endif /* VREPRINT */
1045#ifdef VWERASE
1046 tty.main.c_cc[VWERASE] = CDISABLE;
1047#endif /* VWERASE */
1048#ifdef VDISCARD
1049 tty.main.c_cc[VDISCARD] = CDISABLE;
1050#endif /* VDISCARD */
86a5659e
JB
1051#endif /* mips or HAVE_TCATTR */
1052#ifdef AIX
1053#ifndef IBMR2AIX
1054 /* AIX enhanced edit loses NULs, so disable it */
91bac16a
JB
1055 tty.main.c_line = 0;
1056 tty.main.c_iflag &= ~ASCEDIT;
86a5659e 1057#else
91bac16a
JB
1058 tty.main.c_cc[VSTRT] = 255;
1059 tty.main.c_cc[VSTOP] = 255;
1060 tty.main.c_cc[VSUSP] = 255;
1061 tty.main.c_cc[VDSUSP] = 255;
86a5659e
JB
1062#endif /* IBMR2AIX */
1063 /* Also, PTY overloads NUL and BREAK.
1064 don't ignore break, but don't signal either, so it looks like NUL.
1065 This really serves a purpose only if running in an XTERM window
1066 or via TELNET or the like, but does no harm elsewhere. */
91bac16a
JB
1067 tty.main.c_iflag &= ~IGNBRK;
1068 tty.main.c_iflag &= ~BRKINT;
86a5659e
JB
1069#endif
1070#else /* if not HAVE_TERMIO */
1071#ifdef VMS
91bac16a 1072 tty.main.tt_char |= TT$M_NOECHO;
86a5659e 1073 if (meta_key)
986ffb24 1074 tty.main.tt_char |= TT$M_EIGHTBIT;
86a5659e 1075 if (flow_control)
91bac16a 1076 tty.main.tt_char |= TT$M_TTSYNC;
86a5659e 1077 else
91bac16a
JB
1078 tty.main.tt_char &= ~TT$M_TTSYNC;
1079 tty.main.tt2_char |= TT2$M_PASTHRU | TT2$M_XON;
86a5659e 1080#else /* not VMS (BSD, that is) */
91bac16a 1081 tty.main.sg_flags &= ~(ECHO | CRMOD | XTABS);
86a5659e 1082 if (meta_key)
91bac16a
JB
1083 tty.main.sg_flags |= ANYP;
1084 tty.main.sg_flags |= interrupt_input ? RAW : CBREAK;
86a5659e
JB
1085#endif /* not VMS (BSD, that is) */
1086#endif /* not HAVE_TERMIO */
1087
91bac16a
JB
1088 /* If going to use CBREAK mode, we must request C-g to interrupt
1089 and turn off start and stop chars, etc. If not going to use
1090 CBREAK mode, do this anyway so as to turn off local flow
1091 control for user coming over network on 4.2; in this case,
1092 only t_stopc and t_startc really matter. */
1093#ifndef HAVE_TERMIO
50b8cf60 1094#ifdef HAVE_TCHARS
91bac16a
JB
1095 /* Note: if not using CBREAK mode, it makes no difference how we
1096 set this */
1097 tty.tchars = new_tchars;
1098 tty.tchars.t_intrc = quit_char;
1099 if (flow_control)
1100 {
1101 tty.tchars.t_startc = '\021';
1102 tty.tchars.t_stopc = '\023';
1103 }
1104
1105/* LPASS8 is new in 4.3, and makes cbreak mode provide all 8 bits. */
1106#ifndef LPASS8
1107#define LPASS8 0
86a5659e 1108#endif
91bac16a
JB
1109
1110#ifdef BSD4_1
1111#define LNOFLSH 0100000
1112#endif
1113
1114 tty.lmode = LDECCTQ | LLITOUT | LPASS8 | LNOFLSH | old_tty.lmode;
1115
1116#ifdef BSD4_1
1117 lmode = tty.lmode;
1118#endif
1119
50b8cf60 1120#endif /* HAVE_TCHARS */
91bac16a
JB
1121#endif /* not HAVE_TERMIO */
1122
50b8cf60 1123#ifdef HAVE_LTCHARS
91bac16a 1124 tty.ltchars = new_ltchars;
50b8cf60 1125#endif /* HAVE_LTCHARS */
91bac16a
JB
1126
1127 EMACS_SET_TTY (input_fd, &tty, 0);
86a5659e
JB
1128
1129 /* This code added to insure that, if flow-control is not to be used,
0137dbf7 1130 we have an unlocked terminal at the start. */
91bac16a 1131
86a5659e
JB
1132#ifdef TCXONC
1133 if (!flow_control) ioctl (0, TCXONC, 1);
1134#endif
1135#ifndef APOLLO
1136#ifdef TIOCSTART
1137 if (!flow_control) ioctl (0, TIOCSTART, 0);
1138#endif
1139#endif
1140
1141#ifdef AIX
1142 hft_init ();
1143#ifdef IBMR2AIX
1144 {
1145 /* IBM's HFT device usually thinks a ^J should be LF/CR. We need it
1146 to be only LF. This is the way that is done. */
1147 struct termio tty;
1148
1149 if (ioctl (1, HFTGETID, &tty) != -1)
1150 write (1, "\033[20l", 5);
1151 }
1152#endif
1153#endif
1154
86a5659e
JB
1155#ifdef VMS
1156/* Appears to do nothing when in PASTHRU mode.
91bac16a 1157 SYS$QIOW (0, input_fd, IO$_SETMODE|IO$M_OUTBAND, 0, 0, 0,
86a5659e
JB
1158 interrupt_signal, oob_chars, 0, 0, 0, 0);
1159*/
1160 queue_kbd_input (0);
1161#endif /* VMS */
1162 }
1163
1164#ifdef F_SETFL
eb8c3be9 1165#ifdef F_GETOWN /* F_SETFL does not imply existence of F_GETOWN */
86a5659e
JB
1166 if (interrupt_input)
1167 {
1168 old_fcntl_owner = fcntl (0, F_GETOWN, 0);
1169 fcntl (0, F_SETOWN, getpid ());
1170 init_sigio ();
1171 }
1172#endif /* F_GETOWN */
1173#endif /* F_SETFL */
1174
1175#ifdef BSD4_1
1176 if (interrupt_input)
1177 init_sigio ();
1178#endif
1179
1180#ifdef VMS /* VMS sometimes has this symbol but lacks setvbuf. */
1181#undef _IOFBF
1182#endif
1183#ifdef _IOFBF
1184 /* This symbol is defined on recent USG systems.
1185 Someone says without this call USG won't really buffer the file
1186 even with a call to setbuf. */
1187 setvbuf (stdout, _sobuf, _IOFBF, sizeof _sobuf);
1188#else
1189 setbuf (stdout, _sobuf);
1190#endif
1191 set_terminal_modes ();
1192 if (term_initted && no_redraw_on_reenter)
1193 {
1194 if (display_completed)
1195 direct_output_forward_char (0);
1196 }
1197 else
1198 {
0137dbf7
JB
1199 frame_garbaged = 1;
1200#ifdef MULTI_FRAME
1201 if (FRAMEP (Vterminal_frame))
1202 FRAME_GARBAGED_P (XFRAME (Vterminal_frame)) = 1;
86a5659e
JB
1203#endif
1204 }
91bac16a 1205
86a5659e
JB
1206 term_initted = 1;
1207}
1208
1209/* Return nonzero if safe to use tabs in output.
1210 At the time this is called, init_sys_modes has not been done yet. */
1211
1212tabs_safe_p ()
1213{
91bac16a
JB
1214 struct emacs_tty tty;
1215
1216 EMACS_GET_TTY (input_fd, &tty);
1217 return EMACS_TTY_TABS_OK (&tty);
86a5659e
JB
1218}
1219
1220/* Get terminal size from system.
1221 Store number of lines into *heightp and width into *widthp.
1222 If zero or a negative number is stored, the value is not valid. */
1223
0137dbf7 1224get_frame_size (widthp, heightp)
86a5659e
JB
1225 int *widthp, *heightp;
1226{
86a5659e 1227
86a5659e 1228#ifdef TIOCGWINSZ
91bac16a
JB
1229
1230 /* BSD-style. */
86a5659e 1231 struct winsize size;
91bac16a
JB
1232
1233 if (ioctl (input_fd, TIOCGWINSZ, &size) == -1)
1234 *widthp = *heightp = 0;
1235 else
1236 {
1237 *widthp = size.ws_col;
1238 *heightp = size.ws_row;
1239 }
1240
1241#else
1242#ifdef TIOCGSIZE
1243
1244 /* SunOS - style. */
1245 struct ttysize size;
1246
1247 if (ioctl (input_fd, TIOCGSIZE, &size) == -1)
1248 *widthp = *heightp = 0;
1249 else
1250 {
1251 *widthp = size.ts_cols;
1252 *heightp = size.ts_lines;
1253 }
1254
1255#else
86a5659e 1256#ifdef VMS
91bac16a
JB
1257
1258 struct sensemode tty;
1259
1260 SYS$QIOW (0, input_fd, IO$_SENSEMODE, &tty, 0, 0,
86a5659e
JB
1261 &tty.class, 12, 0, 0, 0, 0);
1262 *widthp = tty.scr_wid;
1263 *heightp = tty.scr_len;
91bac16a 1264
86a5659e 1265#else /* system doesn't know size */
91bac16a 1266
86a5659e
JB
1267 *widthp = 0;
1268 *heightp = 0;
91bac16a
JB
1269
1270#endif /* not VMS */
1271#endif /* not SunOS-style */
1272#endif /* not BSD-style */
86a5659e 1273}
91bac16a 1274
86a5659e 1275\f
91bac16a 1276/* Prepare the terminal for exiting Emacs; move the cursor to the
0137dbf7 1277 bottom of the frame, turn off interrupt-driven I/O, etc. */
86a5659e
JB
1278reset_sys_modes ()
1279{
1280 if (noninteractive)
1281 {
1282 fflush (stdout);
1283 return;
1284 }
1285 if (!term_initted)
1286 return;
1287 if (read_socket_hook || !EQ (Vwindow_system, Qnil))
1288 return;
0137dbf7
JB
1289 cursor_to (FRAME_HEIGHT (selected_frame) - 1, 0);
1290 clear_end_of_line (FRAME_WIDTH (selected_frame));
86a5659e 1291 /* clear_end_of_line may move the cursor */
0137dbf7 1292 cursor_to (FRAME_HEIGHT (selected_frame) - 1, 0);
86a5659e
JB
1293#ifdef IBMR2AIX
1294 {
1295 /* HFT devices normally use ^J as a LF/CR. We forced it to
1296 do the LF only. Now, we need to reset it. */
1297 struct termio tty;
1298
1299 if (ioctl (1, HFTGETID, &tty) != -1)
1300 write (1, "\033[20h", 5);
1301 }
1302#endif
1303
1304 reset_terminal_modes ();
1305 fflush (stdout);
1306#ifdef BSD
1307#ifndef BSD4_1
1308 /* Avoid possible loss of output when changing terminal modes. */
1309 fsync (fileno (stdout));
1310#endif
1311#endif
91bac16a 1312
86a5659e 1313#ifdef F_SETFL
eb8c3be9 1314#ifdef F_SETOWN /* F_SETFL does not imply existence of F_SETOWN */
86a5659e
JB
1315 if (interrupt_input)
1316 {
1317 reset_sigio ();
1318 fcntl (0, F_SETOWN, old_fcntl_owner);
1319 }
1320#endif /* F_SETOWN */
1321#endif /* F_SETFL */
1322#ifdef BSD4_1
1323 if (interrupt_input)
1324 reset_sigio ();
1325#endif /* BSD4_1 */
91bac16a 1326
1334b321 1327 while (EMACS_SET_TTY (input_fd, &old_tty, 0) < 0 && errno == EINTR)
91bac16a 1328 ;
86a5659e
JB
1329
1330#ifdef AIX
1331 hft_reset ();
1332#endif
1333}
1334\f
1335#ifdef HAVE_PTYS
1336
1337/* Set up the proper status flags for use of a pty. */
1338
1339setup_pty (fd)
1340 int fd;
1341{
1342 /* I'm told that TOICREMOTE does not mean control chars
1343 "can't be sent" but rather that they don't have
1344 input-editing or signaling effects.
1345 That should be good, because we have other ways
1346 to do those things in Emacs.
1347 However, telnet mode seems not to work on 4.2.
1348 So TIOCREMOTE is turned off now. */
1349
1350 /* Under hp-ux, if TIOCREMOTE is turned on, some calls
1351 will hang. In particular, the "timeout" feature (which
1352 causes a read to return if there is no data available)
1353 does this. Also it is known that telnet mode will hang
1354 in such a way that Emacs must be stopped (perhaps this
1355 is the same problem).
1356
1357 If TIOCREMOTE is turned off, then there is a bug in
1358 hp-ux which sometimes loses data. Apparently the
1359 code which blocks the master process when the internal
1360 buffer fills up does not work. Other than this,
1361 though, everything else seems to work fine.
1362
1363 Since the latter lossage is more benign, we may as well
1364 lose that way. -- cph */
1365#ifdef FIONBIO
1366#ifdef SYSV_PTYS
1367 {
1368 int on = 1;
1369 ioctl (fd, FIONBIO, &on);
1370 }
1371#endif
1372#endif
1373#ifdef IBMRTAIX
1374 /* On AIX, the parent gets SIGHUP when a pty attached child dies. So, we */
1375 /* ignore SIGHUP once we've started a child on a pty. Note that this may */
1376 /* cause EMACS not to die when it should, i.e., when its own controlling */
1377 /* tty goes away. I've complained to the AIX developers, and they may */
1378 /* change this behavior, but I'm not going to hold my breath. */
1379 signal (SIGHUP, SIG_IGN);
1380#endif
1381}
1382#endif /* HAVE_PTYS */
1383\f
1384#ifdef VMS
1385
1386/* Assigning an input channel is done at the start of Emacs execution.
1387 This is called each time Emacs is resumed, also, but does nothing
1388 because input_chain is no longer zero. */
1389
1390init_vms_input ()
1391{
1392 int status;
1393
91bac16a 1394 if (input_fd == 0)
86a5659e 1395 {
91bac16a 1396 status = SYS$ASSIGN (&input_dsc, &input_fd, 0, 0);
86a5659e
JB
1397 if (! (status & 1))
1398 LIB$STOP (status);
1399 }
1400}
1401
1402/* Deassigning the input channel is done before exiting. */
1403
1404stop_vms_input ()
1405{
91bac16a 1406 return SYS$DASSGN (input_fd);
86a5659e
JB
1407}
1408
1409short input_buffer;
1410
1411/* Request reading one character into the keyboard buffer.
1412 This is done as soon as the buffer becomes empty. */
1413
1414queue_kbd_input ()
1415{
1416 int status;
210b2b4f
JB
1417 extern kbd_input_ast ();
1418
86a5659e
JB
1419 waiting_for_ast = 0;
1420 stop_input = 0;
91bac16a 1421 status = SYS$QIO (0, input_fd, IO$_READVBLK,
86a5659e
JB
1422 &input_iosb, kbd_input_ast, 1,
1423 &input_buffer, 1, 0, terminator_mask, 0, 0);
1424}
1425
1426int input_count;
1427
1428/* Ast routine that is called when keyboard input comes in
1429 in accord with the SYS$QIO above. */
1430
1431kbd_input_ast ()
1432{
1433 register int c = -1;
1434 int old_errno = errno;
ffd56f97 1435 extern EMACS_TIME *input_available_clear_time;
86a5659e
JB
1436
1437 if (waiting_for_ast)
1438 SYS$SETEF (input_ef);
1439 waiting_for_ast = 0;
1440 input_count++;
1441#ifdef ASTDEBUG
1442 if (input_count == 25)
1443 exit (1);
1444 printf ("Ast # %d,", input_count);
1445 printf (" iosb = %x, %x, %x, %x",
1446 input_iosb.offset, input_iosb.status, input_iosb.termlen,
1447 input_iosb.term);
1448#endif
1449 if (input_iosb.offset)
1450 {
1451 c = input_buffer;
1452#ifdef ASTDEBUG
1453 printf (", char = 0%o", c);
1454#endif
1455 }
1456#ifdef ASTDEBUG
1457 printf ("\n");
1458 fflush (stdout);
1459 sleep (1);
1460#endif
1461 if (! stop_input)
1462 queue_kbd_input ();
1463 if (c >= 0)
1464 {
1465 struct input_event e;
1466 e.kind = ascii_keystroke;
210b2b4f
JB
1467 XSET (e.code, Lisp_Int, c);
1468#ifdef MULTI_FRAME
1469 XSET(e.frame_or_window, Lisp_Frame, selected_frame);
1470#else
1471 e.frame_or_window = Qnil;
1472#endif
86a5659e
JB
1473 kbd_buffer_store_event (&e);
1474 }
ffd56f97
JB
1475 if (input_available_clear_time)
1476 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
86a5659e
JB
1477 errno = old_errno;
1478}
1479
1480/* Wait until there is something in kbd_buffer. */
1481
1482wait_for_kbd_input ()
1483{
1484 extern int have_process_input, process_exited;
1485
1486 /* If already something, avoid doing system calls. */
1487 if (detect_input_pending ())
1488 {
1489 return;
1490 }
1491 /* Clear a flag, and tell ast routine above to set it. */
1492 SYS$CLREF (input_ef);
1493 waiting_for_ast = 1;
1494 /* Check for timing error: ast happened while we were doing that. */
1495 if (!detect_input_pending ())
1496 {
1497 /* No timing error: wait for flag to be set. */
1498 set_waiting_for_input (0);
1499 SYS$WFLOR (input_ef, input_eflist);
1500 clear_waiting_for_input (0);
1501 if (!detect_input_pending ())
1502 /* Check for subprocess input availability */
1503 {
1504 int dsp = have_process_input || process_exited;
1505
1506 SYS$CLREF (process_ef);
1507 if (have_process_input)
1508 process_command_input ();
1509 if (process_exited)
1510 process_exit ();
1511 if (dsp)
1512 {
1513 update_mode_lines++;
1514 redisplay_preserve_echo_area ();
1515 }
1516 }
1517 }
1518 waiting_for_ast = 0;
1519}
1520
1521/* Get rid of any pending QIO, when we are about to suspend
1522 or when we want to throw away pending input.
1523 We wait for a positive sign that the AST routine has run
1524 and therefore there is no I/O request queued when we return.
1525 SYS$SETAST is used to avoid a timing error. */
1526
1527end_kbd_input ()
1528{
1529#ifdef ASTDEBUG
1530 printf ("At end_kbd_input.\n");
1531 fflush (stdout);
1532 sleep (1);
1533#endif
1534 if (LIB$AST_IN_PROG ()) /* Don't wait if suspending from kbd_buffer_store_event! */
1535 {
91bac16a 1536 SYS$CANCEL (input_fd);
86a5659e
JB
1537 return;
1538 }
1539
1540 SYS$SETAST (0);
1541 /* Clear a flag, and tell ast routine above to set it. */
1542 SYS$CLREF (input_ef);
1543 waiting_for_ast = 1;
1544 stop_input = 1;
91bac16a 1545 SYS$CANCEL (input_fd);
86a5659e
JB
1546 SYS$SETAST (1);
1547 SYS$WAITFR (input_ef);
1548 waiting_for_ast = 0;
1549}
1550
1551/* Wait for either input available or time interval expiry. */
1552
1553input_wait_timeout (timeval)
1554 int timeval; /* Time to wait, in seconds */
1555{
1556 int time [2];
1557 static int zero = 0;
1558 static int large = -10000000;
1559
1560 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
1561
1562 /* If already something, avoid doing system calls. */
1563 if (detect_input_pending ())
1564 {
1565 return;
1566 }
1567 /* Clear a flag, and tell ast routine above to set it. */
1568 SYS$CLREF (input_ef);
1569 waiting_for_ast = 1;
1570 /* Check for timing error: ast happened while we were doing that. */
1571 if (!detect_input_pending ())
1572 {
1573 /* No timing error: wait for flag to be set. */
1574 SYS$CANTIM (1, 0);
1575 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
1576 SYS$WFLOR (timer_ef, timer_eflist); /* Wait for timer expiry or input */
1577 }
1578 waiting_for_ast = 0;
1579}
1580
1581/* The standard `sleep' routine works some other way
1582 and it stops working if you have ever quit out of it.
1583 This one continues to work. */
1584
1585sys_sleep (timeval)
1586 int timeval;
1587{
1588 int time [2];
1589 static int zero = 0;
1590 static int large = -10000000;
1591
1592 LIB$EMUL (&timeval, &large, &zero, time); /* Convert to VMS format */
1593
1594 SYS$CANTIM (1, 0);
1595 if (SYS$SETIMR (timer_ef, time, 0, 1) & 1) /* Set timer */
1596 SYS$WAITFR (timer_ef); /* Wait for timer expiry only */
1597}
1598
1599init_sigio ()
1600{
1601 request_sigio ();
1602}
1603
1604reset_sigio ()
1605{
1606 unrequest_sigio ();
1607}
1608
1609request_sigio ()
1610{
1611 croak ("request sigio");
1612}
1613
1614unrequest_sigio ()
1615{
1616 croak ("unrequest sigio");
1617}
1618
1619#endif /* VMS */
1620\f
1621/* Note that VMS compiler won't accept defined (CANNOT_DUMP). */
1622#ifndef CANNOT_DUMP
1623#define NEED_STARTS
1624#endif
1625
1626#ifndef SYSTEM_MALLOC
1627#ifndef NEED_STARTS
1628#define NEED_STARTS
1629#endif
1630#endif
1631
1632#ifdef NEED_STARTS
1633/* Some systems that cannot dump also cannot implement these. */
1634
1635/*
1636 * Return the address of the start of the text segment prior to
1637 * doing an unexec. After unexec the return value is undefined.
1638 * See crt0.c for further explanation and _start.
1639 *
1640 */
1641
1642#ifndef CANNOT_UNEXEC
1643char *
1644start_of_text ()
1645{
1646#ifdef TEXT_START
1647 return ((char *) TEXT_START);
1648#else
1649#ifdef GOULD
1650 extern csrt ();
1651 return ((char *) csrt);
1652#else /* not GOULD */
1653 extern int _start ();
1654 return ((char *) _start);
1655#endif /* GOULD */
1656#endif /* TEXT_START */
1657}
1658#endif /* not CANNOT_UNEXEC */
1659
1660/*
1661 * Return the address of the start of the data segment prior to
1662 * doing an unexec. After unexec the return value is undefined.
1663 * See crt0.c for further information and definition of data_start.
1664 *
1665 * Apparently, on BSD systems this is etext at startup. On
1666 * USG systems (swapping) this is highly mmu dependent and
1667 * is also dependent on whether or not the program is running
1668 * with shared text. Generally there is a (possibly large)
1669 * gap between end of text and start of data with shared text.
1670 *
1671 * On Uniplus+ systems with shared text, data starts at a
1672 * fixed address. Each port (from a given oem) is generally
1673 * different, and the specific value of the start of data can
1674 * be obtained via the UniPlus+ specific "uvar" system call,
1675 * however the method outlined in crt0.c seems to be more portable.
1676 *
1677 * Probably what will have to happen when a USG unexec is available,
1678 * at least on UniPlus, is temacs will have to be made unshared so
1679 * that text and data are contiguous. Then once loadup is complete,
1680 * unexec will produce a shared executable where the data can be
1681 * at the normal shared text boundry and the startofdata variable
1682 * will be patched by unexec to the correct value.
1683 *
1684 */
1685
1686char *
1687start_of_data ()
1688{
1689#ifdef DATA_START
1690 return ((char *) DATA_START);
6c65530f
JB
1691#else
1692#ifdef ORDINARY_LINK
1693 /*
1694 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
1695 * data_start isn't defined. We take the address of environ, which
1696 * is known to live at or near the start of the system crt0.c, and
1697 * we don't sweat the handful of bytes that might lose.
1698 */
1699 extern char **environ;
1700
1701 return((char *) &environ);
86a5659e
JB
1702#else
1703 extern int data_start;
1704 return ((char *) &data_start);
6c65530f
JB
1705#endif /* ORDINARY_LINK */
1706#endif /* DATA_START */
86a5659e
JB
1707}
1708#endif /* NEED_STARTS (not CANNOT_DUMP or not SYSTEM_MALLOC) */
1709
1710#ifndef CANNOT_DUMP
1711/* Some systems that cannot dump also cannot implement these. */
1712
1713/*
1714 * Return the address of the end of the text segment prior to
1715 * doing an unexec. After unexec the return value is undefined.
1716 */
1717
1718char *
1719end_of_text ()
1720{
1721#ifdef TEXT_END
1722 return ((char *) TEXT_END);
1723#else
1724 extern int etext;
1725 return ((char *) &etext);
1726#endif
1727}
1728
1729/*
1730 * Return the address of the end of the data segment prior to
1731 * doing an unexec. After unexec the return value is undefined.
1732 */
1733
1734char *
1735end_of_data ()
1736{
1737#ifdef DATA_END
1738 return ((char *) DATA_END);
1739#else
1740 extern int edata;
1741 return ((char *) &edata);
1742#endif
1743}
1744
1745#endif /* not CANNOT_DUMP */
1746\f
1747/* Get_system_name returns as its value
1748 a string for the Lisp function system-name to return. */
1749
1750#ifdef BSD4_1
1751#include <whoami.h>
1752#endif
1753
e36ba278
RS
1754/* Can't have this within the function since `static' is #defined to
1755 nothing for some USG systems. */
86a5659e 1756#ifdef USG
e36ba278
RS
1757#ifdef HAVE_GETHOSTNAME
1758static char get_system_name_name[256];
1759#else /* not HAVE_GETHOSTNAME */
86a5659e 1760static struct utsname get_system_name_name;
e36ba278
RS
1761#endif /* not HAVE_GETHOSTNAME */
1762#endif /* USG */
86a5659e 1763
f8a80313
RS
1764#ifndef BSD4_1
1765#ifndef USG
1766#ifndef VMS
1767#ifdef HAVE_SOCKETS
1768#include <sys/socket.h>
1769#include <netdb.h>
1770#endif /* HAVE_SOCKETS */
1771#endif /* not VMS */
1772#endif /* not USG */
1773#endif /* not BSD4_1 */
1774
86a5659e
JB
1775char *
1776get_system_name ()
1777{
1778#ifdef USG
e36ba278
RS
1779#ifdef HAVE_GETHOSTNAME
1780 gethostname (get_system_name_name, sizeof (get_system_name_name));
1781 return get_system_name_name;
1782#else /* not HAVE_GETHOSTNAME */
86a5659e
JB
1783 uname (&get_system_name_name);
1784 return (get_system_name_name.nodename);
e36ba278 1785#endif /* not HAVE_GETHOSTNAME */
86a5659e
JB
1786#else /* Not USG */
1787#ifdef BSD4_1
1788 return sysname;
1789#else /* not USG, not 4.1 */
1790 static char system_name_saved[32];
1791#ifdef VMS
1792 char *sp;
1793 if ((sp = egetenv ("SYS$NODE")) == 0)
1794 sp = "vax-vms";
1795 else
1796 {
1797 char *end;
1798
1799 if ((end = index (sp, ':')) != 0)
1800 *end = '\0';
1801 }
1802 strcpy (system_name_saved, sp);
1803#else /* not VMS */
1804 gethostname (system_name_saved, sizeof (system_name_saved));
f8a80313
RS
1805#ifdef HAVE_SOCKETS
1806 /* Turn the hostname into the official, fully-qualified hostname.
1807 Don't do this if we're going to dump; this can confuse system
1808 libraries on some machines and make the dumped emacs core dump. */
1809#ifndef CANNOT_DUMP
1810 if (initialized)
1811#endif /* not CANNOT_DUMP */
1812 {
1813 struct hostent *hp;
1814 hp = gethostbyname (system_name_saved);
1815 if (hp && strlen (hp->h_name) < sizeof(system_name_saved))
1816 strcpy (system_name_saved, hp->h_name);
1817 }
1818#endif /* HAVE_SOCKETS */
86a5659e
JB
1819#endif /* not VMS */
1820 return system_name_saved;
1821#endif /* not USG, not 4.1 */
1822#endif /* not USG */
1823}
210b2b4f
JB
1824
1825#ifdef VMS
1826#ifndef HAVE_GETHOSTNAME
1827void gethostname(buf, len)
1828 char *buf;
1829 int len;
1830{
1831 char *s;
1832 s = getenv ("SYS$NODE");
1833 if (s == NULL)
1834 buf[0] = '\0';
1835 else {
1836 strncpy (buf, s, len - 2);
1837 buf[len - 1] = '\0';
1838 } /* else */
1839} /* static void gethostname */
1840#endif /* ! HAVE_GETHOSTNAME */
1841#endif /* VMS */
1842
86a5659e
JB
1843\f
1844#ifndef VMS
1845#ifndef HAVE_SELECT
1846
1847#ifdef HAVE_X_WINDOWS
1848/* Cause explanatory error message at compile time,
1849 since the select emulation is not good enough for X. */
1850int *x = &x_windows_lose_if_no_select_system_call;
1851#endif
1852
1853/* Emulate as much as select as is possible under 4.1 and needed by Gnu Emacs
1854 * Only checks read descriptors.
1855 */
1856/* How long to wait between checking fds in select */
1857#define SELECT_PAUSE 1
1858int select_alarmed;
1859
1860/* For longjmp'ing back to read_input_waiting. */
1861
1862jmp_buf read_alarm_throw;
1863
1864/* Nonzero if the alarm signal should throw back to read_input_waiting.
1865 The read_socket_hook function sets this to 1 while it is waiting. */
1866
1867int read_alarm_should_throw;
1868
1869SIGTYPE
1870select_alarm ()
1871{
1872 select_alarmed = 1;
1873#ifdef BSD4_1
1874 sigrelse (SIGALRM);
1875#else /* not BSD4_1 */
1876 signal (SIGALRM, SIG_IGN);
1877#endif /* not BSD4_1 */
1878 if (read_alarm_should_throw)
1879 longjmp (read_alarm_throw, 1);
1880}
1881
1882/* Only rfds are checked. */
1883int
1884select (nfds, rfds, wfds, efds, timeout)
1885 int nfds;
1886 int *rfds, *wfds, *efds, *timeout;
1887{
1888 int ravail = 0, orfds = 0, old_alarm;
1889 int timeoutval = timeout ? *timeout : 100000;
1890 int *local_timeout = &timeoutval;
1891 extern int proc_buffered_char[];
1892#ifndef subprocesses
1893 int process_tick = 0, update_tick = 0;
1894#else
1895 extern int process_tick, update_tick;
1896#endif
1897 SIGTYPE (*old_trap) ();
1898 unsigned char buf;
1899
1900 if (rfds)
1901 {
1902 orfds = *rfds;
1903 *rfds = 0;
1904 }
1905 if (wfds)
1906 *wfds = 0;
1907 if (efds)
1908 *efds = 0;
1909
1910 /* If we are looking only for the terminal, with no timeout,
1911 just read it and wait -- that's more efficient. */
1912 if (orfds == 1 && *local_timeout == 100000 && process_tick == update_tick)
1913 {
1914 if (! detect_input_pending ())
1915 read_input_waiting ();
1916 *rfds = 1;
1917 return 1;
1918 }
1919
1920 /* Once a second, till the timer expires, check all the flagged read
1921 * descriptors to see if any input is available. If there is some then
1922 * set the corresponding bit in the return copy of rfds.
1923 */
1924 while (1)
1925 {
1926 register int to_check, bit, fd;
1927
1928 if (rfds)
1929 {
1930 for (to_check = nfds, bit = 1, fd = 0; --to_check >= 0; bit <<= 1, fd++)
1931 {
1932 if (orfds & bit)
1933 {
1934 int avail = 0, status = 0;
1935
1936 if (bit == 1)
1937 avail = detect_input_pending (); /* Special keyboard handler */
1938 else
1939 {
1940#ifdef FIONREAD
1941 status = ioctl (fd, FIONREAD, &avail);
1942#else /* no FIONREAD */
1943 /* Hoping it will return -1 if nothing available
1944 or 0 if all 0 chars requested are read. */
1945 if (proc_buffered_char[fd] >= 0)
1946 avail = 1;
1947 else
1948 {
1949 avail = read (fd, &buf, 1);
1950 if (avail > 0)
1951 proc_buffered_char[fd] = buf;
1952 }
1953#endif /* no FIONREAD */
1954 }
1955 if (status >= 0 && avail > 0)
1956 {
1957 (*rfds) |= bit;
1958 ravail++;
1959 }
1960 }
1961 }
1962 }
1963 if (*local_timeout == 0 || ravail != 0 || process_tick != update_tick)
1964 break;
1965 old_alarm = alarm (0);
34567704 1966 old_trap = signal (SIGALRM, select_alarm);
86a5659e
JB
1967 select_alarmed = 0;
1968 alarm (SELECT_PAUSE);
1969 /* Wait for a SIGALRM (or maybe a SIGTINT) */
1970 while (select_alarmed == 0 && *local_timeout != 0
1971 && process_tick == update_tick)
1972 {
1973 /* If we are interested in terminal input,
1974 wait by reading the terminal.
1975 That makes instant wakeup for terminal input at least. */
1976 if (orfds & 1)
1977 {
1978 read_input_waiting ();
1979 if (detect_input_pending ())
1980 select_alarmed = 1;
1981 }
1982 else
1983 pause ();
1984 }
1985 (*local_timeout) -= SELECT_PAUSE;
1986 /* Reset the old alarm if there was one */
1987 alarm (0);
1988 signal (SIGALRM, old_trap);
1989 if (old_alarm != 0)
1990 {
1991 /* Reset or forge an interrupt for the original handler. */
1992 old_alarm -= SELECT_PAUSE;
1993 if (old_alarm <= 0)
1994 kill (getpid (), SIGALRM); /* Fake an alarm with the orig' handler */
1995 else
1996 alarm (old_alarm);
1997 }
1998 if (*local_timeout == 0) /* Stop on timer being cleared */
1999 break;
2000 }
2001 return ravail;
2002}
2003
2004/* Read keyboard input into the standard buffer,
2005 waiting for at least one character. */
2006
2007/* Make all keyboard buffers much bigger when using X windows. */
2008#ifdef HAVE_X_WINDOWS
2009#define BUFFER_SIZE_FACTOR 16
2010#else
2011#define BUFFER_SIZE_FACTOR 1
2012#endif
2013
2014read_input_waiting ()
2015{
2016 char buf[256 * BUFFER_SIZE_FACTOR];
2017 struct input_event e;
34567704
JB
2018 int nread, i;
2019 extern int quit_char;
86a5659e
JB
2020
2021 if (read_socket_hook)
2022 {
2023 read_alarm_should_throw = 0;
2024 if (! setjmp (read_alarm_throw))
2025 nread = (*read_socket_hook) (0, buf, 256 * BUFFER_SIZE_FACTOR, 1, 0);
2026 else
2027 nread = -1;
2028 }
2029 else
2030 nread = read (fileno (stdin), buf, 1);
2031
2032 /* Scan the chars for C-g and store them in kbd_buffer. */
2033 e.kind = ascii_keystroke;
6c65530f 2034 e.frame_or_window = selected_frame;
57ef1664 2035 e.modifiers = 0;
86a5659e
JB
2036 for (i = 0; i < nread; i++)
2037 {
2038 XSET (e.code, Lisp_Int, buf[i]);
2039 kbd_buffer_store_event (&e);
2040 /* Don't look at input that follows a C-g too closely.
2041 This reduces lossage due to autorepeat on C-g. */
34567704 2042 if (buf[i] == quit_char)
86a5659e
JB
2043 break;
2044 }
2045}
2046
2047#endif /* not HAVE_SELECT */
2048#endif /* not VMS */
2049\f
2050#ifdef BSD4_1
86a5659e
JB
2051/*
2052 * Partially emulate 4.2 open call.
2053 * open is defined as this in 4.1.
2054 *
2055 * - added by Michael Bloom @ Citicorp/TTI
2056 *
2057 */
2058
2059int
2060sys_open (path, oflag, mode)
2061 char *path;
2062 int oflag, mode;
2063{
2064 if (oflag & O_CREAT)
2065 return creat (path, mode);
2066 else
2067 return open (path, oflag);
2068}
2069
2070init_sigio ()
2071{
2072 if (noninteractive)
2073 return;
2074 lmode = LINTRUP | lmode;
2075 ioctl (0, TIOCLSET, &lmode);
2076}
2077
2078reset_sigio ()
2079{
2080 if (noninteractive)
2081 return;
2082 lmode = ~LINTRUP & lmode;
2083 ioctl (0, TIOCLSET, &lmode);
2084}
2085
2086request_sigio ()
2087{
2088 sigrelse (SIGTINT);
2089
2090 interrupts_deferred = 0;
2091}
2092
2093unrequest_sigio ()
2094{
2095 sighold (SIGTINT);
2096
2097 interrupts_deferred = 1;
2098}
2099
2100/* still inside #ifdef BSD4_1 */
2101#ifdef subprocesses
2102
2103int sigheld; /* Mask of held signals */
2104
2105sigholdx (signum)
2106 int signum;
2107{
2108 sigheld |= sigbit (signum);
2109 sighold (signum);
2110}
2111
2112sigisheld (signum)
2113 int signum;
2114{
2115 sigheld |= sigbit (signum);
2116}
2117
2118sigunhold (signum)
2119 int signum;
2120{
2121 sigheld &= ~sigbit (signum);
2122 sigrelse (signum);
2123}
2124
2125sigfree () /* Free all held signals */
2126{
2127 int i;
2128 for (i = 0; i < NSIG; i++)
2129 if (sigheld & sigbit (i))
2130 sigrelse (i);
2131 sigheld = 0;
2132}
2133
2134sigbit (i)
2135{
2136 return 1 << (i - 1);
2137}
2138#endif /* subprocesses */
2139#endif /* BSD4_1 */
2140\f
2141/* POSIX signals support - DJB */
2142/* Anyone with POSIX signals should have ANSI C declarations */
2143
2144#ifdef POSIX_SIGNALS
2145
2146sigset_t old_mask, empty_mask, full_mask, temp_mask;
2147static struct sigaction new_action, old_action;
2148
2149init_signals ()
2150{
00eaaa32
JB
2151 sigemptyset (&empty_mask);
2152 sigfillset (&full_mask);
86a5659e
JB
2153}
2154
86a5659e
JB
2155signal_handler_t
2156sys_signal (int signal_number, signal_handler_t action)
2157{
2158#ifdef DGUX
2159 /* This gets us restartable system calls for efficiency.
2160 The "else" code will works as well. */
2161 return (berk_signal (signal_number, action));
2162#else
2163 sigemptyset (&new_action.sa_mask);
2164 new_action.sa_handler = action;
4a785b6e 2165 new_action.sa_flags = 0;
d32b2f3c 2166 sigaction (signal_number, &new_action, &old_action);
86a5659e
JB
2167 return (old_action.sa_handler);
2168#endif /* DGUX */
2169}
2170
e065a56e
JB
2171#ifndef __GNUC__
2172/* If we're compiling with GCC, we don't need this function, since it
2173 can be written as a macro. */
2174sigset_t
2175sys_sigmask (int sig)
2176{
2177 sigset_t mask;
2178 sigemptyset (&mask);
2179 sigaddset (&mask, sig);
2180 return mask;
2181}
2182#endif
2183
86a5659e
JB
2184int
2185sys_sigpause (sigset_t new_mask)
2186{
2187 /* pause emulating berk sigpause... */
2188 sigsuspend (&new_mask);
2189 return (EINTR);
2190}
2191
2192/* I'd like to have these guys return pointers to the mask storage in here,
2193 but there'd be trouble if the code was saving multiple masks. I'll be
2194 safe and pass the structure. It normally won't be more than 2 bytes
2195 anyhow. - DJB */
2196
2197sigset_t
2198sys_sigblock (sigset_t new_mask)
2199{
2200 sigset_t old_mask;
2201 sigprocmask (SIG_BLOCK, &new_mask, &old_mask);
2202 return (old_mask);
2203}
2204
2205sigset_t
2206sys_sigunblock (sigset_t new_mask)
2207{
2208 sigset_t old_mask;
2209 sigprocmask (SIG_UNBLOCK, &new_mask, &old_mask);
2210 return (old_mask);
2211}
2212
2213sigset_t
2214sys_sigsetmask (sigset_t new_mask)
2215{
2216 sigset_t old_mask;
2217 sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
2218 return (old_mask);
2219}
2220
2221#endif /* POSIX_SIGNALS */
2222\f
2223#ifndef BSTRING
2224
2225void
2226bzero (b, length)
2227 register char *b;
2228 register int length;
2229{
2230#ifdef VMS
2231 short zero = 0;
2232 long max_str = 65535;
2233
2234 while (length > max_str) {
2235 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
2236 length -= max_str;
2237 b += max_str;
2238 }
2239 max_str = length;
2240 (void) LIB$MOVC5 (&zero, &zero, &zero, &max_str, b);
2241#else
2242 while (length-- > 0)
2243 *b++ = 0;
2244#endif /* not VMS */
2245}
2246
2247/* Saying `void' requires a declaration, above, where bcopy is used
2248 and that declaration causes pain for systems where bcopy is a macro. */
2249bcopy (b1, b2, length)
2250 register char *b1;
2251 register char *b2;
2252 register int length;
2253{
2254#ifdef VMS
2255 long max_str = 65535;
2256
2257 while (length > max_str) {
2258 (void) LIB$MOVC3 (&max_str, b1, b2);
2259 length -= max_str;
2260 b1 += max_str;
2261 b2 += max_str;
2262 }
2263 max_str = length;
2264 (void) LIB$MOVC3 (&length, b1, b2);
2265#else
2266 while (length-- > 0)
2267 *b2++ = *b1++;
2268#endif /* not VMS */
2269}
2270
2271int
2272bcmp (b1, b2, length) /* This could be a macro! */
2273 register char *b1;
2274 register char *b2;
2275 register int length;
2276{
2277#ifdef VMS
2278 struct dsc$descriptor_s src1 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b1};
2279 struct dsc$descriptor_s src2 = {length, DSC$K_DTYPE_T, DSC$K_CLASS_S, b2};
2280
2281 return STR$COMPARE (&src1, &src2);
2282#else
2283 while (length-- > 0)
2284 if (*b1++ != *b2++)
2285 return 1;
2286
2287 return 0;
2288#endif /* not VMS */
2289}
2290#endif /* not BSTRING */
2291\f
9927a7b1 2292#ifndef HAVE_RANDOM
86a5659e
JB
2293#ifdef USG
2294/*
2295 * The BSD random returns numbers in the range of
2296 * 0 to 2e31 - 1. The USG rand returns numbers in the
2297 * range of 0 to 2e15 - 1. This is probably not significant
2298 * in this usage.
2299 */
2300
2301long
2302random ()
2303{
2304 /* Arrange to return a range centered on zero. */
2305 return (rand () << 15) + rand () - (1 << 29);
2306}
2307
2308srandom (arg)
2309 int arg;
2310{
2311 srand (arg);
2312}
2313
2314#endif /* USG */
2315
2316#ifdef BSD4_1
2317long random ()
2318{
2319 /* Arrange to return a range centered on zero. */
2320 return (rand () << 15) + rand () - (1 << 29);
2321}
2322
2323srandom (arg)
2324 int arg;
2325{
2326 srand (arg);
2327}
2328#endif /* BSD4_1 */
9927a7b1 2329#endif
86a5659e
JB
2330\f
2331#ifdef WRONG_NAME_INSQUE
2332
2333insque (q,p)
2334 caddr_t q,p;
2335{
2336 _insque (q,p);
2337}
2338
2339#endif
2340\f
2341#ifdef VMS
2342
2343#ifdef getenv
2344/* If any place else asks for the TERM variable,
2345 allow it to be overridden with the EMACS_TERM variable
2346 before attempting to translate the logical name TERM. As a last
2347 resort, ask for VAX C's special idea of the TERM variable. */
2348#undef getenv
2349char *
2350sys_getenv (name)
2351 char *name;
2352{
2353 register char *val;
2354 static char buf[256];
2355 static struct dsc$descriptor_s equiv
2356 = {sizeof (buf), DSC$K_DTYPE_T, DSC$K_CLASS_S, buf};
2357 static struct dsc$descriptor_s d_name
2358 = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
2359 short eqlen;
2360
2361 if (!strcmp (name, "TERM"))
2362 {
2363 val = (char *) getenv ("EMACS_TERM");
2364 if (val)
2365 return val;
2366 }
2367
2368 d_name.dsc$w_length = strlen (name);
2369 d_name.dsc$a_pointer = name;
986ffb24 2370 if (LIB$SYS_TRNLOG (&d_name, &eqlen, &equiv) == 1)
86a5659e
JB
2371 {
2372 char *str = (char *) xmalloc (eqlen + 1);
2373 bcopy (buf, str, eqlen);
2374 str[eqlen] = '\0';
2375 /* This is a storage leak, but a pain to fix. With luck,
2376 no one will ever notice. */
2377 return str;
2378 }
2379 return (char *) getenv (name);
2380}
2381#endif /* getenv */
2382
2383#ifdef abort
2384/* Since VMS doesn't believe in core dumps, the only way to debug this beast is
2385 to force a call on the debugger from within the image. */
2386#undef abort
2387sys_abort ()
2388{
2389 reset_sys_modes ();
2390 LIB$SIGNAL (SS$_DEBUG);
2391}
2392#endif /* abort */
2393#endif /* VMS */
2394\f
2395#ifdef VMS
2396#ifdef LINK_CRTL_SHARE
2397#ifdef SHAREABLE_LIB_BUG
eb8c3be9 2398/* Variables declared noshare and initialized in sharable libraries
86a5659e
JB
2399 cannot be shared. The VMS linker incorrectly forces you to use a private
2400 version which is uninitialized... If not for this "feature", we
2401 could use the C library definition of sys_nerr and sys_errlist. */
2402int sys_nerr = 35;
2403char *sys_errlist[] =
2404 {
2405 "error 0",
2406 "not owner",
2407 "no such file or directory",
2408 "no such process",
2409 "interrupted system call",
2410 "i/o error",
2411 "no such device or address",
2412 "argument list too long",
2413 "exec format error",
2414 "bad file number",
2415 "no child process",
2416 "no more processes",
2417 "not enough memory",
2418 "permission denied",
2419 "bad address",
2420 "block device required",
2421 "mount devices busy",
2422 "file exists",
2423 "cross-device link",
2424 "no such device",
2425 "not a directory",
2426 "is a directory",
2427 "invalid argument",
2428 "file table overflow",
2429 "too many open files",
2430 "not a typewriter",
2431 "text file busy",
2432 "file too big",
2433 "no space left on device",
2434 "illegal seek",
2435 "read-only file system",
2436 "too many links",
2437 "broken pipe",
2438 "math argument",
2439 "result too large",
2440 "I/O stream empty",
2441 "vax/vms specific error code nontranslatable error"
2442 };
2443#endif /* SHAREABLE_LIB_BUG */
2444#endif /* LINK_CRTL_SHARE */
2445#endif /* VMS */
2446\f
2447#ifdef INTERRUPTIBLE_OPEN
2448
2449int
2450/* VARARGS 2 */
2451sys_open (path, oflag, mode)
2452 char *path;
2453 int oflag, mode;
2454{
2455 register int rtnval;
2456
2457 while ((rtnval = open (path, oflag, mode)) == -1
2458 && (errno == EINTR));
2459 return (rtnval);
2460}
2461
2462#endif /* INTERRUPTIBLE_OPEN */
2463
2464#ifdef INTERRUPTIBLE_CLOSE
2465
2466sys_close (fd)
2467 int fd;
2468{
2469 register int rtnval;
2470
2471 while ((rtnval = close (fd)) == -1
2472 && (errno == EINTR));
2473 return rtnval;
2474}
2475
2476#endif /* INTERRUPTIBLE_CLOSE */
2477
2478#ifdef INTERRUPTIBLE_IO
2479
2480int
2481sys_read (fildes, buf, nbyte)
2482 int fildes;
2483 char *buf;
2484 unsigned int nbyte;
2485{
2486 register int rtnval;
2487
2488 while ((rtnval = read (fildes, buf, nbyte)) == -1
2489 && (errno == EINTR));
2490 return (rtnval);
2491}
2492
2493int
2494sys_write (fildes, buf, nbyte)
2495 int fildes;
2496 char *buf;
2497 unsigned int nbyte;
2498{
2499 register int rtnval;
2500
2501 while ((rtnval = write (fildes, buf, nbyte)) == -1
2502 && (errno == EINTR));
2503 return (rtnval);
2504}
2505
2506#endif /* INTERRUPTIBLE_IO */
2507\f
2508#ifdef USG
2509/*
2510 * All of the following are for USG.
2511 *
2512 * On USG systems the system calls are INTERRUPTIBLE by signals
2513 * that the user program has elected to catch. Thus the system call
2514 * must be retried in these cases. To handle this without massive
2515 * changes in the source code, we remap the standard system call names
2516 * to names for our own functions in sysdep.c that do the system call
2517 * with retries. Actually, for portability reasons, it is good
2518 * programming practice, as this example shows, to limit all actual
eb8c3be9 2519 * system calls to a single occurrence in the source. Sure, this
86a5659e
JB
2520 * adds an extra level of function call overhead but it is almost
2521 * always negligible. Fred Fish, Unisoft Systems Inc.
2522 */
2523
00eaaa32 2524#ifndef HAVE_SYS_SIGLIST
86a5659e
JB
2525char *sys_siglist[NSIG + 1] =
2526{
2527#ifdef AIX
2528/* AIX has changed the signals a bit */
2529 "bogus signal", /* 0 */
2530 "hangup", /* 1 SIGHUP */
2531 "interrupt", /* 2 SIGINT */
2532 "quit", /* 3 SIGQUIT */
2533 "illegal instruction", /* 4 SIGILL */
2534 "trace trap", /* 5 SIGTRAP */
2535 "IOT instruction", /* 6 SIGIOT */
2536 "crash likely", /* 7 SIGDANGER */
2537 "floating point exception", /* 8 SIGFPE */
2538 "kill", /* 9 SIGKILL */
2539 "bus error", /* 10 SIGBUS */
2540 "segmentation violation", /* 11 SIGSEGV */
2541 "bad argument to system call", /* 12 SIGSYS */
2542 "write on a pipe with no one to read it", /* 13 SIGPIPE */
2543 "alarm clock", /* 14 SIGALRM */
2544 "software termination signum", /* 15 SIGTERM */
2545 "user defined signal 1", /* 16 SIGUSR1 */
2546 "user defined signal 2", /* 17 SIGUSR2 */
2547 "death of a child", /* 18 SIGCLD */
2548 "power-fail restart", /* 19 SIGPWR */
2549 "bogus signal", /* 20 */
2550 "bogus signal", /* 21 */
2551 "bogus signal", /* 22 */
2552 "bogus signal", /* 23 */
2553 "bogus signal", /* 24 */
2554 "LAN I/O interrupt", /* 25 SIGAIO */
2555 "PTY I/O interrupt", /* 26 SIGPTY */
2556 "I/O intervention required", /* 27 SIGIOINT */
2557 "HFT grant", /* 28 SIGGRANT */
2558 "HFT retract", /* 29 SIGRETRACT */
2559 "HFT sound done", /* 30 SIGSOUND */
2560 "HFT input ready", /* 31 SIGMSG */
2561#else /* not AIX */
2562 "bogus signal", /* 0 */
2563 "hangup", /* 1 SIGHUP */
2564 "interrupt", /* 2 SIGINT */
2565 "quit", /* 3 SIGQUIT */
2566 "illegal instruction", /* 4 SIGILL */
2567 "trace trap", /* 5 SIGTRAP */
2568 "IOT instruction", /* 6 SIGIOT */
2569 "EMT instruction", /* 7 SIGEMT */
2570 "floating point exception", /* 8 SIGFPE */
2571 "kill", /* 9 SIGKILL */
2572 "bus error", /* 10 SIGBUS */
2573 "segmentation violation", /* 11 SIGSEGV */
2574 "bad argument to system call", /* 12 SIGSYS */
2575 "write on a pipe with no one to read it", /* 13 SIGPIPE */
2576 "alarm clock", /* 14 SIGALRM */
2577 "software termination signum", /* 15 SIGTERM */
2578 "user defined signal 1", /* 16 SIGUSR1 */
2579 "user defined signal 2", /* 17 SIGUSR2 */
2580 "death of a child", /* 18 SIGCLD */
2581 "power-fail restart", /* 19 SIGPWR */
2582#endif /* not AIX */
2583 0
2584 };
70080de1 2585#endif /* HAVE_SYS_SIGLIST */
86a5659e
JB
2586
2587/*
2588 * Warning, this function may not duplicate 4.2 action properly
2589 * under error conditions.
2590 */
2591
2592#ifndef MAXPATHLEN
2593/* In 4.1, param.h fails to define this. */
2594#define MAXPATHLEN 1024
2595#endif
2596
2597#ifndef HAVE_GETWD
2598
2599char *
2600getwd (pathname)
2601 char *pathname;
2602{
2603 char *npath, *spath;
2604 extern char *getcwd ();
2605
9ac0d9e0 2606 BLOCK_INPUT; /* getcwd uses malloc */
86a5659e
JB
2607 spath = npath = getcwd ((char *) 0, MAXPATHLEN);
2608 /* On Altos 3068, getcwd can return @hostname/dir, so discard
2609 up to first slash. Should be harmless on other systems. */
2610 while (*npath && *npath != '/')
2611 npath++;
2612 strcpy (pathname, npath);
2613 free (spath); /* getcwd uses malloc */
9ac0d9e0 2614 UNBLOCK_INPUT;
86a5659e
JB
2615 return pathname;
2616}
2617
2618#endif /* HAVE_GETWD */
2619
2620/*
2621 * Emulate rename using unlink/link. Note that this is
2622 * only partially correct. Also, doesn't enforce restriction
2623 * that files be of same type (regular->regular, dir->dir, etc).
2624 */
2625
4746118a
JB
2626#ifndef HAVE_RENAME
2627
86a5659e
JB
2628rename (from, to)
2629 char *from;
2630 char *to;
2631{
2632 if (access (from, 0) == 0)
2633 {
2634 unlink (to);
2635 if (link (from, to) == 0)
2636 if (unlink (from) == 0)
2637 return (0);
2638 }
2639 return (-1);
2640}
2641
4746118a
JB
2642#endif
2643
86a5659e
JB
2644#ifndef HAVE_VFORK
2645
2646/*
2647 * Substitute fork for vfork on USG flavors.
2648 */
2649
2650vfork ()
2651{
2652 return (fork ());
2653}
2654
2655#endif /* not HAVE_VFORK */
2656
2657#ifdef MISSING_UTIMES
2658
2659/* HPUX (among others) sets HAVE_TIMEVAL but does not implement utimes. */
2660
2661utimes ()
2662{
2663}
2664#endif
2665
2666#ifdef IRIS_UTIME
2667
2668/* The IRIS (3.5) has timevals, but uses sys V utime, and doesn't have the
2669 utimbuf structure defined anywhere but in the man page. */
2670
2671struct utimbuf
2672 {
2673 long actime;
2674 long modtime;
2675 };
2676
2677utimes (name, tvp)
2678 char *name;
2679 struct timeval tvp[];
2680{
2681 struct utimbuf utb;
2682 utb.actime = tvp[0].tv_sec;
2683 utb.modtime = tvp[1].tv_sec;
2684 utime (name, &utb);
2685}
2686#endif /* IRIS_UTIME */
2687
2688
2689#ifdef HPUX
2690#ifndef HAVE_PERROR
2691
2692/* HPUX curses library references perror, but as far as we know
2693 it won't be called. Anyway this definition will do for now. */
2694
2695perror ()
2696{
2697}
2698
2699#endif /* not HAVE_PERROR */
2700#endif /* HPUX */
2701
2702#ifndef HAVE_DUP2
2703
2704/*
2705 * Emulate BSD dup2. First close newd if it already exists.
2706 * Then, attempt to dup oldd. If not successful, call dup2 recursively
2707 * until we are, then close the unsuccessful ones.
2708 */
2709
2710dup2 (oldd, newd)
2711 int oldd;
2712 int newd;
2713{
2714 register int fd, ret;
2715
2716 sys_close (newd);
2717
2718#ifdef F_DUPFD
2719 fd = fcntl (oldd, F_DUPFD, newd);
2720 if (fd != newd)
2721 error ("can't dup2 (%i,%i) : %s", oldd, newd, sys_errlist[errno]);
2722#else
2723 fd = dup (old);
2724 if (fd == -1)
2725 return -1;
2726 if (fd == new)
2727 return new;
2728 ret = dup2 (old,new);
2729 sys_close (fd);
2730 return ret;
2731#endif
2732}
2733
2734#endif /* not HAVE_DUP2 */
2735
2736/*
2737 * Gettimeofday. Simulate as much as possible. Only accurate
2738 * to nearest second. Emacs doesn't use tzp so ignore it for now.
2739 * Only needed when subprocesses are defined.
2740 */
2741
2742#ifdef subprocesses
2743#ifndef VMS
2744#ifndef HAVE_GETTIMEOFDAY
2745#ifdef HAVE_TIMEVAL
2746
2747/* ARGSUSED */
2748gettimeofday (tp, tzp)
2749 struct timeval *tp;
2750 struct timezone *tzp;
2751{
2752 extern long time ();
2753
2754 tp->tv_sec = time ((long *)0);
2755 tp->tv_usec = 0;
4ca7594f
RS
2756 if (tzp != 0)
2757 tzp->tz_minuteswest = -1;
86a5659e
JB
2758}
2759
2760#endif
2761#endif
2762#endif
2763#endif /* subprocess && !HAVE_GETTIMEOFDAY && HAVE_TIMEVAL && !VMS */
2764
2765/*
2766 * This function will go away as soon as all the stubs fixed. (fnf)
2767 */
2768
2769croak (badfunc)
2770 char *badfunc;
2771{
2772 printf ("%s not yet implemented\r\n", badfunc);
2773 reset_sys_modes ();
2774 exit (1);
2775}
2776
2777#endif /* USG */
2778\f
2779#ifdef DGUX
2780
2781char *sys_siglist[NSIG + 1] =
2782{
2783 "null signal", /* 0 SIGNULL */
2784 "hangup", /* 1 SIGHUP */
2785 "interrupt", /* 2 SIGINT */
2786 "quit", /* 3 SIGQUIT */
2787 "illegal instruction", /* 4 SIGILL */
2788 "trace trap", /* 5 SIGTRAP */
2789 "abort termination", /* 6 SIGABRT */
2790 "SIGEMT", /* 7 SIGEMT */
2791 "floating point exception", /* 8 SIGFPE */
2792 "kill", /* 9 SIGKILL */
2793 "bus error", /* 10 SIGBUS */
2794 "segmentation violation", /* 11 SIGSEGV */
2795 "bad argument to system call", /* 12 SIGSYS */
2796 "write on a pipe with no reader", /* 13 SIGPIPE */
2797 "alarm clock", /* 14 SIGALRM */
2798 "software termination signal", /* 15 SIGTERM */
2799 "user defined signal 1", /* 16 SIGUSR1 */
2800 "user defined signal 2", /* 17 SIGUSR2 */
2801 "child stopped or terminated", /* 18 SIGCLD */
2802 "power-fail restart", /* 19 SIGPWR */
2803 "window size changed", /* 20 SIGWINCH */
2804 "undefined", /* 21 */
eb8c3be9 2805 "pollable event occurred", /* 22 SIGPOLL */
86a5659e
JB
2806 "sendable stop signal not from tty", /* 23 SIGSTOP */
2807 "stop signal from tty", /* 24 SIGSTP */
2808 "continue a stopped process", /* 25 SIGCONT */
2809 "attempted background tty read", /* 26 SIGTTIN */
2810 "attempted background tty write", /* 27 SIGTTOU */
2811 "undefined", /* 28 */
2812 "undefined", /* 29 */
2813 "undefined", /* 30 */
2814 "undefined", /* 31 */
2815 "undefined", /* 32 */
2816 "socket (TCP/IP) urgent data arrival", /* 33 SIGURG */
2817 "I/O is possible", /* 34 SIGIO */
2818 "exceeded cpu time limit", /* 35 SIGXCPU */
2819 "exceeded file size limit", /* 36 SIGXFSZ */
2820 "virtual time alarm", /* 37 SIGVTALRM */
2821 "profiling time alarm", /* 38 SIGPROF */
2822 "undefined", /* 39 */
2823 "file record locks revoked", /* 40 SIGLOST */
2824 "undefined", /* 41 */
2825 "undefined", /* 42 */
2826 "undefined", /* 43 */
2827 "undefined", /* 44 */
2828 "undefined", /* 45 */
2829 "undefined", /* 46 */
2830 "undefined", /* 47 */
2831 "undefined", /* 48 */
2832 "undefined", /* 49 */
2833 "undefined", /* 50 */
2834 "undefined", /* 51 */
2835 "undefined", /* 52 */
2836 "undefined", /* 53 */
2837 "undefined", /* 54 */
2838 "undefined", /* 55 */
2839 "undefined", /* 56 */
2840 "undefined", /* 57 */
2841 "undefined", /* 58 */
2842 "undefined", /* 59 */
2843 "undefined", /* 60 */
2844 "undefined", /* 61 */
2845 "undefined", /* 62 */
2846 "undefined", /* 63 */
2847 "notification message in mess. queue", /* 64 SIGDGNOTIFY */
2848 0
2849};
2850
2851#endif /* DGUX */
2852\f
2853/* Directory routines for systems that don't have them. */
2854
2855#ifdef SYSV_SYSTEM_DIR
2856
2857#include <dirent.h>
2858
2859#ifndef AIX
2860int
2861closedir (dirp)
2862 register DIR *dirp; /* stream from opendir */
2863{
2864 sys_close (dirp->dd_fd);
9ac0d9e0
JB
2865 xfree ((char *) dirp->dd_buf); /* directory block defined in <dirent.h> */
2866 xfree ((char *) dirp);
86a5659e
JB
2867}
2868#endif /* not AIX */
2869#endif /* SYSV_SYSTEM_DIR */
2870
2871#ifdef NONSYSTEM_DIR_LIBRARY
2872
2873DIR *
2874opendir (filename)
2875 char *filename; /* name of directory */
2876{
2877 register DIR *dirp; /* -> malloc'ed storage */
2878 register int fd; /* file descriptor for read */
2879 struct stat sbuf; /* result of fstat */
2880
2881 fd = sys_open (filename, 0);
2882 if (fd < 0)
2883 return 0;
2884
9ac0d9e0 2885 BLOCK_INPUT;
86a5659e
JB
2886 if (fstat (fd, &sbuf) < 0
2887 || (sbuf.st_mode & S_IFMT) != S_IFDIR
2888 || (dirp = (DIR *) malloc (sizeof (DIR))) == 0)
2889 {
2890 sys_close (fd);
9ac0d9e0 2891 UNBLOCK_INPUT;
86a5659e
JB
2892 return 0; /* bad luck today */
2893 }
9ac0d9e0 2894 UNBLOCK_INPUT;
86a5659e
JB
2895
2896 dirp->dd_fd = fd;
2897 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
2898
2899 return dirp;
2900}
2901
2902void
2903closedir (dirp)
2904 register DIR *dirp; /* stream from opendir */
2905{
2906 sys_close (dirp->dd_fd);
9ac0d9e0 2907 xfree ((char *) dirp);
86a5659e
JB
2908}
2909
2910
2911#ifndef VMS
2912#define DIRSIZ 14
2913struct olddir
2914 {
2915 ino_t od_ino; /* inode */
2916 char od_name[DIRSIZ]; /* filename */
2917 };
2918#endif /* not VMS */
2919
2920struct direct dir_static; /* simulated directory contents */
2921
2922/* ARGUSED */
2923struct direct *
2924readdir (dirp)
2925 register DIR *dirp; /* stream from opendir */
2926{
2927#ifndef VMS
2928 register struct olddir *dp; /* -> directory data */
2929#else /* VMS */
2930 register struct dir$_name *dp; /* -> directory data */
2931 register struct dir$_version *dv; /* -> version data */
2932#endif /* VMS */
2933
2934 for (; ;)
2935 {
2936 if (dirp->dd_loc >= dirp->dd_size)
2937 dirp->dd_loc = dirp->dd_size = 0;
2938
2939 if (dirp->dd_size == 0 /* refill buffer */
2940 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
2941 return 0;
2942
2943#ifndef VMS
2944 dp = (struct olddir *) &dirp->dd_buf[dirp->dd_loc];
2945 dirp->dd_loc += sizeof (struct olddir);
2946
2947 if (dp->od_ino != 0) /* not deleted entry */
2948 {
2949 dir_static.d_ino = dp->od_ino;
2950 strncpy (dir_static.d_name, dp->od_name, DIRSIZ);
2951 dir_static.d_name[DIRSIZ] = '\0';
2952 dir_static.d_namlen = strlen (dir_static.d_name);
2953 dir_static.d_reclen = sizeof (struct direct)
2954 - MAXNAMLEN + 3
2955 + dir_static.d_namlen - dir_static.d_namlen % 4;
2956 return &dir_static; /* -> simulated structure */
2957 }
2958#else /* VMS */
2959 dp = (struct dir$_name *) dirp->dd_buf;
2960 if (dirp->dd_loc == 0)
2961 dirp->dd_loc = (dp->dir$b_namecount&1) ? dp->dir$b_namecount + 1
2962 : dp->dir$b_namecount;
2963 dv = (struct dir$_version *)&dp->dir$t_name[dirp->dd_loc];
2964 dir_static.d_ino = dv->dir$w_fid_num;
2965 dir_static.d_namlen = dp->dir$b_namecount;
2966 dir_static.d_reclen = sizeof (struct direct)
2967 - MAXNAMLEN + 3
2968 + dir_static.d_namlen - dir_static.d_namlen % 4;
2969 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
2970 dir_static.d_name[dir_static.d_namlen] = '\0';
2971 dirp->dd_loc = dirp->dd_size; /* only one record at a time */
2972 return &dir_static;
2973#endif /* VMS */
2974 }
2975}
2976
2977#ifdef VMS
2978/* readdirver is just like readdir except it returns all versions of a file
2979 as separate entries. */
2980
2981/* ARGUSED */
2982struct direct *
2983readdirver (dirp)
2984 register DIR *dirp; /* stream from opendir */
2985{
2986 register struct dir$_name *dp; /* -> directory data */
2987 register struct dir$_version *dv; /* -> version data */
2988
2989 if (dirp->dd_loc >= dirp->dd_size - sizeof (struct dir$_name))
2990 dirp->dd_loc = dirp->dd_size = 0;
2991
2992 if (dirp->dd_size == 0 /* refill buffer */
2993 && (dirp->dd_size = sys_read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
2994 return 0;
2995
2996 dp = (struct dir$_name *) dirp->dd_buf;
2997 if (dirp->dd_loc == 0)
2998 dirp->dd_loc = (dp->dir$b_namecount & 1) ? dp->dir$b_namecount + 1
2999 : dp->dir$b_namecount;
3000 dv = (struct dir$_version *) &dp->dir$t_name[dirp->dd_loc];
3001 strncpy (dir_static.d_name, dp->dir$t_name, dp->dir$b_namecount);
3002 sprintf (&dir_static.d_name[dp->dir$b_namecount], ";%d", dv->dir$w_version);
3003 dir_static.d_namlen = strlen (dir_static.d_name);
3004 dir_static.d_ino = dv->dir$w_fid_num;
3005 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3
3006 + dir_static.d_namlen - dir_static.d_namlen % 4;
3007 dirp->dd_loc = ((char *) (++dv) - dp->dir$t_name);
3008 return &dir_static;
3009}
3010
3011#endif /* VMS */
3012
3013#endif /* NONSYSTEM_DIR_LIBRARY */
3014\f
3015/* Functions for VMS */
3016#ifdef VMS
91bac16a 3017#include "vms-pwd.h"
86a5659e
JB
3018#include <acldef.h>
3019#include <chpdef.h>
3020#include <jpidef.h>
3021
3022/* Return as a string the VMS error string pertaining to STATUS.
3023 Reuses the same static buffer each time it is called. */
3024
3025char *
3026vmserrstr (status)
3027 int status; /* VMS status code */
3028{
3029 int bufadr[2];
3030 short len;
3031 static char buf[257];
3032
3033 bufadr[0] = sizeof buf - 1;
3034 bufadr[1] = (int) buf;
3035 if (! (SYS$GETMSG (status, &len, bufadr, 0x1, 0) & 1))
3036 return "untranslatable VMS error status";
3037 buf[len] = '\0';
3038 return buf;
3039}
3040
3041#ifdef access
3042#undef access
3043
3044/* The following is necessary because 'access' emulation by VMS C (2.0) does
3045 * not work correctly. (It also doesn't work well in version 2.3.)
3046 */
3047
3048#ifdef VMS4_4
3049
3050#define DESCRIPTOR(name,string) struct dsc$descriptor_s name = \
3051 { strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string }
3052
3053typedef union {
3054 struct {
3055 unsigned short s_buflen;
3056 unsigned short s_code;
3057 char *s_bufadr;
3058 unsigned short *s_retlenadr;
3059 } s;
3060 int end;
3061} item;
3062#define buflen s.s_buflen
3063#define code s.s_code
3064#define bufadr s.s_bufadr
3065#define retlenadr s.s_retlenadr
3066
3067#define R_OK 4 /* test for read permission */
3068#define W_OK 2 /* test for write permission */
3069#define X_OK 1 /* test for execute (search) permission */
3070#define F_OK 0 /* test for presence of file */
3071
3072int
3073sys_access (path, mode)
3074 char *path;
3075 int mode;
3076{
3077 static char *user = NULL;
3078 char dir_fn[512];
3079
3080 /* translate possible directory spec into .DIR file name, so brain-dead
3081 * access can treat the directory like a file. */
3082 if (directory_file_name (path, dir_fn))
3083 path = dir_fn;
3084
3085 if (mode == F_OK)
3086 return access (path, mode);
3087 if (user == NULL && (user = (char *) getenv ("USER")) == NULL)
3088 return -1;
3089 {
3090 int stat;
3091 int flags;
3092 int acces;
3093 unsigned short int dummy;
3094 item itemlst[3];
3095 static int constant = ACL$C_FILE;
3096 DESCRIPTOR (path_desc, path);
3097 DESCRIPTOR (user_desc, user);
3098
3099 flags = 0;
3100 acces = 0;
3101 if ((mode & X_OK) && ((stat = access (path, mode)) < 0 || mode == X_OK))
3102 return stat;
3103 if (mode & R_OK)
3104 acces |= CHP$M_READ;
3105 if (mode & W_OK)
3106 acces |= CHP$M_WRITE;
3107 itemlst[0].buflen = sizeof (int);
3108 itemlst[0].code = CHP$_FLAGS;
3109 itemlst[0].bufadr = (char *) &flags;
3110 itemlst[0].retlenadr = &dummy;
3111 itemlst[1].buflen = sizeof (int);
3112 itemlst[1].code = CHP$_ACCESS;
3113 itemlst[1].bufadr = (char *) &acces;
3114 itemlst[1].retlenadr = &dummy;
3115 itemlst[2].end = CHP$_END;
3116 stat = SYS$CHECK_ACCESS (&constant, &path_desc, &user_desc, itemlst);
3117 return stat == SS$_NORMAL ? 0 : -1;
3118 }
3119}
3120
3121#else /* not VMS4_4 */
3122
3123#include <prvdef.h>
3124#define ACE$M_WRITE 2
3125#define ACE$C_KEYID 1
3126
3127static unsigned short memid, grpid;
3128static unsigned int uic;
3129
3130/* Called from init_sys_modes, so it happens not very often
3131 but at least each time Emacs is loaded. */
3132sys_access_reinit ()
3133{
3134 uic = 0;
3135}
3136
3137int
3138sys_access (filename, type)
3139 char * filename;
3140 int type;
3141{
3142 struct FAB fab;
3143 struct XABPRO xab;
3144 int status, size, i, typecode, acl_controlled;
3145 unsigned int *aclptr, *aclend, aclbuf[60];
3146 union prvdef prvmask;
3147
3148 /* Get UIC and GRP values for protection checking. */
3149 if (uic == 0)
3150 {
3151 status = LIB$GETJPI (&JPI$_UIC, 0, 0, &uic, 0, 0);
3152 if (! (status & 1))
3153 return -1;
3154 memid = uic & 0xFFFF;
3155 grpid = uic >> 16;
3156 }
3157
3158 if (type != 2) /* not checking write access */
3159 return access (filename, type);
3160
3161 /* Check write protection. */
3162
3163#define CHECKPRIV(bit) (prvmask.bit)
3164#define WRITEABLE(field) (! ((xab.xab$w_pro >> field) & XAB$M_NOWRITE))
3165
3166 /* Find privilege bits */
986ffb24 3167 status = SYS$SETPRV (0, 0, 0, prvmask);
86a5659e
JB
3168 if (! (status & 1))
3169 error ("Unable to find privileges: %s", vmserrstr (status));
3170 if (CHECKPRIV (PRV$V_BYPASS))
3171 return 0; /* BYPASS enabled */
3172 fab = cc$rms_fab;
3173 fab.fab$b_fac = FAB$M_GET;
3174 fab.fab$l_fna = filename;
3175 fab.fab$b_fns = strlen (filename);
3176 fab.fab$l_xab = &xab;
3177 xab = cc$rms_xabpro;
3178 xab.xab$l_aclbuf = aclbuf;
3179 xab.xab$w_aclsiz = sizeof (aclbuf);
986ffb24 3180 status = SYS$OPEN (&fab, 0, 0);
86a5659e
JB
3181 if (! (status & 1))
3182 return -1;
986ffb24 3183 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
3184 /* Check system access */
3185 if (CHECKPRIV (PRV$V_SYSPRV) && WRITEABLE (XAB$V_SYS))
3186 return 0;
3187 /* Check ACL entries, if any */
3188 acl_controlled = 0;
3189 if (xab.xab$w_acllen > 0)
3190 {
3191 aclptr = aclbuf;
3192 aclend = &aclbuf[xab.xab$w_acllen / 4];
3193 while (*aclptr && aclptr < aclend)
3194 {
3195 size = (*aclptr & 0xff) / 4;
3196 typecode = (*aclptr >> 8) & 0xff;
3197 if (typecode == ACE$C_KEYID)
3198 for (i = size - 1; i > 1; i--)
3199 if (aclptr[i] == uic)
3200 {
3201 acl_controlled = 1;
3202 if (aclptr[1] & ACE$M_WRITE)
3203 return 0; /* Write access through ACL */
3204 }
3205 aclptr = &aclptr[size];
3206 }
3207 if (acl_controlled) /* ACL specified, prohibits write access */
3208 return -1;
3209 }
3210 /* No ACL entries specified, check normal protection */
3211 if (WRITEABLE (XAB$V_WLD)) /* World writeable */
3212 return 0;
3213 if (WRITEABLE (XAB$V_GRP) &&
3214 (unsigned short) (xab.xab$l_uic >> 16) == grpid)
3215 return 0; /* Group writeable */
3216 if (WRITEABLE (XAB$V_OWN) &&
3217 (xab.xab$l_uic & 0xFFFF) == memid)
3218 return 0; /* Owner writeable */
3219
3220 return -1; /* Not writeable */
3221}
3222#endif /* not VMS4_4 */
3223#endif /* access */
3224
3225static char vtbuf[NAM$C_MAXRSS+1];
3226
3227/* translate a vms file spec to a unix path */
3228char *
3229sys_translate_vms (vfile)
3230 char * vfile;
3231{
3232 char * p;
3233 char * targ;
3234
3235 if (!vfile)
3236 return 0;
3237
3238 targ = vtbuf;
3239
3240 /* leading device or logical name is a root directory */
3241 if (p = strchr (vfile, ':'))
3242 {
3243 *targ++ = '/';
3244 while (vfile < p)
3245 *targ++ = *vfile++;
3246 vfile++;
3247 *targ++ = '/';
3248 }
3249 p = vfile;
3250 if (*p == '[' || *p == '<')
3251 {
3252 while (*++vfile != *p + 2)
3253 switch (*vfile)
3254 {
3255 case '.':
3256 if (vfile[-1] == *p)
3257 *targ++ = '.';
3258 *targ++ = '/';
3259 break;
3260
3261 case '-':
3262 *targ++ = '.';
3263 *targ++ = '.';
3264 break;
3265
3266 default:
3267 *targ++ = *vfile;
3268 break;
3269 }
3270 vfile++;
3271 *targ++ = '/';
3272 }
3273 while (*vfile)
3274 *targ++ = *vfile++;
3275
3276 return vtbuf;
3277}
3278
3279static char utbuf[NAM$C_MAXRSS+1];
3280
3281/* translate a unix path to a VMS file spec */
3282char *
3283sys_translate_unix (ufile)
3284 char * ufile;
3285{
3286 int slash_seen = 0;
3287 char *p;
3288 char * targ;
3289
3290 if (!ufile)
3291 return 0;
3292
3293 targ = utbuf;
3294
3295 if (*ufile == '/')
3296 {
3297 ufile++;
3298 }
3299
3300 while (*ufile)
3301 {
3302 switch (*ufile)
3303 {
3304 case '/':
3305 if (slash_seen)
3306 if (index (&ufile[1], '/'))
3307 *targ++ = '.';
3308 else
3309 *targ++ = ']';
3310 else
3311 {
3312 *targ++ = ':';
3313 if (index (&ufile[1], '/'))
3314 *targ++ = '[';
3315 slash_seen = 1;
3316 }
3317 break;
3318
3319 case '.':
3320 if (strncmp (ufile, "./", 2) == 0)
3321 {
3322 if (!slash_seen)
3323 {
3324 *targ++ = '[';
3325 slash_seen = 1;
3326 }
3327 ufile++; /* skip the dot */
3328 if (index (&ufile[1], '/'))
3329 *targ++ = '.';
3330 else
3331 *targ++ = ']';
3332 }
3333 else if (strncmp (ufile, "../", 3) == 0)
3334 {
3335 if (!slash_seen)
3336 {
3337 *targ++ = '[';
3338 slash_seen = 1;
3339 }
3340 *targ++ = '-';
3341 ufile += 2; /* skip the dots */
3342 if (index (&ufile[1], '/'))
3343 *targ++ = '.';
3344 else
3345 *targ++ = ']';
3346 }
3347 else
3348 *targ++ = *ufile;
3349 break;
3350
3351 default:
3352 *targ++ = *ufile;
3353 break;
3354 }
3355 ufile++;
3356 }
3357 *targ = '\0';
3358
3359 return utbuf;
3360}
3361
3362char *
3363getwd (pathname)
3364 char *pathname;
3365{
3366 char *ptr;
210b2b4f 3367 extern char *getcwd ();
86a5659e 3368
210b2b4f
JB
3369#define MAXPATHLEN 1024
3370
9ac0d9e0 3371 ptr = xmalloc (MAXPATHLEN);
210b2b4f
JB
3372 getcwd (ptr, MAXPATHLEN);
3373 strcpy (pathname, ptr);
9ac0d9e0 3374 xfree (ptr);
210b2b4f
JB
3375
3376 return pathname;
86a5659e
JB
3377}
3378
3379getppid ()
3380{
3381 long item_code = JPI$_OWNER;
3382 unsigned long parent_id;
3383 int status;
3384
3385 if (((status = LIB$GETJPI (&item_code, 0, 0, &parent_id)) & 1) == 0)
3386 {
3387 errno = EVMSERR;
3388 vaxc$errno = status;
3389 return -1;
3390 }
3391 return parent_id;
3392}
3393
3394#undef getuid
3395unsigned
3396sys_getuid ()
3397{
3398 return (getgid () << 16) | getuid ();
3399}
3400
3401int
3402sys_read (fildes, buf, nbyte)
3403 int fildes;
3404 char *buf;
3405 unsigned int nbyte;
3406{
3407 return read (fildes, buf, (nbyte < MAXIOSIZE ? nbyte : MAXIOSIZE));
3408}
3409
3410#if 0
3411int
3412sys_write (fildes, buf, nbyte)
3413 int fildes;
3414 char *buf;
3415 unsigned int nbyte;
3416{
3417 register int nwrote, rtnval = 0;
3418
3419 while (nbyte > MAXIOSIZE && (nwrote = write (fildes, buf, MAXIOSIZE)) > 0) {
3420 nbyte -= nwrote;
3421 buf += nwrote;
3422 rtnval += nwrote;
3423 }
3424 if (nwrote < 0)
3425 return rtnval ? rtnval : -1;
3426 if ((nwrote = write (fildes, buf, nbyte)) < 0)
3427 return rtnval ? rtnval : -1;
3428 return (rtnval + nwrote);
3429}
3430#endif /* 0 */
3431
3432/*
3433 * VAX/VMS VAX C RTL really loses. It insists that records
3434 * end with a newline (carriage return) character, and if they
3435 * don't it adds one (nice of it isn't it!)
3436 *
3437 * Thus we do this stupidity below.
3438 */
3439
3440int
3441sys_write (fildes, buf, nbytes)
3442 int fildes;
3443 char *buf;
3444 unsigned int nbytes;
3445{
3446 register char *p;
3447 register char *e;
23b0668c
JB
3448 int sum = 0;
3449 struct stat st;
3450
3451 fstat (fildes, &st);
86a5659e 3452 p = buf;
86a5659e
JB
3453 while (nbytes > 0)
3454 {
23b0668c
JB
3455 int len, retval;
3456
3457 /* Handle fixed-length files with carriage control. */
3458 if (st.st_fab_rfm == FAB$C_FIX
3459 && ((st.st_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
3460 {
3461 len = st.st_fab_mrs;
3462 retval = write (fildes, p, min (len, nbytes));
3463 if (retval != len)
3464 return -1;
3465 retval++; /* This skips the implied carriage control */
3466 }
3467 else
3468 {
3469 e = p + min (MAXIOSIZE, nbytes) - 1;
3470 while (*e != '\n' && e > p) e--;
3471 if (p == e) /* Ok.. so here we add a newline... sigh. */
3472 e = p + min (MAXIOSIZE, nbytes) - 1;
3473 len = e + 1 - p;
3474 retval = write (fildes, p, len);
3475 if (retval != len)
3476 return -1;
3477 }
3478 p += retval;
3479 sum += retval;
86a5659e
JB
3480 nbytes -= retval;
3481 }
3482 return sum;
3483}
3484
3485/* Create file NEW copying its attributes from file OLD. If
3486 OLD is 0 or does not exist, create based on the value of
3487 vms_stmlf_recfm. */
3488
3489/* Protection value the file should ultimately have.
3490 Set by create_copy_attrs, and use by rename_sansversions. */
3491static unsigned short int fab_final_pro;
3492
3493int
3494creat_copy_attrs (old, new)
3495 char *old, *new;
3496{
3497 struct FAB fab = cc$rms_fab;
3498 struct XABPRO xabpro;
3499 char aclbuf[256]; /* Choice of size is arbitrary. See below. */
3500 extern int vms_stmlf_recfm;
3501
3502 if (old)
3503 {
3504 fab.fab$b_fac = FAB$M_GET;
3505 fab.fab$l_fna = old;
3506 fab.fab$b_fns = strlen (old);
3507 fab.fab$l_xab = (char *) &xabpro;
3508 xabpro = cc$rms_xabpro;
3509 xabpro.xab$l_aclbuf = aclbuf;
3510 xabpro.xab$w_aclsiz = sizeof aclbuf;
3511 /* Call $OPEN to fill in the fab & xabpro fields. */
986ffb24 3512 if (SYS$OPEN (&fab, 0, 0) & 1)
86a5659e 3513 {
986ffb24 3514 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
3515 fab.fab$l_alq = 0; /* zero the allocation quantity */
3516 if (xabpro.xab$w_acllen > 0)
3517 {
3518 if (xabpro.xab$w_acllen > sizeof aclbuf)
3519 /* If the acl buffer was too short, redo open with longer one.
3520 Wouldn't need to do this if there were some system imposed
3521 limit on the size of an ACL, but I can't find any such. */
3522 {
3523 xabpro.xab$l_aclbuf = (char *) alloca (xabpro.xab$w_acllen);
3524 xabpro.xab$w_aclsiz = xabpro.xab$w_acllen;
986ffb24
JB
3525 if (SYS$OPEN (&fab, 0, 0) & 1)
3526 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
3527 else
3528 old = 0;
3529 }
3530 }
3531 else
3532 xabpro.xab$l_aclbuf = 0;
3533 }
3534 else
3535 old = 0;
3536 }
3537 fab.fab$l_fna = new;
3538 fab.fab$b_fns = strlen (new);
3539 if (!old)
3540 {
3541 fab.fab$l_xab = 0;
3542 fab.fab$b_rfm = vms_stmlf_recfm ? FAB$C_STMLF : FAB$C_VAR;
3543 fab.fab$b_rat = FAB$M_CR;
3544 }
3545
3546 /* Set the file protections such that we will be able to manipulate
3547 this file. Once we are done writing and renaming it, we will set
3548 the protections back. */
3549 if (old)
3550 fab_final_pro = xabpro.xab$w_pro;
3551 else
986ffb24 3552 SYS$SETDFPROT (0, &fab_final_pro);
86a5659e
JB
3553 xabpro.xab$w_pro &= 0xff0f; /* set O:rewd for now. This is set back later. */
3554
3555 /* Create the new file with either default attrs or attrs copied
3556 from old file. */
3557 if (!(SYS$CREATE (&fab, 0, 0) & 1))
3558 return -1;
986ffb24 3559 SYS$CLOSE (&fab, 0, 0);
86a5659e
JB
3560 /* As this is a "replacement" for creat, return a file descriptor
3561 opened for writing. */
3562 return open (new, O_WRONLY);
3563}
3564
3565#ifdef creat
3566#undef creat
3567#include <varargs.h>
3568#ifdef __GNUC__
3569#ifndef va_count
3570#define va_count(X) ((X) = *(((int *) &(va_alist)) - 1))
3571#endif
3572#endif
3573
3574sys_creat (va_alist)
3575 va_dcl
3576{
eb8c3be9 3577 va_list list_incrementer;
86a5659e
JB
3578 char *name;
3579 int mode;
3580 int rfd; /* related file descriptor */
3581 int fd; /* Our new file descriptor */
3582 int count;
3583 struct stat st_buf;
3584 char rfm[12];
3585 char rat[15];
3586 char mrs[13];
3587 char fsz[13];
3588 extern int vms_stmlf_recfm;
3589
3590 va_count (count);
eb8c3be9
JB
3591 va_start (list_incrementer);
3592 name = va_arg (list_incrementer, char *);
3593 mode = va_arg (list_incrementer, int);
86a5659e 3594 if (count > 2)
eb8c3be9
JB
3595 rfd = va_arg (list_incrementer, int);
3596 va_end (list_incrementer);
86a5659e
JB
3597 if (count > 2)
3598 {
3599 /* Use information from the related file descriptor to set record
3600 format of the newly created file. */
3601 fstat (rfd, &st_buf);
3602 switch (st_buf.st_fab_rfm)
3603 {
3604 case FAB$C_FIX:
3605 strcpy (rfm, "rfm = fix");
3606 sprintf (mrs, "mrs = %d", st_buf.st_fab_mrs);
3607 strcpy (rat, "rat = ");
3608 if (st_buf.st_fab_rat & FAB$M_CR)
3609 strcat (rat, "cr");
3610 else if (st_buf.st_fab_rat & FAB$M_FTN)
3611 strcat (rat, "ftn");
3612 else if (st_buf.st_fab_rat & FAB$M_PRN)
3613 strcat (rat, "prn");
3614 if (st_buf.st_fab_rat & FAB$M_BLK)
3615 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
3616 strcat (rat, ", blk");
3617 else
3618 strcat (rat, "blk");
3619 return creat (name, 0, rfm, rat, mrs);
3620
3621 case FAB$C_VFC:
3622 strcpy (rfm, "rfm = vfc");
3623 sprintf (fsz, "fsz = %d", st_buf.st_fab_fsz);
3624 strcpy (rat, "rat = ");
3625 if (st_buf.st_fab_rat & FAB$M_CR)
3626 strcat (rat, "cr");
3627 else if (st_buf.st_fab_rat & FAB$M_FTN)
3628 strcat (rat, "ftn");
3629 else if (st_buf.st_fab_rat & FAB$M_PRN)
3630 strcat (rat, "prn");
3631 if (st_buf.st_fab_rat & FAB$M_BLK)
3632 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
3633 strcat (rat, ", blk");
3634 else
3635 strcat (rat, "blk");
3636 return creat (name, 0, rfm, rat, fsz);
3637
3638 case FAB$C_STM:
3639 strcpy (rfm, "rfm = stm");
3640 break;
3641
3642 case FAB$C_STMCR:
3643 strcpy (rfm, "rfm = stmcr");
3644 break;
3645
3646 case FAB$C_STMLF:
3647 strcpy (rfm, "rfm = stmlf");
3648 break;
3649
3650 case FAB$C_UDF:
3651 strcpy (rfm, "rfm = udf");
3652 break;
3653
3654 case FAB$C_VAR:
3655 strcpy (rfm, "rfm = var");
3656 break;
3657 }
3658 strcpy (rat, "rat = ");
3659 if (st_buf.st_fab_rat & FAB$M_CR)
3660 strcat (rat, "cr");
3661 else if (st_buf.st_fab_rat & FAB$M_FTN)
3662 strcat (rat, "ftn");
3663 else if (st_buf.st_fab_rat & FAB$M_PRN)
3664 strcat (rat, "prn");
3665 if (st_buf.st_fab_rat & FAB$M_BLK)
3666 if (st_buf.st_fab_rat & (FAB$M_CR|FAB$M_FTN|FAB$M_PRN))
3667 strcat (rat, ", blk");
3668 else
3669 strcat (rat, "blk");
3670 }
3671 else
3672 {
3673 strcpy (rfm, vms_stmlf_recfm ? "rfm = stmlf" : "rfm=var");
3674 strcpy (rat, "rat=cr");
3675 }
3676 /* Until the VAX C RTL fixes the many bugs with modes, always use
3677 mode 0 to get the user's default protection. */
3678 fd = creat (name, 0, rfm, rat);
3679 if (fd < 0 && errno == EEXIST)
3680 {
3681 if (unlink (name) < 0)
3682 report_file_error ("delete", build_string (name));
3683 fd = creat (name, 0, rfm, rat);
3684 }
3685 return fd;
3686}
3687#endif /* creat */
3688
3689/* fwrite to stdout is S L O W. Speed it up by using fputc...*/
3690sys_fwrite (ptr, size, num, fp)
3691 register char * ptr;
3692 FILE * fp;
3693{
3694 register int tot = num * size;
3695
3696 while (tot--)
3697 fputc (*ptr++, fp);
3698}
3699
3700/*
3701 * The VMS C library routine creat actually creates a new version of an
3702 * existing file rather than truncating the old version. There are times
3703 * when this is not the desired behavior, for instance, when writing an
3704 * auto save file (you only want one version), or when you don't have
3705 * write permission in the directory containing the file (but the file
3706 * itself is writable). Hence this routine, which is equivalent to
3707 * "close (creat (fn, 0));" on Unix if fn already exists.
3708 */
3709int
3710vms_truncate (fn)
3711 char *fn;
3712{
3713 struct FAB xfab = cc$rms_fab;
3714 struct RAB xrab = cc$rms_rab;
3715 int status;
3716
3717 xfab.fab$l_fop = FAB$M_TEF; /* free allocated but unused blocks on close */
3718 xfab.fab$b_fac = FAB$M_TRN | FAB$M_GET; /* allow truncate and get access */
3719 xfab.fab$b_shr = FAB$M_NIL; /* allow no sharing - file must be locked */
3720 xfab.fab$l_fna = fn;
3721 xfab.fab$b_fns = strlen (fn);
3722 xfab.fab$l_dna = ";0"; /* default to latest version of the file */
3723 xfab.fab$b_dns = 2;
3724 xrab.rab$l_fab = &xfab;
3725
3726 /* This gibberish opens the file, positions to the first record, and
3727 deletes all records from there until the end of file. */
986ffb24 3728 if ((SYS$OPEN (&xfab) & 01) == 01)
86a5659e 3729 {
986ffb24
JB
3730 if ((SYS$CONNECT (&xrab) & 01) == 01 &&
3731 (SYS$FIND (&xrab) & 01) == 01 &&
3732 (SYS$TRUNCATE (&xrab) & 01) == 01)
86a5659e
JB
3733 status = 0;
3734 else
3735 status = -1;
3736 }
3737 else
3738 status = -1;
986ffb24 3739 SYS$CLOSE (&xfab);
86a5659e
JB
3740 return status;
3741}
3742
3743/* Define this symbol to actually read SYSUAF.DAT. This requires either
3744 SYSPRV or a readable SYSUAF.DAT. */
3745
3746#ifdef READ_SYSUAF
3747/*
3748 * getuaf.c
3749 *
3750 * Routine to read the VMS User Authorization File and return
3751 * a specific user's record.
3752 */
3753
3754static struct UAF retuaf;
3755
3756struct UAF *
3757get_uaf_name (uname)
3758 char * uname;
3759{
3760 register status;
3761 struct FAB uaf_fab;
3762 struct RAB uaf_rab;
3763
3764 uaf_fab = cc$rms_fab;
3765 uaf_rab = cc$rms_rab;
3766 /* initialize fab fields */
3767 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
3768 uaf_fab.fab$b_fns = 21;
3769 uaf_fab.fab$b_fac = FAB$M_GET;
3770 uaf_fab.fab$b_org = FAB$C_IDX;
3771 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
3772 /* initialize rab fields */
3773 uaf_rab.rab$l_fab = &uaf_fab;
3774 /* open the User Authorization File */
986ffb24 3775 status = SYS$OPEN (&uaf_fab);
86a5659e
JB
3776 if (!(status&1))
3777 {
3778 errno = EVMSERR;
3779 vaxc$errno = status;
3780 return 0;
3781 }
986ffb24 3782 status = SYS$CONNECT (&uaf_rab);
86a5659e
JB
3783 if (!(status&1))
3784 {
3785 errno = EVMSERR;
3786 vaxc$errno = status;
3787 return 0;
3788 }
3789 /* read the requested record - index is in uname */
3790 uaf_rab.rab$l_kbf = uname;
3791 uaf_rab.rab$b_ksz = strlen (uname);
3792 uaf_rab.rab$b_rac = RAB$C_KEY;
3793 uaf_rab.rab$l_ubf = (char *)&retuaf;
3794 uaf_rab.rab$w_usz = sizeof retuaf;
986ffb24 3795 status = SYS$GET (&uaf_rab);
86a5659e
JB
3796 if (!(status&1))
3797 {
3798 errno = EVMSERR;
3799 vaxc$errno = status;
3800 return 0;
3801 }
3802 /* close the User Authorization File */
986ffb24 3803 status = SYS$DISCONNECT (&uaf_rab);
86a5659e
JB
3804 if (!(status&1))
3805 {
3806 errno = EVMSERR;
3807 vaxc$errno = status;
3808 return 0;
3809 }
986ffb24 3810 status = SYS$CLOSE (&uaf_fab);
86a5659e
JB
3811 if (!(status&1))
3812 {
3813 errno = EVMSERR;
3814 vaxc$errno = status;
3815 return 0;
3816 }
3817 return &retuaf;
3818}
3819
3820struct UAF *
3821get_uaf_uic (uic)
3822 unsigned long uic;
3823{
3824 register status;
3825 struct FAB uaf_fab;
3826 struct RAB uaf_rab;
3827
3828 uaf_fab = cc$rms_fab;
3829 uaf_rab = cc$rms_rab;
3830 /* initialize fab fields */
3831 uaf_fab.fab$l_fna = "SYS$SYSTEM:SYSUAF.DAT";
3832 uaf_fab.fab$b_fns = 21;
3833 uaf_fab.fab$b_fac = FAB$M_GET;
3834 uaf_fab.fab$b_org = FAB$C_IDX;
3835 uaf_fab.fab$b_shr = FAB$M_GET|FAB$M_PUT|FAB$M_UPD|FAB$M_DEL;
3836 /* initialize rab fields */
3837 uaf_rab.rab$l_fab = &uaf_fab;
3838 /* open the User Authorization File */
986ffb24 3839 status = SYS$OPEN (&uaf_fab);
86a5659e
JB
3840 if (!(status&1))
3841 {
3842 errno = EVMSERR;
3843 vaxc$errno = status;
3844 return 0;
3845 }
986ffb24 3846 status = SYS$CONNECT (&uaf_rab);
86a5659e
JB
3847 if (!(status&1))
3848 {
3849 errno = EVMSERR;
3850 vaxc$errno = status;
3851 return 0;
3852 }
3853 /* read the requested record - index is in uic */
3854 uaf_rab.rab$b_krf = 1; /* 1st alternate key */
3855 uaf_rab.rab$l_kbf = (char *) &uic;
3856 uaf_rab.rab$b_ksz = sizeof uic;
3857 uaf_rab.rab$b_rac = RAB$C_KEY;
3858 uaf_rab.rab$l_ubf = (char *)&retuaf;
3859 uaf_rab.rab$w_usz = sizeof retuaf;
986ffb24 3860 status = SYS$GET (&uaf_rab);
86a5659e
JB
3861 if (!(status&1))
3862 {
3863 errno = EVMSERR;
3864 vaxc$errno = status;
3865 return 0;
3866 }
3867 /* close the User Authorization File */
986ffb24 3868 status = SYS$DISCONNECT (&uaf_rab);
86a5659e
JB
3869 if (!(status&1))
3870 {
3871 errno = EVMSERR;
3872 vaxc$errno = status;
3873 return 0;
3874 }
986ffb24 3875 status = SYS$CLOSE (&uaf_fab);
86a5659e
JB
3876 if (!(status&1))
3877 {
3878 errno = EVMSERR;
3879 vaxc$errno = status;
3880 return 0;
3881 }
3882 return &retuaf;
3883}
3884
3885static struct passwd retpw;
3886
3887struct passwd *
3888cnv_uaf_pw (up)
3889 struct UAF * up;
3890{
3891 char * ptr;
3892
3893 /* copy these out first because if the username is 32 chars, the next
3894 section will overwrite the first byte of the UIC */
3895 retpw.pw_uid = up->uaf$w_mem;
3896 retpw.pw_gid = up->uaf$w_grp;
3897
3898 /* I suppose this is not the best sytle, to possibly overwrite one
3899 byte beyond the end of the field, but what the heck... */
3900 ptr = &up->uaf$t_username[UAF$S_USERNAME];
3901 while (ptr[-1] == ' ')
3902 ptr--;
3903 *ptr = '\0';
3904 strcpy (retpw.pw_name, up->uaf$t_username);
3905
3906 /* the rest of these are counted ascii strings */
3907 strncpy (retpw.pw_gecos, &up->uaf$t_owner[1], up->uaf$t_owner[0]);
3908 retpw.pw_gecos[up->uaf$t_owner[0]] = '\0';
3909 strncpy (retpw.pw_dir, &up->uaf$t_defdev[1], up->uaf$t_defdev[0]);
3910 retpw.pw_dir[up->uaf$t_defdev[0]] = '\0';
3911 strncat (retpw.pw_dir, &up->uaf$t_defdir[1], up->uaf$t_defdir[0]);
3912 retpw.pw_dir[up->uaf$t_defdev[0] + up->uaf$t_defdir[0]] = '\0';
3913 strncpy (retpw.pw_shell, &up->uaf$t_defcli[1], up->uaf$t_defcli[0]);
3914 retpw.pw_shell[up->uaf$t_defcli[0]] = '\0';
3915
3916 return &retpw;
3917}
3918#else /* not READ_SYSUAF */
3919static struct passwd retpw;
3920#endif /* not READ_SYSUAF */
3921
3922struct passwd *
3923getpwnam (name)
3924 char * name;
3925{
3926#ifdef READ_SYSUAF
3927 struct UAF *up;
3928#else
3929 char * user;
3930 char * dir;
3931 unsigned char * full;
3932#endif /* READ_SYSUAF */
3933 char *ptr = name;
3934
3935 while (*ptr)
3936 {
3937 if ('a' <= *ptr && *ptr <= 'z')
3938 *ptr -= 040;
3939 ptr++;
3940 }
3941#ifdef READ_SYSUAF
3942 if (!(up = get_uaf_name (name)))
3943 return 0;
3944 return cnv_uaf_pw (up);
3945#else
3946 if (strcmp (name, getenv ("USER")) == 0)
3947 {
3948 retpw.pw_uid = getuid ();
3949 retpw.pw_gid = getgid ();
3950 strcpy (retpw.pw_name, name);
3951 if (full = egetenv ("FULLNAME"))
3952 strcpy (retpw.pw_gecos, full);
3953 else
3954 *retpw.pw_gecos = '\0';
3955 strcpy (retpw.pw_dir, egetenv ("HOME"));
3956 *retpw.pw_shell = '\0';
3957 return &retpw;
3958 }
3959 else
3960 return 0;
3961#endif /* not READ_SYSUAF */
3962}
3963
3964struct passwd *
3965getpwuid (uid)
3966 unsigned long uid;
3967{
3968#ifdef READ_SYSUAF
3969 struct UAF * up;
3970
3971 if (!(up = get_uaf_uic (uid)))
3972 return 0;
3973 return cnv_uaf_pw (up);
3974#else
3975 if (uid == sys_getuid ())
3976 return getpwnam (egetenv ("USER"));
3977 else
3978 return 0;
3979#endif /* not READ_SYSUAF */
3980}
3981
3982/* return total address space available to the current process. This is
3983 the sum of the current p0 size, p1 size and free page table entries
3984 available. */
3985vlimit ()
3986{
3987 int item_code;
3988 unsigned long free_pages;
3989 unsigned long frep0va;
3990 unsigned long frep1va;
3991 register status;
3992
3993 item_code = JPI$_FREPTECNT;
3994 if (((status = LIB$GETJPI (&item_code, 0, 0, &free_pages)) & 1) == 0)
3995 {
3996 errno = EVMSERR;
3997 vaxc$errno = status;
3998 return -1;
3999 }
4000 free_pages *= 512;
4001
4002 item_code = JPI$_FREP0VA;
4003 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep0va)) & 1) == 0)
4004 {
4005 errno = EVMSERR;
4006 vaxc$errno = status;
4007 return -1;
4008 }
4009 item_code = JPI$_FREP1VA;
4010 if (((status = LIB$GETJPI (&item_code, 0, 0, &frep1va)) & 1) == 0)
4011 {
4012 errno = EVMSERR;
4013 vaxc$errno = status;
4014 return -1;
4015 }
4016
4017 return free_pages + frep0va + (0x7fffffff - frep1va);
4018}
4019
4020define_logical_name (varname, string)
4021 char *varname;
4022 char *string;
4023{
4024 struct dsc$descriptor_s strdsc =
4025 {strlen (string), DSC$K_DTYPE_T, DSC$K_CLASS_S, string};
4026 struct dsc$descriptor_s envdsc =
4027 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4028 struct dsc$descriptor_s lnmdsc =
4029 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4030
4031 return LIB$SET_LOGICAL (&envdsc, &strdsc, &lnmdsc, 0, 0);
4032}
4033
4034delete_logical_name (varname)
4035 char *varname;
4036{
4037 struct dsc$descriptor_s envdsc =
4038 {strlen (varname), DSC$K_DTYPE_T, DSC$K_CLASS_S, varname};
4039 struct dsc$descriptor_s lnmdsc =
4040 {7, DSC$K_DTYPE_T, DSC$K_CLASS_S, "LNM$JOB"};
4041
4042 return LIB$DELETE_LOGICAL (&envdsc, &lnmdsc);
4043}
4044
4045ulimit ()
4046{}
4047
86a5659e
JB
4048setpgrp ()
4049{}
4050
4051execvp ()
4052{
4053 error ("execvp system call not implemented");
4054}
4055
4056int
4057rename (from, to)
4058 char *from, *to;
4059{
4060 int status;
4061 struct FAB from_fab = cc$rms_fab, to_fab = cc$rms_fab;
4062 struct NAM from_nam = cc$rms_nam, to_nam = cc$rms_nam;
4063 char from_esn[NAM$C_MAXRSS];
4064 char to_esn[NAM$C_MAXRSS];
4065
4066 from_fab.fab$l_fna = from;
4067 from_fab.fab$b_fns = strlen (from);
4068 from_fab.fab$l_nam = &from_nam;
4069 from_fab.fab$l_fop = FAB$M_NAM;
4070
4071 from_nam.nam$l_esa = from_esn;
4072 from_nam.nam$b_ess = sizeof from_esn;
4073
4074 to_fab.fab$l_fna = to;
4075 to_fab.fab$b_fns = strlen (to);
4076 to_fab.fab$l_nam = &to_nam;
4077 to_fab.fab$l_fop = FAB$M_NAM;
4078
4079 to_nam.nam$l_esa = to_esn;
4080 to_nam.nam$b_ess = sizeof to_esn;
4081
4082 status = SYS$RENAME (&from_fab, 0, 0, &to_fab);
4083
4084 if (status & 1)
4085 return 0;
4086 else
4087 {
4088 if (status == RMS$_DEV)
4089 errno = EXDEV;
4090 else
4091 errno = EVMSERR;
4092 vaxc$errno = status;
4093 return -1;
4094 }
4095}
4096
4097/* This function renames a file like `rename', but it strips
4098 the version number from the "to" filename, such that the "to" file is
4099 will always be a new version. It also sets the file protection once it is
4100 finished. The protection that we will use is stored in fab_final_pro,
4101 and was set when we did a creat_copy_attrs to create the file that we
4102 are renaming.
4103
4104 We could use the chmod function, but Eunichs uses 3 bits per user category
eb8c3be9 4105 to describe the protection, and VMS uses 4 (write and delete are separate
86a5659e
JB
4106 bits). To maintain portability, the VMS implementation of `chmod' wires
4107 the W and D bits together. */
4108
4109
4110static struct fibdef fib; /* We need this initialized to zero */
4111char vms_file_written[NAM$C_MAXRSS];
4112
4113int
4114rename_sans_version (from,to)
4115 char *from, *to;
4116{
4117 short int chan;
4118 int stat;
4119 short int iosb[4];
4120 int status;
4121 struct FAB to_fab = cc$rms_fab;
4122 struct NAM to_nam = cc$rms_nam;
4123 struct dsc$descriptor fib_d ={sizeof (fib),0,0,(char*) &fib};
4124 struct dsc$descriptor fib_attr[2]
4125 = {{sizeof (fab_final_pro),ATR$C_FPRO,0,(char*) &fab_final_pro},{0,0,0,0}};
4126 char to_esn[NAM$C_MAXRSS];
4127
4128 $DESCRIPTOR (disk,to_esn);
4129
4130 to_fab.fab$l_fna = to;
4131 to_fab.fab$b_fns = strlen (to);
4132 to_fab.fab$l_nam = &to_nam;
4133 to_fab.fab$l_fop = FAB$M_NAM;
4134
4135 to_nam.nam$l_esa = to_esn;
4136 to_nam.nam$b_ess = sizeof to_esn;
4137
4138 status = SYS$PARSE (&to_fab, 0, 0); /* figure out the full file name */
4139
4140 if (to_nam.nam$l_fnb && NAM$M_EXP_VER)
4141 *(to_nam.nam$l_ver) = '\0';
4142
4143 stat = rename (from, to_esn);
4144 if (stat < 0)
4145 return stat;
4146
4147 strcpy (vms_file_written, to_esn);
4148
4149 to_fab.fab$l_fna = vms_file_written; /* this points to the versionless name */
4150 to_fab.fab$b_fns = strlen (vms_file_written);
4151
4152 /* Now set the file protection to the correct value */
986ffb24 4153 SYS$OPEN (&to_fab, 0, 0); /* This fills in the nam$w_fid fields */
86a5659e
JB
4154
4155 /* Copy these fields into the fib */
4156 fib.fib$r_fid_overlay.fib$w_fid[0] = to_nam.nam$w_fid[0];
4157 fib.fib$r_fid_overlay.fib$w_fid[1] = to_nam.nam$w_fid[1];
4158 fib.fib$r_fid_overlay.fib$w_fid[2] = to_nam.nam$w_fid[2];
4159
986ffb24 4160 SYS$CLOSE (&to_fab, 0, 0);
86a5659e 4161
986ffb24 4162 stat = SYS$ASSIGN (&disk, &chan, 0, 0); /* open a channel to the disk */
86a5659e 4163 if (!stat)
986ffb24
JB
4164 LIB$SIGNAL (stat);
4165 stat = SYS$QIOW (0, chan, IO$_MODIFY, iosb, 0, 0, &fib_d,
86a5659e
JB
4166 0, 0, 0, &fib_attr, 0);
4167 if (!stat)
986ffb24
JB
4168 LIB$SIGNAL (stat);
4169 stat = SYS$DASSGN (chan);
86a5659e 4170 if (!stat)
986ffb24 4171 LIB$SIGNAL (stat);
0137dbf7 4172 strcpy (vms_file_written, to_esn); /* We will write this to the terminal*/
86a5659e
JB
4173 return 0;
4174}
4175
4176link (file, new)
4177 char * file, * new;
4178{
4179 register status;
4180 struct FAB fab;
4181 struct NAM nam;
4182 unsigned short fid[3];
4183 char esa[NAM$C_MAXRSS];
4184
4185 fab = cc$rms_fab;
4186 fab.fab$l_fop = FAB$M_OFP;
4187 fab.fab$l_fna = file;
4188 fab.fab$b_fns = strlen (file);
4189 fab.fab$l_nam = &nam;
4190
4191 nam = cc$rms_nam;
4192 nam.nam$l_esa = esa;
4193 nam.nam$b_ess = NAM$C_MAXRSS;
4194
4195 status = SYS$PARSE (&fab);
4196 if ((status & 1) == 0)
4197 {
4198 errno = EVMSERR;
4199 vaxc$errno = status;
4200 return -1;
4201 }
4202 status = SYS$SEARCH (&fab);
4203 if ((status & 1) == 0)
4204 {
4205 errno = EVMSERR;
4206 vaxc$errno = status;
4207 return -1;
4208 }
4209
4210 fid[0] = nam.nam$w_fid[0];
4211 fid[1] = nam.nam$w_fid[1];
4212 fid[2] = nam.nam$w_fid[2];
4213
4214 fab.fab$l_fna = new;
4215 fab.fab$b_fns = strlen (new);
4216
4217 status = SYS$PARSE (&fab);
4218 if ((status & 1) == 0)
4219 {
4220 errno = EVMSERR;
4221 vaxc$errno = status;
4222 return -1;
4223 }
4224
4225 nam.nam$w_fid[0] = fid[0];
4226 nam.nam$w_fid[1] = fid[1];
4227 nam.nam$w_fid[2] = fid[2];
4228
4229 nam.nam$l_esa = nam.nam$l_name;
4230 nam.nam$b_esl = nam.nam$b_name + nam.nam$b_type + nam.nam$b_ver;
4231
4232 status = SYS$ENTER (&fab);
4233 if ((status & 1) == 0)
4234 {
4235 errno = EVMSERR;
4236 vaxc$errno = status;
4237 return -1;
4238 }
4239
4240 return 0;
4241}
4242
4243croak (badfunc)
4244 char *badfunc;
4245{
4246 printf ("%s not yet implemented\r\n", badfunc);
4247 reset_sys_modes ();
4248 exit (1);
4249}
4250
4251long
4252random ()
4253{
4254 /* Arrange to return a range centered on zero. */
4255 return rand () - (1 << 30);
4256}
4257
4258srandom (seed)
4259{
4260 srand (seed);
4261}
4262#endif /* VMS */
4263\f
4264#ifdef AIX
4265
4266/* Called from init_sys_modes. */
4267hft_init ()
4268{
4269 int junk;
4270
4271 /* If we're not on an HFT we shouldn't do any of this. We determine
4272 if we are on an HFT by trying to get an HFT error code. If this
4273 call fails, we're not on an HFT. */
4274#ifdef IBMR2AIX
4275 if (ioctl (0, HFQERROR, &junk) < 0)
4276 return;
4277#else /* not IBMR2AIX */
4278 if (ioctl (0, HFQEIO, 0) < 0)
4279 return;
4280#endif /* not IBMR2AIX */
4281
4282 /* On AIX the default hft keyboard mapping uses backspace rather than delete
4283 as the rubout key's ASCII code. Here this is changed. The bug is that
4284 there's no way to determine the old mapping, so in reset_sys_modes
4285 we need to assume that the normal map had been present. Of course, this
4286 code also doesn't help if on a terminal emulator which doesn't understand
4287 HFT VTD's. */
4288 {
4289 struct hfbuf buf;
4290 struct hfkeymap keymap;
4291
4292 buf.hf_bufp = (char *)&keymap;
4293 buf.hf_buflen = sizeof (keymap);
4294 keymap.hf_nkeys = 2;
4295 keymap.hfkey[0].hf_kpos = 15;
4296 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
4297#ifdef IBMR2AIX
4298 keymap.hfkey[0].hf_keyidh = '<';
4299#else /* not IBMR2AIX */
4300 keymap.hfkey[0].hf_page = '<';
4301#endif /* not IBMR2AIX */
4302 keymap.hfkey[0].hf_char = 127;
4303 keymap.hfkey[1].hf_kpos = 15;
4304 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
4305#ifdef IBMR2AIX
4306 keymap.hfkey[1].hf_keyidh = '<';
4307#else /* not IBMR2AIX */
4308 keymap.hfkey[1].hf_page = '<';
4309#endif /* not IBMR2AIX */
4310 keymap.hfkey[1].hf_char = 127;
4311 hftctl (0, HFSKBD, &buf);
4312 }
4313 /* The HFT system on AIX doesn't optimize for scrolling, so it's really ugly
4314 at times. */
4315 line_ins_del_ok = char_ins_del_ok = 0;
4316}
4317
4318/* Reset the rubout key to backspace. */
4319
4320hft_reset ()
4321{
4322 struct hfbuf buf;
4323 struct hfkeymap keymap;
4324 int junk;
4325
4326#ifdef IBMR2AIX
4327 if (ioctl (0, HFQERROR, &junk) < 0)
4328 return;
4329#else /* not IBMR2AIX */
4330 if (ioctl (0, HFQEIO, 0) < 0)
4331 return;
4332#endif /* not IBMR2AIX */
4333
4334 buf.hf_bufp = (char *)&keymap;
4335 buf.hf_buflen = sizeof (keymap);
4336 keymap.hf_nkeys = 2;
4337 keymap.hfkey[0].hf_kpos = 15;
4338 keymap.hfkey[0].hf_kstate = HFMAPCHAR | HFSHFNONE;
4339#ifdef IBMR2AIX
4340 keymap.hfkey[0].hf_keyidh = '<';
4341#else /* not IBMR2AIX */
4342 keymap.hfkey[0].hf_page = '<';
4343#endif /* not IBMR2AIX */
4344 keymap.hfkey[0].hf_char = 8;
4345 keymap.hfkey[1].hf_kpos = 15;
4346 keymap.hfkey[1].hf_kstate = HFMAPCHAR | HFSHFSHFT;
4347#ifdef IBMR2AIX
4348 keymap.hfkey[1].hf_keyidh = '<';
4349#else /* not IBMR2AIX */
4350 keymap.hfkey[1].hf_page = '<';
4351#endif /* not IBMR2AIX */
4352 keymap.hfkey[1].hf_char = 8;
4353 hftctl (0, HFSKBD, &buf);
4354}
4355
4356#endif /* AIX */