1 /* $OpenBSD: vars.c,v 1.1 2019/07/17 14:36:32 visa Exp $ */
2
3 /*
4 * Copyright (c) 1998-2000 Michael Shalayeff
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 */
29
30 #include <sys/param.h>
31 #include <sys/reboot.h>
32
33 #include <limits.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #include "cmd.h"
39
40 extern int debug;
41 int db_console = -1;
42
43 static int Xdevice(void);
44 #ifdef DEBUG
45 static int Xdebug(void);
46 #endif
47 static int Xdb_console(void);
48 static int Ximage(void);
49 static int Xhowto(void);
50 static int Xtimeout(void);
51 int Xset(void);
52
53 const struct cmd_table cmd_set[] = {
54 {"howto", CMDT_VAR, Xhowto},
55 #ifdef DEBUG
56 {"debug", CMDT_VAR, Xdebug},
57 #endif
58 {"device", CMDT_VAR, Xdevice},
59 {"image", CMDT_VAR, Ximage},
60 {"timeout",CMDT_VAR, Xtimeout},
61 {"db_console", CMDT_VAR, Xdb_console},
62 {NULL,0}
63 };
64
65 #ifdef DEBUG
66 static int
Xdebug(void)67 Xdebug(void)
68 {
69 if (cmd.argc != 2)
70 printf( "o%s\n", debug? "n": "ff" );
71 else
72 debug = (cmd.argv[1][0] == '0' ||
73 (cmd.argv[1][0] == 'o' && cmd.argv[1][1] == 'f'))?
74 0: 1;
75 return 0;
76 }
77 #endif
78
79 int
Xdb_console(void)80 Xdb_console(void)
81 {
82 if (cmd.argc != 2) {
83 switch (db_console) {
84 case 0:
85 printf("off\n");
86 break;
87 case 1:
88 printf("on\n");
89 break;
90 default:
91 printf("unset\n");
92 break;
93 }
94 } else {
95 if (strcmp(cmd.argv[1], "0") == 0 ||
96 strcmp(cmd.argv[1], "off") == 0)
97 db_console = 0;
98 else if (strcmp(cmd.argv[1], "1") == 0 ||
99 strcmp(cmd.argv[1], "on") == 0)
100 db_console = 1;
101 }
102
103 return (0);
104 }
105
106 static int
Xtimeout(void)107 Xtimeout(void)
108 {
109 if (cmd.argc != 2)
110 printf( "%d\n", cmd.timeout );
111 else
112 cmd.timeout = (int)strtol( cmd.argv[1], (char **)NULL, 0 );
113 return 0;
114 }
115
116 /* called only w/ no arguments */
117 int
Xset(void)118 Xset(void)
119 {
120 const struct cmd_table *ct;
121
122 printf("boot\n");
123 for (ct = cmd_set; ct->cmd_name != NULL; ct++) {
124 printf("%s\t ", ct->cmd_name);
125 (*ct->cmd_exec)();
126 }
127 return 0;
128 }
129
130 static int
Xdevice(void)131 Xdevice(void)
132 {
133 if (cmd.argc != 2)
134 printf("%s\n", cmd.bootdev);
135 else
136 strlcpy(cmd.bootdev, cmd.argv[1], sizeof(cmd.bootdev));
137 return 0;
138 }
139
140 static int
Ximage(void)141 Ximage(void)
142 {
143 if (cmd.argc != 2)
144 printf("%s\n", cmd.image);
145 else
146 strlcpy(cmd.image, cmd.argv[1], sizeof(cmd.image));
147 return 0;
148 }
149
150 static int
Xhowto(void)151 Xhowto(void)
152 {
153 if (cmd.argc == 1) {
154 if (cmd.boothowto) {
155 putchar('-');
156 if (cmd.boothowto & RB_ASKNAME)
157 putchar('a');
158 if (cmd.boothowto & RB_CONFIG)
159 putchar('c');
160 if (cmd.boothowto & RB_SINGLE)
161 putchar('s');
162 if (cmd.boothowto & RB_KDB)
163 putchar('d');
164 }
165 putchar('\n');
166 } else
167 bootparse(1);
168 return 0;
169 }
170
171 int
bootparse(int i)172 bootparse(int i)
173 {
174 char *cp;
175 int howto = cmd.boothowto;
176
177 for (; i < cmd.argc; i++) {
178 cp = cmd.argv[i];
179 if (*cp == '-') {
180 while (*++cp) {
181 switch (*cp) {
182 case 'a':
183 howto |= RB_ASKNAME;
184 break;
185 case 'c':
186 howto |= RB_CONFIG;
187 break;
188 case 's':
189 howto |= RB_SINGLE;
190 break;
191 case 'd':
192 howto |= RB_KDB;
193 break;
194 default:
195 printf("howto: bad option: %c\n", *cp);
196 return 1;
197 }
198 }
199 }
200 }
201 cmd.boothowto = howto;
202 return 0;
203 }
204