param.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. unsigned i;
  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,
  54. const char *text, clish_ptype_t * ptype)
  55. {
  56. clish_param_t *this = malloc(sizeof(clish_param_t));
  57. if (this) {
  58. clish_param_init(this, name, text, ptype);
  59. }
  60. return this;
  61. }
  62. /*---------------------------------------------------------
  63. * PUBLIC METHODS
  64. *--------------------------------------------------------- */
  65. void clish_param_delete(clish_param_t * this)
  66. {
  67. clish_param_fini(this);
  68. free(this);
  69. }
  70. /*--------------------------------------------------------- */
  71. void clish_param_insert_param(clish_param_t * this, clish_param_t * param)
  72. {
  73. return clish_paramv_insert(this->paramv, param);
  74. }
  75. /*---------------------------------------------------------
  76. * PUBLIC ATTRIBUTES
  77. *--------------------------------------------------------- */
  78. const char *clish_param__get_name(const clish_param_t * this)
  79. {
  80. if (!this)
  81. return NULL;
  82. return this->name;
  83. }
  84. /*--------------------------------------------------------- */
  85. const char *clish_param__get_text(const clish_param_t * this)
  86. {
  87. return this->text;
  88. }
  89. /*--------------------------------------------------------- */
  90. const char *clish_param__get_range(const clish_param_t * this)
  91. {
  92. return clish_ptype__get_range(this->ptype);
  93. }
  94. /*--------------------------------------------------------- */
  95. clish_ptype_t *clish_param__get_ptype(const clish_param_t * this)
  96. {
  97. return this->ptype;
  98. }
  99. /*--------------------------------------------------------- */
  100. void clish_param__set_default(clish_param_t * this, const char *defval)
  101. {
  102. assert(NULL == this->defval);
  103. this->defval = lub_string_dup(defval);
  104. }
  105. /*--------------------------------------------------------- */
  106. const char *clish_param__get_default(const clish_param_t * this)
  107. {
  108. return this->defval;
  109. }
  110. /*--------------------------------------------------------- */
  111. void clish_param__set_mode(clish_param_t * this, clish_param_mode_e mode)
  112. {
  113. assert(this);
  114. this->mode = mode;
  115. }
  116. /*--------------------------------------------------------- */
  117. clish_param_mode_e clish_param__get_mode(const clish_param_t * this)
  118. {
  119. return this->mode;
  120. }
  121. /*--------------------------------------------------------- */
  122. char *clish_param_validate(const clish_param_t * this, const char *text)
  123. {
  124. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(this)) {
  125. if (lub_string_nocasecmp(clish_param__get_value(this), text))
  126. return NULL;
  127. }
  128. return clish_ptype_translate(this->ptype, text);
  129. }
  130. /*--------------------------------------------------------- */
  131. void clish_param_help(const clish_param_t * this, size_t offset)
  132. {
  133. const char *range = clish_ptype__get_range(this->ptype);
  134. const char *name;
  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, offset);
  144. }
  145. return;
  146. }
  147. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(this))
  148. name = clish_param__get_value(this);
  149. else
  150. name = clish_ptype__get_text(this->ptype);
  151. fprintf(stderr, "%s %*c%s",
  152. name, (int)(offset - strlen(name)), ' ', this->text);
  153. if (NULL != range) {
  154. fprintf(stderr, " (%s)", range);
  155. }
  156. fprintf(stderr, "\n");
  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. const unsigned 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. }
  216. return this;
  217. }
  218. /*--------------------------------------------------------- */
  219. void clish_paramv_delete(clish_paramv_t * this)
  220. {
  221. clish_paramv_fini(this);
  222. free(this);
  223. }
  224. /*--------------------------------------------------------- */
  225. void clish_paramv_insert(clish_paramv_t * this, clish_param_t * param)
  226. {
  227. size_t new_size = ((this->paramc + 1) * sizeof(clish_param_t *));
  228. clish_param_t **tmp;
  229. /* resize the parameter vector */
  230. tmp = realloc(this->paramv, new_size);
  231. if (NULL != tmp) {
  232. this->paramv = tmp;
  233. /* insert reference to the parameter */
  234. this->paramv[this->paramc++] = param;
  235. }
  236. }
  237. /*--------------------------------------------------------- */
  238. clish_param_t *clish_paramv__get_param(const clish_paramv_t * this,
  239. unsigned index)
  240. {
  241. clish_param_t *result = NULL;
  242. if (index < this->paramc) {
  243. result = this->paramv[index];
  244. }
  245. return result;
  246. }
  247. /*--------------------------------------------------------- */
  248. const unsigned clish_paramv__get_count(const clish_paramv_t * this)
  249. {
  250. return this->paramc;
  251. }
  252. /*--------------------------------------------------------- */
  253. void clish_param__set_value(clish_param_t * this, const char * value)
  254. {
  255. assert(NULL == this->value);
  256. this->value = lub_string_dup(value);
  257. }
  258. /*--------------------------------------------------------- */
  259. char *clish_param__get_value(const clish_param_t * this)
  260. {
  261. if (this->value)
  262. return this->value;
  263. return this->name;
  264. }
  265. /*--------------------------------------------------------- */
  266. void clish_param__set_hidden(clish_param_t * this, bool_t hidden)
  267. {
  268. this->hidden = hidden;
  269. }
  270. /*--------------------------------------------------------- */
  271. bool_t clish_param__get_hidden(const clish_param_t * this)
  272. {
  273. return this->hidden;
  274. }
  275. /*--------------------------------------------------------- */
  276. void clish_param__set_test(clish_param_t * this, const char *test)
  277. {
  278. assert(NULL == this->test);
  279. this->test = lub_string_dup(test);
  280. }
  281. /*--------------------------------------------------------- */
  282. char *clish_param__get_test(const clish_param_t * this,
  283. const char * viewid, const clish_command_t * cmd,
  284. clish_pargv_t * pargv)
  285. {
  286. if (!this->test)
  287. return NULL;
  288. return clish_variable_expand(this->test, viewid, cmd, pargv);
  289. }