shell_parse.c 8.3 KB

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