home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / watcher / part01 / read_hist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-27  |  3.3 KB  |  137 lines

  1. /*
  2.    read_hist: open the file containing the results of our last run.  If
  3.    it is not there then we assume that this is a first run and set the
  4.    head of the list to NULL.  Otherwise, we read the results of the previous
  5.    run into a mess of a data structure for later use in comparisons.
  6.  
  7.    Assumed format of history file:
  8.  
  9.    command
  10.     key
  11.         name type value
  12.  
  13.    witht the following definitions:
  14.     command: pipeline that was executed
  15.     key: value of key on line
  16.     name: output field name
  17.     type: field type (same as defined in output format)
  18.     value: what was in the field.
  19.  
  20.    We create a linked list of linked lists of linked lists.  Improvement
  21.    would be to change to a tree of some sort to speed up searches.
  22.  
  23.    Kenneth Ingham
  24.  
  25.    Copyright (C) 1987 The University of New Mexico
  26. */
  27.  
  28. #include "defs.h"
  29.  
  30. read_hist()
  31. {
  32.     extern char histfilename[];
  33.     extern FILE *hf;
  34.     extern int vflag;
  35.     extern struct old_cmd_st *chead;
  36.  
  37.     char line[MAX_STR];
  38.     struct old_cmd_st *cp;
  39.     struct val_st *vp;
  40.     struct key_st *kp;
  41.     int len;
  42.     char *sp;
  43.  
  44.     if (vflag)
  45.         printf("Using %s for historyfile\n", histfilename);
  46.  
  47.     hf = fopen(histfilename, "r");
  48.     if (hf == NULL) {
  49.         if (vflag)
  50.             printf("This is a first run.\n");
  51.         chead = NULL;
  52.         return;
  53.     }
  54.  
  55.     chead = NULL;  cp = NULL;  kp = NULL;
  56.     while (fgets(line, MAX_STR, hf) != NULL) {
  57.         line[strlen(line)-1] = '\0'; /* kill trailing cr */
  58.         if (line[0] != '\t') { /* command */
  59.             if (chead == NULL)  {
  60.                 cp = (struct old_cmd_st *)malloc(sizeof(struct old_cmd_st));
  61.                 chead = cp;
  62.             }
  63.             else {
  64.                 cp->next = (struct old_cmd_st *)malloc(sizeof(struct old_cmd_st));
  65.                 cp = cp->next;
  66.             }
  67.  
  68.             cp->pipeline = malloc((unsigned)strlen(line)+1);
  69.             (void) strcpy(cp->pipeline, line);
  70.             cp->next = NULL;
  71.         }
  72.         else if (line[0] == '\t' && line[1] != '\t') { /* key */
  73.             if (cp == NULL) {
  74.                 printf("Bad history file: keyword found ");
  75.                 printf("before pipeline.  Ignoring history ");
  76.                 printf("file.\n");
  77.                 chead = NULL;
  78.                 return;
  79.             }
  80.             if (cp->keys == NULL) {
  81.                 cp->keys = (struct key_st *)malloc(sizeof(struct key_st));
  82.                 kp = cp->keys;
  83.             }
  84.             else {
  85.                 kp->next = (struct key_st *)malloc(sizeof(struct key_st));
  86.                 kp = kp->next;
  87.             }
  88.             kp->next = NULL;
  89.             kp->key_value = malloc((unsigned)strlen(line)+1);
  90.             (void) strcpy(kp->key_value, &line[1]);
  91.         }
  92.         else if (line[0] == '\t' && line[1] == '\t') { /* vals */
  93.             if (kp == NULL) {
  94.                 printf("Bad history file: value found ");
  95.                 printf("before keyword.  Ignoring history ");
  96.                 printf("file.\n");
  97.                 chead = NULL;
  98.                 return;
  99.             }
  100.             if (kp->vals == NULL) {
  101.                 kp->vals = (struct val_st *)malloc(sizeof(struct val_st));
  102.                 vp = kp->vals;
  103.             }
  104.             else {
  105.                 vp->next = (struct val_st *)malloc(sizeof(struct val_st));
  106.                 vp = vp->next;
  107.             }
  108.             vp->next = NULL;
  109.             sp = index(&line[2], ' ');
  110.             len = sp - &line[2];
  111.             vp->name = malloc((unsigned)len+1);
  112.             (void) strncpy(vp->name, &line[2], len);
  113.             vp->name[len] = '\0';
  114.             sp++;
  115.             len = strlen(line) - len;
  116.             switch (*sp) {
  117.                 case 's':
  118.                     vp->val.strval = malloc((unsigned)len+1);
  119.                     (void) strcpy(vp->val.strval, sp+2);
  120.                     vp->type = STRING;
  121.                     break;
  122.                 case 'd':
  123.                     vp->val.floatval = atof(sp+2);
  124.                     vp->type = FLOAT;
  125.                     break;
  126.                 default:
  127.                     /* bad condition */
  128.                     printf("Unknown data type in history file.\n");
  129.                     printf("Offending line: %s\n",line);
  130.                     exit(1);
  131.                     break;
  132.             }
  133.         }
  134.     }
  135.     (void) fclose(hf);
  136. }
  137.