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