kentry.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <syslog.h>
  6. #include <faux/faux.h>
  7. #include <faux/str.h>
  8. #include <faux/list.h>
  9. #include <klish/khelper.h>
  10. #include <klish/kaction.h>
  11. #include <klish/kentry.h>
  12. #include <klish/khotkey.h>
  13. // WARNING: Changing this structure don't forget to update kentry_link()
  14. struct kentry_s {
  15. char *name; // Mandatory name (identifier within entries tree)
  16. char *help; // Help for the entry
  17. kentry_t *parent; // Parent kentry_t element
  18. bool_t container; // Is entry container (element with hidden path)
  19. kentry_mode_e mode; // Mode of nested ENTRYs list
  20. kentry_purpose_e purpose; // Special purpose of ENTRY
  21. size_t min; // Min occurs of entry
  22. size_t max; // Max occurs of entry
  23. char *ref_str; // Text reference to aliased ENTRY
  24. char *value; // Additional info
  25. bool_t restore; // Should entry restore its depth while execution
  26. bool_t order; // Is entry ordered
  27. kentry_filter_e filter; // Is entry filter. Filter can't have inline actions.
  28. faux_list_t *entrys; // Nested ENTRYs
  29. faux_list_t *actions; // Nested ACTIONs
  30. faux_list_t *hotkeys; // Hotkeys
  31. // Fast links to nested entries with special purposes.
  32. kentry_t** nested_by_purpose;
  33. void *udata;
  34. kentry_udata_free_fn udata_free_fn;
  35. };
  36. // Simple methods
  37. // Name
  38. KGET_STR(entry, name);
  39. // Help
  40. KGET_STR(entry, help);
  41. KSET_STR(entry, help);
  42. // Parent
  43. KGET(entry, kentry_t *, parent);
  44. KSET(entry, kentry_t *, parent);
  45. // Container
  46. KGET_BOOL(entry, container);
  47. KSET_BOOL(entry, container);
  48. // Mode
  49. KGET(entry, kentry_mode_e, mode);
  50. KSET(entry, kentry_mode_e, mode);
  51. // Purpose
  52. KGET(entry, kentry_purpose_e, purpose);
  53. KSET(entry, kentry_purpose_e, purpose);
  54. // Min occurs
  55. KGET(entry, size_t, min);
  56. KSET(entry, size_t, min);
  57. // Max occurs
  58. KGET(entry, size_t, max);
  59. KSET(entry, size_t, max);
  60. // Ref string (must be resolved later)
  61. KGET_STR(entry, ref_str);
  62. KSET_STR(entry, ref_str);
  63. // Value
  64. KGET_STR(entry, value);
  65. KSET_STR(entry, value);
  66. // Restore
  67. KGET_BOOL(entry, restore);
  68. KSET_BOOL(entry, restore);
  69. // Order
  70. KGET_BOOL(entry, order);
  71. KSET_BOOL(entry, order);
  72. // Filter
  73. KGET(entry, kentry_filter_e, filter);
  74. KSET(entry, kentry_filter_e, filter);
  75. // Nested ENTRYs list
  76. KGET(entry, faux_list_t *, entrys);
  77. static KCMP_NESTED(entry, entry, name);
  78. static KCMP_NESTED_BY_KEY(entry, entry, name);
  79. KADD_NESTED(entry, kentry_t *, entrys);
  80. KFIND_NESTED(entry, entry);
  81. KNESTED_LEN(entry, entrys);
  82. KNESTED_IS_EMPTY(entry, entrys);
  83. KNESTED_ITER(entry, entrys);
  84. KNESTED_EACH(entry, kentry_t *, entrys);
  85. // ACTION list
  86. KGET(entry, faux_list_t *, actions);
  87. KADD_NESTED(entry, kaction_t *, actions);
  88. KNESTED_LEN(entry, actions);
  89. KNESTED_ITER(entry, actions);
  90. KNESTED_EACH(entry, kaction_t *, actions);
  91. // HOTKEY list
  92. KGET(entry, faux_list_t *, hotkeys);
  93. KCMP_NESTED(entry, hotkey, key);
  94. KADD_NESTED(entry, khotkey_t *, hotkeys);
  95. KNESTED_LEN(entry, hotkeys);
  96. KNESTED_ITER(entry, hotkeys);
  97. KNESTED_EACH(entry, khotkey_t *, hotkeys);
  98. kentry_t *kentry_new(const char *name)
  99. {
  100. kentry_t *entry = NULL;
  101. if (faux_str_is_empty(name))
  102. return NULL;
  103. entry = faux_zmalloc(sizeof(*entry));
  104. assert(entry);
  105. if (!entry)
  106. return NULL;
  107. // Initialize
  108. entry->name = faux_str_dup(name);
  109. entry->help = NULL;
  110. entry->parent = NULL;
  111. entry->container = BOOL_FALSE;
  112. entry->mode = KENTRY_MODE_SEQUENCE;
  113. entry->purpose = KENTRY_PURPOSE_COMMON;
  114. entry->min = 1;
  115. entry->max = 1;
  116. entry->ref_str = NULL;
  117. entry->value = NULL;
  118. entry->restore = BOOL_FALSE;
  119. entry->order = BOOL_FALSE;
  120. entry->filter = KENTRY_FILTER_FALSE;
  121. entry->udata = NULL;
  122. entry->udata_free_fn = NULL;
  123. // ENTRY list
  124. entry->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  125. kentry_entry_compare, kentry_entry_kcompare,
  126. (void (*)(void *))kentry_free);
  127. assert(entry->entrys);
  128. // ACTION list
  129. entry->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  130. NULL, NULL, (void (*)(void *))kaction_free);
  131. assert(entry->actions);
  132. // HOTKEY list
  133. entry->hotkeys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  134. kentry_hotkey_compare, NULL, (void (*)(void *))khotkey_free);
  135. assert(entry->hotkeys);
  136. entry->nested_by_purpose = faux_zmalloc(
  137. KENTRY_PURPOSE_MAX * sizeof(*(entry->nested_by_purpose)));
  138. return entry;
  139. }
  140. static void kentry_free_non_link(kentry_t *entry)
  141. {
  142. if (!entry)
  143. return;
  144. faux_list_free(entry->entrys);
  145. faux_list_free(entry->actions);
  146. faux_list_free(entry->hotkeys);
  147. faux_free(entry->nested_by_purpose);
  148. }
  149. static void kentry_free_common(kentry_t *entry)
  150. {
  151. if (!entry)
  152. return;
  153. faux_str_free(entry->name);
  154. faux_str_free(entry->value);
  155. faux_str_free(entry->help);
  156. faux_str_free(entry->ref_str);
  157. if (entry->udata && entry->udata_free_fn)
  158. entry->udata_free_fn(entry->udata);
  159. }
  160. void kentry_free(kentry_t *entry)
  161. {
  162. if (!entry)
  163. return;
  164. // If ENTRY is not a link
  165. if (!kentry_ref_str(entry))
  166. kentry_free_non_link(entry);
  167. // For links and non-links
  168. kentry_free_common(entry);
  169. faux_free(entry);
  170. }
  171. bool_t kentry_link(kentry_t *dst, const kentry_t *src)
  172. {
  173. assert(dst);
  174. if (!dst)
  175. return BOOL_FALSE;
  176. assert(src);
  177. if (!src)
  178. return BOOL_FALSE;
  179. // Free all fields that will be linker to src later
  180. kentry_free_non_link(dst);
  181. // Copy structure by hand because else some fields must be
  182. // returned back anyway and temp memory must be allocated. I think it
  183. // worse.
  184. // TODO: Some fields must be copied from src if they are not defined in
  185. // dst explicitly. But now I don't know how to do so. These fields are:
  186. // container, purpose, min, max, restore, order.
  187. // name - orig
  188. // help - orig
  189. if (!dst->help)
  190. dst->help = faux_str_dup(src->help);
  191. // parent - orig
  192. // container - orig
  193. // mode - ref
  194. dst->mode = src->mode;
  195. // purpose - orig
  196. // min - orig
  197. // max - orig
  198. // ref_str - orig
  199. // value - orig
  200. if (!dst->value)
  201. dst->value = faux_str_dup(src->value);
  202. // restore - orig
  203. // order - orig
  204. // filter - ref
  205. dst->filter = src->filter;
  206. // entrys - ref
  207. dst->entrys = src->entrys;
  208. // actions - ref
  209. dst->actions = src->actions;
  210. // hotkeys - ref
  211. dst->hotkeys = src->hotkeys;
  212. // nested_by_purpose - ref
  213. dst->nested_by_purpose = src->nested_by_purpose;
  214. // udata - orig
  215. // udata_free_fn - orig
  216. return BOOL_TRUE;
  217. }
  218. kentry_t *kentry_nested_by_purpose(const kentry_t *entry, kentry_purpose_e purpose)
  219. {
  220. assert(entry);
  221. if (!entry)
  222. return NULL;
  223. return entry->nested_by_purpose[purpose];
  224. }
  225. bool_t kentry_set_nested_by_purpose(kentry_t *entry, kentry_purpose_e purpose,
  226. kentry_t *nested)
  227. {
  228. assert(entry);
  229. if (!entry)
  230. return BOOL_FALSE;
  231. entry->nested_by_purpose[purpose] = nested;
  232. return BOOL_TRUE;
  233. }
  234. void *kentry_udata(const kentry_t *entry)
  235. {
  236. assert(entry);
  237. if (!entry)
  238. return NULL;
  239. return entry->udata;
  240. }
  241. bool_t kentry_set_udata(kentry_t *entry, void *data, kentry_udata_free_fn free_fn)
  242. {
  243. assert(entry);
  244. if (!entry)
  245. return BOOL_FALSE;
  246. // Free old udata value
  247. if (entry->udata) {
  248. if (entry->udata_free_fn)
  249. entry->udata_free_fn(entry->udata);
  250. else if (free_fn)
  251. free_fn(entry->udata);
  252. }
  253. entry->udata = data;
  254. entry->udata_free_fn = free_fn;
  255. return BOOL_TRUE;
  256. }
  257. // Get integral value of "in" field of all ENTRY's actions
  258. // false < true < tty
  259. kaction_io_e kentry_in(const kentry_t *entry)
  260. {
  261. kentry_actions_node_t *iter = NULL;
  262. kaction_t *action = NULL;
  263. kaction_io_e io = KACTION_IO_FALSE;
  264. if (!entry)
  265. return io;
  266. iter = kentry_actions_iter(entry);
  267. while ((action = kentry_actions_each(&iter))) {
  268. kaction_io_e cur_io = kaction_in(action);
  269. if (cur_io > io)
  270. io = cur_io;
  271. }
  272. return io;
  273. }
  274. // Get integral value of "out" field of all ENTRY's actions
  275. // false < true < tty
  276. kaction_io_e kentry_out(const kentry_t *entry)
  277. {
  278. kentry_actions_node_t *iter = NULL;
  279. kaction_t *action = NULL;
  280. kaction_io_e io = KACTION_IO_FALSE;
  281. if (!entry)
  282. return io;
  283. iter = kentry_actions_iter(entry);
  284. while ((action = kentry_actions_each(&iter))) {
  285. kaction_io_e cur_io = kaction_out(action);
  286. if (cur_io > io)
  287. io = cur_io;
  288. }
  289. return io;
  290. }