parse.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * libuci - Library for the Unified Configuration Interface
  3. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * this program is free software; you can redistribute it and/or modify
  6. * it under the terms of the gnu lesser general public license version 2.1
  7. * as published by the free software foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. /*
  15. * This file contains the code for parsing uci config files
  16. */
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #define LINEBUF 128
  23. #define LINEBUF_MAX 4096
  24. /*
  25. * Fetch a new line from the input stream and resize buffer if necessary
  26. */
  27. static void uci_getln(struct uci_context *ctx)
  28. {
  29. struct uci_parse_context *pctx = ctx->pctx;
  30. char *p;
  31. int ofs;
  32. if (pctx->buf == NULL) {
  33. pctx->buf = uci_malloc(ctx, LINEBUF);
  34. pctx->bufsz = LINEBUF;
  35. }
  36. ofs = 0;
  37. do {
  38. p = &pctx->buf[ofs];
  39. p[ofs] = 0;
  40. p = fgets(p, pctx->bufsz - ofs, pctx->file);
  41. if (!p || !p[ofs])
  42. return;
  43. ofs += strlen(p);
  44. if (pctx->buf[ofs - 1] == '\n') {
  45. pctx->line++;
  46. pctx->buf[ofs - 1] = 0;
  47. return;
  48. }
  49. if (pctx->bufsz > LINEBUF_MAX/2) {
  50. pctx->byte = LINEBUF_MAX;
  51. UCI_THROW(ctx, UCI_ERR_PARSE);
  52. }
  53. pctx->bufsz *= 2;
  54. pctx->buf = uci_realloc(ctx, pctx->buf, pctx->bufsz);
  55. } while (1);
  56. }
  57. /*
  58. * Clean up all extra memory used by the parser
  59. */
  60. static void uci_parse_cleanup(struct uci_context *ctx)
  61. {
  62. struct uci_parse_context *pctx;
  63. pctx = ctx->pctx;
  64. if (!pctx)
  65. return;
  66. if (pctx->cfg) {
  67. uci_list_del(&pctx->cfg->list);
  68. uci_drop_file(pctx->cfg);
  69. }
  70. if (pctx->buf)
  71. free(pctx->buf);
  72. if (pctx->file)
  73. fclose(pctx->file);
  74. free(pctx);
  75. }
  76. /*
  77. * move the string pointer forward until a non-whitespace character or
  78. * EOL is reached
  79. */
  80. static void skip_whitespace(char **str)
  81. {
  82. while (**str && isspace(**str))
  83. *str += 1;
  84. }
  85. static inline void addc(char **dest, char **src)
  86. {
  87. **dest = **src;
  88. *dest += 1;
  89. *src += 1;
  90. }
  91. static inline void parse_backslash(char **str, char **target)
  92. {
  93. /* skip backslash */
  94. *str += 1;
  95. /* FIXME: decode escaped characters? */
  96. addc(target, str);
  97. }
  98. /*
  99. * parse a double quoted string argument from the command line
  100. */
  101. static void parse_double_quote(struct uci_context *ctx, char **str, char **target)
  102. {
  103. char c;
  104. /* skip quote character */
  105. *str += 1;
  106. while ((c = **str)) {
  107. switch(c) {
  108. case '\\':
  109. parse_backslash(str, target);
  110. continue;
  111. case '"':
  112. **target = 0;
  113. *str += 1;
  114. return;
  115. default:
  116. addc(target, str);
  117. break;
  118. }
  119. }
  120. ctx->pctx->byte = *str - ctx->pctx->buf;
  121. UCI_THROW(ctx, UCI_ERR_PARSE);
  122. }
  123. /*
  124. * parse a single quoted string argument from the command line
  125. */
  126. static void parse_single_quote(struct uci_context *ctx, char **str, char **target)
  127. {
  128. char c;
  129. /* skip quote character */
  130. *str += 1;
  131. while ((c = **str)) {
  132. switch(c) {
  133. case '\'':
  134. **target = 0;
  135. *str += 1;
  136. return;
  137. default:
  138. addc(target, str);
  139. }
  140. }
  141. ctx->pctx->byte = *str - ctx->pctx->buf;
  142. UCI_THROW(ctx, UCI_ERR_PARSE);
  143. }
  144. /*
  145. * parse a string from the command line and detect the quoting style
  146. */
  147. static void parse_str(struct uci_context *ctx, char **str, char **target)
  148. {
  149. do {
  150. switch(**str) {
  151. case '\\':
  152. parse_backslash(str, target);
  153. continue;
  154. case '\'':
  155. parse_single_quote(ctx, str, target);
  156. break;
  157. case '"':
  158. parse_double_quote(ctx, str, target);
  159. break;
  160. case 0:
  161. goto done;
  162. default:
  163. addc(target, str);
  164. break;
  165. }
  166. } while (**str && !isspace(**str));
  167. done:
  168. /*
  169. * if the string was unquoted and we've stopped at a whitespace
  170. * character, skip to the next one, because the whitespace will
  171. * be overwritten by a null byte here
  172. */
  173. if (**str)
  174. *str += 1;
  175. /* terminate the parsed string */
  176. **target = 0;
  177. }
  178. /*
  179. * extract the next argument from the command line
  180. */
  181. static char *next_arg(struct uci_context *ctx, char **str, bool required)
  182. {
  183. char *val;
  184. char *ptr;
  185. val = ptr = *str;
  186. skip_whitespace(str);
  187. parse_str(ctx, str, &ptr);
  188. if (required && !*val) {
  189. ctx->pctx->byte = *str - ctx->pctx->buf;
  190. UCI_THROW(ctx, UCI_ERR_PARSE);
  191. }
  192. return uci_strdup(ctx, val);
  193. }
  194. /*
  195. * verify that the end of the line or command is reached.
  196. * throw an error if extra arguments are given on the command line
  197. */
  198. static void assert_eol(struct uci_context *ctx, char **str)
  199. {
  200. char *tmp;
  201. tmp = next_arg(ctx, str, false);
  202. if (tmp && *tmp) {
  203. ctx->pctx->byte = tmp - ctx->pctx->buf;
  204. UCI_THROW(ctx, UCI_ERR_PARSE);
  205. }
  206. }
  207. /*
  208. * parse the 'config' uci command (open a section)
  209. */
  210. static void uci_parse_config(struct uci_context *ctx, char **str)
  211. {
  212. char *type, *name;
  213. *str += strlen(*str) + 1;
  214. if (!*str) {
  215. ctx->pctx->byte = *str - ctx->pctx->buf;
  216. UCI_THROW(ctx, UCI_ERR_PARSE);
  217. }
  218. type = next_arg(ctx, str, true);
  219. name = next_arg(ctx, str, false);
  220. assert_eol(ctx, str);
  221. }
  222. /*
  223. * parse the 'option' uci command (open a value)
  224. */
  225. static void uci_parse_option(struct uci_context *ctx, char **str)
  226. {
  227. char *name, *value;
  228. *str += strlen(*str) + 1;
  229. name = next_arg(ctx, str, true);
  230. value = next_arg(ctx, str, true);
  231. assert_eol(ctx, str);
  232. DPRINTF("\tOption: %s=\"%s\"\n", name, value);
  233. }
  234. /*
  235. * parse a complete input line, split up combined commands by ';'
  236. */
  237. static void uci_parse_line(struct uci_context *ctx)
  238. {
  239. struct uci_parse_context *pctx = ctx->pctx;
  240. char *word, *brk;
  241. for (word = strtok_r(pctx->buf, ";", &brk);
  242. word;
  243. word = strtok_r(NULL, ";", &brk)) {
  244. char *pbrk;
  245. word = strtok_r(word, " \t", &pbrk);
  246. switch(word[0]) {
  247. case 'c':
  248. if ((word[1] == 0) || !strcmp(word + 1, "onfig"))
  249. uci_parse_config(ctx, &word);
  250. break;
  251. case 'o':
  252. if ((word[1] == 0) || !strcmp(word + 1, "ption"))
  253. uci_parse_option(ctx, &word);
  254. break;
  255. default:
  256. pctx->byte = word - pctx->buf;
  257. UCI_THROW(ctx, UCI_ERR_PARSE);
  258. break;
  259. }
  260. }
  261. }
  262. int uci_load(struct uci_context *ctx, const char *name)
  263. {
  264. struct uci_parse_context *pctx;
  265. struct stat statbuf;
  266. char *filename;
  267. bool confpath;
  268. UCI_HANDLE_ERR(ctx);
  269. UCI_ASSERT(ctx, name != NULL);
  270. /* make sure no memory from previous parse attempts is leaked */
  271. uci_parse_cleanup(ctx);
  272. pctx = (struct uci_parse_context *) uci_malloc(ctx, sizeof(struct uci_parse_context));
  273. ctx->pctx = pctx;
  274. switch (name[0]) {
  275. case '.':
  276. case '/':
  277. /* absolute/relative path outside of /etc/config */
  278. filename = (char *) name;
  279. confpath = false;
  280. break;
  281. default:
  282. filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
  283. sprintf(filename, UCI_CONFDIR "/%s", name);
  284. confpath = true;
  285. break;
  286. }
  287. if ((stat(filename, &statbuf) < 0) ||
  288. ((statbuf.st_mode & S_IFMT) != S_IFREG))
  289. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  290. pctx->file = fopen(filename, "r");
  291. if (filename != name)
  292. free(filename);
  293. if (!pctx->file)
  294. UCI_THROW(ctx, UCI_ERR_IO);
  295. pctx->cfg = uci_alloc_file(ctx, name);
  296. while (!feof(pctx->file)) {
  297. uci_getln(ctx);
  298. if (pctx->buf[0])
  299. uci_parse_line(ctx);
  300. }
  301. /* add to main config file list */
  302. uci_list_add(&ctx->root, &pctx->cfg->list);
  303. pctx->cfg = NULL;
  304. /* no error happened, we can get rid of the parser context now */
  305. uci_parse_cleanup(ctx);
  306. return 0;
  307. }