-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_prompt.c
75 lines (67 loc) · 1.15 KB
/
shell_prompt.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
#include "main.h"
/**
* rm_comment - deletes comments from the input
* @input: string input
* Return: input
*/
char *rm_comment(char *input)
{
int i, input_width;
input_width = 0;
for (i = 0; input[i]; i++)
{
if (input[i] == '#')
{
if (i == 0)
{
free(input);
return (NULL);
}
if (input[i - 1] == ' ' || input[i - 1] == '\t' || input[i - 1] == ';')
input_width = i;
}
}
if (input_width != 0)
{
input = _realloc(input, i, input_width + 1);
input[input_width] = '\0';
}
return (input);
}
/**
* shell_loop - shell loop
* @commandArg: relevant data
* Return: void
*/
void shell_loop(param *commandArg)
{
int loop, i_eof;
char *input;
loop = 1;
while (loop == 1)
{
write(STDIN_FILENO, ":)", 2);
input = read_line(&i_eof);
if (i_eof != -1)
{
input = rm_comment(input);
if (input == NULL)
continue;
if (check_syntax_error(commandArg, input) == 1)
{
commandArg->status = 2;
free(input);
continue;
}
input = rep_var(input, commandArg);
loop = split_commands(commandArg, input);
commandArg->counter += 1;
free(input);
}
else
{
loop = 0;
free(input);
}
}
}