1
0

shell_parse.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 (BOOL_TRUE != clish_param__get_optional(param)) {
  158. nopt_param = param;
  159. nopt_index = index;
  160. }
  161. /* Validate the current parameter. */
  162. if (NULL != 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 =
  181. arg ? clish_param_validate(param,
  182. arg) : NULL;
  183. }
  184. if (validated) {
  185. /* add (or update) this parameter */
  186. if (is_switch) {
  187. clish_pargv_insert(pargv, param,
  188. clish_param__get_name(cparam));
  189. clish_pargv_insert(pargv, cparam,
  190. validated);
  191. } else {
  192. clish_pargv_insert(pargv, param,
  193. validated);
  194. }
  195. lub_string_free(validated);
  196. /* Next command line argument */
  197. /* Don't change idx if this is the last
  198. unfinished optional argument.
  199. */
  200. if (!(clish_param__get_optional(param) &&
  201. (*idx == need_index) &&
  202. (need_index == (argc - 1)))) {
  203. (*idx)++;
  204. /* Walk through the nested parameters */
  205. if (rec_paramc) {
  206. retval = clish_shell_parse_pargv(pargv, cmd,
  207. context, rec_paramv,
  208. argv, idx, last, need_index);
  209. if (CLISH_LINE_OK != retval)
  210. return retval;
  211. }
  212. }
  213. /* Choose the next parameter */
  214. if (BOOL_TRUE == clish_param__get_optional(param)) {
  215. if (nopt_param)
  216. index = nopt_index + 1;
  217. else
  218. index = 0;
  219. } else {
  220. index++;
  221. }
  222. } else {
  223. /* Choose the next parameter if current
  224. * is not validated.
  225. */
  226. if (BOOL_TRUE ==
  227. clish_param__get_optional(param))
  228. index++;
  229. else {
  230. if (!arg)
  231. break;
  232. else
  233. return CLISH_BAD_PARAM;
  234. }
  235. }
  236. } else {
  237. return CLISH_BAD_PARAM;
  238. }
  239. }
  240. /* Check for non-optional parameters without values */
  241. if ((*idx >= argc) && (index < paramc)) {
  242. unsigned j = index;
  243. const clish_param_t *param;
  244. while (j < paramc) {
  245. param = clish_paramv__get_param(paramv, j++);
  246. if (BOOL_TRUE != clish_param__get_optional(param))
  247. return CLISH_LINE_PARTIAL;
  248. }
  249. }
  250. /* If the number of arguments is bigger than number of
  251. * params than it's a args. So generate the args entry
  252. * in the list of completions.
  253. */
  254. if (last && up_level &&
  255. clish_command__get_args(cmd) &&
  256. (clish_pargv__get_count(last) == 0) &&
  257. (*idx <= argc) && (index >= paramc)) {
  258. clish_pargv_insert(last, clish_command__get_args(cmd), "");
  259. }
  260. /*
  261. * if we've satisfied all the parameters we can now construct
  262. * an 'args' parameter if one exists
  263. */
  264. if (up_level && (*idx < argc) && (index >= paramc)) {
  265. const char *arg = lub_argv__get_arg(argv, *idx);
  266. const clish_param_t *param = clish_command__get_args(cmd);
  267. char *args = NULL;
  268. if (!param)
  269. return CLISH_BAD_CMD;
  270. /*
  271. * put all the argument into a single string
  272. */
  273. while (NULL != arg) {
  274. bool_t quoted = lub_argv__get_quoted(argv, *idx);
  275. if (BOOL_TRUE == quoted) {
  276. lub_string_cat(&args, "\"");
  277. }
  278. /* place the current argument in the string */
  279. lub_string_cat(&args, arg);
  280. if (BOOL_TRUE == quoted) {
  281. lub_string_cat(&args, "\"");
  282. }
  283. (*idx)++;
  284. arg = lub_argv__get_arg(argv, *idx);
  285. if (NULL != arg) {
  286. /* add a space if there are more arguments */
  287. lub_string_cat(&args, " ");
  288. }
  289. }
  290. /* add (or update) this parameter */
  291. clish_pargv_insert(pargv, param, args);
  292. lub_string_free(args);
  293. }
  294. return CLISH_LINE_OK;
  295. }
  296. /*----------------------------------------------------------- */
  297. clish_shell_state_t clish_shell__get_state(const clish_shell_t *this)
  298. {
  299. return this->state;
  300. }
  301. /*----------------------------------------------------------- */
  302. void clish_shell__set_state(clish_shell_t *this,
  303. clish_shell_state_t state)
  304. {
  305. assert(this);
  306. this->state = state;
  307. }
  308. /*----------------------------------------------------------- */