home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / uclkermit.zip / cuclx5.c < prev    next >
C/C++ Source or Header  |  1988-08-16  |  4KB  |  162 lines

  1. /* File uclk5md.c    Icarus Sparry, Bath University
  2.     Machine-dependent parts of UCL Kermit.
  3.  
  4.         Modified to run on UNIX System 5 as on GEC 63 systems */
  5.  
  6. /* Revision History:
  7.     0:    First attempt. 24 Oct 85
  8.         Changed sttyb to termio, deleted all code in #ifdef BSD
  9.         changed stty,gtty to ioctl and altered flags
  10. */
  11.  
  12. /*    The following routines are possibly machine or system dependent:
  13.     it is believed that all such dependencies in the code
  14.     have been grouped here. */
  15.  
  16. #include    <stdio.h>
  17. #include    <termio.h>
  18. #include    <signal.h>
  19.  
  20.  
  21. /* Variables in main file: */
  22. extern    char    timflag;
  23. extern    int    debug, image, timoex();
  24. extern    FILE    *dfp, *fp;
  25.  
  26. #define CR    13
  27. #define LF    10
  28.  
  29. struct    termio    cookedmode, rawmode;
  30.  
  31. char ascedit(c)        /* edit character for sending */
  32. /*    This filtering routine converts unix LF line-terminator
  33.     to CR/LF pair iff in 7-bit mode.
  34.     Returns modified char. */
  35. char    c;
  36. {
  37.     if (image == 0) {    /* only if 7-bit */
  38.         c &= 0x7f;
  39.         if (c == LF)
  40.             encode(CR);    /* CR of CR/LF; */
  41.     }    /* bufill() adds the LF */
  42.     return(c);
  43. }    /* End of ascedit() */
  44.  
  45. char ascout(a)        /* char to file coping with CR/LF */
  46. /* This filtering routine is used to write 7-bit data to file as
  47.     it is received. Not used for 8-bit data, either image
  48.     or 8-prefixing. */
  49. char    a;
  50. {
  51.     char    ret;
  52.     static    char    olda = 0;
  53.  
  54.     /* for unix, replace each CR, LF or CR/LF or LF/CR by a single LF */
  55.     if ( ( (a == CR) && (olda == LF) ) || ( (a == LF) && (olda == CR) ) )
  56.         ret = olda = 0;
  57.     else
  58.     {    /* if not CR/LF pair */
  59.         olda = a;
  60.         if (a == CR) a = LF;    /* CR => LF for unix */
  61.     /* if converting for non-unix system, probably need LF => CR instead */
  62.         ret = putc(a,fp);
  63.     }
  64.     return (ret);
  65. }    /* End of ascout() */
  66.  
  67. char    filerr()        /* return error-code or 0 */
  68. /* called when EOF encountered reading or writing to data-file;
  69.     if really endfile, returns 0, else system error-code. */
  70. {
  71.     char    ret;
  72.  
  73.     ret = ferror(fp);
  74.     clearerr(fp);
  75.     return(ret);
  76. }    /* End of filerr() */
  77.  
  78. flushinput()        /* UCL 2.8 version */
  79. /* Dump all pending input to clear stacked up NAK's.
  80.  * Note that because of a bug in unix 2.8, this also flushes OUTPUT! */
  81. {
  82.     ioctl(0,TCFLSH,0);
  83. }    /* End of flushinput() */
  84.  
  85. /* Routines to set terminal input into "raw" or "cooked" mode. */
  86.  
  87. static    char    cookedok = 0;
  88.  
  89. cooktty()        /* restore terminal state */
  90. {
  91.     if (cookedok != 0)        /* provided made raw */
  92.         ioctl(0,TCSETAW,&cookedmode);
  93. }    /* End of cooktty */
  94.  
  95. rawtty()        /* set terminal raw */
  96. {
  97.     if (cookedok == 0) {    /* first time only */
  98.         ioctl(0,TCGETA,&cookedmode); /* Save current mode so we can */
  99.         ioctl(0,TCGETA,&rawmode);    /* restore it later */
  100.         cookedok = 1;
  101.         rawmode.c_iflag = IXON|IXOFF;
  102.         rawmode.c_oflag &= ~OPOST;
  103.         rawmode.c_lflag = 0;
  104.         rawmode.c_cc[VEOF] = 1;        /* Require at least one */
  105.     }
  106.     ioctl(0,TCSETAW,&rawmode);    /* Put tty in raw mode */
  107. }    /* End of rawtty() */
  108.  
  109. /* end of tty cook/uncook routines */
  110.  
  111. /* UCL Input & Timeout Routines */
  112.  
  113. /* Timeouts are always accompanied by attempting to read
  114.     the line. Two situations are catered for: normally,
  115.     if a timeout can be set which cancels a hanging
  116.     read() call, then this is done; else if a test
  117.     can be made as to whether chars are available AND
  118.     a sleep() call is available, these are linked to
  119.     provide a timeout based on decrementing after
  120.     each sleep() [this is the "Berkeley 4.2" situation].
  121.     The following code uses "#ifdef BSD42" to differentiate
  122.     between "4.2" & "normal"; this has been hacked into
  123.     the VAX C-compiler at UCL but can be inserted into
  124.     the text elsewhere. */
  125.  
  126. char nextin()        /* read next char, checking time-flag */
  127. /* return char (or 0 if timer expired) */
  128. {
  129.     char    c;
  130.     static char buff[64];
  131.     static int count = 0;
  132.     long    lcount;
  133.  
  134.     /* this code if timeout cancels read() */
  135.     read (0,&c,1);
  136.     if (debug > 2)
  137.         fprintf(dfp," next = %xx ",((int)c)&0xff );
  138.     return(c);
  139. }    /* end of nextin() */
  140.  
  141.  
  142.  
  143. timoset(sex)        /* set timeout */
  144. char    sex;    /* # of seconds */
  145. {
  146.     timflag = sex;
  147.     /* only set timeout-alarms if non-4.2 code being used */
  148.     signal(SIGALRM,timoex);
  149.     alarm(timflag);
  150. }    /* End of timoset() */
  151.  
  152. /* The routines which action and clear timeouts, timoex()
  153.     and timocan() are not system-dependent; see above. */
  154.  
  155. unbuffer()        /* unbuffer output */
  156. /* system-dependent action to write quickly to terminal */
  157. {
  158.     setbuf(stdout,0);    /* UNbuffer output! */
  159. }    /* End of unbuffer() */
  160.  
  161. /* End of File */
  162.