param.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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->access = NULL;
  33. this->paramv = clish_paramv_new();
  34. }
  35. /*--------------------------------------------------------- */
  36. static void clish_param_fini(clish_param_t * this)
  37. {
  38. /* deallocate the memory for this instance */
  39. lub_string_free(this->defval);
  40. lub_string_free(this->name);
  41. lub_string_free(this->text);
  42. lub_string_free(this->value);
  43. lub_string_free(this->test);
  44. lub_string_free(this->completion);
  45. lub_string_free(this->access);
  46. clish_paramv_delete(this->paramv);
  47. }
  48. /*---------------------------------------------------------
  49. * PUBLIC META FUNCTIONS
  50. *--------------------------------------------------------- */
  51. clish_param_t *clish_param_new(const char *name, const char *text,
  52. clish_ptype_t *ptype)
  53. {
  54. clish_param_t *this = malloc(sizeof(clish_param_t));
  55. if (this)
  56. clish_param_init(this, name, text, ptype);
  57. return this;
  58. }
  59. /*---------------------------------------------------------
  60. * PUBLIC METHODS
  61. *--------------------------------------------------------- */
  62. void clish_param_delete(clish_param_t * this)
  63. {
  64. clish_param_fini(this);
  65. free(this);
  66. }
  67. /*--------------------------------------------------------- */
  68. void clish_param_insert_param(clish_param_t * this, clish_param_t * param)
  69. {
  70. return clish_paramv_insert(this->paramv, param);
  71. }
  72. /*---------------------------------------------------------
  73. * PUBLIC ATTRIBUTES
  74. *--------------------------------------------------------- */
  75. const char *clish_param__get_name(const clish_param_t * this)
  76. {
  77. if (!this)
  78. return NULL;
  79. return this->name;
  80. }
  81. /*--------------------------------------------------------- */
  82. const char *clish_param__get_text(const clish_param_t * this)
  83. {
  84. return this->text;
  85. }
  86. /*--------------------------------------------------------- */
  87. const char *clish_param__get_range(const clish_param_t * this)
  88. {
  89. return clish_ptype__get_range(this->ptype);
  90. }
  91. /*--------------------------------------------------------- */
  92. clish_ptype_t *clish_param__get_ptype(const clish_param_t * this)
  93. {
  94. return this->ptype;
  95. }
  96. /*--------------------------------------------------------- */
  97. void clish_param__set_default(clish_param_t * this, const char *defval)
  98. {
  99. assert(!this->defval);
  100. this->defval = lub_string_dup(defval);
  101. }
  102. /*--------------------------------------------------------- */
  103. const char *clish_param__get_default(const clish_param_t * this)
  104. {
  105. return this->defval;
  106. }
  107. /*--------------------------------------------------------- */
  108. void clish_param__set_mode(clish_param_t * this, clish_param_mode_e mode)
  109. {
  110. assert(this);
  111. this->mode = mode;
  112. }
  113. /*--------------------------------------------------------- */
  114. clish_param_mode_e clish_param__get_mode(const clish_param_t * this)
  115. {
  116. return this->mode;
  117. }
  118. /*--------------------------------------------------------- */
  119. char *clish_param_validate(const clish_param_t * this, const char *text)
  120. {
  121. if (CLISH_PARAM_SUBCOMMAND == clish_param__get_mode(this)) {
  122. if (lub_string_nocasecmp(clish_param__get_value(this), text))
  123. return NULL;
  124. }
  125. return clish_ptype_translate(this->ptype, text);
  126. }
  127. /*--------------------------------------------------------- */
  128. void clish_param_help(const clish_param_t * this, clish_help_t *help)
  129. {
  130. const char *range = clish_ptype__get_range(this->ptype);
  131. const char *name;
  132. char *str = NULL;
  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, help);
  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. lub_string_cat(&str, this->text);
  151. if (range) {
  152. lub_string_cat(&str, " (");
  153. lub_string_cat(&str, range);
  154. lub_string_cat(&str, ")");
  155. }
  156. lub_argv_add(help->name, name);
  157. lub_argv_add(help->help, str);
  158. lub_string_free(str);
  159. lub_argv_add(help->detail, NULL);
  160. }
  161. /*--------------------------------------------------------- */
  162. void clish_param_help_arrow(const clish_param_t * this, size_t offset)
  163. {
  164. fprintf(stderr, "%*c\n", (int)offset, '^');
  165. this = this; /* Happy compiler */
  166. }
  167. /*--------------------------------------------------------- */
  168. clish_param_t *clish_param__get_param(const clish_param_t * this,
  169. unsigned index)
  170. {
  171. return clish_paramv__get_param(this->paramv, index);
  172. }
  173. /*--------------------------------------------------------- */
  174. clish_paramv_t *clish_param__get_paramv(clish_param_t * this)
  175. {
  176. return this->paramv;
  177. }
  178. /*--------------------------------------------------------- */
  179. unsigned int clish_param__get_param_count(const clish_param_t * this)
  180. {
  181. return clish_paramv__get_count(this->paramv);
  182. }
  183. /*--------------------------------------------------------- */
  184. void clish_param__set_optional(clish_param_t * this, bool_t optional)
  185. {
  186. this->optional = optional;
  187. }
  188. /*--------------------------------------------------------- */
  189. bool_t clish_param__get_optional(const clish_param_t * this)
  190. {
  191. return this->optional;
  192. }
  193. /*--------------------------------------------------------- */
  194. void clish_param__set_order(clish_param_t * this, bool_t order)
  195. {
  196. this->order = order;
  197. }
  198. /*--------------------------------------------------------- */
  199. bool_t clish_param__get_order(const clish_param_t * this)
  200. {
  201. return this->order;
  202. }
  203. /*--------------------------------------------------------- */
  204. /* paramv methods */
  205. /*--------------------------------------------------------- */
  206. static void clish_paramv_init(clish_paramv_t * this)
  207. {
  208. this->paramc = 0;
  209. this->paramv = NULL;
  210. }
  211. /*--------------------------------------------------------- */
  212. static void clish_paramv_fini(clish_paramv_t * this)
  213. {
  214. unsigned i;
  215. /* finalize each of the parameter instances */
  216. for (i = 0; i < this->paramc; i++) {
  217. clish_param_delete(this->paramv[i]);
  218. }
  219. /* free the parameter vector */
  220. free(this->paramv);
  221. this->paramc = 0;
  222. }
  223. /*--------------------------------------------------------- */
  224. clish_paramv_t *clish_paramv_new(void)
  225. {
  226. clish_paramv_t *this = malloc(sizeof(clish_paramv_t));
  227. if (this)
  228. clish_paramv_init(this);
  229. return this;
  230. }
  231. /*--------------------------------------------------------- */
  232. void clish_paramv_delete(clish_paramv_t * this)
  233. {
  234. clish_paramv_fini(this);
  235. free(this);
  236. }
  237. /*--------------------------------------------------------- */
  238. void clish_paramv_insert(clish_paramv_t * this, clish_param_t * param)
  239. {
  240. size_t new_size = ((this->paramc + 1) * sizeof(clish_param_t *));
  241. clish_param_t **tmp;
  242. /* resize the parameter vector */
  243. tmp = realloc(this->paramv, new_size);
  244. if (tmp) {
  245. this->paramv = tmp;
  246. /* insert reference to the parameter */
  247. this->paramv[this->paramc++] = param;
  248. }
  249. }
  250. /*--------------------------------------------------------- */
  251. int clish_paramv_remove(clish_paramv_t *this, unsigned int index)
  252. {
  253. size_t new_size;
  254. clish_param_t **tmp;
  255. clish_param_t **dst, **src;
  256. size_t n;
  257. if (this->paramc < 1)
  258. return -1;
  259. if (index >= this->paramc)
  260. return -1;
  261. new_size = ((this->paramc - 1) * sizeof(clish_param_t *));
  262. dst = this->paramv + index;
  263. src = dst + 1;
  264. n = this->paramc - index - 1;
  265. if (n)
  266. memmove(dst, src, n * sizeof(clish_param_t *));
  267. /* Resize the parameter vector */
  268. if (new_size) {
  269. tmp = realloc(this->paramv, new_size);
  270. if (!tmp)
  271. return -1;
  272. this->paramv = tmp;
  273. } else {
  274. free(this->paramv);
  275. this->paramv = NULL;
  276. }
  277. this->paramc--;
  278. return 0;
  279. }
  280. /*--------------------------------------------------------- */
  281. clish_param_t *clish_paramv__get_param(const clish_paramv_t * this,
  282. unsigned int index)
  283. {
  284. clish_param_t *result = NULL;
  285. if (index < this->paramc)
  286. result = this->paramv[index];
  287. return result;
  288. }
  289. /*--------------------------------------------------------- */
  290. clish_param_t *clish_paramv_find_param(const clish_paramv_t * this,
  291. const char *name)
  292. {
  293. clish_param_t *res = NULL;
  294. unsigned int i;
  295. for (i = 0; i < this->paramc; i++) {
  296. if (!strcmp(clish_param__get_name(this->paramv[i]), name))
  297. return this->paramv[i];
  298. if ((res = clish_paramv_find_param(
  299. clish_param__get_paramv(this->paramv[i]), name)))
  300. return res;
  301. }
  302. return res;
  303. }
  304. /*--------------------------------------------------------- */
  305. const char *clish_paramv_find_default(const clish_paramv_t * this,
  306. const char *name)
  307. {
  308. clish_param_t *res = clish_paramv_find_param(this, name);
  309. if (res)
  310. return clish_param__get_default(res);
  311. return NULL;
  312. }
  313. /*--------------------------------------------------------- */
  314. unsigned int clish_paramv__get_count(const clish_paramv_t * this)
  315. {
  316. return this->paramc;
  317. }
  318. /*--------------------------------------------------------- */
  319. void clish_param__set_value(clish_param_t * this, const char * value)
  320. {
  321. assert(!this->value);
  322. this->value = lub_string_dup(value);
  323. }
  324. /*--------------------------------------------------------- */
  325. char *clish_param__get_value(const clish_param_t * this)
  326. {
  327. if (this->value)
  328. return this->value;
  329. return this->name;
  330. }
  331. /*--------------------------------------------------------- */
  332. void clish_param__set_hidden(clish_param_t * this, bool_t hidden)
  333. {
  334. this->hidden = hidden;
  335. }
  336. /*--------------------------------------------------------- */
  337. bool_t clish_param__get_hidden(const clish_param_t * this)
  338. {
  339. return this->hidden;
  340. }
  341. /*--------------------------------------------------------- */
  342. void clish_param__set_test(clish_param_t * this, const char *test)
  343. {
  344. assert(!this->test);
  345. this->test = lub_string_dup(test);
  346. }
  347. /*--------------------------------------------------------- */
  348. char *clish_param__get_test(const clish_param_t *this)
  349. {
  350. return this->test;
  351. }
  352. /*--------------------------------------------------------- */
  353. void clish_param__set_completion(clish_param_t *this, const char *completion)
  354. {
  355. assert(!this->completion);
  356. this->completion = lub_string_dup(completion);
  357. }
  358. /*--------------------------------------------------------- */
  359. char *clish_param__get_completion(const clish_param_t *this)
  360. {
  361. return this->completion;
  362. }
  363. /*--------------------------------------------------------- */
  364. void clish_param__set_access(clish_param_t *this, const char *access)
  365. {
  366. if (this->access)
  367. lub_string_free(this->access);
  368. this->access = lub_string_dup(access);
  369. }
  370. /*--------------------------------------------------------- */
  371. char *clish_param__get_access(const clish_param_t *this)
  372. {
  373. return this->access;
  374. }