1
0

shell_parse.c 8.4 KB

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