-
Notifications
You must be signed in to change notification settings - Fork 29
/
shell_loop.c
77 lines (69 loc) · 1.13 KB
/
shell_loop.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
#include "holberton.h"
/**
* without_comment - deletes comments from the input
*
* @in: input string
* Return: input without comments
*/
char *without_comment(char *in)
{
int i, up_to;
up_to = 0;
for (i = 0; in[i]; i++)
{
if (in[i] == '#')
{
if (i == 0)
{
free(in);
return (NULL);
}
if (in[i - 1] == ' ' || in[i - 1] == '\t' || in[i - 1] == ';')
up_to = i;
}
}
if (up_to != 0)
{
in = _realloc(in, i, up_to + 1);
in[up_to] = '\0';
}
return (in);
}
/**
* shell_loop - Loop of shell
* @datash: data relevant (av, input, args)
*
* Return: no return.
*/
void shell_loop(data_shell *datash)
{
int loop, i_eof;
char *input;
loop = 1;
while (loop == 1)
{
write(STDIN_FILENO, "^-^ ", 4);
input = read_line(&i_eof);
if (i_eof != -1)
{
input = without_comment(input);
if (input == NULL)
continue;
if (check_syntax_error(datash, input) == 1)
{
datash->status = 2;
free(input);
continue;
}
input = rep_var(input, datash);
loop = split_commands(datash, input);
datash->counter += 1;
free(input);
}
else
{
loop = 0;
free(input);
}
}
}