kentry.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. bool_t 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_BOOL(entry, filter);
  74. KSET_BOOL(entry, 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 = BOOL_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. // name - orig
  185. // help - orig
  186. // parent - orig
  187. // container - orig
  188. dst->mode = src->mode;
  189. // purpose - orig
  190. // min - orig
  191. // max - orig
  192. // ref_str - orig
  193. // value - orig
  194. // restore - orig
  195. // order - orig
  196. dst->filter = src->filter;
  197. dst->entrys = src->entrys;
  198. dst->actions = src->actions;
  199. dst->hotkeys = src->hotkeys;
  200. dst->nested_by_purpose = src->nested_by_purpose;
  201. // udata - orig
  202. // udata_free_fn - orig
  203. return BOOL_TRUE;
  204. }
  205. kentry_t *kentry_nested_by_purpose(const kentry_t *entry, kentry_purpose_e purpose)
  206. {
  207. assert(entry);
  208. if (!entry)
  209. return NULL;
  210. return entry->nested_by_purpose[purpose];
  211. }
  212. bool_t kentry_set_nested_by_purpose(kentry_t *entry, kentry_purpose_e purpose,
  213. kentry_t *nested)
  214. {
  215. assert(entry);
  216. if (!entry)
  217. return BOOL_FALSE;
  218. entry->nested_by_purpose[purpose] = nested;
  219. return BOOL_TRUE;
  220. }
  221. void *kentry_udata(const kentry_t *entry)
  222. {
  223. assert(entry);
  224. if (!entry)
  225. return NULL;
  226. return entry->udata;
  227. }
  228. bool_t kentry_set_udata(kentry_t *entry, void *data, kentry_udata_free_fn free_fn)
  229. {
  230. assert(entry);
  231. if (!entry)
  232. return BOOL_FALSE;
  233. // Free old udata value
  234. if (entry->udata) {
  235. if (entry->udata_free_fn)
  236. entry->udata_free_fn(entry->udata);
  237. else if (free_fn)
  238. free_fn(entry->udata);
  239. }
  240. entry->udata = data;
  241. entry->udata_free_fn = free_fn;
  242. return BOOL_TRUE;
  243. }
  244. bool_t kentry_interactive(const kentry_t *entry)
  245. {
  246. kentry_actions_node_t *iter = NULL;
  247. kaction_t *action = NULL;
  248. if (!entry)
  249. return BOOL_FALSE;
  250. iter = kentry_actions_iter(entry);
  251. while ((action = kentry_actions_each(&iter))) {
  252. if (kaction_interactive(action))
  253. return BOOL_TRUE;
  254. }
  255. return BOOL_FALSE;
  256. }