-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms_quotation_marks.c
116 lines (106 loc) · 2.71 KB
/
ms_quotation_marks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_quotation_marks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlana <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 17:48:02 by dlana #+# #+# */
/* Updated: 2022/03/11 17:51:52 by dlana ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ms_switch_qm(char c, int *qm_o, int *qm_d)
{
if (c == DOUBLE_Q_MARK && (*qm_o) == 1)
(*qm_d) = (*qm_d) * (-1);
if (c == ONE_Q_MARK && (*qm_d) == 1)
(*qm_o) = (*qm_o) * (-1);
}
void ms_search_space_after_arg(char *str, t_arg *arg, int i)
{
int c;
c = 0;
if (str[i] == ' ')
{
c = i;
while (str[c] != '\0' && str[c] == ' ')
c++;
if (str[c] != '\0')
arg->space = YES;
}
}
int ms_cut_quotation_marks(char *str, t_arg *arg, int i)
{
int c;
char q_m;
c = 0;
q_m = str[i];
i++;
arg->q_m = (int)q_m;
while (str[i] != q_m && str[i] != '\0')
{
c++;
i++;
}
ms_malloc_str(&arg->str, c);
i = i - c;
c = 0;
while (str[i] != q_m && str[i] != '\0')
ms_record_char(&arg->str, str, &c, &i);
arg->str[c] = '\0';
if (str[i] == q_m)
{
i++;
ms_search_space_after_arg(str, arg, i);
}
return (i);
}
int ms_record_args_without_qm(char *str, t_arg *arg, int i, int *num_arg)
{
int c;
c = 0;
arg->q_m = NO;
if (str[i] != ' ')
{
while (str[i] != 39 && str[i] != 34 && str[i] != '\0' && str[i] != ' ')
{
c++;
i++;
}
ms_malloc_str(&arg->str, c);
i = i - c;
c = 0;
while (str[i] != 39 && str[i] != 34 && str[i] != '\0' && str[i] != ' ')
ms_record_char(&arg->str, str, &c, &i);
arg->str[c] = '\0';
ms_search_space_after_arg(str, arg, i);
(*num_arg)++;
return (i);
}
return (i + 1);
}
void ms_create_struct_without_qm(t_cmd *cmd)
{
int i;
int num_arg;
i = 0;
num_arg = 0;
ms_malloc_arg(&cmd->arg, cmd->num_arg);
while (cmd->str[i] != '\0')
{
cmd->arg[num_arg].space = NO;
if (cmd->str[i] == 34 || cmd->str[i] == 39)
{
i = ms_cut_quotation_marks(cmd->str, &cmd->arg[num_arg], i);
num_arg++;
}
else
{
while (cmd->str[i] != 34 && cmd->str[i] != 39
&& cmd->str[i] != '\0')
i = ms_record_args_without_qm(cmd->str,
&cmd->arg[num_arg], i, &num_arg);
}
}
}