shell_xml.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /*
  2. * ------------------------------------------------------
  3. * shell_xml.c
  4. *
  5. * This file implements the means to read an XML encoded file and populate the
  6. * CLI tree based on the contents.
  7. * ------------------------------------------------------
  8. */
  9. #include "private.h"
  10. #include "xmlapi.h"
  11. #include "lub/string.h"
  12. #include "lub/ctype.h"
  13. #include "lub/system.h"
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <sys/types.h>
  19. #include <dirent.h>
  20. typedef void (PROCESS_FN) (clish_shell_t * instance,
  21. clish_xmlnode_t * element, void *parent);
  22. /* Define a control block for handling the decode of an XML file */
  23. typedef struct clish_xml_cb_s clish_xml_cb_t;
  24. struct clish_xml_cb_s {
  25. const char *element;
  26. PROCESS_FN *handler;
  27. };
  28. /* forward declare the handler functions */
  29. static PROCESS_FN
  30. process_clish_module,
  31. process_startup,
  32. process_view,
  33. process_command,
  34. process_param,
  35. process_action,
  36. process_ptype,
  37. process_overview,
  38. process_detail,
  39. process_namespace,
  40. process_config,
  41. process_var,
  42. process_wdog;
  43. static clish_xml_cb_t xml_elements[] = {
  44. {"CLISH_MODULE", process_clish_module},
  45. {"STARTUP", process_startup},
  46. {"VIEW", process_view},
  47. {"COMMAND", process_command},
  48. {"PARAM", process_param},
  49. {"ACTION", process_action},
  50. {"PTYPE", process_ptype},
  51. {"OVERVIEW", process_overview},
  52. {"DETAIL", process_detail},
  53. {"NAMESPACE", process_namespace},
  54. {"CONFIG", process_config},
  55. {"VAR", process_var},
  56. {"WATCHDOG", process_wdog},
  57. {NULL, NULL}
  58. };
  59. /*
  60. * if CLISH_PATH is unset in the environment then this is the value used.
  61. */
  62. const char *default_path = "/etc/clish;~/.clish";
  63. /*-------------------------------------------------------- */
  64. void clish_shell_load_scheme(clish_shell_t *this, const char *xml_path)
  65. {
  66. const char *path = xml_path;
  67. char *buffer;
  68. char *dirname;
  69. char *saveptr;
  70. /* use the default path */
  71. if (!path)
  72. path = default_path;
  73. /* take a copy of the path */
  74. buffer = lub_system_tilde_expand(path);
  75. /* now loop though each directory */
  76. for (dirname = strtok_r(buffer, ";", &saveptr);
  77. dirname; dirname = strtok_r(NULL, ";", &saveptr)) {
  78. DIR *dir;
  79. struct dirent *entry;
  80. /* search this directory for any XML files */
  81. dir = opendir(dirname);
  82. if (NULL == dir) {
  83. #ifdef DEBUG
  84. tinyrl_printf(this->tinyrl,
  85. "*** Failed to open '%s' directory\n",
  86. dirname);
  87. #endif
  88. continue;
  89. }
  90. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  91. const char *extension = strrchr(entry->d_name, '.');
  92. /* check the filename */
  93. if ((NULL != extension) &&
  94. (0 == strcmp(".xml", extension))) {
  95. char *filename = NULL;
  96. /* build the filename */
  97. lub_string_cat(&filename, dirname);
  98. lub_string_cat(&filename, "/");
  99. lub_string_cat(&filename, entry->d_name);
  100. /* load this file */
  101. (void)clish_shell_xml_read(this, filename);
  102. /* release the resource */
  103. lub_string_free(filename);
  104. }
  105. }
  106. /* all done for this directory */
  107. closedir(dir);
  108. }
  109. /* tidy up */
  110. lub_string_free(buffer);
  111. #ifdef DEBUG
  112. clish_shell_dump(this);
  113. #endif
  114. }
  115. /*
  116. * ------------------------------------------------------
  117. * This function reads an element from the XML stream and processes it.
  118. * ------------------------------------------------------
  119. */
  120. static void process_node(clish_shell_t * shell, clish_xmlnode_t * node, void *parent)
  121. {
  122. switch (clish_xmlnode_get_type(node)) {
  123. case CLISH_XMLNODE_ELM: {
  124. clish_xml_cb_t * cb;
  125. char name[128];
  126. unsigned int namelen = sizeof(name);
  127. if (clish_xmlnode_get_name(node, name, &namelen) == 0) {
  128. for (cb = &xml_elements[0]; cb->element; cb++) {
  129. if (0 == strcmp(name, cb->element)) {
  130. #ifdef DEBUG
  131. fprintf(stderr, "NODE:");
  132. clish_xmlnode_print(node, stderr);
  133. fprintf(stderr, "\n");
  134. #endif
  135. /* process the elements at this level */
  136. cb->handler(shell, node, parent);
  137. break;
  138. }
  139. }
  140. }
  141. break;
  142. }
  143. case CLISH_XMLNODE_DOC:
  144. case CLISH_XMLNODE_TEXT:
  145. case CLISH_XMLNODE_ATTR:
  146. case CLISH_XMLNODE_PI:
  147. case CLISH_XMLNODE_COMMENT:
  148. case CLISH_XMLNODE_DECL:
  149. case CLISH_XMLNODE_UNKNOWN:
  150. default:
  151. break;
  152. }
  153. }
  154. /* ------------------------------------------------------ */
  155. static void process_children(clish_shell_t * shell,
  156. clish_xmlnode_t * element, void *parent)
  157. {
  158. clish_xmlnode_t *node = NULL;
  159. while ((node = clish_xmlnode_next_child(element, node)) != NULL) {
  160. /* Now deal with all the contained elements */
  161. process_node(shell, node, parent);
  162. }
  163. }
  164. /* ------------------------------------------------------ */
  165. static void
  166. process_clish_module(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  167. {
  168. // create the global view
  169. if (!shell->global)
  170. shell->global = clish_shell_find_create_view(shell,
  171. "global", "");
  172. process_children(shell, element, shell->global);
  173. }
  174. /* ------------------------------------------------------ */
  175. static void process_view(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  176. {
  177. clish_view_t *view;
  178. int allowed = 1;
  179. char *name = clish_xmlnode_fetch_attr(element, "name");
  180. char *prompt = clish_xmlnode_fetch_attr(element, "prompt");
  181. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  182. char *restore = clish_xmlnode_fetch_attr(element, "restore");
  183. char *access = clish_xmlnode_fetch_attr(element, "access");
  184. /* Check permissions */
  185. if (access) {
  186. allowed = 0;
  187. if (shell->client_hooks->access_fn)
  188. allowed = shell->client_hooks->access_fn(shell, access);
  189. }
  190. if (!allowed)
  191. goto process_view_end;
  192. assert(name);
  193. /* re-use a view if it already exists */
  194. view = clish_shell_find_create_view(shell, name, prompt);
  195. if (depth && (lub_ctype_isdigit(*depth))) {
  196. unsigned res = atoi(depth);
  197. clish_view__set_depth(view, res);
  198. }
  199. if (restore) {
  200. if (!lub_string_nocasecmp(restore, "depth"))
  201. clish_view__set_restore(view, CLISH_RESTORE_DEPTH);
  202. else if (!lub_string_nocasecmp(restore, "view"))
  203. clish_view__set_restore(view, CLISH_RESTORE_VIEW);
  204. else
  205. clish_view__set_restore(view, CLISH_RESTORE_NONE);
  206. }
  207. process_children(shell, element, view);
  208. process_view_end:
  209. clish_xml_release(name);
  210. clish_xml_release(prompt);
  211. clish_xml_release(depth);
  212. clish_xml_release(restore);
  213. clish_xml_release(access);
  214. }
  215. /* ------------------------------------------------------ */
  216. static void process_ptype(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  217. {
  218. clish_ptype_method_e method;
  219. clish_ptype_preprocess_e preprocess;
  220. clish_ptype_t *ptype;
  221. char *name = clish_xmlnode_fetch_attr(element, "name");
  222. char *help = clish_xmlnode_fetch_attr(element, "help");
  223. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  224. char *method_name = clish_xmlnode_fetch_attr(element, "method");
  225. char *preprocess_name = clish_xmlnode_fetch_attr(element, "preprocess");
  226. assert(name);
  227. assert(pattern);
  228. method = clish_ptype_method_resolve(method_name);
  229. preprocess = clish_ptype_preprocess_resolve(preprocess_name);
  230. ptype = clish_shell_find_create_ptype(shell,
  231. name, help, pattern, method, preprocess);
  232. assert(ptype);
  233. clish_xml_release(name);
  234. clish_xml_release(help);
  235. clish_xml_release(pattern);
  236. clish_xml_release(method_name);
  237. clish_xml_release(preprocess_name);
  238. }
  239. /* ------------------------------------------------------ */
  240. static void
  241. process_overview(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  242. {
  243. char *content = NULL;
  244. unsigned int content_len = 2048;
  245. int result;
  246. /*
  247. * the code below faithfully assume that we'll be able fully store
  248. * the content of the node. If it's really, really big, we may have
  249. * an issue (but then, if it's that big, how the hell does it
  250. * already fits in allocated memory?)
  251. * Ergo, it -should- be safe.
  252. */
  253. do {
  254. content = (char*)realloc(content, content_len);
  255. result = clish_xmlnode_get_content(element, content,
  256. &content_len);
  257. } while (result == -E2BIG);
  258. if (result == 0 && content) {
  259. /* set the overview text for this view */
  260. assert(NULL == shell->overview);
  261. /* store the overview */
  262. shell->overview = lub_string_dup(content);
  263. }
  264. if (content)
  265. free(content);
  266. }
  267. /* ------------------------------------------------------ */
  268. static void
  269. process_command(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  270. {
  271. clish_view_t *v = (clish_view_t *) parent;
  272. clish_command_t *cmd = NULL;
  273. clish_command_t *old;
  274. char *alias_name = NULL;
  275. clish_view_t *alias_view = NULL;
  276. int allowed = 1;
  277. char *access = clish_xmlnode_fetch_attr(element, "access");
  278. char *name = clish_xmlnode_fetch_attr(element, "name");
  279. char *help = clish_xmlnode_fetch_attr(element, "help");
  280. char *view = clish_xmlnode_fetch_attr(element, "view");
  281. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  282. char *escape_chars = clish_xmlnode_fetch_attr(element, "escape_chars");
  283. char *args_name = clish_xmlnode_fetch_attr(element, "args");
  284. char *args_help = clish_xmlnode_fetch_attr(element, "args_help");
  285. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  286. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  287. char *ref = clish_xmlnode_fetch_attr(element, "ref");
  288. /* Check permissions */
  289. if (access) {
  290. allowed = 0;
  291. if (shell->client_hooks->access_fn)
  292. allowed = shell->client_hooks->access_fn(shell, access);
  293. }
  294. if (!allowed)
  295. goto process_command_end;
  296. assert(name);
  297. /* check this command doesn't already exist */
  298. old = clish_view_find_command(v, name, BOOL_FALSE);
  299. if (old) {
  300. /* flag the duplication then ignore further definition */
  301. printf("DUPLICATE COMMAND: %s\n",
  302. clish_command__get_name(old));
  303. goto process_command_end;
  304. }
  305. assert(help);
  306. /* Reference 'ref' field */
  307. if (ref) {
  308. char *saveptr;
  309. const char *delim = "@";
  310. char *view_name = NULL;
  311. char *cmdn = NULL;
  312. char *str = lub_string_dup(ref);
  313. cmdn = strtok_r(str, delim, &saveptr);
  314. if (!cmdn) {
  315. printf("EMPTY REFERENCE COMMAND: %s\n", name);
  316. lub_string_free(str);
  317. goto process_command_end;
  318. }
  319. alias_name = lub_string_dup(cmdn);
  320. view_name = strtok_r(NULL, delim, &saveptr);
  321. if (!view_name)
  322. alias_view = v;
  323. else
  324. alias_view = clish_shell_find_create_view(shell,
  325. view_name, NULL);
  326. lub_string_free(str);
  327. }
  328. /* create a command */
  329. cmd = clish_view_new_command(v, name, help);
  330. assert(cmd);
  331. clish_command__set_pview(cmd, v);
  332. /* define some specialist escape characters */
  333. if (escape_chars)
  334. clish_command__set_escape_chars(cmd, escape_chars);
  335. if (args_name) {
  336. /* define a "rest of line" argument */
  337. clish_param_t *param;
  338. clish_ptype_t *tmp = NULL;
  339. assert(args_help);
  340. tmp = clish_shell_find_ptype(shell, "internal_ARGS");
  341. assert(tmp);
  342. param = clish_param_new(args_name, args_help, tmp);
  343. clish_command__set_args(cmd, param);
  344. }
  345. /* define the view which this command changes to */
  346. if (view) {
  347. clish_view_t *next = clish_shell_find_create_view(shell, view,
  348. NULL);
  349. /* reference the next view */
  350. clish_command__set_view(cmd, next);
  351. }
  352. /* define the view id which this command changes to */
  353. if (viewid)
  354. clish_command__set_viewid(cmd, viewid);
  355. /* lock field */
  356. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  357. clish_command__set_lock(cmd, BOOL_FALSE);
  358. else
  359. clish_command__set_lock(cmd, BOOL_TRUE);
  360. /* interrupt field */
  361. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  362. clish_command__set_interrupt(cmd, BOOL_TRUE);
  363. else
  364. clish_command__set_interrupt(cmd, BOOL_FALSE);
  365. /* Set alias */
  366. if (alias_name) {
  367. assert(!((alias_view == v) && (!strcmp(alias_name, name))));
  368. clish_command__set_alias(cmd, alias_name);
  369. assert(alias_view);
  370. clish_command__set_alias_view(cmd, alias_view);
  371. lub_string_free(alias_name);
  372. }
  373. process_children(shell, element, cmd);
  374. process_command_end:
  375. clish_xml_release(access);
  376. clish_xml_release(name);
  377. clish_xml_release(help);
  378. clish_xml_release(view);
  379. clish_xml_release(viewid);
  380. clish_xml_release(escape_chars);
  381. clish_xml_release(args_name);
  382. clish_xml_release(args_help);
  383. clish_xml_release(lock);
  384. clish_xml_release(interrupt);
  385. clish_xml_release(ref);
  386. }
  387. /* ------------------------------------------------------ */
  388. static void
  389. process_startup(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  390. {
  391. clish_view_t *v = (clish_view_t *) parent;
  392. clish_command_t *cmd = NULL;
  393. char *view = clish_xmlnode_fetch_attr(element, "view");
  394. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  395. char *default_shebang =
  396. clish_xmlnode_fetch_attr(element, "default_shebang");
  397. char *timeout = clish_xmlnode_fetch_attr(element, "timeout");
  398. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  399. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  400. assert(!shell->startup);
  401. assert(view);
  402. /* create a command with NULL help */
  403. cmd = clish_view_new_command(v, "startup", NULL);
  404. clish_command__set_lock(cmd, BOOL_FALSE);
  405. /* define the view which this command changes to */
  406. clish_view_t *next = clish_shell_find_create_view(shell, view, NULL);
  407. /* reference the next view */
  408. clish_command__set_view(cmd, next);
  409. /* define the view id which this command changes to */
  410. if (viewid)
  411. clish_command__set_viewid(cmd, viewid);
  412. if (default_shebang)
  413. clish_shell__set_default_shebang(shell, default_shebang);
  414. if (timeout)
  415. clish_shell__set_timeout(shell, atoi(timeout));
  416. /* lock field */
  417. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  418. clish_command__set_lock(cmd, BOOL_FALSE);
  419. else
  420. clish_command__set_lock(cmd, BOOL_TRUE);
  421. /* interrupt field */
  422. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  423. clish_command__set_interrupt(cmd, BOOL_TRUE);
  424. else
  425. clish_command__set_interrupt(cmd, BOOL_FALSE);
  426. /* remember this command */
  427. shell->startup = cmd;
  428. clish_xml_release(view);
  429. clish_xml_release(viewid);
  430. clish_xml_release(default_shebang);
  431. clish_xml_release(timeout);
  432. clish_xml_release(lock);
  433. clish_xml_release(interrupt);
  434. process_children(shell, element, cmd);
  435. }
  436. /* ------------------------------------------------------ */
  437. static void
  438. process_param(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  439. {
  440. clish_command_t *cmd = NULL;
  441. clish_param_t *p_param = NULL;
  442. clish_xmlnode_t *pelement;
  443. char *pname;
  444. pelement = clish_xmlnode_parent(element);
  445. pname = clish_xmlnode_get_all_name(pelement);
  446. if (pname && lub_string_nocasecmp(pname, "PARAM") == 0)
  447. p_param = (clish_param_t *)parent;
  448. else
  449. cmd = (clish_command_t *)parent;
  450. if (pname)
  451. free(pname);
  452. if (cmd || p_param) {
  453. char *name = clish_xmlnode_fetch_attr(element, "name");
  454. char *help = clish_xmlnode_fetch_attr(element, "help");
  455. char *ptype = clish_xmlnode_fetch_attr(element, "ptype");
  456. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  457. char *defval = clish_xmlnode_fetch_attr(element, "default");
  458. char *mode = clish_xmlnode_fetch_attr(element, "mode");
  459. char *optional = clish_xmlnode_fetch_attr(element, "optional");
  460. char *order = clish_xmlnode_fetch_attr(element, "order");
  461. char *value = clish_xmlnode_fetch_attr(element, "value");
  462. char *hidden = clish_xmlnode_fetch_attr(element, "hidden");
  463. char *test = clish_xmlnode_fetch_attr(element, "test");
  464. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  465. clish_param_t *param;
  466. clish_ptype_t *tmp = NULL;
  467. assert((!cmd) || (cmd != shell->startup));
  468. /* create a command */
  469. assert(name);
  470. assert(help);
  471. assert(ptype);
  472. if (*ptype) {
  473. tmp = clish_shell_find_create_ptype(shell, ptype,
  474. NULL, NULL,
  475. CLISH_PTYPE_REGEXP,
  476. CLISH_PTYPE_NONE);
  477. assert(tmp);
  478. }
  479. param = clish_param_new(name, help, tmp);
  480. /* If prefix is set clish will emulate old optional
  481. * command syntax over newer optional command mechanism.
  482. * It will create nested PARAM.
  483. */
  484. if (prefix) {
  485. const char *ptype_name = "__SUBCOMMAND";
  486. clish_param_t *opt_param = NULL;
  487. /* Create a ptype for prefix-named subcommand that
  488. * will contain the nested optional parameter. The
  489. * name of ptype is hardcoded. It's not good but
  490. * it's only the service ptype.
  491. */
  492. tmp = (clish_ptype_t *)lub_bintree_find(
  493. &shell->ptype_tree, ptype_name);
  494. if (!tmp)
  495. tmp = clish_shell_find_create_ptype(shell,
  496. ptype_name, "Option", "[^\\\\]+",
  497. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  498. assert(tmp);
  499. opt_param = clish_param_new(prefix, help, tmp);
  500. clish_param__set_mode(opt_param,
  501. CLISH_PARAM_SUBCOMMAND);
  502. clish_param__set_optional(opt_param, BOOL_TRUE);
  503. if (test)
  504. clish_param__set_test(opt_param, test);
  505. /* add the parameter to the command */
  506. if (cmd)
  507. clish_command_insert_param(cmd, opt_param);
  508. /* add the parameter to the param */
  509. if (p_param)
  510. clish_param_insert_param(p_param, opt_param);
  511. /* Unset cmd and set parent param to opt_param */
  512. cmd = NULL;
  513. p_param = opt_param;
  514. }
  515. if (defval)
  516. clish_param__set_default(param, defval);
  517. if (hidden && lub_string_nocasecmp(hidden, "true") == 0)
  518. clish_param__set_hidden(param, BOOL_TRUE);
  519. else
  520. clish_param__set_hidden(param, BOOL_FALSE);
  521. if (mode) {
  522. if (lub_string_nocasecmp(mode, "switch") == 0) {
  523. clish_param__set_mode(param,
  524. CLISH_PARAM_SWITCH);
  525. /* Force hidden attribute */
  526. clish_param__set_hidden(param, BOOL_TRUE);
  527. } else if (lub_string_nocasecmp(mode, "subcommand") == 0)
  528. clish_param__set_mode(param,
  529. CLISH_PARAM_SUBCOMMAND);
  530. else
  531. clish_param__set_mode(param,
  532. CLISH_PARAM_COMMON);
  533. }
  534. if (optional && lub_string_nocasecmp(optional, "true") == 0)
  535. clish_param__set_optional(param, BOOL_TRUE);
  536. else
  537. clish_param__set_optional(param, BOOL_FALSE);
  538. if (order && lub_string_nocasecmp(order, "true") == 0)
  539. clish_param__set_order(param, BOOL_TRUE);
  540. else
  541. clish_param__set_order(param, BOOL_FALSE);
  542. if (value) {
  543. clish_param__set_value(param, value);
  544. /* Force mode to subcommand */
  545. clish_param__set_mode(param,
  546. CLISH_PARAM_SUBCOMMAND);
  547. }
  548. if (test && !prefix)
  549. clish_param__set_test(param, test);
  550. if (completion)
  551. clish_param__set_completion(param, completion);
  552. /* add the parameter to the command */
  553. if (cmd)
  554. clish_command_insert_param(cmd, param);
  555. /* add the parameter to the param */
  556. if (p_param)
  557. clish_param_insert_param(p_param, param);
  558. clish_xml_release(name);
  559. clish_xml_release(help);
  560. clish_xml_release(ptype);
  561. clish_xml_release(prefix);
  562. clish_xml_release(defval);
  563. clish_xml_release(mode);
  564. clish_xml_release(optional);
  565. clish_xml_release(order);
  566. clish_xml_release(value);
  567. clish_xml_release(hidden);
  568. clish_xml_release(test);
  569. clish_xml_release(completion);
  570. process_children(shell, element, param);
  571. }
  572. }
  573. /* ------------------------------------------------------ */
  574. static void
  575. process_action(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  576. {
  577. clish_action_t *action = NULL;
  578. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  579. char *shebang = clish_xmlnode_fetch_attr(element, "shebang");
  580. clish_xmlnode_t *pelement = clish_xmlnode_parent(element);
  581. char *pname = clish_xmlnode_get_all_name(pelement);
  582. char *text;
  583. if (pname && lub_string_nocasecmp(pname, "VAR") == 0)
  584. action = clish_var__get_action((clish_var_t *)parent);
  585. else
  586. action = clish_command__get_action((clish_command_t *)parent);
  587. assert(action);
  588. if (pname)
  589. free(pname);
  590. text = clish_xmlnode_get_all_content(element);
  591. if (text && *text) {
  592. /* store the action */
  593. clish_action__set_script(action, text);
  594. }
  595. if (text)
  596. free(text);
  597. if (builtin)
  598. clish_action__set_builtin(action, builtin);
  599. if (shebang)
  600. clish_action__set_shebang(action, shebang);
  601. clish_xml_release(builtin);
  602. clish_xml_release(shebang);
  603. }
  604. /* ------------------------------------------------------ */
  605. static void
  606. process_detail(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  607. {
  608. clish_command_t *cmd = (clish_command_t *) parent;
  609. /* read the following text element */
  610. char *text = clish_xmlnode_get_all_content(element);
  611. if (text && *text) {
  612. /* store the action */
  613. clish_command__set_detail(cmd, text);
  614. }
  615. if (text)
  616. free(text);
  617. }
  618. /* ------------------------------------------------------ */
  619. static void
  620. process_namespace(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  621. {
  622. clish_view_t *v = (clish_view_t *) parent;
  623. clish_nspace_t *nspace = NULL;
  624. char *view = clish_xmlnode_fetch_attr(element, "ref");
  625. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  626. char *prefix_help = clish_xmlnode_fetch_attr(element, "prefix_help");
  627. char *help = clish_xmlnode_fetch_attr(element, "help");
  628. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  629. char *context_help = clish_xmlnode_fetch_attr(element, "context_help");
  630. char *inherit = clish_xmlnode_fetch_attr(element, "inherit");
  631. char *access = clish_xmlnode_fetch_attr(element, "access");
  632. int allowed = 1;
  633. if (access) {
  634. allowed = 0;
  635. if (shell->client_hooks->access_fn)
  636. allowed = shell->client_hooks->access_fn(shell, access);
  637. }
  638. if (!allowed)
  639. goto process_namespace_end;
  640. assert(view);
  641. clish_view_t *ref_view = clish_shell_find_create_view(shell,
  642. view, NULL);
  643. assert(ref_view);
  644. /* Don't include itself without prefix */
  645. if ((ref_view == v) && !prefix)
  646. goto process_namespace_end;
  647. nspace = clish_nspace_new(ref_view);
  648. assert(nspace);
  649. clish_view_insert_nspace(v, nspace);
  650. if (prefix) {
  651. clish_nspace__set_prefix(nspace, prefix);
  652. if (prefix_help)
  653. clish_nspace_create_prefix_cmd(nspace,
  654. "prefix",
  655. prefix_help);
  656. else
  657. clish_nspace_create_prefix_cmd(nspace,
  658. "prefix",
  659. "Prefix for the imported commands.");
  660. }
  661. if (help && lub_string_nocasecmp(help, "true") == 0)
  662. clish_nspace__set_help(nspace, BOOL_TRUE);
  663. else
  664. clish_nspace__set_help(nspace, BOOL_FALSE);
  665. if (completion && lub_string_nocasecmp(completion, "false") == 0)
  666. clish_nspace__set_completion(nspace, BOOL_FALSE);
  667. else
  668. clish_nspace__set_completion(nspace, BOOL_TRUE);
  669. if (context_help && lub_string_nocasecmp(context_help, "true") == 0)
  670. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  671. else
  672. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  673. if (inherit && lub_string_nocasecmp(inherit, "false") == 0)
  674. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  675. else
  676. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  677. process_namespace_end:
  678. clish_xml_release(view);
  679. clish_xml_release(prefix);
  680. clish_xml_release(prefix_help);
  681. clish_xml_release(help);
  682. clish_xml_release(completion);
  683. clish_xml_release(context_help);
  684. clish_xml_release(inherit);
  685. clish_xml_release(access);
  686. }
  687. /* ------------------------------------------------------ */
  688. static void
  689. process_config(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  690. {
  691. clish_command_t *cmd = (clish_command_t *)parent;
  692. clish_config_t *config;
  693. if (!cmd)
  694. return;
  695. config = clish_command__get_config(cmd);
  696. /* read the following text element */
  697. char *operation = clish_xmlnode_fetch_attr(element, "operation");
  698. char *priority = clish_xmlnode_fetch_attr(element, "priority");
  699. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  700. char *file = clish_xmlnode_fetch_attr(element, "file");
  701. char *splitter = clish_xmlnode_fetch_attr(element, "splitter");
  702. char *seq = clish_xmlnode_fetch_attr(element, "sequence");
  703. char *unique = clish_xmlnode_fetch_attr(element, "unique");
  704. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  705. if (operation && !lub_string_nocasecmp(operation, "unset"))
  706. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  707. else if (operation && !lub_string_nocasecmp(operation, "none"))
  708. clish_config__set_op(config, CLISH_CONFIG_NONE);
  709. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  710. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  711. else {
  712. clish_config__set_op(config, CLISH_CONFIG_SET);
  713. /* The priority if no clearly specified */
  714. clish_config__set_priority(config, 0x7f00);
  715. }
  716. if (priority) {
  717. long val = 0;
  718. char *endptr;
  719. unsigned short pri;
  720. val = strtol(priority, &endptr, 0);
  721. if (endptr == priority)
  722. pri = 0;
  723. else if (val > 0xffff)
  724. pri = 0xffff;
  725. else if (val < 0)
  726. pri = 0;
  727. else
  728. pri = (unsigned short)val;
  729. clish_config__set_priority(config, pri);
  730. }
  731. if (pattern)
  732. clish_config__set_pattern(config, pattern);
  733. else
  734. clish_config__set_pattern(config, "^${__cmd}");
  735. if (file)
  736. clish_config__set_file(config, file);
  737. if (splitter && lub_string_nocasecmp(splitter, "false") == 0)
  738. clish_config__set_splitter(config, BOOL_FALSE);
  739. else
  740. clish_config__set_splitter(config, BOOL_TRUE);
  741. if (unique && lub_string_nocasecmp(unique, "false") == 0)
  742. clish_config__set_unique(config, BOOL_FALSE);
  743. else
  744. clish_config__set_unique(config, BOOL_TRUE);
  745. if (seq)
  746. clish_config__set_seq(config, seq);
  747. else
  748. /* The entries without sequence cannot be non-unique */
  749. clish_config__set_unique(config, BOOL_TRUE);
  750. if (depth)
  751. clish_config__set_depth(config, depth);
  752. clish_xml_release(operation);
  753. clish_xml_release(priority);
  754. clish_xml_release(pattern);
  755. clish_xml_release(file);
  756. clish_xml_release(splitter);
  757. clish_xml_release(seq);
  758. clish_xml_release(unique);
  759. clish_xml_release(depth);
  760. }
  761. /* ------------------------------------------------------ */
  762. static void process_var(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  763. {
  764. clish_var_t *var = NULL;
  765. char *name = clish_xmlnode_fetch_attr(element, "name");
  766. char *dynamic = clish_xmlnode_fetch_attr(element, "dynamic");
  767. char *value = clish_xmlnode_fetch_attr(element, "value");
  768. assert(name);
  769. /* Check if this var doesn't already exist */
  770. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  771. if (var) {
  772. printf("DUPLICATE VAR: %s\n", name);
  773. assert(!var);
  774. }
  775. /* Create var instance */
  776. var = clish_var_new(name);
  777. lub_bintree_insert(&shell->var_tree, var);
  778. if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
  779. clish_var__set_dynamic(var, BOOL_TRUE);
  780. if (value)
  781. clish_var__set_value(var, value);
  782. clish_xml_release(name);
  783. clish_xml_release(dynamic);
  784. clish_xml_release(value);
  785. process_children(shell, element, var);
  786. }
  787. /* ------------------------------------------------------ */
  788. static void process_wdog(clish_shell_t *shell,
  789. clish_xmlnode_t *element, void *parent)
  790. {
  791. clish_view_t *v = (clish_view_t *)parent;
  792. clish_command_t *cmd = NULL;
  793. assert(!shell->wdog);
  794. /* create a command with NULL help */
  795. cmd = clish_view_new_command(v, "watchdog", NULL);
  796. clish_command__set_lock(cmd, BOOL_FALSE);
  797. /* Remember this command */
  798. shell->wdog = cmd;
  799. process_children(shell, element, cmd);
  800. }
  801. /* ------------------------------------------------------ */
  802. int clish_shell_xml_read(clish_shell_t * shell, const char *filename)
  803. {
  804. int ret = -1;
  805. clish_xmldoc_t *doc;
  806. doc = clish_xmldoc_read(filename);
  807. if (clish_xmldoc_is_valid(doc)) {
  808. clish_xmlnode_t *root = clish_xmldoc_get_root(doc);
  809. process_node(shell, root, NULL);
  810. ret = 0;
  811. } else {
  812. int errcaps = clish_xmldoc_error_caps(doc);
  813. printf("Unable to open file '%s'", filename);
  814. if ((errcaps & CLISH_XMLERR_LINE) == CLISH_XMLERR_LINE)
  815. printf(", at line %d", clish_xmldoc_get_err_line(doc));
  816. if ((errcaps & CLISH_XMLERR_COL) == CLISH_XMLERR_COL)
  817. printf(", at column %d", clish_xmldoc_get_err_col(doc));
  818. if ((errcaps & CLISH_XMLERR_DESC) == CLISH_XMLERR_DESC)
  819. printf(", message is %s", clish_xmldoc_get_err_msg(doc));
  820. printf("\n");
  821. }
  822. clish_xmldoc_release(doc);
  823. return ret;
  824. }
  825. /* ------------------------------------------------------ */