xref: /openbsd-src/usr.sbin/config/cmd.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: cmd.c,v 1.16 2009/06/03 21:42:16 beck 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 #ifndef LINT
28 static char rcsid[] = "$OpenBSD: cmd.c,v 1.16 2009/06/03 21:42:16 beck Exp $";
29 #endif
30 
31 #include <sys/types.h>
32 #include <sys/device.h>
33 #include <sys/time.h>
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <limits.h>
37 #include <nlist.h>
38 #include <string.h>
39 #include "misc.h"
40 #define	CMD_NOEXTERN
41 #include "cmd.h"
42 #include "ukc.h"
43 #include "exec.h"
44 
45 extern int ukc_mod_kernel;
46 static void int_variable_adjust(const cmd_t *, int, const char *);
47 
48 /* Our command table */
49 cmd_table_t cmd_table[] = {
50 	{"help",   Xhelp,	"",		"Command help list"},
51 	{"add",	   Xadd,	"dev",		"Add a device"},
52 	{"base",   Xbase,	"8|10|16",	"Base on large numbers"},
53 	{"change", Xchange,	"devno|dev",	"Change device"},
54 	{"disable",Xdisable,	"attr val|devno|dev",	"Disable device"},
55 	{"enable", Xenable,	"attr val|devno|dev",	"Enable device"},
56 	{"find",   Xfind,	"devno|dev",	"Find device"},
57 	{"list",   Xlist,	"",		"List configuration"},
58 	{"lines",  Xlines,	"count",	"# of lines per page"},
59 	{"show",   Xshow,	"[attr [val]]",	"Show attribute"},
60 	{"exit",   Xexit,	"",		"Exit, without saving changes"},
61 	{"quit",   Xquit,	"",		"Quit, saving current changes"},
62 	{"timezone", Xtimezone,	"[mins [dst]]",	"Show/change timezone"},
63 	{"bufcachepercent", Xbufcachepct, "[number]",
64 	 "Show/change BUFCACHEPERCENT"},
65 	{"nkmempg", Xnkmempg,	"[number]",	"Show/change NKMEMPAGES"},
66 	{"shmseg", Xshmseg,	"[number]",	"Show/change SHMSEG"},
67 	{"shmmaxpgs", Xshmmaxpgs,"[number]",	"Show/change SHMMAXPGS"},
68 	{NULL,     NULL,	NULL,		NULL}
69 };
70 
71 int
72 Xhelp(cmd_t *cmd)
73 {
74 	cmd_table_t *cmd_table = cmd->table;
75 	int i;
76 
77 	/* Hmm, print out cmd_table here... */
78 	for (i = 0; cmd_table[i].cmd != NULL; i++)
79 		printf("\t%-16s%-20s%s\n", cmd_table[i].cmd,
80 		    cmd_table[i].opt, cmd_table[i].help);
81 	return (CMD_CONT);
82 }
83 
84 int
85 Xadd(cmd_t *cmd)
86 {
87 	short unit, state;
88 	int a;
89 
90 	if (strlen(cmd->args) == 0)
91 		printf("Dev expected\n");
92 	else if (device(cmd->args, &a, &unit, &state) == 0)
93 		add(cmd->args, a, unit, state);
94 	else
95 		printf("Unknown argument\n");
96 	return (CMD_CONT);
97 }
98 
99 int
100 Xbase(cmd_t *cmd)
101 {
102 	int a;
103 
104 	if (strlen(cmd->args) == 0)
105 		printf("8|10|16 expected\n");
106 	else if (number(&cmd->args[0], &a) == 0) {
107 		if (a == 8 || a == 10 || a == 16) {
108 			base = a;
109 		} else {
110 			printf("8|10|16 expected\n");
111 		}
112 	} else
113 		printf("Unknown argument\n");
114 	return (CMD_CONT);
115 }
116 
117 int
118 Xchange(cmd_t *cmd)
119 {
120 	short unit, state;
121 	int a;
122 
123 	if (strlen(cmd->args) == 0)
124 		printf("DevNo or Dev expected\n");
125 	else if (number(cmd->args, &a) == 0)
126 		change(a);
127 	else if (device(cmd->args, &a, &unit, &state) == 0)
128 		common_dev(cmd->args, a, unit, state, UC_CHANGE);
129 	else
130 		printf("Unknown argument\n");
131 	return (CMD_CONT);
132 }
133 
134 int
135 Xdisable(cmd_t *cmd)
136 {
137 	short unit, state;
138 	int a;
139 
140 	if (strlen(cmd->args) == 0)
141 		printf("Attr, DevNo or Dev expected\n");
142 	else if (attr(cmd->args, &a) == 0)
143 		common_attr(cmd->args, a, UC_DISABLE);
144 	else if (number(cmd->args, &a) == 0)
145 		disable(a);
146 	else if (device(cmd->args, &a, &unit, &state) == 0)
147 		common_dev(cmd->args, a, unit, state, UC_DISABLE);
148 	else
149 		printf("Unknown argument\n");
150 	return (CMD_CONT);
151 }
152 
153 int
154 Xenable(cmd_t *cmd)
155 {
156 	short unit, state;
157 	int a;
158 
159 	if (strlen(cmd->args) == 0)
160 		printf("Attr, DevNo or Dev expected\n");
161 	else if (attr(cmd->args, &a) == 0)
162 		common_attr(cmd->args, a, UC_ENABLE);
163 	else if (number(cmd->args, &a) == 0)
164 		enable(a);
165 	else if (device(cmd->args, &a, &unit, &state) == 0)
166 		common_dev(cmd->args, a, unit, state, UC_ENABLE);
167 	else
168 		printf("Unknown argument\n");
169 	return (CMD_CONT);
170 }
171 
172 int
173 Xfind(cmd_t *cmd)
174 {
175 	short unit, state;
176 	int a;
177 
178 	if (strlen(cmd->args) == 0)
179 		printf("DevNo or Dev expected\n");
180 	else if (number(cmd->args, &a) == 0)
181 		pdev(a);
182 	else if (device(cmd->args, &a, &unit, &state) == 0)
183 		common_dev(cmd->args, a, unit, state, UC_FIND);
184 	else
185 		printf("Unknown argument\n");
186 	return (CMD_CONT);
187 }
188 
189 int
190 Xlines(cmd_t *cmd)
191 {
192 	int a;
193 
194 	if (strlen(cmd->args) == 0)
195 		printf("Argument expected\n");
196 	else if (number(cmd->args, &a) == 0)
197 		lines = a;
198 	else
199 		printf("Unknown argument\n");
200 	return (CMD_CONT);
201 }
202 
203 int
204 Xlist(cmd_t *cmd)
205 {
206 	struct cfdata *cd;
207 	int	i = 0;
208 
209 	cnt = 0;
210 	cd = get_cfdata(0);
211 
212 	while (cd->cf_attach != 0) {
213 		if (more())
214 			break;
215 		pdev(i++);
216 		cd++;
217 	}
218 
219 	if (nopdev == 0) {
220 		while (i <= (totdev+maxpseudo)) {
221 			if (more())
222 				break;
223 			pdev(i++);
224 		}
225 	}
226 	cnt = -1;
227 	return (CMD_CONT);
228 }
229 
230 int
231 Xshow(cmd_t *cmd)
232 {
233 	if (strlen(cmd->args) == 0)
234 		show();
235 	else
236 		show_attr(&cmd->args[0]);
237 	return (CMD_CONT);
238 }
239 
240 int
241 Xquit(cmd_t *cmd)
242 {
243 	/* Nothing to do here */
244 	return (CMD_SAVE);
245 }
246 
247 int
248 Xexit(cmd_t *cmd)
249 {
250 	/* Nothing to do here */
251 	return (CMD_EXIT);
252 }
253 
254 int
255 Xtimezone(cmd_t *cmd)
256 {
257 	struct timezone *tz;
258 	int	num;
259 	char	*c;
260 
261 	ukc_mod_kernel = 1;
262 	tz = (struct timezone *)adjust((caddr_t)(nl[TZ_TZ].n_value));
263 
264 	if (strlen(cmd->args) == 0) {
265 		printf("timezone = %d, dst = %d\n",
266 		    tz->tz_minuteswest, tz->tz_dsttime);
267 	} else {
268 		if (number(cmd->args, &num) == 0) {
269 			tz->tz_minuteswest = num;
270 			c = cmd->args;
271 			while ((*c != '\0') && !isspace(*c))
272 				c++;
273 			while (isspace(*c))
274 				c++;
275 			if (strlen(c) != 0 && number(c, &num) == 0)
276 				tz->tz_dsttime = num;
277 			printf("timezone = %d, dst = %d\n",
278 			    tz->tz_minuteswest, tz->tz_dsttime);
279 		} else
280 			printf("Unknown argument\n");
281 	}
282 	return (CMD_CONT);
283 }
284 
285 void
286 int_variable_adjust(const cmd_t *cmd, int idx, const char *name)
287 {
288 	int *v, num;
289 
290 	if (nl[idx].n_type != 0) {
291 		ukc_mod_kernel = 1;
292 
293 		v = (int *)adjust((caddr_t)(nl[idx].n_value));
294 
295 		if (strlen(cmd->args) == 0) {
296 			printf("%s = %d\n", name, *v);
297 		} else {
298 			if (number(cmd->args, &num) == 0) {
299 				*v = num;
300 				printf("%s = %d\n", name, *v);
301 			} else
302 				printf("Unknown argument\n");
303 		}
304 	} else
305 		printf("This kernel does not support modification of %s.\n",
306 		    name);
307 }
308 
309 int
310 Xbufcachepct(cmd_t *cmd)
311 {
312 	int_variable_adjust(cmd, I_BUFCACHEPCT, "bufcachepercent");
313 	return (CMD_CONT);
314 }
315 
316 int
317 Xnkmempg(cmd_t *cmd)
318 {
319 	int_variable_adjust(cmd, I_NKMEMPG, "nkmempages");
320 	return (CMD_CONT);
321 }
322 
323 int
324 Xshmseg(cmd_t *cmd)
325 {
326 	int_variable_adjust(cmd, I_SHMSEG, "shmseg");
327 	return (CMD_CONT);
328 }
329 
330 int
331 Xshmmaxpgs(cmd_t *cmd)
332 {
333 	int_variable_adjust(cmd, I_SHMMAXPGS, "shmmaxpgs");
334 	return (CMD_CONT);
335 }
336