pargv.c 12 KB

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