Blame


1 e2b5610c 2022-04-11 op /*
2 c00beb4f 2022-04-14 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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 <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>
23 e2b5610c 2022-04-11 op
24 e2b5610c 2022-04-11 op #include "db.h"
25 e2b5610c 2022-04-11 op #include "dictionary.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 enum {
30 e2b5610c 2022-04-11 op MODE_SQLPORTS,
31 e2b5610c 2022-04-11 op MODE_WIKI,
32 e2b5610c 2022-04-11 op };
33 e2b5610c 2022-04-11 op
34 e2b5610c 2022-04-11 op char *
35 e2b5610c 2022-04-11 op xstrdup(const char *s)
36 e2b5610c 2022-04-11 op {
37 e2b5610c 2022-04-11 op char *t;
38 e2b5610c 2022-04-11 op
39 e2b5610c 2022-04-11 op if (s == NULL)
40 e2b5610c 2022-04-11 op return NULL;
41 e2b5610c 2022-04-11 op
42 e2b5610c 2022-04-11 op if ((t = strdup(s)) == NULL)
43 e2b5610c 2022-04-11 op err(1, "strdup");
44 e2b5610c 2022-04-11 op return t;
45 e2b5610c 2022-04-11 op }
46 e2b5610c 2022-04-11 op
47 e2b5610c 2022-04-11 op __dead void
48 e2b5610c 2022-04-11 op usage(void)
49 e2b5610c 2022-04-11 op {
50 e2b5610c 2022-04-11 op fprintf(stderr, "usage: %s [-o dbpath] [-m p|w] [path]\n",
51 e2b5610c 2022-04-11 op getprogname());
52 e2b5610c 2022-04-11 op exit(1);
53 e2b5610c 2022-04-11 op }
54 e2b5610c 2022-04-11 op
55 e2b5610c 2022-04-11 op int
56 e2b5610c 2022-04-11 op main(int argc, char **argv)
57 e2b5610c 2022-04-11 op {
58 e2b5610c 2022-04-11 op struct dictionary dict;
59 e2b5610c 2022-04-11 op struct db_entry *entries = NULL;
60 e2b5610c 2022-04-11 op const char *dbpath = NULL;
61 e2b5610c 2022-04-11 op FILE *fp;
62 e2b5610c 2022-04-11 op size_t i, len = 0;
63 e2b5610c 2022-04-11 op int ch, r = 0, mode = MODE_SQLPORTS;
64 e2b5610c 2022-04-11 op
65 e2b5610c 2022-04-11 op #ifndef PROFILE
66 e2b5610c 2022-04-11 op /* sqlite needs flock */
67 e2b5610c 2022-04-11 op if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
68 e2b5610c 2022-04-11 op err(1, "pledge");
69 e2b5610c 2022-04-11 op #endif
70 e2b5610c 2022-04-11 op
71 e2b5610c 2022-04-11 op while ((ch = getopt(argc, argv, "m:o:")) != -1) {
72 e2b5610c 2022-04-11 op switch (ch) {
73 e2b5610c 2022-04-11 op case 'm':
74 e2b5610c 2022-04-11 op switch (*optarg) {
75 e2b5610c 2022-04-11 op case 'p':
76 e2b5610c 2022-04-11 op mode = MODE_SQLPORTS;
77 e2b5610c 2022-04-11 op break;
78 e2b5610c 2022-04-11 op case 'w':
79 e2b5610c 2022-04-11 op mode = MODE_WIKI;
80 e2b5610c 2022-04-11 op break;
81 e2b5610c 2022-04-11 op default:
82 e2b5610c 2022-04-11 op usage();
83 e2b5610c 2022-04-11 op }
84 e2b5610c 2022-04-11 op break;
85 e2b5610c 2022-04-11 op case 'o':
86 e2b5610c 2022-04-11 op dbpath = optarg;
87 e2b5610c 2022-04-11 op break;
88 e2b5610c 2022-04-11 op default:
89 e2b5610c 2022-04-11 op usage();
90 e2b5610c 2022-04-11 op }
91 e2b5610c 2022-04-11 op }
92 e2b5610c 2022-04-11 op argc -= optind;
93 e2b5610c 2022-04-11 op argv += optind;
94 e2b5610c 2022-04-11 op
95 e2b5610c 2022-04-11 op if (dbpath == NULL)
96 e2b5610c 2022-04-11 op dbpath = "db";
97 e2b5610c 2022-04-11 op
98 e2b5610c 2022-04-11 op if (!dictionary_init(&dict))
99 e2b5610c 2022-04-11 op err(1, "dictionary_init");
100 e2b5610c 2022-04-11 op
101 e2b5610c 2022-04-11 op if (mode == MODE_SQLPORTS)
102 e2b5610c 2022-04-11 op r = idx_ports(&dict, &entries, &len, argc, argv);
103 e2b5610c 2022-04-11 op else
104 e2b5610c 2022-04-11 op r = idx_wiki(&dict, &entries, &len, argc, argv);
105 e2b5610c 2022-04-11 op
106 e2b5610c 2022-04-11 op if (r == 0) {
107 e2b5610c 2022-04-11 op if ((fp = fopen(dbpath, "w+")) == NULL)
108 e2b5610c 2022-04-11 op err(1, "can't open %s", dbpath);
109 e2b5610c 2022-04-11 op if (db_create(fp, &dict, entries, len) == -1) {
110 e2b5610c 2022-04-11 op warn("db_create");
111 e2b5610c 2022-04-11 op unlink(dbpath);
112 e2b5610c 2022-04-11 op r = 1;
113 e2b5610c 2022-04-11 op }
114 e2b5610c 2022-04-11 op fclose(fp);
115 e2b5610c 2022-04-11 op }
116 e2b5610c 2022-04-11 op
117 e2b5610c 2022-04-11 op for (i = 0; i < len; ++i) {
118 e2b5610c 2022-04-11 op free(entries[i].name);
119 e2b5610c 2022-04-11 op free(entries[i].descr);
120 e2b5610c 2022-04-11 op }
121 e2b5610c 2022-04-11 op free(entries);
122 e2b5610c 2022-04-11 op dictionary_free(&dict);
123 e2b5610c 2022-04-11 op
124 e2b5610c 2022-04-11 op return r;
125 e2b5610c 2022-04-11 op }