xref: /openbsd-src/usr.sbin/config/cmd.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: cmd.c,v 1.18 2009/12/10 22:07:19 kettenis Exp $ */
2 
3 /*
4  * Copyright (c) 1999-2001 Mats O Jansson.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/device.h>
29 #include <sys/time.h>
30 #include <ctype.h>
31 #include <stdio.h>
32 #include <limits.h>
33 #include <nlist.h>
34 #include <string.h>
35 #include "misc.h"
36 #define	CMD_NOEXTERN
37 #include "cmd.h"
38 #include "ukc.h"
39 #include "exec.h"
40 
41 extern int ukc_mod_kernel;
42 static void int_variable_adjust(const cmd_t *, int, const char *);
43 
44 /* Our command table */
45 cmd_table_t cmd_table[] = {
46 	{"help",   Xhelp,	"",		"Command help list"},
47 	{"add",	   Xadd,	"dev",		"Add a device"},
48 	{"base",   Xbase,	"8|10|16",	"Base on large numbers"},
49 	{"change", Xchange,	"devno|dev",	"Change device"},
50 	{"disable",Xdisable,	"attr val|devno|dev",	"Disable device"},
51 	{"enable", Xenable,	"attr val|devno|dev",	"Enable device"},
52 	{"find",   Xfind,	"devno|dev",	"Find device"},
53 	{"list",   Xlist,	"",		"List configuration"},
54 	{"lines",  Xlines,	"count",	"# of lines per page"},
55 	{"show",   Xshow,	"[attr [val]]",	"Show attribute"},
56 	{"exit",   Xexit,	"",		"Exit, without saving changes"},
57 	{"quit",   Xquit,	"",		"Quit, saving current changes"},
58 	{"timezone", Xtimezone,	"[mins [dst]]",	"Show/change timezone"},
59 	{"bufcachepercent", Xbufcachepct, "[number]",
60 	 "Show/change BUFCACHEPERCENT"},
61 	{"nkmempg", Xnkmempg,	"[number]",	"Show/change NKMEMPAGES"},
62 	{NULL,     NULL,	NULL,		NULL}
63 };
64 
65 int
66 Xhelp(cmd_t *cmd)
67 {
68 	cmd_table_t *cmd_table = cmd->table;
69 	int i;
70 
71 	/* Hmm, print out cmd_table here... */
72 	for (i = 0; cmd_table[i].cmd != NULL; i++)
73 		printf("\t%-16s%-20s%s\n", cmd_table[i].cmd,
74 		    cmd_table[i].opt, cmd_table[i].help);
75 	return (CMD_CONT);
76 }
77 
78 int
79 Xadd(cmd_t *cmd)
80 {
81 	short unit, state;
82 	int a;
83 
84 	if (strlen(cmd->args) == 0)
85 		printf("Dev expected\n");
86 	else if (device(cmd->args, &a, &unit, &state) == 0)
87 		add(cmd->args, a, unit, state);
88 	else
89 		printf("Unknown argument\n");
90 	return (CMD_CONT);
91 }
92 
93 int
94 Xbase(cmd_t *cmd)
95 {
96 	int a;
97 
98 	if (strlen(cmd->args) == 0)
99 		printf("8|10|16 expected\n");
100 	else if (number(&cmd->args[0], &a) == 0) {
101 		if (a == 8 || a == 10 || a == 16) {
102 			base = a;
103 		} else {
104 			printf("8|10|16 expected\n");
105 		}
106 	} else
107 		printf("Unknown argument\n");
108 	return (CMD_CONT);
109 }
110 
111 int
112 Xchange(cmd_t *cmd)
113 {
114 	short unit, state;
115 	int a;
116 
117 	if (strlen(cmd->args) == 0)
118 		printf("DevNo or Dev expected\n");
119 	else if (number(cmd->args, &a) == 0)
120 		change(a);
121 	else if (device(cmd->args, &a, &unit, &state) == 0)
122 		common_dev(cmd->args, a, unit, state, UC_CHANGE);
123 	else
124 		printf("Unknown argument\n");
125 	return (CMD_CONT);
126 }
127 
128 int
129 Xdisable(cmd_t *cmd)
130 {
131 	short unit, state;
132 	int a;
133 
134 	if (strlen(cmd->args) == 0)
135 		printf("Attr, DevNo or Dev expected\n");
136 	else if (attr(cmd->args, &a) == 0)
137 		common_attr(cmd->args, a, UC_DISABLE);
138 	else if (number(cmd->args, &a) == 0)
139 		disable(a);
140 	else if (device(cmd->args, &a, &unit, &state) == 0)
141 		common_dev(cmd->args, a, unit, state, UC_DISABLE);
142 	else
143 		printf("Unknown argument\n");
144 	return (CMD_CONT);
145 }
146 
147 int
148 Xenable(cmd_t *cmd)
149 {
150 	short unit, state;
151 	int a;
152 
153 	if (strlen(cmd->args) == 0)
154 		printf("Attr, DevNo or Dev expected\n");
155 	else if (attr(cmd->args, &a) == 0)
156 		common_attr(cmd->args, a, UC_ENABLE);
157 	else if (number(cmd->args, &a) == 0)
158 		enable(a);
159 	else if (device(cmd->args, &a, &unit, &state) == 0)
160 		common_dev(cmd->args, a, unit, state, UC_ENABLE);
161 	else
162 		printf("Unknown argument\n");
163 	return (CMD_CONT);
164 }
165 
166 int
167 Xfind(cmd_t *cmd)
168 {
169 	short unit, state;
170 	int a;
171 
172 	if (strlen(cmd->args) == 0)
173 		printf("DevNo or Dev expected\n");
174 	else if (number(cmd->args, &a) == 0)
175 		pdev(a);
176 	else if (device(cmd->args, &a, &unit, &state) == 0)
177 		common_dev(cmd->args, a, unit, state, UC_FIND);
178 	else
179 		printf("Unknown argument\n");
180 	return (CMD_CONT);
181 }
182 
183 int
184 Xlines(cmd_t *cmd)
185 {
186 	int a;
187 
188 	if (strlen(cmd->args) == 0)
189 		printf("Argument expected\n");
190 	else if (number(cmd->args, &a) == 0)
191 		lines = a;
192 	else
193 		printf("Unknown argument\n");
194 	return (CMD_CONT);
195 }
196 
197 int
198 Xlist(cmd_t *cmd)
199 {
200 	struct cfdata *cd;
201 	int	i = 0;
202 
203 	cnt = 0;
204 	cd = get_cfdata(0);
205 
206 	while (cd->cf_attach != 0) {
207 		if (more())
208 			break;
209 		pdev(i++);
210 		cd++;
211 	}
212 
213 	if (nopdev == 0) {
214 		while (i <= (totdev+maxpseudo)) {
215 			if (more())
216 				break;
217 			pdev(i++);
218 		}
219 	}
220 	cnt = -1;
221 	return (CMD_CONT);
222 }
223 
224 int
225 Xshow(cmd_t *cmd)
226 {
227 	if (strlen(cmd->args) == 0)
228 		show();
229 	else
230 		show_attr(&cmd->args[0]);
231 	return (CMD_CONT);
232 }
233 
234 int
235 Xquit(cmd_t *cmd)
236 {
237 	/* Nothing to do here */
238 	return (CMD_SAVE);
239 }
240 
241 int
242 Xexit(cmd_t *cmd)
243 {
244 	/* Nothing to do here */
245 	return (CMD_EXIT);
246 }
247 
248 int
249 Xtimezone(cmd_t *cmd)
250 {
251 	struct timezone *tz;
252 	int	num;
253 	char	*c;
254 
255 	ukc_mod_kernel = 1;
256 	tz = (struct timezone *)adjust((caddr_t)(nl[TZ_TZ].n_value));
257 
258 	if (strlen(cmd->args) == 0) {
259 		printf("timezone = %d, dst = %d\n",
260 		    tz->tz_minuteswest, tz->tz_dsttime);
261 	} else {
262 		if (number(cmd->args, &num) == 0) {
263 			tz->tz_minuteswest = num;
264 			c = cmd->args;
265 			while ((*c != '\0') && !isspace(*c))
266 				c++;
267 			while (isspace(*c))
268 				c++;
269 			if (strlen(c) != 0 && number(c, &num) == 0)
270 				tz->tz_dsttime = num;
271 			printf("timezone = %d, dst = %d\n",
272 			    tz->tz_minuteswest, tz->tz_dsttime);
273 		} else
274 			printf("Unknown argument\n");
275 	}
276 	return (CMD_CONT);
277 }
278 
279 void
280 int_variable_adjust(const cmd_t *cmd, int idx, const char *name)
281 {
282 	int *v, num;
283 
284 	if (nl[idx].n_type != 0) {
285 		ukc_mod_kernel = 1;
286 
287 		v = (int *)adjust((caddr_t)(nl[idx].n_value));
288 
289 		if (strlen(cmd->args) == 0) {
290 			printf("%s = %d\n", name, *v);
291 		} else {
292 			if (number(cmd->args, &num) == 0) {
293 				*v = num;
294 				printf("%s = %d\n", name, *v);
295 			} else
296 				printf("Unknown argument\n");
297 		}
298 	} else
299 		printf("This kernel does not support modification of %s.\n",
300 		    name);
301 }
302 
303 int
304 Xbufcachepct(cmd_t *cmd)
305 {
306 	int_variable_adjust(cmd, I_BUFCACHEPCT, "bufcachepercent");
307 	return (CMD_CONT);
308 }
309 
310 int
311 Xnkmempg(cmd_t *cmd)
312 {
313 	int_variable_adjust(cmd, I_NKMEMPG, "nkmempages");
314 	return (CMD_CONT);
315 }
316