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