Blocked 80689, the fix to ael.y; already in trunk.
[asterisk/asterisk.git] / utils / check_expr.c
index 531af49..3eeff07 100644 (file)
 #include <stdarg.h>
 #include <string.h>
 #include <stdlib.h>
+
+struct ast_channel 
+{
+       char x; /* basically empty! */
+};
+
+#include <../include/asterisk/compat.h>
 #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;
+#define AST_API_MODULE 1
+#include "asterisk/inline_api.h"
+
+#define AST_API_MODULE 1
+#include "asterisk/lock.h"
+
+#include "asterisk/strings.h"
+
+/* I included this from utils.c, so as not to have everything in that .c
+   file included */
+/*!
+ * core handler for dynamic strings.
+ * This is not meant to be called directly, but rather through the
+ * various wrapper macros
+ *     ast_str_set(...)
+ *     ast_str_append(...)
+ *     ast_str_set_va(...)
+ *     ast_str_append_va(...)
+ */
+int __ast_str_helper(struct ast_str **buf, size_t max_len,
+       int append, const char *fmt, va_list ap)
+{
+       int res, need;
+       int offset = (append && (*buf)->len) ? (*buf)->used : 0;
+
+       if (max_len < 0)
+               max_len = (*buf)->len;  /* don't exceed the allocated space */
+       /*
+        * Ask vsnprintf how much space we need. Remember that vsnprintf
+        * does not count the final '\0' so we must add 1.
+        */
+       res = vsnprintf((*buf)->str + offset, (*buf)->len - offset, fmt, ap);
+
+       need = res + offset + 1;
+       /*
+        * If there is not enough space and we are below the max length,
+        * reallocate the buffer and return a message telling to retry.
+        */
+       if (need > (*buf)->len && (max_len == 0 || (*buf)->len < max_len) ) {
+               if (max_len && max_len < need)  /* truncate as needed */
+                       need = max_len;
+               else if (max_len == 0)  /* if unbounded, give more room for next time */
+                       need += 16 + need/4;
+               if (ast_str_make_space(buf, need)) {
+                       return AST_DYNSTR_BUILD_FAILED;
+               }
+               (*buf)->str[offset] = '\0';     /* Truncate the partial write. */
+
+               /* va_end() and va_start() must be done before calling
+                * vsnprintf() again. */
+               return AST_DYNSTR_BUILD_RETRY;
+       }
+       /* update space used, keep in mind the truncation */
+       (*buf)->used = (res + offset > (*buf)->len) ? (*buf)->len : res + offset;
+
+       return res;
+}
+
+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 +119,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 */
 {
@@ -66,84 +147,101 @@ char *find_var(const char *varname) /* the list should be pretty short, if there
        return 0;
 }
 
+void set_var(const char *varname, const char *varval);
+
 void set_var(const char *varname, const char *varval)
 {
-       struct varz *t = calloc(1,sizeof(struct varz));
+       struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
+       if (!t)
+               return;
        strcpy(t->varname, varname);
        strcpy(t->varval, varval);
        t->next = global_varlist;
        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);
+
+struct ast_custom_function *ast_custom_function_find(const char *name);
+
+struct ast_custom_function *ast_custom_function_find(const char *name)
+{
+       return 0;
+}
+
 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;
@@ -199,7 +297,7 @@ int check_eval(char *buffer, char *error_report)
        *ep++ = 0;
 
        /* now, run the test */
-       result = ast_expr(evalbuf, s, sizeof(s));
+       result = ast_expr(evalbuf, s, sizeof(s),NULL);
        if (result) {
                sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
                return 1;
@@ -210,6 +308,8 @@ int check_eval(char *buffer, char *error_report)
 }
 
 
+void parse_file(const char *fname);
+
 void parse_file(const char *fname)
 {
        FILE *f = fopen(fname,"r");
@@ -219,7 +319,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 +401,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 +431,5 @@ main(int argc,char **argv)
        /* parse command args for x=y and set varz */
        
        parse_file(argv[1]);
+       return 0;
 }