8 int global_expr_count = 0;
9 int global_expr_max_size = 0;
10 int global_expr_tot_size = 0;
11 int global_warn_count = 0;
12 int global_OK_count = 0;
16 char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
17 char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
21 struct varz *global_varlist;
23 /* Our own version of ast_log, since the expr parser uses it. */
25 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
27 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
32 printf("LOG: lev:%d file:%s line:%d func: %s ",
33 level, file, line, function);
39 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
42 for (t= global_varlist; t; t = t->next) {
43 if (!strcmp(t->varname, varname)) {
50 void set_var(const char *varname, const char *varval)
52 struct varz *t = calloc(1,sizeof(struct varz));
53 strcpy(t->varname, varname);
54 strcpy(t->varval, varval);
55 t->next = global_varlist;
59 int check_expr(char *buffer, char *error_report)
67 for (cp=buffer;*cp;cp++) {
82 || *cp == ')' These are pretty hard to track, as they are in funcalls, etc. */
85 /* skip to the other end */
87 while (*cp && *cp != '"')
90 fprintf(stderr,"Trouble? Unterminated double quote found at line %d\n",
95 if ((*cp == '>'||*cp == '<' ||*cp=='!') && (*(cp+1) == '=')) {
102 if ((cp > buffer && *(cp-1) != ' ') || *(cp+oplen) != ' ') {
105 sprintf(tbuf,"WARNING: line %d, '%c' operator not separated by spaces. This may lead to confusion. You may wish to use double quotes to quote the grouping it is in. Please check!\n",
108 sprintf(tbuf,"WARNING: line %d, '%c%c' operator not separated by spaces. This may lead to confusion. You may wish to use double quotes to quote the grouping it is in. Please check!\n",
109 global_lineno, *cp, *(cp+1));
110 strcat(error_report,tbuf);
121 int check_eval(char *buffer, char *error_report)
123 char *cp, *ep, *xp, *s;
125 extern char *ast_expr(char *);
132 for (cp=buffer;*cp;cp++) {
133 if (*cp == '$' && *(cp+1) == '{') {
151 strncpy(varname,cp+2, xp-cp-2);
152 varname[xp-cp-2] = 0;
154 val = find_var(varname);
161 *ep++ = '5'; /* why not */
167 printf("Unterminated variable reference at line %d\n", global_lineno);
171 else if (*cp == '\\') {
172 /* braindead simple elim of backslash */
181 /* now, run the test */
182 s = ast_expr(evalbuf);
184 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
188 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
194 void parse_file(const char *fname)
196 FILE *f = fopen(fname,"r");
197 FILE *l = fopen("expr2_log","w");
200 char buffer[30000]; /* I sure hope no expr gets this big! */
203 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n");
207 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
213 while ((c1 = fgetc(f)) != EOF) {
216 else if (c1 == '[') {
217 if (last_char == '$') {
222 char error_report[30000];
224 while ((c1 = fgetc(f)) != EOF) {
230 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
233 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
238 buffer[bufcount++] = c1;
241 fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
244 printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
248 buffer[bufcount] = 0;
250 global_expr_tot_size += bufcount;
252 if (bufcount > global_expr_max_size)
253 global_expr_max_size = bufcount;
255 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
257 /* print error report */
258 printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
259 global_lineno, buffer);
260 fprintf(l, "%s", error_report);
263 printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
267 retval = check_eval(buffer, error_report);
268 fprintf(l, "%s", error_report);
273 printf("Summary:\n Expressions detected: %d\n Expressions OK: %d\n Total # Warnings: %d\n Longest Expr: %d chars\n Ave expr len: %d chars\n",
277 global_expr_max_size,
278 (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
285 main(int argc,char **argv)
291 printf("Hey-- give me a path to an extensions.conf file!\n");
295 for (argc1=2;argc1 < argc; argc1++) {
296 if ((eq = strchr(argv[argc1],'='))) {
298 set_var(argv[argc1],eq+1);
302 /* parse command args for x=y and set varz */