shell_file.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include "private.h"
  4. /*----------------------------------------------------------- */
  5. bool_t clish_shell_push_file(clish_shell_t * this, const char * fname,
  6. bool_t stop_on_error)
  7. {
  8. FILE *file;
  9. bool_t res;
  10. assert(this);
  11. if (!fname)
  12. return BOOL_FALSE;
  13. file = fopen(fname, "r");
  14. if (!file)
  15. return BOOL_FALSE;
  16. res = clish_shell_push_fd(this, file, stop_on_error);
  17. if (!res)
  18. fclose(file);
  19. return res;
  20. }
  21. /*----------------------------------------------------------- */
  22. bool_t clish_shell_push_fd(clish_shell_t * this, FILE * file,
  23. bool_t stop_on_error)
  24. {
  25. assert(this);
  26. /* allocate a control node */
  27. clish_shell_file_t *node = malloc(sizeof(clish_shell_file_t));
  28. bool_t result = BOOL_TRUE;
  29. if (node) {
  30. /* intialise the node */
  31. node->file = file;
  32. node->stop_on_error = stop_on_error;
  33. node->next = this->current_file;
  34. /* put the node at the top of the file stack */
  35. this->current_file = node;
  36. /* now switch the terminal's input stream */
  37. tinyrl__set_istream(this->tinyrl, file);
  38. result = BOOL_TRUE;
  39. }
  40. return result;
  41. }
  42. /*----------------------------------------------------------- */
  43. bool_t clish_shell_pop_file(clish_shell_t * this)
  44. {
  45. bool_t result = BOOL_FALSE;
  46. clish_shell_file_t *node = this->current_file;
  47. if (node) {
  48. /* remove the current file from the stack... */
  49. this->current_file = node->next;
  50. /* and close the current file...
  51. */
  52. fclose(node->file);
  53. if (node->next) {
  54. /* now switch the terminal's input stream */
  55. tinyrl__set_istream(this->tinyrl, node->next->file);
  56. result = BOOL_TRUE;
  57. }
  58. /* and free up the memory */
  59. free(node);
  60. }
  61. return result;
  62. }
  63. /*----------------------------------------------------------- */