parse_config.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 = flags & 0xFF;
  117. mintokens = (flags & 0xFF00) >> 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. for (t = 0; *line && *line != delims[0] && t < ntokens; t++) {
  140. /* Pin token */
  141. tokens[t] = line;
  142. /* Combine remaining arguments? */
  143. if ((t != (ntokens-1)) || !(flags & PARSE_GREEDY)) {
  144. /* Vanilla token, find next delimiter */
  145. line += strcspn(line, delims[0] ? delims : delims + 1);
  146. } else {
  147. /* Combining, find comment char if any */
  148. line = strchrnul(line, delims[0]);
  149. /* Trim any extra delimiters from the end */
  150. if (flags & PARSE_TRIM) {
  151. while (strchr(delims + 1, line[-1]) != NULL)
  152. line--;
  153. }
  154. }
  155. /* Token not terminated? */
  156. if (line[0] == delims[0])
  157. *line = '\0';
  158. else if (line[0] != '\0')
  159. *(line++) = '\0';
  160. #if 0 /* unused so far */
  161. if (flags & PARSE_ESCAPE) {
  162. const char *from;
  163. char *to;
  164. from = to = tokens[t];
  165. while (*from) {
  166. if (*from == '\\') {
  167. from++;
  168. *to++ = bb_process_escape_sequence(&from);
  169. } else {
  170. *to++ = *from++;
  171. }
  172. }
  173. *to = '\0';
  174. }
  175. #endif
  176. /* Skip possible delimiters */
  177. if (flags & PARSE_COLLAPSE)
  178. line += strspn(line, delims + 1);
  179. }
  180. if (t < mintokens) {
  181. bb_error_msg("bad line %u: %d tokens found, %d needed",
  182. parser->lineno, t, mintokens);
  183. if (flags & PARSE_MIN_DIE)
  184. xfunc_die();
  185. goto again;
  186. }
  187. return t;
  188. }