shell_libxml2.c 6.0 KB

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