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