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