shell_loop.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * shell_loop.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <unistd.h>
  11. /*-------------------------------------------------------- */
  12. /* The main CLI loop while interactive execution.
  13. */
  14. int clish_shell_loop(clish_shell_t *this)
  15. {
  16. int running = 0;
  17. int retval = SHELL_STATE_OK;
  18. assert(this);
  19. if (!tinyrl__get_istream(this->tinyrl))
  20. return SHELL_STATE_IO_ERROR;
  21. /* Check the shell isn't closing down */
  22. if (this && (SHELL_STATE_CLOSING == this->state))
  23. return retval;
  24. /* Loop reading and executing lines until the user quits */
  25. while (!running) {
  26. retval = SHELL_STATE_OK;
  27. /* Get input from the stream */
  28. running = clish_shell_readline(this, NULL);
  29. if (running) {
  30. switch (this->state) {
  31. case SHELL_STATE_SCRIPT_ERROR:
  32. case SHELL_STATE_SYNTAX_ERROR:
  33. /* Interactive session doesn't exit on error */
  34. if (tinyrl__get_isatty(this->tinyrl) ||
  35. (this->current_file &&
  36. !this->current_file->stop_on_error))
  37. running = 0;
  38. retval = this->state;
  39. default:
  40. break;
  41. }
  42. }
  43. if (SHELL_STATE_CLOSING == this->state)
  44. running = -1;
  45. if (running)
  46. running = clish_shell_pop_file(this);
  47. }
  48. return retval;
  49. }
  50. /*-------------------------------------------------------- */