load.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. // Different TAGs types
  32. typedef enum {
  33. KTAG_NONE,
  34. KTAG_ACTION,
  35. KTAG_PARAM,
  36. KTAG_SWITCH, // PARAM alias
  37. KTAG_SUBCOMMAND, // PARAM alias
  38. KTAG_MULTI, // 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_MAX,
  50. } ktags_e;
  51. static const char * const kxml_tags[] = {
  52. NULL,
  53. "ACTION",
  54. "PARAM",
  55. "SWITCH",
  56. "SUBCOMMAND",
  57. "MULTI",
  58. "COMMAND",
  59. "FILTER",
  60. "VIEW",
  61. "PTYPE",
  62. "PLUGIN",
  63. "KLISH",
  64. "ENTRY",
  65. "COND",
  66. "COMPL",
  67. "HELP",
  68. };
  69. static kxml_process_fn *kxml_handlers[] = {
  70. NULL,
  71. process_action,
  72. process_param,
  73. process_param,
  74. process_param,
  75. process_param,
  76. process_command,
  77. process_command,
  78. process_view,
  79. process_ptype,
  80. process_plugin,
  81. process_klish,
  82. process_entry,
  83. process_command,
  84. process_command,
  85. process_command,
  86. };
  87. static const char *kxml_tag_name(ktags_e tag)
  88. {
  89. if ((KTAG_NONE == tag) || (tag >= KTAG_MAX))
  90. return "NONE";
  91. return kxml_tags[tag];
  92. }
  93. static ktags_e kxml_node_tag(const kxml_node_t *node)
  94. {
  95. ktags_e tag = KTAG_NONE;
  96. char *name = NULL;
  97. if (!node)
  98. return KTAG_NONE;
  99. if (kxml_node_type(node) != KXML_NODE_ELM)
  100. return KTAG_NONE;
  101. name = kxml_node_name(node);
  102. if (!name)
  103. return KTAG_NONE; // Strange case
  104. for (tag = (KTAG_NONE + 1); tag < KTAG_MAX; tag++) {
  105. if (faux_str_casecmp(name, kxml_tags[tag]) == 0)
  106. break;
  107. }
  108. kxml_node_name_free(name);
  109. if (tag >= KTAG_MAX)
  110. return KTAG_NONE;
  111. return tag;
  112. }
  113. static kxml_process_fn *kxml_node_handler(const kxml_node_t *node)
  114. {
  115. return kxml_handlers[kxml_node_tag(node)];
  116. }
  117. /** @brief Reads an element from the XML stream and processes it.
  118. */
  119. static bool_t process_node(const kxml_node_t *node, void *parent, faux_error_t *error)
  120. {
  121. kxml_process_fn *handler = NULL;
  122. // Process only KXML_NODE_ELM. Don't process other types like:
  123. // KXML_NODE_DOC,
  124. // KXML_NODE_TEXT,
  125. // KXML_NODE_ATTR,
  126. // KXML_NODE_COMMENT,
  127. // KXML_NODE_PI,
  128. // KXML_NODE_DECL,
  129. // KXML_NODE_UNKNOWN,
  130. if (kxml_node_type(node) != KXML_NODE_ELM)
  131. return BOOL_TRUE;
  132. handler = kxml_node_handler(node);
  133. if (!handler) { // Unknown element
  134. faux_error_sprintf(error,
  135. TAG": Unknown tag \"%s\"", kxml_node_name(node));
  136. return BOOL_FALSE;
  137. }
  138. #ifdef KXML_DEBUG
  139. printf("kxml: Tag \"%s\"\n", kxml_node_name(node));
  140. #endif
  141. return handler(node, parent, error);
  142. }
  143. static bool_t kxml_load_file(kscheme_t *scheme, const char *filename,
  144. faux_error_t *error)
  145. {
  146. kxml_doc_t *doc = NULL;
  147. kxml_node_t *root = NULL;
  148. bool_t r = BOOL_FALSE;
  149. if (!scheme)
  150. return BOOL_FALSE;
  151. if (!filename)
  152. return BOOL_FALSE;
  153. #ifdef KXML_DEBUG
  154. printf("kxml: Processing XML file \"%s\"\n", filename);
  155. #endif
  156. doc = kxml_doc_read(filename);
  157. if (!kxml_doc_is_valid(doc)) {
  158. /* int errcaps = kxml_doc_error_caps(doc);
  159. printf("Unable to open file '%s'", filename);
  160. if ((errcaps & kxml_ERR_LINE) == kxml_ERR_LINE)
  161. printf(", at line %d", kxml_doc_err_line(doc));
  162. if ((errcaps & kxml_ERR_COL) == kxml_ERR_COL)
  163. printf(", at column %d", kxml_doc_err_col(doc));
  164. if ((errcaps & kxml_ERR_DESC) == kxml_ERR_DESC)
  165. printf(", message is %s", kxml_doc_err_msg(doc));
  166. printf("\n");
  167. */ kxml_doc_release(doc);
  168. return BOOL_FALSE;
  169. }
  170. root = kxml_doc_root(doc);
  171. r = process_node(root, scheme, error);
  172. kxml_doc_release(doc);
  173. if (!r) {
  174. faux_error_sprintf(error, TAG": Illegal file %s", filename);
  175. return BOOL_FALSE;
  176. }
  177. return BOOL_TRUE;
  178. }
  179. /** @brief Default path to get XML files from.
  180. */
  181. static const char *default_path = "/etc/klish;~/.klish";
  182. static const char *path_separators = ":;";
  183. bool_t kxml_load_scheme(kscheme_t *scheme, const char *xml_path,
  184. faux_error_t *error)
  185. {
  186. char *path = NULL;
  187. char *fn = NULL;
  188. char *saveptr = NULL;
  189. bool_t ret = BOOL_TRUE;
  190. assert(scheme);
  191. if (!scheme)
  192. return BOOL_FALSE;
  193. // Use the default path if xml path is not specified.
  194. // Dup is needed because sring will be tokenized but
  195. // the xml_path is must be const.
  196. if (!xml_path)
  197. path = faux_str_dup(default_path);
  198. else
  199. path = faux_str_dup(xml_path);
  200. #ifdef KXML_DEBUG
  201. printf("kxml: Loading scheme \"%s\"\n", path);
  202. #endif
  203. // Loop through each directory
  204. for (fn = strtok_r(path, path_separators, &saveptr);
  205. fn; fn = strtok_r(NULL, path_separators, &saveptr)) {
  206. DIR *dir = NULL;
  207. struct dirent *entry = NULL;
  208. char *realpath = NULL;
  209. // Expand tilde. Tilde must be the first symbol.
  210. realpath = faux_expand_tilde(fn);
  211. // Regular file
  212. if (faux_isfile(realpath)) {
  213. if (!kxml_load_file(scheme, realpath, error))
  214. ret = BOOL_FALSE;
  215. faux_str_free(realpath);
  216. continue;
  217. }
  218. // Search this directory for any XML files
  219. #ifdef KXML_DEBUG
  220. printf("kxml: Processing XML dir \"%s\"\n", realpath);
  221. #endif
  222. dir = opendir(realpath);
  223. if (!dir) {
  224. faux_str_free(realpath);
  225. continue;
  226. }
  227. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  228. const char *extension = strrchr(entry->d_name, '.');
  229. char *filename = NULL;
  230. // Check the filename
  231. if (!extension || strcmp(".xml", extension))
  232. continue;
  233. filename = faux_str_sprintf("%s/%s", realpath, entry->d_name);
  234. if (!kxml_load_file(scheme, filename, error))
  235. ret = BOOL_FALSE;
  236. faux_str_free(filename);
  237. }
  238. closedir(dir);
  239. faux_str_free(realpath);
  240. }
  241. faux_str_free(path);
  242. return ret;
  243. }
  244. /** @brief Iterate through element's children.
  245. */
  246. static bool_t process_children(const kxml_node_t *element, void *parent,
  247. faux_error_t *error)
  248. {
  249. const kxml_node_t *node = NULL;
  250. while ((node = kxml_node_next_child(element, node)) != NULL) {
  251. bool_t res = BOOL_FALSE;
  252. res = process_node(node, parent, error);
  253. if (!res)
  254. return res;
  255. }
  256. return BOOL_TRUE;
  257. }
  258. static bool_t process_klish(const kxml_node_t *element, void *parent,
  259. faux_error_t *error)
  260. {
  261. return process_children(element, parent, error);
  262. }
  263. static bool_t process_plugin(const kxml_node_t *element, void *parent,
  264. faux_error_t *error)
  265. {
  266. iplugin_t iplugin = {};
  267. kplugin_t *plugin = NULL;
  268. bool_t res = BOOL_FALSE;
  269. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  270. if (parent_tag != KTAG_KLISH) {
  271. faux_error_sprintf(error,
  272. TAG": Tag \"%s\" can't contain PLUGIN tag",
  273. kxml_tag_name(parent_tag));
  274. return BOOL_FALSE;
  275. }
  276. iplugin.name = kxml_node_attr(element, "name");
  277. iplugin.id = kxml_node_attr(element, "id");
  278. iplugin.file = kxml_node_attr(element, "file");
  279. iplugin.conf = kxml_node_content(element);
  280. plugin = iplugin_load(&iplugin, error);
  281. if (!plugin)
  282. goto err;
  283. if (!kscheme_add_plugins((kscheme_t *)parent, plugin)) {
  284. faux_error_sprintf(error, TAG": Can't add PLUGIN \"%s\". "
  285. "Probably duplication",
  286. kplugin_name(plugin));
  287. kplugin_free(plugin);
  288. goto err;
  289. }
  290. if (!process_children(element, plugin, error))
  291. goto err;
  292. res = BOOL_TRUE;
  293. err:
  294. kxml_node_attr_free(iplugin.name);
  295. kxml_node_attr_free(iplugin.id);
  296. kxml_node_attr_free(iplugin.file);
  297. kxml_node_content_free(iplugin.conf);
  298. return res;
  299. }
  300. static bool_t process_action(const kxml_node_t *element, void *parent,
  301. faux_error_t *error)
  302. {
  303. iaction_t iaction = {};
  304. kaction_t *action = NULL;
  305. bool_t res = BOOL_FALSE;
  306. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  307. kentry_t *parent_entry = (kentry_t *)parent;
  308. iaction.sym = kxml_node_attr(element, "sym");
  309. iaction.lock = kxml_node_attr(element, "lock");
  310. iaction.interrupt = kxml_node_attr(element, "interrupt");
  311. iaction.interactive = kxml_node_attr(element, "interactive");
  312. iaction.exec_on = kxml_node_attr(element, "exec_on");
  313. iaction.update_retcode = kxml_node_attr(element, "update_retcode");
  314. iaction.permanent = kxml_node_attr(element, "permanent");
  315. iaction.sync = kxml_node_attr(element, "sync");
  316. iaction.script = kxml_node_content(element);
  317. action = iaction_load(&iaction, error);
  318. if (!action)
  319. goto err;
  320. if ( (KTAG_ENTRY != parent_tag) &&
  321. (KTAG_COMMAND != parent_tag) &&
  322. (KTAG_SUBCOMMAND != parent_tag) &&
  323. (KTAG_FILTER != parent_tag) &&
  324. (KTAG_COND != parent_tag) &&
  325. (KTAG_COMPL != parent_tag) &&
  326. (KTAG_HELP != parent_tag) &&
  327. (KTAG_PTYPE != parent_tag)) {
  328. faux_error_sprintf(error,
  329. TAG": Tag \"%s\" can't contain ACTION tag",
  330. kxml_tag_name(parent_tag));
  331. kaction_free(action);
  332. goto err;
  333. }
  334. if (!kentry_add_actions(parent_entry, action)) {
  335. faux_error_sprintf(error,
  336. TAG": Can't add ACTION #%d to ENTRY \"%s\". "
  337. "Probably duplication",
  338. kentry_actions_len(parent_entry) + 1,
  339. kentry_name(parent_entry));
  340. kaction_free(action);
  341. goto err;
  342. }
  343. if (!process_children(element, action, error))
  344. goto err;
  345. res = BOOL_TRUE;
  346. err:
  347. kxml_node_attr_free(iaction.sym);
  348. kxml_node_attr_free(iaction.lock);
  349. kxml_node_attr_free(iaction.interrupt);
  350. kxml_node_attr_free(iaction.interactive);
  351. kxml_node_attr_free(iaction.exec_on);
  352. kxml_node_attr_free(iaction.update_retcode);
  353. kxml_node_attr_free(iaction.permanent);
  354. kxml_node_attr_free(iaction.sync);
  355. kxml_node_content_free(iaction.script);
  356. return res;
  357. }
  358. static kentry_t *add_entry_to_hierarchy(ktags_e parent_tag, void *parent,
  359. ientry_t *ientry, faux_error_t *error)
  360. {
  361. kentry_t *entry = NULL;
  362. assert(ientry);
  363. // Parent is mandatory field
  364. if (!parent) {
  365. faux_error_sprintf(error,
  366. TAG": Broken parent object for entry \"%s\"",
  367. ientry->name);
  368. return NULL;
  369. }
  370. // High level ENTRY
  371. if (KTAG_KLISH == parent_tag) {
  372. kscheme_t *scheme = (kscheme_t *)parent;
  373. // Does such ENTRY already exist
  374. entry = kscheme_find_entry(scheme, ientry->name);
  375. if (entry) {
  376. if (!ientry_parse(ientry, entry, error))
  377. return NULL;
  378. } else { // New entry object
  379. entry = ientry_load(ientry, error);
  380. if (!entry)
  381. return NULL;
  382. if (!kscheme_add_entrys(scheme, entry)) {
  383. faux_error_sprintf(error, TAG": Can't add entry \"%s\". "
  384. "Probably duplication",
  385. kentry_name(entry));
  386. kentry_free(entry);
  387. return NULL;
  388. }
  389. }
  390. // ENTRY within ENTRY
  391. } else {
  392. kentry_t *parent_entry = (kentry_t *)parent;
  393. // Does such ENTRY already exist
  394. entry = kentry_find_entry(parent_entry, ientry->name);
  395. if (entry) {
  396. if (!ientry_parse(ientry, entry, error))
  397. return NULL;
  398. } else { // New entry object
  399. entry = ientry_load(ientry, error);
  400. if (!entry)
  401. return NULL;
  402. kentry_set_parent(entry, parent_entry);
  403. if (!kentry_add_entrys(parent_entry, entry)) {
  404. faux_error_sprintf(error, TAG": Can't add entry \"%s\". "
  405. "Probably duplication",
  406. kentry_name(entry));
  407. kentry_free(entry);
  408. return NULL;
  409. }
  410. }
  411. }
  412. return entry;
  413. }
  414. static bool_t process_entry(const kxml_node_t *element, void *parent,
  415. faux_error_t *error)
  416. {
  417. ientry_t ientry = {};
  418. kentry_t *entry = NULL;
  419. bool_t res = BOOL_FALSE;
  420. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  421. // Mandatory entry name
  422. ientry.name = kxml_node_attr(element, "name");
  423. if (!ientry.name) {
  424. faux_error_sprintf(error, TAG": entry without name");
  425. return BOOL_FALSE;
  426. }
  427. ientry.help = kxml_node_attr(element, "help");
  428. ientry.container = kxml_node_attr(element, "container");
  429. ientry.mode = kxml_node_attr(element, "mode");
  430. ientry.purpose = kxml_node_attr(element, "purpose");
  431. ientry.min = kxml_node_attr(element, "min");
  432. ientry.max = kxml_node_attr(element, "max");
  433. ientry.ref = kxml_node_attr(element, "ref");
  434. ientry.value = kxml_node_attr(element, "value");
  435. ientry.restore = kxml_node_attr(element, "restore");
  436. ientry.order = kxml_node_attr(element, "order");
  437. ientry.filter = kxml_node_attr(element, "filter");
  438. // Check for parent tag type. All other types really are entries too.
  439. if ((parent_tag == KTAG_ACTION) ||
  440. // (parent_tag == KTAG_HOTKEY) ||
  441. (parent_tag == KTAG_PLUGIN)) {
  442. faux_error_sprintf(error,
  443. TAG": Tag \"%s\" can't contain ENTRY tag",
  444. kxml_tag_name(parent_tag));
  445. goto err;
  446. }
  447. if (!(entry = add_entry_to_hierarchy(parent_tag, parent, &ientry, error)))
  448. goto err;
  449. if (!process_children(element, entry, error))
  450. goto err;
  451. res = BOOL_TRUE;
  452. err:
  453. kxml_node_attr_free(ientry.name);
  454. kxml_node_attr_free(ientry.help);
  455. kxml_node_attr_free(ientry.container);
  456. kxml_node_attr_free(ientry.mode);
  457. kxml_node_attr_free(ientry.purpose);
  458. kxml_node_attr_free(ientry.min);
  459. kxml_node_attr_free(ientry.max);
  460. kxml_node_attr_free(ientry.ref);
  461. kxml_node_attr_free(ientry.value);
  462. kxml_node_attr_free(ientry.restore);
  463. kxml_node_attr_free(ientry.order);
  464. kxml_node_attr_free(ientry.filter);
  465. return res;
  466. }
  467. static bool_t process_view(const kxml_node_t *element, void *parent,
  468. faux_error_t *error)
  469. {
  470. ientry_t ientry = {};
  471. kentry_t *entry = NULL;
  472. bool_t res = BOOL_FALSE;
  473. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  474. // Mandatory VIEW name
  475. ientry.name = kxml_node_attr(element, "name");
  476. if (!ientry.name) {
  477. faux_error_sprintf(error, TAG": VIEW without name");
  478. return BOOL_FALSE;
  479. }
  480. ientry.help = kxml_node_attr(element, "help");
  481. ientry.container = "true";
  482. ientry.mode = "switch";
  483. ientry.purpose = "common";
  484. ientry.min = "1";
  485. ientry.max = "1";
  486. ientry.ref = kxml_node_attr(element, "ref");
  487. ientry.value = NULL;
  488. ientry.restore = "false";
  489. ientry.order = "false";
  490. ientry.filter = "false";
  491. if ((parent_tag == KTAG_ACTION) ||
  492. // (parent_tag == KTAG_HOTKEY) ||
  493. (parent_tag == KTAG_PLUGIN)) {
  494. faux_error_sprintf(error,
  495. TAG": Tag \"%s\" can't contain VIEW tag",
  496. kxml_tag_name(parent_tag));
  497. goto err;
  498. }
  499. if (!(entry = add_entry_to_hierarchy(parent_tag, parent, &ientry, error)))
  500. goto err;
  501. if (!process_children(element, entry, error))
  502. goto err;
  503. res = BOOL_TRUE;
  504. err:
  505. kxml_node_attr_free(ientry.name);
  506. kxml_node_attr_free(ientry.help);
  507. kxml_node_attr_free(ientry.ref);
  508. return res;
  509. }
  510. static bool_t process_ptype(const kxml_node_t *element, void *parent,
  511. faux_error_t *error)
  512. {
  513. ientry_t ientry = {};
  514. kentry_t *entry = NULL;
  515. bool_t res = BOOL_FALSE;
  516. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  517. // Mandatory PTYPE name
  518. ientry.name = kxml_node_attr(element, "name");
  519. if (!ientry.name) {
  520. faux_error_sprintf(error, TAG": PTYPE without name");
  521. return BOOL_FALSE;
  522. }
  523. ientry.help = kxml_node_attr(element, "help");
  524. ientry.container = "true";
  525. ientry.mode = "sequence";
  526. ientry.purpose = "ptype";
  527. ientry.min = "1";
  528. ientry.max = "1";
  529. ientry.ref = kxml_node_attr(element, "ref");
  530. ientry.value = kxml_node_attr(element, "value");
  531. ientry.restore = "false";
  532. ientry.order = "true";
  533. ientry.filter = "false";
  534. // Parent must be a KLISH tag or VIEW
  535. if ((parent_tag != KTAG_KLISH) &&
  536. (KTAG_VIEW != parent_tag)) {
  537. faux_error_sprintf(error,
  538. TAG": Tag \"%s\" can't contain PTYPE tag",
  539. kxml_tag_name(parent_tag));
  540. goto err;
  541. }
  542. if (!(entry = add_entry_to_hierarchy(parent_tag, parent, &ientry, error)))
  543. goto err;
  544. if (!process_children(element, entry, error))
  545. goto err;
  546. res = BOOL_TRUE;
  547. err:
  548. kxml_node_attr_free(ientry.name);
  549. kxml_node_attr_free(ientry.help);
  550. kxml_node_attr_free(ientry.ref);
  551. kxml_node_attr_free(ientry.value);
  552. return res;
  553. }
  554. static bool_t process_param(const kxml_node_t *element, void *parent,
  555. faux_error_t *error)
  556. {
  557. ientry_t ientry = {};
  558. kentry_t *entry = NULL;
  559. bool_t res = BOOL_FALSE;
  560. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  561. kentry_t *parent_entry = (kentry_t *)parent;
  562. kentry_t *entry_add_to = parent_entry;
  563. // Mandatory PARAM name
  564. ientry.name = kxml_node_attr(element, "name");
  565. if (!ientry.name) {
  566. faux_error_sprintf(error, TAG": PARAM without name");
  567. return BOOL_FALSE;
  568. }
  569. ientry.help = kxml_node_attr(element, "help");
  570. ientry.container = kxml_node_attr(element, "container");
  571. ientry.mode = kxml_node_attr(element, "mode");
  572. ientry.purpose = "common";
  573. ientry.min = kxml_node_attr(element, "min");
  574. ientry.max = kxml_node_attr(element, "max");
  575. ientry.ref = kxml_node_attr(element, "ref");
  576. ientry.value = kxml_node_attr(element, "value");
  577. ientry.restore = "false";
  578. ientry.order = kxml_node_attr(element, "order");
  579. ientry.filter = "false";
  580. entry = ientry_load(&ientry, error);
  581. if (!entry)
  582. goto err;
  583. if ((KTAG_COMMAND != parent_tag) &&
  584. (KTAG_PARAM != parent_tag) &&
  585. (KTAG_ENTRY != parent_tag) &&
  586. (KTAG_SWITCH != parent_tag) &&
  587. (KTAG_SUBCOMMAND != parent_tag) &&
  588. (KTAG_MULTI != parent_tag) &&
  589. (KTAG_COND != parent_tag) &&
  590. (KTAG_COMPL != parent_tag) &&
  591. (KTAG_HELP != parent_tag) &&
  592. (KTAG_PTYPE != parent_tag)) {
  593. faux_error_sprintf(error,
  594. TAG": Tag \"%s\" can't contain PARAM tag",
  595. kxml_tag_name(parent_tag));
  596. kentry_free(entry);
  597. goto err;
  598. }
  599. // Add newly created entry to special container in 'sequence' mode if
  600. // parent entry can has 'switch' mode.
  601. if (kentry_mode(parent_entry) == KENTRY_MODE_SWITCH) {
  602. const char *seq_entry_name = "__sequence";
  603. kentry_t *seq_entry = kentry_find_entry(parent_entry, seq_entry_name);
  604. if (!seq_entry) {
  605. seq_entry = kentry_new(seq_entry_name);
  606. assert(seq_entry);
  607. kentry_set_container(seq_entry, BOOL_TRUE);
  608. kentry_set_mode(seq_entry, KENTRY_MODE_SEQUENCE);
  609. kentry_add_entrys(parent_entry, seq_entry);
  610. }
  611. entry_add_to = seq_entry;
  612. }
  613. if (!kentry_add_entrys(entry_add_to, entry)) {
  614. faux_error_sprintf(error,
  615. TAG": Can't add PARAM \"%s\" to ENTRY \"%s\". "
  616. "Probably duplication",
  617. kentry_name(entry_add_to), kentry_name(entry_add_to));
  618. kentry_free(entry);
  619. goto err;
  620. }
  621. if (!process_children(element, entry, error))
  622. goto err;
  623. res = BOOL_TRUE;
  624. err:
  625. kxml_node_attr_free(ientry.name);
  626. kxml_node_attr_free(ientry.help);
  627. kxml_node_attr_free(ientry.container);
  628. kxml_node_attr_free(ientry.mode);
  629. kxml_node_attr_free(ientry.min);
  630. kxml_node_attr_free(ientry.max);
  631. kxml_node_attr_free(ientry.ref);
  632. kxml_node_attr_free(ientry.value);
  633. kxml_node_attr_free(ientry.order);
  634. return res;
  635. }
  636. static bool_t process_command(const kxml_node_t *element, void *parent,
  637. faux_error_t *error)
  638. {
  639. ientry_t ientry = {};
  640. kentry_t *entry = NULL;
  641. bool_t res = BOOL_FALSE;
  642. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  643. kentry_t *parent_entry = (kentry_t *)parent;
  644. // Mandatory COMMAND name
  645. ientry.name = kxml_node_attr(element, "name");
  646. if (!ientry.name) {
  647. faux_error_sprintf(error, TAG": COMMAND without name");
  648. return BOOL_FALSE;
  649. }
  650. ientry.help = kxml_node_attr(element, "help");
  651. ientry.container = "false";
  652. ientry.mode = "sequence";
  653. ientry.purpose = "common";
  654. ientry.min = "1";
  655. ientry.max = "1";
  656. ientry.ref = kxml_node_attr(element, "ref");
  657. ientry.value = kxml_node_attr(element, "value");
  658. ientry.restore = kxml_node_attr(element, "restore");
  659. ientry.order = "false";
  660. ientry.filter = kxml_node_attr(element, "filter");
  661. entry = ientry_load(&ientry, error);
  662. if (!entry)
  663. goto err;
  664. if ((KTAG_COMMAND != parent_tag) &&
  665. (KTAG_VIEW != parent_tag) &&
  666. (KTAG_ENTRY != parent_tag)) {
  667. faux_error_sprintf(error,
  668. TAG": Tag \"%s\" can't contain COMMAND tag",
  669. kxml_tag_name(parent_tag));
  670. kentry_free(entry);
  671. goto err;
  672. }
  673. if (!kentry_add_entrys(parent_entry, entry)) {
  674. faux_error_sprintf(error,
  675. TAG": Can't add PARAM \"%s\" to ENTRY \"%s\". "
  676. "Probably duplication",
  677. kentry_name(entry), kentry_name(parent_entry));
  678. kentry_free(entry);
  679. goto err;
  680. }
  681. if (!process_children(element, entry, error))
  682. goto err;
  683. res = BOOL_TRUE;
  684. err:
  685. kxml_node_attr_free(ientry.name);
  686. kxml_node_attr_free(ientry.help);
  687. kxml_node_attr_free(ientry.ref);
  688. kxml_node_attr_free(ientry.value);
  689. kxml_node_attr_free(ientry.restore);
  690. kxml_node_attr_free(ientry.filter);
  691. return res;
  692. }