shell_parse.c 8.4 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. /* 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. /* Use real arg or PARAM's default value as argument */
  85. if (*idx < argc)
  86. arg = lub_argv__get_arg(argv, *idx);
  87. /* Is parameter in "switch" mode? */
  88. if (CLISH_PARAM_SWITCH == clish_param__get_mode(param))
  89. is_switch = 1;
  90. /* Check the 'test' conditions */
  91. if (param && !line_test(param, context)) {
  92. index++;
  93. continue;
  94. }
  95. /* Add param for help and completion */
  96. if (last && (*idx == need_index) &&
  97. (NULL == clish_pargv_find_arg(pargv, clish_param__get_name(param)))) {
  98. if (is_switch) {
  99. unsigned rec_paramc = clish_param__get_param_count(param);
  100. for (i = 0; i < rec_paramc; i++) {
  101. cparam = clish_param__get_param(param, i);
  102. if (!cparam)
  103. break;
  104. if (!line_test(cparam, context))
  105. continue;
  106. if (CLISH_PARAM_SUBCOMMAND ==
  107. clish_param__get_mode(cparam)) {
  108. const char *pname =
  109. clish_param__get_value(cparam);
  110. if (!arg || (arg &&
  111. (pname == lub_string_nocasestr(pname,
  112. arg))))
  113. clish_pargv_insert(last,
  114. cparam, arg);
  115. } else {
  116. clish_pargv_insert(last,
  117. cparam, arg);
  118. }
  119. }
  120. } else {
  121. if (CLISH_PARAM_SUBCOMMAND ==
  122. clish_param__get_mode(param)) {
  123. const char *pname =
  124. clish_param__get_value(param);
  125. if (!arg || (arg &&
  126. (pname == lub_string_nocasestr(pname, arg))))
  127. clish_pargv_insert(last, param, arg);
  128. } else {
  129. clish_pargv_insert(last, param, arg);
  130. }
  131. }
  132. }
  133. /* Set parameter value */
  134. if (param) {
  135. char *validated = NULL;
  136. clish_paramv_t *rec_paramv =
  137. clish_param__get_paramv(param);
  138. unsigned rec_paramc =
  139. clish_param__get_param_count(param);
  140. /* Save the index of last non-option parameter
  141. * to restore index if the optional parameters
  142. * will be used.
  143. */
  144. if (!clish_param__get_optional(param)) {
  145. nopt_param = param;
  146. nopt_index = index;
  147. }
  148. /* Validate the current parameter. */
  149. if (clish_pargv_find_arg(pargv, clish_param__get_name(param))) {
  150. /* Duplicated parameter */
  151. validated = NULL;
  152. } else if (is_switch) {
  153. for (i = 0; i < rec_paramc; i++) {
  154. cparam = clish_param__get_param(param, i);
  155. if (!cparam)
  156. break;
  157. if (!line_test(cparam, context))
  158. continue;
  159. if ((validated = arg ?
  160. clish_param_validate(cparam, arg) : NULL)) {
  161. rec_paramv = clish_param__get_paramv(cparam);
  162. rec_paramc = clish_param__get_param_count(cparam);
  163. break;
  164. }
  165. }
  166. } else {
  167. validated = arg ?
  168. clish_param_validate(param, arg) : NULL;
  169. }
  170. if (validated) {
  171. /* add (or update) this parameter */
  172. if (is_switch) {
  173. clish_pargv_insert(pargv, param,
  174. clish_param__get_name(cparam));
  175. clish_pargv_insert(pargv, cparam,
  176. validated);
  177. } else {
  178. clish_pargv_insert(pargv, param,
  179. validated);
  180. }
  181. lub_string_free(validated);
  182. /* Next command line argument */
  183. /* Don't change idx if this is the last
  184. unfinished optional argument.
  185. */
  186. if (!(clish_param__get_optional(param) &&
  187. (*idx == need_index) &&
  188. (need_index == (argc - 1)))) {
  189. (*idx)++;
  190. /* Walk through the nested parameters */
  191. if (rec_paramc) {
  192. retval = clish_shell_parse_pargv(pargv, cmd,
  193. context, rec_paramv,
  194. argv, idx, last, need_index);
  195. if (CLISH_LINE_OK != retval)
  196. return retval;
  197. }
  198. }
  199. /* Choose the next parameter */
  200. if (clish_param__get_optional(param) &&
  201. !clish_param__get_order(param)) {
  202. if (nopt_param)
  203. index = nopt_index + 1;
  204. else
  205. index = 0;
  206. } else {
  207. /* Save non-option position in
  208. case of ordered optional param */
  209. nopt_param = param;
  210. nopt_index = index;
  211. index++;
  212. }
  213. } else {
  214. /* Choose the next parameter if current
  215. * is not validated.
  216. */
  217. if (clish_param__get_optional(param))
  218. index++;
  219. else {
  220. if (!arg)
  221. break;
  222. else
  223. return CLISH_BAD_PARAM;
  224. }
  225. }
  226. } else {
  227. return CLISH_BAD_PARAM;
  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_t 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_t state)
  294. {
  295. assert(this);
  296. this->state = state;
  297. }
  298. /*----------------------------------------------------------- */