param.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. return 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_GET_STR(param, completion);
  131. CLISH_SET(param, clish_param_mode_e, mode);
  132. CLISH_GET(param, clish_param_mode_e, mode);
  133. CLISH_GET(param, clish_paramv_t *, paramv);
  134. CLISH_SET(param, bool_t, optional);
  135. CLISH_GET(param, bool_t, optional);
  136. CLISH_SET(param, bool_t, order);
  137. CLISH_GET(param, bool_t, order);
  138. CLISH_SET(param, bool_t, hidden);
  139. CLISH_GET(param, bool_t, hidden);
  140. /*--------------------------------------------------------- */
  141. _CLISH_GET_STR(param, value)
  142. {
  143. assert(inst);
  144. if (inst->value)
  145. return inst->value;
  146. return inst->name;
  147. }
  148. /*--------------------------------------------------------- */
  149. _CLISH_GET_STR(param, range)
  150. {
  151. assert(inst);
  152. return clish_ptype__get_range(inst->ptype);
  153. }
  154. /*--------------------------------------------------------- */
  155. clish_param_t *clish_param__get_param(const clish_param_t * this,
  156. unsigned int index)
  157. {
  158. assert(this);
  159. return clish_paramv__get_param(this->paramv, index);
  160. }
  161. /*--------------------------------------------------------- */
  162. _CLISH_GET(param, unsigned int, param_count)
  163. {
  164. assert(inst);
  165. return clish_paramv__get_count(inst->paramv);
  166. }