2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 fprintf(stderr, "usage: %s [-d db] -l | -s | query",
39 print_entry(struct db *db, struct db_entry *entry, void *data)
41 printf("%-18s %s\n", entry->name, entry->descr);
46 main(int argc, char **argv)
51 int list = 0, stats = 0, docid = -1;
53 while ((ch = getopt(argc, argv, "d:lp:s")) != -1) {
62 docid = strtonum(optarg, 0, INT_MAX, &errstr);
64 errx(1, "document id is %s: %s", errstr,
83 if ((fd = open(dbpath, O_RDONLY)) == -1)
84 err(1, "can't open %s", dbpath);
86 if (pledge("stdio", NULL) == -1)
89 if (db_open(&db, fd) == -1)
93 if (db_listall(&db, print_entry, NULL) == -1)
98 if (db_stats(&db, &st) == -1)
100 printf("unique words = %zu\n", st.nwords);
101 printf("documents = %zu\n", st.ndocs);
102 printf("longest word = %s\n", st.longest_word);
103 printf("most popular = %s (%zu)\n", st.most_popular,
104 st.most_popular_ndocs);
105 } else if (docid != -1) {
108 if (db_doc_by_id(&db, docid, &e) == -1)
109 errx(1, "failed to fetch document #%d", docid);
110 print_entry(&db, &e, NULL);
114 if (fts(&db, *argv, print_entry, NULL) == -1)
115 errx(1, "fts failed");