parse_redirections.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "sh.h"
  2. /*
  3. ** Free memory of redirect struct
  4. ** r->file is not freed as I couldn't check when it wasn't allocated
  5. */
  6. void free_redir(t_redir *r)
  7. {
  8. ft_strdel(&r->cmd);
  9. ft_strdel(&r->operator);
  10. ft_strdel(&r->ofd);
  11. free(r);
  12. }
  13. /*
  14. ** while not found chevron (increment begin position of check):
  15. ** - for every block:
  16. ** - if chev is inside the block and the block is non-coted
  17. ** - return chevron position
  18. ** else: return error
  19. */
  20. int check_chev_is_on_non_coted_block(t_e *e, char *str, char c)
  21. {
  22. int j;
  23. int chev;
  24. chev = 0;
  25. while (ft_search_pos_char(&str[chev + 1], c) != -1)
  26. {
  27. j = -1;
  28. chev += ft_search_pos_char(&str[chev + 1], c) + 1;
  29. while (e->b[++j] != NULL)
  30. if (e->b[j]->type == 0 && e->slen + chev >= e->b[j]->begin && \
  31. e->slen + chev <= e->b[j]->end)
  32. return (chev);
  33. }
  34. return (-1);
  35. }
  36. /*
  37. ** Check if there is visible char before and after chevrons
  38. */
  39. int check_before_after_chevron(char *str, int chev)
  40. {
  41. if ((str[chev - 1] == ' ' && ft_isvisible(str[chev - 2]) == 0) || \
  42. (str[chev + 1] == ' ' && ft_isvisible(str[chev + 2]) == 0) || \
  43. ft_isprint(str[chev - 1]) == 0 || ft_isprint(str[chev + 1] \
  44. == 0 && str[chev + 1] != '>' && str[chev + 1] != '<') || \
  45. ((str[chev + 1] == '>' || str[chev + 1] == '<') \
  46. && ft_isprint(str[chev + 2]) == 0))
  47. {
  48. error_message("", "", "nothing specified before or after chevron");
  49. return (1);
  50. }
  51. return (0);
  52. }
  53. /*
  54. ** Handle splitting if '&[1-2]' is here
  55. ** echo test|2>|tata
  56. ** echo test|2>|&1|tata
  57. ** Check '&[1-2]' presence: andp: and presence
  58. ** If '&' presence: parse one way, else: parse other way
  59. */
  60. void parse_and(char *str, t_redir *r, int file_pos)
  61. {
  62. char *and;
  63. int andp;
  64. and = ft_strchr(str, '&');
  65. andp = (and != NULL && (and[1] == '0' || and[1] == '1' \
  66. || and[1] == '2' || and[1] == '-')) ? 1 : 0;
  67. r->ofd = andp == 1 ? ft_strsub(and, 1, 1) : NULL;
  68. r->file = andp == 1 ? &and[2] : ft_strtrim(&str[file_pos]);
  69. }