load.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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_COND != parent_tag) &&
  328. (KTAG_COMPL != parent_tag) &&
  329. (KTAG_HELP != parent_tag) &&
  330. (KTAG_PROMPT != parent_tag) &&
  331. (KTAG_PTYPE != parent_tag)) {
  332. faux_error_sprintf(error,
  333. TAG": Tag \"%s\" can't contain ACTION tag",
  334. kxml_tag_name(parent_tag));
  335. kaction_free(action);
  336. goto err;
  337. }
  338. if (!kentry_add_actions(parent_entry, action)) {
  339. faux_error_sprintf(error,
  340. TAG": Can't add ACTION #%d to ENTRY \"%s\". "
  341. "Probably duplication",
  342. kentry_actions_len(parent_entry) + 1,
  343. kentry_name(parent_entry));
  344. kaction_free(action);
  345. goto err;
  346. }
  347. if (!process_children(element, action, error))
  348. goto err;
  349. res = BOOL_TRUE;
  350. err:
  351. kxml_node_attr_free(iaction.sym);
  352. kxml_node_attr_free(iaction.lock);
  353. kxml_node_attr_free(iaction.interrupt);
  354. kxml_node_attr_free(iaction.interactive);
  355. kxml_node_attr_free(iaction.exec_on);
  356. kxml_node_attr_free(iaction.update_retcode);
  357. kxml_node_attr_free(iaction.permanent);
  358. kxml_node_attr_free(iaction.sync);
  359. kxml_node_content_free(iaction.script);
  360. return res;
  361. }
  362. static kentry_t *add_entry_to_hierarchy(const kxml_node_t *element, void *parent,
  363. ientry_t *ientry, faux_error_t *error)
  364. {
  365. kentry_t *entry = NULL;
  366. ktags_e tag = kxml_node_tag(element);
  367. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  368. assert(ientry);
  369. // Parent is mandatory field
  370. if (!parent) {
  371. faux_error_sprintf(error,
  372. TAG": Broken parent object for entry \"%s\"",
  373. ientry->name);
  374. return NULL;
  375. }
  376. if ((parent_tag == KTAG_ACTION) ||
  377. // (parent_tag == KTAG_HOTKEY) ||
  378. (parent_tag == KTAG_PLUGIN)) {
  379. faux_error_sprintf(error,
  380. TAG": Tag \"%s\" can't contain %s tag \"%s\"",
  381. kxml_tag_name(parent_tag),
  382. kxml_tag_name(tag), ientry->name);
  383. return NULL;
  384. }
  385. // High level ENTRY
  386. if (KTAG_KLISH == parent_tag) {
  387. kscheme_t *scheme = (kscheme_t *)parent;
  388. // Does such ENTRY already exist
  389. entry = kscheme_find_entry(scheme, ientry->name);
  390. if (entry) {
  391. if (!ientry_parse(ientry, entry, error))
  392. return NULL;
  393. } else { // New entry object
  394. entry = ientry_load(ientry, error);
  395. if (!entry)
  396. return NULL;
  397. if (!kscheme_add_entrys(scheme, entry)) {
  398. faux_error_sprintf(error, TAG": Can't add entry \"%s\". "
  399. "Probably duplication",
  400. kentry_name(entry));
  401. kentry_free(entry);
  402. return NULL;
  403. }
  404. }
  405. // ENTRY within ENTRY
  406. } else {
  407. kentry_t *parent_entry = (kentry_t *)parent;
  408. // Does such ENTRY already exist
  409. entry = kentry_find_entry(parent_entry, ientry->name);
  410. if (entry) {
  411. if (!ientry_parse(ientry, entry, error))
  412. return NULL;
  413. } else { // New entry object
  414. entry = ientry_load(ientry, error);
  415. if (!entry)
  416. return NULL;
  417. kentry_set_parent(entry, parent_entry);
  418. if (!kentry_add_entrys(parent_entry, entry)) {
  419. faux_error_sprintf(error, TAG": Can't add entry \"%s\". "
  420. "Probably duplication",
  421. kentry_name(entry));
  422. kentry_free(entry);
  423. return NULL;
  424. }
  425. }
  426. }
  427. return entry;
  428. }
  429. static bool_t process_entry(const kxml_node_t *element, void *parent,
  430. faux_error_t *error)
  431. {
  432. ientry_t ientry = {};
  433. kentry_t *entry = NULL;
  434. bool_t res = BOOL_FALSE;
  435. // Mandatory entry name
  436. ientry.name = kxml_node_attr(element, "name");
  437. if (!ientry.name) {
  438. faux_error_sprintf(error, TAG": entry without name");
  439. return BOOL_FALSE;
  440. }
  441. ientry.help = kxml_node_attr(element, "help");
  442. ientry.container = kxml_node_attr(element, "container");
  443. ientry.mode = kxml_node_attr(element, "mode");
  444. ientry.purpose = kxml_node_attr(element, "purpose");
  445. ientry.min = kxml_node_attr(element, "min");
  446. ientry.max = kxml_node_attr(element, "max");
  447. ientry.ref = kxml_node_attr(element, "ref");
  448. ientry.value = kxml_node_attr(element, "value");
  449. ientry.restore = kxml_node_attr(element, "restore");
  450. ientry.order = kxml_node_attr(element, "order");
  451. ientry.filter = kxml_node_attr(element, "filter");
  452. if (!(entry = add_entry_to_hierarchy(element, parent, &ientry, error)))
  453. goto err;
  454. if (!process_children(element, entry, error))
  455. goto err;
  456. res = BOOL_TRUE;
  457. err:
  458. kxml_node_attr_free(ientry.name);
  459. kxml_node_attr_free(ientry.help);
  460. kxml_node_attr_free(ientry.container);
  461. kxml_node_attr_free(ientry.mode);
  462. kxml_node_attr_free(ientry.purpose);
  463. kxml_node_attr_free(ientry.min);
  464. kxml_node_attr_free(ientry.max);
  465. kxml_node_attr_free(ientry.ref);
  466. kxml_node_attr_free(ientry.value);
  467. kxml_node_attr_free(ientry.restore);
  468. kxml_node_attr_free(ientry.order);
  469. kxml_node_attr_free(ientry.filter);
  470. return res;
  471. }
  472. static bool_t process_view(const kxml_node_t *element, void *parent,
  473. faux_error_t *error)
  474. {
  475. ientry_t ientry = {};
  476. kentry_t *entry = NULL;
  477. bool_t res = BOOL_FALSE;
  478. // Mandatory VIEW name
  479. ientry.name = kxml_node_attr(element, "name");
  480. if (!ientry.name) {
  481. faux_error_sprintf(error, TAG": VIEW without name");
  482. return BOOL_FALSE;
  483. }
  484. ientry.help = kxml_node_attr(element, "help");
  485. ientry.container = "true";
  486. ientry.mode = "switch";
  487. ientry.purpose = "common";
  488. ientry.min = "1";
  489. ientry.max = "1";
  490. ientry.ref = kxml_node_attr(element, "ref");
  491. ientry.value = NULL;
  492. ientry.restore = "false";
  493. ientry.order = "false";
  494. ientry.filter = "false";
  495. if (!(entry = add_entry_to_hierarchy(element, parent, &ientry, error)))
  496. goto err;
  497. if (!process_children(element, entry, error))
  498. goto err;
  499. res = BOOL_TRUE;
  500. err:
  501. kxml_node_attr_free(ientry.name);
  502. kxml_node_attr_free(ientry.help);
  503. kxml_node_attr_free(ientry.ref);
  504. return res;
  505. }
  506. static bool_t process_ptype(const kxml_node_t *element, void *parent,
  507. faux_error_t *error)
  508. {
  509. ientry_t ientry = {};
  510. kentry_t *entry = NULL;
  511. bool_t res = BOOL_FALSE;
  512. bool_t is_name = BOOL_FALSE;
  513. // Mandatory PTYPE name or reference
  514. ientry.name = kxml_node_attr(element, "name");
  515. ientry.ref = kxml_node_attr(element, "ref");
  516. if (ientry.name) {
  517. is_name = BOOL_TRUE;
  518. } else {
  519. if (!ientry.ref) {
  520. faux_error_sprintf(error,
  521. TAG": PTYPE without name or reference");
  522. return BOOL_FALSE;
  523. }
  524. ientry.name = "__ptype";
  525. }
  526. ientry.help = kxml_node_attr(element, "help");
  527. ientry.container = "true";
  528. ientry.mode = "sequence";
  529. ientry.purpose = "ptype";
  530. ientry.min = "1";
  531. ientry.max = "1";
  532. ientry.value = kxml_node_attr(element, "value");
  533. ientry.restore = "false";
  534. ientry.order = "true";
  535. ientry.filter = "false";
  536. if (!(entry = add_entry_to_hierarchy(element, parent, &ientry, error)))
  537. goto err;
  538. if (!process_children(element, entry, error))
  539. goto err;
  540. res = BOOL_TRUE;
  541. err:
  542. if (is_name)
  543. kxml_node_attr_free(ientry.name);
  544. kxml_node_attr_free(ientry.help);
  545. kxml_node_attr_free(ientry.ref);
  546. kxml_node_attr_free(ientry.value);
  547. return res;
  548. }
  549. static kentry_t *create_ptype(const char *ptype,
  550. const char *compl, const char *help, const char *ref)
  551. {
  552. kentry_t *ptype_entry = NULL;
  553. if (!ptype && !ref)
  554. return NULL;
  555. ptype_entry = kentry_new("__ptype");
  556. assert(ptype_entry);
  557. kentry_set_purpose(ptype_entry, KENTRY_PURPOSE_PTYPE);
  558. if (ref) {
  559. kentry_set_ref_str(ptype_entry, ref);
  560. // If ptype is reference then another actions is not needed.
  561. return ptype_entry;
  562. } else {
  563. kaction_t *ptype_action = kaction_new();
  564. assert(ptype_action);
  565. kaction_set_sym_ref(ptype_action, ptype);
  566. kaction_set_permanent(ptype_action, TRI_TRUE);
  567. kentry_add_actions(ptype_entry, ptype_action);
  568. }
  569. if (compl) {
  570. kentry_t *compl_entry = NULL;
  571. kaction_t *compl_action = NULL;
  572. compl_action = kaction_new();
  573. assert(compl_action);
  574. kaction_set_sym_ref(compl_action, compl);
  575. kaction_set_permanent(compl_action, TRI_TRUE);
  576. compl_entry = kentry_new("__compl");
  577. assert(compl_entry);
  578. kentry_set_purpose(compl_entry, KENTRY_PURPOSE_COMPLETION);
  579. kentry_add_actions(compl_entry, compl_action);
  580. kentry_add_entrys(ptype_entry, compl_entry);
  581. }
  582. if (help) {
  583. kentry_t *help_entry = NULL;
  584. kaction_t *help_action = NULL;
  585. help_action = kaction_new();
  586. assert(help_action);
  587. kaction_set_sym_ref(help_action, help);
  588. kaction_set_permanent(help_action, TRI_TRUE);
  589. help_entry = kentry_new("__help");
  590. assert(help_entry);
  591. kentry_set_purpose(help_entry, KENTRY_PURPOSE_HELP);
  592. kentry_add_actions(help_entry, help_action);
  593. kentry_add_entrys(ptype_entry, help_entry);
  594. }
  595. return ptype_entry;
  596. }
  597. // PARAM, SWITCH, SEQ
  598. static bool_t process_param(const kxml_node_t *element, void *parent,
  599. faux_error_t *error)
  600. {
  601. ientry_t ientry = {};
  602. kentry_t *entry = NULL;
  603. bool_t res = BOOL_FALSE;
  604. ktags_e tag = kxml_node_tag(element);
  605. bool_t is_mode = BOOL_FALSE;
  606. char *ptype_str = NULL;
  607. // Mandatory PARAM name
  608. ientry.name = kxml_node_attr(element, "name");
  609. if (!ientry.name) {
  610. faux_error_sprintf(error, TAG": PARAM without name");
  611. return BOOL_FALSE;
  612. }
  613. ientry.help = kxml_node_attr(element, "help");
  614. // Container
  615. if (KTAG_PARAM == tag)
  616. ientry.container = "false";
  617. else
  618. ientry.container = "true"; // SWITCH, SEQ
  619. // Mode
  620. switch (tag) {
  621. case KTAG_PARAM:
  622. ientry.mode = kxml_node_attr(element, "mode");
  623. is_mode = BOOL_TRUE;
  624. break;
  625. case KTAG_SWITCH:
  626. ientry.mode = "switch";
  627. break;
  628. case KTAG_SEQ:
  629. ientry.mode = "sequence";
  630. break;
  631. default:
  632. ientry.mode = "empty";
  633. break;
  634. }
  635. ientry.purpose = "common";
  636. ientry.min = kxml_node_attr(element, "min");
  637. ientry.max = kxml_node_attr(element, "max");
  638. ientry.ref = kxml_node_attr(element, "ref");
  639. ientry.value = kxml_node_attr(element, "value");
  640. ientry.restore = "false";
  641. ientry.order = kxml_node_attr(element, "order");
  642. ientry.filter = "false";
  643. if (!(entry = add_entry_to_hierarchy(element, parent, &ientry, error)))
  644. goto err;
  645. // Special attribute "ptype". It exists for more simple XML only. It
  646. // just links existing PTYPE. User can to don't specify nested tag PTYPE.
  647. ptype_str = kxml_node_attr(element, "ptype");
  648. if (ptype_str) {
  649. kentry_t *ptype_entry = create_ptype(NULL, NULL, NULL, ptype_str);
  650. assert(ptype_entry);
  651. kentry_add_entrys(entry, ptype_entry);
  652. }
  653. if (!process_children(element, entry, error))
  654. goto err;
  655. res = BOOL_TRUE;
  656. err:
  657. kxml_node_attr_free(ientry.name);
  658. kxml_node_attr_free(ientry.help);
  659. if (is_mode)
  660. kxml_node_attr_free(ientry.mode);
  661. kxml_node_attr_free(ientry.min);
  662. kxml_node_attr_free(ientry.max);
  663. kxml_node_attr_free(ientry.ref);
  664. kxml_node_attr_free(ientry.value);
  665. kxml_node_attr_free(ientry.order);
  666. kxml_node_attr_free(ptype_str);
  667. return res;
  668. }
  669. // COMMAND, FILTER, COND, COMPL, HELP, PROMPT
  670. static bool_t process_command(const kxml_node_t *element, void *parent,
  671. faux_error_t *error)
  672. {
  673. ientry_t ientry = {};
  674. kentry_t *entry = NULL;
  675. bool_t res = BOOL_FALSE;
  676. ktags_e tag = kxml_node_tag(element);
  677. bool_t is_name = BOOL_FALSE;
  678. kentry_entrys_node_t *iter = NULL;
  679. kentry_t *nested_entry = NULL;
  680. bool_t ptype_exists = BOOL_FALSE;
  681. // Mandatory COMMAND name
  682. ientry.name = kxml_node_attr(element, "name");
  683. if (ientry.name) {
  684. is_name = BOOL_TRUE;
  685. } else {
  686. switch (tag) {
  687. case KTAG_COMMAND:
  688. case KTAG_FILTER:
  689. faux_error_sprintf(error, TAG": COMMAND without name");
  690. return BOOL_FALSE;
  691. case KTAG_COND:
  692. ientry.name = "__cond";
  693. break;
  694. case KTAG_COMPL:
  695. ientry.name = "__compl";
  696. break;
  697. case KTAG_HELP:
  698. ientry.name = "__help";
  699. break;
  700. case KTAG_PROMPT:
  701. ientry.name = "__prompt";
  702. break;
  703. default:
  704. faux_error_sprintf(error, TAG": Unknown tag");
  705. return BOOL_FALSE;
  706. }
  707. }
  708. ientry.help = kxml_node_attr(element, "help");
  709. ientry.container = "false";
  710. ientry.mode = kxml_node_attr(element, "mode");
  711. // Purpose
  712. switch (tag) {
  713. case KTAG_COND:
  714. ientry.purpose = "cond";
  715. break;
  716. case KTAG_COMPL:
  717. ientry.purpose = "completion";
  718. break;
  719. case KTAG_HELP:
  720. ientry.purpose = "help";
  721. break;
  722. case KTAG_PROMPT:
  723. ientry.purpose = "prompt";
  724. break;
  725. default:
  726. ientry.purpose = "common";
  727. break;
  728. }
  729. ientry.min = "1";
  730. ientry.max = "1";
  731. ientry.ref = kxml_node_attr(element, "ref");
  732. if ((KTAG_FILTER == tag) || (KTAG_COMMAND == tag)) {
  733. ientry.value = kxml_node_attr(element, "value");
  734. ientry.restore = kxml_node_attr(element, "restore");
  735. } else {
  736. ientry.value = NULL;
  737. ientry.restore = "false";
  738. }
  739. ientry.order = "false";
  740. // Filter
  741. if (KTAG_FILTER == tag)
  742. ientry.filter = "true";
  743. else
  744. ientry.filter = "false";
  745. if (!(entry = add_entry_to_hierarchy(element, parent, &ientry, error)))
  746. goto err;
  747. if (!process_children(element, entry, error))
  748. goto err;
  749. // Add special PTYPE for command. It uses symbol from internal klish
  750. // plugin.
  751. // Iterate child entries to find out is there PTYPE entry already. We
  752. // can't use kentry_nested_by_purpose() because it's not calculated yet.
  753. iter = kentry_entrys_iter(entry);
  754. while ((nested_entry = kentry_entrys_each(&iter))) {
  755. if (kentry_purpose(nested_entry) == KENTRY_PURPOSE_PTYPE) {
  756. ptype_exists = BOOL_TRUE;
  757. break;
  758. }
  759. }
  760. if (!ptype_exists) {
  761. kentry_t *ptype_entry = create_ptype(
  762. "COMMAND@klish",
  763. "completion_COMMAND@klish",
  764. "help_COMMAND@klish",
  765. NULL);
  766. assert(ptype_entry);
  767. kentry_add_entrys(entry, ptype_entry);
  768. }
  769. res = BOOL_TRUE;
  770. err:
  771. if (is_name)
  772. kxml_node_attr_free(ientry.name);
  773. kxml_node_attr_free(ientry.help);
  774. kxml_node_attr_free(ientry.mode);
  775. kxml_node_attr_free(ientry.ref);
  776. if ((KTAG_FILTER == tag) || (KTAG_COMMAND == tag)) {
  777. kxml_node_attr_free(ientry.value);
  778. kxml_node_attr_free(ientry.restore);
  779. }
  780. return res;
  781. }
  782. static bool_t process_hotkey(const kxml_node_t *element, void *parent,
  783. faux_error_t *error)
  784. {
  785. ihotkey_t ihotkey = {};
  786. khotkey_t *hotkey = NULL;
  787. bool_t res = BOOL_FALSE;
  788. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  789. kentry_t *parent_entry = (kentry_t *)parent;
  790. ihotkey.key = kxml_node_attr(element, "key");
  791. if (!ihotkey.key) {
  792. faux_error_sprintf(error, TAG": hotkey without \"key\" attribute");
  793. return BOOL_FALSE;
  794. }
  795. ihotkey.cmd = kxml_node_attr(element, "cmd");
  796. if (!ihotkey.cmd) {
  797. faux_error_sprintf(error, TAG": hotkey without \"cmd\" attribute");
  798. return BOOL_FALSE;
  799. }
  800. hotkey = ihotkey_load(&ihotkey, error);
  801. if (!hotkey)
  802. goto err;
  803. if ( (KTAG_ENTRY != parent_tag) &&
  804. (KTAG_VIEW != parent_tag)) {
  805. faux_error_sprintf(error,
  806. TAG": Tag \"%s\" can't contain HOTKEY tag",
  807. kxml_tag_name(parent_tag));
  808. khotkey_free(hotkey);
  809. goto err;
  810. }
  811. if (!kentry_add_hotkeys(parent_entry, hotkey)) {
  812. faux_error_sprintf(error,
  813. TAG": Can't add HOTKEY \"%s\" to ENTRY \"%s\". "
  814. "Probably duplication",
  815. khotkey_key(hotkey),
  816. kentry_name(parent_entry));
  817. khotkey_free(hotkey);
  818. goto err;
  819. }
  820. // HOTKEY doesn't have children
  821. res = BOOL_TRUE;
  822. err:
  823. kxml_node_attr_free(ihotkey.key);
  824. kxml_node_attr_free(ihotkey.cmd);
  825. return res;
  826. }