pargv.c 12 KB

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