1 /* $OpenBSD: vars.c,v 1.15 2014/07/11 12:33:12 jasper 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 <libsa.h> 32 #include <sys/reboot.h> 33 #include <lib/libkern/funcs.h> 34 #include "cmd.h" 35 36 extern char prog_ident[]; 37 extern int debug; 38 int db_console = -1; 39 40 static int Xaddr(void); 41 static int Xdevice(void); 42 #ifdef DEBUG 43 static int Xdebug(void); 44 #endif 45 static int Xdb_console(void); 46 static int Ximage(void); 47 static int Xhowto(void); 48 static int Xtty(void); 49 static int Xtimeout(void); 50 int Xset(void); 51 int Xenv(void); 52 53 const struct cmd_table cmd_set[] = { 54 {"addr", CMDT_VAR, Xaddr}, 55 {"howto", CMDT_VAR, Xhowto}, 56 #ifdef DEBUG 57 {"debug", CMDT_VAR, Xdebug}, 58 #endif 59 {"device", CMDT_VAR, Xdevice}, 60 {"tty", CMDT_VAR, Xtty}, 61 {"image", CMDT_VAR, Ximage}, 62 {"timeout",CMDT_VAR, Xtimeout}, 63 {"db_console", CMDT_VAR, Xdb_console}, 64 {NULL,0} 65 }; 66 67 #ifdef DEBUG 68 static int 69 Xdebug(void) 70 { 71 if (cmd.argc != 2) 72 printf( "o%s\n", debug? "n": "ff" ); 73 else 74 debug = (cmd.argv[1][0] == '0' || 75 (cmd.argv[1][0] == 'o' && cmd.argv[1][1] == 'f'))? 76 0: 1; 77 return 0; 78 } 79 #endif 80 81 int 82 Xdb_console(void) 83 { 84 if (cmd.argc != 2) { 85 switch (db_console) { 86 case 0: 87 printf("off\n"); 88 break; 89 case 1: 90 printf("on\n"); 91 break; 92 default: 93 printf("unset\n"); 94 break; 95 } 96 } else { 97 if (strcmp(cmd.argv[1], "0") == 0 || 98 strcmp(cmd.argv[1], "off") == 0) 99 db_console = 0; 100 else if (strcmp(cmd.argv[1], "1") == 0 || 101 strcmp(cmd.argv[1], "on") == 0) 102 db_console = 1; 103 } 104 105 return (0); 106 } 107 108 static int 109 Xtimeout(void) 110 { 111 if (cmd.argc != 2) 112 printf( "%d\n", cmd.timeout ); 113 else 114 cmd.timeout = (int)strtol( cmd.argv[1], (char **)NULL, 0 ); 115 return 0; 116 } 117 118 /* called only w/ no arguments */ 119 int 120 Xset(void) 121 { 122 const struct cmd_table *ct; 123 124 printf("%s\n", prog_ident); 125 for (ct = cmd_set; ct->cmd_name != NULL; ct++) { 126 printf("%s\t ", ct->cmd_name); 127 (*ct->cmd_exec)(); 128 } 129 return 0; 130 } 131 132 static int 133 Xdevice(void) 134 { 135 if (cmd.argc != 2) 136 printf("%s\n", cmd.bootdev); 137 else 138 strlcpy(cmd.bootdev, cmd.argv[1], sizeof(cmd.bootdev)); 139 return 0; 140 } 141 142 static int 143 Ximage(void) 144 { 145 if (cmd.argc != 2) 146 printf("%s\n", cmd.image); 147 else 148 strlcpy(cmd.image, cmd.argv[1], sizeof(cmd.image)); 149 return 0; 150 } 151 152 static int 153 Xaddr(void) 154 { 155 if (cmd.argc != 2) 156 printf("%p\n", cmd.addr); 157 else 158 cmd.addr = (void *)strtol(cmd.argv[1], NULL, 0); 159 return 0; 160 } 161 162 static int 163 Xtty(void) 164 { 165 dev_t dev; 166 167 if (cmd.argc != 2) 168 printf("%s\n", ttyname(0)); 169 else { 170 dev = ttydev(cmd.argv[1]); 171 if (dev == NODEV) 172 printf("%s not a console device\n", cmd.argv[1]); 173 else { 174 printf("switching console to %s\n", cmd.argv[1]); 175 if (cnset(dev)) 176 printf("%s console not present\n", 177 cmd.argv[1]); 178 else 179 printf("%s\n", prog_ident); 180 } 181 } 182 return 0; 183 } 184 185 static int 186 Xhowto(void) 187 { 188 if (cmd.argc == 1) { 189 if (cmd.boothowto) { 190 putchar('-'); 191 if (cmd.boothowto & RB_ASKNAME) 192 putchar('a'); 193 if (cmd.boothowto & RB_CONFIG) 194 putchar('c'); 195 if (cmd.boothowto & RB_SINGLE) 196 putchar('s'); 197 if (cmd.boothowto & RB_KDB) 198 putchar('d'); 199 } 200 putchar('\n'); 201 } else 202 bootparse(1); 203 return 0; 204 } 205 206 int 207 bootparse(int i) 208 { 209 char *cp; 210 int howto = cmd.boothowto; 211 212 for (; i < cmd.argc; i++) { 213 cp = cmd.argv[i]; 214 if (*cp == '-') { 215 while (*++cp) { 216 switch (*cp) { 217 case 'a': 218 howto |= RB_ASKNAME; 219 break; 220 case 'c': 221 howto |= RB_CONFIG; 222 break; 223 case 's': 224 howto |= RB_SINGLE; 225 break; 226 case 'd': 227 howto |= RB_KDB; 228 break; 229 default: 230 printf("howto: bad option: %c\n", *cp); 231 return 1; 232 } 233 } 234 } else { 235 printf("boot: illegal argument %s\n", cmd.argv[i]); 236 return 1; 237 } 238 } 239 cmd.boothowto = howto; 240 return 0; 241 } 242 243 /* 244 * maintain environment as a sequence of '\n' separated 245 * variable definitions in the form <name>=[<value>] 246 * terminated by the usual '\0' 247 */ 248 char *environ; 249 250 int 251 Xenv(void) 252 { 253 if (cmd.argc == 1) { 254 if (environ) 255 printf("%s", environ); 256 else 257 printf("empty\n"); 258 } else { 259 char *p, *q; 260 int l; 261 262 for (p = environ; p && *p; p = q) { 263 l = strlen(cmd.argv[1]); 264 for (q = p; *q != '='; q++) 265 ; 266 l = max(l, q - p) + 1; 267 for (q = p; *q != '\n'; q++) 268 ; 269 if (*q) 270 q++; 271 if (!strncmp(p, cmd.argv[1], l)) { 272 while((*p++ = *q++)) 273 ; 274 p--; 275 } 276 } 277 if (!p) 278 p = environ = alloc(4096); 279 snprintf(p, environ + 4096 - p, "%s=%s\n", 280 cmd.argv[1], (cmd.argc==3?cmd.argv[2]:"")); 281 } 282 283 return 0; 284 } 285