main.c revision 1.8 1 1.8 jnemeth /* $NetBSD: main.c,v 1.8 2009/06/04 02:57:01 jnemeth 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 *
16 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 ad */
28 1.1 ad
29 1.1 ad #include <sys/cdefs.h>
30 1.1 ad #ifndef lint
31 1.8 jnemeth __RCSID("$NetBSD: main.c,v 1.8 2009/06/04 02:57:01 jnemeth Exp $");
32 1.1 ad #endif /* !lint */
33 1.1 ad
34 1.1 ad #include <sys/module.h>
35 1.1 ad
36 1.2 jmmv #include <assert.h>
37 1.2 jmmv #include <stdbool.h>
38 1.1 ad #include <stdio.h>
39 1.1 ad #include <stdlib.h>
40 1.1 ad #include <string.h>
41 1.1 ad #include <unistd.h>
42 1.1 ad #include <err.h>
43 1.1 ad
44 1.2 jmmv #include <prop/proplib.h>
45 1.2 jmmv
46 1.2 jmmv int main(int, char **);
47 1.2 jmmv static void parse_bool_param(prop_dictionary_t, const char *,
48 1.4 ad const char *);
49 1.2 jmmv static void parse_int_param(prop_dictionary_t, const char *,
50 1.4 ad const char *);
51 1.2 jmmv static void parse_param(prop_dictionary_t, const char *,
52 1.4 ad void (*)(prop_dictionary_t, const char *,
53 1.4 ad const char *));
54 1.2 jmmv static void parse_string_param(prop_dictionary_t, const char *,
55 1.4 ad const char *);
56 1.1 ad static void usage(void) __dead;
57 1.8 jnemeth static void merge_dicts(prop_dictionary_t, const prop_dictionary_t);
58 1.1 ad
59 1.1 ad int
60 1.1 ad main(int argc, char **argv)
61 1.1 ad {
62 1.2 jmmv modctl_load_t cmdargs;
63 1.8 jnemeth prop_dictionary_t ext_props, props;
64 1.8 jnemeth bool merge_props, output_props;
65 1.8 jnemeth const char *ext_file;
66 1.2 jmmv char *propsstr;
67 1.1 ad int ch;
68 1.2 jmmv int flags;
69 1.1 ad
70 1.8 jnemeth ext_file = NULL;
71 1.8 jnemeth ext_props = NULL;
72 1.8 jnemeth props = prop_dictionary_create();
73 1.8 jnemeth merge_props = output_props = false;
74 1.2 jmmv flags = 0;
75 1.1 ad
76 1.8 jnemeth while ((ch = getopt(argc, argv, "b:fi:m:ps:")) != -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.8 jnemeth case 'm':
91 1.8 jnemeth merge_props = true;
92 1.8 jnemeth ext_file = optarg;
93 1.8 jnemeth break;
94 1.8 jnemeth
95 1.5 jnemeth case 'p':
96 1.5 jnemeth output_props = true;
97 1.5 jnemeth break;
98 1.5 jnemeth
99 1.2 jmmv case 's':
100 1.2 jmmv parse_param(props, optarg, parse_string_param);
101 1.2 jmmv break;
102 1.2 jmmv
103 1.1 ad default:
104 1.1 ad usage();
105 1.1 ad /* NOTREACHED */
106 1.1 ad }
107 1.1 ad }
108 1.1 ad
109 1.1 ad argc -= optind;
110 1.1 ad argv += optind;
111 1.1 ad
112 1.2 jmmv propsstr = prop_dictionary_externalize(props);
113 1.2 jmmv if (propsstr == NULL)
114 1.2 jmmv errx(EXIT_FAILURE, "Failed to process properties");
115 1.2 jmmv
116 1.8 jnemeth if (output_props) {
117 1.8 jnemeth if (merge_props) {
118 1.8 jnemeth ext_props =
119 1.8 jnemeth prop_dictionary_internalize_from_file(ext_file);
120 1.8 jnemeth if (ext_props == NULL) {
121 1.8 jnemeth errx(EXIT_FAILURE, "Failed to read existing "
122 1.8 jnemeth "property list");
123 1.8 jnemeth }
124 1.8 jnemeth
125 1.8 jnemeth free(propsstr);
126 1.8 jnemeth merge_dicts(ext_props, props);
127 1.8 jnemeth propsstr = prop_dictionary_externalize(ext_props);
128 1.8 jnemeth }
129 1.8 jnemeth
130 1.7 jnemeth fputs(propsstr, stdout);
131 1.8 jnemeth } else {
132 1.5 jnemeth if (argc != 1)
133 1.5 jnemeth usage();
134 1.5 jnemeth cmdargs.ml_filename = argv[0];
135 1.5 jnemeth cmdargs.ml_flags = flags;
136 1.5 jnemeth cmdargs.ml_props = propsstr;
137 1.5 jnemeth cmdargs.ml_propslen = strlen(propsstr);
138 1.2 jmmv
139 1.5 jnemeth if (modctl(MODCTL_LOAD, &cmdargs)) {
140 1.5 jnemeth err(EXIT_FAILURE, NULL);
141 1.5 jnemeth }
142 1.1 ad }
143 1.1 ad
144 1.2 jmmv free(propsstr);
145 1.2 jmmv prop_object_release(props);
146 1.2 jmmv
147 1.1 ad exit(EXIT_SUCCESS);
148 1.1 ad }
149 1.1 ad
150 1.4 ad static void
151 1.2 jmmv parse_bool_param(prop_dictionary_t props, const char *name,
152 1.4 ad const char *value)
153 1.2 jmmv {
154 1.2 jmmv bool boolvalue;
155 1.2 jmmv
156 1.2 jmmv assert(name != NULL);
157 1.2 jmmv assert(value != NULL);
158 1.2 jmmv
159 1.2 jmmv if (strcasecmp(value, "1") == 0 ||
160 1.2 jmmv strcasecmp(value, "true") == 0 ||
161 1.2 jmmv strcasecmp(value, "yes") == 0)
162 1.2 jmmv boolvalue = true;
163 1.2 jmmv else if (strcasecmp(value, "0") == 0 ||
164 1.2 jmmv strcasecmp(value, "false") == 0 ||
165 1.2 jmmv strcasecmp(value, "no") == 0)
166 1.2 jmmv boolvalue = false;
167 1.2 jmmv else
168 1.2 jmmv errx(EXIT_FAILURE, "Invalid boolean value `%s'", value);
169 1.2 jmmv
170 1.2 jmmv prop_dictionary_set(props, name, prop_bool_create(boolvalue));
171 1.2 jmmv }
172 1.2 jmmv
173 1.4 ad static void
174 1.2 jmmv parse_int_param(prop_dictionary_t props, const char *name,
175 1.4 ad const char *value)
176 1.2 jmmv {
177 1.2 jmmv int64_t intvalue;
178 1.2 jmmv
179 1.2 jmmv assert(name != NULL);
180 1.2 jmmv assert(value != NULL);
181 1.2 jmmv
182 1.2 jmmv if (dehumanize_number(value, &intvalue) != 0)
183 1.2 jmmv err(EXIT_FAILURE, "Invalid integer value `%s'", value);
184 1.2 jmmv
185 1.2 jmmv prop_dictionary_set(props, name,
186 1.2 jmmv prop_number_create_integer(intvalue));
187 1.2 jmmv }
188 1.2 jmmv
189 1.4 ad static void
190 1.2 jmmv parse_param(prop_dictionary_t props, const char *origstr,
191 1.4 ad void (*fmt_handler)(prop_dictionary_t, const char *, const char *))
192 1.2 jmmv {
193 1.2 jmmv char *name, *value;
194 1.2 jmmv
195 1.2 jmmv name = strdup(origstr);
196 1.2 jmmv
197 1.2 jmmv value = strchr(name, '=');
198 1.2 jmmv if (value == NULL) {
199 1.2 jmmv free(name);
200 1.2 jmmv errx(EXIT_FAILURE, "Invalid parameter `%s'", origstr);
201 1.2 jmmv }
202 1.2 jmmv *value++ = '\0';
203 1.2 jmmv
204 1.2 jmmv fmt_handler(props, name, value);
205 1.2 jmmv
206 1.2 jmmv free(name);
207 1.2 jmmv }
208 1.2 jmmv
209 1.4 ad static void
210 1.2 jmmv parse_string_param(prop_dictionary_t props, const char *name,
211 1.4 ad const char *value)
212 1.2 jmmv {
213 1.2 jmmv
214 1.2 jmmv assert(name != NULL);
215 1.2 jmmv assert(value != NULL);
216 1.2 jmmv
217 1.2 jmmv prop_dictionary_set(props, name, prop_string_create_cstring(value));
218 1.2 jmmv }
219 1.2 jmmv
220 1.1 ad static void
221 1.1 ad usage(void)
222 1.1 ad {
223 1.1 ad
224 1.2 jmmv (void)fprintf(stderr,
225 1.6 wiz "Usage: %s [-f] [-b var=boolean] [-i var=integer] "
226 1.6 wiz "[-s var=string] module\n"
227 1.8 jnemeth " %s -p [-m plist] [-b var=boolean] [-i var=integer] "
228 1.6 wiz "[-s var=string]\n",
229 1.6 wiz getprogname(), getprogname());
230 1.1 ad exit(EXIT_FAILURE);
231 1.1 ad }
232 1.8 jnemeth
233 1.8 jnemeth static void
234 1.8 jnemeth merge_dicts(prop_dictionary_t existing_dict, const prop_dictionary_t new_dict)
235 1.8 jnemeth {
236 1.8 jnemeth prop_dictionary_keysym_t props_keysym;
237 1.8 jnemeth prop_object_iterator_t props_iter;
238 1.8 jnemeth prop_object_t props_obj;
239 1.8 jnemeth const char *props_key;
240 1.8 jnemeth
241 1.8 jnemeth props_iter = prop_dictionary_iterator(new_dict);
242 1.8 jnemeth if (props_iter == NULL) {
243 1.8 jnemeth errx(EXIT_FAILURE, "Failed to iterate new property list");
244 1.8 jnemeth }
245 1.8 jnemeth
246 1.8 jnemeth while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) {
247 1.8 jnemeth props_keysym = (prop_dictionary_keysym_t)props_obj;
248 1.8 jnemeth props_key = prop_dictionary_keysym_cstring_nocopy(props_keysym);
249 1.8 jnemeth props_obj = prop_dictionary_get_keysym(new_dict, props_keysym);
250 1.8 jnemeth if ((props_obj == NULL) || !prop_dictionary_set(existing_dict,
251 1.8 jnemeth props_key, props_obj)) {
252 1.8 jnemeth errx(EXIT_FAILURE, "Failed to copy "
253 1.8 jnemeth "existing property list");
254 1.8 jnemeth }
255 1.8 jnemeth }
256 1.8 jnemeth prop_object_iterator_release(props_iter);
257 1.8 jnemeth
258 1.8 jnemeth return;
259 1.8 jnemeth }
260