shell_roxml.c 6.8 KB

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