pipe_exec.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "sh.h"
  2. /*
  3. ** Launch multi-piping
  4. ** Free memory of fd, cmd_opt, p variables.
  5. ** Wait for infinite fork to display again promt
  6. */
  7. void launch_pipes(t_e *e, t_pipe *p, t_pipe_redir **t)
  8. {
  9. p->i = -1;
  10. p->cmd_opt = (char***)malloc(sizeof(char**) * (p->nbr + 1));
  11. while (++p->i <= p->pnbr)
  12. {
  13. if (t[p->i]->itxt != NULL)
  14. {
  15. p->cmd_opt[p->i] = cmd_splitting(e, t[p->i]->itxt, ' ');
  16. remove_cotes(p->cmd_opt[p->i]);
  17. }
  18. handle_multi_piping(e, p, t[p->i]);
  19. free(p->cmd_opt[p->i]);
  20. if (p->i != 0)
  21. free(p->fd[p->i - 1]);
  22. }
  23. free(p->cmd_pipe);
  24. p->cmd_opt[p->i] = NULL;
  25. free(p->fd);
  26. free(p);
  27. p->i = -1;
  28. while (++p->i <= p->nbr)
  29. wait(NULL);
  30. }
  31. /*
  32. ** Exit father if the command contain exit
  33. ** Fork for execve()
  34. ** Close current pipe output and redirect stdout into current pipe
  35. ** except for last cmd
  36. ** Close previous pipe input and redirect previous pipe into stdin
  37. ** except for first cmd
  38. ** Execute built-ins and executables which reads on stdin
  39. ** Exit current son fork
  40. ** Close previous pipe file descriptors except for first cmd to free fd
  41. */
  42. void handle_multi_piping(t_e *e, t_pipe *p, t_pipe_redir *t)
  43. {
  44. int ok;
  45. ok = manage_cmd_1(e, p->cmd_opt[p->i]);
  46. if (fork() == 0)
  47. {
  48. open_left_redirections(e, p, t);
  49. if (p->i < p->nbr)
  50. {
  51. close(p->fd[p->i][0]);
  52. dup2(p->fd[p->i][1], STDOUT);
  53. }
  54. if (p->i > 0)
  55. {
  56. close(p->fd[p->i - 1][1]);
  57. dup2(p->fd[p->i - 1][0], STDIN);
  58. }
  59. if (ok == 0)
  60. inside_pipe(e, p, t);
  61. exit(0);
  62. }
  63. if (p->i > 0)
  64. {
  65. close(p->fd[p->i - 1][0]);
  66. close(p->fd[p->i - 1][1]);
  67. }
  68. }
  69. void inside_pipe(t_e *e, t_pipe *p, t_pipe_redir *t)
  70. {
  71. if (ft_strstr(t->operator, ">") != NULL)
  72. open_right_redirections(e, p, t);
  73. if (t->itxt != NULL && ft_strcmp(t->operator, "<<") != 0)
  74. manage_cmd_2(e, p->cmd_opt[p->i]);
  75. if (ft_strstr(t->operator, ">") != NULL)
  76. close_redirections(e, p, t);
  77. }
  78. /*
  79. ** Special function to handle double redirections of fd and execution fork
  80. ** fork, open left and right redirections
  81. ** execute cmd, close fd, father wait for end of son fork
  82. */
  83. void launch_double_redirections(t_e *e, t_pipe *p, t_pipe_redir **t)
  84. {
  85. int fd;
  86. if (ft_strcmp(t[0]->operator, "<<") == 0)
  87. {
  88. prompt_redirection(e, p, t[0], t[1]);
  89. return ;
  90. }
  91. else if (fork() == 0)
  92. {
  93. fd = open(t[0]->otxt, O_RDONLY);
  94. close(0);
  95. dup(fd);
  96. open_right_redirections(e, p, t[1]);
  97. manage_cmd_2(e, cmd_splitting(e, t[0]->itxt, ' '));
  98. close(STDOUT);
  99. close(fd);
  100. exit(0);
  101. }
  102. wait(NULL);
  103. }
  104. /*
  105. ** check every cmd, every cote type
  106. ** if block starts and ends with same cote:
  107. ** cut first and last char
  108. */
  109. void remove_cotes(char **cmd)
  110. {
  111. int i;
  112. int j;
  113. char *param;
  114. i = -1;
  115. param = "\'\"`";
  116. while (cmd[++i] != NULL)
  117. {
  118. j = -1;
  119. while (param[++j] != '\0')
  120. if (cmd[i][0] == param[j] && \
  121. cmd[i][ft_strlen(cmd[i]) - 1] == param[j])
  122. {
  123. cmd[i][ft_strlen(cmd[i]) - 1] = '\0';
  124. cmd[i] = &cmd[i][1];
  125. break ;
  126. }
  127. }
  128. }