pipe_exec.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ** check every cmd, every cote type
  80. ** if block starts and ends with same cote:
  81. ** cut first and last char
  82. */
  83. void remove_cotes(char **cmd)
  84. {
  85. int i;
  86. int j;
  87. char *param;
  88. i = -1;
  89. param = "\'\"`";
  90. while (cmd[++i] != NULL)
  91. {
  92. j = -1;
  93. while (param[++j] != '\0')
  94. if (cmd[i][0] == param[j] && \
  95. cmd[i][ft_strlen(cmd[i]) - 1] == param[j])
  96. {
  97. cmd[i][ft_strlen(cmd[i]) - 1] = '\0';
  98. cmd[i] = &cmd[i][1];
  99. break ;
  100. }
  101. }
  102. }