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