shell_parse.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * shell_parse.c
  3. */
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "lub/string.h"
  7. #include "lub/system.h"
  8. #include "private.h"
  9. /*----------------------------------------------------------- */
  10. /* The standard clish_param_validate() is not enough when PTYPE can
  11. * contain ACTION. So we need context etc. to really validate param.
  12. */
  13. static char *clish_shell_param_validate(const clish_param_t *param, const char *text,
  14. clish_context_t *context)
  15. {
  16. clish_ptype_t *ptype = NULL;
  17. clish_ptype_method_e method = CLISH_PTYPE_METHOD_MAX;
  18. char *out = NULL;
  19. clish_context_t ctx = {};
  20. clish_pargv_t *pargv = NULL;
  21. clish_param_t *value_param = NULL;
  22. int retval = 0;
  23. assert(param);
  24. assert(context);
  25. if (!param || !context)
  26. return NULL;
  27. ptype = clish_param__get_ptype(param);
  28. assert(ptype);
  29. if (!ptype)
  30. return NULL;
  31. method = clish_ptype__get_method(ptype);
  32. // Check is it common non-code PTYPE
  33. if (method != CLISH_PTYPE_METHOD_CODE)
  34. return clish_param_validate(param, text);
  35. // Prepare dummy pargv structure to provide 'value' parameter to the
  36. // ACTION script. This parameter contain current value to check.
  37. value_param = clish_param_new("value", "Dummy param for PTYPE's ACTION",
  38. clish_param__get_ptype_name(param));
  39. assert(value_param);
  40. if (!value_param)
  41. return NULL;
  42. clish_param__set_ptype(value_param, ptype);
  43. pargv = clish_pargv_new(); // Dummy pargv
  44. assert(pargv);
  45. if (!pargv)
  46. return NULL;
  47. clish_pargv_insert(pargv, value_param, text);
  48. // Prepare context for ACTION execution
  49. clish_context_dup(&ctx, context);
  50. clish_context__set_action(&ctx, clish_ptype__get_action(ptype));
  51. clish_context__set_pargv(&ctx, pargv);
  52. // Try to execute ACTION
  53. retval = clish_shell_exec_action(&ctx, &out);
  54. // Cleanup dummy structures
  55. clish_pargv_delete(pargv);
  56. clish_param_delete(value_param);
  57. if (retval) {
  58. lub_string_free(out);
  59. return NULL; // Fail on bad ACTION retval
  60. }
  61. if (out)
  62. return out;
  63. return lub_string_dup(text);
  64. }
  65. /*----------------------------------------------------------- */
  66. clish_pargv_status_e clish_shell_parse(
  67. clish_shell_t *this, const char *line,
  68. const clish_command_t **ret_cmd, clish_pargv_t **pargv)
  69. {
  70. clish_pargv_status_e result = CLISH_BAD_CMD;
  71. clish_context_t context;
  72. const clish_command_t *cmd;
  73. lub_argv_t *argv = NULL;
  74. unsigned int idx;
  75. *ret_cmd = cmd = clish_shell_resolve_command(this, line);
  76. if (!cmd)
  77. return result;
  78. /* Now construct the parameters for the command */
  79. /* Prepare context */
  80. *pargv = clish_pargv_new();
  81. clish_context_init(&context, this);
  82. clish_context__set_cmd(&context, cmd);
  83. clish_context__set_pargv(&context, *pargv);
  84. idx = lub_string_wordcount(clish_command__get_name(cmd));
  85. argv = lub_argv_new(line, 0);
  86. result = clish_shell_parse_pargv(*pargv, cmd, &context,
  87. clish_command__get_paramv(cmd),
  88. argv, &idx, NULL, 0);
  89. lub_argv_delete(argv);
  90. if (CLISH_LINE_OK != result) {
  91. clish_pargv_delete(*pargv);
  92. *pargv = NULL;
  93. }
  94. return result;
  95. }
  96. /*--------------------------------------------------------- */
  97. static bool_t line_test(const clish_param_t *param, void *context)
  98. {
  99. char *str = NULL;
  100. const char *teststr = NULL;
  101. bool_t res;
  102. if (!param)
  103. return BOOL_FALSE;
  104. teststr = clish_param__get_test(param);
  105. if (!teststr)
  106. return BOOL_TRUE;
  107. str = clish_shell_expand(teststr, SHELL_VAR_ACTION, context);
  108. if (!str)
  109. return BOOL_FALSE;
  110. res = lub_system_line_test(str);
  111. lub_string_free(str);
  112. return res;
  113. }
  114. /*--------------------------------------------------------- */
  115. clish_pargv_status_e clish_shell_parse_pargv(clish_pargv_t *pargv,
  116. const clish_command_t *cmd,
  117. void *context,
  118. clish_paramv_t *paramv,
  119. const lub_argv_t *argv,
  120. unsigned *idx, clish_pargv_t *last, unsigned need_index)
  121. {
  122. unsigned argc = lub_argv__get_count(argv);
  123. unsigned index = 0;
  124. unsigned nopt_index = 0;
  125. clish_param_t *nopt_param = NULL;
  126. unsigned i;
  127. clish_pargv_status_e retval;
  128. unsigned paramc = clish_paramv__get_count(paramv);
  129. int up_level = 0; /* Is it a first level of param nesting? */
  130. assert(pargv);
  131. assert(cmd);
  132. /* Check is it a first level of PARAM nesting. */
  133. if (paramv == clish_command__get_paramv(cmd))
  134. up_level = 1;
  135. while (index < paramc) {
  136. const char *arg = NULL;
  137. clish_param_t *param = clish_paramv__get_param(paramv, index);
  138. clish_param_t *cparam = NULL;
  139. int is_switch = 0;
  140. if (!param)
  141. return CLISH_BAD_PARAM;
  142. /* Use real arg or PARAM's default value as argument */
  143. if (*idx < argc)
  144. arg = lub_argv__get_arg(argv, *idx);
  145. /* Is parameter in "switch" mode? */
  146. if (CLISH_PARAM_SWITCH == clish_param__get_mode(param))
  147. is_switch = 1;
  148. /* Check the 'test' conditions */
  149. if (!line_test(param, context)) {
  150. index++;
  151. continue;
  152. }
  153. /* Add param for help and completion */
  154. if (last && (*idx == need_index) &&
  155. (NULL == clish_pargv_find_arg(pargv, clish_param__get_name(param)))) {
  156. if (is_switch) {
  157. unsigned rec_paramc = clish_param__get_param_count(param);
  158. for (i = 0; i < rec_paramc; i++) {
  159. cparam = clish_param__get_param(param, i);
  160. if (!cparam)
  161. break;
  162. if (!line_test(cparam, context))
  163. continue;
  164. if (CLISH_PARAM_SUBCOMMAND ==
  165. clish_param__get_mode(cparam)) {
  166. const char *pname =
  167. clish_param__get_value(cparam);
  168. if (!arg || (arg &&
  169. (pname == lub_string_nocasestr(pname,
  170. arg))))
  171. clish_pargv_insert(last,
  172. cparam, arg);
  173. } else {
  174. clish_pargv_insert(last,
  175. cparam, arg);
  176. }
  177. }
  178. } else {
  179. if (CLISH_PARAM_SUBCOMMAND ==
  180. clish_param__get_mode(param)) {
  181. const char *pname =
  182. clish_param__get_value(param);
  183. if (!arg || (arg &&
  184. (pname == lub_string_nocasestr(pname, arg))))
  185. clish_pargv_insert(last, param, arg);
  186. } else {
  187. clish_pargv_insert(last, param, arg);
  188. }
  189. }
  190. }
  191. /* Set parameter value */
  192. {
  193. char *validated = NULL;
  194. clish_paramv_t *rec_paramv =
  195. clish_param__get_paramv(param);
  196. unsigned rec_paramc =
  197. clish_param__get_param_count(param);
  198. /* Save the index of last non-option parameter
  199. * to restore index if the optional parameters
  200. * will be used.
  201. */
  202. if (!clish_param__get_optional(param)) {
  203. nopt_param = param;
  204. nopt_index = index;
  205. }
  206. /* Validate the current parameter. */
  207. if (clish_pargv_find_arg(pargv, clish_param__get_name(param))) {
  208. /* Duplicated parameter */
  209. validated = NULL;
  210. } else if (is_switch) {
  211. for (i = 0; i < rec_paramc; i++) {
  212. cparam = clish_param__get_param(param, i);
  213. if (!cparam)
  214. break;
  215. if (!line_test(cparam, context))
  216. continue;
  217. if ((validated = arg ?
  218. clish_shell_param_validate(
  219. cparam, arg, context) :
  220. NULL)) {
  221. rec_paramv = clish_param__get_paramv(cparam);
  222. rec_paramc = clish_param__get_param_count(cparam);
  223. break;
  224. }
  225. }
  226. } else {
  227. validated = arg ?
  228. clish_shell_param_validate(
  229. param, arg, context) : NULL;
  230. }
  231. if (validated) {
  232. /* add (or update) this parameter */
  233. if (is_switch) {
  234. clish_pargv_insert(pargv, param,
  235. clish_param__get_name(cparam));
  236. clish_pargv_insert(pargv, cparam,
  237. validated);
  238. } else {
  239. clish_pargv_insert(pargv, param,
  240. validated);
  241. }
  242. lub_string_free(validated);
  243. /* Next command line argument */
  244. /* Don't change idx if this is the last
  245. unfinished optional argument.
  246. */
  247. if (!(clish_param__get_optional(param) &&
  248. (*idx == need_index) &&
  249. (need_index == (argc - 1)))) {
  250. (*idx)++;
  251. /* Walk through the nested parameters */
  252. if (rec_paramc) {
  253. retval = clish_shell_parse_pargv(pargv, cmd,
  254. context, rec_paramv,
  255. argv, idx, last, need_index);
  256. if (CLISH_LINE_OK != retval)
  257. return retval;
  258. }
  259. }
  260. /* Choose the next parameter */
  261. if (clish_param__get_optional(param) &&
  262. !clish_param__get_order(param)) {
  263. if (nopt_param)
  264. index = nopt_index + 1;
  265. else
  266. index = 0;
  267. } else {
  268. /* Save non-option position in
  269. case of ordered optional param */
  270. nopt_param = param;
  271. nopt_index = index;
  272. index++;
  273. }
  274. } else {
  275. /* Choose the next parameter if current
  276. * is not validated.
  277. */
  278. if (clish_param__get_optional(param))
  279. index++;
  280. else {
  281. if (!arg)
  282. break;
  283. else
  284. return CLISH_BAD_PARAM;
  285. }
  286. }
  287. }
  288. }
  289. /* Check for non-optional parameters without values */
  290. if ((*idx >= argc) && (index < paramc)) {
  291. unsigned j = index;
  292. const clish_param_t *param;
  293. while (j < paramc) {
  294. param = clish_paramv__get_param(paramv, j++);
  295. if (BOOL_TRUE != clish_param__get_optional(param))
  296. return CLISH_LINE_PARTIAL;
  297. }
  298. }
  299. /* If the number of arguments is bigger than number of
  300. * params than it's a args. So generate the args entry
  301. * in the list of completions.
  302. */
  303. if (last && up_level &&
  304. clish_command__get_args(cmd) &&
  305. (clish_pargv__get_count(last) == 0) &&
  306. (*idx <= argc) && (index >= paramc)) {
  307. clish_pargv_insert(last, clish_command__get_args(cmd), "");
  308. }
  309. /*
  310. * if we've satisfied all the parameters we can now construct
  311. * an 'args' parameter if one exists
  312. */
  313. if (up_level && (*idx < argc) && (index >= paramc)) {
  314. const char *arg = lub_argv__get_arg(argv, *idx);
  315. const clish_param_t *param = clish_command__get_args(cmd);
  316. char *args = NULL;
  317. if (!param)
  318. return CLISH_BAD_CMD;
  319. /*
  320. * put all the argument into a single string
  321. */
  322. while (NULL != arg) {
  323. bool_t quoted = lub_argv__get_quoted(argv, *idx);
  324. char *enc = NULL;
  325. if (BOOL_TRUE == quoted) {
  326. lub_string_cat(&args, "\"");
  327. }
  328. /* place the current argument in the string */
  329. /* Escape quote and backslash */
  330. enc = lub_string_encode(arg, lub_string_esc_quoted);
  331. lub_string_cat(&args, enc);
  332. lub_string_free(enc);
  333. if (BOOL_TRUE == quoted) {
  334. lub_string_cat(&args, "\"");
  335. }
  336. (*idx)++;
  337. arg = lub_argv__get_arg(argv, *idx);
  338. if (NULL != arg) {
  339. /* add a space if there are more arguments */
  340. lub_string_cat(&args, " ");
  341. }
  342. }
  343. /* add (or update) this parameter */
  344. clish_pargv_insert(pargv, param, args);
  345. lub_string_free(args);
  346. }
  347. return CLISH_LINE_OK;
  348. }
  349. CLISH_SET(shell, clish_shell_state_e, state);
  350. CLISH_GET(shell, clish_shell_state_e, state);