summaryrefslogtreecommitdiff
path: root/src/libmowgli/ext/program_opts.c
blob: 93b04acc4646fcd63bce05dbe89041d820ba4f49 (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
/*
 * libmowgli: A collection of useful routines for programming.
 * program_opts.h: Replacement for GNU getopt().
 *
 * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "mowgli.h"

#include "ext/getopt_long.h"

void
mowgli_program_opts_consumer_str(const char *arg, void *userdata)
{
	return_if_fail(arg != NULL);
	return_if_fail(userdata != NULL);

	*(char **) userdata = mowgli_strdup(arg);
}

void
mowgli_program_opts_consumer_int(const char *arg, void *userdata)
{
	return_if_fail(arg != NULL);
	return_if_fail(userdata != NULL);

	*(int *) userdata = atoi(arg);
}

void
mowgli_program_opts_consumer_bool(const char *arg, void *userdata)
{
	return_if_fail(arg != NULL);
	return_if_fail(userdata != NULL);

	*(bool *) userdata = true;
}

static inline mowgli_getopt_option_t *
mowgli_program_opts_convert(const mowgli_program_opts_t *opts, size_t opts_size)
{
	mowgli_getopt_option_t *g_opts;
	size_t i;

	return_val_if_fail(opts != NULL, NULL);

	g_opts = mowgli_alloc_array(sizeof(mowgli_getopt_option_t), opts_size);

	for (i = 0; i < opts_size; i++)
	{
		if (opts[i].longopt == NULL)
			continue;

		g_opts[i].name = opts[i].longopt;
		g_opts[i].iflag = i;

		if (opts[i].has_param)
			g_opts[i].has_arg = 1;
	}

	return g_opts;
}

static inline const char *
mowgli_program_opts_compute_optstr(const mowgli_program_opts_t *opts, size_t opts_size)
{
	static char buf[256];
	char *p = buf;
	size_t i;

	return_val_if_fail(opts != NULL, NULL);

	memset(buf, '\0', sizeof buf);

	for (i = 0; i < opts_size; i++)
	{
		if (!opts[i].smallopt)
			continue;

		*p++ = opts[i].smallopt;

		if (opts[i].has_param)
			*p++ = ':';
	}

	*p = '\0';

	return buf;
}

static inline void
mowgli_program_opts_dispatch(const mowgli_program_opts_t *opt, const char *optarg)
{
	return_if_fail(opt != NULL);

	if (opt->has_param && (optarg == NULL))
	{
		fprintf(stderr, "no optarg for option %s", opt->longopt);
		return;
	}

	opt->consumer(optarg, opt->userdata);
}

void
mowgli_program_opts_parse(const mowgli_program_opts_t *opts, size_t opts_size, int *argc, char ***argv)
{
	mowgli_getopt_option_t *g_opts;
	const char *shortops;
	int c;
	size_t i;
	int opt_index;

	return_if_fail(opts != NULL);
	return_if_fail(opts_size > 0);
	return_if_fail(argc != NULL);
	return_if_fail(argv != NULL);

	g_opts = mowgli_program_opts_convert(opts, opts_size);
	shortops = mowgli_program_opts_compute_optstr(opts, opts_size);

	for (;;)
	{
		const mowgli_program_opts_t *opt = NULL;

		c = mowgli_getopt_long(*argc, *argv, shortops, g_opts, &opt_index);

		if (c == -1)
			break;

		switch (c)
		{
		case 0:

			/* long-option was provided, resolve it. */
			opt = &opts[g_opts[opt_index].iflag];
			break;
		default:

			for (i = 0; i < opts_size; i++)
				if (opts[i].smallopt == c)
				{
					opt = &opts[i];
					break;
				}

			break;
		}

		mowgli_program_opts_dispatch(opt, mowgli_optarg);
	}

	mowgli_free(g_opts);
}