parse_config.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * config file parser helper
  4. *
  5. * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later.
  9. */
  10. /* Uncomment to enable test applet */
  11. ////config:config PARSE
  12. ////config: bool "Uniform config file parser debugging applet: parse"
  13. ////config: default n
  14. ////config: help
  15. ////config: Typical usage of parse API:
  16. ////config: char *t[3];
  17. ////config: parser_t *p = config_open(filename);
  18. ////config: while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
  19. ////config: bb_error_msg("TOKENS: '%s''%s''%s'", t[0], t[1], t[2]);
  20. ////config: }
  21. ////config: config_close(p);
  22. ////applet:IF_PARSE(APPLET(parse, BB_DIR_USR_BIN, BB_SUID_DROP))
  23. //kbuild:lib-y += parse_config.o
  24. //usage:#define parse_trivial_usage
  25. //usage: "[-x] [-n MAXTOKENS] [-m MINTOKENS] [-d DELIMS] [-f FLAGS] FILE..."
  26. //usage:#define parse_full_usage "\n\n"
  27. //usage: " -x Suppress output (for benchmarking)"
  28. #include "libbb.h"
  29. #if defined ENABLE_PARSE && ENABLE_PARSE
  30. int parse_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  31. int parse_main(int argc UNUSED_PARAM, char **argv)
  32. {
  33. const char *delims = "# \t";
  34. char **t;
  35. unsigned flags = PARSE_NORMAL;
  36. int mintokens = 0, ntokens = 128;
  37. unsigned noout;
  38. opt_complementary = "-1";
  39. noout = 1 & getopt32(argv, "xn:+m:+d:f:+", &ntokens, &mintokens, &delims, &flags);
  40. //argc -= optind;
  41. argv += optind;
  42. t = xmalloc(sizeof(t[0]) * ntokens);
  43. while (*argv) {
  44. int n;
  45. parser_t *p = config_open(*argv);
  46. while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
  47. if (!noout) {
  48. for (int i = 0; i < n; ++i)
  49. printf("[%s]", t[i]);
  50. puts("");
  51. }
  52. }
  53. config_close(p);
  54. argv++;
  55. }
  56. return EXIT_SUCCESS;
  57. }
  58. #endif
  59. parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path))
  60. {
  61. FILE* fp;
  62. parser_t *parser;
  63. fp = fopen_func(filename);
  64. if (!fp)
  65. return NULL;
  66. parser = xzalloc(sizeof(*parser));
  67. parser->fp = fp;
  68. return parser;
  69. }
  70. parser_t* FAST_FUNC config_open(const char *filename)
  71. {
  72. return config_open2(filename, fopen_or_warn_stdin);
  73. }
  74. void FAST_FUNC config_close(parser_t *parser)
  75. {
  76. if (parser) {
  77. if (PARSE_KEEP_COPY) /* compile-time constant */
  78. free(parser->data);
  79. fclose(parser->fp);
  80. free(parser->line);
  81. free(parser->nline);
  82. free(parser);
  83. }
  84. }
  85. /* This function reads an entire line from a text file,
  86. * up to a newline, exclusive.
  87. * Trailing '\' is recognized as line continuation.
  88. * Returns -1 if EOF/error.
  89. */
  90. static int get_line_with_continuation(parser_t *parser)
  91. {
  92. ssize_t len, nlen;
  93. char *line;
  94. len = getline(&parser->line, &parser->line_alloc, parser->fp);
  95. if (len <= 0)
  96. return len;
  97. line = parser->line;
  98. for (;;) {
  99. parser->lineno++;
  100. if (line[len - 1] == '\n')
  101. len--;
  102. if (len == 0 || line[len - 1] != '\\')
  103. break;
  104. len--;
  105. nlen = getline(&parser->nline, &parser->nline_alloc, parser->fp);
  106. if (nlen <= 0)
  107. break;
  108. if (parser->line_alloc < len + nlen + 1) {
  109. parser->line_alloc = len + nlen + 1;
  110. line = parser->line = xrealloc(line, parser->line_alloc);
  111. }
  112. memcpy(&line[len], parser->nline, nlen);
  113. len += nlen;
  114. }
  115. line[len] = '\0';
  116. return len;
  117. }
  118. /*
  119. 0. If parser is NULL return 0.
  120. 1. Read a line from config file. If nothing to read then return 0.
  121. Handle continuation character. Advance lineno for each physical line.
  122. Discard everything past comment character.
  123. 2. if PARSE_TRIM is set (default), remove leading and trailing delimiters.
  124. 3. If resulting line is empty goto 1.
  125. 4. Look for first delimiter. If !PARSE_COLLAPSE or !PARSE_TRIM is set then
  126. remember the token as empty.
  127. 5. Else (default) if number of seen tokens is equal to max number of tokens
  128. (token is the last one) and PARSE_GREEDY is set then the remainder
  129. of the line is the last token.
  130. Else (token is not last or PARSE_GREEDY is not set) just replace
  131. first delimiter with '\0' thus delimiting the token.
  132. 6. Advance line pointer past the end of token. If number of seen tokens
  133. is less than required number of tokens then goto 4.
  134. 7. Check the number of seen tokens is not less the min number of tokens.
  135. Complain or die otherwise depending on PARSE_MIN_DIE.
  136. 8. Return the number of seen tokens.
  137. mintokens > 0 make config_read() print error message if less than mintokens
  138. (but more than 0) are found. Empty lines are always skipped (not warned about).
  139. */
  140. #undef config_read
  141. int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims)
  142. {
  143. char *line;
  144. int ntokens, mintokens;
  145. int t;
  146. if (!parser)
  147. return 0;
  148. ntokens = (uint8_t)flags;
  149. mintokens = (uint8_t)(flags >> 8);
  150. again:
  151. memset(tokens, 0, sizeof(tokens[0]) * ntokens);
  152. /* Read one line (handling continuations with backslash) */
  153. if (get_line_with_continuation(parser) < 0)
  154. return 0;
  155. line = parser->line;
  156. /* Skip token in the start of line? */
  157. if (flags & PARSE_TRIM)
  158. line += strspn(line, delims + 1);
  159. if (line[0] == '\0' || line[0] == delims[0])
  160. goto again;
  161. if (flags & PARSE_KEEP_COPY) {
  162. free(parser->data);
  163. parser->data = xstrdup(line);
  164. }
  165. /* Tokenize the line */
  166. t = 0;
  167. do {
  168. /* Pin token */
  169. tokens[t] = line;
  170. /* Combine remaining arguments? */
  171. if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) {
  172. /* Vanilla token, find next delimiter */
  173. line += strcspn(line, delims[0] ? delims : delims + 1);
  174. } else {
  175. /* Combining, find comment char if any */
  176. line = strchrnul(line, PARSE_EOL_COMMENTS ? delims[0] : '\0');
  177. /* Trim any extra delimiters from the end */
  178. if (flags & PARSE_TRIM) {
  179. while (strchr(delims + 1, line[-1]) != NULL)
  180. line--;
  181. }
  182. }
  183. /* Token not terminated? */
  184. if (*line == delims[0])
  185. *line = '\0';
  186. else if (*line != '\0')
  187. *line++ = '\0';
  188. #if 0 /* unused so far */
  189. if (flags & PARSE_ESCAPE) {
  190. strcpy_and_process_escape_sequences(tokens[t], tokens[t]);
  191. }
  192. #endif
  193. /* Skip possible delimiters */
  194. if (flags & PARSE_COLLAPSE)
  195. line += strspn(line, delims + 1);
  196. t++;
  197. } while (*line && *line != delims[0] && t < ntokens);
  198. if (t < mintokens) {
  199. bb_error_msg("bad line %u: %d tokens found, %d needed",
  200. parser->lineno, t, mintokens);
  201. if (flags & PARSE_MIN_DIE)
  202. xfunc_die();
  203. goto again;
  204. }
  205. return t;
  206. }