load.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. #ifdef DEBUG
  21. #define KXML_DEBUG DEBUG
  22. #endif
  23. typedef bool_t (kxml_process_fn)(const kxml_node_t *element,
  24. void *parent, faux_error_t *error);
  25. static kxml_process_fn
  26. process_action,
  27. process_param,
  28. process_command,
  29. process_view,
  30. process_ptype,
  31. process_plugin,
  32. process_klish;
  33. // Different TAGs types
  34. typedef enum {
  35. KTAG_NONE,
  36. KTAG_ACTION,
  37. KTAG_PARAM,
  38. KTAG_COMMAND,
  39. KTAG_VIEW,
  40. KTAG_PTYPE,
  41. KTAG_PLUGIN,
  42. KTAG_KLISH,
  43. KTAG_MAX
  44. } ktags_e;
  45. static const char * const kxml_tags[] = {
  46. NULL,
  47. "ACTION",
  48. "PARAM",
  49. "COMMAND",
  50. "VIEW",
  51. "PTYPE",
  52. "PLUGIN",
  53. "KLISH"
  54. };
  55. static kxml_process_fn *kxml_handlers[] = {
  56. NULL,
  57. process_action,
  58. process_param,
  59. process_command,
  60. process_view,
  61. process_ptype,
  62. process_plugin,
  63. process_klish
  64. };
  65. static const char *kxml_tag_name(ktags_e tag)
  66. {
  67. if ((KTAG_NONE == tag) || (tag >= KTAG_MAX))
  68. return "NONE";
  69. return kxml_tags[tag];
  70. }
  71. static ktags_e kxml_node_tag(const kxml_node_t *node)
  72. {
  73. ktags_e tag = KTAG_NONE;
  74. char *name = NULL;
  75. if (!node)
  76. return KTAG_NONE;
  77. if (kxml_node_type(node) != KXML_NODE_ELM)
  78. return KTAG_NONE;
  79. name = kxml_node_name(node);
  80. if (!name)
  81. return KTAG_NONE; // Strange case
  82. for (tag = (KTAG_NONE + 1); tag < KTAG_MAX; tag++) {
  83. if (faux_str_casecmp(name, kxml_tags[tag]) == 0)
  84. break;
  85. }
  86. kxml_node_name_free(name);
  87. if (tag >= KTAG_MAX)
  88. return KTAG_NONE;
  89. return tag;
  90. }
  91. static kxml_process_fn *kxml_node_handler(const kxml_node_t *node)
  92. {
  93. return kxml_handlers[kxml_node_tag(node)];
  94. }
  95. /** @brief Reads an element from the XML stream and processes it.
  96. */
  97. static bool_t process_node(const kxml_node_t *node, void *parent, faux_error_t *error)
  98. {
  99. kxml_process_fn *handler = NULL;
  100. // Process only KXML_NODE_ELM. Don't process other types like:
  101. // KXML_NODE_DOC,
  102. // KXML_NODE_TEXT,
  103. // KXML_NODE_ATTR,
  104. // KXML_NODE_COMMENT,
  105. // KXML_NODE_PI,
  106. // KXML_NODE_DECL,
  107. // KXML_NODE_UNKNOWN,
  108. if (kxml_node_type(node) != KXML_NODE_ELM)
  109. return BOOL_TRUE;
  110. handler = kxml_node_handler(node);
  111. if (!handler) { // Unknown element
  112. faux_error_sprintf(error,
  113. TAG": Unknown tag \"%s\"", kxml_node_name(node));
  114. return BOOL_FALSE;
  115. }
  116. #ifdef KXML_DEBUG
  117. printf("kxml: Tag \"%s\"\n", kxml_node_name(node));
  118. #endif
  119. return handler(node, parent, error);
  120. }
  121. static bool_t kxml_load_file(kscheme_t *scheme, const char *filename,
  122. faux_error_t *error)
  123. {
  124. kxml_doc_t *doc = NULL;
  125. kxml_node_t *root = NULL;
  126. bool_t r = BOOL_FALSE;
  127. if (!scheme)
  128. return BOOL_FALSE;
  129. if (!filename)
  130. return BOOL_FALSE;
  131. #ifdef KXML_DEBUG
  132. printf("kxml: Processing XML file \"%s\"\n", filename);
  133. #endif
  134. doc = kxml_doc_read(filename);
  135. if (!kxml_doc_is_valid(doc)) {
  136. /* int errcaps = kxml_doc_error_caps(doc);
  137. printf("Unable to open file '%s'", filename);
  138. if ((errcaps & kxml_ERR_LINE) == kxml_ERR_LINE)
  139. printf(", at line %d", kxml_doc_err_line(doc));
  140. if ((errcaps & kxml_ERR_COL) == kxml_ERR_COL)
  141. printf(", at column %d", kxml_doc_err_col(doc));
  142. if ((errcaps & kxml_ERR_DESC) == kxml_ERR_DESC)
  143. printf(", message is %s", kxml_doc_err_msg(doc));
  144. printf("\n");
  145. */ kxml_doc_release(doc);
  146. return BOOL_FALSE;
  147. }
  148. root = kxml_doc_root(doc);
  149. r = process_node(root, scheme, error);
  150. kxml_doc_release(doc);
  151. if (!r) {
  152. faux_error_sprintf(error, TAG": Illegal file %s", filename);
  153. return BOOL_FALSE;
  154. }
  155. return BOOL_TRUE;
  156. }
  157. /** @brief Default path to get XML files from.
  158. */
  159. static const char *default_path = "/etc/klish;~/.klish";
  160. static const char *path_separators = ":;";
  161. bool_t kxml_load_scheme(kscheme_t *scheme, const char *xml_path,
  162. faux_error_t *error)
  163. {
  164. char *path = NULL;
  165. char *fn = NULL;
  166. char *saveptr = NULL;
  167. bool_t ret = BOOL_TRUE;
  168. assert(scheme);
  169. if (!scheme)
  170. return BOOL_FALSE;
  171. // Use the default path if xml path is not specified.
  172. // Dup is needed because sring will be tokenized but
  173. // the xml_path is must be const.
  174. if (!xml_path)
  175. path = faux_str_dup(default_path);
  176. else
  177. path = faux_str_dup(xml_path);
  178. #ifdef KXML_DEBUG
  179. printf("kxml: Loading scheme \"%s\"\n", path);
  180. #endif
  181. // Loop through each directory
  182. for (fn = strtok_r(path, path_separators, &saveptr);
  183. fn; fn = strtok_r(NULL, path_separators, &saveptr)) {
  184. DIR *dir = NULL;
  185. struct dirent *entry = NULL;
  186. char *realpath = NULL;
  187. // Expand tilde. Tilde must be the first symbol.
  188. realpath = faux_expand_tilde(fn);
  189. // Regular file
  190. if (faux_isfile(realpath)) {
  191. if (!kxml_load_file(scheme, realpath, error))
  192. ret = BOOL_FALSE;
  193. faux_str_free(realpath);
  194. continue;
  195. }
  196. // Search this directory for any XML files
  197. #ifdef KXML_DEBUG
  198. printf("kxml: Processing XML dir \"%s\"\n", realpath);
  199. #endif
  200. dir = opendir(realpath);
  201. if (!dir) {
  202. faux_str_free(realpath);
  203. continue;
  204. }
  205. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  206. const char *extension = strrchr(entry->d_name, '.');
  207. char *filename = NULL;
  208. // Check the filename
  209. if (!extension || strcmp(".xml", extension))
  210. continue;
  211. filename = faux_str_sprintf("%s/%s", realpath, entry->d_name);
  212. if (!kxml_load_file(scheme, filename, error))
  213. ret = BOOL_FALSE;
  214. faux_str_free(filename);
  215. }
  216. closedir(dir);
  217. faux_str_free(realpath);
  218. }
  219. faux_str_free(path);
  220. return ret;
  221. }
  222. /** @brief Iterate through element's children.
  223. */
  224. static bool_t process_children(const kxml_node_t *element, void *parent,
  225. faux_error_t *error)
  226. {
  227. const kxml_node_t *node = NULL;
  228. while ((node = kxml_node_next_child(element, node)) != NULL) {
  229. bool_t res = BOOL_FALSE;
  230. res = process_node(node, parent, error);
  231. if (!res)
  232. return res;
  233. }
  234. return BOOL_TRUE;
  235. }
  236. static bool_t process_klish(const kxml_node_t *element, void *parent,
  237. faux_error_t *error)
  238. {
  239. return process_children(element, parent, error);
  240. }
  241. static bool_t process_view(const kxml_node_t *element, void *parent,
  242. faux_error_t *error)
  243. {
  244. iview_t iview = {};
  245. kview_t *view = NULL;
  246. bool_t res = BOOL_FALSE;
  247. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  248. iview.name = kxml_node_attr(element, "name");
  249. view = iview_load(&iview, error);
  250. if (!view)
  251. goto err;
  252. if (parent_tag != KTAG_KLISH) {
  253. faux_error_sprintf(error,
  254. TAG": Tag \"%s\" can't contain VIEW tag",
  255. kxml_tag_name(parent_tag));
  256. return BOOL_FALSE;
  257. }
  258. if (!kscheme_add_view((kscheme_t *)parent, view)) {
  259. faux_error_sprintf(error, TAG": Can't add VIEW \"%s\"",
  260. kview_name(view));
  261. kview_free(view);
  262. goto err;
  263. }
  264. if (!process_children(element, view, error))
  265. goto err;
  266. res = BOOL_TRUE;
  267. err:
  268. kxml_node_attr_free(iview.name);
  269. return res;
  270. }
  271. static bool_t process_ptype(const kxml_node_t *element, void *parent,
  272. faux_error_t *error)
  273. {
  274. iptype_t iptype = {};
  275. kptype_t *ptype = NULL;
  276. bool_t res = BOOL_FALSE;
  277. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  278. iptype.name = kxml_node_attr(element, "name");
  279. iptype.help = kxml_node_attr(element, "help");
  280. ptype = iptype_load(&iptype, error);
  281. if (!ptype)
  282. goto err;
  283. if (parent_tag != KTAG_KLISH) {
  284. faux_error_sprintf(error,
  285. TAG": Tag \"%s\" can't contain PTYPE tag",
  286. kxml_tag_name(parent_tag));
  287. return BOOL_FALSE;
  288. }
  289. if (!kscheme_add_ptype((kscheme_t *)parent, ptype)) {
  290. faux_error_sprintf(error, TAG": Can't add PTYPE \"%s\"",
  291. kptype_name(ptype));
  292. kptype_free(ptype);
  293. goto err;
  294. }
  295. if (!process_children(element, ptype, error))
  296. goto err;
  297. res = BOOL_TRUE;
  298. err:
  299. kxml_node_attr_free(iptype.name);
  300. kxml_node_attr_free(iptype.help);
  301. return res;
  302. }
  303. static bool_t process_plugin(const kxml_node_t *element, void *parent,
  304. faux_error_t *error)
  305. {
  306. iplugin_t iplugin = {};
  307. kplugin_t *plugin = NULL;
  308. bool_t res = BOOL_FALSE;
  309. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  310. iplugin.name = kxml_node_attr(element, "name");
  311. iplugin.id = kxml_node_attr(element, "id");
  312. iplugin.file = kxml_node_attr(element, "file");
  313. iplugin.conf = kxml_node_content(element);
  314. plugin = iplugin_load(&iplugin, error);
  315. if (!plugin)
  316. goto err;
  317. if (parent_tag != KTAG_KLISH) {
  318. faux_error_sprintf(error,
  319. TAG": Tag \"%s\" can't contain PLUGIN tag",
  320. kxml_tag_name(parent_tag));
  321. return BOOL_FALSE;
  322. }
  323. if (!kscheme_add_plugin((kscheme_t *)parent, plugin)) {
  324. faux_error_sprintf(error, TAG": Can't add PLUGIN \"%s\"",
  325. kplugin_name(plugin));
  326. kplugin_free(plugin);
  327. goto err;
  328. }
  329. if (!process_children(element, plugin, error))
  330. goto err;
  331. res = BOOL_TRUE;
  332. err:
  333. kxml_node_attr_free(iplugin.name);
  334. kxml_node_attr_free(iplugin.id);
  335. kxml_node_attr_free(iplugin.file);
  336. kxml_node_content_free(iplugin.conf);
  337. return res;
  338. }
  339. static bool_t process_param(const kxml_node_t *element, void *parent,
  340. faux_error_t *error)
  341. {
  342. iparam_t iparam = {};
  343. kparam_t *param = NULL;
  344. bool_t res = BOOL_FALSE;
  345. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  346. iparam.name = kxml_node_attr(element, "name");
  347. iparam.help = kxml_node_attr(element, "help");
  348. iparam.ptype = kxml_node_attr(element, "ptype");
  349. param = iparam_load(&iparam, error);
  350. if (!param)
  351. goto err;
  352. if (KTAG_COMMAND == parent_tag) {
  353. kcommand_t *command = (kcommand_t *)parent;
  354. if (!kcommand_add_param(command, param)) {
  355. faux_error_sprintf(error,
  356. TAG": Can't add PARAM \"%s\" to COMMAND \"%s\"",
  357. kparam_name(param), kcommand_name(command));
  358. kparam_free(param);
  359. goto err;
  360. }
  361. } else if (KTAG_PARAM == parent_tag) {
  362. kparam_t *parent_param = (kparam_t *)parent;
  363. if (!kparam_add_param(parent_param, param)) {
  364. faux_error_sprintf(error,
  365. TAG": Can't add PARAM \"%s\" to PARAM \"%s\"",
  366. kparam_name(param), kparam_name(parent_param));
  367. kparam_free(param);
  368. goto err;
  369. }
  370. } else {
  371. faux_error_sprintf(error,
  372. TAG": Tag \"%s\" can't contain PARAM tag",
  373. kxml_tag_name(parent_tag));
  374. return BOOL_FALSE;
  375. }
  376. if (!process_children(element, param, error))
  377. goto err;
  378. res = BOOL_TRUE;
  379. err:
  380. kxml_node_attr_free(iparam.name);
  381. kxml_node_attr_free(iparam.help);
  382. kxml_node_attr_free(iparam.ptype);
  383. return res;
  384. }
  385. static bool_t process_command(const kxml_node_t *element, void *parent,
  386. faux_error_t *error)
  387. {
  388. icommand_t icommand = {};
  389. kcommand_t *command = NULL;
  390. bool_t res = BOOL_FALSE;
  391. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  392. icommand.name = kxml_node_attr(element, "name");
  393. icommand.help = kxml_node_attr(element, "help");
  394. command = icommand_load(&icommand, error);
  395. if (!command)
  396. goto err;
  397. if (parent_tag != KTAG_VIEW) {
  398. faux_error_sprintf(error,
  399. TAG": Tag \"%s\" can't contain COMMAND tag",
  400. kxml_tag_name(parent_tag));
  401. return BOOL_FALSE;
  402. }
  403. if (!kview_add_command((kview_t *)parent, command)) {
  404. faux_error_sprintf(error, TAG": Can't add COMMAND \"%s\"",
  405. kcommand_name(command));
  406. kcommand_free(command);
  407. goto err;
  408. }
  409. if (!process_children(element, command, error))
  410. goto err;
  411. res = BOOL_TRUE;
  412. err:
  413. kxml_node_attr_free(icommand.name);
  414. kxml_node_attr_free(icommand.help);
  415. return res;
  416. }
  417. static bool_t process_action(const kxml_node_t *element, void *parent,
  418. faux_error_t *error)
  419. {
  420. iaction_t iaction = {};
  421. kaction_t *action = NULL;
  422. bool_t res = BOOL_FALSE;
  423. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  424. iaction.sym = kxml_node_attr(element, "sym");
  425. iaction.lock = kxml_node_attr(element, "lock");
  426. iaction.interrupt = kxml_node_attr(element, "interrupt");
  427. iaction.interactive = kxml_node_attr(element, "interactive");
  428. iaction.exec_on = kxml_node_attr(element, "exec_on");
  429. iaction.update_retcode = kxml_node_attr(element, "update_retcode");
  430. iaction.script = kxml_node_content(element);
  431. action = iaction_load(&iaction, error);
  432. if (!action)
  433. goto err;
  434. if (KTAG_COMMAND == parent_tag) {
  435. kcommand_t *command = (kcommand_t *)parent;
  436. if (!kcommand_add_action(command, action)) {
  437. faux_error_sprintf(error,
  438. TAG": Can't add ACTION #%d to COMMAND \"%s\"",
  439. kcommand_actions_len(command) + 1,
  440. kcommand_name(command));
  441. kaction_free(action);
  442. goto err;
  443. }
  444. } else if (KTAG_PTYPE == parent_tag) {
  445. kptype_t *ptype = (kptype_t *)parent;
  446. if (!kptype_add_action(ptype, action)) {
  447. faux_error_sprintf(error,
  448. TAG": Can't add ACTION #%d to PTYPE \"%s\"",
  449. kptype_actions_len(ptype) + 1,
  450. kptype_name(ptype));
  451. kaction_free(action);
  452. goto err;
  453. }
  454. } else {
  455. faux_error_sprintf(error,
  456. TAG": Tag \"%s\" can't contain ACTION tag",
  457. kxml_tag_name(parent_tag));
  458. return BOOL_FALSE;
  459. }
  460. if (!process_children(element, action, error))
  461. goto err;
  462. res = BOOL_TRUE;
  463. err:
  464. kxml_node_attr_free(iaction.sym);
  465. kxml_node_attr_free(iaction.lock);
  466. kxml_node_attr_free(iaction.interrupt);
  467. kxml_node_attr_free(iaction.interactive);
  468. kxml_node_attr_free(iaction.exec_on);
  469. kxml_node_attr_free(iaction.update_retcode);
  470. kxml_node_content_free(iaction.script);
  471. return res;
  472. }