shell_tinyxml.cpp 21 KB

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