xref: /openbsd-src/usr.sbin/config/cmd.c (revision 97dabcf62b33aa49ed8aca4783a24418baa9677f)
1*97dabcf6Scheloha /*	$OpenBSD: cmd.c,v 1.22 2019/09/06 21:30:31 cheloha Exp $ */
235319decSderaadt 
335319decSderaadt /*
42b06a591Smaja  * Copyright (c) 1999-2001 Mats O Jansson.  All rights reserved.
535319decSderaadt  *
635319decSderaadt  * Redistribution and use in source and binary forms, with or without
735319decSderaadt  * modification, are permitted provided that the following conditions
835319decSderaadt  * are met:
935319decSderaadt  * 1. Redistributions of source code must retain the above copyright
1035319decSderaadt  *    notice, this list of conditions and the following disclaimer.
1135319decSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
1235319decSderaadt  *    notice, this list of conditions and the following disclaimer in the
1335319decSderaadt  *    documentation and/or other materials provided with the distribution.
1435319decSderaadt  *
1535319decSderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1635319decSderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1735319decSderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1835319decSderaadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1935319decSderaadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2035319decSderaadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2135319decSderaadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2235319decSderaadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2335319decSderaadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2435319decSderaadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2535319decSderaadt  */
2635319decSderaadt 
277bfdb724Sderaadt #include <sys/types.h>
287bfdb724Sderaadt #include <sys/device.h>
29c6b62359Sedd 
3015377c9bSd #include <ctype.h>
3135319decSderaadt #include <limits.h>
3235319decSderaadt #include <nlist.h>
33c6b62359Sedd #include <stdio.h>
347bfdb724Sderaadt #include <string.h>
35c6b62359Sedd 
3635319decSderaadt #include "misc.h"
3735319decSderaadt #define	CMD_NOEXTERN
3835319decSderaadt #include "cmd.h"
3935319decSderaadt #include "ukc.h"
407bfdb724Sderaadt #include "exec.h"
4135319decSderaadt 
422b06a591Smaja extern int ukc_mod_kernel;
43660f2848Sespie static void int_variable_adjust(const cmd_t *, int, const char *);
442b06a591Smaja 
4535319decSderaadt /* Our command table */
4635319decSderaadt cmd_table_t cmd_table[] = {
47660f2848Sespie 	{"help",   Xhelp,	"",		"Command help list"},
48660f2848Sespie 	{"add",	   Xadd,	"dev",		"Add a device"},
49660f2848Sespie 	{"base",   Xbase,	"8|10|16",	"Base on large numbers"},
50660f2848Sespie 	{"change", Xchange,	"devno|dev",	"Change device"},
5135319decSderaadt 	{"disable",Xdisable,	"attr val|devno|dev",	"Disable device"},
5235319decSderaadt 	{"enable", Xenable,	"attr val|devno|dev",	"Enable device"},
53660f2848Sespie 	{"find",   Xfind,	"devno|dev",	"Find device"},
54660f2848Sespie 	{"list",   Xlist,	"",		"List configuration"},
55660f2848Sespie 	{"lines",  Xlines,	"count",	"# of lines per page"},
56660f2848Sespie 	{"show",   Xshow,	"[attr [val]]",	"Show attribute"},
57660f2848Sespie 	{"exit",   Xexit,	"",		"Exit, without saving changes"},
58660f2848Sespie 	{"quit",   Xquit,	"",		"Quit, saving current changes"},
59660f2848Sespie 	{"nkmempg", Xnkmempg,	"[number]",	"Show/change NKMEMPAGES"},
6035319decSderaadt 	{NULL,     NULL,	NULL,		NULL}
6135319decSderaadt };
6235319decSderaadt 
6335319decSderaadt int
Xhelp(cmd_t * cmd)64660f2848Sespie Xhelp(cmd_t *cmd)
6535319decSderaadt {
6635319decSderaadt 	cmd_table_t *cmd_table = cmd->table;
6735319decSderaadt 	int i;
6835319decSderaadt 
6935319decSderaadt 	/* Hmm, print out cmd_table here... */
7035319decSderaadt 	for (i = 0; cmd_table[i].cmd != NULL; i++)
719d0fcf29Sbeck 		printf("\t%-16s%-20s%s\n", cmd_table[i].cmd,
7235319decSderaadt 		    cmd_table[i].opt, cmd_table[i].help);
7335319decSderaadt 	return (CMD_CONT);
7435319decSderaadt }
7535319decSderaadt 
7635319decSderaadt int
Xadd(cmd_t * cmd)77660f2848Sespie Xadd(cmd_t *cmd)
7835319decSderaadt {
7935319decSderaadt 	short unit, state;
80a8d5091fSderaadt 	int a;
8135319decSderaadt 
8235319decSderaadt 	if (strlen(cmd->args) == 0)
8335319decSderaadt 		printf("Dev expected\n");
8435319decSderaadt 	else if (device(cmd->args, &a, &unit, &state) == 0)
8535319decSderaadt 		add(cmd->args, a, unit, state);
8635319decSderaadt 	else
8735319decSderaadt 		printf("Unknown argument\n");
8835319decSderaadt 	return (CMD_CONT);
8935319decSderaadt }
9035319decSderaadt 
9135319decSderaadt int
Xbase(cmd_t * cmd)92660f2848Sespie Xbase(cmd_t *cmd)
9335319decSderaadt {
9435319decSderaadt 	int a;
9535319decSderaadt 
9635319decSderaadt 	if (strlen(cmd->args) == 0)
9735319decSderaadt 		printf("8|10|16 expected\n");
9835319decSderaadt 	else if (number(&cmd->args[0], &a) == 0) {
9935319decSderaadt 		if (a == 8 || a == 10 || a == 16) {
10035319decSderaadt 			base = a;
10135319decSderaadt 		} else {
10235319decSderaadt 			printf("8|10|16 expected\n");
10335319decSderaadt 		}
10435319decSderaadt 	} else
10535319decSderaadt 		printf("Unknown argument\n");
10635319decSderaadt 	return (CMD_CONT);
10735319decSderaadt }
10835319decSderaadt 
10935319decSderaadt int
Xchange(cmd_t * cmd)110660f2848Sespie Xchange(cmd_t *cmd)
11135319decSderaadt {
11235319decSderaadt 	short unit, state;
113a8d5091fSderaadt 	int a;
11435319decSderaadt 
11535319decSderaadt 	if (strlen(cmd->args) == 0)
11635319decSderaadt 		printf("DevNo or Dev expected\n");
11735319decSderaadt 	else if (number(cmd->args, &a) == 0)
11835319decSderaadt 		change(a);
11935319decSderaadt 	else if (device(cmd->args, &a, &unit, &state) == 0)
12035319decSderaadt 		common_dev(cmd->args, a, unit, state, UC_CHANGE);
12135319decSderaadt 	else
12235319decSderaadt 		printf("Unknown argument\n");
12335319decSderaadt 	return (CMD_CONT);
12435319decSderaadt }
12535319decSderaadt 
12635319decSderaadt int
Xdisable(cmd_t * cmd)127660f2848Sespie Xdisable(cmd_t *cmd)
12835319decSderaadt {
12935319decSderaadt 	short unit, state;
130a8d5091fSderaadt 	int a;
13135319decSderaadt 
13235319decSderaadt 	if (strlen(cmd->args) == 0)
13335319decSderaadt 		printf("Attr, DevNo or Dev expected\n");
13435319decSderaadt 	else if (attr(cmd->args, &a) == 0)
13535319decSderaadt 		common_attr(cmd->args, a, UC_DISABLE);
13635319decSderaadt 	else if (number(cmd->args, &a) == 0)
13735319decSderaadt 		disable(a);
13835319decSderaadt 	else if (device(cmd->args, &a, &unit, &state) == 0)
13935319decSderaadt 		common_dev(cmd->args, a, unit, state, UC_DISABLE);
14035319decSderaadt 	else
14135319decSderaadt 		printf("Unknown argument\n");
14235319decSderaadt 	return (CMD_CONT);
14335319decSderaadt }
14435319decSderaadt 
14535319decSderaadt int
Xenable(cmd_t * cmd)146660f2848Sespie Xenable(cmd_t *cmd)
14735319decSderaadt {
14835319decSderaadt 	short unit, state;
149a8d5091fSderaadt 	int a;
15035319decSderaadt 
15135319decSderaadt 	if (strlen(cmd->args) == 0)
15235319decSderaadt 		printf("Attr, DevNo or Dev expected\n");
15335319decSderaadt 	else if (attr(cmd->args, &a) == 0)
1542ef21be0Smaja 		common_attr(cmd->args, a, UC_ENABLE);
15535319decSderaadt 	else if (number(cmd->args, &a) == 0)
15635319decSderaadt 		enable(a);
15735319decSderaadt 	else if (device(cmd->args, &a, &unit, &state) == 0)
15835319decSderaadt 		common_dev(cmd->args, a, unit, state, UC_ENABLE);
15935319decSderaadt 	else
16035319decSderaadt 		printf("Unknown argument\n");
16135319decSderaadt 	return (CMD_CONT);
16235319decSderaadt }
16335319decSderaadt 
16435319decSderaadt int
Xfind(cmd_t * cmd)165660f2848Sespie Xfind(cmd_t *cmd)
16635319decSderaadt {
16735319decSderaadt 	short unit, state;
168a8d5091fSderaadt 	int a;
16935319decSderaadt 
17035319decSderaadt 	if (strlen(cmd->args) == 0)
17135319decSderaadt 		printf("DevNo or Dev expected\n");
17235319decSderaadt 	else if (number(cmd->args, &a) == 0)
17335319decSderaadt 		pdev(a);
17435319decSderaadt 	else if (device(cmd->args, &a, &unit, &state) == 0)
17535319decSderaadt 		common_dev(cmd->args, a, unit, state, UC_FIND);
17635319decSderaadt 	else
17735319decSderaadt 		printf("Unknown argument\n");
17835319decSderaadt 	return (CMD_CONT);
17935319decSderaadt }
18035319decSderaadt 
18135319decSderaadt int
Xlines(cmd_t * cmd)182660f2848Sespie Xlines(cmd_t *cmd)
18335319decSderaadt {
18435319decSderaadt 	int a;
18535319decSderaadt 
18635319decSderaadt 	if (strlen(cmd->args) == 0)
18735319decSderaadt 		printf("Argument expected\n");
18835319decSderaadt 	else if (number(cmd->args, &a) == 0)
18935319decSderaadt 		lines = a;
19035319decSderaadt 	else
19135319decSderaadt 		printf("Unknown argument\n");
19235319decSderaadt 	return (CMD_CONT);
19335319decSderaadt }
19435319decSderaadt 
19535319decSderaadt int
Xlist(cmd_t * cmd)196660f2848Sespie Xlist(cmd_t *cmd)
19735319decSderaadt {
19835319decSderaadt 	struct cfdata *cd;
199a8d5091fSderaadt 	int	i = 0;
20035319decSderaadt 
20135319decSderaadt 	cnt = 0;
20235319decSderaadt 	cd = get_cfdata(0);
20335319decSderaadt 
20435319decSderaadt 	while (cd->cf_attach != 0) {
20535319decSderaadt 		if (more())
20635319decSderaadt 			break;
20735319decSderaadt 		pdev(i++);
20835319decSderaadt 		cd++;
20935319decSderaadt 	}
21035319decSderaadt 
211ae39f4b4Smaja 	if (nopdev == 0) {
212ae39f4b4Smaja 		while (i <= (totdev+maxpseudo)) {
213ae39f4b4Smaja 			if (more())
214ae39f4b4Smaja 				break;
215ae39f4b4Smaja 			pdev(i++);
216ae39f4b4Smaja 		}
217ae39f4b4Smaja 	}
21835319decSderaadt 	cnt = -1;
21935319decSderaadt 	return (CMD_CONT);
22035319decSderaadt }
22135319decSderaadt 
22235319decSderaadt int
Xshow(cmd_t * cmd)223660f2848Sespie Xshow(cmd_t *cmd)
22435319decSderaadt {
22535319decSderaadt 	if (strlen(cmd->args) == 0)
22635319decSderaadt 		show();
22735319decSderaadt 	else
22835319decSderaadt 		show_attr(&cmd->args[0]);
22935319decSderaadt 	return (CMD_CONT);
23035319decSderaadt }
23135319decSderaadt 
23235319decSderaadt int
Xquit(cmd_t * cmd)233660f2848Sespie Xquit(cmd_t *cmd)
23435319decSderaadt {
23535319decSderaadt 	/* Nothing to do here */
23635319decSderaadt 	return (CMD_SAVE);
23735319decSderaadt }
23835319decSderaadt 
23935319decSderaadt int
Xexit(cmd_t * cmd)240660f2848Sespie Xexit(cmd_t *cmd)
24135319decSderaadt {
24235319decSderaadt 	/* Nothing to do here */
24335319decSderaadt 	return (CMD_EXIT);
24435319decSderaadt }
24515377c9bSd 
246660f2848Sespie void
int_variable_adjust(const cmd_t * cmd,int idx,const char * name)247660f2848Sespie int_variable_adjust(const cmd_t *cmd, int idx, const char *name)
248660f2848Sespie {
249660f2848Sespie 	int *v, num;
250660f2848Sespie 
251660f2848Sespie 	if (nl[idx].n_type != 0) {
2525309eac8Smaja 		ukc_mod_kernel = 1;
2535309eac8Smaja 
254660f2848Sespie 		v = (int *)adjust((caddr_t)(nl[idx].n_value));
2555309eac8Smaja 
2565309eac8Smaja 		if (strlen(cmd->args) == 0) {
257660f2848Sespie 			printf("%s = %d\n", name, *v);
2585309eac8Smaja 		} else {
2595309eac8Smaja 			if (number(cmd->args, &num) == 0) {
260660f2848Sespie 				*v = num;
261660f2848Sespie 				printf("%s = %d\n", name, *v);
2625309eac8Smaja 			} else
2635309eac8Smaja 				printf("Unknown argument\n");
2645309eac8Smaja 		}
2655309eac8Smaja 	} else
266660f2848Sespie 		printf("This kernel does not support modification of %s.\n",
267660f2848Sespie 		    name);
268660f2848Sespie }
2695309eac8Smaja 
270660f2848Sespie int
Xnkmempg(cmd_t * cmd)271660f2848Sespie Xnkmempg(cmd_t *cmd)
2725309eac8Smaja {
273660f2848Sespie 	int_variable_adjust(cmd, I_NKMEMPG, "nkmempages");
2745309eac8Smaja 	return (CMD_CONT);
2755309eac8Smaja }
276