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