1
0

shell_xml.c 34 KB

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