~emersion/mrsh

ref: f5a0db4e910db6c961413e8a60131dd18372affe mrsh/builtin/set.c -rw-r--r-- 5.6 KiB
f5a0db4eDrew DeVault Avoid null dereference in argv_free 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
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <mrsh/builtin.h>
#include <mrsh/shell.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "builtin.h"

static const char set_usage[] =
	"usage: set [(-|+)abCefhmnuvx] [-o option] [args...]\n";

struct option_map {
	const char *name;
	char short_name;
	enum mrsh_option value;
};

static const struct option_map options[] = {
	{ "allexport", 'a', MRSH_OPT_ALLEXPORT },
	{ "notify", 'b', MRSH_OPT_NOTIFY },
	{ "noclobber", 'C', MRSH_OPT_NOCLOBBER },
	{ "errexit", 'e', MRSH_OPT_ERREXIT },
	{ "noglob", 'f', MRSH_OPT_NOGLOB },
	{ NULL, 'h', MRSH_OPT_PRELOOKUP },
	{ "monitor", 'm', MRSH_OPT_MONITOR },
	{ "noexec", 'n', MRSH_OPT_NOEXEC },
	{ "ignoreeof", 0, MRSH_OPT_IGNOREEOF },
	{ "nolog", 0, MRSH_OPT_NOLOG },
	{ "vi", 0, MRSH_OPT_VI },
	{ "nounset", 'u', MRSH_OPT_NOUNSET },
	{ "verbose", 'v', MRSH_OPT_VERBOSE },
	{ "xtrace", 'x', MRSH_OPT_XTRACE },
};

const char *print_options(struct mrsh_state *state) {
	static char opts[sizeof(options) / sizeof(options[0]) + 1];
	int i = 0;
	for (size_t j = 0; j < sizeof(options) / sizeof(options[0]); ++j) {
		if (options[j].short_name && (state->options & options[j].value)) {
			opts[i++] = options[j].short_name;
		}
	}
	opts[i] = 0;
	return opts;
}

static const struct option_map *find_option(char opt) {
	for (size_t i = 0; i < sizeof(options) / sizeof(options[0]); ++i) {
		if (options[i].short_name && options[i].short_name == opt) {
			return &options[i];
		}
	}
	return NULL;
}

static const struct option_map *find_long_option(const char *opt) {
	for (size_t i = 0; i < sizeof(options) / sizeof(options[0]); ++i) {
		if (options[i].name && strcmp(options[i].name, opt) == 0) {
			return &options[i];
		}
	}
	return NULL;
}

static char **argv_dup(char *argv_0, int argc, char *argv[]) {
	char **_argv = malloc((argc + 1) * sizeof(char *));
	_argv[0] = argv_0;
	for (int i = 1; i < argc; ++i) {
		_argv[i] = strdup(argv[i - 1]);
	}
	return _argv;
}

static void argv_free(int argc, char **argv) {
	if (!argv) {
		return;
	}
	for (int i = 0; i < argc; ++i) {
		free(argv[i]);
	}
	free(argv);
}

struct collect_var {
	const char *key, *value;
};

struct collect_iter {
	size_t len;
	size_t count;
	struct collect_var *values;
};

static void collect_vars(const char *key, void *_value, void *user_data) {
	const char *value = _value;
	struct collect_iter *iter = user_data;
	if ((iter->count + 1) * sizeof(struct collect_var) >= iter->len) {
		iter->len *= 2;
		iter->values = realloc(iter->values,
				iter->len * sizeof(struct collect_var));
	}
	iter->values[iter->count].key = key;
	iter->values[iter->count++].value = value;
}

static int varcmp(const void *p1, const void *p2) {
	const struct collect_var *v1 = p1;
	const struct collect_var *v2 = p2;
	return strcmp(v1->key, v2->key);
}

static int set(struct mrsh_state *state, int argc, char *argv[], bool cmdline) {
	if (argc == 1 && !cmdline) {
		struct collect_iter iter = {
			.len = 64,
			.count = 0,
			.values = malloc(64 * sizeof(struct collect_var)),
		};
		mrsh_hashtable_for_each(&state->variables, collect_vars, &iter);
		qsort(iter.values, iter.count, sizeof(struct collect_var), varcmp);
		for (size_t i = 0; i < iter.count; ++i) {
			printf("%s=", iter.values[i].key);
			print_escaped(iter.values[i].value);
			printf("\n");
		}
		free(iter.values);
		return EXIT_SUCCESS;
	}

	bool force_positional = false;
	int i;
	for (i = 1; i < argc; ++i) {
		if (strcmp(argv[i], "--") == 0) {
			force_positional = true;
			++i;
			break;
		}
		if (argv[i][0] != '-' && argv[i][0] != '+') {
			break;
		}
		if (argv[i][1] == '\0') {
			fprintf(stderr, set_usage);
			return EXIT_FAILURE;
		}
		const struct option_map *option;
		switch (argv[i][1]) {
		case 'o':
			if (i + 1 == argc) {
				fprintf(stderr, set_usage);
				return EXIT_FAILURE;
			}
			option = find_long_option(argv[i + 1]);
			if (!option) {
				fprintf(stderr, set_usage);
				return EXIT_FAILURE;
			}
			if (argv[i][0] == '-') {
				state->options |= option->value;
			} else {
				state->options &= ~option->value;
			}
			++i;
			continue;
		case 'c':
			if (!cmdline) {
				fprintf(stderr, set_usage);
				return EXIT_FAILURE;
			}
			state->input = fmemopen(argv[i + 1], strlen(argv[i + 1]), "r");
			++i;
			if (!state->input) {
				fprintf(stderr, "fmemopen failed: %s", strerror(errno));
				return EXIT_FAILURE;
			}
			break;
		case 's':
			state->input = stdin;
			break;
		default:
			for (int j = 1; argv[i][j]; ++j) {
				option = find_option(argv[i][j]);
				if (!option) {
					fprintf(stderr, set_usage);
					return EXIT_FAILURE;
				}
				if (argv[i][0] == '-') {
					state->options |= option->value;
				} else {
					state->options &= ~option->value;
				}
			}
			break;
		}
	}

	if (i != argc || force_positional) {
		char *argv_0;
		if (cmdline) {
			argv_0 = strdup(argv[i++]);
			state->input = fopen(argv_0, "r");
			// TODO: Turn off -m if the user didn't explicitly set it
			state->interactive = false;
			if (!state->input) {
				fprintf(stderr, "could not open %s for reading: %s\n",
					argv_0, strerror(errno));
				return EXIT_FAILURE;
			}
		} else {
			argv_0 = strdup(state->argv[0]);
		}
		argv_free(state->argc, state->argv);
		state->argc = argc - i + 1;
		state->argv = argv_dup(argv_0, state->argc, &argv[i]);
	} else if (cmdline) {
		// No args given, but we need to initialize state->argv
		state->argc = 1;
		state->argv = argv_dup(strdup(argv[0]), 1, argv);
	}

	return EXIT_SUCCESS;
}

int builtin_set(struct mrsh_state *state, int argc, char *argv[]) {
	return set(state, argc, argv, false);
}

int mrsh_process_args(struct mrsh_state *state, int argc, char *argv[]) {
	return set(state, argc, argv, true);
}