shell_xml.c 28 KB

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