parse_config.c 5.4 KB

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