parse_config.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * config file parser helper
  3. *
  4. * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  7. * Also for use in uClibc (http://uclibc.org/) licensed under LGPLv2.1 or later.
  8. */
  9. #if !defined _LIBC
  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. #else
  40. # include <unistd.h>
  41. # include <string.h>
  42. # include <malloc.h>
  43. # include <bits/uClibc_page.h>
  44. # include "internal/parse_config.h"
  45. # ifndef FAST_FUNC
  46. # define FAST_FUNC
  47. # endif
  48. # define fopen_or_warn_stdin fopen
  49. # define bb_error_msg(...)
  50. # define xstrdup strdup
  51. # define xfunc_die() return 0
  52. /* Read up to EOF or EOL, treat line-continuations as extending the line.
  53. Return number of bytes read into .line, -1 otherwise */
  54. static off_t bb_get_chunk_with_continuation(parser_t* parsr)
  55. {
  56. off_t pos = 0;
  57. char *chp;
  58. while (1) {
  59. if (fgets(parsr->line + pos, parsr->line_len - pos, parsr->fp) == NULL) {
  60. memset(parsr->line, 0, parsr->line_len);
  61. pos = -1;
  62. break;
  63. }
  64. pos += strlen(parsr->line + pos);
  65. chp = strchr(parsr->line, '\n');
  66. if (chp) {
  67. --pos;
  68. if (--*chp == '\\')
  69. --pos;
  70. else
  71. break;
  72. } else if (parsr->allocated) {
  73. parsr->line_len += PAGE_SIZE;
  74. parsr->data = realloc(parsr->data,
  75. parsr->data_len + parsr->line_len);
  76. parsr->line = parsr->data + parsr->data_len;
  77. } else {
  78. /* discard rest of line if not enough space in buffer */
  79. int c;
  80. do {
  81. c = fgetc(parsr->fp);
  82. } while (c != EOF && c != '\n');
  83. break;
  84. }
  85. }
  86. return pos;
  87. }
  88. #endif
  89. /*
  90. Typical usage:
  91. ----- CUT -----
  92. char *t[3]; // tokens placeholder
  93. parser_t *p = config_open(filename);
  94. if (p) {
  95. // parse line-by-line
  96. while (config_read(p, t, 3, 0, delimiters, flags)) { // 1..3 tokens
  97. // use tokens
  98. bb_error_msg("TOKENS: [%s][%s][%s]", t[0], t[1], t[2]);
  99. }
  100. ...
  101. // free parser
  102. config_close(p);
  103. }
  104. ----- CUT -----
  105. */
  106. static __always_inline parser_t * FAST_FUNC config_open2(const char *filename,
  107. FILE* FAST_FUNC (*fopen_func)(const char *path, const char *mode))
  108. {
  109. parser_t *parser;
  110. FILE* fp;
  111. fp = fopen_func(filename, "r");
  112. if (!fp)
  113. return NULL;
  114. parser = calloc(1, sizeof(*parser));
  115. if (parser) {
  116. parser->fp = fp;
  117. }
  118. return parser;
  119. }
  120. parser_t * FAST_FUNC config_open(const char *filename)
  121. {
  122. return config_open2(filename, fopen_or_warn_stdin);
  123. }
  124. #ifdef UNUSED
  125. static void config_free_data(parser_t *parser)
  126. {
  127. free(parser->data);
  128. parser->data = parser->line = NULL;
  129. }
  130. #endif
  131. void FAST_FUNC config_close(parser_t *parser)
  132. {
  133. if (parser) {
  134. fclose(parser->fp);
  135. if (parser->allocated)
  136. free(parser->data);
  137. free(parser);
  138. }
  139. }
  140. /*
  141. 0. If parser is NULL return 0.
  142. 1. Read a line from config file. If nothing to read then return 0.
  143. Handle continuation character. Advance lineno for each physical line.
  144. Discard everything past comment character.
  145. 2. if PARSE_TRIM is set (default), remove leading and trailing delimiters.
  146. 3. If resulting line is empty goto 1.
  147. 4. Look for first delimiter. If !PARSE_COLLAPSE or !PARSE_TRIM is set then
  148. remember the token as empty.
  149. 5. Else (default) if number of seen tokens is equal to max number of tokens
  150. (token is the last one) and PARSE_GREEDY is set then the remainder
  151. of the line is the last token.
  152. Else (token is not last or PARSE_GREEDY is not set) just replace
  153. first delimiter with '\0' thus delimiting the token.
  154. 6. Advance line pointer past the end of token. If number of seen tokens
  155. is less than required number of tokens then goto 4.
  156. 7. Check the number of seen tokens is not less the min number of tokens.
  157. Complain or die otherwise depending on PARSE_MIN_DIE.
  158. 8. Return the number of seen tokens.
  159. mintokens > 0 make config_read() print error message if less than mintokens
  160. (but more than 0) are found. Empty lines are always skipped (not warned about).
  161. */
  162. #undef config_read
  163. int FAST_FUNC config_read(parser_t *parser, char ***tokens,
  164. unsigned flags, const char *delims)
  165. {
  166. char *line;
  167. int ntokens, mintokens;
  168. off_t len;
  169. int t;
  170. if (parser == NULL)
  171. return 0;
  172. ntokens = flags & 0xFF;
  173. mintokens = (flags & 0xFF00) >> 8;
  174. again:
  175. if (parser->data == NULL) {
  176. if (parser->line_len == 0)
  177. parser->line_len = 81;
  178. if (parser->data_len == 0)
  179. parser->data_len += 1 + ntokens * sizeof(char *);
  180. parser->data = malloc(parser->data_len + parser->line_len);
  181. if (parser->data == NULL)
  182. return 0;
  183. parser->allocated |= 1;
  184. } /* else { assert(parser->data_len > 0); } */
  185. parser->line = parser->data + parser->data_len;
  186. /*config_free_data(parser);*/
  187. /* Read one line (handling continuations with backslash) */
  188. len = bb_get_chunk_with_continuation(parser);
  189. if (len == -1)
  190. return 0;
  191. line = parser->line;
  192. /* Skip multiple token-delimiters in the start of line? */
  193. if (flags & PARSE_TRIM)
  194. line += strspn(line, delims + 1);
  195. if (line[0] == '\0' || line[0] == delims[0])
  196. goto again;
  197. *tokens = (char **) parser->data;
  198. memset(*tokens, 0, sizeof(*tokens[0]) * ntokens);
  199. /* Tokenize the line */
  200. for (t = 0; *line && *line != delims[0] && t < ntokens; t++) {
  201. /* Pin token */
  202. *(*tokens + t) = line;
  203. /* Combine remaining arguments? */
  204. if ((t != ntokens-1) || !(flags & PARSE_GREEDY)) {
  205. /* Vanilla token, find next delimiter */
  206. line += strcspn(line, delims[0] ? delims : delims + 1);
  207. } else {
  208. /* Combining, find comment char if any */
  209. line = strchrnul(line, delims[0]);
  210. /* Trim any extra delimiters from the end */
  211. if (flags & PARSE_TRIM) {
  212. while (strchr(delims + 1, line[-1]) != NULL)
  213. line--;
  214. }
  215. }
  216. /* Token not terminated? */
  217. if (line[0] == delims[0])
  218. *line = '\0';
  219. else if (line[0] != '\0')
  220. *(line++) = '\0';
  221. #if 0 /* unused so far */
  222. if (flags & PARSE_ESCAPE) {
  223. const char *from;
  224. char *to;
  225. from = to = tokens[t];
  226. while (*from) {
  227. if (*from == '\\') {
  228. from++;
  229. *to++ = bb_process_escape_sequence(&from);
  230. } else {
  231. *to++ = *from++;
  232. }
  233. }
  234. *to = '\0';
  235. }
  236. #endif
  237. /* Skip possible delimiters */
  238. if (flags & PARSE_COLLAPSE)
  239. line += strspn(line, delims + 1);
  240. }
  241. if (t < mintokens) {
  242. bb_error_msg(/*"bad line %u: "*/"%d tokens found, %d needed",
  243. /*parser->lineno, */t, mintokens);
  244. if (flags & PARSE_MIN_DIE)
  245. xfunc_die();
  246. goto again;
  247. }
  248. return t;
  249. }