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