shell_xml.c 29 KB

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