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