check_expr.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  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.
  13. *
  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.
  17. */
  18. #include <stdio.h>
  19. #include <stddef.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <../include/asterisk/ast_expr.h>
  24. static int global_lineno = 1;
  25. static int global_expr_count=0;
  26. static int global_expr_max_size=0;
  27. static int global_expr_tot_size=0;
  28. static int global_warn_count=0;
  29. static int global_OK_count=0;
  30. struct varz
  31. {
  32. char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
  33. char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
  34. struct varz *next;
  35. };
  36. struct varz *global_varlist;
  37. /* Our own version of ast_log, since the expr parser uses it. */
  38. void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
  39. void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
  40. {
  41. va_list vars;
  42. va_start(vars,fmt);
  43. printf("LOG: lev:%d file:%s line:%d func: %s ",
  44. level, file, line, function);
  45. vprintf(fmt, vars);
  46. fflush(stdout);
  47. va_end(vars);
  48. }
  49. void ast_register_file_version(const char *file, const char *version);
  50. void ast_unregister_file_version(const char *file);
  51. char *find_var(const char *varname);
  52. void set_var(const char *varname, const char *varval);
  53. unsigned int check_expr(char* buffer, char* error_report);
  54. int check_eval(char *buffer, char *error_report);
  55. void parse_file(const char *fname);
  56. void ast_register_file_version(const char *file, const char *version)
  57. {
  58. }
  59. void ast_unregister_file_version(const char *file)
  60. {
  61. }
  62. char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
  63. {
  64. struct varz *t;
  65. for (t= global_varlist; t; t = t->next) {
  66. if (!strcmp(t->varname, varname)) {
  67. return t->varval;
  68. }
  69. }
  70. return 0;
  71. }
  72. void set_var(const char *varname, const char *varval)
  73. {
  74. struct varz *t = calloc(1,sizeof(struct varz));
  75. strcpy(t->varname, varname);
  76. strcpy(t->varval, varval);
  77. t->next = global_varlist;
  78. global_varlist = t;
  79. }
  80. unsigned int check_expr(char* buffer, char* error_report)
  81. {
  82. char* cp;
  83. unsigned int warn_found = 0;
  84. error_report[0] = 0;
  85. for (cp = buffer; *cp; ++cp)
  86. {
  87. switch (*cp)
  88. {
  89. case '"':
  90. /* skip to the other end */
  91. while (*(++cp) && *cp != '"') ;
  92. if (*cp == 0)
  93. {
  94. fprintf(stderr,
  95. "Trouble? Unterminated double quote found at line %d\n",
  96. global_lineno);
  97. }
  98. break;
  99. case '>':
  100. case '<':
  101. case '!':
  102. if ( (*(cp + 1) == '=')
  103. && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
  104. {
  105. char msg[200];
  106. snprintf(msg,
  107. sizeof(msg),
  108. "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",
  109. global_lineno, *cp, *(cp + 1));
  110. strcat(error_report, msg);
  111. ++global_warn_count;
  112. ++warn_found;
  113. }
  114. break;
  115. case '|':
  116. case '&':
  117. case '=':
  118. case '+':
  119. case '-':
  120. case '*':
  121. case '/':
  122. case '%':
  123. case '?':
  124. case ':':
  125. if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
  126. {
  127. char msg[200];
  128. snprintf(msg,
  129. sizeof(msg),
  130. "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",
  131. global_lineno, *cp );
  132. strcat(error_report, msg);
  133. ++global_warn_count;
  134. ++warn_found;
  135. }
  136. break;
  137. }
  138. }
  139. return warn_found;
  140. }
  141. int check_eval(char *buffer, char *error_report)
  142. {
  143. char *cp, *ep;
  144. char s[4096];
  145. char evalbuf[80000];
  146. int result;
  147. error_report[0] = 0;
  148. ep = evalbuf;
  149. for (cp=buffer;*cp;cp++) {
  150. if (*cp == '$' && *(cp+1) == '{') {
  151. int brack_lev = 1;
  152. char *xp= cp+2;
  153. while (*xp) {
  154. if (*xp == '{')
  155. brack_lev++;
  156. else if (*xp == '}')
  157. brack_lev--;
  158. if (brack_lev == 0)
  159. break;
  160. xp++;
  161. }
  162. if (*xp == '}') {
  163. char varname[200];
  164. char *val;
  165. strncpy(varname,cp+2, xp-cp-2);
  166. varname[xp-cp-2] = 0;
  167. cp = xp;
  168. val = find_var(varname);
  169. if (val) {
  170. char *z = val;
  171. while (*z)
  172. *ep++ = *z++;
  173. }
  174. else {
  175. *ep++ = '5'; /* why not */
  176. *ep++ = '5';
  177. *ep++ = '5';
  178. }
  179. }
  180. else {
  181. printf("Unterminated variable reference at line %d\n", global_lineno);
  182. *ep++ = *cp;
  183. }
  184. }
  185. else if (*cp == '\\') {
  186. /* braindead simple elim of backslash */
  187. cp++;
  188. *ep++ = *cp;
  189. }
  190. else
  191. *ep++ = *cp;
  192. }
  193. *ep++ = 0;
  194. /* now, run the test */
  195. result = ast_expr(evalbuf, s, sizeof(s));
  196. if (result) {
  197. sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
  198. return 1;
  199. } else {
  200. sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
  201. return 1;
  202. }
  203. }
  204. void parse_file(const char *fname)
  205. {
  206. FILE *f = fopen(fname,"r");
  207. FILE *l = fopen("expr2_log","w");
  208. int c1;
  209. char last_char= 0;
  210. char buffer[30000]; /* I sure hope no expr gets this big! */
  211. if (!f) {
  212. fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
  213. exit(20);
  214. }
  215. if (!l) {
  216. fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
  217. exit(21);
  218. }
  219. global_lineno = 1;
  220. while ((c1 = fgetc(f)) != EOF) {
  221. if (c1 == '\n')
  222. global_lineno++;
  223. else if (c1 == '[') {
  224. if (last_char == '$') {
  225. /* bingo, an expr */
  226. int bracklev = 1;
  227. int bufcount = 0;
  228. int retval;
  229. char error_report[30000];
  230. while ((c1 = fgetc(f)) != EOF) {
  231. if (c1 == '[')
  232. bracklev++;
  233. else if (c1 == ']')
  234. bracklev--;
  235. if (c1 == '\n') {
  236. fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
  237. fclose(f);
  238. fclose(l);
  239. printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
  240. }
  241. if (bracklev == 0)
  242. break;
  243. buffer[bufcount++] = c1;
  244. }
  245. if (c1 == EOF) {
  246. fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
  247. fclose(f);
  248. fclose(l);
  249. printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
  250. exit(22);
  251. }
  252. buffer[bufcount] = 0;
  253. /* update stats */
  254. global_expr_tot_size += bufcount;
  255. global_expr_count++;
  256. if (bufcount > global_expr_max_size)
  257. global_expr_max_size = bufcount;
  258. retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
  259. if (retval != 0) {
  260. /* print error report */
  261. printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
  262. global_lineno, buffer);
  263. fprintf(l, "%s", error_report);
  264. }
  265. else {
  266. printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
  267. global_OK_count++;
  268. }
  269. error_report[0] = 0;
  270. retval = check_eval(buffer, error_report);
  271. fprintf(l, "%s", error_report);
  272. }
  273. }
  274. last_char = c1;
  275. }
  276. 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",
  277. global_expr_count,
  278. global_OK_count,
  279. global_warn_count,
  280. global_expr_max_size,
  281. (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
  282. fclose(f);
  283. fclose(l);
  284. }
  285. int main(int argc,char **argv)
  286. {
  287. int argc1;
  288. char *eq;
  289. if (argc < 2) {
  290. printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
  291. printf(" and run them thru the parser, looking for problems\n");
  292. printf("Hey-- give me a path to an extensions.conf file!\n");
  293. printf(" You can also follow the file path with a series of variable decls,\n");
  294. printf(" of the form, varname=value, each separated from the next by spaces.\n");
  295. printf(" (this might allow you to avoid division by zero messages, check that math\n");
  296. printf(" is being done correctly, etc.)\n");
  297. printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
  298. printf(" you to possible problems where you might be expecting those operators as part of a string.\n");
  299. printf(" (to include operators in a string, wrap with double quotes!)\n");
  300. exit(19);
  301. }
  302. global_varlist = 0;
  303. for (argc1=2;argc1 < argc; argc1++) {
  304. if ((eq = strchr(argv[argc1],'='))) {
  305. *eq = 0;
  306. set_var(argv[argc1],eq+1);
  307. }
  308. }
  309. /* parse command args for x=y and set varz */
  310. parse_file(argv[1]);
  311. return 0;
  312. }