1
0

shell_tinyxml.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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 *cmdn = NULL;
  223. char *str = lub_string_dup(ref);
  224. cmdn = strtok_r(str, delim, &saveptr);
  225. if (!cmdn) {
  226. printf("EMPTY REFERENCE COMMAND: %s\n", name);
  227. lub_string_free(str);
  228. return;
  229. }
  230. alias_name = lub_string_dup(cmdn);
  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. assert(!((alias_view == v) && (!strcmp(alias_name, name))));
  279. clish_command__set_alias(cmd, alias_name);
  280. assert(alias_view);
  281. clish_command__set_alias_view(cmd, alias_view);
  282. lub_string_free(alias_name);
  283. }
  284. process_children(shell, element, cmd);
  285. }
  286. ///////////////////////////////////////
  287. static void
  288. process_startup(clish_shell_t * shell, TiXmlElement * element, void *parent)
  289. {
  290. clish_view_t *v = (clish_view_t *) parent;
  291. clish_command_t *cmd = NULL;
  292. const char *view = element->Attribute("view");
  293. const char *viewid = element->Attribute("viewid");
  294. const char *default_shebang = element->Attribute("default_shebang");
  295. const char *timeout = element->Attribute("timeout");
  296. const char *lock = element->Attribute("lock");
  297. const char *interrupt = element->Attribute("interrupt");
  298. assert(!shell->startup);
  299. assert(view);
  300. /* create a command with NULL help */
  301. cmd = clish_view_new_command(v, "startup", NULL);
  302. clish_command__set_lock(cmd, BOOL_FALSE);
  303. // define the view which this command changes to
  304. clish_view_t *next = clish_shell_find_create_view(shell, view, NULL);
  305. // reference the next view
  306. clish_command__set_view(cmd, next);
  307. // define the view id which this command changes to
  308. if (viewid)
  309. clish_command__set_viewid(cmd, viewid);
  310. if (default_shebang)
  311. clish_shell__set_default_shebang(shell, default_shebang);
  312. if (timeout)
  313. clish_shell__set_timeout(shell, atoi(timeout));
  314. /* lock field */
  315. if (lock && (lub_string_nocasecmp(lock, "false") == 0))
  316. clish_command__set_lock(cmd, BOOL_FALSE);
  317. else
  318. clish_command__set_lock(cmd, BOOL_TRUE);
  319. /* interrupt field */
  320. if (interrupt && (lub_string_nocasecmp(interrupt, "true") == 0))
  321. clish_command__set_interrupt(cmd, BOOL_TRUE);
  322. else
  323. clish_command__set_interrupt(cmd, BOOL_FALSE);
  324. // remember this command
  325. shell->startup = cmd;
  326. process_children(shell, element, cmd);
  327. }
  328. ///////////////////////////////////////
  329. static void
  330. process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
  331. {
  332. clish_command_t *cmd = NULL;
  333. clish_param_t *p_param = NULL;
  334. if (0 == lub_string_nocasecmp(element->Parent()->Value(), "PARAM"))
  335. p_param = (clish_param_t *)parent;
  336. else
  337. cmd = (clish_command_t *)parent;
  338. if (cmd || p_param) {
  339. assert((!cmd) || (cmd != shell->startup));
  340. const char *name = element->Attribute("name");
  341. const char *help = element->Attribute("help");
  342. const char *ptype = element->Attribute("ptype");
  343. const char *prefix = element->Attribute("prefix");
  344. const char *defval = element->Attribute("default");
  345. const char *mode = element->Attribute("mode");
  346. const char *optional = element->Attribute("optional");
  347. const char *order = element->Attribute("order");
  348. const char *value = element->Attribute("value");
  349. const char *hidden = element->Attribute("hidden");
  350. const char *test = element->Attribute("test");
  351. const char *completion = element->Attribute("completion");
  352. clish_param_t *param;
  353. clish_ptype_t *tmp = NULL;
  354. // create a command
  355. assert(name);
  356. assert(help);
  357. assert(ptype);
  358. if (ptype && *ptype) {
  359. tmp = clish_shell_find_create_ptype(shell, ptype,
  360. NULL, NULL,
  361. CLISH_PTYPE_REGEXP,
  362. CLISH_PTYPE_NONE);
  363. assert(tmp);
  364. }
  365. param = clish_param_new(name, help, tmp);
  366. /* If prefix is set clish will emulate old optional
  367. * command syntax over newer optional command mechanism.
  368. * It will create nested PARAM.
  369. */
  370. if (prefix) {
  371. const char *ptype_name = "__SUBCOMMAND";
  372. clish_param_t *opt_param = NULL;
  373. /* Create a ptype for prefix-named subcommand that
  374. * will contain the nested optional parameter. The
  375. * name of ptype is hardcoded. It's not good but
  376. * it's only the service ptype.
  377. */
  378. tmp = (clish_ptype_t *)lub_bintree_find(
  379. &shell->ptype_tree, ptype_name);
  380. if (!tmp)
  381. tmp = clish_shell_find_create_ptype(shell,
  382. ptype_name, "Option", "[^\\\\]+",
  383. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  384. assert(tmp);
  385. opt_param = clish_param_new(prefix, help, tmp);
  386. clish_param__set_mode(opt_param,
  387. CLISH_PARAM_SUBCOMMAND);
  388. clish_param__set_optional(opt_param, BOOL_TRUE);
  389. if (test)
  390. clish_param__set_test(opt_param, test);
  391. if (cmd)
  392. // add the parameter to the command
  393. clish_command_insert_param(cmd, opt_param);
  394. if (p_param)
  395. // add the parameter to the param
  396. clish_param_insert_param(p_param, opt_param);
  397. /* Unset cmd and set parent param to opt_param */
  398. cmd = NULL;
  399. p_param = opt_param;
  400. }
  401. if (defval)
  402. clish_param__set_default(param, defval);
  403. if (hidden && (lub_string_nocasecmp(hidden, "true") == 0))
  404. clish_param__set_hidden(param, BOOL_TRUE);
  405. else
  406. clish_param__set_hidden(param, BOOL_FALSE);
  407. if (mode) {
  408. if (!lub_string_nocasecmp(mode, "switch")) {
  409. clish_param__set_mode(param,
  410. CLISH_PARAM_SWITCH);
  411. /* Force hidden attribute */
  412. clish_param__set_hidden(param, BOOL_TRUE);
  413. } else if (!lub_string_nocasecmp(mode, "subcommand"))
  414. clish_param__set_mode(param,
  415. CLISH_PARAM_SUBCOMMAND);
  416. else
  417. clish_param__set_mode(param,
  418. CLISH_PARAM_COMMON);
  419. }
  420. if (optional && (lub_string_nocasecmp(optional, "true") == 0))
  421. clish_param__set_optional(param, BOOL_TRUE);
  422. else
  423. clish_param__set_optional(param, BOOL_FALSE);
  424. if (order && (lub_string_nocasecmp(order, "true") == 0))
  425. clish_param__set_order(param, BOOL_TRUE);
  426. else
  427. clish_param__set_order(param, BOOL_FALSE);
  428. if (value) {
  429. clish_param__set_value(param, value);
  430. /* Force mode to subcommand */
  431. clish_param__set_mode(param,
  432. CLISH_PARAM_SUBCOMMAND);
  433. }
  434. if (test && !prefix)
  435. clish_param__set_test(param, test);
  436. if (completion)
  437. clish_param__set_completion(param, completion);
  438. if (cmd)
  439. // add the parameter to the command
  440. clish_command_insert_param(cmd, param);
  441. if (p_param)
  442. // add the parameter to the param
  443. clish_param_insert_param(p_param, param);
  444. process_children(shell, element, param);
  445. }
  446. }
  447. ////////////////////////////////////////
  448. static void
  449. process_action(clish_shell_t * shell, TiXmlElement * element, void *parent)
  450. {
  451. clish_action_t *action = NULL;
  452. TiXmlNode *text = element->FirstChild();
  453. const char *builtin = element->Attribute("builtin");
  454. const char *shebang = element->Attribute("shebang");
  455. if (!lub_string_nocasecmp(element->Parent()->Value(), "VAR"))
  456. action = clish_var__get_action((clish_var_t *)parent);
  457. else
  458. action = clish_command__get_action((clish_command_t *)parent);
  459. assert(action);
  460. if (text) {
  461. assert(TiXmlNode::TEXT == text->Type());
  462. // store the action
  463. clish_action__set_script(action, text->Value());
  464. }
  465. if (builtin)
  466. clish_action__set_builtin(action, builtin);
  467. if (shebang)
  468. clish_action__set_shebang(action, shebang);
  469. }
  470. ////////////////////////////////////////
  471. static void
  472. process_detail(clish_shell_t * shell, TiXmlElement * element, void *parent)
  473. {
  474. clish_command_t *cmd = (clish_command_t *) parent;
  475. // read the following text element
  476. TiXmlNode *text = element->FirstChild();
  477. if (cmd && text) {
  478. assert(TiXmlNode::TEXT == text->Type());
  479. // store the action
  480. clish_command__set_detail(cmd, text->Value());
  481. }
  482. }
  483. ///////////////////////////////////////
  484. static void
  485. process_namespace(clish_shell_t * shell, TiXmlElement * element, void *parent)
  486. {
  487. clish_view_t *v = (clish_view_t *) parent;
  488. clish_nspace_t *nspace = NULL;
  489. const char *view = element->Attribute("ref");
  490. const char *prefix = element->Attribute("prefix");
  491. const char *prefix_help = element->Attribute("prefix_help");
  492. const char *help = element->Attribute("help");
  493. const char *completion = element->Attribute("completion");
  494. const char *context_help = element->Attribute("context_help");
  495. const char *inherit = element->Attribute("inherit");
  496. const char *access = element->Attribute("access");
  497. bool allowed = true;
  498. if (access) {
  499. allowed = false;
  500. if (shell->client_hooks->access_fn)
  501. allowed = shell->client_hooks->access_fn(shell, access)
  502. ? true : false;
  503. }
  504. if (!allowed)
  505. return;
  506. assert(view);
  507. clish_view_t *ref_view = clish_shell_find_create_view(shell,
  508. view, NULL);
  509. assert(ref_view);
  510. /* Don't include itself without prefix */
  511. if ((ref_view == v) && !prefix)
  512. return;
  513. nspace = clish_nspace_new(ref_view);
  514. assert(nspace);
  515. clish_view_insert_nspace(v, nspace);
  516. if (prefix) {
  517. clish_nspace__set_prefix(nspace, prefix);
  518. if (prefix_help)
  519. clish_nspace_create_prefix_cmd(nspace,
  520. "prefix",
  521. prefix_help);
  522. else
  523. clish_nspace_create_prefix_cmd(nspace,
  524. "prefix",
  525. "Prefix for the imported commands.");
  526. }
  527. if (help && (lub_string_nocasecmp(help, "true") == 0))
  528. clish_nspace__set_help(nspace, BOOL_TRUE);
  529. else
  530. clish_nspace__set_help(nspace, BOOL_FALSE);
  531. if (completion && (lub_string_nocasecmp(completion, "false") == 0))
  532. clish_nspace__set_completion(nspace, BOOL_FALSE);
  533. else
  534. clish_nspace__set_completion(nspace, BOOL_TRUE);
  535. if (context_help && (lub_string_nocasecmp(context_help, "true") == 0))
  536. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  537. else
  538. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  539. if (inherit && (lub_string_nocasecmp(inherit, "false") == 0))
  540. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  541. else
  542. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  543. }
  544. ////////////////////////////////////////
  545. static void
  546. process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
  547. {
  548. clish_command_t *cmd = (clish_command_t *)parent;
  549. clish_config_t *config;
  550. if (!cmd)
  551. return;
  552. config = clish_command__get_config(cmd);
  553. // read the following text element
  554. const char *operation = element->Attribute("operation");
  555. const char *priority = element->Attribute("priority");
  556. const char *pattern = element->Attribute("pattern");
  557. const char *file = element->Attribute("file");
  558. const char *splitter = element->Attribute("splitter");
  559. const char *seq = element->Attribute("sequence");
  560. const char *unique = element->Attribute("unique");
  561. const char *depth = element->Attribute("depth");
  562. if (operation && !lub_string_nocasecmp(operation, "unset"))
  563. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  564. else if (operation && !lub_string_nocasecmp(operation, "none"))
  565. clish_config__set_op(config, CLISH_CONFIG_NONE);
  566. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  567. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  568. else {
  569. clish_config__set_op(config, CLISH_CONFIG_SET);
  570. /* The priority if no clearly specified */
  571. clish_config__set_priority(config, 0x7f00);
  572. }
  573. if (priority && (*priority != '\0')) {
  574. long val = 0;
  575. char *endptr;
  576. unsigned short pri;
  577. val = strtol(priority, &endptr, 0);
  578. if (endptr == priority)
  579. pri = 0;
  580. else if (val > 0xffff)
  581. pri = 0xffff;
  582. else if (val < 0)
  583. pri = 0;
  584. else
  585. pri = (unsigned short)val;
  586. clish_config__set_priority(config, pri);
  587. }
  588. if (pattern)
  589. clish_config__set_pattern(config, pattern);
  590. else
  591. clish_config__set_pattern(config, "^${__cmd}");
  592. if (file)
  593. clish_config__set_file(config, file);
  594. if (splitter && (lub_string_nocasecmp(splitter, "false") == 0))
  595. clish_config__set_splitter(config, BOOL_FALSE);
  596. else
  597. clish_config__set_splitter(config, BOOL_TRUE);
  598. if (unique && (lub_string_nocasecmp(unique, "false") == 0))
  599. clish_config__set_unique(config, BOOL_FALSE);
  600. else
  601. clish_config__set_unique(config, BOOL_TRUE);
  602. if (seq)
  603. clish_config__set_seq(config, seq);
  604. else
  605. /* The entries without sequence cannot be non-unique */
  606. clish_config__set_unique(config, BOOL_TRUE);
  607. if (depth)
  608. clish_config__set_depth(config, depth);
  609. }
  610. ///////////////////////////////////////
  611. static void process_var(clish_shell_t * shell, TiXmlElement * element, void *)
  612. {
  613. clish_var_t *var = NULL;
  614. const char *name = element->Attribute("name");
  615. const char *dynamic = element->Attribute("dynamic");
  616. const char *value = element->Attribute("value");
  617. assert(name);
  618. /* Check if this var doesn't already exist */
  619. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  620. if (var) {
  621. printf("DUPLICATE VAR: %s\n", name);
  622. assert(!var);
  623. }
  624. /* Create var instance */
  625. var = clish_var_new(name);
  626. lub_bintree_insert(&shell->var_tree, var);
  627. if (dynamic && (lub_string_nocasecmp(dynamic, "true") == 0))
  628. clish_var__set_dynamic(var, BOOL_TRUE);
  629. if (value)
  630. clish_var__set_value(var, value);
  631. process_children(shell, element, var);
  632. }
  633. ///////////////////////////////////////
  634. static void process_wdog(clish_shell_t *shell,
  635. TiXmlElement *element, void *parent)
  636. {
  637. clish_view_t *v = (clish_view_t *)parent;
  638. clish_command_t *cmd = NULL;
  639. assert(!shell->wdog);
  640. /* create a command with NULL help */
  641. cmd = clish_view_new_command(v, "watchdog", NULL);
  642. clish_command__set_lock(cmd, BOOL_FALSE);
  643. /* Remember this command */
  644. shell->wdog = cmd;
  645. process_children(shell, element, cmd);
  646. }
  647. ///////////////////////////////////////
  648. int clish_shell_xml_read(clish_shell_t * shell, const char *filename)
  649. {
  650. int ret = -1;
  651. TiXmlDocument doc;
  652. // keep the white space
  653. TiXmlBase::SetCondenseWhiteSpace(false);
  654. if (doc.LoadFile(filename)) {
  655. TiXmlNode *child = 0;
  656. while ((child = doc.IterateChildren(child))) {
  657. process_node(shell, child, NULL);
  658. }
  659. ret = 0;
  660. } else {
  661. printf("Unable to open %s (%s at line %d, col %d)\n",
  662. filename, doc.ErrorDesc(),
  663. doc.ErrorRow(), doc.ErrorCol());
  664. }
  665. return ret;
  666. }
  667. ///////////////////////////////////////