-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.c
315 lines (265 loc) · 6.49 KB
/
job.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <assert.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <wait.h>
#include "absyn.h"
#include "common.h"
#include "job.h"
#include "lexer.h"
#include "memory.h"
#include "parser.tab.h"
bool do_exit = false;
static Job *job_list = NULL;
static struct termios original_termios = {0};
void enable_raw_mode(void) {
static struct termios raw = {0};
tcgetattr(STDIN_FILENO, &original_termios);
raw = original_termios;
raw.c_lflag = ICANON | ECHO | ECHOE;
raw.c_cc[VMIN] = 2;
raw.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
void disable_raw_mode(void) {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios);
}
Command *new_command(void) {
Command *cmd = gc_alloc(sizeof(Command));
cmd->argc = 0;
for (size_t i = 0; i < ARGV_MAX; i++)
cmd->argv[i] = NULL;
cmd->next = NULL;
return cmd;
}
Command *add_command(Command *head, Command *new_cmd) {
Command *tmp = head;
while (tmp->next)
tmp = tmp->next;
tmp->next = new_cmd;
return tmp->next;
}
void add_argv(Command *cmd, const char *arg) {
cmd->argv[cmd->argc++] = gc_strndup(arg, strlen(arg));
}
Job *find_job_by_id(int job_id) {
Job **current = &job_list;
while (*current) {
if ((*current)->job_id == job_id)
return *current;
current = &(*current)->next;
}
return NULL;
}
int get_job_id(pid_t pgid) {
Job **current = &job_list;
while (*current) {
if ((*current)->pgid == pgid)
return (*current)->job_id;
current = &(*current)->next;
}
return -1;
}
Job *add_job(pid_t pgid, const char *command, int status) {
Job *job = gc_alloc(sizeof(Job));
job->job_id = rand();
job->pgid = pgid;
job->command = gc_strndup(command, strlen(command));
job->status = status;
job->next = job_list;
job_list = job;
return job;
}
void update_job_status(pid_t pgid, int status) {
Job **current = &job_list;
while (*current) {
if ((*current)->pgid == pgid) {
(*current)->status = status;
return;
}
current = &(*current)->next;
}
}
void remove_job(pid_t pgid) {
Job **current = &job_list;
while (*current) {
if ((*current)->pgid == pgid) {
*current = (*current)->next;
return;
}
current = &(*current)->next;
}
}
void kill_job(int job_id, int signal) {
Job *job = find_job_by_id(job_id);
kill(-job->pgid, signal);
remove_job(job->pgid);
}
void kill_job_by_status(int status) {
Job **current = &job_list;
while (*current) {
if ((*current)->status == status)
kill_job((*current)->job_id, SIGINT);
current = &(*current)->next;
}
}
void handle_sigchld(int _) {
int status;
pid_t pid;
while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) > 0) {
if (WIFEXITED(status) || WIFSIGNALED(status)) {
update_job_status(pid, JSTAT_Done);
remove_job(pid);
} else if (WIFSTOPPED(status)) {
update_job_status(pid, JSTAT_Stopped);
} else if (WIFCONTINUED(status)) {
update_job_status(pid, JSTAT_Running);
}
}
}
void handle_sigstop(int _) {
pid_t fg_pid = tcgetpgrp(STDIN_FILENO);
if (fg_pid != getpid()) {
kill(-fg_pid, SIGSTOP);
}
}
void handle_sigint(int _) { fprintf(stderr, "\nSIGINT thrown\n"); }
void handle_terminal_signals(void) {
struct sigaction sa;
sa.sa_handler = SIG_IGN;
sigaction(SIGTTOU, &sa, NULL);
sigaction(SIGTTIN, &sa, NULL);
sigaction(SIGSTOP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
}
void launch_job(Command *cmds, bool background) {
int pipe_fds[2];
int prev_fd = -1;
pid_t pgid = 0;
Job *job = NULL;
Command **current_cmd = &cmds;
while (*current_cmd) {
if ((*current_cmd)->next != NULL) {
if (pipe(pipe_fds) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
}
pid_t pid = fork();
if (pid == 0) {
setpgid(0, 0);
if (!background) {
tcsetpgrp(STDIN_FILENO, getpid());
}
if (prev_fd != -1) {
dup2(prev_fd, STDIN_FILENO);
close(prev_fd);
}
if ((*current_cmd)->next != NULL) {
dup2(pipe_fds[1], STDOUT_FILENO);
close(pipe_fds[1]);
close(pipe_fds[0]);
}
signal(SIGINT, handle_sigint);
signal(SIGSTOP, handle_sigstop);
signal(SIGCHLD, handle_sigchld);
execvp((*current_cmd)->argv[0], (char *const *)&(*current_cmd)->argv[0]);
perror("execvp");
exit(EXIT_FAILURE);
} else if (pid > 0) {
if (pgid == 0)
pgid = pid;
setpgid(pid, pgid);
if (!job) {
job = add_job(pgid, (*current_cmd)->argv[0], background);
}
if (prev_fd != -1)
close(prev_fd);
if ((*current_cmd)->next != NULL) {
close(pipe_fds[1]);
prev_fd = pipe_fds[0];
}
} else {
perror("fork");
exit(EXIT_FAILURE);
}
current_cmd = &(*current_cmd)->next;
}
if (!background) {
tcsetpgrp(STDIN_FILENO, pgid);
int status;
waitpid(-pgid, &status, WUNTRACED);
if (WIFSTOPPED(status)) {
update_job_status(pgid, JSTAT_Stopped);
} else {
remove_job(pgid);
}
tcsetpgrp(STDIN_FILENO, getpid());
} else {
fprintf(stderr, "[%d] %d\n", job->job_id, pgid);
}
}
void execute_fg(int job_id) {
Job *job = find_job_by_id(job_id);
if (job) {
tcsetpgrp(STDIN_FILENO, job->pgid);
kill(-job->pgid, SIGCONT);
waitpid(-job->pgid, NULL, WUNTRACED);
tcsetpgrp(STDIN_FILENO, getpid());
}
}
void execute_bg(int job_id) {
Job *job = find_job_by_id(job_id);
if (job) {
kill(-job->pgid, SIGCONT);
job->status = JSTAT_Running;
}
}
int main(int argc, char **argv) {
gc_init();
atexit(gc_shutdown);
handle_terminal_signals();
enable_raw_mode();
for (;;) {
printf("squash> ");
fflush(stdout);
int inchr = 0;
char *prompt = gc_alloc(LINE_SIZE);
size_t cursor = 0;
while (read(STDIN_FILENO, &inchr, 1)
&& inchr != '\x04'
&& inchr != '\x03'
&& inchr != '\x1a'
&& inchr != '\x00'
&& inchr != '\r'
&& inchr != '\n') {
if (inchr == '\x04'
|| inchr == '\x03'
|| inchr == '\x1a'
|| inchr == '\x00')
goto exit_check;
prompt[cursor++] = inchr;
if (cursor >= LINE_SIZE)
break;
}
printf("\n");
prompt[cursor] = ';';
YY_BUFFER_STATE buffer = yy_scan_string(prompt);
while (yyparse())
;
yy_delete_buffer(buffer);
exit_check:
if (inchr == '\x04'
|| inchr == '\x03'
|| inchr == '\x1a'
|| inchr == '\x00'
|| do_exit == true)
break;
}
disable_raw_mode();
}