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