1
0

shell_tinyxml.cpp 19 KB

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