load.c 21 KB

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