home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / ckermit5a190 / ck9tio.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  49KB  |  1,470 lines

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