parse_config.c 6.7 KB

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