ientry.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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, "ptype"))
  55. purpose = KENTRY_PURPOSE_PTYPE;
  56. else if (!faux_str_casecmp(info->purpose, "prompt"))
  57. purpose = KENTRY_PURPOSE_PROMPT;
  58. else if (!faux_str_casecmp(info->purpose, "cond"))
  59. purpose = KENTRY_PURPOSE_COND;
  60. else if (!faux_str_casecmp(info->purpose, "completion"))
  61. purpose = KENTRY_PURPOSE_COMPLETION;
  62. else if (!faux_str_casecmp(info->purpose, "help"))
  63. purpose = KENTRY_PURPOSE_HELP;
  64. if ((KENTRY_PURPOSE_NONE == purpose) || !kentry_set_purpose(entry, purpose)) {
  65. faux_error_add(error, TAG": Illegal 'purpose' attribute");
  66. retcode = BOOL_FALSE;
  67. }
  68. }
  69. // Min occurs
  70. if (!faux_str_is_empty(info->min)) {
  71. unsigned int i = 0;
  72. if (!faux_conv_atoui(info->min, &i, 0) ||
  73. !kentry_set_min(entry, (size_t)i)) {
  74. faux_error_add(error, TAG": Illegal 'min' attribute");
  75. retcode = BOOL_FALSE;
  76. }
  77. }
  78. // Max occurs
  79. if (!faux_str_is_empty(info->max)) {
  80. unsigned int i = 0;
  81. if (!faux_conv_atoui(info->max, &i, 0) ||
  82. !kentry_set_max(entry, (size_t)i)) {
  83. faux_error_add(error, TAG": Illegal 'max' attribute");
  84. retcode = BOOL_FALSE;
  85. }
  86. }
  87. // Ref string
  88. if (!faux_str_is_empty(info->ref)) {
  89. if (!kentry_set_ref_str(entry, info->ref)) {
  90. faux_error_add(error, TAG": Illegal 'ref' attribute");
  91. retcode = BOOL_FALSE;
  92. }
  93. }
  94. // Value
  95. if (!faux_str_is_empty(info->value)) {
  96. if (!kentry_set_value(entry, info->value)) {
  97. faux_error_add(error, TAG": Illegal 'value' attribute");
  98. retcode = BOOL_FALSE;
  99. }
  100. }
  101. // Restore
  102. if (!faux_str_is_empty(info->restore)) {
  103. bool_t b = BOOL_FALSE;
  104. if (!faux_conv_str2bool(info->restore, &b) ||
  105. !kentry_set_restore(entry, b)) {
  106. faux_error_add(error, TAG": Illegal 'restore' attribute");
  107. retcode = BOOL_FALSE;
  108. }
  109. }
  110. // Order
  111. if (!faux_str_is_empty(info->order)) {
  112. bool_t b = BOOL_FALSE;
  113. if (!faux_conv_str2bool(info->order, &b) ||
  114. !kentry_set_order(entry, b)) {
  115. faux_error_add(error, TAG": Illegal 'order' attribute");
  116. retcode = BOOL_FALSE;
  117. }
  118. }
  119. // Filter
  120. if (!faux_str_is_empty(info->filter)) {
  121. bool_t b = BOOL_FALSE;
  122. if (!faux_conv_str2bool(info->filter, &b) ||
  123. !kentry_set_filter(entry, b)) {
  124. faux_error_add(error, TAG": Illegal 'filter' attribute");
  125. retcode = BOOL_FALSE;
  126. }
  127. }
  128. return retcode;
  129. }
  130. bool_t ientry_parse_nested(const ientry_t *ientry, kentry_t *kentry,
  131. faux_error_t *error)
  132. {
  133. bool_t retval = BOOL_TRUE;
  134. if (!kentry || !ientry) {
  135. faux_error_add(error, TAG": Internal error");
  136. return BOOL_FALSE;
  137. }
  138. // ENTRY list
  139. // ENTRYs can be duplicate. Duplicated ENTRY will add nested
  140. // elements to existent ENTRY. Also it can overwrite ENTRY attributes.
  141. // So there is no special rule which attribute value will be "on top".
  142. // It's a random. Technically later ENTRYs will rewrite previous
  143. // values.
  144. if (ientry->entrys) {
  145. ientry_t **p_ientry = NULL;
  146. for (p_ientry = *ientry->entrys; *p_ientry; p_ientry++) {
  147. kentry_t *nkentry = NULL;
  148. ientry_t *nientry = *p_ientry;
  149. const char *entry_name = nientry->name;
  150. if (entry_name)
  151. nkentry = kentry_find_entry(kentry, entry_name);
  152. // ENTRY already exists
  153. if (nkentry) {
  154. if (!ientry_parse(nientry, nkentry, error)) {
  155. retval = BOOL_FALSE;
  156. continue;
  157. }
  158. if (!ientry_parse_nested(nientry, nkentry,
  159. error)) {
  160. retval = BOOL_FALSE;
  161. continue;
  162. }
  163. continue;
  164. }
  165. // New ENTRY
  166. nkentry = ientry_load(nientry, error);
  167. if (!nkentry) {
  168. retval = BOOL_FALSE;
  169. continue;
  170. }
  171. kentry_set_parent(nkentry, kentry); // Set parent entry
  172. if (!kentry_add_entrys(kentry, nkentry)) {
  173. faux_error_sprintf(error,
  174. TAG": Can't add ENTRY \"%s\"",
  175. kentry_name(nkentry));
  176. kentry_free(nkentry);
  177. retval = BOOL_FALSE;
  178. continue;
  179. }
  180. }
  181. }
  182. // ACTION list
  183. if (ientry->actions) {
  184. iaction_t **p_iaction = NULL;
  185. for (p_iaction = *ientry->actions; *p_iaction; p_iaction++) {
  186. kaction_t *kaction = NULL;
  187. iaction_t *iaction = *p_iaction;
  188. kaction = iaction_load(iaction, error);
  189. if (!kaction) {
  190. retval = BOOL_FALSE;
  191. continue;
  192. }
  193. if (!kentry_add_actions(kentry, kaction)) {
  194. faux_error_sprintf(error,
  195. TAG": Can't add ACTION #%d",
  196. kentry_actions_len(kentry) + 1);
  197. kaction_free(kaction);
  198. retval = BOOL_FALSE;
  199. continue;
  200. }
  201. }
  202. }
  203. // HOTKEY list
  204. if (ientry->hotkeys) {
  205. ihotkey_t **p_ihotkey = NULL;
  206. for (p_ihotkey = *ientry->hotkeys; *p_ihotkey; p_ihotkey++) {
  207. khotkey_t *khotkey = NULL;
  208. ihotkey_t *ihotkey = *p_ihotkey;
  209. khotkey = ihotkey_load(ihotkey, error);
  210. if (!khotkey) {
  211. retval = BOOL_FALSE;
  212. continue;
  213. }
  214. if (!kentry_add_hotkeys(kentry, khotkey)) {
  215. faux_error_sprintf(error,
  216. TAG": Can't add HOTKEY \"%s\"",
  217. khotkey_key(khotkey));
  218. khotkey_free(khotkey);
  219. retval = BOOL_FALSE;
  220. continue;
  221. }
  222. }
  223. }
  224. if (!retval)
  225. faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
  226. kentry_name(kentry));
  227. return retval;
  228. }
  229. kentry_t *ientry_load(const ientry_t *ientry, faux_error_t *error)
  230. {
  231. kentry_t *kentry = NULL;
  232. if (!ientry)
  233. return NULL;
  234. // Name [mandatory]
  235. if (faux_str_is_empty(ientry->name)) {
  236. faux_error_add(error, TAG": Empty 'name' attribute");
  237. return NULL;
  238. }
  239. kentry = kentry_new(ientry->name);
  240. if (!kentry) {
  241. faux_error_sprintf(error, TAG" \"%s\": Can't create object",
  242. ientry->name);
  243. return NULL;
  244. }
  245. if (!ientry_parse(ientry, kentry, error)) {
  246. kentry_free(kentry);
  247. return NULL;
  248. }
  249. // Parse nested elements
  250. if (!ientry_parse_nested(ientry, kentry, error)) {
  251. kentry_free(kentry);
  252. return NULL;
  253. }
  254. return kentry;
  255. }
  256. char *ientry_deploy(const kentry_t *kentry, int level)
  257. {
  258. char *str = NULL;
  259. char *tmp = NULL;
  260. char *mode = NULL;
  261. char *purpose = NULL;
  262. kentry_entrys_node_t *entrys_iter = NULL;
  263. kentry_actions_node_t *actions_iter = NULL;
  264. kentry_hotkeys_node_t *hotkeys_iter = NULL;
  265. char *num = NULL;
  266. tmp = faux_str_sprintf("%*cENTRY {\n", level, ' ');
  267. faux_str_cat(&str, tmp);
  268. faux_str_free(tmp);
  269. attr2ctext(&str, "name", kentry_name(kentry), level + 1);
  270. attr2ctext(&str, "help", kentry_help(kentry), level + 1);
  271. attr2ctext(&str, "ref", kentry_ref_str(kentry), level + 1);
  272. // Links (ENTRY with 'ref' attribute) doesn't need the following filds
  273. // that will be replaced by content of referenced ENTRY
  274. if (faux_str_is_empty(kentry_ref_str(kentry))) {
  275. attr2ctext(&str, "container", faux_conv_bool2str(kentry_container(kentry)), level + 1);
  276. // Mode
  277. switch (kentry_mode(kentry)) {
  278. case KENTRY_MODE_SEQUENCE:
  279. mode = "sequence";
  280. break;
  281. case KENTRY_MODE_SWITCH:
  282. mode = "switch";
  283. break;
  284. case KENTRY_MODE_EMPTY:
  285. mode = "empty";
  286. break;
  287. default:
  288. mode = NULL;
  289. }
  290. attr2ctext(&str, "mode", mode, level + 1);
  291. // Purpose
  292. switch (kentry_purpose(kentry)) {
  293. case KENTRY_PURPOSE_COMMON:
  294. purpose = "common";
  295. break;
  296. case KENTRY_PURPOSE_PTYPE:
  297. purpose = "ptype";
  298. break;
  299. case KENTRY_PURPOSE_PROMPT:
  300. purpose = "prompt";
  301. break;
  302. case KENTRY_PURPOSE_COND:
  303. purpose = "cond";
  304. break;
  305. case KENTRY_PURPOSE_COMPLETION:
  306. purpose = "completion";
  307. break;
  308. case KENTRY_PURPOSE_HELP:
  309. purpose = "help";
  310. break;
  311. default:
  312. purpose = NULL;
  313. }
  314. attr2ctext(&str, "purpose", purpose, level + 1);
  315. // Min occurs
  316. num = faux_str_sprintf("%u", kentry_min(kentry));
  317. attr2ctext(&str, "min", num, level + 1);
  318. faux_str_free(num);
  319. num = NULL;
  320. // Max occurs
  321. num = faux_str_sprintf("%u", kentry_max(kentry));
  322. attr2ctext(&str, "max", num, level + 1);
  323. faux_str_free(num);
  324. num = NULL;
  325. attr2ctext(&str, "value", kentry_value(kentry), level + 1);
  326. attr2ctext(&str, "restore", faux_conv_bool2str(kentry_restore(kentry)), level + 1);
  327. attr2ctext(&str, "order", faux_conv_bool2str(kentry_order(kentry)), level + 1);
  328. attr2ctext(&str, "filter", faux_conv_bool2str(kentry_filter(kentry)), level + 1);
  329. // ENTRY list
  330. entrys_iter = kentry_entrys_iter(kentry);
  331. if (entrys_iter) {
  332. kentry_t *nentry = NULL;
  333. tmp = faux_str_sprintf("\n%*cENTRY_LIST\n\n", level + 1, ' ');
  334. faux_str_cat(&str, tmp);
  335. faux_str_free(tmp);
  336. while ((nentry = kentry_entrys_each(&entrys_iter))) {
  337. tmp = ientry_deploy(nentry, level + 2);
  338. faux_str_cat(&str, tmp);
  339. faux_str_free(tmp);
  340. }
  341. tmp = faux_str_sprintf("%*cEND_ENTRY_LIST,\n", level + 1, ' ');
  342. faux_str_cat(&str, tmp);
  343. faux_str_free(tmp);
  344. }
  345. // ACTION list
  346. actions_iter = kentry_actions_iter(kentry);
  347. if (actions_iter) {
  348. kaction_t *action = NULL;
  349. tmp = faux_str_sprintf("\n%*cACTION_LIST\n\n", level + 1, ' ');
  350. faux_str_cat(&str, tmp);
  351. faux_str_free(tmp);
  352. while ((action = kentry_actions_each(&actions_iter))) {
  353. tmp = iaction_deploy(action, level + 2);
  354. faux_str_cat(&str, tmp);
  355. faux_str_free(tmp);
  356. }
  357. tmp = faux_str_sprintf("%*cEND_ACTION_LIST,\n", level + 1, ' ');
  358. faux_str_cat(&str, tmp);
  359. faux_str_free(tmp);
  360. }
  361. // HOTKEY list
  362. hotkeys_iter = kentry_hotkeys_iter(kentry);
  363. if (hotkeys_iter) {
  364. khotkey_t *hotkey = NULL;
  365. tmp = faux_str_sprintf("\n%*cHOTKEY_LIST\n\n", level + 1, ' ');
  366. faux_str_cat(&str, tmp);
  367. faux_str_free(tmp);
  368. while ((hotkey = kentry_hotkeys_each(&hotkeys_iter))) {
  369. tmp = ihotkey_deploy(hotkey, level + 2);
  370. faux_str_cat(&str, tmp);
  371. faux_str_free(tmp);
  372. }
  373. tmp = faux_str_sprintf("%*cEND_HOTKEY_LIST,\n", level + 1, ' ');
  374. faux_str_cat(&str, tmp);
  375. faux_str_free(tmp);
  376. }
  377. } // ref_str
  378. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  379. faux_str_cat(&str, tmp);
  380. faux_str_free(tmp);
  381. return str;
  382. }