1
0

shell_libxml2.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * ------------------------------------------------------
  3. * shell_roxml.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 roxml
  8. * ------------------------------------------------------
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #if defined(HAVE_LIB_LIBXML2)
  14. #include <errno.h>
  15. #include <string.h>
  16. #include <libxml/parser.h>
  17. #include <libxml/tree.h>
  18. #include "xmlapi.h"
  19. /* dummy stuff ; really a xmlDoc */
  20. struct clish_xmldoc_s {
  21. int dummy;
  22. };
  23. /* dummy stuff ; really a xmlNode */
  24. struct clish_xmlnode_s {
  25. int dummy;
  26. };
  27. static inline xmlDoc *xmldoc_to_doc(clish_xmldoc_t *doc)
  28. {
  29. return (xmlDoc*)doc;
  30. }
  31. static inline xmlNode *xmlnode_to_node(clish_xmlnode_t *node)
  32. {
  33. return (xmlNode*)node;
  34. }
  35. static inline clish_xmldoc_t *doc_to_xmldoc(xmlDoc *node)
  36. {
  37. return (clish_xmldoc_t*)node;
  38. }
  39. static inline clish_xmlnode_t *node_to_xmlnode(xmlNode *node)
  40. {
  41. return (clish_xmlnode_t*)node;
  42. }
  43. /*
  44. * public interface
  45. */
  46. clish_xmldoc_t *clish_xmldoc_read(const char *filename)
  47. {
  48. xmlDoc *doc = xmlReadFile(filename, NULL, 0);
  49. return doc_to_xmldoc(doc);
  50. }
  51. void clish_xmldoc_release(clish_xmldoc_t *doc)
  52. {
  53. if (doc) {
  54. xmlFreeDoc(xmldoc_to_doc(doc));
  55. xmlCleanupParser();
  56. }
  57. }
  58. int clish_xmldoc_is_valid(clish_xmldoc_t *doc)
  59. {
  60. return doc != NULL;
  61. }
  62. int clish_xmldoc_error_caps(clish_xmldoc_t *doc)
  63. {
  64. return CLISH_XMLERR_NOCAPS;
  65. }
  66. int clish_xmldoc_get_err_line(clish_xmldoc_t *doc)
  67. {
  68. return -1;
  69. }
  70. int clish_xmldoc_get_err_col(clish_xmldoc_t *doc)
  71. {
  72. return -1;
  73. }
  74. const char *clish_xmldoc_get_err_msg(clish_xmldoc_t *doc)
  75. {
  76. return "";
  77. }
  78. int clish_xmlnode_get_type(clish_xmlnode_t *node)
  79. {
  80. if (node) {
  81. xmlNode *n = xmlnode_to_node(node);
  82. switch (n->type) {
  83. case XML_ELEMENT_NODE:
  84. return CLISH_XMLNODE_ELM;
  85. case XML_TEXT_NODE:
  86. return CLISH_XMLNODE_TEXT;
  87. case XML_COMMENT_NODE:
  88. return CLISH_XMLNODE_COMMENT;
  89. case XML_PI_NODE:
  90. return CLISH_XMLNODE_PI;
  91. case XML_ATTRIBUTE_NODE:
  92. return CLISH_XMLNODE_ATTR;
  93. default:
  94. break;
  95. }
  96. }
  97. return CLISH_XMLNODE_UNKNOWN;
  98. }
  99. clish_xmlnode_t *clish_xmldoc_get_root(clish_xmldoc_t *doc)
  100. {
  101. if (doc) {
  102. xmlNode *root = xmlDocGetRootElement(xmldoc_to_doc(doc));
  103. return node_to_xmlnode(root);
  104. }
  105. return NULL;
  106. }
  107. clish_xmlnode_t *clish_xmlnode_parent(clish_xmlnode_t *node)
  108. {
  109. if (node) {
  110. xmlNode *n = xmlnode_to_node(node);
  111. xmlNode *root = xmlDocGetRootElement(n->doc);
  112. if (n != root)
  113. return node_to_xmlnode(n->parent);
  114. }
  115. return NULL;
  116. }
  117. clish_xmlnode_t *clish_xmlnode_next_child(clish_xmlnode_t *parent,
  118. clish_xmlnode_t *curchild)
  119. {
  120. xmlNode *child;
  121. if (!parent)
  122. return NULL;
  123. if (curchild) {
  124. child = xmlnode_to_node(curchild)->next;
  125. } else {
  126. child = xmlnode_to_node(parent)->children;
  127. }
  128. return node_to_xmlnode(child);
  129. }
  130. char *clish_xmlnode_fetch_attr(clish_xmlnode_t *node,
  131. const char *attrname)
  132. {
  133. xmlNode *n;
  134. if (!node || !attrname)
  135. return NULL;
  136. n = xmlnode_to_node(node);
  137. if (n->type == XML_ELEMENT_NODE) {
  138. xmlAttr *a = n->properties;
  139. while (a) {
  140. if (strcmp((char*)a->name, attrname) == 0) {
  141. if (a->children && a->children->content)
  142. return (char *)a->children->content;
  143. else
  144. return NULL;
  145. }
  146. a = a->next;
  147. }
  148. }
  149. return NULL;
  150. }
  151. int clish_xmlnode_get_content(clish_xmlnode_t *node, char *content,
  152. unsigned int *contentlen)
  153. {
  154. xmlNode *n;
  155. xmlNode *c;
  156. int rlen = 0;
  157. if (content && contentlen && *contentlen)
  158. *content = 0;
  159. if (!node || !content || !contentlen)
  160. return -EINVAL;
  161. if (*contentlen <= 1)
  162. return -EINVAL;
  163. *content = 0;
  164. n = xmlnode_to_node(node);
  165. /* first, get the content length */
  166. c = n->children;
  167. while (c) {
  168. if (c->type == XML_TEXT_NODE && !xmlIsBlankNode(c)) {
  169. rlen += strlen((char*)c->content);
  170. }
  171. c = c->next;
  172. }
  173. ++rlen;
  174. if (rlen <= *contentlen) {
  175. c = n->children;
  176. while (c) {
  177. if (c->type == XML_TEXT_NODE && !xmlIsBlankNode(c)) {
  178. strcat(content, (char*)c->content);
  179. }
  180. c = c->next;
  181. }
  182. return 0;
  183. } else {
  184. *contentlen = rlen;
  185. return -E2BIG;
  186. }
  187. }
  188. int clish_xmlnode_get_name(clish_xmlnode_t *node, char *name,
  189. unsigned int *namelen)
  190. {
  191. int rlen;
  192. xmlNode *n;
  193. if (name && namelen && *namelen)
  194. *name = 0;
  195. if (!node || !name || !namelen)
  196. return -EINVAL;
  197. if (*namelen <= 1)
  198. return -EINVAL;
  199. *name = 0;
  200. n = xmlnode_to_node(node);
  201. rlen = strlen((char*)n->name) + 1;
  202. if (rlen <= *namelen) {
  203. sprintf(name, "%s", (char*)n->name);
  204. return 0;
  205. } else {
  206. *namelen = rlen;
  207. return -E2BIG;
  208. }
  209. }
  210. void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out)
  211. {
  212. xmlNode *n;
  213. xmlAttr *a;
  214. n = xmlnode_to_node(node);
  215. if (n && n->name) {
  216. fprintf(out, "<%s", (char*)n->name);
  217. a = n->properties;
  218. while (a) {
  219. char *av = "";
  220. if (a->children && a->children->content)
  221. av = (char*)a->children->content;
  222. fprintf(out, " %s='%s'", (char*)a->name, av);
  223. a = a->next;
  224. }
  225. fprintf(out, ">");
  226. }
  227. }
  228. void clish_xml_release(void *p)
  229. {
  230. /* do we allocate memory? not yet. */
  231. }
  232. #endif /* HAVE_LIB_LIBXML2 */