-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdvalidator.c
97 lines (88 loc) · 2.97 KB
/
cmdvalidator.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmdvalidator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dcelsa <dcelsa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/02 16:06:31 by dcelsa #+# #+# */
/* Updated: 2022/04/03 00:28:06 by dcelsa ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void redirvalidator(t_bounds *bounds, t_list *qtxt, char *prog)
{
t_bounds crsr;
crsr.begin = bounds->begin;
crsr.end = bounds->end;
if (!istoken(crsr.begin, "<>"))
crsr.begin = symbdefiner(&crsr, "<>", qtxt);
while (crsr.begin < bounds->end)
{
if (istoken(++crsr.begin, "<>"))
crsr.begin++;
while (*crsr.begin && !istoken(crsr.begin, " <>&|"))
crsr.begin++;
crsr.begin -= istoken(crsr.begin, "<>");
if (istoken(crsr.begin, "<>|&") || *crsr.begin == '*')
parserr(prog, crsr.begin);
crsr.begin = symbdefiner(&crsr, "<>", qtxt);
}
}
static void dlrvalidator(char *cmds, char *prog, t_list *qtxt)
{
t_bounds bounds;
bounds.begin = cmds;
bounds.end = cmds + ft_strlen(cmds);
if (*bounds.begin != '$')
bounds.begin = symbdefiner(&bounds, "$", qtxt);
while (bounds.begin < bounds.end)
{
if (!ft_isalpha(*(++bounds.begin)) && !istoken(bounds.begin, " '\"?")
&& *bounds.begin)
parserr(prog, bounds.begin);
bounds.begin = symbdefiner(&bounds, "$", qtxt);
}
}
static void checkbounds(t_bounds *crsr, char *prog)
{
char *cursor;
if (crsr->end - crsr->begin < 2 && istoken(crsr->begin, "|&<>"))
parserr(prog, crsr->end);
if ((*crsr->end == '&' && *(crsr->end + 1) != '&')
|| (*crsr->end == '|' && *(crsr->end + 1) == '&'))
parserr(prog, crsr->end);
cursor = crsr->begin + 1;
if (!istoken(crsr->begin, "&|<>") && !*cursor)
return ;
while (cursor < crsr->end && *cursor == ' ')
cursor++;
if (cursor < crsr->end)
return ;
parserr(prog, crsr->end);
}
int strvalidator(char *prog, char *cmds)
{
t_bounds crsr;
t_list *qtxt;
pid_t pid;
int stat_loc;
pid = fork();
if (pid && waitpid(pid, &stat_loc, WUNTRACED))
return (stat_loc);
while (*cmds == ' ')
cmds++;
qtxt = NULL;
quotedtxt(cmds, prog, &qtxt, TRUE);
dlrvalidator(cmds, prog, qtxt);
crsr.begin = cmds;
while (crsr.begin < cmds + ft_strlen(cmds))
{
crsr.end = crsr.begin + ft_strlen(crsr.begin);
crsr.end = symbdefiner(&crsr, SYMBS, qtxt);
checkbounds(&crsr, prog);
redirvalidator(&crsr, qtxt, prog);
crsr.begin = crsr.end + (*crsr.end == *(crsr.end + 1));
}
exit(0);
}