ref: 2a9e336e1c577494a32d35add8fda052a371dce4 mrsh/shell/task/task.c -rw-r--r-- 9.8 KiB View raw
                                                                                
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <mrsh/ast.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "shell/shell.h"
#include "shell/task.h"

static int run_subshell(struct context *ctx, struct mrsh_array *array) {
	pid_t pid = fork();
	if (pid < 0) {
		perror("fork");
		return TASK_STATUS_ERROR;
	} else if (pid == 0) {
		ctx->state->child = true;

		if (!(ctx->state->options & MRSH_OPT_MONITOR)) {
			// If job control is disabled, stdin is /dev/null
			int fd = open("/dev/null", O_CLOEXEC | O_RDONLY);
			if (fd < 0) {
				fprintf(stderr, "failed to open /dev/null: %s\n",
					strerror(errno));
				exit(1);
			}
			dup2(fd, STDIN_FILENO);
			close(fd);
		}

		int ret = run_command_list_array(ctx, array);
		if (ret < 0) {
			exit(127);
		}

		if (ctx->state->exit >= 0) {
			exit(ctx->state->exit);
		}
		exit(ret);
	}

	struct process *proc = process_create(ctx->state, pid);
	return job_wait_process(proc);
}

static int run_if_clause(struct context *ctx, struct mrsh_if_clause *ic) {
	int ret = run_command_list_array(ctx, &ic->condition);
	if (ret < 0) {
		return ret;
	}

	if (ret == 0) {
		return run_command_list_array(ctx, &ic->body);
	} else {
		if (ic->else_part) {
			return run_command(ctx, ic->else_part);
		}
		return 0;
	}
}

static int run_loop_clause(struct context *ctx, struct mrsh_loop_clause *lc) {
	int loop_num = ++ctx->state->frame->nloops;

	int loop_ret = 0;
	while (ctx->state->exit == -1) {
		int ret = run_command_list_array(ctx, &lc->condition);
		if (ret == TASK_STATUS_INTERRUPTED) {
			goto interrupt;
		} else if (ret < 0) {
			return ret;
		}

		bool break_loop;
		switch (lc->type) {
		case MRSH_LOOP_WHILE:
			break_loop = ret > 0;
			break;
		case MRSH_LOOP_UNTIL:
			break_loop = ret == 0;
			break;
		}
		if (break_loop) {
			break;
		}

		loop_ret = run_command_list_array(ctx, &lc->body);
		if (loop_ret == TASK_STATUS_INTERRUPTED) {
			goto interrupt;
		} else if (loop_ret < 0) {
			return loop_ret;
		}

		continue;

interrupt:
		if (ctx->state->frame->nloops < loop_num) {
			loop_ret = TASK_STATUS_INTERRUPTED; // break to parent loop
			break;
		}
		switch (ctx->state->frame->branch_control) {
		case MRSH_BRANCH_BREAK:
		case MRSH_BRANCH_RETURN:
		case MRSH_BRANCH_EXIT:
			break_loop = true;
			loop_ret = 0;
			break;
		case MRSH_BRANCH_CONTINUE:
			break;
		}
		if (break_loop) {
			break;
		}
	}

	--ctx->state->frame->nloops;
	return loop_ret;
}

static int run_for_clause(struct context *ctx, struct mrsh_for_clause *fc) {
	int loop_num = ++ctx->state->frame->nloops;

	struct mrsh_array word_fields = {0};
	for (size_t i = 0; i < fc->word_list.len; i++) {
		// TODO: this mutates the AST
		struct mrsh_word **word_ptr =
			(struct mrsh_word **)&fc->word_list.data[i];
		int ret = run_word(ctx, word_ptr, TILDE_EXPANSION_NAME);
		if (ret < 0) {
			return ret;
		}
		struct mrsh_word_string *ws = mrsh_word_get_string(*word_ptr);

		mrsh_array_add(&word_fields, strdup(ws->str));
	}

	struct mrsh_array expanded_fields = {0};
	if (!expand_pathnames(&expanded_fields, &word_fields)) {
		return TASK_STATUS_ERROR;
	}
	for (size_t i = 0; i < word_fields.len; i++) {
		free(word_fields.data[i]);
	}
	mrsh_array_finish(&word_fields);

	int loop_ret = 0;
	size_t word_index = 0;
	while (ctx->state->exit == -1) {
		if (word_index == expanded_fields.len) {
			break;
		}

		mrsh_env_set(ctx->state, fc->name, expanded_fields.data[word_index],
			MRSH_VAR_ATTRIB_NONE);
		word_index++;

		loop_ret = run_command_list_array(ctx, &fc->body);
		if (loop_ret == TASK_STATUS_INTERRUPTED) {
			goto interrupt;
		} else if (loop_ret < 0) {
			return loop_ret;
		}

		continue;

interrupt:
		if (ctx->state->frame->nloops < loop_num) {
			loop_ret = TASK_STATUS_INTERRUPTED; // break to parent loop
			break;
		}
		bool break_loop = false;
		switch (ctx->state->frame->branch_control) {
		case MRSH_BRANCH_BREAK:
		case MRSH_BRANCH_RETURN:
		case MRSH_BRANCH_EXIT:
			break_loop = true;
			loop_ret = 0;
			break;
		case MRSH_BRANCH_CONTINUE:
			break;
		}
		if (break_loop) {
			break;
		}
	}

	for (size_t i = 0; i < expanded_fields.len; i++) {
		free(expanded_fields.data[i]);
	}
	mrsh_array_finish(&expanded_fields);

	--ctx->state->frame->nloops;
	return loop_ret;
}

static int run_case_clause(struct context *ctx, struct mrsh_case_clause *cc) {
	struct mrsh_word *word = mrsh_word_copy(cc->word);
	int ret = run_word(ctx, &word, TILDE_EXPANSION_NAME);
	if (ret < 0) {
		mrsh_word_destroy(word);
		return ret;
	}
	char *word_str = mrsh_word_str(word);
	mrsh_word_destroy(word);

	int case_ret = 0;
	for (size_t i = 0; i < cc->items.len; ++i) {
		struct mrsh_case_item *ci = cc->items.data[i];

		bool selected = false;
		for (size_t j = 0; j < ci->patterns.len; ++j) {
			// TODO: this mutates the AST
			struct mrsh_word **word_ptr =
				(struct mrsh_word **)&ci->patterns.data[j];
			int ret = run_word(ctx, word_ptr, TILDE_EXPANSION_NAME);
			if (ret < 0) {
				return ret;
			}
			char *pattern_str = mrsh_word_str(*word_ptr);
			selected = fnmatch(pattern_str, word_str, 0) == 0;
			free(pattern_str);
			if (selected) {
				break;
			}
		}

		if (selected) {
			case_ret = run_command_list_array(ctx, &ci->body);
			break;
		}
	}

	free(word_str);
	return case_ret;
}

static int run_function_definition(struct context *ctx,
		struct mrsh_function_definition *fnd) {
	struct mrsh_function *fn = calloc(1, sizeof(struct mrsh_function));
	fn->body = mrsh_command_copy(fnd->body);
	struct mrsh_function *old_fn =
		mrsh_hashtable_set(&ctx->state->functions, fnd->name, fn);
	mrsh_function_destroy(old_fn);
	return 0;
}

int run_command(struct context *ctx, struct mrsh_command *cmd) {
	switch (cmd->type) {
	case MRSH_SIMPLE_COMMAND:;
		struct mrsh_simple_command *sc = mrsh_command_get_simple_command(cmd);
		return run_simple_command(ctx, sc);
	case MRSH_BRACE_GROUP:;
		struct mrsh_brace_group *bg = mrsh_command_get_brace_group(cmd);
		return run_command_list_array(ctx, &bg->body);
	case MRSH_SUBSHELL:;
		struct mrsh_subshell *s = mrsh_command_get_subshell(cmd);
		return run_subshell(ctx, &s->body);
	case MRSH_IF_CLAUSE:;
		struct mrsh_if_clause *ic = mrsh_command_get_if_clause(cmd);
		return run_if_clause(ctx, ic);
	case MRSH_LOOP_CLAUSE:;
		struct mrsh_loop_clause *lc = mrsh_command_get_loop_clause(cmd);
		return run_loop_clause(ctx, lc);
	case MRSH_FOR_CLAUSE:;
		struct mrsh_for_clause *fc = mrsh_command_get_for_clause(cmd);
		return run_for_clause(ctx, fc);
	case MRSH_CASE_CLAUSE:;
		struct mrsh_case_clause *cc =
			mrsh_command_get_case_clause(cmd);
		return run_case_clause(ctx, cc);
	case MRSH_FUNCTION_DEFINITION:;
		struct mrsh_function_definition *fnd =
			mrsh_command_get_function_definition(cmd);
		return run_function_definition(ctx, fnd);
	}
	assert(false);
}

int run_and_or_list(struct context *ctx, struct mrsh_and_or_list *and_or_list) {
	switch (and_or_list->type) {
	case MRSH_AND_OR_LIST_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_and_or_list_get_pipeline(and_or_list);
		return run_pipeline(ctx, pl);
	case MRSH_AND_OR_LIST_BINOP:;
		struct mrsh_binop *binop = mrsh_and_or_list_get_binop(and_or_list);
		int left_status = run_and_or_list(ctx, binop->left);
		switch (binop->type) {
		case MRSH_BINOP_AND:
			if (left_status != 0) {
				return left_status;
			}
			break;
		case MRSH_BINOP_OR:
			if (left_status == 0) {
				return 0;
			}
			break;
		}
		return run_and_or_list(ctx, binop->right);
	}
	assert(false);
}

/**
 * Put the process into its job's process group. This has to be done both in the
 * parent and the child because of potential race conditions.
 */
static struct process *init_async_child(struct context *ctx, pid_t pid) {
	struct process *proc = process_create(ctx->state, pid);

	if (ctx->state->options & MRSH_OPT_MONITOR) {
		job_add_process(ctx->job, proc);
	}

	return proc;
}

int run_command_list_array(struct context *ctx, struct mrsh_array *array) {
	struct mrsh_state *state = ctx->state;

	int ret = 0;
	for (size_t i = 0; i < array->len; ++i) {
		struct mrsh_command_list *list = array->data[i];
		if (list->ampersand) {
			struct context child_ctx = *ctx;
			child_ctx.background = true;
			if (child_ctx.job == NULL) {
				child_ctx.job = job_create(state, &list->node);
			}

			pid_t pid = fork();
			if (pid < 0) {
				perror("fork");
				return TASK_STATUS_ERROR;
			} else if (pid == 0) {
				ctx = NULL; // Use child_ctx instead
				state->child = true;

				init_async_child(&child_ctx, getpid());
				if (state->options & MRSH_OPT_MONITOR) {
					init_job_child_process(state);
				}

				if (!(state->options & MRSH_OPT_MONITOR)) {
					// If job control is disabled, stdin is /dev/null
					int fd = open("/dev/null", O_CLOEXEC | O_RDONLY);
					if (fd < 0) {
						fprintf(stderr, "failed to open /dev/null: %s\n",
							strerror(errno));
						exit(1);
					}
					dup2(fd, STDIN_FILENO);
					close(fd);
				}

				int ret = run_and_or_list(&child_ctx, list->and_or_list);
				if (ret < 0) {
					exit(127);
				}
				exit(ret);
			}

			ret = 0;
			init_async_child(&child_ctx, pid);
		} else {
			ret = run_and_or_list(ctx, list->and_or_list);
			if (ret < 0) {
				return ret;
			}
		}

		if (ret >= 0) {
			state->last_status = ret;
		}
	}
	return ret;
}

static void destroy_terminated_jobs(struct mrsh_state *state) {
	for (ssize_t i = 0; i < (ssize_t)state->jobs.len; ++i) {
		struct mrsh_job *job = state->jobs.data[i];
		if (job_poll(job) >= 0) {
			job_destroy(job);
			--i;
		}
	}
}

int mrsh_run_program(struct mrsh_state *state, struct mrsh_program *prog) {
	struct context ctx = { .state = state };
	int ret = run_command_list_array(&ctx, &prog->body);
	destroy_terminated_jobs(state);
	return ret;
}

int mrsh_run_word(struct mrsh_state *state, struct mrsh_word **word) {
	struct context ctx = { .state = state };
	int last_status = state->last_status;
	int ret = run_word(&ctx, word, TILDE_EXPANSION_NAME);
	state->last_status = last_status;
	return ret;
}