pargv.c 12 KB

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