home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCP_Blars.lzh / dcpio.c < prev    next >
Text File  |  1992-01-25  |  6KB  |  338 lines

  1. /*
  2.  * "DCP" a uucp clone. Copyright Richard H. Lamb 1985,1986,1987
  3.  * IO routines
  4.  * Ported to OS-9/68000:  Wolfgang Ocker    January 1988
  5.  */
  6.  
  7. #include <sgstat.h>
  8. #include <signal.h>
  9.  
  10. #include "dcp.h"
  11.  
  12. #ifndef min
  13. #define min(a,b) ((a<b)?(a):(b))
  14. #endif
  15.  
  16. extern int I_HAVE_HAYES;
  17.  
  18. struct _sgs ttybufa, ttybufb;
  19.  
  20. static short rflag;
  21. int          fpr, fpw;
  22.  
  23. static int line_baud;
  24.  
  25. static int byte_rates[] = { 5, 7, 11, 13, 15, 30, 60, 120, 180,
  26.                             200, 240, 360, 480, 720, 960, 1920 } ;
  27.  
  28. static char last_device[40] = "";
  29.  
  30.  
  31. void clkint(signum)
  32. {
  33.   switch (signum) {
  34.     case SIG_DATAREADY:
  35.       rflag = TRUE;
  36.       break;
  37.     case SIG_CARRIER:
  38.       ;
  39.     default:
  40.       ;
  41.   }
  42. }
  43.  
  44.  
  45. setline(tty, baud)
  46.   int tty, baud;
  47. {
  48.   _gs_opt(tty, &ttybufa);
  49.   ttybufb = ttybufa;
  50.  
  51.   if (baud < 0) {
  52.     ttybufa._sgs_parity = D8S1NONE | 0x80;      /* RTS/CTS */
  53.     baud = -baud;
  54.     _ss_enrts(tty);
  55.   }
  56.   else
  57.     if (baud == 0)
  58.       ttybufa._sgs_parity = (ttybufa._sgs_parity & 0x80) | D8S1NONE;
  59.     else
  60.       ttybufa._sgs_parity = D8S1NONE;
  61.  
  62.   ttybufa._sgs_echo   = 0;
  63.   ttybufa._sgs_pause  = 0;
  64.   ttybufa._sgs_case   = 0;
  65.   ttybufa._sgs_backsp = 0;
  66.   ttybufa._sgs_delete = 0;
  67.   ttybufa._sgs_alf    = 0;
  68.   ttybufa._sgs_nulls  = 0;
  69.   ttybufa._sgs_bspch  = 0;
  70.   ttybufa._sgs_dlnch  = 0;
  71.   ttybufa._sgs_eorch  = 0;
  72.   ttybufa._sgs_eofch  = 0;
  73.   ttybufa._sgs_rlnch  = 0;
  74.   ttybufa._sgs_dulnch = 0;
  75.   ttybufa._sgs_psch   = 0;
  76.   ttybufa._sgs_kbich  = 0;
  77.   ttybufa._sgs_kbach  = 0;
  78.   ttybufa._sgs_bsech  = 0;
  79.   ttybufa._sgs_bellch = 0;
  80.   ttybufa._sgs_xon    = 0;
  81.   ttybufa._sgs_xoff   = 0;
  82.   ttybufa._sgs_tabcr  = 0;
  83.  
  84.   if (baud) {
  85.     ttybufa._sgs_baud = baud;
  86.     line_baud         = baud;
  87.   }
  88.   else
  89.     line_baud = ttybufa._sgs_baud;
  90.  
  91.   _ss_opt(tty, &ttybufa);
  92. }
  93.  
  94.  
  95. swrite(data, num)
  96.   int           num;
  97.   unsigned char *data;
  98. {
  99.   write(fpw, data, num);
  100.   printmsg(10, "swrite: \"%.*s\"", num, data);
  101.   return(num);
  102. }
  103.  
  104. /*
  105.  * non-blocking read essential to "g" protocol
  106.  * see "dcpgpkt.c" for description
  107.  * Requests for I/O should get qued and an event flag given. Then the
  108.  * REQUESTING process (e.g.gmachine()) waits for the event
  109.  * flag to fire processing either a read or a write.
  110.  */
  111.  
  112. sread(buf, num, timeout)
  113.   register int           num;
  114.   register unsigned char *buf;
  115.   int                    timeout;
  116. {
  117.   register int ticks, wait_ticks;
  118.   register int to_read;
  119.   static int   clk_tck = -1;
  120.   static int   sig_not_init = TRUE;
  121.  
  122.   if (timeout <= 0)
  123.     if (_gs_rdy(fpr) < num)
  124.       return(0);
  125.     else
  126.       return(read(fpr, buf, num));
  127.  
  128.   if (sig_not_init) {
  129.     intercept(clkint);
  130.     sig_not_init = FALSE;
  131.  
  132.     clk_tck = CLK_TCK;
  133.   }
  134.  
  135.   ticks = timeout * clk_tck;
  136.  
  137.   while (ticks > 0) {
  138.     rflag = FALSE;
  139.     if ((to_read = _gs_rdy(fpr)) >= num)
  140.       return(read(fpr, buf, num));
  141.  
  142.     to_read    = num - to_read;      /* Anzahl zu lesender Zeichen */
  143.     wait_ticks = to_read * clk_tck  / byte_rates[line_baud];
  144.  
  145.     if (wait_ticks <= 10) {
  146.       _ss_ssig(fpr, SIG_DATAREADY);
  147.  
  148.       sigmask(1);
  149.       if (rflag) {
  150.         _ss_rel(fpr);
  151.         sigmask(-1);
  152.         continue;
  153.       }
  154.  
  155.       ticks = tsleep(ticks);
  156.       _ss_rel(fpr);
  157.     }
  158.     else
  159.       ticks = tsleep(min(ticks, wait_ticks));
  160.   }
  161.  
  162.   if (_gs_rdy(fpr) >= num)
  163.     return(read(fpr, buf, num));
  164.  
  165.   return(0);
  166. }
  167.  
  168. #ifdef  NOTDEF    
  169.   /*
  170.    * To realize a timeout under OS-9 we must use the _ss_ssig()
  171.    * PutStat and a sleep() 
  172.    */
  173.   for (i = 0; i < num; ) {
  174.     while ((_gs_rdy(fpr) > 0) && (i < num)) {
  175.       read(fpr, &c, 1);
  176.       buf[i++] = c;
  177.     }
  178.  
  179.     if (i < num) {
  180.       rflag = FALSE;
  181.       _ss_ssig(fpr, SIG_DATAREADY);
  182.       tsleep(1);
  183.       if (!rflag && (_gs_rdy(fpr) <= 0) && !rflag) {
  184.         if (sleep(timeout) == 0)
  185.           if (!rflag && (_gs_rdy(fpr) <= 0) && !rflag) {
  186.             printmsg(1, "Time out (%d)!!!", timeout);
  187.             _ss_rel(fpr);
  188.             return(i);
  189.           }
  190.       }
  191.       if (!rflag)
  192.         _ss_rel(fpr);
  193.     }
  194.   }
  195.  
  196.   return(i);
  197. }
  198. #endif
  199.  
  200.  
  201. openline(name, speed)
  202.   char *name;
  203.   char *speed;
  204. {
  205.   if (name != NULL)
  206.     if (last_device[0] != '\0' && strcmp(last_device, name)) {
  207.       last_device[0] = '\0';
  208.     }
  209.  
  210.   printmsg(8, "open line %s, speed %s", (name ? name : "NULL"),
  211.            (speed ? speed : "NULL"));
  212.  
  213.   if (name == NULL)
  214.     return(openrline());
  215.   else
  216.         strcpy(last_device, name);
  217.  
  218.   if ((fpr = open(name, S_IREAD | S_IWRITE | S_ISHARE)) < 0)
  219.     return(-1);
  220.  
  221.   fpw = fpr;
  222.  
  223.   setline(fpw, getbaud(speed));
  224.  
  225.   return(0);
  226. }
  227.  
  228. openrline()
  229. {
  230.   fpw = 1;
  231.   fpr = 0;
  232.  
  233.   setline(fpw, 0);
  234.   setline(fpr, 0);
  235. }
  236.  
  237. /* closeline()
  238. {
  239.   if (fpr != -1) {
  240.     _ss_opt(fpr, &ttybufb);
  241.     close(fpr);
  242.   }
  243.   
  244.   if (fpw != -1) {
  245.     _ss_opt(fpw, &ttybufb);
  246.     close(fpw);
  247.   }
  248. }
  249. */
  250.  
  251. closeline()
  252. {
  253.   if (I_HAVE_HAYES && (fpw != -1)) {
  254.     sleep(2);
  255.     write(fpw, "+++", 3);
  256.     sleep(2);
  257.     write(fpw, "\raaaatz\r", 8);
  258.     sleep(2);
  259.   }
  260.       
  261.     last_device[0] = '\0';
  262.  
  263.   if (fpr != -1) {
  264.     _ss_opt(fpw, &ttybufb);
  265.     close(fpr);
  266.   }
  267.  
  268.   if (fpw != -1 && fpw != fpr) {
  269.     _ss_opt(fpr, &ttybufb);
  270.     close(fpw);
  271.   }
  272. }
  273.  
  274. #define ABS(x) ((x) < 0 ? -(x) : (x))
  275.  
  276. int SIOSpeed(s)
  277.   char *s;
  278. {
  279.   int baud;
  280.   
  281.   if (baud = ABS(getbaud(s))) {
  282.     ttybufa._sgs_baud = baud;
  283.     _ss_opt(fpw, &ttybufa);
  284.     _ss_opt(fpr, &ttybufa);
  285.   }
  286. }
  287.  
  288.  
  289. struct {
  290.   int  baudrate;
  291.   char *code;
  292. } rates[] = { {B19200, "19200"},
  293.               {B9600,  "9600"},
  294.               {B4800,  "4800"},
  295.               {B2400,  "2400"},
  296.               {B1200,  "1200"},
  297.               {B300,   "300"} };
  298.  
  299.  
  300. int getbaud(s)
  301.   char *s;
  302. {
  303.   int  i, baud;
  304.   char c;
  305.   char stmp[100];
  306.  
  307.   
  308.   if (s == NULL)
  309.     return(0);
  310.  
  311.   strcpy(stmp, s);
  312.   s = stmp;
  313.  
  314.   c = s[strlen(s)-1];
  315.  
  316.   if (!isdigit(c))
  317.     s[strlen(s)-1] = '\0';
  318.  
  319.   i = baud = 0;
  320.  
  321.   do {
  322.     if (strcmp(s, rates[i].code) == 0) {
  323.       baud = rates[i].baudrate;
  324.       break;
  325.     }
  326.   } while (rates[++i].baudrate != 0);
  327.  
  328.   if (baud == 0)
  329.     baud = B1200;
  330.  
  331.   if (c == 'h' || c == 'H' || c == 'r' || c == 'R')
  332.     baud = -baud;
  333.  
  334.   return(baud);
  335. }
  336.  
  337.    
  338.