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