summaryrefslogtreecommitdiffstats
path: root/usr.bin/devmenu/devfilter.c
blob: 873f87fa55389f8cb3c139fb785416d6d56035f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright 1995 Massachusetts Institute of Technology
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that both the above copyright notice and this
 * permission notice appear in all copies, that both the above
 * copyright notice and this permission notice appear in all
 * supporting documentation, and that the name of M.I.T. not be used
 * in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  M.I.T. makes
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied
 * warranty.
 * 
 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/param.h>
#include <sys/devconf.h>
#include <sys/proc.h>
#include <vm/vm.h>
#include <sys/sysctl.h>
#include <err.h>
#include <sysexits.h>

#include "devmenu.h"

char *
devmenu_toname(const struct devconf *dev)
{
	static char buf[2*MAXDEVNAME];
	snprintf(buf, sizeof buf, "%s%d", dev->dc_name, dev->dc_unit);
	return buf;
}

int
devmenu_filter(const struct devconf *dev, char **userlist)
{
	int i;
	char *name;

	if (!userlist)
		return 1;

	name = devmenu_toname(dev);

	for (i = 0; userlist[i]; i++) {
		if (strcmp(name, userlist[i]) == 0) {
			return 1;
		}
	}

	return 0;
}

struct devconf **
devmenu_alldevs(void)
{
	int mib[3];
	size_t size;
	int ndevs, i, ndx;
	struct devconf **rv;

	size = sizeof ndevs;
	mib[0] = CTL_HW;
	mib[1] = HW_DEVCONF;
	mib[2] = DEVCONF_NUMBER;

	if (sysctl(mib, 3, &ndevs, &size, 0, 0) < 0) {
		err(EX_OSERR, "sysctl failed reading hw.devconf.number");
	}

	rv = malloc((ndevs + 1) * sizeof *rv);
	if (!rv) {
		err(EX_UNAVAILABLE, "malloc(%lu)", 
		    (unsigned long)(ndevs * sizeof *rv));
	}

	for (ndx = 0, i = 1; i <= ndevs; i++) {
		mib[2] = i;
		if (sysctl(mib, 3, 0, &size, 0, 0) < 0) {
			continue;
		}

		rv[ndx] = malloc(size);
		if (!rv[ndx]) {
			err(EX_UNAVAILABLE, "malloc(%lu)", 
			    (unsigned long)size);
		}

		if (sysctl(mib, 3, rv[ndx], &size, 0, 0) < 0) {
			err(EX_OSERR, "sysctl reading hw.devconf.%d", i);
		}

		ndx++;
	}

	rv[ndx] = 0;

	return rv;
}

void
devmenu_freedevs(struct devconf ***devpp)
{
	struct devconf **devp = *devpp;
	int i;

	for (i = 0; devp[i]; i++) {
		free(devp[i]);
	}

	free(devp);
	*devpp = 0;
}

const char *
devmenu_common(const char *title, const char *hfile, char **devnames,
	       const char *prompt, const char *none, enum dc_class class,
	       int states)
{
	struct devconf **devs;
	int s;
	unsigned char **items = 0;
	int nitems = 0;
	int itemindex = 0;
	char *name;
	int i;
	static char resbuf[2*MAXDEVNAME];

	if (hfile) {
		use_helpfile((char *)hfile);
	}
	devs = devmenu_alldevs();

	for (i = 0; devs[i]; i++) {
		if (states && !((1 << devs[i]->dc_state) & states))
			continue;
		if (class && !(devs[i]->dc_class & class))
			continue;
		if (devmenu_filter(devs[i], devnames)) {
			++nitems;
			items = realloc(items, 2 * nitems * sizeof *items);
			if (!items) {
				err(EX_UNAVAILABLE, "malloc(%lu)",
				    (unsigned long)(2 * nitems * sizeof *items));
			}

			name = devmenu_toname(devs[i]);

			items[itemindex] = strdup(name);
			if (!items[itemindex]) {
				err(EX_UNAVAILABLE, "strdup-malloc(%lu)",
				    (unsigned long)(strlen(name) + 1));
			}

			items[itemindex + 1] = devs[i]->dc_descr;
			itemindex += 2;
		}
	}

	if (!nitems) {
		dialog_msgbox((char *)title, none, -1, -1, 1);
		return "none";
	}

	name = resbuf;

	if(dialog_menu((char *)title, prompt, 24, 78, 18, nitems, items,
		       resbuf, 0, 0) != 0) {
		name = "none";
	} 

	for (i = 0; i < 2 * nitems; i += 2) {
		free(items[i]);
	}

	free(items);

	devmenu_freedevs(&devs);
	return name;
}
OpenPOWER on IntegriCloud