1
0

param.c 12 KB

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