1
0

pargv.c 11 KB

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