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