home *** CD-ROM | disk | FTP | other *** search
- /*** parser.c - parses the input data */
-
- #include <stdio.h>
- #include <strings.h>
- #include <math.h>
- #include "common.h"
- #include "plot.h"
-
- /* Parser */
- parse(op)
- FILE *op;
- {
- char *strcpy();
- int len,match,i;
- char line[MAXCHAR];
- char name[MAXCHAR],value[MAXCHAR];
- char word[100][MAXCHAR];
- char *keywords[MAXKEY];
-
- initkeywords(keywords);
-
- while (len=getline(op,line, MAXCHAR) > 0)
- if (getwords(line,len,word) >= 2) {
- strcpy(name, word[0]);
- strcpy(value,word[1]);
- match = NO;
- i = 0;
- while (i < MAXKEY && match==NO ) {
- if (strcmp(keywords[i],name) == 0) {
- match = YES;
- assignvalue(i,value);
- }
- i++;
- }
- if (match==NO) fprintf(stderr,"%s: Unknown keyword\n",name);
- }
- }
-
- /* Initialize the keywords */
- initkeywords(keywords)
- char *keywords[];
- {
- keywords[0] = "xlabel";
- keywords[1] = "ylabel";
- keywords[2] = "toplabel";
- keywords[3] = "grid";
- keywords[4] = "equalscale";
- keywords[5] = "postscript";
- keywords[6] = "printplot";
- keywords[7] = "linetypes";
- keywords[8] = "scale";
- keywords[9] = "xticks";
- keywords[10]= "yticks";
- keywords[11]= "contlabel";
- keywords[12]= "joinlevel";
- keywords[13]= "printer";
- keywords[14]= "landscape";
- }
-
- /* Assign a value to a keyword */
- assignvalue(index,value)
- int index;
- char *value;
- {
- double atof();
- char *strcpy();
- char *sprintf();
- extern char xlabel[MAXCHAR];
- extern char ylabel[MAXCHAR];
- extern char toplabel[MAXCHAR];
- extern char printer[MAXCHAR];
- extern int grid,equalscale,postscript,printplot;
- extern int linetypes,contlabel,joincurve,landscape;
- extern char *joinlevel;
- extern int xticks,yticks;
- extern double scale;
-
- switch (index) {
- case 0 : strcpy(xlabel , value);
- fprintf(stdout," Options : Xlabel = %s\n",xlabel);
- break;
- case 1 : strcpy(ylabel , value);
- fprintf(stdout," Options : Ylabel = %s\n",ylabel);
- break;
- case 2 : strcpy(toplabel , value);
- fprintf(stdout," Options : Top label = %s\n",toplabel);
- break;
- case 3 : if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
- (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
- grid = ON;
- if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
- grid = OFF;
- fprintf(stdout," Options : grid = %s\n",value);
- }
- else
- fprintf(stdout," Options : Unknown keyword = %s\n",value);
- break;
- case 4 : if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
- (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
- equalscale = ON;
- if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
- equalscale = OFF;
- fprintf(stdout," Options : equalscale = %s\n",value);
- }
- else
- fprintf(stdout," Options : Unknown keyword = %s\n",value);
- break;
- case 5 : if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
- (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
- postscript = ON;
- if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
- postscript = OFF;
- fprintf(stdout," Options : postscript = %s\n",value);
- }
- else
- fprintf(stdout," Options : Unknown keyword = %s\n",value);
- break;
- case 6 : if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
- (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
- printplot = ON;
- if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
- printplot = OFF;
- fprintf(stdout," Options : printplot = %s\n",value);
- }
- else
- fprintf(stdout," Options : Unknown keyword = %s\n",value);
- break;
- case 7 : linetypes = atoi(value);
- if (linetypes <=0 || linetypes >= 4) linetypes = 2;
- fprintf(stdout," Options : linetypes = %d\n",linetypes);
- break;
- case 8 : scale = atof(value);
- if (scale < 0.1 || scale > 1) scale = 1.0;
- fprintf(stdout," Options : scale = %f\n",scale);
- break;
- case 9 : xticks = atoi(value);
- if (xticks <= 0 || xticks > 20) xticks = 4;
- fprintf(stdout," Options : xticks = %d\n",xticks);
- break;
- case 10: yticks = atoi(value);
- if (yticks <= 0 || yticks > 20) yticks = 4;
- fprintf(stdout," Options : yticks = %d\n",yticks);
- break;
- case 11: if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
- (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
- contlabel = ON;
- if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
- contlabel = OFF;
- fprintf(stdout," Options : contlabel = %s\n",value);
- }
- else
- fprintf(stdout," Options : Unknown keyword = %s\n",value);
- break;
- case 12: if ((strcmp(value,"HIGH")==0)||(strcmp(value,"high")==0)||
- (strcmp(value,"LOW" )==0)||(strcmp(value,"low" )==0)) {
- joincurve = ON;
- fprintf(stdout," Options : joincurve = ON\n");
- fprintf(stdout," Options : joinlevel = %s\n",value);
- } else {
- joinlevel = OFF;
- fprintf(stdout," Warning : Invalid keyword -");
- fprintf(stdout," [-j] [HIGH/high/LOW/low]\n");
- }
- break;
- case 13: sprintf(printer,"-P%s",value);
- fprintf(stdout," Commands: Printer = %s\n",printer);
- break;
- case 14: if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
- (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
- landscape = ON;
- if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
- landscape = OFF;
- fprintf(stdout," Options : landscape = %s\n",value);
- }
- else
- fprintf(stdout," Options : Unknown keyword = %s\n",value);
- break;
- default: break;
- }
- }
-
- /* Convert a character into a number */
- chartoint(ch)
- char ch;
- {
- int num;
-
- switch (ch) {
- case '0' : num = 0; break;
- case '1' : num = 1; break;
- case '2' : num = 2; break;
- case '3' : num = 3; break;
- case '4' : num = 4; break;
- case '5' : num = 5; break;
- case '6' : num = 6; break;
- case '7' : num = 7; break;
- case '8' : num = 8; break;
- case '9' : num = 9; break;
- default : fprintf(stderr,"Warning : Nonnumeric character %c in string",ch);
- num = 0; break;
- }
- return(num);
- }
-
- /* get line into s */
- getline(op,s,lim)
- FILE *op;
- char s[];
- int lim;
- {
- int c,i,j;
-
- /* read the line until termination marks ';' or '#' */
- for (i=0; i<lim-1 && (c=getc(op))!=EOF && c!='\n'&&c!='#'&& c!=';' ; ++i)
- s[i] = c;
- if (i==lim-1 || c==';' ||c=='#' || c=='\n') s[i++] = '\n';
- if (c==';'||c=='#')
- for (j=0; (c=getc(op))!=EOF && c!='\n'; ++j);
- s[i] = '\0';
-
- return(i);
- }
-
- /* Get the words in a line */
- getwords(s,length,word)
- char s[];
- int length;
- char word[100][MAXCHAR];
- {
- int i,nc,nw,inword,collectword;
- char c;
-
- nc = nw = 0;
- inword = NO;
- collectword = NO;
-
- if (length==1 && s[0] == '\n') return(nw);
- for (i=0; s[i] != '\0'; i++) {
- c = s[i];
- if (c == ' ' || c=='\n' || c == '\t' || c=='=') {
- if (inword == YES && (collectword==NO || c=='\n') ) {
- word[nw++][nc++] = '\0';
- inword = NO;
- nc = 0;
- } else if (collectword == YES)
- word[nw][nc++] = c;
- } else if (c == '\'' || c == '"') {
- if (collectword == NO) collectword = YES;
- else {
- collectword = NO;
- word[nw++][nc++] = '\0';
- inword = NO;
- nc = 0;
- }
- } else {
- word[nw][nc++] = c;
- if (inword == NO) inword = YES;
- }
- }
- return(nw);
- }
-
- /* Convert a word into a number - don't need this */
- /* use the library function atof() instead */
- double word_to_number(word)
-
- char word[];
-
- {
- int i;
- int decimal_flag;
- int exponent_flag;
- int decimal_count;
- int new_term;
- double number,sign,exponent,exp_sign;
-
- sign = 1.;
- exp_sign = 1.;
- decimal_flag = OFF;
- exponent_flag = OFF;
- decimal_count = 0;
- number = 0.;
- exponent = 0.;
-
- for (i = 0; word[i] != '\0'; i++)
- {
- if (word[i] == '-' && exponent_flag == OFF)
- sign = -1.;
- else if (word[i] != '+')
- {
- if (word[i] == '.')
- decimal_flag = ON;
- else
- if (word[i] == 'e')
- exponent_flag = ON;
- else
- {
- new_term = word[i] - '0';
- if (exponent_flag)
- {
- if (word[i] == '-')
- exp_sign = -1.;
- else
- exponent = exponent * 10. + (double) new_term;
- }
- else {
- number = number * 10. + (double) new_term;
- if (decimal_flag)
- decimal_count++;
- }
- }
- }
- }
- for (; decimal_count > 0; decimal_count--)
- number /= 10.;
-
- number = number * sign * pow((double)10.,(exp_sign * exponent));
-
- return number;
- }
-
-