shell_roxml.c 6.8 KB

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