File: ccontrol-parse.c

package info (click to toggle)
ccontrol 0.9.1+20060806-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 440 kB
  • ctags: 188
  • sloc: ansic: 1,478; sh: 302; makefile: 142; python: 141; xml: 64
file content (524 lines) | stat: -rw-r--r-- 10,953 bytes parent folder | download | duplicates (2)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include "ccontrol.h"
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <fnmatch.h>
#include <fcntl.h>
#include <errno.h>
#include "stdrusty.h"

enum token_type {
	CC = TYPE_CC,
	CPLUSPLUS = TYPE_CPLUSPLUS,
	LD = TYPE_LD,
	MAKE = TYPE_MAKE,
	NONE,
	INDENT,
	NO_PARALLEL,
	PARALLEL,
	CCACHE,
	DISTCC,
	DISTCC_HOSTS,
	DISTCPLUSPLUS_HOSTS,
	LEFT_BRACKET,
	RIGHT_BRACKET,
	EQUALS,
	NUMBER,
	STRING,
	VERBOSE,
	NICE,
	INCLUDE,
	CPUS,
	DISABLE,
	ADD,
	ENV,
	LOCK_FILE,
	END,
};

#include "keywords.c"

struct token
{
	enum token_type type;
	struct string string;
};

static unsigned int slen(struct string string)
{
	return string.end - string.start;
}

/* Length up to and including char. 0 == not found.  */
static unsigned int upto(struct string data, char c)
{
	char *end;

	end = memchr(data.start, c, slen(data));
	if (end)
		return end - data.start + 1;
	return 0;
}

static void __attribute__((noreturn))
parse_error(struct token tok, const char *str)
{
	unsigned int line = 1;
	char *input_head = tok.string.start_of_file;

	/* Count what line we're on. */
	while (input_head < tok.string.start) {
		if (*input_head == '\n')
			line++;
		input_head++;
	}
	fatal("parse error on line ", 0,
	      int_to_string(line), ": expected ", str, NULL);
}

static bool swallow_whitspace(struct string *data)
{
	bool ret = false;

	while (data->start[0] == ' ' || data->start[0] == '\t') {
		ret = true;
		data->start++;
		if (data->start == data->end)
			break;
	}
	return ret;
}

static struct string get_string(struct string *data)
{
	struct string str;
	str.start_of_file = data->start_of_file;
	str.start = data->start;
	while (data->start != data->end
	       && *data->start != '='
	       && *data->start != '['
	       && *data->start != ']'
	       && *data->start != ' '
	       && *data->start != '\n'
	       && *data->start != '\t')
		data->start++;
	str.end = data->start;
	return str;
}

/* Token takes one character. */
static struct token single_digit_token(struct string data,enum token_type type)
{
	return ((struct token) { type,
			{ data.start, data.start+1, data.start_of_file } });
}

static struct token peek_token(struct string data)
{
	struct token tok;
	unsigned int num;
	bool new_line = (data.start == data.start_of_file);

	/* We might need to skip over comments. */
	while (data.start != data.end) {
		const struct ccontrol_command *cmd;
		char *start;

		if (data.start[0] == '\n') {
			new_line = true;
			data.start++;
			continue;
		}

		/* Whitespace: only relevant if after nl, followed by stuff. */
		start = data.start;
		if (swallow_whitspace(&data) && new_line) {
			if (slen(data)) {
				if (data.start[0] == '\n' || data.start[0] == '#')
					continue;

				tok.type = INDENT;
				tok.string.start_of_file = data.start_of_file;
				tok.string.start = start;
				tok.string.end = data.start;
				return tok;
			}
		}

		/* Comment?  Ignore to end of line. */
		if (data.start[0] == '#') {
			num = upto(data, '\n');
			if (!num)
				break;
			data.start += num;
			new_line = true;
			continue;
		}

		if (data.start[0] == '[')
			return single_digit_token(data, LEFT_BRACKET);

		if (data.start[0] == ']')
			return single_digit_token(data, RIGHT_BRACKET);

		if (data.start[0] == '=')
			return single_digit_token(data, EQUALS);

		tok.string = get_string(&data);
		cmd = find_keyword(tok.string.start, slen(tok.string));
		if (cmd)
			tok.type = cmd->type;
		else {
			/* Number or string? */
			unsigned int i;

			for (i = 0; i < slen(tok.string); i++)
				if (!(isdigit(tok.string.start[i]) ||
				      tok.string.start[i] == '-'))
					break;
			if (i > 0 && i == slen(tok.string))
				tok.type = NUMBER;
			else
				tok.type = STRING;
		}
		return tok;
	}

	tok.type = END;
	tok.string = data;
	return tok;
}

static void swallow_token(struct string *data, struct token tok)
{
	data->start = tok.string.end;
}

static struct token get_token(struct string *data)
{
	struct token tok = peek_token(*data);
	swallow_token(data, tok);
	return tok;
}

/* Optional = <token>. */
static struct token get_value(struct string *data)
{
	struct token tok;

	tok = peek_token(*data);
	if (tok.type == EQUALS) {
		swallow_token(data, tok);
		return get_token(data);
	}
	tok.type = NONE;
	tok.string.start = data->start;
	tok.string.end = data->start;
	return tok;
}

static int to_int(struct token tok, int min, int max)
{
	int num;
	if (tok.type != NUMBER)
		parse_error(tok, "'= some-number'");

	num = atoi(tok.string.start);
	if (num < min || num > max)
		parse_error(tok, "'= number in valid range'");
	return num;
}

/* '=' <path> */
static char *get_path(struct string *data)
{
	char *p;
	const char *prefix = "";
	struct token tok;

	tok = get_value(data);
	if (tok.type != STRING)
		parse_error(tok, "'= some-path'");

	if (tok.string.start[0] == '~') {
		prefix = getenv("HOME") ?: "";
		tok.string.start++;
	}

	p = new_array(char, strlen(prefix) + slen(tok.string) + 1);
	memcpy(p, prefix, strlen(prefix));
	memcpy(p + strlen(prefix), tok.string.start, slen(tok.string));
	p[strlen(prefix)+slen(tok.string)] = '\0';

	return p;
}

static char *get_optional_path(struct string *data)
{
	struct token tok;

	tok = peek_token(*data);
	if (tok.type == DISABLE) {
		swallow_token(data, tok);
		return NULL;
	}

	return get_path(data);
}

/* 'disable' or '=' <values...> */
static char *get_to_eol(struct string *data, const char *expect)
{
	char *p;
	unsigned int len;
	struct token tok;

	tok = get_token(data);
	if (tok.type == DISABLE)
		return NULL;
	if (tok.type != EQUALS)
		parse_error(tok, expect);

	swallow_whitspace(data);
	len = upto(*data, '\n');
	if (!len) {
		struct token tok;

		tok.type = END;
		tok.string = *data;
		parse_error(tok, "something");
	}

	/* Turn \n into \0. */
	p = malloc(len);
	memcpy(p, data->start, len);
	p[len-1] = '\0';

	/* Leave \n so we can detect indent. */
	data->start += len-1;

	return p;
}

static struct add *new_add(char *arg, struct add *next)
{
	struct add *add = malloc(sizeof(struct add));
	add->arg = arg;
	add->next = next;
	return add;
}

static void read_section_file(const char *configname, struct section *sec);

/* With thanks to Joseph Heller. */
static void read_section_section(struct string *data, struct section *sec)
{
	struct token tok;
	struct add **add;
	char *p;

	while ((tok = peek_token(*data)).type == INDENT) {
		swallow_token(data, tok);
		tok = get_token(data);
		/* Lines are of form "x" or "x = value". */
		switch (tok.type) {
		case NO_PARALLEL:
			sec->no_parallel = get_to_eol(data, "= targets");
			/* In case parent set it, and we're disabling it. */
			if (!sec->no_parallel)
				unsetenv("CCONTROL_NO_PARALLEL");
			break;
		case CPUS:
			sec->cpus = to_int(get_value(data), 1, 1000000);
			break;
		case CCACHE:
			sec->ccache = get_optional_path(data);
			break;
		case DISTCC:
			sec->distcc = get_optional_path(data);
			break;
		case DISTCC_HOSTS:
			sec->distcc_hosts = get_to_eol(data, "= some-hosts");
			if (!sec->distcplusplus_hosts_set)
				sec->distcplusplus_hosts = sec->distcc_hosts;
			break;
		case DISTCPLUSPLUS_HOSTS:
			sec->distcplusplus_hosts = get_to_eol(data,
							      "= some-hosts");
			sec->distcplusplus_hosts_set = true;
			break;
		case VERBOSE:
			sec->verbose = true;
			break;
		case NICE:
			sec->nice = to_int(get_value(data), -19, 20);
			break;
		case CC:
		case CPLUSPLUS:
		case LD:
		case MAKE:
			sec->names[tok.type] = get_path(data);
			break;
		case INCLUDE:
			read_section_file(get_path(data), sec);
			break;
		case ADD:
			tok = get_token(data);
			switch (tok.type) {
			case MAKE:
				add = &sec->make_add;
				break;
			case ENV:
				add = &sec->env_add;
				break;
			default:
				parse_error(tok, "make or env");
			}
			p = get_to_eol(data, "= argument");
			if (!p)
				*add = NULL;
			else 
				*add = new_add(p, *add);
			break;
		case LOCK_FILE:
			sec->lock_file = get_path(data);
			sec->lock_fd = open(sec->lock_file, O_RDWR|O_CREAT, 
					    0600);
			if (sec->lock_fd < 0)
				fatal("could not open lock file: ", errno, 
				      sec->lock_file, NULL);
			break;
		default:
			parse_error(tok, "some instruction");
		}
	}
}

static void read_section_file(const char *configname, struct section *sec)
{
	unsigned long len;
	struct string data;

	data.start_of_file = suck_file(open(configname, O_RDONLY), &len);
	if (!data.start_of_file)
		fatal("reading included file ", errno, configname, NULL);
	data.start = data.start_of_file;
	data.end = data.start_of_file + len;

	read_section_section(&data, sec);
}

static bool read_section(struct string *data, struct section *sec)
{
	struct token tok = get_token(data);

	if (tok.type == END)
		return false;

	if (tok.type != LEFT_BRACKET)
		parse_error(tok, "'[' to start new section");

	tok = get_token(data);
	if (tok.type != STRING)
		parse_error(tok, "path after '[' in section start");
	sec->name = tok.string;
	
	tok = get_token(data);
	if (tok.type != RIGHT_BRACKET)
		parse_error(tok, "']' after name in section start");

	read_section_section(data, sec);

	return true;
}

#ifndef PATH_MAX
#define PATH_MAX 4096
#endif

static bool section_matches(const char *dirname, struct section sec)
{
	unsigned int len = slen(sec.name);
	char pattern[len + 2];

	/* Default always matches. */
	if (len == 0)
		return true;

	memcpy(pattern, sec.name.start, len);
	/* Append a * if not there already. */
	if (pattern[len-1] == '*')
		pattern[len] = '\0';
	else {
		pattern[len] = '*';
		pattern[len+1] = '\0';
	}
	return (fnmatch(pattern, dirname, 0) == 0);
}

static struct section get_default(int fd)
{
	struct section def = { .cpus = 1,
			       .nice = 10,
			       .distcplusplus_hosts_set = false };
	def.lock_fd = fd;
	return def;
}

static void read_config_file(const char *configname, int fd,
			     const char *dirname,
			     struct section *result)
{
	unsigned long len;
	struct string data;

	data.start_of_file = suck_file(fd, &len);
	if (!data.start_of_file)
		fatal("reading ", errno, configname, NULL);
	data.start = data.start_of_file;
	data.end = data.start_of_file + len;

	/* Trivial parser. */
	for (;;) {
		struct section sec = *result;
		struct token tok;

		tok = peek_token(data);
		if (tok.type == INCLUDE) {
			char *included;
			int incfd;
			swallow_token(&data, tok);

			included = get_path(&data);
			incfd = open(included, O_RDONLY);
			read_config_file(included, incfd, dirname, result);
			close(incfd);
			continue;
		}

		if (!read_section(&data, &sec))
			break;

		if (section_matches(dirname, sec)) {
			if (sec.verbose) {
				unsigned int len = slen(sec.name);
				char str[len+1];
				memcpy(str, sec.name.start, len);
				str[len] = '\0';
				verbose(sec.verbose, "Using section ", str);
			}
			*result = sec;
		}
	}

	free(data.start_of_file);
}

struct section read_config(const char *configname, const char *dir, int fd)
{
	struct section result;

	result = get_default(fd);
	read_config_file(configname, fd, dir, &result);
	return result;
}