home *** CD-ROM | disk | FTP | other *** search
- /* query.c - send query strings to the terminal and listen for answer.
-
- Author: Craig Bishop
- Modified by David MacKenzie
- loosely based on a the original program by Michael Cooper. */
-
- #include <stdio.h>
- #include <setjmp.h>
- #include <signal.h>
-
- #include "qterm.h"
-
- jmp_buf env;
-
- /* Query the terminal and return a pointer to the table entry for the
- terminal type that it identifies itself as, or NULL if the terminal
- type is not recognized. */
-
- struct qt *
- queryterm ()
- {
- static int firsttime = TRUE;
- register struct qt **qtpp;
- register struct qt *prevqtp;
- struct qt *term;
-
- fflush (stdin);
-
- prevqtp = *termtab;
- for (qtpp = termtab; *qtpp; prevqtp = *qtpp++)
- {
- if (firsttime || strcmp ((*qtpp)->sendstr, prevqtp->sendstr) != STREQUAL)
- {
- firsttime = FALSE;
-
- fputs ((*qtpp)->sendstr, stderr);
-
- if (term_listen ())
- {
- term = findmatch (recvbuf, qtpp);
- if (term != NULLQT)
- return term;
- }
- }
- }
-
- return NULLQT;
- }
-
- /* Listen for a response from the terminal. Time out after WAITSEC
- seconds. Return a pointer to the string the terminal responded with. */
-
- char *
- term_listen ()
- {
- register int i;
-
- if (setjmp (env))
- {
- fflush (stdin);
- return recvbuf;
- }
- signal (SIGALRM, wakeup);
-
- for (i = 0; i < FIELDSIZ - 1; recvbuf[++i] = 0)
- {
- alarm (WAITSEC);
- recvbuf[i] = getch ();
- alarm (0);
- }
-
- fflush (stdin);
- return recvbuf;
- }
-
- /* If STR matches any of the response strings for the recvstr in TTSTART,
- return a pointer to that table entry, otherwise NULL. */
-
- struct qt *
- findmatch (str, ttstart)
- register char *str;
- register struct qt **ttstart;
- {
- register char *ssp = (*ttstart)->sendstr;
-
- for (; *ttstart && strcmp (ssp, (*ttstart)->sendstr) == STREQUAL; ttstart++)
- if (strcmp (str, (*ttstart)->recvstr) == STREQUAL)
- return *ttstart;
-
- return NULLQT;
- }
-
- /* Return a character from the terminal with parity bits stripped off. */
-
- char
- getch ()
- {
- char c;
-
- if (read (0, &c, 1) > 0)
- return c & CMASK;
- return 0;
- }
-
- /* SIGALRM handler. */
-
- void
- wakeup ()
- {
- longjmp (env, 1);
- }
-