-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms_count_arg_divided_qm.c
75 lines (69 loc) · 2.04 KB
/
ms_count_arg_divided_qm.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_count_arg_divided_qm.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlana <dlana@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 18:16:19 by dlana #+# #+# */
/* Updated: 2022/03/14 16:19:48 by dlana ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ms_check_quotation_marks(t_cmd *cmd, int i, t_data *data)
{
char q_m;
q_m = cmd->str[i];
i++;
while (cmd->str[i] != q_m && cmd->str[i] != '\0')
i++;
if (cmd->str[i] == q_m)
cmd->num_arg++;
else
{
data->num_error = ERR_TOKEN;
cmd->num_arg = 0;
cmd->array_empty = YES;
return (ms_error_2(data->num_error, q_m));
}
return (i + 1);
}
int ms_count_args_without_qm(t_cmd *cmd, int i)
{
if (cmd->str[i] == ' ')
{
while (cmd->str[i] == ' ')
i++;
}
if (cmd->str[i] != 39 && cmd->str[i] != 34 && cmd->str[i] != '\0')
cmd->num_arg++;
while (cmd->str[i] != 39 && cmd->str[i] != 34 && cmd->str[i] != '\0')
{
if (cmd->str[i] == ' ')
{
while (cmd->str[i] == ' ' && cmd->str[i] != '\0')
i++;
if (cmd->str[i] != 39 && cmd->str[i] != 34 && cmd->str[i] != '\0')
cmd->num_arg++;
}
else
i++;
}
return (i);
}
int ms_count_arg_divided_qm(t_cmd *cmd, t_data *data)
{
int i;
i = 0;
cmd->num_arg = 0;
while (cmd->str[i] != '\0')
{
if (cmd->str[i] == 34 || cmd->str[i] == 39)
i = ms_check_quotation_marks(cmd, i, data);
else
i = ms_count_args_without_qm(cmd, i);
if (i == -1)
return (-1);
}
return (0);
}