File: parseargs.c

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (168 lines) | stat: -rw-r--r-- 3,284 bytes parent folder | download | duplicates (3)
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
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "zdtmtst.h"

static struct long_opt *opt_head;

static int help;
TEST_OPTION(help, bool, "print help message and exit", 0);

void __push_opt(struct long_opt *opt)
{
	opt->next = opt_head;
	/* FIXME: barrier ? */
	opt_head = opt;
}

int parse_opt_bool(char *param, void *arg)
{
	if (param == NULL || !strcmp(param, "on") || !strcmp(param, "yes") || !strcmp(param, "true")) {
		*(int *)arg = 1;
		return 0;
	}
	if (!strcmp(param, "off") || !strcmp(param, "no") || !strcmp(param, "false")) {
		*(int *)arg = 0;
		return 0;
	}
	return -EINVAL;
}

int parse_opt_int(char *param, void *arg)
{
	char *tail;
	if (param == NULL || param[0] == '\0')
		return -EINVAL;
	*(int *)arg = strtol(param, &tail, 0);
	if (tail[0] != '\0')
		return -EINVAL;
	return 0;
}

int parse_opt_uint(char *param, void *arg)
{
	char *tail;
	if (param == NULL || param[0] == '\0')
		return -EINVAL;
	*(unsigned int *)arg = strtoul(param, &tail, 0);
	if (tail[0] != '\0')
		return -EINVAL;
	return 0;
}

int parse_opt_long(char *param, void *arg)
{
	char *tail;
	if (param == NULL || param[0] == '\0')
		return -EINVAL;
	*(long *)arg = strtol(param, &tail, 0);
	if (tail[0] != '\0')
		return -EINVAL;
	return 0;
}

int parse_opt_ulong(char *param, void *arg)
{
	char *tail;
	if (param == NULL || param[0] == '\0')
		return -EINVAL;
	*(unsigned long *)arg = strtoul(param, &tail, 0);
	if (tail[0] != '\0')
		return -EINVAL;
	return 0;
}

int parse_opt_string(char *param, void *arg)
{
	if (param == NULL || param[0] == '\0')
		return -EINVAL;
	*(char **)arg = param;
	return 0;
}

static void printopt(const struct long_opt *opt)
{
	const char *obracket = "", *cbracket = "";

	if (!opt->is_required) {
		obracket = "[";
		cbracket = "]";
	}

	fprintf(stderr, "  %s--%s=%s%s\t%s\n", obracket, opt->name, opt->type, cbracket, opt->doc);
}

static void helpexit(void)
{
	struct long_opt *opt;

	fputs("Usage:\n", stderr);

	for (opt = opt_head; opt; opt = opt->next)
		printopt(opt);

	exit(1);
}

const char __attribute__((weak)) * test_doc;
const char __attribute__((weak)) * test_author;

static void prdoc(void)
{
	if (test_doc)
		fprintf(stderr, "%s\n", test_doc);
	if (test_author)
		fprintf(stderr, "Author: %s\n", test_author);
}

void parseargs(int argc, char **argv)
{
	int i;
	struct long_opt *opt;

	for (i = 1; i < argc; i++) {
		char *name, *value;

		if (strlen(argv[i]) < 2 || strncmp(argv[i], "--", 2)) {
			fprintf(stderr, "%s: options should start with --\n", argv[i]);
			helpexit();
		}

		name = argv[i] + 2;

		value = strchr(name, '=');
		if (value)
			value++;

		for (opt = opt_head; opt; opt = opt->next)
			if (!strncmp(name, opt->name, value - name - 1)) {
				if (opt->parse_opt(value, opt->value)) {
					fprintf(stderr, "%s: failed to parse\n", argv[i]);
					helpexit();
				} else
					/* -1 marks fulfilled requirement */
					opt->is_required = -opt->is_required;

				break;
			}

		if (!opt) {
			fprintf(stderr, "%s: unknown option\n", argv[i]);
			helpexit();
		}
	}

	if (help) {
		prdoc();
		helpexit();
	}

	for (opt = opt_head; opt; opt = opt->next)
		if (opt->is_required > 0) {
			fprintf(stderr, "mandatory flag --%s not given\n", opt->name);
			helpexit();
		}
}