shell_roxml.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_ROXML)
  14. #include <errno.h>
  15. #include <roxml.h>
  16. #include "xmlapi.h"
  17. /* dummy stuff ; really a node_t */
  18. struct clish_xmldoc_s {
  19. int dummy;
  20. };
  21. /* dummy stuff ; really a node_t */
  22. struct clish_xmlnode_s {
  23. int dummy;
  24. };
  25. static inline node_t *xmldoc_to_node(clish_xmldoc_t *doc)
  26. {
  27. return (node_t*)doc;
  28. }
  29. static inline node_t *xmlnode_to_node(clish_xmlnode_t *node)
  30. {
  31. return (node_t*)node;
  32. }
  33. static inline clish_xmldoc_t *node_to_xmldoc(node_t *node)
  34. {
  35. return (clish_xmldoc_t*)node;
  36. }
  37. static inline clish_xmlnode_t *node_to_xmlnode(node_t *node)
  38. {
  39. return (clish_xmlnode_t*)node;
  40. }
  41. /*
  42. * public interface
  43. */
  44. clish_xmldoc_t *clish_xmldoc_read(const char *filename)
  45. {
  46. node_t *doc = roxml_load_doc((char*)filename);
  47. return node_to_xmldoc(doc);
  48. }
  49. void clish_xmldoc_release(clish_xmldoc_t *doc)
  50. {
  51. if (doc) {
  52. node_t *node = xmldoc_to_node(doc);
  53. roxml_release(RELEASE_ALL);
  54. roxml_close(node);
  55. }
  56. }
  57. int clish_xmldoc_is_valid(clish_xmldoc_t *doc)
  58. {
  59. return doc != NULL;
  60. }
  61. int clish_xmldoc_error_caps(clish_xmldoc_t *doc)
  62. {
  63. return CLISH_XMLERR_NOCAPS;
  64. }
  65. int clish_xmldoc_get_err_line(clish_xmldoc_t *doc)
  66. {
  67. return -1;
  68. }
  69. int clish_xmldoc_get_err_col(clish_xmldoc_t *doc)
  70. {
  71. return -1;
  72. }
  73. const char *clish_xmldoc_get_err_msg(clish_xmldoc_t *doc)
  74. {
  75. return "";
  76. }
  77. int clish_xmlnode_get_type(clish_xmlnode_t *node)
  78. {
  79. if (node) {
  80. int type = roxml_get_type(xmlnode_to_node(node));
  81. switch (type) {
  82. case ROXML_ELM_NODE:
  83. return CLISH_XMLNODE_ELM;
  84. case ROXML_TXT_NODE:
  85. return CLISH_XMLNODE_TEXT;
  86. case ROXML_CMT_NODE:
  87. return CLISH_XMLNODE_COMMENT;
  88. case ROXML_PI_NODE:
  89. return CLISH_XMLNODE_PI;
  90. case ROXML_ATTR_NODE:
  91. return CLISH_XMLNODE_ATTR;
  92. default:
  93. break;
  94. }
  95. }
  96. return CLISH_XMLNODE_UNKNOWN;
  97. }
  98. clish_xmlnode_t *clish_xmldoc_get_root(clish_xmldoc_t *doc)
  99. {
  100. node_t *root;
  101. char *name = NULL;
  102. if (!doc)
  103. return NULL;
  104. root = roxml_get_root(xmldoc_to_node(doc));
  105. if (!root)
  106. return NULL;
  107. /* The root node is always documentRoot since libroxml-2.2.2. */
  108. /* It's good but not compatible with another XML parsers. */
  109. name = roxml_get_name(root, NULL, 0);
  110. if (0 == strcmp(name, "documentRoot"))
  111. root = roxml_get_chld(root, NULL, 0);
  112. roxml_release(name);
  113. return node_to_xmlnode(root);
  114. }
  115. clish_xmlnode_t *clish_xmlnode_parent(clish_xmlnode_t *node)
  116. {
  117. if (node) {
  118. node_t *roxn = xmlnode_to_node(node);
  119. node_t *root = roxml_get_root(roxn);
  120. if (roxn != root)
  121. return node_to_xmlnode(roxml_get_parent(roxn));
  122. }
  123. return NULL;
  124. }
  125. clish_xmlnode_t *clish_xmlnode_next_child(clish_xmlnode_t *parent,
  126. clish_xmlnode_t *curchild)
  127. {
  128. node_t *roxc;
  129. if (!parent)
  130. return NULL;
  131. roxc = xmlnode_to_node(curchild);
  132. if (roxc) {
  133. return node_to_xmlnode(roxml_get_next_sibling(roxc));
  134. } else {
  135. node_t *roxp = xmlnode_to_node(parent);
  136. node_t *child = NULL;
  137. int count;
  138. count = roxml_get_chld_nb(roxp);
  139. if (count)
  140. child = roxml_get_chld(roxp, NULL, 0);
  141. return node_to_xmlnode(child);
  142. }
  143. return NULL;
  144. }
  145. static int i_is_needle(char *src, const char *needle)
  146. {
  147. int nlen = strlen(needle);
  148. int slen = strlen(src);
  149. if (slen >= nlen) {
  150. if (strncmp(src, needle, nlen) == 0)
  151. return 1;
  152. }
  153. return 0;
  154. }
  155. /* warning: dst == src is valid */
  156. static void i_decode_and_copy(char *dst, char *src)
  157. {
  158. while (*src) {
  159. if (*src == '&') {
  160. if (i_is_needle(src, "&lt;")) {
  161. *dst++ = '<';
  162. src += 4;
  163. } else if (i_is_needle(src, "&gt;")) {
  164. *dst++ = '>';
  165. src += 4;
  166. } else if (i_is_needle(src, "&amp;")) {
  167. *dst++ = '&';
  168. src += 5;
  169. } else {
  170. *dst++ = *src++;
  171. }
  172. } else {
  173. *dst++ = *src++;
  174. }
  175. }
  176. *dst++ = 0;
  177. }
  178. char *clish_xmlnode_fetch_attr(clish_xmlnode_t *node,
  179. const char *attrname)
  180. {
  181. node_t *roxn;
  182. node_t *attr;
  183. char *content;
  184. if (!node || !attrname)
  185. return NULL;
  186. roxn = xmlnode_to_node(node);
  187. attr = roxml_get_attr(roxn, (char*)attrname, 0);
  188. content = roxml_get_content(attr, NULL, 0, NULL);
  189. if (content) {
  190. i_decode_and_copy(content, content);
  191. }
  192. return content;
  193. }
  194. static int i_get_content(node_t *n, char *v, unsigned int *vl)
  195. {
  196. char *c;
  197. int len;
  198. c = roxml_get_content(n, NULL, 0, NULL);
  199. if (c) {
  200. len = strlen(c) + 1;
  201. if (len <= *vl) {
  202. i_decode_and_copy(v, c);
  203. roxml_release(c);
  204. return 0;
  205. } else {
  206. *vl = len;
  207. roxml_release(c);
  208. return -E2BIG;
  209. }
  210. }
  211. *vl = (unsigned int)-1;
  212. return -ENOMEM;
  213. }
  214. int clish_xmlnode_get_content(clish_xmlnode_t *node, char *content,
  215. unsigned int *contentlen)
  216. {
  217. if (content && contentlen && *contentlen)
  218. *content = 0;
  219. if (!node || !content || !contentlen)
  220. return -EINVAL;
  221. if (*contentlen <= 1)
  222. return -EINVAL;
  223. *content = 0;
  224. return i_get_content(xmlnode_to_node(node), content, contentlen);
  225. }
  226. static int i_get_name(node_t *n, char *v, unsigned int *vl)
  227. {
  228. char *c;
  229. int len;
  230. c = roxml_get_name(n, NULL, 0);
  231. if (c) {
  232. len = strlen(c) + 1;
  233. if (len <= *vl) {
  234. sprintf(v, "%s", c);
  235. roxml_release(c);
  236. return 0;
  237. } else {
  238. *vl = len;
  239. roxml_release(c);
  240. return -E2BIG;
  241. }
  242. }
  243. *vl = (unsigned int)-1;
  244. return -ENOMEM;
  245. }
  246. int clish_xmlnode_get_name(clish_xmlnode_t *node, char *name,
  247. unsigned int *namelen)
  248. {
  249. if (name && namelen && *namelen)
  250. *name = 0;
  251. if (!node || !name || !namelen)
  252. return -EINVAL;
  253. if (*namelen <= 1)
  254. return -EINVAL;
  255. *name = 0;
  256. return i_get_name(xmlnode_to_node(node), name, namelen);
  257. }
  258. void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out)
  259. {
  260. node_t *roxn;
  261. char *name;
  262. roxn = xmlnode_to_node(node);
  263. name = roxml_get_name(roxn, NULL, 0);
  264. if (name) {
  265. fprintf(out, "<%s", name);
  266. roxml_release(name);
  267. if (roxml_get_attr_nb(roxn)) {
  268. int attr_count = roxml_get_attr_nb(roxn);
  269. int attr_pos;
  270. for (attr_pos = 0; attr_pos < attr_count; ++attr_pos) {
  271. node_t *attr = roxml_get_attr(roxn, NULL, attr_pos);
  272. char *n = roxml_get_name(attr, NULL, 0);
  273. char *v = roxml_get_content(attr, NULL, 0, NULL);
  274. if (n && v) {
  275. fprintf(out, " %s='%s'", n, v);
  276. }
  277. if (v)
  278. roxml_release(v);
  279. if (n)
  280. roxml_release(n);
  281. }
  282. }
  283. fprintf(out, ">");
  284. }
  285. }
  286. void clish_xml_release(void *p)
  287. {
  288. if (p) {
  289. roxml_release(p);
  290. }
  291. }
  292. #endif /* HAVE_LIB_ROXML */