shell_parse.c 8.8 KB

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