expat_api.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * ------------------------------------------------------
  3. * shell_expat.c
  4. *
  5. * This file implements the means to read an XML encoded file
  6. * and populate the CLI tree based on the contents. It implements
  7. * the clish_xml API using the expat XML parser
  8. *
  9. * expat is not your typicall XML parser. It does not work
  10. * by creating a full in-memory XML tree, but by calling specific
  11. * callbacks (element handlers) regularly while parsing. It's up
  12. * to the user to create the corresponding XML tree if needed
  13. * (obviously, this is what we're doing, as we really need the XML
  14. * tree in klish).
  15. *
  16. * The code below do that. It transforms the output of expat
  17. * to a DOM representation of the underlying XML file. This is
  18. * a bit overkill, and maybe a later implementation will help to
  19. * cut the work to something simpler, but the current klish
  20. * implementation requires this.
  21. * ------------------------------------------------------
  22. */
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. /* FreeBSD have verbatim version of expat named bsdxml */
  30. #ifdef HAVE_LIB_BSDXML
  31. #include <bsdxml.h>
  32. #else
  33. #include <expat.h>
  34. #endif
  35. #include "xmlapi.h"
  36. /** DOM_like XML node
  37. *
  38. * @struct clish_xmlnode_s
  39. */
  40. struct clish_xmlnode_s {
  41. char *name;
  42. clish_xmlnode_t *parent; /**< parent node */
  43. clish_xmlnode_t *children; /**< list of children */
  44. clish_xmlnode_t *next; /**< next sibling */
  45. clish_xmlnode_t *attributes; /**< attributes are nodes too */
  46. char *content; /**< !NULL for text and attributes nodes */
  47. clish_xmlnodetype_e type; /**< node type */
  48. int depth; /**< node depth */
  49. clish_xmldoc_t *doc;
  50. };
  51. /** DOM-like XML document
  52. *
  53. * @struct clish_xmldoc_s
  54. */
  55. struct clish_xmldoc_s {
  56. clish_xmlnode_t *root; /**< list of root elements */
  57. clish_xmlnode_t *current; /**< current element */
  58. char *filename; /**< current filename */
  59. };
  60. /*
  61. * Expat need these functions to be able to build a DOM-like tree that
  62. * will be usable by klish.
  63. */
  64. /** Put a element at the and of an element list
  65. *
  66. * @param first first element of the list
  67. * @param node element to add
  68. * @return new first element of the list
  69. */
  70. static clish_xmlnode_t *clish_expat_list_push_back(clish_xmlnode_t *first, clish_xmlnode_t *node)
  71. {
  72. clish_xmlnode_t *cur = first;
  73. clish_xmlnode_t *prev = NULL;
  74. while (cur) {
  75. prev = cur;
  76. cur = cur->next;
  77. }
  78. if (prev) {
  79. prev->next = node;
  80. return first;
  81. }
  82. return node;
  83. }
  84. /** Generic add_attr() function
  85. *
  86. * @param first first attribute in the attribute list
  87. * @param n attribute name
  88. * @param v attribute value
  89. * @return the new first attribute in the attribute list
  90. */
  91. static clish_xmlnode_t *clish_expat_add_attr(clish_xmlnode_t *first, const char *n, const char *v)
  92. {
  93. clish_xmlnode_t *node;
  94. node = malloc(sizeof(clish_xmlnode_t));
  95. if (!node)
  96. return first;
  97. node->name = strdup(n);
  98. node->content = strdup(v);
  99. node->children = NULL;
  100. node->attributes = NULL;
  101. node->next = NULL;
  102. node->type = CLISH_XMLNODE_ATTR;
  103. node->depth = 0;
  104. return clish_expat_list_push_back(first, node);
  105. }
  106. /** Run through an expat attribute list, and create a DOM-like attribute list
  107. *
  108. * @param node parent node
  109. * @param attr NULL-terminated attribute liste
  110. *
  111. * Each attribute uses two slots in the expat attribute list. The first one is
  112. * used to store the name, the second one is used to store the value.
  113. */
  114. static void clish_expat_add_attrlist(clish_xmlnode_t *node, const char **attr)
  115. {
  116. int i;
  117. for (i = 0; attr[i]; i += 2) {
  118. node->attributes = clish_expat_add_attr(node->attributes,
  119. attr[i], attr[i+1]);
  120. }
  121. }
  122. /** Generic make_node() function
  123. *
  124. * @param parent XML parent node
  125. * @param type XML node type
  126. * @param n node name (can be NULL, strdup'ed)
  127. * @param v node content (can be NULL, strdup'ed)
  128. * @param attr attribute list
  129. * @return a new node or NULL on error
  130. */
  131. static clish_xmlnode_t *clish_expat_make_node(clish_xmlnode_t *parent,
  132. clish_xmlnodetype_e type,
  133. const char *n,
  134. const char *v,
  135. const char **attr)
  136. {
  137. clish_xmlnode_t *node;
  138. node = malloc(sizeof(clish_xmlnode_t));
  139. if (!node)
  140. return NULL;
  141. node->name = n ? strdup(n) : NULL;
  142. node->content = v ? strdup(v) : NULL;
  143. node->children = NULL;
  144. node->attributes = NULL;
  145. node->next = NULL;
  146. node->parent = parent;
  147. node->doc = parent ? parent->doc : NULL;
  148. node->depth = parent ? parent->depth + 1 : 0;
  149. node->type = type;
  150. if (attr)
  151. clish_expat_add_attrlist(node, attr);
  152. if (parent)
  153. parent->children = clish_expat_list_push_back(parent->children, node);
  154. return node;
  155. }
  156. /** Add a new XML root
  157. *
  158. * @param doc XML document
  159. * @param el root node name
  160. * @param attr expat attribute list
  161. * @return a new root element
  162. */
  163. static clish_xmlnode_t *clish_expat_add_root(clish_xmldoc_t *doc, const char *el, const char **attr)
  164. {
  165. clish_xmlnode_t *node;
  166. node = clish_expat_make_node(NULL, CLISH_XMLNODE_ELM, el, NULL, attr);
  167. if (!node)
  168. return doc->root;
  169. doc->root = clish_expat_list_push_back(doc->root, node);
  170. return node;
  171. }
  172. /** Add a new XML element as a child
  173. *
  174. * @param cur parent XML element
  175. * @param el element name
  176. * @param attr expat attribute list
  177. * @return a new XMl element
  178. */
  179. static clish_xmlnode_t *clish_expat_add_child(clish_xmlnode_t *cur, const char *el, const char **attr)
  180. {
  181. clish_xmlnode_t *node;
  182. node = clish_expat_make_node(cur, CLISH_XMLNODE_ELM, el, NULL, attr);
  183. if (!node)
  184. return cur;
  185. return node;
  186. }
  187. /** Expat handler: element content
  188. *
  189. * @param data user data
  190. * @param s content (not nul-termainated)
  191. * @param len content length
  192. */
  193. static void clish_expat_chardata_handler(void *data, const char *s, int len)
  194. {
  195. clish_xmldoc_t *doc = data;
  196. if (doc->current) {
  197. char *content = malloc(len + 1);
  198. strncpy(content, s, len);
  199. content[len] = '\0';
  200. clish_expat_make_node(doc->current, CLISH_XMLNODE_TEXT, NULL, content, NULL);
  201. /*
  202. * the previous call is a bit too generic, and strdup() content
  203. * so we need to free out own version of content.
  204. */
  205. free(content);
  206. }
  207. }
  208. /** Expat handler: start XML element
  209. *
  210. * @param data user data
  211. * @param el element name (nul-terminated)
  212. * @param attr expat attribute list
  213. */
  214. static void clish_expat_element_start(void *data, const char *el, const char **attr)
  215. {
  216. clish_xmldoc_t *doc = data;
  217. if (!doc->current) {
  218. doc->current = clish_expat_add_root(doc, el, attr);
  219. } else {
  220. doc->current = clish_expat_add_child(doc->current, el, attr);
  221. }
  222. }
  223. /** Expat handler: end XML element
  224. *
  225. * @param data user data
  226. * @param el element name
  227. */
  228. static void clish_expat_element_end(void *data, const char *el)
  229. {
  230. clish_xmldoc_t *doc = data;
  231. if (doc->current) {
  232. doc->current = doc->current->parent;
  233. }
  234. el = el; /* Happy compiler */
  235. }
  236. /** Free a node, its children and its attributes
  237. *
  238. * @param node node to free
  239. */
  240. static void clish_expat_free_node(clish_xmlnode_t *cur)
  241. {
  242. clish_xmlnode_t *node;
  243. clish_xmlnode_t *first;
  244. if (cur->attributes) {
  245. first = cur->attributes;
  246. while (first) {
  247. node = first;
  248. first = first->next;
  249. clish_expat_free_node(node);
  250. }
  251. }
  252. if (cur->children) {
  253. first = cur->children;
  254. while (first) {
  255. node = first;
  256. first = first->next;
  257. clish_expat_free_node(node);
  258. }
  259. }
  260. if (cur->name)
  261. free(cur->name);
  262. if (cur->content)
  263. free(cur->content);
  264. free(cur);
  265. }
  266. /*
  267. * Public interface
  268. */
  269. int clish_xmldoc_start(void)
  270. {
  271. return 0;
  272. }
  273. int clish_xmldoc_stop(void)
  274. {
  275. return 0;
  276. }
  277. clish_xmldoc_t *clish_xmldoc_read(const char *filename)
  278. {
  279. clish_xmldoc_t *doc;
  280. struct stat sb;
  281. int fd;
  282. char *buffer;
  283. XML_Parser parser;
  284. int rb;
  285. doc = malloc(sizeof(clish_xmldoc_t));
  286. if (!doc)
  287. return NULL;
  288. memset(doc, 0, sizeof(clish_xmldoc_t));
  289. doc->filename = strdup(filename);
  290. parser = XML_ParserCreate(NULL);
  291. if (!parser)
  292. goto error_parser_create;
  293. XML_SetUserData(parser, doc);
  294. XML_SetCharacterDataHandler(parser, clish_expat_chardata_handler);
  295. XML_SetElementHandler(parser,
  296. clish_expat_element_start,
  297. clish_expat_element_end);
  298. fd = open(filename, O_RDONLY);
  299. if (fd < 0)
  300. goto error_open;
  301. fstat(fd, &sb);
  302. buffer = malloc(sb.st_size+1);
  303. rb = read(fd, buffer, sb.st_size);
  304. if (rb < 0) {
  305. close(fd);
  306. goto error_parse;
  307. }
  308. buffer[sb.st_size] = 0;
  309. close(fd);
  310. if (!XML_Parse(parser, buffer, sb.st_size, 1))
  311. goto error_parse;
  312. XML_ParserFree(parser);
  313. free(buffer);
  314. return doc;
  315. error_parse:
  316. free(buffer);
  317. error_open:
  318. XML_ParserFree(parser);
  319. error_parser_create:
  320. clish_xmldoc_release(doc);
  321. return NULL;
  322. }
  323. void clish_xmldoc_release(clish_xmldoc_t *doc)
  324. {
  325. if (doc) {
  326. clish_xmlnode_t *node;
  327. while (doc->root) {
  328. node = doc->root;
  329. doc->root = node->next;
  330. clish_expat_free_node(node);
  331. }
  332. if (doc->filename)
  333. free(doc->filename);
  334. free(doc);
  335. }
  336. }
  337. int clish_xmldoc_is_valid(clish_xmldoc_t *doc)
  338. {
  339. return doc && doc->root;
  340. }
  341. int clish_xmldoc_error_caps(clish_xmldoc_t *doc)
  342. {
  343. doc = doc; /* Happy compiler */
  344. return CLISH_XMLERR_NOCAPS;
  345. }
  346. int clish_xmldoc_get_err_line(clish_xmldoc_t *doc)
  347. {
  348. doc = doc; /* Happy compiler */
  349. return -1;
  350. }
  351. int clish_xmldoc_get_err_col(clish_xmldoc_t *doc)
  352. {
  353. doc = doc; /* Happy compiler */
  354. return -1;
  355. }
  356. const char *clish_xmldoc_get_err_msg(clish_xmldoc_t *doc)
  357. {
  358. doc = doc; /* Happy compiler */
  359. return "";
  360. }
  361. int clish_xmlnode_get_type(clish_xmlnode_t *node)
  362. {
  363. if (node)
  364. return node->type;
  365. return CLISH_XMLNODE_UNKNOWN;
  366. }
  367. clish_xmlnode_t *clish_xmldoc_get_root(clish_xmldoc_t *doc)
  368. {
  369. if (doc)
  370. return doc->root;
  371. return NULL;
  372. }
  373. clish_xmlnode_t *clish_xmlnode_parent(clish_xmlnode_t *node)
  374. {
  375. if (node)
  376. return node->parent;
  377. return NULL;
  378. }
  379. clish_xmlnode_t *clish_xmlnode_next_child(clish_xmlnode_t *parent,
  380. clish_xmlnode_t *curchild)
  381. {
  382. if (curchild)
  383. return curchild->next;
  384. if (parent)
  385. return parent->children;
  386. return NULL;
  387. }
  388. char *clish_xmlnode_fetch_attr(clish_xmlnode_t *node,
  389. const char *attrname)
  390. {
  391. if (node) {
  392. clish_xmlnode_t *n = node->attributes;
  393. while (n) {
  394. if (strcmp(n->name, attrname) == 0)
  395. return n->content;
  396. n = n->next;
  397. }
  398. }
  399. return NULL;
  400. }
  401. int clish_xmlnode_get_content(clish_xmlnode_t *node, char *content,
  402. unsigned int *contentlen)
  403. {
  404. unsigned int minlen = 1;
  405. if (node && content && contentlen) {
  406. clish_xmlnode_t *children = node->children;
  407. while (children) {
  408. if (children->type == CLISH_XMLNODE_TEXT && children->content)
  409. minlen += strlen(children->content);
  410. children = children->next;
  411. }
  412. if (minlen >= *contentlen) {
  413. *contentlen = minlen + 1;
  414. return -E2BIG;
  415. }
  416. children = node->children;
  417. *content = 0;
  418. while (children) {
  419. if (children->type == CLISH_XMLNODE_TEXT && children->content)
  420. strcat(content, children->content);
  421. children = children->next;
  422. }
  423. return 0;
  424. }
  425. return -EINVAL;
  426. }
  427. int clish_xmlnode_get_name(clish_xmlnode_t *node, char *name,
  428. unsigned int *namelen)
  429. {
  430. if (node && name && namelen) {
  431. if (strlen(node->name) >= *namelen) {
  432. *namelen = strlen(node->name) + 1;
  433. return -E2BIG;
  434. }
  435. snprintf(name, *namelen, "%s", node->name);
  436. name[*namelen - 1] = '\0';
  437. return 0;
  438. }
  439. return -EINVAL;
  440. }
  441. void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out)
  442. {
  443. if (node) {
  444. int i;
  445. clish_xmlnode_t *a;
  446. for (i=0; i<node->depth; ++i) {
  447. fprintf(out, " ");
  448. }
  449. fprintf(out, "<%s", node->name);
  450. a = node->attributes;
  451. while (a) {
  452. fprintf(out, " %s='%s'", a->name, a->content);
  453. a = a->next;
  454. }
  455. fprintf(out, ">...");
  456. }
  457. }
  458. void clish_xml_release(void *p)
  459. {
  460. p = p; /* Happy compiler */
  461. /* nothing to release */
  462. }