shell_expat.c 11 KB

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