shell_xml.c 31 KB

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