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 static int global_lineno = 1;
92 static int global_expr_count=0;
93 static int global_expr_max_size=0;
94 static int global_expr_tot_size=0;
95 static int global_warn_count=0;
96 static int global_OK_count=0;
100 char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
101 char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
105 struct varz *global_varlist;
107 /* Our own version of ast_log, since the expr parser uses it. */
109 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
111 void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
116 printf("LOG: lev:%d file:%s line:%d func: %s ",
117 level, file, line, function);
122 void ast_register_file_version(const char *file, const char *version);
123 void ast_unregister_file_version(const char *file);
125 char *find_var(const char *varname);
126 void set_var(const char *varname, const char *varval);
127 unsigned int check_expr(char* buffer, char* error_report);
128 int check_eval(char *buffer, char *error_report);
129 void parse_file(const char *fname);
131 void ast_register_file_version(const char *file, const char *version)
135 void ast_unregister_file_version(const char *file)
139 char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
142 for (t= global_varlist; t; t = t->next) {
143 if (!strcmp(t->varname, varname)) {
150 void set_var(const char *varname, const char *varval);
152 void set_var(const char *varname, const char *varval)
154 struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
157 strcpy(t->varname, varname);
158 strcpy(t->varval, varval);
159 t->next = global_varlist;
163 unsigned int check_expr(char* buffer, char* error_report)
166 unsigned int warn_found = 0;
170 for (cp = buffer; *cp; ++cp)
175 /* skip to the other end */
176 while (*(++cp) && *cp != '"') ;
181 "Trouble? Unterminated double quote found at line %d\n",
189 if ( (*(cp + 1) == '=')
190 && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
195 "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",
196 global_lineno, *cp, *(cp + 1));
197 strcat(error_report, msg);
213 if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
218 "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",
219 global_lineno, *cp );
220 strcat(error_report, msg);
231 int check_eval(char *buffer, char *error_report);
233 struct ast_custom_function *ast_custom_function_find(const char *name);
235 struct ast_custom_function *ast_custom_function_find(const char *name)
240 int check_eval(char *buffer, char *error_report)
250 for (cp=buffer;*cp;cp++) {
251 if (*cp == '$' && *(cp+1) == '{') {
269 strncpy(varname,cp+2, xp-cp-2);
270 varname[xp-cp-2] = 0;
272 val = find_var(varname);
279 *ep++ = '5'; /* why not */
285 printf("Unterminated variable reference at line %d\n", global_lineno);
289 else if (*cp == '\\') {
290 /* braindead simple elim of backslash */
299 /* now, run the test */
300 result = ast_expr(evalbuf, s, sizeof(s),NULL);
302 sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
305 sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
311 void parse_file(const char *fname);
313 void parse_file(const char *fname)
315 FILE *f = fopen(fname,"r");
316 FILE *l = fopen("expr2_log","w");
319 char buffer[30000]; /* I sure hope no expr gets this big! */
322 fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
326 fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
332 while ((c1 = fgetc(f)) != EOF) {
335 else if (c1 == '[') {
336 if (last_char == '$') {
341 char error_report[30000];
343 while ((c1 = fgetc(f)) != EOF) {
349 fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
352 printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
357 buffer[bufcount++] = c1;
360 fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
363 printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
367 buffer[bufcount] = 0;
369 global_expr_tot_size += bufcount;
371 if (bufcount > global_expr_max_size)
372 global_expr_max_size = bufcount;
374 retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
376 /* print error report */
377 printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
378 global_lineno, buffer);
379 fprintf(l, "%s", error_report);
382 printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
386 retval = check_eval(buffer, error_report);
387 fprintf(l, "%s", error_report);
392 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",
396 global_expr_max_size,
397 (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
404 int main(int argc,char **argv)
410 printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
411 printf(" and run them thru the parser, looking for problems\n");
412 printf("Hey-- give me a path to an extensions.conf file!\n");
413 printf(" You can also follow the file path with a series of variable decls,\n");
414 printf(" of the form, varname=value, each separated from the next by spaces.\n");
415 printf(" (this might allow you to avoid division by zero messages, check that math\n");
416 printf(" is being done correctly, etc.)\n");
417 printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
418 printf(" you to possible problems where you might be expecting those operators as part of a string.\n");
419 printf(" (to include operators in a string, wrap with double quotes!)\n");
424 for (argc1=2;argc1 < argc; argc1++) {
425 if ((eq = strchr(argv[argc1],'='))) {
427 set_var(argv[argc1],eq+1);
431 /* parse command args for x=y and set varz */