home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / o / os9.zip / CK9TIO.C < prev    next >
C/C++ Source or Header  |  1992-11-23  |  49KB  |  1,468 lines

  1. char *ckxv = "OS-9 tty I/O, 5A(08) 12 Oct 92";
  2.  
  3. /*  C K 9 T I O  */
  4.  
  5. /* C-Kermit interrupt, terminal control & i/o functions for os9/68k systems */
  6.  
  7. /*
  8.  Author: Peter Scholz
  9.  Ruhr University Bochum, Department of analytical chemistry
  10.  Federal Republic of Germany, February 1987
  11.  
  12.  Bob Larson (Blarson@ecla.usc.edu)
  13.  Cleanup, fix timeouts, fix connect escape
  14.  Rewrite ttinl for less overhead
  15.  
  16.  Edition: 5A(01)
  17.  Chris Hemsing (Chris@lfm.rwth-aachen.de)
  18.  Cleaned up timed i/o using F$Alarm
  19.  Tmode/Xmode stuff
  20.  Send break stuff
  21.  All time related stuff new
  22.  
  23.  07/25/91 Chris  Hemsing        minor bug fixes, changes for gnu (ansi) C
  24.  
  25.  12/02/91 Bob Larson    Blarson@usc.edu
  26.         alarm() code was a nice idea, but doesn't work.  Fixed back to
  27.         code that will interupt i/o in progress. Minor cleanup.
  28.         Retrofited current unix myread() code to speed things back up.
  29.  
  30.  01/14/92 Chris  Hemsing        minor xmode bug fixes, sigmask <->longjump
  31.                                 OS-9 does interrupt i/o, but bad drivers don't
  32.                                 -> alarm code not reintroduced.
  33.  Edition: 5A(05)
  34.           Bob Larson            conchk bug fix
  35.  Edition: 5A(06)
  36.  03/04/92 Chris  Hemsing        ttopen: dup(0) fix, compare upper case
  37.  Edition: 5A(06)
  38.  09/28/92 Chris  Hemsing        cccbrk bug fix
  39.  Edition: 5A(06)
  40.  10/12/92 Chris  Hemsing        improved CTRL-E handling (catch)
  41.  
  42.   adapted from unix C-Kermit
  43.   Author: Frank da Cruz (SY.FDC@CU20B),
  44.   Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  45.   York.  Permission is granted to any individual or institution to use this
  46.   software as long as it is not sold for profit.  This copyright notice must be
  47.   retained.  This software may not be included in commercial products without
  48.   written permission of Columbia University.
  49. */
  50.  
  51.  
  52. #include <dir.h>   /* Directory */
  53. #ifdef NULL
  54. #undef NULL
  55. #endif NULL
  56. #ifndef GREGORIAN
  57. #define GREGORIAN 0
  58. #define JULIAN 1
  59. #endif
  60. #include <signal.h>     /* Interrupts */
  61. #include <errno.h>
  62. #include <sgstat.h>     /* Set/Get tty modes */
  63. #include <sg_codes.h>   /* setstat/getstat function codes */
  64. #include <setsys.h>
  65. #include <setjmp.h>
  66. #include <strings.h>
  67. #include <time.h>
  68. #include <modes.h>      /* osk i/o modes S_IREAD, etc */
  69. #include "ckcdeb.h"     /* Typedefs, formats for debug() */
  70.  
  71. /* Maximum length for the name of a tty device */
  72.  
  73. #ifndef DEVNAMLEN
  74. #define DEVNAMLEN MAXNAMLEN*4+2 /* leave room for /     and     terminating
  75.  \0 */
  76. #endif                          /* not really an OS-9 restriction */
  77.                                 /* *4 is arbitrary */
  78.                                 /* nfm getstat call bug: SS_DevNm always
  79.                                    copies all 32 bytes, nfm does the first
  80.                                    part of the name => sum is more than
  81.                                    32 bytes
  82.                                    e.g "term": all fine
  83.                                    because 32 bytes incl. "term" are copied
  84.                                    "n0/node1/term":  "n0/node1/" done by nfm
  85.                                    "term" is done by remote's scf
  86.                                    (32 bytes) */
  87. char *ckxsys = " OS-9/68000";
  88.  
  89. #define MYREAD                  /* use improved input code */
  90.                                 /* Warning: untested without this defiend */
  91.  
  92. /*
  93.  Variables available to outside world:
  94.  
  95.    dftty  -- Pointer to default tty name string, like "/dev/tty".
  96.    dfloc  -- 0 if dftty is console, 1 if external line.
  97.    dfprty -- Default parity
  98.    dfflow -- Default flow control
  99.    ckxech -- Flag for who echoes console typein:
  100.      1 - The program (system echo is turned off)
  101.      0 - The system (or front end, or terminal).
  102.    functions that want to do their own echoing should check this flag
  103.    before doing so.
  104.  
  105.  Functions for assigned communication line (either external or console tty):
  106.  
  107.    syscleanup()            -- System dependent shutdown
  108.    sysinit()               -- System dependent program initialization
  109.    ttopen(ttname,local,mdmtyp,timo) -- Open the named tty for exclusive access.
  110.    ttclos()                -- Close & reset the tty.
  111.    ttpkt(speed,flow)       -- Put the tty in packet mode and set the speed.
  112.    ttvt(speed,flow)        -- Put the tty in virtual terminal mode.
  113.     or in DIALING or CONNECTED modem control state.
  114.    ttinl(dest,max,timo)    -- Timed read line from the tty.
  115.    ttinc(timo)             -- Timed read character from tty.
  116.    myread()                -- Raw mode bulk buffer read, gives subsequent
  117.                                 chars one at a time and simulates FIONREAD.
  118.    myunrd(c)               -- Places c back in buffer to be read (one only)
  119.    ttchk()                 -- See how many characters in tty input buffer.
  120.    ttxin(n,buf)            -- Read n characters from tty (untimed).
  121.    ttol(string,length)     -- Write a string to the tty.
  122.    ttoc(c)                 -- Write a character to the tty.
  123.    ttflui()                -- Flush tty input buffer.
  124.  */
  125.  
  126. /*
  127. Functions for console terminal:
  128.  
  129.    congm()   -- Get console terminal modes.
  130.    concb(esc) -- Put the console in single-character wakeup mode with no echo.
  131.    conbin(esc) -- Put the console in binary (raw) mode.
  132.    conres()  -- Restore the console to mode obtained by congm().
  133.    conoc(c)  -- Unbuffered output, one character to console.
  134.    conol(s)  -- Unbuffered output, null-terminated string to the console.
  135.    conola(s) -- Unbuffered output, array of strings to the console.
  136.    conxo(n,s) -- Unbuffered output, n characters to the console.
  137.    conchk()  -- Check if characters available at console (bsd 4.2).
  138.   Check if escape char (^\) typed at console (System III/V).
  139.    coninc(timo)  -- Timed get a character from the console.
  140.    conint()  -- Enable terminal interrupts on the console if not background.
  141.    connoi()  -- Disable terminal interrupts on the console if not background.
  142.  
  143. Time functions
  144.  
  145.    msleep(m) -- Millisecond sleep
  146.    ztime(s) -- Return date/time string
  147.    rtimer() --  Reset timer
  148.    gtimer()  -- Get elapsed time since last call to rtimer()
  149. */
  150.  
  151. /* Declarations */
  152.  
  153. extern int errno;   /* System call error return */
  154.  
  155. /* dftty is the device name of the default device for file transfer */
  156. /* dfloc is 0 if dftty is the user's console terminal, 1 if an external line */
  157.  
  158.     char *dftty;        /* Remote by default, use normal */
  159.     int dfloc = 0;      /* controlling terminal name. */
  160.  
  161.     int dfprty = 0;     /* Parity (0 = none) */
  162.     int ttprty = 0;     /* current parity */
  163.     static int ttpmsk = 0377;           /* Parity stripping mask. */
  164.     int dfflow = 1;     /* Xon/Xoff flow control */
  165.     int backgrd = 0;    /* Assume in foreground (no '&' ) */
  166.  
  167.     int ckxech = 0;     /* 0 if system normally echoes console characters, else
  168.  1 */
  169.  
  170.     int ttypn = -1;    /* TTY path number, also used in ck9con.c */
  171.  
  172.     int ttnproto = 0;                   /* Network protocol, 0 = none. */
  173.     int ttcarr = CAR_AUT;               /* Carrier handling mode. */
  174.  
  175. char myttystr[DEVNAMLEN];
  176.  
  177. /* Declarations of variables global within this module */
  178.  
  179. static char ttnmsv[DEVNAMLEN];   /* always name of ttypn device */
  180. static char dfttystr[DEVNAMLEN];
  181. static int
  182.     xlocal = 0;    /* Flag for tty local or remote */
  183.  
  184. static struct sgbuf      /* sgtty info... */
  185.   ttold, ttraw, tttvt,   /* for communication line */
  186.   ccold, ccraw, cccbrk;  /* and for console */
  187.  
  188.  
  189. static time_t   tcount;   /* Elapsed time counter */
  190. static time_t   timowhen; /* timeout time */
  191.  
  192. static SIGTYP (*saval)() = NULL;                /* For saving alarm handler */
  193. static jmp_buf sjbuf;                   /* Longjump buffers */
  194. static SIGTYP timerh();
  195.  
  196. #define B50     0
  197. #define B75     1
  198. #define B110    2
  199. #define B134    3
  200. #define B150    4
  201. #define B300    5
  202. #define B600    6
  203. #define B1200   7
  204. #define B1800   8
  205. #define B2000   9
  206. #define B2400  10
  207. #define B3600  11
  208. #define B4800  12
  209. #define B7200  13
  210. #define B9600  14
  211. #define B19200 15
  212. #define B38400 16
  213.  
  214. static unsigned short bdrts[] = {
  215.         50, 75, 110, 134, 150, 300, 600, 1200,
  216.         1800, 2000, 2400, 3600, 4800, 7200, 9600, 19200, 38400
  217. };
  218.  
  219. /* for unix signal emulation */
  220.  
  221. #define MAXSIG 4
  222. typedef VOID (*fptr)();
  223. fptr sigtbl[MAXSIG+1];
  224. fptr signal();
  225.  
  226. syscleanup() {
  227.     return 0;                   /* none needed */
  228. }
  229.  
  230. /*  S Y S I N I T  --  System-dependent program initialization.  */
  231.  
  232. sysinit() {
  233. /* initialize unix signal emulation */
  234.     register int i;
  235.     register char *cp;
  236.     VOID catch();
  237.     char *getenv();
  238.  
  239.     for(i=0;i<=MAXSIG;i++)
  240.         sigtbl[i] = SIG_DFL;
  241.     intercept(catch);
  242.     if(isatty(0)) {             /* must disable buffering before io */
  243.         setbuf(stdin,(char *)NULL);
  244.         setbuf(stdout,(char *)NULL);
  245.     }
  246.     /* get terminal name from enviornment variable PORT,
  247.      * use device of stderr as a backup
  248.      */
  249.     myttystr[0] = '/';
  250.     if((cp = getenv("PORT")) == NULL) {
  251.         if(_gs_devn(2, myttystr+1) < 0) return -1;
  252.     } else strcpy(myttystr, cp);
  253.     strcpy(dfttystr, myttystr);
  254.     dftty = &dfttystr[0];
  255.     backgrd = (isatty(0) && isatty(1)) ? 0 : 1;
  256.     congm();
  257.     return 0;
  258. }
  259.  
  260. /*  T T O P E N  --  Open a tty for exclusive access.  */
  261.  
  262. /*
  263.   Call with:
  264.     ttname: character string - device name or network host name.
  265.     lcl:
  266.   If called with lcl < 0, sets value of lcl as follows:
  267.   0: the terminal named by ttname is the job's controlling terminal.
  268.   1: the terminal named by ttname is not the job's controlling terminal.
  269.   But watch out: if a line is already open, or if requested line can't
  270.   be opened, then lcl remains (and is returned as) -1.
  271.     modem:
  272.   Less than zero: ttname is a network host name.
  273.   Zero or greater: ttname is a terminal device name.
  274.   Zero means a local connection (don't use modem signals).
  275.   Positive means use modem signals.
  276.    timo:
  277.   0 = no timer.
  278.   nonzero = number of seconds to wait for open() to return before timing out.
  279.  
  280.   Returns:
  281.     0 on success
  282.    -5 if device is in use
  283.    -4 if access to device is denied
  284.    -3 if access to lock directory denied
  285.    -2 upon timeout waiting for device to open
  286.    -1 on other error
  287. */
  288.  
  289. int
  290. ttopen(ttname,lcl,modem,timo) char *ttname; int *lcl, modem, timo; {
  291.  
  292. char dev_buffer[DEVNAMLEN];
  293.     debug(F111,"ttopen entry modem",ttname,modem);
  294.     debug(F101," ttypn","",ttypn);
  295.     debug(F101," lcl","",*lcl);
  296.  
  297.     if (ttypn > -1)             /* if device already opened */
  298.     {
  299.         if (ustrncmp(ttname,ttnmsv,DEVNAMLEN))/* are new & old names equal? */
  300.                                               /* compare ignoring case */
  301.           ttclos(0);                    /* no, close old ttname, open new */
  302.         else                    /* else same, ignore this call, */
  303.           return(0);            /* and return. */
  304.     }
  305.     if (timo < 0) timo = 0;             /* Safety */
  306.     if (setjmp(sjbuf))              /* Timer went off? */
  307.     {
  308.         debug(F100,"ttopen timout","",0); /*    Get here on timeout. */
  309.         return(-2);                     /* and return error code. */
  310.     }
  311.     else
  312.     {
  313.         if (timo)
  314.         {                           /* Don't time out if timo == 0 */
  315.             /* This won't work, since alarm() will not interupt i/o. */
  316.             /* Unfortunatatly, there is no non-blocking open to replace */
  317.             /* it with.   blarson@usc.edu */
  318.             /* Alarm will interrupt i/o when using a proper driver that */
  319.             /* checks for pending signals smaller than deadly */
  320.             /* chris@lfm.rwth-aachen.de */
  321.             saval = signal(SIGALRM,timerh); /* Enable timer interrupt */
  322.             alarm((unsigned int)timo);                      /* Set it. */
  323.         }
  324.         strncpy(ttnmsv,ttname,DEVNAMLEN); /* keep ttnmsv corresp. to ttypn */
  325.         dev_buffer[0] = '/';
  326.         _gs_devn(0,&dev_buffer[1]);         /* get name of stdin */
  327.         if (*ttname=='\0' || ustrncmp(ttname,dev_buffer,DEVNAMLEN)==0)
  328.           ttypn = open(ttname,S_IREAD|S_IWRITE);
  329.           /* Try open for read/write; we cannot open own stdin non-share*/
  330.           /* Don't simply dup(0) or dup(1); they might only be open
  331.              for mere read (0) or mere write (1) */
  332.         else
  333.           ttypn = open(ttname,S_IREAD|S_IWRITE|S_ISHARE); /* Try to open for
  334.           read/write nonshare*/
  335.         if (timo)
  336.             ttimoff();                  /* Turn off timer */
  337.         if (ttypn < 0)      /* If couldn't open, fail. */
  338.         {
  339.             perror(ttname);
  340.             if ((errno == E_DEVBSY) ||
  341.                 (errno == E_SHARE )) return(-5);
  342.             return(-1);
  343.         }
  344.     }
  345.     debug(F111,"ttopen ok",ttname,*lcl);
  346.  
  347.  
  348. /* Caller wants us to figure out if line is controlling tty */
  349.  
  350.     if (*lcl == -1)
  351.     {
  352.         if (isatty(0))
  353.         {               /* if stdin not redirected */
  354.             dev_buffer[0] = '/';
  355.             _gs_devn(0,&dev_buffer[1]); /* ...with real name of stdin   */
  356.             xlocal = (ustrncmp(ttname,dev_buffer,DEVNAMLEN) != 0);
  357.             debug(F111," ttyname",dev_buffer,xlocal);
  358.         }
  359.         else   /* Else, if stdin redirected... */
  360.         {
  361.         /* Just assume local, so "set speed" and similar commands will work */
  362.         /* If not really local, how could it work anyway?... */
  363.             xlocal = 1;
  364.             debug(F101," redirected stdin","",xlocal);
  365.         }
  366.     }
  367.  
  368. /* line locking not needed -- os9 has exclusive access flag on open */
  369.  
  370. /* Got the line, now set the desired value for local. */
  371.  
  372.     if (*lcl < 0) *lcl = xlocal;
  373.  
  374. /* Get tty device settings */
  375.  
  376.     _gs_opt(ttypn,&ttold);   /* Get sgtty info */
  377.     _gs_opt(ttypn,&ttraw);   /* And a copy of it for packets*/
  378.     _gs_opt(ttypn,&tttvt);   /* And one for virtual tty service */
  379.     debug(F101,"ttopen, ttypn","",ttypn);
  380.     debug(F101," lcl","",*lcl);
  381.     return(0);
  382. }
  383.  
  384. /*  T T C L O S  --  Close the TTY */
  385.  
  386. ttclos(foo) int foo; {
  387.     if (ttypn < 0) return(0);  /* Wasn't open. */
  388.     if (xlocal) {
  389.         if (tthang())   /* Hang up phone line */
  390.             fprintf(stderr,"Warning, problem hanging up the phone\n");
  391.     }
  392.     ttres();    /* Reset modes. */
  393. /* Relinquish exclusive access if we might have had it... */
  394.     close(ttypn);   /* Close it. */
  395.     debug (F101,"closed connection, ttypn","",ttypn);
  396.     ttypn = -1;    /* Mark it as closed. */
  397.     return(0);
  398. }
  399.  
  400. #ifdef MYREAD
  401.  
  402. /* Private buffer for myread() and its companions.  Not for use by anything
  403.  * else.  ttflui() is allowed to reset them to initial values.  ttchk() is
  404.  * allowed to read my_count.
  405.  *
  406.  * my_item is an index into mybuf[].  Increment it *before* reading mybuf[].
  407.  *
  408.  * A global parity mask variable could be useful too.  We could use it to
  409.  * let myread() strip the parity on its own, instead of stripping sign
  410.  * bits as it does now.
  411.  */
  412.  
  413. #define MYBUFLEN 256
  414. static CHAR mybuf[MYBUFLEN];            /* Buffer, including push back */
  415. static int my_count = 0;                /* Number of chars still in mybuf */
  416. static int my_item = -1;                /* Last index read from mybuf[] */
  417.  
  418. /* myread() -- Efficient read of one character from communications line.
  419.  *
  420.  * Uses a private buffer to minimize the number of expensive read() system
  421.  * calls.  Essentially performs the equivalent of read() of 1 character, which
  422.  * is then returned.  By reading all available input from the system buffers
  423.  * to the private buffer in one chunk, and then working from this buffer, the
  424.  * number of system calls is reduced in any case where more than one character
  425.  * arrives during the processing of the previous chunk, for instance high
  426.  * baud rates or network type connections where input arrives in packets.
  427.  * If the time needed for a read() system call approaches the time for more
  428.  * than one character to arrive, then this mechanism automatically compensates
  429.  * for that by performing bigger read()s less frequently.  If the system load
  430.  * is high, the same mechanism compensates for that too.
  431.  *
  432.  * myread() is a macro that returns the next character from the buffer.  If the
  433.  * buffer is empty, mygetbuf() is called.  See mygetbuf() for possible error
  434.  * returns.
  435.  *
  436.  * This should be efficient enough for any one-character-at-a-time loops.
  437.  * For even better efficiency you might use memcpy()/bcopy() or such between
  438.  * buffers (since they are often better optimized for copying), but it may not
  439.  * be worth it if you have to take an extra pass over the buffer to strip
  440.  * parity and check for CTRL-C anyway.
  441.  *
  442.  * Note that if you have been using myread() from another program module, you
  443.  * may have some trouble accessing this macro version and the private variables
  444.  * it uses.  In that case, just add a function in this module, that invokes the
  445.  * macro.
  446.  */
  447. #define myread()  (--my_count < 0 ? mygetbuf() : 255 & (int)mybuf[++my_item])
  448.  
  449. /* Specification: Push back up to one character onto myread()'s queue.
  450.  *
  451.  * This implementation: Push back characters into mybuf. At least one character
  452.  * must have been read through myread() before myunrd() may be used.  After
  453.  * EOF or read error, again, myunrd() can not be used.  Sometimes more than
  454.  * one character can be pushed back, but only one character is guaranteed.
  455.  * Since a previous myread() must have read its character out of mybuf[],
  456.  * that guarantees that there is space for at least one character.  If push
  457.  * back was really needed after EOF, a small addition could provide that.
  458.  *
  459.  * myunrd() is currently not called from anywhere inside kermit...
  460.  */
  461. #ifdef NOTUSED
  462. myunrd(ch) CHAR ch; {
  463.     if (my_item >= 0) {
  464.         mybuf[my_item--] = ch;
  465.         ++my_count;
  466.     }
  467. }
  468. #endif
  469.  
  470. /* mygetbuf() -- Fill buffer for myread() and return first character.
  471.  *
  472.  * This function is what myread() uses when it can't get the next character
  473.  * directly from its buffer.  First, it calls a system dependent myfillbuf()
  474.  * to read at least one new character into the buffer, and then it returns
  475.  * the first character just as myread() would have done.  This function also
  476.  * is responsible for all error conditions that myread() can indicate.
  477.  *
  478.  * Returns: When OK     => a positive character, 0 or greater.
  479.  *          When EOF    => -2.
  480.  *          When error  => -3, error code in errno.
  481.  *
  482.  * Older myread()s additionally returned -1 to indicate that there was nothing
  483.  * to read, upon which the caller would call myread() again until it got
  484.  * something.  The new myread()/mygetbuf() always gets something.  If it
  485.  * doesn't, then make it do so!  Any program that actually depends on the old
  486.  * behaviour will break.
  487.  *
  488.  * The older version also used to return -2 both for EOF and other errors,
  489.  * and used to set errno to 9999 on EOF.  The errno stuff is gone, EOF and
  490.  * other errors now return different results, although Kermit currently never
  491.  * checks to see which it was.  It just disconnects in both cases.
  492.  *
  493.  * Kermit lets the user use the quit key to perform some special commands
  494.  * during file transfer.  This causes read(), and thus also mygetbuf(), to
  495.  * finish without reading anything and return the EINTR error.  This should
  496.  * be checked by the caller.  Mygetbuf() could retry the read() on EINTR,
  497.  * but if there is nothing to read, this could delay Kermit's reaction to
  498.  * the command, and make Kermit appear unresponsive.
  499.  *
  500.  * The debug() call should be removed for optimum performance.
  501.  */
  502. int
  503. mygetbuf() {
  504.     my_count = myfillbuf();
  505.     /* debug(F101, "myfillbuf read", "", my_count); */
  506.     if (my_count <= 0)
  507.       return(my_count < 0 ? -3 : -2);
  508.     --my_count;
  509.     return(255 & (int)mybuf[my_item = 0]);
  510. }
  511.  
  512. /* myfillbuf():
  513.  * System-dependent read() into mybuf[], as many characters as possible.
  514.  *
  515.  * Returns: OK => number of characters read, always more than zero.
  516.  *          EOF => 0
  517.  *          Error => -1, error code in errno.
  518.  *
  519.  * If there is input available in the system's buffers, all of it should be
  520.  * read into mybuf[] and the function return immediately.  If no input is
  521.  * available, it should wait for a character to arrive, and return with that
  522.  * one in mybuf[] as soon as possible.  It may wait somewhat past the first
  523.  * character, but be aware that any such delay lengthens the packet turnaround
  524.  * time during kermit file transfers.  Should never return with zero characters
  525.  * unless EOF or irrecoverable read error.
  526.  *
  527.  * Correct functioning depends on the correct tty parameters being used.
  528.  * Better control of current parameters is required than may have been the
  529.  * case in older Kermit releases.  For instance, O_NDELAY (or equivalent) can
  530.  * no longer be sometimes off and sometimes on like it used to, unless a
  531.  * special myfillbuf() is written to handle that.  Otherwise the ordinary
  532.  * myfillbuf()s may think they have come to EOF.
  533.  *
  534.  * If your system has a facility to directly perform the functioning of
  535.  * myfillbuf(), then use it.  If the system can tell you how many characters
  536.  * are available in its buffers, then read that amount (but not less than 1).
  537.  * If the system can return a special indication when you try to read without
  538.  * anything to read, while allowing you to read all there is when there is
  539.  * something, you may loop until there is something to read, but probably that
  540.  * is not good for the system load.
  541.  */
  542.  
  543.         /* This is for OSK.  _gs_rdy returns the number
  544.          * of characters available for reading. If none are available, wait
  545.          * until something arrives, otherwise return all there is.
  546.          * The OSK version needs to handle timeouts explicitly since
  547.          * OSK alarm() does not interupt I/O.  (Only done when filling buffer
  548.          * to reduce overhead.)
  549.          */
  550. int
  551. myfillbuf() {
  552.     register int avail;
  553.  
  554.     (void) remove_alarm();      /* let's fake it instead */
  555.     for(;;) {
  556.         if (timowhen && time((time_t *)0) >= timowhen) { /*check for timeout*/
  557.             timowhen = 0;
  558.             if (sigtbl[MAXSIG] != SIG_DFL) (*sigtbl[MAXSIG])(SIGALRM);
  559.             return -1;
  560.         }
  561.         if ((avail = _gs_rdy(ttypn)) > 0) break;
  562.         sigmask(1);
  563.         _ss_ssig(ttypn, SIGARB);
  564.         sleep(1);               /* interupted by signal if incoming char */
  565.         _ss_rel(ttypn, SIGARB);
  566.     }
  567.  
  568.     if (avail > MYBUFLEN)
  569.       avail = MYBUFLEN;
  570.  
  571.     return(read(ttypn, mybuf, (int) avail));
  572. }
  573.  
  574. #endif /* MYREAD */
  575. /*  T T H A N G -- Hangup phone line */
  576.  
  577. tthang() {
  578. /*    if (ttypn < 0) return(0); /* Not open. */
  579.     return 0;                   /* not implemented */
  580. }
  581.  
  582.  
  583. /*  T T R E S  --  Restore terminal to "normal" mode.  */
  584.  
  585. ttres() {    /* Restore the tty to normal. */
  586.     if (ttypn < 0) return(-1);  /* Not open. */
  587. #ifdef XMODE
  588.     if (xmode(ttold.sg_baud) < 0) return(-1);
  589. #endif
  590.     tsleep(2);    /* Wait for pending i/o to finish. */
  591.     if (_ss_opt(ttypn,&ttold) < 0) return(-1); /* Restore sgtty stuff */
  592.     return(0);
  593. }
  594.  
  595. /*  T T P K T  --  Condition the communication line for packets. */
  596. /*  or for modem dialing */
  597.  
  598. /*  If called with speed > -1, also set the speed.  */
  599.  
  600. /*  Returns 0 on success, -1 on failure.  */
  601.  
  602. ttpkt(speed,flow,parity)
  603. long speed;
  604. int flow, parity;
  605. {
  606. int s;
  607.     if (ttypn < 0) return(-1);  /* Not open. */
  608.     if (speed >-1)
  609.         if ((s=ttsspd(speed/10)) <0) return(-1); /* Check the speed */
  610.  
  611.     ttprty = parity;
  612.  
  613.     ttraw.sg_case =
  614.     ttraw.sg_backsp =
  615.     ttraw.sg_delete =
  616.     ttraw.sg_echo =
  617.     ttraw.sg_alf =
  618.     ttraw.sg_nulls =
  619.     ttraw.sg_pause =
  620.     ttraw.sg_bspch =
  621.     ttraw.sg_dlnch =
  622.     ttraw.sg_eorch =
  623.     ttraw.sg_eofch =
  624.     ttraw.sg_rlnch =
  625.     ttraw.sg_dulnch =
  626.     ttraw.sg_psch =
  627.     ttraw.sg_kbich =
  628.     ttraw.sg_kbach =
  629.     ttraw.sg_bsech =
  630.     ttraw.sg_tabcr = 0;
  631.     if (speed >-1)
  632.         ttraw.sg_baud = (char)s;
  633.     if (flow==1) {
  634.         ttraw.sg_xon  = 0x11;
  635.         ttraw.sg_xoff = 0x13;
  636.     } else ttraw.sg_xon = ttraw.sg_xoff = 0;
  637.     if(_ss_opt(ttypn,&ttraw) < 0) return(-1);    /* set new modes . */
  638.     ttflui();    /* Flush any pending input */
  639.     return(0);
  640. }
  641.  
  642. /*  T T V T -- Condition communication line for use as virtual terminal  */
  643.  
  644. ttvt(speed,flow) long speed; int flow; {
  645.  
  646. int s;
  647.     if (ttypn < 0) return(-1);  /* Not open. */
  648.     if ((s=ttsspd(speed/10)) <0) return(-1); /* This speed not supported */
  649.  
  650.     tttvt.sg_case =
  651.     tttvt.sg_backsp =
  652.     tttvt.sg_delete =
  653.     tttvt.sg_echo =
  654.     tttvt.sg_alf =
  655.     tttvt.sg_nulls =
  656.     tttvt.sg_pause =
  657.     tttvt.sg_bspch =
  658.     tttvt.sg_dlnch =
  659.     tttvt.sg_eorch =
  660.     tttvt.sg_eofch =
  661.     tttvt.sg_rlnch =
  662.     tttvt.sg_dulnch =
  663.     tttvt.sg_psch =
  664.     tttvt.sg_kbich =
  665.     tttvt.sg_kbach =
  666.     tttvt.sg_bsech =
  667.     tttvt.sg_tabcr = 0;
  668.     tttvt.sg_baud = (char)s;
  669.     if (flow==1) {
  670.         tttvt.sg_xon  = 0x11;
  671.         tttvt.sg_xoff = 0x13;
  672.     } else tttvt.sg_xon = tttvt.sg_xoff = 0;
  673.     s = _ss_opt(ttypn,&tttvt);   /* set new modes . */
  674.     debug (F101,"ss_opt on tty was :","",s);
  675.     return(0);
  676. }
  677.  
  678. /*  T T F L U I  --  Flush tty input buffer */
  679.  
  680. ttflui() {
  681.     int n;
  682.     char flushbuf[256];
  683.  
  684.     if (ttypn < 0) return(-1);  /* Not open. */
  685. #ifdef MYREAD
  686.     my_count = 0;                       /* initialize myread() stuff */
  687.     my_item = -1;
  688. #endif
  689.     while((n=_gs_rdy(ttypn))>0)
  690.         read(ttypn, flushbuf, n>256 ? 256 : (unsigned int)n);
  691.     return 0;
  692. }
  693.  
  694. /*  T T C H K  --  Tell how many characters are waiting in tty input buffer  */
  695. ttchk() {
  696.     register int n;
  697.  
  698.     n = _gs_rdy(ttypn);
  699.     if (n < 0) n = 0;
  700. #ifdef MYREAD
  701.     if (my_count > 0) n += my_count;
  702. #endif
  703.     return n;
  704. }
  705.  
  706. /*  T T X I N  --  Get n characters from tty input buffer  */
  707. /*  Returns number of characters actually gotten, or -1 on failure  */
  708. /*  Intended for use only when it is known that n characters are actually */
  709. /*  Available in the input buffer.  */
  710.  
  711. ttxin(n,buf) int n; CHAR *buf; {
  712.     register int c;
  713. #ifdef MYREAD
  714.     register CHAR *bp, *bpe;
  715. #else
  716.     register int x;
  717. #endif
  718.  
  719.     ttpmsk = (ttprty) ? 0177 : 0377;         /* Parity stripping mask. */
  720.     debug(F101,"ttxin n","",n);
  721. #ifdef MYREAD
  722.     for( bpe = (bp = buf) + n; (bp != bpe) && (c = myread()) >= 0; )
  723.         *bp++ = c & ttpmsk;
  724.     if (c < 0) return -1;
  725.     *bp = '\0';
  726.     return n;
  727. #else
  728.     x = read(ttypn,(char *)buf,(unsigned int)n);
  729.     if (ttprty) {
  730.         for (c = 0; c < n; c++) buf[c] &= 0177;
  731.     }
  732.     if (x > 0) buf[x] = '\0';
  733.     if (x < 0) x = -1;
  734.     return(x);
  735. #endif
  736. }
  737.  
  738. /*  C O N I N T  --  Console Interrupt setter  */
  739. VOID
  740. conint(f,s) SIGTYP (*f)(), (*s)();
  741. {
  742.     if (backgrd) return;  /* must ignore signals in bkgrd */
  743.     signal(SIGINT,f);  /* console escape in pkt modes */
  744. }
  745.  
  746.  
  747. /*  C O N N O I  --  Reset console terminal interrupts */
  748. VOID
  749. connoi() {    /* Console-no-interrupts */
  750.     signal(SIGQUIT,(fptr)SIG_IGN);
  751.     signal(SIGINT,(fptr)SIG_IGN);
  752. }
  753.  
  754.  
  755.  
  756. /*  T T O L  --  Similar to "ttinl", but for writing.  */
  757.  
  758. ttol(s,n) int n; CHAR *s; {
  759.     int x;
  760.     debug(F101,"ttol: ttypn","",ttypn);
  761.     if (ttypn < 0) return(-1);  /* Not open. */
  762.     x = write(ttypn,s,(unsigned int)n);
  763.     debug(F111,"ttol",s,n);
  764.     if (x < 0) debug(F101,"ttol failed","",x);
  765.     return(x);
  766. }
  767.  
  768.  
  769. /*  T T O C  --  Output a character to the communication line  */
  770.  
  771. int
  772. #ifdef CK_ANSIC
  773. ttoc(char c)
  774. #else
  775. ttoc(c) char c;
  776. #endif /* CK_ANSIC */
  777. /* ttoc */{
  778.     if (ttypn < 0) return(-1);  /* Not open. */
  779.     return(write(ttypn,&c,1));
  780. }
  781.  
  782.  
  783. /*  T T I N L  --  Read a record (up to break character) from comm line.  */
  784. /*
  785.   Reads up to "max" characters from the communication line, terminating on
  786.   the packet-end character (eol), or timing out and returning -1 if the eol
  787.   character not encountered within "timo" seconds.  The characters that were
  788.   input are copied into "dest" with their parity bits stripped if parity was
  789.   selected.  Returns the number of characters read.  Characters after the
  790.   eol are available upon the next call to this function.
  791.  
  792.   The idea is to minimize the number of system calls per packet, and also to
  793.   minimize timeouts.  This function is the inner loop of the program and must
  794.   be as efficient as possible.  The current strategy is to use myread().
  795.  
  796.   WARNING: this function calls parchk(), which is defined in another module.
  797.   Normally, ckutio.c does not depend on code from any other module, but there
  798.   is an exception in this case because all the other ck?tio.c modules also
  799.   need to call parchk(), so it's better to have it defined in a common place.
  800. */
  801. #define CTRLC '\03'
  802.  
  803. int
  804. #ifdef PARSENSE
  805. #ifdef CK_ANSIC
  806. ttinl(CHAR *dest, int max,int timo, CHAR eol, CHAR start)
  807. #else
  808. ttinl(dest,max,timo,eol,start) int max,timo; CHAR *dest, eol, start;
  809. #endif /* CK_ANSIC */
  810. #else /* not PARSENSE */
  811. #ifdef CK_ANSIC
  812. ttinl(CHAR *dest, int max,int timo, CHAR eol)
  813. #else
  814. ttinl(dest,max,timo,eol) int max,timo; CHAR *dest, eol;
  815. #endif /* __SDTC__ */
  816. #endif /* PARSENSE */
  817. /* ttinl */ {
  818.  
  819.     CHAR ch;
  820.  
  821.     if (ttypn < 0) return(-1);          /* Not open. */
  822.  
  823.     debug(F101,"ttinl max","",max);
  824.     debug(F101,"ttinl timo","",timo);
  825.  
  826.     *dest = '\0';                       /* Clear destination buffer */
  827.     if (timo < 0) timo = 0;             /* Safety */
  828.     if (setjmp(sjbuf)) {                /* Timer went off? */
  829.         debug(F100,"ttinl timout","",0); /* Get here on timeout. */
  830.         debug(F110," with",(char *) dest,0);
  831.         return(-1);                     /* and return error code. */
  832.     } else {
  833.         register int i, n;              /* local variables */
  834.         int ccn = 0;
  835. #ifdef PARSENSE
  836.         int flag = 0;
  837.  
  838.         debug(F000,"ttinl start","",start);
  839.         flag = 0;                       /* Start of packet flag */
  840. #endif /* PARSENSE */
  841.  
  842.         if (timo) {                         /* Don't time out if timo == 0 */
  843.             saval = signal(SIGALRM,timerh); /* Enable timer interrupt */
  844.             alarm((unsigned int)timo);                      /* Set it. */
  845.         }
  846.         ttpmsk = (ttprty) ? 0177 : 0377; /* Set parity stripping mask. */
  847.  
  848. /* Now read into destination, stripping parity and looking for the */
  849. /* the packet terminator, and also for two Ctrl-C's typed in a row. */
  850.  
  851.         i = 0;                          /* Destination index */
  852.         debug(F101,"ttinl eol","",(int)eol);
  853.  
  854. #ifdef MYREAD
  855.         while (i < max-1) {
  856.             debug(F101,"ttinl i","",i);
  857.             if ((n = myread()) < 0) {
  858.                 debug(F101,"ttinl myread failure, n","",n);
  859.                 debug(F101,"ttinl myread errno,","",errno);
  860.                 break;
  861.             }
  862. #else
  863.         while ((i < max-1)  &&  (n = read(ttyfd, &ch, 1)) > 0) {
  864.             n = ch;
  865. #endif /* MYREAD */
  866.  
  867. #ifdef PARSENSE
  868.             if ((flag == 0) && ((n & 0x7f) == start)) flag = 1;
  869.             if (flag) dest[i++] = n & ttpmsk;
  870. #else
  871.             dest[i++] = n & ttpmsk;
  872. #endif /* PARSENSE */
  873.             if ((n & 0x7f) == CTRLC) {  /* Check for ^C^C */
  874.                 if (++ccn > 1) {        /* If we got 2 in a row, bail out. */
  875.                     if (timo) {         /* Clear timer. */
  876.                         ttimoff();
  877.                     }
  878.                     fprintf(stderr,"^C...\r\l"); /* Echo Ctrl-C */
  879.                     return(-2);
  880.                 }
  881.             } else ccn = 0;             /* Not ^C, so reset ^C counter, */
  882.  
  883. #ifdef PARSENSE
  884.             if (flag == 0) {
  885.                 debug(F101,"ttinl skipping","",n);
  886.                 continue;
  887.             }
  888. #endif /* PARSENSE */
  889.  
  890.     /* Check for end of packet */
  891.  
  892.             if ((n & 0x7f) == eol) {
  893.                 debug(F101,"ttinl got eol","",(int)eol);
  894.                 dest[i] = '\0'; /* Yes, terminate the string, */
  895.                 /* debug(F101,"ttinl i","",i); */
  896. #ifdef PARSENSE
  897. /* Here's where we actually check and adjust the parity. */
  898. /* The major flaw here is if parity is NONE (ttprty = 0) and the packets */
  899. /* really do have no parity, then parchk() is called for every packet. */
  900. /* In practice, this doesn't really harm efficiency noticably, but it would */
  901. /* be better if ttinl() had a way of knowing to stop doing this once a */
  902. /* particular file transfer had been started and checked. */
  903.                 if (ttprty == 0) {
  904.                     if ((ttprty = parchk(dest,start,i)) > 0) {
  905.                         int j;
  906.                         debug(F101,"ttinl senses parity","",ttprty);
  907.                         debug(F110,"ttinl packet before",dest,0);
  908.                         ttpmsk = 0x7f;
  909.                         for (j = 0; j < i; j++)
  910.                           dest[j] &= 0x7f;      /* Strip parity from packet */
  911.                         debug(F110,"ttinl packet after ",dest,0);
  912.                     } else debug(F101,"parchk","",ttprty);
  913.                 }
  914. #endif /* PARSENSE */
  915.                 if (timo) {                     /* Turn off timer. */
  916.                     ttimoff();
  917.                 }
  918.                 debug(F111,"ttinl got", dest,i);
  919.                 return(i);
  920.             }
  921.         }                               /* end of while() */
  922.         if (timo)                       /* Turn off timer. */
  923.           ttimoff();
  924.         return(-1);
  925.     }
  926. }
  927.  
  928. /*  T T I N C --  Read a character from the communication line  */
  929. /*
  930.  On success, returns the character that was read, >= 0.
  931.  On failure, returns -3 on internal error, -2 on communication disconnect,
  932.  -1 on timeout and other
  933. */
  934. int
  935. ttinc(timo) int timo; {
  936.     CHAR ch = 0;
  937.     int n = 0;
  938.  
  939.     if (ttypn < 0) return(-1);          /* Not open. */
  940.     if (timo <= 0) {                    /* Untimed. */
  941.         /* not realy untimed, ckudia.c uses alarm() */
  942. #ifdef MYREAD
  943.         /* comm line failure returns -1 thru myread, so no &= 0377 */
  944.         n = myread();                   /* Wait for a character... */
  945.         /* debug(F101,"ttinc n","",n); */
  946. #else
  947.         while ((n = read(ttypn,&ch,1)) == 0) /* Wait for a character. */
  948.         /* Shouldn't have to loop in ver 5A. */
  949.           ;
  950. /* debug(F000,"ttinc","",ch); */
  951.         return( (n < 1) ? -3 : (ch & ttpmsk) );
  952. #endif /* MYREAD */
  953.     } else {
  954.         saval = signal(SIGALRM,timerh); /* Timed, set up timer. */
  955.         alarm((unsigned int)timo);
  956.         if (setjmp(sjbuf)) {
  957.             n = -1;
  958.         } else {
  959. #ifdef MYREAD
  960.             n = myread();               /* If managing own buffer... */
  961.             debug(F101,"ttinc myread","",n);
  962. #else
  963.             n = timoread(ttypn,(char *)&ch,1);  /*Otherwise call the system.*/
  964.             debug(F101,"ttinc read","",n);
  965.             if (n > 0)
  966.               n = ch & 255;
  967.             else
  968.               n = (n < 0) ? -3 : -2;    /* Special return codes */
  969. #endif /* MYREAD */
  970.         }
  971.         ttimoff();                          /* Turn off timer */
  972.     }
  973.     return( (n < 0) ? n : (n & ttpmsk) ); /* Return masked char or neg. */
  974. }
  975.  
  976. /*  T T S N D B  --  Send a BREAK signal  */
  977.  
  978. ttsndb()
  979. {
  980. struct sgbuf before;
  981. int i;
  982.     if (ttypn < 0) return(-1);  /* Not open. */
  983. #ifndef XMODE
  984.     if (send_break(ttypn) == 0) return(0); /* all fine done by driver */
  985.     if (errno!=E_UNKSVC) return(-1); /*should have been unknown service call*/
  986.     if (_gs_opt(ttypn,&before) < 0) return(-1); /* din't get old speed */
  987.     for (i=B50; i<before.sg_baud; i++) /* try to switch to a lower speed */
  988.     {
  989.       if (ttsspd((int)bdrts[i]/10) != -1) break;
  990.     }
  991.     if (i == before.sg_baud) return(-1); /* no lower speed supported */
  992. #else
  993.     if (_gs_opt(ttypn,&before) < 0) return(-1); /* din't get old speed */
  994.     ttsspd((int)bdrts[B300]/10);/* xmode always return success, since it only
  995.                                    patches device descriptor */
  996. #endif
  997.     write(ttypn,"\0",1);
  998.     write(ttypn,"\0",1); /* send two zeroes */
  999.     msleep(400);      /* wait chars to appear (50 baud == 400 milli seconds */
  1000.     ttsspd((int)bdrts[before.sg_baud]/10); /* restore old baud rate*/
  1001.     return(0);
  1002. }
  1003.  
  1004. VOID
  1005. rtimer() {
  1006.     tcount = time( (time_t *) 0 );
  1007. }
  1008.  
  1009.  
  1010. /*  G T I M E R --  Get current value of elapsed time counter in seconds  */
  1011.  
  1012. int
  1013. gtimer() {
  1014.     int x;
  1015.     x = (int) (time( (time_t *) 0 ) - tcount);
  1016.     return( (x < 0) ? 0 : x );
  1017. }
  1018.  
  1019. /*  Z T I M E  --  Return date/time string  */
  1020. VOID
  1021. ztime(s) char **s;
  1022. {
  1023. time_t t;
  1024.   t = time((time_t*)NULL);
  1025.   if (t != (time_t)-1)
  1026.     *s =ctime(&t);
  1027.   else
  1028.     *s = "Ddd Mmm 00 00:00:00 0000\n";  /* Return dummy in asctime() format */
  1029. }
  1030.  
  1031. /*  C O N G M  --  Get console terminal modes.  */
  1032.  
  1033. /*
  1034.  Saves current console mode, and establishes variables for switching between
  1035.  current (presumably normal) mode and other modes.
  1036. */
  1037.  
  1038. congm() {
  1039.     if (!isatty(0)) return(-1);  /* only for real ttys */
  1040.     _gs_opt(0,&ccold);   /* Structure for restoring */
  1041.     _gs_opt(0,&cccbrk);  /* For setting CBREAK mode */
  1042.     cccbrk.sg_echo  =
  1043.     cccbrk.sg_pause =
  1044.     cccbrk.sg_eofch =
  1045.     cccbrk.sg_psch = 0; /* kermit interpreter uses CTRL-W */
  1046.  
  1047.     _gs_opt(0,&ccraw);   /* For setting RAW mode */
  1048.     ccraw.sg_case =
  1049.     ccraw.sg_backsp =
  1050.     ccraw.sg_delete =
  1051.     ccraw.sg_echo =
  1052.     ccraw.sg_alf =
  1053.     ccraw.sg_nulls =
  1054.     ccraw.sg_pause =
  1055.     ccraw.sg_bspch =
  1056.     ccraw.sg_dlnch =
  1057.     ccraw.sg_eorch =
  1058.     ccraw.sg_eofch =
  1059.     ccraw.sg_rlnch =
  1060.     ccraw.sg_dulnch =
  1061.     ccraw.sg_psch =
  1062.     ccraw.sg_kbich =
  1063.     ccraw.sg_kbach =
  1064.     ccraw.sg_bsech =
  1065.     ccraw.sg_tabcr =
  1066.     ccraw.sg_xon =
  1067.     ccraw.sg_xoff = 0;
  1068.         return(0);
  1069. }
  1070.  
  1071.  
  1072. /*  C O N C B --  Put console in cbreak mode.  */
  1073. /* we can ignore esc since conchk() works on OS-9, no need for a special esc */
  1074. /*  Returns 0 if ok, -1 if not  */
  1075.  
  1076. int
  1077. #ifdef CK_ANSIC
  1078. concb(char esc)
  1079. #else
  1080. concb(esc) char esc;
  1081. #endif /* CK_ANSIC */
  1082. /* concb */ {
  1083.     int x;
  1084.     if (!isatty(0)) return(0);  /* only for real ttys */
  1085.     ckxech = 1;                 /* Program can echo characters */
  1086.     x = _ss_opt(0,&cccbrk) | _ss_opt(1,&cccbrk);
  1087.     debug(F101,"console set to cbreak mode","",esc);
  1088.     return(x);
  1089. }
  1090.  
  1091. /*  C O N B I N  --  Put console in binary mode  */
  1092.  
  1093. /* we can ignore esc since conchk() works on OS-9, no need for a special esc */
  1094. /*  Returns 0 if ok, -1 if not  */
  1095.  
  1096. int
  1097. #ifdef CK_ANSIC
  1098. conbin(char esc)
  1099. #else
  1100. conbin(esc) char esc;
  1101. #endif /* CK_ANSIC */
  1102. /* conbin */ {
  1103.     if (!isatty(0)) return(0);  /* only for real ttys */
  1104.     ckxech = 1;                 /* Program can echo characters */
  1105.     _ss_opt(0,&ccraw);
  1106.     _ss_opt(1,&ccraw);          /* set new modes . */
  1107.     debug(F101,"console switched to raw mode","",esc);
  1108.     return(0);
  1109. }
  1110.  
  1111.  
  1112. /*  C O N R E S  --  Restore the console terminal  */
  1113.  
  1114. conres() {
  1115.     if(!isatty(0)) return 0; /* only for real ttys with known modes*/
  1116.     tsleep(2);
  1117.     ckxech = 0;    /* OS-9 will echo chars */
  1118.     return(_ss_opt(0,&ccold)| _ss_opt(1,&ccold)); /* Restore controlling tty */
  1119. }
  1120.  
  1121. /*  C O N O C  --  Output a character to the console terminal  */
  1122. int
  1123. #ifdef CK_ANSIC
  1124. conoc(char c)
  1125. #else
  1126. conoc(c) char c;
  1127. #endif /* CK_ANSIC */
  1128. /* conoc */ {
  1129.     return((write(1,&c,1)>0) ? 1:0);
  1130. }
  1131.  
  1132. /*  C O N X O  --  Write x characters to the console terminal  */
  1133. /*VOID*/
  1134. int
  1135. conxo(x,s) char *s; int x; {
  1136.     write(1,s,(unsigned int)x);
  1137. }
  1138.  
  1139. /*  C O N O L  --  Write a line to the console terminal  */
  1140. /*VOID*/
  1141. int
  1142. conol(s) char *s; {
  1143.     int len;
  1144.     len = strlen(s);
  1145.     write(1,s,(unsigned int)len);
  1146. }
  1147.  
  1148. /*  C O N O L L L  --  Output a string followed by LF  */
  1149. static VOID
  1150. conolll(s) char *s; {
  1151.     conol(s);
  1152.     write(1,"\l",1);
  1153. }
  1154.  
  1155. /*  C O N O L A  --  Write an array of lines to the console terminal */
  1156. /*VOID */
  1157. int
  1158. conola(s) char *s[]; {
  1159.     int i;
  1160.     for (i=0 ; *s[i] ; i++) conolll(s[i]);
  1161. }
  1162.  
  1163. /*  C O N O L L  --  Output a string followed by CRLF  */
  1164. /*VOID*/
  1165. int
  1166. conoll(s) char *s; {
  1167.     conol(s);
  1168.     write(1,"\r\l",2);
  1169. }
  1170.  
  1171. /*  C O N C H K  --  Return how many characters available at console  */
  1172. conchk()
  1173. {
  1174.     int x;
  1175.     x=_gs_rdy(0);
  1176.     if(x<0) x=0; /* always return 0 irrespective the type of error */
  1177.                  /* e.q. /nil would return E_UNKSVC and conchk is only */
  1178.                  /* checked on != 0 anyway, Bob Larson */
  1179.     debug (F101,"conchk","",x);
  1180.     return x;
  1181. }
  1182.  
  1183. /*  C O N I N C  --  Get a character from the console  */
  1184. int
  1185. coninc(timo) int timo; {
  1186.     int n = 0; CHAR ch;
  1187.  
  1188.     if (timo <= 0 ) {                   /* Untimed. */
  1189.         n = read(0, (char *)&ch, 1);            /* Read a character. */
  1190.         ch &= 0377;
  1191.         if (n > 0)                      /* Return the character if read ok */
  1192.           return(ch);
  1193.         else                            /* otherwise... */
  1194.         {
  1195.             debug(F101, "coninc(0) errno","",errno);
  1196.             return(-1);             /* Return -1 as error indication */
  1197.         }
  1198.     }
  1199.     if (setjmp(sjbuf)) n = -2;
  1200.     else {
  1201.         saval = signal(SIGALRM,timerh); /* Timed read, so set up timer */
  1202.         alarm((unsigned int)timo);
  1203.         n = timoread(0, (char *)&ch, 1);
  1204.         ttimoff();                          /* Turn off timer */
  1205.         ch &= 0377;
  1206.     }
  1207.     if (n > 0) return(ch);
  1208.     else
  1209.         return(-1);
  1210. }
  1211.  
  1212. /* emulate unix signal functions */
  1213. fptr
  1214. signal(sig,func)
  1215. int sig;
  1216. fptr func;
  1217. {
  1218.     fptr temp;
  1219.     if (sig < MAXSIG) {
  1220.         temp = sigtbl[sig];
  1221.         sigtbl[sig] = func;
  1222.         return temp;
  1223.     }
  1224.     if (sig == SIGALRM) {
  1225.         temp = sigtbl[MAXSIG];
  1226.         sigtbl[MAXSIG] = func;
  1227.         return temp;
  1228.     }
  1229.     return (fptr)-1;
  1230. }
  1231.  
  1232. VOID
  1233. catch(sig) register int sig;{
  1234.     register fptr temp;
  1235.  
  1236.     if(sig==SIGARB) return; /* nothing to do with i/o signals */
  1237.     if(sig < MAXSIG) {
  1238.         if ((temp=sigtbl[sig])==SIG_DFL) doexit(sig,-1);
  1239.         if(temp!=(SIGTYP (*)())SIG_IGN)
  1240.           (*temp)(sig);
  1241.         else
  1242.         {
  1243.           if (sig == SIGQUIT) /* output ceises on CTRL-E typed, so notify */
  1244.             stdin->_flag |= _ERR;
  1245.         }
  1246.         return;
  1247.     }
  1248.     if(sig == SIGALRM) {
  1249.         if ((temp=sigtbl[MAXSIG])==SIG_DFL) return;
  1250.         if (temp!=(SIGTYP (*)())SIG_IGN) (*temp)(sig);
  1251.         return;
  1252.     }
  1253.     doexit(sig,-1);
  1254. }
  1255.  
  1256. int isatty(path)
  1257. int path;
  1258. {
  1259.     struct sgbuf buffer;
  1260.     return((_gs_opt(path,&buffer)<0) ? 0 : buffer.sg_class == 0);
  1261. }
  1262.  
  1263. /*********************************************************************/
  1264. int msleep(m_secs) /* sleeps at least !! (not exact) m_secs milli seconds */
  1265. register int m_secs;
  1266. {
  1267. register unsigned int i;
  1268. register int tick_rate;
  1269.   if ((tick_rate = _getsys(D_TckSec,2)) < 0) return(-1); /* clock   not on */
  1270.   i = (unsigned int)(((double)m_secs*(double)tick_rate)/1000.0);
  1271.   tsleep(i+2);  /* +1 for rounding and another
  1272.                    +1 because tsleep is accurate not more than +/-(!) 1 tick */
  1273.   return(0);
  1274. }
  1275. /**********************************************************************/
  1276. psuspend(foo) int foo;
  1277. {
  1278.   return(-1);
  1279. }
  1280.  
  1281. long ttgspd()                           /* Get current tty speed */
  1282. {
  1283. struct sgbuf buffer;
  1284.   if (ttypn < 0) return(-1);
  1285.   if(_gs_opt(ttypn,&buffer) < 0) return(-1);
  1286.   return(bdrts[buffer.sg_baud]);
  1287. }
  1288.  
  1289.  
  1290. /*  T T S C A R R  --  Set ttcarr variable, controlling carrier handling.
  1291.  *
  1292.  *  0 = Off: Always ignore carrier. E.g. you can connect without carrier.
  1293.  *  1 = On: Heed carrier, except during dialing. Carrier loss gives disconnect.
  1294.  *  2 = Auto: For "modem direct": The same as "Off".
  1295.  *            For real modem types: Heed carrier during connect, but ignore
  1296.  *                it anytime else.  Compatible with pre-5A C-Kermit versions.
  1297.  *
  1298.  * As you can see, this setting does not affect dialing, which always ignores
  1299.  * carrier (unless there is some special exception for some modem type).  It
  1300.  * does affect ttopen() if it is set before ttopen() is used.  This setting
  1301.  * takes effect on the next call to ttopen()/ttpkt()/ttvt().  And they are
  1302.  * (or should be) always called before any communications is tried, which
  1303.  * means that, practically speaking, the effect is immediate.
  1304.  *
  1305.  * Of course, nothing of this applies to remote mode (xlocal = 0).
  1306.  *
  1307.  * Someone has yet to uncover how to manipulate the carrier in the BSD
  1308.  * environment (or any non-termio using environment).  Until that time, this
  1309.  * will simply be a no-op for BSD.
  1310.  *
  1311.  * Note that in previous versions, the carrier was most often left unchanged
  1312.  * in ttpkt()/ttvt() unless they were called with DIALING or CONNECT.  This
  1313.  * has changed.  Now it is controlled by ttcarr in conjunction with these
  1314.  * modes.
  1315.  */
  1316. ttscarr(carrier) int carrier; {
  1317.     ttcarr = carrier;
  1318.     debug(F101, "ttscarr","",ttcarr);
  1319.     return(ttcarr);
  1320. }
  1321.  
  1322. /*  T T G M D M  --  Get modem signals  */
  1323. /*
  1324.  Looks for the modem signals CTS, DSR, and CTS, and returns those that are
  1325.  on in as its return value, in a bit mask as described for ttwmdm.  Returns:
  1326.  -3 Not implemented
  1327.  -2 if the line does not have modem control
  1328.  -1 on error.
  1329.  >= 0 on success, with a bit mask containing the modem signals that are on.
  1330. */
  1331. ttgmdm()
  1332. {
  1333.   return(-3);
  1334. }
  1335.  
  1336. /*  T T S S P D  --  Set the transmission of tty to ten times the argument */
  1337.  
  1338. ttsspd(speed) int speed; {
  1339. int s;
  1340. struct sgbuf before;
  1341.     debug (F101,"ttsspd: speed(cps):","",speed);
  1342.     if (ttypn < 0) return(-1);
  1343.     switch (speed) {
  1344.         case 5:         s = B50;        break;
  1345.         case 7:         s = B75;        break;
  1346.         case 11:        s = B110;       break;
  1347.         case 13:        s = B134;       break;
  1348.         case 15:        s = B150;       break;
  1349.         case 30:        s = B300;       break;
  1350.         case 60:        s = B600;       break;
  1351.         case 120:       s = B1200;      break;
  1352.         case 180:       s = B1800;      break;
  1353.         case 200:       s = B2000;      break;
  1354.         case 240:       s = B2400;      break;
  1355.         case 360:       s = B3600;      break;
  1356.         case 480:       s = B4800;      break;
  1357.         case 720:       s = B7200;      break;
  1358.         case 960:       s = B9600;      break;
  1359.         case 1920:      s = B19200;     break;
  1360.         case 3840:      s = B38400;     break;
  1361.         case 888:       return(-1); /* no 75/1200 split speed */
  1362.         default:        return -1;
  1363.     }
  1364.     _gs_opt(ttypn,&before);
  1365.     before.sg_baud = (char)s;
  1366. #ifdef XMODE
  1367.     xmode((char)s); /* xmode open and closes => new options
  1368.                        => restore as they were before */
  1369.     return(_ss_opt(ttypn,&before) < 0 ? -1 : s);
  1370. #else
  1371.     return(_ss_opt(ttypn,&before) < 0 ? -1 : s);
  1372. #endif
  1373. }
  1374.  
  1375. #ifdef XMODE
  1376. int     /* change the line tty speed via xmode, special hack for bad drivers*/
  1377. xmode(os9_speed) char os9_speed;
  1378. {
  1379. char command[DEVNAMLEN+17]; /* e.g. xmode baud=38400 = 17 chars */
  1380. char devicename[DEVNAMLEN];
  1381. long old_speed;
  1382.     if ((old_speed = ttgspd()) < 0) return(-1);
  1383.     devicename[0] = '/';
  1384.     _gs_devn(ttypn,&devicename[1]);
  1385.     sprintf(command,"xmode %s baud=%d",devicename,bdrts[os9_speed]);
  1386.     debug (F110,"xmode command = ",command,0);
  1387.     debug (F101,"xmode baud","",os9_speed);
  1388.     zsyscmd(command);
  1389.     close(ttypn);       /* hopefully not iniz'ed */
  1390.     if ((ttypn=open(devicename,0x43)) < 0) /* no open with new speed anymore*/
  1391.     {
  1392.       sprintf(command,"xmode %s baud=%d",devicename,old_speed);
  1393.       zsyscmd(command); /* restore old speed */
  1394.       ttypn=open(devicename,0x43); /* now it should work again */
  1395.       return (-1);
  1396.     }
  1397.     return(os9_speed);
  1398. }
  1399. #endif
  1400. VOID
  1401. ttimoff() {                           /* Turn off any timer interrupts */
  1402.     alarm(0);
  1403.     if (saval)
  1404.       signal(SIGALRM,saval);
  1405.     else
  1406.       signal(SIGALRM,(fptr)SIG_DFL);
  1407.     saval = NULL;
  1408. }
  1409.  
  1410. int alarm(secs) unsigned int secs;
  1411. /* after secs seconds the signal SIGALRM will be sent */
  1412. /* 0 will clear the alarm */
  1413. {
  1414.     if (secs) {
  1415.         timowhen = time((time_t *)0) + secs;
  1416.         return(add_alarm(secs,SIGALRM));
  1417.     }
  1418.     timowhen = 0;
  1419.     return(remove_alarm());
  1420. }
  1421.  
  1422. /* read from serial device with working timeout */
  1423. int
  1424. timoread(ttypn, cp, n)
  1425. int ttypn;
  1426. char *cp;
  1427. int n;
  1428. {
  1429.     if (timowhen == 0) return read(ttypn, cp, n);
  1430.     (void) remove_alarm();
  1431.     for(;;) {
  1432.         if (_gs_rdy(ttypn) >= n) return read(ttypn, cp, n);
  1433.         if (time((time_t *)0) >= timowhen) {
  1434.             timowhen = 0;
  1435.             if (sigtbl[MAXSIG] != SIG_DFL) (*sigtbl[MAXSIG])(SIGALRM);
  1436.             return -1;
  1437.         }
  1438.         _ss_ssig(ttypn, SIGARB);
  1439.         sleep(1);       /* interupted by _ss_ssig if incoming char */
  1440.         _ss_rel(ttypn, SIGARB);
  1441.     }
  1442. }
  1443.  
  1444. /* Timeout handler for communication line input functions */
  1445. static SIGTYP
  1446. timerh(sig) int sig;{
  1447.     ttimoff();
  1448.     sigmask(-1);        /*  we are in an intercept routine but do not perform
  1449.                             a F$RTE (done implicitly but rts). => we have to
  1450.                             decrement the sigmask as F$RTE does. Warning:
  1451.                             longjump only restores the cpu registers NOT the
  1452.                             fpu registers. So, don't use fpu at all or at
  1453.                             least don't use common fpu (double or float)
  1454.                             register variables */
  1455.     longjmp(sjbuf,1);
  1456. }
  1457. int ustrncmp(a2,a3,d4) /*string compare but ignore upper and lower case*/
  1458. register char *a2,*a3;
  1459. register int d4;
  1460. {
  1461.   while((--d4>=0)&&(_toupper(*a2)==_toupper(*a3)))
  1462.   {
  1463.     if (*a3++ == '\0') return 0;
  1464.     a2++;
  1465.   }
  1466.   return (d4 < 0) ? 0 : *a2-*a3;
  1467. }
  1468.