cotes_parsing.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "sh.h"
  2. /*
  3. * ** cotes_parsing.c
  4. * */
  5. t_cotes *init_variables(t_cotes *c);
  6. void replace_spaces_between_cotes(t_e *e, char *line);
  7. void detect_coted_blocks(t_e *e, t_cotes *c, char *line);
  8. char *replace_ack_by_space(char *cmd);
  9. ————————————————————————————————————————
  10. t_cotes *init_variables(t_cotes *c)
  11. {
  12. c = (t_cotes*)malloc(sizeof(t_cotes));
  13. c->cotes = 0;
  14. c->pos_l = 0;
  15. c->pos_c = 0;
  16. return (c);
  17. }
  18. /*
  19. ** Replace spaces between cotes by 'ack'/6 char
  20. ** Allocate a new string
  21. ** If found opening cote:
  22. ** - If found opening and closing cotes:
  23. ** - copy string and replace ' ' by 6, don't copy parameters
  24. ** - If end of block: put close at 0
  25. ** Else: simply copy char
  26. ** free struct
  27. ** launch comma split and all sub-functions
  28. */
  29. void replace_spaces_between_cotes(t_e *e, char *line)
  30. {
  31. char *cmd;
  32. t_cotes *c;
  33. int i;
  34. c = init_variables(c);
  35. cmd = ft_strnew(ft_strlen(line));
  36. while (line[c->pos_l] != '\0')
  37. {
  38. i = -1;
  39. while (e->par_open[++i] != '\0')
  40. if (line[c->pos_l] == e->par_open[i])
  41. {
  42. detect_coted_blocks(e, c, line);
  43. if (c->cotes == 1 && line[c->pos_l++] == ' ')
  44. cmd[c->pos_c++] = 6;
  45. else if (c->cotes == 1 && (line[c->pos_l] == \
  46. e->par_open[c->j] || line[c->pos_l] == e->par_close[c->j]))
  47. if (line[++c->pos_l] == e->par_open[c->j])
  48. c->cotes = 0;
  49. }
  50. cmd[c->pos_c++] = line[c->pos_l++];
  51. }
  52. free(c);
  53. e->cmd = line;
  54. //ft_printf("|%s|\n", e->cmd); // debug
  55. comma_split(e, e->cmd);
  56. }
  57. /*
  58. ** for every coted types:
  59. ** - if there is an open and close coted of same type:
  60. ** - change position to first cote, set it's a coted block, break
  61. */
  62. void detect_coted_blocks(t_e *e, t_cotes *c, char *line)
  63. {
  64. int first;
  65. c->j = -1;
  66. while (e->par_open[++c->j])
  67. if ((first = \
  68. ft_search_pos_char(&line[c->pos_l], e->par_open[c->j])) != -1 &&\
  69. ft_search_pos_char(&line[c->pos_l + first + 1], e->par_close[c->j]) != -1)
  70. {
  71. c->pos_l += first;
  72. c->cotes = 1;
  73. break ;
  74. }
  75. }
  76. /*
  77. ** After spliting at space and tab:
  78. ** Replace 6 char by spaces
  79. */
  80. char *replace_ack_by_space(char *cmd)
  81. {
  82. int pos;
  83. pos = -1;
  84. while (cmd[++pos])
  85. if (cmd[pos] == 6)
  86. cmd[pos] = ' ';
  87. return (cmd);
  88. }
  89. ——————————————————————
  90. /*
  91. ** Struct
  92. ** cotes: bollean if there is cotes at current position
  93. ** pos_l: position on line
  94. ** pos_c: position on cmd
  95. ** j: position on parameters lists
  96. */
  97. typedef struct s_cotes {
  98. int cotes;
  99. int pos_l;
  100. int pos_c;
  101. int j;
  102. } t_cotes;