shell_push_file.c 977 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include "private.h"
  4. bool_t
  5. clish_shell_push_file(clish_shell_t * this, const char * fname, bool_t stop_on_error)
  6. {
  7. FILE *file;
  8. bool_t res;
  9. assert(this);
  10. if (!fname)
  11. return BOOL_FALSE;
  12. file = fopen(fname, "r");
  13. if (!file)
  14. return BOOL_FALSE;
  15. res = clish_shell_push_fd(this, file, stop_on_error);
  16. if (!res)
  17. fclose(file);
  18. return res;
  19. }
  20. bool_t
  21. clish_shell_push_fd(clish_shell_t * this, FILE * file, bool_t stop_on_error)
  22. {
  23. assert(this);
  24. /* allocate a control node */
  25. clish_shell_file_t *node = malloc(sizeof(clish_shell_file_t));
  26. bool_t result = BOOL_TRUE;
  27. if (NULL != node) {
  28. /* intialise the node */
  29. node->file = file;
  30. node->stop_on_error = stop_on_error;
  31. node->next = this->current_file;
  32. /* put the node at the top of the file stack */
  33. this->current_file = node;
  34. /* now switch the terminal's input stream */
  35. tinyrl__set_istream(this->tinyrl, file);
  36. result = BOOL_TRUE;
  37. }
  38. return result;
  39. }