load.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. /** @file load.c
  2. * @brief Common part for XML parsing.
  3. *
  4. * Different XML parsing engines can provide a functions in a form of
  5. * standardized API. This code uses this API and parses XML to kscheme.
  6. */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <errno.h>
  11. #include <sys/types.h>
  12. #include <dirent.h>
  13. #include <faux/faux.h>
  14. #include <faux/str.h>
  15. #include <faux/error.h>
  16. #include <klish/kscheme.h>
  17. #include <klish/ischeme.h>
  18. #include <klish/kxml.h>
  19. #define TAG "XML"
  20. typedef bool_t (kxml_process_fn)(const kxml_node_t *element,
  21. void *parent, faux_error_t *error);
  22. static kxml_process_fn
  23. process_action,
  24. process_param,
  25. process_command,
  26. process_view,
  27. process_ptype,
  28. process_plugin,
  29. process_klish;
  30. /* process_startup,
  31. process_overview,
  32. process_detail,
  33. process_namespace,
  34. process_config,
  35. process_var,
  36. process_wdog,
  37. process_hotkey,
  38. process_hook;
  39. */
  40. // Different TAGs types
  41. typedef enum {
  42. KTAG_NONE,
  43. KTAG_ACTION,
  44. KTAG_PARAM,
  45. KTAG_COMMAND,
  46. KTAG_VIEW,
  47. KTAG_PTYPE,
  48. KTAG_PLUGIN,
  49. KTAG_KLISH,
  50. KTAG_MAX
  51. } ktags_e;
  52. static const char *kxml_tags[] = {
  53. NULL,
  54. "ACTION",
  55. "PARAM",
  56. "COMMAND",
  57. "VIEW",
  58. "PTYPE",
  59. "PLUGIN",
  60. "KLISH"
  61. };
  62. static kxml_process_fn *kxml_handlers[] = {
  63. NULL,
  64. process_action,
  65. process_param,
  66. process_command,
  67. process_view,
  68. process_ptype,
  69. process_plugin,
  70. process_klish
  71. };
  72. static ktags_e kxml_node_tag(const kxml_node_t *node)
  73. {
  74. ktags_e tag = KTAG_NONE;
  75. char *name = NULL;
  76. if (!node)
  77. return KTAG_NONE;
  78. if (kxml_node_type(node) != KXML_NODE_ELM)
  79. return KTAG_NONE;
  80. name = kxml_node_name(node);
  81. if (!name)
  82. return KTAG_NONE; // Strange case
  83. for (tag = KTAG_NONE; tag < KTAG_MAX; tag++) {
  84. if (faux_str_casecmp(name, kxml_tags[tag]))
  85. break;
  86. }
  87. kxml_node_name_free(name);
  88. if (tag >= KTAG_MAX)
  89. return KTAG_NONE;
  90. return tag;
  91. }
  92. static kxml_process_fn *kxml_node_handler(const kxml_node_t *node)
  93. {
  94. return kxml_handlers[kxml_node_tag(node)];
  95. }
  96. /** @brief Reads an element from the XML stream and processes it.
  97. */
  98. static bool_t process_node(const kxml_node_t *node, void *parent, faux_error_t *error)
  99. {
  100. kxml_process_fn *handler = kxml_node_handler(node);
  101. if (!handler)
  102. return BOOL_TRUE; // Unknown element
  103. return handler(node, parent, error);
  104. }
  105. static bool_t kxml_load_file(kscheme_t *scheme, const char *filename,
  106. faux_error_t *error)
  107. {
  108. kxml_doc_t *doc = NULL;
  109. kxml_node_t *root = NULL;
  110. bool_t r = BOOL_FALSE;
  111. if (!scheme)
  112. return BOOL_FALSE;
  113. if (!filename)
  114. return BOOL_FALSE;
  115. doc = kxml_doc_read(filename);
  116. if (!kxml_doc_is_valid(doc)) {
  117. /* int errcaps = kxml_doc_error_caps(doc);
  118. printf("Unable to open file '%s'", filename);
  119. if ((errcaps & kxml_ERR_LINE) == kxml_ERR_LINE)
  120. printf(", at line %d", kxml_doc_err_line(doc));
  121. if ((errcaps & kxml_ERR_COL) == kxml_ERR_COL)
  122. printf(", at column %d", kxml_doc_err_col(doc));
  123. if ((errcaps & kxml_ERR_DESC) == kxml_ERR_DESC)
  124. printf(", message is %s", kxml_doc_err_msg(doc));
  125. printf("\n");
  126. */ kxml_doc_release(doc);
  127. return BOOL_FALSE;
  128. }
  129. root = kxml_doc_root(doc);
  130. r = process_node(root, scheme, error);
  131. kxml_doc_release(doc);
  132. if (!r) {
  133. faux_error_sprintf(error, TAG": Illegal file %s", filename);
  134. return BOOL_FALSE;
  135. }
  136. return BOOL_TRUE;
  137. }
  138. /** @brief Default path to get XML files from.
  139. */
  140. const char *default_path = "/etc/klish;~/.klish";
  141. static const char *path_separators = ":;";
  142. kscheme_t *kxml_load_scheme(const char *xml_path, faux_error_t *error)
  143. {
  144. kscheme_t *scheme = NULL;
  145. const char *path = xml_path;
  146. char *realpath = NULL;
  147. char *fn = NULL;
  148. char *saveptr = NULL;
  149. bool_t ret = BOOL_TRUE;
  150. // New kscheme instance
  151. scheme = kscheme_new();
  152. if (!scheme)
  153. return NULL;
  154. // Use the default path if not specified
  155. if (!path)
  156. path = default_path;
  157. realpath = faux_expand_tilde(path);
  158. // Loop through each directory
  159. for (fn = strtok_r(realpath, path_separators, &saveptr);
  160. fn; fn = strtok_r(NULL, path_separators, &saveptr)) {
  161. DIR *dir = NULL;
  162. struct dirent *entry = NULL;
  163. // Regular file
  164. if (faux_isfile(fn)) {
  165. if (!kxml_load_file(scheme, fn, error)) {
  166. ret = BOOL_FALSE;
  167. continue;
  168. }
  169. }
  170. // Search this directory for any XML files
  171. dir = opendir(fn);
  172. if (!dir)
  173. continue;
  174. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  175. const char *extension = strrchr(entry->d_name, '.');
  176. char *filename = NULL;
  177. // Check the filename
  178. if (!extension || strcmp(".xml", extension))
  179. continue;
  180. filename = faux_str_sprintf("%s/%s", fn, entry->d_name);
  181. if (!kxml_load_file(scheme, filename, error)) {
  182. ret = BOOL_FALSE;
  183. continue;
  184. }
  185. faux_str_free(filename);
  186. }
  187. closedir(dir);
  188. }
  189. faux_str_free(realpath);
  190. if (!ret) { // Some errors while XML parsing
  191. kscheme_free(scheme);
  192. return NULL;
  193. }
  194. return scheme;
  195. }
  196. /** @brief Iterate through element's children.
  197. */
  198. static bool_t process_children(const kxml_node_t *element, void *parent,
  199. faux_error_t *error)
  200. {
  201. const kxml_node_t *node = NULL;
  202. while ((node = kxml_node_next_child(element, node)) != NULL) {
  203. bool_t res = BOOL_FALSE;
  204. res = process_node(node, parent, error);
  205. if (!res)
  206. return res;
  207. }
  208. return BOOL_TRUE;
  209. }
  210. static bool_t process_klish(const kxml_node_t *element, void *parent,
  211. faux_error_t *error)
  212. {
  213. return process_children(element, parent, error);
  214. }
  215. static bool_t process_view(const kxml_node_t *element, void *parent,
  216. faux_error_t *error)
  217. {
  218. iview_t iview = {};
  219. kview_t *view = NULL;
  220. bool_t res = BOOL_FALSE;
  221. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  222. if (parent_tag != KTAG_KLISH) {
  223. faux_error_add(error, TAG": Only KLISH tag can contain VIEW tag");
  224. return BOOL_FALSE;
  225. }
  226. iview.name = kxml_node_attr(element, "name");
  227. view = iview_load(&iview, error);
  228. if (!view)
  229. goto err;
  230. if (!kscheme_add_view((kscheme_t *)parent, view)) {
  231. faux_error_sprintf(error, TAG": Can't add VIEW \"%s\"",
  232. kview_name(view));
  233. kview_free(view);
  234. goto err;
  235. }
  236. if (!process_children(element, view, error))
  237. goto err;
  238. res = BOOL_TRUE;
  239. err:
  240. kxml_node_attr_free(iview.name);
  241. return res;
  242. }
  243. /*
  244. static int process_ptype(kxml_node_t *element,
  245. void *parent)
  246. {
  247. clish_ptype_method_e method;
  248. clish_ptype_preprocess_e preprocess;
  249. int res = -1;
  250. clish_ptype_t *ptype;
  251. char *name = kxml_node_attr(element, "name");
  252. char *help = kxml_node_attr(element, "help");
  253. char *pattern = kxml_node_attr(element, "pattern");
  254. char *method_name = kxml_node_attr(element, "method");
  255. char *preprocess_name = kxml_node_attr(element, "preprocess");
  256. char *completion = kxml_node_attr(element, "completion");
  257. // Check syntax
  258. if (!name) {
  259. fprintf(stderr, kxml__ERROR_ATTR("name"));
  260. goto error;
  261. }
  262. method = clish_ptype_method_resolve(method_name);
  263. if (CLISH_PTYPE_METHOD_MAX == method) {
  264. fprintf(stderr, kxml__ERROR_ATTR("method"));
  265. goto error;
  266. }
  267. if ((method != CLISH_PTYPE_METHOD_CODE) && !pattern) {
  268. fprintf(stderr, kxml__ERROR_ATTR("pattern"));
  269. goto error;
  270. }
  271. preprocess = clish_ptype_preprocess_resolve(preprocess_name);
  272. ptype = clish_shell_find_create_ptype(shell,
  273. name, help, pattern, method, preprocess);
  274. if (completion)
  275. clish_ptype__set_completion(ptype, completion);
  276. res = process_children(shell, element, ptype);
  277. error:
  278. kxml_node_attr_free(name);
  279. kxml_node_attr_free(help);
  280. kxml_node_attr_free(pattern);
  281. kxml_node_attr_free(method_name);
  282. kxml_node_attr_free(preprocess_name);
  283. kxml_node_attr_free(completion);
  284. parent = parent; // Happy compiler
  285. return res;
  286. }
  287. */
  288. #if 0
  289. static int process_overview(kxml_node_t *element,
  290. void *parent)
  291. {
  292. char *content = NULL;
  293. unsigned int content_len = 2048;
  294. int result;
  295. /*
  296. * the code below faithfully assume that we'll be able fully store
  297. * the content of the node. If it's really, really big, we may have
  298. * an issue (but then, if it's that big, how the hell does it
  299. * already fits in allocated memory?)
  300. * Ergo, it -should- be safe.
  301. */
  302. do {
  303. char *new = (char*)realloc(content, content_len);
  304. if (!new) {
  305. if (content)
  306. free(content);
  307. return -1;
  308. }
  309. content = new;
  310. result = kxml_node_content(element, content,
  311. &content_len);
  312. } while (result == -E2BIG);
  313. if (result == 0 && content) {
  314. /* set the overview text for this view */
  315. assert(NULL == shell->overview);
  316. /* store the overview */
  317. shell->overview = lub_string_dup(content);
  318. }
  319. if (content)
  320. free(content);
  321. parent = parent; /* Happy compiler */
  322. return 0;
  323. }
  324. static int process_command(kxml_node_t *element,
  325. void *parent)
  326. {
  327. clish_view_t *v = (clish_view_t *) parent;
  328. clish_command_t *cmd = NULL;
  329. clish_command_t *old;
  330. int res = -1;
  331. char *access = kxml_node_attr(element, "access");
  332. char *name = kxml_node_attr(element, "name");
  333. char *help = kxml_node_attr(element, "help");
  334. char *view = kxml_node_attr(element, "view");
  335. char *viewid = kxml_node_attr(element, "viewid");
  336. char *escape_chars = kxml_node_attr(element, "escape_chars");
  337. char *args_name = kxml_node_attr(element, "args");
  338. char *args_help = kxml_node_attr(element, "args_help");
  339. char *ref = kxml_node_attr(element, "ref");
  340. /* Check syntax */
  341. if (!name) {
  342. fprintf(stderr, kxml__ERROR_ATTR("name"));
  343. goto error;
  344. }
  345. if (!help) {
  346. fprintf(stderr, kxml__ERROR_ATTR("help"));
  347. goto error;
  348. }
  349. /* check this command doesn't already exist */
  350. old = clish_view_find_command(v, name, BOOL_FALSE);
  351. if (old) {
  352. fprintf(stderr, kxml__ERROR_STR"Duplicate COMMAND name=\"%s\".\n", name);
  353. goto error;
  354. }
  355. /* create a command */
  356. cmd = clish_view_new_command(v, name, help);
  357. clish_command__set_pview(cmd, v);
  358. /* Reference 'ref' field */
  359. if (ref) {
  360. char *saveptr = NULL;
  361. const char *delim = "@";
  362. char *view_name = NULL;
  363. char *cmdn = NULL;
  364. char *str = lub_string_dup(ref);
  365. cmdn = strtok_r(str, delim, &saveptr);
  366. if (!cmdn) {
  367. fprintf(stderr, kxml__ERROR_STR"Invalid \"ref\" attribute value.\n");
  368. lub_string_free(str);
  369. goto error;
  370. }
  371. clish_command__set_alias(cmd, cmdn); /* alias name */
  372. view_name = strtok_r(NULL, delim, &saveptr);
  373. clish_command__set_alias_view(cmd, view_name);
  374. lub_string_free(str);
  375. }
  376. /* define some specialist escape characters */
  377. if (escape_chars)
  378. clish_command__set_escape_chars(cmd, escape_chars);
  379. if (args_name) {
  380. /* define a "rest of line" argument */
  381. clish_param_t *param;
  382. /* Check syntax */
  383. if (!args_help) {
  384. fprintf(stderr, kxml__ERROR_ATTR("args_help"));
  385. goto error;
  386. }
  387. param = clish_param_new(args_name, args_help, "__ptype_ARGS");
  388. clish_command__set_args(cmd, param);
  389. }
  390. /* define the view which this command changes to */
  391. if (view) {
  392. /* reference the next view */
  393. clish_command__set_viewname(cmd, view);
  394. }
  395. /* define the view id which this command changes to */
  396. if (viewid)
  397. clish_command__set_viewid(cmd, viewid);
  398. if (access)
  399. clish_command__set_access(cmd, access);
  400. //process_command_end:
  401. res = process_children(shell, element, cmd);
  402. error:
  403. kxml_node_attr_free(access);
  404. kxml_node_attr_free(name);
  405. kxml_node_attr_free(help);
  406. kxml_node_attr_free(view);
  407. kxml_node_attr_free(viewid);
  408. kxml_node_attr_free(escape_chars);
  409. kxml_node_attr_free(args_name);
  410. kxml_node_attr_free(args_help);
  411. kxml_node_attr_free(ref);
  412. return res;
  413. }
  414. static int process_startup(kxml_node_t *element,
  415. void *parent)
  416. {
  417. clish_command_t *cmd = NULL;
  418. int res = -1;
  419. char *view = kxml_node_attr(element, "view");
  420. char *viewid = kxml_node_attr(element, "viewid");
  421. char *timeout = kxml_node_attr(element, "timeout");
  422. char *default_plugin = kxml_node_attr(element,
  423. "default_plugin");
  424. char *default_shebang = kxml_node_attr(element,
  425. "default_shebang");
  426. char *default_expand = kxml_node_attr(element,
  427. "default_expand");
  428. /* Check syntax */
  429. if (!view) {
  430. fprintf(stderr, kxml__ERROR_ATTR("view"));
  431. goto error;
  432. }
  433. if (shell->startup) {
  434. fprintf(stderr, kxml__ERROR_STR"STARTUP tag duplication.\n");
  435. goto error;
  436. }
  437. /* create a command with NULL help */
  438. cmd = clish_command_new("startup", NULL);
  439. clish_command__set_internal(cmd, BOOL_TRUE);
  440. /* reference the next view */
  441. clish_command__set_viewname(cmd, view);
  442. /* define the view id which this command changes to */
  443. if (viewid)
  444. clish_command__set_viewid(cmd, viewid);
  445. if (default_shebang)
  446. clish_shell__set_default_shebang(shell, default_shebang);
  447. if (default_expand)
  448. clish_shell__set_default_expand(shell,
  449. (lub_string_nocasecmp(default_expand, "true") == 0));
  450. if (timeout) {
  451. unsigned int to = 0;
  452. lub_conv_atoui(timeout, &to, 0);
  453. clish_shell__set_idle_timeout(shell, to);
  454. }
  455. /* If we need the default plugin */
  456. if (default_plugin && (0 == strcmp(default_plugin, "false")))
  457. shell->default_plugin = BOOL_FALSE;
  458. /* remember this command */
  459. shell->startup = cmd;
  460. res = process_children(shell, element, cmd);
  461. error:
  462. kxml_node_attr_free(view);
  463. kxml_node_attr_free(viewid);
  464. kxml_node_attr_free(default_plugin);
  465. kxml_node_attr_free(default_shebang);
  466. kxml_node_attr_free(default_expand);
  467. kxml_node_attr_free(timeout);
  468. parent = parent; /* Happy compiler */
  469. return res;
  470. }
  471. static int process_param(kxml_node_t *element,
  472. void *parent)
  473. {
  474. clish_command_t *cmd = NULL;
  475. clish_param_t *p_param = NULL;
  476. kxml_node_t *pelement;
  477. clish_param_t *param;
  478. char *pname;
  479. int res = -1;
  480. char *name = kxml_node_attr(element, "name");
  481. char *help = kxml_node_attr(element, "help");
  482. char *ptype = kxml_node_attr(element, "ptype");
  483. char *prefix = kxml_node_attr(element, "prefix");
  484. char *defval = kxml_node_attr(element, "default");
  485. char *mode = kxml_node_attr(element, "mode");
  486. char *optional = kxml_node_attr(element, "optional");
  487. char *order = kxml_node_attr(element, "order");
  488. char *value = kxml_node_attr(element, "value");
  489. char *hidden = kxml_node_attr(element, "hidden");
  490. char *test = kxml_node_attr(element, "test");
  491. char *completion = kxml_node_attr(element, "completion");
  492. char *access = kxml_node_attr(element, "access");
  493. /* The PARAM can be child of COMMAND or another PARAM */
  494. pelement = kxml_node_parent(element);
  495. pname = kxml_node_all_name(pelement);
  496. if (pname && lub_string_nocasecmp(pname, "PARAM") == 0)
  497. p_param = (clish_param_t *)parent;
  498. else
  499. cmd = (clish_command_t *)parent;
  500. if (pname)
  501. free(pname);
  502. if (!cmd && !p_param)
  503. goto error;
  504. /* Check syntax */
  505. if (cmd && (cmd == shell->startup)) {
  506. fprintf(stderr, kxml__ERROR_STR"STARTUP can't contain PARAMs.\n");
  507. goto error;
  508. }
  509. if (!name) {
  510. fprintf(stderr, kxml__ERROR_ATTR("name"));
  511. goto error;
  512. }
  513. if (!help) {
  514. fprintf(stderr, kxml__ERROR_ATTR("help"));
  515. goto error;
  516. }
  517. if (!ptype) {
  518. fprintf(stderr, kxml__ERROR_ATTR("ptype"));
  519. goto error;
  520. }
  521. param = clish_param_new(name, help, ptype);
  522. /* If prefix is set clish will emulate old optional
  523. * command syntax over newer optional command mechanism.
  524. * It will create nested PARAM.
  525. */
  526. if (prefix) {
  527. const char *ptype_name = "__ptype_SUBCOMMAND";
  528. clish_param_t *opt_param = NULL;
  529. char *str = NULL;
  530. clish_ptype_t *tmp;
  531. /* Create a ptype for prefix-named subcommand that
  532. * will contain the nested optional parameter. The
  533. * name of ptype is hardcoded. It's not good but
  534. * it's only the service ptype.
  535. */
  536. tmp = clish_shell_find_create_ptype(shell,
  537. ptype_name, "Option", "[^\\\\]+",
  538. CLISH_PTYPE_METHOD_REGEXP, CLISH_PTYPE_PRE_NONE);
  539. assert(tmp);
  540. lub_string_cat(&str, "_prefix_");
  541. lub_string_cat(&str, name);
  542. opt_param = clish_param_new(str, help, ptype_name);
  543. lub_string_free(str);
  544. clish_param__set_mode(opt_param,
  545. CLISH_PARAM_SUBCOMMAND);
  546. clish_param__set_value(opt_param, prefix);
  547. clish_param__set_optional(opt_param, BOOL_TRUE);
  548. if (test)
  549. clish_param__set_test(opt_param, test);
  550. /* Add the parameter to the command */
  551. if (cmd)
  552. clish_command_insert_param(cmd, opt_param);
  553. /* Add the parameter to the param */
  554. if (p_param)
  555. clish_param_insert_param(p_param, opt_param);
  556. /* Unset cmd and set parent param to opt_param */
  557. cmd = NULL;
  558. p_param = opt_param;
  559. }
  560. if (defval)
  561. clish_param__set_defval(param, defval);
  562. if (hidden && lub_string_nocasecmp(hidden, "true") == 0)
  563. clish_param__set_hidden(param, BOOL_TRUE);
  564. else
  565. clish_param__set_hidden(param, BOOL_FALSE);
  566. if (mode) {
  567. if (lub_string_nocasecmp(mode, "switch") == 0) {
  568. clish_param__set_mode(param,
  569. CLISH_PARAM_SWITCH);
  570. /* Force hidden attribute */
  571. clish_param__set_hidden(param, BOOL_TRUE);
  572. } else if (lub_string_nocasecmp(mode, "subcommand") == 0)
  573. clish_param__set_mode(param,
  574. CLISH_PARAM_SUBCOMMAND);
  575. else
  576. clish_param__set_mode(param,
  577. CLISH_PARAM_COMMON);
  578. }
  579. if (optional && lub_string_nocasecmp(optional, "true") == 0)
  580. clish_param__set_optional(param, BOOL_TRUE);
  581. else
  582. clish_param__set_optional(param, BOOL_FALSE);
  583. if (order && lub_string_nocasecmp(order, "true") == 0)
  584. clish_param__set_order(param, BOOL_TRUE);
  585. else
  586. clish_param__set_order(param, BOOL_FALSE);
  587. if (value) {
  588. clish_param__set_value(param, value);
  589. /* Force mode to subcommand */
  590. clish_param__set_mode(param,
  591. CLISH_PARAM_SUBCOMMAND);
  592. }
  593. if (test && !prefix)
  594. clish_param__set_test(param, test);
  595. if (completion)
  596. clish_param__set_completion(param, completion);
  597. if (access)
  598. clish_param__set_access(param, access);
  599. /* Add the parameter to the command */
  600. if (cmd)
  601. clish_command_insert_param(cmd, param);
  602. /* Add the parameter to the param */
  603. if (p_param)
  604. clish_param_insert_param(p_param, param);
  605. res = process_children(shell, element, param);
  606. error:
  607. kxml_node_attr_free(name);
  608. kxml_node_attr_free(help);
  609. kxml_node_attr_free(ptype);
  610. kxml_node_attr_free(prefix);
  611. kxml_node_attr_free(defval);
  612. kxml_node_attr_free(mode);
  613. kxml_node_attr_free(optional);
  614. kxml_node_attr_free(order);
  615. kxml_node_attr_free(value);
  616. kxml_node_attr_free(hidden);
  617. kxml_node_attr_free(test);
  618. kxml_node_attr_free(completion);
  619. kxml_node_attr_free(access);
  620. return res;
  621. }
  622. static int process_action(kxml_node_t *element,
  623. void *parent)
  624. {
  625. clish_action_t *action = NULL;
  626. char *builtin = kxml_node_attr(element, "builtin");
  627. char *shebang = kxml_node_attr(element, "shebang");
  628. char *lock = kxml_node_attr(element, "lock");
  629. char *interrupt = kxml_node_attr(element, "interrupt");
  630. char *interactive = kxml_node_attr(element, "interactive");
  631. char *expand = kxml_node_attr(element, "expand");
  632. kxml_node_t *pelement = kxml_node_parent(element);
  633. char *pname = kxml_node_all_name(pelement);
  634. char *text;
  635. clish_sym_t *sym = NULL;
  636. if (pname && lub_string_nocasecmp(pname, "VAR") == 0)
  637. action = clish_var__action((clish_var_t *)parent);
  638. else if (pname && lub_string_nocasecmp(pname, "PTYPE") == 0)
  639. action = clish_ptype__action((clish_ptype_t *)parent);
  640. else
  641. action = clish_command__action((clish_command_t *)parent);
  642. if (pname)
  643. free(pname);
  644. text = kxml_node_all_content(element);
  645. if (text && *text) {
  646. /* store the action */
  647. clish_action__set_script(action, text);
  648. }
  649. if (text)
  650. free(text);
  651. if (builtin)
  652. sym = clish_shell_add_unresolved_sym(shell, builtin,
  653. CLISH_SYM_TYPE_ACTION);
  654. else
  655. sym = shell->hooks[CLISH_SYM_TYPE_ACTION];
  656. clish_action__set_builtin(action, sym);
  657. if (shebang)
  658. clish_action__set_shebang(action, shebang);
  659. /* lock */
  660. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  661. clish_action__set_lock(action, BOOL_FALSE);
  662. /* interactive */
  663. if (interactive && lub_string_nocasecmp(interactive, "true") == 0)
  664. clish_action__set_interactive(action, BOOL_TRUE);
  665. /* interrupt */
  666. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  667. clish_action__set_interrupt(action, BOOL_TRUE);
  668. /* expand */
  669. if (expand)
  670. clish_action__set_expand(action, lub_tri_from_string(expand));
  671. kxml_node_attr_free(builtin);
  672. kxml_node_attr_free(shebang);
  673. kxml_node_attr_free(lock);
  674. kxml_node_attr_free(interrupt);
  675. kxml_node_attr_free(interactive);
  676. kxml_node_attr_free(expand);
  677. return 0;
  678. }
  679. static int process_detail(kxml_node_t *element,
  680. void *parent)
  681. {
  682. clish_command_t *cmd = (clish_command_t *) parent;
  683. /* read the following text element */
  684. char *text = kxml_node_all_content(element);
  685. if (text && *text) {
  686. /* store the action */
  687. clish_command__set_detail(cmd, text);
  688. }
  689. if (text)
  690. free(text);
  691. shell = shell; /* Happy compiler */
  692. return 0;
  693. }
  694. static int process_namespace(kxml_node_t *element,
  695. void *parent)
  696. {
  697. clish_view_t *v = (clish_view_t *)parent;
  698. clish_nspace_t *nspace = NULL;
  699. int res = -1;
  700. char *view = kxml_node_attr(element, "ref");
  701. char *prefix = kxml_node_attr(element, "prefix");
  702. char *prefix_help = kxml_node_attr(element, "prefix_help");
  703. char *help = kxml_node_attr(element, "help");
  704. char *completion = kxml_node_attr(element, "completion");
  705. char *context_help = kxml_node_attr(element, "context_help");
  706. char *inherit = kxml_node_attr(element, "inherit");
  707. char *access = kxml_node_attr(element, "access");
  708. /* Check syntax */
  709. if (!view) {
  710. fprintf(stderr, kxml__ERROR_ATTR("ref"));
  711. goto error;
  712. }
  713. clish_view_t *ref_view = clish_shell_find_view(shell, view);
  714. /* Don't include itself without prefix */
  715. if ((ref_view == v) && !prefix)
  716. goto process_namespace_end;
  717. nspace = clish_nspace_new(view);
  718. clish_view_insert_nspace(v, nspace);
  719. if (prefix) {
  720. clish_nspace__set_prefix(nspace, prefix);
  721. if (prefix_help)
  722. clish_nspace_create_prefix_cmd(nspace,
  723. "prefix",
  724. prefix_help);
  725. else
  726. clish_nspace_create_prefix_cmd(nspace,
  727. "prefix",
  728. "Prefix for imported commands.");
  729. }
  730. if (help && lub_string_nocasecmp(help, "true") == 0)
  731. clish_nspace__set_help(nspace, BOOL_TRUE);
  732. else
  733. clish_nspace__set_help(nspace, BOOL_FALSE);
  734. if (completion && lub_string_nocasecmp(completion, "false") == 0)
  735. clish_nspace__set_completion(nspace, BOOL_FALSE);
  736. else
  737. clish_nspace__set_completion(nspace, BOOL_TRUE);
  738. if (context_help && lub_string_nocasecmp(context_help, "true") == 0)
  739. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  740. else
  741. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  742. if (inherit && lub_string_nocasecmp(inherit, "false") == 0)
  743. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  744. else
  745. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  746. if (access)
  747. clish_nspace__set_access(nspace, access);
  748. process_namespace_end:
  749. res = 0;
  750. error:
  751. kxml_node_attr_free(view);
  752. kxml_node_attr_free(prefix);
  753. kxml_node_attr_free(prefix_help);
  754. kxml_node_attr_free(help);
  755. kxml_node_attr_free(completion);
  756. kxml_node_attr_free(context_help);
  757. kxml_node_attr_free(inherit);
  758. kxml_node_attr_free(access);
  759. return res;
  760. }
  761. static int process_config(kxml_node_t *element,
  762. void *parent)
  763. {
  764. clish_command_t *cmd = (clish_command_t *)parent;
  765. clish_config_t *config;
  766. if (!cmd)
  767. return 0;
  768. config = clish_command__config(cmd);
  769. /* read the following text element */
  770. char *operation = kxml_node_attr(element, "operation");
  771. char *priority = kxml_node_attr(element, "priority");
  772. char *pattern = kxml_node_attr(element, "pattern");
  773. char *file = kxml_node_attr(element, "file");
  774. char *splitter = kxml_node_attr(element, "splitter");
  775. char *seq = kxml_node_attr(element, "sequence");
  776. char *unique = kxml_node_attr(element, "unique");
  777. char *depth = kxml_node_attr(element, "depth");
  778. if (operation && !lub_string_nocasecmp(operation, "unset"))
  779. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  780. else if (operation && !lub_string_nocasecmp(operation, "none"))
  781. clish_config__set_op(config, CLISH_CONFIG_NONE);
  782. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  783. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  784. else {
  785. clish_config__set_op(config, CLISH_CONFIG_SET);
  786. /* The priority if no clearly specified */
  787. clish_config__set_priority(config, 0x7f00);
  788. }
  789. if (priority) {
  790. unsigned short pri = 0;
  791. lub_conv_atous(priority, &pri, 0);
  792. clish_config__set_priority(config, pri);
  793. }
  794. if (pattern)
  795. clish_config__set_pattern(config, pattern);
  796. else
  797. clish_config__set_pattern(config, "^${__cmd}");
  798. if (file)
  799. clish_config__set_file(config, file);
  800. if (splitter && lub_string_nocasecmp(splitter, "false") == 0)
  801. clish_config__set_splitter(config, BOOL_FALSE);
  802. else
  803. clish_config__set_splitter(config, BOOL_TRUE);
  804. if (unique && lub_string_nocasecmp(unique, "false") == 0)
  805. clish_config__set_unique(config, BOOL_FALSE);
  806. else
  807. clish_config__set_unique(config, BOOL_TRUE);
  808. if (seq)
  809. clish_config__set_seq(config, seq);
  810. else
  811. /* The entries without sequence cannot be non-unique */
  812. clish_config__set_unique(config, BOOL_TRUE);
  813. if (depth)
  814. clish_config__set_depth(config, depth);
  815. kxml_node_attr_free(operation);
  816. kxml_node_attr_free(priority);
  817. kxml_node_attr_free(pattern);
  818. kxml_node_attr_free(file);
  819. kxml_node_attr_free(splitter);
  820. kxml_node_attr_free(seq);
  821. kxml_node_attr_free(unique);
  822. kxml_node_attr_free(depth);
  823. shell = shell; /* Happy compiler */
  824. return 0;
  825. }
  826. static int process_var(kxml_node_t *element,
  827. void *parent)
  828. {
  829. clish_var_t *var = NULL;
  830. int res = -1;
  831. char *name = kxml_node_attr(element, "name");
  832. char *dynamic = kxml_node_attr(element, "dynamic");
  833. char *value = kxml_node_attr(element, "value");
  834. /* Check syntax */
  835. if (!name) {
  836. fprintf(stderr, kxml__ERROR_ATTR("name"));
  837. goto error;
  838. }
  839. /* Check if this var doesn't already exist */
  840. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  841. if (var) {
  842. fprintf(stderr, kxml__ERROR_STR"Duplicate VAR name=\"%s\".\n", name);
  843. goto error;
  844. }
  845. /* Create var instance */
  846. var = clish_var_new(name);
  847. lub_bintree_insert(&shell->var_tree, var);
  848. if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
  849. clish_var__set_dynamic(var, BOOL_TRUE);
  850. if (value)
  851. clish_var__set_value(var, value);
  852. res = process_children(shell, element, var);
  853. error:
  854. kxml_node_attr_free(name);
  855. kxml_node_attr_free(dynamic);
  856. kxml_node_attr_free(value);
  857. parent = parent; /* Happy compiler */
  858. return res;
  859. }
  860. static int process_wdog(clish_shell_t *shell,
  861. kxml_node_t *element, void *parent)
  862. {
  863. clish_command_t *cmd = NULL;
  864. int res = -1;
  865. /* Check syntax */
  866. if (shell->wdog) {
  867. fprintf(stderr, kxml__ERROR_STR"WATCHDOG tag duplication.\n");
  868. goto error;
  869. }
  870. /* Create a command with NULL help */
  871. cmd = clish_command_new("watchdog", NULL);
  872. /* Remember this command */
  873. shell->wdog = cmd;
  874. res = process_children(shell, element, cmd);
  875. error:
  876. parent = parent; /* Happy compiler */
  877. return res;
  878. }
  879. static int process_hotkey(kxml_node_t *element,
  880. void *parent)
  881. {
  882. clish_view_t *v = (clish_view_t *)parent;
  883. int res = -1;
  884. char *key = kxml_node_attr(element, "key");
  885. char *cmd = kxml_node_attr(element, "cmd");
  886. /* Check syntax */
  887. if (!key) {
  888. fprintf(stderr, kxml__ERROR_ATTR("key"));
  889. goto error;
  890. }
  891. if (!cmd) {
  892. fprintf(stderr, kxml__ERROR_ATTR("cmd"));
  893. goto error;
  894. }
  895. clish_view_insert_hotkey(v, key, cmd);
  896. res = 0;
  897. error:
  898. kxml_node_attr_free(key);
  899. kxml_node_attr_free(cmd);
  900. shell = shell; /* Happy compiler */
  901. return res;
  902. }
  903. static int process_plugin(kxml_node_t *element,
  904. void *parent)
  905. {
  906. clish_plugin_t *plugin;
  907. char *file = kxml_node_attr(element, "file");
  908. char *name = kxml_node_attr(element, "name");
  909. char *alias = kxml_node_attr(element, "alias");
  910. char *rtld_global = kxml_node_attr(element, "rtld_global");
  911. int res = -1;
  912. char *text;
  913. /* Check syntax */
  914. if (!name) {
  915. fprintf(stderr, kxml__ERROR_ATTR("name"));
  916. goto error;
  917. }
  918. plugin = clish_shell_find_plugin(shell, name);
  919. if (plugin) {
  920. fprintf(stderr,
  921. kxml__ERROR_STR"PLUGIN %s duplication.\n", name);
  922. goto error;
  923. }
  924. plugin = clish_shell_create_plugin(shell, name);
  925. if (alias && *alias)
  926. clish_plugin__set_alias(plugin, alias);
  927. if (file && *file)
  928. clish_plugin__set_file(plugin, file);
  929. if (rtld_global && lub_string_nocasecmp(rtld_global, "true") == 0)
  930. clish_plugin__set_rtld_global(plugin, BOOL_TRUE);
  931. /* Get PLUGIN body content */
  932. text = kxml_node_all_content(element);
  933. if (text && *text)
  934. clish_plugin__set_conf(plugin, text);
  935. if (text)
  936. free(text);
  937. res = 0;
  938. error:
  939. kxml_node_attr_free(file);
  940. kxml_node_attr_free(name);
  941. kxml_node_attr_free(alias);
  942. kxml_node_attr_free(rtld_global);
  943. parent = parent; /* Happy compiler */
  944. return res;
  945. }
  946. static int process_hook(kxml_node_t *element,
  947. void *parent)
  948. {
  949. char *name = kxml_node_attr(element, "name");
  950. char *builtin = kxml_node_attr(element, "builtin");
  951. int res = -1;
  952. int type = CLISH_SYM_TYPE_NONE;
  953. /* Check syntax */
  954. if (!name) {
  955. fprintf(stderr, kxml__ERROR_ATTR("name"));
  956. goto error;
  957. }
  958. /* Find out HOOK type */
  959. if (!strcmp(name, "action"))
  960. type = CLISH_SYM_TYPE_ACTION;
  961. else if (!strcmp(name, "access"))
  962. type = CLISH_SYM_TYPE_ACCESS;
  963. else if (!strcmp(name, "config"))
  964. type = CLISH_SYM_TYPE_CONFIG;
  965. else if (!strcmp(name, "log"))
  966. type = CLISH_SYM_TYPE_LOG;
  967. if (CLISH_SYM_TYPE_NONE == type) {
  968. fprintf(stderr, kxml__ERROR_STR"Unknown HOOK name %s.\n", name);
  969. goto error;
  970. }
  971. /* Check duplicate HOOK tag */
  972. if (shell->hooks_use[type]) {
  973. fprintf(stderr,
  974. kxml__ERROR_STR"HOOK %s duplication.\n", name);
  975. goto error;
  976. }
  977. shell->hooks_use[type] = BOOL_TRUE;
  978. clish_sym__set_name(shell->hooks[type], builtin);
  979. res = 0;
  980. error:
  981. kxml_node_attr_free(name);
  982. kxml_node_attr_free(builtin);
  983. parent = parent; /* Happy compiler */
  984. return res;
  985. }
  986. #endif