~emersion/mrsh

ref: fb1a6e2ce995f61ca8e41e8ac3f591bc50922592 mrsh/shell/task_word.c -rw-r--r-- 5.9 KiB
fb1a6e2cemersion readme: add link to overall project status 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
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
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <errno.h>
#include <mrsh/parser.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>

#include "buffer.h"
#include "builtin.h"
#include "shell.h"

#define TOKEN_COMMAND_READ_SIZE 128

struct task_word {
	struct task task;
	struct mrsh_word **word_ptr;
	enum tilde_expansion tilde_expansion;

	// only if it's a command
	bool started;
	struct process process;
	int fd;
};

static bool read_full(int fd, char *buf, size_t size) {
	size_t n_read = 0;
	do {
		ssize_t n = read(fd, buf, size - n_read);
		if (n < 0 && errno == EINTR) {
			continue;
		} else if (n < 0) {
			fprintf(stderr, "failed to read(): %s\n", strerror(errno));
			return false;
		}
		n_read += n;
	} while (n_read < size);
	return true;
}

static void task_word_swap(struct task_word *tt,
		struct mrsh_word *new_word) {
	mrsh_word_destroy(*tt->word_ptr);
	*tt->word_ptr = new_word;
}

static bool task_word_command_start(struct task_word *tt,
		struct context *ctx) {
	struct mrsh_word *word = *tt->word_ptr;
	struct mrsh_word_command *wc = mrsh_word_get_command(word);
	assert(wc != NULL);

	int fd = create_anonymous_file();
	if (fd < 0) {
		fprintf(stderr, "failed to create anonymous file: %s\n",
			strerror(errno));
		return false;
	}

	pid_t pid = fork();
	if (pid < 0) {
		fprintf(stderr, "failed to fork(): %s\n", strerror(errno));
		return false;
	} else if (pid == 0) {
		dup2(fd, STDOUT_FILENO);

		if (ctx->stdin_fileno >= 0) {
			close(ctx->stdin_fileno);
		}
		if (ctx->stdout_fileno >= 0) {
			close(ctx->stdout_fileno);
		}

		struct mrsh_parser *parser =
			mrsh_parser_create_from_buffer(wc->command, strlen(wc->command));
		if (parser == NULL) {
			fprintf(stderr, "failed to create parser");
			exit(EXIT_FAILURE);
		}
		struct mrsh_program *prog = mrsh_parse_program(parser);
		mrsh_parser_destroy(parser);
		if (prog == NULL) {
			exit(EXIT_SUCCESS);
		}

		struct mrsh_state state = {0};
		mrsh_state_init(&state);
		mrsh_run_program(&state, prog);
		mrsh_state_finish(&state);

		exit(state.exit >= 0 ? state.exit : EXIT_SUCCESS);
	} else {
		process_init(&tt->process, pid);
		tt->fd = fd;
		return true;
	}
}

static const char *parameter_get_value(struct mrsh_state *state, char *name) {
	static char value[16];
	char *end;
	long lvalue = strtol(name, &end, 10);
	// Special cases
	if (strcmp(name, "@") == 0) {
		// TODO
	} else if (strcmp(name, "*") == 0) {
		// TODO
	} else if (strcmp(name, "#") == 0) {
		sprintf(value, "%d", state->argc);
		return value;
	} else if (strcmp(name, "?") == 0) {
		sprintf(value, "%d", state->last_status);
		return value;
	} else if (strcmp(name, "-") == 0) {
		return print_options(state);
	} else if (strcmp(name, "$") == 0) {
		sprintf(value, "%d", (int)getpid());
		return value;
	} else if (strcmp(name, "!") == 0) {
		// TODO
	} else if (!end[0]) {
		if (lvalue >= state->argc) {
			return NULL;
		}
		return state->argv[lvalue];
	}
	// User-set cases
	return (const char *)mrsh_hashtable_get(&state->variables, name);
}

static int task_word_poll(struct task *task, struct context *ctx) {
	struct task_word *tt = (struct task_word *)task;
	struct mrsh_word *word = *tt->word_ptr;

	switch (word->type) {
	case MRSH_WORD_STRING:;
		struct mrsh_word_string *ws = mrsh_word_get_string(word);
		assert(ws != NULL);
		if (!ws->single_quoted && tt->tilde_expansion != TILDE_EXPANSION_NONE) {
			// TODO: TILDE_EXPANSION_ASSIGNMENT
			expand_tilde(ctx->state, &ws->str);
		}
		return 0;
	case MRSH_WORD_PARAMETER:;
		struct mrsh_word_parameter *wp = mrsh_word_get_parameter(word);
		assert(wp != NULL);
		const char *value = parameter_get_value(ctx->state, wp->name);
		if (value == NULL) {
			if ((ctx->state->options & MRSH_OPT_NOUNSET)) {
				fprintf(stderr, "%s: %s: unbound variable\n",
						ctx->state->argv[0], wp->name);
				return TASK_STATUS_ERROR;
			}
			value = "";
		}
		struct mrsh_word_string *replacement =
			mrsh_word_string_create(strdup(value), false);
		task_word_swap(tt, &replacement->word);
		return 0;
	case MRSH_WORD_COMMAND:;
		struct mrsh_word_command *wc = mrsh_word_get_command(word);
		assert(wc != NULL);

		if (!tt->started) {
			if (!task_word_command_start(tt, ctx)) {
				return TASK_STATUS_ERROR;
			}
			tt->started = true;
		}

		int ret = process_poll(&tt->process);
		if (ret != TASK_STATUS_WAIT) {
			off_t size = lseek(tt->fd, 0, SEEK_END);
			if (size < 0) {
				fprintf(stderr, "failed to lseek(): %s\n", strerror(errno));
				return TASK_STATUS_ERROR;
			}
			lseek(tt->fd, 0, SEEK_SET);

			char *buf = malloc(size + 1);
			if (buf == NULL) {
				fprintf(stderr, "failed to malloc(): %s\n", strerror(errno));
				return TASK_STATUS_ERROR;
			}

			if (!read_full(tt->fd, buf, size)) {
				return TASK_STATUS_ERROR;
			}
			buf[size] = '\0';

			close(tt->fd);
			tt->fd = -1;

			// Trim newlines at the end
			ssize_t i = size - 1;
			while (i >= 0 && buf[i] == '\n') {
				buf[i] = '\0';
				--i;
			}

			struct mrsh_word_string *ws = mrsh_word_string_create(buf, false);
			task_word_swap(tt, &ws->word);
		}
		return ret;
	case MRSH_WORD_LIST:
		assert(0);
	}
	assert(0);
}

static const struct task_interface task_word_impl = {
	.poll = task_word_poll,
};

struct task *task_word_create(struct mrsh_word **word_ptr,
		enum tilde_expansion tilde_expansion) {
	struct mrsh_word *word = *word_ptr;

	if (word->type == MRSH_WORD_LIST) {
		struct mrsh_word_list *wl = mrsh_word_get_list(word);
		assert(wl != NULL);
		struct task *task_list = task_list_create();
		for (size_t i = 0; i < wl->children.len; ++i) {
			struct mrsh_word **child_ptr =
				(struct mrsh_word **)&wl->children.data[i];
			if (i > 0 || wl->double_quoted) {
				tilde_expansion = TILDE_EXPANSION_NONE;
			}
			task_list_add(task_list,
				task_word_create(child_ptr, tilde_expansion));
		}
		return task_list;
	}

	struct task_word *tt = calloc(1, sizeof(struct task_word));
	task_init(&tt->task, &task_word_impl);
	tt->word_ptr = word_ptr;
	tt->tilde_expansion = tilde_expansion;
	return &tt->task;
}