2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)main.c 8.1 (Berkeley) 6/4/93
33 * $DragonFly: src/lib/libc/db/test/btree.tests/main.c,v 1.7 2005/11/12 23:01:55 swildner Exp $
36 #include <sys/param.h>
46 typedef struct cmd_table {
50 void (*func) (DB *, char **);
51 char *usage, *descrip;
57 void append (DB *, char **);
58 void bstat (DB *, char **);
59 void cursor (DB *, char **);
60 void delcur (DB *, char **);
61 void delete (DB *, char **);
62 void dump (DB *, char **);
63 void first (DB *, char **);
64 void get (DB *, char **);
65 void help (DB *, char **);
66 void iafter (DB *, char **);
67 void ibefore (DB *, char **);
68 void icursor (DB *, char **);
69 void insert (DB *, char **);
70 void keydata (DBT *, DBT *);
71 void last (DB *, char **);
72 void list (DB *, char **);
73 void load (DB *, char **);
74 void mstat (DB *, char **);
75 void next (DB *, char **);
76 int parse (char *, char **, int);
77 void previous (DB *, char **);
78 void show (DB *, char **);
82 cmd_table commands[] = {
83 "?", 0, 0, help, "help", NULL,
84 "a", 2, 1, append, "append key def", "append key with data def",
85 "b", 0, 0, bstat, "bstat", "stat btree",
86 "c", 1, 1, cursor, "cursor word", "move cursor to word",
87 "delc", 0, 0, delcur, "delcur", "delete key the cursor references",
88 "dele", 1, 1, delete, "delete word", "delete word",
89 "d", 0, 0, dump, "dump", "dump database",
90 "f", 0, 0, first, "first", "move cursor to first record",
91 "g", 1, 1, get, "get key", "locate key",
92 "h", 0, 0, help, "help", "print command summary",
93 "ia", 2, 1, iafter, "iafter key data", "insert data after key",
94 "ib", 2, 1, ibefore, "ibefore key data", "insert data before key",
95 "ic", 2, 1, icursor, "icursor key data", "replace cursor",
96 "in", 2, 1, insert, "insert key def", "insert key with data def",
97 "la", 0, 0, last, "last", "move cursor to last record",
98 "li", 1, 1, list, "list file", "list to a file",
99 "loa", 1, 0, load, "load file", NULL,
100 "loc", 1, 1, get, "get key", NULL,
101 "m", 0, 0, mstat, "mstat", "stat memory pool",
102 "n", 0, 0, next, "next", "move cursor forward one record",
103 "p", 0, 0, previous, "previous", "move cursor back one record",
104 "q", 0, 0, NULL, "quit", "quit",
105 "sh", 1, 0, show, "show page", "dump a page",
109 int recno; /* use record numbers */
110 char *dict = "words"; /* default dictionary */
114 main(int argc, char **argv)
131 while ((c = getopt(argc, argv, "bc:di:lp:ru")) != EOF) {
134 b.lorder = BIG_ENDIAN;
137 b.cachesize = atoi(optarg);
146 b.lorder = LITTLE_ENDIAN;
149 b.psize = atoi(optarg);
165 db = dbopen(*argv == NULL ? NULL : *argv, O_RDWR,
168 db = dbopen(*argv == NULL ? NULL : *argv, O_CREAT|O_RDWR,
172 fprintf(stderr, "dbopen: %s\n", strerror(errno));
186 char *lbuf, *argv[4], buf[512];
188 if ((ifp = fopen("/dev/tty", "r")) == NULL) {
190 "/dev/tty: %s\n", strerror(errno));
196 if ((lbuf = fgets(&buf[0], 512, ifp)) == NULL)
198 if (lbuf[0] == '\n') {
202 lbuf[strlen(lbuf) - 1] = '\0';
207 argc = parse(lbuf, &argv[0], 3);
211 for (i = 0; commands[i].cmd != NULL; i++)
212 if (strncmp(commands[i].cmd, argv[0],
213 strlen(commands[i].cmd)) == 0)
216 if (commands[i].cmd == NULL) {
218 "%s: command unknown ('help' for help)\n", lbuf);
222 if (commands[i].nargs != argc - 1) {
223 fprintf(stderr, "usage: %s\n", commands[i].usage);
227 if (recno && commands[i].rconv) {
228 static recno_t nlong;
229 nlong = atoi(argv[1]);
230 argv[1] = (char *)&nlong;
233 (*commands[i].func)(db, argv);
235 if ((db->sync)(db) == RET_ERROR)
237 else if ((db->close)(db) == RET_ERROR)
242 parse(char *lbuf, char **argv, int maxargc)
250 while (*c != '\0' && argc < maxargc) {
253 while (!isspace(*c) && *c != '\0') {
263 append(DB *db, char **argv)
270 "append only available for recno db's.\n");
274 key.size = sizeof(recno_t);
276 data.size = strlen(data.data);
277 status = (db->put)(db, &key, &data, R_APPEND);
280 perror("append/put");
283 printf("%s (duplicate key)\n", argv[1]);
291 cursor(DB *db, char **argv)
298 key.size = sizeof(recno_t);
300 key.size = strlen(argv[1]) + 1;
301 status = (*db->seq)(db, &key, &data, R_CURSOR);
304 perror("cursor/seq");
307 printf("key not found\n");
310 keydata(&key, &data);
316 delcur(DB *db, char **argv)
320 status = (*db->del)(db, NULL, R_CURSOR);
322 if (status == RET_ERROR)
323 perror("delcur/del");
327 delete(DB *db, char **argv)
334 key.size = sizeof(recno_t);
336 key.size = strlen(argv[1]) + 1;
338 status = (*db->del)(db, &key, 0);
341 perror("delete/del");
344 printf("key not found\n");
352 dump(DB *db, char **argv)
358 first(DB *db, char **argv)
363 status = (*db->seq)(db, &key, &data, R_FIRST);
370 printf("no more keys\n");
373 keydata(&key, &data);
379 get(DB *db, char **argv)
386 key.size = sizeof(recno_t);
388 key.size = strlen(argv[1]) + 1;
390 status = (*db->get)(db, &key, &data, 0);
397 printf("key not found\n");
400 keydata(&key, &data);
406 help(DB *db, char **argv)
410 for (i = 0; commands[i].cmd; i++)
411 if (commands[i].descrip)
413 commands[i].usage, commands[i].descrip);
417 iafter(DB *db, char **argv)
424 "iafter only available for recno db's.\n");
428 key.size = sizeof(recno_t);
430 data.size = strlen(data.data);
431 status = (db->put)(db, &key, &data, R_IAFTER);
434 perror("iafter/put");
437 printf("%s (duplicate key)\n", argv[1]);
445 ibefore(DB *db, char **argv)
452 "ibefore only available for recno db's.\n");
456 key.size = sizeof(recno_t);
458 data.size = strlen(data.data);
459 status = (db->put)(db, &key, &data, R_IBEFORE);
462 perror("ibefore/put");
465 printf("%s (duplicate key)\n", argv[1]);
473 icursor(DB *db, char **argv)
480 key.size = sizeof(recno_t);
482 key.size = strlen(argv[1]) + 1;
484 data.size = strlen(argv[2]) + 1;
486 status = (*db->put)(db, &key, &data, R_CURSOR);
489 perror("icursor/put");
492 printf("%s (duplicate key)\n", argv[1]);
500 insert(DB *db, char **argv)
507 key.size = sizeof(recno_t);
509 key.size = strlen(argv[1]) + 1;
511 data.size = strlen(argv[2]) + 1;
513 status = (*db->put)(db, &key, &data, R_NOOVERWRITE);
516 perror("insert/put");
519 printf("%s (duplicate key)\n", argv[1]);
527 last(DB *db, char **argv)
532 status = (*db->seq)(db, &key, &data, R_LAST);
539 printf("no more keys\n");
542 keydata(&key, &data);
548 list(DB *db, char **argv)
554 if ((fp = fopen(argv[1], "w")) == NULL) {
555 fprintf(stderr, "%s: %s\n", argv[1], strerror(errno));
558 status = (*db->seq)(db, &key, &data, R_FIRST);
559 while (status == RET_SUCCESS) {
560 fprintf(fp, "%s\n", key.data);
561 status = (*db->seq)(db, &key, &data, R_NEXT);
563 if (status == RET_ERROR)
569 load(DB *db, char **argv)
577 char *lp, buf[16 * 1024];
580 if ((fp = fopen(argv[1], "r")) == NULL) {
581 fprintf(stderr, "%s: %s\n", argv[1], strerror(errno));
584 printf("loading %s...\n", argv[1]);
586 for (cnt = 1; (lp = fgetline(fp, &len)) != NULL; ++cnt) {
589 key.size = sizeof(recno_t);
595 for (p = lp + len - 1, t = buf; p >= lp; *t++ = *p--);
601 status = (*db->put)(db, &key, &data, R_NOOVERWRITE);
609 "duplicate: %ld {%s}\n", cnt, data.data);
612 "duplicate: %ld {%s}\n", cnt, key.data);
622 next(DB *db, char **argv)
627 status = (*db->seq)(db, &key, &data, R_NEXT);
634 printf("no more keys\n");
637 keydata(&key, &data);
643 previous(DB *db, char **argv)
648 status = (*db->seq)(db, &key, &data, R_PREV);
652 perror("previous/seq");
655 printf("no more keys\n");
658 keydata(&key, &data);
664 show(DB *db, char **argv)
672 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) {
673 printf("getpage of %ld failed\n", pg);
680 mpool_put(t->bt_mp, h, 0);
684 bstat(DB *db, char **argv)
691 mstat(DB *db, char **argv)
694 mpool_stat(((BTREE *)db->internal)->bt_mp);
698 keydata(DBT *key, DBT *data)
700 if (!recno && key->size > 0)
701 printf("%s/", key->data);
703 printf("%s", data->data);
711 "usage: %s [-bdlu] [-c cache] [-i file] [-p page] [file]\n",