158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
4f1e396bcSPaul Traina * Copyright (c) 1992, 1993, 1994
558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved.
658f0484fSRodney W. Grimes *
758f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes * are met:
1058f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes * without specific prior written permission.
1858f0484fSRodney W. Grimes *
1958f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes * SUCH DAMAGE.
3058f0484fSRodney W. Grimes */
3158f0484fSRodney W. Grimes
3258f0484fSRodney W. Grimes #include <sys/param.h>
3358f0484fSRodney W. Grimes #include <sys/stat.h>
3458f0484fSRodney W. Grimes
3558f0484fSRodney W. Grimes #include <ctype.h>
3658f0484fSRodney W. Grimes #include <errno.h>
3758f0484fSRodney W. Grimes #include <fcntl.h>
3858f0484fSRodney W. Grimes #include <limits.h>
3958f0484fSRodney W. Grimes #include <stdio.h>
4058f0484fSRodney W. Grimes #include <stdlib.h>
4158f0484fSRodney W. Grimes #include <string.h>
4258f0484fSRodney W. Grimes #include <unistd.h>
4358f0484fSRodney W. Grimes
4458f0484fSRodney W. Grimes #include <db.h>
4558f0484fSRodney W. Grimes
4658f0484fSRodney W. Grimes enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
4758f0484fSRodney W. Grimes
48c05ac53bSDavid E. O'Brien void compare(DBT *, DBT *);
49c05ac53bSDavid E. O'Brien DBTYPE dbtype(char *);
50c05ac53bSDavid E. O'Brien void dump(DB *, int);
511372519bSDavid E. O'Brien void err(const char *, ...) __printflike(1, 2);
52c05ac53bSDavid E. O'Brien void get(DB *, DBT *);
53c05ac53bSDavid E. O'Brien void getdata(DB *, DBT *, DBT *);
54c05ac53bSDavid E. O'Brien void put(DB *, DBT *, DBT *);
55c05ac53bSDavid E. O'Brien void rem(DB *, DBT *);
56c05ac53bSDavid E. O'Brien char *sflags(int);
57c05ac53bSDavid E. O'Brien void synk(DB *);
58c05ac53bSDavid E. O'Brien void *rfile(char *, size_t *);
59c05ac53bSDavid E. O'Brien void seq(DB *, DBT *);
60c05ac53bSDavid E. O'Brien u_int setflags(char *);
61c05ac53bSDavid E. O'Brien void *setinfo(DBTYPE, char *);
62c05ac53bSDavid E. O'Brien void usage(void);
63c05ac53bSDavid E. O'Brien void *xmalloc(char *, size_t);
6458f0484fSRodney W. Grimes
65f1e396bcSPaul Traina DBTYPE type; /* Database type. */
66f1e396bcSPaul Traina void *infop; /* Iflags. */
67f1e396bcSPaul Traina u_long lineno; /* Current line in test script. */
68f1e396bcSPaul Traina u_int flags; /* Current DB flags. */
69f1e396bcSPaul Traina int ofd = STDOUT_FILENO; /* Standard output fd. */
7058f0484fSRodney W. Grimes
7158f0484fSRodney W. Grimes DB *XXdbp; /* Global for gdb. */
72f1e396bcSPaul Traina int XXlineno; /* Fast breakpoint for gdb. */
7358f0484fSRodney W. Grimes
7458f0484fSRodney W. Grimes int
main(argc,argv)7558f0484fSRodney W. Grimes main(argc, argv)
7658f0484fSRodney W. Grimes int argc;
7758f0484fSRodney W. Grimes char *argv[];
7858f0484fSRodney W. Grimes {
7958f0484fSRodney W. Grimes extern int optind;
8058f0484fSRodney W. Grimes extern char *optarg;
8158f0484fSRodney W. Grimes enum S command, state;
8258f0484fSRodney W. Grimes DB *dbp;
8358f0484fSRodney W. Grimes DBT data, key, keydata;
8458f0484fSRodney W. Grimes size_t len;
85f1e396bcSPaul Traina int ch, oflags, sflag;
86f1e396bcSPaul Traina char *fname, *infoarg, *p, *t, buf[8 * 1024];
8758f0484fSRodney W. Grimes
8858f0484fSRodney W. Grimes infoarg = NULL;
8958f0484fSRodney W. Grimes fname = NULL;
9058f0484fSRodney W. Grimes oflags = O_CREAT | O_RDWR;
91f1e396bcSPaul Traina sflag = 0;
928f9872ccSKevin Lo while ((ch = getopt(argc, argv, "f:i:lo:s")) != -1)
9358f0484fSRodney W. Grimes switch (ch) {
9458f0484fSRodney W. Grimes case 'f':
9558f0484fSRodney W. Grimes fname = optarg;
9658f0484fSRodney W. Grimes break;
9758f0484fSRodney W. Grimes case 'i':
9858f0484fSRodney W. Grimes infoarg = optarg;
9958f0484fSRodney W. Grimes break;
10058f0484fSRodney W. Grimes case 'l':
10158f0484fSRodney W. Grimes oflags |= DB_LOCK;
10258f0484fSRodney W. Grimes break;
10358f0484fSRodney W. Grimes case 'o':
10458f0484fSRodney W. Grimes if ((ofd = open(optarg,
10558f0484fSRodney W. Grimes O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
10658f0484fSRodney W. Grimes err("%s: %s", optarg, strerror(errno));
10758f0484fSRodney W. Grimes break;
108f1e396bcSPaul Traina case 's':
109f1e396bcSPaul Traina sflag = 1;
110f1e396bcSPaul Traina break;
11158f0484fSRodney W. Grimes case '?':
11258f0484fSRodney W. Grimes default:
11358f0484fSRodney W. Grimes usage();
11458f0484fSRodney W. Grimes }
11558f0484fSRodney W. Grimes argc -= optind;
11658f0484fSRodney W. Grimes argv += optind;
11758f0484fSRodney W. Grimes
11858f0484fSRodney W. Grimes if (argc != 2)
11958f0484fSRodney W. Grimes usage();
12058f0484fSRodney W. Grimes
12158f0484fSRodney W. Grimes /* Set the type. */
12258f0484fSRodney W. Grimes type = dbtype(*argv++);
12358f0484fSRodney W. Grimes
12458f0484fSRodney W. Grimes /* Open the descriptor file. */
125f1e396bcSPaul Traina if (strcmp(*argv, "-") && freopen(*argv, "r", stdin) == NULL)
12658f0484fSRodney W. Grimes err("%s: %s", *argv, strerror(errno));
12758f0484fSRodney W. Grimes
12858f0484fSRodney W. Grimes /* Set up the db structure as necessary. */
12958f0484fSRodney W. Grimes if (infoarg == NULL)
13058f0484fSRodney W. Grimes infop = NULL;
13158f0484fSRodney W. Grimes else
13258f0484fSRodney W. Grimes for (p = strtok(infoarg, ",\t "); p != NULL;
13358f0484fSRodney W. Grimes p = strtok(0, ",\t "))
13458f0484fSRodney W. Grimes if (*p != '\0')
13558f0484fSRodney W. Grimes infop = setinfo(type, p);
13658f0484fSRodney W. Grimes
137f1e396bcSPaul Traina /*
138f1e396bcSPaul Traina * Open the DB. Delete any preexisting copy, you almost never
139f1e396bcSPaul Traina * want it around, and it often screws up tests.
140f1e396bcSPaul Traina */
14158f0484fSRodney W. Grimes if (fname == NULL) {
14258f0484fSRodney W. Grimes p = getenv("TMPDIR");
14358f0484fSRodney W. Grimes if (p == NULL)
14458f0484fSRodney W. Grimes p = "/var/tmp";
145099d8832SKris Kennaway (void)snprintf(buf, sizeof(buf), "%s/__dbtest", p);
14658f0484fSRodney W. Grimes fname = buf;
14758f0484fSRodney W. Grimes (void)unlink(buf);
148f1e396bcSPaul Traina } else if (!sflag)
149f1e396bcSPaul Traina (void)unlink(fname);
150f1e396bcSPaul Traina
15158f0484fSRodney W. Grimes if ((dbp = dbopen(fname,
15258f0484fSRodney W. Grimes oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL)
15358f0484fSRodney W. Grimes err("dbopen: %s", strerror(errno));
15458f0484fSRodney W. Grimes XXdbp = dbp;
15558f0484fSRodney W. Grimes
15658f0484fSRodney W. Grimes state = COMMAND;
15758f0484fSRodney W. Grimes for (lineno = 1;
15858f0484fSRodney W. Grimes (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
159f1e396bcSPaul Traina /* Delete the newline, displaying the key/data is easier. */
160f1e396bcSPaul Traina if (ofd == STDOUT_FILENO && (t = strchr(p, '\n')) != NULL)
161f1e396bcSPaul Traina *t = '\0';
162f1e396bcSPaul Traina if ((len = strlen(buf)) == 0 || isspace(*p) || *p == '#')
163f1e396bcSPaul Traina continue;
164f1e396bcSPaul Traina
165f1e396bcSPaul Traina /* Convenient gdb break point. */
166f1e396bcSPaul Traina if (XXlineno == lineno)
167f1e396bcSPaul Traina XXlineno = 1;
16858f0484fSRodney W. Grimes switch (*p) {
16958f0484fSRodney W. Grimes case 'c': /* compare */
17058f0484fSRodney W. Grimes if (state != COMMAND)
17158f0484fSRodney W. Grimes err("line %lu: not expecting command", lineno);
17258f0484fSRodney W. Grimes state = KEY;
17358f0484fSRodney W. Grimes command = COMPARE;
17458f0484fSRodney W. Grimes break;
17558f0484fSRodney W. Grimes case 'e': /* echo */
17658f0484fSRodney W. Grimes if (state != COMMAND)
17758f0484fSRodney W. Grimes err("line %lu: not expecting command", lineno);
17858f0484fSRodney W. Grimes /* Don't display the newline, if CR at EOL. */
17958f0484fSRodney W. Grimes if (p[len - 2] == '\r')
18058f0484fSRodney W. Grimes --len;
181f1e396bcSPaul Traina if (write(ofd, p + 1, len - 1) != len - 1 ||
182f1e396bcSPaul Traina write(ofd, "\n", 1) != 1)
18358f0484fSRodney W. Grimes err("write: %s", strerror(errno));
18458f0484fSRodney W. Grimes break;
18558f0484fSRodney W. Grimes case 'g': /* get */
18658f0484fSRodney W. Grimes if (state != COMMAND)
18758f0484fSRodney W. Grimes err("line %lu: not expecting command", lineno);
18858f0484fSRodney W. Grimes state = KEY;
18958f0484fSRodney W. Grimes command = GET;
19058f0484fSRodney W. Grimes break;
19158f0484fSRodney W. Grimes case 'p': /* put */
19258f0484fSRodney W. Grimes if (state != COMMAND)
19358f0484fSRodney W. Grimes err("line %lu: not expecting command", lineno);
19458f0484fSRodney W. Grimes state = KEY;
19558f0484fSRodney W. Grimes command = PUT;
19658f0484fSRodney W. Grimes break;
19758f0484fSRodney W. Grimes case 'r': /* remove */
19858f0484fSRodney W. Grimes if (state != COMMAND)
19958f0484fSRodney W. Grimes err("line %lu: not expecting command", lineno);
200f1e396bcSPaul Traina if (flags == R_CURSOR) {
201f1e396bcSPaul Traina rem(dbp, &key);
202f1e396bcSPaul Traina state = COMMAND;
203f1e396bcSPaul Traina } else {
20458f0484fSRodney W. Grimes state = KEY;
20558f0484fSRodney W. Grimes command = REMOVE;
206f1e396bcSPaul Traina }
207f1e396bcSPaul Traina break;
208f1e396bcSPaul Traina case 'S': /* sync */
209f1e396bcSPaul Traina if (state != COMMAND)
210f1e396bcSPaul Traina err("line %lu: not expecting command", lineno);
211f1e396bcSPaul Traina synk(dbp);
212f1e396bcSPaul Traina state = COMMAND;
21358f0484fSRodney W. Grimes break;
21458f0484fSRodney W. Grimes case 's': /* seq */
21558f0484fSRodney W. Grimes if (state != COMMAND)
21658f0484fSRodney W. Grimes err("line %lu: not expecting command", lineno);
21758f0484fSRodney W. Grimes if (flags == R_CURSOR) {
21858f0484fSRodney W. Grimes state = KEY;
21958f0484fSRodney W. Grimes command = SEQ;
22058f0484fSRodney W. Grimes } else
22158f0484fSRodney W. Grimes seq(dbp, &key);
22258f0484fSRodney W. Grimes break;
22358f0484fSRodney W. Grimes case 'f':
22458f0484fSRodney W. Grimes flags = setflags(p + 1);
22558f0484fSRodney W. Grimes break;
22658f0484fSRodney W. Grimes case 'D': /* data file */
22758f0484fSRodney W. Grimes if (state != DATA)
22858f0484fSRodney W. Grimes err("line %lu: not expecting data", lineno);
22958f0484fSRodney W. Grimes data.data = rfile(p + 1, &data.size);
23058f0484fSRodney W. Grimes goto ldata;
23158f0484fSRodney W. Grimes case 'd': /* data */
23258f0484fSRodney W. Grimes if (state != DATA)
23358f0484fSRodney W. Grimes err("line %lu: not expecting data", lineno);
23458f0484fSRodney W. Grimes data.data = xmalloc(p + 1, len - 1);
23558f0484fSRodney W. Grimes data.size = len - 1;
23658f0484fSRodney W. Grimes ldata: switch (command) {
23758f0484fSRodney W. Grimes case COMPARE:
23858f0484fSRodney W. Grimes compare(&keydata, &data);
23958f0484fSRodney W. Grimes break;
24058f0484fSRodney W. Grimes case PUT:
24158f0484fSRodney W. Grimes put(dbp, &key, &data);
24258f0484fSRodney W. Grimes break;
24358f0484fSRodney W. Grimes default:
24458f0484fSRodney W. Grimes err("line %lu: command doesn't take data",
24558f0484fSRodney W. Grimes lineno);
24658f0484fSRodney W. Grimes }
24758f0484fSRodney W. Grimes if (type != DB_RECNO)
24858f0484fSRodney W. Grimes free(key.data);
24958f0484fSRodney W. Grimes free(data.data);
25058f0484fSRodney W. Grimes state = COMMAND;
25158f0484fSRodney W. Grimes break;
25258f0484fSRodney W. Grimes case 'K': /* key file */
25358f0484fSRodney W. Grimes if (state != KEY)
25458f0484fSRodney W. Grimes err("line %lu: not expecting a key", lineno);
25558f0484fSRodney W. Grimes if (type == DB_RECNO)
25658f0484fSRodney W. Grimes err("line %lu: 'K' not available for recno",
25758f0484fSRodney W. Grimes lineno);
25858f0484fSRodney W. Grimes key.data = rfile(p + 1, &key.size);
25958f0484fSRodney W. Grimes goto lkey;
26058f0484fSRodney W. Grimes case 'k': /* key */
26158f0484fSRodney W. Grimes if (state != KEY)
26258f0484fSRodney W. Grimes err("line %lu: not expecting a key", lineno);
26358f0484fSRodney W. Grimes if (type == DB_RECNO) {
26458f0484fSRodney W. Grimes static recno_t recno;
26558f0484fSRodney W. Grimes recno = atoi(p + 1);
26658f0484fSRodney W. Grimes key.data = &recno;
26758f0484fSRodney W. Grimes key.size = sizeof(recno);
26858f0484fSRodney W. Grimes } else {
26958f0484fSRodney W. Grimes key.data = xmalloc(p + 1, len - 1);
27058f0484fSRodney W. Grimes key.size = len - 1;
27158f0484fSRodney W. Grimes }
27258f0484fSRodney W. Grimes lkey: switch (command) {
27358f0484fSRodney W. Grimes case COMPARE:
27458f0484fSRodney W. Grimes getdata(dbp, &key, &keydata);
27558f0484fSRodney W. Grimes state = DATA;
27658f0484fSRodney W. Grimes break;
27758f0484fSRodney W. Grimes case GET:
27858f0484fSRodney W. Grimes get(dbp, &key);
27958f0484fSRodney W. Grimes if (type != DB_RECNO)
28058f0484fSRodney W. Grimes free(key.data);
28158f0484fSRodney W. Grimes state = COMMAND;
28258f0484fSRodney W. Grimes break;
28358f0484fSRodney W. Grimes case PUT:
28458f0484fSRodney W. Grimes state = DATA;
28558f0484fSRodney W. Grimes break;
28658f0484fSRodney W. Grimes case REMOVE:
28758f0484fSRodney W. Grimes rem(dbp, &key);
288f1e396bcSPaul Traina if ((type != DB_RECNO) && (flags != R_CURSOR))
28958f0484fSRodney W. Grimes free(key.data);
29058f0484fSRodney W. Grimes state = COMMAND;
29158f0484fSRodney W. Grimes break;
29258f0484fSRodney W. Grimes case SEQ:
29358f0484fSRodney W. Grimes seq(dbp, &key);
294f1e396bcSPaul Traina if ((type != DB_RECNO) && (flags != R_CURSOR))
29558f0484fSRodney W. Grimes free(key.data);
29658f0484fSRodney W. Grimes state = COMMAND;
29758f0484fSRodney W. Grimes break;
29858f0484fSRodney W. Grimes default:
29958f0484fSRodney W. Grimes err("line %lu: command doesn't take a key",
30058f0484fSRodney W. Grimes lineno);
30158f0484fSRodney W. Grimes }
30258f0484fSRodney W. Grimes break;
30358f0484fSRodney W. Grimes case 'o':
30458f0484fSRodney W. Grimes dump(dbp, p[1] == 'r');
30558f0484fSRodney W. Grimes break;
30658f0484fSRodney W. Grimes default:
30758f0484fSRodney W. Grimes err("line %lu: %s: unknown command character",
308f1e396bcSPaul Traina lineno, p);
30958f0484fSRodney W. Grimes }
31058f0484fSRodney W. Grimes }
31158f0484fSRodney W. Grimes #ifdef STATISTICS
312f1e396bcSPaul Traina /*
313f1e396bcSPaul Traina * -l must be used (DB_LOCK must be set) for this to be
314f1e396bcSPaul Traina * used, otherwise a page will be locked and it will fail.
315f1e396bcSPaul Traina */
316f1e396bcSPaul Traina if (type == DB_BTREE && oflags & DB_LOCK)
31758f0484fSRodney W. Grimes __bt_stat(dbp);
31858f0484fSRodney W. Grimes #endif
31958f0484fSRodney W. Grimes if (dbp->close(dbp))
32058f0484fSRodney W. Grimes err("db->close: %s", strerror(errno));
32158f0484fSRodney W. Grimes (void)close(ofd);
32258f0484fSRodney W. Grimes exit(0);
32358f0484fSRodney W. Grimes }
32458f0484fSRodney W. Grimes
32558f0484fSRodney W. Grimes #define NOOVERWRITE "put failed, would overwrite key\n"
32658f0484fSRodney W. Grimes
32758f0484fSRodney W. Grimes void
compare(db1,db2)32858f0484fSRodney W. Grimes compare(db1, db2)
32958f0484fSRodney W. Grimes DBT *db1, *db2;
33058f0484fSRodney W. Grimes {
3318fb3f3f6SDavid E. O'Brien size_t len;
3328fb3f3f6SDavid E. O'Brien u_char *p1, *p2;
33358f0484fSRodney W. Grimes
33458f0484fSRodney W. Grimes if (db1->size != db2->size)
33558f0484fSRodney W. Grimes printf("compare failed: key->data len %lu != data len %lu\n",
33658f0484fSRodney W. Grimes db1->size, db2->size);
33758f0484fSRodney W. Grimes
33858f0484fSRodney W. Grimes len = MIN(db1->size, db2->size);
33958f0484fSRodney W. Grimes for (p1 = db1->data, p2 = db2->data; len--;)
34058f0484fSRodney W. Grimes if (*p1++ != *p2++) {
34158f0484fSRodney W. Grimes printf("compare failed at offset %d\n",
34258f0484fSRodney W. Grimes p1 - (u_char *)db1->data);
34358f0484fSRodney W. Grimes break;
34458f0484fSRodney W. Grimes }
34558f0484fSRodney W. Grimes }
34658f0484fSRodney W. Grimes
34758f0484fSRodney W. Grimes void
get(dbp,kp)34858f0484fSRodney W. Grimes get(dbp, kp)
34958f0484fSRodney W. Grimes DB *dbp;
35058f0484fSRodney W. Grimes DBT *kp;
35158f0484fSRodney W. Grimes {
35258f0484fSRodney W. Grimes DBT data;
35358f0484fSRodney W. Grimes
35458f0484fSRodney W. Grimes switch (dbp->get(dbp, kp, &data, flags)) {
35558f0484fSRodney W. Grimes case 0:
35658f0484fSRodney W. Grimes (void)write(ofd, data.data, data.size);
357f1e396bcSPaul Traina if (ofd == STDOUT_FILENO)
358f1e396bcSPaul Traina (void)write(ofd, "\n", 1);
35958f0484fSRodney W. Grimes break;
36058f0484fSRodney W. Grimes case -1:
36158f0484fSRodney W. Grimes err("line %lu: get: %s", lineno, strerror(errno));
36258f0484fSRodney W. Grimes /* NOTREACHED */
36358f0484fSRodney W. Grimes case 1:
364f1e396bcSPaul Traina #define NOSUCHKEY "get failed, no such key\n"
365f1e396bcSPaul Traina if (ofd != STDOUT_FILENO)
36658f0484fSRodney W. Grimes (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
367f1e396bcSPaul Traina else
368f1e396bcSPaul Traina (void)fprintf(stderr, "%d: %.*s: %s",
369f1e396bcSPaul Traina lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
370f1e396bcSPaul Traina #undef NOSUCHKEY
37158f0484fSRodney W. Grimes break;
37258f0484fSRodney W. Grimes }
37358f0484fSRodney W. Grimes }
37458f0484fSRodney W. Grimes
37558f0484fSRodney W. Grimes void
getdata(dbp,kp,dp)37658f0484fSRodney W. Grimes getdata(dbp, kp, dp)
37758f0484fSRodney W. Grimes DB *dbp;
37858f0484fSRodney W. Grimes DBT *kp, *dp;
37958f0484fSRodney W. Grimes {
38058f0484fSRodney W. Grimes switch (dbp->get(dbp, kp, dp, flags)) {
38158f0484fSRodney W. Grimes case 0:
38258f0484fSRodney W. Grimes return;
38358f0484fSRodney W. Grimes case -1:
38458f0484fSRodney W. Grimes err("line %lu: getdata: %s", lineno, strerror(errno));
38558f0484fSRodney W. Grimes /* NOTREACHED */
38658f0484fSRodney W. Grimes case 1:
387f1e396bcSPaul Traina err("line %lu: getdata failed, no such key", lineno);
38858f0484fSRodney W. Grimes /* NOTREACHED */
38958f0484fSRodney W. Grimes }
39058f0484fSRodney W. Grimes }
39158f0484fSRodney W. Grimes
39258f0484fSRodney W. Grimes void
put(dbp,kp,dp)39358f0484fSRodney W. Grimes put(dbp, kp, dp)
39458f0484fSRodney W. Grimes DB *dbp;
39558f0484fSRodney W. Grimes DBT *kp, *dp;
39658f0484fSRodney W. Grimes {
39758f0484fSRodney W. Grimes switch (dbp->put(dbp, kp, dp, flags)) {
39858f0484fSRodney W. Grimes case 0:
39958f0484fSRodney W. Grimes break;
40058f0484fSRodney W. Grimes case -1:
40158f0484fSRodney W. Grimes err("line %lu: put: %s", lineno, strerror(errno));
40258f0484fSRodney W. Grimes /* NOTREACHED */
40358f0484fSRodney W. Grimes case 1:
40458f0484fSRodney W. Grimes (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
40558f0484fSRodney W. Grimes break;
40658f0484fSRodney W. Grimes }
40758f0484fSRodney W. Grimes }
40858f0484fSRodney W. Grimes
40958f0484fSRodney W. Grimes void
rem(dbp,kp)41058f0484fSRodney W. Grimes rem(dbp, kp)
41158f0484fSRodney W. Grimes DB *dbp;
41258f0484fSRodney W. Grimes DBT *kp;
41358f0484fSRodney W. Grimes {
41458f0484fSRodney W. Grimes switch (dbp->del(dbp, kp, flags)) {
41558f0484fSRodney W. Grimes case 0:
41658f0484fSRodney W. Grimes break;
41758f0484fSRodney W. Grimes case -1:
418f1e396bcSPaul Traina err("line %lu: rem: %s", lineno, strerror(errno));
41958f0484fSRodney W. Grimes /* NOTREACHED */
42058f0484fSRodney W. Grimes case 1:
421f1e396bcSPaul Traina #define NOSUCHKEY "rem failed, no such key\n"
422f1e396bcSPaul Traina if (ofd != STDOUT_FILENO)
42358f0484fSRodney W. Grimes (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
424f1e396bcSPaul Traina else if (flags != R_CURSOR)
425f1e396bcSPaul Traina (void)fprintf(stderr, "%d: %.*s: %s",
426f1e396bcSPaul Traina lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
427f1e396bcSPaul Traina else
428f1e396bcSPaul Traina (void)fprintf(stderr,
429f1e396bcSPaul Traina "%d: rem of cursor failed\n", lineno);
430f1e396bcSPaul Traina #undef NOSUCHKEY
43158f0484fSRodney W. Grimes break;
43258f0484fSRodney W. Grimes }
43358f0484fSRodney W. Grimes }
43458f0484fSRodney W. Grimes
43558f0484fSRodney W. Grimes void
synk(dbp)436f1e396bcSPaul Traina synk(dbp)
437f1e396bcSPaul Traina DB *dbp;
438f1e396bcSPaul Traina {
439f1e396bcSPaul Traina switch (dbp->sync(dbp, flags)) {
440f1e396bcSPaul Traina case 0:
441f1e396bcSPaul Traina break;
442f1e396bcSPaul Traina case -1:
443f1e396bcSPaul Traina err("line %lu: synk: %s", lineno, strerror(errno));
444f1e396bcSPaul Traina /* NOTREACHED */
445f1e396bcSPaul Traina }
446f1e396bcSPaul Traina }
447f1e396bcSPaul Traina
448f1e396bcSPaul Traina void
seq(dbp,kp)44958f0484fSRodney W. Grimes seq(dbp, kp)
45058f0484fSRodney W. Grimes DB *dbp;
45158f0484fSRodney W. Grimes DBT *kp;
45258f0484fSRodney W. Grimes {
45358f0484fSRodney W. Grimes DBT data;
45458f0484fSRodney W. Grimes
45558f0484fSRodney W. Grimes switch (dbp->seq(dbp, kp, &data, flags)) {
45658f0484fSRodney W. Grimes case 0:
45758f0484fSRodney W. Grimes (void)write(ofd, data.data, data.size);
458f1e396bcSPaul Traina if (ofd == STDOUT_FILENO)
459f1e396bcSPaul Traina (void)write(ofd, "\n", 1);
46058f0484fSRodney W. Grimes break;
46158f0484fSRodney W. Grimes case -1:
46258f0484fSRodney W. Grimes err("line %lu: seq: %s", lineno, strerror(errno));
46358f0484fSRodney W. Grimes /* NOTREACHED */
46458f0484fSRodney W. Grimes case 1:
465f1e396bcSPaul Traina #define NOSUCHKEY "seq failed, no such key\n"
466f1e396bcSPaul Traina if (ofd != STDOUT_FILENO)
46758f0484fSRodney W. Grimes (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
468f1e396bcSPaul Traina else if (flags == R_CURSOR)
469f1e396bcSPaul Traina (void)fprintf(stderr, "%d: %.*s: %s",
470f1e396bcSPaul Traina lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
471f1e396bcSPaul Traina else
472f1e396bcSPaul Traina (void)fprintf(stderr,
473f1e396bcSPaul Traina "%d: seq (%s) failed\n", lineno, sflags(flags));
474f1e396bcSPaul Traina #undef NOSUCHKEY
47558f0484fSRodney W. Grimes break;
47658f0484fSRodney W. Grimes }
47758f0484fSRodney W. Grimes }
47858f0484fSRodney W. Grimes
47958f0484fSRodney W. Grimes void
dump(dbp,rev)48058f0484fSRodney W. Grimes dump(dbp, rev)
48158f0484fSRodney W. Grimes DB *dbp;
48258f0484fSRodney W. Grimes int rev;
48358f0484fSRodney W. Grimes {
48458f0484fSRodney W. Grimes DBT key, data;
48558f0484fSRodney W. Grimes int flags, nflags;
48658f0484fSRodney W. Grimes
48758f0484fSRodney W. Grimes if (rev) {
48858f0484fSRodney W. Grimes flags = R_LAST;
48958f0484fSRodney W. Grimes nflags = R_PREV;
49058f0484fSRodney W. Grimes } else {
49158f0484fSRodney W. Grimes flags = R_FIRST;
49258f0484fSRodney W. Grimes nflags = R_NEXT;
49358f0484fSRodney W. Grimes }
49458f0484fSRodney W. Grimes for (;; flags = nflags)
49558f0484fSRodney W. Grimes switch (dbp->seq(dbp, &key, &data, flags)) {
49658f0484fSRodney W. Grimes case 0:
49758f0484fSRodney W. Grimes (void)write(ofd, data.data, data.size);
498f1e396bcSPaul Traina if (ofd == STDOUT_FILENO)
499f1e396bcSPaul Traina (void)write(ofd, "\n", 1);
50058f0484fSRodney W. Grimes break;
50158f0484fSRodney W. Grimes case 1:
50258f0484fSRodney W. Grimes goto done;
50358f0484fSRodney W. Grimes case -1:
50458f0484fSRodney W. Grimes err("line %lu: (dump) seq: %s",
50558f0484fSRodney W. Grimes lineno, strerror(errno));
50658f0484fSRodney W. Grimes /* NOTREACHED */
50758f0484fSRodney W. Grimes }
50858f0484fSRodney W. Grimes done: return;
50958f0484fSRodney W. Grimes }
51058f0484fSRodney W. Grimes
51158f0484fSRodney W. Grimes u_int
setflags(s)51258f0484fSRodney W. Grimes setflags(s)
51358f0484fSRodney W. Grimes char *s;
51458f0484fSRodney W. Grimes {
51558f0484fSRodney W. Grimes char *p, *index();
51658f0484fSRodney W. Grimes
51758f0484fSRodney W. Grimes for (; isspace(*s); ++s);
518f1e396bcSPaul Traina if (*s == '\n' || *s == '\0')
51958f0484fSRodney W. Grimes return (0);
52058f0484fSRodney W. Grimes if ((p = index(s, '\n')) != NULL)
52158f0484fSRodney W. Grimes *p = '\0';
522f1e396bcSPaul Traina if (!strcmp(s, "R_CURSOR")) return (R_CURSOR);
523f1e396bcSPaul Traina if (!strcmp(s, "R_FIRST")) return (R_FIRST);
524f1e396bcSPaul Traina if (!strcmp(s, "R_IAFTER")) return (R_IAFTER);
525f1e396bcSPaul Traina if (!strcmp(s, "R_IBEFORE")) return (R_IBEFORE);
526f1e396bcSPaul Traina if (!strcmp(s, "R_LAST")) return (R_LAST);
527f1e396bcSPaul Traina if (!strcmp(s, "R_NEXT")) return (R_NEXT);
528f1e396bcSPaul Traina if (!strcmp(s, "R_NOOVERWRITE")) return (R_NOOVERWRITE);
529f1e396bcSPaul Traina if (!strcmp(s, "R_PREV")) return (R_PREV);
530f1e396bcSPaul Traina if (!strcmp(s, "R_SETCURSOR")) return (R_SETCURSOR);
531f1e396bcSPaul Traina
53258f0484fSRodney W. Grimes err("line %lu: %s: unknown flag", lineno, s);
53358f0484fSRodney W. Grimes /* NOTREACHED */
53458f0484fSRodney W. Grimes }
53558f0484fSRodney W. Grimes
536f1e396bcSPaul Traina char *
sflags(flags)537f1e396bcSPaul Traina sflags(flags)
538f1e396bcSPaul Traina int flags;
539f1e396bcSPaul Traina {
540f1e396bcSPaul Traina switch (flags) {
541f1e396bcSPaul Traina case R_CURSOR: return ("R_CURSOR");
542f1e396bcSPaul Traina case R_FIRST: return ("R_FIRST");
543f1e396bcSPaul Traina case R_IAFTER: return ("R_IAFTER");
544f1e396bcSPaul Traina case R_IBEFORE: return ("R_IBEFORE");
545f1e396bcSPaul Traina case R_LAST: return ("R_LAST");
546f1e396bcSPaul Traina case R_NEXT: return ("R_NEXT");
547f1e396bcSPaul Traina case R_NOOVERWRITE: return ("R_NOOVERWRITE");
548f1e396bcSPaul Traina case R_PREV: return ("R_PREV");
549f1e396bcSPaul Traina case R_SETCURSOR: return ("R_SETCURSOR");
550f1e396bcSPaul Traina }
551f1e396bcSPaul Traina
552f1e396bcSPaul Traina return ("UNKNOWN!");
553f1e396bcSPaul Traina }
554f1e396bcSPaul Traina
55558f0484fSRodney W. Grimes DBTYPE
dbtype(s)55658f0484fSRodney W. Grimes dbtype(s)
55758f0484fSRodney W. Grimes char *s;
55858f0484fSRodney W. Grimes {
55958f0484fSRodney W. Grimes if (!strcmp(s, "btree"))
56058f0484fSRodney W. Grimes return (DB_BTREE);
56158f0484fSRodney W. Grimes if (!strcmp(s, "hash"))
56258f0484fSRodney W. Grimes return (DB_HASH);
56358f0484fSRodney W. Grimes if (!strcmp(s, "recno"))
56458f0484fSRodney W. Grimes return (DB_RECNO);
56558f0484fSRodney W. Grimes err("%s: unknown type (use btree, hash or recno)", s);
56658f0484fSRodney W. Grimes /* NOTREACHED */
56758f0484fSRodney W. Grimes }
56858f0484fSRodney W. Grimes
56958f0484fSRodney W. Grimes void *
setinfo(type,s)57058f0484fSRodney W. Grimes setinfo(type, s)
57158f0484fSRodney W. Grimes DBTYPE type;
57258f0484fSRodney W. Grimes char *s;
57358f0484fSRodney W. Grimes {
57458f0484fSRodney W. Grimes static BTREEINFO ib;
57558f0484fSRodney W. Grimes static HASHINFO ih;
57658f0484fSRodney W. Grimes static RECNOINFO rh;
57758f0484fSRodney W. Grimes char *eq, *index();
57858f0484fSRodney W. Grimes
57958f0484fSRodney W. Grimes if ((eq = index(s, '=')) == NULL)
58058f0484fSRodney W. Grimes err("%s: illegal structure set statement", s);
58158f0484fSRodney W. Grimes *eq++ = '\0';
58258f0484fSRodney W. Grimes if (!isdigit(*eq))
58358f0484fSRodney W. Grimes err("%s: structure set statement must be a number", s);
58458f0484fSRodney W. Grimes
58558f0484fSRodney W. Grimes switch (type) {
58658f0484fSRodney W. Grimes case DB_BTREE:
58758f0484fSRodney W. Grimes if (!strcmp("flags", s)) {
58858f0484fSRodney W. Grimes ib.flags = atoi(eq);
58958f0484fSRodney W. Grimes return (&ib);
59058f0484fSRodney W. Grimes }
59158f0484fSRodney W. Grimes if (!strcmp("cachesize", s)) {
59258f0484fSRodney W. Grimes ib.cachesize = atoi(eq);
59358f0484fSRodney W. Grimes return (&ib);
59458f0484fSRodney W. Grimes }
59558f0484fSRodney W. Grimes if (!strcmp("maxkeypage", s)) {
59658f0484fSRodney W. Grimes ib.maxkeypage = atoi(eq);
59758f0484fSRodney W. Grimes return (&ib);
59858f0484fSRodney W. Grimes }
59958f0484fSRodney W. Grimes if (!strcmp("minkeypage", s)) {
60058f0484fSRodney W. Grimes ib.minkeypage = atoi(eq);
60158f0484fSRodney W. Grimes return (&ib);
60258f0484fSRodney W. Grimes }
60358f0484fSRodney W. Grimes if (!strcmp("lorder", s)) {
60458f0484fSRodney W. Grimes ib.lorder = atoi(eq);
60558f0484fSRodney W. Grimes return (&ib);
60658f0484fSRodney W. Grimes }
60758f0484fSRodney W. Grimes if (!strcmp("psize", s)) {
60858f0484fSRodney W. Grimes ib.psize = atoi(eq);
60958f0484fSRodney W. Grimes return (&ib);
61058f0484fSRodney W. Grimes }
61158f0484fSRodney W. Grimes break;
61258f0484fSRodney W. Grimes case DB_HASH:
61358f0484fSRodney W. Grimes if (!strcmp("bsize", s)) {
61458f0484fSRodney W. Grimes ih.bsize = atoi(eq);
61558f0484fSRodney W. Grimes return (&ih);
61658f0484fSRodney W. Grimes }
61758f0484fSRodney W. Grimes if (!strcmp("ffactor", s)) {
61858f0484fSRodney W. Grimes ih.ffactor = atoi(eq);
61958f0484fSRodney W. Grimes return (&ih);
62058f0484fSRodney W. Grimes }
62158f0484fSRodney W. Grimes if (!strcmp("nelem", s)) {
62258f0484fSRodney W. Grimes ih.nelem = atoi(eq);
62358f0484fSRodney W. Grimes return (&ih);
62458f0484fSRodney W. Grimes }
62558f0484fSRodney W. Grimes if (!strcmp("cachesize", s)) {
62658f0484fSRodney W. Grimes ih.cachesize = atoi(eq);
62758f0484fSRodney W. Grimes return (&ih);
62858f0484fSRodney W. Grimes }
62958f0484fSRodney W. Grimes if (!strcmp("lorder", s)) {
63058f0484fSRodney W. Grimes ih.lorder = atoi(eq);
63158f0484fSRodney W. Grimes return (&ih);
63258f0484fSRodney W. Grimes }
63358f0484fSRodney W. Grimes break;
63458f0484fSRodney W. Grimes case DB_RECNO:
63558f0484fSRodney W. Grimes if (!strcmp("flags", s)) {
63658f0484fSRodney W. Grimes rh.flags = atoi(eq);
63758f0484fSRodney W. Grimes return (&rh);
63858f0484fSRodney W. Grimes }
63958f0484fSRodney W. Grimes if (!strcmp("cachesize", s)) {
64058f0484fSRodney W. Grimes rh.cachesize = atoi(eq);
64158f0484fSRodney W. Grimes return (&rh);
64258f0484fSRodney W. Grimes }
64358f0484fSRodney W. Grimes if (!strcmp("lorder", s)) {
64458f0484fSRodney W. Grimes rh.lorder = atoi(eq);
64558f0484fSRodney W. Grimes return (&rh);
64658f0484fSRodney W. Grimes }
64758f0484fSRodney W. Grimes if (!strcmp("reclen", s)) {
64858f0484fSRodney W. Grimes rh.reclen = atoi(eq);
64958f0484fSRodney W. Grimes return (&rh);
65058f0484fSRodney W. Grimes }
65158f0484fSRodney W. Grimes if (!strcmp("bval", s)) {
65258f0484fSRodney W. Grimes rh.bval = atoi(eq);
65358f0484fSRodney W. Grimes return (&rh);
65458f0484fSRodney W. Grimes }
65558f0484fSRodney W. Grimes if (!strcmp("psize", s)) {
65658f0484fSRodney W. Grimes rh.psize = atoi(eq);
65758f0484fSRodney W. Grimes return (&rh);
65858f0484fSRodney W. Grimes }
65958f0484fSRodney W. Grimes break;
66058f0484fSRodney W. Grimes }
66158f0484fSRodney W. Grimes err("%s: unknown structure value", s);
66258f0484fSRodney W. Grimes /* NOTREACHED */
66358f0484fSRodney W. Grimes }
66458f0484fSRodney W. Grimes
66558f0484fSRodney W. Grimes void *
rfile(name,lenp)66658f0484fSRodney W. Grimes rfile(name, lenp)
66758f0484fSRodney W. Grimes char *name;
66858f0484fSRodney W. Grimes size_t *lenp;
66958f0484fSRodney W. Grimes {
67058f0484fSRodney W. Grimes struct stat sb;
67158f0484fSRodney W. Grimes void *p;
67258f0484fSRodney W. Grimes int fd;
67358f0484fSRodney W. Grimes char *np, *index();
67458f0484fSRodney W. Grimes
67558f0484fSRodney W. Grimes for (; isspace(*name); ++name);
67658f0484fSRodney W. Grimes if ((np = index(name, '\n')) != NULL)
67758f0484fSRodney W. Grimes *np = '\0';
67858f0484fSRodney W. Grimes if ((fd = open(name, O_RDONLY, 0)) < 0 ||
67958f0484fSRodney W. Grimes fstat(fd, &sb))
68058f0484fSRodney W. Grimes err("%s: %s\n", name, strerror(errno));
68158f0484fSRodney W. Grimes #ifdef NOT_PORTABLE
68258f0484fSRodney W. Grimes if (sb.st_size > (off_t)SIZE_T_MAX)
68358f0484fSRodney W. Grimes err("%s: %s\n", name, strerror(E2BIG));
68458f0484fSRodney W. Grimes #endif
68558f0484fSRodney W. Grimes if ((p = (void *)malloc((u_int)sb.st_size)) == NULL)
68658f0484fSRodney W. Grimes err("%s", strerror(errno));
68758f0484fSRodney W. Grimes (void)read(fd, p, (int)sb.st_size);
68858f0484fSRodney W. Grimes *lenp = sb.st_size;
68958f0484fSRodney W. Grimes (void)close(fd);
69058f0484fSRodney W. Grimes return (p);
69158f0484fSRodney W. Grimes }
69258f0484fSRodney W. Grimes
69358f0484fSRodney W. Grimes void *
xmalloc(text,len)69458f0484fSRodney W. Grimes xmalloc(text, len)
69558f0484fSRodney W. Grimes char *text;
69658f0484fSRodney W. Grimes size_t len;
69758f0484fSRodney W. Grimes {
69858f0484fSRodney W. Grimes void *p;
69958f0484fSRodney W. Grimes
70058f0484fSRodney W. Grimes if ((p = (void *)malloc(len)) == NULL)
70158f0484fSRodney W. Grimes err("%s", strerror(errno));
70258f0484fSRodney W. Grimes memmove(p, text, len);
70358f0484fSRodney W. Grimes return (p);
70458f0484fSRodney W. Grimes }
70558f0484fSRodney W. Grimes
70658f0484fSRodney W. Grimes void
usage()70758f0484fSRodney W. Grimes usage()
70858f0484fSRodney W. Grimes {
70958f0484fSRodney W. Grimes (void)fprintf(stderr,
71058f0484fSRodney W. Grimes "usage: dbtest [-l] [-f file] [-i info] [-o file] type script\n");
71158f0484fSRodney W. Grimes exit(1);
71258f0484fSRodney W. Grimes }
71358f0484fSRodney W. Grimes
71458f0484fSRodney W. Grimes #include <stdarg.h>
71558f0484fSRodney W. Grimes
71658f0484fSRodney W. Grimes void
err(const char * fmt,...)71758f0484fSRodney W. Grimes err(const char *fmt, ...)
71858f0484fSRodney W. Grimes {
71958f0484fSRodney W. Grimes va_list ap;
720a82bbc73SAlfred Perlstein
72158f0484fSRodney W. Grimes va_start(ap, fmt);
72258f0484fSRodney W. Grimes (void)fprintf(stderr, "dbtest: ");
72358f0484fSRodney W. Grimes (void)vfprintf(stderr, fmt, ap);
72458f0484fSRodney W. Grimes va_end(ap);
72558f0484fSRodney W. Grimes (void)fprintf(stderr, "\n");
72658f0484fSRodney W. Grimes exit(1);
72758f0484fSRodney W. Grimes /* NOTREACHED */
72858f0484fSRodney W. Grimes }
729