param.c 8.6 KB

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