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.
27 char x; /* basically empty! */
30 #include <../include/asterisk/compat.h>
31 #include <../include/asterisk/ast_expr.h>
33 #define AST_API_MODULE 1
34 #include "asterisk/inline_api.h"
36 #define AST_API_MODULE 1
37 #include "asterisk/lock.h"
39 #include "asterisk/strings.h"
41 /* I included this from utils.c, so as not to have everything in that .c
44 * core handler for dynamic strings.
45 * This is not meant to be called directly, but rather through the
46 * various wrapper macros
50 * ast_str_append_va(...)
52 int __ast_str_helper(struct ast_str **buf, size_t max_len,
53 int append, const char *fmt, va_list ap)
56 int offset = (append && (*buf)->len) ? (*buf)->used : 0;
59 max_len = (*buf)->len; /* don't exceed the allocated space */
61 * Ask vsnprintf how much space we need. Remember that vsnprintf
62 * does not count the final '\0' so we must add 1.
64 res = vsnprintf((*buf)->str + offset, (*buf)->len - offset, fmt, ap);
66 need = res + offset + 1;
68 * If there is not enough space and we are below the max length,
69 * reallocate the buffer and return a message telling to retry.
71 if (need > (*buf)->len && (max_len == 0 || (*buf)->len < max_len) ) {
72 if (max_len && max_len < need) /* truncate as needed */
74 else if (max_len == 0) /* if unbounded, give more room for next time */
76 if (ast_str_make_space(buf, need)) {
77 return AST_DYNSTR_BUILD_FAILED;
79 (*buf)->str[offset] = '\0'; /* Truncate the partial write. */
81 /* va_end() and va_start() must be done before calling
82 * vsnprintf() again. */
83 return AST_DYNSTR_BUILD_RETRY;
85 /* update space used, keep in mind the truncation */
86 (*buf)->used = (res + offset > (*buf)->len) ? (*buf)->len : res + offset;
91 void ast_store_lock_info(enum ast_lock_type type, const char *filename,
92 int line_num, const char *func, const char *lock_name, void *lock_addr)
94 /* not a lot to do in a standalone w/o threading! */
97 void ast_mark_lock_acquired(void)
99 /* not a lot to do in a standalone w/o threading! */
102 void ast_remove_lock_info(void *lock_addr)
104 /* not a lot to do in a standalone w/o threading! */
107 static int global_lineno = 1;
108 static int global_expr_count=0;
109 static int global_expr_max_size=0;
110 static int global_expr_tot_size=0;
111 static int global_warn_count=0;
112 static int global_OK_count=0;
116 char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
117 char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
121 struct varz *global_varlist;
123 /* Our own version of ast_log, since the expr parser uses it. */
125 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
127 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
132 printf("LOG: lev:%d file:%s line:%d func: %s ",
133 level, file, line, function);
138 void ast_register_file_version(const char *file, const char *version);
139 void ast_unregister_file_version(const char *file);
141 char *find_var(const char *varname);
142 void set_var(const char *varname, const char *varval);
143 unsigned int check_expr(char* buffer, char* error_report);
144 int check_eval(char *buffer, char *error_report);
145 void parse_file(const char *fname);
147 void ast_register_file_version(const char *file, const char *version)
151 void ast_unregister_file_version(const char *file)
155 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
158 for (t= global_varlist; t; t = t->next) {
159 if (!strcmp(t->varname, varname)) {
166 void set_var(const char *varname, const char *varval);
168 void set_var(const char *varname, const char *varval)
170 struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
173 strcpy(t->varname, varname);
174 strcpy(t->varval, varval);
175 t->next = global_varlist;
179 unsigned int check_expr(char* buffer, char* error_report)
182 unsigned int warn_found = 0;
186 for (cp = buffer; *cp; ++cp)
191 /* skip to the other end */
192 while (*(++cp) && *cp != '"') ;
197 "Trouble? Unterminated double quote found at line %d\n",
205 if ( (*(cp + 1) == '=')
206 && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
211 "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",
212 global_lineno, *cp, *(cp + 1));
213 strcat(error_report, msg);
229 if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
234 "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",
235 global_lineno, *cp );
236 strcat(error_report, msg);
247 int check_eval(char *buffer, char *error_report);
249 struct ast_custom_function *ast_custom_function_find(const char *name);
251 struct ast_custom_function *ast_custom_function_find(const char *name)
256 int check_eval(char *buffer, char *error_report)
266 for (cp=buffer;*cp;cp++) {
267 if (*cp == '$' && *(cp+1) == '{') {
285 strncpy(varname,cp+2, xp-cp-2);
286 varname[xp-cp-2] = 0;
288 val = find_var(varname);
295 *ep++ = '5'; /* why not */
301 printf("Unterminated variable reference at line %d\n", global_lineno);
305 else if (*cp == '\\') {
306 /* braindead simple elim of backslash */
315 /* now, run the test */
316 result = ast_expr(evalbuf, s, sizeof(s),NULL);
318 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
321 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
327 void parse_file(const char *fname);
329 void parse_file(const char *fname)
331 FILE *f = fopen(fname,"r");
332 FILE *l = fopen("expr2_log","w");
335 char buffer[30000]; /* I sure hope no expr gets this big! */
338 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
342 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
348 while ((c1 = fgetc(f)) != EOF) {
351 else if (c1 == '[') {
352 if (last_char == '$') {
357 char error_report[30000];
359 while ((c1 = fgetc(f)) != EOF) {
365 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
368 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
373 buffer[bufcount++] = c1;
376 fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
379 printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
383 buffer[bufcount] = 0;
385 global_expr_tot_size += bufcount;
387 if (bufcount > global_expr_max_size)
388 global_expr_max_size = bufcount;
390 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
392 /* print error report */
393 printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
394 global_lineno, buffer);
395 fprintf(l, "%s", error_report);
398 printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
402 retval = check_eval(buffer, error_report);
403 fprintf(l, "%s", error_report);
408 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",
412 global_expr_max_size,
413 (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
420 int main(int argc,char **argv)
426 printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
427 printf(" and run them thru the parser, looking for problems\n");
428 printf("Hey-- give me a path to an extensions.conf file!\n");
429 printf(" You can also follow the file path with a series of variable decls,\n");
430 printf(" of the form, varname=value, each separated from the next by spaces.\n");
431 printf(" (this might allow you to avoid division by zero messages, check that math\n");
432 printf(" is being done correctly, etc.)\n");
433 printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
434 printf(" you to possible problems where you might be expecting those operators as part of a string.\n");
435 printf(" (to include operators in a string, wrap with double quotes!)\n");
440 for (argc1=2;argc1 < argc; argc1++) {
441 if ((eq = strchr(argv[argc1],'='))) {
443 set_var(argv[argc1],eq+1);
447 /* parse command args for x=y and set varz */