shell_xml.c 29 KB

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