A trunk that builds is a productive trunk.
[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 int global_lineno = 1;
27 int global_expr_count = 0;
28 int global_expr_max_size = 0;
29 int global_expr_tot_size = 0;
30 int global_warn_count = 0;
31 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
58 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
59 {
60         struct varz *t;
61         for (t= global_varlist; t; t = t->next) {
62                 if (!strcmp(t->varname, varname)) {
63                         return t->varval;
64                 }
65         }
66         return 0;
67 }
68
69 void set_var(const char *varname, const char *varval)
70 {
71         struct varz *t = calloc(1,sizeof(struct varz));
72         if (!t)
73                 return;
74         strcpy(t->varname, varname);
75         strcpy(t->varval, varval);
76         t->next = global_varlist;
77         global_varlist = t;
78 }
79
80 int check_expr(char *buffer, char *error_report)
81 {
82         char *cp;
83         int oplen = 0;
84         int warn_found = 0;
85
86         error_report[0] = 0;
87         
88         for (cp=buffer;*cp;cp++) {
89                 
90                 if (*cp == '|' 
91                         || *cp == '&'
92                         || *cp == '='
93                         || *cp == '>'
94                         || *cp == '<'
95                         || *cp == '+'
96                         || *cp == '-'
97                         || *cp == '*'
98                         || *cp == '/'
99                         || *cp == '%'
100                         || *cp == '?'
101                         || *cp == ':'
102                         /*      || *cp == '('
103                                 || *cp == ')' These are pretty hard to track, as they are in funcalls, etc. */
104                         || *cp == '"') {
105                         if (*cp == '"') {
106                                 /* skip to the other end */
107                                 cp++;
108                                 while (*cp && *cp != '"')
109                                         cp++;
110                                 if (*cp == 0) {
111                                         fprintf(stderr,"Trouble? Unterminated double quote found at line %d\n",
112                                                         global_lineno);
113                                 }
114                         }
115                         else {
116                                 if ((*cp == '>'||*cp == '<' ||*cp=='!') && (*(cp+1) == '=')) {
117                                         oplen = 2;
118                                 }
119                                 else {
120                                         oplen = 1;
121                                 }
122                                 
123                                 if ((cp > buffer && *(cp-1) != ' ') || *(cp+oplen) != ' ') {
124                                         char tbuf[1000];
125                                         if (oplen == 1)
126                                                 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",
127                                                                 global_lineno, *cp);
128                                         else
129                                                 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",
130                                                                 global_lineno, *cp, *(cp+1));
131                                         strcat(error_report,tbuf);
132
133                                         global_warn_count++;
134                                         warn_found++;
135                                 }
136                         }
137                 }
138         }
139         return warn_found;
140 }
141
142 int check_eval(char *buffer, char *error_report)
143 {
144         char *cp, *ep, *xp;
145         char s[4096];
146         char evalbuf[80000];
147         int oplen = 0;
148         int warn_found = 0;
149         int result;
150
151         error_report[0] = 0;
152         ep = evalbuf;
153
154         for (cp=buffer;*cp;cp++) {
155                 if (*cp == '$' && *(cp+1) == '{') {
156                         int brack_lev = 1;
157                         char *xp= cp+2;
158                         
159                         while (*xp) {
160                                 if (*xp == '{')
161                                         brack_lev++;
162                                 else if (*xp == '}')
163                                         brack_lev--;
164                                 
165                                 if (brack_lev == 0)
166                                         break;
167                                 xp++;
168                         }
169                         if (*xp == '}') {
170                                 char varname[200];
171                                 char *val;
172                                 
173                                 strncpy(varname,cp+2, xp-cp-2);
174                                 varname[xp-cp-2] = 0;
175                                 cp = xp;
176                                 val = find_var(varname);
177                                 if (val) {
178                                         char *z = val;
179                                         while (*z)
180                                                 *ep++ = *z++;
181                                 }
182                                 else {
183                                         *ep++ = '5';  /* why not */
184                                         *ep++ = '5';
185                                         *ep++ = '5';
186                                 }
187                         }
188                         else {
189                                 printf("Unterminated variable reference at line %d\n", global_lineno);
190                                 *ep++ = *cp;
191                         }
192                 }
193                 else if (*cp == '\\') {
194                         /* braindead simple elim of backslash */
195                         cp++;
196                         *ep++ = *cp;
197                 }
198                 else
199                         *ep++ = *cp;
200         }
201         *ep++ = 0;
202
203         /* now, run the test */
204         result = ast_expr(evalbuf, s, sizeof(s));
205         if (result) {
206                 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
207                 return 1;
208         } else {
209                 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
210                 return 1;
211         }
212 }
213
214
215 void parse_file(const char *fname)
216 {
217         FILE *f = fopen(fname,"r");
218         FILE *l = fopen("expr2_log","w");
219         int c1;
220         char last_char= 0;
221         char buffer[30000]; /* I sure hope no expr gets this big! */
222         
223         if (!f) {
224                 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n");
225                 exit(20);
226         }
227         if (!l) {
228                 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
229                 exit(21);
230         }
231         
232         global_lineno = 1;
233         
234         while ((c1 = fgetc(f)) != EOF) {
235                 if (c1 == '\n')
236                         global_lineno++;
237                 else if (c1 == '[') {
238                         if (last_char == '$') {
239                                 /* bingo, an expr */
240                                 int bracklev = 1;
241                                 int bufcount = 0;
242                                 int retval;
243                                 char error_report[30000];
244                                 
245                                 while ((c1 = fgetc(f)) != EOF) {
246                                         if (c1 == '[')
247                                                 bracklev++;
248                                         else if (c1 == ']')
249                                                 bracklev--;
250                                         if (c1 == '\n') {
251                                                 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
252                                                 fclose(f);
253                                                 fclose(l);
254                                                 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
255                                         }
256                                         
257                                         if (bracklev == 0)
258                                                 break;
259                                         buffer[bufcount++] = c1;
260                                 }
261                                 if (c1 == EOF) {
262                                         fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
263                                         fclose(f);
264                                         fclose(l);
265                                         printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
266                                         exit(22);
267                                 }
268                                 
269                                 buffer[bufcount] = 0;
270                                 /* update stats */
271                                 global_expr_tot_size += bufcount;
272                                 global_expr_count++;
273                                 if (bufcount > global_expr_max_size)
274                                         global_expr_max_size = bufcount;
275                                 
276                                 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
277                                 if (retval != 0) {
278                                         /* print error report */
279                                         printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n", 
280                                                    global_lineno, buffer);
281                                         fprintf(l, "%s", error_report);
282                                 }
283                                 else {
284                                         printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
285                                         global_OK_count++;
286                                 }
287                                 error_report[0] = 0;
288                                 retval = check_eval(buffer, error_report);
289                                 fprintf(l, "%s", error_report);
290                         }
291                 }
292                 last_char = c1;
293         }
294         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",
295                    global_expr_count,
296                    global_OK_count,
297                    global_warn_count,
298                    global_expr_max_size,
299                    (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
300         
301         fclose(f);
302         fclose(l);
303 }
304
305
306 main(int argc,char **argv)
307 {
308         int argc1;
309         char *eq;
310         
311         if (argc < 2) {
312                 printf("Hey-- give me a path to an extensions.conf file!\n");
313                 exit(19);
314         }
315         global_varlist = 0;
316         for (argc1=2;argc1 < argc; argc1++) {
317                 if ((eq = strchr(argv[argc1],'='))) {
318                         *eq = 0;
319                         set_var(argv[argc1],eq+1);
320                 }
321         }
322
323         /* parse command args for x=y and set varz */
324         
325         parse_file(argv[1]);
326 }