pargv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * paramv.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include "lub/argv.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <assert.h>
  11. /*--------------------------------------------------------- */
  12. /*
  13. * Search for the specified parameter and return its value
  14. */
  15. static clish_parg_t *find_parg(clish_pargv_t * this, const char *name)
  16. {
  17. unsigned i;
  18. clish_parg_t *result = NULL;
  19. if (!this || !name)
  20. return NULL;
  21. /* scan the parameters in this instance */
  22. for (i = 0; i < this->pargc; i++) {
  23. clish_parg_t *parg = this->pargv[i];
  24. const char *pname = clish_param__get_name(parg->param);
  25. if (0 == strcmp(pname, name)) {
  26. result = parg;
  27. break;
  28. }
  29. }
  30. return result;
  31. }
  32. /*--------------------------------------------------------- */
  33. int clish_pargv_insert(clish_pargv_t * this,
  34. const clish_param_t * param, const char *value)
  35. {
  36. if (!this || !param)
  37. return -1;
  38. clish_parg_t *parg = find_parg(this, clish_param__get_name(param));
  39. if (NULL != parg) {
  40. /* release the current value */
  41. lub_string_free(parg->value);
  42. } else {
  43. size_t new_size = ((this->pargc + 1) * sizeof(clish_parg_t *));
  44. clish_parg_t **tmp;
  45. /* resize the parameter vector */
  46. tmp = realloc(this->pargv, new_size);
  47. this->pargv = tmp;
  48. /* insert reference to the parameter */
  49. parg = malloc(sizeof(*parg));
  50. this->pargv[this->pargc++] = parg;
  51. parg->param = param;
  52. }
  53. parg->value = NULL;
  54. if (value)
  55. parg->value = lub_string_dup(value);
  56. return 0;
  57. }
  58. /*--------------------------------------------------------- */
  59. static void set_defaults(clish_pargv_t * this, const clish_command_t * cmd)
  60. {
  61. unsigned index = 0;
  62. const clish_param_t *param;
  63. /* scan through all the parameters for this command */
  64. while ((param = clish_command__get_param(cmd, index++))) {
  65. const char *defval = clish_param__get_default(param);
  66. if (NULL != defval) {
  67. if ('\0' != *defval) {
  68. /* add the translated value to the vector */
  69. char *translated =
  70. clish_ptype_translate(clish_param__get_ptype
  71. (param),
  72. defval);
  73. clish_pargv_insert(this, param, translated);
  74. lub_string_free(translated);
  75. } else {
  76. /* insert the empty default */
  77. clish_pargv_insert(this, param, defval);
  78. }
  79. }
  80. }
  81. }
  82. /*--------------------------------------------------------- */
  83. clish_pargv_status_t
  84. clish_pargv_parse(clish_pargv_t * this,
  85. const clish_command_t * cmd,
  86. clish_paramv_t * paramv,
  87. const lub_argv_t * argv,
  88. unsigned *idx, clish_pargv_t * last, unsigned need_index)
  89. {
  90. unsigned start = *idx;
  91. unsigned argc = lub_argv__get_count(argv);
  92. unsigned index = 0;
  93. unsigned nopt_index = 0;
  94. clish_param_t *nopt_param = NULL;
  95. unsigned i;
  96. clish_pargv_status_t retval;
  97. unsigned paramc = clish_paramv__get_count(paramv);
  98. assert(this);
  99. while (index < paramc) {
  100. const char *arg;
  101. clish_param_t *param = clish_paramv__get_param(paramv,index);
  102. clish_param_t *cparam = NULL;
  103. int is_switch = 0;
  104. /* Use real arg or PARAM's default value as argument */
  105. if (*idx >= argc)
  106. arg = clish_param__get_default(param);
  107. else
  108. arg = lub_argv__get_arg(argv, *idx);
  109. /* Is parameter in "switch" mode? */
  110. if (CLISH_PARAM_SWITCH == clish_param__get_mode(param))
  111. is_switch = 1;
  112. /* Add param for help and completion */
  113. if (last && (*idx == need_index) &&
  114. (NULL == clish_pargv_find_arg(this, clish_param__get_name(param)))) {
  115. if (is_switch) {
  116. clish_paramv_t *rec_paramv = clish_param__get_paramv(param);
  117. unsigned rec_paramc = clish_param__get_param_count(param);
  118. for (i = 0; i < rec_paramc; i++) {
  119. cparam = clish_param__get_param(param, i);
  120. if (!cparam)
  121. break;
  122. if (CLISH_PARAM_SUBCOMMAND ==
  123. clish_param__get_mode(cparam)) {
  124. const char *pname =
  125. clish_param__get_value(cparam);
  126. if (!arg || (arg &&
  127. (pname == lub_string_nocasestr(pname,
  128. arg))))
  129. clish_pargv_insert(last,
  130. cparam, arg);
  131. } else {
  132. clish_pargv_insert(last,
  133. cparam, arg);
  134. }
  135. }
  136. } else {
  137. if (CLISH_PARAM_SUBCOMMAND ==
  138. clish_param__get_mode(param)) {
  139. const char *pname =
  140. clish_param__get_value(param);
  141. if (!arg || (arg &&
  142. (pname == lub_string_nocasestr(pname, arg))))
  143. clish_pargv_insert(last, param, arg);
  144. } else {
  145. clish_pargv_insert(last, param, arg);
  146. }
  147. }
  148. }
  149. /* Set parameter value */
  150. if (NULL != param) {
  151. char *validated = NULL;
  152. clish_paramv_t *rec_paramv =
  153. clish_param__get_paramv(param);
  154. unsigned rec_paramc =
  155. clish_param__get_param_count(param);
  156. /* Save the index of last non-option parameter
  157. * to restore index if the optional parameters
  158. * will be used.
  159. */
  160. if (BOOL_TRUE != clish_param__get_optional(param)) {
  161. nopt_param = param;
  162. nopt_index = index;
  163. }
  164. /* Validate the current parameter. */
  165. if (NULL != clish_pargv_find_arg(this, clish_param__get_name(param))) {
  166. /* Duplicated parameter */
  167. validated = NULL;
  168. } else if (is_switch) {
  169. for (i = 0; i < rec_paramc; i++) {
  170. cparam =
  171. clish_param__get_param(param, i);
  172. if (!cparam)
  173. break;
  174. if (validated =
  175. arg ? clish_param_validate(cparam,
  176. arg) :
  177. NULL) {
  178. rec_paramv =
  179. clish_param__get_paramv
  180. (cparam);
  181. rec_paramc =
  182. clish_param__get_param_count
  183. (cparam);
  184. break;
  185. }
  186. }
  187. } else {
  188. validated =
  189. arg ? clish_param_validate(param,
  190. arg) : NULL;
  191. }
  192. if (validated) {
  193. /* add (or update) this parameter */
  194. if (is_switch) {
  195. clish_pargv_insert(this, param,
  196. clish_param__get_name(cparam));
  197. clish_pargv_insert(this, cparam,
  198. validated);
  199. } else {
  200. clish_pargv_insert(this, param,
  201. validated);
  202. }
  203. lub_string_free(validated);
  204. /* Next command line argument */
  205. (*idx)++;
  206. /* Walk through the nested parameters */
  207. if (rec_paramc) {
  208. retval = clish_pargv_parse(this, NULL, rec_paramv,
  209. argv, idx, last, need_index);
  210. if (CLISH_LINE_OK != retval)
  211. return retval;
  212. }
  213. /* Choose the next parameter */
  214. if (BOOL_TRUE == clish_param__get_optional(param)) {
  215. if (nopt_param)
  216. index = nopt_index + 1;
  217. else
  218. index = 0;
  219. } else {
  220. index++;
  221. }
  222. } else {
  223. /* Choose the next parameter if current
  224. * is not validated.
  225. */
  226. if (BOOL_TRUE ==
  227. clish_param__get_optional(param))
  228. index++;
  229. else
  230. return CLISH_BAD_PARAM;
  231. }
  232. } else {
  233. return CLISH_BAD_PARAM;
  234. }
  235. }
  236. /* Check for non-optional parameters without values */
  237. if ((*idx >= argc) && (index < paramc)) {
  238. unsigned j = index;
  239. const clish_param_t *param;
  240. while (j < paramc) {
  241. param = clish_paramv__get_param(paramv, j++);
  242. if (BOOL_TRUE != clish_param__get_optional(param))
  243. return CLISH_BAD_PARAM;
  244. }
  245. }
  246. /* If the number of arguments is bigger than number of
  247. * params than it's a args. So generate the args entry
  248. * in the list of completions.
  249. */
  250. if (last &&
  251. cmd && clish_command__get_args(cmd) &&
  252. (clish_pargv__get_count(last) == 0) &&
  253. (*idx <= argc) && (index >= paramc)) {
  254. clish_pargv_insert(last, clish_command__get_args(cmd), "");
  255. }
  256. /*
  257. * if we've satisfied all the parameters we can now construct
  258. * an 'args' parameter if one exists
  259. */
  260. if (cmd && (*idx < argc) && (index >= paramc)) {
  261. const char *arg = lub_argv__get_arg(argv, *idx);
  262. const clish_param_t *param = clish_command__get_args(cmd);
  263. char *args = NULL;
  264. if (!param)
  265. return CLISH_BAD_PARAM;
  266. /*
  267. * put all the argument into a single string
  268. */
  269. while (NULL != arg) {
  270. bool_t quoted = lub_argv__get_quoted(argv, *idx);
  271. if (BOOL_TRUE == quoted) {
  272. lub_string_cat(&args, "\"");
  273. }
  274. /* place the current argument in the string */
  275. lub_string_cat(&args, arg);
  276. if (BOOL_TRUE == quoted) {
  277. lub_string_cat(&args, "\"");
  278. }
  279. (*idx)++;
  280. arg = lub_argv__get_arg(argv, *idx);
  281. if (NULL != arg) {
  282. /* add a space if there are more arguments */
  283. lub_string_cat(&args, " ");
  284. }
  285. }
  286. /* add (or update) this parameter */
  287. clish_pargv_insert(this, param, args);
  288. lub_string_free(args);
  289. }
  290. return CLISH_LINE_OK;
  291. }
  292. /*--------------------------------------------------------- */
  293. static clish_pargv_status_t
  294. clish_pargv_init(clish_pargv_t * this,
  295. const clish_command_t * cmd, const lub_argv_t * argv)
  296. {
  297. unsigned idx = lub_argv_wordcount(clish_command__get_name(cmd));
  298. this->pargc = 0;
  299. return clish_pargv_parse(this, cmd,
  300. clish_command__get_paramv(cmd),
  301. argv, &idx, NULL, 0);
  302. }
  303. /*--------------------------------------------------------- */
  304. clish_pargv_t *clish_pargv_create(void)
  305. {
  306. clish_pargv_t *this;
  307. this = malloc(sizeof(clish_pargv_t));
  308. this->pargc = 0;
  309. this->pargv = NULL;
  310. return this;
  311. }
  312. /*--------------------------------------------------------- */
  313. clish_pargv_t *clish_pargv_new(const clish_command_t * cmd,
  314. const char *line, size_t offset, clish_pargv_status_t * status)
  315. {
  316. clish_pargv_t *this = NULL;
  317. lub_argv_t *argv = NULL;
  318. if (!cmd || !line)
  319. return NULL;
  320. this = clish_pargv_create();
  321. if (!this)
  322. return NULL;
  323. argv = lub_argv_new(line, offset);
  324. *status = clish_pargv_init(this, cmd, argv);
  325. lub_argv_delete(argv);
  326. switch (*status) {
  327. case CLISH_LINE_OK:
  328. break;
  329. case CLISH_BAD_CMD:
  330. case CLISH_BAD_PARAM:
  331. case CLISH_BAD_HISTORY:
  332. /* commit suicide */
  333. clish_pargv_delete(this);
  334. this = NULL;
  335. break;
  336. }
  337. return this;
  338. }
  339. /*--------------------------------------------------------- */
  340. static void clish_pargv_fini(clish_pargv_t * this)
  341. {
  342. unsigned i;
  343. /* cleanup time */
  344. for (i = 0; i < this->pargc; i++) {
  345. lub_string_free(this->pargv[i]->value);
  346. this->pargv[i]->value = NULL;
  347. free(this->pargv[i]);
  348. }
  349. free(this->pargv);
  350. }
  351. /*--------------------------------------------------------- */
  352. void clish_pargv_delete(clish_pargv_t * this)
  353. {
  354. if (!this)
  355. return;
  356. clish_pargv_fini(this);
  357. free(this);
  358. }
  359. /*--------------------------------------------------------- */
  360. unsigned clish_pargv__get_count(clish_pargv_t * this)
  361. {
  362. if (!this)
  363. return 0;
  364. return this->pargc;
  365. }
  366. /*--------------------------------------------------------- */
  367. clish_parg_t *clish_pargv__get_parg(clish_pargv_t * this, unsigned index)
  368. {
  369. if (!this)
  370. return NULL;
  371. if (index > this->pargc)
  372. return NULL;
  373. return this->pargv[index];
  374. }
  375. /*--------------------------------------------------------- */
  376. const clish_param_t *clish_pargv__get_param(clish_pargv_t * this,
  377. unsigned index)
  378. {
  379. clish_parg_t *tmp;
  380. if (!this)
  381. return NULL;
  382. if (index >= this->pargc)
  383. return NULL;
  384. tmp = this->pargv[index];
  385. return tmp->param;
  386. }
  387. /*--------------------------------------------------------- */
  388. const char *clish_parg__get_value(const clish_parg_t * this)
  389. {
  390. if (!this)
  391. return NULL;
  392. return this->value;
  393. }
  394. /*--------------------------------------------------------- */
  395. const char *clish_parg__get_name(const clish_parg_t * this)
  396. {
  397. if (!this)
  398. return NULL;
  399. return clish_param__get_name(this->param);
  400. }
  401. /*--------------------------------------------------------- */
  402. const clish_ptype_t *clish_parg__get_ptype(const clish_parg_t * this)
  403. {
  404. if (!this)
  405. return NULL;
  406. return clish_param__get_ptype(this->param);
  407. }
  408. /*--------------------------------------------------------- */
  409. const clish_parg_t *clish_pargv_find_arg(clish_pargv_t * this, const char *name)
  410. {
  411. if (!this)
  412. return NULL;
  413. return find_parg(this, name);
  414. }
  415. /*--------------------------------------------------------- */