home *** CD-ROM | disk | FTP | other *** search
- /*
- read_hist: open the file containing the results of our last run. If
- it is not there then we assume that this is a first run and set the
- head of the list to NULL. Otherwise, we read the results of the previous
- run into a mess of a data structure for later use in comparisons.
-
- Assumed format of history file:
-
- command
- key
- name type value
-
- witht the following definitions:
- command: pipeline that was executed
- key: value of key on line
- name: output field name
- type: field type (same as defined in output format)
- value: what was in the field.
-
- We create a linked list of linked lists of linked lists. Improvement
- would be to change to a tree of some sort to speed up searches.
-
- Kenneth Ingham
-
- Copyright (C) 1987 The University of New Mexico
- */
-
- #include "defs.h"
-
- read_hist()
- {
- extern char histfilename[];
- extern FILE *hf;
- extern int vflag;
- extern struct old_cmd_st *chead;
-
- char line[MAX_STR];
- struct old_cmd_st *cp;
- struct val_st *vp;
- struct key_st *kp;
- int len;
- char *sp;
-
- if (vflag)
- printf("Using %s for historyfile\n", histfilename);
-
- hf = fopen(histfilename, "r");
- if (hf == NULL) {
- if (vflag)
- printf("This is a first run.\n");
- chead = NULL;
- return;
- }
-
- chead = NULL; cp = NULL; kp = NULL;
- while (fgets(line, MAX_STR, hf) != NULL) {
- line[strlen(line)-1] = '\0'; /* kill trailing cr */
- if (line[0] != '\t') { /* command */
- if (chead == NULL) {
- cp = (struct old_cmd_st *)malloc(sizeof(struct old_cmd_st));
- chead = cp;
- }
- else {
- cp->next = (struct old_cmd_st *)malloc(sizeof(struct old_cmd_st));
- cp = cp->next;
- }
-
- cp->pipeline = malloc((unsigned)strlen(line)+1);
- (void) strcpy(cp->pipeline, line);
- cp->next = NULL;
- }
- else if (line[0] == '\t' && line[1] != '\t') { /* key */
- if (cp == NULL) {
- printf("Bad history file: keyword found ");
- printf("before pipeline. Ignoring history ");
- printf("file.\n");
- chead = NULL;
- return;
- }
- if (cp->keys == NULL) {
- cp->keys = (struct key_st *)malloc(sizeof(struct key_st));
- kp = cp->keys;
- }
- else {
- kp->next = (struct key_st *)malloc(sizeof(struct key_st));
- kp = kp->next;
- }
- kp->next = NULL;
- kp->key_value = malloc((unsigned)strlen(line)+1);
- (void) strcpy(kp->key_value, &line[1]);
- }
- else if (line[0] == '\t' && line[1] == '\t') { /* vals */
- if (kp == NULL) {
- printf("Bad history file: value found ");
- printf("before keyword. Ignoring history ");
- printf("file.\n");
- chead = NULL;
- return;
- }
- if (kp->vals == NULL) {
- kp->vals = (struct val_st *)malloc(sizeof(struct val_st));
- vp = kp->vals;
- }
- else {
- vp->next = (struct val_st *)malloc(sizeof(struct val_st));
- vp = vp->next;
- }
- vp->next = NULL;
- sp = index(&line[2], ' ');
- len = sp - &line[2];
- vp->name = malloc((unsigned)len+1);
- (void) strncpy(vp->name, &line[2], len);
- vp->name[len] = '\0';
- sp++;
- len = strlen(line) - len;
- switch (*sp) {
- case 's':
- vp->val.strval = malloc((unsigned)len+1);
- (void) strcpy(vp->val.strval, sp+2);
- vp->type = STRING;
- break;
- case 'd':
- vp->val.floatval = atof(sp+2);
- vp->type = FLOAT;
- break;
- default:
- /* bad condition */
- printf("Unknown data type in history file.\n");
- printf("Offending line: %s\n",line);
- exit(1);
- break;
- }
- }
- }
- (void) fclose(hf);
- }
-