shell_xml.c 27 KB

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