home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / Watcher / hist_value.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-02  |  1.9 KB  |  85 lines

  1. /*
  2.    hist_value: a value has been found in the history file.  Insert it
  3.    into the list.
  4.  
  5.    Kenneth Ingham
  6.  
  7.    Copyright (C) 1988 The University of New Mexico
  8. */
  9.  
  10. #include "defs.h"
  11. #include "y.tab.h"
  12.  
  13. hist_value(line, key_ptr, value_ptr)
  14. char *line;
  15. struct key_st **key_ptr;
  16. struct val_st **value_ptr;
  17. {
  18.     unsigned int len;
  19.     char *sp, *strnsave();
  20.  
  21.     /* consistency check */
  22.     if (*key_ptr == NULL) {
  23.         printf("Bad history file: value found before keyword.\n");
  24.         printf("Ignoring history file.\n");
  25.         return FAIL;
  26.     }
  27.  
  28.     /* is this the first value for this key? */
  29.     if ((*key_ptr)->vals == NULL) {
  30.         (*key_ptr)->vals = allocate(struct val_st);
  31.         *value_ptr = (*key_ptr)->vals;
  32.     }
  33.     else {
  34.         (*value_ptr)->next = allocate(struct val_st);
  35.         *value_ptr = (*value_ptr)->next;
  36.     }
  37.  
  38.     (*value_ptr)->next = NULL;
  39.  
  40.     /* 
  41.        find the space which separates the name from the type, then
  42.        get the name
  43.     */
  44.     sp = index(&line[2], ' ');
  45.     if (sp == NULL) {
  46.         printf("Garbled history file; no name-type sep.\n");
  47.         printf("Ignoring history file.\n");
  48.         return FAIL;
  49.     }
  50.     len = sp - &line[2];
  51.     (*value_ptr)->name = strnsave(&line[2], len);
  52.     (*value_ptr)->name[len] = '\0';
  53.  
  54.     sp++; /* sp now points at the type, sp+2 is the first char of val */
  55.     if (!*(sp+1) || !*(sp+2)) {
  56.         printf("Garbled history file; no value.\n");
  57.         printf("Ignoring history file.\n");
  58.         return FAIL;
  59.     }
  60.  
  61.     switch (*sp) {
  62.         case 's':
  63.             (*value_ptr)->val.type = STRING;
  64.             (*value_ptr)->val.data.string = strsave(sp+2);
  65.             break;
  66.         case 'f':
  67.             (*value_ptr)->val.type = FLOAT;
  68.             (*value_ptr)->val.data.real = atof(sp+2);
  69.             break;
  70.         case 'd':
  71.             (*value_ptr)->val.type = INTEGER;
  72.             (*value_ptr)->val.data.integer = atoi(sp+2);
  73.             break;
  74.         default:
  75.             /* bad condition */
  76.             printf("Unknown data type in history file.\n");
  77.             printf("Offending line: %s\n",line);
  78.             printf("Ignoring history file.\n");
  79.             return FAIL;
  80.             break;
  81.     }
  82.  
  83.     return SUCCESS;
  84. }
  85.