1 1.18 thorpej /* $NetBSD: main.c,v 1.18 2020/06/07 05:49:05 thorpej 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.18 thorpej __RCSID("$NetBSD: main.c,v 1.18 2020/06/07 05:49:05 thorpej Exp $"); 32 1.1 ad #endif /* !lint */ 33 1.1 ad 34 1.1 ad #include <sys/module.h> 35 1.10 jnemeth #include <sys/queue.h> 36 1.1 ad 37 1.2 jmmv #include <assert.h> 38 1.2 jmmv #include <stdbool.h> 39 1.1 ad #include <stdio.h> 40 1.1 ad #include <stdlib.h> 41 1.1 ad #include <string.h> 42 1.1 ad #include <unistd.h> 43 1.1 ad #include <err.h> 44 1.1 ad 45 1.2 jmmv #include <prop/proplib.h> 46 1.2 jmmv 47 1.14 pooka #include "prog_ops.h" 48 1.14 pooka 49 1.2 jmmv static void parse_bool_param(prop_dictionary_t, const char *, 50 1.4 ad const char *); 51 1.2 jmmv static void parse_int_param(prop_dictionary_t, const char *, 52 1.4 ad const char *); 53 1.2 jmmv static void parse_param(prop_dictionary_t, const char *, 54 1.4 ad void (*)(prop_dictionary_t, const char *, 55 1.4 ad const char *)); 56 1.2 jmmv static void parse_string_param(prop_dictionary_t, const char *, 57 1.4 ad const char *); 58 1.1 ad static void usage(void) __dead; 59 1.8 jnemeth static void merge_dicts(prop_dictionary_t, const prop_dictionary_t); 60 1.1 ad 61 1.1 ad int 62 1.1 ad main(int argc, char **argv) 63 1.1 ad { 64 1.10 jnemeth SIMPLEQ_HEAD(del_head, del_item) del_head; 65 1.2 jmmv modctl_load_t cmdargs; 66 1.8 jnemeth prop_dictionary_t ext_props, props; 67 1.10 jnemeth bool del_props, merge_props, output_props; 68 1.8 jnemeth const char *ext_file; 69 1.2 jmmv char *propsstr; 70 1.1 ad int ch; 71 1.2 jmmv int flags; 72 1.1 ad 73 1.10 jnemeth struct del_item { 74 1.10 jnemeth SIMPLEQ_ENTRY(del_item) del_items; 75 1.10 jnemeth const char *del_key; 76 1.10 jnemeth } *delp; 77 1.10 jnemeth 78 1.10 jnemeth SIMPLEQ_INIT(&del_head); 79 1.8 jnemeth ext_file = NULL; 80 1.8 jnemeth ext_props = NULL; 81 1.8 jnemeth props = prop_dictionary_create(); 82 1.10 jnemeth del_props = merge_props = output_props = false; 83 1.2 jmmv flags = 0; 84 1.1 ad 85 1.11 jnemeth while ((ch = getopt(argc, argv, "Pb:d:fi:m:ps:")) != -1) { 86 1.1 ad switch (ch) { 87 1.11 jnemeth case 'P': 88 1.11 jnemeth flags |= MODCTL_NO_PROP; 89 1.11 jnemeth break; 90 1.2 jmmv case 'b': 91 1.2 jmmv parse_param(props, optarg, parse_bool_param); 92 1.2 jmmv break; 93 1.2 jmmv 94 1.10 jnemeth case 'd': 95 1.10 jnemeth del_props = true; 96 1.10 jnemeth delp = malloc(sizeof(struct del_item)); 97 1.10 jnemeth if (delp == NULL) 98 1.10 jnemeth errx(EXIT_FAILURE, "Out of memory"); 99 1.10 jnemeth delp->del_key = optarg; 100 1.10 jnemeth SIMPLEQ_INSERT_TAIL(&del_head, delp, del_items); 101 1.10 jnemeth break; 102 1.10 jnemeth 103 1.1 ad case 'f': 104 1.2 jmmv flags |= MODCTL_LOAD_FORCE; 105 1.2 jmmv break; 106 1.2 jmmv 107 1.2 jmmv case 'i': 108 1.2 jmmv parse_param(props, optarg, parse_int_param); 109 1.1 ad break; 110 1.2 jmmv 111 1.8 jnemeth case 'm': 112 1.8 jnemeth merge_props = true; 113 1.8 jnemeth ext_file = optarg; 114 1.8 jnemeth break; 115 1.8 jnemeth 116 1.5 jnemeth case 'p': 117 1.5 jnemeth output_props = true; 118 1.5 jnemeth break; 119 1.5 jnemeth 120 1.2 jmmv case 's': 121 1.2 jmmv parse_param(props, optarg, parse_string_param); 122 1.2 jmmv break; 123 1.2 jmmv 124 1.1 ad default: 125 1.1 ad usage(); 126 1.1 ad /* NOTREACHED */ 127 1.1 ad } 128 1.1 ad } 129 1.1 ad 130 1.1 ad argc -= optind; 131 1.1 ad argv += optind; 132 1.1 ad 133 1.2 jmmv propsstr = prop_dictionary_externalize(props); 134 1.2 jmmv if (propsstr == NULL) 135 1.2 jmmv errx(EXIT_FAILURE, "Failed to process properties"); 136 1.2 jmmv 137 1.8 jnemeth if (output_props) { 138 1.8 jnemeth if (merge_props) { 139 1.8 jnemeth ext_props = 140 1.8 jnemeth prop_dictionary_internalize_from_file(ext_file); 141 1.8 jnemeth if (ext_props == NULL) { 142 1.8 jnemeth errx(EXIT_FAILURE, "Failed to read existing " 143 1.8 jnemeth "property list"); 144 1.8 jnemeth } 145 1.8 jnemeth 146 1.8 jnemeth free(propsstr); 147 1.8 jnemeth merge_dicts(ext_props, props); 148 1.10 jnemeth 149 1.10 jnemeth if (del_props) 150 1.10 jnemeth SIMPLEQ_FOREACH(delp, &del_head, del_items) 151 1.10 jnemeth prop_dictionary_remove(ext_props, 152 1.10 jnemeth delp->del_key); 153 1.10 jnemeth 154 1.8 jnemeth propsstr = prop_dictionary_externalize(ext_props); 155 1.10 jnemeth if (propsstr == NULL) 156 1.10 jnemeth errx(EXIT_FAILURE, "Failed to process " 157 1.10 jnemeth "properties"); 158 1.8 jnemeth } 159 1.8 jnemeth 160 1.7 jnemeth fputs(propsstr, stdout); 161 1.8 jnemeth } else { 162 1.5 jnemeth if (argc != 1) 163 1.5 jnemeth usage(); 164 1.14 pooka if (prog_init && prog_init() == -1) 165 1.14 pooka err(1, "prog init failed"); 166 1.5 jnemeth cmdargs.ml_filename = argv[0]; 167 1.5 jnemeth cmdargs.ml_flags = flags; 168 1.5 jnemeth cmdargs.ml_props = propsstr; 169 1.5 jnemeth cmdargs.ml_propslen = strlen(propsstr); 170 1.2 jmmv 171 1.14 pooka if (prog_modctl(MODCTL_LOAD, &cmdargs)) { 172 1.16 khorben err(EXIT_FAILURE, "%s", cmdargs.ml_filename); 173 1.5 jnemeth } 174 1.1 ad } 175 1.1 ad 176 1.2 jmmv free(propsstr); 177 1.2 jmmv prop_object_release(props); 178 1.2 jmmv 179 1.1 ad exit(EXIT_SUCCESS); 180 1.1 ad } 181 1.1 ad 182 1.4 ad static void 183 1.2 jmmv parse_bool_param(prop_dictionary_t props, const char *name, 184 1.4 ad const char *value) 185 1.2 jmmv { 186 1.2 jmmv bool boolvalue; 187 1.2 jmmv 188 1.2 jmmv assert(name != NULL); 189 1.2 jmmv assert(value != NULL); 190 1.2 jmmv 191 1.2 jmmv if (strcasecmp(value, "1") == 0 || 192 1.2 jmmv strcasecmp(value, "true") == 0 || 193 1.2 jmmv strcasecmp(value, "yes") == 0) 194 1.2 jmmv boolvalue = true; 195 1.2 jmmv else if (strcasecmp(value, "0") == 0 || 196 1.2 jmmv strcasecmp(value, "false") == 0 || 197 1.2 jmmv strcasecmp(value, "no") == 0) 198 1.2 jmmv boolvalue = false; 199 1.2 jmmv else 200 1.2 jmmv errx(EXIT_FAILURE, "Invalid boolean value `%s'", value); 201 1.2 jmmv 202 1.18 thorpej if (!prop_dictionary_set_bool(props, name, boolvalue)) 203 1.18 thorpej err(EXIT_FAILURE, "prop_dictionary_set_bool"); 204 1.2 jmmv } 205 1.2 jmmv 206 1.4 ad static void 207 1.2 jmmv parse_int_param(prop_dictionary_t props, const char *name, 208 1.4 ad const char *value) 209 1.2 jmmv { 210 1.2 jmmv int64_t intvalue; 211 1.2 jmmv 212 1.2 jmmv assert(name != NULL); 213 1.2 jmmv assert(value != NULL); 214 1.2 jmmv 215 1.2 jmmv if (dehumanize_number(value, &intvalue) != 0) 216 1.2 jmmv err(EXIT_FAILURE, "Invalid integer value `%s'", value); 217 1.2 jmmv 218 1.18 thorpej if (!prop_dictionary_set_int64(props, name, intvalue)) 219 1.18 thorpej err(EXIT_FAILURE, "prop_dictionary_set_int64"); 220 1.2 jmmv } 221 1.2 jmmv 222 1.4 ad static void 223 1.2 jmmv parse_param(prop_dictionary_t props, const char *origstr, 224 1.4 ad void (*fmt_handler)(prop_dictionary_t, const char *, const char *)) 225 1.2 jmmv { 226 1.2 jmmv char *name, *value; 227 1.2 jmmv 228 1.2 jmmv name = strdup(origstr); 229 1.2 jmmv 230 1.2 jmmv value = strchr(name, '='); 231 1.2 jmmv if (value == NULL) { 232 1.2 jmmv free(name); 233 1.2 jmmv errx(EXIT_FAILURE, "Invalid parameter `%s'", origstr); 234 1.2 jmmv } 235 1.2 jmmv *value++ = '\0'; 236 1.2 jmmv 237 1.2 jmmv fmt_handler(props, name, value); 238 1.2 jmmv 239 1.2 jmmv free(name); 240 1.2 jmmv } 241 1.2 jmmv 242 1.4 ad static void 243 1.2 jmmv parse_string_param(prop_dictionary_t props, const char *name, 244 1.4 ad const char *value) 245 1.2 jmmv { 246 1.2 jmmv 247 1.2 jmmv assert(name != NULL); 248 1.2 jmmv assert(value != NULL); 249 1.2 jmmv 250 1.18 thorpej if (!prop_dictionary_set_string(props, name, value)) 251 1.18 thorpej err(EXIT_FAILURE, "prop_dictionary_set_string"); 252 1.2 jmmv } 253 1.2 jmmv 254 1.1 ad static void 255 1.1 ad usage(void) 256 1.1 ad { 257 1.1 ad 258 1.2 jmmv (void)fprintf(stderr, 259 1.13 wiz "Usage: %s [-fP] [-b var=boolean] [-i var=integer] " 260 1.6 wiz "[-s var=string] module\n" 261 1.12 jnemeth " %s -p [-b var=boolean] [-d var] [-i var=integer] " 262 1.10 jnemeth "[-m plist]\n [-s var=string]\n", 263 1.6 wiz getprogname(), getprogname()); 264 1.1 ad exit(EXIT_FAILURE); 265 1.1 ad } 266 1.8 jnemeth 267 1.8 jnemeth static void 268 1.8 jnemeth merge_dicts(prop_dictionary_t existing_dict, const prop_dictionary_t new_dict) 269 1.8 jnemeth { 270 1.8 jnemeth prop_dictionary_keysym_t props_keysym; 271 1.8 jnemeth prop_object_iterator_t props_iter; 272 1.8 jnemeth prop_object_t props_obj; 273 1.8 jnemeth const char *props_key; 274 1.8 jnemeth 275 1.8 jnemeth props_iter = prop_dictionary_iterator(new_dict); 276 1.8 jnemeth if (props_iter == NULL) { 277 1.8 jnemeth errx(EXIT_FAILURE, "Failed to iterate new property list"); 278 1.8 jnemeth } 279 1.8 jnemeth 280 1.8 jnemeth while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) { 281 1.8 jnemeth props_keysym = (prop_dictionary_keysym_t)props_obj; 282 1.18 thorpej props_key = prop_dictionary_keysym_value(props_keysym); 283 1.8 jnemeth props_obj = prop_dictionary_get_keysym(new_dict, props_keysym); 284 1.8 jnemeth if ((props_obj == NULL) || !prop_dictionary_set(existing_dict, 285 1.8 jnemeth props_key, props_obj)) { 286 1.8 jnemeth errx(EXIT_FAILURE, "Failed to copy " 287 1.8 jnemeth "existing property list"); 288 1.8 jnemeth } 289 1.8 jnemeth } 290 1.8 jnemeth prop_object_iterator_release(props_iter); 291 1.8 jnemeth 292 1.8 jnemeth return; 293 1.8 jnemeth } 294