home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_08 / v7n8110a.txt < prev    next >
Text File  |  1989-10-02  |  3KB  |  130 lines

  1.  
  2.  
  3. *** Listing 4 ***
  4.  
  5. /*
  6.  * hn_port.c - UNIX version of port-level i/o for the HNL
  7.  *
  8.  * Copyright(c) 1988 by Hobart Corporation.
  9.  * Used with permission.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <termio.h>
  14. #include <hn.h>
  15. #include <hn_errno.h>
  16.  
  17.  
  18. hn_port *hn_popen(char *dev, hn_bps bps, hn_parity par,
  19.         unsigned data_bits, unsigned stop_bits)
  20.     {
  21.     static struct termio t =
  22.         {
  23.         IGNBRK | INPCK | ISTRIP, 0, CREAD | CLOCAL, 0, 0,
  24.         0, 0, 0, 0, 0, 0, 0, 0
  25.         };
  26.     static unsigned actual_bps[] = {1200, 2400, 4800, 9600};
  27.     static unsigned unix_bps[] = {B1200, B2400, B4800, B9600};
  28.     static unsigned unix_parity[] = {0, PARENB & PARODD, PARENB};
  29.     hn_port *p = NULL;
  30.     FILE *in = NULL, *out = NULL;
  31.     int err = HN_ENONE;
  32.  
  33.     if ((in = fopen(dev, "r")) == NULL)
  34.         err = HN_EDEVNAME;
  35.     else if ((out = fopen(dev, "w")) == NULL)
  36.         err = HN_EDEVNAME;
  37.     else if (bps >= DIM(unix_bps))
  38.         err = HN_EBPS;
  39.     else if (par >= DIM(unix_parity))
  40.         err = HN_EPARITY;
  41.     else if (data_bits != 7 && data_bits != 8)
  42.         err = HN_EDATABITS;
  43.     else if (stop_bits != 1 && stop_bits != 2)
  44.         err = HN_ESTOPBITS;
  45.     if (err == HN_ENONE)
  46.         {
  47.         t.c_cflag |= unix_bps[bps];
  48.         t.c_cflag |= unix_par[parity];
  49.         t.c_cflag |= (data_bits == 7) ? CS7 : CS8;
  50.         if (stop_bits == 2)
  51.             t.c_cflag |= CSTOPB;
  52.         if (ioctl(fileno(in), TCSETA, &t) == -1)
  53.             err = HN_ENOTSETUP;
  54.         else
  55.             {
  56.             setbuf(in, NULL);
  57.             if ((p = malloc(sizeof(hn_port))) == NULL)
  58.                 err = HN_ENOPORTS;
  59.             }
  60.         }
  61.     if (err != HN_ENONE)
  62.         {
  63.         if (in != NULL)
  64.             fclose(in);
  65.         if (out != NULL)
  66.             fclose(out);
  67.         if (p != NULL)
  68.             free(p);
  69.         hn_errno = err;
  70.         return NULL;
  71.         }
  72.     p->in = in;
  73.     p->out = out;
  74.     p->errno = HN_ENONE;
  75.     return p;
  76.     }
  77.  
  78. int hn_pclose(hn_port *p)
  79.     {
  80.     if (p != NULL)
  81.         {
  82.         if (fclose(p->in) == EOF | fclose(p->out) == EOF)
  83.             {
  84.             hn_errno = HN_ECANTCLOSE;
  85.             return EOF;
  86.             }
  87.         free(p);
  88.         }
  89.     return 0;
  90.     }
  91.  
  92. int hn_penable(hn_port *p)
  93.     {
  94.     return 0;
  95.     }
  96.  
  97. int hn_pdisable(hn_port *p)
  98.     {
  99.     return 0;
  100.     }
  101.  
  102. int hn_pgetc(hn_port *p)
  103.     {
  104.     int c;
  105.  
  106.     if ((c = fgetc(p->in)) != EOF)
  107.         return c;
  108.     if (ferror(p->in))
  109.         hn_errno = p->errno = HN_ECANTGET;
  110.     return EOF;
  111.     }
  112.  
  113. int hn_pputc(int c, hn_port *p)
  114.     {
  115.     if (fputc(c, p->out) != EOF)
  116.         return c;
  117.     if (ferror(p->out))
  118.         hn_errno = p->errno = HN_ECANTPUT;
  119.     return EOF;
  120.     }
  121.  
  122. int hn_pflush(hn_port *p)
  123.     {
  124.     if (fflush(p->out) == 0)
  125.         return 0;
  126.     hn_errno = p->errno = HN_ECANTFLUSH;
  127.     return EOF;
  128.     }
  129.  
  130.