]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/eeprom/eeprom.c
Add two missing eventhandler.h headers
[FreeBSD/FreeBSD.git] / usr.sbin / eeprom / eeprom.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3  *
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  *      from: NetBSD: main.c,v 1.15 2001/02/19 23:22:42 cgd Exp
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sysexits.h>
42 #include <unistd.h>
43
44 #include "ofw_options.h"
45
46 static int      action(char *);
47 static void     dump_config(void);
48 static void     usage(void);
49
50 static void
51 usage(void)
52 {
53
54         fprintf(stderr,
55             "usage: eeprom -a\n"
56             "       eeprom [-] name[=value] ...\n");
57         exit(EX_USAGE);
58 }
59
60 int
61 main(int argc, char *argv[])
62 {
63         int do_stdin, opt;
64         int aflag, rv;
65         char *cp;
66         char line[BUFSIZ];
67
68         aflag = do_stdin = 0;
69         rv = EX_OK;
70         while ((opt = getopt(argc, argv, "-a")) != -1) {
71                 switch (opt) {
72                 case '-':
73                         if (aflag)
74                                 usage();
75                         do_stdin = 1;
76                         break;
77                 case 'a':
78                         if (do_stdin)
79                                 usage();
80                         aflag = 1;
81                         break;
82                 case '?':
83                 default:
84                         usage();
85                         /* NOTREACHED */
86                 }
87         }
88         argc -= optind;
89         argv += optind;
90
91         if (aflag) {
92                 if (argc != 0)
93                         usage();
94                 dump_config();
95         } else {
96                 if (do_stdin) {
97                         while (fgets(line, BUFSIZ, stdin) != NULL &&
98                             rv == EX_OK) {
99                                 if (line[0] == '\n')
100                                         continue;
101                                 if ((cp = strrchr(line, '\n')) != NULL)
102                                         *cp = '\0';
103                                 rv = action(line);
104                         }
105                         if (ferror(stdin))
106                                 err(EX_NOINPUT, "stdin");
107                 } else {
108                         if (argc == 0)
109                                 usage();
110                         while (argc && rv == EX_OK) {
111                                 rv = action(*argv);
112                                 ++argv;
113                                 --argc;
114                         }
115                 }
116         }
117         return (rv);
118 }
119
120 static int
121 action(char *line)
122 {
123         int rv;
124         char *keyword, *arg;
125
126         keyword = strdup(line);
127         if (keyword == NULL)
128                 err(EX_OSERR, "malloc() failed");
129         if ((arg = strrchr(keyword, '=')) != NULL)
130                 *arg++ = '\0';
131         switch (rv = ofwo_action(keyword, arg)) {
132                 case EX_UNAVAILABLE:
133                         warnx("nothing available for '%s'.", keyword);
134                         break;
135                 case EX_DATAERR:
136                         warnx("invalid value '%s' for '%s'.", arg, keyword);
137                         break;
138         }
139         free(keyword);
140         return(rv);
141 }
142
143 static void
144 dump_config(void)
145 {
146
147         ofwo_dump();
148 }