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