pipe_exec.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. open_left_redirections(e, p, t[p->i]);
  19. if (ft_strcmp(t[p->i]->operator, "<<") != 0)
  20. handle_multi_piping(e, p, t[p->i]);
  21. if (ft_strstr(t[p->i]->operator, ">") == NULL)
  22. close_redirections(e, p, t[p->i]);
  23. free(p->cmd_opt[p->i]);
  24. if (p->i != 0)
  25. free(p->fd[p->i - 1]);
  26. }
  27. free(p->cmd_pipe);
  28. p->cmd_opt[p->i] = NULL;
  29. free(p->fd);
  30. free(p);
  31. p->i = -1;
  32. while (++p->i <= p->nbr)
  33. wait(NULL);
  34. }
  35. /*
  36. ** Exit father if the command contain exit
  37. ** Fork for execve()
  38. ** Close current pipe output and redirect stdout into current pipe
  39. ** except for last cmd
  40. ** Close previous pipe input and redirect previous pipe into stdin
  41. ** except for first cmd
  42. ** Execute built-ins and executables which reads on stdin
  43. ** Exit current son fork
  44. ** Close previous pipe file descriptors except for first cmd to free fd
  45. */
  46. void handle_multi_piping(t_e *e, t_pipe *p, t_pipe_redir *t)
  47. {
  48. int ok;
  49. if (t->itxt != NULL)
  50. ok = manage_cmd_1(e, p->cmd_opt[p->i]);
  51. if (fork() == 0)
  52. {
  53. if (p->i < p->nbr)
  54. {
  55. close(p->fd[p->i][0]);
  56. dup2(p->fd[p->i][1], STDOUT);
  57. }
  58. if (p->i > 0)
  59. {
  60. close(p->fd[p->i - 1][1]);
  61. dup2(p->fd[p->i - 1][0], STDIN);
  62. }
  63. if (ok == 0)
  64. inside_pipe(e, p, t);
  65. exit(0);
  66. }
  67. if (p->i > 0)
  68. {
  69. close(p->fd[p->i - 1][0]);
  70. close(p->fd[p->i - 1][1]);
  71. }
  72. }
  73. void inside_pipe(t_e *e, t_pipe *p, t_pipe_redir *t)
  74. {
  75. if (ft_strstr(t->operator, ">") != NULL)
  76. open_right_redirections(e, p, t);
  77. if (t->itxt != NULL)
  78. manage_cmd_2(e, p->cmd_opt[p->i]);
  79. if (ft_strstr(t->operator, ">") != NULL)
  80. close_redirections(e, p, t);
  81. }
  82. /*
  83. ** check every cmd, every cote type
  84. ** if block starts and ends with same cote:
  85. ** cut first and last char
  86. */
  87. void remove_cotes(char **cmd)
  88. {
  89. int i;
  90. int j;
  91. char *param;
  92. i = -1;
  93. param = "\'\"`";
  94. while (cmd[++i] != NULL)
  95. {
  96. j = -1;
  97. while (param[++j] != '\0')
  98. if (cmd[i][0] == param[j] && \
  99. cmd[i][ft_strlen(cmd[i]) - 1] == param[j])
  100. {
  101. cmd[i][ft_strlen(cmd[i]) - 1] = '\0';
  102. cmd[i] = &cmd[i][1];
  103. break ;
  104. }
  105. }
  106. }