aboutsummaryrefslogtreecommitdiffstats
path: root/libgeome_get_from_patterned_command.c
blob: 7490dbb1173351424ecf8edf24aec2a8cacb134f (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


static char *
substitute(struct libgeome_context *ctx, const char *text, uint64_t features)
{
	size_t len = 0, sublen;
	size_t i;
	int negative, include;
	uint64_t if_features, digit;
	char *ret, *p;

	for (i = 0; text[i];) {
		if (text[i] != '%') {
			i += 1U;
			len += 1U;
		} else if (text[i + 1U] == '%') {
			i += 2U;
			len += 1U;
		} else if (text[i + 1U] == '{') {
			i += 2U;
			negative = text[i] == '!';
			i += (size_t)negative;
			if_features = 0;
			for (; isdigit(text[i]); i++) {
				digit = (uint64_t)(text[i] & 15);
				if (if_features > (UINT64_MAX - digit) / 10U)
					goto invalid;
				if_features = if_features * 10U + digit;
			}
			if (!if_features || text[i++] != ':')
				goto invalid;
			include = !!(if_features & features);
			include ^= negative;
			sublen = 0;
			for (;;) {
				if (text[i] == '}') {
					i += 1U;
					break;
				} else if (!text[i]) {
					goto invalid;
				} else if (text[i] != '%') {
					i += 1U;
					sublen += 1U;
				} else if (text[i + 1U] == '%') {
					i += 1U;
					sublen += 2U;
				} else if (text[i + 1U] == '}') {
					i += 1U;
					sublen += 2U;
				} else {
					goto invalid;
				}
			}
			if (include ^ negative)
				len += sublen;
		} else {
		invalid:
			ctx->print_error(ctx, "invalid %-pattern in `%s'\n", text);
			return NULL;
		}
	}

	p = ret = malloc(len + 1U);
	if (!ret) {
		ctx->print_error(ctx, "malloc: %s\n", strerror(errno));
		return NULL;
	}

	while (*text) {
		if (*text != '%') {
			*p++ = *text++;
		} else if (text[i + 1U] == '%') {
			text = &text[2];
			*p++ = '%';
		} else if (text[i + 1U] == '{') {
			text = &text[2];
			negative = *text == '!';
			text = &text[negative];
			if_features = 0;
			while (isdigit(*text)) {
				digit = (uint64_t)(*text++ & 15);
				if_features = if_features * 10U + digit;
			}
			text++;
			include = !!(if_features & features);
			include ^= negative;
			for (; *text != '}'; text++) {
				if (include)
					*p++ = text[*text == '%'];
				if (*text == '%')
					text++;
			}
			text++;
		}
	}
	*p = '\0';

	return ret;
}


int
libgeome_get_from_patterned_command(struct libgeome_context *ctx, struct libgeome_data *out, size_t limit, /* TODO test */
                                    unsigned int alarm_sec, const char *path, const char *const *argv)
{
	const char **argv_new = NULL;
	char **array;
	size_t count = 0;
	size_t index = 0;
	size_t i, argc = 0;
	int ret = -1;

	if (strchr(path, '%'))
		count += 1U;

	for (i = 0; argv[i]; i++)
		if (strchr(argv[i], '%'))
			count += 1U;
	argc = i;

	if (!count)
		return libgeome_get_from_patterned_command(ctx, out, limit, alarm_sec, path, argv);

	array = calloc(count, sizeof(*array));
	if (!array) {
		ctx->print_error(ctx, "calloc: %s\n", strerror(errno));
		return -1;
	}

	if (strchr(path, '%')) {
		path = array[index++] = substitute(ctx, path, out->requested_data);
		if (!path)
			goto out;
	}

	if (index != count) {
		argv_new = calloc(argc + 1U, sizeof(*argv_new));
		if (!argv_new) {
			ctx->print_error(ctx, "calloc: %s\n", strerror(errno));
			goto out;
		}
		argv_new[argc] = NULL;
		for (i = 0; argv[i]; i++) {
			if (strchr(argv[i], '%')) {
				argv_new[i] = array[index++] = substitute(ctx, argv[i], out->requested_data);
				if (!argv_new[i])
					goto out;
			} else {
				argv_new[i] = argv[i];
			}
		}
		argv = argv_new;
	}

	ret = libgeome_get_from_patterned_command(ctx, out, limit, alarm_sec, path, argv);
out:
	for (i = 0; i < count; i++)
		free(array[i]);
	free(array);
	free(argv_new);
	return ret;
}