shell_tinyxml_read.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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, process_detail, process_namespace, process_config;
  34. static clish_xml_cb_t xml_elements[] = {
  35. {"CLISH_MODULE", process_clish_module},
  36. {"STARTUP", process_startup},
  37. {"VIEW", process_view},
  38. {"COMMAND", process_command},
  39. {"PARAM", process_param},
  40. {"ACTION", process_action},
  41. {"PTYPE", process_ptype},
  42. {"OVERVIEW", process_overview},
  43. {"DETAIL", process_detail},
  44. {"NAMESPACE", process_namespace},
  45. {"CONFIG", process_config},
  46. {NULL, NULL}
  47. };
  48. ///////////////////////////////////////
  49. // This function reads an element from the XML stream and processes it.
  50. ///////////////////////////////////////
  51. static void process_node(clish_shell_t * shell, TiXmlNode * node, void *parent)
  52. {
  53. switch (node->Type()) {
  54. case TiXmlNode::DOCUMENT:
  55. break;
  56. case TiXmlNode::ELEMENT:
  57. clish_xml_cb_t * cb;
  58. for (cb = &xml_elements[0]; cb->element; cb++) {
  59. if (0 == strcmp(node->Value(), cb->element)) {
  60. TiXmlElement *element = (TiXmlElement *) node;
  61. #ifdef DEBUG
  62. fprintf(stderr, "NODE:");
  63. element->Print(stderr, 0);
  64. fprintf(stderr, "\n***\n");
  65. #endif
  66. // process the elements at this level
  67. cb->handler(shell, element, parent);
  68. break;
  69. }
  70. }
  71. break;
  72. case TiXmlNode::COMMENT:
  73. case TiXmlNode::TEXT:
  74. case TiXmlNode::DECLARATION:
  75. case TiXmlNode::TYPECOUNT:
  76. case TiXmlNode::UNKNOWN:
  77. default:
  78. break;
  79. }
  80. }
  81. ///////////////////////////////////////
  82. static void
  83. process_children(clish_shell_t * shell,
  84. TiXmlElement * element, void *parent = NULL)
  85. {
  86. for (TiXmlNode * node = element->FirstChild();
  87. NULL != node; node = element->IterateChildren(node)) {
  88. // Now deal with all the contained elements
  89. process_node(shell, node, parent);
  90. }
  91. }
  92. ///////////////////////////////////////
  93. static void
  94. process_clish_module(clish_shell_t * shell, TiXmlElement * element, void *)
  95. {
  96. // create the global view
  97. if (NULL == shell->global) {
  98. shell->global =
  99. clish_shell_find_create_view(shell, "global", "");
  100. }
  101. process_children(shell, element, shell->global);
  102. }
  103. ///////////////////////////////////////
  104. static void process_view(clish_shell_t * shell, TiXmlElement * element, void *)
  105. {
  106. clish_view_t *view;
  107. const char *name = element->Attribute("name");
  108. const char *prompt = element->Attribute("prompt");
  109. const char *depth = element->Attribute("depth");
  110. const char *restore = element->Attribute("restore");
  111. // re-use a view if it already exists
  112. view = clish_shell_find_create_view(shell, name, prompt);
  113. if ((depth != NULL) && (*depth != '\0') && (lub_ctype_isdigit(*depth))) {
  114. unsigned res = atoi(depth);
  115. clish_view__set_depth(view, res);
  116. }
  117. if (restore && !lub_string_nocasecmp(restore, "depth"))
  118. clish_view__set_restore(view, CLISH_RESTORE_DEPTH);
  119. else if (restore && !lub_string_nocasecmp(restore, "view"))
  120. clish_view__set_restore(view, CLISH_RESTORE_VIEW);
  121. else
  122. clish_view__set_restore(view, CLISH_RESTORE_NONE);
  123. process_children(shell, element, view);
  124. }
  125. ///////////////////////////////////////
  126. static void process_ptype(clish_shell_t * shell, TiXmlElement * element, void *)
  127. {
  128. clish_ptype_method_e method;
  129. clish_ptype_preprocess_e preprocess;
  130. clish_ptype_t *ptype;
  131. const char *name = element->Attribute("name");
  132. const char *help = element->Attribute("help");
  133. const char *pattern = element->Attribute("pattern");
  134. const char *method_name = element->Attribute("method");
  135. const char *preprocess_name = element->Attribute("preprocess");
  136. assert(name);
  137. assert(pattern);
  138. method = clish_ptype_method_resolve(method_name);
  139. preprocess = clish_ptype_preprocess_resolve(preprocess_name);
  140. ptype = clish_shell_find_create_ptype(shell,
  141. name,
  142. help,
  143. pattern, method, preprocess);
  144. assert(ptype);
  145. }
  146. ///////////////////////////////////////
  147. static void
  148. process_overview(clish_shell_t * shell, TiXmlElement * element, void *)
  149. {
  150. // read the following text element
  151. TiXmlNode *text = element->FirstChild();
  152. if (NULL != text) {
  153. assert(TiXmlNode::TEXT == text->Type());
  154. // set the overview text for this view
  155. assert(NULL == shell->overview);
  156. // store the overview
  157. shell->overview = lub_string_dup(text->Value());
  158. }
  159. }
  160. ////////////////////////////////////////
  161. static void
  162. process_command(clish_shell_t * shell, TiXmlElement * element, void *parent)
  163. {
  164. clish_view_t *v = (clish_view_t *) parent;
  165. clish_command_t *cmd = NULL;
  166. const char *access = element->Attribute("access");
  167. bool allowed = true;
  168. if (NULL != access) {
  169. allowed = false; // err on the side of caution
  170. if (shell->client_hooks->access_fn) {
  171. // get the client to authenticate
  172. allowed =
  173. shell->client_hooks->access_fn(shell,
  174. access) ? true :
  175. false;
  176. }
  177. }
  178. if (allowed) {
  179. const char *name = element->Attribute("name");
  180. const char *help = element->Attribute("help");
  181. const char *view = element->Attribute("view");
  182. const char *viewid = element->Attribute("viewid");
  183. const char *escape_chars = element->Attribute("escape_chars");
  184. const char *args_name = element->Attribute("args");
  185. const char *args_help = element->Attribute("args_help");
  186. clish_command_t *old = clish_view_find_command(v, name, BOOL_FALSE);
  187. // check this command doesn't already exist
  188. if (NULL != old) {
  189. // flag the duplication then ignore further definition
  190. printf("DUPLICATE COMMAND: %s\n",
  191. clish_command__get_name(old));
  192. } else {
  193. assert(name);
  194. assert(help);
  195. /* create a command */
  196. cmd = clish_view_new_command(v, name, help);
  197. assert(cmd);
  198. clish_command__set_pview(cmd, v);
  199. if (NULL != escape_chars) {
  200. /* define some specialist escape characters */
  201. clish_command__set_escape_chars(cmd,
  202. escape_chars);
  203. }
  204. if (NULL != args_name) {
  205. /* define a "rest of line" argument */
  206. clish_param_t *param;
  207. clish_ptype_t *tmp = NULL;
  208. assert(NULL != args_help);
  209. tmp = clish_shell_find_create_ptype(shell,
  210. "internal_ARGS",
  211. "Arguments", "[^\\]+",
  212. CLISH_PTYPE_REGEXP,
  213. CLISH_PTYPE_NONE);
  214. assert(tmp);
  215. param =
  216. clish_param_new(args_name, args_help, tmp);
  217. clish_command__set_args(cmd, param);
  218. }
  219. // define the view which this command changes to
  220. if (NULL != view) {
  221. clish_view_t *next =
  222. clish_shell_find_create_view(shell, view,
  223. NULL);
  224. // reference the next view
  225. clish_command__set_view(cmd, next);
  226. }
  227. // define the view id which this command changes to
  228. if (NULL != viewid) {
  229. clish_command__set_viewid(cmd, viewid);
  230. }
  231. process_children(shell, element, cmd);
  232. }
  233. }
  234. }
  235. ///////////////////////////////////////
  236. static void
  237. process_startup(clish_shell_t * shell, TiXmlElement * element, void *parent)
  238. {
  239. clish_view_t *v = (clish_view_t *) parent;
  240. clish_command_t *cmd = NULL;
  241. const char *view = element->Attribute("view");
  242. const char *viewid = element->Attribute("viewid");
  243. const char *env_view = getenv("CLISH_VIEW");
  244. const char *env_viewid = getenv("CLISH_VIEWID");
  245. /* Redefine startup view if environment is set. */
  246. if (env_view)
  247. view = env_view;
  248. if (env_viewid)
  249. viewid = env_viewid;
  250. assert(NULL == shell->startup);
  251. assert(view);
  252. /* create a command with NULL help */
  253. cmd = clish_view_new_command(v, "startup", NULL);
  254. // define the view which this command changes to
  255. clish_view_t *next = clish_shell_find_create_view(shell, view, NULL);
  256. // reference the next view
  257. clish_command__set_view(cmd, next);
  258. // define the view id which this command changes to
  259. if (NULL != viewid) {
  260. clish_command__set_viewid(cmd, viewid);
  261. }
  262. // remember this command
  263. shell->startup = cmd;
  264. process_children(shell, element, cmd);
  265. }
  266. ///////////////////////////////////////
  267. static void
  268. process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
  269. {
  270. clish_command_t *cmd = NULL;
  271. clish_param_t *p_param = NULL;
  272. if (0 == lub_string_nocasecmp(element->Parent()->Value(), "PARAM"))
  273. p_param = (clish_param_t *) parent;
  274. else
  275. cmd = (clish_command_t *) parent;
  276. if ((NULL != cmd) || (NULL != p_param)) {
  277. assert((!cmd) || (cmd != shell->startup));
  278. const char *name = element->Attribute("name");
  279. const char *help = element->Attribute("help");
  280. const char *ptype = element->Attribute("ptype");
  281. const char *prefix = element->Attribute("prefix");
  282. const char *defval = element->Attribute("default");
  283. const char *mode = element->Attribute("mode");
  284. const char *optional = element->Attribute("optional");
  285. const char *value = element->Attribute("value");
  286. clish_param_t *param;
  287. clish_ptype_t *tmp = NULL;
  288. // create a command
  289. assert(name);
  290. assert(help);
  291. assert(ptype);
  292. if (ptype && *ptype) {
  293. tmp = clish_shell_find_create_ptype(shell, ptype,
  294. NULL, NULL,
  295. CLISH_PTYPE_REGEXP,
  296. CLISH_PTYPE_NONE);
  297. assert(tmp);
  298. }
  299. param = clish_param_new(name, help, tmp);
  300. /* If prefix is set clish will emulate old optional
  301. * command syntax over newer optional command mechanism.
  302. * It will create nested PARAM.
  303. */
  304. if (NULL != prefix) {
  305. const char *ptype_name = "internal_SUBCOMMAND";
  306. clish_param_t *opt_param = NULL;
  307. /* Create a ptype for prefix-named subcommand that
  308. * will contain the nested optional parameter. The
  309. * name of ptype is hardcoded. It's not good but
  310. * it's only the service ptype.
  311. */
  312. tmp = (clish_ptype_t *)lub_bintree_find(
  313. &shell->ptype_tree, ptype_name);
  314. if (!tmp)
  315. tmp = clish_shell_find_create_ptype(shell,
  316. ptype_name, "Option", "[^\\]+",
  317. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  318. assert(tmp);
  319. opt_param = clish_param_new(prefix, help, tmp);
  320. clish_param__set_mode(opt_param,
  321. CLISH_PARAM_SUBCOMMAND);
  322. clish_param__set_optional(opt_param, BOOL_TRUE);
  323. if (cmd)
  324. // add the parameter to the command
  325. clish_command_insert_param(cmd, opt_param);
  326. if (p_param)
  327. // add the parameter to the param
  328. clish_param_insert_param(p_param, opt_param);
  329. /* Unset cmd and set parent param to opt_param */
  330. cmd = NULL;
  331. p_param = opt_param;
  332. }
  333. if (NULL != defval) {
  334. clish_param__set_default(param, defval);
  335. }
  336. if (NULL != mode) {
  337. if (!lub_string_nocasecmp(mode, "switch"))
  338. clish_param__set_mode(param,
  339. CLISH_PARAM_SWITCH);
  340. else if (!lub_string_nocasecmp(mode, "subcommand"))
  341. clish_param__set_mode(param,
  342. CLISH_PARAM_SUBCOMMAND);
  343. else
  344. clish_param__set_mode(param,
  345. CLISH_PARAM_COMMON);
  346. }
  347. if (optional && (lub_string_nocasecmp(optional, "true") == 0))
  348. clish_param__set_optional(param, BOOL_TRUE);
  349. else
  350. clish_param__set_optional(param, BOOL_FALSE);
  351. if (NULL != value) {
  352. clish_param__set_value(param, value);
  353. /* Force mode to subcommand */
  354. clish_param__set_mode(param,
  355. CLISH_PARAM_SUBCOMMAND);
  356. }
  357. if (cmd)
  358. // add the parameter to the command
  359. clish_command_insert_param(cmd, param);
  360. if (p_param)
  361. // add the parameter to the param
  362. clish_param_insert_param(p_param, param);
  363. process_children(shell, element, param);
  364. }
  365. }
  366. ////////////////////////////////////////
  367. static void
  368. process_action(clish_shell_t * shell, TiXmlElement * element, void *parent)
  369. {
  370. clish_command_t *cmd = (clish_command_t *) parent;
  371. if (NULL != cmd) {
  372. // read the following text element
  373. TiXmlNode *text = element->FirstChild();
  374. const char *builtin = element->Attribute("builtin");
  375. if (NULL != text) {
  376. assert(TiXmlNode::TEXT == text->Type());
  377. // store the action
  378. clish_command__set_action(cmd, text->Value());
  379. }
  380. if (NULL != builtin) {
  381. // store the action
  382. clish_command__set_builtin(cmd, builtin);
  383. }
  384. }
  385. }
  386. ////////////////////////////////////////
  387. static void
  388. process_detail(clish_shell_t * shell, TiXmlElement * element, void *parent)
  389. {
  390. clish_command_t *cmd = (clish_command_t *) parent;
  391. // read the following text element
  392. TiXmlNode *text = element->FirstChild();
  393. if ((NULL != cmd) && (NULL != text)) {
  394. assert(TiXmlNode::TEXT == text->Type());
  395. // store the action
  396. clish_command__set_detail(cmd, text->Value());
  397. }
  398. }
  399. ///////////////////////////////////////
  400. static void
  401. process_namespace(clish_shell_t * shell, TiXmlElement * element, void *parent)
  402. {
  403. clish_view_t *v = (clish_view_t *) parent;
  404. clish_nspace_t *nspace = NULL;
  405. const char *view = element->Attribute("ref");
  406. const char *prefix = element->Attribute("prefix");
  407. const char *help = element->Attribute("help");
  408. const char *completion = element->Attribute("completion");
  409. const char *context_help = element->Attribute("context_help");
  410. const char *inherit = element->Attribute("inherit");
  411. assert(view);
  412. clish_view_t *ref_view =
  413. clish_shell_find_create_view(shell, view, NULL);
  414. assert(ref_view);
  415. nspace = clish_nspace_new(ref_view);
  416. assert(nspace);
  417. clish_view_insert_nspace(v, nspace);
  418. if (NULL != prefix) {
  419. clish_nspace__set_prefix(nspace, prefix);
  420. }
  421. if (help && (lub_string_nocasecmp(help, "true") == 0))
  422. clish_nspace__set_help(nspace, BOOL_TRUE);
  423. else
  424. clish_nspace__set_help(nspace, BOOL_FALSE);
  425. if (completion && (lub_string_nocasecmp(completion, "false") == 0))
  426. clish_nspace__set_completion(nspace, BOOL_FALSE);
  427. else
  428. clish_nspace__set_completion(nspace, BOOL_TRUE);
  429. if (context_help && (lub_string_nocasecmp(context_help, "true") == 0))
  430. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  431. else
  432. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  433. if (inherit && (lub_string_nocasecmp(inherit, "false") == 0))
  434. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  435. else
  436. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  437. }
  438. ////////////////////////////////////////
  439. static void
  440. process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
  441. {
  442. clish_command_t *cmd = (clish_command_t *) parent;
  443. if (NULL == cmd)
  444. return;
  445. // read the following text element
  446. const char *operation = element->Attribute("operation");
  447. const char *priority = element->Attribute("priority");
  448. const char *pattern = element->Attribute("pattern");
  449. const char *file = element->Attribute("file");
  450. const char *splitter = element->Attribute("splitter");
  451. const char *seq = element->Attribute("sequence");
  452. const char *seq_num = element->Attribute("sequence_num");
  453. if (operation && !lub_string_nocasecmp(operation, "unset"))
  454. clish_command__set_cfg_op(cmd, CLISH_CONFIG_UNSET);
  455. else if (operation && !lub_string_nocasecmp(operation, "none"))
  456. clish_command__set_cfg_op(cmd, CLISH_CONFIG_NONE);
  457. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  458. clish_command__set_cfg_op(cmd, CLISH_CONFIG_DUMP);
  459. else if (operation && !lub_string_nocasecmp(operation, "copy"))
  460. clish_command__set_cfg_op(cmd, CLISH_CONFIG_COPY);
  461. else
  462. clish_command__set_cfg_op(cmd, CLISH_CONFIG_SET);
  463. if ((priority != NULL) && (*priority != '\0')) {
  464. long val = 0;
  465. char *endptr;
  466. unsigned short pri;
  467. val = strtol(priority, &endptr, 0);
  468. if (endptr == priority)
  469. pri = 0;
  470. else if (val > 0xffff)
  471. pri = 0xffff;
  472. else if (val < 0)
  473. pri = 0;
  474. else
  475. pri = (unsigned short)val;
  476. clish_command__set_priority(cmd, pri);
  477. }
  478. if (pattern != NULL)
  479. clish_command__set_pattern(cmd, pattern);
  480. else
  481. clish_command__set_pattern(cmd, "^${cmd}");
  482. if (file != NULL)
  483. clish_command__set_file(cmd, file);
  484. if (splitter && (lub_string_nocasecmp(splitter, "false") == 0))
  485. clish_command__set_splitter(cmd, BOOL_FALSE);
  486. else
  487. clish_command__set_splitter(cmd, BOOL_TRUE);
  488. if (seq && (lub_string_nocasecmp(seq, "true") == 0))
  489. clish_command__set_seq(cmd, BOOL_TRUE);
  490. else
  491. clish_command__set_seq(cmd, BOOL_FALSE);
  492. if ((seq_num != NULL) && (*seq_num != '\0')) {
  493. long val = 0;
  494. char *endptr;
  495. unsigned short pri;
  496. val = strtol(seq_num, &endptr, 0);
  497. if (endptr == seq_num)
  498. pri = 0;
  499. else if (val > 0xffff)
  500. pri = 0xffff;
  501. else if (val < 0)
  502. pri = 0;
  503. else
  504. pri = (unsigned short)val;
  505. if (0 != pri) {
  506. clish_command__set_seq(cmd, BOOL_TRUE);
  507. clish_command__set_seq_num(cmd, pri);
  508. }
  509. }
  510. }
  511. ///////////////////////////////////////
  512. int clish_shell_xml_read(clish_shell_t * shell, const char *filename)
  513. {
  514. int ret = -1;
  515. TiXmlDocument doc;
  516. // keep the white space
  517. TiXmlBase::SetCondenseWhiteSpace(false);
  518. if (doc.LoadFile(filename)) {
  519. TiXmlNode *child = 0;
  520. while ((child = doc.IterateChildren(child))) {
  521. process_node(shell, child, NULL);
  522. }
  523. ret = 0;
  524. } else {
  525. printf("Unable to open %s\n", filename);
  526. }
  527. return ret;
  528. }
  529. ///////////////////////////////////////