1
0

shell_xml.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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. /* Create a ptype for prefix-named subcommand that
  650. * will contain the nested optional parameter. The
  651. * name of ptype is hardcoded. It's not good but
  652. * it's only the service ptype.
  653. */
  654. tmp = (clish_ptype_t *)lub_bintree_find(
  655. &shell->ptype_tree, ptype_name);
  656. if (!tmp)
  657. tmp = clish_shell_find_create_ptype(shell,
  658. ptype_name, "Option", "[^\\\\]+",
  659. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  660. opt_param = clish_param_new(prefix, help, tmp);
  661. clish_param__set_mode(opt_param,
  662. CLISH_PARAM_SUBCOMMAND);
  663. clish_param__set_optional(opt_param, BOOL_TRUE);
  664. if (test)
  665. clish_param__set_test(opt_param, test);
  666. /* add the parameter to the command */
  667. if (cmd)
  668. clish_command_insert_param(cmd, opt_param);
  669. /* add the parameter to the param */
  670. if (p_param)
  671. clish_param_insert_param(p_param, opt_param);
  672. /* Unset cmd and set parent param to opt_param */
  673. cmd = NULL;
  674. p_param = opt_param;
  675. }
  676. if (defval)
  677. clish_param__set_default(param, defval);
  678. if (hidden && lub_string_nocasecmp(hidden, "true") == 0)
  679. clish_param__set_hidden(param, BOOL_TRUE);
  680. else
  681. clish_param__set_hidden(param, BOOL_FALSE);
  682. if (mode) {
  683. if (lub_string_nocasecmp(mode, "switch") == 0) {
  684. clish_param__set_mode(param,
  685. CLISH_PARAM_SWITCH);
  686. /* Force hidden attribute */
  687. clish_param__set_hidden(param, BOOL_TRUE);
  688. } else if (lub_string_nocasecmp(mode, "subcommand") == 0)
  689. clish_param__set_mode(param,
  690. CLISH_PARAM_SUBCOMMAND);
  691. else
  692. clish_param__set_mode(param,
  693. CLISH_PARAM_COMMON);
  694. }
  695. if (optional && lub_string_nocasecmp(optional, "true") == 0)
  696. clish_param__set_optional(param, BOOL_TRUE);
  697. else
  698. clish_param__set_optional(param, BOOL_FALSE);
  699. if (order && lub_string_nocasecmp(order, "true") == 0)
  700. clish_param__set_order(param, BOOL_TRUE);
  701. else
  702. clish_param__set_order(param, BOOL_FALSE);
  703. if (value) {
  704. clish_param__set_value(param, value);
  705. /* Force mode to subcommand */
  706. clish_param__set_mode(param,
  707. CLISH_PARAM_SUBCOMMAND);
  708. }
  709. if (test && !prefix)
  710. clish_param__set_test(param, test);
  711. if (completion)
  712. clish_param__set_completion(param, completion);
  713. /* add the parameter to the command */
  714. if (cmd)
  715. clish_command_insert_param(cmd, param);
  716. /* add the parameter to the param */
  717. if (p_param)
  718. clish_param_insert_param(p_param, param);
  719. res = process_children(shell, element, param);
  720. error:
  721. clish_xml_release(name);
  722. clish_xml_release(help);
  723. clish_xml_release(ptype);
  724. clish_xml_release(prefix);
  725. clish_xml_release(defval);
  726. clish_xml_release(mode);
  727. clish_xml_release(optional);
  728. clish_xml_release(order);
  729. clish_xml_release(value);
  730. clish_xml_release(hidden);
  731. clish_xml_release(test);
  732. clish_xml_release(completion);
  733. }
  734. return res;
  735. }
  736. /* ------------------------------------------------------ */
  737. static int
  738. process_action(clish_shell_t *shell, clish_xmlnode_t *element, void *parent)
  739. {
  740. clish_action_t *action = NULL;
  741. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  742. char *shebang = clish_xmlnode_fetch_attr(element, "shebang");
  743. clish_xmlnode_t *pelement = clish_xmlnode_parent(element);
  744. char *pname = clish_xmlnode_get_all_name(pelement);
  745. char *text;
  746. clish_sym_t *sym = NULL;
  747. if (pname && lub_string_nocasecmp(pname, "VAR") == 0)
  748. action = clish_var__get_action((clish_var_t *)parent);
  749. else
  750. action = clish_command__get_action((clish_command_t *)parent);
  751. if (pname)
  752. free(pname);
  753. text = clish_xmlnode_get_all_content(element);
  754. if (text && *text) {
  755. /* store the action */
  756. clish_action__set_script(action, text);
  757. }
  758. if (text)
  759. free(text);
  760. if (builtin)
  761. sym = clish_shell_add_unresolved_sym(shell, builtin,
  762. CLISH_SYM_TYPE_ACTION);
  763. else
  764. sym = shell->hooks[CLISH_SYM_TYPE_ACTION];
  765. clish_action__set_builtin(action, sym);
  766. if (shebang)
  767. clish_action__set_shebang(action, shebang);
  768. clish_xml_release(builtin);
  769. clish_xml_release(shebang);
  770. return 0;
  771. }
  772. /* ------------------------------------------------------ */
  773. static int
  774. process_detail(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  775. {
  776. clish_command_t *cmd = (clish_command_t *) parent;
  777. /* read the following text element */
  778. char *text = clish_xmlnode_get_all_content(element);
  779. if (text && *text) {
  780. /* store the action */
  781. clish_command__set_detail(cmd, text);
  782. }
  783. if (text)
  784. free(text);
  785. return 0;
  786. }
  787. /* ------------------------------------------------------ */
  788. static int
  789. process_namespace(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  790. {
  791. clish_view_t *v = (clish_view_t *) parent;
  792. clish_nspace_t *nspace = NULL;
  793. int res = -1;
  794. char *view = clish_xmlnode_fetch_attr(element, "ref");
  795. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  796. char *prefix_help = clish_xmlnode_fetch_attr(element, "prefix_help");
  797. char *help = clish_xmlnode_fetch_attr(element, "help");
  798. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  799. char *context_help = clish_xmlnode_fetch_attr(element, "context_help");
  800. char *inherit = clish_xmlnode_fetch_attr(element, "inherit");
  801. char *access = clish_xmlnode_fetch_attr(element, "access");
  802. // int allowed = 1;
  803. // if (access) {
  804. // allowed = 0;
  805. // if (shell->hooks->access_fn)
  806. // allowed = shell->hooks->access_fn(shell, access);
  807. // }
  808. // if (!allowed)
  809. // goto process_namespace_end;
  810. /* Check syntax */
  811. if (!view) {
  812. fprintf(stderr, CLISH_XML_ERROR_ATTR("ref"));
  813. goto error;
  814. }
  815. clish_view_t *ref_view = clish_shell_find_create_view(shell,
  816. view, NULL);
  817. /* Don't include itself without prefix */
  818. if ((ref_view == v) && !prefix)
  819. goto process_namespace_end;
  820. nspace = clish_nspace_new(ref_view);
  821. clish_view_insert_nspace(v, nspace);
  822. if (prefix) {
  823. clish_nspace__set_prefix(nspace, prefix);
  824. if (prefix_help)
  825. clish_nspace_create_prefix_cmd(nspace,
  826. "prefix",
  827. prefix_help);
  828. else
  829. clish_nspace_create_prefix_cmd(nspace,
  830. "prefix",
  831. "Prefix for the imported commands.");
  832. }
  833. if (help && lub_string_nocasecmp(help, "true") == 0)
  834. clish_nspace__set_help(nspace, BOOL_TRUE);
  835. else
  836. clish_nspace__set_help(nspace, BOOL_FALSE);
  837. if (completion && lub_string_nocasecmp(completion, "false") == 0)
  838. clish_nspace__set_completion(nspace, BOOL_FALSE);
  839. else
  840. clish_nspace__set_completion(nspace, BOOL_TRUE);
  841. if (context_help && lub_string_nocasecmp(context_help, "true") == 0)
  842. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  843. else
  844. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  845. if (inherit && lub_string_nocasecmp(inherit, "false") == 0)
  846. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  847. else
  848. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  849. process_namespace_end:
  850. res = 0;
  851. error:
  852. clish_xml_release(view);
  853. clish_xml_release(prefix);
  854. clish_xml_release(prefix_help);
  855. clish_xml_release(help);
  856. clish_xml_release(completion);
  857. clish_xml_release(context_help);
  858. clish_xml_release(inherit);
  859. clish_xml_release(access);
  860. return res;
  861. }
  862. /* ------------------------------------------------------ */
  863. static int
  864. process_config(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  865. {
  866. clish_command_t *cmd = (clish_command_t *)parent;
  867. clish_config_t *config;
  868. if (!cmd)
  869. return 0;
  870. config = clish_command__get_config(cmd);
  871. /* read the following text element */
  872. char *operation = clish_xmlnode_fetch_attr(element, "operation");
  873. char *priority = clish_xmlnode_fetch_attr(element, "priority");
  874. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  875. char *file = clish_xmlnode_fetch_attr(element, "file");
  876. char *splitter = clish_xmlnode_fetch_attr(element, "splitter");
  877. char *seq = clish_xmlnode_fetch_attr(element, "sequence");
  878. char *unique = clish_xmlnode_fetch_attr(element, "unique");
  879. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  880. if (operation && !lub_string_nocasecmp(operation, "unset"))
  881. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  882. else if (operation && !lub_string_nocasecmp(operation, "none"))
  883. clish_config__set_op(config, CLISH_CONFIG_NONE);
  884. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  885. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  886. else {
  887. clish_config__set_op(config, CLISH_CONFIG_SET);
  888. /* The priority if no clearly specified */
  889. clish_config__set_priority(config, 0x7f00);
  890. }
  891. if (priority) {
  892. long val = 0;
  893. char *endptr;
  894. unsigned short pri;
  895. val = strtol(priority, &endptr, 0);
  896. if (endptr == priority)
  897. pri = 0;
  898. else if (val > 0xffff)
  899. pri = 0xffff;
  900. else if (val < 0)
  901. pri = 0;
  902. else
  903. pri = (unsigned short)val;
  904. clish_config__set_priority(config, pri);
  905. }
  906. if (pattern)
  907. clish_config__set_pattern(config, pattern);
  908. else
  909. clish_config__set_pattern(config, "^${__cmd}");
  910. if (file)
  911. clish_config__set_file(config, file);
  912. if (splitter && lub_string_nocasecmp(splitter, "false") == 0)
  913. clish_config__set_splitter(config, BOOL_FALSE);
  914. else
  915. clish_config__set_splitter(config, BOOL_TRUE);
  916. if (unique && lub_string_nocasecmp(unique, "false") == 0)
  917. clish_config__set_unique(config, BOOL_FALSE);
  918. else
  919. clish_config__set_unique(config, BOOL_TRUE);
  920. if (seq)
  921. clish_config__set_seq(config, seq);
  922. else
  923. /* The entries without sequence cannot be non-unique */
  924. clish_config__set_unique(config, BOOL_TRUE);
  925. if (depth)
  926. clish_config__set_depth(config, depth);
  927. clish_xml_release(operation);
  928. clish_xml_release(priority);
  929. clish_xml_release(pattern);
  930. clish_xml_release(file);
  931. clish_xml_release(splitter);
  932. clish_xml_release(seq);
  933. clish_xml_release(unique);
  934. clish_xml_release(depth);
  935. return 0;
  936. }
  937. /* ------------------------------------------------------ */
  938. static int
  939. process_var(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  940. {
  941. clish_var_t *var = NULL;
  942. int res = -1;
  943. char *name = clish_xmlnode_fetch_attr(element, "name");
  944. char *dynamic = clish_xmlnode_fetch_attr(element, "dynamic");
  945. char *value = clish_xmlnode_fetch_attr(element, "value");
  946. /* Check syntax */
  947. if (!name) {
  948. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  949. goto error;
  950. }
  951. /* Check if this var doesn't already exist */
  952. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  953. if (var) {
  954. fprintf(stderr, CLISH_XML_ERROR_STR"Duplicate VAR name=\"%s\".\n", name);
  955. goto error;
  956. }
  957. /* Create var instance */
  958. var = clish_var_new(name);
  959. lub_bintree_insert(&shell->var_tree, var);
  960. if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
  961. clish_var__set_dynamic(var, BOOL_TRUE);
  962. if (value)
  963. clish_var__set_value(var, value);
  964. res = process_children(shell, element, var);
  965. error:
  966. clish_xml_release(name);
  967. clish_xml_release(dynamic);
  968. clish_xml_release(value);
  969. return res;
  970. }
  971. /* ------------------------------------------------------ */
  972. static int
  973. process_wdog(clish_shell_t *shell,
  974. clish_xmlnode_t *element, void *parent)
  975. {
  976. clish_view_t *v = (clish_view_t *)parent;
  977. clish_command_t *cmd = NULL;
  978. int res = -1;
  979. /* Check syntax */
  980. if (shell->wdog) {
  981. fprintf(stderr, CLISH_XML_ERROR_STR"WATCHDOG tag duplication.\n");
  982. goto error;
  983. }
  984. /* Create a command with NULL help */
  985. cmd = clish_view_new_command(v, "watchdog", NULL);
  986. clish_command__set_lock(cmd, BOOL_FALSE);
  987. /* Remember this command */
  988. shell->wdog = cmd;
  989. res = process_children(shell, element, cmd);
  990. error:
  991. return res;
  992. }
  993. /* ------------------------------------------------------ */
  994. static int
  995. process_hotkey(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
  996. {
  997. clish_view_t *v = (clish_view_t *)parent;
  998. int res = -1;
  999. char *key = clish_xmlnode_fetch_attr(element, "key");
  1000. char *cmd = clish_xmlnode_fetch_attr(element, "cmd");
  1001. /* Check syntax */
  1002. if (!key) {
  1003. fprintf(stderr, CLISH_XML_ERROR_ATTR("key"));
  1004. goto error;
  1005. }
  1006. if (!cmd) {
  1007. fprintf(stderr, CLISH_XML_ERROR_ATTR("cmd"));
  1008. goto error;
  1009. }
  1010. clish_view_insert_hotkey(v, key, cmd);
  1011. res = 0;
  1012. error:
  1013. clish_xml_release(key);
  1014. clish_xml_release(cmd);
  1015. return res;
  1016. }
  1017. /* ------------------------------------------------------ */
  1018. static int
  1019. process_plugin(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
  1020. {
  1021. clish_plugin_t *plugin;
  1022. char *file = clish_xmlnode_fetch_attr(element, "file");
  1023. char *name = clish_xmlnode_fetch_attr(element, "name");
  1024. int res = -1;
  1025. char *text;
  1026. /* Check syntax */
  1027. if (!file) {
  1028. fprintf(stderr, CLISH_XML_ERROR_ATTR("file"));
  1029. goto error;
  1030. }
  1031. plugin = clish_plugin_new(file, name); /* Really - the name is alias */
  1032. lub_list_add(shell->plugins, plugin);
  1033. /* Get PLUGIN body content */
  1034. text = clish_xmlnode_get_all_content(element);
  1035. if (text && *text)
  1036. clish_plugin__set_conf(plugin, text);
  1037. if (text)
  1038. free(text);
  1039. res = 0;
  1040. error:
  1041. clish_xml_release(file);
  1042. clish_xml_release(name);
  1043. return res;
  1044. }
  1045. /* ------------------------------------------------------ */
  1046. static int
  1047. process_hook(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
  1048. {
  1049. char *name = clish_xmlnode_fetch_attr(element, "name");
  1050. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  1051. int res = -1;
  1052. int type = CLISH_SYM_TYPE_NONE;
  1053. /* Check syntax */
  1054. if (!name) {
  1055. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  1056. goto error;
  1057. }
  1058. /* Find out HOOK type */
  1059. if (!strcmp(name, "action"))
  1060. type = CLISH_SYM_TYPE_ACTION;
  1061. else if (!strcmp(name, "access"))
  1062. type = CLISH_SYM_TYPE_ACCESS;
  1063. else if (!strcmp(name, "config"))
  1064. type = CLISH_SYM_TYPE_CONFIG;
  1065. else if (!strcmp(name, "log"))
  1066. type = CLISH_SYM_TYPE_LOG;
  1067. if (CLISH_SYM_TYPE_NONE == type) {
  1068. fprintf(stderr, CLISH_XML_ERROR_STR"Unknown HOOK name %s.\n", name);
  1069. goto error;
  1070. }
  1071. /* Check duplicate HOOK tag */
  1072. if (shell->hooks_use[type]) {
  1073. fprintf(stderr,
  1074. CLISH_XML_ERROR_STR"HOOK %s duplication.\n", name);
  1075. goto error;
  1076. }
  1077. shell->hooks_use[type] = BOOL_TRUE;
  1078. clish_sym__set_name(shell->hooks[type], builtin);
  1079. res = 0;
  1080. error:
  1081. clish_xml_release(name);
  1082. clish_xml_release(builtin);
  1083. return res;
  1084. }
  1085. /* ------------------------------------------------------ */