* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)main.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
typedef struct cmd_table
{
void (*func
) __P((DB
*, char **));
void append
__P((DB
*, char **));
void bstat
__P((DB
*, char **));
void cursor
__P((DB
*, char **));
void delcur
__P((DB
*, char **));
void delete __P((DB
*, char **));
void dump
__P((DB
*, char **));
void first
__P((DB
*, char **));
void get
__P((DB
*, char **));
void help
__P((DB
*, char **));
void iafter
__P((DB
*, char **));
void ibefore
__P((DB
*, char **));
void icursor
__P((DB
*, char **));
void insert
__P((DB
*, char **));
void keydata
__P((DBT
*, DBT
*));
void last
__P((DB
*, char **));
void list
__P((DB
*, char **));
void load
__P((DB
*, char **));
void mstat
__P((DB
*, char **));
void next
__P((DB
*, char **));
int parse
__P((char *, char **, int));
void previous
__P((DB
*, char **));
void show
__P((DB
*, char **));
"?", 0, 0, help
, "help", NULL
,
"a", 2, 1, append
, "append key def", "append key with data def",
"b", 0, 0, bstat
, "bstat", "stat btree",
"c", 1, 1, cursor
, "cursor word", "move cursor to word",
"delc", 0, 0, delcur
, "delcur", "delete key the cursor references",
"dele", 1, 1, delete, "delete word", "delete word",
"d", 0, 0, dump
, "dump", "dump database",
"f", 0, 0, first
, "first", "move cursor to first record",
"g", 1, 1, get
, "get key", "locate key",
"h", 0, 0, help
, "help", "print command summary",
"ia", 2, 1, iafter
, "iafter key data", "insert data after key",
"ib", 2, 1, ibefore
, "ibefore key data", "insert data before key",
"ic", 2, 1, icursor
, "icursor key data", "replace cursor",
"in", 2, 1, insert
, "insert key def", "insert key with data def",
"la", 0, 0, last
, "last", "move cursor to last record",
"li", 1, 1, list
, "list file", "list to a file",
"loa", 1, 0, load
, "load file", NULL
,
"loc", 1, 1, get
, "get key", NULL
,
"m", 0, 0, mstat
, "mstat", "stat memory pool",
"n", 0, 0, next
, "next", "move cursor forward one record",
"p", 0, 0, previous
, "previous", "move cursor back one record",
"q", 0, 0, NULL
, "quit", "quit",
"sh", 1, 0, show
, "show page", "dump a page",
int recno
; /* use record numbers */
char *dict
= "words"; /* default dictionary */
while ((c
= getopt(argc
, argv
, "bc:di:lp:ru")) != EOF
) {
b
.cachesize
= atoi(optarg
);
b
.lorder
= LITTLE_ENDIAN
;
db
= dbopen(*argv
== NULL
? NULL
: *argv
, O_RDWR
,
db
= dbopen(*argv
== NULL
? NULL
: *argv
, O_CREAT
|O_RDWR
,
(void)fprintf(stderr
, "dbopen: %s\n", strerror(errno
));
char *lbuf
, *argv
[4], buf
[512];
if ((ifp
= fopen("/dev/tty", "r")) == NULL
) {
"/dev/tty: %s\n", strerror(errno
));
if ((lbuf
= fgets(&buf
[0], 512, ifp
)) == NULL
)
lbuf
[strlen(lbuf
) - 1] = '\0';
argc
= parse(lbuf
, &argv
[0], 3);
for (i
= 0; commands
[i
].cmd
!= NULL
; i
++)
if (strncmp(commands
[i
].cmd
, argv
[0],
strlen(commands
[i
].cmd
)) == 0)
if (commands
[i
].cmd
== NULL
) {
"%s: command unknown ('help' for help)\n", lbuf
);
if (commands
[i
].nargs
!= argc
- 1) {
(void)fprintf(stderr
, "usage: %s\n", commands
[i
].usage
);
if (recno
&& commands
[i
].rconv
) {
argv
[1] = (char *)&nlong
;
(*commands
[i
].func
)(db
, argv
);
if ((db
->sync
)(db
) == RET_ERROR
)
else if ((db
->close
)(db
) == RET_ERROR
)
parse(lbuf
, argv
, maxargc
)
while (*c
!= '\0' && argc
< maxargc
) {
while (!isspace(*c
) && *c
!= '\0') {
"append only available for recno db's.\n");
key
.size
= sizeof(recno_t
);
data
.size
= strlen(data
.data
);
status
= (db
->put
)(db
, &key
, &data
, R_APPEND
);
(void)printf("%s (duplicate key)\n", argv
[1]);
key
.size
= sizeof(recno_t
);
key
.size
= strlen(argv
[1]) + 1;
status
= (*db
->seq
)(db
, &key
, &data
, R_CURSOR
);
(void)printf("key not found\n");
status
= (*db
->del
)(db
, NULL
, R_CURSOR
);
key
.size
= sizeof(recno_t
);
key
.size
= strlen(argv
[1]) + 1;
status
= (*db
->del
)(db
, &key
, 0);
(void)printf("key not found\n");
status
= (*db
->seq
)(db
, &key
, &data
, R_FIRST
);
(void)printf("no more keys\n");
key
.size
= sizeof(recno_t
);
key
.size
= strlen(argv
[1]) + 1;
status
= (*db
->get
)(db
, &key
, &data
, 0);
(void)printf("key not found\n");
for (i
= 0; commands
[i
].cmd
; i
++)
commands
[i
].usage
, commands
[i
].descrip
);
"iafter only available for recno db's.\n");
key
.size
= sizeof(recno_t
);
data
.size
= strlen(data
.data
);
status
= (db
->put
)(db
, &key
, &data
, R_IAFTER
);
(void)printf("%s (duplicate key)\n", argv
[1]);
"ibefore only available for recno db's.\n");
key
.size
= sizeof(recno_t
);
data
.size
= strlen(data
.data
);
status
= (db
->put
)(db
, &key
, &data
, R_IBEFORE
);
(void)printf("%s (duplicate key)\n", argv
[1]);
key
.size
= sizeof(recno_t
);
key
.size
= strlen(argv
[1]) + 1;
data
.size
= strlen(argv
[2]) + 1;
status
= (*db
->put
)(db
, &key
, &data
, R_CURSOR
);
(void)printf("%s (duplicate key)\n", argv
[1]);
key
.size
= sizeof(recno_t
);
key
.size
= strlen(argv
[1]) + 1;
data
.size
= strlen(argv
[2]) + 1;
status
= (*db
->put
)(db
, &key
, &data
, R_NOOVERWRITE
);
(void)printf("%s (duplicate key)\n", argv
[1]);
status
= (*db
->seq
)(db
, &key
, &data
, R_LAST
);
(void)printf("no more keys\n");
if ((fp
= fopen(argv
[1], "w")) == NULL
) {
(void)fprintf(stderr
, "%s: %s\n", argv
[1], strerror(errno
));
status
= (*db
->seq
)(db
, &key
, &data
, R_FIRST
);
while (status
== RET_SUCCESS
) {
(void)fprintf(fp
, "%s\n", key
.data
);
status
= (*db
->seq
)(db
, &key
, &data
, R_NEXT
);
char *lp
, buf
[16 * 1024];
if ((fp
= fopen(argv
[1], "r")) == NULL
) {
(void)fprintf(stderr
, "%s: %s\n", argv
[1], strerror(errno
));
(void)printf("loading %s...\n", argv
[1]);
for (cnt
= 1; (lp
= fgetline(fp
, &len
)) != NULL
; ++cnt
) {
key
.size
= sizeof(recno_t
);
for (p
= lp
+ len
- 1, t
= buf
; p
>= lp
; *t
++ = *p
--);
status
= (*db
->put
)(db
, &key
, &data
, R_NOOVERWRITE
);
"duplicate: %ld {%s}\n", cnt
, data
.data
);
"duplicate: %ld {%s}\n", cnt
, key
.data
);
status
= (*db
->seq
)(db
, &key
, &data
, R_NEXT
);
(void)printf("no more keys\n");
status
= (*db
->seq
)(db
, &key
, &data
, R_PREV
);
(void)printf("no more keys\n");
if ((h
= mpool_get(t
->bt_mp
, pg
, 0)) == NULL
) {
(void)printf("getpage of %ld failed\n", pg
);
mpool_put(t
->bt_mp
, h
, 0);
mpool_stat(((BTREE
*)db
->internal
)->bt_mp
);
if (!recno
&& key
->size
> 0)
(void)printf("%s/", key
->data
);
(void)printf("%s", data
->data
);
"usage: %s [-bdlu] [-c cache] [-i file] [-p page] [file]\n",