kentry.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. size_t min; // Min occurs of entry
  18. size_t max; // Max occurs of entry
  19. char *ptype_str; // Text reference to PTYPE
  20. kentry_t *ptype; // Resolved entry's PTYPE
  21. char *ref_str; // Text reference to aliased ENTRY
  22. char *value; // Additional info
  23. bool_t restore; // Should entry restore its depth while execution
  24. faux_list_t *entrys; // Nested ENTRYs
  25. faux_list_t *actions; // Nested ACTIONs
  26. };
  27. // Simple methods
  28. // Name
  29. KGET_STR(entry, name);
  30. // Help
  31. KGET_STR(entry, help);
  32. KSET_STR(entry, help);
  33. // Parent
  34. KGET(entry, kentry_t *, parent);
  35. KSET(entry, kentry_t *, parent);
  36. // Container
  37. KGET_BOOL(entry, container);
  38. KSET_BOOL(entry, container);
  39. // Mode
  40. KGET(entry, kentry_mode_e, mode);
  41. KSET(entry, kentry_mode_e, mode);
  42. // Min occurs
  43. KGET(entry, size_t, min);
  44. KSET(entry, size_t, min);
  45. // Max occurs
  46. KGET(entry, size_t, max);
  47. KSET(entry, size_t, max);
  48. // PTYPE string (must be resolved later)
  49. KGET_STR(entry, ptype_str);
  50. KSET_STR(entry, ptype_str);
  51. // PTYPE (resolved)
  52. KGET(entry, kentry_t *, ptype);
  53. KSET(entry, kentry_t *, ptype);
  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. // Nested ENTRYs list
  64. KGET(entry, faux_list_t *, entrys);
  65. static KCMP_NESTED(entry, entry, name);
  66. static KCMP_NESTED_BY_KEY(entry, entry, name);
  67. KADD_NESTED(entry, entry);
  68. KFIND_NESTED(entry, entry);
  69. KNESTED_LEN(entry, entry);
  70. KNESTED_IS_EMPTY(entry, entry);
  71. KNESTED_ITER(entry, entry);
  72. KNESTED_EACH(entry, entry);
  73. // ACTION list
  74. KGET(entry, faux_list_t *, actions);
  75. KADD_NESTED(entry, action);
  76. KNESTED_LEN(entry, action);
  77. KNESTED_ITER(entry, action);
  78. KNESTED_EACH(entry, action);
  79. kentry_t *kentry_new(const char *name)
  80. {
  81. kentry_t *entry = NULL;
  82. if (faux_str_is_empty(name))
  83. return NULL;
  84. entry = faux_zmalloc(sizeof(*entry));
  85. assert(entry);
  86. if (!entry)
  87. return NULL;
  88. // Initialize
  89. entry->name = faux_str_dup(name);
  90. entry->help = NULL;
  91. entry->parent = NULL;
  92. entry->container = BOOL_FALSE;
  93. entry->mode = KENTRY_MODE_SEQUENCE;
  94. entry->min = 1;
  95. entry->max = 1;
  96. entry->ptype_str = NULL;
  97. entry->ptype = NULL;
  98. entry->ref_str = NULL;
  99. entry->value = NULL;
  100. entry->restore = BOOL_FALSE;
  101. // ENTRY list
  102. entry->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  103. kentry_entry_compare, kentry_entry_kcompare,
  104. (void (*)(void *))kentry_free);
  105. assert(entry->entrys);
  106. // ACTION list
  107. entry->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  108. NULL, NULL, (void (*)(void *))kaction_free);
  109. assert(entry->actions);
  110. return entry;
  111. }
  112. static void kentry_free_non_link(kentry_t *entry)
  113. {
  114. if (!entry)
  115. return;
  116. faux_str_free(entry->ptype_str);
  117. faux_str_free(entry->value);
  118. faux_list_free(entry->entrys);
  119. faux_list_free(entry->actions);
  120. }
  121. static void kentry_free_common(kentry_t *entry)
  122. {
  123. if (!entry)
  124. return;
  125. faux_str_free(entry->name);
  126. faux_str_free(entry->help);
  127. faux_str_free(entry->ref_str);
  128. }
  129. void kentry_free(kentry_t *entry)
  130. {
  131. if (!entry)
  132. return;
  133. // If ENTRY is not a link
  134. if (!kentry_ref_str(entry))
  135. kentry_free_non_link(entry);
  136. // For links and non-links
  137. kentry_free_common(entry);
  138. faux_free(entry);
  139. }
  140. bool_t kentry_link(kentry_t *dst, const kentry_t *src)
  141. {
  142. assert(dst);
  143. if (!dst)
  144. return BOOL_FALSE;
  145. assert(src);
  146. if (!src)
  147. return BOOL_FALSE;
  148. // Free all fields that will be linker to src later
  149. kentry_free_non_link(dst);
  150. // Copy structure by hand because else some fields must be
  151. // returned back anyway and temp memory must be allocated. I think it
  152. // worse.
  153. // name - orig
  154. // help - orig
  155. // parent - orig
  156. dst->container = src->container;
  157. dst->mode = src->mode;
  158. dst->min = src->min;
  159. dst->max = src->max;
  160. dst->ptype_str = src->ptype_str;
  161. dst->ptype = src->ptype;
  162. // ref_str - orig
  163. dst->value = src->value;
  164. dst->restore = src->restore;
  165. dst->entrys = src->entrys;
  166. dst->actions = src->actions;
  167. return BOOL_TRUE;
  168. }