check_expr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. /*** MODULEINFO
  19. <support_level>extended</support_level>
  20. ***/
  21. #include "asterisk.h"
  22. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  23. #include "asterisk/ast_expr.h"
  24. #define AST_API_MODULE 1
  25. #include "asterisk/inline_api.h"
  26. #define AST_API_MODULE 1
  27. #include "asterisk/lock.h"
  28. #ifndef DEBUG_THREADS
  29. enum ast_lock_type {
  30. AST_MUTEX,
  31. AST_RDLOCK,
  32. AST_WRLOCK,
  33. };
  34. #endif
  35. #ifdef DEBUG_THREADLOCALS
  36. #define MALLOC_FAILURE_MSG \
  37. ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file);
  38. void * attribute_malloc _ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func);
  39. void * attribute_malloc _ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func)
  40. {
  41. void *p;
  42. if (!(p = calloc(num, len)))
  43. MALLOC_FAILURE_MSG;
  44. return p;
  45. }
  46. #endif
  47. #ifdef DEBUG_THREADS
  48. #if !defined(LOW_MEMORY)
  49. #ifdef HAVE_BKTR
  50. void ast_store_lock_info(enum ast_lock_type type, const char *filename,
  51. int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt);
  52. void ast_store_lock_info(enum ast_lock_type type, const char *filename,
  53. int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt)
  54. {
  55. /* not a lot to do in a standalone w/o threading! */
  56. }
  57. void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt);
  58. void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt)
  59. {
  60. /* not a lot to do in a standalone w/o threading! */
  61. }
  62. int __ast_bt_get_addresses(struct ast_bt *bt);
  63. int __ast_bt_get_addresses(struct ast_bt *bt)
  64. {
  65. /* Suck it, you stupid utils directory! */
  66. return 0;
  67. }
  68. char **__ast_bt_get_symbols(void **addresses, size_t num_frames);
  69. char **__ast_bt_get_symbols(void **addresses, size_t num_frames)
  70. {
  71. char **foo = calloc(num_frames, sizeof(char *) + 1);
  72. if (foo) {
  73. int i;
  74. for (i = 0; i < num_frames; i++) {
  75. foo[i] = (char *) foo + sizeof(char *) * num_frames;
  76. }
  77. }
  78. return foo;
  79. }
  80. #else
  81. void ast_store_lock_info(enum ast_lock_type type, const char *filename,
  82. int line_num, const char *func, const char *lock_name, void *lock_addr);
  83. void ast_store_lock_info(enum ast_lock_type type, const char *filename,
  84. int line_num, const char *func, const char *lock_name, void *lock_addr)
  85. {
  86. /* not a lot to do in a standalone w/o threading! */
  87. }
  88. void ast_remove_lock_info(void *lock_addr);
  89. void ast_remove_lock_info(void *lock_addr)
  90. {
  91. /* not a lot to do in a standalone w/o threading! */
  92. }
  93. #endif /* HAVE_BKTR */
  94. void ast_suspend_lock_info(void *lock_addr)
  95. {
  96. }
  97. void ast_restore_lock_info(void *lock_addr)
  98. {
  99. }
  100. void ast_mark_lock_acquired(void *);
  101. void ast_mark_lock_acquired(void *foo)
  102. {
  103. /* not a lot to do in a standalone w/o threading! */
  104. }
  105. #endif
  106. #endif /* DEBUG_THREADS */
  107. static int global_lineno = 1;
  108. static int global_expr_count=0;
  109. static int global_expr_max_size=0;
  110. static int global_expr_tot_size=0;
  111. static int global_warn_count=0;
  112. static int global_OK_count=0;
  113. struct varz
  114. {
  115. char varname[100]; /* a really ultra-simple, space-wasting linked list of var=val data */
  116. char varval[1000]; /* if any varname is bigger than 100 chars, or val greater than 1000, then **CRASH** */
  117. struct varz *next;
  118. };
  119. struct varz *global_varlist;
  120. /* Our own version of ast_log, since the expr parser uses it. */
  121. void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__((format(printf,5,6)));
  122. void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
  123. {
  124. va_list vars;
  125. va_start(vars,fmt);
  126. printf("LOG: lev:%d file:%s line:%d func: %s ",
  127. level, file, line, function);
  128. vprintf(fmt, vars);
  129. fflush(stdout);
  130. va_end(vars);
  131. }
  132. //void ast_register_file_version(const char *file, const char *version);
  133. //void ast_unregister_file_version(const char *file);
  134. char *find_var(const char *varname);
  135. void set_var(const char *varname, const char *varval);
  136. unsigned int check_expr(char* buffer, char* error_report);
  137. int check_eval(char *buffer, char *error_report);
  138. void parse_file(const char *fname);
  139. void ast_register_file_version(const char *file, const char *version);
  140. void ast_register_file_version(const char *file, const char *version) { }
  141. #if !defined(LOW_MEMORY)
  142. int ast_add_profile(const char *x, uint64_t scale) { return 0;}
  143. #endif
  144. int ast_atomic_fetchadd_int_slow(volatile int *p, int v)
  145. {
  146. int ret;
  147. ret = *p;
  148. *p += v;
  149. return ret;
  150. }
  151. void ast_unregister_file_version(const char *file);
  152. void ast_unregister_file_version(const char *file)
  153. {
  154. }
  155. char *find_var(const char *varname) /* the list should be pretty short, if there's any list at all */
  156. {
  157. struct varz *t;
  158. for (t= global_varlist; t; t = t->next) {
  159. if (!strcmp(t->varname, varname)) {
  160. return t->varval;
  161. }
  162. }
  163. return 0;
  164. }
  165. void set_var(const char *varname, const char *varval);
  166. void set_var(const char *varname, const char *varval)
  167. {
  168. struct varz *t = (struct varz*)calloc(1,sizeof(struct varz));
  169. if (!t)
  170. return;
  171. strcpy(t->varname, varname);
  172. strcpy(t->varval, varval);
  173. t->next = global_varlist;
  174. global_varlist = t;
  175. }
  176. unsigned int check_expr(char* buffer, char* error_report)
  177. {
  178. char* cp;
  179. unsigned int warn_found = 0;
  180. error_report[0] = 0;
  181. for (cp = buffer; *cp; ++cp)
  182. {
  183. switch (*cp)
  184. {
  185. case '"':
  186. /* skip to the other end */
  187. while (*(++cp) && *cp != '"') ;
  188. if (*cp == 0)
  189. {
  190. fprintf(stderr,
  191. "Trouble? Unterminated double quote found at line %d\n",
  192. global_lineno);
  193. }
  194. break;
  195. case '>':
  196. case '<':
  197. case '!':
  198. if ( (*(cp + 1) == '=')
  199. && ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 2) != ' ') ) )
  200. {
  201. char msg[200];
  202. snprintf(msg,
  203. sizeof(msg),
  204. "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",
  205. global_lineno, *cp, *(cp + 1));
  206. strcat(error_report, msg);
  207. ++global_warn_count;
  208. ++warn_found;
  209. }
  210. break;
  211. case '|':
  212. case '&':
  213. case '=':
  214. case '+':
  215. case '-':
  216. case '*':
  217. case '/':
  218. case '%':
  219. case '?':
  220. case ':':
  221. if ( ( (cp > buffer) && (*(cp - 1) != ' ') ) || (*(cp + 1) != ' ') )
  222. {
  223. char msg[200];
  224. snprintf(msg,
  225. sizeof(msg),
  226. "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",
  227. global_lineno, *cp );
  228. strcat(error_report, msg);
  229. ++global_warn_count;
  230. ++warn_found;
  231. }
  232. break;
  233. }
  234. }
  235. return warn_found;
  236. }
  237. int check_eval(char *buffer, char *error_report);
  238. struct ast_custom_function *ast_custom_function_find(const char *name);
  239. struct ast_custom_function *ast_custom_function_find(const char *name)
  240. {
  241. return 0;
  242. }
  243. int check_eval(char *buffer, char *error_report)
  244. {
  245. char *cp, *ep;
  246. char s[4096];
  247. char evalbuf[80000];
  248. int result;
  249. error_report[0] = 0;
  250. ep = evalbuf;
  251. for (cp=buffer;*cp;cp++) {
  252. if (*cp == '$' && *(cp+1) == '{') {
  253. int brack_lev = 1;
  254. char *xp= cp+2;
  255. while (*xp) {
  256. if (*xp == '{')
  257. brack_lev++;
  258. else if (*xp == '}')
  259. brack_lev--;
  260. if (brack_lev == 0)
  261. break;
  262. xp++;
  263. }
  264. if (*xp == '}') {
  265. char varname[200];
  266. char *val;
  267. strncpy(varname,cp+2, xp-cp-2);
  268. varname[xp-cp-2] = 0;
  269. cp = xp;
  270. val = find_var(varname);
  271. if (val) {
  272. char *z = val;
  273. while (*z)
  274. *ep++ = *z++;
  275. }
  276. else {
  277. *ep++ = '5'; /* why not */
  278. *ep++ = '5';
  279. *ep++ = '5';
  280. }
  281. }
  282. else {
  283. printf("Unterminated variable reference at line %d\n", global_lineno);
  284. *ep++ = *cp;
  285. }
  286. }
  287. else if (*cp == '\\') {
  288. /* braindead simple elim of backslash */
  289. cp++;
  290. *ep++ = *cp;
  291. }
  292. else
  293. *ep++ = *cp;
  294. }
  295. *ep++ = 0;
  296. /* now, run the test */
  297. result = ast_expr(evalbuf, s, sizeof(s),NULL);
  298. if (result) {
  299. sprintf(error_report,"line %d, evaluation of $[ %s ] result: %s\n", global_lineno, evalbuf, s);
  300. return 1;
  301. } else {
  302. sprintf(error_report,"line %d, evaluation of $[ %s ] result: ****SYNTAX ERROR****\n", global_lineno, evalbuf);
  303. return 1;
  304. }
  305. }
  306. void parse_file(const char *fname);
  307. void parse_file(const char *fname)
  308. {
  309. FILE *f = fopen(fname,"r");
  310. FILE *l = fopen("expr2_log","w");
  311. int c1;
  312. char last_char= 0;
  313. char buffer[30000]; /* I sure hope no expr gets this big! */
  314. if (!f) {
  315. fprintf(stderr,"Couldn't open %s for reading... need an extensions.conf file to parse!\n",fname);
  316. exit(20);
  317. }
  318. if (!l) {
  319. fprintf(stderr,"Couldn't open 'expr2_log' file for writing... please fix and re-run!\n");
  320. exit(21);
  321. }
  322. global_lineno = 1;
  323. while ((c1 = fgetc(f)) != EOF) {
  324. if (c1 == '\n')
  325. global_lineno++;
  326. else if (c1 == '[') {
  327. if (last_char == '$') {
  328. /* bingo, an expr */
  329. int bracklev = 1;
  330. int bufcount = 0;
  331. int retval;
  332. char error_report[30000];
  333. while ((c1 = fgetc(f)) != EOF) {
  334. if (c1 == '[')
  335. bracklev++;
  336. else if (c1 == ']')
  337. bracklev--;
  338. if (c1 == '\n') {
  339. fprintf(l, "ERROR-- A newline in an expression? Weird! ...at line %d\n", global_lineno);
  340. fclose(f);
  341. fclose(l);
  342. printf("--- ERROR --- A newline in the middle of an expression at line %d!\n", global_lineno);
  343. }
  344. if (bracklev == 0)
  345. break;
  346. buffer[bufcount++] = c1;
  347. }
  348. if (c1 == EOF) {
  349. fprintf(l, "ERROR-- End of File Reached in the middle of an Expr at line %d\n", global_lineno);
  350. fclose(f);
  351. fclose(l);
  352. printf("--- ERROR --- EOF reached in middle of an expression at line %d!\n", global_lineno);
  353. exit(22);
  354. }
  355. buffer[bufcount] = 0;
  356. /* update stats */
  357. global_expr_tot_size += bufcount;
  358. global_expr_count++;
  359. if (bufcount > global_expr_max_size)
  360. global_expr_max_size = bufcount;
  361. retval = check_expr(buffer, error_report); /* check_expr should bump the warning counter */
  362. if (retval != 0) {
  363. /* print error report */
  364. printf("Warning(s) at line %d, expression: $[%s]; see expr2_log file for details\n",
  365. global_lineno, buffer);
  366. fprintf(l, "%s", error_report);
  367. }
  368. else {
  369. printf("OK -- $[%s] at line %d\n", buffer, global_lineno);
  370. global_OK_count++;
  371. }
  372. error_report[0] = 0;
  373. retval = check_eval(buffer, error_report);
  374. fprintf(l, "%s", error_report);
  375. }
  376. }
  377. last_char = c1;
  378. }
  379. 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",
  380. global_expr_count,
  381. global_OK_count,
  382. global_warn_count,
  383. global_expr_max_size,
  384. (global_expr_count) ? global_expr_tot_size/global_expr_count : 0);
  385. fclose(f);
  386. fclose(l);
  387. }
  388. int main(int argc,char **argv)
  389. {
  390. int argc1;
  391. char *eq;
  392. if (argc < 2) {
  393. printf("check_expr -- a program to look thru extensions.conf files for $[...] expressions,\n");
  394. printf(" and run them thru the parser, looking for problems\n");
  395. printf("Hey-- give me a path to an extensions.conf file!\n");
  396. printf(" You can also follow the file path with a series of variable decls,\n");
  397. printf(" of the form, varname=value, each separated from the next by spaces.\n");
  398. printf(" (this might allow you to avoid division by zero messages, check that math\n");
  399. printf(" is being done correctly, etc.)\n");
  400. printf(" Note that messages about operators not being surrounded by spaces is merely to alert\n");
  401. printf(" you to possible problems where you might be expecting those operators as part of a string.\n");
  402. printf(" (to include operators in a string, wrap with double quotes!)\n");
  403. exit(19);
  404. }
  405. global_varlist = 0;
  406. for (argc1=2;argc1 < argc; argc1++) {
  407. if ((eq = strchr(argv[argc1],'='))) {
  408. *eq = 0;
  409. set_var(argv[argc1],eq+1);
  410. }
  411. }
  412. /* parse command args for x=y and set varz */
  413. parse_file(argv[1]);
  414. return 0;
  415. }