cfg_parser.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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) { st->cur_opt = 0; return -1; }
  651. st->cur_opt = 0;
  652. break;
  653. case '[':
  654. if (st->section.parser == NULL) {
  655. ERR("%s:%d:%d: Syntax error\n", st->file,
  656. t.start.line, t.start.col);
  657. return -1;
  658. }
  659. if (st->section.parser(st->section.param, st, 0) < 0) return -1;
  660. break;
  661. /* Empty line */
  662. case '\n': continue;
  663. default:
  664. ERR("%s:%d:%d: Syntax error\n",
  665. st->file, t.start.line, t.start.col);
  666. return -1;
  667. }
  668. }
  669. return 0;
  670. }
  671. cfg_option_t* cfg_lookup_token(cfg_option_t* table, str* token)
  672. {
  673. int len, i;
  674. int (*cmp)(const char* s1, const char* s2, size_t n) = NULL;
  675. if (table == NULL) return NULL;
  676. for(i = 0; table[i].name; i++) {
  677. len = strlen(table[i].name);
  678. if (table[i].flags & CFG_PREFIX) {
  679. if (token->len < len) continue;
  680. } else {
  681. if (token->len != len) continue;
  682. }
  683. if (table[i].flags & CFG_CASE_SENSITIVE) cmp = strncmp;
  684. else cmp = strncasecmp;
  685. if (cmp(token->s, table[i].name, len)) continue;
  686. return table + i;
  687. }
  688. if (table[i].flags & CFG_DEFAULT) {
  689. return table + i;
  690. }
  691. return NULL;
  692. }
  693. int cfg_eat_equal(cfg_parser_t* st, unsigned int flags)
  694. {
  695. cfg_token_t t;
  696. int ret;
  697. ret = cfg_get_token(&t, st, flags);
  698. if (ret < 0) return ret;
  699. if (ret > 0) {
  700. ERR("%s:%d:%d: Delimiter '=' missing\n",
  701. st->file, st->line, st->col);
  702. return ret;
  703. }
  704. if (t.type != '=') {
  705. ERR("%s:%d:%d: Syntax error, '=' expected\n",
  706. st->file, t.start.line, t.start.col);
  707. return -1;
  708. }
  709. return 0;
  710. }
  711. int cfg_eat_eol(cfg_parser_t* st, unsigned int flags)
  712. {
  713. cfg_token_t t;
  714. int ret;
  715. /* Skip EOL */
  716. ret = cfg_get_token(&t, st, 0);
  717. if (ret < 0) return ret;
  718. if (ret > 0) return 0;
  719. if (t.type != '\n') {
  720. ERR("%s:%d:%d: End of line expected\n",
  721. st->file, t.start.line, t.start.col);
  722. return -1;
  723. }
  724. return 0;
  725. }
  726. int cfg_parse_enum(void* param, cfg_parser_t* st, unsigned int flags)
  727. {
  728. int ret;
  729. cfg_token_t t;
  730. cfg_option_t* values, *val;
  731. values = (cfg_option_t*)param;
  732. ret = cfg_get_token(&t, st, flags);
  733. if (ret != 0) return ret;
  734. if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
  735. ERR("%s:%d:%d: Invalid enum value '%.*s'\n",
  736. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  737. return -1;
  738. }
  739. if (values) {
  740. if ((val = cfg_lookup_token(values, &t.val)) == NULL) {
  741. ERR("%s:%d:%d Unsupported enum value '%.*s'\n",
  742. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  743. return -1;
  744. }
  745. return process_option(st, val);
  746. } else {
  747. return 0;
  748. }
  749. }
  750. int cfg_parse_enum_opt(void* param, cfg_parser_t* st, unsigned int flags)
  751. {
  752. int ret;
  753. if (cfg_eat_equal(st, flags)) return -1;
  754. ret = cfg_parse_enum(param, st, CFG_EXTENDED_ALPHA | flags);
  755. if (ret > 0) {
  756. ERR("%s:%d:%d: Option value missing\n",
  757. st->file, st->line, st->col);
  758. return ret;
  759. } else if (ret < 0) return ret;
  760. if (cfg_eat_eol(st, flags)) return -1;
  761. return 0;
  762. }
  763. int cfg_parse_str(void* param, cfg_parser_t* st, unsigned int flags)
  764. {
  765. str* val;
  766. int ret;
  767. char* buf;
  768. cfg_token_t t;
  769. ret = cfg_get_token(&t, st, flags);
  770. if (ret != 0) return ret;
  771. if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
  772. ERR("%s:%d:%d: Invalid string value '%.*s', a string expected.\n",
  773. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  774. return -1;
  775. }
  776. if (!param) return 0;
  777. val = (str*)param;
  778. if (flags & CFG_STR_STATIC) {
  779. if (!val->s || val->len <= t.val.len) {
  780. ERR("%s:%d:%d: Destination string buffer too small\n",
  781. st->file, t.start.line, t.start.col);
  782. return -1;
  783. }
  784. buf = val->s;
  785. } else if (flags & CFG_STR_SHMMEM) {
  786. if ((buf = shm_malloc(t.val.len + 1)) == NULL) {
  787. ERR("%s:%d:%d: Out of shared memory\n", st->file,
  788. t.start.line, t.start.col);
  789. return -1;
  790. }
  791. if (val->s) shm_free(val->s);
  792. } else if (flags & CFG_STR_MALLOC) {
  793. if ((buf = malloc(t.val.len + 1)) == NULL) {
  794. ERR("%s:%d:%d: Out of malloc memory\n", st->file,
  795. t.start.line, t.start.col);
  796. return -1;
  797. }
  798. if (val->s) free(val->s);
  799. } else if (flags & CFG_STR_PKGMEM) {
  800. if ((buf = pkg_malloc(t.val.len + 1)) == NULL) {
  801. ERR("%s:%d:%d: Out of private memory\n", st->file,
  802. t.start.line, t.start.col);
  803. return -1;
  804. }
  805. if (val->s) pkg_free(val->s);
  806. } else {
  807. *val = t.val;
  808. return 0;
  809. }
  810. memcpy(buf, t.val.s, t.val.len);
  811. buf[t.val.len] = '\0';
  812. val->s = buf;
  813. val->len = t.val.len;
  814. return 0;
  815. }
  816. int cfg_parse_str_opt(void* param, cfg_parser_t* st, unsigned int flags)
  817. {
  818. int ret;
  819. if (cfg_eat_equal(st, flags)) return -1;
  820. ret = cfg_parse_str(param, st, flags | CFG_EXTENDED_ALPHA);
  821. if (ret > 0) {
  822. ERR("%s:%d:%d: Option value missing\n",
  823. st->file, st->line, st->col);
  824. } else if (ret < 0) return ret;
  825. if (cfg_eat_eol(st, flags)) return -1;
  826. return 0;
  827. }
  828. int cfg_parse_int(void* param, cfg_parser_t* st, unsigned int flags)
  829. {
  830. int* val;
  831. int ret, tmp;
  832. cfg_token_t t;
  833. val = (int*)param;
  834. ret = cfg_get_token(&t, st, flags);
  835. if (ret != 0) return ret;
  836. if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
  837. ERR("%s:%d:%d: Invalid integer value '%.*s'\n",
  838. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  839. return -1;
  840. }
  841. if (str2sint(&t.val, &tmp) < 0) {
  842. ERR("%s:%d:%d: Invalid integer value '%.*s'\n",
  843. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  844. return -1;
  845. }
  846. if (val) *val = tmp;
  847. return 0;
  848. }
  849. int cfg_parse_int_opt(void* param, cfg_parser_t* st, unsigned int flags)
  850. {
  851. int ret;
  852. if (cfg_eat_equal(st, flags)) return -1;
  853. ret = cfg_parse_int(param, st, flags);
  854. if (ret > 0) {
  855. ERR("%s:%d:%d: Option value missing\n",
  856. st->file, st->line, st->col);
  857. } else if (ret < 0) return ret;
  858. if (cfg_eat_eol(st, flags)) return -1;
  859. return 0;
  860. }
  861. int cfg_parse_bool(void* param, cfg_parser_t* st, unsigned int flags)
  862. {
  863. int ret, *val;
  864. cfg_token_t t;
  865. cfg_option_t* map;
  866. val = (int*)param;
  867. ret = cfg_get_token(&t, st, flags);
  868. if (ret != 0) return ret;
  869. if (t.type != CFG_TOKEN_ALPHA && t.type != CFG_TOKEN_STRING) {
  870. ERR("%s:%d:%d: Invalid option value '%.*s', boolean expected\n",
  871. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  872. return -1;
  873. }
  874. if ((map = cfg_lookup_token(cfg_bool_values, &t.val)) == NULL) {
  875. ERR("%s:%d:%d: Invalid option value '%.*s', boolean expected\n",
  876. st->file, t.start.line, t.start.col, STR_FMT(&t.val));
  877. return -1;
  878. }
  879. if (val) *val = map->val;
  880. return 0;
  881. }
  882. int cfg_parse_bool_opt(void* param, cfg_parser_t* st, unsigned int flags)
  883. {
  884. int ret;
  885. if (cfg_eat_equal(st, flags)) return -1;
  886. ret = cfg_parse_bool(param, st, CFG_EXTENDED_ALPHA | flags);
  887. if (ret > 0) {
  888. ERR("%s:%d:%d: Option value missing\n",
  889. st->file, st->line, st->col);
  890. } else if (ret < 0) return ret;
  891. if (cfg_eat_eol(st, flags)) return -1;
  892. return 0;
  893. }