load.c 28 KB

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