parse_redirections.c 1.7 KB

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