main.c revision 1.4
1/*	$NetBSD: main.c,v 1.4 2008/11/12 12:35:53 ad Exp $	*/
2
3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30#ifndef lint
31__RCSID("$NetBSD: main.c,v 1.4 2008/11/12 12:35:53 ad Exp $");
32#endif /* !lint */
33
34#include <sys/module.h>
35
36#include <assert.h>
37#include <stdbool.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <unistd.h>
42#include <err.h>
43
44#include <prop/proplib.h>
45
46int		main(int, char **);
47static void	parse_bool_param(prop_dictionary_t, const char *,
48				 const char *);
49static void	parse_int_param(prop_dictionary_t, const char *,
50				const char *);
51static void	parse_param(prop_dictionary_t, const char *,
52			    void (*)(prop_dictionary_t, const char *,
53			    const char *));
54static void	parse_string_param(prop_dictionary_t, const char *,
55				   const char *);
56static void	usage(void) __dead;
57
58int
59main(int argc, char **argv)
60{
61	modctl_load_t cmdargs;
62	prop_dictionary_t props;
63	char *propsstr;
64	int ch;
65	int flags;
66
67	flags = 0;
68	props = prop_dictionary_create();
69
70	while ((ch = getopt(argc, argv, "b:fi:s:")) != -1) {
71		switch (ch) {
72		case 'b':
73			parse_param(props, optarg, parse_bool_param);
74			break;
75
76		case 'f':
77			flags |= MODCTL_LOAD_FORCE;
78			break;
79
80		case 'i':
81			parse_param(props, optarg, parse_int_param);
82			break;
83
84		case 's':
85			parse_param(props, optarg, parse_string_param);
86			break;
87
88		default:
89			usage();
90			/* NOTREACHED */
91		}
92	}
93
94	argc -= optind;
95	argv += optind;
96	if (argc != 1)
97		usage();
98
99	propsstr = prop_dictionary_externalize(props);
100	if (propsstr == NULL)
101		errx(EXIT_FAILURE, "Failed to process properties");
102
103	cmdargs.ml_filename = argv[0];
104	cmdargs.ml_flags = flags;
105	cmdargs.ml_props = propsstr;
106	cmdargs.ml_propslen = strlen(propsstr);
107
108	if (modctl(MODCTL_LOAD, &cmdargs)) {
109		err(EXIT_FAILURE, NULL);
110	}
111
112	free(propsstr);
113	prop_object_release(props);
114
115	exit(EXIT_SUCCESS);
116}
117
118static void
119parse_bool_param(prop_dictionary_t props, const char *name,
120		 const char *value)
121{
122	bool boolvalue;
123
124	assert(name != NULL);
125	assert(value != NULL);
126
127	if (strcasecmp(value, "1") == 0 ||
128	    strcasecmp(value, "true") == 0 ||
129	    strcasecmp(value, "yes") == 0)
130		boolvalue = true;
131	else if (strcasecmp(value, "0") == 0 ||
132	    strcasecmp(value, "false") == 0 ||
133	    strcasecmp(value, "no") == 0)
134		boolvalue = false;
135	else
136		errx(EXIT_FAILURE, "Invalid boolean value `%s'", value);
137
138	prop_dictionary_set(props, name, prop_bool_create(boolvalue));
139}
140
141static void
142parse_int_param(prop_dictionary_t props, const char *name,
143		const char *value)
144{
145	int64_t intvalue;
146
147	assert(name != NULL);
148	assert(value != NULL);
149
150	if (dehumanize_number(value, &intvalue) != 0)
151		err(EXIT_FAILURE, "Invalid integer value `%s'", value);
152
153	prop_dictionary_set(props, name,
154	    prop_number_create_integer(intvalue));
155}
156
157static void
158parse_param(prop_dictionary_t props, const char *origstr,
159	    void (*fmt_handler)(prop_dictionary_t, const char *, const char *))
160{
161	char *name, *value;
162
163	name = strdup(origstr);
164
165	value = strchr(name, '=');
166	if (value == NULL) {
167		free(name);
168		errx(EXIT_FAILURE, "Invalid parameter `%s'", origstr);
169	}
170	*value++ = '\0';
171
172	fmt_handler(props, name, value);
173
174	free(name);
175}
176
177static void
178parse_string_param(prop_dictionary_t props, const char *name,
179		   const char *value)
180{
181
182	assert(name != NULL);
183	assert(value != NULL);
184
185	prop_dictionary_set(props, name, prop_string_create_cstring(value));
186}
187
188static void
189usage(void)
190{
191
192	(void)fprintf(stderr,
193	    "Usage: %s [-b var=boolean] [-f] [-i var=integer] "
194	    "[-s var=string]\n"
195	    "       <module_name>\n",
196	    getprogname());
197	exit(EXIT_FAILURE);
198}
199