shell_parse.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. cparam = clish_param__get_param(param, i);
  102. if (!cparam)
  103. break;
  104. if (CLISH_PARAM_SUBCOMMAND ==
  105. clish_param__get_mode(cparam)) {
  106. const char *pname =
  107. clish_param__get_value(cparam);
  108. if (!arg || (arg &&
  109. (pname == lub_string_nocasestr(pname,
  110. arg))))
  111. clish_pargv_insert(last,
  112. cparam, arg);
  113. } else {
  114. clish_pargv_insert(last,
  115. cparam, arg);
  116. }
  117. }
  118. } else {
  119. if (CLISH_PARAM_SUBCOMMAND ==
  120. clish_param__get_mode(param)) {
  121. const char *pname =
  122. clish_param__get_value(param);
  123. if (!arg || (arg &&
  124. (pname == lub_string_nocasestr(pname, arg))))
  125. clish_pargv_insert(last, param, arg);
  126. } else {
  127. clish_pargv_insert(last, param, arg);
  128. }
  129. }
  130. }
  131. /* Set parameter value */
  132. if (param) {
  133. char *validated = NULL;
  134. clish_paramv_t *rec_paramv =
  135. clish_param__get_paramv(param);
  136. unsigned rec_paramc =
  137. clish_param__get_param_count(param);
  138. /* Save the index of last non-option parameter
  139. * to restore index if the optional parameters
  140. * will be used.
  141. */
  142. if (BOOL_TRUE != clish_param__get_optional(param)) {
  143. nopt_param = param;
  144. nopt_index = index;
  145. }
  146. /* Validate the current parameter. */
  147. if (NULL != clish_pargv_find_arg(pargv, clish_param__get_name(param))) {
  148. /* Duplicated parameter */
  149. validated = NULL;
  150. } else if (is_switch) {
  151. for (i = 0; i < rec_paramc; i++) {
  152. cparam =
  153. clish_param__get_param(param, i);
  154. if (!cparam)
  155. break;
  156. if ((validated =
  157. arg ? clish_param_validate(cparam,
  158. arg) :
  159. NULL)) {
  160. rec_paramv =
  161. clish_param__get_paramv
  162. (cparam);
  163. rec_paramc =
  164. clish_param__get_param_count
  165. (cparam);
  166. break;
  167. }
  168. }
  169. } else {
  170. validated =
  171. arg ? clish_param_validate(param,
  172. arg) : NULL;
  173. }
  174. if (validated) {
  175. /* add (or update) this parameter */
  176. if (is_switch) {
  177. clish_pargv_insert(pargv, param,
  178. clish_param__get_name(cparam));
  179. clish_pargv_insert(pargv, cparam,
  180. validated);
  181. } else {
  182. clish_pargv_insert(pargv, param,
  183. validated);
  184. }
  185. lub_string_free(validated);
  186. /* Next command line argument */
  187. /* Don't change idx if this is the last
  188. unfinished optional argument.
  189. */
  190. if (!(clish_param__get_optional(param) &&
  191. (*idx == need_index) &&
  192. (need_index == (argc - 1)))) {
  193. (*idx)++;
  194. /* Walk through the nested parameters */
  195. if (rec_paramc) {
  196. retval = clish_shell_parse_pargv(pargv, cmd,
  197. context, rec_paramv,
  198. argv, idx, last, need_index);
  199. if (CLISH_LINE_OK != retval)
  200. return retval;
  201. }
  202. }
  203. /* Choose the next parameter */
  204. if (BOOL_TRUE == clish_param__get_optional(param)) {
  205. if (nopt_param)
  206. index = nopt_index + 1;
  207. else
  208. index = 0;
  209. } else {
  210. index++;
  211. }
  212. } else {
  213. /* Choose the next parameter if current
  214. * is not validated.
  215. */
  216. if (BOOL_TRUE ==
  217. 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. /*----------------------------------------------------------- */