head 1.1; access; symbols; locks; strict; comment @ * @; 1.1 date 2000.01.09.10.57.06; author ahuxley; state Exp; branches; next ; desc @Variable definition / table space testing code @ 1.1 log @Initial revision @ text @#include #include #include #include "utils.h" char *fget_logical_line( FILE *fp, char *logi_line_buf, int size) { int i, j, s, l; char phys_line_buf[2048]; debug_level = 5; for (i=0;;i++) { /* get the physical line - not an error if end of file */ if (fgets(phys_line_buf, sizeof(phys_line_buf), fp) == NULL) break; /* if it's later than the first physical line then we scan for first * non-white-space char. */ if (i == 0) j = 0; else { for (j=0, s=strlen(phys_line_buf); j size) return(NULL); strcpy(logi_line_buf+strlen(logi_line_buf), phys_line_buf+j); /* erase NL on end */ l = strlen(logi_line_buf); if (logi_line_buf[l-1] == '\n') logi_line_buf[--l] = '\0'; /* if not backslash terminated, we're at end of logical line */ if (logi_line_buf[l-1] == '\\') logi_line_buf[--l] = '\0'; else break; } return(logi_line_buf); } int split_into_words( char *word_buf[], int size, char *line_buf) { int inword, inquotes, moveon; int i; char *cp, *word_start; inword=0; inquotes=0; moveon=1; for (i=0,cp=line_buf; *cp != '\0'; moveon && cp++) { moveon = 1; /* *after* quotes we're always in a word */ if (!inword && inquotes) { word_start = cp; inword = 1; /* start of normal word */ } else if (!inword && *cp != ' ' && *cp != '\t' && *cp != '"') { word_start = cp; inword = 1; /* encountering quotes while not in a word, means the next char is the start of a word - left to be picked up next time round */ } else if (!inquotes && *cp == '"') { inquotes = 1; /* but we do need to shuffle everything after this up one */ (void) memmove(cp, cp+1, strlen(cp+1)+1); moveon = 0; /* normal end of word */ } else if (!inquotes && (*cp == ' ' || *cp == '\t')) { *cp = '\0'; inword = 0; if ((word_buf[i++] = strdup(word_start)) == NULL) internal("strndup: %s", strerror[errno]); /* encountering end quotes does not mean end of word ("hell"o) */ } else if (inquotes && *cp == '"') { inquotes = 0; /* *cp = '\0'; */ inword = 0; /* but we do need to shuffle everything after this up one */ (void) memmove(cp, cp+1, strlen(cp+1)+1); moveon = 0; } } /* if we're in a word when we run out of data, we're at a word end */ /* should really check quotes balance, etc */ if (inquotes /* && inword */) { error("unbalanced quotes"); } if (inword) { if ((word_buf[i++] = strdup(word_start)) == NULL) internal("strndup: %s", strerror[errno]); inword = 0; } word_buf[i] = NULL; return(i); } main() { char buf[2048]; char *wbuf[128]; int c, i; char **table_space = (char **) NULL; while (!feof(stdin)) { debug(5, "main: starting to read new logical line"); fget_logical_line(stdin, buf, sizeof(buf)); debug(5, "main: logical line: %s", buf); c=split_into_words(wbuf, sizeof(wbuf), buf); debug(5, "main: word count: %d", c); if (strcmp(wbuf[0], "#") == 0 || strcmp(wbuf[0], "comment") == 0) { debug(5, "main: encountered comment"); } if (strcmp(wbuf[0], "set") == 0) { debug(5, "main: encountered assignment"); if (!valid_variable(wbuf[1])) error("'%s' is not a valid variable", wbuf[1]); assign(table_space, wbuf[1], wbuf[2]); } else { error("'%s' is not a valid command", wbuf[0]); } for (i=0; i