-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_shell.c
More file actions
237 lines (224 loc) · 5.16 KB
/
_shell.c
File metadata and controls
237 lines (224 loc) · 5.16 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "shell.h"
static char *free_env_list;
static int err_Cnt;
/**
* main - basic shelll
* @argc: num of cmd line args
* @argv: args at cmd line
* @env: environment data
* Return: int
*/
int main(int argc, char *argv[], char *env[])
{
char *command = NULL;
int ttrue = 1, cmd_Count = 0, mode, sig = 0;
char **Cmd = NULL;
my_ret my_lists;
my_lists.free_env_list = NULL, my_lists.alias_List = NULL;
free_env_list = NULL, err_Cnt = 1;
script_check(argc, argv, env, my_lists, Cmd), mode = isatty(STDIN_FILENO);
signal(SIGINT, SIG_IGN);
while (ttrue)
{
if (mode)
print_Prompt1();
command = read_Cmd(mode, &sig);
if (!command || sig == 1)
{
if (mode)
{free(command);
continue; }
else
{free(command);
free_env(env, my_lists.free_env_list);
free_list(my_lists.alias_List);
exit(0); }
}
cmd_Count = _strlen(command);
if (command[0] == '\0' || command[0] == '\n')
{free(command);
continue; }
if (cmd_Count != 0)
{
Cmd = parser1(command, argv[0]);
free(command);
my_lists = parser2(Cmd, argv[0], env, my_lists);
free_2d(Cmd);
}
err_Cnt++;
}
free_env(env, my_lists.free_env_list), free_list(my_lists.alias_List);
return (0);
}
/**
* read_Cmd - reads command into string
* @mode: mode
* @sig: signal
* Return: string
*/
char *read_Cmd(int mode, int *sig)
{
size_t bufsize = 1024;
ssize_t cmd_Check = 1;
char *cmd_Str = NULL, *buf = NULL, *cmd_temp = NULL;
int ptr_len = 0, cmd_index = 0, buf_len = 0, x = 0;
while ((cmd_Check = getline(&buf, &bufsize, stdin)) != -1)
{buf = comment_check(buf);
if (!buf)
{
if (mode)
print_Prompt1();
continue; }
buf_len = _strlen(buf);
if (!cmd_Str)
cmd_Str = malloc(buf_len + 1);
else
{
cmd_temp = _realloc(cmd_Str, cmd_index, ptr_len);
if (cmd_temp)
cmd_Str = cmd_temp;
else
free(cmd_Str), cmd_Str = NULL; }
_strcpy(cmd_Str + ptr_len, buf);
if (buf[buf_len - 1] == '\n')
{free(buf);
for (x = _strlen(cmd_Str) - 1; x >= 0; x--)
{
if (cmd_Str[x] == ' ' || cmd_Str[x] == '\n')
cmd_Str[x] = '\0';
else
break; }
return (cmd_Str); }
ptr_len += buf_len; }
if (cmd_Check == -1)
*sig = 1;
free(buf);
return (cmd_Str);
}
/**
* parser1 - tokenizes multiple cmds into strings
* @cmd_Str: cmd string input
* @argv: argv[0]
* Return: 2d array of strings
*/
char **parser1(char *cmd_Str, char *argv)
{
char **cmd_List = NULL;
char *tmp = NULL;
int x = 0, y = 0, xx = 0, cmd_Pos = 0, cmd_Str_Len = 0, cmd_Count = 1;
if (!cmd_Str)
return (NULL);
StrDimensions(cmd_Str, argv, &cmd_Str_Len, &cmd_Count);
cmd_List = malloc(sizeof(char *) * cmd_Str_Len + cmd_Count);
if (!cmd_List)
return (NULL);
while (cmd_Str[cmd_Pos])
{
if (cmd_Str[cmd_Pos] == ';')
cmd_Pos++;
else
{
y = 0;
tmp = _strtok(cmd_Str, &cmd_Pos, ';');
if (!tmp)
return (NULL);
while (tmp[y])
y++;
cmd_List[x] = malloc(sizeof(char) * (y + 1));
if (!cmd_List[x])
return (NULL);
for (xx = 0; xx < y; xx++)
cmd_List[x][xx] = tmp[xx];
cmd_List[x][xx] = '\0';
if (tmp)
free(tmp);
x++;
}
}
cmd_List[x] = NULL;
return (cmd_List);
}
/**
* parser2 - tokenizes strings into tokens
* @cmd_List: 2d array of strings
* @argv: argv[0]
* @env: environ
* @my_lists: return values
* Return: void. Passes tokens to execution func
*/
my_ret parser2(char **cmd_List, char *argv, char **env, my_ret my_lists)
{
int x = 0, i = 0, tok_idx = 0, chars = 0, words = 0;
int ret = 0;
char **tokes = NULL;
char *our_path = NULL;
for (x = 0; cmd_List[x]; x++)
{
cmd_List[x] = alias_Check(cmd_List[x], my_lists.alias_List, argv);
dim2(cmd_List[x], &chars, &words, argv);
tokes = malloc(sizeof(char *) * chars + words);
tokes[0] = _strtok(cmd_List[x], &tok_idx, ' ');
if (_strcmp(tokes[0], "alias") == 0)
{
my_lists.alias_List = alias_Options(argv, cmd_List[x], my_lists.alias_List);
free(tokes[0]);
free(tokes);
continue;
}
for (i = 1; i < words; i++)
tokes[i] = _strtok(cmd_List[x], &tok_idx, ' ');
tokes[i] = NULL;
my_lists.free_env_list = builtin(cmd_List, my_lists.alias_List,
my_lists.free_env_list, tokes, argv, env, &ret);
if (ret == 0)
{
our_path = get_path(env, tokes[0]);
if (our_path)
tokes[0] = our_path;
else
{
handle_err(argv, 2, tokes[0]);
free_2d(tokes);
continue;
}
exec_Cmd(tokes, argv, env);
}
free_2d(tokes);
chars = 0, words = 0, tok_idx = 0;
}
return (my_lists);
}
/**
* handle_err - Handles errors
* @argv: Argument
* @err_num: Counting error numbers
* @token: token
* Return: Void
*/
void handle_err(char *argv, int err_num, char *token)
{
char *temp = NULL;
char *num = NULL;
int error_cnt = 0;
char *not_found = "not found\n", *illegal = "Illegal number";
error_cnt = err_Cnt;
num = print_number(num, error_cnt);
if (err_num == 2 || err_num == 20)
{
temp = cat_err(num, argv, not_found, token);
write(STDERR_FILENO, temp, _strlen(temp));
}
else if (err_num == 122)
{
temp = cat_err2(num, argv, illegal, token);
write(STDERR_FILENO, temp, _strlen(temp));
}
else
{
temp = cat_err(num, argv, "No such file or directory", token);
write(STDERR_FILENO, temp, _strlen(temp));
perror(token);
}
free(temp);
free(num);
}