2 c00beb4f 2022-04-14 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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 <fcntl.h>
19 e2b5610c 2022-04-11 op #include <stdio.h>
20 e2b5610c 2022-04-11 op #include <stdlib.h>
21 e2b5610c 2022-04-11 op #include <string.h>
22 e2b5610c 2022-04-11 op #include <unistd.h>
24 e2b5610c 2022-04-11 op #include "db.h"
25 e2b5610c 2022-04-11 op #include "dictionary.h"
27 e2b5610c 2022-04-11 op #include "mkftsidx.h"
31 e2b5610c 2022-04-11 op MODE_SQLPORTS,
36 e2b5610c 2022-04-11 op xstrdup(const char *s)
40 e2b5610c 2022-04-11 op if (s == NULL)
43 e2b5610c 2022-04-11 op if ((t = strdup(s)) == NULL)
44 e2b5610c 2022-04-11 op err(1, "strdup");
51 2dfbb886 2022-04-17 op fprintf(stderr, "usage: %s [-o dbpath] [-m f|p|w] [file ...]\n",
52 e2b5610c 2022-04-11 op getprogname());
57 e2b5610c 2022-04-11 op main(int argc, char **argv)
59 e2b5610c 2022-04-11 op struct dictionary dict;
60 e2b5610c 2022-04-11 op struct db_entry *entries = NULL;
61 e2b5610c 2022-04-11 op const char *dbpath = NULL;
63 e2b5610c 2022-04-11 op size_t i, len = 0;
64 e2b5610c 2022-04-11 op int ch, r = 0, mode = MODE_SQLPORTS;
66 e2b5610c 2022-04-11 op #ifndef PROFILE
67 e2b5610c 2022-04-11 op /* sqlite needs flock */
68 e2b5610c 2022-04-11 op if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
69 e2b5610c 2022-04-11 op err(1, "pledge");
72 e2b5610c 2022-04-11 op while ((ch = getopt(argc, argv, "m:o:")) != -1) {
75 e2b5610c 2022-04-11 op switch (*optarg) {
77 2dfbb886 2022-04-17 op mode = MODE_FILES;
80 e2b5610c 2022-04-11 op mode = MODE_SQLPORTS;
83 e2b5610c 2022-04-11 op mode = MODE_WIKI;
90 e2b5610c 2022-04-11 op dbpath = optarg;
96 e2b5610c 2022-04-11 op argc -= optind;
97 e2b5610c 2022-04-11 op argv += optind;
99 e2b5610c 2022-04-11 op if (dbpath == NULL)
100 e2b5610c 2022-04-11 op dbpath = "db";
102 e2b5610c 2022-04-11 op if (!dictionary_init(&dict))
103 e2b5610c 2022-04-11 op err(1, "dictionary_init");
105 2dfbb886 2022-04-17 op if (mode == MODE_FILES)
106 2dfbb886 2022-04-17 op r = idx_files(&dict, &entries, &len, argc, argv);
107 2dfbb886 2022-04-17 op else if (mode == MODE_SQLPORTS)
108 e2b5610c 2022-04-11 op r = idx_ports(&dict, &entries, &len, argc, argv);
110 e2b5610c 2022-04-11 op r = idx_wiki(&dict, &entries, &len, argc, argv);
112 e2b5610c 2022-04-11 op if (r == 0) {
113 e2b5610c 2022-04-11 op if ((fp = fopen(dbpath, "w+")) == NULL)
114 e2b5610c 2022-04-11 op err(1, "can't open %s", dbpath);
115 e2b5610c 2022-04-11 op if (db_create(fp, &dict, entries, len) == -1) {
116 e2b5610c 2022-04-11 op warn("db_create");
117 e2b5610c 2022-04-11 op unlink(dbpath);
123 e2b5610c 2022-04-11 op for (i = 0; i < len; ++i) {
124 e2b5610c 2022-04-11 op free(entries[i].name);
125 e2b5610c 2022-04-11 op free(entries[i].descr);
127 e2b5610c 2022-04-11 op free(entries);
128 e2b5610c 2022-04-11 op dictionary_free(&dict);