1*2ad03484Smiod /* $OpenBSD: main.c,v 1.26 2022/11/09 07:20:12 miod Exp $ */
2f3a87fc6Sderaadt /* $NetBSD: main.c,v 1.3 1996/05/16 16:00:55 thorpej Exp $ */
3df930be7Sderaadt
4608f9123Sniklas /*-
5608f9123Sniklas * Copyright (c) 1996 The NetBSD Foundation, Inc.
6df930be7Sderaadt * All rights reserved.
7df930be7Sderaadt *
8608f9123Sniklas * This code is derived from software contributed to The NetBSD Foundation
9608f9123Sniklas * by Jason R. Thorpe.
10608f9123Sniklas *
11df930be7Sderaadt * Redistribution and use in source and binary forms, with or without
12df930be7Sderaadt * modification, are permitted provided that the following conditions
13df930be7Sderaadt * are met:
14df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright
15df930be7Sderaadt * notice, this list of conditions and the following disclaimer.
16df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright
17df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the
18df930be7Sderaadt * documentation and/or other materials provided with the distribution.
19df930be7Sderaadt *
20608f9123Sniklas * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21608f9123Sniklas * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22608f9123Sniklas * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23608f9123Sniklas * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24608f9123Sniklas * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25608f9123Sniklas * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26608f9123Sniklas * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27608f9123Sniklas * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28608f9123Sniklas * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29608f9123Sniklas * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30608f9123Sniklas * POSSIBILITY OF SUCH DAMAGE.
31df930be7Sderaadt */
32df930be7Sderaadt
33df930be7Sderaadt #include <err.h>
34df930be7Sderaadt #include <string.h>
35df930be7Sderaadt #include <stdio.h>
36ab95fc2fSfgsch #include <stdlib.h>
37df930be7Sderaadt #include <unistd.h>
38df930be7Sderaadt
39df930be7Sderaadt
40df930be7Sderaadt #include <machine/openpromio.h>
41df930be7Sderaadt
42df930be7Sderaadt #include "defs.h"
43df930be7Sderaadt
44c72b5b24Smillert static void action(char *);
45c72b5b24Smillert static void dump_prom(void);
46c72b5b24Smillert static void usage(void);
47df930be7Sderaadt
48df930be7Sderaadt char *path_openprom = "/dev/openprom";
49df930be7Sderaadt int eval = 0;
50ab95fc2fSfgsch int print_tree = 0;
51df930be7Sderaadt int verbose = 0;
52df930be7Sderaadt
53df930be7Sderaadt extern char *__progname;
54df930be7Sderaadt
55df930be7Sderaadt int
main(int argc,char * argv[])563dad9bb3Sderaadt main(int argc, char *argv[])
57df930be7Sderaadt {
58df930be7Sderaadt int ch, do_stdin = 0;
59df930be7Sderaadt char *cp, line[BUFSIZE];
60*2ad03484Smiod char *optstring = "f:pv-";
61df930be7Sderaadt
62df930be7Sderaadt while ((ch = getopt(argc, argv, optstring)) != -1)
63df930be7Sderaadt switch (ch) {
64df930be7Sderaadt case '-':
65df930be7Sderaadt do_stdin = 1;
66df930be7Sderaadt break;
67df930be7Sderaadt case 'f':
68*2ad03484Smiod path_openprom = optarg;
69df930be7Sderaadt break;
70ab95fc2fSfgsch case 'p':
71ab95fc2fSfgsch print_tree = 1;
72ab95fc2fSfgsch break;
73df930be7Sderaadt case 'v':
74df930be7Sderaadt verbose = 1;
75df930be7Sderaadt break;
76df930be7Sderaadt default:
77df930be7Sderaadt usage();
78df930be7Sderaadt }
79df930be7Sderaadt argc -= optind;
80df930be7Sderaadt argv += optind;
81df930be7Sderaadt
828a8d2e48Stedu if (print_tree) {
83ab95fc2fSfgsch op_tree();
84ab95fc2fSfgsch exit(0);
85ab95fc2fSfgsch }
86df930be7Sderaadt
87df930be7Sderaadt if (do_stdin) {
88df930be7Sderaadt while (fgets(line, BUFSIZE, stdin) != NULL) {
89df930be7Sderaadt if (line[0] == '\n')
90df930be7Sderaadt continue;
91df930be7Sderaadt if ((cp = strrchr(line, '\n')) != NULL)
92df930be7Sderaadt *cp = '\0';
93df930be7Sderaadt action(line);
94df930be7Sderaadt }
95df930be7Sderaadt if (ferror(stdin))
96df930be7Sderaadt err(++eval, "stdin");
97df930be7Sderaadt } else {
98df930be7Sderaadt if (argc == 0) {
99df930be7Sderaadt dump_prom();
100*2ad03484Smiod exit(eval);
101df930be7Sderaadt }
102df930be7Sderaadt
103df930be7Sderaadt while (argc) {
104f3a87fc6Sderaadt action(*argv);
105df930be7Sderaadt ++argv;
106df930be7Sderaadt --argc;
107df930be7Sderaadt }
108df930be7Sderaadt }
109df930be7Sderaadt
110*2ad03484Smiod exit(eval);
111df930be7Sderaadt }
112df930be7Sderaadt
113df930be7Sderaadt /*
114df930be7Sderaadt * Separate the keyword from the argument (if any), find the keyword in
115df930be7Sderaadt * the table, and call the corresponding handler function.
116df930be7Sderaadt */
117df930be7Sderaadt static void
action(char * line)1183dad9bb3Sderaadt action(char *line)
119df930be7Sderaadt {
120df930be7Sderaadt char *keyword, *arg, *cp;
121df930be7Sderaadt
122df930be7Sderaadt keyword = strdup(line);
123ca198d9fSderaadt if (!keyword)
124ca198d9fSderaadt errx(1, "out of memory");
125df930be7Sderaadt if ((arg = strrchr(keyword, '=')) != NULL)
126df930be7Sderaadt *arg++ = '\0';
127df930be7Sderaadt
128df930be7Sderaadt /*
129df930be7Sderaadt * The whole point of the Openprom is that one
130df930be7Sderaadt * isn't required to know the keywords. With this
131df930be7Sderaadt * in mind, we just dump the whole thing off to
132df930be7Sderaadt * the generic op_handler.
133df930be7Sderaadt */
134df930be7Sderaadt if ((cp = op_handler(keyword, arg)) != NULL)
135b638aa94Smillert warnx("%s", cp);
136df930be7Sderaadt }
137df930be7Sderaadt
138df930be7Sderaadt /*
139df930be7Sderaadt * Dump the contents of the prom corresponding to all known keywords.
140df930be7Sderaadt */
141df930be7Sderaadt static void
dump_prom(void)1423dad9bb3Sderaadt dump_prom(void)
143df930be7Sderaadt {
144df930be7Sderaadt /*
145df930be7Sderaadt * We have a special dump routine for this.
146df930be7Sderaadt */
147df930be7Sderaadt op_dump();
148fa530530Skettenis }
149df930be7Sderaadt
150df930be7Sderaadt static void
usage(void)1513dad9bb3Sderaadt usage(void)
152df930be7Sderaadt {
153df930be7Sderaadt
1540bf314e8Sjmc fprintf(stderr,
155*2ad03484Smiod "usage: %s [-pv] [-f device] [field[=value] ...]\n",
1560bf314e8Sjmc __progname);
157df930be7Sderaadt exit(1);
158df930be7Sderaadt }
159