~emersion/mrsh

ref: 2195cd2616b71838ecd10878bb04f70a52cb238c mrsh/shell.c -rw-r--r-- 5.2 KiB
2195cd26emersion Update README with build instructions 3 years ago
                                                                                
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
#define _POSIX_C_SOURCE 200112L
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <mrsh/builtin.h>
#include <mrsh/shell.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

void mrsh_state_init(struct mrsh_state *state) {
	state->exit = -1;
}

static int run_simple_command(struct mrsh_state *state,
		struct mrsh_simple_command *sc) {
	int argc = 1 + sc->arguments.len;
	char *argv[argc + 1];
	argv[0] = sc->name;
	memcpy(argv + 1, sc->arguments.data, sc->arguments.len * sizeof(void *));
	argv[argc] = NULL;

	// TODO: redirections for builtins
	int ret = mrsh_builtin(state, argc, argv);
	if (ret != -1) {
		return ret;
	}

	pid_t pid = fork();
	if (pid < 0) {
		return -1;
	} else if (pid == 0) {
		for (size_t i = 0; i < sc->assignments.len; ++i) {
			struct mrsh_assignment *assign = sc->assignments.data[i];
			setenv(assign->name, assign->value, true);
		}

		for (size_t i = 0; i < sc->io_redirects.len; ++i) {
			struct mrsh_io_redirect *redir = sc->io_redirects.data[i];

			// TODO: filename expansions
			int fd, default_redir_fd;
			errno = 0;
			if (strcmp(redir->op, "<") == 0) {
				fd = open(redir->filename, O_RDONLY);
				default_redir_fd = STDIN_FILENO;
			} else if (strcmp(redir->op, ">") == 0 ||
					strcmp(redir->op, ">|") == 0) {
				fd = open(redir->filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
				default_redir_fd = STDOUT_FILENO;
			} else if (strcmp(redir->op, ">>") == 0) {
				fd = open(redir->filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
				default_redir_fd = STDOUT_FILENO;
			} else if (strcmp(redir->op, "<>") == 0) {
				fd = open(redir->filename, O_RDWR | O_CREAT, 0644);
				default_redir_fd = STDIN_FILENO;
			} else {
				assert(false); // TODO
			}
			if (fd < 0) {
				fprintf(stderr, "cannot open %s: %s\n", redir->filename,
					strerror(errno));
				exit(EXIT_FAILURE);
			}

			int redir_fd = redir->io_number;
			if (redir_fd < 0) {
				redir_fd = default_redir_fd;
			}

			errno = 0;
			int ret = dup2(fd, redir_fd);
			if (ret < 0) {
				fprintf(stderr, "cannot duplicate file descriptor: %s\n",
					strerror(errno));
				exit(EXIT_FAILURE);
			}
		}

		errno = 0;
		execvp(sc->name, argv);

		// Something went wrong
		fprintf(stderr, "%s: %s\n", sc->name, strerror(errno));
		exit(127);
	} else {
		int status;
		if (waitpid(pid, &status, 0) != pid) {
			return -1;
		}
		return WEXITSTATUS(status);
	}
}

static int run_node(struct mrsh_state *state, struct mrsh_node *node);

static int run_command_list_array(struct mrsh_state *state,
		struct mrsh_array *array) {
	int exit_status = 0;
	for (size_t i = 0; i < array->len; ++i) {
		struct mrsh_command_list *list = array->data[i];
		// TODO: handle list->ampersand
		exit_status = run_node(state, list->node);
	}
	return exit_status;
}

static int run_command(struct mrsh_state *state, struct mrsh_command *cmd);

static int run_if_clause(struct mrsh_state *state, struct mrsh_if_clause *ic) {
	int condition_status = run_command_list_array(state, &ic->condition);
	if (condition_status < 0) {
		return condition_status;
	}

	if (condition_status == 0) {
		return run_command_list_array(state, &ic->body);
	} else {
		if (ic->else_part == NULL) {
			return 0;
		}
		return run_command(state, ic->else_part);
	}
}

static int run_command(struct mrsh_state *state, struct mrsh_command *cmd) {
	switch (cmd->type) {
	case MRSH_SIMPLE_COMMAND:;
		struct mrsh_simple_command *sc = mrsh_command_get_simple_command(cmd);
		assert(sc != NULL);
		return run_simple_command(state, sc);
	case MRSH_BRACE_GROUP:;
		struct mrsh_brace_group *bg = mrsh_command_get_brace_group(cmd);
		assert(bg != NULL);
		return run_command_list_array(state, &bg->body);
	case MRSH_IF_CLAUSE:;
		struct mrsh_if_clause *ic = mrsh_command_get_if_clause(cmd);
		assert(ic != NULL);
		return run_if_clause(state, ic);
	}
	assert(false);
}

static int run_pipeline(struct mrsh_state *state, struct mrsh_pipeline *pl) {
	int exit_status = 0;
	for (size_t i = 0; i < pl->commands.len; ++i) {
		struct mrsh_command *cmd = pl->commands.data[i];
		exit_status = run_command(state, cmd);
	}

	if (pl->bang) {
		exit_status = !exit_status;
	}
	return exit_status;
}

static int run_binop(struct mrsh_state *state, struct mrsh_binop *binop) {
	int left_status = run_node(state, binop->left);
	if (left_status < 0) {
		return left_status;
	}

	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_node(state, binop->right);
}

static int run_node(struct mrsh_state *state, struct mrsh_node *node) {
	switch (node->type) {
	case MRSH_NODE_PIPELINE:;
		struct mrsh_pipeline *pl = mrsh_node_get_pipeline(node);
		assert(pl != NULL);
		return run_pipeline(state, pl);
		break;
	case MRSH_NODE_BINOP:;
		struct mrsh_binop *binop = mrsh_node_get_binop(node);
		assert(binop != NULL);
		return run_binop(state, binop);
		break;
	}
	assert(false);
}

int mrsh_run_command_list(struct mrsh_state *state,
		struct mrsh_command_list *list) {
	return run_node(state, list->node);
}

int mrsh_run_program(struct mrsh_state *state, struct mrsh_program *prog) {
	return run_command_list_array(state, &prog->body);
}