shell_parse.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. clish_pargv_status_t clish_shell_parse(
  11. clish_shell_t *this, const char *line,
  12. const clish_command_t **ret_cmd, clish_pargv_t **pargv)
  13. {
  14. clish_pargv_status_t result = CLISH_BAD_CMD;
  15. clish_context_t context;
  16. const clish_command_t *cmd;
  17. lub_argv_t *argv = NULL;
  18. unsigned int idx;
  19. *ret_cmd = cmd = clish_shell_resolve_command(this, line);
  20. if (!cmd)
  21. return result;
  22. /* Now construct the parameters for the command */
  23. /* Prepare context */
  24. *pargv = clish_pargv_new();
  25. clish_context_init(&context, this);
  26. clish_context__set_cmd(&context, cmd);
  27. clish_context__set_pargv(&context, *pargv);
  28. idx = lub_string_wordcount(clish_command__get_name(cmd));
  29. argv = lub_argv_new(line, 0);
  30. result = clish_shell_parse_pargv(*pargv, cmd, &context,
  31. clish_command__get_paramv(cmd),
  32. argv, &idx, NULL, 0);
  33. lub_argv_delete(argv);
  34. if (CLISH_LINE_OK != result) {
  35. clish_pargv_delete(*pargv);
  36. *pargv = NULL;
  37. }
  38. return result;
  39. }
  40. /*--------------------------------------------------------- */
  41. static bool_t line_test(const clish_param_t *param, void *context)
  42. {
  43. char *str = NULL;
  44. char *teststr = NULL;
  45. bool_t res;
  46. if (!param)
  47. return BOOL_FALSE;
  48. teststr = clish_param__get_test(param);
  49. if (!teststr)
  50. return BOOL_TRUE;
  51. str = clish_shell_expand(teststr, SHELL_VAR_ACTION, context);
  52. if (!str)
  53. return BOOL_FALSE;
  54. res = lub_system_line_test(str);
  55. lub_string_free(str);
  56. return res;
  57. }
  58. /*--------------------------------------------------------- */
  59. clish_pargv_status_t clish_shell_parse_pargv(clish_pargv_t *pargv,
  60. const clish_command_t *cmd,
  61. void *context,
  62. clish_paramv_t *paramv,
  63. const lub_argv_t *argv,
  64. unsigned *idx, clish_pargv_t *last, unsigned need_index)
  65. {
  66. unsigned argc = lub_argv__get_count(argv);
  67. unsigned index = 0;
  68. unsigned nopt_index = 0;
  69. clish_param_t *nopt_param = NULL;
  70. unsigned i;
  71. clish_pargv_status_t retval;
  72. unsigned paramc = clish_paramv__get_count(paramv);
  73. int up_level = 0; /* Is it a first level of param nesting? */
  74. assert(pargv);
  75. assert(cmd);
  76. /* Check is it a first level of PARAM nesting. */
  77. if (paramv == clish_command__get_paramv(cmd))
  78. up_level = 1;
  79. while (index < paramc) {
  80. const char *arg = NULL;
  81. clish_param_t *param = clish_paramv__get_param(paramv, index);
  82. clish_param_t *cparam = NULL;
  83. int is_switch = 0;
  84. if (!param)
  85. return CLISH_BAD_PARAM;
  86. /* Use real arg or PARAM's default value as argument */
  87. if (*idx < argc)
  88. arg = lub_argv__get_arg(argv, *idx);
  89. /* Is parameter in "switch" mode? */
  90. if (CLISH_PARAM_SWITCH == clish_param__get_mode(param))
  91. is_switch = 1;
  92. /* Check the 'test' conditions */
  93. if (!line_test(param, context)) {
  94. index++;
  95. continue;
  96. }
  97. /* Add param for help and completion */
  98. if (last && (*idx == need_index) &&
  99. (NULL == clish_pargv_find_arg(pargv, clish_param__get_name(param)))) {
  100. if (is_switch) {
  101. unsigned rec_paramc = clish_param__get_param_count(param);
  102. for (i = 0; i < rec_paramc; i++) {
  103. cparam = clish_param__get_param(param, i);
  104. if (!cparam)
  105. break;
  106. if (!line_test(cparam, context))
  107. continue;
  108. if (CLISH_PARAM_SUBCOMMAND ==
  109. clish_param__get_mode(cparam)) {
  110. const char *pname =
  111. clish_param__get_value(cparam);
  112. if (!arg || (arg &&
  113. (pname == lub_string_nocasestr(pname,
  114. arg))))
  115. clish_pargv_insert(last,
  116. cparam, arg);
  117. } else {
  118. clish_pargv_insert(last,
  119. cparam, arg);
  120. }
  121. }
  122. } else {
  123. if (CLISH_PARAM_SUBCOMMAND ==
  124. clish_param__get_mode(param)) {
  125. const char *pname =
  126. clish_param__get_value(param);
  127. if (!arg || (arg &&
  128. (pname == lub_string_nocasestr(pname, arg))))
  129. clish_pargv_insert(last, param, arg);
  130. } else {
  131. clish_pargv_insert(last, param, arg);
  132. }
  133. }
  134. }
  135. /* Set parameter value */
  136. {
  137. char *validated = NULL;
  138. clish_paramv_t *rec_paramv =
  139. clish_param__get_paramv(param);
  140. unsigned rec_paramc =
  141. clish_param__get_param_count(param);
  142. /* Save the index of last non-option parameter
  143. * to restore index if the optional parameters
  144. * will be used.
  145. */
  146. if (!clish_param__get_optional(param)) {
  147. nopt_param = param;
  148. nopt_index = index;
  149. }
  150. /* Validate the current parameter. */
  151. if (clish_pargv_find_arg(pargv, clish_param__get_name(param))) {
  152. /* Duplicated parameter */
  153. validated = NULL;
  154. } else if (is_switch) {
  155. for (i = 0; i < rec_paramc; i++) {
  156. cparam = clish_param__get_param(param, i);
  157. if (!cparam)
  158. break;
  159. if (!line_test(cparam, context))
  160. continue;
  161. if ((validated = arg ?
  162. clish_param_validate(cparam, arg) : NULL)) {
  163. rec_paramv = clish_param__get_paramv(cparam);
  164. rec_paramc = clish_param__get_param_count(cparam);
  165. break;
  166. }
  167. }
  168. } else {
  169. validated = arg ?
  170. clish_param_validate(param, arg) : NULL;
  171. }
  172. if (validated) {
  173. /* add (or update) this parameter */
  174. if (is_switch) {
  175. clish_pargv_insert(pargv, param,
  176. clish_param__get_name(cparam));
  177. clish_pargv_insert(pargv, cparam,
  178. validated);
  179. } else {
  180. clish_pargv_insert(pargv, param,
  181. validated);
  182. }
  183. lub_string_free(validated);
  184. /* Next command line argument */
  185. /* Don't change idx if this is the last
  186. unfinished optional argument.
  187. */
  188. if (!(clish_param__get_optional(param) &&
  189. (*idx == need_index) &&
  190. (need_index == (argc - 1)))) {
  191. (*idx)++;
  192. /* Walk through the nested parameters */
  193. if (rec_paramc) {
  194. retval = clish_shell_parse_pargv(pargv, cmd,
  195. context, rec_paramv,
  196. argv, idx, last, need_index);
  197. if (CLISH_LINE_OK != retval)
  198. return retval;
  199. }
  200. }
  201. /* Choose the next parameter */
  202. if (clish_param__get_optional(param) &&
  203. !clish_param__get_order(param)) {
  204. if (nopt_param)
  205. index = nopt_index + 1;
  206. else
  207. index = 0;
  208. } else {
  209. /* Save non-option position in
  210. case of ordered optional param */
  211. nopt_param = param;
  212. nopt_index = index;
  213. index++;
  214. }
  215. } else {
  216. /* Choose the next parameter if current
  217. * is not validated.
  218. */
  219. if (clish_param__get_optional(param))
  220. index++;
  221. else {
  222. if (!arg)
  223. break;
  224. else
  225. return CLISH_BAD_PARAM;
  226. }
  227. }
  228. }
  229. }
  230. /* Check for non-optional parameters without values */
  231. if ((*idx >= argc) && (index < paramc)) {
  232. unsigned j = index;
  233. const clish_param_t *param;
  234. while (j < paramc) {
  235. param = clish_paramv__get_param(paramv, j++);
  236. if (BOOL_TRUE != clish_param__get_optional(param))
  237. return CLISH_LINE_PARTIAL;
  238. }
  239. }
  240. /* If the number of arguments is bigger than number of
  241. * params than it's a args. So generate the args entry
  242. * in the list of completions.
  243. */
  244. if (last && up_level &&
  245. clish_command__get_args(cmd) &&
  246. (clish_pargv__get_count(last) == 0) &&
  247. (*idx <= argc) && (index >= paramc)) {
  248. clish_pargv_insert(last, clish_command__get_args(cmd), "");
  249. }
  250. /*
  251. * if we've satisfied all the parameters we can now construct
  252. * an 'args' parameter if one exists
  253. */
  254. if (up_level && (*idx < argc) && (index >= paramc)) {
  255. const char *arg = lub_argv__get_arg(argv, *idx);
  256. const clish_param_t *param = clish_command__get_args(cmd);
  257. char *args = NULL;
  258. if (!param)
  259. return CLISH_BAD_CMD;
  260. /*
  261. * put all the argument into a single string
  262. */
  263. while (NULL != arg) {
  264. bool_t quoted = lub_argv__get_quoted(argv, *idx);
  265. if (BOOL_TRUE == quoted) {
  266. lub_string_cat(&args, "\"");
  267. }
  268. /* place the current argument in the string */
  269. lub_string_cat(&args, arg);
  270. if (BOOL_TRUE == quoted) {
  271. lub_string_cat(&args, "\"");
  272. }
  273. (*idx)++;
  274. arg = lub_argv__get_arg(argv, *idx);
  275. if (NULL != arg) {
  276. /* add a space if there are more arguments */
  277. lub_string_cat(&args, " ");
  278. }
  279. }
  280. /* add (or update) this parameter */
  281. clish_pargv_insert(pargv, param, args);
  282. lub_string_free(args);
  283. }
  284. return CLISH_LINE_OK;
  285. }
  286. /*----------------------------------------------------------- */
  287. clish_shell_state_e clish_shell__get_state(const clish_shell_t *this)
  288. {
  289. return this->state;
  290. }
  291. /*----------------------------------------------------------- */
  292. void clish_shell__set_state(clish_shell_t *this,
  293. clish_shell_state_e state)
  294. {
  295. assert(this);
  296. this->state = state;
  297. }
  298. /*----------------------------------------------------------- */