1*d147ce22Sthorpej /* $NetBSD: main.c,v 1.18 2020/06/07 05:49:05 thorpej Exp $ */
2bbc79e58Sad
3bbc79e58Sad /*-
4bbc79e58Sad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5bbc79e58Sad * All rights reserved.
6bbc79e58Sad *
7bbc79e58Sad * Redistribution and use in source and binary forms, with or without
8bbc79e58Sad * modification, are permitted provided that the following conditions
9bbc79e58Sad * are met:
10bbc79e58Sad * 1. Redistributions of source code must retain the above copyright
11bbc79e58Sad * notice, this list of conditions and the following disclaimer.
12bbc79e58Sad * 2. Redistributions in binary form must reproduce the above copyright
13bbc79e58Sad * notice, this list of conditions and the following disclaimer in the
14bbc79e58Sad * documentation and/or other materials provided with the distribution.
15bbc79e58Sad *
16bbc79e58Sad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17bbc79e58Sad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18bbc79e58Sad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19bbc79e58Sad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20bbc79e58Sad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21bbc79e58Sad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22bbc79e58Sad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23bbc79e58Sad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24bbc79e58Sad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25bbc79e58Sad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26bbc79e58Sad * POSSIBILITY OF SUCH DAMAGE.
27bbc79e58Sad */
28bbc79e58Sad
29bbc79e58Sad #include <sys/cdefs.h>
30bbc79e58Sad #ifndef lint
31*d147ce22Sthorpej __RCSID("$NetBSD: main.c,v 1.18 2020/06/07 05:49:05 thorpej Exp $");
32bbc79e58Sad #endif /* !lint */
33bbc79e58Sad
34bbc79e58Sad #include <sys/module.h>
355ce0121eSjnemeth #include <sys/queue.h>
36bbc79e58Sad
379e8fd365Sjmmv #include <assert.h>
389e8fd365Sjmmv #include <stdbool.h>
39bbc79e58Sad #include <stdio.h>
40bbc79e58Sad #include <stdlib.h>
41bbc79e58Sad #include <string.h>
42bbc79e58Sad #include <unistd.h>
43bbc79e58Sad #include <err.h>
44bbc79e58Sad
459e8fd365Sjmmv #include <prop/proplib.h>
469e8fd365Sjmmv
47e73ffddaSpooka #include "prog_ops.h"
48e73ffddaSpooka
499e8fd365Sjmmv static void parse_bool_param(prop_dictionary_t, const char *,
509e8fd365Sjmmv const char *);
519e8fd365Sjmmv static void parse_int_param(prop_dictionary_t, const char *,
529e8fd365Sjmmv const char *);
539e8fd365Sjmmv static void parse_param(prop_dictionary_t, const char *,
540efea177Sad void (*)(prop_dictionary_t, const char *,
550efea177Sad const char *));
569e8fd365Sjmmv static void parse_string_param(prop_dictionary_t, const char *,
579e8fd365Sjmmv const char *);
58bbc79e58Sad static void usage(void) __dead;
593ee4fdb5Sjnemeth static void merge_dicts(prop_dictionary_t, const prop_dictionary_t);
60bbc79e58Sad
61bbc79e58Sad int
main(int argc,char ** argv)62bbc79e58Sad main(int argc, char **argv)
63bbc79e58Sad {
645ce0121eSjnemeth SIMPLEQ_HEAD(del_head, del_item) del_head;
659e8fd365Sjmmv modctl_load_t cmdargs;
663ee4fdb5Sjnemeth prop_dictionary_t ext_props, props;
675ce0121eSjnemeth bool del_props, merge_props, output_props;
683ee4fdb5Sjnemeth const char *ext_file;
699e8fd365Sjmmv char *propsstr;
70bbc79e58Sad int ch;
719e8fd365Sjmmv int flags;
72bbc79e58Sad
735ce0121eSjnemeth struct del_item {
745ce0121eSjnemeth SIMPLEQ_ENTRY(del_item) del_items;
755ce0121eSjnemeth const char *del_key;
765ce0121eSjnemeth } *delp;
775ce0121eSjnemeth
785ce0121eSjnemeth SIMPLEQ_INIT(&del_head);
793ee4fdb5Sjnemeth ext_file = NULL;
803ee4fdb5Sjnemeth ext_props = NULL;
819e8fd365Sjmmv props = prop_dictionary_create();
825ce0121eSjnemeth del_props = merge_props = output_props = false;
833ee4fdb5Sjnemeth flags = 0;
84bbc79e58Sad
85cbd36566Sjnemeth while ((ch = getopt(argc, argv, "Pb:d:fi:m:ps:")) != -1) {
86bbc79e58Sad switch (ch) {
87cbd36566Sjnemeth case 'P':
88cbd36566Sjnemeth flags |= MODCTL_NO_PROP;
89cbd36566Sjnemeth break;
909e8fd365Sjmmv case 'b':
919e8fd365Sjmmv parse_param(props, optarg, parse_bool_param);
92bbc79e58Sad break;
939e8fd365Sjmmv
945ce0121eSjnemeth case 'd':
955ce0121eSjnemeth del_props = true;
965ce0121eSjnemeth delp = malloc(sizeof(struct del_item));
975ce0121eSjnemeth if (delp == NULL)
985ce0121eSjnemeth errx(EXIT_FAILURE, "Out of memory");
995ce0121eSjnemeth delp->del_key = optarg;
1005ce0121eSjnemeth SIMPLEQ_INSERT_TAIL(&del_head, delp, del_items);
1015ce0121eSjnemeth break;
1025ce0121eSjnemeth
1039e8fd365Sjmmv case 'f':
1049e8fd365Sjmmv flags |= MODCTL_LOAD_FORCE;
1059e8fd365Sjmmv break;
1069e8fd365Sjmmv
1079e8fd365Sjmmv case 'i':
1089e8fd365Sjmmv parse_param(props, optarg, parse_int_param);
1099e8fd365Sjmmv break;
1109e8fd365Sjmmv
1113ee4fdb5Sjnemeth case 'm':
1123ee4fdb5Sjnemeth merge_props = true;
1133ee4fdb5Sjnemeth ext_file = optarg;
1143ee4fdb5Sjnemeth break;
1153ee4fdb5Sjnemeth
11647371f5fSjnemeth case 'p':
11747371f5fSjnemeth output_props = true;
11847371f5fSjnemeth break;
11947371f5fSjnemeth
1209e8fd365Sjmmv case 's':
1219e8fd365Sjmmv parse_param(props, optarg, parse_string_param);
1229e8fd365Sjmmv break;
1239e8fd365Sjmmv
124bbc79e58Sad default:
125bbc79e58Sad usage();
126bbc79e58Sad /* NOTREACHED */
127bbc79e58Sad }
128bbc79e58Sad }
129bbc79e58Sad
130bbc79e58Sad argc -= optind;
131bbc79e58Sad argv += optind;
132bbc79e58Sad
1339e8fd365Sjmmv propsstr = prop_dictionary_externalize(props);
1349e8fd365Sjmmv if (propsstr == NULL)
1359e8fd365Sjmmv errx(EXIT_FAILURE, "Failed to process properties");
1369e8fd365Sjmmv
1373ee4fdb5Sjnemeth if (output_props) {
1383ee4fdb5Sjnemeth if (merge_props) {
1393ee4fdb5Sjnemeth ext_props =
1403ee4fdb5Sjnemeth prop_dictionary_internalize_from_file(ext_file);
1413ee4fdb5Sjnemeth if (ext_props == NULL) {
1423ee4fdb5Sjnemeth errx(EXIT_FAILURE, "Failed to read existing "
1433ee4fdb5Sjnemeth "property list");
1443ee4fdb5Sjnemeth }
1453ee4fdb5Sjnemeth
1463ee4fdb5Sjnemeth free(propsstr);
1473ee4fdb5Sjnemeth merge_dicts(ext_props, props);
1485ce0121eSjnemeth
1495ce0121eSjnemeth if (del_props)
1505ce0121eSjnemeth SIMPLEQ_FOREACH(delp, &del_head, del_items)
1515ce0121eSjnemeth prop_dictionary_remove(ext_props,
1525ce0121eSjnemeth delp->del_key);
1535ce0121eSjnemeth
1543ee4fdb5Sjnemeth propsstr = prop_dictionary_externalize(ext_props);
1555ce0121eSjnemeth if (propsstr == NULL)
1565ce0121eSjnemeth errx(EXIT_FAILURE, "Failed to process "
1575ce0121eSjnemeth "properties");
1583ee4fdb5Sjnemeth }
1593ee4fdb5Sjnemeth
1603431c6b6Sjnemeth fputs(propsstr, stdout);
1613ee4fdb5Sjnemeth } else {
16247371f5fSjnemeth if (argc != 1)
16347371f5fSjnemeth usage();
164e73ffddaSpooka if (prog_init && prog_init() == -1)
165e73ffddaSpooka err(1, "prog init failed");
1669e8fd365Sjmmv cmdargs.ml_filename = argv[0];
1679e8fd365Sjmmv cmdargs.ml_flags = flags;
1689e8fd365Sjmmv cmdargs.ml_props = propsstr;
1699e8fd365Sjmmv cmdargs.ml_propslen = strlen(propsstr);
1709e8fd365Sjmmv
171e73ffddaSpooka if (prog_modctl(MODCTL_LOAD, &cmdargs)) {
1723df9a0e8Skhorben err(EXIT_FAILURE, "%s", cmdargs.ml_filename);
173bbc79e58Sad }
17447371f5fSjnemeth }
175bbc79e58Sad
1769e8fd365Sjmmv free(propsstr);
1779e8fd365Sjmmv prop_object_release(props);
1789e8fd365Sjmmv
179bbc79e58Sad exit(EXIT_SUCCESS);
180bbc79e58Sad }
181bbc79e58Sad
1820efea177Sad static void
parse_bool_param(prop_dictionary_t props,const char * name,const char * value)1839e8fd365Sjmmv parse_bool_param(prop_dictionary_t props, const char *name,
1849e8fd365Sjmmv const char *value)
1859e8fd365Sjmmv {
1869e8fd365Sjmmv bool boolvalue;
1879e8fd365Sjmmv
1889e8fd365Sjmmv assert(name != NULL);
1899e8fd365Sjmmv assert(value != NULL);
1909e8fd365Sjmmv
1919e8fd365Sjmmv if (strcasecmp(value, "1") == 0 ||
1929e8fd365Sjmmv strcasecmp(value, "true") == 0 ||
1939e8fd365Sjmmv strcasecmp(value, "yes") == 0)
1949e8fd365Sjmmv boolvalue = true;
1959e8fd365Sjmmv else if (strcasecmp(value, "0") == 0 ||
1969e8fd365Sjmmv strcasecmp(value, "false") == 0 ||
1979e8fd365Sjmmv strcasecmp(value, "no") == 0)
1989e8fd365Sjmmv boolvalue = false;
1999e8fd365Sjmmv else
2009e8fd365Sjmmv errx(EXIT_FAILURE, "Invalid boolean value `%s'", value);
2019e8fd365Sjmmv
202*d147ce22Sthorpej if (!prop_dictionary_set_bool(props, name, boolvalue))
203*d147ce22Sthorpej err(EXIT_FAILURE, "prop_dictionary_set_bool");
2049e8fd365Sjmmv }
2059e8fd365Sjmmv
2060efea177Sad static void
parse_int_param(prop_dictionary_t props,const char * name,const char * value)2079e8fd365Sjmmv parse_int_param(prop_dictionary_t props, const char *name,
2089e8fd365Sjmmv const char *value)
2099e8fd365Sjmmv {
2109e8fd365Sjmmv int64_t intvalue;
2119e8fd365Sjmmv
2129e8fd365Sjmmv assert(name != NULL);
2139e8fd365Sjmmv assert(value != NULL);
2149e8fd365Sjmmv
2159e8fd365Sjmmv if (dehumanize_number(value, &intvalue) != 0)
2169e8fd365Sjmmv err(EXIT_FAILURE, "Invalid integer value `%s'", value);
2179e8fd365Sjmmv
218*d147ce22Sthorpej if (!prop_dictionary_set_int64(props, name, intvalue))
219*d147ce22Sthorpej err(EXIT_FAILURE, "prop_dictionary_set_int64");
2209e8fd365Sjmmv }
2219e8fd365Sjmmv
2220efea177Sad static void
parse_param(prop_dictionary_t props,const char * origstr,void (* fmt_handler)(prop_dictionary_t,const char *,const char *))2239e8fd365Sjmmv parse_param(prop_dictionary_t props, const char *origstr,
2249e8fd365Sjmmv void (*fmt_handler)(prop_dictionary_t, const char *, const char *))
2259e8fd365Sjmmv {
2269e8fd365Sjmmv char *name, *value;
2279e8fd365Sjmmv
2289e8fd365Sjmmv name = strdup(origstr);
2299e8fd365Sjmmv
2309e8fd365Sjmmv value = strchr(name, '=');
2319e8fd365Sjmmv if (value == NULL) {
2329e8fd365Sjmmv free(name);
2339e8fd365Sjmmv errx(EXIT_FAILURE, "Invalid parameter `%s'", origstr);
2349e8fd365Sjmmv }
2359e8fd365Sjmmv *value++ = '\0';
2369e8fd365Sjmmv
2379e8fd365Sjmmv fmt_handler(props, name, value);
2389e8fd365Sjmmv
2399e8fd365Sjmmv free(name);
2409e8fd365Sjmmv }
2419e8fd365Sjmmv
2420efea177Sad static void
parse_string_param(prop_dictionary_t props,const char * name,const char * value)2439e8fd365Sjmmv parse_string_param(prop_dictionary_t props, const char *name,
2449e8fd365Sjmmv const char *value)
2459e8fd365Sjmmv {
2469e8fd365Sjmmv
2479e8fd365Sjmmv assert(name != NULL);
2489e8fd365Sjmmv assert(value != NULL);
2499e8fd365Sjmmv
250*d147ce22Sthorpej if (!prop_dictionary_set_string(props, name, value))
251*d147ce22Sthorpej err(EXIT_FAILURE, "prop_dictionary_set_string");
2529e8fd365Sjmmv }
2539e8fd365Sjmmv
254bbc79e58Sad static void
usage(void)255bbc79e58Sad usage(void)
256bbc79e58Sad {
257bbc79e58Sad
2589e8fd365Sjmmv (void)fprintf(stderr,
259bba67e2cSwiz "Usage: %s [-fP] [-b var=boolean] [-i var=integer] "
26004c4f4a1Swiz "[-s var=string] module\n"
26159cc9994Sjnemeth " %s -p [-b var=boolean] [-d var] [-i var=integer] "
2625ce0121eSjnemeth "[-m plist]\n [-s var=string]\n",
26304c4f4a1Swiz getprogname(), getprogname());
264bbc79e58Sad exit(EXIT_FAILURE);
265bbc79e58Sad }
2663ee4fdb5Sjnemeth
2673ee4fdb5Sjnemeth static void
merge_dicts(prop_dictionary_t existing_dict,const prop_dictionary_t new_dict)2683ee4fdb5Sjnemeth merge_dicts(prop_dictionary_t existing_dict, const prop_dictionary_t new_dict)
2693ee4fdb5Sjnemeth {
2703ee4fdb5Sjnemeth prop_dictionary_keysym_t props_keysym;
2713ee4fdb5Sjnemeth prop_object_iterator_t props_iter;
2723ee4fdb5Sjnemeth prop_object_t props_obj;
2733ee4fdb5Sjnemeth const char *props_key;
2743ee4fdb5Sjnemeth
2753ee4fdb5Sjnemeth props_iter = prop_dictionary_iterator(new_dict);
2763ee4fdb5Sjnemeth if (props_iter == NULL) {
2773ee4fdb5Sjnemeth errx(EXIT_FAILURE, "Failed to iterate new property list");
2783ee4fdb5Sjnemeth }
2793ee4fdb5Sjnemeth
2803ee4fdb5Sjnemeth while ((props_obj = prop_object_iterator_next(props_iter)) != NULL) {
2813ee4fdb5Sjnemeth props_keysym = (prop_dictionary_keysym_t)props_obj;
282*d147ce22Sthorpej props_key = prop_dictionary_keysym_value(props_keysym);
2833ee4fdb5Sjnemeth props_obj = prop_dictionary_get_keysym(new_dict, props_keysym);
2843ee4fdb5Sjnemeth if ((props_obj == NULL) || !prop_dictionary_set(existing_dict,
2853ee4fdb5Sjnemeth props_key, props_obj)) {
2863ee4fdb5Sjnemeth errx(EXIT_FAILURE, "Failed to copy "
2873ee4fdb5Sjnemeth "existing property list");
2883ee4fdb5Sjnemeth }
2893ee4fdb5Sjnemeth }
2903ee4fdb5Sjnemeth prop_object_iterator_release(props_iter);
2913ee4fdb5Sjnemeth
2923ee4fdb5Sjnemeth return;
2933ee4fdb5Sjnemeth }
294