param.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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->order = BOOL_FALSE;
  28. this->value = NULL;
  29. this->hidden = BOOL_FALSE;
  30. this->test = NULL;
  31. this->completion = 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. lub_string_free(this->name);
  40. lub_string_free(this->text);
  41. lub_string_free(this->value);
  42. lub_string_free(this->test);
  43. lub_string_free(this->completion);
  44. clish_paramv_delete(this->paramv);
  45. }
  46. /*---------------------------------------------------------
  47. * PUBLIC META FUNCTIONS
  48. *--------------------------------------------------------- */
  49. clish_param_t *clish_param_new(const char *name, const char *text,
  50. 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. 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(!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, clish_help_t *help)
  127. {
  128. const char *range = clish_ptype__get_range(this->ptype);
  129. const char *name;
  130. char *str = NULL;
  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, help);
  140. }
  141. return;
  142. }
  143. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(this))
  144. name = clish_param__get_value(this);
  145. else
  146. if (!(name = clish_ptype__get_text(this->ptype)))
  147. name = clish_ptype__get_name(this->ptype);
  148. lub_string_cat(&str, this->text);
  149. if (range) {
  150. lub_string_cat(&str, " (");
  151. lub_string_cat(&str, range);
  152. lub_string_cat(&str, ")");
  153. }
  154. lub_argv_add(help->name, name);
  155. lub_argv_add(help->help, str);
  156. lub_string_free(str);
  157. lub_argv_add(help->detail, NULL);
  158. }
  159. /*--------------------------------------------------------- */
  160. void clish_param_help_arrow(const clish_param_t * this, size_t offset)
  161. {
  162. fprintf(stderr, "%*c\n", (int)offset, '^');
  163. this = this; /* Happy compiler */
  164. }
  165. /*--------------------------------------------------------- */
  166. clish_param_t *clish_param__get_param(const clish_param_t * this,
  167. unsigned index)
  168. {
  169. return clish_paramv__get_param(this->paramv, index);
  170. }
  171. /*--------------------------------------------------------- */
  172. clish_paramv_t *clish_param__get_paramv(clish_param_t * this)
  173. {
  174. return this->paramv;
  175. }
  176. /*--------------------------------------------------------- */
  177. unsigned int clish_param__get_param_count(const clish_param_t * this)
  178. {
  179. return clish_paramv__get_count(this->paramv);
  180. }
  181. /*--------------------------------------------------------- */
  182. void clish_param__set_optional(clish_param_t * this, bool_t optional)
  183. {
  184. this->optional = optional;
  185. }
  186. /*--------------------------------------------------------- */
  187. bool_t clish_param__get_optional(const clish_param_t * this)
  188. {
  189. return this->optional;
  190. }
  191. /*--------------------------------------------------------- */
  192. void clish_param__set_order(clish_param_t * this, bool_t order)
  193. {
  194. this->order = order;
  195. }
  196. /*--------------------------------------------------------- */
  197. bool_t clish_param__get_order(const clish_param_t * this)
  198. {
  199. return this->order;
  200. }
  201. /*--------------------------------------------------------- */
  202. /* paramv methods */
  203. /*--------------------------------------------------------- */
  204. static void clish_paramv_init(clish_paramv_t * this)
  205. {
  206. this->paramc = 0;
  207. this->paramv = NULL;
  208. }
  209. /*--------------------------------------------------------- */
  210. static void clish_paramv_fini(clish_paramv_t * this)
  211. {
  212. unsigned i;
  213. /* finalize each of the parameter instances */
  214. for (i = 0; i < this->paramc; i++) {
  215. clish_param_delete(this->paramv[i]);
  216. }
  217. /* free the parameter vector */
  218. free(this->paramv);
  219. this->paramc = 0;
  220. }
  221. /*--------------------------------------------------------- */
  222. clish_paramv_t *clish_paramv_new(void)
  223. {
  224. clish_paramv_t *this = malloc(sizeof(clish_paramv_t));
  225. if (this)
  226. clish_paramv_init(this);
  227. return this;
  228. }
  229. /*--------------------------------------------------------- */
  230. void clish_paramv_delete(clish_paramv_t * this)
  231. {
  232. clish_paramv_fini(this);
  233. free(this);
  234. }
  235. /*--------------------------------------------------------- */
  236. void clish_paramv_insert(clish_paramv_t * this, clish_param_t * param)
  237. {
  238. size_t new_size = ((this->paramc + 1) * sizeof(clish_param_t *));
  239. clish_param_t **tmp;
  240. /* resize the parameter vector */
  241. tmp = realloc(this->paramv, new_size);
  242. if (tmp) {
  243. this->paramv = tmp;
  244. /* insert reference to the parameter */
  245. this->paramv[this->paramc++] = param;
  246. }
  247. }
  248. /*--------------------------------------------------------- */
  249. clish_param_t *clish_paramv__get_param(const clish_paramv_t * this,
  250. unsigned index)
  251. {
  252. clish_param_t *result = NULL;
  253. if (index < this->paramc)
  254. result = this->paramv[index];
  255. return result;
  256. }
  257. /*--------------------------------------------------------- */
  258. clish_param_t *clish_paramv_find_param(const clish_paramv_t * this,
  259. const char *name)
  260. {
  261. clish_param_t *res = NULL;
  262. unsigned int i;
  263. for (i = 0; i < this->paramc; i++) {
  264. if (!strcmp(clish_param__get_name(this->paramv[i]), name))
  265. return this->paramv[i];
  266. if ((res = clish_paramv_find_param(
  267. clish_param__get_paramv(this->paramv[i]), name)))
  268. return res;
  269. }
  270. return res;
  271. }
  272. /*--------------------------------------------------------- */
  273. const char *clish_paramv_find_default(const clish_paramv_t * this,
  274. const char *name)
  275. {
  276. clish_param_t *res = clish_paramv_find_param(this, name);
  277. if (res)
  278. return clish_param__get_default(res);
  279. return NULL;
  280. }
  281. /*--------------------------------------------------------- */
  282. unsigned int clish_paramv__get_count(const clish_paramv_t * this)
  283. {
  284. return this->paramc;
  285. }
  286. /*--------------------------------------------------------- */
  287. void clish_param__set_value(clish_param_t * this, const char * value)
  288. {
  289. assert(!this->value);
  290. this->value = lub_string_dup(value);
  291. }
  292. /*--------------------------------------------------------- */
  293. char *clish_param__get_value(const clish_param_t * this)
  294. {
  295. if (this->value)
  296. return this->value;
  297. return this->name;
  298. }
  299. /*--------------------------------------------------------- */
  300. void clish_param__set_hidden(clish_param_t * this, bool_t hidden)
  301. {
  302. this->hidden = hidden;
  303. }
  304. /*--------------------------------------------------------- */
  305. bool_t clish_param__get_hidden(const clish_param_t * this)
  306. {
  307. return this->hidden;
  308. }
  309. /*--------------------------------------------------------- */
  310. void clish_param__set_test(clish_param_t * this, const char *test)
  311. {
  312. assert(!this->test);
  313. this->test = lub_string_dup(test);
  314. }
  315. /*--------------------------------------------------------- */
  316. char *clish_param__get_test(const clish_param_t *this)
  317. {
  318. return this->test;
  319. }
  320. /*--------------------------------------------------------- */
  321. void clish_param__set_completion(clish_param_t *this, const char *completion)
  322. {
  323. assert(!this->completion);
  324. this->completion = lub_string_dup(completion);
  325. }
  326. /*--------------------------------------------------------- */
  327. char *clish_param__get_completion(const clish_param_t *this)
  328. {
  329. return this->completion;
  330. }