load.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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_nspace,
  30. process_klish,
  31. process_entry;
  32. // Different TAGs types
  33. typedef enum {
  34. KTAG_NONE,
  35. KTAG_ACTION,
  36. KTAG_PARAM,
  37. KTAG_SWITCH, // PARAM alias
  38. KTAG_SUBCOMMAND, // PARAM alias
  39. KTAG_MULTI, // PARAM alias
  40. KTAG_COMMAND,
  41. KTAG_FILTER,
  42. KTAG_VIEW,
  43. KTAG_PTYPE,
  44. KTAG_PLUGIN,
  45. KTAG_NSPACE,
  46. KTAG_KLISH,
  47. KTAG_ENTRY,
  48. KTAG_COND,
  49. KTAG_COMPL,
  50. KTAG_HELP,
  51. KTAG_MAX,
  52. } ktags_e;
  53. static const char * const kxml_tags[] = {
  54. NULL,
  55. "ACTION",
  56. "PARAM",
  57. "SWITCH",
  58. "SUBCOMMAND",
  59. "MULTI",
  60. "COMMAND",
  61. "FILTER",
  62. "VIEW",
  63. "PTYPE",
  64. "PLUGIN",
  65. "NSPACE",
  66. "KLISH",
  67. "ENTRY",
  68. "COND",
  69. "COMPL",
  70. "HELP",
  71. };
  72. static kxml_process_fn *kxml_handlers[] = {
  73. NULL,
  74. process_action,
  75. process_param,
  76. process_param,
  77. process_param,
  78. process_param,
  79. process_command,
  80. process_command,
  81. process_view,
  82. process_ptype,
  83. process_plugin,
  84. process_nspace,
  85. process_klish,
  86. process_entry,
  87. process_command,
  88. process_command,
  89. process_command,
  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_SUBCOMMAND != parent_tag) &&
  327. (KTAG_FILTER != parent_tag) &&
  328. (KTAG_COND != parent_tag) &&
  329. (KTAG_COMPL != parent_tag) &&
  330. (KTAG_HELP != 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(ktags_e parent_tag, void *parent,
  363. ientry_t *ientry, faux_error_t *error)
  364. {
  365. kentry_t *entry = NULL;
  366. assert(ientry);
  367. // Parent is mandatory field
  368. if (!parent) {
  369. faux_error_sprintf(error,
  370. TAG": Broken parent object for entry \"%s\"",
  371. ientry->name);
  372. return NULL;
  373. }
  374. // High level ENTRY
  375. if (KTAG_KLISH == parent_tag) {
  376. kscheme_t *scheme = (kscheme_t *)parent;
  377. // Does such ENTRY already exist
  378. entry = kscheme_find_entry(scheme, ientry->name);
  379. if (entry) {
  380. if (!ientry_parse(ientry, entry, error))
  381. return NULL;
  382. } else { // New entry object
  383. entry = ientry_load(ientry, error);
  384. if (!entry)
  385. return NULL;
  386. if (!kscheme_add_entrys(scheme, entry)) {
  387. faux_error_sprintf(error, TAG": Can't add entry \"%s\". "
  388. "Probably duplication",
  389. kentry_name(entry));
  390. kentry_free(entry);
  391. return NULL;
  392. }
  393. }
  394. // ENTRY within ENTRY
  395. } else {
  396. kentry_t *parent_entry = (kentry_t *)parent;
  397. // Does such ENTRY already exist
  398. entry = kentry_find_entry(parent_entry, ientry->name);
  399. if (entry) {
  400. if (!ientry_parse(ientry, entry, error))
  401. return NULL;
  402. } else { // New entry object
  403. entry = ientry_load(ientry, error);
  404. if (!entry)
  405. return NULL;
  406. kentry_set_parent(entry, parent_entry);
  407. if (!kentry_add_entrys(parent_entry, entry)) {
  408. faux_error_sprintf(error, TAG": Can't add entry \"%s\". "
  409. "Probably duplication",
  410. kentry_name(entry));
  411. kentry_free(entry);
  412. return NULL;
  413. }
  414. }
  415. }
  416. return entry;
  417. }
  418. static bool_t process_entry(const kxml_node_t *element, void *parent,
  419. faux_error_t *error)
  420. {
  421. ientry_t ientry = {};
  422. kentry_t *entry = NULL;
  423. bool_t res = BOOL_FALSE;
  424. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  425. // Mandatory entry name
  426. ientry.name = kxml_node_attr(element, "name");
  427. if (!ientry.name) {
  428. faux_error_sprintf(error, TAG": entry without name");
  429. return BOOL_FALSE;
  430. }
  431. ientry.help = kxml_node_attr(element, "help");
  432. ientry.container = kxml_node_attr(element, "container");
  433. ientry.mode = kxml_node_attr(element, "mode");
  434. ientry.purpose = kxml_node_attr(element, "purpose");
  435. ientry.min = kxml_node_attr(element, "min");
  436. ientry.max = kxml_node_attr(element, "max");
  437. ientry.ref = kxml_node_attr(element, "ref");
  438. ientry.value = kxml_node_attr(element, "value");
  439. ientry.restore = kxml_node_attr(element, "restore");
  440. ientry.order = kxml_node_attr(element, "order");
  441. ientry.filter = kxml_node_attr(element, "filter");
  442. // Check for parent tag type. All other types really are entries too.
  443. if ((parent_tag == KTAG_ACTION) ||
  444. // (parent_tag == KTAG_HOTKEY) ||
  445. (parent_tag == KTAG_PLUGIN)) {
  446. faux_error_sprintf(error,
  447. TAG": Tag \"%s\" can't contain ENTRY tag",
  448. kxml_tag_name(parent_tag));
  449. goto err;
  450. }
  451. if (!(entry = add_entry_to_hierarchy(parent_tag, parent, &ientry, error)))
  452. goto err;
  453. if (!process_children(element, entry, error))
  454. goto err;
  455. res = BOOL_TRUE;
  456. err:
  457. kxml_node_attr_free(ientry.name);
  458. kxml_node_attr_free(ientry.help);
  459. kxml_node_attr_free(ientry.container);
  460. kxml_node_attr_free(ientry.mode);
  461. kxml_node_attr_free(ientry.purpose);
  462. kxml_node_attr_free(ientry.min);
  463. kxml_node_attr_free(ientry.max);
  464. kxml_node_attr_free(ientry.ref);
  465. kxml_node_attr_free(ientry.value);
  466. kxml_node_attr_free(ientry.restore);
  467. kxml_node_attr_free(ientry.order);
  468. kxml_node_attr_free(ientry.filter);
  469. return res;
  470. }
  471. static bool_t process_view(const kxml_node_t *element, void *parent,
  472. faux_error_t *error)
  473. {
  474. ientry_t ientry = {};
  475. kentry_t *entry = NULL;
  476. bool_t res = BOOL_FALSE;
  477. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  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 = NULL;
  491. ientry.value = NULL;
  492. ientry.restore = "false";
  493. ientry.order = "false";
  494. ientry.filter = "false";
  495. // Parent must be a KLISH tag
  496. if (parent_tag != KTAG_KLISH) {
  497. faux_error_sprintf(error,
  498. TAG": Tag \"%s\" can't contain VIEW tag",
  499. kxml_tag_name(parent_tag));
  500. goto err;
  501. }
  502. if (!(entry = add_entry_to_hierarchy(parent_tag, parent, &ientry, error)))
  503. goto err;
  504. if (!process_children(element, entry, error))
  505. goto err;
  506. res = BOOL_TRUE;
  507. err:
  508. kxml_node_attr_free(ientry.name);
  509. kxml_node_attr_free(ientry.help);
  510. return res;
  511. }
  512. // It's a namespace for really different objects but not for VIEWs only.
  513. static bool_t process_nspace(const kxml_node_t *element, void *parent,
  514. faux_error_t *error)
  515. {
  516. ientry_t ientry = {};
  517. kentry_t *entry = NULL;
  518. bool_t res = BOOL_FALSE;
  519. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  520. // Mandatory NSPACE name
  521. ientry.name = kxml_node_attr(element, "name");
  522. if (!ientry.name) {
  523. faux_error_sprintf(error, TAG": NSPACE without name");
  524. return BOOL_FALSE;
  525. }
  526. ientry.help = kxml_node_attr(element, "help");
  527. ientry.container = kxml_node_attr(element, "container");
  528. ientry.mode = "common";
  529. ientry.purpose = kxml_node_attr(element, "common");;
  530. ientry.min = kxml_node_attr(element, "min");
  531. ientry.max = kxml_node_attr(element, "max");
  532. ientry.ref = kxml_node_attr(element, "ref");
  533. if (!ientry.ref) {
  534. faux_error_sprintf(error, TAG": NSPACE without reference");
  535. return BOOL_FALSE;
  536. }
  537. ientry.value = kxml_node_attr(element, "value");
  538. ientry.restore = kxml_node_attr(element, "restore");
  539. ientry.order = kxml_node_attr(element, "order");
  540. ientry.filter = "false";
  541. if ( (KTAG_PLUGIN == parent_tag) ||
  542. // (KTAG_HOTKEY == parent_tag) ||
  543. (KTAG_ACTION == parent_tag)
  544. ) {
  545. faux_error_sprintf(error,
  546. TAG": Tag \"%s\" can't contain NSPACE tag",
  547. kxml_tag_name(parent_tag));
  548. kentry_free(entry);
  549. goto err;
  550. }
  551. if (!(entry = add_entry_to_hierarchy(parent_tag, parent, &ientry, error)))
  552. goto err;
  553. // Don't process NSPACE child elements
  554. res = BOOL_TRUE;
  555. err:
  556. kxml_node_attr_free(ientry.name);
  557. kxml_node_attr_free(ientry.help);
  558. kxml_node_attr_free(ientry.container);
  559. kxml_node_attr_free(ientry.min);
  560. kxml_node_attr_free(ientry.max);
  561. kxml_node_attr_free(ientry.ref);
  562. kxml_node_attr_free(ientry.value);
  563. kxml_node_attr_free(ientry.restore);
  564. kxml_node_attr_free(ientry.order);
  565. return res;
  566. }
  567. static bool_t process_ptype(const kxml_node_t *element, void *parent,
  568. faux_error_t *error)
  569. {
  570. ientry_t ientry = {};
  571. kentry_t *entry = NULL;
  572. bool_t res = BOOL_FALSE;
  573. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  574. // Mandatory PTYPE name
  575. ientry.name = kxml_node_attr(element, "name");
  576. if (!ientry.name) {
  577. faux_error_sprintf(error, TAG": PTYPE without name");
  578. return BOOL_FALSE;
  579. }
  580. ientry.help = kxml_node_attr(element, "help");
  581. ientry.container = "true";
  582. ientry.mode = "sequence";
  583. ientry.purpose = "ptype";
  584. ientry.min = "1";
  585. ientry.max = "1";
  586. ientry.ref = kxml_node_attr(element, "ref");
  587. ientry.value = kxml_node_attr(element, "value");
  588. ientry.restore = "false";
  589. ientry.order = "true";
  590. ientry.filter = "false";
  591. // Parent must be a KLISH tag or VIEW
  592. if ((parent_tag != KTAG_KLISH) &&
  593. (KTAG_VIEW != parent_tag)) {
  594. faux_error_sprintf(error,
  595. TAG": Tag \"%s\" can't contain PTYPE tag",
  596. kxml_tag_name(parent_tag));
  597. goto err;
  598. }
  599. if (!(entry = add_entry_to_hierarchy(parent_tag, parent, &ientry, error)))
  600. goto err;
  601. if (!process_children(element, entry, error))
  602. goto err;
  603. res = BOOL_TRUE;
  604. err:
  605. kxml_node_attr_free(ientry.name);
  606. kxml_node_attr_free(ientry.help);
  607. kxml_node_attr_free(ientry.ref);
  608. kxml_node_attr_free(ientry.value);
  609. return res;
  610. }
  611. static bool_t process_param(const kxml_node_t *element, void *parent,
  612. faux_error_t *error)
  613. {
  614. ientry_t ientry = {};
  615. kentry_t *entry = NULL;
  616. bool_t res = BOOL_FALSE;
  617. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  618. kentry_t *parent_entry = (kentry_t *)parent;
  619. kentry_t *entry_add_to = parent_entry;
  620. // Mandatory PARAM name
  621. ientry.name = kxml_node_attr(element, "name");
  622. if (!ientry.name) {
  623. faux_error_sprintf(error, TAG": PARAM without name");
  624. return BOOL_FALSE;
  625. }
  626. ientry.help = kxml_node_attr(element, "help");
  627. ientry.container = kxml_node_attr(element, "container");
  628. ientry.mode = kxml_node_attr(element, "mode");
  629. ientry.purpose = "common";
  630. ientry.min = kxml_node_attr(element, "min");
  631. ientry.max = kxml_node_attr(element, "max");
  632. ientry.ref = kxml_node_attr(element, "ref");
  633. ientry.value = kxml_node_attr(element, "value");
  634. ientry.restore = "false";
  635. ientry.order = kxml_node_attr(element, "order");
  636. ientry.filter = "false";
  637. entry = ientry_load(&ientry, error);
  638. if (!entry)
  639. goto err;
  640. if ((KTAG_COMMAND != parent_tag) &&
  641. (KTAG_PARAM != parent_tag) &&
  642. (KTAG_ENTRY != parent_tag) &&
  643. (KTAG_SWITCH != parent_tag) &&
  644. (KTAG_SUBCOMMAND != parent_tag) &&
  645. (KTAG_MULTI != parent_tag) &&
  646. (KTAG_COND != parent_tag) &&
  647. (KTAG_COMPL != parent_tag) &&
  648. (KTAG_HELP != parent_tag) &&
  649. (KTAG_PTYPE != parent_tag)) {
  650. faux_error_sprintf(error,
  651. TAG": Tag \"%s\" can't contain PARAM tag",
  652. kxml_tag_name(parent_tag));
  653. kentry_free(entry);
  654. goto err;
  655. }
  656. // Add newly created entry to special container in 'sequence' mode if
  657. // parent entry can has 'switch' mode.
  658. if (kentry_mode(parent_entry) == KENTRY_MODE_SWITCH) {
  659. const char *seq_entry_name = "__sequence";
  660. kentry_t *seq_entry = kentry_find_entry(parent_entry, seq_entry_name);
  661. if (!seq_entry) {
  662. seq_entry = kentry_new(seq_entry_name);
  663. assert(seq_entry);
  664. kentry_set_container(seq_entry, BOOL_TRUE);
  665. kentry_set_mode(seq_entry, KENTRY_MODE_SEQUENCE);
  666. kentry_add_entrys(parent_entry, seq_entry);
  667. }
  668. entry_add_to = seq_entry;
  669. }
  670. if (!kentry_add_entrys(entry_add_to, entry)) {
  671. faux_error_sprintf(error,
  672. TAG": Can't add PARAM \"%s\" to ENTRY \"%s\". "
  673. "Probably duplication",
  674. kentry_name(entry_add_to), kentry_name(entry_add_to));
  675. kentry_free(entry);
  676. goto err;
  677. }
  678. if (!process_children(element, entry, error))
  679. goto err;
  680. res = BOOL_TRUE;
  681. err:
  682. kxml_node_attr_free(ientry.name);
  683. kxml_node_attr_free(ientry.help);
  684. kxml_node_attr_free(ientry.container);
  685. kxml_node_attr_free(ientry.mode);
  686. kxml_node_attr_free(ientry.min);
  687. kxml_node_attr_free(ientry.max);
  688. kxml_node_attr_free(ientry.ref);
  689. kxml_node_attr_free(ientry.value);
  690. kxml_node_attr_free(ientry.order);
  691. return res;
  692. }
  693. static bool_t process_command(const kxml_node_t *element, void *parent,
  694. faux_error_t *error)
  695. {
  696. ientry_t ientry = {};
  697. kentry_t *entry = NULL;
  698. bool_t res = BOOL_FALSE;
  699. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  700. kentry_t *parent_entry = (kentry_t *)parent;
  701. // Mandatory COMMAND name
  702. ientry.name = kxml_node_attr(element, "name");
  703. if (!ientry.name) {
  704. faux_error_sprintf(error, TAG": COMMAND without name");
  705. return BOOL_FALSE;
  706. }
  707. ientry.help = kxml_node_attr(element, "help");
  708. ientry.container = "false";
  709. ientry.mode = "sequence";
  710. ientry.purpose = "common";
  711. ientry.min = "1";
  712. ientry.max = "1";
  713. ientry.ref = kxml_node_attr(element, "ref");
  714. ientry.value = kxml_node_attr(element, "value");
  715. ientry.restore = kxml_node_attr(element, "restore");
  716. ientry.order = "false";
  717. ientry.filter = kxml_node_attr(element, "filter");
  718. entry = ientry_load(&ientry, error);
  719. if (!entry)
  720. goto err;
  721. if ((KTAG_COMMAND != parent_tag) &&
  722. (KTAG_VIEW != parent_tag) &&
  723. (KTAG_ENTRY != parent_tag)) {
  724. faux_error_sprintf(error,
  725. TAG": Tag \"%s\" can't contain COMMAND tag",
  726. kxml_tag_name(parent_tag));
  727. kentry_free(entry);
  728. goto err;
  729. }
  730. if (!kentry_add_entrys(parent_entry, entry)) {
  731. faux_error_sprintf(error,
  732. TAG": Can't add PARAM \"%s\" to ENTRY \"%s\". "
  733. "Probably duplication",
  734. kentry_name(entry), kentry_name(parent_entry));
  735. kentry_free(entry);
  736. goto err;
  737. }
  738. if (!process_children(element, entry, error))
  739. goto err;
  740. res = BOOL_TRUE;
  741. err:
  742. kxml_node_attr_free(ientry.name);
  743. kxml_node_attr_free(ientry.help);
  744. kxml_node_attr_free(ientry.ref);
  745. kxml_node_attr_free(ientry.value);
  746. kxml_node_attr_free(ientry.restore);
  747. kxml_node_attr_free(ientry.filter);
  748. return res;
  749. }