shell_parse.c 8.7 KB

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