pargv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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_value(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_value(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 && (*idx < argc) && (index >= paramc)) {
  255. const char *arg = lub_argv__get_arg(argv, *idx);
  256. const clish_param_t *param = clish_command__get_args(cmd);
  257. char *args = NULL;
  258. if (!param)
  259. return CLISH_BAD_PARAM;
  260. /*
  261. * put all the argument into a single string
  262. */
  263. while (NULL != arg) {
  264. bool_t quoted = lub_argv__get_quoted(argv, *idx);
  265. if (BOOL_TRUE == quoted) {
  266. lub_string_cat(&args, "\"");
  267. }
  268. /* place the current argument in the string */
  269. lub_string_cat(&args, arg);
  270. if (BOOL_TRUE == quoted) {
  271. lub_string_cat(&args, "\"");
  272. }
  273. (*idx)++;
  274. arg = lub_argv__get_arg(argv, *idx);
  275. if (NULL != arg) {
  276. /* add a space if there are more arguments */
  277. lub_string_cat(&args, " ");
  278. }
  279. }
  280. /* add (or update) this parameter */
  281. clish_pargv_insert(this, param, args);
  282. lub_string_free(args);
  283. }
  284. return CLISH_LINE_OK;
  285. }
  286. /*--------------------------------------------------------- */
  287. static clish_pargv_status_t
  288. clish_pargv_init(clish_pargv_t * this,
  289. const clish_command_t * cmd, const lub_argv_t * argv)
  290. {
  291. unsigned idx = lub_argv_wordcount(clish_command__get_name(cmd));
  292. this->pargc = 0;
  293. return clish_pargv_parse(this, cmd,
  294. clish_command__get_paramv(cmd),
  295. argv, &idx, NULL, 0);
  296. }
  297. /*--------------------------------------------------------- */
  298. clish_pargv_t *clish_pargv_create(void)
  299. {
  300. clish_pargv_t *tmp;
  301. tmp = malloc(sizeof(clish_pargv_t));
  302. tmp->pargc = 0;
  303. tmp->pargv = NULL;
  304. return tmp;
  305. }
  306. /*--------------------------------------------------------- */
  307. clish_pargv_t *clish_pargv_new(const clish_command_t * cmd,
  308. const char *line,
  309. size_t offset, clish_pargv_status_t * status)
  310. {
  311. clish_pargv_t *this;
  312. lub_argv_t *argv = lub_argv_new(line, offset);
  313. this = clish_pargv_create();
  314. if (NULL != this) {
  315. *status = clish_pargv_init(this, cmd, argv);
  316. switch (*status) {
  317. case CLISH_LINE_OK:
  318. break;
  319. case CLISH_BAD_CMD:
  320. case CLISH_BAD_PARAM:
  321. case CLISH_BAD_HISTORY:
  322. /* commit suicide */
  323. clish_pargv_delete(this);
  324. this = NULL;
  325. break;
  326. }
  327. }
  328. /* cleanup */
  329. lub_argv_delete(argv);
  330. return this;
  331. }
  332. /*--------------------------------------------------------- */
  333. static void clish_pargv_fini(clish_pargv_t * this)
  334. {
  335. unsigned i;
  336. /* cleanup time */
  337. for (i = 0; i < this->pargc; i++) {
  338. lub_string_free(this->pargv[i]->value);
  339. this->pargv[i]->value = NULL;
  340. free(this->pargv[i]);
  341. }
  342. free(this->pargv);
  343. }
  344. /*--------------------------------------------------------- */
  345. void clish_pargv_delete(clish_pargv_t * this)
  346. {
  347. clish_pargv_fini(this);
  348. free(this);
  349. }
  350. /*--------------------------------------------------------- */
  351. unsigned clish_pargv__get_count(clish_pargv_t * this)
  352. {
  353. return this->pargc;
  354. }
  355. /*--------------------------------------------------------- */
  356. clish_parg_t *clish_pargv__get_parg(clish_pargv_t * this, unsigned index)
  357. {
  358. if (index > this->pargc)
  359. return NULL;
  360. return this->pargv[index];
  361. }
  362. /*--------------------------------------------------------- */
  363. const clish_param_t *clish_pargv__get_param(clish_pargv_t * this,
  364. unsigned index)
  365. {
  366. clish_parg_t *tmp;
  367. if (index >= this->pargc)
  368. return NULL;
  369. tmp = this->pargv[index];
  370. return tmp->param;
  371. }
  372. /*--------------------------------------------------------- */
  373. const char *clish_parg__get_value(const clish_parg_t * this)
  374. {
  375. return this->value;
  376. }
  377. /*--------------------------------------------------------- */
  378. const char *clish_parg__get_name(const clish_parg_t * this)
  379. {
  380. return clish_param__get_name(this->param);
  381. }
  382. /*--------------------------------------------------------- */
  383. const clish_ptype_t *clish_parg__get_ptype(const clish_parg_t * this)
  384. {
  385. return clish_param__get_ptype(this->param);
  386. }
  387. /*--------------------------------------------------------- */
  388. const clish_parg_t *clish_pargv_find_arg(clish_pargv_t * this, const char *name)
  389. {
  390. return find_parg(this, name);
  391. }
  392. /*--------------------------------------------------------- */