Home | History | Annotate | Line # | Download | only in db
db.c revision 1.14
      1 /*	$NetBSD: db.c,v 1.14 2005/06/20 02:53:38 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002-2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Luke Mewburn of Wasabi Systems.
      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 <sys/cdefs.h>
     40 #ifndef lint
     41 #ifdef __RCSID
     42 __RCSID("$NetBSD: db.c,v 1.14 2005/06/20 02:53:38 lukem Exp $");
     43 #endif /* __RCSID */
     44 #endif /* not lint */
     45 
     46 #include <db.h>
     47 #include <ctype.h>
     48 #include <err.h>
     49 #include <fcntl.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 #include <vis.h>
     55 
     56 
     57 typedef enum {
     58 	F_WRITE		= 1<<0,
     59 	F_DELETE	= 1<<1,
     60 	F_SHOW_KEY	= 1<<2,
     61 	F_SHOW_VALUE	= 1<<3,
     62 	F_QUIET		= 1<<10,
     63 	F_IGNORECASE	= 1<<11,
     64 	F_ENDIAN_BIG	= 1<<12,
     65 	F_ENDIAN_LITTLE	= 1<<13,
     66 	F_NO_NUL	= 1<<14,
     67 	F_CREATENEW	= 1<<20,
     68 	F_DUPLICATES	= 1<<21,
     69 	F_REPLACE	= 1<<22,
     70 	F_ENCODE_KEY	= 1<<23,
     71 	F_ENCODE_VAL	= 1<<24,
     72 	F_DECODE_KEY	= 1<<25,
     73 	F_DECODE_VAL	= 1<<26,
     74 } flags_t;
     75 
     76 int	main(int, char *[]);
     77 void	db_print(DBT *, DBT *);
     78 int	db_dump(void);
     79 int	db_del(char *);
     80 int	db_get(char *);
     81 int	db_put(char *, char *);
     82 int	parseline(FILE *, const char *, char **, char **);
     83 int	encode_data(size_t, char *, char **);
     84 int	decode_data(char *, char **);
     85 void	parse_encode_decode_arg(const char *, int);
     86 int	parse_encode_option(char **);
     87 void	usage(void);
     88 
     89 flags_t	 flags = 0;
     90 DB	*db;
     91 char	*outputsep = "\t";
     92 int	encflags = 0;
     93 char	*extra_echars = NULL;
     94 
     95 int
     96 main(int argc, char *argv[])
     97 {
     98 	struct {
     99 		char	*file;
    100 		char	*type;
    101 		DBTYPE	 dbtype;
    102 		void	*info;
    103 		int	 flags;
    104 		mode_t	 mode;
    105 	} oi;
    106 	BTREEINFO	btreeinfo;
    107 	HASHINFO	hashinfo;
    108 	FILE		*infp;
    109 	const char	*infile, *fieldsep;
    110 	char		*p, *key, *val;
    111 	int		ch, rv;
    112 
    113 	setprogname(argv[0]);
    114 
    115 	infile = NULL;
    116 	fieldsep = " ";
    117 	infp = NULL;
    118 	memset(&oi, 0, sizeof(oi));
    119 	oi.mode = 0644;
    120 
    121 				/* parse arguments */
    122 	while ( (ch = getopt(argc, argv,
    123 			     "CDdE:F:f:iKm:NO:qRS:T:U:VwX:")) != -1) {
    124 		switch (ch) {
    125 
    126 		case 'C':
    127 			flags |= F_CREATENEW;
    128 			break;
    129 
    130 		case 'D':
    131 			flags |= F_DUPLICATES;
    132 			break;
    133 
    134 		case 'd':
    135 			flags |= F_DELETE;
    136 			break;
    137 
    138 		case 'E':
    139 			if (! optarg[0] || optarg[1])
    140 				goto badendian;
    141 			switch (toupper((int)optarg[0])) {
    142 			case 'B':
    143 				flags |= F_ENDIAN_BIG;
    144 				break;
    145 			case 'L':
    146 				flags |= F_ENDIAN_LITTLE;
    147 				break;
    148 			case 'H':
    149 				flags &= ~(F_ENDIAN_BIG | F_ENDIAN_LITTLE);
    150 				break;
    151 			default:
    152  badendian:
    153 				errx(1, "Bad endian `%s'", optarg);
    154 			}
    155 			break;
    156 
    157 		case 'F':
    158 			if (! optarg[0])
    159 				errx(1, "Invalid field separator `%s'",
    160 				    optarg);
    161 			fieldsep = optarg;
    162 			break;
    163 
    164 		case 'f':
    165 			infile = optarg;
    166 			break;
    167 
    168 		case 'i':
    169 			flags |= F_IGNORECASE;
    170 			break;
    171 
    172 		case 'K':
    173 			flags |= F_SHOW_KEY;
    174 			break;
    175 
    176 		case 'm':
    177 			oi.mode = (int)strtol(optarg, &p, 8);
    178 			if (p == optarg || *p != '\0')
    179 				errx(1, "Invalid octal number `%s'", optarg);
    180 			break;
    181 
    182 		case 'N':
    183 			flags |= F_NO_NUL;
    184 			break;
    185 
    186 		case 'O':
    187 			outputsep = optarg;
    188 			break;
    189 
    190 		case 'q':
    191 			flags |= F_QUIET;
    192 			break;
    193 
    194 		case 'R':
    195 			flags |= F_REPLACE;
    196 			break;
    197 
    198 		case 'S':
    199 			parse_encode_decode_arg(optarg, 1 /* encode */);
    200 			if (! (flags & (F_ENCODE_KEY | F_ENCODE_VAL)))
    201 				errx(1, "Invalid encoding argument `%s'",
    202 				    optarg);
    203 			break;
    204 
    205 		case 'T':
    206 			encflags = parse_encode_option(&optarg);
    207 			if (! encflags)
    208 				errx(1, "Invalid encoding option `%s'",
    209 				    optarg);
    210 			break;
    211 
    212 		case 'U':
    213 			parse_encode_decode_arg(optarg, 0 /* decode */);
    214 			if (! (flags & (F_DECODE_KEY | F_DECODE_VAL)))
    215 				errx(1, "Invalid decoding argument `%s'",
    216 				    optarg);
    217 			break;
    218 
    219 		case 'V':
    220 			flags |= F_SHOW_VALUE;
    221 			break;
    222 
    223 		case 'w':
    224 			flags |= F_WRITE;
    225 			break;
    226 
    227 		case 'X':
    228 			extra_echars = optarg;
    229 			break;
    230 
    231 		default:
    232 			usage();
    233 
    234 		}
    235 	}
    236 	argc -= optind;
    237 	argv += optind;
    238 
    239 				/* validate arguments */
    240 	if (argc < 2)
    241 		usage();
    242 	oi.type = argv[0];
    243 	oi.file = argv[1];
    244 	argc -= 2;
    245 	argv += 2;
    246 
    247 	if (flags & F_WRITE) {
    248 		if (flags & (F_SHOW_KEY | F_SHOW_VALUE | F_DELETE))
    249 			usage();
    250 		if ((!infile && argc < 2) || (argc % 2))
    251 			usage();
    252 		oi.flags = O_RDWR | O_CREAT | O_EXLOCK;
    253 		if (flags & F_CREATENEW)
    254 			flags |= O_TRUNC;
    255 	} else if (flags & F_DELETE) {
    256 		if (flags & (F_SHOW_KEY | F_SHOW_VALUE | F_WRITE))
    257 			usage();
    258 		if (!infile && argc < 1)
    259 			usage();
    260 		oi.flags = O_RDWR | O_CREAT | O_EXLOCK;
    261 	} else {
    262 		if (! (flags & (F_SHOW_KEY | F_SHOW_VALUE)))
    263 			flags |= (F_SHOW_KEY | F_SHOW_VALUE);
    264 		oi.flags = O_RDONLY | O_SHLOCK;
    265 	}
    266 
    267 				/* validate oi.type */
    268 	if (strcmp(oi.type, "btree") == 0) {
    269 		memset(&btreeinfo, 0, sizeof(btreeinfo));
    270 		if (flags & F_ENDIAN_BIG)
    271 			btreeinfo.lorder = 4321;
    272 		else if (flags & F_ENDIAN_LITTLE)
    273 			btreeinfo.lorder = 1234;
    274 		if (flags & F_DUPLICATES)
    275 			btreeinfo.flags = R_DUP;
    276 		btreeinfo.cachesize = 1024 * 1024;
    277 		oi.info = &btreeinfo;
    278 		oi.dbtype = DB_BTREE;
    279 	} else if (strcmp(oi.type, "hash") == 0) {
    280 		memset(&hashinfo, 0, sizeof(hashinfo));
    281 		if (flags & F_ENDIAN_BIG)
    282 			hashinfo.lorder = 4321;
    283 		else if (flags & F_ENDIAN_LITTLE)
    284 			hashinfo.lorder = 1234;
    285 		hashinfo.cachesize = 1024 * 1024;
    286 		oi.info = &hashinfo;
    287 		oi.dbtype = DB_HASH;
    288 	} else {
    289 		warnx("Unknown database type `%s'", oi.type);
    290 		usage();
    291 	}
    292 
    293 	if (infile) {
    294 		if (strcmp(infile, "-") == 0)
    295 			infp = stdin;
    296 		else if ((infp = fopen(infile, "r")) == NULL)
    297 			err(1, "Opening input file `%s'", infile);
    298 	}
    299 
    300 				/* open database */
    301 	db = dbopen(oi.file, oi.flags, oi.mode, oi.dbtype, oi.info);
    302 	if (db == NULL)
    303 		err(1, "Opening database `%s'", oi.file);
    304 
    305 
    306 				/* manipulate database */
    307 	rv = 0;
    308 	if (flags & F_WRITE) {			/* write entries */
    309 		for (ch = 0; ch < argc; ch += 2)
    310 			if ((rv = db_put(argv[ch], argv[ch+1])))
    311 				goto cleanup;
    312 		if (infp) {
    313 			while (parseline(infp, fieldsep, &key, &val)) {
    314 				if ((rv = db_put(key, val)))
    315 					goto cleanup;
    316 			}
    317 			if (ferror(infp)) {
    318 				warnx("Reading `%s'", infile);
    319 				goto cleanup;
    320 			}
    321 		}
    322 	} else if (!infp && argc == 0) {	/* read all */
    323 		db_dump();
    324 	} else {				/* read/delete specific */
    325 		int	(*dbop)(char *);
    326 
    327 		if (flags & F_DELETE)
    328 			dbop = db_del;
    329 		else
    330 			dbop = db_get;
    331 		for (ch = 0; ch < argc; ch++) {
    332 			if ((rv = dbop(argv[ch])))
    333 				goto cleanup;
    334 		}
    335 		if (infp) {
    336 			while (parseline(infp, fieldsep, &key, NULL)) {
    337 				if ((rv = dbop(key)))
    338 					goto cleanup;
    339 			}
    340 			if (ferror(infp)) {
    341 				warnx("Reading `%s'", infile);
    342 				goto cleanup;
    343 			}
    344 		}
    345 	}
    346 
    347 				/* close database */
    348  cleanup:
    349 	if (db->close(db) == -1)
    350 		err(1, "Closing database `%s'", oi.file);
    351 	if (infp)
    352 		fclose(infp);
    353 	return (rv);
    354 }
    355 
    356 void
    357 db_print(DBT *key, DBT *val)
    358 {
    359 	int	len;
    360 	char	*data;
    361 
    362 #define	MINUSNUL(x)	((x) > 0  ?  (x) - (flags & F_NO_NUL ? 0 : 1)  :  0)
    363 
    364 	if (flags & F_SHOW_KEY) {
    365 		if (flags & F_ENCODE_KEY) {
    366 			len = encode_data(MINUSNUL(key->size),
    367 			    (char *)key->data, &data);
    368 		} else {
    369 			len = (int)MINUSNUL(key->size);
    370 			data = (char *)key->data;
    371 		}
    372 		printf("%.*s", len, data);
    373 	}
    374 	if ((flags & F_SHOW_KEY) && (flags & F_SHOW_VALUE))
    375 		printf("%s", outputsep);
    376 	if (flags & F_SHOW_VALUE) {
    377 		if (flags & F_ENCODE_VAL) {
    378 			len = encode_data(MINUSNUL(val->size),
    379 			    (char *)val->data, &data);
    380 		} else {
    381 			len = (int)MINUSNUL(val->size);
    382 			data = (char *)val->data;
    383 		}
    384 		printf("%.*s", len, data);
    385 	}
    386 	printf("\n");
    387 }
    388 
    389 int
    390 db_dump(void)
    391 {
    392 	DBT	key, val;
    393 	int	rv;
    394 
    395 	while ((rv = db->seq(db, &key, &val, R_NEXT)) == 0)
    396 		db_print(&key, &val);
    397 	if (rv == -1)
    398 		warn("Error dumping database");
    399 	return (rv == 1 ? 0 : 1);
    400 }
    401 
    402 static void
    403 db_makekey(DBT *key, char *keystr, int downcase, int decode)
    404 {
    405 	char	*p, *ks;
    406 	int	klen;
    407 
    408 	memset(key, 0, sizeof(*key));
    409 	if (decode) {
    410 		if ((klen = decode_data(keystr, &ks)) == -1)
    411 			errx(1, "Invalid escape sequence in `%s'",
    412 		  	  keystr);
    413 	} else {
    414 		klen = strlen(keystr);
    415 		ks = keystr;
    416 	}
    417 	key->data = ks;
    418 	key->size = klen + (flags & F_NO_NUL ? 0 : 1);
    419 	if (downcase && flags & F_IGNORECASE) {
    420 		for (p = ks; *p; p++)
    421 			if (isupper((int)*p))
    422 				*p = tolower((int)*p);
    423 	}
    424 }
    425 
    426 int
    427 db_del(char *keystr)
    428 {
    429 	DBT	key;
    430 	int	r = 0;
    431 
    432 	db_makekey(&key, keystr, 1, (flags & F_DECODE_KEY ? 1 : 0));
    433 	switch (db->del(db, &key, 0)) {
    434 	case -1:
    435 		warn("Error deleting key `%s'", keystr);
    436 		r = 1;
    437 		break;
    438 	case 0:
    439 		if (! (flags & F_QUIET))
    440 			printf("Deleted key `%s'\n", keystr);
    441 		break;
    442 	case 1:
    443 		warnx("Key `%s' does not exist", keystr);
    444 		r = 1;
    445 		break;
    446 	}
    447 	if (flags & F_DECODE_KEY)
    448 		free(key.data);
    449 	return (r);
    450 }
    451 
    452 int
    453 db_get(char *keystr)
    454 {
    455 	DBT	key, val;
    456 	int	r = 0;
    457 
    458 	db_makekey(&key, keystr, 1, (flags & F_DECODE_KEY ? 1 : 0));
    459 	switch (db->get(db, &key, &val, 0)) {
    460 	case -1:
    461 		warn("Error reading key `%s'", keystr);
    462 		r = 1;
    463 		break;
    464 	case 0:
    465 		db_print(&key, &val);
    466 		break;
    467 	case 1:
    468 		if (! (flags & F_QUIET)) {
    469 			warnx("Unknown key `%s'", keystr);
    470 			r = 1;
    471 		}
    472 		break;
    473 	}
    474 	if (flags & F_DECODE_KEY)
    475 		free(key.data);
    476 	return (r);
    477 }
    478 
    479 int
    480 db_put(char *keystr, char *valstr)
    481 {
    482 	DBT	key, val;
    483 	int	r = 0;
    484 
    485 	db_makekey(&key, keystr, 1, (flags & F_DECODE_KEY ? 1 : 0));
    486 	db_makekey(&val, valstr, 0, (flags & F_DECODE_VAL ? 1 : 0));
    487 	switch (db->put(db, &key, &val,
    488 	    (flags & F_REPLACE) ? 0 : R_NOOVERWRITE)) {
    489 	case -1:
    490 		warn("Error writing key `%s'", keystr);
    491 		r = 1;
    492 		break;
    493 	case 0:
    494 		if (! (flags & F_QUIET))
    495 			printf("Added key `%s'\n", keystr);
    496 		break;
    497 	case 1:
    498 		if (! (flags & F_QUIET))
    499 			warnx("Key `%s' already exists", keystr);
    500 		r = 1;
    501 		break;
    502 	}
    503 	if (flags & F_DECODE_KEY)
    504 		free(key.data);
    505 	if (flags & F_DECODE_VAL)
    506 		free(val.data);
    507 	return (r);
    508 }
    509 
    510 int
    511 parseline(FILE *fp, const char *sep, char **kp, char **vp)
    512 {
    513 	size_t	len;
    514 	char	*key, *val;
    515 
    516 	key = fgetln(fp, &len);
    517 	if (key == NULL)		/* end of file, or error */
    518 		return (0);
    519 
    520 	if (key[len-1] == '\n')		/* check for \n at EOL */
    521 		key[--len] = '\0';
    522 	else
    523 		return (0);
    524 
    525 	*kp = key;
    526 	if (vp == NULL)			/* don't split if don't want value */
    527 		return (1);
    528 	if ((val = strstr(key, sep)) == NULL)
    529 		val = key + len;
    530 	else {
    531 		*val = '\0';
    532 		val += strlen(sep);
    533 	}
    534 	*vp = val;
    535 	return (1);
    536 }
    537 
    538 int
    539 encode_data(size_t len, char *data, char **edata)
    540 {
    541 	static char	*buf = NULL;
    542 	char		*nbuf;
    543 	static size_t	buflen = 0;
    544 	size_t		elen;
    545 
    546 	elen = 1 + (len * 4);
    547 	if (elen > buflen) {
    548 		if ((nbuf = realloc(buf, elen)) == NULL)
    549 			err(1, "Cannot allocate encoding buffer");
    550 		buf = nbuf;
    551 		buflen = elen;
    552 	}
    553 	*edata = buf;
    554 	if (extra_echars) {
    555 		return (strsvisx(buf, data, len, encflags, extra_echars));
    556 	} else {
    557 		return (strvisx(buf, data, len, encflags));
    558 	}
    559 }
    560 
    561 int
    562 decode_data(char *data, char **ddata)
    563 {
    564 	char	*buf;
    565 
    566 	if ((buf = malloc(strlen(data) + 1)) == NULL)
    567 		err(1, "Cannot allocate decoding buffer");
    568 	*ddata = buf;
    569 	return (strunvis(buf, data));
    570 }
    571 
    572 void
    573 parse_encode_decode_arg(const char *arg, int encode)
    574 {
    575 	if (! arg[0] || arg[1])
    576 		return;
    577 	if (arg[0] == 'k' || arg[0] == 'b') {
    578 		if (encode)
    579 			flags |= F_ENCODE_KEY;
    580 		else
    581 			flags |= F_DECODE_KEY;
    582 	}
    583 	if (arg[0] == 'v' || arg[0] == 'b') {
    584 		if (encode)
    585 			flags |= F_ENCODE_VAL;
    586 		else
    587 			flags |= F_DECODE_VAL;
    588 	}
    589 	return;
    590 }
    591 
    592 int
    593 parse_encode_option(char **arg)
    594 {
    595 	int	r = 0;
    596 
    597 	for(; **arg; (*arg)++) {
    598 		switch (**arg) {
    599 			case 'b':
    600 				r |= VIS_NOSLASH;
    601 				break;
    602 			case 'c':
    603 				r |= VIS_CSTYLE;
    604 				break;
    605 			case 'o':
    606 				r |= VIS_OCTAL;
    607 				break;
    608 			case 's':
    609 				r |= VIS_SAFE;
    610 				break;
    611 			case 't':
    612 				r |= VIS_TAB;
    613 				break;
    614 			case 'w':
    615 				r |= VIS_WHITE;
    616 				break;
    617 			default:
    618 				return (0);
    619 				break;
    620 		}
    621 	}
    622 	return (r);
    623 }
    624 
    625 void
    626 usage(void)
    627 {
    628 	const char *p = getprogname();
    629 
    630 	fprintf(stderr,
    631 "usage: %s    [-KiNqV] [-E endian] [-f infile] [-O outsep] [-S visitem]\n"
    632 "             [-T visspec] [-X extravis] type dbfile [key [...]]\n"
    633 "       %s -d [-iNq] [-E endian] [-f infile] [-U unvisitem]\n"
    634 "             type dbfile [key [...]]\n"
    635 "       %s -w [-CDiNqR] [-E endian] [-F isep] [-f infile] [-m mode]\n"
    636 "             [-U unvisitem] type dbfile [key value [...]]\n"
    637 	    ,p ,p ,p );
    638 	fprintf(stderr,
    639 "Supported modes:\n"
    640 "                read keys  [default]\n"
    641 "   -d           delete keys\n"
    642 "   -w           write (add) keys/values\n"
    643 "Supported options:\n"
    644 "   -C           create empty (truncated) database\n"
    645 "   -D           allow duplicates\n"
    646 "   -E endian    database endian: `B'ig, `L'ittle, `H'ost  [default: H]\n"
    647 "   -F isep      input field separator string  [default: ' ']\n"
    648 "   -f infile    file of keys (read|delete) or keys/vals (write)\n"
    649 "   -i           ignore case of key by converting to lower case\n"
    650 "   -K           print key\n"
    651 "   -m mode      mode of created database  [default: 0644]\n"
    652 "   -N           don't NUL terminate key\n"
    653 "   -O outsep    output field separator string [default: '\t']\n"
    654 "   -q           quiet operation (missing keys aren't errors)\n"
    655 "   -R           replace existing keys\n"
    656 "   -S visitem   items to strvis(3) encode: 'k'ey, 'v'alue, 'b'oth\n"
    657 "   -T visspec   options to control -S encoding like vis(1) options\n"
    658 "   -U unvisitem items to strunvis(3) decode: 'k'ey, 'v'alue, 'b'oth\n"
    659 "   -V           print value\n"
    660 "   -X extravis  extra characters to encode with -S\n"
    661 	    );
    662 	exit(1);
    663 }
    664