kentry.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. kentry_t *ref; // Resolved aliased ENTRY
  23. char *value; // Additional info
  24. bool_t restore; // Should entry restore its depth while execution
  25. faux_list_t *entrys; // Nested ENTRYs
  26. faux_list_t *actions; // Nested ACTIONs
  27. };
  28. // Simple methods
  29. // Name
  30. KGET_STR(entry, name);
  31. // Help
  32. KGET_STR(entry, help);
  33. KSET_STR(entry, help);
  34. // Parent
  35. KGET(entry, kentry_t *, parent);
  36. KSET(entry, kentry_t *, parent);
  37. // Container
  38. KGET_BOOL(entry, container);
  39. KSET_BOOL(entry, container);
  40. // Mode
  41. KGET(entry, kentry_mode_e, mode);
  42. KSET(entry, kentry_mode_e, mode);
  43. // Min occurs
  44. KGET(entry, size_t, min);
  45. KSET(entry, size_t, min);
  46. // Max occurs
  47. KGET(entry, size_t, max);
  48. KSET(entry, size_t, max);
  49. // PTYPE string (must be resolved later)
  50. KGET_STR(entry, ptype_str);
  51. KSET_STR(entry, ptype_str);
  52. // PTYPE (resolved)
  53. KGET(entry, kentry_t *, ptype);
  54. KSET(entry, kentry_t *, ptype);
  55. // Ref string (must be resolved later)
  56. KGET_STR(entry, ref_str);
  57. KSET_STR(entry, ref_str);
  58. // Ref (resolved)
  59. KGET(entry, kentry_t *, ref);
  60. KSET(entry, kentry_t *, ref);
  61. // Value
  62. KGET_STR(entry, value);
  63. KSET_STR(entry, value);
  64. // Restore
  65. KGET_BOOL(entry, restore);
  66. KSET_BOOL(entry, restore);
  67. // Nested ENTRYs list
  68. KGET(entry, faux_list_t *, entrys);
  69. static KCMP_NESTED(entry, entry, name);
  70. static KCMP_NESTED_BY_KEY(entry, entry, name);
  71. KADD_NESTED(entry, entry);
  72. KFIND_NESTED(entry, entry);
  73. KNESTED_ITER(entry, entry);
  74. KNESTED_EACH(entry, entry);
  75. // ACTION list
  76. KGET(entry, faux_list_t *, actions);
  77. KADD_NESTED(entry, action);
  78. KNESTED_LEN(entry, action);
  79. KNESTED_ITER(entry, action);
  80. KNESTED_EACH(entry, action);
  81. kentry_t *kentry_new(const char *name)
  82. {
  83. kentry_t *entry = NULL;
  84. if (faux_str_is_empty(name))
  85. return NULL;
  86. entry = faux_zmalloc(sizeof(*entry));
  87. assert(entry);
  88. if (!entry)
  89. return NULL;
  90. // Initialize
  91. entry->name = faux_str_dup(name);
  92. entry->help = NULL;
  93. entry->parent = NULL;
  94. entry->container = BOOL_FALSE;
  95. entry->mode = KENTRY_MODE_SWITCH;
  96. entry->min = 1;
  97. entry->max = 1;
  98. entry->ptype_str = NULL;
  99. entry->ptype = NULL;
  100. entry->ref_str = NULL;
  101. entry->ref = NULL;
  102. entry->value = NULL;
  103. entry->restore = BOOL_FALSE;
  104. // ENTRY list
  105. entry->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  106. kentry_entry_compare, kentry_entry_kcompare,
  107. (void (*)(void *))kentry_free);
  108. assert(entry->entrys);
  109. // ACTION list
  110. entry->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  111. NULL, NULL, (void (*)(void *))kaction_free);
  112. assert(entry->actions);
  113. return entry;
  114. }
  115. void kentry_free(kentry_t *entry)
  116. {
  117. if (!entry)
  118. return;
  119. faux_str_free(entry->name);
  120. faux_str_free(entry->help);
  121. faux_str_free(entry->ptype_str);
  122. faux_str_free(entry->ref_str);
  123. faux_str_free(entry->value);
  124. faux_list_free(entry->entrys);
  125. faux_list_free(entry->actions);
  126. faux_free(entry);
  127. }