home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / sysdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-21  |  101.3 KB  |  4,395 lines

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