1 /* $OpenBSD: cmd.c,v 1.5 2001/02/04 20:42:12 maja 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.5 2001/02/04 20:42:12 maja 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 int a; 89 short unit, state; 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 117 return (CMD_CONT); 118 } 119 120 int 121 Xchange(cmd) 122 cmd_t *cmd; 123 { 124 int a; 125 short unit, state; 126 127 if (strlen(cmd->args) == 0) 128 printf("DevNo or Dev expected\n"); 129 else if (number(cmd->args, &a) == 0) 130 change(a); 131 else if (device(cmd->args, &a, &unit, &state) == 0) 132 common_dev(cmd->args, a, unit, state, UC_CHANGE); 133 else 134 printf("Unknown argument\n"); 135 136 return (CMD_CONT); 137 } 138 139 int 140 Xdisable(cmd) 141 cmd_t *cmd; 142 { 143 int a; 144 short unit, state; 145 146 if (strlen(cmd->args) == 0) 147 printf("Attr, DevNo or Dev expected\n"); 148 else if (attr(cmd->args, &a) == 0) 149 common_attr(cmd->args, a, UC_DISABLE); 150 else if (number(cmd->args, &a) == 0) 151 disable(a); 152 else if (device(cmd->args, &a, &unit, &state) == 0) 153 common_dev(cmd->args, a, unit, state, UC_DISABLE); 154 else 155 printf("Unknown argument\n"); 156 157 return (CMD_CONT); 158 } 159 160 int 161 Xenable(cmd) 162 cmd_t *cmd; 163 { 164 int a; 165 short unit, state; 166 167 if (strlen(cmd->args) == 0) 168 printf("Attr, DevNo or Dev expected\n"); 169 else if (attr(cmd->args, &a) == 0) 170 common_attr(cmd->args, a, UC_DISABLE); 171 else if (number(cmd->args, &a) == 0) 172 enable(a); 173 else if (device(cmd->args, &a, &unit, &state) == 0) 174 common_dev(cmd->args, a, unit, state, UC_ENABLE); 175 else 176 printf("Unknown argument\n"); 177 178 return (CMD_CONT); 179 } 180 181 int 182 Xfind(cmd) 183 cmd_t *cmd; 184 { 185 int a; 186 short unit, state; 187 188 if (strlen(cmd->args) == 0) 189 printf("DevNo or Dev expected\n"); 190 else if (number(cmd->args, &a) == 0) 191 pdev(a); 192 else if (device(cmd->args, &a, &unit, &state) == 0) 193 common_dev(cmd->args, a, unit, state, UC_FIND); 194 else 195 printf("Unknown argument\n"); 196 197 return (CMD_CONT); 198 } 199 200 int 201 Xlines(cmd) 202 cmd_t *cmd; 203 { 204 int a; 205 206 if (strlen(cmd->args) == 0) 207 printf("Argument expected\n"); 208 else if (number(cmd->args, &a) == 0) 209 lines = a; 210 else 211 printf("Unknown argument\n"); 212 return (CMD_CONT); 213 } 214 215 int 216 Xlist(cmd) 217 cmd_t *cmd; 218 { 219 int i = 0; 220 struct cfdata *cd; 221 222 cnt = 0; 223 224 cd = get_cfdata(0); 225 226 while(cd->cf_attach != 0) { 227 if (more()) 228 break; 229 pdev(i++); 230 cd++; 231 } 232 233 if (nopdev == 0) { 234 while(i <= (totdev+maxpseudo)) { 235 if (more()) 236 break; 237 pdev(i++); 238 } 239 } 240 241 cnt = -1; 242 243 return (CMD_CONT); 244 } 245 246 int 247 Xshow(cmd) 248 cmd_t *cmd; 249 { 250 if (strlen(cmd->args) == 0) 251 show(); 252 else 253 show_attr(&cmd->args[0]); 254 255 return (CMD_CONT); 256 } 257 258 int 259 Xquit(cmd) 260 cmd_t *cmd; 261 { 262 /* Nothing to do here */ 263 return (CMD_SAVE); 264 } 265 266 int 267 Xexit(cmd) 268 cmd_t *cmd; 269 { 270 /* Nothing to do here */ 271 return (CMD_EXIT); 272 } 273 274 int 275 Xtimezone(cmd) 276 cmd_t *cmd; 277 { 278 struct timezone *tz; 279 int num; 280 char *c; 281 282 ukc_mod_kernel = 1; 283 284 tz = (struct timezone *)adjust((caddr_t)(nl[TZ_TZ].n_value)); 285 286 if (strlen(cmd->args) == 0) { 287 printf("timezone = %d, dst = %d\n", 288 tz->tz_minuteswest, tz->tz_dsttime); 289 } else { 290 if (number(cmd->args, &num) == 0) { 291 tz->tz_minuteswest = num; 292 c = cmd->args; 293 while ((*c != '\0') && !isspace(*c)) 294 c++; 295 while ((*c != '\0') && isspace(*c)) 296 c++; 297 if (strlen(c) != 0 && number(c, &num) == 0) 298 tz->tz_dsttime = num; 299 printf("timezone = %d, dst = %d\n", 300 tz->tz_minuteswest, tz->tz_dsttime); 301 } else 302 printf("Unknown argument\n"); 303 } 304 305 return (CMD_CONT); 306 } 307