1
0

shell_roxml.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. if (doc) {
  101. node_t *root = roxml_get_root(xmldoc_to_node(doc));
  102. return node_to_xmlnode(root);
  103. }
  104. return NULL;
  105. }
  106. clish_xmlnode_t *clish_xmlnode_parent(clish_xmlnode_t *node)
  107. {
  108. if (node) {
  109. node_t *roxn = xmlnode_to_node(node);
  110. node_t *root = roxml_get_root(roxn);
  111. if (roxn != root)
  112. return node_to_xmlnode(roxml_get_parent(roxn));
  113. }
  114. return NULL;
  115. }
  116. clish_xmlnode_t *clish_xmlnode_next_child(clish_xmlnode_t *parent,
  117. clish_xmlnode_t *curchild)
  118. {
  119. node_t *roxc;
  120. if (!parent)
  121. return NULL;
  122. roxc = xmlnode_to_node(curchild);
  123. if (roxc) {
  124. return node_to_xmlnode(roxml_get_next_sibling(roxc));
  125. } else {
  126. node_t *roxp = xmlnode_to_node(parent);
  127. node_t *child = NULL;
  128. int count;
  129. count = roxml_get_chld_nb(roxp);
  130. if (count)
  131. child = roxml_get_chld(roxp, NULL, 0);
  132. return node_to_xmlnode(child);
  133. }
  134. return NULL;
  135. }
  136. static int i_is_needle(char *src, const char *needle)
  137. {
  138. int nlen = strlen(needle);
  139. int slen = strlen(src);
  140. if (slen >= nlen) {
  141. if (strncmp(src, needle, nlen) == 0)
  142. return 1;
  143. }
  144. return 0;
  145. }
  146. /* warning: dst == src is valid */
  147. static void i_decode_and_copy(char *dst, char *src)
  148. {
  149. while (*src) {
  150. if (*src == '&') {
  151. if (i_is_needle(src, "&lt;")) {
  152. *dst++ = '<';
  153. src += 4;
  154. } else if (i_is_needle(src, "&gt;")) {
  155. *dst++ = '>';
  156. src += 4;
  157. } else if (i_is_needle(src, "&amp;")) {
  158. *dst++ = '&';
  159. src += 5;
  160. } else {
  161. *dst++ = *src++;
  162. }
  163. } else {
  164. *dst++ = *src++;
  165. }
  166. }
  167. *dst++ = 0;
  168. }
  169. char *clish_xmlnode_fetch_attr(clish_xmlnode_t *node,
  170. const char *attrname)
  171. {
  172. node_t *roxn;
  173. node_t *attr;
  174. char *content;
  175. if (!node || !attrname)
  176. return NULL;
  177. roxn = xmlnode_to_node(node);
  178. attr = roxml_get_attr(roxn, (char*)attrname, 0);
  179. content = roxml_get_content(attr, NULL, 0, NULL);
  180. if (content) {
  181. i_decode_and_copy(content, content);
  182. }
  183. return content;
  184. }
  185. static int i_get_content(node_t *n, char *v, unsigned int *vl)
  186. {
  187. char *c;
  188. int len;
  189. c = roxml_get_content(n, NULL, 0, NULL);
  190. if (c) {
  191. len = strlen(c) + 1;
  192. if (len <= *vl) {
  193. i_decode_and_copy(v, c);
  194. roxml_release(c);
  195. return 0;
  196. } else {
  197. *vl = len;
  198. roxml_release(c);
  199. return -E2BIG;
  200. }
  201. }
  202. *vl = (unsigned int)-1;
  203. return -ENOMEM;
  204. }
  205. int clish_xmlnode_get_content(clish_xmlnode_t *node, char *content,
  206. unsigned int *contentlen)
  207. {
  208. if (content && contentlen && *contentlen)
  209. *content = 0;
  210. if (!node || !content || !contentlen)
  211. return -EINVAL;
  212. if (*contentlen <= 1)
  213. return -EINVAL;
  214. *content = 0;
  215. return i_get_content(xmlnode_to_node(node), content, contentlen);
  216. }
  217. static int i_get_name(node_t *n, char *v, unsigned int *vl)
  218. {
  219. char *c;
  220. int len;
  221. c = roxml_get_name(n, NULL, 0);
  222. if (c) {
  223. len = strlen(c) + 1;
  224. if (len <= *vl) {
  225. sprintf(v, "%s", c);
  226. roxml_release(c);
  227. return 0;
  228. } else {
  229. *vl = len;
  230. roxml_release(c);
  231. return -E2BIG;
  232. }
  233. }
  234. *vl = (unsigned int)-1;
  235. return -ENOMEM;
  236. }
  237. int clish_xmlnode_get_name(clish_xmlnode_t *node, char *name,
  238. unsigned int *namelen)
  239. {
  240. if (name && namelen && *namelen)
  241. *name = 0;
  242. if (!node || !name || !namelen)
  243. return -EINVAL;
  244. if (*namelen <= 1)
  245. return -EINVAL;
  246. *name = 0;
  247. return i_get_name(xmlnode_to_node(node), name, namelen);
  248. }
  249. void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out)
  250. {
  251. node_t *roxn;
  252. char *name;
  253. roxn = xmlnode_to_node(node);
  254. name = roxml_get_name(roxn, NULL, 0);
  255. if (name) {
  256. fprintf(out, "<%s", name);
  257. roxml_release(name);
  258. if (roxml_get_attr_nb(roxn)) {
  259. int attr_count = roxml_get_attr_nb(roxn);
  260. int attr_pos;
  261. for (attr_pos = 0; attr_pos < attr_count; ++attr_pos) {
  262. node_t *attr = roxml_get_attr(roxn, NULL, attr_pos);
  263. char *n = roxml_get_name(attr, NULL, 0);
  264. char *v = roxml_get_content(attr, NULL, 0, NULL);
  265. if (n && v) {
  266. fprintf(out, " %s='%s'", n, v);
  267. }
  268. if (v)
  269. roxml_release(v);
  270. if (n)
  271. roxml_release(n);
  272. }
  273. }
  274. fprintf(out, ">");
  275. }
  276. }
  277. void clish_xml_release(void *p)
  278. {
  279. if (p) {
  280. roxml_release(p);
  281. }
  282. }
  283. #endif /* HAVE_LIB_ROXML */