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