-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeshell.c
More file actions
executable file
·97 lines (83 loc) · 1.55 KB
/
theshell.c
File metadata and controls
executable file
·97 lines (83 loc) · 1.55 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "main.h"
/**
* line_read - Read a line of input from stdin
* @eof_status: A pointer to an integer to store EOF or error status
*
* Return: A pointer to the read line or NULL on EOF or error
*/
char *line_read(int *eof_status)
{
size_t buffer_size = 0;
char *user_input = NULL;
*eof_status = getline(&user_input, &buffer_size, stdin);
return (user_input);
}
/**
* remove_comment - function to remove comment from the shell input
* @in: input
*
* Return: valid command input only (without the comments)
*/
char *remove_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);
}
/**
* theshell - loop of the shell
* @shelldata: data structure containing shell data
*
* Return: nothing
*/
void theshell(shell_state *shelldata)
{
int allow, read;
char *input = NULL;
allow = 1;
while (allow == 1)
{
write(STDIN_FILENO, "$ ", 2);
fflush(stdout);
input = line_read(&read);
if (read != -1)
{
input = remove_comment(input);
if (input == NULL)
continue;
if (check_error(shelldata, input) == -1)
{
free(input);
shelldata->status = 2;
continue;
}
input = rep_var(input, shelldata);
allow = split_commands(shelldata, input);
shelldata->counter += 1;
free(input);
}
else
{
free(input);
allow = 0;
}
}
}