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