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>
32 #define AST_API_MODULE 1
33 #include "asterisk/inline_api.h"
34 #include "asterisk/strings.h"
36 /* I included this from utils.c, so as not to have everything in that .c
39 * core handler for dynamic strings.
40 * This is not meant to be called directly, but rather through the
41 * various wrapper macros
45 * ast_str_append_va(...)
47 int __ast_str_helper(struct ast_str **buf, size_t max_len,
48 int append, const char *fmt, va_list ap)
51 int offset = (append && (*buf)->len) ? (*buf)->used : 0;
54 max_len = (*buf)->len; /* don't exceed the allocated space */
56 * Ask vsnprintf how much space we need. Remember that vsnprintf
57 * does not count the final '\0' so we must add 1.
59 res = vsnprintf((*buf)->str + offset, (*buf)->len - offset, fmt, ap);
61 need = res + offset + 1;
63 * If there is not enough space and we are below the max length,
64 * reallocate the buffer and return a message telling to retry.
66 if (need > (*buf)->len && (max_len == 0 || (*buf)->len < max_len) ) {
67 if (max_len && max_len < need) /* truncate as needed */
69 else if (max_len == 0) /* if unbounded, give more room for next time */
71 if (ast_str_make_space(buf, need)) {
72 return AST_DYNSTR_BUILD_FAILED;
74 (*buf)->str[offset] = '\0'; /* Truncate the partial write. */
76 /* va_end() and va_start() must be done before calling
77 * vsnprintf() again. */
78 return AST_DYNSTR_BUILD_RETRY;
80 /* update space used, keep in mind the truncation */
81 (*buf)->used = (res + offset > (*buf)->len) ? (*buf)->len : res + offset;
86 static int global_lineno = 1;
87 static int global_expr_count=0;
88 static int global_expr_max_size=0;
89 static int global_expr_tot_size=0;
90 static int global_warn_count=0;
91 static int global_OK_count=0;
95 char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
96 char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
100 struct varz *global_varlist;
102 /* Our own version of ast_log, since the expr parser uses it. */
104 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
106 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
111 printf("LOG: lev:%d file:%s line:%d func: %s ",
112 level, file, line, function);
117 void ast_register_file_version(const char *file, const char *version);
118 void ast_unregister_file_version(const char *file);
120 char *find_var(const char *varname);
121 void set_var(const char *varname, const char *varval);
122 unsigned int check_expr(char* buffer, char* error_report);
123 int check_eval(char *buffer, char *error_report);
124 void parse_file(const char *fname);
126 void ast_register_file_version(const char *file, const char *version)
130 void ast_unregister_file_version(const char *file)
134 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
137 for (t= global_varlist; t; t = t->next) {
138 if (!strcmp(t->varname, varname)) {
145 void set_var(const char *varname, const char *varval);
147 void set_var(const char *varname, const char *varval)
149 struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
152 strcpy(t->varname, varname);
153 strcpy(t->varval, varval);
154 t->next = global_varlist;
158 unsigned int check_expr(char* buffer, char* error_report)
161 unsigned int warn_found = 0;
165 for (cp = buffer; *cp; ++cp)
170 /* skip to the other end */
171 while (*(++cp) && *cp != '"') ;
176 "Trouble? Unterminated double quote found at line %d\n",
184 if ( (*(cp + 1) == '=')
185 && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
190 "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",
191 global_lineno, *cp, *(cp + 1));
192 strcat(error_report, msg);
208 if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
213 "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",
214 global_lineno, *cp );
215 strcat(error_report, msg);
226 int check_eval(char *buffer, char *error_report);
228 struct ast_custom_function *ast_custom_function_find(const char *name);
230 struct ast_custom_function *ast_custom_function_find(const char *name)
235 int check_eval(char *buffer, char *error_report)
245 for (cp=buffer;*cp;cp++) {
246 if (*cp == '$' && *(cp+1) == '{') {
264 strncpy(varname,cp+2, xp-cp-2);
265 varname[xp-cp-2] = 0;
267 val = find_var(varname);
274 *ep++ = '5'; /* why not */
280 printf("Unterminated variable reference at line %d\n", global_lineno);
284 else if (*cp == '\\') {
285 /* braindead simple elim of backslash */
294 /* now, run the test */
295 result = ast_expr(evalbuf, s, sizeof(s),NULL);
297 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
300 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
306 void parse_file(const char *fname);
308 void parse_file(const char *fname)
310 FILE *f = fopen(fname,"r");
311 FILE *l = fopen("expr2_log","w");
314 char buffer[30000]; /* I sure hope no expr gets this big! */
317 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
321 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
327 while ((c1 = fgetc(f)) != EOF) {
330 else if (c1 == '[') {
331 if (last_char == '$') {
336 char error_report[30000];
338 while ((c1 = fgetc(f)) != EOF) {
344 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
347 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
352 buffer[bufcount++] = c1;
355 fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
358 printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
362 buffer[bufcount] = 0;
364 global_expr_tot_size += bufcount;
366 if (bufcount > global_expr_max_size)
367 global_expr_max_size = bufcount;
369 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
371 /* print error report */
372 printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
373 global_lineno, buffer);
374 fprintf(l, "%s", error_report);
377 printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
381 retval = check_eval(buffer, error_report);
382 fprintf(l, "%s", error_report);
387 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",
391 global_expr_max_size,
392 (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
399 int main(int argc,char **argv)
405 printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
406 printf(" and run them thru the parser, looking for problems\n");
407 printf("Hey-- give me a path to an extensions.conf file!\n");
408 printf(" You can also follow the file path with a series of variable decls,\n");
409 printf(" of the form, varname=value, each separated from the next by spaces.\n");
410 printf(" (this might allow you to avoid division by zero messages, check that math\n");
411 printf(" is being done correctly, etc.)\n");
412 printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
413 printf(" you to possible problems where you might be expecting those operators as part of a string.\n");
414 printf(" (to include operators in a string, wrap with double quotes!)\n");
419 for (argc1=2;argc1 < argc; argc1++) {
420 if ((eq = strchr(argv[argc1],'='))) {
422 set_var(argv[argc1],eq+1);
426 /* parse command args for x=y and set varz */