pipe_redir_parsing.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "sh.h"
  2. /*
  3. ** Fill redirections
  4. ** Else: fill pipe
  5. */
  6. void fill_pipe_redir_data(t_e *e, t_pipe *p, t_pipe_redir **t)
  7. {
  8. int i;
  9. int j;
  10. i = -1;
  11. j = -1;
  12. while (p->cmd_pipe[++j] != NULL)
  13. {
  14. t[++i] = (t_pipe_redir*)malloc(sizeof(t_pipe_redir));
  15. if (parse_redirection(e, p, p->cmd_pipe[j], t[i]) == 1)
  16. ;
  17. else
  18. {
  19. t[i]->itxt = p->cmd_pipe[j];
  20. t[i]->ifd = 1;
  21. t[i]->operator = "|";
  22. t[i]->ofd = 0;
  23. t[i]->otxt = NULL;
  24. }
  25. }
  26. t[++i] = NULL;
  27. }
  28. /*
  29. ** Get chevrons positions
  30. ** Take closer chevron
  31. ** Prevent segfault by checking there is something before and after chevron
  32. ** Parse redirections: split in four parts:
  33. ** Get position of the first chevron
  34. ** Before chevron, chevron, after chevron
  35. ** Check if fd is specified
  36. ** Check if there is two chevrons
  37. ** Split with strsub
  38. */
  39. int parse_redirection(t_e *e, t_pipe *p, char *str, t_pipe_redir *t)
  40. {
  41. int chev;
  42. int chev2;
  43. int nchev;
  44. int fd;
  45. t_redir *r;
  46. chev = check_chev_is_on_non_coted_block(e, str, '>');
  47. chev2 = check_chev_is_on_non_coted_block(e, str, '<');
  48. if (chev != -1 || chev2 != -1)
  49. {
  50. chev = (chev != -1 && chev < chev2) || chev2 == -1 ? chev : chev2;
  51. if (check_before_after_chevron(str, chev) == 1)
  52. return (0);
  53. r = (t_redir*)malloc(sizeof(t_redir));
  54. fd = str[chev - 1] == '1' || str[chev - 1] == '2' ? 1 : 0;
  55. nchev = str[chev] == str[chev + 1] ? 2 : 1;
  56. r->cmd = ft_strsub(str, 0, chev - fd);
  57. r->operator = ft_strsub(str, chev - fd, nchev + fd);
  58. parse_and(str, r, chev + nchev);
  59. t->itxt = ft_strdup(r->cmd);
  60. t->operator = ft_strdup(r->operator);
  61. t->otxt = ft_strdup(r->file);
  62. // t->otxt = r->ofd;
  63. t->ifd = r->pfd[0];
  64. t->ofd = r->pfd[1];
  65. // redirections(e, p, r);
  66. // free_redir(r);
  67. return (1);
  68. }
  69. else
  70. return (0);
  71. }