home *** CD-ROM | disk | FTP | other *** search
- /* qterm.c -- query the terminal to determine the terminal name.
- This is done by sending an enquiry string
- to the terminal and reading its response.
-
- Author: Craig Bishop
- modified by David MacKenzie
- loosly based on original program by Michael Cooper. */
-
- #include <stdio.h>
- #include <signal.h>
-
- #include "qterm.h"
-
- int user_table = FALSE; /* use user's own .qterm file */
- int user_table_only = FALSE; /* same, but don't add standard table */
- int quiet_mode = FALSE; /* quiet mode */
- int print_strings = FALSE; /* print strings */
-
- char querystr[FIELDSIZ];
- char recvbuf[FIELDSIZ];
- char *program_name;
- char *unterm = "dumb"; /* default terminal type */
-
- void
- usage ()
- {
- fprintf (stderr, "Usage: %s [-adefFqs] [-u unknown-term]\n",
- program_name);
- exit (1);
- }
-
- struct qt *termtab[MAXTERMS];
-
- void
- main (argc, argv)
- int argc;
- char **argv;
- {
- extern int optind;
- extern char *optarg;
- register int i;
- struct qt *qtp;
-
- program_name = argv[0];
-
- while ((i = getopt (argc, argv, "adefFqsu:")) != EOF)
- {
- switch (i)
- {
- case 'a':
- strncpy (querystr, ALTSEND, FIELDSIZ);
- break;
- case 'd':
- strncpy (querystr, DFLTSEND, FIELDSIZ);
- break;
- case 'e':
- strncpy (querystr, ABSEND, FIELDSIZ);
- break;
- case 'f':
- user_table = TRUE;
- break;
- case 'F':
- user_table_only = TRUE;
- break;
- case 'q':
- quiet_mode = TRUE;
- break;
- case 's':
- print_strings = TRUE;
- break;
- case 'u':
- unterm = optarg;
- break;
- default:
- usage ();
- }
- }
- if (optind < argc)
- usage ();
-
- if (!isatty (0))
- {
- fprintf (stderr, "%s: standard input must be a tty\n", program_name);
- exit (1);
- }
-
- if (mktable () == FALSE)
- exit (1);
-
- setterm ();
- signal (SIGHUP, catchint);
- signal (SIGINT, catchint);
- signal (SIGQUIT, catchint);
- setbuf (stdout, NULLSTR);
- setbuf (stderr, NULLSTR);
- qtp = queryterm ();
- fixterm ();
-
- if (qtp != NULLQT)
- prinfo (qtp);
- else
- unknown ();
-
- exit (0);
- }
-
-
- /* Signal handler; clean up and die. */
-
- void
- catchint ()
- {
- fixterm ();
- exit (1);
- }
-
- /* Announce which terminal type we think we have. */
-
- void
- prinfo (ttent)
- register struct qt *ttent;
- {
- register int len;
-
- if (print_strings)
- {
- len = strlen (recvbuf);
- fprintf (stderr, "%s: received %d character%s: %s\n",
- program_name, len, len == 1 ? "" : "s",
- decode (recvbuf));
- }
- if (!quiet_mode)
- {
- fprintf (stderr, "Terminal recognized as %s", ttent->termname);
- if (*ttent->fullname)
- fprintf (stderr, " (%s)\n", ttent->fullname);
- else
- fprintf (stderr, "\n");
- }
- puts (ttent->termname);
- }
-
- /* Announce that we don't know what type of terminal we have. */
-
- void
- unknown ()
- {
- register int len;
-
- if (print_strings)
- {
- len = strlen (recvbuf);
- fprintf (stderr, "%s: received %d character%s",
- program_name, len, len == 1 ? "" : "s");
- if (len)
- fprintf (stderr, ": %s\n", decode (recvbuf));
- else
- fputs ("\n", stderr);
- }
- if (!quiet_mode)
- fprintf (stderr, "Terminal not recognized - defaulting to %s\n", unterm);
-
- puts (unterm);
- }
-
- /* Return a pointer to a copy of the string STR with unprintable
- characters made printable. */
-
- char *
- decode (str)
- register char *str;
- {
- static char decodebuf[BUFSIZ];
- char tmp[10];
-
- /* Use register variables for speed. */
- register char *dbp = decodebuf;
- register char *tmpp = tmp;
-
- for (*dbp = 0; *str; str++)
- {
- if (*str == 27)
- {
- strcat (dbp, "<ESC> ");
- }
- else if (*str <= 33 || *str == 127)
- {
- sprintf (tmpp, "\\%o ", *str);
- strcat (dbp, tmpp);
- }
- else
- {
- sprintf (tmpp, "%c ", *str);
- strcat (dbp, tmpp);
- }
- }
-
- return dbp;
- }
-