check_expr.c 12 KB

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