~emersion/mrsh

ref: 41ead0f046b94515afa805ed67da4e82d38b028f mrsh/shell/word.c -rw-r--r-- 8.6 KiB
41ead0f0Simon Ser build: disable -Wformat-overflow 3 months 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
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
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <ctype.h>
#include <glob.h>
#include <mrsh/buffer.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "shell/shell.h"
#include "shell/word.h"

static bool is_logname_char(char c) {
	// See https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_282
	return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
		(c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-';
}

static ssize_t expand_tilde_at(struct mrsh_state *state, const char *str,
		bool last, char **expanded_ptr) {
	if (str[0] != '~') {
		return -1;
	}

	const char *cur;
	for (cur = str + 1; cur[0] != '\0' && cur[0] != '/'; cur++) {
		if (!is_logname_char(cur[0])) {
			return -1;
		}
	}
	if (cur[0] == '\0' && !last) {
		return -1;
	}
	const char *slash = cur;

	char *name = NULL;
	if (slash > str + 1) {
		name = strndup(str + 1, slash - str - 1);
	}

	const char *dir = NULL;
	if (name == NULL) {
		dir = mrsh_env_get(state, "HOME", NULL);
	} else {
		struct passwd *pw = getpwnam(name);
		if (pw != NULL) {
			dir = pw->pw_dir;
		}
	}
	free(name);

	if (dir == NULL) {
		return -1;
	}

	*expanded_ptr = strdup(dir);
	return slash - str;
}

static void _expand_tilde(struct mrsh_state *state, struct mrsh_word **word_ptr,
		bool assignment, bool first, bool last) {
	struct mrsh_word *word = *word_ptr;
	switch (word->type) {
	case MRSH_WORD_STRING:;
		struct mrsh_word_string *ws = mrsh_word_get_string(word);
		if (ws->single_quoted) {
			break;
		}

		struct mrsh_array words = {0};

		const char *str = ws->str;
		if (first) {
			char *expanded;
			ssize_t offset = expand_tilde_at(state, str, last, &expanded);
			if (offset >= 0) {
				mrsh_array_add(&words,
					mrsh_word_string_create(expanded, true));
				str += offset;
			}
		}

		if (assignment) {
			while (true) {
				const char *colon = strchr(str, ':');
				if (colon == NULL) {
					break;
				}

				char *slice = strndup(str, colon - str + 1);
				mrsh_array_add(&words,
					mrsh_word_string_create(slice, false));

				str = colon + 1;

				char *expanded;
				ssize_t offset = expand_tilde_at(state, str, last, &expanded);
				if (offset >= 0) {
					mrsh_array_add(&words,
						mrsh_word_string_create(expanded, true));
					str += offset;
				}
			}
		}

		if (words.len > 0) {
			char *trailing = strdup(str);
			mrsh_array_add(&words,
				mrsh_word_string_create(trailing, false));

			struct mrsh_word_list *wl = mrsh_word_list_create(&words, false);
			*word_ptr = &wl->word;
			mrsh_word_destroy(word);
		}
		break;
	case MRSH_WORD_LIST:;
		struct mrsh_word_list *wl = mrsh_word_get_list(word);
		if (wl->double_quoted) {
			break;
		}
		for (size_t i = 0; i < wl->children.len; ++i) {
			struct mrsh_word **child_ptr =
				(struct mrsh_word **)&wl->children.data[i];
			_expand_tilde(state, child_ptr, assignment, first && i == 0,
				last && i == wl->children.len - 1);
		}
		break;
	default:
		break;
	}
}

void expand_tilde(struct mrsh_state *state, struct mrsh_word **word_ptr,
		bool assignment) {
	_expand_tilde(state, word_ptr, assignment, true, true);
}

struct split_fields_data {
	struct mrsh_array *fields;
	struct mrsh_word_list *cur_field;
	const char *ifs, *ifs_non_space;
	bool in_ifs, in_ifs_non_space;
};

static void add_to_cur_field(struct split_fields_data *data,
		struct mrsh_word *word) {
	if (data->cur_field == NULL) {
		data->cur_field = mrsh_word_list_create(NULL, false);
		mrsh_array_add(data->fields, data->cur_field);
	}
	mrsh_array_add(&data->cur_field->children, word);
}

static void _split_fields(struct split_fields_data *data,
		const struct mrsh_word *word) {
	switch (word->type) {
	case MRSH_WORD_STRING:;
		const struct mrsh_word_string *ws = mrsh_word_get_string(word);

		if (ws->single_quoted || !ws->split_fields) {
			add_to_cur_field(data, mrsh_word_copy(word));
			data->in_ifs = data->in_ifs_non_space = false;
			return;
		}

		struct mrsh_buffer buf = {0};
		size_t len = strlen(ws->str);
		for (size_t i = 0; i < len; ++i) {
			char c = ws->str[i];
			if (strchr(data->ifs, c) == NULL) {
				mrsh_buffer_append_char(&buf, c);
				data->in_ifs = data->in_ifs_non_space = false;
				continue;
			}

			bool is_ifs_non_space = strchr(data->ifs_non_space, c) != NULL;
			if (!data->in_ifs || (is_ifs_non_space && data->in_ifs_non_space)) {
				mrsh_buffer_append_char(&buf, '\0');
				char *str = mrsh_buffer_steal(&buf);
				add_to_cur_field(data,
					&mrsh_word_string_create(str, false)->word);
				data->cur_field = NULL;
				data->in_ifs = true;
			} else if (is_ifs_non_space) {
				data->in_ifs_non_space = true;
			}
		}

		if (!data->in_ifs) {
			mrsh_buffer_append_char(&buf, '\0');
			char *str = mrsh_buffer_steal(&buf);
			add_to_cur_field(data,
				&mrsh_word_string_create(str, false)->word);
		}

		mrsh_buffer_finish(&buf);
		break;
	case MRSH_WORD_LIST:;
		const struct mrsh_word_list *wl = mrsh_word_get_list(word);

		if (wl->double_quoted) {
			add_to_cur_field(data, mrsh_word_copy(word));
			return;
		}

		for (size_t i = 0; i < wl->children.len; ++i) {
			const struct mrsh_word *child = wl->children.data[i];
			_split_fields(data, child);
		}
		break;
	default:
		assert(false);
	}
}

void split_fields(struct mrsh_array *fields, const struct mrsh_word *word,
		const char *ifs) {
	if (ifs == NULL) {
		ifs = " \t\n";
	} else if (ifs[0] == '\0') {
		mrsh_array_add(fields, mrsh_word_copy(word));
		return;
	}

	size_t ifs_len = strlen(ifs);
	char *ifs_non_space = calloc(ifs_len, sizeof(char));
	size_t ifs_non_space_len = 0;
	for (size_t i = 0; i < ifs_len; ++i) {
		if (!isspace(ifs[i])) {
			ifs_non_space[ifs_non_space_len++] = ifs[i];
		}
	}

	struct split_fields_data data = {
		.fields = fields,
		.ifs = ifs,
		.ifs_non_space = ifs_non_space,
		.in_ifs = true,
	};
	_split_fields(&data, word);

	free(ifs_non_space);
}

void get_fields_str(struct mrsh_array *strs, const struct mrsh_array *fields) {
	for (size_t i = 0; i < fields->len; i++) {
		struct mrsh_word *word = fields->data[i];
		mrsh_array_add(strs, mrsh_word_str(word));
	}
}

static bool is_pathname_metachar(char c) {
	switch (c) {
	case '*':
	case '?':
	case '[':
	case ']':
		return true;
	default:
		return false;
	}
}

static bool needs_pathname_expansion(const struct mrsh_word *word) {
	switch (word->type) {
	case MRSH_WORD_STRING:;
		const struct mrsh_word_string *ws = mrsh_word_get_string(word);
		if (ws->single_quoted) {
			return false;
		}

		size_t len = strlen(ws->str);
		for (size_t i = 0; i < len; i++) {
			if (is_pathname_metachar(ws->str[i])) {
				return true;
			}
		}
		return false;
	case MRSH_WORD_LIST:;
		const struct mrsh_word_list *wl = mrsh_word_get_list(word);
		if (wl->double_quoted) {
			return false;
		}

		for (size_t i = 0; i < wl->children.len; i++) {
			const struct mrsh_word *child = wl->children.data[i];
			if (needs_pathname_expansion(child)) {
				return true;
			}
		}
		return false;
	default:
		assert(false);
	}

}

static void _word_to_pattern(struct mrsh_buffer *buf,
		const struct mrsh_word *word, bool quoted) {
	switch (word->type) {
	case MRSH_WORD_STRING:;
		const struct mrsh_word_string *ws = mrsh_word_get_string(word);

		size_t len = strlen(ws->str);
		for (size_t i = 0; i < len; i++) {
			char c = ws->str[i];
			if (is_pathname_metachar(c) && (quoted || ws->single_quoted)) {
				mrsh_buffer_append_char(buf, '\\');
			}
			mrsh_buffer_append_char(buf, c);
		}
		break;
	case MRSH_WORD_LIST:;
		const struct mrsh_word_list *wl = mrsh_word_get_list(word);

		for (size_t i = 0; i < wl->children.len; i++) {
			const struct mrsh_word *child = wl->children.data[i];
			_word_to_pattern(buf, child, quoted || wl->double_quoted);
		}
		break;
	default:
		assert(false);
	}
}

char *word_to_pattern(const struct mrsh_word *word) {
	if (!needs_pathname_expansion(word)) {
		return NULL;
	}

	struct mrsh_buffer buf = {0};
	_word_to_pattern(&buf, word, false);
	mrsh_buffer_append_char(&buf, '\0');
	return mrsh_buffer_steal(&buf);
}

bool expand_pathnames(struct mrsh_array *expanded,
		const struct mrsh_array *fields) {
	for (size_t i = 0; i < fields->len; ++i) {
		const struct mrsh_word *field = fields->data[i];

		char *pattern = word_to_pattern(field);
		if (pattern == NULL) {
			mrsh_array_add(expanded, mrsh_word_str(field));
			continue;
		}

		glob_t glob_buf;
		int ret = glob(pattern, GLOB_NOSORT, NULL, &glob_buf);
		if (ret == 0) {
			for (size_t i = 0; i < glob_buf.gl_pathc; ++i) {
				mrsh_array_add(expanded, strdup(glob_buf.gl_pathv[i]));
			}
			globfree(&glob_buf);
		} else if (ret == GLOB_NOMATCH) {
			mrsh_array_add(expanded, mrsh_word_str(field));
		} else {
			fprintf(stderr, "glob failed: %d\n", ret);
			free(pattern);
			return false;
		}

		free(pattern);
	}

	return true;
}