-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
106 lines (94 loc) · 2.23 KB
/
main.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
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 1000
#endif
#include "error.h"
#include "program.h"
#include "job.h"
#include "tokenizer.h"
#include "core.h"
#include "built_in.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int next_job_res;
job *j;
initialize_shell();
{
int res = set_environment_vars();
if (res != E_OK)
{
print_error(res);
return 1;
}
argc_s = argc;
argv_s = argv;
}
initialize_buffer();
while (1)
{
update_all_jobs();
notify();
if (shell_isatty)
invite();
if (read_line_to_buffer() == E_EOF)
break;
next1: next_job_res = next_job(&j);
if (next_job_res != E_OK)
{
print_error(next_job_res);
}
else if (!j->number_of_programs)
{
destroy_job(&j);
}
else if (j->number_of_programs > 1 && check_built_in_piped(j) == E_BUILT_IN_PIPED)
{
fprintf(stderr, "built-in commands can not be piped\n");
fflush(stderr);
destroy_job(&j);
}
else if (!strcmp(j->programs[0]->arguments[0], "exit"))
{
destroy_job(&j);
break;
}
else if (!strcmp(j->programs[0]->arguments[0], "cd"))
{
int cd_handler_res = cd_handler(j->programs[0]);
print_error(cd_handler_res);
destroy_job(&j);
}
else if (!strcmp(j->programs[0]->arguments[0], "jobs"))
{
jobs_handler(j);
destroy_job(&j);
}
else if (!strcmp(j->programs[0]->arguments[0], "fg"))
{
fg_handler(j);
destroy_job(&j);
}
else if (!strcmp(j->programs[0]->arguments[0], "bg"))
{
bg_handler(j);
destroy_job(&j);
}
else
{
j->next = job_list;
job_list = j;
spawn_job(j);
}
if (buffer[buffer_position] == ';')
{
++buffer_position;
goto next1;
}
}
update_all_jobs();
notify();
destroy_buffer();
destroy_job_list();
return 0;
}