Blame


1 e2b5610c 2022-04-11 op /*
2 e2b5610c 2022-04-11 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 e2b5610c 2022-04-11 op *
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.
7 e2b5610c 2022-04-11 op *
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.
15 e2b5610c 2022-04-11 op */
16 e2b5610c 2022-04-11 op
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>
20 e2b5610c 2022-04-11 op
21 e2b5610c 2022-04-11 op #include <sqlite3.h>
22 e2b5610c 2022-04-11 op
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"
26 e2b5610c 2022-04-11 op
27 e2b5610c 2022-04-11 op #include "mkftsidx.h"
28 e2b5610c 2022-04-11 op
29 e2b5610c 2022-04-11 op #ifndef SQLPORTS
30 e2b5610c 2022-04-11 op #define SQLPORTS "/usr/local/share/sqlports"
31 e2b5610c 2022-04-11 op #endif
32 e2b5610c 2022-04-11 op
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;"
35 e2b5610c 2022-04-11 op
36 e2b5610c 2022-04-11 op static int
37 e2b5610c 2022-04-11 op countports(sqlite3 *db)
38 e2b5610c 2022-04-11 op {
39 e2b5610c 2022-04-11 op sqlite3_stmt *stmt;
40 e2b5610c 2022-04-11 op int r, n = -1;
41 e2b5610c 2022-04-11 op
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));
46 e2b5610c 2022-04-11 op return -1;
47 e2b5610c 2022-04-11 op }
48 e2b5610c 2022-04-11 op
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);
52 e2b5610c 2022-04-11 op
53 e2b5610c 2022-04-11 op sqlite3_finalize(stmt);
54 e2b5610c 2022-04-11 op return n;
55 e2b5610c 2022-04-11 op }
56 e2b5610c 2022-04-11 op
57 e2b5610c 2022-04-11 op int
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)
60 e2b5610c 2022-04-11 op {
61 e2b5610c 2022-04-11 op const char *dbpath;
62 e2b5610c 2022-04-11 op sqlite3 *db;
63 e2b5610c 2022-04-11 op sqlite3_stmt *stmt;
64 e2b5610c 2022-04-11 op size_t i;
65 e2b5610c 2022-04-11 op int r;
66 e2b5610c 2022-04-11 op
67 e2b5610c 2022-04-11 op if (argc > 1)
68 e2b5610c 2022-04-11 op usage();
69 e2b5610c 2022-04-11 op else if (argc == 1)
70 e2b5610c 2022-04-11 op dbpath = *argv;
71 e2b5610c 2022-04-11 op else
72 e2b5610c 2022-04-11 op dbpath = SQLPORTS;
73 e2b5610c 2022-04-11 op
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));
76 e2b5610c 2022-04-11 op
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!");
79 e2b5610c 2022-04-11 op goto done;
80 e2b5610c 2022-04-11 op }
81 e2b5610c 2022-04-11 op *len = r;
82 e2b5610c 2022-04-11 op
83 e2b5610c 2022-04-11 op if ((*entries = calloc(*len, sizeof(**entries))) == NULL)
84 e2b5610c 2022-04-11 op err(1, "calloc");
85 e2b5610c 2022-04-11 op
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));
89 e2b5610c 2022-04-11 op
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;
93 e2b5610c 2022-04-11 op
94 e2b5610c 2022-04-11 op r = sqlite3_step(stmt);
95 e2b5610c 2022-04-11 op if (r == SQLITE_DONE)
96 e2b5610c 2022-04-11 op break;
97 e2b5610c 2022-04-11 op if (r != SQLITE_ROW)
98 e2b5610c 2022-04-11 op errx(1, "sqlite3_step: %s", sqlite3_errstr(r));
99 e2b5610c 2022-04-11 op
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);
103 e2b5610c 2022-04-11 op
104 e2b5610c 2022-04-11 op (*entries)[i].name = xstrdup(pkgstem);
105 e2b5610c 2022-04-11 op (*entries)[i].descr = xstrdup(comment);
106 e2b5610c 2022-04-11 op
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 : "");
110 e2b5610c 2022-04-11 op if (r == -1)
111 e2b5610c 2022-04-11 op err(1, "asprintf");
112 e2b5610c 2022-04-11 op
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);
118 e2b5610c 2022-04-11 op free(doc);
119 e2b5610c 2022-04-11 op }
120 e2b5610c 2022-04-11 op
121 e2b5610c 2022-04-11 op done:
122 e2b5610c 2022-04-11 op sqlite3_close(db);
123 e2b5610c 2022-04-11 op return 0;
124 e2b5610c 2022-04-11 op }