cfg_parser.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /*
  2. * $Id$
  3. * Standalone Configuration File Parser
  4. *
  5. * Copyright (C) 2008 iptelorg GmbH
  6. * Written by Jan Janak <[email protected]>
  7. *
  8. * This file is part of SER, a free SIP server.
  9. *
  10. * SER is free software; you can redistribute it and/or modify it under the
  11. * terms of the GNU General Public License as published by the Free Software
  12. * Foundation; either version 2 of the License, or (at your option) any later
  13. * version.
  14. *
  15. * SER is distributed in the hope that it will be useful, but WITHOUT ANY
  16. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*!
  25. * \file
  26. * \brief SIP-router core ::
  27. * \ingroup core
  28. *
  29. * Module: \ref core
  30. *
  31. * See \ref ConfigEngine
  32. *
  33. * \page ConfigEngine
  34. * In file \ref cfg_parser.c
  35. * Configuration examples
  36. * - \ref ConfigExample1
  37. * - \ref ConfigExample2
  38. * - \ref ConfigExample3
  39. * - \ref ConfigExample4
  40. * - \ref ConfigExample5
  41. * - \ref ConfigExample6
  42. * - \ref ConfigExample7
  43. * - \ref ConfigExample8
  44. *
  45. * <b>Run-time Allocated Destination Variables</b>
  46. * - the destination variable pointers in arrays are assigned at compile time
  47. * - The address of variables allocated on the heap (typically in dynamically allocated
  48. * structures) is not know and thus we need to assign NULL initially and change the pointer
  49. * at runtime.
  50. * (provide an example).
  51. *
  52. * <b>Built-in parsing functions</b>
  53. *
  54. * *_val functions parse the whole option body (including =)
  55. */
  56. /*! \page ConfigExample1 Configuration engine Example 1: Options without values
  57. *
  58. \verbatim
  59. * str file = STR_STATIC_INIT("test.cfg");
  60. * cfg_parser_t* parser;
  61. * int feature_a = 0, feature_b = 0;
  62. *
  63. * cfg_option_t options[] = {
  64. * {"enable_feature_a", .param = &feature_a, .val = 1},
  65. * {"disable_feature_a", .param = &feature_a, .val = 0},
  66. * {"feature_b", .param = &feature_b, .val = 1},
  67. * {0}
  68. * };
  69. *
  70. * if ((parser = cfg_parser_init(&file)) == NULL) {
  71. * ERR("Error while creating parser\n");
  72. * return -1;
  73. * }
  74. *
  75. * cfg_set_options(parser, options);
  76. *
  77. * if (sr_cfg_parse(parser) < 0) {
  78. * ERR("Error while parsing configuration file\n");
  79. * cfg_parser_close(parser);
  80. * return -1;
  81. * }
  82. *
  83. * cfg_parser_close(parser);
  84. \endverbatim
  85. */
  86. /*! \page ConfigExample2 Configuration engine Example 2: Options with integer values
  87. \verbatim
  88. * cfg_option_t options[] = {
  89. * {"max_number", .param = &max_number, .f = cfg_parse_int_val },
  90. * {"extra_checks", .param = &extra_checks, .f = cfg_parse_bool_val},
  91. * {0}
  92. * };
  93. \endverbatim
  94. */
  95. /*! \page ConfigExample3 Configuration engine Example 3: Enum options
  96. \verbatim
  97. * int scope;
  98. *
  99. * cfg_option_t scopes[] = {
  100. * {"base", .param = &scope, .val = 1},
  101. * {"onelevel", .param = &scope, .val = 2},
  102. * {"one", .param = &scope, .val = 3},
  103. * {"subtree", .param = &scope, .val = 4},
  104. * {"sub", .param = &scope, .val = 5},
  105. * {"children", .param = &scope, .val = 6},
  106. * {0}
  107. * };
  108. *
  109. * cfg_option_t options[] = {
  110. * {"scope", .param = scopes, .f = cfg_parse_enum_val},
  111. * {0}
  112. * };
  113. \endverbatim
  114. */
  115. /*! \page ConfigExample4 Configuration engine Example 4: Options with string values
  116. \verbatim
  117. * str filename = STR_NULL;
  118. *
  119. * cfg_option_t options[] = {
  120. * {"filename", .param = &filename, .f = cfg_parse_str_val},
  121. * {0}
  122. * };
  123. *
  124. * - By default the function returns a pointer to an internal buffer which will be destroyed
  125. * by a subsequent call
  126. * - There are flags to tell the function to copy the resuting string in a pkg, shm, glibc,
  127. * or static buffers
  128. \endverbatim
  129. */
  130. /*! \page ConfigExample5 Configuration engine Example 5: Custom value parsing
  131. * TBD
  132. */
  133. /*! \page ConfigExample6 Configuration engine Example 6: Parsing Sections
  134. * TBD
  135. */
  136. /*! \page ConfigExample7 Configuration engine Example 7: Default Options
  137. * TBD
  138. */
  139. /*! \page ConfigExample8 Configuration engine Example 8: Memory management of strings
  140. *
  141. * Data types with fixed size are easy, they can be copied into a pre-allocated memory
  142. * buffer, strings cannot because their length is unknown.
  143. */
  144. #include "cfg_parser.h"
  145. #include "mem/mem.h"
  146. #include "mem/shm_mem.h"
  147. #include "dprint.h"
  148. #include "trim.h"
  149. #include "ut.h"
  150. #include <string.h>
  151. #include <stdlib.h>
  152. #include <stdio.h>
  153. #include <libgen.h>
  154. /*! \brief The states of the lexical scanner */
  155. enum st {
  156. ST_S, /*!< Begin */
  157. ST_A, /*!< Alphanumeric */
  158. ST_AE, /*!< Alphanumeric escaped */
  159. ST_Q, /*!< Quoted */
  160. ST_QE, /*!< Quoted escaped */
  161. ST_C, /*!< Comment */
  162. ST_CE, /*!< Comment escaped */
  163. ST_E, /*!< Escaped */
  164. };
  165. /*! \brief Test for alphanumeric characters */
  166. #define IS_ALPHA(c) \
  167. (((c) >= 'a' && (c) <= 'z') || \
  168. ((c) >= 'A' && (c) <= 'Z') || \
  169. ((c) >= '0' && (c) <= '9') || \
  170. (c) == '_')
  171. /*! \brief Test for delimiter characters */
  172. #define IS_DELIM(c) \
  173. ((c) == '=' || \
  174. (c) == ':' || \
  175. (c) == ';' || \
  176. (c) == '.' || \
  177. (c) == ',' || \
  178. (c) == '?' || \
  179. (c) == '[' || \
  180. (c) == ']' || \
  181. (c) == '/' || \
  182. (c) == '@' || \
  183. (c) == '!' || \
  184. (c) == '$' || \
  185. (c) == '%' || \
  186. (c) == '&' || \
  187. (c) == '*' || \
  188. (c) == '(' || \
  189. (c) == ')' || \
  190. (c) == '-' || \
  191. (c) == '+' || \
  192. (c) == '|' || \
  193. (c) == '\'')
  194. /*! \brief Whitespace characters */
  195. #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\r')
  196. #define IS_QUOTE(c) ((c) == '\"') /* Quote characters */
  197. #define IS_COMMENT(c) ((c) == '#') /* Characters that start comments */
  198. #define IS_ESCAPE(c) ((c) == '\\') /* Escape characters */
  199. #define IS_EOL(c) ((c) == '\n') /* End of line */
  200. /*! \brief
  201. * Append character to the value of current token
  202. */
  203. #define PUSH(c) \
  204. if (token->val.len >= MAX_TOKEN_LEN) { \
  205. ERR("%s:%d:%d: Token too long\n", \
  206. st->file, st->line, st->col); \
  207. return -1; \
  208. } \
  209. if (token->val.len == 0) { \
  210. token->start.line = st->line; \
  211. token->start.col = st->col; \
  212. } \
  213. token->val.s[token->val.len++] = (c);
  214. /*! \brief
  215. * Return current token from the lexical analyzer
  216. */
  217. #define RETURN(c) \
  218. token->end.line = st->line; \
  219. token->end.col = st->col; \
  220. token->type = (c); \
  221. print_token(token); \
  222. return 0;
  223. /*! \brief
  224. * Get next character and update counters
  225. */
  226. #define READ_CHAR \
  227. c = fgetc(st->f); \
  228. if (IS_EOL(c)) { \
  229. st->line++; \
  230. st->col = 0; \
  231. } else { \
  232. st->col++; \
  233. }
  234. cfg_option_t cfg_bool_values[] = {
  235. {"yes", .val = 1},
  236. {"true", .val = 1},
  237. {"enable", .val = 1},
  238. {"enabled", .val = 1},
  239. {"1", .val = 1},
  240. {"on", .val = 1},
  241. {"no", .val = 0},
  242. {"false", .val = 0},
  243. {"disable", .val = 0},
  244. {"disabled", .val = 0},
  245. {"0", .val = 0},
  246. {"off", .val = 0},
  247. {0}
  248. };
  249. static void print_token(cfg_token_t* token)
  250. {
  251. #ifdef EXTRA_DEBUG
  252. int i, j;
  253. char* buf;
  254. if ((buf = pkg_malloc(token->val.len * 2)) == NULL) {
  255. DBG("token(%d, '%.*s', <%d,%d>-<%d,%d>)\n",
  256. token->type, STR_FMT(&token->val),
  257. token->start.line, token->start.col,
  258. token->end.line, token->end.col);
  259. } else {
  260. for(i = 0, j = 0; i < token->val.len; i++) {
  261. switch(token->val.s[i]) {
  262. case '\n': buf[j++] = '\\'; buf[j++] = 'n'; break;
  263. case '\r': buf[j++] = '\\'; buf[j++] = 'r'; break;
  264. case '\t': buf[j++] = '\\'; buf[j++] = 't'; break;
  265. case '\0': buf[j++] = '\\'; buf[j++] = '0'; break;
  266. case '\\': buf[j++] = '\\'; buf[j++] = '\\'; break;
  267. default: buf[j++] = token->val.s[i];
  268. }
  269. }
  270. DBG("token(%d, '%.*s', <%d,%d>-<%d,%d>)\n",
  271. token->type, j, buf,
  272. token->start.line, token->start.col,
  273. token->end.line, token->end.col);
  274. pkg_free(buf);
  275. }
  276. #endif /* EXTRA_DEBUG */
  277. }
  278. int cfg_get_token(cfg_token_t* token, cfg_parser_t* st, unsigned int flags)
  279. {
  280. static int look_ahead = EOF;
  281. int c;
  282. enum st state;
  283. state = ST_S;
  284. token->val.s = token->buf;
  285. token->val.len = 0;
  286. if (look_ahead != EOF) {
  287. c = look_ahead;
  288. look_ahead = EOF;
  289. } else {
  290. READ_CHAR;
  291. }
  292. while(c != EOF) {
  293. switch(state) {
  294. case ST_S:
  295. if (flags & CFG_EXTENDED_ALPHA) {
  296. if (IS_WHITESPACE(c)) {
  297. /* Do nothing */
  298. } else if (IS_ALPHA(c) ||
  299. IS_ESCAPE(c) ||
  300. IS_DELIM(c)) {
  301. PUSH(c);
  302. state = ST_A;
  303. } else if (IS_QUOTE(c)) {
  304. state = ST_Q;
  305. } else if (IS_COMMENT(c)) {
  306. state = ST_C;
  307. } else if (IS_EOL(c)) {
  308. PUSH(c);
  309. RETURN(c);
  310. } else {
  311. ERR("%s:%d:%d: Invalid character 0x%x\n",
  312. st->file, st->line, st->col, c);
  313. return -1;
  314. }
  315. } else {
  316. if (IS_WHITESPACE(c)) {
  317. /* Do nothing */
  318. } else if (IS_ALPHA(c)) {
  319. PUSH(c);
  320. state = ST_A;
  321. } else if (IS_QUOTE(c)) {
  322. state = ST_Q;
  323. } else if (IS_COMMENT(c)) {
  324. state = ST_C;
  325. } else if (IS_ESCAPE(c)) {
  326. state = ST_E;
  327. } else if (IS_DELIM(c) || IS_EOL(c)) {
  328. PUSH(c);
  329. RETURN(c);
  330. } else {
  331. ERR("%s:%d:%d: Invalid character 0x%x\n",
  332. st->file, st->line, st->col, c);
  333. return -1;
  334. }
  335. }
  336. break;
  337. case ST_A:
  338. if (flags & CFG_EXTENDED_ALPHA) {
  339. if (IS_ALPHA(c) ||
  340. IS_DELIM(c) ||
  341. IS_QUOTE(c)) {
  342. PUSH(c);
  343. } else if (IS_ESCAPE(c)) {
  344. state = ST_AE;
  345. } else if (IS_COMMENT(c) || IS_EOL(c) || IS_WHITESPACE(c)) {
  346. look_ahead = c;
  347. RETURN(CFG_TOKEN_ALPHA);
  348. } else {
  349. ERR("%s:%d:%d: Invalid character 0x%x\n",
  350. st->file, st->line, st->col, c);
  351. return -1;
  352. }
  353. } else {
  354. if (IS_ALPHA(c)) {
  355. PUSH(c);
  356. } else if (IS_ESCAPE(c)) {
  357. state = ST_AE;
  358. } else if (IS_WHITESPACE(c) ||
  359. IS_DELIM(c) ||
  360. IS_QUOTE(c) ||
  361. IS_COMMENT(c) ||
  362. IS_EOL(c)) {
  363. look_ahead = c;
  364. RETURN(CFG_TOKEN_ALPHA);
  365. } else {
  366. ERR("%s:%d:%d: Invalid character 0x%x\n",
  367. st->file, st->line, st->col, c);
  368. return -1;
  369. }
  370. }
  371. break;
  372. case ST_AE:
  373. if (IS_COMMENT(c) ||
  374. IS_QUOTE(c) ||
  375. IS_ESCAPE(c)) {
  376. PUSH(c);
  377. } else if (c == 'r') {
  378. PUSH('\r');
  379. } else if (c == 'n') {
  380. PUSH('\n');
  381. } else if (c == 't') {
  382. PUSH('\t');
  383. } else if (c == ' ') {
  384. PUSH(' ');
  385. } else if (IS_EOL(c)) {
  386. /* Do nothing */
  387. } else {
  388. ERR("%s:%d:%d: Unsupported escape character 0x%x\n",
  389. st->file, st->line, st->col, c);
  390. return -1;
  391. }
  392. state = ST_A;
  393. break;
  394. case ST_Q:
  395. if (IS_QUOTE(c)) {
  396. RETURN(CFG_TOKEN_STRING);
  397. } else if (IS_ESCAPE(c)) {
  398. state = ST_QE;
  399. break;
  400. } else {
  401. PUSH(c);
  402. }
  403. break;
  404. case ST_QE:
  405. if (IS_ESCAPE(c) ||
  406. IS_QUOTE(c)) {
  407. PUSH(c);
  408. } else if (c == 'n') {
  409. PUSH('\n');
  410. } else if (c == 'r') {
  411. PUSH('\r');
  412. } else if (c == 't') {
  413. PUSH('\t');
  414. } else if (IS_EOL(c)) {
  415. /* Do nothing */
  416. } else {
  417. ERR("%s:%d:%d: Unsupported escape character 0x%x\n",
  418. st->file, st->line, st->col, c);
  419. return -1;
  420. }
  421. state = ST_Q;
  422. break;
  423. case ST_C:
  424. if (IS_ESCAPE(c)) {
  425. state = ST_CE;
  426. } else if (IS_EOL(c)) {
  427. state = ST_S;
  428. continue; /* Do not read a new char, return EOL */
  429. } else {
  430. /* Do nothing */
  431. }
  432. break;
  433. case ST_CE:
  434. state = ST_C;
  435. break;
  436. case ST_E:
  437. if (IS_COMMENT(c) ||
  438. IS_QUOTE(c) ||
  439. IS_ESCAPE(c)) {
  440. PUSH(c);
  441. RETURN(c);
  442. } else if (c == 'r') {
  443. PUSH('\r');
  444. RETURN('\r');
  445. } else if (c == 'n') {
  446. PUSH('\n');
  447. RETURN('\n');
  448. } else if (c == 't') {
  449. PUSH('\t');
  450. RETURN('\t');
  451. } else if (c == ' ') {
  452. PUSH(' ');
  453. RETURN(' ');
  454. } else if (IS_EOL(c)) {
  455. /* Escped eol means no eol */
  456. state = ST_S;
  457. } else {
  458. ERR("%s:%d:%d: Unsupported escape character 0x%x\n",
  459. st->file, st->line, st->col, c);
  460. return -1;
  461. }
  462. break;
  463. }
  464. READ_CHAR;
  465. };
  466. switch(state) {
  467. case ST_S:
  468. case ST_C:
  469. case ST_CE:
  470. return 1;
  471. case ST_A:
  472. RETURN(CFG_TOKEN_ALPHA);
  473. case ST_Q:
  474. ERR("%s:%d:%d: Premature end of file, missing closing quote in"
  475. " string constant\n", st->file, st->line, st->col);
  476. return -1;
  477. case ST_QE:
  478. case ST_E:
  479. case ST_AE:
  480. ERR("%s:%d:%d: Premature end of file, missing escaped character\n",
  481. st->file, st->line, st->col);
  482. return -1;
  483. }
  484. BUG("%s:%d:%d: Invalid state %d\n",
  485. st->file, st->line, st->col, state);
  486. return -1;
  487. }
  488. int cfg_parse_section(void* param, cfg_parser_t* st, unsigned int flags)
  489. {
  490. cfg_token_t t;
  491. int ret;
  492. ret = cfg_parse_str(param, st, flags);
  493. if (ret < 0) return ret;
  494. if (ret > 0) {
  495. ERR("%s:%d:%d: Section name missing.\n",
  496. st->file, st->line, st->col);
  497. return ret;
  498. }
  499. ret = cfg_get_token(&t, st, flags);
  500. if (ret < 0) goto error;
  501. if (ret > 0) {
  502. ERR("%s:%d:%d: Closing ']' missing\n", st->file, st->line, st->col);
  503. goto error;
  504. }
  505. if (t.type != ']') {
  506. ERR("%s:%d:%d: Syntax error, ']' expected\n",
  507. st->file, t.start.line, t.start.col);
  508. goto error;
  509. }
  510. if (cfg_eat_eol(st, flags)) goto error;
  511. return 0;
  512. error:
  513. if (param && ((str*)param)->s) {
  514. if (flags & CFG_STR_PKGMEM) {
  515. pkg_free(((str*)param)->s);
  516. ((str*)param)->s = NULL;
  517. } else if (flags & CFG_STR_SHMMEM) {
  518. shm_free(((str*)param)->s);
  519. ((str*)param)->s = NULL;
  520. } else if (flags & CFG_STR_MALLOC) {
  521. free(((str*)param)->s);
  522. ((str*)param)->s = NULL;
  523. }
  524. }
  525. return -1;
  526. }
  527. static char* get_base_name(str* filename)
  528. {
  529. char* tmp1, *tmp2, *res;
  530. int len;
  531. res = NULL;
  532. if ((tmp1 = as_asciiz(filename)) == NULL) {
  533. ERR("cfg_parser: No memory left\n");
  534. goto error;
  535. }
  536. if ((tmp2 = basename(tmp1)) == NULL) {
  537. ERR("cfg_parser: Error in basename\n");
  538. goto error;
  539. }
  540. len = strlen(tmp2);
  541. if ((res = pkg_malloc(len + 1)) == NULL) {
  542. ERR("cfg_parser: No memory left");
  543. goto error;
  544. }
  545. memcpy(res, tmp2, len + 1);
  546. pkg_free(tmp1);
  547. return res;
  548. error:
  549. if (tmp1) pkg_free(tmp1);
  550. return NULL;
  551. }
  552. /** intialize the config parser.
  553. * @param basedir - path to the config file name. If 0 the path
  554. * (base directory) of the main ser.cfg file will be used, else
  555. * basedir will be concatenated to the filename. It will be
  556. * used only if filename is not an absolute path.
  557. * @param filename - config filename (can include path elements).
  558. * @return 0 on error, !=0 on success.
  559. */
  560. cfg_parser_t* cfg_parser_init(str* basedir, str* filename)
  561. {
  562. cfg_parser_t* st;
  563. char* pathname, *base, *abs_pathname;
  564. abs_pathname = NULL;
  565. pathname = filename->s;
  566. st = NULL;
  567. base = NULL;
  568. /* if basedir == 0 or != "" get_abs_pathname */
  569. if (basedir == 0 || basedir->len != 0) {
  570. if ((abs_pathname = get_abs_pathname(basedir, filename)) == NULL) {
  571. ERR("cfg_parser: Error while converting %.*s to absolute"
  572. " pathname\n", STR_FMT(filename));
  573. goto error;
  574. }
  575. pathname = abs_pathname;
  576. }
  577. if ((base = get_base_name(filename)) == NULL) goto error;
  578. if ((st = (cfg_parser_t*)pkg_malloc(sizeof(*st))) == NULL) {
  579. ERR("cfg_parser: No memory left\n");
  580. goto error;
  581. }
  582. memset(st, '\0', sizeof(*st));
  583. if ((st->f = fopen(pathname, "r")) == NULL) {
  584. ERR("cfg_parser: Unable to open file '%s'\n", pathname);
  585. goto error;
  586. }
  587. if (abs_pathname) pkg_free(abs_pathname);
  588. st->file = base;
  589. st->line = 1;
  590. st->col = 0;
  591. return st;
  592. error:
  593. if (st) {
  594. if (st->f) fclose(st->f);
  595. pkg_free(st);
  596. }
  597. if (base) pkg_free(base);
  598. if (abs_pathname) pkg_free(abs_pathname);
  599. return NULL;
  600. }
  601. void cfg_parser_close(cfg_parser_t* st)
  602. {
  603. if (!st) return;
  604. if (st->f) fclose(st->f);
  605. if (st->file) pkg_free(st->file);
  606. pkg_free(st);
  607. }
  608. void cfg_section_parser(cfg_parser_t* st, cfg_func_f parser, void* param)
  609. {
  610. if (st == NULL) return;
  611. st->section.parser = parser;
  612. st->section.param = param;
  613. }
  614. void cfg_set_options(cfg_parser_t* st, cfg_option_t* options)
  615. {
  616. if (st) st->options = options;
  617. }
  618. static int process_option(cfg_parser_t* st, cfg_option_t* opt)
  619. {
  620. if (opt->f) {
  621. /* We have a function so store it and pass opt->dst to it */
  622. if (opt->f(opt->param, st, opt->flags) < 0) return -1;
  623. } else {
  624. /* We have no function, so if we have a pointer to some
  625. * variable in opt->param then store the value of opt->i
  626. * there, the variable is assumed to be of type i
  627. */
  628. if (opt->param) *(int*)opt->param = opt->val;
  629. }
  630. return 0;
  631. }
  632. int sr_cfg_parse(cfg_parser_t* st)
  633. {
  634. int ret;
  635. cfg_token_t t;
  636. cfg_option_t* opt;
  637. while(1) {
  638. ret = cfg_get_token(&t, st, 0);
  639. if (ret < 0) return ret;
  640. if (ret > 0) break;
  641. switch(t.type) {
  642. case CFG_TOKEN_ALPHA:
  643. /* Lookup the option name */
  644. if ((opt = cfg_lookup_token(st->options, &t.val)) == NULL) {
  645. ERR("%s:%d:%d: Unsupported option '%.*s'\n",
  646. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  647. return -1;
  648. }
  649. st->cur_opt = &t;
  650. if (process_option(st, opt) < 0) return -1;
  651. break;
  652. case '[':
  653. if (st->section.parser == NULL) {
  654. ERR("%s:%d:%d: Syntax error\n", st->file,
  655. t.start.line, t.start.col);
  656. return -1;
  657. }
  658. if (st->section.parser(st->section.param, st, 0) < 0) return -1;
  659. break;
  660. /* Empty line */
  661. case '\n': continue;
  662. default:
  663. ERR("%s:%d:%d: Syntax error\n",
  664. st->file, t.start.line, t.start.col);
  665. return -1;
  666. }
  667. }
  668. return 0;
  669. }
  670. cfg_option_t* cfg_lookup_token(cfg_option_t* table, str* token)
  671. {
  672. int len, i;
  673. int (*cmp)(const char* s1, const char* s2, size_t n) = NULL;
  674. if (table == NULL) return NULL;
  675. for(i = 0; table[i].name; i++) {
  676. len = strlen(table[i].name);
  677. if (table[i].flags & CFG_PREFIX) {
  678. if (token->len < len) continue;
  679. } else {
  680. if (token->len != len) continue;
  681. }
  682. if (table[i].flags & CFG_CASE_SENSITIVE) cmp = strncmp;
  683. else cmp = strncasecmp;
  684. if (cmp(token->s, table[i].name, len)) continue;
  685. return table + i;
  686. }
  687. if (table[i].flags & CFG_DEFAULT) {
  688. return table + i;
  689. }
  690. return NULL;
  691. }
  692. int cfg_eat_equal(cfg_parser_t* st, unsigned int flags)
  693. {
  694. cfg_token_t t;
  695. int ret;
  696. ret = cfg_get_token(&t, st, flags);
  697. if (ret < 0) return ret;
  698. if (ret > 0) {
  699. ERR("%s:%d:%d: Delimiter '=' missing\n",
  700. st->file, st->line, st->col);
  701. return ret;
  702. }
  703. if (t.type != '=') {
  704. ERR("%s:%d:%d: Syntax error, '=' expected\n",
  705. st->file, t.start.line, t.start.col);
  706. return -1;
  707. }
  708. return 0;
  709. }
  710. int cfg_eat_eol(cfg_parser_t* st, unsigned int flags)
  711. {
  712. cfg_token_t t;
  713. int ret;
  714. /* Skip EOL */
  715. ret = cfg_get_token(&t, st, 0);
  716. if (ret < 0) return ret;
  717. if (ret > 0) return 0;
  718. if (t.type != '\n') {
  719. ERR("%s:%d:%d: End of line expected\n",
  720. st->file, t.start.line, t.start.col);
  721. return -1;
  722. }
  723. return 0;
  724. }
  725. int cfg_parse_enum(void* param, cfg_parser_t* st, unsigned int flags)
  726. {
  727. int ret;
  728. cfg_token_t t;
  729. cfg_option_t* values, *val;
  730. values = (cfg_option_t*)param;
  731. ret = cfg_get_token(&t, st, flags);
  732. if (ret != 0) return ret;
  733. if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
  734. ERR("%s:%d:%d: Invalid enum value '%.*s'\n",
  735. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  736. return -1;
  737. }
  738. if (values) {
  739. if ((val = cfg_lookup_token(values, &t.val)) == NULL) {
  740. ERR("%s:%d:%d Unsupported enum value '%.*s'\n",
  741. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  742. return -1;
  743. }
  744. return process_option(st, val);
  745. } else {
  746. return 0;
  747. }
  748. }
  749. int cfg_parse_enum_opt(void* param, cfg_parser_t* st, unsigned int flags)
  750. {
  751. int ret;
  752. if (cfg_eat_equal(st, flags)) return -1;
  753. ret = cfg_parse_enum(param, st, CFG_EXTENDED_ALPHA | flags);
  754. if (ret > 0) {
  755. ERR("%s:%d:%d: Option value missing\n",
  756. st->file, st->line, st->col);
  757. return ret;
  758. } else if (ret < 0) return ret;
  759. if (cfg_eat_eol(st, flags)) return -1;
  760. return 0;
  761. }
  762. int cfg_parse_str(void* param, cfg_parser_t* st, unsigned int flags)
  763. {
  764. str* val;
  765. int ret;
  766. char* buf;
  767. cfg_token_t t;
  768. ret = cfg_get_token(&t, st, flags);
  769. if (ret != 0) return ret;
  770. if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
  771. ERR("%s:%d:%d: Invalid string value '%.*s', a string expected.\n",
  772. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  773. return -1;
  774. }
  775. if (!param) return 0;
  776. val = (str*)param;
  777. if (flags & CFG_STR_STATIC) {
  778. if (!val->s || val->len <= t.val.len) {
  779. ERR("%s:%d:%d: Destination string buffer too small\n",
  780. st->file, t.start.line, t.start.col);
  781. return -1;
  782. }
  783. buf = val->s;
  784. } else if (flags & CFG_STR_SHMMEM) {
  785. if ((buf = shm_malloc(t.val.len + 1)) == NULL) {
  786. ERR("%s:%d:%d: Out of shared memory\n", st->file,
  787. t.start.line, t.start.col);
  788. return -1;
  789. }
  790. if (val->s) shm_free(val->s);
  791. } else if (flags & CFG_STR_MALLOC) {
  792. if ((buf = malloc(t.val.len + 1)) == NULL) {
  793. ERR("%s:%d:%d: Out of malloc memory\n", st->file,
  794. t.start.line, t.start.col);
  795. return -1;
  796. }
  797. if (val->s) free(val->s);
  798. } else if (flags & CFG_STR_PKGMEM) {
  799. if ((buf = pkg_malloc(t.val.len + 1)) == NULL) {
  800. ERR("%s:%d:%d: Out of private memory\n", st->file,
  801. t.start.line, t.start.col);
  802. return -1;
  803. }
  804. if (val->s) pkg_free(val->s);
  805. } else {
  806. *val = t.val;
  807. return 0;
  808. }
  809. memcpy(buf, t.val.s, t.val.len);
  810. buf[t.val.len] = '\0';
  811. val->s = buf;
  812. val->len = t.val.len;
  813. return 0;
  814. }
  815. int cfg_parse_str_opt(void* param, cfg_parser_t* st, unsigned int flags)
  816. {
  817. int ret;
  818. if (cfg_eat_equal(st, flags)) return -1;
  819. ret = cfg_parse_str(param, st, flags | CFG_EXTENDED_ALPHA);
  820. if (ret > 0) {
  821. ERR("%s:%d:%d: Option value missing\n",
  822. st->file, st->line, st->col);
  823. } else if (ret < 0) return ret;
  824. if (cfg_eat_eol(st, flags)) return -1;
  825. return 0;
  826. }
  827. int cfg_parse_int(void* param, cfg_parser_t* st, unsigned int flags)
  828. {
  829. int* val;
  830. int ret, tmp;
  831. cfg_token_t t;
  832. val = (int*)param;
  833. ret = cfg_get_token(&t, st, flags);
  834. if (ret != 0) return ret;
  835. if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
  836. ERR("%s:%d:%d: Invalid integer value '%.*s'\n",
  837. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  838. return -1;
  839. }
  840. if (str2sint(&t.val, &tmp) < 0) {
  841. ERR("%s:%d:%d: Invalid integer value '%.*s'\n",
  842. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  843. return -1;
  844. }
  845. if (val) *val = tmp;
  846. return 0;
  847. }
  848. int cfg_parse_int_opt(void* param, cfg_parser_t* st, unsigned int flags)
  849. {
  850. int ret;
  851. if (cfg_eat_equal(st, flags)) return -1;
  852. ret = cfg_parse_int(param, st, flags);
  853. if (ret > 0) {
  854. ERR("%s:%d:%d: Option value missing\n",
  855. st->file, st->line, st->col);
  856. } else if (ret < 0) return ret;
  857. if (cfg_eat_eol(st, flags)) return -1;
  858. return 0;
  859. }
  860. int cfg_parse_bool(void* param, cfg_parser_t* st, unsigned int flags)
  861. {
  862. int ret, *val;
  863. cfg_token_t t;
  864. cfg_option_t* map;
  865. val = (int*)param;
  866. ret = cfg_get_token(&t, st, flags);
  867. if (ret != 0) return ret;
  868. if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
  869. ERR("%s:%d:%d: Invalid option value '%.*s', boolean expected\n",
  870. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  871. return -1;
  872. }
  873. if ((map = cfg_lookup_token(cfg_bool_values, &t.val)) == NULL) {
  874. ERR("%s:%d:%d: Invalid option value '%.*s', boolean expected\n",
  875. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  876. return -1;
  877. }
  878. if (val) *val = map->val;
  879. return 0;
  880. }
  881. int cfg_parse_bool_opt(void* param, cfg_parser_t* st, unsigned int flags)
  882. {
  883. int ret;
  884. if (cfg_eat_equal(st, flags)) return -1;
  885. ret = cfg_parse_bool(param, st, CFG_EXTENDED_ALPHA | flags);
  886. if (ret > 0) {
  887. ERR("%s:%d:%d: Option value missing\n",
  888. st->file, st->line, st->col);
  889. } else if (ret < 0) return ret;
  890. if (cfg_eat_eol(st, flags)) return -1;
  891. return 0;
  892. }