parse_config.c 5.2 KB

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