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