1129590Smarius/*-
2129590Smarius * Copyright (c) 1996 The NetBSD Foundation, Inc.
3129590Smarius * All rights reserved.
4129590Smarius *
5129590Smarius * This code is derived from software contributed to The NetBSD Foundation
6129590Smarius * by Jason R. Thorpe.
7129590Smarius *
8129590Smarius * Redistribution and use in source and binary forms, with or without
9129590Smarius * modification, are permitted provided that the following conditions
10129590Smarius * are met:
11129590Smarius * 1. Redistributions of source code must retain the above copyright
12129590Smarius *    notice, this list of conditions and the following disclaimer.
13129590Smarius * 2. Redistributions in binary form must reproduce the above copyright
14129590Smarius *    notice, this list of conditions and the following disclaimer in the
15129590Smarius *    documentation and/or other materials provided with the distribution.
16129590Smarius *
17129590Smarius * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18129590Smarius * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19129590Smarius * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20129590Smarius * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21129590Smarius * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22129590Smarius * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23129590Smarius * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24129590Smarius * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25129590Smarius * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26129590Smarius * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27129590Smarius * POSSIBILITY OF SUCH DAMAGE.
28129590Smarius *
29129590Smarius *	from: NetBSD: main.c,v 1.15 2001/02/19 23:22:42 cgd Exp
30129590Smarius */
31129590Smarius
32129590Smarius#include <sys/cdefs.h>
33129590Smarius__FBSDID("$FreeBSD$");
34129590Smarius
35129590Smarius#include <err.h>
36129590Smarius#include <stdio.h>
37129590Smarius#include <stdlib.h>
38129590Smarius#include <string.h>
39129590Smarius#include <sysexits.h>
40129590Smarius#include <unistd.h>
41129590Smarius
42129590Smarius#include "ofw_options.h"
43129590Smarius
44129590Smariusstatic int	action(char *);
45129590Smariusstatic void	dump_config(void);
46129590Smariusstatic void	usage(void);
47129590Smarius
48129590Smariusstatic void
49129590Smariususage(void)
50129590Smarius{
51129590Smarius
52129590Smarius	fprintf(stderr,
53129590Smarius	    "usage: eeprom -a\n"
54129590Smarius	    "       eeprom [-] name[=value] ...\n");
55129590Smarius	exit(EX_USAGE);
56129590Smarius}
57129590Smarius
58129590Smariusint
59129590Smariusmain(int argc, char *argv[])
60129590Smarius{
61129590Smarius	int do_stdin, opt;
62129590Smarius	int aflag, rv;
63129590Smarius	char *cp;
64129590Smarius	char line[BUFSIZ];
65129590Smarius
66129590Smarius	aflag = do_stdin = 0;
67129590Smarius	rv = EX_OK;
68129590Smarius	while ((opt = getopt(argc, argv, "-a")) != -1) {
69129590Smarius		switch (opt) {
70129590Smarius		case '-':
71129590Smarius			if (aflag)
72129590Smarius				usage();
73129590Smarius			do_stdin = 1;
74129590Smarius			break;
75129590Smarius		case 'a':
76129590Smarius			if (do_stdin)
77129590Smarius				usage();
78129590Smarius			aflag = 1;
79129590Smarius			break;
80129590Smarius		case '?':
81129590Smarius		default:
82129590Smarius			usage();
83129590Smarius			/* NOTREACHED */
84129590Smarius		}
85129590Smarius	}
86129590Smarius	argc -= optind;
87129590Smarius	argv += optind;
88129590Smarius
89129590Smarius	if (aflag) {
90129590Smarius		if (argc != 0)
91129590Smarius			usage();
92129590Smarius		dump_config();
93129590Smarius	} else {
94129590Smarius		if (do_stdin) {
95129590Smarius			while (fgets(line, BUFSIZ, stdin) != NULL &&
96129590Smarius			    rv == EX_OK) {
97129590Smarius				if (line[0] == '\n')
98129590Smarius					continue;
99129590Smarius				if ((cp = strrchr(line, '\n')) != NULL)
100129590Smarius					*cp = '\0';
101129590Smarius				rv = action(line);
102129590Smarius			}
103129590Smarius			if (ferror(stdin))
104129590Smarius				err(EX_NOINPUT, "stdin");
105129590Smarius		} else {
106129590Smarius			if (argc == 0)
107129590Smarius				usage();
108129590Smarius			while (argc && rv == EX_OK) {
109129590Smarius				rv = action(*argv);
110129590Smarius				++argv;
111129590Smarius				--argc;
112129590Smarius			}
113129590Smarius		}
114129590Smarius	}
115129590Smarius	return (rv);
116129590Smarius}
117129590Smarius
118129590Smariusstatic int
119129590Smariusaction(char *line)
120129590Smarius{
121129590Smarius	int rv;
122129590Smarius	char *keyword, *arg;
123129590Smarius
124129590Smarius	keyword = strdup(line);
125129590Smarius	if (keyword == NULL)
126129590Smarius		err(EX_OSERR, "malloc() failed");
127129590Smarius	if ((arg = strrchr(keyword, '=')) != NULL)
128129590Smarius		*arg++ = '\0';
129129590Smarius	switch (rv = ofwo_action(keyword, arg)) {
130129590Smarius		case EX_UNAVAILABLE:
131129590Smarius			warnx("nothing available for '%s'.", keyword);
132129590Smarius			break;
133129590Smarius		case EX_DATAERR:
134129590Smarius			warnx("invalid value '%s' for '%s'.", arg, keyword);
135129590Smarius			break;
136129590Smarius	}
137129590Smarius	free(keyword);
138129590Smarius	return(rv);
139129590Smarius}
140129590Smarius
141129590Smariusstatic void
142201227Seddump_config(void)
143129590Smarius{
144129590Smarius
145129590Smarius	ofwo_dump();
146129590Smarius}
147