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