res_pjsip: Config option to enable PJSIP logger at load time.
[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_FILE_VERSION(__FILE__, "$Revision$")
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 //void ast_register_file_version(const char *file, const char *version);
156 //void ast_unregister_file_version(const char *file);
157
158 char *find_var(const char *varname);
159 void set_var(const char *varname, const char *varval);
160 unsigned int check_expr(char* buffer, char* error_report);
161 int check_eval(char *buffer, char *error_report);
162 void parse_file(const char *fname);
163
164 void ast_register_file_version(const char *file, const char *version);  
165 void ast_register_file_version(const char *file, const char *version) { }
166 #if !defined(LOW_MEMORY)
167 int ast_add_profile(const char *x, uint64_t scale) { return 0;} 
168 #endif
169 int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
170 {
171         int ret;
172         ret = *p;
173         *p += v;
174         return ret;
175 }
176
177 void ast_unregister_file_version(const char *file);
178 void ast_unregister_file_version(const char *file)
179 {
180 }
181
182 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
183 {
184         struct varz *t;
185         for (t= global_varlist; t; t = t->next) {
186                 if (!strcmp(t->varname, varname)) {
187                         return t->varval;
188                 }
189         }
190         return 0;
191 }
192
193 void set_var(const char *varname, const char *varval);
194
195 void set_var(const char *varname, const char *varval)
196 {
197         struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
198         if (!t)
199                 return;
200         strcpy(t->varname, varname);
201         strcpy(t->varval, varval);
202         t->next = global_varlist;
203         global_varlist = t;
204 }
205
206 unsigned int check_expr(char* buffer, char* error_report)
207 {
208         char* cp;
209         unsigned int warn_found = 0;
210
211         error_report[0] = 0;
212         
213         for (cp = buffer; *cp; ++cp)
214         {
215                 switch (*cp)
216                 {
217                         case '"':
218                                 /* skip to the other end */
219                                 while (*(++cp) && *cp != '"') ;
220
221                                 if (*cp == 0)
222                                 {
223                                         fprintf(stderr,
224                                                 "Trouble? Unterminated double quote found at line %d\n",
225                                                 global_lineno);
226                                 }
227                                 break;
228                                 
229                         case '>':
230                         case '<':
231                         case '!':
232                                 if (   (*(cp + 1) == '=')
233                                         && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
234                                 {
235                                         char msg[200];
236                                         snprintf(msg,
237                                                 sizeof(msg),
238                                                 "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",
239                                                 global_lineno, *cp, *(cp + 1));
240                                         strcat(error_report, msg);
241                                         ++global_warn_count;
242                                         ++warn_found;
243                                 }
244                                 break;
245                                 
246                         case '|':
247                         case '&':
248                         case '=':
249                         case '+':
250                         case '-':
251                         case '*':
252                         case '/':
253                         case '%':
254                         case '?':
255                         case ':':
256                                 if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
257                                 {
258                                         char msg[200];
259                                         snprintf(msg,
260                                                 sizeof(msg),
261                                                 "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",
262                                                 global_lineno, *cp );
263                                         strcat(error_report, msg);
264                                         ++global_warn_count;
265                                         ++warn_found;
266                                 }
267                                 break;
268                 }
269         }
270
271         return warn_found;
272 }
273
274 int check_eval(char *buffer, char *error_report);
275
276 struct ast_custom_function *ast_custom_function_find(const char *name);
277
278 struct ast_custom_function *ast_custom_function_find(const char *name)
279 {
280         return 0;
281 }
282
283 int check_eval(char *buffer, char *error_report)
284 {
285         char *cp, *ep;
286         char s[4096];
287         char evalbuf[80000];
288         int result;
289
290         error_report[0] = 0;
291         ep = evalbuf;
292
293         for (cp=buffer;*cp;cp++) {
294                 if (*cp == '$' && *(cp+1) == '{') {
295                         int brack_lev = 1;
296                         char *xp= cp+2;
297                         
298                         while (*xp) {
299                                 if (*xp == '{')
300                                         brack_lev++;
301                                 else if (*xp == '}')
302                                         brack_lev--;
303                                 
304                                 if (brack_lev == 0)
305                                         break;
306                                 xp++;
307                         }
308                         if (*xp == '}') {
309                                 char varname[200];
310                                 char *val;
311                                 
312                                 strncpy(varname,cp+2, xp-cp-2);
313                                 varname[xp-cp-2] = 0;
314                                 cp = xp;
315                                 val = find_var(varname);
316                                 if (val) {
317                                         char *z = val;
318                                         while (*z)
319                                                 *ep++ = *z++;
320                                 }
321                                 else {
322                                         *ep++ = '5';  /* why not */
323                                         *ep++ = '5';
324                                         *ep++ = '5';
325                                 }
326                         }
327                         else {
328                                 printf("Unterminated variable reference at line %d\n", global_lineno);
329                                 *ep++ = *cp;
330                         }
331                 }
332                 else if (*cp == '\\') {
333                         /* braindead simple elim of backslash */
334                         cp++;
335                         *ep++ = *cp;
336                 }
337                 else
338                         *ep++ = *cp;
339         }
340         *ep++ = 0;
341
342         /* now, run the test */
343         result = ast_expr(evalbuf, s, sizeof(s),NULL);
344         if (result) {
345                 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
346                 return 1;
347         } else {
348                 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
349                 return 1;
350         }
351 }
352
353
354 void parse_file(const char *fname);
355
356 void parse_file(const char *fname)
357 {
358         FILE *f = fopen(fname,"r");
359         FILE *l = fopen("expr2_log","w");
360         int c1;
361         char last_char= 0;
362         char buffer[30000]; /* I sure hope no expr gets this big! */
363         
364         if (!f) {
365                 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
366                 exit(20);
367         }
368         if (!l) {
369                 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
370                 exit(21);
371         }
372         
373         global_lineno = 1;
374         
375         while ((c1 = fgetc(f)) != EOF) {
376                 if (c1 == '\n')
377                         global_lineno++;
378                 else if (c1 == '[') {
379                         if (last_char == '$') {
380                                 /* bingo, an expr */
381                                 int bracklev = 1;
382                                 int bufcount = 0;
383                                 int retval;
384                                 char error_report[30000];
385                                 
386                                 while ((c1 = fgetc(f)) != EOF) {
387                                         if (c1 == '[')
388                                                 bracklev++;
389                                         else if (c1 == ']')
390                                                 bracklev--;
391                                         if (c1 == '\n') {
392                                                 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
393                                                 fclose(f);
394                                                 fclose(l);
395                                                 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
396                                         }
397                                         
398                                         if (bracklev == 0)
399                                                 break;
400                                         buffer[bufcount++] = c1;
401                                 }
402                                 if (c1 == EOF) {
403                                         fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
404                                         fclose(f);
405                                         fclose(l);
406                                         printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
407                                         exit(22);
408                                 }
409                                 
410                                 buffer[bufcount] = 0;
411                                 /* update stats */
412                                 global_expr_tot_size += bufcount;
413                                 global_expr_count++;
414                                 if (bufcount > global_expr_max_size)
415                                         global_expr_max_size = bufcount;
416                                 
417                                 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
418                                 if (retval != 0) {
419                                         /* print error report */
420                                         printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n", 
421                                                    global_lineno, buffer);
422                                         fprintf(l, "%s", error_report);
423                                 }
424                                 else {
425                                         printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
426                                         global_OK_count++;
427                                 }
428                                 error_report[0] = 0;
429                                 retval = check_eval(buffer, error_report);
430                                 fprintf(l, "%s", error_report);
431                         }
432                 }
433                 last_char = c1;
434         }
435         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",
436                    global_expr_count,
437                    global_OK_count,
438                    global_warn_count,
439                    global_expr_max_size,
440                    (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
441         
442         fclose(f);
443         fclose(l);
444 }
445
446
447 int main(int argc,char **argv)
448 {
449         int argc1;
450         char *eq;
451         
452         if (argc < 2) {
453                 printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
454                 printf("              and run them thru the parser, looking for problems\n");
455                 printf("Hey-- give me a path to an extensions.conf file!\n");
456                 printf(" You can also follow the file path with a series of variable decls,\n");
457                 printf("     of the form, varname=value, each separated from the next by spaces.\n");
458                 printf("     (this might allow you to avoid division by zero messages, check that math\n");
459                 printf("      is being done correctly, etc.)\n");
460                 printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
461                 printf("  you to possible problems where you might be expecting those operators as part of a string.\n");
462         printf("  (to include operators in a string, wrap with double quotes!)\n");
463                 
464                 exit(19);
465         }
466         global_varlist = 0;
467         for (argc1=2;argc1 < argc; argc1++) {
468                 if ((eq = strchr(argv[argc1],'='))) {
469                         *eq = 0;
470                         set_var(argv[argc1],eq+1);
471                 }
472         }
473
474         /* parse command args for x=y and set varz */
475         
476         parse_file(argv[1]);
477         return 0;
478 }