1
0

shell_parse.c 8.7 KB

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