home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / bss / pup.arc / MODEMIO.C < prev    next >
C/C++ Source or Header  |  1987-11-09  |  13KB  |  544 lines

  1. #include <puppy.h>
  2. #include <ascii.h>
  3. #include <pupmem.h>
  4.  
  5. /*
  6.     These are all "medium low" level I/O for Puppy. There shouldnt be
  7. any DOS dependencies in here; the hardware drivers do that. These call the
  8. "generic" hardware as described in the interface docs.
  9.  
  10.     They may seem large for just a bunch of I/O routines, but there are
  11. things in here that eliminate many so-called "high-level" functions such as
  12. word wrap, command-ahead and input buffer control, etc. 
  13.  
  14.  
  15.     T. Jennings
  16.     Fido Software
  17.     164 Shipley
  18.     San Francisco CA 94107
  19.  
  20.     (k) all rights reversed
  21.  
  22.  
  23.     All of the following functions call functions from the same
  24.     list, or ones below it. 
  25.  
  26.  
  27.     Puppy's Console I/O. These will log the caller off due to
  28.     time limits, inactivity for 10 minutes or carrier loss. 
  29.  
  30. getargs(prompt)    Displays the prompt, returns a pointer to the inputted string.
  31. isargs()    Return true if there are more args that could be read.
  32. input(prompt,buff,length) Displays a prompt, inputs a line of text. This is
  33.         the thing that does the actual prompting and input when
  34.         getargs runs out of things to return.
  35. ask(ques)    Prints a string, inputs Y or N, returns true if Y. This
  36.         does a nice neat question input.
  37. cmdflush()    Flush any command-ahead.
  38.  
  39.     These do "formatted" I/O; printf() type stuff, word wrap, etc.
  40.  
  41. mprintf()    Same as PRINTF, except goes to the modem. 
  42. mputs()        Put a string to the modem.
  43. fmconout(c)    Outputs characters to the console, wrapping words to fit
  44.         within the set screen width. NOTE: You must issue a ^Z
  45.         after the last character to flush out any characters
  46.         stuck in the word buffer. 
  47.  
  48.     These are "plain" console I/O.
  49.  
  50. mconout(c)    Output C to the console. Maintains line/column
  51.         cursor position, handles control codes, etc.
  52. mconin()    Returns a character from the console. 
  53. mconstat()    Return true if a character available.
  54. mconflush()    Throw away all keyboard input.
  55. ques(q)        A low level question asker: accepts various things
  56.         for input. Does minimal cursor motion & cleanup.
  57.  
  58.     Puppy's Modem I/O. These will log the caller off if carrier
  59.     is lost. These do no control code processing or other
  60.     console type functions.
  61.  
  62. modin(n)    Returns character available, else -1 if N centiseconds elapse.
  63. modout(c)    Outputs C to the modem.
  64. modstat()    Returns true if a modem character available.
  65.  
  66.     Puppy's low level I/O support functions.
  67.  
  68. limitchk()    Checks the set time limit, and logs off if exceeded.
  69. flush(n)    Flush the modem until it is clear for n centiseconds.
  70. cd()        Returns true if Carrier Detect is true or cd_flag is true.
  71. carrierchk()    Checks for modem carrier via cd() and logs off if not present.
  72. limitchk()    Checks caller time limits in effect and logs off if exceeded.
  73.  
  74.     Various non-I/O support functions.
  75.  
  76. delim(c)    Returns true if the character is a delimiter.
  77.  
  78. */
  79.  
  80. /* NOTE: There is a subtle kludge here that enables the detection of 
  81. incoming Crash Mail.
  82.  
  83.     The keyboard input routines in BUFIO.C (mconstat(),
  84. mconin(), etc) strip parity of incoming characters, EXCEPT for TSYNC.
  85.  
  86.     If input() detects a TSYNC at anytime, it clears any
  87. input it may have accumulated and returns a string with the single
  88. character TSYNC in it. 
  89.  
  90.     If getarg() detects a TSYNC in its input, it immediately
  91. returns a pointer to the TSYNC.
  92.  
  93.     All this for Crash Mail. I hate this kludge.
  94.  
  95. */
  96.  
  97. static char line_buff[SS] = "";        /* command line buffer */
  98. static char *_nargp = line_buff;    /* pointer into it */
  99.  
  100. /* These are used by fmconout() */
  101.  
  102. static char word[SS];            /* word we're building */
  103. static char wordi = 0;            /* index/length of word */
  104. #define MARGIN 2            /* right margin on screen */
  105.  
  106. /* This buffer lets Puppy do typeahead even on systems that
  107. dont have interrupts. The input is polled during output & input
  108. and any characters are stored away in here. Input requests check the
  109. buffer and do further polling. */
  110.  
  111. static char conbuf[SS];            /* console type ahead buffer */
  112. static int head = 1;            /* indices into it */
  113. static int tail = 0;
  114.  
  115. /* Main command line input. This function returns a pointer to the
  116. next available argument, if none is available, it displays the prompt
  117. and inputs a string from the console. */
  118.  
  119. char *getarg(prompt)
  120. char *prompt;
  121. {
  122. char *cp;
  123.  
  124.     if (! isargs()) {            /* if line empty, */
  125.         input(prompt,line_buff,sizeof(line_buff)); /* get some input, */
  126.         _nargp= skip_delim(line_buff);    /* init the arg pointer */
  127.     }
  128.     cp= _nargp;                /* ptr to current arg */
  129.     _nargp= next_arg(_nargp);        /* skip to the next one, */
  130.     return(cp);
  131. }
  132. /* Return true if there are any args left. */
  133.  
  134. isargs() {
  135.  
  136.     return(num_args(_nargp));
  137. }
  138.  
  139. /* Flush the command buffer */
  140.  
  141. cmdflush() {
  142.  
  143.     _nargp= line_buff;
  144.     *_nargp= NUL;
  145. }
  146.  
  147. /* Ask a question, return true if "Y" pressed, 0 if "N". */
  148.  
  149. ask(s)
  150. char *s;
  151. {
  152. char c,cp,buff[SS];
  153.  
  154.     strcpy(buff,s);
  155.     strcat(buff,"? [Y,n]: ");
  156.  
  157.     while (1) {
  158.         switch (tolower(*getarg(buff))) {
  159.             case NUL: return(1);    /* CR is Yes */
  160.             case 'y': return(1);    /* Y is Yes */
  161.             case 'n': return(0);    /* N is No */
  162.         }
  163.         cmdflush();            /* else garbage */
  164.     }
  165. }
  166.  
  167. /* Ask a question, get a "hot" (single key) answer. Must ignore ^S and ^Q, 
  168. as it may get things all out of sync. */
  169.  
  170. ques(q)
  171. char *q;
  172. {
  173.     line= 0;            /* AVOID INFINITE RECURSION!!! */
  174.     while (*q) mconout(*q++);    /* output directly, */
  175.  
  176.     while (1) {
  177.         switch (mconin()) {
  178.             case XOF:    /* ignore Control-S */
  179.             case XON:    /* and Control-Q */
  180.             case ACK:    /* Ignore Control-F */
  181.                 break;
  182.  
  183.             case 'n':
  184.             case 'N':
  185.             case ETX:
  186.             case VT:
  187.                 return(0);
  188.                 break;
  189.  
  190.             case ' ':
  191.             case 'y':
  192.             case 'Y':
  193.             case CR:
  194.                 return(1);
  195.                 break;
  196.  
  197.             default: mconout(BEL); break;
  198.         }
  199.     }
  200. }
  201.  
  202. /* Issue a prompt, get a line of input. */
  203.  
  204. input(prompt,buff,len)
  205. char *prompt,*buff;
  206. int len;
  207. {
  208. char c;
  209. int n,i;
  210.  
  211.     mputs(prompt);                /* output the prompt, */
  212.     i= 0;
  213.     line= 0;                /* reset "more" */
  214.     while (1) {
  215.         c= mconin();            /* get a key, */
  216.         switch (c) {
  217.             case TSYNC:        /* KLUDGE */
  218.                 i= 0;        /* clear buffer ... */
  219.                 buff[i]= NUL;
  220.                 return(i);
  221.  
  222.             case CR:        /* end of input */
  223.                 buff[i]= NUL;
  224.                 mputs("\r\n");
  225.                 abort= 0;
  226.                 return(i);
  227.  
  228.             case BS:        /* delete character */
  229.             case DEL:
  230.                 n= 1; goto del;
  231.             case CAN:        /* delete line */
  232.             case ETX:
  233.             case VT:
  234.                 n= i;
  235. del:;                while (n && i) {
  236.                     mconout(BS);
  237.                     mconout(' ');
  238.                     mconout(BS);
  239.                     --n; --i;
  240.                 }
  241.                 break;
  242.  
  243.             case ACK: break;    /* ignore Control-F */
  244.  
  245.             default:        /* insert character */
  246.                 if ((c >= ' ') && (c <= '~') && (i < len-1)) {
  247.                     buff[i++]= c;
  248.                     mconout(c);
  249.  
  250.                 } else mconout(BEL);
  251.                 break;
  252.         }
  253.     }
  254. }
  255.  
  256. /* Output characters to the screen, building words and wrapping them
  257. as necessary to fit within the current width. */
  258.  
  259. fmconout(c)
  260. char c;
  261. {
  262.     if (c >= ' ') {                /* printable chars */
  263.         word[wordi++]= c;        /* build a word */
  264.         if ((wordi + MARGIN < width) && !delim(c)) return;
  265.     }
  266.     word[wordi]= NUL;            /* terminate it for output */
  267.  
  268. /* End of a word, word too large, or a control character. */
  269.  
  270.     if (column + wordi + MARGIN > width) {    /* if word is too long, */
  271.         mconout(CR);            /* go to next line first */
  272.         mconout(LF);
  273.     }
  274.     if (*word) {                /* if there is a word there, */
  275.         for (wordi= 0; word[wordi]; wordi++)
  276.             mconout(word[wordi]);    /* output it */
  277.         wordi= 0;             /* now its gone */
  278.     }
  279.     if ((c < ' ') && (c != SUB)) mconout(c); /* do control chars */
  280. }
  281.  
  282. /* Output a character to the console, and poll for ^C, etc. */
  283.  
  284. mconout(c)
  285. char c;
  286. {
  287.     if (mconstat() == XOF) {        /* if a pause, */
  288.         while (mconin() == XOF);    /* wait for anything but XOF */
  289.     }
  290.  
  291.     modout(c);                /* to the modem, */
  292.     if (c && (c != BEL)) lconout(c);    /* one to console if not bell */
  293.  
  294.     if (c >= ' ') ++column;            /* "cursor" motion */
  295.     else if (c == CR) column= 0;
  296.     else if (c == LF) {            /* next line */
  297.         ++line;
  298.         if (line >= rows - 1) {        /* screen pause, set abort */
  299.             line= 0;        /* reset it, */
  300.             if (pause) {        /* if line pause enabled */
  301.                 mconflush();    /* flush typeahead, */
  302.                 abort= ! ques(" <- More\r");    
  303.             }
  304.         }
  305.     }
  306. }
  307.  
  308. /* Wait for a character from the keyboard. Check for activity, log the guy 
  309. off if he times out. */
  310.  
  311. #define ONEMIN (60L * 1000L)            /* milliseconds in one minute */
  312. #define EIGHTMIN (ONEMIN * 8L)
  313. #define TENMIN (ONEMIN * 10L)            /* ten minutes */
  314.  
  315. mconin() {
  316. FLAG warned;
  317.  
  318.     millisec= 0L;                /* clear the timer, */
  319.     warned= 0;                /* not warned yet */
  320.     while (! mconstat()) {
  321.         if ((millisec > EIGHTMIN) && (!warned)) {
  322.             mputs("\r\nPup says: \07\"I'll hang up on you if you sit idle too long!\"\r\n");
  323.             ++warned;
  324.         }
  325.         if (millisec > TENMIN) {
  326.             mputs("\r\nPup says: \"You were warned!\"\r\n");
  327.             logoff(0,1);
  328.         }
  329.     }
  330.     tail= ++tail % sizeof(conbuf);
  331.     return( (int) conbuf[tail]);
  332. }
  333.  
  334. /* Return 0 if no key available, else the key. */
  335.  
  336. mconstat() {
  337.  
  338. int t;
  339.     pollkbd();                /* poll for chars, */
  340.     t= (tail + 1) % sizeof(conbuf);        /* wrap pointer, */
  341.     return((t == head) ? 0 : conbuf[t]);    /* the char or 0 */
  342. }
  343.  
  344. /* Poll both keyboards, stuff characters into the ring buffer. This handles
  345. all background tasks, like type mode, etc. NOTE: This strips parity off
  346. all incoming characters EXCEPT TSYNC! This lets us support Continuous Mail. */
  347.  
  348. pollkbd() {
  349.  
  350. char c;
  351.  
  352.     limitusr();                /* maybe forced logoff */
  353.  
  354.     if (! test) {                /* if online, */
  355.         carrierchk();            /* watch carrier! */
  356.         if (_mconstat()) {        /* if modem character there */
  357.             c= _mconin();        /* read it, */
  358.             if (c != TSYNC) c &= 0x7f;
  359.             put_c(c);        /* use it */
  360.         }
  361.  
  362.     } else {
  363.         c= keyhit();
  364.         if (c) put_c(c);        /* else just local input */
  365.     }
  366. }
  367.  
  368. /* Put this character into the ring buffer, process the special
  369. control characters as necessary. */
  370.  
  371. put_c(c)
  372. char c;
  373. {
  374.     switch (c) {
  375.         case ETX:            /* Control-C */
  376.         case VT:            /* Control-K */
  377.             abort= 1;        /* flag as an abort, */
  378.         case TSYNC:            /* Continuous Mail ... */
  379.         case XOF:             /* Control-S */
  380.         case ACK:            /* Control-F */
  381.             mconflush();        /* flush typeahead, */
  382.             cmdflush();        /* flush command buffer, */
  383.             break;
  384.  
  385.         case NUL: return;        /* ignore NULs */
  386.     }
  387.     if (head != tail) {            /* if room, install it */
  388.         conbuf[head]= c;
  389.         head= ++head % sizeof(conbuf);
  390.     }
  391. }
  392.  
  393. /* Flush the console buffer. */
  394.  
  395. mconflush() {
  396.  
  397.     while (mconstat()) mconin();        /* flush all characters, */
  398. }
  399.  
  400. /* Formatted print to the modem. */
  401.  
  402. mprintf(f)
  403. char *f;
  404. {
  405. char buf[500];
  406.  
  407.     _spr(buf,&f);
  408.     mputs(buf);
  409. }
  410.  
  411. /* Output a string to the modem. */
  412.  
  413. mputs(s)
  414. char *s;
  415. {
  416.     while (*s) fmconout(*s++);
  417.     fmconout(SUB);                /* flush the word buffer */
  418. }
  419.  
  420. /* Watch the time limits set, and give warnings when the limit comes 
  421. up. When the limit reaches 0, display a message and log the caller off. */
  422.  
  423. limitusr() {
  424. int n;
  425.  
  426.     n= limit - minutes;            /* time left */
  427.  
  428.     if (limit == 0) {            /* if no limit, */
  429.         return;                /* do nothing */
  430.  
  431.     } else if (n < 1) {            /* time is over */
  432.         if (lmtstate < 3) {
  433.             lmtstate= 3;        /* only once */
  434.             limit= 0;        /* DISABLE LIMITS */
  435.             mprintf("\r\n\r\n\07 Pup Says: \"Your time is up!\"\r\n\r\n");
  436.             logoff(0,1);        /* disconnect */
  437.         }
  438.  
  439.     } else if (n < 2) {            /* final PUP SAYS: */
  440.         if (lmtstate < 2) {
  441.             lmtstate= 2;        /* do only once */
  442.             mprintf("\r\n\r\n\07Pup Says: \"I'm gonna hang up on you in 60 seconds!\"r\n\r\n");
  443.         }
  444.  
  445.     } else if (n < 5) {            /* two minute PUP SAYS: */
  446.         if (lmtstate < 1) {        /* if no PUP SAYS: yet, */
  447.             lmtstate= 1;        /* remember this, BEFORE THE MSG */
  448.             mprintf("\r\n\07Pup Says: \"You have %d minutes left!\"\r\n",n);
  449.         }
  450.     }
  451. }
  452.  
  453. /* Check the time limit, and if over, bump the guy off. */
  454.  
  455. limitchk() {
  456.  
  457.     if (limit && (minutes >= limit)) logoff(0,1);
  458. }
  459. /* Output to the modem. */
  460.  
  461. modout(c)
  462. char c;
  463. {
  464.     if (test) return;
  465.  
  466.     carrierchk();                /* always watch this */
  467.     _mconout(c);                /* limit will get checked eventually */
  468. }
  469. /* Get a character from the modem, with a maximum wait time, in
  470. centiseconds. Returns -1 if timeout. */
  471.  
  472. modin(n)
  473. unsigned n;
  474. {
  475. long dly;
  476.  
  477.     if (test) return(-1);        /* cant in test mode */
  478.  
  479.     if (_mconstat()) return(_mconin() & 0xff); /* why wait? */
  480.  
  481.     dly= n * 10L;            /* time, in milliseconds */
  482.  
  483.     millis2= 0L;            /* clear the ticker, */
  484.     while (millis2 < dly) {
  485.         carrierchk();        /* watch disconnect, */
  486.         if (_mconstat())     /* if a char avail, return it. */
  487.             return(_mconin() & 0xff);
  488.     }
  489.     return(-1);            /* timeout */
  490. }
  491.  
  492. /* Delay N centiseconds. Do nothing in the mean time. */
  493.  
  494. delay(n)
  495. int n;
  496. {
  497. long dly;
  498.  
  499.     dly= n * 10L;
  500.     millisec= 0L;
  501.     while (millisec < dly);
  502. }
  503.  
  504. /* Logoff if carrier is lost. */
  505.  
  506. carrierchk() {
  507.  
  508.     if (! cd()) logoff(0,1);
  509. }
  510.  
  511. /* Return true if carrier detect is true or "ignore CD" is true. */
  512.  
  513. cd() {
  514.  
  515.     if (cd_flag || test) return(1);    /* fake on-line */
  516.     return(_cd());            /* real online */
  517. }
  518.  
  519. /* Flush the modem until its quiet for N centiseconds or just flush
  520. whatever is there if n == 0. */
  521.  
  522. flush(n)
  523. int n;
  524. {
  525.     if (test) return;
  526.  
  527.     if (n) while (modin(n) != -1);
  528.     else while (_mconstat()) _mconin();
  529. }
  530.  
  531. /* Return true if the character is a delimiter. */
  532.  
  533. delim(c)
  534. char c;
  535. {
  536. int i;
  537.     switch (c) {
  538.         case ';': return(1);
  539.         case ' ': return(1);
  540.         case ',': return(1);
  541.         default: return(0);
  542.     }
  543. }
  544.