parse_config.c 6.9 KB

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