Home | History | Annotate | Line # | Download | only in memswitch
methods.c revision 1.1
      1 /*	$NetBSD: methods.c,v 1.1 1999/06/21 15:56:03 minoura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Minoura Makoto.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <stdio.h>
     40 #include <string.h>
     41 #include <err.h>
     42 #include <sys/types.h>
     43 
     44 #include "memswitch.h"
     45 
     46 
     47 int
     48 atoi_ (p)
     49 	char **p;
     50 {
     51 	char *p1 = *p;
     52 	int v = 0;
     53 	int first = 1;
     54 
     55 	while (*p1 == ' ' || *p1 == '\t')
     56 		p1++;
     57 
     58 	if (*p1 == 0) {
     59 		*p = 0;
     60 		return 0;
     61 	}
     62 	if (strlen (p1) >= 2 && strncasecmp ("0x", p1, 2) == 0) {
     63 		p1 += 2;
     64 		while (1) {
     65 			if (*p1 >= '0' && *p1 <= '9') {
     66 				v *= 16;
     67 				v += *p1 - '0';
     68 				first = 0;
     69 			} else if (*p1 >= 'A' && *p1 <= 'F') {
     70 				v *= 16;
     71 				v += *p1 - 'A' + 10;
     72 				first = 0;
     73 			} else if (*p1 >= 'a' && *p1 <= 'f') {
     74 				v *= 16;
     75 				v += *p1 - 'a' + 10;
     76 				first = 0;
     77 			} else {
     78 				break;
     79 			}
     80 			p1++;
     81 		}
     82 	} else {
     83 		while (1) {
     84 			if (*p1 >= '0' && *p1 <= '9') {
     85 				v *= 10;
     86 				v += *p1 - '0';
     87 				first = 0;
     88 			} else {
     89 				break;
     90 			}
     91 			p1++;
     92 		}
     93 	}
     94 
     95 	if (first) {
     96 		*p = 0;
     97 		return 0;
     98 	}
     99 
    100 	while (*p1 == ' ' || *p1 == '\t');
    101 	*p = p1;
    102 	return v;
    103 }
    104 
    105 int
    106 fill_uchar (prop)
    107 	struct property *prop;
    108 {
    109 	if (current_values == 0)
    110 		alloc_current_values ();
    111 
    112 	prop->current_value.byte[0] = current_values[prop->offset];
    113 	prop->current_value.byte[1] = 0;
    114 	prop->current_value.byte[2] = 0;
    115 	prop->current_value.byte[3] = 0;
    116 	prop->value_valid = 1;
    117 
    118 	return 0;
    119 }
    120 
    121 int
    122 fill_ushort (prop)
    123 	struct property *prop;
    124 {
    125 	if (current_values == 0)
    126 		alloc_current_values ();
    127 
    128 	prop->current_value.byte[0] = current_values[prop->offset];
    129 	prop->current_value.byte[1] = current_values[prop->offset+1];
    130 	prop->current_value.byte[2] = 0;
    131 	prop->current_value.byte[3] = 0;
    132 	prop->value_valid = 1;
    133 
    134 	return 0;
    135 }
    136 
    137 int
    138 fill_ulong (prop)
    139 	struct property *prop;
    140 {
    141 	if (current_values == 0)
    142 		alloc_current_values ();
    143 
    144 	prop->current_value.byte[0] = current_values[prop->offset];
    145 	prop->current_value.byte[1] = current_values[prop->offset+1];
    146 	prop->current_value.byte[2] = current_values[prop->offset+2];
    147 	prop->current_value.byte[3] = current_values[prop->offset+3];
    148 	prop->value_valid = 1;
    149 
    150 	return 0;
    151 }
    152 
    153 int
    154 flush_uchar (prop)
    155 	struct property *prop;
    156 {
    157 	if (!prop->modified)
    158 		return 0;
    159 
    160 	if (modified_values == 0)
    161 		alloc_modified_values ();
    162 
    163 	modified_values[prop->offset] = prop->modified_value.byte[0];
    164 
    165 	return 0;
    166 }
    167 
    168 int
    169 flush_ushort (prop)
    170 	struct property *prop;
    171 {
    172 	if (!prop->modified)
    173 		return 0;
    174 
    175 	if (modified_values == 0)
    176 		alloc_modified_values ();
    177 
    178 	modified_values[prop->offset] = prop->modified_value.byte[0];
    179 	modified_values[prop->offset+1] = prop->modified_value.byte[1];
    180 
    181 	return 0;
    182 }
    183 
    184 int
    185 flush_ulong (prop)
    186 	struct property *prop;
    187 {
    188 	if (!prop->modified)
    189 		return 0;
    190 
    191 	if (modified_values == 0)
    192 		alloc_modified_values ();
    193 
    194 	modified_values[prop->offset] = prop->modified_value.byte[0];
    195 	modified_values[prop->offset+1] = prop->modified_value.byte[1];
    196 	modified_values[prop->offset+2] = prop->modified_value.byte[2];
    197 	modified_values[prop->offset+3] = prop->modified_value.byte[3];
    198 
    199 	return 0;
    200 }
    201 
    202 int
    203 flush_dummy (prop)
    204 	struct property *prop;
    205 {
    206 	return 0;
    207 }
    208 
    209 int
    210 parse_dummy (prop, value)
    211 	struct property *prop;
    212 	const char *value;
    213 {
    214 	warnx ("Cannot modify %s.%s", prop->class, prop->node);
    215 
    216 	return -1;
    217 }
    218 
    219 int
    220 parse_byte (prop, value)
    221 	struct property *prop;
    222 	const char *value;
    223 {
    224 	const char *p = value;
    225 	int v;
    226 
    227 	v = atoi_ (&p);
    228 	if (p == 0) {
    229 		warnx ("%s: Invalid value", value);
    230 		return -1;
    231 	}
    232 
    233 	if (strcasecmp ("MB", p) == 0)
    234 		v *= 1024 * 1024;
    235 	else if (strcasecmp ("KB", p) == 0)
    236 		v *= 1024;
    237 	else if (*p != 0 &&
    238 		 strcasecmp ("B", p) != 0) {
    239 		warnx ("%s: Invalid value", value);
    240 		return -1;
    241 	}
    242 
    243 	if (v < prop->min) {
    244 		warnx ("%s: Too small", value);
    245 		return -1;
    246 	} else if (v > prop->max) {
    247 		warnx ("%s: Too large", value);
    248 		return -1;
    249 	}
    250 
    251 	prop->modified = 1;
    252 	prop->modified_value.longword = v;
    253 
    254 	return 0;
    255 }
    256 
    257 int
    258 parse_uchar (prop, value)
    259 	struct property *prop;
    260 	const char *value;
    261 {
    262 	const char *p = value;
    263 	int v;
    264 
    265 	v = atoi_ (&p);
    266 	if (p == 0) {
    267 		warnx ("%s: Invalid value", value);
    268 		return -1;
    269 	}
    270 
    271 	if (v < prop->min) {
    272 		warnx ("%s: Too small", value);
    273 		return -1;
    274 	} else if (v > prop->max) {
    275 		warnx ("%s: Too large", value);
    276 		return -1;
    277 	}
    278 
    279 	prop->modified = 1;
    280 	prop->modified_value.byte[0] = v;
    281 
    282 	return 0;
    283 }
    284 
    285 int
    286 parse_ulong (prop, value)
    287 	struct property *prop;
    288 	const char *value;
    289 {
    290 	const char *p = value;
    291 	int v;
    292 
    293 	v = atoi_ (&p);
    294 	if (p == 0) {
    295 		warnx ("%s: Invalid value", value);
    296 		return -1;
    297 	}
    298 
    299 	if (v < prop->min) {
    300 		warnx ("%s: Too small", value);
    301 		return -1;
    302 	} else if (v > prop->max) {
    303 		warnx ("%s: Too large", value);
    304 		return -1;
    305 	}
    306 
    307 	prop->modified = 1;
    308 	prop->modified_value.longword = v;
    309 
    310 	return 0;
    311 }
    312 
    313 int
    314 parse_ushort (prop, value)
    315 	struct property *prop;
    316 	const char *value;
    317 {
    318 	const char *p = value;
    319 	int v;
    320 
    321 	v = atoi_ (&p);
    322 	if (p == 0) {
    323 		warnx ("%s: Invalid value", value);
    324 		return -1;
    325 	}
    326 
    327 	if (v < prop->min) {
    328 		warnx ("%s: Too small", value);
    329 		return -1;
    330 	} else if (v > prop->max) {
    331 		warnx ("%s: Too large", value);
    332 		return -1;
    333 	}
    334 
    335 	prop->modified = 1;
    336 	prop->modified_value.word[0] = v;
    337 
    338 	return 0;
    339 }
    340 
    341 int
    342 parse_time (prop, value)
    343 	struct property *prop;
    344 	const char *value;
    345 {
    346 	const char *p = value;
    347 	int v;
    348 
    349 	while (*p == ' ' || *p == '\t');
    350 	if (*p == '-') {
    351 		p++;
    352 		v = -atoi_ (&p);
    353 	} else
    354 		v = atoi_ (&p);
    355 	if (p == 0) {
    356 		warnx ("%s: Invalid value", value);
    357 		return -1;
    358 	}
    359 
    360 	if (strcasecmp ("hours", p) == 0 || strcasecmp ("hour", p) == 0)
    361 		v *= 60 * 60;
    362 	else if (strcasecmp ("minutes", p) == 0 ||
    363 		 strcasecmp ("minute", p) == 0)
    364 		v *= 60;
    365 	else if (*p != 0 &&
    366 		 strcasecmp ("second", p) != 0 &&
    367 		 strcasecmp ("seconds", p) != 0) {
    368 		warnx ("%s: Invalid value", value);
    369 		return -1;
    370 	}
    371 
    372 	if (v < prop->min) {
    373 		warnx ("%s: Too small", value);
    374 		return -1;
    375 	} else if (v > prop->max) {
    376 		warnx ("%s: Too large", value);
    377 		return -1;
    378 	}
    379 
    380 	prop->modified = 1;
    381 	prop->modified_value.longword = v;
    382 
    383 	return 0;
    384 }
    385 
    386 int
    387 parse_bootdev (prop, value)
    388 	struct property *prop;
    389 	const char *value;
    390 {
    391 	const char *p = value;
    392 	int v;
    393 
    394 	while (*p == ' ' || *p == '\t');
    395 
    396 	if (strcasecmp ("STD", p) == 0)
    397 		v = 0;
    398 	else if (strcasecmp ("ROM", p) == 0)
    399 		v = 0xa000;
    400 	else if (strcasecmp ("RAM", p) == 0)
    401 		v = 0xb000;
    402 	else if (strncasecmp ("HD", p, 2) == 0) {
    403 		p += 2;
    404 		v = atoi_ (&p);
    405 		if (p == 0 || v < 0 || v > 15) {
    406 			warnx ("%s: Invalid value", value);
    407 			return -1;
    408 		}
    409 		v *= 0x0100;
    410 		v += 0x8000;
    411 	} else if (strncasecmp ("FD", p, 2) == 0) {
    412 		p += 2;
    413 		v = atoi_ (&p);
    414 		if (p == 0 || v < 0 || v > 3) {
    415 			warnx ("%s: Invalid value", value);
    416 			return -1;
    417 		}
    418 		v *= 0x0100;
    419 		v += 0x9070;
    420 	} else {
    421 		warnx ("%s: Invalid value", value);
    422 		return -1;
    423 	}
    424 
    425 	prop->modified = 1;
    426 	prop->modified_value.word[0] = v;
    427 
    428 	return 0;
    429 }
    430 
    431 int
    432 print_uchar (prop, str)
    433 	struct property *prop;
    434 	char *str;
    435 {
    436 	if (prop->modified)
    437 		snprintf (str, MAXVALUELEN,
    438 			  "%d", prop->modified_value.byte[0]);
    439 	else {
    440 		if (!prop->value_valid)
    441 			prop->fill (prop);
    442 		snprintf (str, MAXVALUELEN, "%d",
    443 			  prop->current_value.byte[0]);
    444 	}
    445 
    446 	return 0;
    447 }
    448 
    449 int
    450 print_ucharh (prop, str)
    451 	struct property *prop;
    452 	char *str;
    453 {
    454 	if (prop->modified)
    455 		snprintf (str, MAXVALUELEN,
    456 			  "0x%4.4x", prop->modified_value.byte[0]);
    457 	else {
    458 		if (!prop->value_valid)
    459 			prop->fill (prop);
    460 		snprintf (str, MAXVALUELEN,
    461 			  "0x%4.4x", prop->current_value.byte[0]);
    462 	}
    463 
    464 	return 0;
    465 }
    466 
    467 int
    468 print_ushorth (prop, str)
    469 	struct property *prop;
    470 	char *str;
    471 {
    472 	if (prop->modified)
    473 		snprintf (str, MAXVALUELEN,
    474 			  "0x%4.4x", prop->modified_value.word[0]);
    475 	else {
    476 		if (!prop->value_valid)
    477 			prop->fill (prop);
    478 		snprintf (str, MAXVALUELEN,
    479 			  "0x%4.4x", prop->current_value.word[0]);
    480 	}
    481 
    482 	return 0;
    483 }
    484 
    485 int
    486 print_ulong (prop, str)
    487 	struct property *prop;
    488 	char *str;
    489 {
    490 	if (prop->modified)
    491 		snprintf (str, MAXVALUELEN,
    492 			  "%ld", prop->modified_value.longword);
    493 	else {
    494 		if (!prop->value_valid)
    495 			prop->fill (prop);
    496 		snprintf (str, MAXVALUELEN,
    497 			  "%ld", prop->current_value.longword);
    498 	}
    499 
    500 	return 0;
    501 }
    502 
    503 int
    504 print_ulongh (prop, str)
    505 	struct property *prop;
    506 	char *str;
    507 {
    508 	if (prop->modified)
    509 		snprintf (str, MAXVALUELEN,
    510 			  "0x%8.8lx", prop->modified_value.longword);
    511 	else {
    512 		if (!prop->value_valid)
    513 			prop->fill (prop);
    514 		snprintf (str, MAXVALUELEN,
    515 			  "0x%8.8lx", prop->current_value.longword);
    516 	}
    517 
    518 	return 0;
    519 }
    520 
    521 int
    522 print_magic (prop, str)
    523 	struct property *prop;
    524 	char *str;
    525 {
    526 	if (!prop->value_valid)
    527 		prop->fill (prop);
    528 	snprintf (str, MAXVALUELEN, "%c%c%c%c",
    529 		  prop->current_value.byte[0],
    530 		  prop->current_value.byte[1],
    531 		  prop->current_value.byte[2],
    532 		  prop->current_value.byte[3]);
    533 
    534 	return 0;
    535 }
    536 
    537 int
    538 print_timesec (prop, str)
    539 	struct property *prop;
    540 	char *str;
    541 {
    542 	if (prop->modified)
    543 		snprintf (str, MAXVALUELEN,
    544 			  "%ld second", prop->modified_value.longword);
    545 	else {
    546 		if (!prop->value_valid)
    547 			prop->fill (prop);
    548 		snprintf (str, MAXVALUELEN,
    549 			  "%ld second", prop->current_value.longword);
    550 	}
    551 
    552 	return 0;
    553 }
    554 
    555 int
    556 print_bootdev (prop, str)
    557 	struct property *prop;
    558 	char *str;
    559 {
    560 	unsigned int v;
    561 
    562 	if (prop->modified)
    563 		v = prop->modified_value.word[0];
    564 	else {
    565 		if (!prop->value_valid)
    566 			prop->fill (prop);
    567 		v = prop->current_value.word[0];
    568 	}
    569 
    570 	if (v == 0)
    571 		strcpy (str, "STD");
    572 	else if (v == 0xa000)
    573 		strcpy (str, "ROM");
    574 	else if (v == 0xb000)
    575 		strcpy (str, "RAM");
    576 	else if (v >= 0x8000 && v < 0x9000)
    577 		snprintf (str, MAXVALUELEN, "HD%d", (v & 0x0f00) >> 8);
    578 	else if (v >= 0x9000 && v < 0xa000)
    579 		snprintf (str, MAXVALUELEN, "FD%d", (v & 0x0f00) >> 8);
    580 	else
    581 		snprintf (str, MAXVALUELEN, "%8.8x", v);
    582 
    583 	return 0;
    584 }
    585