shell_tinyxml.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. ////////////////////////////////////////
  2. // shell_tinyxml_read.cpp
  3. //
  4. // This file implements the means to read an XML encoded file and populate the
  5. // CLI tree based on the contents.
  6. ////////////////////////////////////////
  7. extern "C" {
  8. #include "private.h"
  9. #include "lub/string.h"
  10. #include "lub/ctype.h"
  11. }
  12. /*lint +libh(tinyxml/tinyxml.h) Add this to the library file list */
  13. #include <stdlib.h>
  14. #include "tinyxml/tinyxml.h"
  15. #include <string.h>
  16. #include <assert.h>
  17. typedef void (PROCESS_FN) (clish_shell_t * instance,
  18. TiXmlElement * element, void *parent);
  19. // Define a control block for handling the decode of an XML file
  20. typedef struct clish_xml_cb_s clish_xml_cb_t;
  21. struct clish_xml_cb_s {
  22. const char *element;
  23. PROCESS_FN *handler;
  24. };
  25. // forward declare the handler functions
  26. static PROCESS_FN
  27. process_clish_module,
  28. process_startup,
  29. process_view,
  30. process_command,
  31. process_param,
  32. process_action,
  33. process_ptype,
  34. process_overview,
  35. process_detail,
  36. process_namespace,
  37. process_config,
  38. process_var;
  39. static clish_xml_cb_t xml_elements[] = {
  40. {"CLISH_MODULE", process_clish_module},
  41. {"STARTUP", process_startup},
  42. {"VIEW", process_view},
  43. {"COMMAND", process_command},
  44. {"PARAM", process_param},
  45. {"ACTION", process_action},
  46. {"PTYPE", process_ptype},
  47. {"OVERVIEW", process_overview},
  48. {"DETAIL", process_detail},
  49. {"NAMESPACE", process_namespace},
  50. {"CONFIG", process_config},
  51. {"VAR", process_var},
  52. {NULL, NULL}
  53. };
  54. ///////////////////////////////////////
  55. // This function reads an element from the XML stream and processes it.
  56. ///////////////////////////////////////
  57. static void process_node(clish_shell_t * shell, TiXmlNode * node, void *parent)
  58. {
  59. switch (node->Type()) {
  60. case TiXmlNode::DOCUMENT:
  61. break;
  62. case TiXmlNode::ELEMENT:
  63. clish_xml_cb_t * cb;
  64. for (cb = &xml_elements[0]; cb->element; cb++) {
  65. if (0 == strcmp(node->Value(), cb->element)) {
  66. TiXmlElement *element = (TiXmlElement *) node;
  67. #ifdef DEBUG
  68. fprintf(stderr, "NODE:");
  69. element->Print(stderr, 0);
  70. fprintf(stderr, "\n***\n");
  71. #endif
  72. // process the elements at this level
  73. cb->handler(shell, element, parent);
  74. break;
  75. }
  76. }
  77. break;
  78. case TiXmlNode::COMMENT:
  79. case TiXmlNode::TEXT:
  80. case TiXmlNode::DECLARATION:
  81. case TiXmlNode::TYPECOUNT:
  82. case TiXmlNode::UNKNOWN:
  83. default:
  84. break;
  85. }
  86. }
  87. ///////////////////////////////////////
  88. static void process_children(clish_shell_t * shell,
  89. TiXmlElement * element, void *parent = NULL)
  90. {
  91. for (TiXmlNode * node = element->FirstChild();
  92. node; node = element->IterateChildren(node)) {
  93. // Now deal with all the contained elements
  94. process_node(shell, node, parent);
  95. }
  96. }
  97. ///////////////////////////////////////
  98. static void
  99. process_clish_module(clish_shell_t * shell, TiXmlElement * element, void *)
  100. {
  101. // create the global view
  102. if (!shell->global)
  103. shell->global = clish_shell_find_create_view(shell,
  104. "global", "");
  105. process_children(shell, element, shell->global);
  106. }
  107. ///////////////////////////////////////
  108. static void process_view(clish_shell_t * shell, TiXmlElement * element, void *)
  109. {
  110. clish_view_t *view;
  111. const char *name = element->Attribute("name");
  112. const char *prompt = element->Attribute("prompt");
  113. const char *depth = element->Attribute("depth");
  114. const char *restore = element->Attribute("restore");
  115. const char *access = element->Attribute("access");
  116. bool allowed = true;
  117. /* Check permissions */
  118. if (access) {
  119. allowed = false;
  120. if (shell->client_hooks->access_fn)
  121. allowed = shell->client_hooks->access_fn(shell, access)
  122. ? true : false;
  123. }
  124. if (!allowed)
  125. return;
  126. // re-use a view if it already exists
  127. view = clish_shell_find_create_view(shell, name, prompt);
  128. if (depth && (*depth != '\0') && (lub_ctype_isdigit(*depth))) {
  129. unsigned res = atoi(depth);
  130. clish_view__set_depth(view, res);
  131. }
  132. if (restore) {
  133. if (!lub_string_nocasecmp(restore, "depth"))
  134. clish_view__set_restore(view, CLISH_RESTORE_DEPTH);
  135. else if (!lub_string_nocasecmp(restore, "view"))
  136. clish_view__set_restore(view, CLISH_RESTORE_VIEW);
  137. else
  138. clish_view__set_restore(view, CLISH_RESTORE_NONE);
  139. }
  140. process_children(shell, element, view);
  141. }
  142. ///////////////////////////////////////
  143. static void process_ptype(clish_shell_t * shell, TiXmlElement * element, void *)
  144. {
  145. clish_ptype_method_e method;
  146. clish_ptype_preprocess_e preprocess;
  147. clish_ptype_t *ptype;
  148. const char *name = element->Attribute("name");
  149. const char *help = element->Attribute("help");
  150. const char *pattern = element->Attribute("pattern");
  151. const char *method_name = element->Attribute("method");
  152. const char *preprocess_name = element->Attribute("preprocess");
  153. assert(name);
  154. assert(pattern);
  155. method = clish_ptype_method_resolve(method_name);
  156. preprocess = clish_ptype_preprocess_resolve(preprocess_name);
  157. ptype = clish_shell_find_create_ptype(shell,
  158. name, help, pattern, method, preprocess);
  159. assert(ptype);
  160. }
  161. ///////////////////////////////////////
  162. static void
  163. process_overview(clish_shell_t * shell, TiXmlElement * element, void *)
  164. {
  165. // read the following text element
  166. TiXmlNode *text = element->FirstChild();
  167. if (text) {
  168. assert(TiXmlNode::TEXT == text->Type());
  169. // set the overview text for this view
  170. assert(NULL == shell->overview);
  171. // store the overview
  172. shell->overview = lub_string_dup(text->Value());
  173. }
  174. }
  175. ////////////////////////////////////////
  176. static void
  177. process_command(clish_shell_t * shell, TiXmlElement * element, void *parent)
  178. {
  179. clish_view_t *v = (clish_view_t *) parent;
  180. clish_command_t *cmd = NULL;
  181. bool allowed = true;
  182. clish_command_t *old;
  183. char *alias_name = NULL;
  184. clish_view_t *alias_view = NULL;
  185. const char *access = element->Attribute("access");
  186. const char *name = element->Attribute("name");
  187. const char *help = element->Attribute("help");
  188. const char *view = element->Attribute("view");
  189. const char *viewid = element->Attribute("viewid");
  190. const char *escape_chars = element->Attribute("escape_chars");
  191. const char *args_name = element->Attribute("args");
  192. const char *args_help = element->Attribute("args_help");
  193. const char *lock = element->Attribute("lock");
  194. const char *interrupt = element->Attribute("interrupt");
  195. const char *ref = element->Attribute("ref");
  196. /* Check permissions */
  197. if (access) {
  198. allowed = false;
  199. if (shell->client_hooks->access_fn)
  200. allowed = shell->client_hooks->access_fn(shell, access)
  201. ? true : false;
  202. }
  203. if (!allowed)
  204. return;
  205. old = clish_view_find_command(v, name, BOOL_FALSE);
  206. // check this command doesn't already exist
  207. if (old) {
  208. // flag the duplication then ignore further definition
  209. printf("DUPLICATE COMMAND: %s\n",
  210. clish_command__get_name(old));
  211. return;
  212. }
  213. assert(name);
  214. assert(help);
  215. /* Reference 'ref' field */
  216. if (ref) {
  217. char *saveptr;
  218. const char *delim = "@";
  219. char *view_name = NULL;
  220. char *cmd = NULL;
  221. char *str = lub_string_dup(ref);
  222. cmd = strtok_r(str, delim, &saveptr);
  223. if (!cmd) {
  224. printf("EMPTY REFERENCE COMMAND: %s\n", name);
  225. lub_string_free(str);
  226. return;
  227. }
  228. alias_name = lub_string_dup(cmd);
  229. view_name = strtok_r(NULL, delim, &saveptr);
  230. if (!view_name)
  231. alias_view = v;
  232. else
  233. alias_view = clish_shell_find_create_view(shell,
  234. view_name, NULL);
  235. lub_string_free(str);
  236. }
  237. /* create a command */
  238. cmd = clish_view_new_command(v, name, help);
  239. assert(cmd);
  240. clish_command__set_pview(cmd, v);
  241. /* define some specialist escape characters */
  242. if (escape_chars)
  243. clish_command__set_escape_chars(cmd, escape_chars);
  244. if (args_name) {
  245. /* define a "rest of line" argument */
  246. clish_param_t *param;
  247. clish_ptype_t *tmp = NULL;
  248. assert(args_help);
  249. tmp = clish_shell_find_ptype(shell, "internal_ARGS");
  250. assert(tmp);
  251. param = clish_param_new(args_name, args_help, tmp);
  252. clish_command__set_args(cmd, param);
  253. }
  254. // define the view which this command changes to
  255. if (view) {
  256. clish_view_t *next = clish_shell_find_create_view(shell, view,
  257. NULL);
  258. // reference the next view
  259. clish_command__set_view(cmd, next);
  260. }
  261. // define the view id which this command changes to
  262. if (viewid)
  263. clish_command__set_viewid(cmd, viewid);
  264. /* lock field */
  265. if (lock && (lub_string_nocasecmp(lock, "false") == 0))
  266. clish_command__set_lock(cmd, BOOL_FALSE);
  267. else
  268. clish_command__set_lock(cmd, BOOL_TRUE);
  269. /* interrupt field */
  270. if (interrupt && (lub_string_nocasecmp(interrupt, "true") == 0))
  271. clish_command__set_interrupt(cmd, BOOL_TRUE);
  272. else
  273. clish_command__set_interrupt(cmd, BOOL_FALSE);
  274. /* Set alias */
  275. if (alias_name) {
  276. clish_command__set_alias(cmd, alias_name);
  277. assert(alias_view);
  278. clish_command__set_alias_view(cmd, alias_view);
  279. lub_string_free(alias_name);
  280. }
  281. process_children(shell, element, cmd);
  282. }
  283. ///////////////////////////////////////
  284. static void
  285. process_startup(clish_shell_t * shell, TiXmlElement * element, void *parent)
  286. {
  287. clish_view_t *v = (clish_view_t *) parent;
  288. clish_command_t *cmd = NULL;
  289. const char *view = element->Attribute("view");
  290. const char *viewid = element->Attribute("viewid");
  291. const char *default_shebang = element->Attribute("default_shebang");
  292. const char *timeout = element->Attribute("timeout");
  293. assert(!shell->startup);
  294. assert(view);
  295. /* create a command with NULL help */
  296. cmd = clish_view_new_command(v, "startup", NULL);
  297. clish_command__set_lock(cmd, BOOL_FALSE);
  298. // define the view which this command changes to
  299. clish_view_t *next = clish_shell_find_create_view(shell, view, NULL);
  300. // reference the next view
  301. clish_command__set_view(cmd, next);
  302. // define the view id which this command changes to
  303. if (viewid)
  304. clish_command__set_viewid(cmd, viewid);
  305. if (default_shebang)
  306. clish_shell__set_default_shebang(shell, default_shebang);
  307. if (timeout)
  308. clish_shell__set_timeout(shell, atoi(timeout));
  309. // remember this command
  310. shell->startup = cmd;
  311. process_children(shell, element, cmd);
  312. }
  313. ///////////////////////////////////////
  314. static void
  315. process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
  316. {
  317. clish_command_t *cmd = NULL;
  318. clish_param_t *p_param = NULL;
  319. if (0 == lub_string_nocasecmp(element->Parent()->Value(), "PARAM"))
  320. p_param = (clish_param_t *)parent;
  321. else
  322. cmd = (clish_command_t *)parent;
  323. if (cmd || p_param) {
  324. assert((!cmd) || (cmd != shell->startup));
  325. const char *name = element->Attribute("name");
  326. const char *help = element->Attribute("help");
  327. const char *ptype = element->Attribute("ptype");
  328. const char *prefix = element->Attribute("prefix");
  329. const char *defval = element->Attribute("default");
  330. const char *mode = element->Attribute("mode");
  331. const char *optional = element->Attribute("optional");
  332. const char *value = element->Attribute("value");
  333. const char *hidden = element->Attribute("hidden");
  334. const char *test = element->Attribute("test");
  335. const char *completion = element->Attribute("completion");
  336. clish_param_t *param;
  337. clish_ptype_t *tmp = NULL;
  338. // create a command
  339. assert(name);
  340. assert(help);
  341. assert(ptype);
  342. if (ptype && *ptype) {
  343. tmp = clish_shell_find_create_ptype(shell, ptype,
  344. NULL, NULL,
  345. CLISH_PTYPE_REGEXP,
  346. CLISH_PTYPE_NONE);
  347. assert(tmp);
  348. }
  349. param = clish_param_new(name, help, tmp);
  350. /* If prefix is set clish will emulate old optional
  351. * command syntax over newer optional command mechanism.
  352. * It will create nested PARAM.
  353. */
  354. if (prefix) {
  355. const char *ptype_name = "__SUBCOMMAND";
  356. clish_param_t *opt_param = NULL;
  357. /* Create a ptype for prefix-named subcommand that
  358. * will contain the nested optional parameter. The
  359. * name of ptype is hardcoded. It's not good but
  360. * it's only the service ptype.
  361. */
  362. tmp = (clish_ptype_t *)lub_bintree_find(
  363. &shell->ptype_tree, ptype_name);
  364. if (!tmp)
  365. tmp = clish_shell_find_create_ptype(shell,
  366. ptype_name, "Option", "[^\\]+",
  367. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  368. assert(tmp);
  369. opt_param = clish_param_new(prefix, help, tmp);
  370. clish_param__set_mode(opt_param,
  371. CLISH_PARAM_SUBCOMMAND);
  372. clish_param__set_optional(opt_param, BOOL_TRUE);
  373. if (test)
  374. clish_param__set_test(opt_param, test);
  375. if (cmd)
  376. // add the parameter to the command
  377. clish_command_insert_param(cmd, opt_param);
  378. if (p_param)
  379. // add the parameter to the param
  380. clish_param_insert_param(p_param, opt_param);
  381. /* Unset cmd and set parent param to opt_param */
  382. cmd = NULL;
  383. p_param = opt_param;
  384. }
  385. if (defval)
  386. clish_param__set_default(param, defval);
  387. if (hidden && (lub_string_nocasecmp(hidden, "true") == 0))
  388. clish_param__set_hidden(param, BOOL_TRUE);
  389. else
  390. clish_param__set_hidden(param, BOOL_FALSE);
  391. if (mode) {
  392. if (!lub_string_nocasecmp(mode, "switch")) {
  393. clish_param__set_mode(param,
  394. CLISH_PARAM_SWITCH);
  395. /* Force hidden attribute */
  396. clish_param__set_hidden(param, BOOL_TRUE);
  397. } else if (!lub_string_nocasecmp(mode, "subcommand"))
  398. clish_param__set_mode(param,
  399. CLISH_PARAM_SUBCOMMAND);
  400. else
  401. clish_param__set_mode(param,
  402. CLISH_PARAM_COMMON);
  403. }
  404. if (optional && (lub_string_nocasecmp(optional, "true") == 0))
  405. clish_param__set_optional(param, BOOL_TRUE);
  406. else
  407. clish_param__set_optional(param, BOOL_FALSE);
  408. if (value) {
  409. clish_param__set_value(param, value);
  410. /* Force mode to subcommand */
  411. clish_param__set_mode(param,
  412. CLISH_PARAM_SUBCOMMAND);
  413. }
  414. if (test && !prefix)
  415. clish_param__set_test(param, test);
  416. if (completion)
  417. clish_param__set_completion(param, completion);
  418. if (cmd)
  419. // add the parameter to the command
  420. clish_command_insert_param(cmd, param);
  421. if (p_param)
  422. // add the parameter to the param
  423. clish_param_insert_param(p_param, param);
  424. process_children(shell, element, param);
  425. }
  426. }
  427. ////////////////////////////////////////
  428. static void
  429. process_action(clish_shell_t * shell, TiXmlElement * element, void *parent)
  430. {
  431. clish_action_t *action = NULL;
  432. TiXmlNode *text = element->FirstChild();
  433. const char *builtin = element->Attribute("builtin");
  434. const char *shebang = element->Attribute("shebang");
  435. if (!lub_string_nocasecmp(element->Parent()->Value(), "VAR"))
  436. action = clish_var__get_action((clish_var_t *)parent);
  437. else
  438. action = clish_command__get_action((clish_command_t *)parent);
  439. assert(action);
  440. if (text) {
  441. assert(TiXmlNode::TEXT == text->Type());
  442. // store the action
  443. clish_action__set_script(action, text->Value());
  444. }
  445. if (builtin)
  446. clish_action__set_builtin(action, builtin);
  447. if (shebang)
  448. clish_action__set_shebang(action, shebang);
  449. }
  450. ////////////////////////////////////////
  451. static void
  452. process_detail(clish_shell_t * shell, TiXmlElement * element, void *parent)
  453. {
  454. clish_command_t *cmd = (clish_command_t *) parent;
  455. // read the following text element
  456. TiXmlNode *text = element->FirstChild();
  457. if (cmd && text) {
  458. assert(TiXmlNode::TEXT == text->Type());
  459. // store the action
  460. clish_command__set_detail(cmd, text->Value());
  461. }
  462. }
  463. ///////////////////////////////////////
  464. static void
  465. process_namespace(clish_shell_t * shell, TiXmlElement * element, void *parent)
  466. {
  467. clish_view_t *v = (clish_view_t *) parent;
  468. clish_nspace_t *nspace = NULL;
  469. const char *view = element->Attribute("ref");
  470. const char *prefix = element->Attribute("prefix");
  471. const char *prefix_help = element->Attribute("prefix_help");
  472. const char *help = element->Attribute("help");
  473. const char *completion = element->Attribute("completion");
  474. const char *context_help = element->Attribute("context_help");
  475. const char *inherit = element->Attribute("inherit");
  476. const char *access = element->Attribute("access");
  477. bool allowed = true;
  478. if (access) {
  479. allowed = false;
  480. if (shell->client_hooks->access_fn)
  481. allowed = shell->client_hooks->access_fn(shell, access)
  482. ? true : false;
  483. }
  484. if (!allowed)
  485. return;
  486. assert(view);
  487. clish_view_t *ref_view = clish_shell_find_create_view(shell,
  488. view, NULL);
  489. assert(ref_view);
  490. nspace = clish_nspace_new(ref_view);
  491. assert(nspace);
  492. clish_view_insert_nspace(v, nspace);
  493. if (prefix) {
  494. clish_nspace__set_prefix(nspace, prefix);
  495. if (prefix_help)
  496. clish_nspace_create_prefix_cmd(nspace,
  497. "prefix",
  498. prefix_help);
  499. else
  500. clish_nspace_create_prefix_cmd(nspace,
  501. "prefix",
  502. "Prefix for the imported commands.");
  503. }
  504. if (help && (lub_string_nocasecmp(help, "true") == 0))
  505. clish_nspace__set_help(nspace, BOOL_TRUE);
  506. else
  507. clish_nspace__set_help(nspace, BOOL_FALSE);
  508. if (completion && (lub_string_nocasecmp(completion, "false") == 0))
  509. clish_nspace__set_completion(nspace, BOOL_FALSE);
  510. else
  511. clish_nspace__set_completion(nspace, BOOL_TRUE);
  512. if (context_help && (lub_string_nocasecmp(context_help, "true") == 0))
  513. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  514. else
  515. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  516. if (inherit && (lub_string_nocasecmp(inherit, "false") == 0))
  517. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  518. else
  519. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  520. }
  521. ////////////////////////////////////////
  522. static void
  523. process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
  524. {
  525. clish_command_t *cmd = (clish_command_t *)parent;
  526. clish_config_t *config;
  527. if (!cmd)
  528. return;
  529. config = clish_command__get_config(cmd);
  530. // read the following text element
  531. const char *operation = element->Attribute("operation");
  532. const char *priority = element->Attribute("priority");
  533. const char *pattern = element->Attribute("pattern");
  534. const char *file = element->Attribute("file");
  535. const char *splitter = element->Attribute("splitter");
  536. const char *seq = element->Attribute("sequence");
  537. const char *unique = element->Attribute("unique");
  538. const char *depth = element->Attribute("depth");
  539. if (operation && !lub_string_nocasecmp(operation, "unset"))
  540. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  541. else if (operation && !lub_string_nocasecmp(operation, "none"))
  542. clish_config__set_op(config, CLISH_CONFIG_NONE);
  543. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  544. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  545. else {
  546. clish_config__set_op(config, CLISH_CONFIG_SET);
  547. /* The priority if no clearly specified */
  548. clish_config__set_priority(config, 0x7f00);
  549. }
  550. if (priority && (*priority != '\0')) {
  551. long val = 0;
  552. char *endptr;
  553. unsigned short pri;
  554. val = strtol(priority, &endptr, 0);
  555. if (endptr == priority)
  556. pri = 0;
  557. else if (val > 0xffff)
  558. pri = 0xffff;
  559. else if (val < 0)
  560. pri = 0;
  561. else
  562. pri = (unsigned short)val;
  563. clish_config__set_priority(config, pri);
  564. }
  565. if (pattern)
  566. clish_config__set_pattern(config, pattern);
  567. else
  568. clish_config__set_pattern(config, "^${__cmd}");
  569. if (file)
  570. clish_config__set_file(config, file);
  571. if (splitter && (lub_string_nocasecmp(splitter, "false") == 0))
  572. clish_config__set_splitter(config, BOOL_FALSE);
  573. else
  574. clish_config__set_splitter(config, BOOL_TRUE);
  575. if (unique && (lub_string_nocasecmp(unique, "false") == 0))
  576. clish_config__set_unique(config, BOOL_FALSE);
  577. else
  578. clish_config__set_unique(config, BOOL_TRUE);
  579. if (seq)
  580. clish_config__set_seq(config, seq);
  581. else
  582. /* The entries without sequence cannot be non-unique */
  583. clish_config__set_unique(config, BOOL_TRUE);
  584. if (depth)
  585. clish_config__set_depth(config, depth);
  586. }
  587. ///////////////////////////////////////
  588. static void process_var(clish_shell_t * shell, TiXmlElement * element, void *)
  589. {
  590. clish_var_t *var = NULL;
  591. const char *name = element->Attribute("name");
  592. const char *dynamic = element->Attribute("dynamic");
  593. const char *value = element->Attribute("value");
  594. assert(name);
  595. /* Check if this var doesn't already exist */
  596. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  597. if (var) {
  598. printf("DUPLICATE VAR: %s\n", name);
  599. assert(!var);
  600. }
  601. /* Create var instance */
  602. var = clish_var_new(name);
  603. lub_bintree_insert(&shell->var_tree, var);
  604. if (dynamic && (lub_string_nocasecmp(dynamic, "true") == 0))
  605. clish_var__set_dynamic(var, BOOL_TRUE);
  606. if (value)
  607. clish_var__set_value(var, value);
  608. process_children(shell, element, var);
  609. }
  610. ///////////////////////////////////////
  611. int clish_shell_xml_read(clish_shell_t * shell, const char *filename)
  612. {
  613. int ret = -1;
  614. TiXmlDocument doc;
  615. // keep the white space
  616. TiXmlBase::SetCondenseWhiteSpace(false);
  617. if (doc.LoadFile(filename)) {
  618. TiXmlNode *child = 0;
  619. while ((child = doc.IterateChildren(child))) {
  620. process_node(shell, child, NULL);
  621. }
  622. ret = 0;
  623. } else {
  624. printf("Unable to open %s (%s at line %d, col %d)\n",
  625. filename, doc.ErrorDesc(),
  626. doc.ErrorRow(), doc.ErrorCol());
  627. }
  628. return ret;
  629. }
  630. ///////////////////////////////////////