ientry.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/list.h>
  7. #include <faux/conv.h>
  8. #include <klish/khelper.h>
  9. #include <klish/ientry.h>
  10. #include <klish/kentry.h>
  11. #define TAG "ENTRY"
  12. bool_t ientry_parse(const ientry_t *info, kentry_t *entry, faux_error_t *error)
  13. {
  14. bool_t retcode = BOOL_TRUE;
  15. if (!info)
  16. return BOOL_FALSE;
  17. if (!entry)
  18. return BOOL_FALSE;
  19. // Help
  20. if (!faux_str_is_empty(info->help)) {
  21. if (!kentry_set_help(entry, info->help)) {
  22. faux_error_add(error, TAG": Illegal 'help' attribute");
  23. retcode = BOOL_FALSE;
  24. }
  25. }
  26. // Container
  27. if (!faux_str_is_empty(info->container)) {
  28. bool_t b = BOOL_FALSE;
  29. if (!faux_conv_str2bool(info->container, &b) ||
  30. !kentry_set_container(entry, b)) {
  31. faux_error_add(error, TAG": Illegal 'container' attribute");
  32. retcode = BOOL_FALSE;
  33. }
  34. }
  35. // Mode
  36. if (!faux_str_is_empty(info->mode)) {
  37. kentry_mode_e mode = KENTRY_MODE_NONE;
  38. if (!faux_str_casecmp(info->mode, "sequence"))
  39. mode = KENTRY_MODE_SEQUENCE;
  40. else if (!faux_str_casecmp(info->mode, "switch"))
  41. mode = KENTRY_MODE_SWITCH;
  42. else if (!faux_str_casecmp(info->mode, "empty"))
  43. mode = KENTRY_MODE_EMPTY;
  44. if ((KENTRY_MODE_NONE == mode) || !kentry_set_mode(entry, mode)) {
  45. faux_error_add(error, TAG": Illegal 'mode' attribute");
  46. retcode = BOOL_FALSE;
  47. }
  48. }
  49. // Purpose
  50. if (!faux_str_is_empty(info->purpose)) {
  51. kentry_purpose_e purpose = KENTRY_PURPOSE_NONE;
  52. if (!faux_str_casecmp(info->purpose, "common"))
  53. purpose = KENTRY_PURPOSE_COMMON;
  54. else if (!faux_str_casecmp(info->purpose, "prompt"))
  55. purpose = KENTRY_PURPOSE_PROMPT;
  56. else if (!faux_str_casecmp(info->purpose, "cond"))
  57. purpose = KENTRY_PURPOSE_COND;
  58. else if (!faux_str_casecmp(info->purpose, "completion"))
  59. purpose = KENTRY_PURPOSE_COMPLETION;
  60. if ((KENTRY_PURPOSE_NONE == purpose) || !kentry_set_purpose(entry, purpose)) {
  61. faux_error_add(error, TAG": Illegal 'purpose' attribute");
  62. retcode = BOOL_FALSE;
  63. }
  64. }
  65. // Min occurs
  66. if (!faux_str_is_empty(info->min)) {
  67. unsigned int i = 0;
  68. if (!faux_conv_atoui(info->min, &i, 0) ||
  69. !kentry_set_min(entry, (size_t)i)) {
  70. faux_error_add(error, TAG": Illegal 'min' attribute");
  71. retcode = BOOL_FALSE;
  72. }
  73. }
  74. // Max occurs
  75. if (!faux_str_is_empty(info->max)) {
  76. unsigned int i = 0;
  77. if (!faux_conv_atoui(info->max, &i, 0) ||
  78. !kentry_set_max(entry, (size_t)i)) {
  79. faux_error_add(error, TAG": Illegal 'max' attribute");
  80. retcode = BOOL_FALSE;
  81. }
  82. }
  83. // Ptype string
  84. if (!faux_str_is_empty(info->ptype)) {
  85. if (!kentry_set_ptype_str(entry, info->ptype)) {
  86. faux_error_add(error, TAG": Illegal 'ptype' attribute");
  87. retcode = BOOL_FALSE;
  88. }
  89. }
  90. // Ref string
  91. if (!faux_str_is_empty(info->ref)) {
  92. if (!kentry_set_ref_str(entry, info->ref)) {
  93. faux_error_add(error, TAG": Illegal 'ref' attribute");
  94. retcode = BOOL_FALSE;
  95. }
  96. }
  97. // Value
  98. if (!faux_str_is_empty(info->value)) {
  99. if (!kentry_set_value(entry, info->value)) {
  100. faux_error_add(error, TAG": Illegal 'value' attribute");
  101. retcode = BOOL_FALSE;
  102. }
  103. }
  104. // Restore
  105. if (!faux_str_is_empty(info->restore)) {
  106. bool_t b = BOOL_FALSE;
  107. if (!faux_conv_str2bool(info->restore, &b) ||
  108. !kentry_set_restore(entry, b)) {
  109. faux_error_add(error, TAG": Illegal 'restore' attribute");
  110. retcode = BOOL_FALSE;
  111. }
  112. }
  113. // Order
  114. if (!faux_str_is_empty(info->order)) {
  115. bool_t b = BOOL_FALSE;
  116. if (!faux_conv_str2bool(info->order, &b) ||
  117. !kentry_set_order(entry, b)) {
  118. faux_error_add(error, TAG": Illegal 'order' attribute");
  119. retcode = BOOL_FALSE;
  120. }
  121. }
  122. // Filter
  123. if (!faux_str_is_empty(info->filter)) {
  124. bool_t b = BOOL_FALSE;
  125. if (!faux_conv_str2bool(info->filter, &b) ||
  126. !kentry_set_filter(entry, b)) {
  127. faux_error_add(error, TAG": Illegal 'filter' attribute");
  128. retcode = BOOL_FALSE;
  129. }
  130. }
  131. return retcode;
  132. }
  133. bool_t ientry_parse_nested(const ientry_t *ientry, kentry_t *kentry,
  134. faux_error_t *error)
  135. {
  136. bool_t retval = BOOL_TRUE;
  137. if (!kentry || !ientry) {
  138. faux_error_add(error, TAG": Internal error");
  139. return BOOL_FALSE;
  140. }
  141. // ENTRY list
  142. // ENTRYs can be duplicate. Duplicated ENTRY will add nested
  143. // elements to existent ENTRY. Also it can overwrite ENTRY attributes.
  144. // So there is no special rule which attribute value will be "on top".
  145. // It's a random. Technically later ENTRYs will rewrite previous
  146. // values.
  147. if (ientry->entrys) {
  148. ientry_t **p_ientry = NULL;
  149. for (p_ientry = *ientry->entrys; *p_ientry; p_ientry++) {
  150. kentry_t *nkentry = NULL;
  151. ientry_t *nientry = *p_ientry;
  152. const char *entry_name = nientry->name;
  153. if (entry_name)
  154. nkentry = kentry_find_entry(kentry, entry_name);
  155. // ENTRY already exists
  156. if (nkentry) {
  157. if (!ientry_parse(nientry, nkentry, error)) {
  158. retval = BOOL_FALSE;
  159. continue;
  160. }
  161. if (!ientry_parse_nested(nientry, nkentry,
  162. error)) {
  163. retval = BOOL_FALSE;
  164. continue;
  165. }
  166. continue;
  167. }
  168. // New ENTRY
  169. nkentry = ientry_load(nientry, error);
  170. if (!nkentry) {
  171. retval = BOOL_FALSE;
  172. continue;
  173. }
  174. kentry_set_parent(nkentry, kentry); // Set parent entry
  175. if (!kentry_add_entrys(kentry, nkentry)) {
  176. faux_error_sprintf(error,
  177. TAG": Can't add ENTRY \"%s\"",
  178. kentry_name(nkentry));
  179. kentry_free(nkentry);
  180. retval = BOOL_FALSE;
  181. continue;
  182. }
  183. }
  184. }
  185. // ACTION list
  186. if (ientry->actions) {
  187. iaction_t **p_iaction = NULL;
  188. for (p_iaction = *ientry->actions; *p_iaction; p_iaction++) {
  189. kaction_t *kaction = NULL;
  190. iaction_t *iaction = *p_iaction;
  191. kaction = iaction_load(iaction, error);
  192. if (!kaction) {
  193. retval = BOOL_FALSE;
  194. continue;
  195. }
  196. if (!kentry_add_actions(kentry, kaction)) {
  197. faux_error_sprintf(error,
  198. TAG": Can't add ACTION #%d",
  199. kentry_actions_len(kentry) + 1);
  200. kaction_free(kaction);
  201. retval = BOOL_FALSE;
  202. continue;
  203. }
  204. }
  205. }
  206. if (!retval)
  207. faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
  208. kentry_name(kentry));
  209. return retval;
  210. }
  211. kentry_t *ientry_load(const ientry_t *ientry, faux_error_t *error)
  212. {
  213. kentry_t *kentry = NULL;
  214. if (!ientry)
  215. return NULL;
  216. // Name [mandatory]
  217. if (faux_str_is_empty(ientry->name)) {
  218. faux_error_add(error, TAG": Empty 'name' attribute");
  219. return NULL;
  220. }
  221. kentry = kentry_new(ientry->name);
  222. if (!kentry) {
  223. faux_error_sprintf(error, TAG" \"%s\": Can't create object",
  224. ientry->name);
  225. return NULL;
  226. }
  227. if (!ientry_parse(ientry, kentry, error)) {
  228. kentry_free(kentry);
  229. return NULL;
  230. }
  231. // Parse nested elements
  232. if (!ientry_parse_nested(ientry, kentry, error)) {
  233. kentry_free(kentry);
  234. return NULL;
  235. }
  236. return kentry;
  237. }
  238. char *ientry_deploy(const kentry_t *kentry, int level)
  239. {
  240. char *str = NULL;
  241. char *tmp = NULL;
  242. char *mode = NULL;
  243. char *purpose = NULL;
  244. kentry_entrys_node_t *entrys_iter = NULL;
  245. kentry_actions_node_t *actions_iter = NULL;
  246. char *num = NULL;
  247. tmp = faux_str_sprintf("%*cENTRY {\n", level, ' ');
  248. faux_str_cat(&str, tmp);
  249. faux_str_free(tmp);
  250. attr2ctext(&str, "name", kentry_name(kentry), level + 1);
  251. attr2ctext(&str, "help", kentry_help(kentry), level + 1);
  252. attr2ctext(&str, "ref", kentry_ref_str(kentry), level + 1);
  253. // Links (ENTRY with 'ref' attribute) doesn't need the following filds
  254. // that will be replaced by content of referenced ENTRY
  255. if (faux_str_is_empty(kentry_ref_str(kentry))) {
  256. attr2ctext(&str, "container", faux_conv_bool2str(kentry_container(kentry)), level + 1);
  257. // Mode
  258. switch (kentry_mode(kentry)) {
  259. case KENTRY_MODE_SEQUENCE:
  260. mode = "sequence";
  261. break;
  262. case KENTRY_MODE_SWITCH:
  263. mode = "switch";
  264. break;
  265. case KENTRY_MODE_EMPTY:
  266. mode = "empty";
  267. break;
  268. default:
  269. mode = NULL;
  270. }
  271. attr2ctext(&str, "mode", mode, level + 1);
  272. // Purpose
  273. switch (kentry_purpose(kentry)) {
  274. case KENTRY_PURPOSE_COMMON:
  275. purpose = "common";
  276. break;
  277. case KENTRY_PURPOSE_PROMPT:
  278. purpose = "prompt";
  279. break;
  280. case KENTRY_PURPOSE_COND:
  281. purpose = "cond";
  282. break;
  283. case KENTRY_PURPOSE_COMPLETION:
  284. purpose = "completion";
  285. break;
  286. default:
  287. purpose = NULL;
  288. }
  289. attr2ctext(&str, "purpose", purpose, level + 1);
  290. // Min occurs
  291. num = faux_str_sprintf("%u", kentry_min(kentry));
  292. attr2ctext(&str, "min", num, level + 1);
  293. faux_str_free(num);
  294. num = NULL;
  295. // Max occurs
  296. num = faux_str_sprintf("%u", kentry_max(kentry));
  297. attr2ctext(&str, "max", num, level + 1);
  298. faux_str_free(num);
  299. num = NULL;
  300. attr2ctext(&str, "ptype", kentry_ptype_str(kentry), level + 1);
  301. attr2ctext(&str, "value", kentry_value(kentry), level + 1);
  302. attr2ctext(&str, "restore", faux_conv_bool2str(kentry_restore(kentry)), level + 1);
  303. attr2ctext(&str, "order", faux_conv_bool2str(kentry_order(kentry)), level + 1);
  304. attr2ctext(&str, "filter", faux_conv_bool2str(kentry_filter(kentry)), level + 1);
  305. // ENTRY list
  306. entrys_iter = kentry_entrys_iter(kentry);
  307. if (entrys_iter) {
  308. kentry_t *nentry = NULL;
  309. tmp = faux_str_sprintf("\n%*cENTRY_LIST\n\n", level + 1, ' ');
  310. faux_str_cat(&str, tmp);
  311. faux_str_free(tmp);
  312. while ((nentry = kentry_entrys_each(&entrys_iter))) {
  313. tmp = ientry_deploy(nentry, level + 2);
  314. faux_str_cat(&str, tmp);
  315. faux_str_free(tmp);
  316. }
  317. tmp = faux_str_sprintf("%*cEND_ENTRY_LIST,\n", level + 1, ' ');
  318. faux_str_cat(&str, tmp);
  319. faux_str_free(tmp);
  320. }
  321. // ACTION list
  322. actions_iter = kentry_actions_iter(kentry);
  323. if (actions_iter) {
  324. kaction_t *action = NULL;
  325. tmp = faux_str_sprintf("\n%*cACTION_LIST\n\n", level + 1, ' ');
  326. faux_str_cat(&str, tmp);
  327. faux_str_free(tmp);
  328. while ((action = kentry_actions_each(&actions_iter))) {
  329. tmp = iaction_deploy(action, level + 2);
  330. faux_str_cat(&str, tmp);
  331. faux_str_free(tmp);
  332. }
  333. tmp = faux_str_sprintf("%*cEND_ACTION_LIST,\n", level + 1, ' ');
  334. faux_str_cat(&str, tmp);
  335. faux_str_free(tmp);
  336. }
  337. } // ref_str
  338. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  339. faux_str_cat(&str, tmp);
  340. faux_str_free(tmp);
  341. return str;
  342. }