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