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