pargv.c 11 KB

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