parse_config.c 6.8 KB

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