param.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <assert.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. /*---------------------------------------------------------
  13. * PRIVATE METHODS
  14. *--------------------------------------------------------- */
  15. static void
  16. clish_param_init(clish_param_t * this,
  17. const char *name, const char *text, clish_ptype_t * ptype)
  18. {
  19. /* initialise the help part */
  20. this->name = lub_string_dup(name);
  21. this->text = lub_string_dup(text);
  22. this->ptype = ptype;
  23. /* set up defaults */
  24. this->defval = NULL;
  25. this->mode = CLISH_PARAM_COMMON;
  26. this->optional = BOOL_FALSE;
  27. this->value = NULL;
  28. this->hidden = BOOL_FALSE;
  29. this->paramv = clish_paramv_new();
  30. }
  31. /*--------------------------------------------------------- */
  32. static void clish_param_fini(clish_param_t * this)
  33. {
  34. unsigned i;
  35. /* deallocate the memory for this instance */
  36. lub_string_free(this->defval);
  37. this->defval = NULL;
  38. lub_string_free(this->name);
  39. this->name = NULL;
  40. lub_string_free(this->text);
  41. this->text = NULL;
  42. lub_string_free(this->value);
  43. this->value = NULL;
  44. clish_paramv_delete(this->paramv);
  45. }
  46. /*---------------------------------------------------------
  47. * PUBLIC META FUNCTIONS
  48. *--------------------------------------------------------- */
  49. clish_param_t *clish_param_new(const char *name,
  50. const char *text, clish_ptype_t * ptype)
  51. {
  52. clish_param_t *this = malloc(sizeof(clish_param_t));
  53. if (this) {
  54. clish_param_init(this, name, text, ptype);
  55. }
  56. return this;
  57. }
  58. /*---------------------------------------------------------
  59. * PUBLIC METHODS
  60. *--------------------------------------------------------- */
  61. void clish_param_delete(clish_param_t * this)
  62. {
  63. clish_param_fini(this);
  64. free(this);
  65. }
  66. /*--------------------------------------------------------- */
  67. void clish_param_insert_param(clish_param_t * this, clish_param_t * param)
  68. {
  69. return clish_paramv_insert(this->paramv, param);
  70. }
  71. /*---------------------------------------------------------
  72. * PUBLIC ATTRIBUTES
  73. *--------------------------------------------------------- */
  74. const char *clish_param__get_name(const clish_param_t * this)
  75. {
  76. if (!this)
  77. return NULL;
  78. return this->name;
  79. }
  80. /*--------------------------------------------------------- */
  81. const char *clish_param__get_text(const clish_param_t * this)
  82. {
  83. return this->text;
  84. }
  85. /*--------------------------------------------------------- */
  86. const char *clish_param__get_range(const clish_param_t * this)
  87. {
  88. return clish_ptype__get_range(this->ptype);
  89. }
  90. /*--------------------------------------------------------- */
  91. clish_ptype_t *clish_param__get_ptype(const clish_param_t * this)
  92. {
  93. return this->ptype;
  94. }
  95. /*--------------------------------------------------------- */
  96. void clish_param__set_default(clish_param_t * this, const char *defval)
  97. {
  98. assert(NULL == this->defval);
  99. this->defval = lub_string_dup(defval);
  100. }
  101. /*--------------------------------------------------------- */
  102. const char *clish_param__get_default(const clish_param_t * this)
  103. {
  104. return this->defval;
  105. }
  106. /*--------------------------------------------------------- */
  107. void clish_param__set_mode(clish_param_t * this, clish_param_mode_e mode)
  108. {
  109. assert(this);
  110. this->mode = mode;
  111. }
  112. /*--------------------------------------------------------- */
  113. clish_param_mode_e clish_param__get_mode(const clish_param_t * this)
  114. {
  115. return this->mode;
  116. }
  117. /*--------------------------------------------------------- */
  118. char *clish_param_validate(const clish_param_t * this, const char *text)
  119. {
  120. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(this)) {
  121. if (lub_string_nocasecmp(clish_param__get_value(this), text))
  122. return NULL;
  123. }
  124. return clish_ptype_translate(this->ptype, text);
  125. }
  126. /*--------------------------------------------------------- */
  127. void clish_param_help(const clish_param_t * this, size_t offset)
  128. {
  129. const char *range = clish_ptype__get_range(this->ptype);
  130. const char *name;
  131. if (CLISH_PARAM_SWITCH == clish_param__get_mode(this)) {
  132. unsigned rec_paramc = clish_param__get_param_count(this);
  133. clish_param_t *cparam;
  134. unsigned i;
  135. for (i = 0; i < rec_paramc; i++) {
  136. cparam = clish_param__get_param(this, i);
  137. if (!cparam)
  138. break;
  139. clish_param_help(cparam, offset);
  140. }
  141. return;
  142. }
  143. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(this))
  144. name = clish_param__get_value(this);
  145. else
  146. name = clish_ptype__get_text(this->ptype);
  147. fprintf(stderr, "%s %*c%s",
  148. name, (int)(offset - strlen(name)), ' ', this->text);
  149. if (NULL != range) {
  150. fprintf(stderr, " (%s)", range);
  151. }
  152. fprintf(stderr, "\n");
  153. }
  154. /*--------------------------------------------------------- */
  155. void clish_param_help_arrow(const clish_param_t * this, size_t offset)
  156. {
  157. fprintf(stderr, "%*c\n", (int)offset, '^');
  158. }
  159. /*--------------------------------------------------------- */
  160. clish_param_t *clish_param__get_param(const clish_param_t * this,
  161. unsigned index)
  162. {
  163. return clish_paramv__get_param(this->paramv, index);
  164. }
  165. /*--------------------------------------------------------- */
  166. clish_paramv_t *clish_param__get_paramv(clish_param_t * this)
  167. {
  168. return this->paramv;
  169. }
  170. /*--------------------------------------------------------- */
  171. const unsigned clish_param__get_param_count(const clish_param_t * this)
  172. {
  173. return clish_paramv__get_count(this->paramv);
  174. }
  175. /*--------------------------------------------------------- */
  176. void clish_param__set_optional(clish_param_t * this, bool_t optional)
  177. {
  178. this->optional = optional;
  179. }
  180. /*--------------------------------------------------------- */
  181. bool_t clish_param__get_optional(const clish_param_t * this)
  182. {
  183. return this->optional;
  184. }
  185. /*--------------------------------------------------------- */
  186. /* paramv methods */
  187. /*--------------------------------------------------------- */
  188. static void clish_paramv_init(clish_paramv_t * this)
  189. {
  190. this->paramc = 0;
  191. this->paramv = NULL;
  192. }
  193. /*--------------------------------------------------------- */
  194. static void clish_paramv_fini(clish_paramv_t * this)
  195. {
  196. unsigned i;
  197. /* finalize each of the parameter instances */
  198. for (i = 0; i < this->paramc; i++) {
  199. clish_param_delete(this->paramv[i]);
  200. }
  201. /* free the parameter vector */
  202. free(this->paramv);
  203. this->paramc = 0;
  204. }
  205. /*--------------------------------------------------------- */
  206. clish_paramv_t *clish_paramv_new(void)
  207. {
  208. clish_paramv_t *this = malloc(sizeof(clish_paramv_t));
  209. if (this) {
  210. clish_paramv_init(this);
  211. }
  212. return this;
  213. }
  214. /*--------------------------------------------------------- */
  215. void clish_paramv_delete(clish_paramv_t * this)
  216. {
  217. clish_paramv_fini(this);
  218. free(this);
  219. }
  220. /*--------------------------------------------------------- */
  221. void clish_paramv_insert(clish_paramv_t * this, clish_param_t * param)
  222. {
  223. size_t new_size = ((this->paramc + 1) * sizeof(clish_param_t *));
  224. clish_param_t **tmp;
  225. /* resize the parameter vector */
  226. tmp = realloc(this->paramv, new_size);
  227. if (NULL != tmp) {
  228. this->paramv = tmp;
  229. /* insert reference to the parameter */
  230. this->paramv[this->paramc++] = param;
  231. }
  232. }
  233. /*--------------------------------------------------------- */
  234. clish_param_t *clish_paramv__get_param(const clish_paramv_t * this,
  235. unsigned index)
  236. {
  237. clish_param_t *result = NULL;
  238. if (index < this->paramc) {
  239. result = this->paramv[index];
  240. }
  241. return result;
  242. }
  243. /*--------------------------------------------------------- */
  244. const unsigned clish_paramv__get_count(const clish_paramv_t * this)
  245. {
  246. return this->paramc;
  247. }
  248. /*--------------------------------------------------------- */
  249. void clish_param__set_value(clish_param_t * this, const char * value)
  250. {
  251. assert(NULL == this->value);
  252. this->value = lub_string_dup(value);
  253. }
  254. /*--------------------------------------------------------- */
  255. char *clish_param__get_value(const clish_param_t * this)
  256. {
  257. if (this->value)
  258. return this->value;
  259. return this->name;
  260. }
  261. /*--------------------------------------------------------- */
  262. void clish_param__set_hidden(clish_param_t * this, bool_t hidden)
  263. {
  264. this->hidden = hidden;
  265. }
  266. /*--------------------------------------------------------- */
  267. bool_t clish_param__get_hidden(const clish_param_t * this)
  268. {
  269. return this->hidden;
  270. }