roxml_api.c 6.4 KB

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