1
0

param.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * param.c
  3. *
  4. * This file provides the implementation of the "param" class
  5. */
  6. #include "private.h"
  7. #include "lub/string.h"
  8. #include "clish/types.h"
  9. #include <assert.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. /*--------------------------------------------------------- */
  14. static void clish_param_init(clish_param_t *this, const char *name,
  15. const char *text, const char *ptype_name)
  16. {
  17. this->name = lub_string_dup(name);
  18. this->text = lub_string_dup(text);
  19. this->ptype_name = lub_string_dup(ptype_name);
  20. /* Set up defaults */
  21. this->ptype = NULL;
  22. this->defval = NULL;
  23. this->mode = CLISH_PARAM_COMMON;
  24. this->optional = BOOL_FALSE;
  25. this->order = BOOL_FALSE;
  26. this->value = NULL;
  27. this->hidden = BOOL_FALSE;
  28. this->test = NULL;
  29. this->completion = NULL;
  30. this->access = NULL;
  31. this->paramv = clish_paramv_new();
  32. }
  33. /*--------------------------------------------------------- */
  34. static void clish_param_fini(clish_param_t * this)
  35. {
  36. /* deallocate the memory for this instance */
  37. lub_string_free(this->defval);
  38. lub_string_free(this->name);
  39. lub_string_free(this->text);
  40. lub_string_free(this->ptype_name);
  41. lub_string_free(this->value);
  42. lub_string_free(this->test);
  43. lub_string_free(this->completion);
  44. lub_string_free(this->access);
  45. clish_paramv_delete(this->paramv);
  46. }
  47. /*--------------------------------------------------------- */
  48. clish_param_t *clish_param_new(const char *name, const char *text,
  49. const char *ptype_name)
  50. {
  51. clish_param_t *this = malloc(sizeof(clish_param_t));
  52. if (this)
  53. clish_param_init(this, name, text, ptype_name);
  54. return this;
  55. }
  56. /*--------------------------------------------------------- */
  57. void clish_param_delete(clish_param_t * this)
  58. {
  59. clish_param_fini(this);
  60. free(this);
  61. }
  62. /*--------------------------------------------------------- */
  63. void clish_param_insert_param(clish_param_t * this, clish_param_t * param)
  64. {
  65. clish_paramv_insert(this->paramv, param);
  66. }
  67. /*--------------------------------------------------------- */
  68. char *clish_param_validate(const clish_param_t * this, const char *text)
  69. {
  70. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(this)) {
  71. if (lub_string_nocasecmp(clish_param__get_value(this), text))
  72. return NULL;
  73. }
  74. return clish_ptype_translate(this->ptype, text);
  75. }
  76. /*--------------------------------------------------------- */
  77. void clish_param_help(const clish_param_t * this, clish_help_t *help)
  78. {
  79. const char *range = clish_ptype__get_range(this->ptype);
  80. const char *name;
  81. char *str = NULL;
  82. if (CLISH_PARAM_SWITCH == clish_param__get_mode(this)) {
  83. unsigned rec_paramc = clish_param__get_param_count(this);
  84. clish_param_t *cparam;
  85. unsigned i;
  86. for (i = 0; i < rec_paramc; i++) {
  87. cparam = clish_param__get_param(this, i);
  88. if (!cparam)
  89. break;
  90. clish_param_help(cparam, help);
  91. }
  92. return;
  93. }
  94. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(this))
  95. name = clish_param__get_value(this);
  96. else
  97. if (!(name = clish_ptype__get_text(this->ptype)))
  98. name = clish_ptype__get_name(this->ptype);
  99. lub_string_cat(&str, this->text);
  100. if (range) {
  101. lub_string_cat(&str, " (");
  102. lub_string_cat(&str, range);
  103. lub_string_cat(&str, ")");
  104. }
  105. lub_argv_add(help->name, name);
  106. lub_argv_add(help->help, str);
  107. lub_string_free(str);
  108. lub_argv_add(help->detail, NULL);
  109. }
  110. /*--------------------------------------------------------- */
  111. void clish_param_help_arrow(const clish_param_t * this, size_t offset)
  112. {
  113. fprintf(stderr, "%*c\n", (int)offset, '^');
  114. this = this; /* Happy compiler */
  115. }
  116. CLISH_SET_STR(param, ptype_name);
  117. CLISH_GET_STR(param, ptype_name);
  118. CLISH_SET_STR(param, access);
  119. CLISH_GET_STR(param, access);
  120. CLISH_GET_STR(param, name);
  121. CLISH_GET_STR(param, text);
  122. CLISH_SET_STR_ONCE(param, value);
  123. CLISH_SET(param, clish_ptype_t *, ptype);
  124. CLISH_GET(param, clish_ptype_t *, ptype);
  125. CLISH_SET_STR_ONCE(param, defval);
  126. CLISH_GET_STR(param, defval);
  127. CLISH_SET_STR_ONCE(param, test);
  128. CLISH_GET_STR(param, test);
  129. CLISH_SET_STR_ONCE(param, completion);
  130. CLISH_SET(param, clish_param_mode_e, mode);
  131. CLISH_GET(param, clish_param_mode_e, mode);
  132. CLISH_GET(param, clish_paramv_t *, paramv);
  133. CLISH_SET(param, bool_t, optional);
  134. CLISH_GET(param, bool_t, optional);
  135. CLISH_SET(param, bool_t, order);
  136. CLISH_GET(param, bool_t, order);
  137. CLISH_SET(param, bool_t, hidden);
  138. CLISH_GET(param, bool_t, hidden);
  139. /*--------------------------------------------------------- */
  140. _CLISH_GET_STR(param, value)
  141. {
  142. assert(inst);
  143. if (inst->value)
  144. return inst->value;
  145. return inst->name;
  146. }
  147. /*--------------------------------------------------------- */
  148. _CLISH_GET_STR(param, range)
  149. {
  150. assert(inst);
  151. return clish_ptype__get_range(inst->ptype);
  152. }
  153. /*--------------------------------------------------------- */
  154. clish_param_t *clish_param__get_param(const clish_param_t * this,
  155. unsigned int index)
  156. {
  157. assert(this);
  158. return clish_paramv__get_param(this->paramv, index);
  159. }
  160. /*--------------------------------------------------------- */
  161. _CLISH_GET(param, unsigned int, param_count)
  162. {
  163. assert(inst);
  164. return clish_paramv__get_count(inst->paramv);
  165. }
  166. /*--------------------------------------------------------- */
  167. _CLISH_GET_STR(param, completion)
  168. {
  169. assert(inst);
  170. if (inst->completion)
  171. return inst->completion;
  172. // If param has no 'completion' field then get completion from ptype
  173. if (inst->ptype)
  174. return clish_ptype__get_completion(inst->ptype);
  175. return NULL;
  176. }