shell_parse.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. if (*out != '\0') // Non-empty str is transformation
  63. return out;
  64. lub_string_free(out);
  65. }
  66. return lub_string_dup(text);
  67. }
  68. /*----------------------------------------------------------- */
  69. clish_pargv_status_e clish_shell_parse(
  70. clish_shell_t *this, const char *line,
  71. const clish_command_t **ret_cmd, clish_pargv_t **pargv)
  72. {
  73. clish_pargv_status_e result = CLISH_BAD_CMD;
  74. clish_context_t context = {};
  75. const clish_command_t *cmd = NULL;
  76. lub_argv_t *argv = NULL;
  77. unsigned int idx = 0;
  78. *ret_cmd = cmd = clish_shell_resolve_command(this, line);
  79. if (!cmd)
  80. return result;
  81. /* Now construct the parameters for the command */
  82. /* Prepare context */
  83. *pargv = clish_pargv_new();
  84. clish_context_init(&context, this);
  85. clish_context__set_cmd(&context, cmd);
  86. clish_context__set_pargv(&context, *pargv);
  87. idx = lub_string_wordcount(clish_command__get_name(cmd));
  88. argv = lub_argv_new(line, 0);
  89. result = clish_shell_parse_pargv(*pargv, cmd, &context,
  90. clish_command__get_paramv(cmd),
  91. argv, &idx, NULL, 0);
  92. lub_argv_delete(argv);
  93. if (CLISH_LINE_OK != result) {
  94. clish_pargv_delete(*pargv);
  95. *pargv = NULL;
  96. }
  97. return result;
  98. }
  99. /*--------------------------------------------------------- */
  100. static bool_t line_test(const clish_param_t *param, void *context)
  101. {
  102. char *str = NULL;
  103. const char *teststr = NULL;
  104. bool_t res;
  105. if (!param)
  106. return BOOL_FALSE;
  107. teststr = clish_param__get_test(param);
  108. if (!teststr)
  109. return BOOL_TRUE;
  110. str = clish_shell_expand(teststr, SHELL_VAR_ACTION, context);
  111. if (!str)
  112. return BOOL_FALSE;
  113. res = lub_system_line_test(str);
  114. lub_string_free(str);
  115. return res;
  116. }
  117. /*--------------------------------------------------------- */
  118. clish_pargv_status_e clish_shell_parse_pargv(clish_pargv_t *pargv,
  119. const clish_command_t *cmd,
  120. void *context,
  121. clish_paramv_t *paramv,
  122. const lub_argv_t *argv,
  123. unsigned *idx, clish_pargv_t *last, unsigned need_index)
  124. {
  125. unsigned int argc = lub_argv__get_count(argv);
  126. unsigned int index = 0;
  127. unsigned int nopt_index = 0;
  128. clish_param_t *nopt_param = NULL;
  129. unsigned int i;
  130. clish_pargv_status_e retval = CLISH_LINE_OK;
  131. unsigned int paramc = clish_paramv__get_count(paramv);
  132. int up_level = 0; /* Is it a first level of param nesting? */
  133. assert(pargv);
  134. assert(cmd);
  135. /* Check is it a first level of PARAM nesting. */
  136. if (paramv == clish_command__get_paramv(cmd))
  137. up_level = 1;
  138. while (index < paramc) {
  139. const char *arg = NULL;
  140. clish_param_t *param = clish_paramv__get_param(paramv, index);
  141. clish_param_t *cparam = NULL;
  142. int is_switch = 0;
  143. if (!param)
  144. return CLISH_BAD_PARAM;
  145. /* Use real arg or PARAM's default value as argument */
  146. if (*idx < argc)
  147. arg = lub_argv__get_arg(argv, *idx);
  148. /* Is parameter in "switch" mode? */
  149. if (CLISH_PARAM_SWITCH == clish_param__get_mode(param))
  150. is_switch = 1;
  151. /* Check the 'test' conditions */
  152. if (!line_test(param, context)) {
  153. index++;
  154. continue;
  155. }
  156. /* Add param for help and completion */
  157. if (last && (*idx == need_index) &&
  158. (NULL == clish_pargv_find_arg(pargv, clish_param__get_name(param)))) {
  159. if (is_switch) {
  160. unsigned int rec_paramc = clish_param__get_param_count(param);
  161. for (i = 0; i < rec_paramc; i++) {
  162. cparam = clish_param__get_param(param, i);
  163. if (!cparam)
  164. break;
  165. if (!line_test(cparam, context))
  166. continue;
  167. if (CLISH_PARAM_SUBCOMMAND ==
  168. clish_param__get_mode(cparam)) {
  169. const char *pname =
  170. clish_param__get_value(cparam);
  171. if (!arg || (arg &&
  172. (pname == lub_string_nocasestr(pname,
  173. arg))))
  174. clish_pargv_insert(last,
  175. cparam, arg);
  176. } else {
  177. clish_pargv_insert(last,
  178. cparam, arg);
  179. }
  180. }
  181. } else {
  182. if (CLISH_PARAM_SUBCOMMAND ==
  183. clish_param__get_mode(param)) {
  184. const char *pname =
  185. clish_param__get_value(param);
  186. if (!arg || (arg &&
  187. (pname == lub_string_nocasestr(pname, arg))))
  188. clish_pargv_insert(last, param, arg);
  189. } else {
  190. clish_pargv_insert(last, param, arg);
  191. }
  192. }
  193. }
  194. /* Set parameter value */
  195. {
  196. char *validated = NULL;
  197. clish_paramv_t *rec_paramv =
  198. clish_param__get_paramv(param);
  199. unsigned int rec_paramc =
  200. clish_param__get_param_count(param);
  201. /* Save the index of last non-option parameter
  202. * to restore index if the optional parameters
  203. * will be used.
  204. */
  205. if (!clish_param__get_optional(param)) {
  206. nopt_param = param;
  207. nopt_index = index;
  208. }
  209. /* Validate the current parameter. */
  210. if (clish_pargv_find_arg(pargv, clish_param__get_name(param))) {
  211. /* Duplicated parameter */
  212. validated = NULL;
  213. } else if (is_switch) {
  214. for (i = 0; i < rec_paramc; i++) {
  215. cparam = clish_param__get_param(param, i);
  216. if (!cparam)
  217. break;
  218. if (!line_test(cparam, context))
  219. continue;
  220. if ((validated = arg ?
  221. clish_shell_param_validate(
  222. cparam, arg, context) :
  223. NULL)) {
  224. rec_paramv = clish_param__get_paramv(cparam);
  225. rec_paramc = clish_param__get_param_count(cparam);
  226. break;
  227. }
  228. }
  229. } else {
  230. validated = arg ?
  231. clish_shell_param_validate(
  232. param, arg, context) : NULL;
  233. }
  234. if (validated) {
  235. /* add (or update) this parameter */
  236. if (is_switch) {
  237. clish_pargv_insert(pargv, param,
  238. clish_param__get_name(cparam));
  239. clish_pargv_insert(pargv, cparam,
  240. validated);
  241. } else {
  242. clish_pargv_insert(pargv, param,
  243. validated);
  244. }
  245. lub_string_free(validated);
  246. /* Next command line argument */
  247. /* Don't change idx if this is the last
  248. unfinished optional argument.
  249. */
  250. if (!(clish_param__get_optional(param) &&
  251. (*idx == need_index) &&
  252. (need_index == (argc - 1)))) {
  253. (*idx)++;
  254. /* Walk through the nested parameters */
  255. if (rec_paramc) {
  256. retval = clish_shell_parse_pargv(pargv, cmd,
  257. context, rec_paramv,
  258. argv, idx, last, need_index);
  259. if (CLISH_LINE_OK != retval)
  260. return retval;
  261. }
  262. }
  263. /* Choose the next parameter */
  264. if (clish_param__get_optional(param) &&
  265. !clish_param__get_order(param)) {
  266. if (nopt_param)
  267. index = nopt_index + 1;
  268. else
  269. index = 0;
  270. } else {
  271. /* Save non-option position in
  272. case of ordered optional param */
  273. nopt_param = param;
  274. nopt_index = index;
  275. index++;
  276. }
  277. } else {
  278. /* Choose the next parameter if current
  279. * is not validated.
  280. */
  281. if (clish_param__get_optional(param))
  282. index++;
  283. else {
  284. if (!arg)
  285. break;
  286. else
  287. return CLISH_BAD_PARAM;
  288. }
  289. }
  290. }
  291. }
  292. /* Check for non-optional parameters without values */
  293. if ((*idx >= argc) && (index < paramc)) {
  294. unsigned int j = index;
  295. const clish_param_t *param = NULL;
  296. while (j < paramc) {
  297. param = clish_paramv__get_param(paramv, j++);
  298. if (BOOL_TRUE != clish_param__get_optional(param))
  299. return CLISH_LINE_PARTIAL;
  300. }
  301. }
  302. /* If the number of arguments is bigger than number of
  303. * params than it's a args. So generate the args entry
  304. * in the list of completions.
  305. */
  306. if (last && up_level &&
  307. clish_command__get_args(cmd) &&
  308. (clish_pargv__get_count(last) == 0) &&
  309. (*idx <= argc) && (index >= paramc)) {
  310. clish_pargv_insert(last, clish_command__get_args(cmd), "");
  311. }
  312. /*
  313. * if we've satisfied all the parameters we can now construct
  314. * an 'args' parameter if one exists
  315. */
  316. if (up_level && (*idx < argc) && (index >= paramc)) {
  317. const char *arg = lub_argv__get_arg(argv, *idx);
  318. const clish_param_t *param = clish_command__get_args(cmd);
  319. char *args = NULL;
  320. if (!param)
  321. return CLISH_BAD_CMD;
  322. /*
  323. * put all the argument into a single string
  324. */
  325. while (NULL != arg) {
  326. bool_t quoted = lub_argv__get_quoted(argv, *idx);
  327. char *enc = NULL;
  328. if (BOOL_TRUE == quoted) {
  329. lub_string_cat(&args, "\"");
  330. }
  331. /* place the current argument in the string */
  332. /* Escape quote and backslash */
  333. enc = lub_string_encode(arg, lub_string_esc_quoted);
  334. lub_string_cat(&args, enc);
  335. lub_string_free(enc);
  336. if (BOOL_TRUE == quoted) {
  337. lub_string_cat(&args, "\"");
  338. }
  339. (*idx)++;
  340. arg = lub_argv__get_arg(argv, *idx);
  341. if (NULL != arg) {
  342. /* add a space if there are more arguments */
  343. lub_string_cat(&args, " ");
  344. }
  345. }
  346. /* add (or update) this parameter */
  347. clish_pargv_insert(pargv, param, args);
  348. lub_string_free(args);
  349. }
  350. /* If command has no actions at all consider it as incompleted.
  351. * Often it's a command parts like "no" prefix for "no iface" command.
  352. */
  353. if (clish_command_is_incomplete(cmd))
  354. return CLISH_LINE_PARTIAL;
  355. return CLISH_LINE_OK;
  356. }
  357. CLISH_SET(shell, clish_shell_state_e, state);
  358. CLISH_GET(shell, clish_shell_state_e, state);