shell_libxml2.c 6.1 KB

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