home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1630 / query.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.1 KB  |  112 lines

  1. /* query.c - send query strings to the terminal and listen for answer.
  2.  
  3.    Author: Craig Bishop
  4.    Modified by David MacKenzie
  5.    loosely based on a the original program by Michael Cooper. */
  6.  
  7. #include <stdio.h>
  8. #include <setjmp.h>
  9. #include <signal.h>
  10.  
  11. #include "qterm.h"
  12.  
  13. jmp_buf env;
  14.  
  15. /* Query the terminal and return a pointer to the table entry for the
  16.    terminal type that it identifies itself as, or NULL if the terminal
  17.    type is not recognized. */
  18.  
  19. struct qt *
  20. queryterm ()
  21. {
  22.   static int firsttime = TRUE;
  23.   register struct qt **qtpp;
  24.   register struct qt *prevqtp;
  25.   struct qt *term;
  26.  
  27.   fflush (stdin);
  28.  
  29.   prevqtp = *termtab;
  30.   for (qtpp = termtab; *qtpp; prevqtp = *qtpp++)
  31.     {
  32.       if (firsttime || strcmp ((*qtpp)->sendstr, prevqtp->sendstr) != STREQUAL)
  33.     {
  34.       firsttime = FALSE;
  35.  
  36.       fputs ((*qtpp)->sendstr, stderr);
  37.  
  38.       if (term_listen ())
  39.         {
  40.           term = findmatch (recvbuf, qtpp);
  41.           if (term != NULLQT)
  42.         return term;
  43.         }
  44.     }
  45.     }
  46.  
  47.   return NULLQT;
  48. }
  49.  
  50. /* Listen for a response from the terminal.  Time out after WAITSEC
  51.    seconds.  Return a pointer to the string the terminal responded with. */
  52.  
  53. char *
  54. term_listen ()
  55. {
  56.   register int i;
  57.  
  58.   if (setjmp (env))
  59.     {
  60.       fflush (stdin);
  61.       return recvbuf;
  62.     }
  63.   signal (SIGALRM, wakeup);
  64.  
  65.   for (i = 0; i < FIELDSIZ - 1; recvbuf[++i] = 0)
  66.     {
  67.       alarm (WAITSEC);
  68.       recvbuf[i] = getch ();
  69.       alarm (0);
  70.     }
  71.  
  72.   fflush (stdin);
  73.   return recvbuf;
  74. }
  75.  
  76. /* If STR matches any of the response strings for the recvstr in TTSTART,
  77.    return a pointer to that table entry, otherwise NULL. */
  78.  
  79. struct qt *
  80. findmatch (str, ttstart)
  81.      register char *str;
  82.      register struct qt **ttstart;
  83. {
  84.   register char *ssp = (*ttstart)->sendstr;
  85.  
  86.   for (; *ttstart && strcmp (ssp, (*ttstart)->sendstr) == STREQUAL; ttstart++)
  87.     if (strcmp (str, (*ttstart)->recvstr) == STREQUAL)
  88.       return *ttstart;
  89.  
  90.   return NULLQT;
  91. }
  92.  
  93. /* Return a character from the terminal with parity bits stripped off. */
  94.  
  95. char
  96. getch ()
  97. {
  98.   char c;
  99.  
  100.   if (read (0, &c, 1) > 0)
  101.     return c & CMASK;
  102.   return 0;
  103. }
  104.  
  105. /* SIGALRM handler. */
  106.  
  107. void
  108. wakeup ()
  109. {
  110.   longjmp (env, 1);
  111. }
  112.