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