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 2dfbb886 2022-04-17 op MODE_FILES,
31 e2b5610c 2022-04-11 op MODE_SQLPORTS,
32 e2b5610c 2022-04-11 op MODE_WIKI,
33 e2b5610c 2022-04-11 op };
34 e2b5610c 2022-04-11 op
35 e2b5610c 2022-04-11 op char *
36 e2b5610c 2022-04-11 op xstrdup(const char *s)
37 e2b5610c 2022-04-11 op {
38 e2b5610c 2022-04-11 op char *t;
39 e2b5610c 2022-04-11 op
40 e2b5610c 2022-04-11 op if (s == NULL)
41 e2b5610c 2022-04-11 op return NULL;
42 e2b5610c 2022-04-11 op
43 e2b5610c 2022-04-11 op if ((t = strdup(s)) == NULL)
44 e2b5610c 2022-04-11 op err(1, "strdup");
45 e2b5610c 2022-04-11 op return t;
46 e2b5610c 2022-04-11 op }
47 e2b5610c 2022-04-11 op
48 e2b5610c 2022-04-11 op __dead void
49 e2b5610c 2022-04-11 op usage(void)
50 e2b5610c 2022-04-11 op {
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());
53 e2b5610c 2022-04-11 op exit(1);
54 e2b5610c 2022-04-11 op }
55 e2b5610c 2022-04-11 op
56 e2b5610c 2022-04-11 op int
57 e2b5610c 2022-04-11 op main(int argc, char **argv)
58 e2b5610c 2022-04-11 op {
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;
62 e2b5610c 2022-04-11 op FILE *fp;
63 e2b5610c 2022-04-11 op size_t i, len = 0;
64 e2b5610c 2022-04-11 op int ch, r = 0, mode = MODE_SQLPORTS;
65 e2b5610c 2022-04-11 op
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");
70 e2b5610c 2022-04-11 op #endif
71 e2b5610c 2022-04-11 op
72 e2b5610c 2022-04-11 op while ((ch = getopt(argc, argv, "m:o:")) != -1) {
73 e2b5610c 2022-04-11 op switch (ch) {
74 e2b5610c 2022-04-11 op case 'm':
75 e2b5610c 2022-04-11 op switch (*optarg) {
76 2dfbb886 2022-04-17 op case 'f':
77 2dfbb886 2022-04-17 op mode = MODE_FILES;
78 2dfbb886 2022-04-17 op break;
79 e2b5610c 2022-04-11 op case 'p':
80 e2b5610c 2022-04-11 op mode = MODE_SQLPORTS;
81 e2b5610c 2022-04-11 op break;
82 e2b5610c 2022-04-11 op case 'w':
83 e2b5610c 2022-04-11 op mode = MODE_WIKI;
84 e2b5610c 2022-04-11 op break;
85 e2b5610c 2022-04-11 op default:
86 e2b5610c 2022-04-11 op usage();
87 e2b5610c 2022-04-11 op }
88 e2b5610c 2022-04-11 op break;
89 e2b5610c 2022-04-11 op case 'o':
90 e2b5610c 2022-04-11 op dbpath = optarg;
91 e2b5610c 2022-04-11 op break;
92 e2b5610c 2022-04-11 op default:
93 e2b5610c 2022-04-11 op usage();
94 e2b5610c 2022-04-11 op }
95 e2b5610c 2022-04-11 op }
96 e2b5610c 2022-04-11 op argc -= optind;
97 e2b5610c 2022-04-11 op argv += optind;
98 e2b5610c 2022-04-11 op
99 e2b5610c 2022-04-11 op if (dbpath == NULL)
100 e2b5610c 2022-04-11 op dbpath = "db";
101 e2b5610c 2022-04-11 op
102 e2b5610c 2022-04-11 op if (!dictionary_init(&dict))
103 e2b5610c 2022-04-11 op err(1, "dictionary_init");
104 e2b5610c 2022-04-11 op
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);
109 e2b5610c 2022-04-11 op else
110 e2b5610c 2022-04-11 op r = idx_wiki(&dict, &entries, &len, argc, argv);
111 e2b5610c 2022-04-11 op
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);
118 e2b5610c 2022-04-11 op r = 1;
119 e2b5610c 2022-04-11 op }
120 e2b5610c 2022-04-11 op fclose(fp);
121 e2b5610c 2022-04-11 op }
122 e2b5610c 2022-04-11 op
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);
126 e2b5610c 2022-04-11 op }
127 e2b5610c 2022-04-11 op free(entries);
128 e2b5610c 2022-04-11 op dictionary_free(&dict);
129 e2b5610c 2022-04-11 op
130 e2b5610c 2022-04-11 op return r;
131 e2b5610c 2022-04-11 op }