heredoc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "sh.h"
  2. /*
  3. ** Launch heredoc: double left redirection <<
  4. ** Unset term caps modes
  5. ** retrieve text through a prompt
  6. ** Set term caps modes
  7. ** backup STDIN, pipe the text in STDIN
  8. ** wait the fork, launch the cmd with the upper text
  9. ** restore STDIN
  10. */
  11. void prompt_redirection(t_e *e, t_pipe *p, t_pipe_redir *t, t_pipe_redir *t2)
  12. {
  13. char *txt;
  14. int pipefd[2];
  15. int stdfd;
  16. int i;
  17. set_term_modes(0);
  18. txt = prompt_heredoc(e, txt, t->otxt, i);
  19. set_term_modes(1);
  20. pipe(pipefd);
  21. if (fork() == 0)
  22. {
  23. close(pipefd[0]);
  24. ft_putstr_fd(txt, pipefd[1]);
  25. ft_strdel(&txt);
  26. exit(0);
  27. }
  28. close(pipefd[1]);
  29. stdfd = dup(0);
  30. dup2(pipefd[0], 0);
  31. wait(NULL);
  32. if (t2 != NULL)
  33. open_right_redirections(e, p, t2);
  34. if (fork() == 0)
  35. {
  36. manage_cmd_2(e, cmd_splitting(e, t->itxt, ' '));
  37. exit(0);
  38. }
  39. wait(NULL);
  40. if (t2 != NULL)
  41. {
  42. close(e->fd_file);
  43. close(1);
  44. dup(e->fdout);
  45. }
  46. close(pipefd[0]);
  47. close(0);
  48. dup(stdfd);
  49. }
  50. /*
  51. ** prompt to enter text, get entered lines
  52. ** join them till it encounter the end of file string except first time
  53. ** read, join the buffer till it encounter ENTER
  54. ** if ctrl + d: print '\n'; return txt
  55. ** return the text
  56. */
  57. char *prompt_heredoc(t_e *e, char *txt, char *eof, int i)
  58. {
  59. t_e *h;
  60. i = 0;
  61. h = (t_e*)malloc(sizeof(t_e));
  62. h->cmd = ft_strnew(0);
  63. while (ft_strcmp(h->cmd, eof) != 0)
  64. {
  65. if (i == 0)
  66. txt = ft_strnew(0);
  67. else
  68. {
  69. txt = ft_strjoinf(txt, ft_strjoinf(h->cmd, "\n", 1), 3);
  70. h->cmd = ft_strnew(0);
  71. }
  72. ft_putchar('>');
  73. if (here_edition(h, 1) == 1)
  74. {
  75. ft_putchar('\n');
  76. return (ft_strjoinf(txt, ft_strjoinf(h->cmd, "\n", 1), 3));
  77. }
  78. i++;
  79. }
  80. ft_strdel(&h->cmd);
  81. free(h);
  82. return (txt);
  83. }
  84. /*
  85. ** break loop if encounter ENTER
  86. ** if heredoc: print '\n'
  87. ** join the buffer
  88. ** if ctrl + d: end of file
  89. ** handle keys moving
  90. */
  91. int here_edition(t_e *h, int heredoc)
  92. {
  93. int ret;
  94. h->key = ft_strnew(4);
  95. while ((ret = read(0, h->key, 1)) > 0)
  96. {
  97. if (h->key[0] == 10)
  98. {
  99. if (heredoc == 1)
  100. ft_putchar('\n');
  101. break ;
  102. }
  103. if (ft_isprint(h->key[0]) == 1)
  104. {
  105. ft_putchar(h->key[0]);
  106. h->key[ret] = '\0';
  107. insert(h);
  108. }
  109. if (h->key[0] == 4)
  110. return (1);
  111. else
  112. handle_keys(h, 0);
  113. }
  114. return (0);
  115. }