From c2dd2c6b994668711a123e5fd60162643521a171 Mon Sep 17 00:00:00 2001 From: murf Date: Wed, 24 Jan 2007 17:43:50 +0000 Subject: updated check_expr via 8322 (refactoring of expression checking impl); elfring contributed a nice code reorg, I contributed some time to get it working again, better messages git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.2@52002 f38db490-d61c-443f-a65b-d21fe96a405b --- utils/Makefile | 7 +-- utils/check_expr.c | 149 ++++++++++++++++++++++++++++++++--------------------- 2 files changed, 94 insertions(+), 62 deletions(-) (limited to 'utils') diff --git a/utils/Makefile b/utils/Makefile index a07a39da7..75aab8e3a 100644 --- a/utils/Makefile +++ b/utils/Makefile @@ -20,6 +20,7 @@ ifeq ($(findstring BSD,${OSARCH}),BSD) CFLAGS+=-I$(CROSS_COMPILE_TARGET)/usr/local/include -L$(CROSS_COMPILE_TARGET)/usr/local/lib endif +# to get check_expr, add it to the TARGET list TARGET=stereorize streamplayer ifneq ($(wildcard $(CROSS_COMPILE_TARGET)/usr/include/popt.h)$(wildcard -f $(CROSS_COMPILE_TARGET)/usr/local/include/popt.h),) @@ -55,13 +56,13 @@ stereorize: stereorize.o frame.o $(CC) $(CFLAGS) -o stereorize stereorize.o frame.o -lm ast_expr2.o: ../ast_expr2.c - gcc -g -c -o $@ $< + gcc -g $(CFLAGS) -c -o $@ $< ast_expr2f.o: ../ast_expr2f.c - gcc -g -c -DSTANDALONE -o $@ $< + gcc -g $(CFLAGS) -c -DSTANDALONE -o $@ $< check_expr: check_expr.c ast_expr2.o ast_expr2f.o - $(CC) $(CFLAGS) -o $@ $^ + $(CC) $(CFLAGS) -o $@ check_expr.c ast_expr2.o ast_expr2f.o smsq: smsq.o $(CC) $(CFLAGS) -o smsq ${SOL} smsq.o -lpopt diff --git a/utils/check_expr.c b/utils/check_expr.c index 531af49c0..8be16888a 100644 --- a/utils/check_expr.c +++ b/utils/check_expr.c @@ -23,12 +23,12 @@ #include #include <../include/asterisk/ast_expr.h> -int global_lineno = 1; -int global_expr_count = 0; -int global_expr_max_size = 0; -int global_expr_tot_size = 0; -int global_warn_count = 0; -int global_OK_count = 0; +static int global_lineno = 1; +static int global_expr_count=0; +static int global_expr_max_size=0; +static int global_expr_tot_size=0; +static int global_warn_count=0; +static int global_OK_count=0; struct varz { @@ -54,6 +54,22 @@ void ast_log(int level, const char *file, int line, const char *function, const fflush(stdout); va_end(vars); } +void ast_register_file_version(const char *file, const char *version); +void ast_unregister_file_version(const char *file); + +char *find_var(const char *varname); +void set_var(const char *varname, const char *varval); +unsigned int check_expr(char* buffer, char* error_report); +int check_eval(char *buffer, char *error_report); +void parse_file(const char *fname); + +void ast_register_file_version(const char *file, const char *version) +{ +} + +void ast_unregister_file_version(const char *file) +{ +} char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */ { @@ -75,75 +91,79 @@ void set_var(const char *varname, const char *varval) global_varlist = t; } -int check_expr(char *buffer, char *error_report) +unsigned int check_expr(char* buffer, char* error_report) { - char *cp; - int oplen = 0; - int warn_found = 0; + char* cp; + unsigned int warn_found = 0; error_report[0] = 0; - for (cp=buffer;*cp;cp++) { - - if (*cp == '|' - || *cp == '&' - || *cp == '=' - || *cp == '>' - || *cp == '<' - || *cp == '+' - || *cp == '-' - || *cp == '*' - || *cp == '/' - || *cp == '%' - || *cp == '?' - || *cp == ':' - /* || *cp == '(' - || *cp == ')' These are pretty hard to track, as they are in funcalls, etc. */ - || *cp == '"') { - if (*cp == '"') { + for (cp = buffer; *cp; ++cp) + { + switch (*cp) + { + case '"': /* skip to the other end */ - cp++; - while (*cp && *cp != '"') - cp++; - if (*cp == 0) { - fprintf(stderr,"Trouble? Unterminated double quote found at line %d\n", - global_lineno); - } - } - else { - if ((*cp == '>'||*cp == '<' ||*cp=='!') && (*(cp+1) == '=')) { - oplen = 2; + while (*(++cp) && *cp != '"') ; + + if (*cp == 0) + { + fprintf(stderr, + "Trouble? Unterminated double quote found at line %d\n", + global_lineno); } - else { - oplen = 1; + break; + + case '>': + case '<': + case '!': + if ( (*(cp + 1) == '=') + && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) ) + { + char msg[200]; + snprintf(msg, + sizeof(msg), + "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", + global_lineno, *cp, *(cp + 1)); + strcat(error_report, msg); + ++global_warn_count; + ++warn_found; } + break; - if ((cp > buffer && *(cp-1) != ' ') || *(cp+oplen) != ' ') { - char tbuf[1000]; - if (oplen == 1) - sprintf(tbuf,"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", - global_lineno, *cp); - else - sprintf(tbuf,"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", - global_lineno, *cp, *(cp+1)); - strcat(error_report,tbuf); - - global_warn_count++; - warn_found++; + case '|': + case '&': + case '=': + case '+': + case '-': + case '*': + case '/': + case '%': + case '?': + case ':': + if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') ) + { + char msg[200]; + snprintf(msg, + sizeof(msg), + "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", + global_lineno, *cp ); + strcat(error_report, msg); + ++global_warn_count; + ++warn_found; } - } + break; } } + return warn_found; } int check_eval(char *buffer, char *error_report) { - char *cp, *ep, *xp; + char *cp, *ep; char s[4096]; char evalbuf[80000]; - int oplen = 0; - int warn_found = 0; int result; error_report[0] = 0; @@ -219,7 +239,7 @@ void parse_file(const char *fname) char buffer[30000]; /* I sure hope no expr gets this big! */ if (!f) { - fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n"); + fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname); exit(20); } if (!l) { @@ -301,13 +321,23 @@ void parse_file(const char *fname) } -main(int argc,char **argv) +int main(int argc,char **argv) { int argc1; char *eq; if (argc < 2) { + printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n"); + printf(" and run them thru the parser, looking for problems\n"); printf("Hey-- give me a path to an extensions.conf file!\n"); + printf(" You can also follow the file path with a series of variable decls,\n"); + printf(" of the form, varname=value, each separated from the next by spaces.\n"); + printf(" (this might allow you to avoid division by zero messages, check that math\n"); + printf(" is being done correctly, etc.)\n"); + printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n"); + printf(" you to possible problems where you might be expecting those operators as part of a string.\n"); + printf(" (to include operators in a string, wrap with double quotes!)\n"); + exit(19); } global_varlist = 0; @@ -321,4 +351,5 @@ main(int argc,char **argv) /* parse command args for x=y and set varz */ parse_file(argv[1]); + return 0; } -- cgit v1.2.3