pargv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 (NULL != 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. (*idx)++;
  223. /* Walk through the nested parameters */
  224. if (rec_paramc) {
  225. retval = clish_pargv_parse(this, cmd,
  226. viewid, rec_paramv,
  227. argv, idx, last, need_index);
  228. if (CLISH_LINE_OK != retval)
  229. return retval;
  230. }
  231. /* Choose the next parameter */
  232. if (BOOL_TRUE == clish_param__get_optional(param)) {
  233. if (nopt_param)
  234. index = nopt_index + 1;
  235. else
  236. index = 0;
  237. } else {
  238. index++;
  239. }
  240. } else {
  241. /* Choose the next parameter if current
  242. * is not validated.
  243. */
  244. if (BOOL_TRUE ==
  245. clish_param__get_optional(param))
  246. index++;
  247. else {
  248. if (!arg)
  249. break;
  250. else
  251. return CLISH_BAD_PARAM;
  252. }
  253. }
  254. } else {
  255. return CLISH_BAD_PARAM;
  256. }
  257. }
  258. /* Check for non-optional parameters without values */
  259. if ((*idx >= argc) && (index < paramc)) {
  260. unsigned j = index;
  261. const clish_param_t *param;
  262. while (j < paramc) {
  263. param = clish_paramv__get_param(paramv, j++);
  264. if (BOOL_TRUE != clish_param__get_optional(param))
  265. return CLISH_LINE_PARTIAL;
  266. }
  267. }
  268. /* If the number of arguments is bigger than number of
  269. * params than it's a args. So generate the args entry
  270. * in the list of completions.
  271. */
  272. if (last && up_level &&
  273. clish_command__get_args(cmd) &&
  274. (clish_pargv__get_count(last) == 0) &&
  275. (*idx <= argc) && (index >= paramc)) {
  276. clish_pargv_insert(last, clish_command__get_args(cmd), "");
  277. }
  278. /*
  279. * if we've satisfied all the parameters we can now construct
  280. * an 'args' parameter if one exists
  281. */
  282. if (up_level && (*idx < argc) && (index >= paramc)) {
  283. const char *arg = lub_argv__get_arg(argv, *idx);
  284. const clish_param_t *param = clish_command__get_args(cmd);
  285. char *args = NULL;
  286. if (!param)
  287. return CLISH_BAD_CMD;
  288. /*
  289. * put all the argument into a single string
  290. */
  291. while (NULL != arg) {
  292. bool_t quoted = lub_argv__get_quoted(argv, *idx);
  293. if (BOOL_TRUE == quoted) {
  294. lub_string_cat(&args, "\"");
  295. }
  296. /* place the current argument in the string */
  297. lub_string_cat(&args, arg);
  298. if (BOOL_TRUE == quoted) {
  299. lub_string_cat(&args, "\"");
  300. }
  301. (*idx)++;
  302. arg = lub_argv__get_arg(argv, *idx);
  303. if (NULL != arg) {
  304. /* add a space if there are more arguments */
  305. lub_string_cat(&args, " ");
  306. }
  307. }
  308. /* add (or update) this parameter */
  309. clish_pargv_insert(this, param, args);
  310. lub_string_free(args);
  311. }
  312. return CLISH_LINE_OK;
  313. }
  314. /*--------------------------------------------------------- */
  315. static clish_pargv_status_t clish_pargv_init(clish_pargv_t * this,
  316. const clish_command_t * cmd, const char *viewid,
  317. const lub_argv_t * argv)
  318. {
  319. unsigned idx = lub_argv_wordcount(clish_command__get_name(cmd));
  320. this->pargc = 0;
  321. return clish_pargv_parse(this, cmd, viewid,
  322. clish_command__get_paramv(cmd),
  323. argv, &idx, NULL, 0);
  324. }
  325. /*--------------------------------------------------------- */
  326. clish_pargv_t *clish_pargv_create(void)
  327. {
  328. clish_pargv_t *this;
  329. this = malloc(sizeof(clish_pargv_t));
  330. this->pargc = 0;
  331. this->pargv = NULL;
  332. return this;
  333. }
  334. /*--------------------------------------------------------- */
  335. clish_pargv_t *clish_pargv_new(const clish_command_t * cmd,
  336. const char *viewid,
  337. const char *line,
  338. size_t offset,
  339. clish_pargv_status_t * status)
  340. {
  341. clish_pargv_t *this = NULL;
  342. lub_argv_t *argv = NULL;
  343. if (!cmd || !line)
  344. return NULL;
  345. this = clish_pargv_create();
  346. if (!this)
  347. return NULL;
  348. argv = lub_argv_new(line, offset);
  349. *status = clish_pargv_init(this, cmd, viewid, argv);
  350. lub_argv_delete(argv);
  351. switch (*status) {
  352. case CLISH_LINE_OK:
  353. break;
  354. default:
  355. /* commit suicide */
  356. clish_pargv_delete(this);
  357. this = NULL;
  358. break;
  359. }
  360. return this;
  361. }
  362. /*--------------------------------------------------------- */
  363. static void clish_pargv_fini(clish_pargv_t * this)
  364. {
  365. unsigned i;
  366. /* cleanup time */
  367. for (i = 0; i < this->pargc; i++) {
  368. lub_string_free(this->pargv[i]->value);
  369. this->pargv[i]->value = NULL;
  370. free(this->pargv[i]);
  371. }
  372. free(this->pargv);
  373. }
  374. /*--------------------------------------------------------- */
  375. void clish_pargv_delete(clish_pargv_t * this)
  376. {
  377. if (!this)
  378. return;
  379. clish_pargv_fini(this);
  380. free(this);
  381. }
  382. /*--------------------------------------------------------- */
  383. unsigned clish_pargv__get_count(clish_pargv_t * this)
  384. {
  385. if (!this)
  386. return 0;
  387. return this->pargc;
  388. }
  389. /*--------------------------------------------------------- */
  390. clish_parg_t *clish_pargv__get_parg(clish_pargv_t * this, unsigned index)
  391. {
  392. if (!this)
  393. return NULL;
  394. if (index > this->pargc)
  395. return NULL;
  396. return this->pargv[index];
  397. }
  398. /*--------------------------------------------------------- */
  399. const clish_param_t *clish_pargv__get_param(clish_pargv_t * this,
  400. unsigned index)
  401. {
  402. clish_parg_t *tmp;
  403. if (!this)
  404. return NULL;
  405. if (index >= this->pargc)
  406. return NULL;
  407. tmp = this->pargv[index];
  408. return tmp->param;
  409. }
  410. /*--------------------------------------------------------- */
  411. const char *clish_parg__get_value(const clish_parg_t * this)
  412. {
  413. if (!this)
  414. return NULL;
  415. return this->value;
  416. }
  417. /*--------------------------------------------------------- */
  418. const char *clish_parg__get_name(const clish_parg_t * this)
  419. {
  420. if (!this)
  421. return NULL;
  422. return clish_param__get_name(this->param);
  423. }
  424. /*--------------------------------------------------------- */
  425. const clish_ptype_t *clish_parg__get_ptype(const clish_parg_t * this)
  426. {
  427. if (!this)
  428. return NULL;
  429. return clish_param__get_ptype(this->param);
  430. }
  431. /*--------------------------------------------------------- */
  432. const clish_parg_t *clish_pargv_find_arg(clish_pargv_t * this, const char *name)
  433. {
  434. if (!this)
  435. return NULL;
  436. return find_parg(this, name);
  437. }
  438. /*--------------------------------------------------------- */