1
0

shell_xml.c 33 KB

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