pipe_parsing.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "sh.h"
  2. /*
  3. ** Check no || are present to prevent segfault
  4. ** Count number of pipes
  5. ** Malloc pipe file descriptors
  6. ** Create pipes
  7. ** Split at pipes
  8. */
  9. void pipe_init(t_e *e, char *all_cmd)
  10. {
  11. t_pipe *p;
  12. int i;
  13. p = (t_pipe*)malloc(sizeof(t_pipe));
  14. if (ft_strstr(all_cmd, "||") != NULL)
  15. {
  16. error_message("", "I do not handle \"||\"", NULL);
  17. return ;
  18. }
  19. count_pipe_nbr(e, p, all_cmd);
  20. p->fd = (int**)malloc(sizeof(int*) * p->nbr);
  21. i = -1;
  22. while (++i < p->nbr)
  23. {
  24. p->fd[i] = (int*)malloc(sizeof(int) * 2);
  25. if (pipe(p->fd[i]) == -1)
  26. {
  27. error_message("pipe", "creation issue", NULL);
  28. return ;
  29. }
  30. }
  31. pipe_init_2(e, p, all_cmd);
  32. }
  33. void pipe_init_2(t_e *e, t_pipe *p, char *all_cmd)
  34. {
  35. t_pr **t;
  36. p->cmd_pipe = cmd_splitting(e, all_cmd, '|');
  37. pipe_parsing(p, e);
  38. t = (t_pr**)malloc(sizeof(t_pr*) * (p->nbr + 2));
  39. fill_pipe_redir_data(e, p, t);
  40. if (ft_strchr(t[0]->operator, '<') != NULL && t[1] != NULL && \
  41. ft_strchr(t[1]->operator, '>') != NULL)
  42. launch_double_redirections(e, p, t);
  43. else
  44. launch_pipes(e, p, t);
  45. }
  46. /*
  47. ** //display_data(t);
  48. ** debug function
  49. ** display content on structure
  50. */
  51. void display_data(t_pr *t)
  52. {
  53. ft_printf("itxt: '%s', ifd: %d, operator: '%s', ofd: %d, otxt: '%s'\n",
  54. t->itxt, t->ifd, t->operator, t->ofd, t->otxt);
  55. }
  56. /*
  57. ** Malloc for commands and their options
  58. ** For all command remove non visible char before and after cmd
  59. ** Check a command is present
  60. ** If cmd == "": return
  61. ** If no cmd between pipe: display error message and return
  62. ** Split cmd and his options
  63. ** Free cmd_pipe
  64. */
  65. void pipe_parsing(t_pipe *p, t_e *e)
  66. {
  67. int val;
  68. int i;
  69. i = -1;
  70. while (p->cmd_pipe[++i] != NULL)
  71. {
  72. val = -1;
  73. if ((val = check_cmd_presence(p->cmd_pipe[i])) != 1)
  74. {
  75. if (val == 0)
  76. error_message("", "no cmd between pipes or semicolons", NULL);
  77. return ;
  78. }
  79. }
  80. }
  81. /*
  82. ** Count number of pipe occurences on comma splitted string.
  83. ** Only pipe betweeen non-coted blocks are counted.
  84. */
  85. void count_pipe_nbr(t_e *e, t_pipe *p, char *str)
  86. {
  87. int i;
  88. int occ;
  89. p->pnbr = 0;
  90. occ = 0;
  91. i = -1;
  92. while (str[++i] != '\0')
  93. {
  94. if (str[i] == '|' && is_on_coted_block(e, e->slen + i) == 1)
  95. p->pnbr++;
  96. else if ((str[i] == '>' || str[i] == '<') && \
  97. is_on_coted_block(e, e->slen + i) == 1)
  98. {
  99. occ++;
  100. i++;
  101. }
  102. }
  103. p->nbr = p->pnbr + occ;
  104. }