check_expr.c 12 KB

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