shell_expat.c 11 KB

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