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_suspend_lock_info(void *lock_addr)
111 void ast_restore_lock_info(void *lock_addr)
114 void ast_mark_lock_acquired(void *);
115 void ast_mark_lock_acquired(void *foo)
117 /* not a lot to do in a standalone w/o threading! */
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;
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** */
136 struct varz *global_varlist;
138 /* Our own version of ast_log, since the expr parser uses it. */
140 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf,5,6)));
142 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
147 printf("LOG: lev:%d file:%s line:%d func: %s ",
148 level, file, line, function);
153 //void ast_register_file_version(const char *file, const char *version);
154 //void ast_unregister_file_version(const char *file);
156 char *find_var(const char *varname);
157 void set_var(const char *varname, const char *varval);
158 unsigned int check_expr(char* buffer, char* error_report);
159 int check_eval(char *buffer, char *error_report);
160 void parse_file(const char *fname);
162 void ast_register_file_version(const char *file, const char *version);
163 void ast_register_file_version(const char *file, const char *version) { }
164 #if !defined(LOW_MEMORY)
165 int ast_add_profile(const char *x, uint64_t scale) { return 0;}
167 int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
175 void ast_unregister_file_version(const char *file);
176 void ast_unregister_file_version(const char *file)
180 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
183 for (t= global_varlist; t; t = t->next) {
184 if (!strcmp(t->varname, varname)) {
191 void set_var(const char *varname, const char *varval);
193 void set_var(const char *varname, const char *varval)
195 struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
198 strcpy(t->varname, varname);
199 strcpy(t->varval, varval);
200 t->next = global_varlist;
204 unsigned int check_expr(char* buffer, char* error_report)
207 unsigned int warn_found = 0;
211 for (cp = buffer; *cp; ++cp)
216 /* skip to the other end */
217 while (*(++cp) && *cp != '"') ;
222 "Trouble? Unterminated double quote found at line %d\n",
230 if ( (*(cp + 1) == '=')
231 && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
236 "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",
237 global_lineno, *cp, *(cp + 1));
238 strcat(error_report, msg);
254 if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
259 "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",
260 global_lineno, *cp );
261 strcat(error_report, msg);
272 int check_eval(char *buffer, char *error_report);
274 struct ast_custom_function *ast_custom_function_find(const char *name);
276 struct ast_custom_function *ast_custom_function_find(const char *name)
281 int check_eval(char *buffer, char *error_report)
291 for (cp=buffer;*cp;cp++) {
292 if (*cp == '$' && *(cp+1) == '{') {
310 strncpy(varname,cp+2, xp-cp-2);
311 varname[xp-cp-2] = 0;
313 val = find_var(varname);
320 *ep++ = '5'; /* why not */
326 printf("Unterminated variable reference at line %d\n", global_lineno);
330 else if (*cp == '\\') {
331 /* braindead simple elim of backslash */
340 /* now, run the test */
341 result = ast_expr(evalbuf, s, sizeof(s),NULL);
343 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
346 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
352 void parse_file(const char *fname);
354 void parse_file(const char *fname)
356 FILE *f = fopen(fname,"r");
357 FILE *l = fopen("expr2_log","w");
360 char buffer[30000]; /* I sure hope no expr gets this big! */
363 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
367 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
373 while ((c1 = fgetc(f)) != EOF) {
376 else if (c1 == '[') {
377 if (last_char == '$') {
382 char error_report[30000];
384 while ((c1 = fgetc(f)) != EOF) {
390 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
393 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
398 buffer[bufcount++] = c1;
401 fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
404 printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
408 buffer[bufcount] = 0;
410 global_expr_tot_size += bufcount;
412 if (bufcount > global_expr_max_size)
413 global_expr_max_size = bufcount;
415 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
417 /* print error report */
418 printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
419 global_lineno, buffer);
420 fprintf(l, "%s", error_report);
423 printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
427 retval = check_eval(buffer, error_report);
428 fprintf(l, "%s", error_report);
433 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",
437 global_expr_max_size,
438 (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
445 int main(int argc,char **argv)
451 printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
452 printf(" and run them thru the parser, looking for problems\n");
453 printf("Hey-- give me a path to an extensions.conf file!\n");
454 printf(" You can also follow the file path with a series of variable decls,\n");
455 printf(" of the form, varname=value, each separated from the next by spaces.\n");
456 printf(" (this might allow you to avoid division by zero messages, check that math\n");
457 printf(" is being done correctly, etc.)\n");
458 printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
459 printf(" you to possible problems where you might be expecting those operators as part of a string.\n");
460 printf(" (to include operators in a string, wrap with double quotes!)\n");
465 for (argc1=2;argc1 < argc; argc1++) {
466 if ((eq = strchr(argv[argc1],'='))) {
468 set_var(argv[argc1],eq+1);
472 /* parse command args for x=y and set varz */