shell_roxml.c 6.5 KB

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