home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / sysdep.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  132KB  |  5,293 lines

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