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