2 e2b5610c 2022-04-11 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 e2b5610c 2022-04-11 op * Permission to use, copy, modify, and distribute this software for any
5 e2b5610c 2022-04-11 op * purpose with or without fee is hereby granted, provided that the above
6 e2b5610c 2022-04-11 op * copyright notice and this permission notice appear in all copies.
8 e2b5610c 2022-04-11 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e2b5610c 2022-04-11 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e2b5610c 2022-04-11 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e2b5610c 2022-04-11 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e2b5610c 2022-04-11 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e2b5610c 2022-04-11 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e2b5610c 2022-04-11 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 e2b5610c 2022-04-11 op #include <err.h>
18 e2b5610c 2022-04-11 op #include <stdio.h>
19 e2b5610c 2022-04-11 op #include <stdlib.h>
21 e2b5610c 2022-04-11 op #include <sqlite3.h>
23 e2b5610c 2022-04-11 op #include "db.h"
24 e2b5610c 2022-04-11 op #include "dictionary.h"
25 e2b5610c 2022-04-11 op #include "tokenize.h"
27 e2b5610c 2022-04-11 op #include "mkftsidx.h"
29 e2b5610c 2022-04-11 op #ifndef SQLPORTS
30 e2b5610c 2022-04-11 op #define SQLPORTS "/usr/local/share/sqlports"
33 e2b5610c 2022-04-11 op #define QNUM "select count(*) from portsq;"
34 e2b5610c 2022-04-11 op #define QALL "select pkgstem, comment, descr_contents from portsq;"
37 e2b5610c 2022-04-11 op countports(sqlite3 *db)
39 e2b5610c 2022-04-11 op sqlite3_stmt *stmt;
40 e2b5610c 2022-04-11 op int r, n = -1;
42 e2b5610c 2022-04-11 op r = sqlite3_prepare_v2(db, QNUM, -1, &stmt, NULL);
43 e2b5610c 2022-04-11 op if (r != SQLITE_OK) {
44 e2b5610c 2022-04-11 op warnx("failed to prepare statement: %s",
45 e2b5610c 2022-04-11 op sqlite3_errstr(r));
49 e2b5610c 2022-04-11 op r = sqlite3_step(stmt);
50 e2b5610c 2022-04-11 op if (r == SQLITE_ROW)
51 e2b5610c 2022-04-11 op n = sqlite3_column_int(stmt, 0);
53 e2b5610c 2022-04-11 op sqlite3_finalize(stmt);
58 e2b5610c 2022-04-11 op idx_ports(struct dictionary *dict, struct db_entry **entries, size_t *len,
59 e2b5610c 2022-04-11 op int argc, char **argv)
61 e2b5610c 2022-04-11 op const char *dbpath;
63 e2b5610c 2022-04-11 op sqlite3_stmt *stmt;
69 e2b5610c 2022-04-11 op else if (argc == 1)
70 e2b5610c 2022-04-11 op dbpath = *argv;
72 e2b5610c 2022-04-11 op dbpath = SQLPORTS;
74 e2b5610c 2022-04-11 op if ((r = sqlite3_open(dbpath, &db)) != SQLITE_OK)
75 e2b5610c 2022-04-11 op errx(1, "can't open %s: %s", dbpath, sqlite3_errstr(r));
77 e2b5610c 2022-04-11 op if ((r = countports(db)) == -1 || r == 0) {
78 e2b5610c 2022-04-11 op warnx("error querying the db or empty portsq table!");
83 e2b5610c 2022-04-11 op if ((*entries = calloc(*len, sizeof(**entries))) == NULL)
84 e2b5610c 2022-04-11 op err(1, "calloc");
86 e2b5610c 2022-04-11 op r = sqlite3_prepare_v2(db, QALL, -1, &stmt, NULL);
87 e2b5610c 2022-04-11 op if (r != SQLITE_OK)
88 e2b5610c 2022-04-11 op errx(1, "failed to prepare statement: %s", sqlite3_errstr(r));
90 e2b5610c 2022-04-11 op for (i = 0; i < *len; ++i) {
91 e2b5610c 2022-04-11 op const char *pkgstem, *comment, *descr;
92 e2b5610c 2022-04-11 op char *doc, **toks;
94 e2b5610c 2022-04-11 op r = sqlite3_step(stmt);
95 e2b5610c 2022-04-11 op if (r == SQLITE_DONE)
97 e2b5610c 2022-04-11 op if (r != SQLITE_ROW)
98 e2b5610c 2022-04-11 op errx(1, "sqlite3_step: %s", sqlite3_errstr(r));
100 e2b5610c 2022-04-11 op pkgstem = sqlite3_column_text(stmt, 0);
101 e2b5610c 2022-04-11 op comment = sqlite3_column_text(stmt, 1);
102 e2b5610c 2022-04-11 op descr = sqlite3_column_text(stmt, 2);
104 e2b5610c 2022-04-11 op (*entries)[i].name = xstrdup(pkgstem);
105 e2b5610c 2022-04-11 op (*entries)[i].descr = xstrdup(comment);
107 e2b5610c 2022-04-11 op r = asprintf(&doc, "%s %s %s", pkgstem,
108 e2b5610c 2022-04-11 op comment != NULL ? comment : "",
109 e2b5610c 2022-04-11 op descr != NULL ? descr : "");
111 e2b5610c 2022-04-11 op err(1, "asprintf");
113 e2b5610c 2022-04-11 op if ((toks = tokenize(doc)) == NULL)
114 e2b5610c 2022-04-11 op err(1, "tokenize");
115 e2b5610c 2022-04-11 op if (!dictionary_add_words(dict, toks, i))
116 e2b5610c 2022-04-11 op err(1, "dictionary_add_words");
117 e2b5610c 2022-04-11 op freetoks(toks);
122 e2b5610c 2022-04-11 op sqlite3_close(db);