No empty .Rs/.Re
[netbsd-mini2440.git] / lib / libc / db / btree / tests / words.c
blobd9c83e2b34b374e54e71e372f27ffa4406348248
1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Mike Olson.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
34 * SUCH DAMAGE.
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.
45 #include <stdio.h>
46 #include <ctype.h>
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/file.h>
50 #include <db.h>
51 #include <btree.h>
53 #define DICTIONARY "/usr/share/dict/words"
55 typedef struct cmd_table {
56 char *cmd;
57 int nargs;
58 int (*func)();
59 char *descrip;
60 } cmd_table;
62 extern int cursor(), delcur(), delete(), first(), help(), insert();
63 extern int last(), lookup(), next(), previous();
65 cmd_table Commands[] = {
66 "cursor", 2, cursor,
67 "cursor <word>: point the scan cursor at <word>",
68 "delcur", 1, delcur,
69 "delcur: delete the word under the scan cursor",
70 "delete", 2, delete,
71 "delete <word>: delete <word> from the dictionary",
72 "first", 1, first,
73 "first: point the scan cursor at the first dictionary entry",
74 "help", 1, help,
75 "help: print this command summary",
76 "insert", 3, insert,
77 "insert <word> <def>: insert <word> into the dictionary with definition <def>",
78 "last", 1, last,
79 "last: point the scan cursor at the last dictionary entry",
80 "lookup", 2, lookup,
81 "lookup <word>: look up <word> in the dictionary",
82 "next", 1, next,
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]";
91 main(argc, argv)
92 int argc;
93 char **argv;
95 char *dbname;
96 int c;
97 char *progname;
98 extern int strcmp();
99 extern char *optarg;
100 extern int optind;
101 DB *t;
102 BTREEINFO b;
104 progname = *argv;
106 b.psize = 0;
107 b.cachesize = 0;
108 b.lorder = 0;
109 b.flags = R_DUP;
110 b.compare = strcmp;
112 while ((c = getopt(argc, argv, "p:c:ulb")) != EOF) {
113 switch (c) {
114 case 'p':
115 b.psize = atoi(optarg);
116 break;
118 case 'c':
119 b.cachesize = atoi(optarg);
120 break;
122 case 'u':
123 b.flags = 0;
124 break;
126 case 'l':
127 b.lorder = LITTLE_ENDIAN;
128 break;
130 case 'b':
131 b.lorder = BIG_ENDIAN;
132 break;
134 default:
135 fprintf(stderr, "%s: usage: %s\n", progname, Usage);
136 exit (1);
140 if (argv[optind] != (char *) NULL)
141 dbname = argv[optind];
143 if ((t = btree_open(dbname, O_CREAT|O_RDWR, 0600, &b)) == (DB *) NULL) {
144 perror(progname);
145 exit (1);
148 load(t);
150 user(t);
153 load(t)
154 DB *t;
156 char *lbuf;
157 int i, l;
158 int status;
159 FILE *fp;
160 DBT key;
161 DBT data;
162 char word[64];
163 char drow[64];
165 printf("loading %s...\n", DICTIONARY);
166 fflush(stdout);
167 if ((fp = fopen(DICTIONARY, "r")) == (FILE *) NULL) {
168 perror("/usr/dict/words");
169 (void) (*(t->close))(t->internal);
170 exit (1);
173 key.data = &word[0];
174 data.data = &drow[0];
175 while ((lbuf = fgets(word, 64, fp)) != (char *) NULL) {
176 l = strlen(lbuf) - 1;
177 lbuf[l] = '\0';
178 for (i = 0; i < l; i++)
179 drow[l - (i + 1)] = word[i];
180 drow[l] = '\0';
182 key.size = data.size = l + 1;
184 status = (*(t->put))(t->internal, &key, &data, R_NOOVERWRITE);
186 switch (status) {
187 case RET_SUCCESS:
188 break;
190 case RET_ERROR:
191 perror("put");
192 break;
194 case RET_SPECIAL:
195 fprintf(stderr, "%s is a duplicate key!\n", lbuf);
196 fflush(stderr);
197 break;
201 (void) fclose(fp);
202 printf("done\n");
203 fflush(stdout);
206 user(t)
207 DB *t;
209 char *lbuf;
210 int argc;
211 int i;
212 char *argv[4];
213 char buf[512];
215 for (;;) {
216 printf("> ");
217 fflush(stdout);
218 if ((lbuf = fgets(&buf[0], 512, stdin)) == (char *) NULL)
219 break;
220 lbuf[strlen(lbuf) - 1] = '\0';
222 if (strcmp(lbuf, "quit") == 0)
223 break;
225 argc = parse(lbuf, &argv[0], 3);
226 if (argc == 0)
227 continue;
229 for (i = 0; Commands[i].cmd != (char *) NULL; i++) {
230 if (strcmp(Commands[i].cmd, argv[0]) == 0)
231 break;
234 if (Commands[i].cmd == (char *) NULL) {
235 fprintf(stderr,
236 "%s: command unknown ('help' for help)\n",
237 lbuf);
238 fflush(stderr);
239 continue;
242 if (Commands[i].nargs != argc) {
243 fprintf(stderr, "arg count\n");
244 fflush(stderr);
245 continue;
248 switch (argc) {
249 case 1:
250 (*(Commands[i].func))(t);
251 break;
252 case 2:
253 (*(Commands[i].func))(t, argv[1]);
254 break;
255 case 3:
256 (*(Commands[i].func))(t, argv[1], argv[2]);
257 break;
258 case 4:
259 (*(Commands[i].func))(t, argv[1], argv[2], argv[3]);
260 break;
263 (void) (*(t->close))(t->internal);
264 exit (0);
268 parse(lbuf, argv, maxargc)
269 char *lbuf;
270 char **argv;
271 int maxargc;
273 int argc = 0;
274 char *c;
276 c = lbuf;
277 while (isspace(*c))
278 c++;
279 while (*c != '\0' && argc < maxargc) {
280 *argv++ = c;
281 argc++;
282 while (!isspace(*c) && *c != '\0') {
283 c++;
285 while (isspace(*c))
286 *c++ = '\0';
288 return (argc);
292 cursor(t, arg)
293 DB *t;
294 char *arg;
296 int status;
297 DBT key;
298 DBT data;
300 key.data = arg;
301 key.size = strlen(arg + 1);
302 status = (*(t->seq))(t->internal, &key, &data, R_CURSOR);
303 if (status == RET_SUCCESS)
304 show(&key, &data);
305 else
306 perror("cursor");
310 delcur(t)
311 DB *t;
313 int status;
315 status = (*(t->delete))(t->internal, (DBT *) NULL, R_CURSOR);
317 if (status == RET_ERROR)
318 perror("delcur");
322 delete(t, arg)
323 DB *t;
324 char *arg;
326 int status;
327 DBT key;
329 key.data = arg;
330 key.size = strlen(arg) + 1;
332 status = (*(t->delete))(t->internal, &key, 0);
333 switch (status) {
334 case RET_SUCCESS:
335 break;
337 case RET_ERROR:
338 perror("delete");
339 break;
341 case RET_SPECIAL:
342 fprintf(stderr, "%s not found\n", arg);
343 fflush(stderr);
344 break;
349 first(t)
350 DB *t;
352 int status;
353 DBT key;
354 DBT data;
356 status = (*(t->seq))(t->internal, &key, &data, R_FIRST);
358 switch (status) {
359 case RET_ERROR:
360 perror("first");
361 break;
363 case RET_SPECIAL:
364 printf("no more keys");
365 break;
367 case RET_SUCCESS:
368 show(&key, &data);
369 break;
373 help(t)
374 DB *t;
376 int i;
378 #ifdef lint
379 t = t;
380 #endif /* lint */
381 for (i = 0; Commands[i].cmd != (char *) NULL; i++)
382 printf("%s\n", Commands[i].descrip);
383 printf("type 'quit' to quit\n");
387 insert(t, arg, def)
388 DB *t;
389 char *arg;
390 char *def;
392 int status;
393 DBT key;
394 DBT data;
396 key.data = arg;
397 key.size = strlen(arg) + 1;
398 data.data = def;
399 data.size = strlen(def) + 1;
401 status = (*(t->put))(t->internal, &key, &data, R_NOOVERWRITE);
402 switch (status) {
403 case RET_SUCCESS:
404 break;
406 case RET_ERROR:
407 perror("put");
408 break;
410 case RET_SPECIAL:
411 fprintf(stderr, "%s is a duplicate key!\n", arg);
412 fflush(stderr);
413 break;
418 last(t)
419 DB *t;
421 int status;
422 DBT key;
423 DBT data;
425 status = (*(t->seq))(t->internal, &key, &data, R_LAST);
427 switch (status) {
428 case RET_ERROR:
429 perror("last");
430 break;
432 case RET_SPECIAL:
433 printf("no more keys");
434 break;
436 case RET_SUCCESS:
437 show(&key, &data);
438 break;
443 lookup(t, arg)
444 DB *t;
445 char *arg;
447 int status;
448 DBT key;
449 DBT data;
451 key.data = arg;
452 key.size = strlen(arg) + 1;
454 status = (*(t->get))(t->internal, &key, &data, 0);
456 switch (status) {
457 case RET_SPECIAL:
458 printf("not found\n");
459 break;
460 case RET_SUCCESS:
461 show(&key, &data);
462 break;
463 case RET_ERROR:
464 perror("get");
465 break;
470 next(t)
471 DB *t;
473 int status;
474 DBT key;
475 DBT data;
477 status = (*(t->seq))(t->internal, &key, &data, R_NEXT);
479 switch (status) {
480 case RET_ERROR:
481 perror("next");
482 break;
484 case RET_SPECIAL:
485 printf("no more keys");
486 break;
488 case RET_SUCCESS:
489 show(&key, &data);
490 break;
495 previous(t)
496 DB *t;
498 int status;
499 DBT key;
500 DBT data;
502 status = (*(t->seq))(t->internal, &key, &data, R_PREV);
504 switch (status) {
505 case RET_ERROR:
506 perror("previous");
507 break;
509 case RET_SPECIAL:
510 printf("no more keys");
511 break;
513 case RET_SUCCESS:
514 show(&key, &data);
515 break;
519 show(key, data)
520 DBT *key;
521 DBT *data;
523 if (key->size > 0)
524 printf("%s", key->data);
525 if (data->size > 0)
526 printf("/%s", data->data);
527 printf("\n");