-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
75 lines (66 loc) · 1.9 KB
/
shell.c
File metadata and controls
75 lines (66 loc) · 1.9 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
#include "hdr.h"
int get_arg_count(char **argv)
/* Returns number of arguments in array */
{
int i;
for (i = 0; argv[i] != NULL; i++);
return i;
}
int main(int ac, char **av, char **env)
/* Launches shell */
{
int status;
struct Stringlist *envlist;
envlist = strarr_to_list(env);
(void)(ac);
(void)(av);
status = 0;
while (1)
status = shell_prompt(status, envlist);
return 0;
}
int return_status(int status, char **argv)
/* Frees memory and then returns status */
{
free_array(argv);
return status;
}
int process_cmd(int status, char **argv, struct Stringlist *env)
/* Checks built-ins for cmd or checks paths for cmd */
{
int tmp;
tmp = check_builtins(argv[0], argv, env);
if (tmp == -1)
return 1;
else if (tmp)
return 0;
status = check_path(argv[0], argv, env);
return status;
}
int shell_prompt(int status, struct Stringlist *env)
/* Reads input and initializes processing */
{
char *input;
char **argv;
write_string("simple_shell> ");
input = read_line(0);
if (!input) {
write_string("simple_shell: Error reading arguments\n");
return 1;
}
argv = string_split(input, ' ');
free(input);
if (argv == NULL) {
write_string("simple_shell: Error processing arguments\n");
return return_status(1, argv);
} else if (argv[0] == NULL) {
return return_status(0, argv);
}
handle_comments(argv);
if (dollar_vars(status, argv, env)) {
write_string("simple_shell: Error converting variables\n");
return return_status(1, argv);
}
status = process_cmd(status, argv, env);
return return_status(status, argv);
}