main.c revision 1.5 1 /* $NetBSD: main.c,v 1.5 2009/05/19 22:09:59 jnemeth 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.5 2009/05/19 22:09:59 jnemeth 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
46 int main(int, char **);
47 static void parse_bool_param(prop_dictionary_t, const char *,
48 const char *);
49 static void parse_int_param(prop_dictionary_t, const char *,
50 const char *);
51 static void parse_param(prop_dictionary_t, const char *,
52 void (*)(prop_dictionary_t, const char *,
53 const char *));
54 static void parse_string_param(prop_dictionary_t, const char *,
55 const char *);
56 static void usage(void) __dead;
57
58 int
59 main(int argc, char **argv)
60 {
61 modctl_load_t cmdargs;
62 prop_dictionary_t props;
63 bool output_props = false;
64 char *propsstr;
65 int ch;
66 int flags;
67
68 flags = 0;
69 props = prop_dictionary_create();
70
71 while ((ch = getopt(argc, argv, "b:fi:ps:")) != -1) {
72 switch (ch) {
73 case 'b':
74 parse_param(props, optarg, parse_bool_param);
75 break;
76
77 case 'f':
78 flags |= MODCTL_LOAD_FORCE;
79 break;
80
81 case 'i':
82 parse_param(props, optarg, parse_int_param);
83 break;
84
85 case 'p':
86 output_props = true;
87 break;
88
89 case 's':
90 parse_param(props, optarg, parse_string_param);
91 break;
92
93 default:
94 usage();
95 /* NOTREACHED */
96 }
97 }
98
99 argc -= optind;
100 argv += optind;
101
102 propsstr = prop_dictionary_externalize(props);
103 if (propsstr == NULL)
104 errx(EXIT_FAILURE, "Failed to process properties");
105
106 if (output_props)
107 puts(propsstr);
108 else {
109 if (argc != 1)
110 usage();
111 cmdargs.ml_filename = argv[0];
112 cmdargs.ml_flags = flags;
113 cmdargs.ml_props = propsstr;
114 cmdargs.ml_propslen = strlen(propsstr);
115
116 if (modctl(MODCTL_LOAD, &cmdargs)) {
117 err(EXIT_FAILURE, NULL);
118 }
119 }
120
121 free(propsstr);
122 prop_object_release(props);
123
124 exit(EXIT_SUCCESS);
125 }
126
127 static void
128 parse_bool_param(prop_dictionary_t props, const char *name,
129 const char *value)
130 {
131 bool boolvalue;
132
133 assert(name != NULL);
134 assert(value != NULL);
135
136 if (strcasecmp(value, "1") == 0 ||
137 strcasecmp(value, "true") == 0 ||
138 strcasecmp(value, "yes") == 0)
139 boolvalue = true;
140 else if (strcasecmp(value, "0") == 0 ||
141 strcasecmp(value, "false") == 0 ||
142 strcasecmp(value, "no") == 0)
143 boolvalue = false;
144 else
145 errx(EXIT_FAILURE, "Invalid boolean value `%s'", value);
146
147 prop_dictionary_set(props, name, prop_bool_create(boolvalue));
148 }
149
150 static void
151 parse_int_param(prop_dictionary_t props, const char *name,
152 const char *value)
153 {
154 int64_t intvalue;
155
156 assert(name != NULL);
157 assert(value != NULL);
158
159 if (dehumanize_number(value, &intvalue) != 0)
160 err(EXIT_FAILURE, "Invalid integer value `%s'", value);
161
162 prop_dictionary_set(props, name,
163 prop_number_create_integer(intvalue));
164 }
165
166 static 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 void
187 parse_string_param(prop_dictionary_t props, const char *name,
188 const char *value)
189 {
190
191 assert(name != NULL);
192 assert(value != NULL);
193
194 prop_dictionary_set(props, name, prop_string_create_cstring(value));
195 }
196
197 static void
198 usage(void)
199 {
200
201 (void)fprintf(stderr,
202 "Usage: %s [-b var=boolean] [-f] [-i var=integer] "
203 "[-s var=string]\n"
204 " {-p|<module_name>}\n",
205 getprogname());
206 exit(EXIT_FAILURE);
207 }
208