home *** CD-ROM | disk | FTP | other *** search
- /* table.c - read in the terminal query table(s).
-
- Author: Craig Bishop
- modified by David MacKenzie
- loosely based on a the original program by Michael Cooper. */
-
- #include <stdio.h>
- #include <pwd.h>
- struct passwd *getpwuid ();
-
- #include "qterm.h"
-
- /* Allocate enough memory to hold a variable of type T. */
- #define talloc(T) ((T*) xmalloc (sizeof (T)))
-
- /* Read the configuration file(s) into a table in memory.
- Return TRUE if successful, FALSE if not. */
-
- int
- mktable ()
- {
- char file[BUFSIZ];
- char *home;
- struct passwd *pwd;
-
- if (user_table || user_table_only)
- {
- home = getenv ("HOME");
- if (home == NULLSTR)
- {
- pwd = getpwuid (getuid ());
- if (pwd == NULL)
- {
- fprintf (stderr, "%s: you have no passwd entry\n", program_name);
- return FALSE;
- }
- home = pwd->pw_dir;
- }
- sprintf (file, "%s/%s", home, STRFILE);
-
- if (readtabfile (file) == FALSE)
- return FALSE;
- }
- if (user_table_only)
- return TRUE;
-
- return readtabfile (TABLEFILE);
- }
-
- /* Read in configuration file FILE.
- Return TRUE if successful, FALSE if not. */
-
- int
- readtabfile (file)
- char *file;
- {
- register int line;
- register char *cp;
- static struct qt **qtpp = termtab;
- char buf[BUFSIZ];
- FILE *fp;
-
- fp = fopen (file, "r");
- if (fp == NULL)
- {
- fprintf (stderr, "%s: ", program_name);
- perror (file);
- return FALSE;
- }
-
- for (*qtpp = talloc (struct qt), line = 1, cp = buf;
- qtpp < &termtab[MAXTERMS - 1] && fgets (cp, BUFSIZ, fp); line++)
- {
- if (*cp == '#' || *cp == '\n')
- continue;
-
- sscanf (cp, "%s%s%s\t%[^\n]",
- (*qtpp)->sendstr, (*qtpp)->recvstr,
- (*qtpp)->termname, (*qtpp)->fullname);
-
- if (*(*qtpp)->sendstr == 0)
- continue;
-
- if (*(*qtpp)->recvstr == 0 || *(*qtpp)->termname == 0)
- {
- fprintf (stderr, "%s: error in configuration file %s on line %d\n",
- program_name, file, line);
- fclose (fp);
- return FALSE;
- }
- if (*querystr)
- strncpy ((*qtpp)->sendstr, querystr, FIELDSIZ);
- else
- strncpy ((*qtpp)->sendstr, fixctl ((*qtpp)->sendstr), FIELDSIZ);
-
- strncpy ((*qtpp)->recvstr, fixctl ((*qtpp)->recvstr), FIELDSIZ);
- *++qtpp = talloc (struct qt);
- }
-
- fclose (fp);
- free (*qtpp);
- *qtpp = NULLQT;
-
- if (qtpp == &termtab[MAXTERMS - 1])
- {
- fprintf (stderr, "%s: limit of %d terminals exceeded\n",
- program_name, MAXTERMS);
- return FALSE;
- }
- return TRUE;
- }
-
- /* Return true if the character C is an octal digit. */
- #define isoctal(c) ((c) >= '0' && (c) <= '7')
-
- /* Return a pointer to a copy of the string STR with
- visual representations of control characters replaced by the
- actual control characters. Also interpret "\ooo" octal escapes. */
-
- char *
- fixctl (str)
- register char *str;
- {
- static char fixbuf[FIELDSIZ + 1];
- register char *fbp = fixbuf;
- int val, digits;
-
- for (*fbp = 0; *str; str++, fbp++)
- {
- if (*str == '^' && str[1])
- *fbp = *++str & 037;
- else if (*str == '\\')
- {
- for (++str, val = digits = 0;
- isoctal (*str) && digits < 3; ++str, ++digits)
- val = val * 8 + *str - '0';
- *fbp = val;
- --str;
- }
- else
- *fbp = *str;
- }
-
- *fbp = 0;
- return fixbuf;
- }
-
- /* Allocate memory with error checking. */
-
- char *
- xmalloc (size)
- unsigned size;
- {
- register char *cp;
-
- cp = malloc (size);
- if (cp == NULLSTR)
- {
- fprintf (stderr, "%s: virtual memory exhausted\n", program_name);
- exit (1);
- }
- return cp;
- }
-