kentry.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/faux.h>
  6. #include <faux/str.h>
  7. #include <faux/list.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kaction.h>
  10. #include <klish/kentry.h>
  11. struct kentry_s {
  12. char *name; // Mandatory name (identifier within entries tree)
  13. char *help; // Help for the entry
  14. kentry_t *parent; // Parent kentry_t element
  15. bool_t container; // Is entry container (element with hidden path)
  16. kentry_mode_e mode; // Mode of nested ENTRYs list
  17. kentry_purpose_e purpose; // Special purpose of ENTRY
  18. size_t min; // Min occurs of entry
  19. size_t max; // Max occurs of entry
  20. char *ref_str; // Text reference to aliased ENTRY
  21. char *value; // Additional info
  22. bool_t restore; // Should entry restore its depth while execution
  23. bool_t order; // Is entry ordered
  24. bool_t filter; // Is entry filter. Filter can't have inline actions.
  25. faux_list_t *entrys; // Nested ENTRYs
  26. faux_list_t *actions; // Nested ACTIONs
  27. // Fast links to nested entries with special purposes:
  28. kentry_t* nested_by_purpose[KENTRY_PURPOSE_MAX];
  29. };
  30. // Simple methods
  31. // Name
  32. KGET_STR(entry, name);
  33. // Help
  34. KGET_STR(entry, help);
  35. KSET_STR(entry, help);
  36. // Parent
  37. KGET(entry, kentry_t *, parent);
  38. KSET(entry, kentry_t *, parent);
  39. // Container
  40. KGET_BOOL(entry, container);
  41. KSET_BOOL(entry, container);
  42. // Mode
  43. KGET(entry, kentry_mode_e, mode);
  44. KSET(entry, kentry_mode_e, mode);
  45. // Purpose
  46. KGET(entry, kentry_purpose_e, purpose);
  47. KSET(entry, kentry_purpose_e, purpose);
  48. // Min occurs
  49. KGET(entry, size_t, min);
  50. KSET(entry, size_t, min);
  51. // Max occurs
  52. KGET(entry, size_t, max);
  53. KSET(entry, size_t, max);
  54. // Ref string (must be resolved later)
  55. KGET_STR(entry, ref_str);
  56. KSET_STR(entry, ref_str);
  57. // Value
  58. KGET_STR(entry, value);
  59. KSET_STR(entry, value);
  60. // Restore
  61. KGET_BOOL(entry, restore);
  62. KSET_BOOL(entry, restore);
  63. // Order
  64. KGET_BOOL(entry, order);
  65. KSET_BOOL(entry, order);
  66. // Filter
  67. KGET_BOOL(entry, filter);
  68. KSET_BOOL(entry, filter);
  69. // Nested ENTRYs list
  70. KGET(entry, faux_list_t *, entrys);
  71. static KCMP_NESTED(entry, entry, name);
  72. static KCMP_NESTED_BY_KEY(entry, entry, name);
  73. KADD_NESTED(entry, kentry_t *, entrys);
  74. KFIND_NESTED(entry, entry);
  75. KNESTED_LEN(entry, entrys);
  76. KNESTED_IS_EMPTY(entry, entrys);
  77. KNESTED_ITER(entry, entrys);
  78. KNESTED_EACH(entry, kentry_t *, entrys);
  79. // ACTION list
  80. KGET(entry, faux_list_t *, actions);
  81. KADD_NESTED(entry, kaction_t *, actions);
  82. KNESTED_LEN(entry, actions);
  83. KNESTED_ITER(entry, actions);
  84. KNESTED_EACH(entry, kaction_t *, actions);
  85. kentry_t *kentry_new(const char *name)
  86. {
  87. kentry_t *entry = NULL;
  88. if (faux_str_is_empty(name))
  89. return NULL;
  90. entry = faux_zmalloc(sizeof(*entry));
  91. assert(entry);
  92. if (!entry)
  93. return NULL;
  94. // Initialize
  95. entry->name = faux_str_dup(name);
  96. entry->help = NULL;
  97. entry->parent = NULL;
  98. entry->container = BOOL_FALSE;
  99. entry->mode = KENTRY_MODE_SEQUENCE;
  100. entry->purpose = KENTRY_PURPOSE_COMMON;
  101. entry->min = 1;
  102. entry->max = 1;
  103. entry->ref_str = NULL;
  104. entry->value = NULL;
  105. entry->restore = BOOL_FALSE;
  106. entry->order = BOOL_FALSE;
  107. entry->filter = BOOL_FALSE;
  108. // ENTRY list
  109. entry->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  110. kentry_entry_compare, kentry_entry_kcompare,
  111. (void (*)(void *))kentry_free);
  112. assert(entry->entrys);
  113. // ACTION list
  114. entry->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  115. NULL, NULL, (void (*)(void *))kaction_free);
  116. assert(entry->actions);
  117. faux_bzero(entry->nested_by_purpose, sizeof(entry->nested_by_purpose));
  118. return entry;
  119. }
  120. static void kentry_free_non_link(kentry_t *entry)
  121. {
  122. if (!entry)
  123. return;
  124. faux_list_free(entry->entrys);
  125. faux_list_free(entry->actions);
  126. }
  127. static void kentry_free_common(kentry_t *entry)
  128. {
  129. if (!entry)
  130. return;
  131. faux_str_free(entry->name);
  132. faux_str_free(entry->value);
  133. faux_str_free(entry->help);
  134. faux_str_free(entry->ref_str);
  135. }
  136. void kentry_free(kentry_t *entry)
  137. {
  138. if (!entry)
  139. return;
  140. // If ENTRY is not a link
  141. if (!kentry_ref_str(entry))
  142. kentry_free_non_link(entry);
  143. // For links and non-links
  144. kentry_free_common(entry);
  145. faux_free(entry);
  146. }
  147. bool_t kentry_link(kentry_t *dst, const kentry_t *src)
  148. {
  149. assert(dst);
  150. if (!dst)
  151. return BOOL_FALSE;
  152. assert(src);
  153. if (!src)
  154. return BOOL_FALSE;
  155. // Free all fields that will be linker to src later
  156. kentry_free_non_link(dst);
  157. // Copy structure by hand because else some fields must be
  158. // returned back anyway and temp memory must be allocated. I think it
  159. // worse.
  160. // name - orig
  161. // help - orig
  162. // parent - orig
  163. // container - orig
  164. dst->mode = src->mode;
  165. // purpose - orig
  166. // min - orig
  167. // max - orig
  168. // ref_str - orig
  169. // value - orig
  170. // restore - orig
  171. // order - orig
  172. dst->filter = src->filter;
  173. dst->entrys = src->entrys;
  174. dst->actions = src->actions;
  175. return BOOL_TRUE;
  176. }
  177. kentry_t *kentry_nested_by_purpose(const kentry_t *entry, kentry_purpose_e purpose)
  178. {
  179. assert(entry);
  180. if (!entry)
  181. return NULL;
  182. return entry->nested_by_purpose[purpose];
  183. }
  184. bool_t kentry_set_nested_by_purpose(kentry_t *entry, kentry_purpose_e purpose,
  185. kentry_t *nested)
  186. {
  187. assert(entry);
  188. if (!entry)
  189. return BOOL_FALSE;
  190. entry->nested_by_purpose[purpose] = nested;
  191. return BOOL_TRUE;
  192. }