kentry.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 *ptype_str; // Text reference to PTYPE
  21. kentry_t *ptype; // Resolved entry's PTYPE
  22. char *ref_str; // Text reference to aliased ENTRY
  23. char *value; // Additional info
  24. bool_t restore; // Should entry restore its depth while execution
  25. bool_t order; // Is entry ordered
  26. bool_t filter; // Is entry filter. Filter can't have inline actions.
  27. faux_list_t *entrys; // Nested ENTRYs
  28. faux_list_t *actions; // Nested ACTIONs
  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. // PTYPE string (must be resolved later)
  55. KGET_STR(entry, ptype_str);
  56. KSET_STR(entry, ptype_str);
  57. // PTYPE (resolved)
  58. KGET(entry, kentry_t *, ptype);
  59. KSET(entry, kentry_t *, ptype);
  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. kentry_t *kentry_new(const char *name)
  92. {
  93. kentry_t *entry = NULL;
  94. if (faux_str_is_empty(name))
  95. return NULL;
  96. entry = faux_zmalloc(sizeof(*entry));
  97. assert(entry);
  98. if (!entry)
  99. return NULL;
  100. // Initialize
  101. entry->name = faux_str_dup(name);
  102. entry->help = NULL;
  103. entry->parent = NULL;
  104. entry->container = BOOL_FALSE;
  105. entry->mode = KENTRY_MODE_SEQUENCE;
  106. entry->purpose = KENTRY_PURPOSE_COMMON;
  107. entry->min = 1;
  108. entry->max = 1;
  109. entry->ptype_str = NULL;
  110. entry->ptype = NULL;
  111. entry->ref_str = NULL;
  112. entry->value = NULL;
  113. entry->restore = BOOL_FALSE;
  114. entry->order = BOOL_FALSE;
  115. entry->filter = BOOL_FALSE;
  116. // ENTRY list
  117. entry->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  118. kentry_entry_compare, kentry_entry_kcompare,
  119. (void (*)(void *))kentry_free);
  120. assert(entry->entrys);
  121. // ACTION list
  122. entry->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  123. NULL, NULL, (void (*)(void *))kaction_free);
  124. assert(entry->actions);
  125. return entry;
  126. }
  127. static void kentry_free_non_link(kentry_t *entry)
  128. {
  129. if (!entry)
  130. return;
  131. faux_str_free(entry->ptype_str);
  132. faux_list_free(entry->entrys);
  133. faux_list_free(entry->actions);
  134. }
  135. static void kentry_free_common(kentry_t *entry)
  136. {
  137. if (!entry)
  138. return;
  139. faux_str_free(entry->name);
  140. faux_str_free(entry->value);
  141. faux_str_free(entry->help);
  142. faux_str_free(entry->ref_str);
  143. }
  144. void kentry_free(kentry_t *entry)
  145. {
  146. if (!entry)
  147. return;
  148. // If ENTRY is not a link
  149. if (!kentry_ref_str(entry))
  150. kentry_free_non_link(entry);
  151. // For links and non-links
  152. kentry_free_common(entry);
  153. faux_free(entry);
  154. }
  155. bool_t kentry_link(kentry_t *dst, const kentry_t *src)
  156. {
  157. assert(dst);
  158. if (!dst)
  159. return BOOL_FALSE;
  160. assert(src);
  161. if (!src)
  162. return BOOL_FALSE;
  163. // Free all fields that will be linker to src later
  164. kentry_free_non_link(dst);
  165. // Copy structure by hand because else some fields must be
  166. // returned back anyway and temp memory must be allocated. I think it
  167. // worse.
  168. // name - orig
  169. // help - orig
  170. // parent - orig
  171. // container - orig
  172. dst->mode = src->mode;
  173. // purpose - orig
  174. // min - orig
  175. // max - orig
  176. dst->ptype_str = src->ptype_str;
  177. dst->ptype = src->ptype;
  178. // ref_str - orig
  179. // value - orig
  180. // restore - orig
  181. // order - orig
  182. dst->filter = src->filter;
  183. dst->entrys = src->entrys;
  184. dst->actions = src->actions;
  185. return BOOL_TRUE;
  186. }