param.c 9.6 KB

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