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