ael_main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * XXX this file probably need a fair amount of cleanup, at the very least:
  3. *
  4. * - documenting its purpose;
  5. * - removing all unnecessary headers and other stuff from the sources
  6. * it was copied from;
  7. * - fixing the formatting
  8. */
  9. /*** MODULEINFO
  10. <support_level>extended</support_level>
  11. ***/
  12. #include "asterisk.h"
  13. #include <locale.h>
  14. #include <ctype.h>
  15. #include <regex.h>
  16. #include <limits.h>
  17. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  18. #include "asterisk/backtrace.h"
  19. #include "asterisk/channel.h"
  20. #include "asterisk/ast_expr.h"
  21. #include "asterisk/module.h"
  22. #include "asterisk/app.h"
  23. #include "asterisk/lock.h"
  24. #include "asterisk/hashtab.h"
  25. #include "asterisk/ael_structs.h"
  26. #include "asterisk/extconf.h"
  27. int option_debug = 0;
  28. int option_verbose = 0;
  29. #if !defined(LOW_MEMORY)
  30. void ast_register_file_version(const char *file, const char *version) { }
  31. void ast_unregister_file_version(const char *file) { }
  32. #endif
  33. /*** MODULEINFO
  34. <depend>res_ael_share</depend>
  35. ***/
  36. struct namelist
  37. {
  38. char name[100];
  39. char name2[100];
  40. struct namelist *next;
  41. };
  42. struct ast_context
  43. {
  44. int extension_count;
  45. char name[100];
  46. char registrar[100];
  47. struct namelist *includes;
  48. struct namelist *ignorepats;
  49. struct namelist *switches;
  50. struct namelist *eswitches;
  51. struct namelist *includes_last;
  52. struct namelist *ignorepats_last;
  53. struct namelist *switches_last;
  54. struct namelist *eswitches_last;
  55. struct ast_context *next;
  56. };
  57. #define ADD_LAST(headptr,memptr) if(!headptr){ headptr=(memptr); (headptr##_last)=(memptr);} else {(headptr##_last)->next = (memptr); (headptr##_last) = (memptr);}
  58. void destroy_namelist(struct namelist *x);
  59. void destroy_namelist(struct namelist *x)
  60. {
  61. struct namelist *z,*z2;
  62. for(z=x; z; z = z2)
  63. {
  64. z2 = z->next;
  65. z->next = 0;
  66. free(z);
  67. }
  68. }
  69. struct namelist *create_name(const char *name);
  70. struct namelist *create_name(const char *name)
  71. {
  72. struct namelist *x = calloc(1, sizeof(*x));
  73. if (!x)
  74. return NULL;
  75. strncpy(x->name, name, sizeof(x->name) - 1);
  76. return x;
  77. }
  78. struct ast_context *context_list;
  79. struct ast_context *last_context;
  80. struct namelist *globalvars;
  81. struct namelist *globalvars_last;
  82. int conts=0, extens=0, priors=0;
  83. char last_exten[18000];
  84. static char config_dir[PATH_MAX];
  85. static char var_dir[PATH_MAX];
  86. const char *ast_config_AST_CONFIG_DIR = config_dir;
  87. const char *ast_config_AST_VAR_DIR = var_dir;
  88. void ast_cli_register_multiple(void);
  89. int ast_add_extension2(struct ast_context *con,
  90. int replace, const char *extension, int priority, const char *label, const char *callerid,
  91. const char *application, void *data, void (*datad)(void *),
  92. const char *registrar);
  93. void pbx_builtin_setvar(void *chan, void *data);
  94. struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar);
  95. struct ast_context * ast_context_find_or_create(void **extcontexts, void *tab, const char *name, const char *registrar);
  96. void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar);
  97. void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar);
  98. void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar);
  99. void ast_merge_contexts_and_delete(void);
  100. void ast_context_verify_includes(void);
  101. struct ast_context * ast_walk_contexts(void);
  102. void ast_cli_unregister_multiple(void);
  103. void ast_context_destroy(void);
  104. void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...);
  105. char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);
  106. void __ast_verbose(const char *file, int line, const char *func, int level, const char *fmt, ...);
  107. struct ast_app *pbx_findapp(const char *app);
  108. void filter_leading_space_from_exprs(char *str);
  109. void filter_newlines(char *str);
  110. static int quiet = 0;
  111. static int no_comp = 0;
  112. static int use_curr_dir = 0;
  113. static int dump_extensions = 0;
  114. static int FIRST_TIME = 0;
  115. static FILE *dumpfile;
  116. void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
  117. {
  118. va_list vars;
  119. va_start(vars,fmt);
  120. printf("LOG: lev:%d file:%s line:%d func: %s ",
  121. level, file, line, function);
  122. vprintf(fmt, vars);
  123. fflush(stdout);
  124. va_end(vars);
  125. }
  126. struct ast_exten *pbx_find_extension(struct ast_channel *chan,
  127. struct ast_context *bypass,
  128. struct pbx_find_info *q,
  129. const char *context,
  130. const char *exten,
  131. int priority,
  132. const char *label,
  133. const char *callerid,
  134. enum ext_match_t action);
  135. struct ast_exten *pbx_find_extension(struct ast_channel *chan,
  136. struct ast_context *bypass,
  137. struct pbx_find_info *q,
  138. const char *context,
  139. const char *exten,
  140. int priority,
  141. const char *label,
  142. const char *callerid,
  143. enum ext_match_t action)
  144. {
  145. return localized_find_extension(bypass, q, context, exten, priority, label, callerid, action);
  146. }
  147. struct ast_app *pbx_findapp(const char *app)
  148. {
  149. return (struct ast_app*)1; /* so as not to trigger an error */
  150. }
  151. struct ast_custom_function *ast_custom_function_find(const char *name);
  152. struct ast_custom_function *ast_custom_function_find(const char *name)
  153. {
  154. return 0; /* in "standalone" mode, functions are just not avail */
  155. }
  156. #if !defined(LOW_MEMORY)
  157. int ast_add_profile(const char *x, uint64_t scale)
  158. {
  159. if (!no_comp)
  160. printf("Executed ast_add_profile();\n");
  161. return 0;
  162. }
  163. #endif
  164. int ast_loader_register(int (*updater)(void))
  165. {
  166. return 1;
  167. }
  168. int ast_loader_unregister(int (*updater)(void))
  169. {
  170. return 1;
  171. }
  172. void ast_module_register(const struct ast_module_info *x)
  173. {
  174. }
  175. void ast_module_unregister(const struct ast_module_info *x)
  176. {
  177. }
  178. void ast_cli_register_multiple(void)
  179. {
  180. if(!no_comp)
  181. printf("Executed ast_cli_register_multiple();\n");
  182. }
  183. void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count);
  184. void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count)
  185. {
  186. if (cp1 && *cp1)
  187. strncpy(cp2,cp1,AST_MAX_EXTENSION); /* Right now, this routine is ONLY being called for
  188. a possible var substitution on extension names,
  189. so....! */
  190. else
  191. *cp2 = 0;
  192. }
  193. int ast_add_extension2(struct ast_context *con,
  194. int replace, const char *extension, int priority, const char *label, const char *callerid,
  195. const char *application, void *data, void (*datad)(void *),
  196. const char *registrar)
  197. {
  198. priors++;
  199. con->extension_count++;
  200. if (strcmp(extension,last_exten) != 0) {
  201. extens++;
  202. strcpy(last_exten, extension);
  203. }
  204. if (!label) {
  205. label = "(null)";
  206. }
  207. if (!callerid) {
  208. callerid = "(null)";
  209. }
  210. if (!application) {
  211. application = "(null)";
  212. }
  213. if(!no_comp)
  214. printf("Executed ast_add_extension2(context=%s, rep=%d, exten=%s, priority=%d, label=%s, callerid=%s, appl=%s, data=%s, FREE, registrar=%s);\n",
  215. con->name, replace, extension, priority, label, callerid, application, (data?(char*)data:"(null)"), registrar);
  216. if( dump_extensions && dumpfile ) {
  217. struct namelist *n;
  218. if( FIRST_TIME ) {
  219. FIRST_TIME = 0;
  220. if( globalvars )
  221. fprintf(dumpfile,"[globals]\n");
  222. for(n=globalvars;n;n=n->next) {
  223. fprintf(dumpfile, "%s\n", n->name);
  224. }
  225. }
  226. /* print out each extension , possibly the context header also */
  227. if( con != last_context ) {
  228. fprintf(dumpfile,"\n\n[%s]\n", con->name);
  229. last_context = con;
  230. for(n=con->ignorepats;n;n=n->next) {
  231. fprintf(dumpfile, "ignorepat => %s\n", n->name);
  232. }
  233. for(n=con->includes;n;n=n->next) {
  234. fprintf(dumpfile, "include => %s\n", n->name);
  235. }
  236. for(n=con->switches;n;n=n->next) {
  237. fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
  238. }
  239. for(n=con->eswitches;n;n=n->next) {
  240. fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
  241. }
  242. }
  243. if( data ) {
  244. filter_newlines((char*)data);
  245. filter_leading_space_from_exprs((char*)data);
  246. /* in previous versions, commas were converted to '|' to separate
  247. args in app calls, but now, commas are used. There used to be
  248. code here to insert backslashes (escapes) before any commas
  249. that may have been embedded in the app args. This code is no more. */
  250. if( strcmp(label,"(null)") != 0 )
  251. fprintf(dumpfile,"exten => %s,%d(%s),%s(%s)\n", extension, priority, label, application, (char*)data);
  252. else
  253. fprintf(dumpfile,"exten => %s,%d,%s(%s)\n", extension, priority, application, (char*)data);
  254. } else {
  255. if( strcmp(label,"(null)") != 0 )
  256. fprintf(dumpfile,"exten => %s,%d(%s),%s\n", extension, priority, label, application);
  257. else
  258. fprintf(dumpfile,"exten => %s,%d,%s\n", extension, priority, application);
  259. }
  260. }
  261. /* since add_extension2 is responsible for the malloc'd data stuff */
  262. free(data);
  263. return 0;
  264. }
  265. void pbx_builtin_setvar(void *chan, void *data)
  266. {
  267. struct namelist *x = create_name(data);
  268. if(!no_comp)
  269. printf("Executed pbx_builtin_setvar(chan, data=%s);\n", (char*)data);
  270. if( dump_extensions ) {
  271. x = create_name(data);
  272. ADD_LAST(globalvars,x);
  273. }
  274. }
  275. struct ast_context * ast_context_create(void **extcontexts, const char *name, const char *registrar)
  276. {
  277. struct ast_context *x = calloc(1, sizeof(*x));
  278. if (!x)
  279. return NULL;
  280. x->next = context_list;
  281. context_list = x;
  282. if (!no_comp)
  283. printf("Executed ast_context_create(conts, name=%s, registrar=%s);\n", name, registrar);
  284. conts++;
  285. strncpy(x->name, name, sizeof(x->name) - 1);
  286. strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
  287. return x;
  288. }
  289. struct ast_context * ast_context_find_or_create(void **extcontexts, void *tab, const char *name, const char *registrar)
  290. {
  291. struct ast_context *x = calloc(1, sizeof(*x));
  292. if (!x)
  293. return NULL;
  294. x->next = context_list;
  295. context_list = x;
  296. if (!no_comp)
  297. printf("Executed ast_context_find_or_create(conts, name=%s, registrar=%s);\n", name, registrar);
  298. conts++;
  299. strncpy(x->name, name, sizeof(x->name) - 1);
  300. strncpy(x->registrar, registrar, sizeof(x->registrar) - 1);
  301. return x;
  302. }
  303. void ast_context_add_ignorepat2(struct ast_context *con, const char *value, const char *registrar)
  304. {
  305. if(!no_comp)
  306. printf("Executed ast_context_add_ignorepat2(con, value=%s, registrar=%s);\n", value, registrar);
  307. if( dump_extensions ) {
  308. struct namelist *x;
  309. x = create_name(value);
  310. ADD_LAST(con->ignorepats,x);
  311. }
  312. }
  313. void ast_context_add_include2(struct ast_context *con, const char *value, const char *registrar)
  314. {
  315. if(!no_comp)
  316. printf("Executed ast_context_add_include2(con, value=%s, registrar=%s);\n", value, registrar);
  317. if( dump_extensions ) {
  318. struct namelist *x;
  319. x = create_name((char*)value);
  320. ADD_LAST(con->includes,x);
  321. }
  322. }
  323. void ast_context_add_switch2(struct ast_context *con, const char *value, const char *data, int eval, const char *registrar)
  324. {
  325. if(!no_comp)
  326. printf("Executed ast_context_add_switch2(con, value=%s, data=%s, eval=%d, registrar=%s);\n", value, data, eval, registrar);
  327. if( dump_extensions ) {
  328. struct namelist *x;
  329. x = create_name((char*)value);
  330. strncpy(x->name2,data,100);
  331. if( eval ) {
  332. ADD_LAST(con->switches,x);
  333. } else {
  334. ADD_LAST(con->eswitches,x);
  335. }
  336. }
  337. }
  338. void ast_merge_contexts_and_delete(void)
  339. {
  340. if(!no_comp)
  341. printf("Executed ast_merge_contexts_and_delete();\n");
  342. }
  343. void ast_context_verify_includes(void)
  344. {
  345. if(!no_comp)
  346. printf("Executed ast_context_verify_includes();\n");
  347. }
  348. struct ast_context * ast_walk_contexts(void)
  349. {
  350. if(!no_comp)
  351. printf("Executed ast_walk_contexts();\n");
  352. return 0;
  353. }
  354. void ast_cli_unregister_multiple(void)
  355. {
  356. if(!no_comp)
  357. printf("Executed ast_cli_unregister_multiple();\n");
  358. }
  359. void ast_context_destroy(void)
  360. {
  361. if( !no_comp)
  362. printf("Executed ast_context_destroy();\n");
  363. }
  364. const char *ast_get_context_name(struct ast_context *con);
  365. const char *ast_get_context_name(struct ast_context *con)
  366. {
  367. return con ? con->name : NULL;
  368. }
  369. struct ast_exten *ast_walk_context_extensions(struct ast_context *con, struct ast_exten *exten);
  370. struct ast_exten *ast_walk_context_extensions(struct ast_context *con, struct ast_exten *exten)
  371. {
  372. return NULL;
  373. }
  374. struct ast_include *ast_walk_context_includes(struct ast_context *con, struct ast_include *inc);
  375. struct ast_include *ast_walk_context_includes(struct ast_context *con, struct ast_include *inc)
  376. {
  377. return NULL;
  378. }
  379. struct ast_ignorepat *ast_walk_context_ignorepats(struct ast_context *con, struct ast_ignorepat *ip);
  380. struct ast_ignorepat *ast_walk_context_ignorepats(struct ast_context *con, struct ast_ignorepat *ip)
  381. {
  382. return NULL;
  383. }
  384. struct ast_sw *ast_walk_context_switches(struct ast_context *con, struct ast_sw *sw);
  385. struct ast_sw *ast_walk_context_switches(struct ast_context *con, struct ast_sw *sw)
  386. {
  387. return NULL;
  388. }
  389. void filter_leading_space_from_exprs(char *str)
  390. {
  391. /* Mainly for aesthetics */
  392. char *t, *v, *u = str;
  393. while ( u && *u ) {
  394. if( *u == '$' && *(u+1) == '[' ) {
  395. t = u+2;
  396. while( *t == '\n' || *t == '\r' || *t == '\t' || *t == ' ' ) {
  397. v = t;
  398. while ( *v ) {
  399. *v = *(v+1);
  400. v++;
  401. }
  402. }
  403. }
  404. u++;
  405. }
  406. }
  407. void filter_newlines(char *str)
  408. {
  409. /* remove all newlines, returns */
  410. char *t=str;
  411. while( t && *t ) {
  412. if( *t == '\n' || *t == '\r' ) {
  413. *t = ' '; /* just replace newlines and returns with spaces; they act as
  414. token separators, and just blindly removing them could be
  415. harmful. */
  416. }
  417. t++;
  418. }
  419. }
  420. extern struct module_symbols mod_data;
  421. int ael_external_load_module(void);
  422. int main(int argc, char **argv)
  423. {
  424. int i;
  425. struct namelist *n;
  426. struct ast_context *lp,*lp2;
  427. for(i=1;i<argc;i++) {
  428. if( argv[i][0] == '-' && argv[i][1] == 'n' )
  429. no_comp =1;
  430. if( argv[i][0] == '-' && argv[i][1] == 'q' ) {
  431. quiet = 1;
  432. no_comp =1;
  433. }
  434. if( argv[i][0] == '-' && argv[i][1] == 'd' )
  435. use_curr_dir =1;
  436. if( argv[i][0] == '-' && argv[i][1] == 'w' )
  437. dump_extensions =1;
  438. }
  439. if( !quiet ) {
  440. printf("\n(If you find progress and other non-error messages irritating, you can use -q to suppress them)\n");
  441. if( !no_comp )
  442. printf("\n(You can use the -n option if you aren't interested in seeing all the instructions generated by the compiler)\n\n");
  443. if( !use_curr_dir )
  444. printf("\n(You can use the -d option if you want to use the current working directory as the CONFIG_DIR. I will look in this dir for extensions.ael* and its included files)\n\n");
  445. if( !dump_extensions )
  446. printf("\n(You can use the -w option to dump extensions.conf format to extensions.conf.aeldump)\n");
  447. }
  448. if( use_curr_dir ) {
  449. strcpy(config_dir, ".");
  450. localized_use_local_dir();
  451. }
  452. else {
  453. strcpy(config_dir, "/etc/asterisk");
  454. localized_use_conf_dir();
  455. }
  456. strcpy(var_dir, "/var/lib/asterisk");
  457. if( dump_extensions ) {
  458. dumpfile = fopen("extensions.conf.aeldump","w");
  459. if( !dumpfile ) {
  460. printf("\n\nSorry, cannot open extensions.conf.aeldump for writing! Correct the situation and try again!\n\n");
  461. exit(10);
  462. }
  463. }
  464. FIRST_TIME = 1;
  465. ael_external_load_module();
  466. ast_log(4, "ael2_parse", __LINE__, "main", "%d contexts, %d extensions, %d priorities\n", conts, extens, priors);
  467. if( dump_extensions && dumpfile ) {
  468. for( lp = context_list; lp; lp = lp->next ) { /* print out any contexts that didn't have any
  469. extensions in them */
  470. if( lp->extension_count == 0 ) {
  471. fprintf(dumpfile,"\n\n[%s]\n", lp->name);
  472. for(n=lp->ignorepats;n;n=n->next) {
  473. fprintf(dumpfile, "ignorepat => %s\n", n->name);
  474. }
  475. for(n=lp->includes;n;n=n->next) {
  476. fprintf(dumpfile, "include => %s\n", n->name);
  477. }
  478. for(n=lp->switches;n;n=n->next) {
  479. fprintf(dumpfile, "switch => %s/%s\n", n->name, n->name2);
  480. }
  481. for(n=lp->eswitches;n;n=n->next) {
  482. fprintf(dumpfile, "eswitch => %s/%s\n", n->name, n->name2);
  483. }
  484. }
  485. }
  486. }
  487. if( dump_extensions && dumpfile )
  488. fclose(dumpfile);
  489. for( lp = context_list; lp; lp = lp2 ) { /* free the ast_context structs */
  490. lp2 = lp->next;
  491. lp->next = 0;
  492. destroy_namelist(lp->includes);
  493. destroy_namelist(lp->ignorepats);
  494. destroy_namelist(lp->switches);
  495. destroy_namelist(lp->eswitches);
  496. free(lp);
  497. }
  498. return 0;
  499. }
  500. int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b);
  501. int ast_hashtab_compare_contexts(const void *ah_a, const void *ah_b)
  502. {
  503. return 0;
  504. }
  505. unsigned int ast_hashtab_hash_contexts(const void *obj);
  506. unsigned int ast_hashtab_hash_contexts(const void *obj)
  507. {
  508. return 0;
  509. }
  510. #ifdef DEBUG_THREADS
  511. #if !defined(LOW_MEMORY)
  512. void ast_mark_lock_acquired(void *lock_addr)
  513. {
  514. }
  515. #ifdef HAVE_BKTR
  516. void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt)
  517. {
  518. }
  519. void ast_store_lock_info(enum ast_lock_type type, const char *filename,
  520. int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt)
  521. {
  522. }
  523. int __ast_bt_get_addresses(struct ast_bt *bt)
  524. {
  525. return 0;
  526. }
  527. char **__ast_bt_get_symbols(void **addresses, size_t num_frames)
  528. {
  529. char **foo = calloc(num_frames, sizeof(char *) + 1);
  530. if (foo) {
  531. int i;
  532. for (i = 0; i < num_frames; i++) {
  533. foo[i] = (char *) foo + sizeof(char *) * num_frames;
  534. }
  535. }
  536. return foo;
  537. }
  538. #else
  539. void ast_remove_lock_info(void *lock_addr)
  540. {
  541. }
  542. void ast_store_lock_info(enum ast_lock_type type, const char *filename,
  543. int line_num, const char *func, const char *lock_name, void *lock_addr)
  544. {
  545. }
  546. #endif /* HAVE_BKTR */
  547. void ast_suspend_lock_info(void *lock_addr)
  548. {
  549. }
  550. void ast_restore_lock_info(void *lock_addr)
  551. {
  552. }
  553. #endif /* !defined(LOW_MEMORY) */
  554. #endif /* DEBUG_THREADS */