config.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Configuration File Parser
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include <time.h>
  19. #include <asterisk/config.h>
  20. #include <asterisk/config_pvt.h>
  21. #include <asterisk/cli.h>
  22. #include <asterisk/lock.h>
  23. #include <asterisk/options.h>
  24. #include <asterisk/logger.h>
  25. #include <asterisk/utils.h>
  26. #include "asterisk.h"
  27. #include "astconf.h"
  28. static int ast_cust_config=0;
  29. struct ast_config *(*global_load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
  30. #ifdef PRESERVE_COMMENTS
  31. ,struct ast_comment_struct *
  32. #endif
  33. );
  34. AST_MUTEX_DEFINE_STATIC(ast_cust_config_lock);
  35. static struct ast_config_reg *ast_cust_config_list;
  36. static char *config_conf_file = "extconfig.conf";
  37. static char *strip(char *buf)
  38. {
  39. char *start;
  40. /* Strip off trailing whitespace, returns, etc */
  41. while (!ast_strlen_zero(buf) && (buf[strlen(buf)-1]<33))
  42. buf[strlen(buf)-1] = '\0';
  43. start = buf;
  44. /* Strip off leading whitespace, returns, etc */
  45. while (*start && (*start < 33))
  46. *start++ = '\0';
  47. return start;
  48. }
  49. #ifdef PRESERVE_COMMENTS
  50. static void free_comments(struct ast_comment *com)
  51. {
  52. struct ast_comment *l;
  53. while (com) {
  54. l = com;
  55. com = com->next;
  56. free(l);
  57. }
  58. }
  59. #endif
  60. void ast_destroy(struct ast_config *ast)
  61. {
  62. struct ast_category *cat, *catn;
  63. struct ast_variable *v, *vn;
  64. if (!ast)
  65. return;
  66. cat = ast->root;
  67. while(cat) {
  68. v = cat->root;
  69. while(v) {
  70. vn = v;
  71. free(v->name);
  72. free(v->value);
  73. #ifdef PRESERVE_COMMENTS
  74. free_comments(v->precomments);
  75. free_comments(v->sameline);
  76. #endif
  77. v = v->next;
  78. free(vn);
  79. }
  80. catn = cat;
  81. #ifdef PRESERVE_COMMENTS
  82. free_comments(cat->precomments);
  83. free_comments(cat->sameline);
  84. #endif
  85. cat = cat->next;
  86. free(catn);
  87. }
  88. #ifdef PRESERVE_COMMENTS
  89. free_comments(ast->trailingcomments);
  90. #endif
  91. free(ast);
  92. }
  93. int ast_true(char *s)
  94. {
  95. if (!s)
  96. return 0;
  97. /* Determine if this is a true value */
  98. if (!strcasecmp(s, "yes") ||
  99. !strcasecmp(s, "true") ||
  100. !strcasecmp(s, "y") ||
  101. !strcasecmp(s, "t") ||
  102. !strcasecmp(s, "1") ||
  103. !strcasecmp(s, "on"))
  104. return -1;
  105. return 0;
  106. }
  107. int ast_false(char *s)
  108. {
  109. if (!s)
  110. return 0;
  111. /* Determine if this is a false value */
  112. if (!strcasecmp(s, "no") ||
  113. !strcasecmp(s, "false") ||
  114. !strcasecmp(s, "n") ||
  115. !strcasecmp(s, "f") ||
  116. !strcasecmp(s, "0") ||
  117. !strcasecmp(s, "off"))
  118. return -1;
  119. return 0;
  120. }
  121. struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
  122. {
  123. struct ast_category *cat;
  124. cat = config->root;
  125. while(cat) {
  126. if (cat->name == category)
  127. return cat->root;
  128. cat = cat->next;
  129. }
  130. cat = config->root;
  131. while(cat) {
  132. if (!strcasecmp(cat->name, category))
  133. return cat->root;
  134. cat = cat->next;
  135. }
  136. return NULL;
  137. }
  138. char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
  139. {
  140. struct ast_variable *v;
  141. if (category) {
  142. v = ast_variable_browse(config, category);
  143. while (v) {
  144. if (value == v->name)
  145. return v->value;
  146. v=v->next;
  147. }
  148. v = ast_variable_browse(config, category);
  149. while (v) {
  150. if (!strcasecmp(value, v->name))
  151. return v->value;
  152. v=v->next;
  153. }
  154. } else {
  155. struct ast_category *cat;
  156. cat = config->root;
  157. while(cat) {
  158. v = cat->root;
  159. while (v) {
  160. if (!strcasecmp(value, v->name))
  161. return v->value;
  162. v=v->next;
  163. }
  164. cat = cat->next;
  165. }
  166. }
  167. return NULL;
  168. }
  169. #ifdef PRESERVE_COMMENTS
  170. int ast_variable_delete(struct ast_config *cfg, char *category, char *variable, char *value)
  171. {
  172. struct ast_variable *v, *pv, *bv, *bpv;
  173. struct ast_category *cat;
  174. cat = cfg->root;
  175. while(cat) {
  176. if (cat->name == category) {
  177. break;
  178. }
  179. cat = cat->next;
  180. }
  181. if (!cat) {
  182. cat = cfg->root;
  183. while(cat) {
  184. if (!strcasecmp(cat->name, category)) {
  185. break;
  186. }
  187. cat = cat->next;
  188. }
  189. }
  190. if (!cat)
  191. return -1;
  192. v = cat->root;
  193. pv = NULL;
  194. while (v) {
  195. if ((variable == v->name) && (!value || !strcmp(v->value, value)))
  196. break;
  197. pv = v;
  198. v=v->next;
  199. }
  200. if (!v) {
  201. /* Get the last one that looks like it */
  202. bv = NULL;
  203. bpv = NULL;
  204. v = cat->root;
  205. pv = NULL;
  206. while (v) {
  207. if (!strcasecmp(variable, v->name) && (!value || !strcmp(v->value, value))) {
  208. bv = v;
  209. bpv = pv;
  210. }
  211. pv = v;
  212. v=v->next;
  213. }
  214. v = bv;
  215. }
  216. if (v) {
  217. /* Unlink from original position */
  218. if (pv)
  219. pv->next = v->next;
  220. else
  221. cat->root = v->next;
  222. v->next = NULL;
  223. free(v->name);
  224. if (v->value)
  225. free(v->value);
  226. free_comments(v->sameline);
  227. free_comments(v->precomments);
  228. return 0;
  229. }
  230. return -1;
  231. }
  232. int ast_category_delete(struct ast_config *cfg, char *category)
  233. {
  234. struct ast_variable *v, *pv;
  235. struct ast_category *cat, *cprev;
  236. cat = cfg->root;
  237. cprev = NULL;
  238. while(cat) {
  239. if (cat->name == category) {
  240. break;
  241. }
  242. cprev = cat;
  243. cat = cat->next;
  244. }
  245. if (!cat) {
  246. cat = cfg->root;
  247. cprev = NULL;
  248. while(cat) {
  249. if (!strcasecmp(cat->name, category)) {
  250. break;
  251. }
  252. cprev = cat;
  253. cat = cat->next;
  254. }
  255. }
  256. if (!cat)
  257. return -1;
  258. /* Unlink it */
  259. if (cprev)
  260. cprev->next = cat->next;
  261. else
  262. cfg->root = cat->next;
  263. v = cat->root;
  264. while (v) {
  265. pv = v;
  266. v=v->next;
  267. if (pv->value)
  268. free(pv->value);
  269. if (pv->name)
  270. free(pv->name);
  271. free_comments(pv->sameline);
  272. free_comments(pv->precomments);
  273. free(pv);
  274. }
  275. free_comments(cat->sameline);
  276. free_comments(cat->precomments);
  277. free(cat);
  278. return 0;
  279. }
  280. struct ast_variable *ast_variable_append_modify(struct ast_config *config, char *category, char *variable, char *value, int newcat, int newvar, int move)
  281. {
  282. struct ast_variable *v, *pv=NULL, *bv, *bpv;
  283. struct ast_category *cat, *pcat;
  284. cat = config->root;
  285. if (!newcat) {
  286. while(cat) {
  287. if (cat->name == category) {
  288. break;
  289. }
  290. cat = cat->next;
  291. }
  292. if (!cat) {
  293. cat = config->root;
  294. while(cat) {
  295. if (!strcasecmp(cat->name, category)) {
  296. break;
  297. }
  298. cat = cat->next;
  299. }
  300. }
  301. }
  302. if (!cat) {
  303. cat = malloc(sizeof(struct ast_category));
  304. if (!cat)
  305. return NULL;
  306. memset(cat, 0, sizeof(struct ast_category));
  307. strncpy(cat->name, category, sizeof(cat->name) - 1);
  308. if (config->root) {
  309. /* Put us at the end */
  310. pcat = config->root;
  311. while(pcat->next)
  312. pcat = pcat->next;
  313. pcat->next = cat;
  314. } else {
  315. /* We're the first one */
  316. config->root = cat;
  317. }
  318. }
  319. if (!newvar) {
  320. v = cat->root;
  321. pv = NULL;
  322. while (v) {
  323. if (variable == v->name)
  324. break;
  325. pv = v;
  326. v=v->next;
  327. }
  328. if (!v) {
  329. /* Get the last one that looks like it */
  330. bv = NULL;
  331. bpv = NULL;
  332. v = cat->root;
  333. pv = NULL;
  334. while (v) {
  335. if (!strcasecmp(variable, v->name)) {
  336. bv = v;
  337. bpv = pv;
  338. }
  339. pv = v;
  340. v=v->next;
  341. }
  342. v = bv;
  343. }
  344. } else v = NULL;
  345. if (v && move) {
  346. /* Unlink from original position */
  347. if (pv)
  348. pv->next = v->next;
  349. else
  350. cat->root = v->next;
  351. v->next = NULL;
  352. }
  353. if (!v) {
  354. v = malloc(sizeof(struct ast_variable));
  355. if (!v)
  356. return NULL;
  357. memset(v, 0, sizeof(struct ast_variable));
  358. v->name = strdup(variable);
  359. move = 1;
  360. }
  361. if (v->value)
  362. free(v->value);
  363. if (value)
  364. v->value = strdup(value);
  365. else
  366. v->value = strdup("");
  367. if (move) {
  368. if (cat->root) {
  369. pv = cat->root;
  370. while (pv->next)
  371. pv = pv->next;
  372. pv->next = v;
  373. } else {
  374. cat->root = v;
  375. }
  376. }
  377. return v;
  378. }
  379. #endif
  380. int ast_category_exist(struct ast_config *config, char *category_name)
  381. {
  382. struct ast_category *category = NULL;
  383. category = config->root;
  384. while(category) {
  385. if (!strcasecmp(category->name,category_name))
  386. return 1;
  387. category = category->next;
  388. }
  389. return 0;
  390. }
  391. #ifdef PRESERVE_COMMENTS
  392. static struct ast_comment *build_comment(char *cmt)
  393. {
  394. struct ast_comment *c;
  395. int len = strlen(cmt) + 1;
  396. c = malloc(sizeof(struct ast_comment) + len);
  397. if (c) {
  398. /* Memset the header */
  399. memset(c, 0, sizeof(struct ast_comment));
  400. /* Copy the rest */
  401. strcpy(c->cmt, cmt);
  402. }
  403. return c;
  404. }
  405. #endif
  406. static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
  407. #ifdef PRESERVE_COMMENTS
  408. , struct ast_comment_struct *acs
  409. #endif
  410. );
  411. static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, char *configfile, int includelevel
  412. #ifdef PRESERVE_COMMENTS
  413. ,struct ast_comment_struct *acs
  414. #endif
  415. )
  416. {
  417. char *c;
  418. char *cur;
  419. char *arg=NULL;
  420. struct ast_config_reg *reg=NULL;
  421. struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
  422. #ifdef PRESERVE_COMMENTS
  423. ,struct ast_comment_struct *
  424. #endif
  425. );
  426. struct ast_variable *v;
  427. #ifdef PRESERVE_COMMENTS
  428. struct ast_comment *com = NULL;
  429. #endif
  430. int object;
  431. /* Strip off lines using ; as comment */
  432. c = strchr(buf, ';');
  433. while (c) {
  434. if ((c == buf) || (*(c-1) != '\\')) {
  435. *c = '\0';
  436. #ifdef PRESERVE_COMMENTS
  437. c++;
  438. if (*c != '!')
  439. com = build_comment(c);
  440. #endif
  441. } else {
  442. *(c-1) = ';';
  443. memmove(c, c + 1, strlen(c + 1));
  444. }
  445. c = strchr(c + 1, ';');
  446. }
  447. cur = strip(buf);
  448. if (!ast_strlen_zero(cur)) {
  449. /* Actually parse the entry */
  450. if (cur[0] == '[') {
  451. /* A category header */
  452. c = strchr(cur, ']');
  453. if (c) {
  454. *c = 0;
  455. *_tmpc = malloc(sizeof(struct ast_category));
  456. if (!*_tmpc) {
  457. ast_destroy(tmp);
  458. ast_log(LOG_WARNING,
  459. "Out of memory, line %d\n", lineno);
  460. return -1;
  461. }
  462. memset(*_tmpc, 0, sizeof(struct ast_category));
  463. strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
  464. (*_tmpc)->root = NULL;
  465. #ifdef PRESERVE_COMMENTS
  466. (*_tmpc)->precomments = acs->root;
  467. (*_tmpc)->sameline = com;
  468. #endif
  469. if (!tmp->prev)
  470. tmp->root = *_tmpc;
  471. else
  472. tmp->prev->next = *_tmpc;
  473. tmp->prev = *_tmpc;
  474. #ifdef PRESERVE_COMMENTS
  475. acs->root = NULL;
  476. acs->prev = NULL;
  477. #endif
  478. *_last = NULL;
  479. } else {
  480. ast_log(LOG_WARNING,
  481. "parse error: no closing ']', line %d of %s\n", lineno, configfile);
  482. }
  483. } else if (cur[0] == '#') {
  484. /* A directive */
  485. cur++;
  486. c = cur;
  487. while(*c && (*c > 32)) c++;
  488. if (*c) {
  489. *c = '\0';
  490. c++;
  491. /* Find real argument */
  492. while(*c && (*c < 33)) c++;
  493. if (!*c)
  494. c = NULL;
  495. } else
  496. c = NULL;
  497. if (!strcasecmp(cur, "include")) {
  498. /* A #include */
  499. if (c) {
  500. while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
  501. /* Get rid of leading mess */
  502. cur = c;
  503. while (!ast_strlen_zero(cur)) {
  504. c = cur + strlen(cur) - 1;
  505. if ((*c == '>') || (*c == '<') || (*c == '\"'))
  506. *c = '\0';
  507. else
  508. break;
  509. }
  510. if((c = strchr(cur,':'))) {
  511. *c = '\0';
  512. c++;
  513. arg = c;
  514. }
  515. if (includelevel < MAX_INCLUDE_LEVEL) {
  516. if(arg && cur) {
  517. load_func = NULL;
  518. if(ast_cust_config_list)
  519. reg = get_ast_cust_config(cur);
  520. if(reg && reg->func)
  521. load_func = reg->func;
  522. if(load_func) {
  523. ast_log(LOG_NOTICE,"External Include '%s' via '%s' config engine\n",arg,cur);
  524. load_func(arg,tmp, _tmpc, _last, includelevel
  525. #ifdef PRESERVE_COMMENTS
  526. ,&acs
  527. #endif
  528. );
  529. }
  530. else
  531. ast_log(LOG_WARNING,"Cant Find Registered Config Engine [%s] for [%s]\n",cur,arg);
  532. }
  533. else {
  534. __ast_load(cur, tmp, _tmpc, _last, includelevel + 1
  535. #ifdef PRESERVE_COMMENTS
  536. ,acs
  537. #endif
  538. );
  539. }
  540. } else
  541. ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
  542. } else
  543. ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
  544. /* Strip off leading and trailing "'s and <>'s */
  545. }
  546. else
  547. ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
  548. } else {
  549. /* Just a line (variable = value) */
  550. if (!*_tmpc) {
  551. ast_log(LOG_WARNING,
  552. "parse error: No category context for line %d of %s\n", lineno, configfile);
  553. ast_destroy(tmp);
  554. return -1;
  555. }
  556. c = strchr(cur, '=');
  557. if (c) {
  558. *c = 0;
  559. c++;
  560. /* Ignore > in => */
  561. if (*c== '>') {
  562. object = 1;
  563. c++;
  564. } else
  565. object = 0;
  566. v = malloc(sizeof(struct ast_variable));
  567. if (v) {
  568. memset(v, 0, sizeof(struct ast_variable));
  569. v->next = NULL;
  570. v->name = strdup(strip(cur));
  571. v->value = strdup(strip(c));
  572. v->lineno = lineno;
  573. v->object = object;
  574. /* Put and reset comments */
  575. #ifdef PRESERVE_COMMENTS
  576. v->precomments = acs->root;
  577. v->sameline = com;
  578. acs->prev = NULL;
  579. acs->root = NULL;
  580. #endif
  581. v->blanklines = 0;
  582. if (*_last)
  583. (*_last)->next = v;
  584. else
  585. (*_tmpc)->root = v;
  586. *_last = v;
  587. } else {
  588. ast_destroy(tmp);
  589. ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
  590. return -1;
  591. }
  592. } else {
  593. ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
  594. }
  595. }
  596. } else {
  597. /* store any comments if there are any */
  598. #ifdef PRESERVE_COMMENTS
  599. if (com) {
  600. if (acs->prev)
  601. acs->prev->next = com;
  602. else
  603. acs->root = com;
  604. acs->prev = com;
  605. } else {
  606. if (*_last)
  607. (*_last)->blanklines++;
  608. }
  609. #endif
  610. }
  611. return 0;
  612. }
  613. #ifdef PRESERVE_COMMENTS
  614. static void dump_comments(FILE *f, struct ast_comment *comment)
  615. {
  616. while (comment) {
  617. fprintf(f, ";%s", comment->cmt);
  618. comment = comment->next;
  619. }
  620. }
  621. #endif
  622. int ast_save(char *configfile, struct ast_config *cfg, char *generator)
  623. {
  624. FILE *f;
  625. char fn[256];
  626. char date[256]="";
  627. time_t t;
  628. struct ast_variable *var;
  629. struct ast_category *cat;
  630. int blanklines = 0;
  631. if (configfile[0] == '/') {
  632. strncpy(fn, configfile, sizeof(fn)-1);
  633. } else {
  634. snprintf(fn, sizeof(fn), "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
  635. }
  636. time(&t);
  637. strncpy(date, ctime(&t), sizeof(date) - 1);
  638. if ((f = fopen(fn, "w"))) {
  639. if ((option_verbose > 1) && !option_debug)
  640. ast_verbose( VERBOSE_PREFIX_2 "Saving '%s': ", fn);
  641. fprintf(f, ";!\n");
  642. fprintf(f, ";! Automatically generated configuration file\n");
  643. fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
  644. fprintf(f, ";! Generator: %s\n", generator);
  645. fprintf(f, ";! Creation Date: %s", date);
  646. fprintf(f, ";!\n");
  647. cat = cfg->root;
  648. while(cat) {
  649. #ifdef PRESERVE_COMMENTS
  650. /* Dump any precomments */
  651. dump_comments(f, cat->precomments);
  652. #endif
  653. /* Dump section with any appropriate comment */
  654. #ifdef PRESERVE_COMMENTS
  655. if (cat->sameline)
  656. fprintf(f, "[%s] ; %s\n", cat->name, cat->sameline->cmt);
  657. else
  658. #endif
  659. fprintf(f, "[%s]\n", cat->name);
  660. var = cat->root;
  661. while(var) {
  662. #ifdef PRESERVE_COMMENTS
  663. dump_comments(f, var->precomments);
  664. #endif
  665. if (var->sameline)
  666. fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
  667. else
  668. fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
  669. if (var->blanklines) {
  670. blanklines = var->blanklines;
  671. while (blanklines) {
  672. fprintf(f, "\n");
  673. blanklines--;
  674. }
  675. }
  676. var = var->next;
  677. }
  678. #if 0
  679. /* Put an empty line */
  680. fprintf(f, "\n");
  681. #endif
  682. cat = cat->next;
  683. }
  684. #ifdef PRESERVE_COMMENTS
  685. dump_comments(f, cfg->trailingcomments);
  686. #endif
  687. } else {
  688. if (option_debug)
  689. printf("Unable to open for writing: %s\n", fn);
  690. else if (option_verbose > 1)
  691. printf( "Unable to write (%s)", strerror(errno));
  692. return -1;
  693. }
  694. fclose(f);
  695. return 0;
  696. }
  697. static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
  698. #ifdef PRESERVE_COMMENTS
  699. , struct ast_comment_struct *acs
  700. #endif
  701. )
  702. {
  703. char fn[256];
  704. char buf[8192];
  705. FILE *f;
  706. int lineno=0;
  707. int master=0;
  708. struct ast_config_reg *reg=NULL;
  709. struct ast_config *(*load_func)(char *, struct ast_config *,struct ast_category **,struct ast_variable **,int
  710. #ifdef PRESERVE_COMMENTS
  711. ,struct ast_comment_struct *
  712. #endif
  713. );
  714. load_func=NULL;
  715. if (strcmp(configfile,config_conf_file) && strcmp(configfile,"asterisk.conf") && ast_cust_config_list) {
  716. if (global_load_func) {
  717. load_func = global_load_func;
  718. } else {
  719. reg = get_ast_cust_config_keyword(configfile);
  720. if (reg && reg->func) {
  721. load_func = reg->func;
  722. } else {
  723. reg = get_ast_cust_config_keyword("global");
  724. if (reg && reg->func)
  725. global_load_func = load_func = reg->func;
  726. }
  727. }
  728. if (load_func) {
  729. ast_log(LOG_NOTICE,"Loading Config %s via %s engine\n",configfile,reg && reg->name ? reg->name : "global");
  730. tmp = load_func(configfile,tmp, _tmpc, _last, includelevel
  731. #ifdef PRESERVE_COMMENTS
  732. ,&acs
  733. #endif
  734. );
  735. if (tmp)
  736. return tmp;
  737. }
  738. }
  739. if (configfile[0] == '/') {
  740. strncpy(fn, configfile, sizeof(fn)-1);
  741. } else {
  742. snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
  743. }
  744. if ((option_verbose > 1) && !option_debug) {
  745. ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
  746. fflush(stdout);
  747. }
  748. if ((f = fopen(fn, "r"))) {
  749. if (option_debug)
  750. ast_log(LOG_DEBUG, "Parsing %s\n", fn);
  751. else if (option_verbose > 1)
  752. ast_verbose( "Found\n");
  753. if (!tmp) {
  754. tmp = malloc(sizeof(struct ast_config));
  755. if (tmp)
  756. memset(tmp, 0, sizeof(struct ast_config));
  757. master = 1;
  758. }
  759. if (!tmp) {
  760. ast_log(LOG_WARNING, "Out of memory\n");
  761. fclose(f);
  762. return NULL;
  763. }
  764. while(!feof(f)) {
  765. lineno++;
  766. if (fgets(buf, sizeof(buf), f)) {
  767. if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel
  768. #ifdef PRESERVE_COMMENTS
  769. , acs
  770. #endif
  771. )) {
  772. fclose(f);
  773. return NULL;
  774. }
  775. }
  776. }
  777. fclose(f);
  778. } else {
  779. if (option_debug)
  780. ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
  781. else if (option_verbose > 1)
  782. ast_verbose( "Not found (%s)\n", strerror(errno));
  783. }
  784. #ifdef PRESERVE_COMMENTS
  785. if (master) {
  786. /* Keep trailing comments */
  787. tmp->trailingcomments = acs->root;
  788. acs->root = NULL;
  789. acs->prev = NULL;
  790. }
  791. #endif
  792. return tmp;
  793. }
  794. struct ast_config_reg *get_ast_cust_config_keyword(char *name)
  795. {
  796. struct ast_config_reg *reg,*ret=NULL;
  797. int x=0;
  798. ast_mutex_lock(&ast_cust_config_lock);
  799. for (reg=ast_cust_config_list;reg && !ret;reg=reg->next) {
  800. for (x=0;x<reg->keycount && !ret ;x++) {
  801. if (!strcmp(reg->keywords[x],name))
  802. ret=reg;
  803. }
  804. }
  805. ast_mutex_unlock(&ast_cust_config_lock);
  806. return ret;
  807. }
  808. struct ast_config_reg *get_ast_cust_config(char *name)
  809. {
  810. struct ast_config_reg *ptr=NULL;
  811. ast_mutex_lock(&ast_cust_config_lock);
  812. for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
  813. if (!strcmp(name,ptr->name))
  814. break;
  815. }
  816. ast_mutex_unlock(&ast_cust_config_lock);
  817. return ptr;
  818. }
  819. void ast_config_destroy_all(void)
  820. {
  821. struct ast_config_reg *key;
  822. ast_mutex_lock(&ast_cust_config_lock);
  823. for (key=ast_cust_config_list;key;key=key->next) {
  824. ast_config_deregister(key);
  825. }
  826. ast_cust_config_list = NULL;
  827. ast_mutex_unlock(&ast_cust_config_lock);
  828. }
  829. struct ast_config_reg *get_config_registrations(void)
  830. {
  831. return ast_cust_config_list;
  832. }
  833. int ast_config_register(struct ast_config_reg *new)
  834. {
  835. struct ast_config_reg *ptr;
  836. ast_mutex_lock(&ast_cust_config_lock);
  837. new->keycount = 0;
  838. if (!ast_cust_config_list) {
  839. ast_cust_config_list = new;
  840. } else {
  841. for(ptr=ast_cust_config_list;ptr->next;ptr=ptr->next);
  842. ptr->next = new;
  843. }
  844. ast_mutex_unlock(&ast_cust_config_lock);
  845. ast_log(LOG_NOTICE,"Registered Config Engine %s\n",new->name);
  846. return 1;
  847. }
  848. int ast_config_deregister(struct ast_config_reg *del)
  849. {
  850. struct ast_config_reg *ptr=NULL,*last=NULL;
  851. ast_mutex_lock(&ast_cust_config_lock);
  852. for (ptr=ast_cust_config_list;ptr;ptr=ptr->next) {
  853. if (ptr == del) {
  854. if (last && ptr->next) {
  855. last->next = ptr->next;
  856. } else if (last && ! ptr->next) {
  857. last->next = NULL;
  858. } else if (!last && ptr->next) {
  859. ast_cust_config_list = ptr->next;
  860. } else if (!last && !ptr->next) {
  861. ast_cust_config_list = NULL;
  862. }
  863. }
  864. last = ptr;
  865. }
  866. ast_mutex_unlock(&ast_cust_config_lock);
  867. return 0;
  868. }
  869. int ast_cust_config_active(void) {
  870. return (ast_cust_config >0) ? 1 : 0;
  871. }
  872. struct ast_config *ast_load(char *configfile)
  873. {
  874. struct ast_category *tmpc=NULL;
  875. struct ast_variable *last = NULL;
  876. #ifdef PRESERVE_COMMENTS
  877. struct ast_comment_struct acs = { NULL, NULL };
  878. #endif
  879. return __ast_load(configfile, NULL, &tmpc, &last, 0
  880. #ifdef PRESERVE_COMMENTS
  881. ,&acs
  882. #endif
  883. );
  884. }
  885. char *ast_category_browse(struct ast_config *config, char *prev)
  886. {
  887. struct ast_category *cat;
  888. if (!prev) {
  889. if (config->root)
  890. return config->root->name;
  891. else
  892. return NULL;
  893. }
  894. cat = config->root;
  895. while(cat) {
  896. if (cat->name == prev) {
  897. if (cat->next)
  898. return cat->next->name;
  899. else
  900. return NULL;
  901. }
  902. cat = cat->next;
  903. }
  904. cat = config->root;
  905. while(cat) {
  906. if (!strcasecmp(cat->name, prev)) {
  907. if (cat->next)
  908. return cat->next->name;
  909. else
  910. return NULL;
  911. }
  912. cat = cat->next;
  913. }
  914. return NULL;
  915. }
  916. struct ast_config *ast_new_config(void)
  917. {
  918. struct ast_config *config;
  919. config = malloc(sizeof(struct ast_config));
  920. memset(config,0,sizeof(struct ast_config));
  921. return config;
  922. }
  923. struct ast_category *ast_new_category(char *name)
  924. {
  925. struct ast_category *category;
  926. category = malloc(sizeof(struct ast_category));
  927. if (category) {
  928. memset(category,0,sizeof(struct ast_category));
  929. strncpy(category->name,name,sizeof(category->name) - 1);
  930. }
  931. return category;
  932. }
  933. struct ast_variable *ast_new_variable(char *name, char *value)
  934. {
  935. struct ast_variable *variable;
  936. variable = malloc(sizeof(struct ast_variable));
  937. if (variable) {
  938. memset(variable,0,sizeof(struct ast_variable));
  939. variable->object=0;
  940. variable->name = malloc(strlen(name)+1);
  941. if (variable->name) {
  942. strcpy(variable->name,name);
  943. variable->value = malloc(strlen(value)+1);
  944. if (variable->value) {
  945. strcpy(variable->value,value);
  946. } else {
  947. free(variable->name);
  948. variable->name = NULL;
  949. }
  950. }
  951. }
  952. if (!variable->value) {
  953. free(variable);
  954. variable = NULL;
  955. }
  956. return variable;
  957. }
  958. int ast_cust_config_register(struct ast_config_reg *new)
  959. {
  960. ast_config_register(new);
  961. read_ast_cust_config();
  962. return 1;
  963. }
  964. int ast_cust_config_deregister(struct ast_config_reg *new)
  965. {
  966. ast_config_deregister(new);
  967. read_ast_cust_config();
  968. return 1;
  969. }
  970. static void clear_cust_keywords(void)
  971. {
  972. struct ast_config_reg *key;
  973. int x;
  974. ast_mutex_lock(&ast_cust_config_lock);
  975. for (key=get_config_registrations();key;key=key->next) {
  976. for (x=0;x<key->keycount;x++) {
  977. key->keywords[x][0] = '\0';
  978. }
  979. key->keycount=0;
  980. }
  981. ast_mutex_unlock(&ast_cust_config_lock);
  982. }
  983. static int config_command(int fd, int argc, char **argv)
  984. {
  985. struct ast_config_reg *key;
  986. int x;
  987. ast_cli(fd,"\n\n");
  988. ast_mutex_lock(&ast_cust_config_lock);
  989. for (key=get_config_registrations();key;key=key->next) {
  990. ast_cli(fd,"\nConfig Engine: %s\n",key->name);
  991. for (x=0;x<key->keycount;x++)
  992. ast_cli(fd,"===>%s\n",key->keywords[x]);
  993. }
  994. ast_mutex_unlock(&ast_cust_config_lock);
  995. ast_cli(fd,"\n\n");
  996. return 0;
  997. }
  998. static struct ast_cli_entry config_command_struct = {
  999. { "show","config","handles", NULL }, config_command,
  1000. "Show Config Handles", NULL };
  1001. int register_config_cli()
  1002. {
  1003. return ast_cli_register(&config_command_struct);
  1004. }
  1005. int read_ast_cust_config(void)
  1006. {
  1007. char *cfg = config_conf_file;
  1008. struct ast_config *config;
  1009. struct ast_variable *v;
  1010. struct ast_config_reg *ptr;
  1011. struct ast_config_reg *test = NULL;
  1012. clear_cust_keywords();
  1013. config = ast_load(cfg);
  1014. if (config) {
  1015. for (v = ast_variable_browse(config,"settings");v;v=v->next) {
  1016. ptr = get_ast_cust_config(v->value);
  1017. if (ptr) {
  1018. if (ptr->keycount >= CONFIG_KEYWORD_ARRAYLEN) {
  1019. ast_log(LOG_WARNING,"Max Number of Bindings exceeded for %s->%s %d/%d\n",v->name,v->value,ptr->keycount,CONFIG_KEYWORD_ARRAYLEN);
  1020. } else {
  1021. if (strcmp(v->name,config_conf_file) && strcmp(v->name,"asterisk.conf")) {
  1022. if (!(test = get_ast_cust_config_keyword(v->name))) {
  1023. ast_log(LOG_NOTICE,"Binding: %s to %s\n",v->name,v->value);
  1024. strncpy(ptr->keywords[ptr->keycount],v->name,sizeof(ptr->keywords[ptr->keycount]) - 1);
  1025. ptr->keywords[ptr->keycount][sizeof(ptr->keywords[ptr->keycount])-1] = '\0';
  1026. ptr->keycount++;
  1027. }
  1028. } else {
  1029. ast_log(LOG_WARNING,"Cannot bind %s, Permission Denied\n",v->name);
  1030. }
  1031. }
  1032. }
  1033. }
  1034. ast_destroy(config);
  1035. }
  1036. return 0;
  1037. }