-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshs.c
More file actions
127 lines (109 loc) · 2 KB
/
shs.c
File metadata and controls
127 lines (109 loc) · 2 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "shs.h"
/**
* main - Entry point
*
* @ac: argument count
* @av: argument vector
*
* Return: 0 on success.
*/
int main(int ac, char **av)
{
shell_data shdata;
(void) ac;
signal(SIGINT, get_signal);
set_data(&shdata, av);
shell_prompt(&shdata);
free_data(&shdata);
if (shdata.status < 0)
return (255);
return (shdata.status);
}
/**
* free_data - frees data structure
*
* @shdata: data structure
* Return: no return
*/
void free_data(shell_data *shdata)
{
unsigned int i;
for (i = 0; shdata->_environ[i]; i++)
{
free(shdata->_environ[i]);
}
free(shdata->_environ);
free(shdata->pid);
}
/**
* set_data - Initialize data structure
*
* @shdata: data structure
* @av: argument vector
* Return: no return
*/
void set_data(shell_data *shdata, char **av)
{
unsigned int i;
shdata->av = av;
shdata->input = NULL;
shdata->args = NULL;
shdata->status = 0;
shdata->counter = 1;
for (i = 0; environ[i]; i++)
;
shdata->_environ = malloc(sizeof(char *) * (i + 1));
for (i = 0; environ[i]; i++)
{
shdata->_environ[i] = _strdup(environ[i]);
}
shdata->_environ[i] = NULL;
shdata->pid = int_to_string(getpid());
}
/**
* shell_prompt - Loop of shell
* @shdata: data relevant (av, input, args)
*
* Return: no return.
*/
void shell_prompt(shell_data *shdata)
{
int should_continue, i_eof;
char *input;
should_continue = 1;
while (should_continue == 1)
{
write(STDIN_FILENO, "^J-S^ > ", 8);
input = _get_line(&i_eof);
if (i_eof != -1)
{
input = without_comment(input);
if (input == NULL)
continue;
if (check_syntax_error(shdata, input) == 1)
{
shdata->status = 2;
free(input);
continue;
}
input = rep_str_var(input, shdata);
should_continue = div_commands(shdata, input);
shdata->counter += 1;
free(input);
}
else
{
should_continue = 0;
free(input);
}
}
}
/**
* get_signal - Handle the Ctrl+C call in the prompt
* @s: Signal handler
*/
void get_signal(int s)
{
(void)s;
write(STDOUT_FILENO, "\n^J-S^ > ", 8);
}