aboutsummaryrefslogtreecommitdiffstats
path: root/opts.c
blob: 7805897556480eeb3e4e8ccc8ddaccf27d39104b (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
/* See LICENSE file for copyright and license details. */
#include "common.h"


static int
sizeopt(size_t *valp, const char *s)
{
	if (isdigit(*s))
		return 0;
	*valp = 0;
	for (; isdigit(*s); s++) {
		if (*valp * 10U + (size_t)(*s & 15) > SIZE_MAX)
			*valp = SIZE_MAX;
		else
			*valp = *valp * 10U + (size_t)(*s & 15);
	}
	return !*s;
}


int
parseopt_vendor(void *config_, char *opt, char *val)
{
	struct config *config = config_;
	if (!strcmp(opt, "output")) {
		if (!val)
			eprintf("-W option \"%s\" should have an associated value", opt);
		config->format &= (enum format)~FORMAT_MASK;
		if (!strcmp(val, "lowercase"))
			config->format |= LOWERCASE_HEX;
		else if (!strcmp(val, "uppercase"))
			config->format |= UPPERCASE_HEX;
		else if (!strcmp(val, "binary"))
			config->format |= BINARY;
		else
			eprintf("-W option \"%s\" expected value \"lowercase\", \"uppercase\", or \"binary\"", opt);

	} else if (!strcmp(opt, "input")) {
		if (!val)
			eprintf("-W option \"%s\" should have an associated value", opt);
		config->format &= (enum format)~FORMAT_MASK;
		if (!strcmp(val, "binary") || !strcmp(val, "text"))
			config->hexinput = 0;
		else if (!strcmp(val, "hexadecimal"))
			config->hexinput = 1;
		else
			eprintf("-W option \"%s\" expected value \"binary\", \"text\", or \"hexadecimal\"", opt);

	} else if (!strcmp(opt, "threads")) {
		if (!val)
			eprintf("-W option \"%s\" should have an associated value", opt);
		if (!strcmp(val, "auto"))
			config->threads = 0;
		else if (!sizeopt(&config->threads, val) || !config->threads)
			eprintf("-W option \"%s\" expected positive integer value or \"auto\"", opt);

	} else if (!strcmp(opt, "recursive")) {
		if (val)
			eprintf("-W option \"%s\" should not have an associated value", opt);
		config->recursive = 1;

	} else if (!strcmp(opt, "no-recursive")) {
		if (val)
			eprintf("-W option \"%s\" should not have an associated value", opt);
		config->recursive = 0;

	} else if (!strcmp(opt, "xdev")) {
		if (val)
			eprintf("-W option \"%s\" should not have an associated value", opt);
		config->recursive = 1;
		config->xdev = 1;

	} else if (!strcmp(opt, "no-xdev")) {
		if (val)
			eprintf("-W option \"%s\" should not have an associated value", opt);
		config->xdev = 0;

	} else if (!strcmp(opt, "xlink")) {
		if (val)
			eprintf("-W option \"%s\" should not have an associated value", opt);
		config->recursive = 1;
		config->xlink = 1;

	} else if (!strcmp(opt, "no-xlink")) {
		if (val)
			eprintf("-W option \"%s\" should not have an associated value", opt);
		config->xlink = 0;

	} else {
		return 0;
	}
	return 1;
}


char *
parseopts(void *config, char *string, int (*parseopt)(void *, char *opt, char *val))
{
	char *opt, *r = string, c, *val;
	char *w = string;
	size_t brackets = 0;

	for (;;) {
		opt = r;
		while (*r && *r != ',' && *r != '=')
			r++;
		for (; *r; r++) {
			if (*r == '[' || *r == '(' || *r == '{')
				brackets++;
			else if (brackets && (*r == ']' || *r == ')' || *r == '}'))
				brackets--;
			else if (!brackets && (*r == ',' || *r == '='))
				break;
		}
		c = *r;
		*r++ = '\0';
		val = NULL;
		if (c == '=') {
			val = r;
			for (; *r; r++) {
				if (*r == '[' || *r == '(' || *r == '{')
					brackets++;
				else if (brackets && (*r == ']' || *r == ')' || *r == '}'))
					brackets--;
				else if (!brackets && *r == ',')
					break;
			}
			c = *r;
			*r++ = '\0';
		}

		if (!(*parseopt)(config, opt, val)) {
			if (*opt || val) {
				if (w != string)
					*w++ = ',';
				w = stpcpy(w, opt);
				if (val) {
					*w++ = '=';
					w = stpcpy(w, val);
				}
			}
		}

		if (!c)
			break;
	}

	*w = '\0';
	return string;
}