parse_config.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. noout = 1 & getopt32(argv, "^" "xn:+m:+d:f:+" "\0" "-1",
  39. &ntokens, &mintokens, &delims, &flags
  40. );
  41. //argc -= optind;
  42. argv += optind;
  43. t = xmalloc(sizeof(t[0]) * ntokens);
  44. while (*argv) {
  45. int n;
  46. parser_t *p = config_open(*argv);
  47. while ((n = config_read(p, t, ntokens, mintokens, delims, flags)) != 0) {
  48. if (!noout) {
  49. for (int i = 0; i < n; ++i)
  50. printf("[%s]", t[i]);
  51. puts("");
  52. }
  53. }
  54. config_close(p);
  55. argv++;
  56. }
  57. return EXIT_SUCCESS;
  58. }
  59. #endif
  60. parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path))
  61. {
  62. FILE* fp;
  63. parser_t *parser;
  64. fp = fopen_func(filename);
  65. if (!fp)
  66. return NULL;
  67. parser = xzalloc(sizeof(*parser));
  68. parser->fp = fp;
  69. return parser;
  70. }
  71. parser_t* FAST_FUNC config_open(const char *filename)
  72. {
  73. return config_open2(filename, fopen_or_warn_stdin);
  74. }
  75. void FAST_FUNC config_close(parser_t *parser)
  76. {
  77. if (parser) {
  78. if (PARSE_KEEP_COPY) /* compile-time constant */
  79. free(parser->data);
  80. fclose(parser->fp);
  81. free(parser->line);
  82. free(parser->nline);
  83. free(parser);
  84. }
  85. }
  86. /* This function reads an entire line from a text file,
  87. * up to a newline, exclusive.
  88. * Trailing '\' is recognized as line continuation.
  89. * Returns -1 if EOF/error.
  90. */
  91. static int get_line_with_continuation(parser_t *parser)
  92. {
  93. ssize_t len, nlen;
  94. char *line;
  95. len = getline(&parser->line, &parser->line_alloc, parser->fp);
  96. if (len <= 0)
  97. return len;
  98. line = parser->line;
  99. for (;;) {
  100. parser->lineno++;
  101. if (line[len - 1] == '\n')
  102. len--;
  103. if (len == 0 || line[len - 1] != '\\')
  104. break;
  105. len--;
  106. nlen = getline(&parser->nline, &parser->nline_alloc, parser->fp);
  107. if (nlen <= 0)
  108. break;
  109. if (parser->line_alloc < len + nlen + 1) {
  110. parser->line_alloc = len + nlen + 1;
  111. line = parser->line = xrealloc(line, parser->line_alloc);
  112. }
  113. memcpy(&line[len], parser->nline, nlen);
  114. len += nlen;
  115. }
  116. line[len] = '\0';
  117. return len;
  118. }
  119. /*
  120. 0. If parser is NULL return 0.
  121. 1. Read a line from config file. If nothing to read then return 0.
  122. Handle continuation character. Advance lineno for each physical line.
  123. Discard everything past comment character.
  124. 2. if PARSE_TRIM is set (default), remove leading and trailing delimiters.
  125. 3. If resulting line is empty goto 1.
  126. 4. Look for first delimiter. If !PARSE_COLLAPSE or !PARSE_TRIM is set then
  127. remember the token as empty.
  128. 5. Else (default) if number of seen tokens is equal to max number of tokens
  129. (token is the last one) and PARSE_GREEDY is set then the remainder
  130. of the line is the last token.
  131. Else (token is not last or PARSE_GREEDY is not set) just replace
  132. first delimiter with '\0' thus delimiting the token.
  133. 6. Advance line pointer past the end of token. If number of seen tokens
  134. is less than required number of tokens then goto 4.
  135. 7. Check the number of seen tokens is not less the min number of tokens.
  136. Complain or die otherwise depending on PARSE_MIN_DIE.
  137. 8. Return the number of seen tokens.
  138. mintokens > 0 make config_read() print error message if less than mintokens
  139. (but more than 0) are found. Empty lines are always skipped (not warned about).
  140. */
  141. #undef config_read
  142. int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims)
  143. {
  144. char *line, *p;
  145. int ntokens, mintokens;
  146. int t;
  147. char alt_comment_ch;
  148. if (!parser)
  149. return 0;
  150. alt_comment_ch = '\0';
  151. if (flags & PARSE_ALT_COMMENTS)
  152. alt_comment_ch = *delims++;
  153. ntokens = (uint8_t)flags;
  154. mintokens = (uint8_t)(flags >> 8);
  155. again:
  156. memset(tokens, 0, sizeof(tokens[0]) * ntokens);
  157. /* Read one line (handling continuations with backslash) */
  158. if (get_line_with_continuation(parser) < 0)
  159. return 0;
  160. line = parser->line;
  161. /* Skip token in the start of line? */
  162. if (flags & PARSE_TRIM)
  163. line += strspn(line, delims + 1);
  164. p = line;
  165. if (flags & PARSE_WS_COMMENTS)
  166. p = skip_whitespace(p);
  167. if (p[0] == '\0' || p[0] == delims[0] || p[0] == alt_comment_ch)
  168. goto again;
  169. if (flags & PARSE_KEEP_COPY) {
  170. free(parser->data);
  171. parser->data = xstrdup(line);
  172. }
  173. /* Tokenize the line */
  174. t = 0;
  175. do {
  176. /* Pin token */
  177. tokens[t] = line;
  178. /* Combine remaining arguments? */
  179. if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) {
  180. /* Vanilla token, find next delimiter */
  181. line += strcspn(line, (delims[0] && (flags & PARSE_EOL_COMMENTS)) ? delims : delims + 1);
  182. } else {
  183. /* Combining, find comment char if any */
  184. line = strchrnul(line, (flags & PARSE_EOL_COMMENTS) ? delims[0] : '\0');
  185. /* Trim any extra delimiters from the end */
  186. if (flags & PARSE_TRIM) {
  187. while (strchr(delims + 1, line[-1]) != NULL)
  188. line--;
  189. }
  190. }
  191. /* Token not terminated? */
  192. if ((flags & PARSE_EOL_COMMENTS) && *line == delims[0])
  193. *line = '\0'; /* ends with comment char: this line is done */
  194. else if (*line != '\0')
  195. *line++ = '\0'; /* token is done, continue parsing line */
  196. #if 0 /* unused so far */
  197. if (flags & PARSE_ESCAPE) {
  198. strcpy_and_process_escape_sequences(tokens[t], tokens[t]);
  199. }
  200. #endif
  201. /* Skip possible delimiters */
  202. if (flags & PARSE_COLLAPSE)
  203. line += strspn(line, delims + 1);
  204. t++;
  205. } while (*line && *line != delims[0] && t < ntokens);
  206. if (t < mintokens) {
  207. bb_error_msg("bad line %u: %d tokens found, %d needed",
  208. parser->lineno, t, mintokens);
  209. if (flags & PARSE_MIN_DIE)
  210. xfunc_die();
  211. goto again;
  212. }
  213. return t;
  214. }