shell_xml.c 33 KB

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