xref: /freebsd/usr.sbin/ofwdump/ofwdump.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
1 /*-
2  * Copyright (c) 2002 by Thomas Moestl <tmm@FreeBSD.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 
33 #include "ofw_util.h"
34 
35 void	usage(void);
36 
37 void
38 usage(void)
39 {
40 
41 	fprintf(stderr,
42 	    "usage: ofwdump -a [-p | -P property] [-R | -S]\n"
43 	    "       ofwdump [-p | -P property] [-r] [-R | -S] [--] nodes\n");
44 	exit(1);
45 }
46 
47 int
48 main(int argc, char *argv[])
49 {
50 	int opt, i, fd;
51 	int aflag, pflag, rflag, Rflag, Sflag;
52 	char *Parg;
53 
54 	aflag = pflag = rflag = Rflag = Sflag = 0;
55 	Parg = NULL;
56 	while ((opt = getopt(argc, argv, "-aprP:RS")) != -1) {
57 		if (opt == '-')
58 			break;
59 		switch (opt) {
60 		case 'a':
61 			aflag = 1;
62 			rflag = 1;
63 			break;
64 		case 'p':
65 			if (Parg != NULL)
66 				usage();
67 			pflag = 1;
68 			break;
69 		case 'r':
70 			rflag = 1;
71 			break;
72 		case 'P':
73 			if (pflag)
74 				usage();
75 			pflag = 1;
76 			Parg = optarg;
77 			break;
78 		case 'R':
79 			if (Sflag)
80 				usage();
81 			Rflag = 1;
82 			break;
83 		case 'S':
84 			if (Rflag)
85 				usage();
86 			Sflag = 1;
87 			break;
88 		default:
89 			usage();
90 		}
91 	}
92 	argc -= optind;
93 	argv += optind;
94 
95 	fd = ofw_open();
96 	if (aflag) {
97 		if (argc != 0)
98 			usage();
99 		ofw_dump(fd, NULL, rflag, pflag, Parg, Rflag, Sflag);
100 	} else {
101 		for (i = 0; i < argc; i++)
102 			ofw_dump(fd, argv[i], rflag, pflag, Parg, Rflag, Sflag);
103 	}
104 	ofw_close(fd);
105 	return (0);
106 }
107