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