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