shell_parse.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * shell_parse.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include <string.h>
  7. #include <assert.h>
  8. /*----------------------------------------------------------- */
  9. clish_pargv_status_t clish_shell_parse(
  10. clish_shell_t *this, const char *line,
  11. const clish_command_t **ret_cmd, clish_pargv_t **pargv)
  12. {
  13. clish_pargv_status_t result = CLISH_BAD_CMD;
  14. clish_context_t context;
  15. const clish_command_t *cmd;
  16. *ret_cmd = cmd = clish_shell_resolve_command(this, line);
  17. if (!cmd)
  18. return result;
  19. /* Now construct the parameters for the command */
  20. *pargv = clish_pargv_new();
  21. context.shell = this;
  22. context.cmd = cmd;
  23. context.pargv = *pargv;
  24. result = clish_pargv_analyze(*pargv, cmd, &context, line, 0);
  25. if (CLISH_LINE_OK != result) {
  26. clish_pargv_delete(*pargv);
  27. *pargv = NULL;
  28. }
  29. if (*pargv) {
  30. char str[100];
  31. char * tmp;
  32. /* Variable __cur_depth */
  33. int depth = clish_shell__get_depth(this);
  34. snprintf(str, sizeof(str) - 1, "%u", depth);
  35. clish_pargv_insert(*pargv, this->param_depth, str);
  36. /* Variable __cur_pwd */
  37. tmp = clish_shell__get_pwd_full(this, depth);
  38. if (tmp) {
  39. clish_pargv_insert(*pargv, this->param_pwd, tmp);
  40. lub_string_free(tmp);
  41. }
  42. /* Variable __interactive */
  43. if (clish_shell__get_interactive(this))
  44. tmp = "1";
  45. else
  46. tmp = "0";
  47. clish_pargv_insert(*pargv, this->param_interactive, tmp);
  48. }
  49. return result;
  50. }
  51. /*----------------------------------------------------------- */
  52. clish_shell_state_t clish_shell__get_state(const clish_shell_t * this)
  53. {
  54. return this->state;
  55. }
  56. /*----------------------------------------------------------- */
  57. void clish_shell__set_state(clish_shell_t * this,
  58. clish_shell_state_t state)
  59. {
  60. assert(this);
  61. this->state = state;
  62. }
  63. /*----------------------------------------------------------- */