pargv.c 11 KB

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