shell_expat.c 11 KB

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