shell_tinyxml_read.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. printf("NODE:");
  63. element->Print(stdout, 0);
  64. printf("\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. // re-use a view if it already exists
  111. view = clish_shell_find_create_view(shell, name, prompt);
  112. if ((depth != NULL) && (*depth != '\0') && (lub_ctype_isdigit(*depth))) {
  113. unsigned res = atoi(depth);
  114. clish_view__set_depth(view, res);
  115. }
  116. process_children(shell, element, view);
  117. }
  118. ///////////////////////////////////////
  119. static void process_ptype(clish_shell_t * shell, TiXmlElement * element, void *)
  120. {
  121. clish_ptype_method_e method;
  122. clish_ptype_preprocess_e preprocess;
  123. clish_ptype_t *ptype;
  124. const char *name = element->Attribute("name");
  125. const char *help = element->Attribute("help");
  126. const char *pattern = element->Attribute("pattern");
  127. const char *method_name = element->Attribute("method");
  128. const char *preprocess_name = element->Attribute("preprocess");
  129. assert(name);
  130. assert(pattern);
  131. method = clish_ptype_method_resolve(method_name);
  132. preprocess = clish_ptype_preprocess_resolve(preprocess_name);
  133. ptype = clish_shell_find_create_ptype(shell,
  134. name,
  135. help,
  136. pattern, method, preprocess);
  137. assert(ptype);
  138. }
  139. ///////////////////////////////////////
  140. static void
  141. process_overview(clish_shell_t * shell, TiXmlElement * element, void *)
  142. {
  143. // read the following text element
  144. TiXmlNode *text = element->FirstChild();
  145. if (NULL != text) {
  146. assert(TiXmlNode::TEXT == text->Type());
  147. // set the overview text for this view
  148. assert(NULL == shell->overview);
  149. // store the overview
  150. shell->overview = lub_string_dup(text->Value());
  151. }
  152. }
  153. ////////////////////////////////////////
  154. static void
  155. process_command(clish_shell_t * shell, TiXmlElement * element, void *parent)
  156. {
  157. clish_view_t *v = (clish_view_t *) parent;
  158. clish_command_t *cmd = NULL;
  159. const char *access = element->Attribute("access");
  160. bool allowed = true;
  161. if (NULL != access) {
  162. allowed = false; // err on the side of caution
  163. if (shell->client_hooks->access_fn) {
  164. // get the client to authenticate
  165. allowed =
  166. shell->client_hooks->access_fn(shell,
  167. access) ? true :
  168. false;
  169. }
  170. }
  171. if (allowed) {
  172. const char *name = element->Attribute("name");
  173. const char *help = element->Attribute("help");
  174. const char *view = element->Attribute("view");
  175. const char *viewid = element->Attribute("viewid");
  176. const char *escape_chars = element->Attribute("escape_chars");
  177. const char *args_name = element->Attribute("args");
  178. const char *args_help = element->Attribute("args_help");
  179. clish_command_t *old = clish_view_find_command(v, name, BOOL_FALSE);
  180. // check this command doesn't already exist
  181. if (NULL != old) {
  182. // flag the duplication then ignore further definition
  183. printf("DUPLICATE COMMAND: %s\n",
  184. clish_command__get_name(old));
  185. } else {
  186. assert(name);
  187. assert(help);
  188. /* create a command */
  189. cmd = clish_view_new_command(v, name, help);
  190. assert(cmd);
  191. clish_command__set_pview(cmd, v);
  192. if (NULL != escape_chars) {
  193. /* define some specialist escape characters */
  194. clish_command__set_escape_chars(cmd,
  195. escape_chars);
  196. }
  197. if (NULL != args_name) {
  198. /* define a "rest of line" argument */
  199. clish_param_t *param;
  200. clish_ptype_t *tmp = NULL;
  201. assert(NULL != args_help);
  202. tmp = clish_shell_find_create_ptype(shell,
  203. "internal_ARGS",
  204. "Arguments", "[^\\]+",
  205. CLISH_PTYPE_REGEXP,
  206. CLISH_PTYPE_NONE);
  207. assert(tmp);
  208. param =
  209. clish_param_new(args_name, args_help, tmp);
  210. clish_command__set_args(cmd, param);
  211. }
  212. // define the view which this command changes to
  213. if (NULL != view) {
  214. clish_view_t *next =
  215. clish_shell_find_create_view(shell, view,
  216. NULL);
  217. // reference the next view
  218. clish_command__set_view(cmd, next);
  219. }
  220. // define the view id which this command changes to
  221. if (NULL != viewid) {
  222. clish_command__set_viewid(cmd, viewid);
  223. }
  224. process_children(shell, element, cmd);
  225. }
  226. }
  227. }
  228. ///////////////////////////////////////
  229. static void
  230. process_startup(clish_shell_t * shell, TiXmlElement * element, void *parent)
  231. {
  232. clish_view_t *v = (clish_view_t *) parent;
  233. clish_command_t *cmd = NULL;
  234. const char *view = element->Attribute("view");
  235. const char *viewid = element->Attribute("viewid");
  236. const char *env_view = getenv("CLISH_VIEW");
  237. const char *env_viewid = getenv("CLISH_VIEWID");
  238. /* Redefine startup view if environment is set. */
  239. if (env_view)
  240. view = env_view;
  241. if (env_viewid)
  242. viewid = env_viewid;
  243. assert(NULL == shell->startup);
  244. assert(view);
  245. /* create a command with NULL help */
  246. cmd = clish_view_new_command(v, "startup", NULL);
  247. // define the view which this command changes to
  248. clish_view_t *next = clish_shell_find_create_view(shell, view, NULL);
  249. // reference the next view
  250. clish_command__set_view(cmd, next);
  251. // define the view id which this command changes to
  252. if (NULL != viewid) {
  253. clish_command__set_viewid(cmd, viewid);
  254. }
  255. // remember this command
  256. shell->startup = cmd;
  257. process_children(shell, element, cmd);
  258. }
  259. ///////////////////////////////////////
  260. static void
  261. process_param(clish_shell_t * shell, TiXmlElement * element, void *parent)
  262. {
  263. clish_command_t *cmd = NULL;
  264. clish_param_t *p_param = NULL;
  265. if (0 == lub_string_nocasecmp(element->Parent()->Value(), "PARAM"))
  266. p_param = (clish_param_t *) parent;
  267. else
  268. cmd = (clish_command_t *) parent;
  269. if ((NULL != cmd) || (NULL != p_param)) {
  270. assert((!cmd) || (cmd != shell->startup));
  271. const char *name = element->Attribute("name");
  272. const char *help = element->Attribute("help");
  273. const char *ptype = element->Attribute("ptype");
  274. const char *prefix = element->Attribute("prefix");
  275. const char *defval = element->Attribute("default");
  276. const char *mode = element->Attribute("mode");
  277. const char *optional = element->Attribute("optional");
  278. clish_param_t *param;
  279. clish_ptype_t *tmp = NULL;
  280. // create a command
  281. assert(name);
  282. assert(help);
  283. assert(ptype);
  284. if (ptype && *ptype) {
  285. tmp = clish_shell_find_create_ptype(shell, ptype,
  286. NULL, NULL,
  287. CLISH_PTYPE_REGEXP,
  288. CLISH_PTYPE_NONE);
  289. assert(tmp);
  290. }
  291. param = clish_param_new(name, help, tmp);
  292. /* If prefix is set clish will emulate old optional
  293. * command syntax over newer optional command mechanism.
  294. * It will create nested PARAM.
  295. */
  296. if (NULL != prefix) {
  297. clish_param_t *opt_param = NULL;
  298. /* Create a ptype for prefix-named subcommand that
  299. * will contain the nested optional parameter. The
  300. * name of ptype is hardcoded. It's not good but
  301. * it's only the service ptype.
  302. */
  303. tmp =
  304. clish_shell_find_create_ptype(shell,
  305. "internal_SUBCOMMAND",
  306. "Option", "[^\\]+",
  307. CLISH_PTYPE_REGEXP,
  308. CLISH_PTYPE_NONE);
  309. assert(tmp);
  310. opt_param = clish_param_new(prefix, help, tmp);
  311. clish_param__set_mode(opt_param,
  312. CLISH_PARAM_SUBCOMMAND);
  313. clish_param__set_optional(opt_param, BOOL_TRUE);
  314. if (cmd)
  315. // add the parameter to the command
  316. clish_command_insert_param(cmd, opt_param);
  317. if (p_param)
  318. // add the parameter to the param
  319. clish_param_insert_param(p_param, opt_param);
  320. /* Unset cmd and set parent param to opt_param */
  321. cmd = NULL;
  322. p_param = opt_param;
  323. }
  324. if (NULL != defval) {
  325. clish_param__set_default(param, defval);
  326. }
  327. if (NULL != mode) {
  328. if (!lub_string_nocasecmp(mode, "switch"))
  329. clish_param__set_mode(param,
  330. CLISH_PARAM_SWITCH);
  331. else if (!lub_string_nocasecmp(mode, "subcommand"))
  332. clish_param__set_mode(param,
  333. CLISH_PARAM_SUBCOMMAND);
  334. else
  335. clish_param__set_mode(param,
  336. CLISH_PARAM_COMMON);
  337. }
  338. if (optional && (lub_string_nocasecmp(optional, "true") == 0))
  339. clish_param__set_optional(param, BOOL_TRUE);
  340. else
  341. clish_param__set_optional(param, BOOL_FALSE);
  342. if (cmd)
  343. // add the parameter to the command
  344. clish_command_insert_param(cmd, param);
  345. if (p_param)
  346. // add the parameter to the param
  347. clish_param_insert_param(p_param, param);
  348. process_children(shell, element, param);
  349. }
  350. }
  351. ////////////////////////////////////////
  352. static void
  353. process_action(clish_shell_t * shell, TiXmlElement * element, void *parent)
  354. {
  355. clish_command_t *cmd = (clish_command_t *) parent;
  356. if (NULL != cmd) {
  357. // read the following text element
  358. TiXmlNode *text = element->FirstChild();
  359. const char *builtin = element->Attribute("builtin");
  360. if (NULL != text) {
  361. assert(TiXmlNode::TEXT == text->Type());
  362. // store the action
  363. clish_command__set_action(cmd, text->Value());
  364. }
  365. if (NULL != builtin) {
  366. // store the action
  367. clish_command__set_builtin(cmd, builtin);
  368. }
  369. }
  370. }
  371. ////////////////////////////////////////
  372. static void
  373. process_detail(clish_shell_t * shell, TiXmlElement * element, void *parent)
  374. {
  375. clish_command_t *cmd = (clish_command_t *) parent;
  376. // read the following text element
  377. TiXmlNode *text = element->FirstChild();
  378. if ((NULL != cmd) && (NULL != text)) {
  379. assert(TiXmlNode::TEXT == text->Type());
  380. // store the action
  381. clish_command__set_detail(cmd, text->Value());
  382. }
  383. }
  384. ///////////////////////////////////////
  385. static void
  386. process_namespace(clish_shell_t * shell, TiXmlElement * element, void *parent)
  387. {
  388. clish_view_t *v = (clish_view_t *) parent;
  389. clish_nspace_t *nspace = NULL;
  390. const char *view = element->Attribute("ref");
  391. const char *prefix = element->Attribute("prefix");
  392. const char *help = element->Attribute("help");
  393. const char *completion = element->Attribute("completion");
  394. const char *context_help = element->Attribute("context_help");
  395. const char *inherit = element->Attribute("inherit");
  396. assert(view);
  397. clish_view_t *ref_view =
  398. clish_shell_find_create_view(shell, view, NULL);
  399. assert(ref_view);
  400. nspace = clish_nspace_new(ref_view);
  401. assert(nspace);
  402. clish_view_insert_nspace(v, nspace);
  403. if (NULL != prefix) {
  404. clish_nspace__set_prefix(nspace, prefix);
  405. }
  406. if (help && (lub_string_nocasecmp(help, "true") == 0))
  407. clish_nspace__set_help(nspace, BOOL_TRUE);
  408. else
  409. clish_nspace__set_help(nspace, BOOL_FALSE);
  410. if (completion && (lub_string_nocasecmp(completion, "false") == 0))
  411. clish_nspace__set_completion(nspace, BOOL_FALSE);
  412. else
  413. clish_nspace__set_completion(nspace, BOOL_TRUE);
  414. if (context_help && (lub_string_nocasecmp(context_help, "true") == 0))
  415. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  416. else
  417. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  418. if (inherit && (lub_string_nocasecmp(inherit, "false") == 0))
  419. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  420. else
  421. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  422. }
  423. ////////////////////////////////////////
  424. static void
  425. process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
  426. {
  427. clish_command_t *cmd = (clish_command_t *) parent;
  428. if (NULL == cmd)
  429. return;
  430. // read the following text element
  431. const char *operation = element->Attribute("operation");
  432. const char *priority = element->Attribute("priority");
  433. const char *pattern = element->Attribute("pattern");
  434. const char *file = element->Attribute("file");
  435. const char *splitter = element->Attribute("splitter");
  436. if (operation && !lub_string_nocasecmp(operation, "unset"))
  437. clish_command__set_cfg_op(cmd, CLISH_CONFIG_UNSET);
  438. else if (operation && !lub_string_nocasecmp(operation, "none"))
  439. clish_command__set_cfg_op(cmd, CLISH_CONFIG_NONE);
  440. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  441. clish_command__set_cfg_op(cmd, CLISH_CONFIG_DUMP);
  442. else if (operation && !lub_string_nocasecmp(operation, "copy"))
  443. clish_command__set_cfg_op(cmd, CLISH_CONFIG_COPY);
  444. else
  445. clish_command__set_cfg_op(cmd, CLISH_CONFIG_SET);
  446. if ((priority != NULL) && (*priority != '\0')) {
  447. long val = 0;
  448. char *endptr;
  449. unsigned short pri;
  450. val = strtol(priority, &endptr, 0);
  451. if (endptr == priority)
  452. pri = 0;
  453. else if (val > 0xffff)
  454. pri = 0xffff;
  455. else if (val < 0)
  456. pri = 0;
  457. else
  458. pri = (unsigned short)val;
  459. clish_command__set_priority(cmd, pri);
  460. }
  461. if (pattern != NULL)
  462. clish_command__set_pattern(cmd, pattern);
  463. else
  464. clish_command__set_pattern(cmd, "^${cmd}");
  465. if (file != NULL)
  466. clish_command__set_file(cmd, file);
  467. if (splitter && (lub_string_nocasecmp(splitter, "false") == 0))
  468. clish_command__set_splitter(cmd, BOOL_FALSE);
  469. else
  470. clish_command__set_splitter(cmd, BOOL_TRUE);
  471. }
  472. ///////////////////////////////////////
  473. int clish_shell_xml_read(clish_shell_t * shell, const char *filename)
  474. {
  475. int ret = -1;
  476. TiXmlDocument doc;
  477. // keep the white space
  478. TiXmlBase::SetCondenseWhiteSpace(false);
  479. if (doc.LoadFile(filename)) {
  480. TiXmlNode *child = 0;
  481. while ((child = doc.IterateChildren(child))) {
  482. process_node(shell, child, NULL);
  483. }
  484. ret = 0;
  485. } else {
  486. printf("Unable to open %s\n", filename);
  487. }
  488. return ret;
  489. }
  490. ///////////////////////////////////////