xref: /netbsd-src/sbin/modload/main.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: main.c,v 1.3 2008/04/28 20:23:09 martin 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.3 2008/04/28 20:23:09 martin 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 *, const char *));
53 static void	parse_string_param(prop_dictionary_t, const char *,
54 		    const char *);
55 static void	usage(void) __dead;
56 
57 int
58 main(int argc, char **argv)
59 {
60 	modctl_load_t cmdargs;
61 	prop_dictionary_t props;
62 	char *propsstr;
63 	int ch;
64 	int flags;
65 
66 	flags = 0;
67 	props = prop_dictionary_create();
68 
69 	while ((ch = getopt(argc, argv, "b:fi:s:")) != -1) {
70 		switch (ch) {
71 		case 'b':
72 			parse_param(props, optarg, parse_bool_param);
73 			break;
74 
75 		case 'f':
76 			flags |= MODCTL_LOAD_FORCE;
77 			break;
78 
79 		case 'i':
80 			parse_param(props, optarg, parse_int_param);
81 			break;
82 
83 		case 's':
84 			parse_param(props, optarg, parse_string_param);
85 			break;
86 
87 		default:
88 			usage();
89 			/* NOTREACHED */
90 		}
91 	}
92 
93 	argc -= optind;
94 	argv += optind;
95 	if (argc != 1)
96 		usage();
97 
98 	propsstr = prop_dictionary_externalize(props);
99 	if (propsstr == NULL)
100 		errx(EXIT_FAILURE, "Failed to process properties");
101 
102 	cmdargs.ml_filename = argv[0];
103 	cmdargs.ml_flags = flags;
104 	cmdargs.ml_props = propsstr;
105 	cmdargs.ml_propslen = strlen(propsstr);
106 
107 	if (modctl(MODCTL_LOAD, &cmdargs)) {
108 		err(EXIT_FAILURE, NULL);
109 	}
110 
111 	free(propsstr);
112 	prop_object_release(props);
113 
114 	exit(EXIT_SUCCESS);
115 }
116 
117 static
118 void
119 parse_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 
141 static
142 void
143 parse_int_param(prop_dictionary_t props, const char *name,
144     const char *value)
145 {
146 	int64_t intvalue;
147 
148 	assert(name != NULL);
149 	assert(value != NULL);
150 
151 	if (dehumanize_number(value, &intvalue) != 0)
152 		err(EXIT_FAILURE, "Invalid integer value `%s'", value);
153 
154 	prop_dictionary_set(props, name,
155 	    prop_number_create_integer(intvalue));
156 }
157 
158 static
159 void
160 parse_param(prop_dictionary_t props, const char *origstr,
161     void (*fmt_handler)(prop_dictionary_t, const char *, const char *))
162 {
163 	char *name, *value;
164 
165 	name = strdup(origstr);
166 
167 	value = strchr(name, '=');
168 	if (value == NULL) {
169 		free(name);
170 		errx(EXIT_FAILURE, "Invalid parameter `%s'", origstr);
171 	}
172 	*value++ = '\0';
173 
174 	fmt_handler(props, name, value);
175 
176 	free(name);
177 }
178 
179 static
180 void
181 parse_string_param(prop_dictionary_t props, const char *name,
182     const char *value)
183 {
184 
185 	assert(name != NULL);
186 	assert(value != NULL);
187 
188 	prop_dictionary_set(props, name, prop_string_create_cstring(value));
189 }
190 
191 static void
192 usage(void)
193 {
194 
195 	(void)fprintf(stderr,
196 	    "Usage: %s [-b var=boolean] [-f] [-i var=integer] "
197 	    "[-s var=string]\n"
198 	    "       <module_name>\n",
199 	    getprogname());
200 	exit(EXIT_FAILURE);
201 }
202