Merged revisions 52052 via svnmerge from
[asterisk/asterisk.git] / utils / check_expr.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 #include <stdio.h>
20 #include <stddef.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <../include/asterisk/ast_expr.h>
25
26 static int global_lineno = 1;
27 static int global_expr_count=0;
28 static int global_expr_max_size=0;
29 static int global_expr_tot_size=0;
30 static int global_warn_count=0;
31 static int global_OK_count=0;
32
33 struct varz
34 {
35         char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
36         char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
37         struct varz *next;
38 };
39
40 struct varz *global_varlist;
41
42 /* Our own version of ast_log, since the expr parser uses it. */
43
44 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
45
46 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
47 {
48         va_list vars;
49         va_start(vars,fmt);
50         
51         printf("LOG: lev:%d file:%s  line:%d func: %s  ",
52                    level, file, line, function);
53         vprintf(fmt, vars);
54         fflush(stdout);
55         va_end(vars);
56 }
57 void ast_register_file_version(const char *file, const char *version);
58 void ast_unregister_file_version(const char *file);
59
60 char *find_var(const char *varname);
61 void set_var(const char *varname, const char *varval);
62 unsigned int check_expr(char* buffer, char* error_report);
63 int check_eval(char *buffer, char *error_report);
64 void parse_file(const char *fname);
65
66 void ast_register_file_version(const char *file, const char *version)
67 {
68 }
69
70 void ast_unregister_file_version(const char *file)
71 {
72 }
73
74 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
75 {
76         struct varz *t;
77         for (t= global_varlist; t; t = t->next) {
78                 if (!strcmp(t->varname, varname)) {
79                         return t->varval;
80                 }
81         }
82         return 0;
83 }
84
85 void set_var(const char *varname, const char *varval)
86 {
87         struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
88         if (!t)
89                 return;
90         strcpy(t->varname, varname);
91         strcpy(t->varval, varval);
92         t->next = global_varlist;
93         global_varlist = t;
94 }
95
96 unsigned int check_expr(char* buffer, char* error_report)
97 {
98         char* cp;
99         unsigned int warn_found = 0;
100
101         error_report[0] = 0;
102         
103         for (cp = buffer; *cp; ++cp)
104         {
105                 switch (*cp)
106                 {
107                         case '"':
108                                 /* skip to the other end */
109                                 while (*(++cp) && *cp != '"') ;
110
111                                 if (*cp == 0)
112                                 {
113                                         fprintf(stderr,
114                                                 "Trouble? Unterminated double quote found at line %d\n",
115                                                 global_lineno);
116                                 }
117                                 break;
118                                 
119                         case '>':
120                         case '<':
121                         case '!':
122                                 if (   (*(cp + 1) == '=')
123                                         && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
124                                 {
125                                         char msg[200];
126                                         snprintf(msg,
127                                                 sizeof(msg),
128                                                 "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",
129                                                 global_lineno, *cp, *(cp + 1));
130                                         strcat(error_report, msg);
131                                         ++global_warn_count;
132                                         ++warn_found;
133                                 }
134                                 break;
135                                 
136                         case '|':
137                         case '&':
138                         case '=':
139                         case '+':
140                         case '-':
141                         case '*':
142                         case '/':
143                         case '%':
144                         case '?':
145                         case ':':
146                                 if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
147                                 {
148                                         char msg[200];
149                                         snprintf(msg,
150                                                 sizeof(msg),
151                                                 "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",
152                                                 global_lineno, *cp );
153                                         strcat(error_report, msg);
154                                         ++global_warn_count;
155                                         ++warn_found;
156                                 }
157                                 break;
158                 }
159         }
160
161         return warn_found;
162 }
163
164 int check_eval(char *buffer, char *error_report)
165 {
166         char *cp, *ep;
167         char s[4096];
168         char evalbuf[80000];
169         int result;
170
171         error_report[0] = 0;
172         ep = evalbuf;
173
174         for (cp=buffer;*cp;cp++) {
175                 if (*cp == '$' && *(cp+1) == '{') {
176                         int brack_lev = 1;
177                         char *xp= cp+2;
178                         
179                         while (*xp) {
180                                 if (*xp == '{')
181                                         brack_lev++;
182                                 else if (*xp == '}')
183                                         brack_lev--;
184                                 
185                                 if (brack_lev == 0)
186                                         break;
187                                 xp++;
188                         }
189                         if (*xp == '}') {
190                                 char varname[200];
191                                 char *val;
192                                 
193                                 strncpy(varname,cp+2, xp-cp-2);
194                                 varname[xp-cp-2] = 0;
195                                 cp = xp;
196                                 val = find_var(varname);
197                                 if (val) {
198                                         char *z = val;
199                                         while (*z)
200                                                 *ep++ = *z++;
201                                 }
202                                 else {
203                                         *ep++ = '5';  /* why not */
204                                         *ep++ = '5';
205                                         *ep++ = '5';
206                                 }
207                         }
208                         else {
209                                 printf("Unterminated variable reference at line %d\n", global_lineno);
210                                 *ep++ = *cp;
211                         }
212                 }
213                 else if (*cp == '\\') {
214                         /* braindead simple elim of backslash */
215                         cp++;
216                         *ep++ = *cp;
217                 }
218                 else
219                         *ep++ = *cp;
220         }
221         *ep++ = 0;
222
223         /* now, run the test */
224         result = ast_expr(evalbuf, s, sizeof(s));
225         if (result) {
226                 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
227                 return 1;
228         } else {
229                 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
230                 return 1;
231         }
232 }
233
234
235 void parse_file(const char *fname)
236 {
237         FILE *f = fopen(fname,"r");
238         FILE *l = fopen("expr2_log","w");
239         int c1;
240         char last_char= 0;
241         char buffer[30000]; /* I sure hope no expr gets this big! */
242         
243         if (!f) {
244                 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
245                 exit(20);
246         }
247         if (!l) {
248                 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
249                 exit(21);
250         }
251         
252         global_lineno = 1;
253         
254         while ((c1 = fgetc(f)) != EOF) {
255                 if (c1 == '\n')
256                         global_lineno++;
257                 else if (c1 == '[') {
258                         if (last_char == '$') {
259                                 /* bingo, an expr */
260                                 int bracklev = 1;
261                                 int bufcount = 0;
262                                 int retval;
263                                 char error_report[30000];
264                                 
265                                 while ((c1 = fgetc(f)) != EOF) {
266                                         if (c1 == '[')
267                                                 bracklev++;
268                                         else if (c1 == ']')
269                                                 bracklev--;
270                                         if (c1 == '\n') {
271                                                 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
272                                                 fclose(f);
273                                                 fclose(l);
274                                                 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
275                                         }
276                                         
277                                         if (bracklev == 0)
278                                                 break;
279                                         buffer[bufcount++] = c1;
280                                 }
281                                 if (c1 == EOF) {
282                                         fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
283                                         fclose(f);
284                                         fclose(l);
285                                         printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
286                                         exit(22);
287                                 }
288                                 
289                                 buffer[bufcount] = 0;
290                                 /* update stats */
291                                 global_expr_tot_size += bufcount;
292                                 global_expr_count++;
293                                 if (bufcount > global_expr_max_size)
294                                         global_expr_max_size = bufcount;
295                                 
296                                 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
297                                 if (retval != 0) {
298                                         /* print error report */
299                                         printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n", 
300                                                    global_lineno, buffer);
301                                         fprintf(l, "%s", error_report);
302                                 }
303                                 else {
304                                         printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
305                                         global_OK_count++;
306                                 }
307                                 error_report[0] = 0;
308                                 retval = check_eval(buffer, error_report);
309                                 fprintf(l, "%s", error_report);
310                         }
311                 }
312                 last_char = c1;
313         }
314         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",
315                    global_expr_count,
316                    global_OK_count,
317                    global_warn_count,
318                    global_expr_max_size,
319                    (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
320         
321         fclose(f);
322         fclose(l);
323 }
324
325
326 int main(int argc,char **argv)
327 {
328         int argc1;
329         char *eq;
330         
331         if (argc < 2) {
332                 printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
333                 printf("              and run them thru the parser, looking for problems\n");
334                 printf("Hey-- give me a path to an extensions.conf file!\n");
335                 printf(" You can also follow the file path with a series of variable decls,\n");
336                 printf("     of the form, varname=value, each separated from the next by spaces.\n");
337                 printf("     (this might allow you to avoid division by zero messages, check that math\n");
338                 printf("      is being done correctly, etc.)\n");
339                 printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
340                 printf("  you to possible problems where you might be expecting those operators as part of a string.\n");
341         printf("  (to include operators in a string, wrap with double quotes!)\n");
342                 
343                 exit(19);
344         }
345         global_varlist = 0;
346         for (argc1=2;argc1 < argc; argc1++) {
347                 if ((eq = strchr(argv[argc1],'='))) {
348                         *eq = 0;
349                         set_var(argv[argc1],eq+1);
350                 }
351         }
352
353         /* parse command args for x=y and set varz */
354         
355         parse_file(argv[1]);
356         return 0;
357 }