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>
24 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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)))
58 #if !defined(LOW_MEMORY)
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)
65 /* not a lot to do in a standalone w/o threading! */
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)
71 /* 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 char **__ast_bt_get_symbols(void **addresses, size_t num_frames);
81 char **__ast_bt_get_symbols(void **addresses, size_t num_frames)
83 char **foo = calloc(num_frames, sizeof(char *) + 1);
86 for (i = 0; i < num_frames; i++) {
87 foo[i] = (char *) foo + sizeof(char *) * num_frames;
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)
98 /* not a lot to do in a standalone w/o threading! */
101 void ast_remove_lock_info(void *lock_addr);
102 void ast_remove_lock_info(void *lock_addr)
104 /* not a lot to do in a standalone w/o threading! */
106 #endif /* HAVE_BKTR */
108 void ast_mark_lock_acquired(void *);
109 void ast_mark_lock_acquired(void *foo)
111 /* not a lot to do in a standalone w/o threading! */
115 static int global_lineno = 1;
116 static int global_expr_count=0;
117 static int global_expr_max_size=0;
118 static int global_expr_tot_size=0;
119 static int global_warn_count=0;
120 static int global_OK_count=0;
124 char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
125 char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
129 struct varz *global_varlist;
131 /* Our own version of ast_log, since the expr parser uses it. */
133 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf,5,6)));
135 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
140 printf("LOG: lev:%d file:%s line:%d func: %s ",
141 level, file, line, function);
146 //void ast_register_file_version(const char *file, const char *version);
147 //void ast_unregister_file_version(const char *file);
149 char *find_var(const char *varname);
150 void set_var(const char *varname, const char *varval);
151 unsigned int check_expr(char* buffer, char* error_report);
152 int check_eval(char *buffer, char *error_report);
153 void parse_file(const char *fname);
155 void ast_register_file_version(const char *file, const char *version);
156 void ast_register_file_version(const char *file, const char *version) { }
157 #if !defined(LOW_MEMORY)
158 int ast_add_profile(const char *x, uint64_t scale) { return 0;}
160 int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
168 void ast_unregister_file_version(const char *file);
169 void ast_unregister_file_version(const char *file)
173 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
176 for (t= global_varlist; t; t = t->next) {
177 if (!strcmp(t->varname, varname)) {
184 void set_var(const char *varname, const char *varval);
186 void set_var(const char *varname, const char *varval)
188 struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
191 strcpy(t->varname, varname);
192 strcpy(t->varval, varval);
193 t->next = global_varlist;
197 unsigned int check_expr(char* buffer, char* error_report)
200 unsigned int warn_found = 0;
204 for (cp = buffer; *cp; ++cp)
209 /* skip to the other end */
210 while (*(++cp) && *cp != '"') ;
215 "Trouble? Unterminated double quote found at line %d\n",
223 if ( (*(cp + 1) == '=')
224 && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
229 "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",
230 global_lineno, *cp, *(cp + 1));
231 strcat(error_report, msg);
247 if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
252 "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",
253 global_lineno, *cp );
254 strcat(error_report, msg);
265 int check_eval(char *buffer, char *error_report);
267 struct ast_custom_function *ast_custom_function_find(const char *name);
269 struct ast_custom_function *ast_custom_function_find(const char *name)
274 int check_eval(char *buffer, char *error_report)
284 for (cp=buffer;*cp;cp++) {
285 if (*cp == '$' && *(cp+1) == '{') {
303 strncpy(varname,cp+2, xp-cp-2);
304 varname[xp-cp-2] = 0;
306 val = find_var(varname);
313 *ep++ = '5'; /* why not */
319 printf("Unterminated variable reference at line %d\n", global_lineno);
323 else if (*cp == '\\') {
324 /* braindead simple elim of backslash */
333 /* now, run the test */
334 result = ast_expr(evalbuf, s, sizeof(s),NULL);
336 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
339 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
345 void parse_file(const char *fname);
347 void parse_file(const char *fname)
349 FILE *f = fopen(fname,"r");
350 FILE *l = fopen("expr2_log","w");
353 char buffer[30000]; /* I sure hope no expr gets this big! */
356 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
360 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
366 while ((c1 = fgetc(f)) != EOF) {
369 else if (c1 == '[') {
370 if (last_char == '$') {
375 char error_report[30000];
377 while ((c1 = fgetc(f)) != EOF) {
383 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
386 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
391 buffer[bufcount++] = c1;
394 fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
397 printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
401 buffer[bufcount] = 0;
403 global_expr_tot_size += bufcount;
405 if (bufcount > global_expr_max_size)
406 global_expr_max_size = bufcount;
408 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
410 /* print error report */
411 printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
412 global_lineno, buffer);
413 fprintf(l, "%s", error_report);
416 printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
420 retval = check_eval(buffer, error_report);
421 fprintf(l, "%s", error_report);
426 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_max_size,
431 (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
438 int main(int argc,char **argv)
444 printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
445 printf(" and run them thru the parser, looking for problems\n");
446 printf("Hey-- give me a path to an extensions.conf file!\n");
447 printf(" You can also follow the file path with a series of variable decls,\n");
448 printf(" of the form, varname=value, each separated from the next by spaces.\n");
449 printf(" (this might allow you to avoid division by zero messages, check that math\n");
450 printf(" is being done correctly, etc.)\n");
451 printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
452 printf(" you to possible problems where you might be expecting those operators as part of a string.\n");
453 printf(" (to include operators in a string, wrap with double quotes!)\n");
458 for (argc1=2;argc1 < argc; argc1++) {
459 if ((eq = strchr(argv[argc1],'='))) {
461 set_var(argv[argc1],eq+1);
465 /* parse command args for x=y and set varz */