param.c 7.7 KB

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