home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc072.zip / ckutio.c < prev    next >
C/C++ Source or Header  |  1989-02-08  |  79KB  |  2,399 lines

  1. char *ckxv = "Unix tty I/O, 4E(048), 13 Jan 89";
  2.  
  3. /*  C K U T I O  */
  4.  
  5. /* C-Kermit interrupt, terminal control & i/o functions for Unix systems */
  6.  
  7. /*
  8.  Author: Frank da Cruz (fdc@cunixc.cc.columbia.edu, FDCCU@CUVMA.BITNET),
  9.  Columbia University Center for Computing Activities.
  10.  First released January 1985.
  11.  Copyright (C) 1985, 1989, Trustees of Columbia University in the City of New
  12.  York.  Permission is granted to any individual or institution to use, copy, or
  13.  redistribute this software so long as it is not sold for profit, provided this
  14.  copyright notice is retained.
  15. */
  16. /* Includes for all Unixes (conditional includes come later) */
  17.  
  18. #include <sys/types.h>                  /* Types */
  19.  
  20. #include <sys/dir.h>                    /* Directory */
  21. #include <ctype.h>                      /* Character types */
  22. #ifdef NULL
  23. #undef NULL
  24. #endif /* NULL */
  25. #include <stdio.h>                      /* Unix Standard i/o */
  26. #include <signal.h>                     /* Interrupts */
  27.  
  28. #ifndef ZILOG
  29. #include <setjmp.h>                     /* Longjumps */
  30. #else
  31. #include <setret.h>
  32. #endif
  33.  
  34. #include "ckcdeb.h"                     /* Typedefs, formats for debug() */
  35.  
  36. /* Maximum length for the name of a tty device */
  37.  
  38. #ifndef DEVNAMLEN
  39. #define DEVNAMLEN 25
  40. #endif
  41.  
  42. /* 4.1 BSD support added by Charles E. Brooks, EDN-VAX */
  43. /* Fortune 32:16 Pro:For 1.8 support mostly like 4.1, added by J-P Dumas */
  44.  
  45. #ifdef BSD4
  46. #define ANYBSD
  47. #ifdef MAXNAMLEN
  48. #define BSD42
  49. #ifdef aegis
  50. char *ckxsys = " Apollo DOMAIN/IX 4.2 BSD";
  51. #else
  52. #ifdef SUNOS4
  53. char *ckxsys = " SUNOS 4.x";
  54. #else
  55. #ifdef ultrix
  56. char *ckxsys = " VAX/Ultrix";
  57. #else
  58. char *ckxsys = " 4.2 BSD";
  59. #endif /* ultrix */
  60. #endif /* sunos4 */
  61. #endif /* aegis */
  62. #else
  63. #ifdef FT18
  64. #define BSD41
  65. char *ckxsys = " Fortune For:Pro 1.8";
  66. #else
  67. #define BSD41
  68. #ifndef C70
  69. char *ckxsys = " 4.1 BSD";
  70. #endif /* not c70 */
  71. #endif /* ft18 */
  72. #endif /* maxnamlen */
  73. #endif /* bsd4 */
  74.  
  75. /* 2.9bsd support contributed by Bradley Smith, UCLA */
  76. #ifdef BSD29
  77. #define ANYBSD
  78. char *ckxsys = " 2.9 BSD";
  79. #endif /* bsd29 */
  80.  
  81. /*
  82.  Version 7 UNIX support contributed by Gregg Wonderly,
  83.  Oklahoma State University:  gregg@okstate.csnet
  84. */
  85. #ifdef  V7
  86. char *ckxsys = " Version 7 UNIX (tm)";
  87. #endif /* v7 */
  88.  
  89. /* BBN C70 support from Frank Wancho, WANCHO@SIMTEL20 */
  90. #ifdef C70
  91. char *ckxsys = " BBN C/70";
  92. #endif /* c70 */
  93.  
  94. /* IBM 370 IX/370 support from Wayne Van Pelt, GE/CRD, Schenectedy, NY */
  95. #ifdef IX370
  96. char *ckxsys = " IBM IX/370";
  97. #endif /* ix370 */
  98.  
  99. /* Amdahl UTS 2.4 (v7 derivative) for IBM 370 series compatible mainframes */
  100. /* Contributed by Garard Gaye, Jean-Pierre Dumas, DUMAS@SUMEX-AIM. */
  101. #ifdef UTS24
  102. char *ckxsys = " Amdahl UTS 2.4";
  103. #endif /* uts24 */
  104.  
  105. /* Pro/Venix Version 1.x support from Columbia U. */
  106. #ifdef PROVX1
  107. char *ckxsys = " Pro-3xx Venix v1";
  108. #endif /* provx1 */
  109.  
  110. /* Tower support contributed by John Bray, Auburn, Alabama */
  111. #ifdef TOWER1
  112. char *ckxsys = " NCR Tower 1632, OS 1.02";
  113. #endif /* tower1 */
  114.  
  115. /* Sys III/V, Xenix, PC/IX support by Herm Fischer, Encino, CA */
  116. #ifdef UXIII
  117. #ifdef XENIX
  118. #ifdef M_I386
  119. char *ckxsys = " Xenix/386";
  120. #else
  121. #ifdef M_I286
  122. char *ckxsys = " Xenix/286";
  123. #else
  124. char *ckxsys = " Xenix/86";
  125. #endif
  126. #endif
  127. #else
  128. #ifdef PCIX
  129. char *ckxsys = " PC/IX";
  130. #else
  131. #ifdef ISIII
  132. char *ckxsys = " Interactive Systems Corp System III";
  133. #else
  134. #ifdef hpux
  135. /* HP 9000 Series changes contributed by Bill Coalson */
  136. char *ckxsys = " HP 9000 Series HP-UX";
  137. #else
  138. #ifdef aegis
  139. /* Apollo Aegis support from SAS Institute, Cary, NC */
  140. char *ckxsys = " Apollo DOMAIN/IX System V";
  141. #else
  142. #ifdef ZILOG
  143. char *ckxsys = " Zilog S8000 Zeus 3.21+";
  144. #else
  145. #ifdef VXVE
  146. /* Control Data Corp VX/VE 5.2.1 System V support by */
  147. /* S.O. Lidie, Lehigh University, LUSOL@LEHICDC1.BITNET */
  148. char *ckxsys = " CDC VX/VE 5.2.1 System V";
  149. #else
  150. #ifdef ATT7300
  151. /* Changes by Joe R. Doupnik, jrd@usu.Bitnet, Utah State Univ */
  152. char *ckxsys = " AT&T 7300/Unix PC System III/System V\n";
  153. #else
  154. char *ckxsys = " AT&T System III/System V";
  155. #endif /* att7300 */
  156. #endif /* vxve  */
  157. #endif /* zilog */
  158. #endif /* aegis */
  159. #endif /* hpux  */
  160. #endif /* isiii */
  161. #endif /* pcix  */
  162. #endif /* xenix */
  163. #endif /* uxiii */
  164.  
  165. /* Features... */
  166.  
  167. #ifdef NEWUUCP
  168. #define LCKDIR
  169. #endif
  170.  
  171. /* Do own buffering, using unbuffered read() calls... */
  172. #ifdef UXIII
  173. #define MYREAD
  174. #endif /* uxiii */
  175.  
  176. #ifdef ATT7300
  177. /* no myread(), bits for attmodem: internal modem in use, restart getty */
  178. #undef MYREAD
  179. #define ISMODEM 1
  180. #define DOGETY 512
  181. #endif  /* att7300 */
  182.  
  183. #ifdef BSD42
  184. #undef MYREAD
  185. #include <errno.h>
  186. #endif /* bsd42 */
  187.  
  188. /*
  189.  Variables available to outside world:
  190.  
  191.    dftty  -- Pointer to default tty name string, like "/dev/tty".
  192.    dfloc  -- 0 if dftty is console, 1 if external line.
  193.    dfprty -- Default parity
  194.    dfflow -- Default flow control
  195.    ckxech -- Flag for who echoes console typein:
  196.      1 - The program (system echo is turned off)
  197.      0 - The system (or front end, or terminal).
  198.    functions that want to do their own echoing should check this flag
  199.    before doing so.
  200.  
  201.    flfnam -- Name of lock file, including its path, e.g.,
  202.                 "/usr/spool/uucp/LCK..cul0" or "/etc/locks/tty77"
  203.    hasLock -- Flag set if this kermit established a uucp lock.
  204.    inbufc -- number of tty line rawmode unread characters
  205.                 (system III/V unixes)
  206.    backgrd -- Flag indicating program executing in background ( & on
  207.                 end of shell command). Used to ignore INT and QUIT signals.
  208.  
  209.  Functions for assigned communication line (either external or console tty):
  210.  
  211.    sysinit()               -- System dependent program initialization
  212.    syscleanup()            -- System dependent program shutdown
  213.    ttopen(ttname,local,mdmtyp) -- Open the named tty for exclusive access.
  214.    ttclos()                -- Close & reset the tty, releasing any access lock.
  215.    ttpkt(speed,flow)       -- Put the tty in packet mode and set the speed.
  216.    ttvt(speed,flow)        -- Put the tty in virtual terminal mode.
  217.                                 or in DIALING or CONNECTED modem control state.
  218.    ttinl(dest,max,timo)    -- Timed read line from the tty.
  219.    ttinc(timo)             -- Timed read character from tty.
  220.    myread()                -- System 3 raw mode bulk buffer read, gives
  221.                            --   subsequent chars one at a time and simulates
  222.                            --   FIONREAD!
  223.    myunrd(c)               -- Places c back in buffer to be read (one only)
  224.    ttchk()                 -- See how many characters in tty input buffer.
  225.    ttxin(n,buf)            -- Read n characters from tty (untimed).
  226.    ttol(string,length)     -- Write a string to the tty.
  227.    ttoc(c)                 -- Write a character to the tty.
  228.    ttflui()                -- Flush tty input buffer.
  229.  
  230.    ttlock(ttname)          -- Lock against uucp collisions (Sys III)
  231.    ttunlck()               -- Unlock "       "     "
  232.    look4lk(ttname)         -- Check if a lock file exists
  233.  
  234.                               For ATT7300/Unix PC, Sys III / Sys V:
  235.    attdial(ttname,speed,telnbr) -- dials ATT7300/Unix PC internal modem
  236.    atthang(ttname)         -- Hangs up internal modem for ATT7300's
  237.    offgetty(ttname)        -- Turns off getty(1m) for comms line
  238.    ongetty(ttname)         -- Restores getty() to comms line
  239. */
  240.  
  241. /*
  242. Functions for console terminal:
  243.  
  244.    congm()   -- Get console terminal modes.
  245.    concb(esc) -- Put the console in single-character wakeup mode with no echo.
  246.    conbin(esc) -- Put the console in binary (raw) mode.
  247.    conres()  -- Restore the console to mode obtained by congm().
  248.    conoc(c)  -- Unbuffered output, one character to console.
  249.    conol(s)  -- Unbuffered output, null-terminated string to the console.
  250.    conola(s) -- Unbuffered output, array of strings to the console.
  251.    conxo(n,s) -- Unbuffered output, n characters to the console.
  252.    conchk()  -- Check if characters available at console (bsd 4.2).
  253.                 Check if escape char (^\) typed at console (System III/V).
  254.    coninc(timo)  -- Timed get a character from the console.
  255.    conint()  -- Enable terminal interrupts on the console if not background.
  256.    connoi()  -- Disable terminal interrupts on the console if not background.
  257.  
  258. Time functions
  259.  
  260.    msleep(m) -- Millisecond sleep
  261.    ztime(&s) -- Return pointer to date/time string
  262.    rtimer() --  Reset timer
  263.    gtimer()  -- Get elapsed time since last call to rtimer()
  264. */
  265.  
  266. /* Conditional Includes */
  267.  
  268. #ifdef FT18
  269. #include <sys/file.h>                   /* File information */
  270. #endif /* ft18 */
  271.  
  272. /* Whether to #include <sys/file.h>... */
  273. #ifndef PROVX1
  274. #ifndef aegis
  275. #ifndef XENIX
  276. #include <sys/file.h>                   /* File information */
  277. #endif /* xenix */
  278. #endif /* aegis */
  279. #endif /* provx1 */
  280.  
  281. #ifdef aegis
  282. #ifdef BSD4
  283. #include <sys/file.h>
  284. #include <fcntl.h>
  285. #endif /* bsd4 */
  286. #endif /* aegis */
  287.  
  288. /* System III, System V */
  289.  
  290. #ifdef UXIII
  291. #include <termio.h>
  292. #include <sys/ioctl.h>
  293. #include <fcntl.h>                      /* directory reading for locking */
  294. #include <errno.h>                      /* error numbers for system returns */
  295. #ifdef  ATT7300
  296. #include <sys/phone.h>                  /* Unix PC, internal modem dialer */
  297. #endif  /* att7300 */
  298. #endif /* uxiii */
  299.  
  300. #ifdef HPUX
  301. #include <sys/modem.h>
  302. #endif
  303.  
  304. /* Not Sys III/V */
  305.  
  306. #ifndef UXIII
  307. #include <sgtty.h>                      /* Set/Get tty modes */
  308. #ifndef PROVX1
  309. #ifndef V7
  310. #ifndef BSD41
  311. #include <sys/time.h>                   /* Clock info (for break generation) */
  312. #endif /* not bsd41 */
  313. #endif /* not v7 */
  314. #endif /* not provx1 */
  315. #endif /* not uxiii */
  316.  
  317. #ifdef BSD41
  318. #include <sys/timeb.h>                  /* BSD 4.1 ... ceb */
  319. #endif /* bsd41 */
  320.  
  321. #ifdef BSD29
  322. #include <sys/timeb.h>                  /* BSD 2.9 (Vic Abell, Purdue) */
  323. #endif /* bsd29 */
  324.  
  325. #ifdef TOWER1
  326. #include <sys/timeb.h>                  /* Clock info for NCR Tower */
  327. #endif /* tower1 */
  328.  
  329. #ifdef aegis
  330. #include "/sys/ins/base.ins.c"
  331. #include "/sys/ins/error.ins.c"
  332. #include "/sys/ins/ios.ins.c"
  333. #include "/sys/ins/sio.ins.c"
  334. #include "/sys/ins/pad.ins.c"
  335. #include "/sys/ins/time.ins.c"
  336. #include "/sys/ins/pfm.ins.c"
  337. #include "/sys/ins/pgm.ins.c"
  338. #include "/sys/ins/ec2.ins.c"
  339. #include "/sys/ins/type_uids.ins.c"
  340. #include <default_acl.h>
  341. #undef TIOCEXCL
  342. #undef FIONREAD
  343. #endif
  344.  
  345. /* The following two conditional #defines are catch-alls for those systems */
  346. /* that didn't have or couldn't find <file.h>... */
  347.  
  348. #ifndef FREAD
  349. #define FREAD 0x01
  350. #endif
  351.  
  352. #ifndef FWRITE
  353. #define FWRITE 0x10
  354. #endif
  355.  
  356. /* Declarations */
  357.  
  358. long time();                            /* All Unixes should have this... */
  359. extern int errno;                       /* System call error code. */
  360.  
  361. /* Special stuff for V7 input buffer peeking */
  362.  
  363. #ifdef  V7
  364. int kmem[2] = { -1, -1};
  365. char *initrawq(), *qaddr[2]={0,0};
  366. #define CON 0
  367. #define TTY 1
  368. #endif /* v7 */
  369.  
  370. /* dftty is the device name of the default device for file transfer */
  371. /* dfloc is 0 if dftty is the user's console terminal, 1 if an external line */
  372.  
  373. #ifdef PROVX1
  374.     char *dftty = "/dev/com1.dout"; /* Only example so far of a system */
  375.     int dfloc = 1;                  /* that goes in local mode by default */
  376. #else
  377.     char *dftty = CTTNAM;               /* Remote by default, use normal */
  378.     int dfloc = 0;                      /* controlling terminal name. */
  379. #endif /* provx1 */
  380.  
  381.     int dfprty = 0;                     /* Default parity (0 = none) */
  382.     int ttprty = 0;                     /* Parity in use. */
  383.     int ttmdm = 0;                      /* Modem in use. */
  384.     int dfflow = 1;                     /* Xon/Xoff flow control */
  385.     int backgrd = 0;                    /* Assume in foreground (no '&' ) */
  386.  
  387. int ckxech = 0; /* 0 if system normally echoes console characters, else 1 */
  388.  
  389. /* Declarations of variables global within this module */
  390.  
  391. static long tcount;                     /* Elapsed time counter */
  392.  
  393. static char *brnuls = "\0\0\0\0\0\0\0"; /* A string of nulls */
  394.  
  395. static jmp_buf sjbuf, jjbuf;            /* Longjump buffer */
  396. static int lkf = 0,                     /* Line lock flag */
  397.     conif = 0,                          /* Console interrupts on/off flag */
  398.     cgmf = 0,                           /* Flag that console modes saved */
  399.     xlocal = 0,                         /* Flag for tty local or remote */
  400.     ttyfd = -1;                         /* TTY file descriptor */
  401. static char escchr;                     /* Escape or attn character */
  402.  
  403. #ifdef BSD42
  404.     static struct timeval tv;           /* For getting time, from sys/time.h */
  405.     static struct timezone tz;
  406. #endif /* bsd42 */
  407.  
  408. #ifdef BSD29
  409.     static long clock;                  /* For getting time from sys/time.h */
  410.     static struct timeb ftp;            /* And from sys/timeb.h */
  411. #endif /* bsd29 */
  412.  
  413. #ifdef BSD41
  414.     static long clock;                  /* For getting time from sys/time.h */
  415.     static struct timeb ftp;            /* And from sys/timeb.h */
  416. #endif /* bsd41 */
  417.  
  418. #ifdef TOWER1
  419. static long clock;                      /* For getting time from sys/time.h */
  420. static struct timeb ftp;                /* And from sys/timeb.h */
  421. #endif /* tower1 */
  422.  
  423. #ifdef V7
  424. static long clock;
  425. #endif /* v7 */
  426.  
  427. /* sgtty/termio information... */
  428.  
  429. #ifdef UXIII
  430.   static struct termio ttold = {0};     /* Init'd for word alignment, */
  431.   static struct termio ttraw = {0};     /* which is important for some */
  432.   static struct termio tttvt = {0};     /* systems, like Zilog... */
  433.   static struct termio ccold = {0};
  434.   static struct termio ccraw = {0};
  435.   static struct termio cccbrk = {0};
  436. #else
  437.   static struct sgttyb                  /* sgtty info... */
  438.     ttold, ttraw, tttvt, ttbuf,         /* for communication line */
  439.     ccold, ccraw, cccbrk;               /* and for console */
  440. #endif /* uxiii */
  441.  
  442. #ifdef ATT7300
  443. static int attmodem = 0;                /* ATT7300 internal-modem status */
  444. #endif  /* att7300 */
  445.  
  446. static char flfnam[80];                 /* uucp lock file path name */
  447. static int hasLock = 0;                 /* =1 if this kermit locked uucp */
  448. static int inbufc = 0;                  /* stuff for efficient SIII raw line */
  449. static int ungotn = -1;                 /* pushback to unread character */
  450. static int conesc = 0;                  /* set to 1 if esc char (^\) typed */
  451.  
  452. static int ttlock();                    /* definition of ttlock subprocedure */
  453. static int ttunlck();                   /* and unlock subprocedure */
  454. static char ttnmsv[DEVNAMLEN];          /* copy of open path for tthang */
  455.  
  456. #ifdef aegis
  457. static status_$t st;                    /* error status return value */
  458. static short concrp = 0;                /* true if console is CRP pad */
  459. #define CONBUFSIZ 10
  460. static char conbuf[CONBUFSIZ];          /* console readahead buffer */
  461. static int  conbufn = 0;                /* # chars in readahead buffer */
  462. static char *conbufp;                   /* next char in readahead buffer */
  463. static uid_$t ttyuid;                   /* tty type uid */
  464. static uid_$t conuid;                   /* stdout type uid */
  465.  
  466. /* APOLLO Aegis main()
  467.  * establish acl usage and cleanup handling
  468.  *    this makes sure that CRP pads
  469.  *    get restored to a usable mode
  470.  */
  471. main(argc,argv) int argc; char **argv; {
  472.         status_$t status;
  473.         pfm_$cleanup_rec dirty;
  474.  
  475.         int pid = getpid();
  476.  
  477.         /* acl usage according to invoking environment */
  478.         default_acl(USE_DEFENV);
  479.  
  480.         /* establish a cleanup continuation */
  481.         status = pfm_$cleanup(dirty);
  482.         if (status.all != pfm_$cleanup_set)
  483.         {
  484.                 /* only handle faults for the original process */
  485.                 if (pid == getpid() && status.all > pgm_$max_severity)
  486.                 {       /* blew up in main process */
  487.                         status_$t quo;
  488.                         pfm_$cleanup_rec clean;
  489.  
  490.                         /* restore the console in any case */
  491.                         conres();
  492.  
  493.                         /* attempt a clean exit */
  494.                         debug(F101, "cleanup fault status", "", status.all);
  495.  
  496.                         /* doexit(), then send status to continuation */
  497.                         quo = pfm_$cleanup(clean);
  498.                         if (quo.all == pfm_$cleanup_set)
  499.                                 doexit(pgm_$program_faulted);
  500.                         else if (quo.all > pgm_$max_severity)
  501.                                 pfm_$signal(quo); /* blew up in doexit() */
  502.                 }
  503.                 /* send to the original continuation */
  504.                 pfm_$signal(status);
  505.                 /*NOTREACHED*/
  506.         }
  507.         return(ckcmai(argc, argv));
  508. }
  509. #endif /* aegis */
  510.  
  511. /*  S Y S I N I T  --  System-dependent program initialization.  */
  512.  
  513. sysinit() {
  514.  
  515. /* for now, nothing... */
  516.     return(0);
  517. }
  518.  
  519. /*  S Y S C L E A N U P  --  System-dependent program cleanup.  */
  520.  
  521. syscleanup() {
  522.  
  523. /* for now, nothing... */
  524.     return(0);
  525. }
  526.  
  527. /*  T T O P E N  --  Open a tty for exclusive access.  */
  528.  
  529. /*  Returns 0 on success, -1 on failure.  */
  530. /*
  531.   If called with lcl < 0, sets value of lcl as follows:
  532.   0: the terminal named by ttname is the job's controlling terminal.
  533.   1: the terminal named by ttname is not the job's controlling terminal.
  534.   But watch out: if a line is already open, or if requested line can't
  535.   be opened, then lcl remains (and is returned as) -1.
  536. */
  537. ttopen(ttname,lcl,modem) char *ttname; int *lcl, modem; {
  538.  
  539. #ifdef UXIII
  540. #ifndef CIE
  541.     char *ctermid();                    /* Wish they all had this! */
  542. #endif /* not cie */
  543. #endif /* uxiii */
  544.  
  545. #ifdef CIE                              /* CIE Regulus doesn't... */
  546. #define ctermid(x) strcpy(x,"")
  547. #endif
  548.  
  549.     char *x; extern char* ttyname();
  550.     char cname[DEVNAMLEN+4];
  551.  
  552.     if (ttyfd > -1)                     /* if comms line already opened */
  553.         if (strncmp(ttname,ttnmsv,DEVNAMLEN)) /* are new & old names equal? */
  554.           ttclos();                     /* no, close old ttname, open new */
  555.         else                            /* else same, ignore this call */
  556.           return(0);
  557.     ttmdm = modem;                      /* Make this available to other fns */
  558.     xlocal = *lcl;                      /* Make this available to other fns */
  559. #ifdef NEWUUCP
  560.     acucntrl("disable",ttname);         /* Open getty on line (4.3BSD) */
  561. #endif /* newuucp */
  562.  
  563. #ifdef UXIII
  564. #ifdef ATT7300
  565. /* Open comms line without waiting for carrier so initial call does not hang*/
  566. /*  because state of "modem" is likely unknown at the initial call. jrd */
  567.     ttyfd = open(ttname,O_RDWR | O_NDELAY);
  568. #else
  569.                             /* if modem connection, don't wait for carrier */
  570.     ttyfd = open(ttname,O_RDWR | (modem ? O_NDELAY : 0) );
  571. #endif  /# att7300 */
  572. #else
  573. #ifdef ultrix
  574.     ttyfd = open(ttname, O_RDWR|O_NDELAY);
  575. #else
  576.     ttyfd = open(ttname,2);             /* Try to open for read/write */
  577. #endif /* ultrix */
  578. #endif /* uxiii */
  579.  
  580.     if (ttyfd < 0) {                    /* If couldn't open, fail. */
  581.         perror(ttname);
  582.         return(-1);
  583.     }
  584. #ifdef aegis
  585.     /* Apollo C runtime claims that console pads are tty devices, which
  586.      * is reasonable, but they aren't any good for packet transfer. */
  587.     ios_$inq_type_uid((short)ttyfd, ttyuid, st);
  588.     if (st.all != status_$ok) {
  589.         fprintf(stderr, "problem getting tty object type: ");
  590.         error_$print(st);
  591.     } else if (ttyuid != sio_$uid) { /* reject non-SIO lines */
  592.         close(ttyfd); ttyfd = -1;
  593.         errno = ENOTTY; perror(ttname);
  594.         return(-1);
  595.     }
  596. #endif /* aegis */
  597.     strncpy(ttnmsv,ttname,DEVNAMLEN);   /* Open, keep copy of name locally. */
  598.  
  599. /* Caller wants us to figure out if line is controlling tty */
  600.  
  601.     debug(F111,"ttopen ok",ttname,*lcl);
  602.     if (*lcl != 0) {
  603.         if (strcmp(ttname,CTTNAM) == 0) {   /* "/dev/tty" always remote */
  604.             debug(F110," Same as CTTNAM",ttname,0);
  605.             xlocal = 0;
  606.         } else if (isatty(0)) {         /* Else, if stdin not redirected */
  607.             x = ttyname(0);             /* then compare its device name */
  608.             strncpy(cname,x,DEVNAMLEN); /* (copy from internal static buf) */
  609.             debug(F110," ttyname(0)",x,0);
  610.             x = ttyname(ttyfd);         /* ...with real name of ttname. */
  611.             xlocal = (strncmp(x,cname,DEVNAMLEN) == 0) ? 0 : 1;
  612.             debug(F111," ttyname",x,xlocal);
  613.         } else {                        /* Else, if stdin redirected... */
  614. #ifdef UXIII
  615. /* Sys III/V provides nice ctermid() function to get name of controlling tty */
  616.             ctermid(cname);             /* Get name of controlling terminal */
  617.             debug(F110," ctermid",cname,0);
  618.             x = ttyname(ttyfd);         /* Compare with name of comm line. */
  619.             xlocal = (strncmp(x,cname,DEVNAMLEN) == 0) ? 0 : 1;
  620.             debug(F111," ttyname",x,xlocal);
  621. #else
  622. /* Just assume local, so "set speed" and similar commands will work */
  623. /* If not really local, how could it work anyway?... */
  624.             xlocal = 1;
  625.             debug(F101," redirected stdin","",xlocal);
  626. #endif /* uxiii */
  627.         }
  628.     }
  629.  
  630. /* Now check if line is locked -- if so fail, else lock for ourselves */
  631.  
  632.     lkf = 0;                            /* Check lock */
  633.     if (xlocal > 0) {
  634.         if (ttlock(ttname) < 0) {
  635.             fprintf(stderr,"Exclusive access to %s denied\n",ttname);
  636.             close(ttyfd); ttyfd = -1;
  637.             debug(F110," Access denied by lock",ttname,0);
  638.             return(-1);                 /* Not if already locked */
  639.         } else lkf = 1;
  640.     }
  641.  
  642.                                         /* Check for/suppress getty on line */
  643. #ifdef ATT7300
  644.     if ((attmodem & DOGETY) == 0)       /* if have not suppressed getty() */
  645.         attmodem |= offgetty(ttname);   /*  do so now and remember response */
  646. #endif  /* att7300 */
  647.  
  648. /* Got the line, now set the desired value for local. */
  649.  
  650.     if (*lcl != 0) *lcl = xlocal;
  651.  
  652. /* Some special stuff for v7... */
  653.  
  654. #ifdef  V7
  655.         if (kmem[TTY] < 0) {    /*  If open, then skip this.  */
  656.                 qaddr[TTY] = initrawq(ttyfd);   /* Init the queue. */
  657.                 if ((kmem[TTY] = open("/dev/kmem", 0)) < 0) {
  658.                         fprintf(stderr, "Can't read /dev/kmem in ttopen.\n");
  659.                         perror("/dev/kmem");
  660.                         exit(1);
  661.                 }
  662.         }
  663. #endif /* v7 */
  664.                                 /* no failure returns after this point */
  665.  
  666. /* Request exclusive access on systems that allow it. */
  667.  
  668. #ifndef XENIX
  669. /* Xenix exclusive access prevents open(close(...)) from working... */
  670. #ifdef TIOCEXCL
  671.         if (ioctl(ttyfd,TIOCEXCL, NULL) < 0)
  672.             fprintf(stderr,"Warning, problem getting exclusive access\n");
  673. #endif /* tiocexcl */
  674. #endif /* xenix */
  675.  
  676. #ifdef ultrix
  677.     {
  678.     int temp = 0;
  679.  
  680.     if (ioctl(ttyfd, TIOCSINUSE, NULL) < 0) {
  681.         fprintf(stderr, "Can't set in-use flag on modem.\n");
  682.         perror("TIOCSINUSE");
  683.     }
  684.     if (modem) {
  685.         ioctl(ttyfd, TIOCMODEM, &temp);
  686.     } else {
  687.         ioctl(ttyfd, TIOCNMODEM, &temp);
  688.     }
  689.     ioctl(ttyfd, TIOCHPCL, 0);
  690.     }
  691. #endif /* ultrix */
  692.  
  693. /* Get tty device settings */
  694.  
  695. #ifndef UXIII
  696.     gtty(ttyfd,&ttold);                 /* Get sgtty info */
  697. #ifdef aegis
  698.     sio_$control((short)ttyfd, sio_$raw_nl, false, st);
  699.     if (xlocal) {       /* ignore breaks from local line */
  700.         sio_$control((short)ttyfd, sio_$int_enable, false, st);
  701.         sio_$control((short)ttyfd, sio_$quit_enable, false, st);
  702.     }
  703. #endif /* aegis */
  704.     gtty(ttyfd,&ttraw);                 /* And a copy of it for packets*/
  705.     gtty(ttyfd,&tttvt);                 /* And one for virtual tty service */
  706. #else
  707.     ioctl(ttyfd,TCGETA,&ttold);         /* Same deal for Sys III, Sys V */
  708. #ifdef aegis
  709.     sio_$control((short)ttyfd, sio_$raw_nl, false, st);
  710.     if (xlocal) {       /* ignore breaks from local line */
  711.         sio_$control((short)ttyfd, sio_$int_enable, false, st);
  712.         sio_$control((short)ttyfd, sio_$quit_enable, false, st);
  713.     }
  714. #endif /* aegis */
  715.     ioctl(ttyfd,TCGETA,&ttraw);
  716.     ioctl(ttyfd,TCGETA,&tttvt);
  717. #endif /* not uxiii */
  718.  
  719. #ifdef VXVE
  720.     ttraw.c_line = 0;                   /* STTY line 0 for VX/VE */
  721.     ioctl(ttyfd,TCSETA,&ttraw);
  722.     tttvt.c_line = 0;                   /* STTY line 0 for VX/VE */
  723.     ioctl(ttyfd,TCSETA,&tttvt);
  724. #endif /* vxve */
  725.  
  726.     debug(F101,"ttopen, ttyfd","",ttyfd);
  727.     debug(F101," lcl","",*lcl);
  728.     debug(F111," lock file",flfnam,lkf);
  729.     return(0);
  730. }
  731.  
  732. /*  T T C L O S  --  Close the TTY, releasing any lock.  */
  733.  
  734. ttclos() {
  735.     if (ttyfd < 0) return(0);           /* Wasn't open. */
  736. #ifdef ultrix
  737.     ioctl(ttyfd, TIOCNCAR, NULL);
  738. #endif
  739.     if (xlocal) {
  740.         if (ttunlck())                  /* Release uucp-style lock */
  741.             fprintf(stderr,"Warning, problem releasing lock\n");
  742.  
  743. /* Ultrix doesn't need tthang() because of TIOCHPCL */
  744. #ifndef ultrix
  745.         if (tthang())                   /* Hang up phone line */
  746.             fprintf(stderr,"Warning, problem hanging up the phone\n");
  747. #endif
  748.     }
  749.     ttres();                            /* Reset modes. */
  750. /* Relinquish exclusive access if we might have had it... */
  751. #ifndef XENIX
  752. #ifdef TIOCEXCL
  753. #ifdef TIOCNXCL
  754.     if (ioctl(ttyfd, TIOCNXCL, NULL) < 0)
  755.         fprintf(stderr,"Warning, problem relinquishing exclusive access\n");
  756. #endif /* tiocnxcl */
  757. #endif /* tiocexcl */
  758. #endif /* not xenix */
  759.     if (ttyfd > -1) close(ttyfd);       /* Close it if tthang didn't */
  760.     ttyfd = -1;                         /* Mark it as closed. */
  761. #ifdef NEWUUCP
  762.     acucntrl("enable",flfnam);          /* Close getty on line. */
  763. #endif /* newuucp */
  764. #ifdef ATT7300
  765.     if (attmodem & DOGETY)              /* was getty(1m) running before us? */
  766.         ongetty(ttnmsv);                        /* yes, restart on tty line */
  767.     attmodem &= ~DOGETY;                /* no phone in use, getty restored */
  768. #endif  /* attt7300 */
  769.     return(0);
  770. }
  771.  
  772. /*  T T H A N G -- Hangup phone line */
  773.  
  774. tthang() {
  775. #ifdef UXIII
  776. #ifdef HPUX
  777.     unsigned long dtr_down = 00000000000,
  778.                   modem_rtn;
  779. #else
  780.     unsigned short ttc_save;
  781. #endif /* hpux */
  782. #endif /* uxiii */
  783. #ifdef ANYBSD
  784.     int ttc_save;
  785. #endif
  786.  
  787.     if (ttyfd < 0) return(0);           /* Not open. */
  788. #ifdef aegis
  789.     sio_$control((short)ttyfd, sio_$dtr, false, st);    /* DTR down */
  790.     msleep(500);                                        /* pause */
  791.     sio_$control((short)ttyfd, sio_$dtr, true,  st);    /* DTR up */
  792. #else
  793. #ifdef ANYBSD
  794.     ioctl(ttyfd,TIOCCDTR,0);            /* Clear DTR */
  795.     msleep(500);                        /* For about 1/2 sec */
  796.     ioctl(ttyfd,TIOCSDTR,0);            /* Restore DTR */
  797.     ttc_save = fcntl(ttyfd,F_GETFL,0);    /* Get flags */
  798.     close(ttyfd);            /* Close/reopen file descriptor */
  799.     if ((ttyfd = open(ttnmsv, ttc_save)) < 0) return(-1);
  800. #endif /* anybsd */
  801. #ifdef UXIII
  802. #ifdef HPUX   /* Hewlett Packard way of modem control  */
  803.     if (ioctl(ttyfd,MCSETAF,&dtr_down) < 0) return(-1); /* lower DTR */
  804.     msleep(500);
  805.     if (ioctl(ttyfd,MCGETA,&modem_rtn) < 0) return(-1); /* get line status */
  806.     if ((modem_rtn & MDCD) != 0) return(-1);        /* check if DCD is low */
  807.     modem_rtn = MRTS | MDTR;                        /* bits for RTS & DTR  */
  808.     if (ioctl(ttyfd,MCSETAF,&modem_rtn) < 0) return(-1);    /*  set lines  */
  809. #else
  810.     ttc_save = ttraw.c_cflag;
  811.     ttraw.c_cflag &= ~CBAUD;            /* swa: set baud rate to 0 to hangup */
  812.     if (ioctl(ttyfd,TCSETAF,&ttraw) < 0) return(-1); /* do it */
  813.     msleep(100);                        /* let things settle */
  814.     ttraw.c_cflag = ttc_save;
  815.  
  816. #ifdef ATT7300
  817.     atthang(ttnmsv);            /* hangup internal modem, if in use */
  818. #endif  /* ATT7300 */
  819.  
  820. /* NOTE - The following #ifndef...#endif can be removed for SCO Xenix 2.1.3 */
  821. /* or later, but must keep for earlier versions, which can't do close/open. */
  822.  
  823. #ifndef XENIX           /* xenix cannot do close/open when carrier drops */
  824.                                 /* following corrects a PC/IX defficiency */
  825.     ttc_save = fcntl(ttyfd,F_GETFL,0);
  826.     close(ttyfd);               /* close/reopen file descriptor */
  827.     ttyfd = -1;                 /* in case reopen fails */
  828.     if ((ttyfd = open(ttnmsv, ttc_save)) < 0) return(-1);
  829. #endif /* not xenix */
  830.     if (ioctl(ttyfd,TCSETAF,&ttraw) < 0) return(-1); /* un-do it */
  831. #endif /* uxiii */
  832. #endif /* hpux  */
  833. #endif /* aegis */
  834.     return (0);
  835. }
  836.  
  837.  
  838. /*  T T R E S  --  Restore terminal to "normal" mode.  */
  839.  
  840. ttres() {                               /* Restore the tty to normal. */
  841.     int x;
  842.  
  843.     if (ttyfd < 0) return(-1);          /* Not open. */
  844. #ifndef UXIII                           /* except for sIII, */
  845.     sleep(1);                           /* Wait for pending i/o to finish. */
  846. #endif  /* uxiii */                     /*   (sIII does wait in ioctls) */
  847.  
  848. #ifdef UXIII
  849.     if (ioctl(ttyfd,TCSETAW,&ttold) < 0) return(-1); /* restore termio stuff */
  850.     if (fcntl(ttyfd,F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NDELAY) < 0 )
  851.       return(-1);
  852. #else /* not uxiii */
  853. #ifdef FIONBIO
  854.     x = 0;
  855.     x = ioctl(ttyfd,FIONBIO,&x);
  856.     if (x < 0) {
  857.         perror("ttres ioctl");
  858.         debug(F101,"ttres ioctl","",x);
  859.     }
  860. #else /* not fionbio */
  861. #ifdef FNDELAY
  862.     x = (fcntl(ttyfd,F_SETFL,fcntl(ttyfd,F_GETFL,0) & ~FNDELAY) == -1);
  863.     debug(F101,"ttres fcntl","",x);
  864.     if (x < 0) perror("fcntl");
  865. #endif /* fndelay */
  866. #endif /* fionbio */
  867.     x = stty(ttyfd,&ttold);             /* Restore sgtty stuff */
  868.     debug(F101,"ttres stty","",x);
  869.     if (x < 0) perror("stty");
  870. #endif /* uxiii */
  871.     return(x);
  872. }
  873.  
  874. /* Exclusive uucp file locking control */
  875. /*
  876.  by H. Fischer, creative non-Bell coding !
  877.  copyright rights for lock modules assigned to Columbia University
  878. */
  879. static char *
  880. xxlast(s,c) char *s; char c; {          /* Equivalent to strrchr() */
  881.     int i;
  882.     for (i = strlen(s); i > 0; i--)
  883.         if ( s[i-1] == c ) return( s + (i - 1) );
  884.     return(NULL);
  885. }
  886. static
  887. look4lk(ttname) char *ttname; {
  888.     extern char *strcat(), *strcpy();
  889.     char *device, *devname;
  890.     char lockfil[50];                   /* Max length for lock file name */
  891.  
  892. #ifdef ISIII
  893.     char *lockdir = "/etc/locks";
  894. #else
  895. #ifdef ATT3BX
  896.     char *lockdir = "/usr/spool/locks";
  897. #else
  898. #ifdef LCKDIR
  899.     char *lockdir = "/usr/spool/uucp/LCK";
  900. #else
  901.     char *lockdir = "/usr/spool/uucp";
  902. #endif /* newuucp */
  903. #endif /* att3bx */
  904. #endif /* isiii */
  905.  
  906.     device = ( (devname=xxlast(ttname,'/')) != NULL ? devname+1 : ttname);
  907.  
  908. #ifdef ISIII
  909.     (void) strcpy( lockfil, device );
  910. #else
  911.     strcat( strcpy( lockfil, "LCK.." ), device );
  912. #endif /* isiii */
  913.  
  914.     if (access( lockdir, 04 ) < 0) {    /* read access denied on lock dir */
  915.         fprintf(stderr,"Warning, read access to lock directory denied\n");
  916.         return( 1 );                    /* cannot check or set lock file */
  917.     }
  918.  
  919.     strcat(strcat(strcpy(flfnam,lockdir),"/"), lockfil);
  920.     debug(F110,"look4lk",flfnam,0);
  921.  
  922.     if ( ! access( flfnam, 00 ) ) {     /* print out lock file entry */
  923.         char lckcmd[40] ;
  924.         strcat( strcpy(lckcmd, "ls -l ") , flfnam);
  925.         system(lckcmd);
  926.         if (access(flfnam,02) == 0)
  927.             printf("(You may type \"! rm %s\" to remove this file)\n",flfnam);
  928.         return( -1 );
  929.     }
  930.     if ( access( lockdir, 02 ) < 0 ) {  /* lock file cannot be written */
  931.         fprintf(stderr,"Warning, write access to lock directory denied\n");
  932.         return( 1 );
  933.     }
  934.     return( 0 );                        /* okay to go ahead and lock */
  935. }
  936.  
  937. /*  T T L O C K  */
  938.  
  939. static
  940. ttlock(ttfd) char *ttfd; {              /* lock uucp if possible */
  941. #ifndef aegis
  942. #ifdef ATT3BX
  943.     FILE *lck_fild;
  944. #endif /* att3bx */
  945.     int lck_fil, l4l;
  946.     int pid_buf = getpid();             /* pid to save in lock file */
  947.  
  948.     hasLock = 0;                        /* not locked yet */
  949.     l4l = look4lk(ttfd);
  950.     if (l4l < 0) return (-1);           /* already locked */
  951.     if (l4l == 1) return (0);           /* can't read/write lock directory */
  952.     lck_fil = creat(flfnam, 0444);      /* create lock file ... */
  953.     if (lck_fil < 0) return (-1);       /* create of lockfile failed */
  954.                 /* creat leaves file handle open for writing -- hf */
  955. #ifdef ATT3BX
  956.     {
  957.     char string[12];
  958.     sprintf(string,"%10d\n", pid_buf); /* Fixed by JZ */
  959.     write (lck_fil, string, 11);
  960.     }
  961. #else
  962.     write (lck_fil, &pid_buf, sizeof(pid_buf) ); /* uucp expects int in file */
  963. #endif /* att3bx */
  964.     close (lck_fil);
  965.     chmod(flfnam,0644);            /* make it readable by uucp */
  966.     hasLock = 1;                        /* now is locked */
  967. #endif /* not aegis */
  968.     return(0);
  969. }
  970.  
  971. /*  T T U N L O C K  */
  972.  
  973. static
  974. ttunlck() {                             /* kill uucp lock if possible */
  975.     if (hasLock) return( unlink( flfnam ) );
  976.     return(0);
  977. }
  978.  
  979. /* New-style (4.3BSD) UUCP line direction control (Stan Barber, Rice U) */
  980.  
  981. #ifdef NEWUUCP
  982. acucntrl(flag,ttname) char *flag, *ttname; {
  983.     char x[DEVNAMLEN+32], *device, *devname;
  984.  
  985.     if (strcmp(ttname,CTTNAM) == 0 || xlocal == 0) /* If not local, */
  986.         return;                         /* just return. */
  987.     device = ((devname = xxlast(ttname,'/')) != NULL ? devname+1 : ttname);
  988.     if (strncmp(device,"LCK..",4) == 0) device += 5;
  989.     sprintf(x,"/usr/lib/uucp/acucntrl %s %s",flag,device);
  990.     debug(F000,"called ",x,0);
  991.     system(x);
  992. }
  993. #endif /* newuucp */
  994.  
  995. /*  T T P K T  --  Condition the communication line for packets. */
  996. /*              or for modem dialing */
  997.  
  998. #define DIALING 4               /* flags (via flow) for modem handling */
  999. #define CONNECT 5
  1000.  
  1001. /*  If called with speed > -1, also set the speed.  */
  1002.  
  1003. /*  Returns 0 on success, -1 on failure.  */
  1004.  
  1005. ttpkt(speed,flow,parity) int speed, flow, parity; {
  1006.     int s, x;
  1007.  
  1008.     if (ttyfd < 0) return(-1);          /* Not open. */
  1009.     ttprty = parity;                    /* Let other tt functions see this. */
  1010.     debug(F101,"ttpkt setting ttprty","",ttprty);
  1011.     s = ttsspd(speed);                  /* Check the speed */
  1012.  
  1013. #ifndef UXIII
  1014.     if (flow == 1) ttraw.sg_flags |= TANDEM; /* Use XON/XOFF if selected */
  1015.     if (flow == 0) ttraw.sg_flags &= ~TANDEM;
  1016.     ttraw.sg_flags |= RAW;              /* Go into raw mode */
  1017.     ttraw.sg_flags &= ~(ECHO|CRMOD);    /* Use CR for break character */
  1018. #ifdef TOWER1
  1019.     ttraw.sg_flags &= ~ANYP;            /* Must tell Tower no parity */
  1020. #endif /* tower1 */
  1021.     if (s > -1) ttraw.sg_ispeed = ttraw.sg_ospeed = s; /* Do the speed */
  1022.     if (stty(ttyfd,&ttraw) < 0) return(-1);     /* Set the new modes. */
  1023.  
  1024. #ifdef MYREAD
  1025. #ifdef BSD4
  1026. /* Try to make reads nonblocking */
  1027. #ifdef aegis
  1028.     return(0);
  1029. #endif /* aegis */
  1030. #ifdef FIONBIO
  1031.     x = 1;
  1032.     if (ioctl(ttyfd,FIONBIO,&x) < 0) {
  1033.         perror("ttpkt ioctl");
  1034.         return(-1);
  1035.     }
  1036. #else /* fionbio */
  1037. #ifdef FNDELAY
  1038.     if (fcntl(ttyfd,F_SETFL,fcntl(ttyfd,F_GETFL,0) | FNDELAY) == -1) {
  1039.         return(-1);
  1040.     }
  1041. #endif /* fndelay */
  1042. #endif /* bsd4 */
  1043.     ttflui();                           /* Flush any pending input */
  1044.     return(0);
  1045. #endif /* bsd4 */
  1046. #else  /* myread */
  1047.     ttflui();                           /* Flush any pending input */
  1048.     return(0);
  1049. #endif /* myread */
  1050. #endif /* not uxiii */
  1051.  
  1052. #ifdef UXIII
  1053.     if (flow == 1) ttraw.c_iflag |= (IXON|IXOFF);
  1054.     if (flow == 0) ttraw.c_iflag &= ~(IXON|IXOFF);
  1055.  
  1056.     if (flow == DIALING)  ttraw.c_cflag |= CLOCAL|HUPCL;
  1057.     if (flow == CONNECT)  ttraw.c_cflag &= ~CLOCAL;
  1058.  
  1059.     ttraw.c_lflag &= ~(ICANON|ECHO);
  1060.     ttraw.c_lflag |= ISIG;              /* do check for interrupt */
  1061.     ttraw.c_iflag |= (BRKINT|IGNPAR);
  1062.     ttraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IUCLC|INPCK|ISTRIP|IXANY);
  1063.     ttraw.c_oflag &= ~OPOST;
  1064.     ttraw.c_cflag &= ~(CSIZE|PARENB);
  1065.     ttraw.c_cflag |= (CS8|CREAD);
  1066. #ifdef IX370
  1067.     ttraw.c_cc[4] = 48;  /* So Series/1 doesn't interrupt on every char */
  1068.     ttraw.c_cc[5] = 1;
  1069. #else
  1070. #ifdef VXVE
  1071.     ttraw.c_cc[4] = 1;   /* [VMIN]  for CDC VX/VE */
  1072.     ttraw.c_cc[5] = 0;   /* [VTIME] for CDC VX/VE */
  1073. #else
  1074. #ifdef MYREAD
  1075.     ttraw.c_cc[4] = 1;   /* return max of this many characters */
  1076.     ttraw.c_cc[5] = 0;   /* or when this many secs/10 expire w/no input */
  1077. #else
  1078.     ttraw.c_cc[4] = 1;   /* [VMIN]  Maybe should be bigger for all Sys V? */
  1079.     ttraw.c_cc[5] = 0;   /* [VTIME] Should be set high enough to ignore */
  1080.                                         /* intercharacter spacing? */
  1081.     /* But then we have to distinguish between Sys III and Sys V.. */
  1082. #endif
  1083. #endif
  1084. #endif
  1085.     if (s > -1) {                       /* set speed */
  1086.         ttraw.c_cflag &= ~CBAUD;
  1087.         ttraw.c_cflag |= s;
  1088.     }
  1089.     if (ioctl(ttyfd,TCSETAW,&ttraw) < 0) return(-1);  /* set new modes . */
  1090.     if (flow == DIALING) {
  1091. #ifndef aegis
  1092.         if (fcntl(ttyfd,F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NDELAY) < 0 )
  1093.                 return(-1);
  1094. #endif /* not aegis */
  1095.         close( open(ttnmsv,2) );        /* magic to force mode change!!! */
  1096.     }
  1097.     ttflui();
  1098.     return(0);
  1099. #endif /* uxiii */
  1100. }
  1101.  
  1102. /*  T T V T -- Condition communication line for use as virtual terminal  */
  1103.  
  1104. ttvt(speed,flow) int speed, flow; {
  1105.     int s;
  1106.     if (ttyfd < 0) return(-1);          /* Not open. */
  1107.  
  1108.     s = ttsspd(speed);                  /* Check the speed */
  1109.  
  1110. #ifndef UXIII
  1111.     if (flow == 1) tttvt.sg_flags |= TANDEM; /* XON/XOFF if selected */
  1112.     if (flow == 0) tttvt.sg_flags &= ~TANDEM;
  1113.     tttvt.sg_flags |= RAW;              /* Raw mode */
  1114. #ifdef TOWER1
  1115.     tttvt.sg_flags &= ~(ECHO|ANYP);     /* No echo or system III ??? parity */
  1116. #else
  1117.     tttvt.sg_flags &= ~ECHO;            /* No echo */
  1118. #endif
  1119.     if (s > -1) tttvt.sg_ispeed = tttvt.sg_ospeed = s; /* Do the speed */
  1120.     if (stty(ttyfd,&tttvt) < 0) return(-1);
  1121.  
  1122. #ifdef MYREAD
  1123. #ifdef BSD4
  1124. /* Make reads nonblocking */
  1125. #ifdef aegis
  1126.         return(0);
  1127. #endif
  1128.         if (fcntl(ttyfd,F_SETFL,fcntl(ttyfd,F_GETFL,0) | FNDELAY) == -1)
  1129.             return(-1);
  1130.         else return(0);
  1131. #endif /* bsd4 */
  1132. #endif /* myread */
  1133.  
  1134. #else /* uxiii */
  1135.     if (flow == 1) tttvt.c_iflag |= (IXON|IXOFF);
  1136.     if (flow == 0) tttvt.c_iflag &= ~(IXON|IXOFF);
  1137.  
  1138.     if (flow == DIALING)  tttvt.c_cflag |= CLOCAL|HUPCL;
  1139.     if (flow == CONNECT)  tttvt.c_cflag &= ~CLOCAL;
  1140.  
  1141.     tttvt.c_lflag &= ~(ISIG|ICANON|ECHO);
  1142.     tttvt.c_iflag |= (IGNBRK|IGNPAR);
  1143.     tttvt.c_iflag &= ~(INLCR|IGNCR|ICRNL|IUCLC|BRKINT|INPCK|ISTRIP|IXANY);
  1144.     tttvt.c_oflag &= ~OPOST;
  1145.     tttvt.c_cflag &= ~(CSIZE|PARENB);
  1146.     tttvt.c_cflag |= (CS8|CREAD);
  1147.     tttvt.c_cc[4] = 1;
  1148.     tttvt.c_cc[5] = 0;
  1149.  
  1150.     if (s > -1) {                       /* set speed */
  1151.         tttvt.c_cflag &= ~CBAUD;
  1152.         tttvt.c_cflag |= s;
  1153.     }
  1154.     if (ioctl(ttyfd,TCSETAW,&tttvt) < 0) return(-1);  /* set new modes . */
  1155.  
  1156.     if (flow == DIALING) {
  1157. #ifndef aegis
  1158.         if (fcntl(ttyfd,F_SETFL, fcntl(ttyfd, F_GETFL, 0) & ~O_NDELAY) < 0 )
  1159.                 return(-1);
  1160. #endif
  1161.         close( open(ttnmsv,2) );        /* magic to force mode change!!! */
  1162.         }
  1163. #endif
  1164.     return(0);
  1165. }
  1166.  
  1167. /*  T T S S P D  --  Return the internal baud rate code for 'speed'.  */
  1168.  
  1169. ttsspd(speed) {
  1170.     int s, spdok;
  1171.  
  1172.     if (speed < 0) return(-1);
  1173.         spdok = 1;                      /* Assume arg ok */
  1174.         switch (speed) {
  1175.             case 0:    s = B0;    break;        /* Just the common ones. */
  1176.             case 110:  s = B110;  break;        /* The others from ttydev.h */
  1177.             case 150:  s = B150;  break;        /* could also be included if */
  1178.             case 300:  s = B300;  break;        /* necessary... */
  1179.             case 600:  s = B600;  break;
  1180.             case 1200: s = B1200; break;
  1181.             case 1800: s = B1800; break;
  1182.             case 2400: s = B2400; break;
  1183.             case 4800: s = B4800; break;
  1184.             case 9600: s = B9600; break;
  1185. #ifdef B19200
  1186.             case 19200: s = B19200; break;
  1187. #else
  1188.             case 19200: s = EXTA; break;
  1189. #endif
  1190. #ifdef B38400
  1191.             case 38400: s = B38400; break;
  1192. #endif
  1193.             default:
  1194.                 spdok = 0;
  1195.                 fprintf(stderr,"Unsupported line speed - %d\n",speed);
  1196.                 fprintf(stderr,"Current speed not changed\n");
  1197.                 break;
  1198.         }
  1199.         if (spdok) return(s); else return(-1);
  1200.  }
  1201.  
  1202. /*  T T F L U I  --  Flush tty input buffer */
  1203.  
  1204. ttflui() {
  1205.  
  1206. #ifndef UXIII
  1207.     long n;
  1208. #endif
  1209.     if (ttyfd < 0) return(-1);          /* Not open. */
  1210.  
  1211.     ungotn = -1;                        /* Initialize myread() stuff */
  1212.     inbufc = 0;
  1213.  
  1214. #ifdef aegis
  1215.     sio_$control((short)ttyfd, sio_$flush_in, true, st);
  1216.     if (st.all != status_$ok)
  1217.     {  fprintf(stderr, "flush failed: "); error_$print(st); }
  1218.     else {      /* sometimes the flush doesn't work */
  1219.         for (;;)
  1220.         {   char buf[256];
  1221.             /* eat all the characters that shouldn't be available */
  1222.             (void)ios_$get((short)ttyfd, ios_$cond_opt, buf, 256L, st);
  1223.             if (st.all == ios_$get_conditional_failed) break;
  1224.             fprintf(stderr, "flush failed(2): "); error_$print(st);
  1225.         }
  1226.     }
  1227. #else
  1228. #ifdef UXIII
  1229. #ifndef VXVE
  1230.     if (ioctl(ttyfd,TCFLSH,0) < 0) perror("flush failed");
  1231. #endif /* vxve */
  1232. #else
  1233. #ifdef TIOCFLUSH
  1234. #ifdef ANYBSD
  1235.     n = FREAD;                          /* Specify read queue */
  1236.     if (ioctl(ttyfd,TIOCFLUSH,&n) < 0) perror("flush failed");
  1237. #else
  1238.     if (ioctl(ttyfd,TIOCFLUSH,0) < 0) perror("flush failed");
  1239. #endif
  1240. #endif
  1241. #endif
  1242. #endif
  1243.     return(0);
  1244. }
  1245.  
  1246. /* Interrupt Functions */
  1247.  
  1248.  
  1249. /* Timeout handler for communication line input functions */
  1250.  
  1251. timerh() {
  1252.     longjmp(sjbuf,1);
  1253. }
  1254.  
  1255.  
  1256. /* Set up terminal interrupts on console terminal */
  1257.  
  1258. #ifdef UXIII
  1259. esctrp() {                              /* trap console escapes (^\) */
  1260.     conesc = 1;
  1261.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  1262. }
  1263. #endif
  1264.  
  1265. #ifdef V7
  1266. esctrp() {                              /* trap console escapes (^\) */
  1267.     conesc = 1;
  1268.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  1269. }
  1270. #endif
  1271.  
  1272. #ifdef C70
  1273. esctrp() {                              /* trap console escapes (^\) */
  1274.     conesc = 1;
  1275.     signal(SIGQUIT,SIG_IGN);            /* ignore until trapped */
  1276. }
  1277. #endif
  1278.  
  1279. /*  C O N I N T  --  Console Interrupt setter  */
  1280.  
  1281. conint(f) int (*f)(); {                 /* Set an interrupt trap. */
  1282.     int x, y;
  1283. #ifdef SIGTSTP
  1284.     int stptrap();                      /* Suspend trap */
  1285. #endif
  1286.  
  1287. /* Check for background operation, even if not running on real tty, so that */
  1288. /* background flag can be set correctly. */
  1289.  
  1290. #ifdef BSD4
  1291.     int mypgrp;                         /* In BSD, we can check whether */
  1292.     int ctpgrp;                         /* this process's group is the */
  1293.                                         /* same as the controlling */
  1294.     mypgrp = getpgrp(0);                /* terminal's process group. */
  1295.     ioctl (1, TIOCGPGRP, &ctpgrp);
  1296.     x = (mypgrp != ctpgrp);             /* If they differ, then background. */
  1297.     debug(F101,"conint process group test","",x);
  1298. #else
  1299.     x = (signal(SIGINT,SIG_IGN) == SIG_IGN);
  1300.     debug(F101,"conint signal test","",x);
  1301. #endif
  1302.     y = isatty(0);
  1303.     debug(F101,"conint isatty test","",y);
  1304. #ifdef BSD29
  1305. /* For some reason the signal() test doesn't work under 2.9 BSD... */
  1306.     backgrd = !y;
  1307. #else
  1308.     backgrd = (x || !y);
  1309. #endif
  1310.     debug(F101,"conint backgrd","",backgrd);
  1311.  
  1312.     signal(SIGHUP,f);                   /* Ensure lockfile cleared on hangup */
  1313.     signal(SIGTERM,f);                  /* or soft kill. */
  1314.  
  1315. /* check if invoked in background -- if so signals set to be ignored */
  1316.  
  1317.     if (backgrd) {                      /* In background, ignore signals */
  1318. #ifdef SIGTSTP
  1319.         signal(SIGTSTP,SIG_IGN);        /* Keyboard stop */
  1320. #endif
  1321.         signal(SIGQUIT,SIG_IGN);        /* Keyboard quit */
  1322.         signal(SIGINT,SIG_IGN);         /* Keyboard interrupt */
  1323.     } else {
  1324.         signal(SIGINT,f);               /* Catch terminal interrupt */
  1325. #ifdef SIGTSTP
  1326.         signal(SIGTSTP,stptrap);        /* Keyboard stop */
  1327. #endif
  1328. #ifdef UXIII
  1329.         signal(SIGQUIT,esctrp);         /* Quit signal, Sys III/V. */
  1330.         if (conesc) conesc = 0;         /* Clear out pending escapes */
  1331. #else
  1332. #ifdef V7
  1333.         signal(SIGQUIT,esctrp);         /* V7 like Sys III/V */
  1334.         if (conesc) conesc = 0;
  1335. #else
  1336. #ifdef aegis
  1337.         signal(SIGQUIT,f);              /* Apollo, catch it like others. */
  1338. #else
  1339.         signal(SIGQUIT,SIG_IGN);        /* Others, ignore like 4D & earlier. */
  1340. #endif
  1341. #endif
  1342. #endif
  1343.         conif = 1;                      /* Flag console interrupts on. */
  1344.     }
  1345.     return;
  1346. }
  1347.  
  1348.  
  1349. /*  C O N N O I  --  Reset console terminal interrupts */
  1350.  
  1351. connoi() {                              /* Console-no-interrupts */
  1352.  
  1353. #ifdef SIGTSTP
  1354.     signal(SIGTSTP,SIG_DFL);
  1355. #endif
  1356.     signal(SIGINT,SIG_DFL);
  1357.     signal(SIGHUP,SIG_DFL);
  1358.     signal(SIGQUIT,SIG_DFL);
  1359.     signal(SIGTERM,SIG_DFL);
  1360.     conif = 0;                          /* Flag interrupt trapping off */
  1361. }
  1362.  
  1363. /*  myread() -- For use by systems that can do nonblocking read() calls  */
  1364. /*
  1365.  Returns:
  1366.   -1  if no characters available, timer expired
  1367.   -2  upon error (such as disconnect),
  1368.   otherwise value of character (0 or greater)
  1369. */
  1370. myread() {
  1371.     static int inbuf_item;
  1372.     static CHAR inbuf[257];
  1373.     CHAR readit;
  1374.  
  1375.     if (ungotn >= 0) {
  1376.         readit = ungotn;
  1377.         ungotn = -1;
  1378.     } else {
  1379.         if (inbufc > 0) {
  1380.             readit = inbuf[++inbuf_item];
  1381.         } else {
  1382. #ifdef aegis
  1383.     /* myread() returns -1 when no input is available.  All the users of */
  1384.     /* myread() explicitly loop until it returns a character or error. */
  1385.     /* The Apollo code waits for input to be available. */
  1386.  
  1387.     /* read in characters */
  1388.             inbufc = ios_$get((short)ttyfd, ios_$cond_opt, inbuf, 256L, st);
  1389.             errno = EIO;
  1390.             if (st.all == ios_$get_conditional_failed) /* get at least one */
  1391.                 inbufc = ios_$get((short)ttyfd, 0, inbuf, 1L, st);
  1392.             if (st.all == ios_$end_of_file) inbufc = 0;
  1393.             else if (st.all != status_$ok)
  1394.             {   inbufc = -1; errno = EIO; }
  1395. #else
  1396.             inbufc = read(ttyfd,inbuf,256);
  1397.             if (inbufc > 0) {
  1398.                 inbuf[inbufc] = '\0';
  1399.                 debug(F101,"myread read","",inbufc);
  1400.             }
  1401. #endif /* aegis */
  1402.             if (inbufc == 0) {
  1403.                 if (ttmdm) {
  1404.                     debug(F101,"myread read=0, ttmdm","",ttmdm);
  1405.                     errno = 9999;       /* magic number for no carrier */
  1406.                     return(-2);         /* end of file has no errno */
  1407.                 } else return(-1);      /* in sys 5 means no data available */
  1408.             }
  1409.             if (inbufc < 0) {           /* Real error */
  1410. #ifdef EWOULDBLOCK
  1411.                 if (errno == EWOULDBLOCK) return(-1); else return(-2);
  1412. #else
  1413.                 return(-2);
  1414. #endif /* ewouldblock */
  1415.             }
  1416.             readit = inbuf[inbuf_item = 0];
  1417.         }
  1418.         inbufc--;
  1419.     }
  1420.     return(((int) readit) & 255);
  1421. }
  1422.  
  1423. myunrd(ch) CHAR ch; {                   /* push back up to one character */
  1424.     ungotn = ch;
  1425. }
  1426.  
  1427. /*  I N I T R A W Q  --  Set up to read /DEV/KMEM for character count.  */
  1428.  
  1429. #ifdef  V7
  1430. /*
  1431.  Used in Version 7 to simulate Berkeley's FIONREAD ioctl call.  This
  1432.  eliminates blocking on a read, because we can read /dev/kmem to get the
  1433.  number of characters available for raw input.  If your system can't
  1434.  or you won't let it read /dev/kmem (the world that is) then you must
  1435.  figure out a different way to do the counting of characters available,
  1436.  or else replace this by a dummy function that always returns 0.
  1437. */
  1438. /*
  1439.  * Call this routine as: initrawq(tty)
  1440.  * where tty is the file descriptor of a terminal.  It will return
  1441.  * (as a char *) the kernel-mode memory address of the rawq character
  1442.  * count, which may then be read.  It has the side-effect of flushing
  1443.  * input on the terminal.
  1444.  */
  1445. /*
  1446.  * John Mackin, Physiology Dept., University of Sydney (Australia)
  1447.  * ...!decvax!mulga!physiol.su.oz!john
  1448.  *
  1449.  * Permission is hereby granted to do anything with this code, as
  1450.  * long as this comment is retained unmodified and no commercial
  1451.  * advantage is gained.
  1452.  */
  1453. #include <a.out.h>
  1454. #include <sys/proc.h>
  1455.  
  1456. char *initrawq(tty) int tty; {
  1457. #ifdef UTS24
  1458.     return(0);
  1459. #else
  1460. #ifdef BSD29
  1461.     return(0);
  1462. #else
  1463.     long lseek();
  1464.     static struct nlist nl[] = {
  1465.         {PROCNAME},
  1466.         {NPROCNAME},
  1467.         {""}
  1468.     };
  1469.     static struct proc *pp;
  1470.     char *malloc(), *qaddr, *p, c;
  1471.     int m, pid, me;
  1472.     NPTYPE xproc;                       /* Its type is defined in makefile. */
  1473.     int catch();
  1474.  
  1475.     me = getpid();
  1476.     if ((m = open("/dev/kmem", 0)) < 0) err("kmem");
  1477.     nlist(BOOTNAME, nl);
  1478.     if (nl[0].n_type == 0) err("proc array");
  1479.  
  1480.     if (nl[1].n_type == 0) err("nproc");
  1481.  
  1482.     lseek(m, (long)(nl[1].n_value), 0);
  1483.     read (m, &xproc, sizeof(xproc));
  1484.     signal(SIGALRM, catch);
  1485.     if ((pid = fork()) == 0) {
  1486.         while(1)
  1487.             read(tty, &c, 1);
  1488.     }
  1489.     alarm(2);
  1490.  
  1491.     if(setjmp(jjbuf) == 0) {
  1492.         while(1)
  1493.             read(tty, &c, 1);
  1494.     }
  1495.     signal(SIGALRM, SIG_DFL);
  1496.  
  1497. #ifdef DIRECT
  1498.     pp = (struct proc *) nl[0].n_value;
  1499. #else
  1500.     if (lseek(m, (long)(nl[0].n_value), 0) < 0L) err("seek");
  1501.     if (read(m, &pp, sizeof(pp)) != sizeof(pp))  err("no read of proc ptr");
  1502. #endif
  1503.     lseek(m, (long)(nl[1].n_value), 0);
  1504.     read(m, &xproc, sizeof(xproc));
  1505.  
  1506.     if (lseek(m, (long)pp, 0) < 0L) err("Can't seek to proc");
  1507.     if ((p = malloc(xproc * sizeof(struct proc))) == NULL) err("malloc");
  1508.     if (read(m,p,xproc * sizeof(struct proc)) != xproc*sizeof(struct proc))
  1509.         err("read proc table");
  1510.     for (pp = (struct proc *)p; xproc > 0; --xproc, ++pp) {
  1511.         if (pp -> p_pid == (short) pid) goto iout;
  1512.     }
  1513.     err("no such proc");
  1514.  
  1515. iout:
  1516.     close(m);
  1517.     qaddr = (char *)(pp -> p_wchan);
  1518.     free (p);
  1519.     kill(pid, SIGKILL);
  1520.     wait((int *)0);             /* Destroy the ZOMBIEs! */
  1521.     return (qaddr);
  1522. #endif
  1523. #endif
  1524. }
  1525.  
  1526. /*  More V7-support functions...  */
  1527.  
  1528. static
  1529. err(s) char *s; {
  1530.     char buf[200];
  1531.  
  1532.     sprintf(buf, "fatal error in initrawq: %s", s);
  1533.     perror(buf);
  1534.     doexit(1);
  1535. }
  1536.  
  1537. static
  1538. catch() {
  1539.     longjmp(jjbuf, -1);
  1540. }
  1541.  
  1542.  
  1543. /*  G E N B R K  --  Simulate a modem break.  */
  1544.  
  1545. #define BSPEED  B150
  1546.  
  1547. genbrk(fn) int fn; {
  1548.     struct sgttyb ttbuf;
  1549.     int ret, sospeed;
  1550.  
  1551.     ret = ioctl(fn, TIOCGETP, &ttbuf);
  1552.     sospeed = ttbuf.sg_ospeed;
  1553.     ttbuf.sg_ospeed = BSPEED;
  1554.     ret = ioctl(fn, TIOCSETP, &ttbuf);
  1555.     ret = write(fn, "\0\0\0\0\0\0\0\0\0\0\0\0", 8);
  1556.     ttbuf.sg_ospeed = sospeed;
  1557.     ret = ioctl(fn, TIOCSETP, &ttbuf);
  1558.     ret = write(fn, "@", 1);
  1559.     return;
  1560. }
  1561. #endif
  1562.  
  1563. /*  T T C H K  --  Tell how many characters are waiting in tty input buffer  */
  1564.  
  1565. ttchk() {
  1566.     int x; long n;
  1567. #ifdef FIONREAD
  1568.     x = ioctl(ttyfd, FIONREAD, &n);     /* Berkeley and maybe some others */
  1569.     debug(F101,"ttchk","",n);
  1570.     return((x < 0) ? 0 : n);
  1571. #else
  1572. #ifdef  V7
  1573.     lseek(kmem[TTY], (long) qaddr[TTY], 0); /* 7th Edition Unix */
  1574.     x = read(kmem[TTY], &n, sizeof(int));
  1575.     return((x == sizeof(int))? n: 0);
  1576. #else
  1577. #ifdef UXIII
  1578.     return(inbufc + (ungotn >= 0) );    /* Sys III, Sys V */
  1579. #else
  1580. #ifdef PROVX1
  1581.     x = ioctl(ttyfd, TIOCQCNT, &ttbuf); /* Pro/3xx Venix V.1 */
  1582.     n = ttbuf.sg_ispeed & 0377;
  1583.     return((x < 0) ? 0 : n);
  1584. #else
  1585. #ifdef aegis
  1586.     return(inbufc + (ungotn >= 0) );    /* Apollo Aegis */
  1587. #else
  1588. #ifdef C70
  1589.     return(inbufc + (ungotn >= 0) );    /* etc... */
  1590. #else
  1591.     return(0);
  1592. #endif
  1593. #endif
  1594. #endif
  1595. #endif
  1596. #endif
  1597. #endif
  1598. }
  1599.  
  1600.  
  1601. /*  T T X I N  --  Get n characters from tty input buffer  */
  1602.  
  1603. /*  Returns number of characters actually gotten, or -1 on failure  */
  1604.  
  1605. /*  Intended for use only when it is known that n characters are actually */
  1606. /*  Available in the input buffer.  */
  1607.  
  1608. ttxin(n,buf) int n; CHAR *buf; {
  1609.     int x;
  1610.  
  1611. #ifdef MYREAD
  1612.     for( x = 0; (x > -1) && (x < n); buf[x++] = myread() );
  1613. #else
  1614.     debug(F101,"ttxin: n","",n);
  1615.     x = read(ttyfd,buf,n);
  1616.     debug(F101," x","",x);
  1617. #endif
  1618.     if (x > 0) buf[x] = '\0';
  1619.     if (x < 0) x = -1;
  1620.     return(x);
  1621. }
  1622.  
  1623. /*  T T O L  --  Similar to "ttinl", but for writing.  */
  1624.  
  1625. ttol(s,n) int n; char *s; {
  1626.     int x;
  1627.     if (ttyfd < 0) return(-1);          /* Not open. */
  1628.     x = write(ttyfd,s,n);
  1629.     debug(F111,"ttol",s,n);
  1630.     if (x < 0) debug(F101,"ttol failed","",x);
  1631.     return(x);
  1632. }
  1633.  
  1634.  
  1635. /*  T T O C  --  Output a character to the communication line  */
  1636.  
  1637. /*
  1638.  This function should only used for interactive, character-mode operations,
  1639.  like terminal connection, script execution, dialer i/o, where the overhead
  1640.  of the signals and alarms does not create a bottleneck.
  1641. */
  1642.  
  1643. ttoc(c) char c; {
  1644.     int x;
  1645.     if (ttyfd < 0) return(-1);          /* Check for not open. */
  1646.     signal(SIGALRM,timerh);        /* Enable timer interrupt */
  1647.     alarm(2);                /* for 2 seconds. */
  1648.     x = write(ttyfd,&c,1);        /* Try to write the character. */
  1649.     if (setjmp(sjbuf)) {        /* Timer went off? */
  1650.     x = -1;                /* Yes, set return code for failure */
  1651.     }
  1652.     alarm(0);                /* Turn off timers, etc. */
  1653.     signal(SIGALRM,SIG_DFL);
  1654.     return(x);
  1655. }
  1656.  
  1657. /*  T T I N L  --  Read a record (up to break character) from comm line.  */
  1658. /*
  1659.   If no break character encountered within "max", return "max" characters,
  1660.   with disposition of any remaining characters undefined.  Otherwise, return
  1661.   the characters that were read, including the break character, in "dest" and
  1662.   the number of characters read as the value of the function, or 0 upon end of
  1663.   file, or -1 if an error occurred.  Times out & returns error if not completed
  1664.   within "timo" seconds.
  1665. */
  1666. #define CTRLC '\03'
  1667. ttinl(dest,max,timo,eol) int max,timo; CHAR *dest, eol; {
  1668.     int x = 0, ccn = 0, c, i, j, m, n;  /* local variables */
  1669.  
  1670.     if (ttyfd < 0) return(-1);          /* Not open. */
  1671.  
  1672.     m = (ttprty) ? 0177 : 0377;         /* Parity stripping mask. */
  1673.     *dest = '\0';                       /* Clear destination buffer */
  1674.     if (timo) signal(SIGALRM,timerh);   /* Enable timer interrupt */
  1675.     alarm(timo);                        /* Set it. */
  1676.     if (setjmp(sjbuf)) {                /* Timer went off? */
  1677.         x = -1;
  1678.     } else {
  1679.         i = 0;                          /* Next char to process */
  1680.         j = 0;                          /* Buffer position */
  1681.         while (1) {
  1682.             if ((n = ttchk()) > 0) {    /* See how many chars have arrived */
  1683.                 if (n > (max - j)) n = max - j;
  1684.                 if ((n = ttxin(n,dest+i)) < 0) { /* Get them all at once */
  1685.                     x = -1;
  1686.                     break;
  1687.                 }
  1688.             } else {                    /* Or else... */
  1689.                 n = 1;                  /* just wait for a char */
  1690.                 if ((c = ttinc(0)) == -1) {
  1691.                     x = -1;
  1692.                     break;
  1693.                 }
  1694.                 dest[i] = c;            /* Got one. */
  1695.             }
  1696.             j = i + n;                  /* Remember next buffer position. */
  1697.             if (j >= max) {
  1698.                 debug(F101,"ttinl buffer overflow","",j);
  1699.                 x = -1;
  1700.                 break;
  1701.             }
  1702.             for (i; i < j; i++) {       /* Go thru all chars we just got */
  1703.                 dest[i] &= m;           /* Strip any parity */
  1704.                 if (dest[i] == eol) {   /* Got eol? */
  1705.                   dest[++i] = '\0';     /* Yes, tie off string, */
  1706.                   alarm(0);             /* turn off timers, etc, */
  1707.                   if (timo) signal(SIGALRM,SIG_DFL); /* and return length. */
  1708.                   return(i);
  1709.               } else if ((dest[i] & 0177) == CTRLC) { /* Check for ^C^C */
  1710.                   if (++ccn > 1) {      /* If we got 2 in a row, clean up */
  1711.                      alarm(0);          /* and exit. */
  1712.                      signal(SIGALRM,SIG_DFL);
  1713.                      fprintf(stderr,"^C...");
  1714.                      ttres();
  1715.                      fprintf(stderr,"\n");
  1716.                      return(-2);
  1717.                   }
  1718.               } else ccn = 0;   /* Not ^C, so reset ^C counter, */
  1719.           }
  1720.         }
  1721.     }
  1722.     debug(F100,"ttinl timout","",0);    /* Get here on timeout. */
  1723.     debug(F111," with",dest,i);
  1724.     alarm(0);                           /* Turn off timer */
  1725.     signal(SIGALRM,SIG_DFL);            /* and interrupt, */
  1726.     return(x);                          /* and return error code. */
  1727. }
  1728.  
  1729. /*  T T I N C --  Read a character from the communication line  */
  1730.  
  1731. ttinc(timo) int timo; {
  1732.     int m, n = 0;
  1733.     CHAR ch = 0;
  1734.  
  1735.     m = (ttprty) ? 0177 : 0377;         /* Parity stripping mask. */
  1736.     if (ttyfd < 0) return(-1);          /* Not open. */
  1737.     if (timo <= 0) {                    /* Untimed. */
  1738. #ifdef MYREAD
  1739.         /* comm line failure returns -1 thru myread, so no &= 0377 */
  1740.         while ((n = myread()) == -1) ;  /* Wait for a character... */
  1741.         if (n == -2) n++;
  1742.         return( (n < 0) ? -1 : n & m );
  1743. #else
  1744.         while ((n = read(ttyfd,&ch,1)) == 0) ; /* Wait for a character. */
  1745.         return( (n < 0) ? -1 : (ch & 0377) );
  1746. #endif
  1747.     }
  1748.     signal(SIGALRM,timerh);             /* Timed, set up timer. */
  1749.     alarm(timo);
  1750.     if (setjmp(sjbuf)) {
  1751.         n = -1;
  1752.     } else {
  1753. #ifdef MYREAD
  1754.         while ((n = myread()) == -1) ;  /* If managing own buffer... */
  1755.         if (n == -2) {
  1756.             n++;
  1757.         } else {
  1758.             ch = n;
  1759.             n = 1;
  1760.         }
  1761. #else
  1762.         n = read(ttyfd,&ch,1);          /* Otherwise call the system. */
  1763. #endif
  1764.     }
  1765.     alarm(0);                           /* Turn off timer, */
  1766.     signal(SIGALRM,SIG_DFL);            /* and interrupt. */
  1767.     return( (n < 0) ? -1 : (ch & m) );  /* Return char or -1. */
  1768. }
  1769.  
  1770. /*  T T S N D B  --  Send a BREAK signal  */
  1771.  
  1772. ttsndb() {
  1773.     int x; long n; char spd;
  1774.  
  1775.     if (ttyfd < 0) return(-1);          /* Not open. */
  1776.  
  1777. #ifdef PROVX1
  1778.     gtty(ttyfd,&ttbuf);                 /* Get current tty flags */
  1779.     spd = ttbuf.sg_ospeed;              /* Save speed */
  1780.     ttbuf.sg_ospeed = B50;              /* Change to 50 baud */
  1781.     stty(ttyfd,&ttbuf);                 /*  ... */
  1782.     write(ttyfd,brnuls,3);              /* Send 3 nulls */
  1783.     ttbuf.sg_ospeed = spd;              /* Restore speed */
  1784.     stty(ttyfd,&ttbuf);                 /*  ... */
  1785.     return(0);
  1786. #else
  1787. #ifdef aegis
  1788.     sio_$control((short)ttyfd, sio_$send_break, 250, st);
  1789.     return(0);
  1790. #else
  1791. #ifdef UXIII
  1792.     if (ioctl(ttyfd,TCSBRK,(char *)0) < 0) {    /* Send a BREAK */
  1793.         perror("Can't send BREAK");
  1794.         return(-1);
  1795.     }
  1796.     return(0);
  1797. #else
  1798. #ifdef ANYBSD
  1799.     n = FWRITE;                         /* Flush output queue. */
  1800.     ioctl(ttyfd,TIOCFLUSH,&n);          /* Ignore any errors.. */
  1801.     if (ioctl(ttyfd,TIOCSBRK,(char *)0) < 0) {  /* Turn on BREAK */
  1802.         perror("Can't send BREAK");
  1803.         return(-1);
  1804.     }
  1805.     x = msleep(275);                    /* Sleep for so many milliseconds */
  1806.     if (ioctl(ttyfd,TIOCCBRK,(char *)0) < 0) {  /* Turn off BREAK */
  1807.         perror("BREAK stuck!!!");
  1808.         doexit(1);                      /* Get out, closing the line. */
  1809.                                         /*   with exit status = 1 */
  1810.     }
  1811.     return(x);
  1812. #else
  1813. #ifdef  V7
  1814.     genbrk(ttyfd);                      /* Simulate a BREAK */
  1815.     return(x);
  1816. #endif
  1817. #endif
  1818. #endif
  1819. #endif
  1820. #endif
  1821. }
  1822.  
  1823. /*  M S L E E P  --  Millisecond version of sleep().  */
  1824.  
  1825. /*
  1826.  Intended only for small intervals.  For big ones, just use sleep().
  1827. */
  1828.  
  1829. msleep(m) int m; {
  1830.  
  1831. #ifdef aegis
  1832.     time_$clock_t dur;
  1833.  
  1834.     dur.c2.high16 = 0;
  1835.     dur.c2.low32  = 250 * m; /* one millisecond = 250 four microsecond ticks */
  1836.     time_$wait(time_$relative, dur, st);
  1837.     return(0);
  1838. #else
  1839. #ifdef PROVX1
  1840.     if (m <= 0) return(0);
  1841.     sleep(-((m * 60 + 500) / 1000));
  1842.     return(0);
  1843. #endif
  1844.  
  1845. #ifdef ANYBSD
  1846.     int t1, t3, t4;
  1847.     if (m <= 0) return(0);
  1848. #ifndef BSD42
  1849. /* 2.9 and 4.1 BSD do it this way */
  1850.     if (ftime(&ftp) < 0) return(-1);    /* Get current time. */
  1851.     t1 = ((ftp.time & 0xff) * 1000) + ftp.millitm;
  1852.     while (1) {
  1853.         ftime(&ftp);                    /* new time */
  1854.         t3 = (((ftp.time & 0xff) * 1000) + ftp.millitm) - t1;
  1855.         if (t3 > m) return(t3);
  1856.     }
  1857. #else
  1858. /* 4.2 & above can do it with select()... */
  1859.     if (gettimeofday(&tv, &tz) < 0) return(-1); /* Get current time. */
  1860.     t1 = tv.tv_sec;                     /* Seconds */
  1861.  
  1862.     tv.tv_sec = 0;                      /* Use select() */
  1863.     tv.tv_usec = m * 1000L;
  1864.     return(select( 0, (int *)0, (int *)0, (int *)0, &tv) );
  1865. #endif
  1866. #endif
  1867.  
  1868. /* The clock-tick business is a pain.  Wm. E. Davidsen suggested: */
  1869. /*   #include <sys/param.h>      */
  1870. /*   #define CLOCK_TICK 1000/HZ  */
  1871. /* But I don't see the symbol HZ in this file on my VAX. */
  1872. /* Maybe just for XENIX. */
  1873.  
  1874. #ifdef UXIII
  1875. #ifdef XENIX
  1876. /* Actually, watch out.  It's 50 on the AT, 20 on older PCs... */
  1877. #define CLOCK_TICK 50                   /* millisecs per clock tick */
  1878. #else
  1879. #ifndef XENIX
  1880. #define CLOCK_TICK 17                   /* 1/60 sec */
  1881. #endif
  1882. #endif
  1883.  
  1884.     extern long times();
  1885.     long t1, t2, tarray[4];
  1886.     int t3;
  1887.  
  1888. /* In SCO Xenix 2.1.3 or later, you can use nap((long)m) to do this. */
  1889.  
  1890.     if (m <= 0) return(0);
  1891.     if ((t1 = times(tarray)) < 0) return(-1);
  1892.     while (1) {
  1893.         if ((t2 = times(tarray)) < 0) return(-1);
  1894.         t3 = ((int)(t2 - t1)) * CLOCK_TICK;
  1895.         if (t3 > m) return(t3);
  1896.     }
  1897. #endif
  1898.  
  1899. #ifdef TOWER1
  1900.     int t1, t3;
  1901.     if (m <= 0) return(0);
  1902.     if (ftime(&ftp) < 0) return(-1);            /* Get current time. */
  1903.     t1 = ((ftp.time & 0xff) * 1000) + ftp.millitm;
  1904.     while (1) {
  1905.         ftime(&ftp);                            /* new time */
  1906.         t3 = (((ftp.time & 0xff) * 1000) + ftp.millitm) - t1;
  1907.         if (t3 > m) return (t3);
  1908.     }
  1909. #endif
  1910. #endif
  1911. }
  1912.  
  1913. /*  R T I M E R --  Reset elapsed time counter  */
  1914.  
  1915. rtimer() {
  1916.     tcount = time( (long *) 0 );
  1917. }
  1918.  
  1919.  
  1920. /*  G T I M E R --  Get current value of elapsed time counter in seconds  */
  1921.  
  1922. gtimer() {
  1923.     int x;
  1924.     x = (int) (time( (long *) 0 ) - tcount);
  1925.     rtimer();
  1926.     return( (x < 0) ? 0 : x );
  1927. }
  1928.  
  1929.  
  1930. /*  Z T I M E  --  Return date/time string  */
  1931.  
  1932. ztime(s) char **s; {
  1933.  
  1934. #ifdef UXIII
  1935.     extern long time();                 /* Sys III/V way to do it */
  1936.     char *ctime();
  1937.     long clock_storage;
  1938.  
  1939.     clock_storage = time( (long *) 0 );
  1940.     *s = ctime( &clock_storage );
  1941. #endif
  1942.  
  1943. #ifdef PROVX1
  1944.     int utime[2];                       /* Venix way */
  1945.     time(utime);
  1946.     *s = ctime(utime);
  1947. #endif
  1948.  
  1949. #ifdef ANYBSD
  1950.     char *asctime();                    /* Berkeley way */
  1951.     struct tm *localtime();
  1952.     struct tm *tp;
  1953. #ifdef BSD42
  1954.     gettimeofday(&tv, &tz);             /* BSD 4.2 */
  1955.     time(&tv.tv_sec);
  1956.     tp = localtime(&tv.tv_sec);
  1957. #else
  1958.     time(&clock);                       /* BSD 4.1, 2.9 ... ceb */
  1959.     tp = localtime(&clock);
  1960. #endif
  1961.     *s = asctime(tp);
  1962. #endif
  1963.  
  1964. #ifdef TOWER1
  1965.     char *asctime();                    /* Tower way */
  1966.     struct tm *localtime();
  1967.     struct tm *tp;
  1968.  
  1969.     time(&clock);
  1970.     tp = localtime(&clock);
  1971.     *s = asctime(tp);
  1972. #endif
  1973. #ifdef V7
  1974.     char *asctime();                    /* V7 way */
  1975.     struct tm *localtime();
  1976.     struct tm *tp;
  1977.  
  1978.     time(&clock);
  1979.     tp = localtime(&clock);
  1980.     *s = asctime(tp);
  1981. #endif
  1982. }
  1983.  
  1984. /*  C O N G M  --  Get console terminal modes.  */
  1985.  
  1986. /*
  1987.  Saves current console mode, and establishes variables for switching between
  1988.  current (presumably normal) mode and other modes.
  1989. */
  1990.  
  1991. congm() {
  1992.     if (!isatty(0)) return(0);          /* only for real ttys */
  1993. #ifdef aegis
  1994.     ios_$inq_type_uid(ios_$stdin, conuid, st);
  1995.     if (st.all != status_$ok)
  1996.     {  fprintf(stderr, "problem getting stdin objtype: "); error_$print(st); }
  1997.     concrp = (conuid == mbx_$uid);
  1998.     conbufn = 0;
  1999. #endif
  2000. #ifndef UXIII
  2001.      gtty(0,&ccold);                    /* Structure for restoring */
  2002.      gtty(0,&cccbrk);                   /* For setting CBREAK mode */
  2003.      gtty(0,&ccraw);                    /* For setting RAW mode */
  2004. #else
  2005.      ioctl(0,TCGETA,&ccold);
  2006.      ioctl(0,TCGETA,&cccbrk);
  2007.      ioctl(0,TCGETA,&ccraw);
  2008. #endif
  2009. #ifdef VXVE
  2010.      cccbrk.c_line = 0;                 /* STTY line 0 for CDC VX/VE */
  2011.      ioctl(0,TCSETA,&cccbrk);
  2012.      ccraw.c_line = 0;                  /* STTY line 0 for CDC VX/VE */
  2013.      ioctl(0,TCSETA,&ccraw);
  2014. #endif /* vxve */
  2015.      cgmf = 1;                          /* Flag that we got them. */
  2016.      return(0);
  2017. }
  2018.  
  2019.  
  2020. /*  C O N C B --  Put console in cbreak mode.  */
  2021.  
  2022. /*  Returns 0 if ok, -1 if not  */
  2023.  
  2024. concb(esc) char esc; {
  2025.     int x;
  2026.     if (!isatty(0)) return(0);          /* only for real ttys */
  2027.     if (cgmf == 0) congm();             /* Get modes if necessary. */
  2028.     escchr = esc;                       /* Make this available to other fns */
  2029.     ckxech = 1;                         /* Program can echo characters */
  2030. #ifdef aegis
  2031.     conbufn = 0;
  2032.     if (concrp) return(write(1, "\035\002", 2));
  2033.     if (conuid == input_pad_$uid) {pad_$raw(ios_$stdin, st); return(0);}
  2034. #endif
  2035. #ifndef UXIII
  2036.     cccbrk.sg_flags |= CBREAK;          /* Set to character wakeup, */
  2037.     cccbrk.sg_flags &= ~ECHO;           /* no echo. */
  2038.     x = stty(0,&cccbrk);
  2039. #else
  2040.     cccbrk.c_lflag &= ~(ICANON|ECHO);
  2041.     cccbrk.c_cc[0] = 003;               /* interrupt char is control-c */
  2042.     cccbrk.c_cc[1] = escchr;            /* escape during packet modes */
  2043.     cccbrk.c_cc[4] = 1;
  2044. #ifdef ZILOG
  2045.     cccbrk.c_cc[5] = 0;
  2046. #else
  2047.     cccbrk.c_cc[5] = 1;
  2048. #endif /* zilog */
  2049.     x = ioctl(0,TCSETAW,&cccbrk);       /* set new modes . */
  2050. #endif
  2051.  
  2052. #ifndef aegis
  2053.     if (x > -1) setbuf(stdout,NULL);    /* Make console unbuffered. */
  2054. #endif
  2055. #ifdef  V7
  2056.     if (kmem[CON] < 0) {
  2057.         qaddr[CON] = initrawq(0);
  2058.         if((kmem[CON] = open("/dev/kmem", 0)) < 0) {
  2059.             fprintf(stderr, "Can't read /dev/kmem in concb.\n");
  2060.             perror("/dev/kmem");
  2061.             exit(1);
  2062.         }
  2063.     }
  2064. #endif
  2065.     return(x);
  2066. }
  2067.  
  2068. /*  C O N B I N  --  Put console in binary mode  */
  2069.  
  2070. /*  Returns 0 if ok, -1 if not  */
  2071.  
  2072. conbin(esc) char esc; {
  2073.     if (!isatty(0)) return(0);          /* only for real ttys */
  2074.     if (cgmf == 0) congm();             /* Get modes if necessary. */
  2075.     escchr = esc;                       /* Make this available to other fns */
  2076.     ckxech = 1;                         /* Program can echo characters */
  2077. #ifdef aegis
  2078.     conbufn = 0; if (concrp) return(write(1, "\035\002", 2));
  2079.     if (conuid == input_pad_$uid) {pad_$raw(ios_$stdin, st); return(0);}
  2080. #endif
  2081. #ifndef UXIII
  2082.     ccraw.sg_flags |= (RAW|TANDEM);     /* Set rawmode, XON/XOFF */
  2083.     ccraw.sg_flags &= ~(ECHO|CRMOD);    /* Set char wakeup, no echo */
  2084.     return(stty(0,&ccraw));
  2085. #else
  2086.     ccraw.c_lflag &= ~(ISIG|ICANON|ECHO);
  2087.     ccraw.c_iflag |= (BRKINT|IGNPAR);
  2088.     ccraw.c_iflag &= ~(IGNBRK|INLCR|IGNCR|ICRNL|IUCLC|IXON|IXANY|IXOFF
  2089.                         |INPCK|ISTRIP);
  2090.     ccraw.c_oflag &= ~OPOST;
  2091. #ifdef ATT7300
  2092.     ccraw.c_cflag = CLOCAL | B9600 | CS8 | CREAD | HUPCL; /* c_cflag */
  2093. #endif
  2094. /*** Kermit used to put the console in 8-bit raw mode, but some users have
  2095.  *** pointed out that this should not be done, since some sites actually
  2096.  *** use terminals with parity settings on their Unix systems, and if we
  2097.  *** override the current settings and stop doing parity, then their terminals
  2098.  *** will display blotches for characters whose parity is wrong.  Therefore,
  2099.  *** the following two lines are commented out (Larry Afrin, Clemson U):
  2100.  ***
  2101.  ***   ccraw.c_cflag &= ~(PARENB|CSIZE);
  2102.  ***   ccraw.c_cflag |= (CS8|CREAD);
  2103.  ***
  2104.  *** Sys III/V sites that have trouble with this can restore these lines.
  2105.  ***/
  2106.     ccraw.c_cc[4] = 1;
  2107.     ccraw.c_cc[5] = 1;
  2108.     return(ioctl(0,TCSETAW,&ccraw) );   /* set new modes . */
  2109. #endif
  2110. }
  2111.  
  2112.  
  2113. /*  C O N R E S  --  Restore the console terminal  */
  2114.  
  2115. conres() {
  2116.     if (cgmf == 0) return(0);           /* Don't do anything if modes */
  2117.     if (!isatty(0)) return(0);          /* only for real ttys */
  2118. #ifndef UXIII                           /* except for sIII, */
  2119.     sleep(1);                           /*  not known! */
  2120. #endif                                  /*   (sIII does wait in ioctls) */
  2121.     ckxech = 0;                         /* System should echo chars */
  2122. #ifdef aegis
  2123.     conbufn = 0; if (concrp) return(write(1, "\035\001", 2));
  2124.     if (conuid == input_pad_$uid) {pad_$cooked(ios_$stdin, st); return(0);}
  2125. #endif
  2126. #ifndef UXIII
  2127.     return(stty(0,&ccold));             /* Restore controlling tty */
  2128. #else
  2129.     return(ioctl(0,TCSETAW,&ccold));
  2130. #endif
  2131. }
  2132.  
  2133. /*  C O N O C  --  Output a character to the console terminal  */
  2134.  
  2135. conoc(c) char c; {
  2136.     write(1,&c,1);
  2137. }
  2138.  
  2139. /*  C O N X O  --  Write x characters to the console terminal  */
  2140.  
  2141. conxo(x,s) char *s; int x; {
  2142.     write(1,s,x);
  2143. }
  2144.  
  2145. /*  C O N O L  --  Write a line to the console terminal  */
  2146.  
  2147. conol(s) char *s; {
  2148.     int len;
  2149.     len = strlen(s);
  2150.     write(1,s,len);
  2151. }
  2152.  
  2153. /*  C O N O L A  --  Write an array of lines to the console terminal */
  2154.  
  2155. conola(s) char *s[]; {
  2156.     int i;
  2157.     for (i=0 ; *s[i] ; i++) conol(s[i]);
  2158. }
  2159.  
  2160. /*  C O N O L L  --  Output a string followed by CRLF  */
  2161.  
  2162. conoll(s) char *s; {
  2163.     conol(s);
  2164.     write(1,"\r\n",2);
  2165. }
  2166.  
  2167. /*  C O N C H K  --  Return how many characters available at console  */
  2168.  
  2169. conchk() {
  2170.     int x; long n;
  2171.  
  2172. #ifdef PROVX1
  2173.     x = ioctl(0, TIOCQCNT, &ttbuf);
  2174.     n = ttbuf.sg_ispeed & 0377;
  2175.     return((x < 0) ? 0 : n);
  2176. #else
  2177. #ifdef aegis
  2178.     if (conbufn > 0) return(conbufn);   /* use old count if nonzero */
  2179.  
  2180.     /* read in more characters */
  2181.     conbufn = ios_$get(ios_$stdin,
  2182.               ios_$cond_opt, conbuf, (long)sizeof(conbuf), st);
  2183.     if (st.all != status_$ok) conbufn = 0;
  2184.     conbufp = conbuf;
  2185.     return(conbufn);
  2186. #else
  2187. #ifdef V7
  2188.     lseek(kmem[CON], (long) qaddr[CON], 0);
  2189.     x = read(kmem[CON], &n, sizeof(int));
  2190.     return((x == sizeof(int))? n: 0);
  2191. #else
  2192. #ifdef UXIII
  2193.     if (conesc) {                       /* Escape typed */
  2194.         conesc = 0;
  2195.         signal(SIGQUIT,esctrp);         /* Restore escape */
  2196.         return(1);
  2197.     }
  2198.     return(0);
  2199. #else
  2200. #ifdef C70
  2201.     if (conesc) {                       /* Escape typed */
  2202.         conesc = 0;
  2203.         signal(SIGQUIT,esctrp);         /* Restore escape */
  2204.         return(1);
  2205.     }
  2206.     return(0);
  2207. #else
  2208. #ifdef FIONREAD
  2209.     x = ioctl(0, FIONREAD, &n);         /* BSD and maybe some others */
  2210.     return((x < 0) ? 0 : n);
  2211. #else
  2212.     return(0);                          /* Others can't do. */
  2213. #endif
  2214. #endif
  2215. #endif
  2216. #endif
  2217. #endif
  2218. #endif
  2219. }
  2220.  
  2221. /*  C O N I N C  --  Get a character from the console  */
  2222.  
  2223. coninc(timo) int timo; {
  2224.     int n = 0; char ch;
  2225. #ifdef aegis
  2226.     fflush(stdout);
  2227.     if (conchk() > 0)
  2228.     {  --conbufn; return(*conbufp++ & 0377); }
  2229. #endif
  2230.     if (timo <= 0 ) {                   /* untimed */
  2231.         n = read(0, &ch, 1);            /* Read a character. */
  2232.         ch &= 0377;
  2233.         if (n > 0) return(ch);          /* Return the char if read */
  2234.         else
  2235. #ifdef UXIII
  2236. #ifndef CIE                             /* CIE Regulus has no such symbol */
  2237.             if (n < 0 && errno == EINTR) /* if read was interrupted by QUIT */
  2238.                 return(escchr);          /* user entered escape character */
  2239.             else                    /* couldnt be ^c, sigint never returns */
  2240. #endif
  2241. #endif
  2242.                 return(-1);             /* Return the char, or -1. */
  2243.         }
  2244.     signal(SIGALRM,timerh);             /* Timed read, so set up timer */
  2245.     alarm(timo);
  2246.     if (setjmp(sjbuf)) n = -2;
  2247.     else {
  2248.         n = read(0, &ch, 1);
  2249.         ch &= 0377;
  2250.     }
  2251.     alarm(0);                           /* Stop timing, we got our character */
  2252.     signal(SIGALRM,SIG_DFL);
  2253.     if (n > 0) return(ch);
  2254.     else
  2255. #ifdef UXIII
  2256. #ifndef CIE                             /* CIE Regulus has no such symbol */
  2257.         if (n == -1 && errno == EINTR)  /* If read interrupted by QUIT, */
  2258.             return(escchr);             /* user entered escape character, */
  2259.         else                            /* can't be ^c, sigint never returns */
  2260. #endif
  2261. #endif
  2262.         return(-1);
  2263. }
  2264.  
  2265. #ifdef ATT7300
  2266.  
  2267. /*  A T T D I A L  --  Dial up the remote system using internal modem
  2268.  * Purpose: to open and dial a number on the internal modem available on the
  2269.  * ATT7300 UNIX PC.  Written by Joe Doupnik. Superceeds version written by
  2270.  * Richard E. Hill, Dickinson, TX. which employed dial(3c).
  2271.  * Uses information in <sys/phone.h> and our status int attmodem.
  2272.  */
  2273. struct updata dialer = {0};             /* Condition Dialer for Data call */
  2274.  
  2275. attdial(ttname,speed,telnbr) char *ttname,*telnbr; int speed;
  2276. {
  2277.     char *telnum;
  2278.     int ttclos();
  2279.  
  2280.     attmodem &= ~ISMODEM;                       /* modem not in use yet */
  2281.                     /* Ensure O_NDELAY is set, else i/o traffic hangs */
  2282.     fcntl(ttyfd, F_SETFL, fcntl(ttyfd, F_GETFL, 0) | O_NDELAY);
  2283.  
  2284.     /* Condition line, check availability & DATA mode, turn on speaker */
  2285.     if (ioctl(ttyfd,PIOCOFFHOOK, &dialer) == -1)
  2286.         {
  2287.         printf("cannot access phone\n");
  2288.         ttclos();
  2289.         return (-2);
  2290.         }
  2291.     ioctl(ttyfd,PIOCGETP,&dialer);      /* get phone dialer parameters */
  2292.  
  2293.     if (dialer.c_lineparam & VOICE)     /* phone must be in DATA mode */
  2294.         {
  2295.         printf(" Should not dial with modem in VOICE mode.\n");
  2296.         printf(" Exit Kermit, switch to DATA and retry call.\n");
  2297.         ttclos();
  2298.         return (-2);
  2299.         }
  2300.                                         /* dialer parameters to be set */
  2301.     dialer.c_lineparam = DATA | DTMF;                   /* dial with tones */
  2302.     dialer.c_waitdialtone = 5;                  /* wait 5 sec for dialtone */
  2303.     dialer.c_feedback = SPEAKERON|NORMSPK|RINGON;  /* control speaker */
  2304.     dialer.c_waitflash = 500;                   /* 0.5 sec flash hook */
  2305.     if(ioctl(ttyfd,PIOCSETP,&dialer) == -1)     /* set phone parameters */
  2306.         {
  2307.         printf("Cannot set modem characteristics\n");
  2308.         ttclos();
  2309.         return (-2);
  2310.         }
  2311.  
  2312. /*  fprintf(stderr,"Phone line status. line_par:%o dialtone_wait:%o \
  2313. line_status:%o feedback:%o\n",
  2314.     dialer.c_lineparam, dialer.c_waitdialtone,
  2315.     dialer.c_linestatus, dialer.c_feedback);
  2316. */
  2317.  
  2318.     attmodem |= ISMODEM;                        /* modem is now in-use */
  2319.     sleep(1);
  2320.     for (telnum = telnbr; *telnum != '\0'; telnum++)    /* dial number */
  2321.         if (ioctl(ttyfd,PIOCDIAL,telnum) != 0)
  2322.                 {
  2323.                 perror("Error in dialing");
  2324.                 ttclos();
  2325.                 return(-2);
  2326.                 }
  2327.     ioctl(ttyfd,PIOCDIAL,"@");                  /* terminator for data call */
  2328.  
  2329.     do  {                                     /* wait for modems to Connect */
  2330.         if (ioctl(ttyfd,PIOCGETP,&dialer) != 0)         /* get params */
  2331.                 {
  2332.                 perror("Cannot get modems to connect");
  2333.                 ttclos();
  2334.                 return(-2);
  2335.                 }
  2336.         } while ((dialer.c_linestatus & MODEMCONNECTED) == 0);
  2337.     signal(SIGHUP, ttclos);             /* hangup on loss of carrier */
  2338.     return(0);                          /* return success */
  2339. }
  2340.  
  2341. /*  A T T H A N G  --  Hangup internal modem of ATT7300.
  2342.  *  attmodem has getty status and modem-in-use bit.
  2343.  */
  2344. atthang(ttname) char *ttname;
  2345. {
  2346.     int myttyfd;                         /* temp tty comms file descriptor */
  2347.  
  2348.     if (attmodem & ISMODEM)         /* if ATT7300 internal modem is in use */
  2349.         if (ttyfd > 0)  /* if ttyfd file still active, disconnect */
  2350.                 {                       /* and put driver in stable state */
  2351.                 myttyfd = open(ttname, O_RDWR | O_NDELAY); /* helps driver */
  2352.                 if (myttyfd < 0)
  2353.                         return (-1);                    /* Oops! */
  2354.                 ioctl(myttyfd,PIOCUNHOLD,&dialer);/* return call to handset */
  2355.                 ioctl(myttyfd,PIOCDISC,&dialer);  /* disconnect phone */
  2356.                 close(myttyfd);
  2357.                 }
  2358.     attmodem &= ~ISMODEM;                       /* phone not in use */
  2359.     return (0);                                 /* ttyfd is unchanged */
  2360. }
  2361.  
  2362. /*      Offgetty, ongetty functions. These function get the 'getty(1m)' off
  2363.         and restore it to the indicated line.  Shell's return codes are:
  2364.         0: Can't do it.  Probably a user logged on.
  2365.         1: No need.  No getty on that line.
  2366.         2: Done, you should restore the getty when you're done. DOGETY
  2367.         System(3), however, returns them as 0, 256, 512, respectively.
  2368.         Thanks to Kevin O'Gorman, Anarm Software Systems.
  2369.  
  2370.         getoff.sh looks like            geton.sh looks like
  2371.                 setgetty $1 0           setgetty $1 1
  2372.                 err=$?                  exit $?
  2373.                 sleep 2
  2374.                 exit $err
  2375. */
  2376.  
  2377. /*  O F F G E T T Y  --  Turn off getty(1m) for the communications tty line
  2378.  * and get status so it can be restarted after the line is hung up.
  2379.  */
  2380. offgetty(ttname) char *ttname;
  2381. {
  2382.         char temp[30];
  2383.         while (*ttname != '\0') ttname++;       /* seek terminator of path */
  2384.         ttname -= 3;                            /* get last 3 chars of name */
  2385.         sprintf(temp,"/usr/bin/getoff.sh %s",ttname);
  2386.         return (system(temp));
  2387. }
  2388.  
  2389. /*  O N G E T T Y  --  Turn on getty(1m) for the communications tty line */
  2390. ongetty(ttname) char *ttname;
  2391. {
  2392.         char temp[30];
  2393.         while (*ttname != '\0') ttname++;       /* comms tty path name */
  2394.         ttname -= 3;
  2395.         sprintf(temp,"/usr/bin/geton.sh %s",ttname);
  2396.         return (system(temp));
  2397. }
  2398. #endif /* ATT7300 */
  2399.