2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
20 <support_level>extended</support_level>
23 #define ASTMM_LIBC ASTMM_IGNORE
26 #include "asterisk/ast_expr.h"
28 #define AST_API_MODULE 1
29 #include "asterisk/inline_api.h"
31 #define AST_API_MODULE 1
32 #include "asterisk/lock.h"
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);
45 void * attribute_malloc __ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func);
47 void * attribute_malloc __ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func)
51 if (!(p = calloc(num, len)))
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)
64 /* not a lot to do in a standalone w/o threading! */
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)
70 /* not a lot to do in a standalone w/o threading! */
74 int __ast_bt_get_addresses(struct ast_bt *bt);
75 int __ast_bt_get_addresses(struct ast_bt *bt)
77 /* Suck it, you stupid utils directory! */
80 struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames);
81 struct ast_vector_string *__ast_bt_get_symbols(void **addresses, size_t num_frames)
85 #endif /* HAVE_BKTR */
87 void ast_suspend_lock_info(void *lock_addr)
90 void ast_restore_lock_info(void *lock_addr)
93 void ast_mark_lock_acquired(void *);
94 void ast_mark_lock_acquired(void *foo)
96 /* not a lot to do in a standalone w/o threading! */
98 #endif /* DEBUG_THREADS */
101 static int global_lineno = 1;
102 static int global_expr_count=0;
103 static int global_expr_max_size=0;
104 static int global_expr_tot_size=0;
105 static int global_warn_count=0;
106 static int global_OK_count=0;
110 char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
111 char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
115 struct varz *global_varlist;
117 /* Our own version of ast_log, since the expr parser uses it. */
119 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf,5,6)));
121 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
126 printf("LOG: lev:%d file:%s line:%d func: %s ",
127 level, file, line, function);
133 char *find_var(const char *varname);
134 void set_var(const char *varname, const char *varval);
135 unsigned int check_expr(char* buffer, char* error_report);
136 int check_eval(char *buffer, char *error_report);
137 void parse_file(const char *fname);
139 int ast_add_profile(const char *x, uint64_t scale) { return 0;}
141 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
144 for (t= global_varlist; t; t = t->next) {
145 if (!strcmp(t->varname, varname)) {
152 void set_var(const char *varname, const char *varval);
154 void set_var(const char *varname, const char *varval)
156 struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
159 strcpy(t->varname, varname);
160 strcpy(t->varval, varval);
161 t->next = global_varlist;
165 unsigned int check_expr(char* buffer, char* error_report)
168 unsigned int warn_found = 0;
172 for (cp = buffer; *cp; ++cp)
177 /* skip to the other end */
178 while (*(++cp) && *cp != '"') ;
183 "Trouble? Unterminated double quote found at line %d\n",
191 if ( (*(cp + 1) == '=')
192 && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
197 "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",
198 global_lineno, *cp, *(cp + 1));
199 strcat(error_report, msg);
215 if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
220 "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",
221 global_lineno, *cp );
222 strcat(error_report, msg);
233 int check_eval(char *buffer, char *error_report);
235 struct ast_custom_function *ast_custom_function_find(const char *name);
237 struct ast_custom_function *ast_custom_function_find(const char *name)
242 int check_eval(char *buffer, char *error_report)
252 for (cp=buffer;*cp;cp++) {
253 if (*cp == '$' && *(cp+1) == '{') {
271 strncpy(varname,cp+2, xp-cp-2);
272 varname[xp-cp-2] = 0;
274 val = find_var(varname);
281 *ep++ = '5'; /* why not */
287 printf("Unterminated variable reference at line %d\n", global_lineno);
291 else if (*cp == '\\') {
292 /* braindead simple elim of backslash */
301 /* now, run the test */
302 result = ast_expr(evalbuf, s, sizeof(s),NULL);
304 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
307 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
313 void parse_file(const char *fname);
315 void parse_file(const char *fname)
317 FILE *f = fopen(fname,"r");
318 FILE *l = fopen("expr2_log","w");
321 char buffer[30000]; /* I sure hope no expr gets this big! */
324 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
328 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
334 while ((c1 = fgetc(f)) != EOF) {
337 else if (c1 == '[') {
338 if (last_char == '$') {
343 char error_report[30000];
345 while ((c1 = fgetc(f)) != EOF) {
351 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
354 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
359 buffer[bufcount++] = c1;
362 fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
365 printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
369 buffer[bufcount] = 0;
371 global_expr_tot_size += bufcount;
373 if (bufcount > global_expr_max_size)
374 global_expr_max_size = bufcount;
376 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
378 /* print error report */
379 printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
380 global_lineno, buffer);
381 fprintf(l, "%s", error_report);
384 printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
388 retval = check_eval(buffer, error_report);
389 fprintf(l, "%s", error_report);
394 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",
398 global_expr_max_size,
399 (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
406 int main(int argc,char **argv)
412 printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
413 printf(" and run them thru the parser, looking for problems\n");
414 printf("Hey-- give me a path to an extensions.conf file!\n");
415 printf(" You can also follow the file path with a series of variable decls,\n");
416 printf(" of the form, varname=value, each separated from the next by spaces.\n");
417 printf(" (this might allow you to avoid division by zero messages, check that math\n");
418 printf(" is being done correctly, etc.)\n");
419 printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
420 printf(" you to possible problems where you might be expecting those operators as part of a string.\n");
421 printf(" (to include operators in a string, wrap with double quotes!)\n");
426 for (argc1=2;argc1 < argc; argc1++) {
427 if ((eq = strchr(argv[argc1],'='))) {
429 set_var(argv[argc1],eq+1);
433 /* parse command args for x=y and set varz */