pargv.c 11 KB

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