shell_xml.c 28 KB

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