forked from adiozdaniel/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
78 lines (70 loc) · 1.15 KB
/
shell.c
File metadata and controls
78 lines (70 loc) · 1.15 KB
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
#include "main.h"
/**
* no_comment - deletes comments from the input
*
* @in: input string
* Return: input without comments
*/
char *no_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 of shell
* @datash: data relevant (av, input, args)
*
* Return: no return.
*/
void shell(data_shell *datash)
{
int loop, i_eof;
char *input;
char *prompt = "Happy&Adioz-Shell: ";
loop = 1;
while (loop == 1)
{
write(STDIN_FILENO, prompt, _strlen(prompt));
input = read_line(&i_eof);
if (i_eof != -1)
{
input = no_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);
}
}
}