1
0

pargv.c 12 KB

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