2 * Copyright (c) 1990 The Regents of the University of California.
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)words.c 5.1 (Berkeley) 4/12/91";
39 #endif /* LIBC_SCCS and not lint */
42 * test1.c -- simple btree test program.
47 #include <sys/param.h>
48 #include <sys/types.h>
53 #define DICTIONARY "/usr/share/dict/words"
55 typedef struct cmd_table
{
62 extern int cursor(), delcur(), delete(), first(), help(), insert();
63 extern int last(), lookup(), next(), previous();
65 cmd_table Commands
[] = {
67 "cursor <word>: point the scan cursor at <word>",
69 "delcur: delete the word under the scan cursor",
71 "delete <word>: delete <word> from the dictionary",
73 "first: point the scan cursor at the first dictionary entry",
75 "help: print this command summary",
77 "insert <word> <def>: insert <word> into the dictionary with definition <def>",
79 "last: point the scan cursor at the last dictionary entry",
81 "lookup <word>: look up <word> in the dictionary",
83 "next: move the scan cursor forward one word",
84 "previous", 1, previous
,
85 "previous: move the scan cursor back one word",
86 (char *) NULL
, 0, NULL
, (char *) NULL
,
89 char *Usage
= "[-p pagesize] [-c cachesize] [-u] [-l|b|n] [dbname]";
112 while ((c
= getopt(argc
, argv
, "p:c:ulb")) != EOF
) {
115 b
.psize
= atoi(optarg
);
119 b
.cachesize
= atoi(optarg
);
127 b
.lorder
= LITTLE_ENDIAN
;
131 b
.lorder
= BIG_ENDIAN
;
135 fprintf(stderr
, "%s: usage: %s\n", progname
, Usage
);
140 if (argv
[optind
] != (char *) NULL
)
141 dbname
= argv
[optind
];
143 if ((t
= btree_open(dbname
, O_CREAT
|O_RDWR
, 0600, &b
)) == (DB
*) NULL
) {
165 printf("loading %s...\n", DICTIONARY
);
167 if ((fp
= fopen(DICTIONARY
, "r")) == (FILE *) NULL
) {
168 perror("/usr/dict/words");
169 (void) (*(t
->close
))(t
->internal
);
174 data
.data
= &drow
[0];
175 while ((lbuf
= fgets(word
, 64, fp
)) != (char *) NULL
) {
176 l
= strlen(lbuf
) - 1;
178 for (i
= 0; i
< l
; i
++)
179 drow
[l
- (i
+ 1)] = word
[i
];
182 key
.size
= data
.size
= l
+ 1;
184 status
= (*(t
->put
))(t
->internal
, &key
, &data
, R_NOOVERWRITE
);
195 fprintf(stderr
, "%s is a duplicate key!\n", lbuf
);
218 if ((lbuf
= fgets(&buf
[0], 512, stdin
)) == (char *) NULL
)
220 lbuf
[strlen(lbuf
) - 1] = '\0';
222 if (strcmp(lbuf
, "quit") == 0)
225 argc
= parse(lbuf
, &argv
[0], 3);
229 for (i
= 0; Commands
[i
].cmd
!= (char *) NULL
; i
++) {
230 if (strcmp(Commands
[i
].cmd
, argv
[0]) == 0)
234 if (Commands
[i
].cmd
== (char *) NULL
) {
236 "%s: command unknown ('help' for help)\n",
242 if (Commands
[i
].nargs
!= argc
) {
243 fprintf(stderr
, "arg count\n");
250 (*(Commands
[i
].func
))(t
);
253 (*(Commands
[i
].func
))(t
, argv
[1]);
256 (*(Commands
[i
].func
))(t
, argv
[1], argv
[2]);
259 (*(Commands
[i
].func
))(t
, argv
[1], argv
[2], argv
[3]);
263 (void) (*(t
->close
))(t
->internal
);
268 parse(lbuf
, argv
, maxargc
)
279 while (*c
!= '\0' && argc
< maxargc
) {
282 while (!isspace(*c
) && *c
!= '\0') {
301 key
.size
= strlen(arg
+ 1);
302 status
= (*(t
->seq
))(t
->internal
, &key
, &data
, R_CURSOR
);
303 if (status
== RET_SUCCESS
)
315 status
= (*(t
->delete))(t
->internal
, (DBT
*) NULL
, R_CURSOR
);
317 if (status
== RET_ERROR
)
330 key
.size
= strlen(arg
) + 1;
332 status
= (*(t
->delete))(t
->internal
, &key
, 0);
342 fprintf(stderr
, "%s not found\n", arg
);
356 status
= (*(t
->seq
))(t
->internal
, &key
, &data
, R_FIRST
);
364 printf("no more keys");
381 for (i
= 0; Commands
[i
].cmd
!= (char *) NULL
; i
++)
382 printf("%s\n", Commands
[i
].descrip
);
383 printf("type 'quit' to quit\n");
397 key
.size
= strlen(arg
) + 1;
399 data
.size
= strlen(def
) + 1;
401 status
= (*(t
->put
))(t
->internal
, &key
, &data
, R_NOOVERWRITE
);
411 fprintf(stderr
, "%s is a duplicate key!\n", arg
);
425 status
= (*(t
->seq
))(t
->internal
, &key
, &data
, R_LAST
);
433 printf("no more keys");
452 key
.size
= strlen(arg
) + 1;
454 status
= (*(t
->get
))(t
->internal
, &key
, &data
, 0);
458 printf("not found\n");
477 status
= (*(t
->seq
))(t
->internal
, &key
, &data
, R_NEXT
);
485 printf("no more keys");
502 status
= (*(t
->seq
))(t
->internal
, &key
, &data
, R_PREV
);
510 printf("no more keys");
524 printf("%s", key
->data
);
526 printf("/%s", data
->data
);