load.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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_entry,
  31. process_hotkey;
  32. // Different TAGs types
  33. typedef enum {
  34. KTAG_NONE,
  35. KTAG_ACTION,
  36. KTAG_PARAM,
  37. KTAG_SWITCH, // PARAM alias
  38. KTAG_SEQ, // PARAM alias
  39. KTAG_COMMAND,
  40. KTAG_FILTER,
  41. KTAG_VIEW,
  42. KTAG_PTYPE,
  43. KTAG_PLUGIN,
  44. KTAG_KLISH,
  45. KTAG_ENTRY,
  46. KTAG_COND,
  47. KTAG_COMPL,
  48. KTAG_HELP,
  49. KTAG_PROMPT,
  50. KTAG_HOTKEY,
  51. KTAG_MAX,
  52. } ktags_e;
  53. static const char * const kxml_tags[] = {
  54. NULL,
  55. "ACTION",
  56. "PARAM",
  57. "SWITCH",
  58. "SEQ",
  59. "COMMAND",
  60. "FILTER",
  61. "VIEW",
  62. "PTYPE",
  63. "PLUGIN",
  64. "KLISH",
  65. "ENTRY",
  66. "COND",
  67. "COMPL",
  68. "HELP",
  69. "PROMPT",
  70. "HOTKEY",
  71. };
  72. static kxml_process_fn *kxml_handlers[] = {
  73. NULL,
  74. process_action,
  75. process_param,
  76. process_param,
  77. process_param,
  78. process_command,
  79. process_command,
  80. process_view,
  81. process_ptype,
  82. process_plugin,
  83. process_klish,
  84. process_entry,
  85. process_command,
  86. process_command,
  87. process_command,
  88. process_command,
  89. process_hotkey,
  90. };
  91. static const char *kxml_tag_name(ktags_e tag)
  92. {
  93. if ((KTAG_NONE == tag) || (tag >= KTAG_MAX))
  94. return "NONE";
  95. return kxml_tags[tag];
  96. }
  97. static ktags_e kxml_node_tag(const kxml_node_t *node)
  98. {
  99. ktags_e tag = KTAG_NONE;
  100. char *name = NULL;
  101. if (!node)
  102. return KTAG_NONE;
  103. if (kxml_node_type(node) != KXML_NODE_ELM)
  104. return KTAG_NONE;
  105. name = kxml_node_name(node);
  106. if (!name)
  107. return KTAG_NONE; // Strange case
  108. for (tag = (KTAG_NONE + 1); tag < KTAG_MAX; tag++) {
  109. if (faux_str_casecmp(name, kxml_tags[tag]) == 0)
  110. break;
  111. }
  112. kxml_node_name_free(name);
  113. if (tag >= KTAG_MAX)
  114. return KTAG_NONE;
  115. return tag;
  116. }
  117. static kxml_process_fn *kxml_node_handler(const kxml_node_t *node)
  118. {
  119. return kxml_handlers[kxml_node_tag(node)];
  120. }
  121. /** @brief Reads an element from the XML stream and processes it.
  122. */
  123. static bool_t process_node(const kxml_node_t *node, void *parent, faux_error_t *error)
  124. {
  125. kxml_process_fn *handler = NULL;
  126. // Process only KXML_NODE_ELM. Don't process other types like:
  127. // KXML_NODE_DOC,
  128. // KXML_NODE_TEXT,
  129. // KXML_NODE_ATTR,
  130. // KXML_NODE_COMMENT,
  131. // KXML_NODE_PI,
  132. // KXML_NODE_DECL,
  133. // KXML_NODE_UNKNOWN,
  134. if (kxml_node_type(node) != KXML_NODE_ELM)
  135. return BOOL_TRUE;
  136. handler = kxml_node_handler(node);
  137. if (!handler) { // Unknown element
  138. faux_error_sprintf(error,
  139. TAG": Unknown tag \"%s\"", kxml_node_name(node));
  140. return BOOL_FALSE;
  141. }
  142. #ifdef KXML_DEBUG
  143. printf("kxml: Tag \"%s\"\n", kxml_node_name(node));
  144. #endif
  145. return handler(node, parent, error);
  146. }
  147. static bool_t kxml_load_file(kscheme_t *scheme, const char *filename,
  148. faux_error_t *error)
  149. {
  150. kxml_doc_t *doc = NULL;
  151. kxml_node_t *root = NULL;
  152. bool_t r = BOOL_FALSE;
  153. if (!scheme)
  154. return BOOL_FALSE;
  155. if (!filename)
  156. return BOOL_FALSE;
  157. #ifdef KXML_DEBUG
  158. printf("kxml: Processing XML file \"%s\"\n", filename);
  159. #endif
  160. doc = kxml_doc_read(filename);
  161. if (!kxml_doc_is_valid(doc)) {
  162. /* int errcaps = kxml_doc_error_caps(doc);
  163. printf("Unable to open file '%s'", filename);
  164. if ((errcaps & kxml_ERR_LINE) == kxml_ERR_LINE)
  165. printf(", at line %d", kxml_doc_err_line(doc));
  166. if ((errcaps & kxml_ERR_COL) == kxml_ERR_COL)
  167. printf(", at column %d", kxml_doc_err_col(doc));
  168. if ((errcaps & kxml_ERR_DESC) == kxml_ERR_DESC)
  169. printf(", message is %s", kxml_doc_err_msg(doc));
  170. printf("\n");
  171. */ kxml_doc_release(doc);
  172. return BOOL_FALSE;
  173. }
  174. root = kxml_doc_root(doc);
  175. r = process_node(root, scheme, error);
  176. kxml_doc_release(doc);
  177. if (!r) {
  178. faux_error_sprintf(error, TAG": Illegal file %s", filename);
  179. return BOOL_FALSE;
  180. }
  181. return BOOL_TRUE;
  182. }
  183. /** @brief Default path to get XML files from.
  184. */
  185. static const char *default_path = "/etc/klish;~/.klish";
  186. static const char *path_separators = ":;";
  187. bool_t kxml_load_scheme(kscheme_t *scheme, const char *xml_path,
  188. faux_error_t *error)
  189. {
  190. char *path = NULL;
  191. char *fn = NULL;
  192. char *saveptr = NULL;
  193. bool_t ret = BOOL_TRUE;
  194. assert(scheme);
  195. if (!scheme)
  196. return BOOL_FALSE;
  197. // Use the default path if xml path is not specified.
  198. // Dup is needed because sring will be tokenized but
  199. // the xml_path is must be const.
  200. if (!xml_path)
  201. path = faux_str_dup(default_path);
  202. else
  203. path = faux_str_dup(xml_path);
  204. #ifdef KXML_DEBUG
  205. printf("kxml: Loading scheme \"%s\"\n", path);
  206. #endif
  207. // Loop through each directory
  208. for (fn = strtok_r(path, path_separators, &saveptr);
  209. fn; fn = strtok_r(NULL, path_separators, &saveptr)) {
  210. DIR *dir = NULL;
  211. struct dirent *entry = NULL;
  212. char *realpath = NULL;
  213. // Expand tilde. Tilde must be the first symbol.
  214. realpath = faux_expand_tilde(fn);
  215. // Regular file
  216. if (faux_isfile(realpath)) {
  217. if (!kxml_load_file(scheme, realpath, error))
  218. ret = BOOL_FALSE;
  219. faux_str_free(realpath);
  220. continue;
  221. }
  222. // Search this directory for any XML files
  223. #ifdef KXML_DEBUG
  224. printf("kxml: Processing XML dir \"%s\"\n", realpath);
  225. #endif
  226. dir = opendir(realpath);
  227. if (!dir) {
  228. faux_str_free(realpath);
  229. continue;
  230. }
  231. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  232. const char *extension = strrchr(entry->d_name, '.');
  233. char *filename = NULL;
  234. // Check the filename
  235. if (!extension || strcmp(".xml", extension))
  236. continue;
  237. filename = faux_str_sprintf("%s/%s", realpath, entry->d_name);
  238. if (!kxml_load_file(scheme, filename, error))
  239. ret = BOOL_FALSE;
  240. faux_str_free(filename);
  241. }
  242. closedir(dir);
  243. faux_str_free(realpath);
  244. }
  245. faux_str_free(path);
  246. return ret;
  247. }
  248. /** @brief Iterate through element's children.
  249. */
  250. static bool_t process_children(const kxml_node_t *element, void *parent,
  251. faux_error_t *error)
  252. {
  253. const kxml_node_t *node = NULL;
  254. while ((node = kxml_node_next_child(element, node)) != NULL) {
  255. bool_t res = BOOL_FALSE;
  256. res = process_node(node, parent, error);
  257. if (!res)
  258. return res;
  259. }
  260. return BOOL_TRUE;
  261. }
  262. static bool_t process_klish(const kxml_node_t *element, void *parent,
  263. faux_error_t *error)
  264. {
  265. return process_children(element, parent, error);
  266. }
  267. static bool_t process_plugin(const kxml_node_t *element, void *parent,
  268. faux_error_t *error)
  269. {
  270. iplugin_t iplugin = {};
  271. kplugin_t *plugin = NULL;
  272. bool_t res = BOOL_FALSE;
  273. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  274. if (parent_tag != KTAG_KLISH) {
  275. faux_error_sprintf(error,
  276. TAG": Tag \"%s\" can't contain PLUGIN tag",
  277. kxml_tag_name(parent_tag));
  278. return BOOL_FALSE;
  279. }
  280. iplugin.name = kxml_node_attr(element, "name");
  281. iplugin.id = kxml_node_attr(element, "id");
  282. iplugin.file = kxml_node_attr(element, "file");
  283. iplugin.conf = kxml_node_content(element);
  284. plugin = iplugin_load(&iplugin, error);
  285. if (!plugin)
  286. goto err;
  287. if (!kscheme_add_plugins((kscheme_t *)parent, plugin)) {
  288. faux_error_sprintf(error, TAG": Can't add PLUGIN \"%s\". "
  289. "Probably duplication",
  290. kplugin_name(plugin));
  291. kplugin_free(plugin);
  292. goto err;
  293. }
  294. if (!process_children(element, plugin, error))
  295. goto err;
  296. res = BOOL_TRUE;
  297. err:
  298. kxml_node_attr_free(iplugin.name);
  299. kxml_node_attr_free(iplugin.id);
  300. kxml_node_attr_free(iplugin.file);
  301. kxml_node_content_free(iplugin.conf);
  302. return res;
  303. }
  304. static bool_t process_action(const kxml_node_t *element, void *parent,
  305. faux_error_t *error)
  306. {
  307. iaction_t iaction = {};
  308. kaction_t *action = NULL;
  309. bool_t res = BOOL_FALSE;
  310. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  311. kentry_t *parent_entry = (kentry_t *)parent;
  312. iaction.sym = kxml_node_attr(element, "sym");
  313. iaction.lock = kxml_node_attr(element, "lock");
  314. iaction.interrupt = kxml_node_attr(element, "interrupt");
  315. iaction.interactive = kxml_node_attr(element, "interactive");
  316. iaction.exec_on = kxml_node_attr(element, "exec_on");
  317. iaction.update_retcode = kxml_node_attr(element, "update_retcode");
  318. iaction.permanent = kxml_node_attr(element, "permanent");
  319. iaction.sync = kxml_node_attr(element, "sync");
  320. iaction.script = kxml_node_content(element);
  321. action = iaction_load(&iaction, error);
  322. if (!action)
  323. goto err;
  324. if ( (KTAG_ENTRY != parent_tag) &&
  325. (KTAG_COMMAND != parent_tag) &&
  326. (KTAG_FILTER != parent_tag) &&
  327. (KTAG_PARAM != parent_tag) &&
  328. (KTAG_COND != parent_tag) &&
  329. (KTAG_COMPL != parent_tag) &&
  330. (KTAG_HELP != parent_tag) &&
  331. (KTAG_PROMPT != parent_tag) &&
  332. (KTAG_PTYPE != parent_tag)) {
  333. faux_error_sprintf(error,
  334. TAG": Tag \"%s\" can't contain ACTION tag",
  335. kxml_tag_name(parent_tag));
  336. kaction_free(action);
  337. goto err;
  338. }
  339. if (!kentry_add_actions(parent_entry, action)) {
  340. faux_error_sprintf(error,
  341. TAG": Can't add ACTION #%d to ENTRY \"%s\". "
  342. "Probably duplication",
  343. kentry_actions_len(parent_entry) + 1,
  344. kentry_name(parent_entry));
  345. kaction_free(action);
  346. goto err;
  347. }
  348. if (!process_children(element, action, error))
  349. goto err;
  350. res = BOOL_TRUE;
  351. err:
  352. kxml_node_attr_free(iaction.sym);
  353. kxml_node_attr_free(iaction.lock);
  354. kxml_node_attr_free(iaction.interrupt);
  355. kxml_node_attr_free(iaction.interactive);
  356. kxml_node_attr_free(iaction.exec_on);
  357. kxml_node_attr_free(iaction.update_retcode);
  358. kxml_node_attr_free(iaction.permanent);
  359. kxml_node_attr_free(iaction.sync);
  360. kxml_node_content_free(iaction.script);
  361. return res;
  362. }
  363. static kentry_t *add_entry_to_hierarchy(const kxml_node_t *element, void *parent,
  364. ientry_t *ientry, faux_error_t *error)
  365. {
  366. kentry_t *entry = NULL;
  367. ktags_e tag = kxml_node_tag(element);
  368. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  369. assert(ientry);
  370. // Parent is mandatory field
  371. if (!parent) {
  372. faux_error_sprintf(error,
  373. TAG": Broken parent object for entry \"%s\"",
  374. ientry->name);
  375. return NULL;
  376. }
  377. if ((parent_tag == KTAG_ACTION) ||
  378. // (parent_tag == KTAG_HOTKEY) ||
  379. (parent_tag == KTAG_PLUGIN)) {
  380. faux_error_sprintf(error,
  381. TAG": Tag \"%s\" can't contain %s tag \"%s\"",
  382. kxml_tag_name(parent_tag),
  383. kxml_tag_name(tag), ientry->name);
  384. return NULL;
  385. }
  386. // High level ENTRY
  387. if (KTAG_KLISH == parent_tag) {
  388. kscheme_t *scheme = (kscheme_t *)parent;
  389. // Does such ENTRY already exist
  390. entry = kscheme_find_entry(scheme, ientry->name);
  391. if (entry) {
  392. if (!ientry_parse(ientry, entry, error))
  393. return NULL;
  394. } else { // New entry object
  395. entry = ientry_load(ientry, error);
  396. if (!entry)
  397. return NULL;
  398. if (!kscheme_add_entrys(scheme, entry)) {
  399. faux_error_sprintf(error, TAG": Can't add entry \"%s\". "
  400. "Probably duplication",
  401. kentry_name(entry));
  402. kentry_free(entry);
  403. return NULL;
  404. }
  405. }
  406. // ENTRY within ENTRY
  407. } else {
  408. kentry_t *parent_entry = (kentry_t *)parent;
  409. // Does such ENTRY already exist
  410. entry = kentry_find_entry(parent_entry, ientry->name);
  411. if (entry) {
  412. if (!ientry_parse(ientry, entry, error))
  413. return NULL;
  414. } else { // New entry object
  415. entry = ientry_load(ientry, error);
  416. if (!entry)
  417. return NULL;
  418. kentry_set_parent(entry, parent_entry);
  419. if (!kentry_add_entrys(parent_entry, entry)) {
  420. faux_error_sprintf(error, TAG": Can't add entry \"%s\". "
  421. "Probably duplication",
  422. kentry_name(entry));
  423. kentry_free(entry);
  424. return NULL;
  425. }
  426. }
  427. }
  428. return entry;
  429. }
  430. static bool_t process_entry(const kxml_node_t *element, void *parent,
  431. faux_error_t *error)
  432. {
  433. ientry_t ientry = {};
  434. kentry_t *entry = NULL;
  435. bool_t res = BOOL_FALSE;
  436. // Mandatory entry name
  437. ientry.name = kxml_node_attr(element, "name");
  438. if (!ientry.name) {
  439. faux_error_sprintf(error, TAG": entry without name");
  440. return BOOL_FALSE;
  441. }
  442. ientry.help = kxml_node_attr(element, "help");
  443. ientry.container = kxml_node_attr(element, "container");
  444. ientry.mode = kxml_node_attr(element, "mode");
  445. ientry.purpose = kxml_node_attr(element, "purpose");
  446. ientry.min = kxml_node_attr(element, "min");
  447. ientry.max = kxml_node_attr(element, "max");
  448. ientry.ref = kxml_node_attr(element, "ref");
  449. ientry.value = kxml_node_attr(element, "value");
  450. ientry.restore = kxml_node_attr(element, "restore");
  451. ientry.order = kxml_node_attr(element, "order");
  452. ientry.filter = kxml_node_attr(element, "filter");
  453. if (!(entry = add_entry_to_hierarchy(element, parent, &ientry, error)))
  454. goto err;
  455. if (!process_children(element, entry, error))
  456. goto err;
  457. res = BOOL_TRUE;
  458. err:
  459. kxml_node_attr_free(ientry.name);
  460. kxml_node_attr_free(ientry.help);
  461. kxml_node_attr_free(ientry.container);
  462. kxml_node_attr_free(ientry.mode);
  463. kxml_node_attr_free(ientry.purpose);
  464. kxml_node_attr_free(ientry.min);
  465. kxml_node_attr_free(ientry.max);
  466. kxml_node_attr_free(ientry.ref);
  467. kxml_node_attr_free(ientry.value);
  468. kxml_node_attr_free(ientry.restore);
  469. kxml_node_attr_free(ientry.order);
  470. kxml_node_attr_free(ientry.filter);
  471. return res;
  472. }
  473. static bool_t process_view(const kxml_node_t *element, void *parent,
  474. faux_error_t *error)
  475. {
  476. ientry_t ientry = {};
  477. kentry_t *entry = NULL;
  478. bool_t res = BOOL_FALSE;
  479. // Mandatory VIEW name
  480. ientry.name = kxml_node_attr(element, "name");
  481. if (!ientry.name) {
  482. faux_error_sprintf(error, TAG": VIEW without name");
  483. return BOOL_FALSE;
  484. }
  485. ientry.help = kxml_node_attr(element, "help");
  486. ientry.container = "true";
  487. ientry.mode = "switch";
  488. ientry.purpose = "common";
  489. ientry.min = "1";
  490. ientry.max = "1";
  491. ientry.ref = kxml_node_attr(element, "ref");
  492. ientry.value = NULL;
  493. ientry.restore = "false";
  494. ientry.order = "false";
  495. ientry.filter = "false";
  496. if (!(entry = add_entry_to_hierarchy(element, parent, &ientry, error)))
  497. goto err;
  498. if (!process_children(element, entry, error))
  499. goto err;
  500. res = BOOL_TRUE;
  501. err:
  502. kxml_node_attr_free(ientry.name);
  503. kxml_node_attr_free(ientry.help);
  504. kxml_node_attr_free(ientry.ref);
  505. return res;
  506. }
  507. static bool_t process_ptype(const kxml_node_t *element, void *parent,
  508. faux_error_t *error)
  509. {
  510. ientry_t ientry = {};
  511. kentry_t *entry = NULL;
  512. bool_t res = BOOL_FALSE;
  513. bool_t is_name = BOOL_FALSE;
  514. // Mandatory PTYPE name or reference
  515. ientry.name = kxml_node_attr(element, "name");
  516. ientry.ref = kxml_node_attr(element, "ref");
  517. if (ientry.name) {
  518. is_name = BOOL_TRUE;
  519. } else {
  520. if (!ientry.ref) {
  521. faux_error_sprintf(error,
  522. TAG": PTYPE without name or reference");
  523. return BOOL_FALSE;
  524. }
  525. ientry.name = "__ptype";
  526. }
  527. ientry.help = kxml_node_attr(element, "help");
  528. ientry.container = "true";
  529. ientry.mode = "sequence";
  530. ientry.purpose = "ptype";
  531. ientry.min = "1";
  532. ientry.max = "1";
  533. ientry.value = kxml_node_attr(element, "value");
  534. ientry.restore = "false";
  535. ientry.order = "true";
  536. ientry.filter = "false";
  537. if (!(entry = add_entry_to_hierarchy(element, parent, &ientry, error)))
  538. goto err;
  539. if (!process_children(element, entry, error))
  540. goto err;
  541. res = BOOL_TRUE;
  542. err:
  543. if (is_name)
  544. kxml_node_attr_free(ientry.name);
  545. kxml_node_attr_free(ientry.help);
  546. kxml_node_attr_free(ientry.ref);
  547. kxml_node_attr_free(ientry.value);
  548. return res;
  549. }
  550. static kentry_t *create_ptype(const char *ptype,
  551. const char *compl, const char *help, const char *ref)
  552. {
  553. kentry_t *ptype_entry = NULL;
  554. if (!ptype && !ref)
  555. return NULL;
  556. ptype_entry = kentry_new("__ptype");
  557. assert(ptype_entry);
  558. kentry_set_purpose(ptype_entry, KENTRY_PURPOSE_PTYPE);
  559. if (ref) {
  560. kentry_set_ref_str(ptype_entry, ref);
  561. // If ptype is reference then another actions is not needed.
  562. return ptype_entry;
  563. } else {
  564. kaction_t *ptype_action = kaction_new();
  565. assert(ptype_action);
  566. kaction_set_sym_ref(ptype_action, ptype);
  567. kaction_set_permanent(ptype_action, TRI_TRUE);
  568. kentry_add_actions(ptype_entry, ptype_action);
  569. }
  570. if (compl) {
  571. kentry_t *compl_entry = NULL;
  572. kaction_t *compl_action = NULL;
  573. compl_action = kaction_new();
  574. assert(compl_action);
  575. kaction_set_sym_ref(compl_action, compl);
  576. kaction_set_permanent(compl_action, TRI_TRUE);
  577. compl_entry = kentry_new("__compl");
  578. assert(compl_entry);
  579. kentry_set_purpose(compl_entry, KENTRY_PURPOSE_COMPLETION);
  580. kentry_add_actions(compl_entry, compl_action);
  581. kentry_add_entrys(ptype_entry, compl_entry);
  582. }
  583. if (help) {
  584. kentry_t *help_entry = NULL;
  585. kaction_t *help_action = NULL;
  586. help_action = kaction_new();
  587. assert(help_action);
  588. kaction_set_sym_ref(help_action, help);
  589. kaction_set_permanent(help_action, TRI_TRUE);
  590. help_entry = kentry_new("__help");
  591. assert(help_entry);
  592. kentry_set_purpose(help_entry, KENTRY_PURPOSE_HELP);
  593. kentry_add_actions(help_entry, help_action);
  594. kentry_add_entrys(ptype_entry, help_entry);
  595. }
  596. return ptype_entry;
  597. }
  598. // PARAM, SWITCH, SEQ
  599. static bool_t process_param(const kxml_node_t *element, void *parent,
  600. faux_error_t *error)
  601. {
  602. ientry_t ientry = {};
  603. kentry_t *entry = NULL;
  604. bool_t res = BOOL_FALSE;
  605. ktags_e tag = kxml_node_tag(element);
  606. bool_t is_mode = BOOL_FALSE;
  607. char *ptype_str = NULL;
  608. // Mandatory PARAM name
  609. ientry.name = kxml_node_attr(element, "name");
  610. if (!ientry.name) {
  611. faux_error_sprintf(error, TAG": PARAM without name");
  612. return BOOL_FALSE;
  613. }
  614. ientry.help = kxml_node_attr(element, "help");
  615. // Container
  616. if (KTAG_PARAM == tag)
  617. ientry.container = "false";
  618. else
  619. ientry.container = "true"; // SWITCH, SEQ
  620. // Mode
  621. switch (tag) {
  622. case KTAG_PARAM:
  623. ientry.mode = kxml_node_attr(element, "mode");
  624. is_mode = BOOL_TRUE;
  625. break;
  626. case KTAG_SWITCH:
  627. ientry.mode = "switch";
  628. break;
  629. case KTAG_SEQ:
  630. ientry.mode = "sequence";
  631. break;
  632. default:
  633. ientry.mode = "empty";
  634. break;
  635. }
  636. ientry.purpose = "common";
  637. ientry.min = kxml_node_attr(element, "min");
  638. ientry.max = kxml_node_attr(element, "max");
  639. ientry.ref = kxml_node_attr(element, "ref");
  640. ientry.value = kxml_node_attr(element, "value");
  641. ientry.restore = "false";
  642. ientry.order = kxml_node_attr(element, "order");
  643. ientry.filter = "false";
  644. if (!(entry = add_entry_to_hierarchy(element, parent, &ientry, error)))
  645. goto err;
  646. // Special attribute "ptype". It exists for more simple XML only. It
  647. // just links existing PTYPE. User can to don't specify nested tag PTYPE.
  648. ptype_str = kxml_node_attr(element, "ptype");
  649. if (ptype_str) {
  650. kentry_t *ptype_entry = create_ptype(NULL, NULL, NULL, ptype_str);
  651. assert(ptype_entry);
  652. kentry_add_entrys(entry, ptype_entry);
  653. }
  654. if (!process_children(element, entry, error))
  655. goto err;
  656. res = BOOL_TRUE;
  657. err:
  658. kxml_node_attr_free(ientry.name);
  659. kxml_node_attr_free(ientry.help);
  660. if (is_mode)
  661. kxml_node_attr_free(ientry.mode);
  662. kxml_node_attr_free(ientry.min);
  663. kxml_node_attr_free(ientry.max);
  664. kxml_node_attr_free(ientry.ref);
  665. kxml_node_attr_free(ientry.value);
  666. kxml_node_attr_free(ientry.order);
  667. kxml_node_attr_free(ptype_str);
  668. return res;
  669. }
  670. // COMMAND, FILTER, COND, COMPL, HELP, PROMPT
  671. static bool_t process_command(const kxml_node_t *element, void *parent,
  672. faux_error_t *error)
  673. {
  674. ientry_t ientry = {};
  675. kentry_t *entry = NULL;
  676. bool_t res = BOOL_FALSE;
  677. ktags_e tag = kxml_node_tag(element);
  678. bool_t is_name = BOOL_FALSE;
  679. bool_t is_filter = BOOL_FALSE;
  680. kentry_entrys_node_t *iter = NULL;
  681. kentry_t *nested_entry = NULL;
  682. bool_t ptype_exists = BOOL_FALSE;
  683. // Mandatory COMMAND name
  684. ientry.name = kxml_node_attr(element, "name");
  685. if (ientry.name) {
  686. is_name = BOOL_TRUE;
  687. } else {
  688. switch (tag) {
  689. case KTAG_COMMAND:
  690. case KTAG_FILTER:
  691. faux_error_sprintf(error, TAG": COMMAND without name");
  692. return BOOL_FALSE;
  693. case KTAG_COND:
  694. ientry.name = "__cond";
  695. break;
  696. case KTAG_COMPL:
  697. ientry.name = "__compl";
  698. break;
  699. case KTAG_HELP:
  700. ientry.name = "__help";
  701. break;
  702. case KTAG_PROMPT:
  703. ientry.name = "__prompt";
  704. break;
  705. default:
  706. faux_error_sprintf(error, TAG": Unknown tag");
  707. return BOOL_FALSE;
  708. }
  709. }
  710. ientry.help = kxml_node_attr(element, "help");
  711. ientry.container = "false";
  712. ientry.mode = kxml_node_attr(element, "mode");
  713. // Purpose
  714. switch (tag) {
  715. case KTAG_COND:
  716. ientry.purpose = "cond";
  717. break;
  718. case KTAG_COMPL:
  719. ientry.purpose = "completion";
  720. break;
  721. case KTAG_HELP:
  722. ientry.purpose = "help";
  723. break;
  724. case KTAG_PROMPT:
  725. ientry.purpose = "prompt";
  726. break;
  727. default:
  728. ientry.purpose = "common";
  729. break;
  730. }
  731. ientry.min = kxml_node_attr(element, "min");
  732. ientry.max = kxml_node_attr(element, "max");
  733. ientry.ref = kxml_node_attr(element, "ref");
  734. if ((KTAG_FILTER == tag) || (KTAG_COMMAND == tag)) {
  735. ientry.value = kxml_node_attr(element, "value");
  736. ientry.restore = kxml_node_attr(element, "restore");
  737. } else {
  738. ientry.value = NULL;
  739. ientry.restore = "false";
  740. }
  741. ientry.order = "false";
  742. // Filter
  743. ientry.filter = kxml_node_attr(element, "filter");
  744. if (ientry.filter) {
  745. is_filter = BOOL_TRUE;
  746. } else {
  747. if (KTAG_FILTER == tag)
  748. ientry.filter = "true";
  749. else
  750. ientry.filter = "false";
  751. }
  752. if (!(entry = add_entry_to_hierarchy(element, parent, &ientry, error)))
  753. goto err;
  754. if (!process_children(element, entry, error))
  755. goto err;
  756. // Add special PTYPE for command. It uses symbol from internal klish
  757. // plugin.
  758. // Iterate child entries to find out is there PTYPE entry already. We
  759. // can't use kentry_nested_by_purpose() because it's not calculated yet.
  760. iter = kentry_entrys_iter(entry);
  761. while ((nested_entry = kentry_entrys_each(&iter))) {
  762. if (kentry_purpose(nested_entry) == KENTRY_PURPOSE_PTYPE) {
  763. ptype_exists = BOOL_TRUE;
  764. break;
  765. }
  766. }
  767. if (!ptype_exists) {
  768. kentry_t *ptype_entry = create_ptype(
  769. "COMMAND@klish",
  770. "completion_COMMAND@klish",
  771. "help_COMMAND@klish",
  772. NULL);
  773. assert(ptype_entry);
  774. kentry_add_entrys(entry, ptype_entry);
  775. }
  776. res = BOOL_TRUE;
  777. err:
  778. if (is_name)
  779. kxml_node_attr_free(ientry.name);
  780. kxml_node_attr_free(ientry.help);
  781. kxml_node_attr_free(ientry.mode);
  782. kxml_node_attr_free(ientry.min);
  783. kxml_node_attr_free(ientry.max);
  784. kxml_node_attr_free(ientry.ref);
  785. if ((KTAG_FILTER == tag) || (KTAG_COMMAND == tag)) {
  786. kxml_node_attr_free(ientry.value);
  787. kxml_node_attr_free(ientry.restore);
  788. }
  789. if (is_filter)
  790. kxml_node_attr_free(ientry.filter);
  791. return res;
  792. }
  793. static bool_t process_hotkey(const kxml_node_t *element, void *parent,
  794. faux_error_t *error)
  795. {
  796. ihotkey_t ihotkey = {};
  797. khotkey_t *hotkey = NULL;
  798. bool_t res = BOOL_FALSE;
  799. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  800. kentry_t *parent_entry = (kentry_t *)parent;
  801. ihotkey.key = kxml_node_attr(element, "key");
  802. if (!ihotkey.key) {
  803. faux_error_sprintf(error, TAG": hotkey without \"key\" attribute");
  804. return BOOL_FALSE;
  805. }
  806. ihotkey.cmd = kxml_node_attr(element, "cmd");
  807. if (!ihotkey.cmd) {
  808. faux_error_sprintf(error, TAG": hotkey without \"cmd\" attribute");
  809. return BOOL_FALSE;
  810. }
  811. hotkey = ihotkey_load(&ihotkey, error);
  812. if (!hotkey)
  813. goto err;
  814. if ( (KTAG_ENTRY != parent_tag) &&
  815. (KTAG_VIEW != parent_tag)) {
  816. faux_error_sprintf(error,
  817. TAG": Tag \"%s\" can't contain HOTKEY tag",
  818. kxml_tag_name(parent_tag));
  819. khotkey_free(hotkey);
  820. goto err;
  821. }
  822. if (!kentry_add_hotkeys(parent_entry, hotkey)) {
  823. faux_error_sprintf(error,
  824. TAG": Can't add HOTKEY \"%s\" to ENTRY \"%s\". "
  825. "Probably duplication",
  826. khotkey_key(hotkey),
  827. kentry_name(parent_entry));
  828. khotkey_free(hotkey);
  829. goto err;
  830. }
  831. // HOTKEY doesn't have children
  832. res = BOOL_TRUE;
  833. err:
  834. kxml_node_attr_free(ihotkey.key);
  835. kxml_node_attr_free(ihotkey.cmd);
  836. return res;
  837. }