-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_helpers.c
209 lines (188 loc) · 4.32 KB
/
input_helpers.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "shell.h"
char *get_args(char *line, int *exe_ret);
int call_args(char **args, char **front, int *exe_ret);
int run_args(char **args, char **front, int *exe_ret);
int handle_args(int *exe_ret);
int check_args(char **args);
/**
* get_args - Gets a command from standard input.
* @line: A buffer to store the command.
* @exe_ret: The return value of the last executed command.
*
* Return: If an error occurs - NULL.
* Otherwise - a pointer to the stored command.
*/
char *get_args(char *line, int *exe_ret)
{
size_t n = 0;
ssize_t read;
char *prompt = "$ ";
if (line)
free(line);
read = _getline(&line, &n, STDIN_FILENO);
if (read == -1)
return (NULL);
if (read == 1)
{
hist++;
if (isatty(STDIN_FILENO))
write(STDOUT_FILENO, prompt, 2);
return (get_args(line, exe_ret));
}
line[read - 1] = '\0';
variable_replacement(&line, exe_ret);
handle_line(&line, read);
return (line);
}
/**
* call_args - Partitions operators from commands and calls them.
* @args: An array of arguments.
* @front: A double pointer to the beginning of args.
* @exe_ret: The return value of the parent process' last executed command.
*
* Return: The return value of the last executed command.
*/
int call_args(char **args, char **front, int *exe_ret)
{
int ret, index;
if (!args[0])
return (*exe_ret);
for (index = 0; args[index]; index++)
{
if (_strncmp(args[index], "||", 2) == 0)
{
free(args[index]);
args[index] = NULL;
args = replace_aliases(args);
ret = run_args(args, front, exe_ret);
if (*exe_ret != 0)
{
args = &args[++index];
index = 0;
}
else
{
for (index++; args[index]; index++)
free(args[index]);
return (ret);
}
}
else if (_strncmp(args[index], "&&", 2) == 0)
{
free(args[index]);
args[index] = NULL;
args = replace_aliases(args);
ret = run_args(args, front, exe_ret);
if (*exe_ret == 0)
{
args = &args[++index];
index = 0;
}
else
{
for (index++; args[index]; index++)
free(args[index]);
return (ret);
}
}
}
args = replace_aliases(args);
ret = run_args(args, front, exe_ret);
return (ret);
}
/**
* run_args - Calls the execution of a command.
* @args: An array of arguments.
* @front: A double pointer to the beginning of args.
* @exe_ret: The return value of the parent process' last executed command.
*
* Return: The return value of the last executed command.
*/
int run_args(char **args, char **front, int *exe_ret)
{
int ret, i;
int (*builtin)(char **args, char **front);
builtin = get_builtin(args[0]);
if (builtin)
{
ret = builtin(args + 1, front);
if (ret != EXIT)
*exe_ret = ret;
}
else
{
*exe_ret = execute(args, front);
ret = *exe_ret;
}
hist++;
for (i = 0; args[i]; i++)
free(args[i]);
return (ret);
}
/**
* handle_args - Gets, calls, and runs the execution of a command.
* @exe_ret: The return value of the parent process' last executed command.
*
* Return: If an end-of-file is read - END_OF_FILE (-2).
* If the input cannot be tokenized - -1.
* O/w - The exit value of the last executed command.
*/
int handle_args(int *exe_ret)
{
int ret = 0, index;
char **args, *line = NULL, **front;
line = get_args(line, exe_ret);
if (!line)
return (END_OF_FILE);
args = _strtok(line, " ");
free(line);
if (!args)
return (ret);
if (check_args(args) != 0)
{
*exe_ret = 2;
free_args(args, args);
return (*exe_ret);
}
front = args;
for (index = 0; args[index]; index++)
{
if (_strncmp(args[index], ";", 1) == 0)
{
free(args[index]);
args[index] = NULL;
ret = call_args(args, front, exe_ret);
args = &args[++index];
index = 0;
}
}
if (args)
ret = call_args(args, front, exe_ret);
free(front);
return (ret);
}
/**
* check_args - Checks if there are any leading ';', ';;', '&&', or '||'.
* @args: 2D pointer to tokenized commands and arguments.
*
* Return: If a ';', '&&', or '||' is placed at an invalid position - 2.
* Otherwise - 0.
*/
int check_args(char **args)
{
size_t i;
char *cur, *nex;
for (i = 0; args[i]; i++)
{
cur = args[i];
if (cur[0] == ';' || cur[0] == '&' || cur[0] == '|')
{
if (i == 0 || cur[1] == ';')
return (create_error(&args[i], 2));
nex = args[i + 1];
if (nex && (nex[0] == ';' || nex[0] == '&' || nex[0] == '|'))
return (create_error(&args[i + 1], 2));
}
}
return (0);
}