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

  1. /* File uclkmd.c        Chris Kennington        12th March 1986.
  2.           Machine-dependent parts of UCL Kermit.                  */
  3.  
  4. /* The following routines are possibly machine or system dependent:
  5.     it is believed that all such dependencies in the code
  6.           have been grouped here.                                 */
  7.  
  8. #include        <stdio.h>
  9. #include        <sgtty.h>
  10. #include        <signal.h>
  11.  
  12.  
  13. /* Variables in main file:                                      */
  14. extern  char    timflag;
  15. extern  int     debug, image, timoex();
  16. extern  FILE    *dfp, *fp;
  17.  
  18. #define CR      13
  19. #define LF      10
  20.  
  21.  
  22. struct  sgttyb  cookedmode, rawmode;
  23.  
  24.  
  25.  
  26.  
  27.  
  28. char ascedit(c)         /* edit character for sending           */
  29. /* This filtering routine converts unix LF line-terminator
  30.           to CR/LF pair iff in 7-bit mode.
  31.     Returns modified char.                                      */
  32. char    c;
  33. {
  34.     if (image == 0) {           /* only if 7-bit                */
  35.           c &= 0x7f;
  36.           if (c == LF)
  37.               encode(CR);         /* CR of CR/LF;                 */
  38.     }                           /*  bufill() adds the LF        */
  39.     return(c);
  40. }               /* End of ascedit()                             */
  41.  
  42.  
  43.  
  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) )
  56.       || ( (a == LF) && (olda == CR) ) )
  57.           ret = olda = 0;
  58.     else {                  /*  if not CR/LF pair   */
  59.           olda = a;
  60.           if (a == CR)
  61.               a = LF;         /*  CR => LF for unix   */
  62.   /* if converting for non-unix system, probably need LF => CR instead */
  63.           ret = putc(a,fp);
  64.     }
  65.     return (ret);
  66. }                       /* End of ascout()                      */
  67.  
  68.  
  69.  
  70. char  filerr()          /* return error-code or 0       */
  71. /* called when EOF encountered reading or writing to data-file;
  72.           if really endfile, returns 0, else system error-code.   */
  73. {
  74.     char    ret;
  75.  
  76.     ret = ferror(fp);
  77.     clearerr(fp);
  78.     return(ret);
  79. }                       /* End of filerr()              */
  80.  
  81.  
  82.  
  83. flushinput()            /* UCL 2.8 version              */
  84. /*  Dump all pending input to clear stacked up NAK's.
  85.  *  Note that because of a bug in unix 2.8, this also flushes OUTPUT! */
  86. {
  87.      ioctl(0,TIOCFLUSH,0);
  88.      return;
  89. }                       /* End of flushinput()          */
  90.  
  91.  
  92.  
  93. /*  Routines to set terminal input into "raw" or "cooked" mode. */
  94.  
  95. static  char  cookedok = 0;
  96.  
  97. cooktty()               /* restore terminal state       */
  98. {
  99.     if (cookedok != 0)          /*  provided made raw   */
  100.           stty(0,&cookedmode);
  101.     return;
  102. }                       /* End of cooktty               */
  103.  
  104. rawtty()                /* set terminal raw             */
  105. {
  106.     if (cookedok == 0) {        /* first time only      */
  107.           gtty(0,&cookedmode);            /* Save current mode so we can */
  108.           gtty(0,&rawmode);               /* restore it later */
  109.           cookedok = 1;
  110.           rawmode.sg_flags |= (RAW|TANDEM);
  111.           rawmode.sg_flags &= ~(ECHO|CRMOD);
  112.     }
  113.     stty(0,&rawmode);               /* Put tty in raw mode */
  114.     return;
  115. }                       /* End of rawtty() */
  116.  
  117. /* end of tty cook/uncook routines                      */
  118.  
  119.  
  120.  
  121. /*  UCL Input & Timeout Routines                        */
  122.  
  123. /*  Timeouts are always accompanied by attempting to read
  124.           the line.  Two situations are catered for: normally,
  125.           if a timeout can be set which cancels a hanging
  126.           read() call, then this is done; else if a test
  127.           can be made as to whether chars are available AND
  128.           a sleep() call is available, these are linked to
  129.           provide a timeout based on decrementing after
  130.           each sleep() [this is the "Berkeley 4.2" situation].
  131.      The following code uses "#ifdef BSD42" to differentiate
  132.           between "4.2" & "normal"; this has been hacked into
  133.           the VAX C-compiler at UCL but can be inserted into
  134.           the text elsewhere.                                     */
  135.  
  136. char nextin()            /* read next char, checking time-flag  */
  137. /* return char (or 0 if timer expired)                          */
  138. {
  139.     char   c;
  140.     static char buff[64];
  141.     static int  count = 0, cmax =  0;
  142.     long   lcount;
  143.  
  144. #ifndef BSD42
  145. /* this code if timeout cancels read()                  */
  146.    read (0,&c,1);
  147. #else
  148. /* this code if must test whether chars available       */
  149.     if (count != 0)
  150.           ;                       /* chars available      */
  151.     else {
  152.           while (count == 0) {
  153.               sleep(1);           /* wait a tick          */
  154.               if (timflag == 0) {
  155.                     if (debug) {
  156.                         printmsg("Timeout... ");
  157.                         fprintf(dfp," Timeout... ");
  158.                     }
  159.                     return(0);      /* timer expired        */
  160.               }
  161.               else {
  162.                     --timflag;      /* decrement timer      */
  163.                     ioctl(0,FIONREAD,&lcount); /* new count */
  164.                     count = lcount;
  165.           }   }
  166.           count &= 0x3f;          /* max 63 chars         */
  167.           read(0,buff,count);     /* read them            */
  168.           cmax = count;
  169.     }
  170.     c = buff[cmax - (count--)];          /* return next char     */
  171. #endif
  172.     if (debug > 2)
  173.           fprintf(dfp," next = %xx ",((int)c)&0xff );
  174.     return(c);
  175. }                       /* end of nextin()              */
  176.  
  177.  
  178.  
  179. timoset(sex)            /* set timeout                  */
  180. char  sex;                      /* # of seconds         */
  181. {
  182.     timflag = sex;
  183. #ifndef BSD42
  184. /* only set timeout-alarms if non-4.2 code being used */
  185.    signal(SIGALRM,timoex);
  186.    alarm(timflag);
  187. #endif
  188.     return;
  189. }                       /* End of timoset()             */
  190.  
  191. /* The routines which action and clear timeouts, timoex()
  192.      and timocan() are not system-dependent; see above. */
  193.  
  194.  
  195.  
  196. unbuffer()              /* unbuffer output              */
  197. /* system-dependent action to write quickly to terminal */
  198. {
  199.     setbuf(stdout,0);           /*  UNbuffer output!    */
  200.     return;
  201. }                       /* End of unbuffer()            */
  202.  
  203.  
  204. /*  End of File uclkmd.c                                */
  205.