summaryrefslogtreecommitdiff
path: root/args.c
blob: 66cc680181bc9e720897e5bc2c9c590b1137a931 (plain)
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
// Copyright (C) 2002 Andrew Tridgell
// Copyright (C) 2009-2016 Joel Rosdahl
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include "ccache.h"

struct args *
args_init(int init_argc, char **init_args)
{
	struct args *args = (struct args *)x_malloc(sizeof(struct args));
	args->argc = 0;
	args->argv = (char **)x_malloc(sizeof(char *));
	args->argv[0] = NULL;
	for (int i = 0; i < init_argc; i++) {
		args_add(args, init_args[i]);
	}
	return args;
}

struct args *
args_init_from_string(const char *command)
{
	char *p = x_strdup(command);
	char *q = p;
	char *word, *saveptr = NULL;
	struct args *args = args_init(0, NULL);
	while ((word = strtok_r(q, " \t\r\n", &saveptr))) {
		args_add(args, word);
		q = NULL;
	}

	free(p);
	return args;
}

struct args *
args_init_from_gcc_atfile(const char *filename)
{
	char *argtext;
	if (!(argtext = read_text_file(filename, 0))) {
		return NULL;
	}

	struct args *args = args_init(0, NULL);
	char *pos = argtext;
	char *argbuf = x_malloc(strlen(argtext) + 1);
	char *argpos = argbuf;

	// Used to track quoting state; if \0, we are not inside quotes. Otherwise
	// stores the quoting character that started it, for matching the end quote.
	char quoting = '\0';

	while (1) {
		switch (*pos) {
		case '\\':
			pos++;
			if (*pos == '\0') {
				continue;
			}
			break;

		case '\"':
		case '\'':
			if (quoting != '\0') {
				if (quoting == *pos) {
					quoting = '\0';
					pos++;
					continue;
				} else {
					break;
				}
			} else {
				quoting = *pos;
				pos++;
				continue;
			}

		case '\n':
		case '\r':
		case '\t':
		case ' ':
			if (quoting) {
				break;
			}
		// Fall through.

		case '\0':
			// End of token
			*argpos = '\0';
			if (argbuf[0] != '\0') {
				args_add(args, argbuf);
			}
			argpos = argbuf;
			if (*pos == '\0') {
				goto out;
			} else {
				pos++;
				continue;
			}
		}

		*argpos = *pos;
		pos++;
		argpos++;
	}

out:
	free(argbuf);
	free(argtext);
	return args;
}

struct args *
args_copy(struct args *args)
{
	return args_init(args->argc, args->argv);
}

// Insert all arguments in src into dest at position index. If replace is true,
// the element at dest->argv[index] is replaced with the contents of src and
// everything past it is shifted. Otherwise, dest->argv[index] is also shifted.
//
// src is consumed by this operation and should not be freed or used again by
// the caller.
void
args_insert(struct args *dest, int index, struct args *src, bool replace)
{
	// Adjustments made if we are replacing or shifting the element currently at
	// dest->argv[index].
	int offset = replace ? 1 : 0;

	if (replace) {
		free(dest->argv[index]);
	}

	if (src->argc == 0) {
		if (replace) {
			// Have to shift everything down by 1 since we replaced with an empty
			// list.
			for (int i = index; i < dest->argc; i++) {
				dest->argv[i] = dest->argv[i + 1];
			}
			dest->argc--;
		}
		args_free(src);
		return;
	}

	if (src->argc == 1 && replace) {
		// Trivial case; replace with 1 element.
		dest->argv[index] = src->argv[0];
		src->argc = 0;
		args_free(src);
		return;
	}

	dest->argv = (char **)x_realloc(
	  dest->argv,
	  (src->argc + dest->argc + 1 - offset) *
	  sizeof(char *));

	// Shift arguments over.
	for (int i = dest->argc; i >= index + offset; i--) {
		dest->argv[i + src->argc - offset] = dest->argv[i];
	}

	// Copy the new arguments into place.
	for (int i = 0; i < src->argc; i++) {
		dest->argv[i + index] = src->argv[i];
	}

	dest->argc += src->argc - offset;
	src->argc = 0;
	args_free(src);
}

void
args_free(struct args *args)
{
	if (!args) {
		return;
	}
	for (int i = 0; i < args->argc; ++i) {
		if (args->argv[i]) {
			free(args->argv[i]);
		}
	}
	free(args->argv);
	free(args);
}

void
args_add(struct args *args, const char *s)
{
	args->argv = (char **)x_realloc(args->argv,
	                                (args->argc + 2) * sizeof(char *));
	args->argv[args->argc] = x_strdup(s);
	args->argc++;
	args->argv[args->argc] = NULL;
}

// Add all arguments in to_append to args.
void
args_extend(struct args *args, struct args *to_append)
{
	for (int i = 0; i < to_append->argc; i++) {
		args_add(args, to_append->argv[i]);
	}
}

// Pop the last element off the args list.
void
args_pop(struct args *args, int n)
{
	while (n--) {
		args->argc--;
		free(args->argv[args->argc]);
		args->argv[args->argc] = NULL;
	}
}

// Set argument at given index.
void
args_set(struct args *args, int index, const char *value)
{
	assert(index < args->argc);
	free(args->argv[index]);
	args->argv[index] = x_strdup(value);
}

// Remove the first element of the argument list.
void
args_remove_first(struct args *args)
{
	free(args->argv[0]);
	memmove(&args->argv[0], &args->argv[1], args->argc * sizeof(args->argv[0]));
	args->argc--;
}

// Add an argument into the front of the argument list.
void
args_add_prefix(struct args *args, const char *s)
{
	args->argv = (char **)x_realloc(args->argv,
	                                (args->argc + 2) * sizeof(char *));
	memmove(&args->argv[1], &args->argv[0],
	        (args->argc+1) * sizeof(args->argv[0]));
	args->argv[0] = x_strdup(s);
	args->argc++;
}

// Strip any arguments beginning with the specified prefix.
void
args_strip(struct args *args, const char *prefix)
{
	for (int i = 0; i < args->argc; ) {
		if (str_startswith(args->argv[i], prefix)) {
			free(args->argv[i]);
			memmove(&args->argv[i],
			        &args->argv[i+1],
			        (args->argc - i) * sizeof(args->argv[i]));
			args->argc--;
		} else {
			i++;
		}
	}
}

// Format args to a space-separated string. Does not quote spaces. Caller
// frees.
char *
args_to_string(struct args *args)
{
	unsigned size = 0;
	for (char **p = args->argv; *p; p++) {
		size += strlen(*p) + 1;
	}

	char *result = x_malloc(size + 1);
	int pos = 0;
	for (char **p = args->argv; *p; p++) {
		pos += sprintf(&result[pos], "%s ", *p);
	}
	result[pos - 1] = '\0';
	return result;
}

// Returns true if args1 equals args2, else false.
bool
args_equal(struct args *args1, struct args *args2)
{
	if (args1->argc != args2->argc) {
		return false;
	}
	for (int i = 0; i < args1->argc; i++) {
		if (!str_eq(args1->argv[i], args2->argv[i])) {
			return false;
		}
	}
	return true;
}