pargv.c 12 KB

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