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