shell_parse.c 8.3 KB

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