pargv.c 12 KB

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