shell_xml.c 33 KB

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