param.c 9.4 KB

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