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

  1. /* table.c - read in the terminal query table(s).
  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 <pwd.h>
  9. struct passwd *getpwuid ();
  10.  
  11. #include "qterm.h"
  12.  
  13. /* Allocate enough memory to hold a variable of type T. */
  14. #define talloc(T)    ((T*) xmalloc (sizeof (T)))
  15.  
  16. /* Read the configuration file(s) into a table in memory.
  17.    Return TRUE if successful, FALSE if not. */
  18.  
  19. int
  20. mktable ()
  21. {
  22.   char file[BUFSIZ];
  23.   char *home;
  24.   struct passwd *pwd;
  25.  
  26.   if (user_table || user_table_only)
  27.     {
  28.       home = getenv ("HOME");
  29.       if (home == NULLSTR)
  30.     {
  31.       pwd = getpwuid (getuid ());
  32.       if (pwd == NULL)
  33.         {
  34.           fprintf (stderr, "%s: you have no passwd entry\n", program_name);
  35.           return FALSE;
  36.         }
  37.       home = pwd->pw_dir;
  38.     }
  39.       sprintf (file, "%s/%s", home, STRFILE);
  40.  
  41.       if (readtabfile (file) == FALSE)
  42.     return FALSE;
  43.     }
  44.   if (user_table_only)
  45.     return TRUE;
  46.  
  47.   return readtabfile (TABLEFILE);
  48. }
  49.  
  50. /* Read in configuration file FILE.
  51.    Return TRUE if successful, FALSE if not. */
  52.  
  53. int
  54. readtabfile (file)
  55.      char *file;
  56. {
  57.   register int line;
  58.   register char *cp;
  59.   static struct qt **qtpp = termtab;
  60.   char buf[BUFSIZ];
  61.   FILE *fp;
  62.  
  63.   fp = fopen (file, "r");
  64.   if (fp == NULL)
  65.     {
  66.       fprintf (stderr, "%s: ", program_name);
  67.       perror (file);
  68.       return FALSE;
  69.     }
  70.  
  71.   for (*qtpp = talloc (struct qt), line = 1, cp = buf;
  72.        qtpp < &termtab[MAXTERMS - 1] && fgets (cp, BUFSIZ, fp); line++)
  73.     {
  74.       if (*cp == '#' || *cp == '\n')
  75.     continue;
  76.  
  77.       sscanf (cp, "%s%s%s\t%[^\n]",
  78.           (*qtpp)->sendstr, (*qtpp)->recvstr,
  79.           (*qtpp)->termname, (*qtpp)->fullname);
  80.  
  81.       if (*(*qtpp)->sendstr == 0)
  82.     continue;
  83.  
  84.       if (*(*qtpp)->recvstr == 0 || *(*qtpp)->termname == 0)
  85.     {
  86.       fprintf (stderr, "%s: error in configuration file %s on line %d\n",
  87.            program_name, file, line);
  88.       fclose (fp);
  89.       return FALSE;
  90.     }
  91.       if (*querystr)
  92.     strncpy ((*qtpp)->sendstr, querystr, FIELDSIZ);
  93.       else
  94.     strncpy ((*qtpp)->sendstr, fixctl ((*qtpp)->sendstr), FIELDSIZ);
  95.  
  96.       strncpy ((*qtpp)->recvstr, fixctl ((*qtpp)->recvstr), FIELDSIZ);
  97.       *++qtpp = talloc (struct qt);
  98.     }
  99.  
  100.   fclose (fp);
  101.   free (*qtpp);
  102.   *qtpp = NULLQT;
  103.  
  104.   if (qtpp == &termtab[MAXTERMS - 1])
  105.     {
  106.       fprintf (stderr, "%s: limit of %d terminals exceeded\n",
  107.            program_name, MAXTERMS);
  108.       return FALSE;
  109.     }
  110.   return TRUE;
  111. }
  112.  
  113. /* Return true if the character C is an octal digit. */
  114. #define isoctal(c) ((c) >= '0' && (c) <= '7')
  115.  
  116. /* Return a pointer to a copy of the string STR with
  117.    visual representations of control characters replaced by the
  118.    actual control characters.  Also interpret "\ooo" octal escapes. */
  119.  
  120. char *
  121. fixctl (str)
  122.      register char *str;
  123. {
  124.   static char fixbuf[FIELDSIZ + 1];
  125.   register char *fbp = fixbuf;
  126.   int val, digits;
  127.  
  128.   for (*fbp = 0; *str; str++, fbp++)
  129.     {
  130.       if (*str == '^' && str[1])
  131.     *fbp = *++str & 037;
  132.       else if (*str == '\\')
  133.     {
  134.       for (++str, val = digits = 0;
  135.            isoctal (*str) && digits < 3; ++str, ++digits)
  136.         val = val * 8 + *str - '0';
  137.       *fbp = val;
  138.       --str;
  139.     }
  140.       else
  141.     *fbp = *str;
  142.     }
  143.  
  144.   *fbp = 0;
  145.   return fixbuf;
  146. }
  147.  
  148. /* Allocate memory with error checking. */
  149.  
  150. char *
  151. xmalloc (size)
  152.      unsigned size;
  153. {
  154.   register char *cp;
  155.  
  156.   cp = malloc (size);
  157.   if (cp == NULLSTR)
  158.     {
  159.       fprintf (stderr, "%s: virtual memory exhausted\n", program_name);
  160.       exit (1);
  161.     }
  162.   return cp;
  163. }
  164.