shell_roxml.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. snprintf(v, *vl, "%s", c);
  235. v[*vl - 1] = '\0';
  236. roxml_release(c);
  237. return 0;
  238. } else {
  239. *vl = len;
  240. roxml_release(c);
  241. return -E2BIG;
  242. }
  243. }
  244. *vl = (unsigned int)-1;
  245. return -ENOMEM;
  246. }
  247. int clish_xmlnode_get_name(clish_xmlnode_t *node, char *name,
  248. unsigned int *namelen)
  249. {
  250. if (name && namelen && *namelen)
  251. *name = 0;
  252. if (!node || !name || !namelen)
  253. return -EINVAL;
  254. if (*namelen <= 1)
  255. return -EINVAL;
  256. *name = 0;
  257. return i_get_name(xmlnode_to_node(node), name, namelen);
  258. }
  259. void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out)
  260. {
  261. node_t *roxn;
  262. char *name;
  263. roxn = xmlnode_to_node(node);
  264. name = roxml_get_name(roxn, NULL, 0);
  265. if (name) {
  266. fprintf(out, "<%s", name);
  267. roxml_release(name);
  268. if (roxml_get_attr_nb(roxn)) {
  269. int attr_count = roxml_get_attr_nb(roxn);
  270. int attr_pos;
  271. for (attr_pos = 0; attr_pos < attr_count; ++attr_pos) {
  272. node_t *attr = roxml_get_attr(roxn, NULL, attr_pos);
  273. char *n = roxml_get_name(attr, NULL, 0);
  274. char *v = roxml_get_content(attr, NULL, 0, NULL);
  275. if (n && v) {
  276. fprintf(out, " %s='%s'", n, v);
  277. }
  278. if (v)
  279. roxml_release(v);
  280. if (n)
  281. roxml_release(n);
  282. }
  283. }
  284. fprintf(out, ">");
  285. }
  286. }
  287. void clish_xml_release(void *p)
  288. {
  289. if (p) {
  290. roxml_release(p);
  291. }
  292. }
  293. #endif /* HAVE_LIB_ROXML */