1 |
|
|
/* $OpenBSD: mandocdb.c,v 1.172 2016/07/19 13:30:16 schwarze Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
4 |
|
|
* Copyright (c) 2011-2016 Ingo Schwarze <schwarze@openbsd.org> |
5 |
|
|
* |
6 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
7 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
8 |
|
|
* copyright notice and this permission notice appear in all copies. |
9 |
|
|
* |
10 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES |
11 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR |
13 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 |
|
|
*/ |
18 |
|
|
#include <sys/types.h> |
19 |
|
|
#include <sys/stat.h> |
20 |
|
|
#include <sys/wait.h> |
21 |
|
|
|
22 |
|
|
#include <assert.h> |
23 |
|
|
#include <ctype.h> |
24 |
|
|
#include <err.h> |
25 |
|
|
#include <errno.h> |
26 |
|
|
#include <fcntl.h> |
27 |
|
|
#include <fts.h> |
28 |
|
|
#include <limits.h> |
29 |
|
|
#include <stddef.h> |
30 |
|
|
#include <stdio.h> |
31 |
|
|
#include <stdint.h> |
32 |
|
|
#include <stdlib.h> |
33 |
|
|
#include <string.h> |
34 |
|
|
#include <unistd.h> |
35 |
|
|
|
36 |
|
|
#include <sqlite3.h> |
37 |
|
|
|
38 |
|
|
#include "mandoc_aux.h" |
39 |
|
|
#include "mandoc_ohash.h" |
40 |
|
|
#include "mandoc.h" |
41 |
|
|
#include "roff.h" |
42 |
|
|
#include "mdoc.h" |
43 |
|
|
#include "man.h" |
44 |
|
|
#include "manconf.h" |
45 |
|
|
#include "mansearch.h" |
46 |
|
|
|
47 |
|
|
extern int mansearch_keymax; |
48 |
|
|
extern const char *const mansearch_keynames[]; |
49 |
|
|
|
50 |
|
|
#define SQL_EXEC(_v) \ |
51 |
|
|
if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \ |
52 |
|
|
say("", "%s: %s", (_v), sqlite3_errmsg(db)) |
53 |
|
|
#define SQL_BIND_TEXT(_s, _i, _v) \ |
54 |
|
|
if (SQLITE_OK != sqlite3_bind_text \ |
55 |
|
|
((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \ |
56 |
|
|
say(mlink->file, "%s", sqlite3_errmsg(db)) |
57 |
|
|
#define SQL_BIND_INT(_s, _i, _v) \ |
58 |
|
|
if (SQLITE_OK != sqlite3_bind_int \ |
59 |
|
|
((_s), (_i)++, (_v))) \ |
60 |
|
|
say(mlink->file, "%s", sqlite3_errmsg(db)) |
61 |
|
|
#define SQL_BIND_INT64(_s, _i, _v) \ |
62 |
|
|
if (SQLITE_OK != sqlite3_bind_int64 \ |
63 |
|
|
((_s), (_i)++, (_v))) \ |
64 |
|
|
say(mlink->file, "%s", sqlite3_errmsg(db)) |
65 |
|
|
#define SQL_STEP(_s) \ |
66 |
|
|
if (SQLITE_DONE != sqlite3_step((_s))) \ |
67 |
|
|
say(mlink->file, "%s", sqlite3_errmsg(db)) |
68 |
|
|
|
69 |
|
|
enum op { |
70 |
|
|
OP_DEFAULT = 0, /* new dbs from dir list or default config */ |
71 |
|
|
OP_CONFFILE, /* new databases from custom config file */ |
72 |
|
|
OP_UPDATE, /* delete/add entries in existing database */ |
73 |
|
|
OP_DELETE, /* delete entries from existing database */ |
74 |
|
|
OP_TEST /* change no databases, report potential problems */ |
75 |
|
|
}; |
76 |
|
|
|
77 |
|
|
struct str { |
78 |
|
|
const struct mpage *mpage; /* if set, the owning parse */ |
79 |
|
|
uint64_t mask; /* bitmask in sequence */ |
80 |
|
|
char key[]; /* rendered text */ |
81 |
|
|
}; |
82 |
|
|
|
83 |
|
|
struct inodev { |
84 |
|
|
ino_t st_ino; |
85 |
|
|
dev_t st_dev; |
86 |
|
|
}; |
87 |
|
|
|
88 |
|
|
struct mpage { |
89 |
|
|
struct inodev inodev; /* used for hashing routine */ |
90 |
|
|
int64_t pageid; /* pageid in mpages SQL table */ |
91 |
|
|
char *sec; /* section from file content */ |
92 |
|
|
char *arch; /* architecture from file content */ |
93 |
|
|
char *title; /* title from file content */ |
94 |
|
|
char *desc; /* description from file content */ |
95 |
|
|
struct mlink *mlinks; /* singly linked list */ |
96 |
|
|
int form; /* format from file content */ |
97 |
|
|
int name_head_done; |
98 |
|
|
}; |
99 |
|
|
|
100 |
|
|
struct mlink { |
101 |
|
|
char file[PATH_MAX]; /* filename rel. to manpath */ |
102 |
|
|
char *dsec; /* section from directory */ |
103 |
|
|
char *arch; /* architecture from directory */ |
104 |
|
|
char *name; /* name from file name (not empty) */ |
105 |
|
|
char *fsec; /* section from file name suffix */ |
106 |
|
|
struct mlink *next; /* singly linked list */ |
107 |
|
|
struct mpage *mpage; /* parent */ |
108 |
|
|
int dform; /* format from directory */ |
109 |
|
|
int fform; /* format from file name suffix */ |
110 |
|
|
int gzip; /* filename has a .gz suffix */ |
111 |
|
|
}; |
112 |
|
|
|
113 |
|
|
enum stmt { |
114 |
|
|
STMT_DELETE_PAGE = 0, /* delete mpage */ |
115 |
|
|
STMT_INSERT_PAGE, /* insert mpage */ |
116 |
|
|
STMT_INSERT_LINK, /* insert mlink */ |
117 |
|
|
STMT_INSERT_NAME, /* insert name */ |
118 |
|
|
STMT_SELECT_NAME, /* retrieve existing name flags */ |
119 |
|
|
STMT_INSERT_KEY, /* insert parsed key */ |
120 |
|
|
STMT__MAX |
121 |
|
|
}; |
122 |
|
|
|
123 |
|
|
typedef int (*mdoc_fp)(struct mpage *, const struct roff_meta *, |
124 |
|
|
const struct roff_node *); |
125 |
|
|
|
126 |
|
|
struct mdoc_handler { |
127 |
|
|
mdoc_fp fp; /* optional handler */ |
128 |
|
|
uint64_t mask; /* set unless handler returns 0 */ |
129 |
|
|
}; |
130 |
|
|
|
131 |
|
|
|
132 |
|
|
int mandocdb(int, char *[]); |
133 |
|
|
|
134 |
|
|
static void dbclose(int); |
135 |
|
|
static void dbadd(struct mpage *); |
136 |
|
|
static void dbadd_mlink(const struct mlink *mlink); |
137 |
|
|
static void dbadd_mlink_name(const struct mlink *mlink); |
138 |
|
|
static int dbopen(int); |
139 |
|
|
static void dbprune(void); |
140 |
|
|
static void filescan(const char *); |
141 |
|
|
static void mlink_add(struct mlink *, const struct stat *); |
142 |
|
|
static void mlink_check(struct mpage *, struct mlink *); |
143 |
|
|
static void mlink_free(struct mlink *); |
144 |
|
|
static void mlinks_undupe(struct mpage *); |
145 |
|
|
static void mpages_free(void); |
146 |
|
|
static void mpages_merge(struct mparse *); |
147 |
|
|
static void names_check(void); |
148 |
|
|
static void parse_cat(struct mpage *, int); |
149 |
|
|
static void parse_man(struct mpage *, const struct roff_meta *, |
150 |
|
|
const struct roff_node *); |
151 |
|
|
static void parse_mdoc(struct mpage *, const struct roff_meta *, |
152 |
|
|
const struct roff_node *); |
153 |
|
|
static int parse_mdoc_head(struct mpage *, const struct roff_meta *, |
154 |
|
|
const struct roff_node *); |
155 |
|
|
static int parse_mdoc_Fd(struct mpage *, const struct roff_meta *, |
156 |
|
|
const struct roff_node *); |
157 |
|
|
static void parse_mdoc_fname(struct mpage *, const struct roff_node *); |
158 |
|
|
static int parse_mdoc_Fn(struct mpage *, const struct roff_meta *, |
159 |
|
|
const struct roff_node *); |
160 |
|
|
static int parse_mdoc_Fo(struct mpage *, const struct roff_meta *, |
161 |
|
|
const struct roff_node *); |
162 |
|
|
static int parse_mdoc_Nd(struct mpage *, const struct roff_meta *, |
163 |
|
|
const struct roff_node *); |
164 |
|
|
static int parse_mdoc_Nm(struct mpage *, const struct roff_meta *, |
165 |
|
|
const struct roff_node *); |
166 |
|
|
static int parse_mdoc_Sh(struct mpage *, const struct roff_meta *, |
167 |
|
|
const struct roff_node *); |
168 |
|
|
static int parse_mdoc_Va(struct mpage *, const struct roff_meta *, |
169 |
|
|
const struct roff_node *); |
170 |
|
|
static int parse_mdoc_Xr(struct mpage *, const struct roff_meta *, |
171 |
|
|
const struct roff_node *); |
172 |
|
|
static void putkey(const struct mpage *, char *, uint64_t); |
173 |
|
|
static void putkeys(const struct mpage *, char *, size_t, uint64_t); |
174 |
|
|
static void putmdockey(const struct mpage *, |
175 |
|
|
const struct roff_node *, uint64_t); |
176 |
|
|
static int render_string(char **, size_t *); |
177 |
|
|
static void say(const char *, const char *, ...) |
178 |
|
|
__attribute__((__format__ (printf, 2, 3))); |
179 |
|
|
static int set_basedir(const char *, int); |
180 |
|
|
static int treescan(void); |
181 |
|
|
static size_t utf8(unsigned int, char [7]); |
182 |
|
|
|
183 |
|
|
static char tempfilename[32]; |
184 |
|
|
static int nodb; /* no database changes */ |
185 |
|
|
static int mparse_options; /* abort the parse early */ |
186 |
|
|
static int use_all; /* use all found files */ |
187 |
|
|
static int debug; /* print what we're doing */ |
188 |
|
|
static int warnings; /* warn about crap */ |
189 |
|
|
static int write_utf8; /* write UTF-8 output; else ASCII */ |
190 |
|
|
static int exitcode; /* to be returned by main */ |
191 |
|
|
static enum op op; /* operational mode */ |
192 |
|
|
static char basedir[PATH_MAX]; /* current base directory */ |
193 |
|
|
static struct ohash mpages; /* table of distinct manual pages */ |
194 |
|
|
static struct ohash mlinks; /* table of directory entries */ |
195 |
|
|
static struct ohash names; /* table of all names */ |
196 |
|
|
static struct ohash strings; /* table of all strings */ |
197 |
|
|
static sqlite3 *db = NULL; /* current database */ |
198 |
|
|
static sqlite3_stmt *stmts[STMT__MAX]; /* current statements */ |
199 |
|
|
static uint64_t name_mask; |
200 |
|
|
|
201 |
|
|
static const struct mdoc_handler mdocs[MDOC_MAX] = { |
202 |
|
|
{ NULL, 0 }, /* Ap */ |
203 |
|
|
{ NULL, 0 }, /* Dd */ |
204 |
|
|
{ NULL, 0 }, /* Dt */ |
205 |
|
|
{ NULL, 0 }, /* Os */ |
206 |
|
|
{ parse_mdoc_Sh, TYPE_Sh }, /* Sh */ |
207 |
|
|
{ parse_mdoc_head, TYPE_Ss }, /* Ss */ |
208 |
|
|
{ NULL, 0 }, /* Pp */ |
209 |
|
|
{ NULL, 0 }, /* D1 */ |
210 |
|
|
{ NULL, 0 }, /* Dl */ |
211 |
|
|
{ NULL, 0 }, /* Bd */ |
212 |
|
|
{ NULL, 0 }, /* Ed */ |
213 |
|
|
{ NULL, 0 }, /* Bl */ |
214 |
|
|
{ NULL, 0 }, /* El */ |
215 |
|
|
{ NULL, 0 }, /* It */ |
216 |
|
|
{ NULL, 0 }, /* Ad */ |
217 |
|
|
{ NULL, TYPE_An }, /* An */ |
218 |
|
|
{ NULL, TYPE_Ar }, /* Ar */ |
219 |
|
|
{ NULL, TYPE_Cd }, /* Cd */ |
220 |
|
|
{ NULL, TYPE_Cm }, /* Cm */ |
221 |
|
|
{ NULL, TYPE_Dv }, /* Dv */ |
222 |
|
|
{ NULL, TYPE_Er }, /* Er */ |
223 |
|
|
{ NULL, TYPE_Ev }, /* Ev */ |
224 |
|
|
{ NULL, 0 }, /* Ex */ |
225 |
|
|
{ NULL, TYPE_Fa }, /* Fa */ |
226 |
|
|
{ parse_mdoc_Fd, 0 }, /* Fd */ |
227 |
|
|
{ NULL, TYPE_Fl }, /* Fl */ |
228 |
|
|
{ parse_mdoc_Fn, 0 }, /* Fn */ |
229 |
|
|
{ NULL, TYPE_Ft }, /* Ft */ |
230 |
|
|
{ NULL, TYPE_Ic }, /* Ic */ |
231 |
|
|
{ NULL, TYPE_In }, /* In */ |
232 |
|
|
{ NULL, TYPE_Li }, /* Li */ |
233 |
|
|
{ parse_mdoc_Nd, 0 }, /* Nd */ |
234 |
|
|
{ parse_mdoc_Nm, 0 }, /* Nm */ |
235 |
|
|
{ NULL, 0 }, /* Op */ |
236 |
|
|
{ NULL, 0 }, /* Ot */ |
237 |
|
|
{ NULL, TYPE_Pa }, /* Pa */ |
238 |
|
|
{ NULL, 0 }, /* Rv */ |
239 |
|
|
{ NULL, TYPE_St }, /* St */ |
240 |
|
|
{ parse_mdoc_Va, TYPE_Va }, /* Va */ |
241 |
|
|
{ parse_mdoc_Va, TYPE_Vt }, /* Vt */ |
242 |
|
|
{ parse_mdoc_Xr, 0 }, /* Xr */ |
243 |
|
|
{ NULL, 0 }, /* %A */ |
244 |
|
|
{ NULL, 0 }, /* %B */ |
245 |
|
|
{ NULL, 0 }, /* %D */ |
246 |
|
|
{ NULL, 0 }, /* %I */ |
247 |
|
|
{ NULL, 0 }, /* %J */ |
248 |
|
|
{ NULL, 0 }, /* %N */ |
249 |
|
|
{ NULL, 0 }, /* %O */ |
250 |
|
|
{ NULL, 0 }, /* %P */ |
251 |
|
|
{ NULL, 0 }, /* %R */ |
252 |
|
|
{ NULL, 0 }, /* %T */ |
253 |
|
|
{ NULL, 0 }, /* %V */ |
254 |
|
|
{ NULL, 0 }, /* Ac */ |
255 |
|
|
{ NULL, 0 }, /* Ao */ |
256 |
|
|
{ NULL, 0 }, /* Aq */ |
257 |
|
|
{ NULL, TYPE_At }, /* At */ |
258 |
|
|
{ NULL, 0 }, /* Bc */ |
259 |
|
|
{ NULL, 0 }, /* Bf */ |
260 |
|
|
{ NULL, 0 }, /* Bo */ |
261 |
|
|
{ NULL, 0 }, /* Bq */ |
262 |
|
|
{ NULL, TYPE_Bsx }, /* Bsx */ |
263 |
|
|
{ NULL, TYPE_Bx }, /* Bx */ |
264 |
|
|
{ NULL, 0 }, /* Db */ |
265 |
|
|
{ NULL, 0 }, /* Dc */ |
266 |
|
|
{ NULL, 0 }, /* Do */ |
267 |
|
|
{ NULL, 0 }, /* Dq */ |
268 |
|
|
{ NULL, 0 }, /* Ec */ |
269 |
|
|
{ NULL, 0 }, /* Ef */ |
270 |
|
|
{ NULL, TYPE_Em }, /* Em */ |
271 |
|
|
{ NULL, 0 }, /* Eo */ |
272 |
|
|
{ NULL, TYPE_Fx }, /* Fx */ |
273 |
|
|
{ NULL, TYPE_Ms }, /* Ms */ |
274 |
|
|
{ NULL, 0 }, /* No */ |
275 |
|
|
{ NULL, 0 }, /* Ns */ |
276 |
|
|
{ NULL, TYPE_Nx }, /* Nx */ |
277 |
|
|
{ NULL, TYPE_Ox }, /* Ox */ |
278 |
|
|
{ NULL, 0 }, /* Pc */ |
279 |
|
|
{ NULL, 0 }, /* Pf */ |
280 |
|
|
{ NULL, 0 }, /* Po */ |
281 |
|
|
{ NULL, 0 }, /* Pq */ |
282 |
|
|
{ NULL, 0 }, /* Qc */ |
283 |
|
|
{ NULL, 0 }, /* Ql */ |
284 |
|
|
{ NULL, 0 }, /* Qo */ |
285 |
|
|
{ NULL, 0 }, /* Qq */ |
286 |
|
|
{ NULL, 0 }, /* Re */ |
287 |
|
|
{ NULL, 0 }, /* Rs */ |
288 |
|
|
{ NULL, 0 }, /* Sc */ |
289 |
|
|
{ NULL, 0 }, /* So */ |
290 |
|
|
{ NULL, 0 }, /* Sq */ |
291 |
|
|
{ NULL, 0 }, /* Sm */ |
292 |
|
|
{ NULL, 0 }, /* Sx */ |
293 |
|
|
{ NULL, TYPE_Sy }, /* Sy */ |
294 |
|
|
{ NULL, TYPE_Tn }, /* Tn */ |
295 |
|
|
{ NULL, 0 }, /* Ux */ |
296 |
|
|
{ NULL, 0 }, /* Xc */ |
297 |
|
|
{ NULL, 0 }, /* Xo */ |
298 |
|
|
{ parse_mdoc_Fo, 0 }, /* Fo */ |
299 |
|
|
{ NULL, 0 }, /* Fc */ |
300 |
|
|
{ NULL, 0 }, /* Oo */ |
301 |
|
|
{ NULL, 0 }, /* Oc */ |
302 |
|
|
{ NULL, 0 }, /* Bk */ |
303 |
|
|
{ NULL, 0 }, /* Ek */ |
304 |
|
|
{ NULL, 0 }, /* Bt */ |
305 |
|
|
{ NULL, 0 }, /* Hf */ |
306 |
|
|
{ NULL, 0 }, /* Fr */ |
307 |
|
|
{ NULL, 0 }, /* Ud */ |
308 |
|
|
{ NULL, TYPE_Lb }, /* Lb */ |
309 |
|
|
{ NULL, 0 }, /* Lp */ |
310 |
|
|
{ NULL, TYPE_Lk }, /* Lk */ |
311 |
|
|
{ NULL, TYPE_Mt }, /* Mt */ |
312 |
|
|
{ NULL, 0 }, /* Brq */ |
313 |
|
|
{ NULL, 0 }, /* Bro */ |
314 |
|
|
{ NULL, 0 }, /* Brc */ |
315 |
|
|
{ NULL, 0 }, /* %C */ |
316 |
|
|
{ NULL, 0 }, /* Es */ |
317 |
|
|
{ NULL, 0 }, /* En */ |
318 |
|
|
{ NULL, TYPE_Dx }, /* Dx */ |
319 |
|
|
{ NULL, 0 }, /* %Q */ |
320 |
|
|
{ NULL, 0 }, /* br */ |
321 |
|
|
{ NULL, 0 }, /* sp */ |
322 |
|
|
{ NULL, 0 }, /* %U */ |
323 |
|
|
{ NULL, 0 }, /* Ta */ |
324 |
|
|
{ NULL, 0 }, /* ll */ |
325 |
|
|
}; |
326 |
|
|
|
327 |
|
|
|
328 |
|
|
int |
329 |
|
|
mandocdb(int argc, char *argv[]) |
330 |
|
|
{ |
331 |
|
|
struct manconf conf; |
332 |
|
|
struct mparse *mp; |
333 |
|
|
const char *path_arg, *progname; |
334 |
|
|
size_t j, sz; |
335 |
|
|
int ch, i; |
336 |
|
|
|
337 |
|
|
if (pledge("stdio rpath wpath cpath fattr flock proc exec", NULL) == -1) { |
338 |
|
|
warn("pledge"); |
339 |
|
|
return (int)MANDOCLEVEL_SYSERR; |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
memset(&conf, 0, sizeof(conf)); |
343 |
|
|
memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *)); |
344 |
|
|
|
345 |
|
|
/* |
346 |
|
|
* We accept a few different invocations. |
347 |
|
|
* The CHECKOP macro makes sure that invocation styles don't |
348 |
|
|
* clobber each other. |
349 |
|
|
*/ |
350 |
|
|
#define CHECKOP(_op, _ch) do \ |
351 |
|
|
if (OP_DEFAULT != (_op)) { \ |
352 |
|
|
warnx("-%c: Conflicting option", (_ch)); \ |
353 |
|
|
goto usage; \ |
354 |
|
|
} while (/*CONSTCOND*/0) |
355 |
|
|
|
356 |
|
|
path_arg = NULL; |
357 |
|
|
op = OP_DEFAULT; |
358 |
|
|
|
359 |
|
|
while (-1 != (ch = getopt(argc, argv, "aC:Dd:npQT:tu:v"))) |
360 |
|
|
switch (ch) { |
361 |
|
|
case 'a': |
362 |
|
|
use_all = 1; |
363 |
|
|
break; |
364 |
|
|
case 'C': |
365 |
|
|
CHECKOP(op, ch); |
366 |
|
|
path_arg = optarg; |
367 |
|
|
op = OP_CONFFILE; |
368 |
|
|
break; |
369 |
|
|
case 'D': |
370 |
|
|
debug++; |
371 |
|
|
break; |
372 |
|
|
case 'd': |
373 |
|
|
CHECKOP(op, ch); |
374 |
|
|
path_arg = optarg; |
375 |
|
|
op = OP_UPDATE; |
376 |
|
|
break; |
377 |
|
|
case 'n': |
378 |
|
|
nodb = 1; |
379 |
|
|
break; |
380 |
|
|
case 'p': |
381 |
|
|
warnings = 1; |
382 |
|
|
break; |
383 |
|
|
case 'Q': |
384 |
|
|
mparse_options |= MPARSE_QUICK; |
385 |
|
|
break; |
386 |
|
|
case 'T': |
387 |
|
|
if (strcmp(optarg, "utf8")) { |
388 |
|
|
warnx("-T%s: Unsupported output format", |
389 |
|
|
optarg); |
390 |
|
|
goto usage; |
391 |
|
|
} |
392 |
|
|
write_utf8 = 1; |
393 |
|
|
break; |
394 |
|
|
case 't': |
395 |
|
|
CHECKOP(op, ch); |
396 |
|
|
dup2(STDOUT_FILENO, STDERR_FILENO); |
397 |
|
|
op = OP_TEST; |
398 |
|
|
nodb = warnings = 1; |
399 |
|
|
break; |
400 |
|
|
case 'u': |
401 |
|
|
CHECKOP(op, ch); |
402 |
|
|
path_arg = optarg; |
403 |
|
|
op = OP_DELETE; |
404 |
|
|
break; |
405 |
|
|
case 'v': |
406 |
|
|
/* Compatibility with espie@'s makewhatis. */ |
407 |
|
|
break; |
408 |
|
|
default: |
409 |
|
|
goto usage; |
410 |
|
|
} |
411 |
|
|
|
412 |
|
|
argc -= optind; |
413 |
|
|
argv += optind; |
414 |
|
|
|
415 |
|
|
if (nodb) { |
416 |
|
|
if (pledge("stdio rpath wpath cpath", NULL) == -1) { |
417 |
|
|
warn("pledge"); |
418 |
|
|
return (int)MANDOCLEVEL_SYSERR; |
419 |
|
|
} |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
if (OP_CONFFILE == op && argc > 0) { |
423 |
|
|
warnx("-C: Too many arguments"); |
424 |
|
|
goto usage; |
425 |
|
|
} |
426 |
|
|
|
427 |
|
|
exitcode = (int)MANDOCLEVEL_OK; |
428 |
|
|
mchars_alloc(); |
429 |
|
|
mp = mparse_alloc(mparse_options, MANDOCLEVEL_BADARG, NULL, NULL); |
430 |
|
|
mandoc_ohash_init(&mpages, 6, offsetof(struct mpage, inodev)); |
431 |
|
|
mandoc_ohash_init(&mlinks, 6, offsetof(struct mlink, file)); |
432 |
|
|
|
433 |
|
|
if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) { |
434 |
|
|
|
435 |
|
|
/* |
436 |
|
|
* Most of these deal with a specific directory. |
437 |
|
|
* Jump into that directory first. |
438 |
|
|
*/ |
439 |
|
|
if (OP_TEST != op && 0 == set_basedir(path_arg, 1)) |
440 |
|
|
goto out; |
441 |
|
|
|
442 |
|
|
if (dbopen(1)) { |
443 |
|
|
/* |
444 |
|
|
* The existing database is usable. Process |
445 |
|
|
* all files specified on the command-line. |
446 |
|
|
*/ |
447 |
|
|
if (!nodb) { |
448 |
|
|
if (pledge("stdio rpath wpath cpath fattr flock", NULL) == -1) { |
449 |
|
|
warn("pledge"); |
450 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
451 |
|
|
goto out; |
452 |
|
|
} |
453 |
|
|
} |
454 |
|
|
use_all = 1; |
455 |
|
|
for (i = 0; i < argc; i++) |
456 |
|
|
filescan(argv[i]); |
457 |
|
|
if (OP_TEST != op) |
458 |
|
|
dbprune(); |
459 |
|
|
} else { |
460 |
|
|
/* |
461 |
|
|
* Database missing or corrupt. |
462 |
|
|
* Recreate from scratch. |
463 |
|
|
*/ |
464 |
|
|
exitcode = (int)MANDOCLEVEL_OK; |
465 |
|
|
op = OP_DEFAULT; |
466 |
|
|
if (0 == treescan()) |
467 |
|
|
goto out; |
468 |
|
|
if (0 == dbopen(0)) |
469 |
|
|
goto out; |
470 |
|
|
} |
471 |
|
|
if (OP_DELETE != op) |
472 |
|
|
mpages_merge(mp); |
473 |
|
|
dbclose(OP_DEFAULT == op ? 0 : 1); |
474 |
|
|
} else { |
475 |
|
|
/* |
476 |
|
|
* If we have arguments, use them as our manpaths. |
477 |
|
|
* If we don't, grok from manpath(1) or however else |
478 |
|
|
* manconf_parse() wants to do it. |
479 |
|
|
*/ |
480 |
|
|
if (argc > 0) { |
481 |
|
|
conf.manpath.paths = mandoc_reallocarray(NULL, |
482 |
|
|
argc, sizeof(char *)); |
483 |
|
|
conf.manpath.sz = (size_t)argc; |
484 |
|
|
for (i = 0; i < argc; i++) |
485 |
|
|
conf.manpath.paths[i] = mandoc_strdup(argv[i]); |
486 |
|
|
} else |
487 |
|
|
manconf_parse(&conf, path_arg, NULL, NULL); |
488 |
|
|
|
489 |
|
|
if (conf.manpath.sz == 0) { |
490 |
|
|
exitcode = (int)MANDOCLEVEL_BADARG; |
491 |
|
|
say("", "Empty manpath"); |
492 |
|
|
} |
493 |
|
|
|
494 |
|
|
/* |
495 |
|
|
* First scan the tree rooted at a base directory, then |
496 |
|
|
* build a new database and finally move it into place. |
497 |
|
|
* Ignore zero-length directories and strip trailing |
498 |
|
|
* slashes. |
499 |
|
|
*/ |
500 |
|
|
for (j = 0; j < conf.manpath.sz; j++) { |
501 |
|
|
sz = strlen(conf.manpath.paths[j]); |
502 |
|
|
if (sz && conf.manpath.paths[j][sz - 1] == '/') |
503 |
|
|
conf.manpath.paths[j][--sz] = '\0'; |
504 |
|
|
if (0 == sz) |
505 |
|
|
continue; |
506 |
|
|
|
507 |
|
|
if (j) { |
508 |
|
|
mandoc_ohash_init(&mpages, 6, |
509 |
|
|
offsetof(struct mpage, inodev)); |
510 |
|
|
mandoc_ohash_init(&mlinks, 6, |
511 |
|
|
offsetof(struct mlink, file)); |
512 |
|
|
} |
513 |
|
|
|
514 |
|
|
if ( ! set_basedir(conf.manpath.paths[j], argc > 0)) |
515 |
|
|
continue; |
516 |
|
|
if (0 == treescan()) |
517 |
|
|
continue; |
518 |
|
|
if (0 == dbopen(0)) |
519 |
|
|
continue; |
520 |
|
|
|
521 |
|
|
mpages_merge(mp); |
522 |
|
|
if (warnings && !nodb && |
523 |
|
|
! (MPARSE_QUICK & mparse_options)) |
524 |
|
|
names_check(); |
525 |
|
|
dbclose(0); |
526 |
|
|
|
527 |
|
|
if (j + 1 < conf.manpath.sz) { |
528 |
|
|
mpages_free(); |
529 |
|
|
ohash_delete(&mpages); |
530 |
|
|
ohash_delete(&mlinks); |
531 |
|
|
} |
532 |
|
|
} |
533 |
|
|
} |
534 |
|
|
out: |
535 |
|
|
manconf_free(&conf); |
536 |
|
|
mparse_free(mp); |
537 |
|
|
mchars_free(); |
538 |
|
|
mpages_free(); |
539 |
|
|
ohash_delete(&mpages); |
540 |
|
|
ohash_delete(&mlinks); |
541 |
|
|
return exitcode; |
542 |
|
|
usage: |
543 |
|
|
progname = getprogname(); |
544 |
|
|
fprintf(stderr, "usage: %s [-aDnpQ] [-C file] [-Tutf8]\n" |
545 |
|
|
" %s [-aDnpQ] [-Tutf8] dir ...\n" |
546 |
|
|
" %s [-DnpQ] [-Tutf8] -d dir [file ...]\n" |
547 |
|
|
" %s [-Dnp] -u dir [file ...]\n" |
548 |
|
|
" %s [-Q] -t file ...\n", |
549 |
|
|
progname, progname, progname, progname, progname); |
550 |
|
|
|
551 |
|
|
return (int)MANDOCLEVEL_BADARG; |
552 |
|
|
} |
553 |
|
|
|
554 |
|
|
/* |
555 |
|
|
* Scan a directory tree rooted at "basedir" for manpages. |
556 |
|
|
* We use fts(), scanning directory parts along the way for clues to our |
557 |
|
|
* section and architecture. |
558 |
|
|
* |
559 |
|
|
* If use_all has been specified, grok all files. |
560 |
|
|
* If not, sanitise paths to the following: |
561 |
|
|
* |
562 |
|
|
* [./]man*[/<arch>]/<name>.<section> |
563 |
|
|
* or |
564 |
|
|
* [./]cat<section>[/<arch>]/<name>.0 |
565 |
|
|
* |
566 |
|
|
* TODO: accommodate for multi-language directories. |
567 |
|
|
*/ |
568 |
|
|
static int |
569 |
|
|
treescan(void) |
570 |
|
|
{ |
571 |
|
|
char buf[PATH_MAX]; |
572 |
|
|
FTS *f; |
573 |
|
|
FTSENT *ff; |
574 |
|
|
struct mlink *mlink; |
575 |
|
|
int dform, gzip; |
576 |
|
|
char *dsec, *arch, *fsec, *cp; |
577 |
|
|
const char *path; |
578 |
|
|
const char *argv[2]; |
579 |
|
|
|
580 |
|
|
argv[0] = "."; |
581 |
|
|
argv[1] = (char *)NULL; |
582 |
|
|
|
583 |
|
|
f = fts_open((char * const *)argv, |
584 |
|
|
FTS_PHYSICAL | FTS_NOCHDIR, NULL); |
585 |
|
|
if (f == NULL) { |
586 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
587 |
|
|
say("", "&fts_open"); |
588 |
|
|
return 0; |
589 |
|
|
} |
590 |
|
|
|
591 |
|
|
dsec = arch = NULL; |
592 |
|
|
dform = FORM_NONE; |
593 |
|
|
|
594 |
|
|
while ((ff = fts_read(f)) != NULL) { |
595 |
|
|
path = ff->fts_path + 2; |
596 |
|
|
switch (ff->fts_info) { |
597 |
|
|
|
598 |
|
|
/* |
599 |
|
|
* Symbolic links require various sanity checks, |
600 |
|
|
* then get handled just like regular files. |
601 |
|
|
*/ |
602 |
|
|
case FTS_SL: |
603 |
|
|
if (realpath(path, buf) == NULL) { |
604 |
|
|
if (warnings) |
605 |
|
|
say(path, "&realpath"); |
606 |
|
|
continue; |
607 |
|
|
} |
608 |
|
|
if (strstr(buf, basedir) != buf) { |
609 |
|
|
if (warnings) say("", |
610 |
|
|
"%s: outside base directory", buf); |
611 |
|
|
continue; |
612 |
|
|
} |
613 |
|
|
/* Use logical inode to avoid mpages dupe. */ |
614 |
|
|
if (stat(path, ff->fts_statp) == -1) { |
615 |
|
|
if (warnings) |
616 |
|
|
say(path, "&stat"); |
617 |
|
|
continue; |
618 |
|
|
} |
619 |
|
|
/* FALLTHROUGH */ |
620 |
|
|
|
621 |
|
|
/* |
622 |
|
|
* If we're a regular file, add an mlink by using the |
623 |
|
|
* stored directory data and handling the filename. |
624 |
|
|
*/ |
625 |
|
|
case FTS_F: |
626 |
|
|
if ( ! strcmp(path, MANDOC_DB)) |
627 |
|
|
continue; |
628 |
|
|
if ( ! use_all && ff->fts_level < 2) { |
629 |
|
|
if (warnings) |
630 |
|
|
say(path, "Extraneous file"); |
631 |
|
|
continue; |
632 |
|
|
} |
633 |
|
|
gzip = 0; |
634 |
|
|
fsec = NULL; |
635 |
|
|
while (fsec == NULL) { |
636 |
|
|
fsec = strrchr(ff->fts_name, '.'); |
637 |
|
|
if (fsec == NULL || strcmp(fsec+1, "gz")) |
638 |
|
|
break; |
639 |
|
|
gzip = 1; |
640 |
|
|
*fsec = '\0'; |
641 |
|
|
fsec = NULL; |
642 |
|
|
} |
643 |
|
|
if (fsec == NULL) { |
644 |
|
|
if ( ! use_all) { |
645 |
|
|
if (warnings) |
646 |
|
|
say(path, |
647 |
|
|
"No filename suffix"); |
648 |
|
|
continue; |
649 |
|
|
} |
650 |
|
|
} else if ( ! strcmp(++fsec, "html")) { |
651 |
|
|
if (warnings) |
652 |
|
|
say(path, "Skip html"); |
653 |
|
|
continue; |
654 |
|
|
} else if ( ! strcmp(fsec, "ps")) { |
655 |
|
|
if (warnings) |
656 |
|
|
say(path, "Skip ps"); |
657 |
|
|
continue; |
658 |
|
|
} else if ( ! strcmp(fsec, "pdf")) { |
659 |
|
|
if (warnings) |
660 |
|
|
say(path, "Skip pdf"); |
661 |
|
|
continue; |
662 |
|
|
} else if ( ! use_all && |
663 |
|
|
((dform == FORM_SRC && |
664 |
|
|
strncmp(fsec, dsec, strlen(dsec))) || |
665 |
|
|
(dform == FORM_CAT && strcmp(fsec, "0")))) { |
666 |
|
|
if (warnings) |
667 |
|
|
say(path, "Wrong filename suffix"); |
668 |
|
|
continue; |
669 |
|
|
} else |
670 |
|
|
fsec[-1] = '\0'; |
671 |
|
|
|
672 |
|
|
mlink = mandoc_calloc(1, sizeof(struct mlink)); |
673 |
|
|
if (strlcpy(mlink->file, path, |
674 |
|
|
sizeof(mlink->file)) >= |
675 |
|
|
sizeof(mlink->file)) { |
676 |
|
|
say(path, "Filename too long"); |
677 |
|
|
free(mlink); |
678 |
|
|
continue; |
679 |
|
|
} |
680 |
|
|
mlink->dform = dform; |
681 |
|
|
mlink->dsec = dsec; |
682 |
|
|
mlink->arch = arch; |
683 |
|
|
mlink->name = ff->fts_name; |
684 |
|
|
mlink->fsec = fsec; |
685 |
|
|
mlink->gzip = gzip; |
686 |
|
|
mlink_add(mlink, ff->fts_statp); |
687 |
|
|
continue; |
688 |
|
|
|
689 |
|
|
case FTS_D: |
690 |
|
|
case FTS_DP: |
691 |
|
|
break; |
692 |
|
|
|
693 |
|
|
default: |
694 |
|
|
if (warnings) |
695 |
|
|
say(path, "Not a regular file"); |
696 |
|
|
continue; |
697 |
|
|
} |
698 |
|
|
|
699 |
|
|
switch (ff->fts_level) { |
700 |
|
|
case 0: |
701 |
|
|
/* Ignore the root directory. */ |
702 |
|
|
break; |
703 |
|
|
case 1: |
704 |
|
|
/* |
705 |
|
|
* This might contain manX/ or catX/. |
706 |
|
|
* Try to infer this from the name. |
707 |
|
|
* If we're not in use_all, enforce it. |
708 |
|
|
*/ |
709 |
|
|
cp = ff->fts_name; |
710 |
|
|
if (ff->fts_info == FTS_DP) { |
711 |
|
|
dform = FORM_NONE; |
712 |
|
|
dsec = NULL; |
713 |
|
|
break; |
714 |
|
|
} |
715 |
|
|
|
716 |
|
|
if ( ! strncmp(cp, "man", 3)) { |
717 |
|
|
dform = FORM_SRC; |
718 |
|
|
dsec = cp + 3; |
719 |
|
|
} else if ( ! strncmp(cp, "cat", 3)) { |
720 |
|
|
dform = FORM_CAT; |
721 |
|
|
dsec = cp + 3; |
722 |
|
|
} else { |
723 |
|
|
dform = FORM_NONE; |
724 |
|
|
dsec = NULL; |
725 |
|
|
} |
726 |
|
|
|
727 |
|
|
if (dsec != NULL || use_all) |
728 |
|
|
break; |
729 |
|
|
|
730 |
|
|
if (warnings) |
731 |
|
|
say(path, "Unknown directory part"); |
732 |
|
|
fts_set(f, ff, FTS_SKIP); |
733 |
|
|
break; |
734 |
|
|
case 2: |
735 |
|
|
/* |
736 |
|
|
* Possibly our architecture. |
737 |
|
|
* If we're descending, keep tabs on it. |
738 |
|
|
*/ |
739 |
|
|
if (ff->fts_info != FTS_DP && dsec != NULL) |
740 |
|
|
arch = ff->fts_name; |
741 |
|
|
else |
742 |
|
|
arch = NULL; |
743 |
|
|
break; |
744 |
|
|
default: |
745 |
|
|
if (ff->fts_info == FTS_DP || use_all) |
746 |
|
|
break; |
747 |
|
|
if (warnings) |
748 |
|
|
say(path, "Extraneous directory part"); |
749 |
|
|
fts_set(f, ff, FTS_SKIP); |
750 |
|
|
break; |
751 |
|
|
} |
752 |
|
|
} |
753 |
|
|
|
754 |
|
|
fts_close(f); |
755 |
|
|
return 1; |
756 |
|
|
} |
757 |
|
|
|
758 |
|
|
/* |
759 |
|
|
* Add a file to the mlinks table. |
760 |
|
|
* Do not verify that it's a "valid" looking manpage (we'll do that |
761 |
|
|
* later). |
762 |
|
|
* |
763 |
|
|
* Try to infer the manual section, architecture, and page name from the |
764 |
|
|
* path, assuming it looks like |
765 |
|
|
* |
766 |
|
|
* [./]man*[/<arch>]/<name>.<section> |
767 |
|
|
* or |
768 |
|
|
* [./]cat<section>[/<arch>]/<name>.0 |
769 |
|
|
* |
770 |
|
|
* See treescan() for the fts(3) version of this. |
771 |
|
|
*/ |
772 |
|
|
static void |
773 |
|
|
filescan(const char *file) |
774 |
|
|
{ |
775 |
|
|
char buf[PATH_MAX]; |
776 |
|
|
struct stat st; |
777 |
|
|
struct mlink *mlink; |
778 |
|
|
char *p, *start; |
779 |
|
|
|
780 |
|
|
assert(use_all); |
781 |
|
|
|
782 |
|
|
if (0 == strncmp(file, "./", 2)) |
783 |
|
|
file += 2; |
784 |
|
|
|
785 |
|
|
/* |
786 |
|
|
* We have to do lstat(2) before realpath(3) loses |
787 |
|
|
* the information whether this is a symbolic link. |
788 |
|
|
* We need to know that because for symbolic links, |
789 |
|
|
* we want to use the orginal file name, while for |
790 |
|
|
* regular files, we want to use the real path. |
791 |
|
|
*/ |
792 |
|
|
if (-1 == lstat(file, &st)) { |
793 |
|
|
exitcode = (int)MANDOCLEVEL_BADARG; |
794 |
|
|
say(file, "&lstat"); |
795 |
|
|
return; |
796 |
|
|
} else if (0 == ((S_IFREG | S_IFLNK) & st.st_mode)) { |
797 |
|
|
exitcode = (int)MANDOCLEVEL_BADARG; |
798 |
|
|
say(file, "Not a regular file"); |
799 |
|
|
return; |
800 |
|
|
} |
801 |
|
|
|
802 |
|
|
/* |
803 |
|
|
* We have to resolve the file name to the real path |
804 |
|
|
* in any case for the base directory check. |
805 |
|
|
*/ |
806 |
|
|
if (NULL == realpath(file, buf)) { |
807 |
|
|
exitcode = (int)MANDOCLEVEL_BADARG; |
808 |
|
|
say(file, "&realpath"); |
809 |
|
|
return; |
810 |
|
|
} |
811 |
|
|
|
812 |
|
|
if (OP_TEST == op) |
813 |
|
|
start = buf; |
814 |
|
|
else if (strstr(buf, basedir) == buf) |
815 |
|
|
start = buf + strlen(basedir); |
816 |
|
|
else { |
817 |
|
|
exitcode = (int)MANDOCLEVEL_BADARG; |
818 |
|
|
say("", "%s: outside base directory", buf); |
819 |
|
|
return; |
820 |
|
|
} |
821 |
|
|
|
822 |
|
|
/* |
823 |
|
|
* Now we are sure the file is inside our tree. |
824 |
|
|
* If it is a symbolic link, ignore the real path |
825 |
|
|
* and use the original name. |
826 |
|
|
* This implies passing stuff like "cat1/../man1/foo.1" |
827 |
|
|
* on the command line won't work. So don't do that. |
828 |
|
|
* Note the stat(2) can still fail if the link target |
829 |
|
|
* doesn't exist. |
830 |
|
|
*/ |
831 |
|
|
if (S_IFLNK & st.st_mode) { |
832 |
|
|
if (-1 == stat(buf, &st)) { |
833 |
|
|
exitcode = (int)MANDOCLEVEL_BADARG; |
834 |
|
|
say(file, "&stat"); |
835 |
|
|
return; |
836 |
|
|
} |
837 |
|
|
if (strlcpy(buf, file, sizeof(buf)) >= sizeof(buf)) { |
838 |
|
|
say(file, "Filename too long"); |
839 |
|
|
return; |
840 |
|
|
} |
841 |
|
|
start = buf; |
842 |
|
|
if (OP_TEST != op && strstr(buf, basedir) == buf) |
843 |
|
|
start += strlen(basedir); |
844 |
|
|
} |
845 |
|
|
|
846 |
|
|
mlink = mandoc_calloc(1, sizeof(struct mlink)); |
847 |
|
|
mlink->dform = FORM_NONE; |
848 |
|
|
if (strlcpy(mlink->file, start, sizeof(mlink->file)) >= |
849 |
|
|
sizeof(mlink->file)) { |
850 |
|
|
say(start, "Filename too long"); |
851 |
|
|
free(mlink); |
852 |
|
|
return; |
853 |
|
|
} |
854 |
|
|
|
855 |
|
|
/* |
856 |
|
|
* First try to guess our directory structure. |
857 |
|
|
* If we find a separator, try to look for man* or cat*. |
858 |
|
|
* If we find one of these and what's underneath is a directory, |
859 |
|
|
* assume it's an architecture. |
860 |
|
|
*/ |
861 |
|
|
if (NULL != (p = strchr(start, '/'))) { |
862 |
|
|
*p++ = '\0'; |
863 |
|
|
if (0 == strncmp(start, "man", 3)) { |
864 |
|
|
mlink->dform = FORM_SRC; |
865 |
|
|
mlink->dsec = start + 3; |
866 |
|
|
} else if (0 == strncmp(start, "cat", 3)) { |
867 |
|
|
mlink->dform = FORM_CAT; |
868 |
|
|
mlink->dsec = start + 3; |
869 |
|
|
} |
870 |
|
|
|
871 |
|
|
start = p; |
872 |
|
|
if (NULL != mlink->dsec && NULL != (p = strchr(start, '/'))) { |
873 |
|
|
*p++ = '\0'; |
874 |
|
|
mlink->arch = start; |
875 |
|
|
start = p; |
876 |
|
|
} |
877 |
|
|
} |
878 |
|
|
|
879 |
|
|
/* |
880 |
|
|
* Now check the file suffix. |
881 |
|
|
* Suffix of `.0' indicates a catpage, `.1-9' is a manpage. |
882 |
|
|
*/ |
883 |
|
|
p = strrchr(start, '\0'); |
884 |
|
|
while (p-- > start && '/' != *p && '.' != *p) |
885 |
|
|
/* Loop. */ ; |
886 |
|
|
|
887 |
|
|
if ('.' == *p) { |
888 |
|
|
*p++ = '\0'; |
889 |
|
|
mlink->fsec = p; |
890 |
|
|
} |
891 |
|
|
|
892 |
|
|
/* |
893 |
|
|
* Now try to parse the name. |
894 |
|
|
* Use the filename portion of the path. |
895 |
|
|
*/ |
896 |
|
|
mlink->name = start; |
897 |
|
|
if (NULL != (p = strrchr(start, '/'))) { |
898 |
|
|
mlink->name = p + 1; |
899 |
|
|
*p = '\0'; |
900 |
|
|
} |
901 |
|
|
mlink_add(mlink, &st); |
902 |
|
|
} |
903 |
|
|
|
904 |
|
|
static void |
905 |
|
|
mlink_add(struct mlink *mlink, const struct stat *st) |
906 |
|
|
{ |
907 |
|
|
struct inodev inodev; |
908 |
|
|
struct mpage *mpage; |
909 |
|
|
unsigned int slot; |
910 |
|
|
|
911 |
|
|
assert(NULL != mlink->file); |
912 |
|
|
|
913 |
|
|
mlink->dsec = mandoc_strdup(mlink->dsec ? mlink->dsec : ""); |
914 |
|
|
mlink->arch = mandoc_strdup(mlink->arch ? mlink->arch : ""); |
915 |
|
|
mlink->name = mandoc_strdup(mlink->name ? mlink->name : ""); |
916 |
|
|
mlink->fsec = mandoc_strdup(mlink->fsec ? mlink->fsec : ""); |
917 |
|
|
|
918 |
|
|
if ('0' == *mlink->fsec) { |
919 |
|
|
free(mlink->fsec); |
920 |
|
|
mlink->fsec = mandoc_strdup(mlink->dsec); |
921 |
|
|
mlink->fform = FORM_CAT; |
922 |
|
|
} else if ('1' <= *mlink->fsec && '9' >= *mlink->fsec) |
923 |
|
|
mlink->fform = FORM_SRC; |
924 |
|
|
else |
925 |
|
|
mlink->fform = FORM_NONE; |
926 |
|
|
|
927 |
|
|
slot = ohash_qlookup(&mlinks, mlink->file); |
928 |
|
|
assert(NULL == ohash_find(&mlinks, slot)); |
929 |
|
|
ohash_insert(&mlinks, slot, mlink); |
930 |
|
|
|
931 |
|
|
memset(&inodev, 0, sizeof(inodev)); /* Clear padding. */ |
932 |
|
|
inodev.st_ino = st->st_ino; |
933 |
|
|
inodev.st_dev = st->st_dev; |
934 |
|
|
slot = ohash_lookup_memory(&mpages, (char *)&inodev, |
935 |
|
|
sizeof(struct inodev), inodev.st_ino); |
936 |
|
|
mpage = ohash_find(&mpages, slot); |
937 |
|
|
if (NULL == mpage) { |
938 |
|
|
mpage = mandoc_calloc(1, sizeof(struct mpage)); |
939 |
|
|
mpage->inodev.st_ino = inodev.st_ino; |
940 |
|
|
mpage->inodev.st_dev = inodev.st_dev; |
941 |
|
|
ohash_insert(&mpages, slot, mpage); |
942 |
|
|
} else |
943 |
|
|
mlink->next = mpage->mlinks; |
944 |
|
|
mpage->mlinks = mlink; |
945 |
|
|
mlink->mpage = mpage; |
946 |
|
|
} |
947 |
|
|
|
948 |
|
|
static void |
949 |
|
|
mlink_free(struct mlink *mlink) |
950 |
|
|
{ |
951 |
|
|
|
952 |
|
|
free(mlink->dsec); |
953 |
|
|
free(mlink->arch); |
954 |
|
|
free(mlink->name); |
955 |
|
|
free(mlink->fsec); |
956 |
|
|
free(mlink); |
957 |
|
|
} |
958 |
|
|
|
959 |
|
|
static void |
960 |
|
|
mpages_free(void) |
961 |
|
|
{ |
962 |
|
|
struct mpage *mpage; |
963 |
|
|
struct mlink *mlink; |
964 |
|
|
unsigned int slot; |
965 |
|
|
|
966 |
|
|
mpage = ohash_first(&mpages, &slot); |
967 |
|
|
while (NULL != mpage) { |
968 |
|
|
while (NULL != (mlink = mpage->mlinks)) { |
969 |
|
|
mpage->mlinks = mlink->next; |
970 |
|
|
mlink_free(mlink); |
971 |
|
|
} |
972 |
|
|
free(mpage->sec); |
973 |
|
|
free(mpage->arch); |
974 |
|
|
free(mpage->title); |
975 |
|
|
free(mpage->desc); |
976 |
|
|
free(mpage); |
977 |
|
|
mpage = ohash_next(&mpages, &slot); |
978 |
|
|
} |
979 |
|
|
} |
980 |
|
|
|
981 |
|
|
/* |
982 |
|
|
* For each mlink to the mpage, check whether the path looks like |
983 |
|
|
* it is formatted, and if it does, check whether a source manual |
984 |
|
|
* exists by the same name, ignoring the suffix. |
985 |
|
|
* If both conditions hold, drop the mlink. |
986 |
|
|
*/ |
987 |
|
|
static void |
988 |
|
|
mlinks_undupe(struct mpage *mpage) |
989 |
|
|
{ |
990 |
|
|
char buf[PATH_MAX]; |
991 |
|
|
struct mlink **prev; |
992 |
|
|
struct mlink *mlink; |
993 |
|
|
char *bufp; |
994 |
|
|
|
995 |
|
|
mpage->form = FORM_CAT; |
996 |
|
|
prev = &mpage->mlinks; |
997 |
|
|
while (NULL != (mlink = *prev)) { |
998 |
|
|
if (FORM_CAT != mlink->dform) { |
999 |
|
|
mpage->form = FORM_NONE; |
1000 |
|
|
goto nextlink; |
1001 |
|
|
} |
1002 |
|
|
(void)strlcpy(buf, mlink->file, sizeof(buf)); |
1003 |
|
|
bufp = strstr(buf, "cat"); |
1004 |
|
|
assert(NULL != bufp); |
1005 |
|
|
memcpy(bufp, "man", 3); |
1006 |
|
|
if (NULL != (bufp = strrchr(buf, '.'))) |
1007 |
|
|
*++bufp = '\0'; |
1008 |
|
|
(void)strlcat(buf, mlink->dsec, sizeof(buf)); |
1009 |
|
|
if (NULL == ohash_find(&mlinks, |
1010 |
|
|
ohash_qlookup(&mlinks, buf))) |
1011 |
|
|
goto nextlink; |
1012 |
|
|
if (warnings) |
1013 |
|
|
say(mlink->file, "Man source exists: %s", buf); |
1014 |
|
|
if (use_all) |
1015 |
|
|
goto nextlink; |
1016 |
|
|
*prev = mlink->next; |
1017 |
|
|
mlink_free(mlink); |
1018 |
|
|
continue; |
1019 |
|
|
nextlink: |
1020 |
|
|
prev = &(*prev)->next; |
1021 |
|
|
} |
1022 |
|
|
} |
1023 |
|
|
|
1024 |
|
|
static void |
1025 |
|
|
mlink_check(struct mpage *mpage, struct mlink *mlink) |
1026 |
|
|
{ |
1027 |
|
|
struct str *str; |
1028 |
|
|
unsigned int slot; |
1029 |
|
|
|
1030 |
|
|
/* |
1031 |
|
|
* Check whether the manual section given in a file |
1032 |
|
|
* agrees with the directory where the file is located. |
1033 |
|
|
* Some manuals have suffixes like (3p) on their |
1034 |
|
|
* section number either inside the file or in the |
1035 |
|
|
* directory name, some are linked into more than one |
1036 |
|
|
* section, like encrypt(1) = makekey(8). |
1037 |
|
|
*/ |
1038 |
|
|
|
1039 |
|
|
if (FORM_SRC == mpage->form && |
1040 |
|
|
strcasecmp(mpage->sec, mlink->dsec)) |
1041 |
|
|
say(mlink->file, "Section \"%s\" manual in %s directory", |
1042 |
|
|
mpage->sec, mlink->dsec); |
1043 |
|
|
|
1044 |
|
|
/* |
1045 |
|
|
* Manual page directories exist for each kernel |
1046 |
|
|
* architecture as returned by machine(1). |
1047 |
|
|
* However, many manuals only depend on the |
1048 |
|
|
* application architecture as returned by arch(1). |
1049 |
|
|
* For example, some (2/ARM) manuals are shared |
1050 |
|
|
* across the "armish" and "zaurus" kernel |
1051 |
|
|
* architectures. |
1052 |
|
|
* A few manuals are even shared across completely |
1053 |
|
|
* different architectures, for example fdformat(1) |
1054 |
|
|
* on amd64, i386, sparc, and sparc64. |
1055 |
|
|
*/ |
1056 |
|
|
|
1057 |
|
|
if (strcasecmp(mpage->arch, mlink->arch)) |
1058 |
|
|
say(mlink->file, "Architecture \"%s\" manual in " |
1059 |
|
|
"\"%s\" directory", mpage->arch, mlink->arch); |
1060 |
|
|
|
1061 |
|
|
/* |
1062 |
|
|
* XXX |
1063 |
|
|
* parse_cat() doesn't set NAME_TITLE yet. |
1064 |
|
|
*/ |
1065 |
|
|
|
1066 |
|
|
if (FORM_CAT == mpage->form) |
1067 |
|
|
return; |
1068 |
|
|
|
1069 |
|
|
/* |
1070 |
|
|
* Check whether this mlink |
1071 |
|
|
* appears as a name in the NAME section. |
1072 |
|
|
*/ |
1073 |
|
|
|
1074 |
|
|
slot = ohash_qlookup(&names, mlink->name); |
1075 |
|
|
str = ohash_find(&names, slot); |
1076 |
|
|
assert(NULL != str); |
1077 |
|
|
if ( ! (NAME_TITLE & str->mask)) |
1078 |
|
|
say(mlink->file, "Name missing in NAME section"); |
1079 |
|
|
} |
1080 |
|
|
|
1081 |
|
|
/* |
1082 |
|
|
* Run through the files in the global vector "mpages" |
1083 |
|
|
* and add them to the database specified in "basedir". |
1084 |
|
|
* |
1085 |
|
|
* This handles the parsing scheme itself, using the cues of directory |
1086 |
|
|
* and filename to determine whether the file is parsable or not. |
1087 |
|
|
*/ |
1088 |
|
|
static void |
1089 |
|
|
mpages_merge(struct mparse *mp) |
1090 |
|
|
{ |
1091 |
|
|
char any[] = "any"; |
1092 |
|
|
struct mpage *mpage, *mpage_dest; |
1093 |
|
|
struct mlink *mlink, *mlink_dest; |
1094 |
|
|
struct roff_man *man; |
1095 |
|
|
char *sodest; |
1096 |
|
|
char *cp; |
1097 |
|
|
int fd; |
1098 |
|
|
unsigned int pslot; |
1099 |
|
|
|
1100 |
|
|
if ( ! nodb) |
1101 |
|
|
SQL_EXEC("BEGIN TRANSACTION"); |
1102 |
|
|
|
1103 |
|
|
mpage = ohash_first(&mpages, &pslot); |
1104 |
|
|
while (mpage != NULL) { |
1105 |
|
|
mlinks_undupe(mpage); |
1106 |
|
|
if ((mlink = mpage->mlinks) == NULL) { |
1107 |
|
|
mpage = ohash_next(&mpages, &pslot); |
1108 |
|
|
continue; |
1109 |
|
|
} |
1110 |
|
|
|
1111 |
|
|
name_mask = NAME_MASK; |
1112 |
|
|
mandoc_ohash_init(&names, 4, offsetof(struct str, key)); |
1113 |
|
|
mandoc_ohash_init(&strings, 6, offsetof(struct str, key)); |
1114 |
|
|
mparse_reset(mp); |
1115 |
|
|
man = NULL; |
1116 |
|
|
sodest = NULL; |
1117 |
|
|
|
1118 |
|
|
if ((fd = mparse_open(mp, mlink->file)) == -1) { |
1119 |
|
|
say(mlink->file, "&open"); |
1120 |
|
|
goto nextpage; |
1121 |
|
|
} |
1122 |
|
|
|
1123 |
|
|
/* |
1124 |
|
|
* Interpret the file as mdoc(7) or man(7) source |
1125 |
|
|
* code, unless it is known to be formatted. |
1126 |
|
|
*/ |
1127 |
|
|
if (mlink->dform != FORM_CAT || mlink->fform != FORM_CAT) { |
1128 |
|
|
mparse_readfd(mp, fd, mlink->file); |
1129 |
|
|
close(fd); |
1130 |
|
|
mparse_result(mp, &man, &sodest); |
1131 |
|
|
} |
1132 |
|
|
|
1133 |
|
|
if (sodest != NULL) { |
1134 |
|
|
mlink_dest = ohash_find(&mlinks, |
1135 |
|
|
ohash_qlookup(&mlinks, sodest)); |
1136 |
|
|
if (mlink_dest == NULL) { |
1137 |
|
|
mandoc_asprintf(&cp, "%s.gz", sodest); |
1138 |
|
|
mlink_dest = ohash_find(&mlinks, |
1139 |
|
|
ohash_qlookup(&mlinks, cp)); |
1140 |
|
|
free(cp); |
1141 |
|
|
} |
1142 |
|
|
if (mlink_dest != NULL) { |
1143 |
|
|
|
1144 |
|
|
/* The .so target exists. */ |
1145 |
|
|
|
1146 |
|
|
mpage_dest = mlink_dest->mpage; |
1147 |
|
|
while (1) { |
1148 |
|
|
mlink->mpage = mpage_dest; |
1149 |
|
|
|
1150 |
|
|
/* |
1151 |
|
|
* If the target was already |
1152 |
|
|
* processed, add the links |
1153 |
|
|
* to the database now. |
1154 |
|
|
* Otherwise, this will |
1155 |
|
|
* happen when we come |
1156 |
|
|
* to the target. |
1157 |
|
|
*/ |
1158 |
|
|
|
1159 |
|
|
if (mpage_dest->pageid) |
1160 |
|
|
dbadd_mlink_name(mlink); |
1161 |
|
|
|
1162 |
|
|
if (mlink->next == NULL) |
1163 |
|
|
break; |
1164 |
|
|
mlink = mlink->next; |
1165 |
|
|
} |
1166 |
|
|
|
1167 |
|
|
/* Move all links to the target. */ |
1168 |
|
|
|
1169 |
|
|
mlink->next = mlink_dest->next; |
1170 |
|
|
mlink_dest->next = mpage->mlinks; |
1171 |
|
|
mpage->mlinks = NULL; |
1172 |
|
|
} |
1173 |
|
|
goto nextpage; |
1174 |
|
|
} else if (man != NULL && man->macroset == MACROSET_MDOC) { |
1175 |
|
|
mdoc_validate(man); |
1176 |
|
|
mpage->form = FORM_SRC; |
1177 |
|
|
mpage->sec = man->meta.msec; |
1178 |
|
|
mpage->sec = mandoc_strdup( |
1179 |
|
|
mpage->sec == NULL ? "" : mpage->sec); |
1180 |
|
|
mpage->arch = man->meta.arch; |
1181 |
|
|
mpage->arch = mandoc_strdup( |
1182 |
|
|
mpage->arch == NULL ? "" : mpage->arch); |
1183 |
|
|
mpage->title = mandoc_strdup(man->meta.title); |
1184 |
|
|
} else if (man != NULL && man->macroset == MACROSET_MAN) { |
1185 |
|
|
man_validate(man); |
1186 |
|
|
mpage->form = FORM_SRC; |
1187 |
|
|
mpage->sec = mandoc_strdup(man->meta.msec); |
1188 |
|
|
mpage->arch = mandoc_strdup(mlink->arch); |
1189 |
|
|
mpage->title = mandoc_strdup(man->meta.title); |
1190 |
|
|
} else { |
1191 |
|
|
mpage->form = FORM_CAT; |
1192 |
|
|
mpage->sec = mandoc_strdup(mlink->dsec); |
1193 |
|
|
mpage->arch = mandoc_strdup(mlink->arch); |
1194 |
|
|
mpage->title = mandoc_strdup(mlink->name); |
1195 |
|
|
} |
1196 |
|
|
putkey(mpage, mpage->sec, TYPE_sec); |
1197 |
|
|
if (*mpage->arch != '\0') |
1198 |
|
|
putkey(mpage, mpage->arch, TYPE_arch); |
1199 |
|
|
|
1200 |
|
|
for ( ; mlink != NULL; mlink = mlink->next) { |
1201 |
|
|
if ('\0' != *mlink->dsec) |
1202 |
|
|
putkey(mpage, mlink->dsec, TYPE_sec); |
1203 |
|
|
if ('\0' != *mlink->fsec) |
1204 |
|
|
putkey(mpage, mlink->fsec, TYPE_sec); |
1205 |
|
|
putkey(mpage, '\0' == *mlink->arch ? |
1206 |
|
|
any : mlink->arch, TYPE_arch); |
1207 |
|
|
putkey(mpage, mlink->name, NAME_FILE); |
1208 |
|
|
} |
1209 |
|
|
|
1210 |
|
|
assert(mpage->desc == NULL); |
1211 |
|
|
if (man != NULL && man->macroset == MACROSET_MDOC) |
1212 |
|
|
parse_mdoc(mpage, &man->meta, man->first); |
1213 |
|
|
else if (man != NULL) |
1214 |
|
|
parse_man(mpage, &man->meta, man->first); |
1215 |
|
|
else |
1216 |
|
|
parse_cat(mpage, fd); |
1217 |
|
|
if (mpage->desc == NULL) |
1218 |
|
|
mpage->desc = mandoc_strdup(mpage->mlinks->name); |
1219 |
|
|
|
1220 |
|
|
if (warnings && !use_all) |
1221 |
|
|
for (mlink = mpage->mlinks; mlink; |
1222 |
|
|
mlink = mlink->next) |
1223 |
|
|
mlink_check(mpage, mlink); |
1224 |
|
|
|
1225 |
|
|
dbadd(mpage); |
1226 |
|
|
mlink = mpage->mlinks; |
1227 |
|
|
|
1228 |
|
|
nextpage: |
1229 |
|
|
ohash_delete(&strings); |
1230 |
|
|
ohash_delete(&names); |
1231 |
|
|
mpage = ohash_next(&mpages, &pslot); |
1232 |
|
|
} |
1233 |
|
|
|
1234 |
|
|
if (0 == nodb) |
1235 |
|
|
SQL_EXEC("END TRANSACTION"); |
1236 |
|
|
} |
1237 |
|
|
|
1238 |
|
|
static void |
1239 |
|
|
names_check(void) |
1240 |
|
|
{ |
1241 |
|
|
sqlite3_stmt *stmt; |
1242 |
|
|
const char *name, *sec, *arch, *key; |
1243 |
|
|
|
1244 |
|
|
sqlite3_prepare_v2(db, |
1245 |
|
|
"SELECT name, sec, arch, key FROM (" |
1246 |
|
|
"SELECT name AS key, pageid FROM names " |
1247 |
|
|
"WHERE bits & ? AND NOT EXISTS (" |
1248 |
|
|
"SELECT pageid FROM mlinks " |
1249 |
|
|
"WHERE mlinks.pageid == names.pageid " |
1250 |
|
|
"AND mlinks.name == names.name" |
1251 |
|
|
")" |
1252 |
|
|
") JOIN (" |
1253 |
|
|
"SELECT sec, arch, name, pageid FROM mlinks " |
1254 |
|
|
"GROUP BY pageid" |
1255 |
|
|
") USING (pageid);", |
1256 |
|
|
-1, &stmt, NULL); |
1257 |
|
|
|
1258 |
|
|
if (sqlite3_bind_int64(stmt, 1, NAME_TITLE) != SQLITE_OK) |
1259 |
|
|
say("", "%s", sqlite3_errmsg(db)); |
1260 |
|
|
|
1261 |
|
|
while (sqlite3_step(stmt) == SQLITE_ROW) { |
1262 |
|
|
name = (const char *)sqlite3_column_text(stmt, 0); |
1263 |
|
|
sec = (const char *)sqlite3_column_text(stmt, 1); |
1264 |
|
|
arch = (const char *)sqlite3_column_text(stmt, 2); |
1265 |
|
|
key = (const char *)sqlite3_column_text(stmt, 3); |
1266 |
|
|
say("", "%s(%s%s%s) lacks mlink \"%s\"", name, sec, |
1267 |
|
|
'\0' == *arch ? "" : "/", |
1268 |
|
|
'\0' == *arch ? "" : arch, key); |
1269 |
|
|
} |
1270 |
|
|
sqlite3_finalize(stmt); |
1271 |
|
|
} |
1272 |
|
|
|
1273 |
|
|
static void |
1274 |
|
|
parse_cat(struct mpage *mpage, int fd) |
1275 |
|
|
{ |
1276 |
|
|
FILE *stream; |
1277 |
|
|
char *line, *p, *title; |
1278 |
|
|
size_t linesz, plen, titlesz; |
1279 |
|
|
ssize_t len; |
1280 |
|
|
int offs; |
1281 |
|
|
|
1282 |
|
|
stream = (-1 == fd) ? |
1283 |
|
|
fopen(mpage->mlinks->file, "r") : |
1284 |
|
|
fdopen(fd, "r"); |
1285 |
|
|
if (NULL == stream) { |
1286 |
|
|
if (-1 != fd) |
1287 |
|
|
close(fd); |
1288 |
|
|
if (warnings) |
1289 |
|
|
say(mpage->mlinks->file, "&fopen"); |
1290 |
|
|
return; |
1291 |
|
|
} |
1292 |
|
|
|
1293 |
|
|
line = NULL; |
1294 |
|
|
linesz = 0; |
1295 |
|
|
|
1296 |
|
|
/* Skip to first blank line. */ |
1297 |
|
|
|
1298 |
|
|
while (getline(&line, &linesz, stream) != -1) |
1299 |
|
|
if (*line == '\n') |
1300 |
|
|
break; |
1301 |
|
|
|
1302 |
|
|
/* |
1303 |
|
|
* Assume the first line that is not indented |
1304 |
|
|
* is the first section header. Skip to it. |
1305 |
|
|
*/ |
1306 |
|
|
|
1307 |
|
|
while (getline(&line, &linesz, stream) != -1) |
1308 |
|
|
if (*line != '\n' && *line != ' ') |
1309 |
|
|
break; |
1310 |
|
|
|
1311 |
|
|
/* |
1312 |
|
|
* Read up until the next section into a buffer. |
1313 |
|
|
* Strip the leading and trailing newline from each read line, |
1314 |
|
|
* appending a trailing space. |
1315 |
|
|
* Ignore empty (whitespace-only) lines. |
1316 |
|
|
*/ |
1317 |
|
|
|
1318 |
|
|
titlesz = 0; |
1319 |
|
|
title = NULL; |
1320 |
|
|
|
1321 |
|
|
while ((len = getline(&line, &linesz, stream)) != -1) { |
1322 |
|
|
if (*line != ' ') |
1323 |
|
|
break; |
1324 |
|
|
offs = 0; |
1325 |
|
|
while (isspace((unsigned char)line[offs])) |
1326 |
|
|
offs++; |
1327 |
|
|
if (line[offs] == '\0') |
1328 |
|
|
continue; |
1329 |
|
|
title = mandoc_realloc(title, titlesz + len - offs); |
1330 |
|
|
memcpy(title + titlesz, line + offs, len - offs); |
1331 |
|
|
titlesz += len - offs; |
1332 |
|
|
title[titlesz - 1] = ' '; |
1333 |
|
|
} |
1334 |
|
|
free(line); |
1335 |
|
|
|
1336 |
|
|
/* |
1337 |
|
|
* If no page content can be found, or the input line |
1338 |
|
|
* is already the next section header, or there is no |
1339 |
|
|
* trailing newline, reuse the page title as the page |
1340 |
|
|
* description. |
1341 |
|
|
*/ |
1342 |
|
|
|
1343 |
|
|
if (NULL == title || '\0' == *title) { |
1344 |
|
|
if (warnings) |
1345 |
|
|
say(mpage->mlinks->file, |
1346 |
|
|
"Cannot find NAME section"); |
1347 |
|
|
fclose(stream); |
1348 |
|
|
free(title); |
1349 |
|
|
return; |
1350 |
|
|
} |
1351 |
|
|
|
1352 |
|
|
title[titlesz - 1] = '\0'; |
1353 |
|
|
|
1354 |
|
|
/* |
1355 |
|
|
* Skip to the first dash. |
1356 |
|
|
* Use the remaining line as the description (no more than 70 |
1357 |
|
|
* bytes). |
1358 |
|
|
*/ |
1359 |
|
|
|
1360 |
|
|
if (NULL != (p = strstr(title, "- "))) { |
1361 |
|
|
for (p += 2; ' ' == *p || '\b' == *p; p++) |
1362 |
|
|
/* Skip to next word. */ ; |
1363 |
|
|
} else { |
1364 |
|
|
if (warnings) |
1365 |
|
|
say(mpage->mlinks->file, |
1366 |
|
|
"No dash in title line"); |
1367 |
|
|
p = title; |
1368 |
|
|
} |
1369 |
|
|
|
1370 |
|
|
plen = strlen(p); |
1371 |
|
|
|
1372 |
|
|
/* Strip backspace-encoding from line. */ |
1373 |
|
|
|
1374 |
|
|
while (NULL != (line = memchr(p, '\b', plen))) { |
1375 |
|
|
len = line - p; |
1376 |
|
|
if (0 == len) { |
1377 |
|
|
memmove(line, line + 1, plen--); |
1378 |
|
|
continue; |
1379 |
|
|
} |
1380 |
|
|
memmove(line - 1, line + 1, plen - len); |
1381 |
|
|
plen -= 2; |
1382 |
|
|
} |
1383 |
|
|
|
1384 |
|
|
mpage->desc = mandoc_strdup(p); |
1385 |
|
|
fclose(stream); |
1386 |
|
|
free(title); |
1387 |
|
|
} |
1388 |
|
|
|
1389 |
|
|
/* |
1390 |
|
|
* Put a type/word pair into the word database for this particular file. |
1391 |
|
|
*/ |
1392 |
|
|
static void |
1393 |
|
|
putkey(const struct mpage *mpage, char *value, uint64_t type) |
1394 |
|
|
{ |
1395 |
|
|
char *cp; |
1396 |
|
|
|
1397 |
|
|
assert(NULL != value); |
1398 |
|
|
if (TYPE_arch == type) |
1399 |
|
|
for (cp = value; *cp; cp++) |
1400 |
|
|
if (isupper((unsigned char)*cp)) |
1401 |
|
|
*cp = _tolower((unsigned char)*cp); |
1402 |
|
|
putkeys(mpage, value, strlen(value), type); |
1403 |
|
|
} |
1404 |
|
|
|
1405 |
|
|
/* |
1406 |
|
|
* Grok all nodes at or below a certain mdoc node into putkey(). |
1407 |
|
|
*/ |
1408 |
|
|
static void |
1409 |
|
|
putmdockey(const struct mpage *mpage, |
1410 |
|
|
const struct roff_node *n, uint64_t m) |
1411 |
|
|
{ |
1412 |
|
|
|
1413 |
|
|
for ( ; NULL != n; n = n->next) { |
1414 |
|
|
if (NULL != n->child) |
1415 |
|
|
putmdockey(mpage, n->child, m); |
1416 |
|
|
if (n->type == ROFFT_TEXT) |
1417 |
|
|
putkey(mpage, n->string, m); |
1418 |
|
|
} |
1419 |
|
|
} |
1420 |
|
|
|
1421 |
|
|
static void |
1422 |
|
|
parse_man(struct mpage *mpage, const struct roff_meta *meta, |
1423 |
|
|
const struct roff_node *n) |
1424 |
|
|
{ |
1425 |
|
|
const struct roff_node *head, *body; |
1426 |
|
|
char *start, *title; |
1427 |
|
|
char byte; |
1428 |
|
|
size_t sz; |
1429 |
|
|
|
1430 |
|
|
if (n == NULL) |
1431 |
|
|
return; |
1432 |
|
|
|
1433 |
|
|
/* |
1434 |
|
|
* We're only searching for one thing: the first text child in |
1435 |
|
|
* the BODY of a NAME section. Since we don't keep track of |
1436 |
|
|
* sections in -man, run some hoops to find out whether we're in |
1437 |
|
|
* the correct section or not. |
1438 |
|
|
*/ |
1439 |
|
|
|
1440 |
|
|
if (n->type == ROFFT_BODY && n->tok == MAN_SH) { |
1441 |
|
|
body = n; |
1442 |
|
|
if ((head = body->parent->head) != NULL && |
1443 |
|
|
(head = head->child) != NULL && |
1444 |
|
|
head->next == NULL && |
1445 |
|
|
head->type == ROFFT_TEXT && |
1446 |
|
|
strcmp(head->string, "NAME") == 0 && |
1447 |
|
|
body->child != NULL) { |
1448 |
|
|
|
1449 |
|
|
/* |
1450 |
|
|
* Suck the entire NAME section into memory. |
1451 |
|
|
* Yes, we might run away. |
1452 |
|
|
* But too many manuals have big, spread-out |
1453 |
|
|
* NAME sections over many lines. |
1454 |
|
|
*/ |
1455 |
|
|
|
1456 |
|
|
title = NULL; |
1457 |
|
|
deroff(&title, body); |
1458 |
|
|
if (NULL == title) |
1459 |
|
|
return; |
1460 |
|
|
|
1461 |
|
|
/* |
1462 |
|
|
* Go through a special heuristic dance here. |
1463 |
|
|
* Conventionally, one or more manual names are |
1464 |
|
|
* comma-specified prior to a whitespace, then a |
1465 |
|
|
* dash, then a description. Try to puzzle out |
1466 |
|
|
* the name parts here. |
1467 |
|
|
*/ |
1468 |
|
|
|
1469 |
|
|
start = title; |
1470 |
|
|
for ( ;; ) { |
1471 |
|
|
sz = strcspn(start, " ,"); |
1472 |
|
|
if ('\0' == start[sz]) |
1473 |
|
|
break; |
1474 |
|
|
|
1475 |
|
|
byte = start[sz]; |
1476 |
|
|
start[sz] = '\0'; |
1477 |
|
|
|
1478 |
|
|
/* |
1479 |
|
|
* Assume a stray trailing comma in the |
1480 |
|
|
* name list if a name begins with a dash. |
1481 |
|
|
*/ |
1482 |
|
|
|
1483 |
|
|
if ('-' == start[0] || |
1484 |
|
|
('\\' == start[0] && '-' == start[1])) |
1485 |
|
|
break; |
1486 |
|
|
|
1487 |
|
|
putkey(mpage, start, NAME_TITLE); |
1488 |
|
|
if ( ! (mpage->name_head_done || |
1489 |
|
|
strcasecmp(start, meta->title))) { |
1490 |
|
|
putkey(mpage, start, NAME_HEAD); |
1491 |
|
|
mpage->name_head_done = 1; |
1492 |
|
|
} |
1493 |
|
|
|
1494 |
|
|
if (' ' == byte) { |
1495 |
|
|
start += sz + 1; |
1496 |
|
|
break; |
1497 |
|
|
} |
1498 |
|
|
|
1499 |
|
|
assert(',' == byte); |
1500 |
|
|
start += sz + 1; |
1501 |
|
|
while (' ' == *start) |
1502 |
|
|
start++; |
1503 |
|
|
} |
1504 |
|
|
|
1505 |
|
|
if (start == title) { |
1506 |
|
|
putkey(mpage, start, NAME_TITLE); |
1507 |
|
|
if ( ! (mpage->name_head_done || |
1508 |
|
|
strcasecmp(start, meta->title))) { |
1509 |
|
|
putkey(mpage, start, NAME_HEAD); |
1510 |
|
|
mpage->name_head_done = 1; |
1511 |
|
|
} |
1512 |
|
|
free(title); |
1513 |
|
|
return; |
1514 |
|
|
} |
1515 |
|
|
|
1516 |
|
|
while (isspace((unsigned char)*start)) |
1517 |
|
|
start++; |
1518 |
|
|
|
1519 |
|
|
if (0 == strncmp(start, "-", 1)) |
1520 |
|
|
start += 1; |
1521 |
|
|
else if (0 == strncmp(start, "\\-\\-", 4)) |
1522 |
|
|
start += 4; |
1523 |
|
|
else if (0 == strncmp(start, "\\-", 2)) |
1524 |
|
|
start += 2; |
1525 |
|
|
else if (0 == strncmp(start, "\\(en", 4)) |
1526 |
|
|
start += 4; |
1527 |
|
|
else if (0 == strncmp(start, "\\(em", 4)) |
1528 |
|
|
start += 4; |
1529 |
|
|
|
1530 |
|
|
while (' ' == *start) |
1531 |
|
|
start++; |
1532 |
|
|
|
1533 |
|
|
mpage->desc = mandoc_strdup(start); |
1534 |
|
|
free(title); |
1535 |
|
|
return; |
1536 |
|
|
} |
1537 |
|
|
} |
1538 |
|
|
|
1539 |
|
|
for (n = n->child; n; n = n->next) { |
1540 |
|
|
if (NULL != mpage->desc) |
1541 |
|
|
break; |
1542 |
|
|
parse_man(mpage, meta, n); |
1543 |
|
|
} |
1544 |
|
|
} |
1545 |
|
|
|
1546 |
|
|
static void |
1547 |
|
|
parse_mdoc(struct mpage *mpage, const struct roff_meta *meta, |
1548 |
|
|
const struct roff_node *n) |
1549 |
|
|
{ |
1550 |
|
|
|
1551 |
|
|
assert(NULL != n); |
1552 |
|
|
for (n = n->child; NULL != n; n = n->next) { |
1553 |
|
|
switch (n->type) { |
1554 |
|
|
case ROFFT_ELEM: |
1555 |
|
|
case ROFFT_BLOCK: |
1556 |
|
|
case ROFFT_HEAD: |
1557 |
|
|
case ROFFT_BODY: |
1558 |
|
|
case ROFFT_TAIL: |
1559 |
|
|
if (NULL != mdocs[n->tok].fp) |
1560 |
|
|
if (0 == (*mdocs[n->tok].fp)(mpage, meta, n)) |
1561 |
|
|
break; |
1562 |
|
|
if (mdocs[n->tok].mask) |
1563 |
|
|
putmdockey(mpage, n->child, |
1564 |
|
|
mdocs[n->tok].mask); |
1565 |
|
|
break; |
1566 |
|
|
default: |
1567 |
|
|
assert(n->type != ROFFT_ROOT); |
1568 |
|
|
continue; |
1569 |
|
|
} |
1570 |
|
|
if (NULL != n->child) |
1571 |
|
|
parse_mdoc(mpage, meta, n); |
1572 |
|
|
} |
1573 |
|
|
} |
1574 |
|
|
|
1575 |
|
|
static int |
1576 |
|
|
parse_mdoc_Fd(struct mpage *mpage, const struct roff_meta *meta, |
1577 |
|
|
const struct roff_node *n) |
1578 |
|
|
{ |
1579 |
|
|
char *start, *end; |
1580 |
|
|
size_t sz; |
1581 |
|
|
|
1582 |
|
|
if (SEC_SYNOPSIS != n->sec || |
1583 |
|
|
NULL == (n = n->child) || |
1584 |
|
|
n->type != ROFFT_TEXT) |
1585 |
|
|
return 0; |
1586 |
|
|
|
1587 |
|
|
/* |
1588 |
|
|
* Only consider those `Fd' macro fields that begin with an |
1589 |
|
|
* "inclusion" token (versus, e.g., #define). |
1590 |
|
|
*/ |
1591 |
|
|
|
1592 |
|
|
if (strcmp("#include", n->string)) |
1593 |
|
|
return 0; |
1594 |
|
|
|
1595 |
|
|
if ((n = n->next) == NULL || n->type != ROFFT_TEXT) |
1596 |
|
|
return 0; |
1597 |
|
|
|
1598 |
|
|
/* |
1599 |
|
|
* Strip away the enclosing angle brackets and make sure we're |
1600 |
|
|
* not zero-length. |
1601 |
|
|
*/ |
1602 |
|
|
|
1603 |
|
|
start = n->string; |
1604 |
|
|
if ('<' == *start || '"' == *start) |
1605 |
|
|
start++; |
1606 |
|
|
|
1607 |
|
|
if (0 == (sz = strlen(start))) |
1608 |
|
|
return 0; |
1609 |
|
|
|
1610 |
|
|
end = &start[(int)sz - 1]; |
1611 |
|
|
if ('>' == *end || '"' == *end) |
1612 |
|
|
end--; |
1613 |
|
|
|
1614 |
|
|
if (end > start) |
1615 |
|
|
putkeys(mpage, start, end - start + 1, TYPE_In); |
1616 |
|
|
return 0; |
1617 |
|
|
} |
1618 |
|
|
|
1619 |
|
|
static void |
1620 |
|
|
parse_mdoc_fname(struct mpage *mpage, const struct roff_node *n) |
1621 |
|
|
{ |
1622 |
|
|
char *cp; |
1623 |
|
|
size_t sz; |
1624 |
|
|
|
1625 |
|
|
if (n->type != ROFFT_TEXT) |
1626 |
|
|
return; |
1627 |
|
|
|
1628 |
|
|
/* Skip function pointer punctuation. */ |
1629 |
|
|
|
1630 |
|
|
cp = n->string; |
1631 |
|
|
while (*cp == '(' || *cp == '*') |
1632 |
|
|
cp++; |
1633 |
|
|
sz = strcspn(cp, "()"); |
1634 |
|
|
|
1635 |
|
|
putkeys(mpage, cp, sz, TYPE_Fn); |
1636 |
|
|
if (n->sec == SEC_SYNOPSIS) |
1637 |
|
|
putkeys(mpage, cp, sz, NAME_SYN); |
1638 |
|
|
} |
1639 |
|
|
|
1640 |
|
|
static int |
1641 |
|
|
parse_mdoc_Fn(struct mpage *mpage, const struct roff_meta *meta, |
1642 |
|
|
const struct roff_node *n) |
1643 |
|
|
{ |
1644 |
|
|
|
1645 |
|
|
if (n->child == NULL) |
1646 |
|
|
return 0; |
1647 |
|
|
|
1648 |
|
|
parse_mdoc_fname(mpage, n->child); |
1649 |
|
|
|
1650 |
|
|
for (n = n->child->next; n != NULL; n = n->next) |
1651 |
|
|
if (n->type == ROFFT_TEXT) |
1652 |
|
|
putkey(mpage, n->string, TYPE_Fa); |
1653 |
|
|
|
1654 |
|
|
return 0; |
1655 |
|
|
} |
1656 |
|
|
|
1657 |
|
|
static int |
1658 |
|
|
parse_mdoc_Fo(struct mpage *mpage, const struct roff_meta *meta, |
1659 |
|
|
const struct roff_node *n) |
1660 |
|
|
{ |
1661 |
|
|
|
1662 |
|
|
if (n->type != ROFFT_HEAD) |
1663 |
|
|
return 1; |
1664 |
|
|
|
1665 |
|
|
if (n->child != NULL) |
1666 |
|
|
parse_mdoc_fname(mpage, n->child); |
1667 |
|
|
|
1668 |
|
|
return 0; |
1669 |
|
|
} |
1670 |
|
|
|
1671 |
|
|
static int |
1672 |
|
|
parse_mdoc_Va(struct mpage *mpage, const struct roff_meta *meta, |
1673 |
|
|
const struct roff_node *n) |
1674 |
|
|
{ |
1675 |
|
|
char *cp; |
1676 |
|
|
|
1677 |
|
|
if (n->type != ROFFT_ELEM && n->type != ROFFT_BODY) |
1678 |
|
|
return 0; |
1679 |
|
|
|
1680 |
|
|
if (n->child != NULL && |
1681 |
|
|
n->child->next == NULL && |
1682 |
|
|
n->child->type == ROFFT_TEXT) |
1683 |
|
|
return 1; |
1684 |
|
|
|
1685 |
|
|
cp = NULL; |
1686 |
|
|
deroff(&cp, n); |
1687 |
|
|
if (cp != NULL) { |
1688 |
|
|
putkey(mpage, cp, TYPE_Vt | (n->tok == MDOC_Va || |
1689 |
|
|
n->type == ROFFT_BODY ? TYPE_Va : 0)); |
1690 |
|
|
free(cp); |
1691 |
|
|
} |
1692 |
|
|
|
1693 |
|
|
return 0; |
1694 |
|
|
} |
1695 |
|
|
|
1696 |
|
|
static int |
1697 |
|
|
parse_mdoc_Xr(struct mpage *mpage, const struct roff_meta *meta, |
1698 |
|
|
const struct roff_node *n) |
1699 |
|
|
{ |
1700 |
|
|
char *cp; |
1701 |
|
|
|
1702 |
|
|
if (NULL == (n = n->child)) |
1703 |
|
|
return 0; |
1704 |
|
|
|
1705 |
|
|
if (NULL == n->next) { |
1706 |
|
|
putkey(mpage, n->string, TYPE_Xr); |
1707 |
|
|
return 0; |
1708 |
|
|
} |
1709 |
|
|
|
1710 |
|
|
mandoc_asprintf(&cp, "%s(%s)", n->string, n->next->string); |
1711 |
|
|
putkey(mpage, cp, TYPE_Xr); |
1712 |
|
|
free(cp); |
1713 |
|
|
return 0; |
1714 |
|
|
} |
1715 |
|
|
|
1716 |
|
|
static int |
1717 |
|
|
parse_mdoc_Nd(struct mpage *mpage, const struct roff_meta *meta, |
1718 |
|
|
const struct roff_node *n) |
1719 |
|
|
{ |
1720 |
|
|
|
1721 |
|
|
if (n->type == ROFFT_BODY) |
1722 |
|
|
deroff(&mpage->desc, n); |
1723 |
|
|
return 0; |
1724 |
|
|
} |
1725 |
|
|
|
1726 |
|
|
static int |
1727 |
|
|
parse_mdoc_Nm(struct mpage *mpage, const struct roff_meta *meta, |
1728 |
|
|
const struct roff_node *n) |
1729 |
|
|
{ |
1730 |
|
|
|
1731 |
|
|
if (SEC_NAME == n->sec) |
1732 |
|
|
putmdockey(mpage, n->child, NAME_TITLE); |
1733 |
|
|
else if (n->sec == SEC_SYNOPSIS && n->type == ROFFT_HEAD) { |
1734 |
|
|
if (n->child == NULL) |
1735 |
|
|
putkey(mpage, meta->name, NAME_SYN); |
1736 |
|
|
else |
1737 |
|
|
putmdockey(mpage, n->child, NAME_SYN); |
1738 |
|
|
} |
1739 |
|
|
if ( ! (mpage->name_head_done || |
1740 |
|
|
n->child == NULL || n->child->string == NULL || |
1741 |
|
|
strcasecmp(n->child->string, meta->title))) { |
1742 |
|
|
putkey(mpage, n->child->string, ROFFT_HEAD); |
1743 |
|
|
mpage->name_head_done = 1; |
1744 |
|
|
} |
1745 |
|
|
return 0; |
1746 |
|
|
} |
1747 |
|
|
|
1748 |
|
|
static int |
1749 |
|
|
parse_mdoc_Sh(struct mpage *mpage, const struct roff_meta *meta, |
1750 |
|
|
const struct roff_node *n) |
1751 |
|
|
{ |
1752 |
|
|
|
1753 |
|
|
return n->sec == SEC_CUSTOM && n->type == ROFFT_HEAD; |
1754 |
|
|
} |
1755 |
|
|
|
1756 |
|
|
static int |
1757 |
|
|
parse_mdoc_head(struct mpage *mpage, const struct roff_meta *meta, |
1758 |
|
|
const struct roff_node *n) |
1759 |
|
|
{ |
1760 |
|
|
|
1761 |
|
|
return n->type == ROFFT_HEAD; |
1762 |
|
|
} |
1763 |
|
|
|
1764 |
|
|
/* |
1765 |
|
|
* Add a string to the hash table for the current manual. |
1766 |
|
|
* Each string has a bitmask telling which macros it belongs to. |
1767 |
|
|
* When we finish the manual, we'll dump the table. |
1768 |
|
|
*/ |
1769 |
|
|
static void |
1770 |
|
|
putkeys(const struct mpage *mpage, char *cp, size_t sz, uint64_t v) |
1771 |
|
|
{ |
1772 |
|
|
struct ohash *htab; |
1773 |
|
|
struct str *s; |
1774 |
|
|
const char *end; |
1775 |
|
|
unsigned int slot; |
1776 |
|
|
int i, mustfree; |
1777 |
|
|
|
1778 |
|
|
if (0 == sz) |
1779 |
|
|
return; |
1780 |
|
|
|
1781 |
|
|
mustfree = render_string(&cp, &sz); |
1782 |
|
|
|
1783 |
|
|
if (TYPE_Nm & v) { |
1784 |
|
|
htab = &names; |
1785 |
|
|
v &= name_mask; |
1786 |
|
|
if (v & NAME_FIRST) |
1787 |
|
|
name_mask &= ~NAME_FIRST; |
1788 |
|
|
if (debug > 1) |
1789 |
|
|
say(mpage->mlinks->file, |
1790 |
|
|
"Adding name %*s, bits=0x%llu", (int)sz, cp, v); |
1791 |
|
|
} else { |
1792 |
|
|
htab = &strings; |
1793 |
|
|
if (debug > 1) |
1794 |
|
|
for (i = 0; i < mansearch_keymax; i++) |
1795 |
|
|
if ((uint64_t)1 << i & v) |
1796 |
|
|
say(mpage->mlinks->file, |
1797 |
|
|
"Adding key %s=%*s", |
1798 |
|
|
mansearch_keynames[i], (int)sz, cp); |
1799 |
|
|
} |
1800 |
|
|
|
1801 |
|
|
end = cp + sz; |
1802 |
|
|
slot = ohash_qlookupi(htab, cp, &end); |
1803 |
|
|
s = ohash_find(htab, slot); |
1804 |
|
|
|
1805 |
|
|
if (NULL != s && mpage == s->mpage) { |
1806 |
|
|
s->mask |= v; |
1807 |
|
|
return; |
1808 |
|
|
} else if (NULL == s) { |
1809 |
|
|
s = mandoc_calloc(1, sizeof(struct str) + sz + 1); |
1810 |
|
|
memcpy(s->key, cp, sz); |
1811 |
|
|
ohash_insert(htab, slot, s); |
1812 |
|
|
} |
1813 |
|
|
s->mpage = mpage; |
1814 |
|
|
s->mask = v; |
1815 |
|
|
|
1816 |
|
|
if (mustfree) |
1817 |
|
|
free(cp); |
1818 |
|
|
} |
1819 |
|
|
|
1820 |
|
|
/* |
1821 |
|
|
* Take a Unicode codepoint and produce its UTF-8 encoding. |
1822 |
|
|
* This isn't the best way to do this, but it works. |
1823 |
|
|
* The magic numbers are from the UTF-8 packaging. |
1824 |
|
|
* They're not as scary as they seem: read the UTF-8 spec for details. |
1825 |
|
|
*/ |
1826 |
|
|
static size_t |
1827 |
|
|
utf8(unsigned int cp, char out[7]) |
1828 |
|
|
{ |
1829 |
|
|
size_t rc; |
1830 |
|
|
|
1831 |
|
|
rc = 0; |
1832 |
|
|
if (cp <= 0x0000007F) { |
1833 |
|
|
rc = 1; |
1834 |
|
|
out[0] = (char)cp; |
1835 |
|
|
} else if (cp <= 0x000007FF) { |
1836 |
|
|
rc = 2; |
1837 |
|
|
out[0] = (cp >> 6 & 31) | 192; |
1838 |
|
|
out[1] = (cp & 63) | 128; |
1839 |
|
|
} else if (cp <= 0x0000FFFF) { |
1840 |
|
|
rc = 3; |
1841 |
|
|
out[0] = (cp >> 12 & 15) | 224; |
1842 |
|
|
out[1] = (cp >> 6 & 63) | 128; |
1843 |
|
|
out[2] = (cp & 63) | 128; |
1844 |
|
|
} else if (cp <= 0x001FFFFF) { |
1845 |
|
|
rc = 4; |
1846 |
|
|
out[0] = (cp >> 18 & 7) | 240; |
1847 |
|
|
out[1] = (cp >> 12 & 63) | 128; |
1848 |
|
|
out[2] = (cp >> 6 & 63) | 128; |
1849 |
|
|
out[3] = (cp & 63) | 128; |
1850 |
|
|
} else if (cp <= 0x03FFFFFF) { |
1851 |
|
|
rc = 5; |
1852 |
|
|
out[0] = (cp >> 24 & 3) | 248; |
1853 |
|
|
out[1] = (cp >> 18 & 63) | 128; |
1854 |
|
|
out[2] = (cp >> 12 & 63) | 128; |
1855 |
|
|
out[3] = (cp >> 6 & 63) | 128; |
1856 |
|
|
out[4] = (cp & 63) | 128; |
1857 |
|
|
} else if (cp <= 0x7FFFFFFF) { |
1858 |
|
|
rc = 6; |
1859 |
|
|
out[0] = (cp >> 30 & 1) | 252; |
1860 |
|
|
out[1] = (cp >> 24 & 63) | 128; |
1861 |
|
|
out[2] = (cp >> 18 & 63) | 128; |
1862 |
|
|
out[3] = (cp >> 12 & 63) | 128; |
1863 |
|
|
out[4] = (cp >> 6 & 63) | 128; |
1864 |
|
|
out[5] = (cp & 63) | 128; |
1865 |
|
|
} else |
1866 |
|
|
return 0; |
1867 |
|
|
|
1868 |
|
|
out[rc] = '\0'; |
1869 |
|
|
return rc; |
1870 |
|
|
} |
1871 |
|
|
|
1872 |
|
|
/* |
1873 |
|
|
* If the string contains escape sequences, |
1874 |
|
|
* replace it with an allocated rendering and return 1, |
1875 |
|
|
* such that the caller can free it after use. |
1876 |
|
|
* Otherwise, do nothing and return 0. |
1877 |
|
|
*/ |
1878 |
|
|
static int |
1879 |
|
|
render_string(char **public, size_t *psz) |
1880 |
|
|
{ |
1881 |
|
|
const char *src, *scp, *addcp, *seq; |
1882 |
|
|
char *dst; |
1883 |
|
|
size_t ssz, dsz, addsz; |
1884 |
|
|
char utfbuf[7], res[6]; |
1885 |
|
|
int seqlen, unicode; |
1886 |
|
|
|
1887 |
|
|
res[0] = '\\'; |
1888 |
|
|
res[1] = '\t'; |
1889 |
|
|
res[2] = ASCII_NBRSP; |
1890 |
|
|
res[3] = ASCII_HYPH; |
1891 |
|
|
res[4] = ASCII_BREAK; |
1892 |
|
|
res[5] = '\0'; |
1893 |
|
|
|
1894 |
|
|
src = scp = *public; |
1895 |
|
|
ssz = *psz; |
1896 |
|
|
dst = NULL; |
1897 |
|
|
dsz = 0; |
1898 |
|
|
|
1899 |
|
|
while (scp < src + *psz) { |
1900 |
|
|
|
1901 |
|
|
/* Leave normal characters unchanged. */ |
1902 |
|
|
|
1903 |
|
|
if (strchr(res, *scp) == NULL) { |
1904 |
|
|
if (dst != NULL) |
1905 |
|
|
dst[dsz++] = *scp; |
1906 |
|
|
scp++; |
1907 |
|
|
continue; |
1908 |
|
|
} |
1909 |
|
|
|
1910 |
|
|
/* |
1911 |
|
|
* Found something that requires replacing, |
1912 |
|
|
* make sure we have a destination buffer. |
1913 |
|
|
*/ |
1914 |
|
|
|
1915 |
|
|
if (dst == NULL) { |
1916 |
|
|
dst = mandoc_malloc(ssz + 1); |
1917 |
|
|
dsz = scp - src; |
1918 |
|
|
memcpy(dst, src, dsz); |
1919 |
|
|
} |
1920 |
|
|
|
1921 |
|
|
/* Handle single-char special characters. */ |
1922 |
|
|
|
1923 |
|
|
switch (*scp) { |
1924 |
|
|
case '\\': |
1925 |
|
|
break; |
1926 |
|
|
case '\t': |
1927 |
|
|
case ASCII_NBRSP: |
1928 |
|
|
dst[dsz++] = ' '; |
1929 |
|
|
scp++; |
1930 |
|
|
continue; |
1931 |
|
|
case ASCII_HYPH: |
1932 |
|
|
dst[dsz++] = '-'; |
1933 |
|
|
/* FALLTHROUGH */ |
1934 |
|
|
case ASCII_BREAK: |
1935 |
|
|
scp++; |
1936 |
|
|
continue; |
1937 |
|
|
default: |
1938 |
|
|
abort(); |
1939 |
|
|
} |
1940 |
|
|
|
1941 |
|
|
/* |
1942 |
|
|
* Found an escape sequence. |
1943 |
|
|
* Read past the slash, then parse it. |
1944 |
|
|
* Ignore everything except characters. |
1945 |
|
|
*/ |
1946 |
|
|
|
1947 |
|
|
scp++; |
1948 |
|
|
if (mandoc_escape(&scp, &seq, &seqlen) != ESCAPE_SPECIAL) |
1949 |
|
|
continue; |
1950 |
|
|
|
1951 |
|
|
/* |
1952 |
|
|
* Render the special character |
1953 |
|
|
* as either UTF-8 or ASCII. |
1954 |
|
|
*/ |
1955 |
|
|
|
1956 |
|
|
if (write_utf8) { |
1957 |
|
|
unicode = mchars_spec2cp(seq, seqlen); |
1958 |
|
|
if (unicode <= 0) |
1959 |
|
|
continue; |
1960 |
|
|
addsz = utf8(unicode, utfbuf); |
1961 |
|
|
if (addsz == 0) |
1962 |
|
|
continue; |
1963 |
|
|
addcp = utfbuf; |
1964 |
|
|
} else { |
1965 |
|
|
addcp = mchars_spec2str(seq, seqlen, &addsz); |
1966 |
|
|
if (addcp == NULL) |
1967 |
|
|
continue; |
1968 |
|
|
if (*addcp == ASCII_NBRSP) { |
1969 |
|
|
addcp = " "; |
1970 |
|
|
addsz = 1; |
1971 |
|
|
} |
1972 |
|
|
} |
1973 |
|
|
|
1974 |
|
|
/* Copy the rendered glyph into the stream. */ |
1975 |
|
|
|
1976 |
|
|
ssz += addsz; |
1977 |
|
|
dst = mandoc_realloc(dst, ssz + 1); |
1978 |
|
|
memcpy(dst + dsz, addcp, addsz); |
1979 |
|
|
dsz += addsz; |
1980 |
|
|
} |
1981 |
|
|
if (dst != NULL) { |
1982 |
|
|
*public = dst; |
1983 |
|
|
*psz = dsz; |
1984 |
|
|
} |
1985 |
|
|
|
1986 |
|
|
/* Trim trailing whitespace and NUL-terminate. */ |
1987 |
|
|
|
1988 |
|
|
while (*psz > 0 && (*public)[*psz - 1] == ' ') |
1989 |
|
|
--*psz; |
1990 |
|
|
if (dst != NULL) { |
1991 |
|
|
(*public)[*psz] = '\0'; |
1992 |
|
|
return 1; |
1993 |
|
|
} else |
1994 |
|
|
return 0; |
1995 |
|
|
} |
1996 |
|
|
|
1997 |
|
|
static void |
1998 |
|
|
dbadd_mlink(const struct mlink *mlink) |
1999 |
|
|
{ |
2000 |
|
|
size_t i; |
2001 |
|
|
|
2002 |
|
|
i = 1; |
2003 |
|
|
SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->dsec); |
2004 |
|
|
SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->arch); |
2005 |
|
|
SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->name); |
2006 |
|
|
SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, mlink->mpage->pageid); |
2007 |
|
|
SQL_STEP(stmts[STMT_INSERT_LINK]); |
2008 |
|
|
sqlite3_reset(stmts[STMT_INSERT_LINK]); |
2009 |
|
|
} |
2010 |
|
|
|
2011 |
|
|
static void |
2012 |
|
|
dbadd_mlink_name(const struct mlink *mlink) |
2013 |
|
|
{ |
2014 |
|
|
uint64_t bits; |
2015 |
|
|
size_t i; |
2016 |
|
|
|
2017 |
|
|
dbadd_mlink(mlink); |
2018 |
|
|
|
2019 |
|
|
i = 1; |
2020 |
|
|
SQL_BIND_INT64(stmts[STMT_SELECT_NAME], i, mlink->mpage->pageid); |
2021 |
|
|
bits = NAME_FILE & NAME_MASK; |
2022 |
|
|
if (sqlite3_step(stmts[STMT_SELECT_NAME]) == SQLITE_ROW) { |
2023 |
|
|
bits |= sqlite3_column_int64(stmts[STMT_SELECT_NAME], 0); |
2024 |
|
|
sqlite3_reset(stmts[STMT_SELECT_NAME]); |
2025 |
|
|
} |
2026 |
|
|
|
2027 |
|
|
i = 1; |
2028 |
|
|
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, bits); |
2029 |
|
|
SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, mlink->name); |
2030 |
|
|
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mlink->mpage->pageid); |
2031 |
|
|
SQL_STEP(stmts[STMT_INSERT_NAME]); |
2032 |
|
|
sqlite3_reset(stmts[STMT_INSERT_NAME]); |
2033 |
|
|
} |
2034 |
|
|
|
2035 |
|
|
/* |
2036 |
|
|
* Flush the current page's terms (and their bits) into the database. |
2037 |
|
|
* Wrap the entire set of additions in a transaction to make sqlite be a |
2038 |
|
|
* little faster. |
2039 |
|
|
* Also, handle escape sequences at the last possible moment. |
2040 |
|
|
*/ |
2041 |
|
|
static void |
2042 |
|
|
dbadd(struct mpage *mpage) |
2043 |
|
|
{ |
2044 |
|
|
struct mlink *mlink; |
2045 |
|
|
struct str *key; |
2046 |
|
|
char *cp; |
2047 |
|
|
size_t i; |
2048 |
|
|
unsigned int slot; |
2049 |
|
|
int mustfree; |
2050 |
|
|
|
2051 |
|
|
mlink = mpage->mlinks; |
2052 |
|
|
|
2053 |
|
|
if (nodb) { |
2054 |
|
|
for (key = ohash_first(&names, &slot); NULL != key; |
2055 |
|
|
key = ohash_next(&names, &slot)) |
2056 |
|
|
free(key); |
2057 |
|
|
for (key = ohash_first(&strings, &slot); NULL != key; |
2058 |
|
|
key = ohash_next(&strings, &slot)) |
2059 |
|
|
free(key); |
2060 |
|
|
if (0 == debug) |
2061 |
|
|
return; |
2062 |
|
|
while (NULL != mlink) { |
2063 |
|
|
fputs(mlink->name, stdout); |
2064 |
|
|
if (NULL == mlink->next || |
2065 |
|
|
strcmp(mlink->dsec, mlink->next->dsec) || |
2066 |
|
|
strcmp(mlink->fsec, mlink->next->fsec) || |
2067 |
|
|
strcmp(mlink->arch, mlink->next->arch)) { |
2068 |
|
|
putchar('('); |
2069 |
|
|
if ('\0' == *mlink->dsec) |
2070 |
|
|
fputs(mlink->fsec, stdout); |
2071 |
|
|
else |
2072 |
|
|
fputs(mlink->dsec, stdout); |
2073 |
|
|
if ('\0' != *mlink->arch) |
2074 |
|
|
printf("/%s", mlink->arch); |
2075 |
|
|
putchar(')'); |
2076 |
|
|
} |
2077 |
|
|
mlink = mlink->next; |
2078 |
|
|
if (NULL != mlink) |
2079 |
|
|
fputs(", ", stdout); |
2080 |
|
|
} |
2081 |
|
|
printf(" - %s\n", mpage->desc); |
2082 |
|
|
return; |
2083 |
|
|
} |
2084 |
|
|
|
2085 |
|
|
if (debug) |
2086 |
|
|
say(mlink->file, "Adding to database"); |
2087 |
|
|
|
2088 |
|
|
cp = mpage->desc; |
2089 |
|
|
i = strlen(cp); |
2090 |
|
|
mustfree = render_string(&cp, &i); |
2091 |
|
|
i = 1; |
2092 |
|
|
SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, cp); |
2093 |
|
|
SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, mpage->form); |
2094 |
|
|
SQL_STEP(stmts[STMT_INSERT_PAGE]); |
2095 |
|
|
mpage->pageid = sqlite3_last_insert_rowid(db); |
2096 |
|
|
sqlite3_reset(stmts[STMT_INSERT_PAGE]); |
2097 |
|
|
if (mustfree) |
2098 |
|
|
free(cp); |
2099 |
|
|
|
2100 |
|
|
while (NULL != mlink) { |
2101 |
|
|
dbadd_mlink(mlink); |
2102 |
|
|
mlink = mlink->next; |
2103 |
|
|
} |
2104 |
|
|
mlink = mpage->mlinks; |
2105 |
|
|
|
2106 |
|
|
for (key = ohash_first(&names, &slot); NULL != key; |
2107 |
|
|
key = ohash_next(&names, &slot)) { |
2108 |
|
|
assert(key->mpage == mpage); |
2109 |
|
|
i = 1; |
2110 |
|
|
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, key->mask); |
2111 |
|
|
SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, key->key); |
2112 |
|
|
SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mpage->pageid); |
2113 |
|
|
SQL_STEP(stmts[STMT_INSERT_NAME]); |
2114 |
|
|
sqlite3_reset(stmts[STMT_INSERT_NAME]); |
2115 |
|
|
free(key); |
2116 |
|
|
} |
2117 |
|
|
for (key = ohash_first(&strings, &slot); NULL != key; |
2118 |
|
|
key = ohash_next(&strings, &slot)) { |
2119 |
|
|
assert(key->mpage == mpage); |
2120 |
|
|
i = 1; |
2121 |
|
|
SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask); |
2122 |
|
|
SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->key); |
2123 |
|
|
SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, mpage->pageid); |
2124 |
|
|
SQL_STEP(stmts[STMT_INSERT_KEY]); |
2125 |
|
|
sqlite3_reset(stmts[STMT_INSERT_KEY]); |
2126 |
|
|
free(key); |
2127 |
|
|
} |
2128 |
|
|
} |
2129 |
|
|
|
2130 |
|
|
static void |
2131 |
|
|
dbprune(void) |
2132 |
|
|
{ |
2133 |
|
|
struct mpage *mpage; |
2134 |
|
|
struct mlink *mlink; |
2135 |
|
|
size_t i; |
2136 |
|
|
unsigned int slot; |
2137 |
|
|
|
2138 |
|
|
if (0 == nodb) |
2139 |
|
|
SQL_EXEC("BEGIN TRANSACTION"); |
2140 |
|
|
|
2141 |
|
|
for (mpage = ohash_first(&mpages, &slot); NULL != mpage; |
2142 |
|
|
mpage = ohash_next(&mpages, &slot)) { |
2143 |
|
|
mlink = mpage->mlinks; |
2144 |
|
|
if (debug) |
2145 |
|
|
say(mlink->file, "Deleting from database"); |
2146 |
|
|
if (nodb) |
2147 |
|
|
continue; |
2148 |
|
|
for ( ; NULL != mlink; mlink = mlink->next) { |
2149 |
|
|
i = 1; |
2150 |
|
|
SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE], |
2151 |
|
|
i, mlink->dsec); |
2152 |
|
|
SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE], |
2153 |
|
|
i, mlink->arch); |
2154 |
|
|
SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE], |
2155 |
|
|
i, mlink->name); |
2156 |
|
|
SQL_STEP(stmts[STMT_DELETE_PAGE]); |
2157 |
|
|
sqlite3_reset(stmts[STMT_DELETE_PAGE]); |
2158 |
|
|
} |
2159 |
|
|
} |
2160 |
|
|
|
2161 |
|
|
if (0 == nodb) |
2162 |
|
|
SQL_EXEC("END TRANSACTION"); |
2163 |
|
|
} |
2164 |
|
|
|
2165 |
|
|
/* |
2166 |
|
|
* Close an existing database and its prepared statements. |
2167 |
|
|
* If "real" is not set, rename the temporary file into the real one. |
2168 |
|
|
*/ |
2169 |
|
|
static void |
2170 |
|
|
dbclose(int real) |
2171 |
|
|
{ |
2172 |
|
|
size_t i; |
2173 |
|
|
int status; |
2174 |
|
|
pid_t child; |
2175 |
|
|
|
2176 |
|
|
if (nodb) |
2177 |
|
|
return; |
2178 |
|
|
|
2179 |
|
|
for (i = 0; i < STMT__MAX; i++) { |
2180 |
|
|
sqlite3_finalize(stmts[i]); |
2181 |
|
|
stmts[i] = NULL; |
2182 |
|
|
} |
2183 |
|
|
|
2184 |
|
|
sqlite3_close(db); |
2185 |
|
|
db = NULL; |
2186 |
|
|
|
2187 |
|
|
if (real) |
2188 |
|
|
return; |
2189 |
|
|
|
2190 |
|
|
if ('\0' == *tempfilename) { |
2191 |
|
|
if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) { |
2192 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2193 |
|
|
say(MANDOC_DB, "&rename"); |
2194 |
|
|
} |
2195 |
|
|
return; |
2196 |
|
|
} |
2197 |
|
|
|
2198 |
|
|
switch (child = fork()) { |
2199 |
|
|
case -1: |
2200 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2201 |
|
|
say("", "&fork cmp"); |
2202 |
|
|
return; |
2203 |
|
|
case 0: |
2204 |
|
|
execlp("cmp", "cmp", "-s", |
2205 |
|
|
tempfilename, MANDOC_DB, (char *)NULL); |
2206 |
|
|
say("", "&exec cmp"); |
2207 |
|
|
exit(0); |
2208 |
|
|
default: |
2209 |
|
|
break; |
2210 |
|
|
} |
2211 |
|
|
if (-1 == waitpid(child, &status, 0)) { |
2212 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2213 |
|
|
say("", "&wait cmp"); |
2214 |
|
|
} else if (WIFSIGNALED(status)) { |
2215 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2216 |
|
|
say("", "cmp died from signal %d", WTERMSIG(status)); |
2217 |
|
|
} else if (WEXITSTATUS(status)) { |
2218 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2219 |
|
|
say(MANDOC_DB, |
2220 |
|
|
"Data changed, but cannot replace database"); |
2221 |
|
|
} |
2222 |
|
|
|
2223 |
|
|
*strrchr(tempfilename, '/') = '\0'; |
2224 |
|
|
switch (child = fork()) { |
2225 |
|
|
case -1: |
2226 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2227 |
|
|
say("", "&fork rm"); |
2228 |
|
|
return; |
2229 |
|
|
case 0: |
2230 |
|
|
execlp("rm", "rm", "-rf", tempfilename, (char *)NULL); |
2231 |
|
|
say("", "&exec rm"); |
2232 |
|
|
exit((int)MANDOCLEVEL_SYSERR); |
2233 |
|
|
default: |
2234 |
|
|
break; |
2235 |
|
|
} |
2236 |
|
|
if (-1 == waitpid(child, &status, 0)) { |
2237 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2238 |
|
|
say("", "&wait rm"); |
2239 |
|
|
} else if (WIFSIGNALED(status) || WEXITSTATUS(status)) { |
2240 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2241 |
|
|
say("", "%s: Cannot remove temporary directory", |
2242 |
|
|
tempfilename); |
2243 |
|
|
} |
2244 |
|
|
} |
2245 |
|
|
|
2246 |
|
|
/* |
2247 |
|
|
* This is straightforward stuff. |
2248 |
|
|
* Open a database connection to a "temporary" database, then open a set |
2249 |
|
|
* of prepared statements we'll use over and over again. |
2250 |
|
|
* If "real" is set, we use the existing database; if not, we truncate a |
2251 |
|
|
* temporary one. |
2252 |
|
|
* Must be matched by dbclose(). |
2253 |
|
|
*/ |
2254 |
|
|
static int |
2255 |
|
|
dbopen(int real) |
2256 |
|
|
{ |
2257 |
|
|
const char *sql; |
2258 |
|
|
int rc, ofl; |
2259 |
|
|
|
2260 |
|
|
if (nodb) |
2261 |
|
|
return 1; |
2262 |
|
|
|
2263 |
|
|
*tempfilename = '\0'; |
2264 |
|
|
ofl = SQLITE_OPEN_READWRITE; |
2265 |
|
|
|
2266 |
|
|
if (real) { |
2267 |
|
|
rc = sqlite3_open_v2(MANDOC_DB, &db, ofl, NULL); |
2268 |
|
|
if (SQLITE_OK != rc) { |
2269 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2270 |
|
|
if (SQLITE_CANTOPEN != rc) |
2271 |
|
|
say(MANDOC_DB, "%s", sqlite3_errstr(rc)); |
2272 |
|
|
return 0; |
2273 |
|
|
} |
2274 |
|
|
goto prepare_statements; |
2275 |
|
|
} |
2276 |
|
|
|
2277 |
|
|
ofl |= SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE; |
2278 |
|
|
|
2279 |
|
|
remove(MANDOC_DB "~"); |
2280 |
|
|
rc = sqlite3_open_v2(MANDOC_DB "~", &db, ofl, NULL); |
2281 |
|
|
if (SQLITE_OK == rc) |
2282 |
|
|
goto create_tables; |
2283 |
|
|
if (MPARSE_QUICK & mparse_options) { |
2284 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2285 |
|
|
say(MANDOC_DB "~", "%s", sqlite3_errstr(rc)); |
2286 |
|
|
return 0; |
2287 |
|
|
} |
2288 |
|
|
|
2289 |
|
|
(void)strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX", |
2290 |
|
|
sizeof(tempfilename)); |
2291 |
|
|
if (NULL == mkdtemp(tempfilename)) { |
2292 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2293 |
|
|
say("", "&%s", tempfilename); |
2294 |
|
|
return 0; |
2295 |
|
|
} |
2296 |
|
|
(void)strlcat(tempfilename, "/" MANDOC_DB, |
2297 |
|
|
sizeof(tempfilename)); |
2298 |
|
|
rc = sqlite3_open_v2(tempfilename, &db, ofl, NULL); |
2299 |
|
|
if (SQLITE_OK != rc) { |
2300 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2301 |
|
|
say("", "%s: %s", tempfilename, sqlite3_errstr(rc)); |
2302 |
|
|
return 0; |
2303 |
|
|
} |
2304 |
|
|
|
2305 |
|
|
create_tables: |
2306 |
|
|
sql = "CREATE TABLE \"mpages\" (\n" |
2307 |
|
|
" \"desc\" TEXT NOT NULL,\n" |
2308 |
|
|
" \"form\" INTEGER NOT NULL,\n" |
2309 |
|
|
" \"pageid\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n" |
2310 |
|
|
");\n" |
2311 |
|
|
"\n" |
2312 |
|
|
"CREATE TABLE \"mlinks\" (\n" |
2313 |
|
|
" \"sec\" TEXT NOT NULL,\n" |
2314 |
|
|
" \"arch\" TEXT NOT NULL,\n" |
2315 |
|
|
" \"name\" TEXT NOT NULL,\n" |
2316 |
|
|
" \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) " |
2317 |
|
|
"ON DELETE CASCADE\n" |
2318 |
|
|
");\n" |
2319 |
|
|
"CREATE INDEX mlinks_pageid_idx ON mlinks (pageid);\n" |
2320 |
|
|
"\n" |
2321 |
|
|
"CREATE TABLE \"names\" (\n" |
2322 |
|
|
" \"bits\" INTEGER NOT NULL,\n" |
2323 |
|
|
" \"name\" TEXT NOT NULL,\n" |
2324 |
|
|
" \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) " |
2325 |
|
|
"ON DELETE CASCADE,\n" |
2326 |
|
|
" UNIQUE (\"name\", \"pageid\") ON CONFLICT REPLACE\n" |
2327 |
|
|
");\n" |
2328 |
|
|
"\n" |
2329 |
|
|
"CREATE TABLE \"keys\" (\n" |
2330 |
|
|
" \"bits\" INTEGER NOT NULL,\n" |
2331 |
|
|
" \"key\" TEXT NOT NULL,\n" |
2332 |
|
|
" \"pageid\" INTEGER NOT NULL REFERENCES mpages(pageid) " |
2333 |
|
|
"ON DELETE CASCADE\n" |
2334 |
|
|
");\n" |
2335 |
|
|
"CREATE INDEX keys_pageid_idx ON keys (pageid);\n"; |
2336 |
|
|
|
2337 |
|
|
if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) { |
2338 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2339 |
|
|
say(MANDOC_DB, "%s", sqlite3_errmsg(db)); |
2340 |
|
|
sqlite3_close(db); |
2341 |
|
|
return 0; |
2342 |
|
|
} |
2343 |
|
|
|
2344 |
|
|
prepare_statements: |
2345 |
|
|
if (SQLITE_OK != sqlite3_exec(db, |
2346 |
|
|
"PRAGMA foreign_keys = ON", NULL, NULL, NULL)) { |
2347 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2348 |
|
|
say(MANDOC_DB, "PRAGMA foreign_keys: %s", |
2349 |
|
|
sqlite3_errmsg(db)); |
2350 |
|
|
sqlite3_close(db); |
2351 |
|
|
return 0; |
2352 |
|
|
} |
2353 |
|
|
|
2354 |
|
|
sql = "DELETE FROM mpages WHERE pageid IN " |
2355 |
|
|
"(SELECT pageid FROM mlinks WHERE " |
2356 |
|
|
"sec=? AND arch=? AND name=?)"; |
2357 |
|
|
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL); |
2358 |
|
|
sql = "INSERT INTO mpages " |
2359 |
|
|
"(desc,form) VALUES (?,?)"; |
2360 |
|
|
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL); |
2361 |
|
|
sql = "INSERT INTO mlinks " |
2362 |
|
|
"(sec,arch,name,pageid) VALUES (?,?,?,?)"; |
2363 |
|
|
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL); |
2364 |
|
|
sql = "SELECT bits FROM names where pageid = ?"; |
2365 |
|
|
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_SELECT_NAME], NULL); |
2366 |
|
|
sql = "INSERT INTO names " |
2367 |
|
|
"(bits,name,pageid) VALUES (?,?,?)"; |
2368 |
|
|
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_NAME], NULL); |
2369 |
|
|
sql = "INSERT INTO keys " |
2370 |
|
|
"(bits,key,pageid) VALUES (?,?,?)"; |
2371 |
|
|
sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL); |
2372 |
|
|
|
2373 |
|
|
/* |
2374 |
|
|
* When opening a new database, we can turn off |
2375 |
|
|
* synchronous mode for much better performance. |
2376 |
|
|
*/ |
2377 |
|
|
|
2378 |
|
|
if (real && SQLITE_OK != sqlite3_exec(db, |
2379 |
|
|
"PRAGMA synchronous = OFF", NULL, NULL, NULL)) { |
2380 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2381 |
|
|
say(MANDOC_DB, "PRAGMA synchronous: %s", |
2382 |
|
|
sqlite3_errmsg(db)); |
2383 |
|
|
sqlite3_close(db); |
2384 |
|
|
return 0; |
2385 |
|
|
} |
2386 |
|
|
|
2387 |
|
|
return 1; |
2388 |
|
|
} |
2389 |
|
|
|
2390 |
|
|
static int |
2391 |
|
|
set_basedir(const char *targetdir, int report_baddir) |
2392 |
|
|
{ |
2393 |
|
|
static char startdir[PATH_MAX]; |
2394 |
|
|
static int getcwd_status; /* 1 = ok, 2 = failure */ |
2395 |
|
|
static int chdir_status; /* 1 = changed directory */ |
2396 |
|
|
char *cp; |
2397 |
|
|
|
2398 |
|
|
/* |
2399 |
|
|
* Remember the original working directory, if possible. |
2400 |
|
|
* This will be needed if the second or a later directory |
2401 |
|
|
* on the command line is given as a relative path. |
2402 |
|
|
* Do not error out if the current directory is not |
2403 |
|
|
* searchable: Maybe it won't be needed after all. |
2404 |
|
|
*/ |
2405 |
|
|
if (0 == getcwd_status) { |
2406 |
|
|
if (NULL == getcwd(startdir, sizeof(startdir))) { |
2407 |
|
|
getcwd_status = 2; |
2408 |
|
|
(void)strlcpy(startdir, strerror(errno), |
2409 |
|
|
sizeof(startdir)); |
2410 |
|
|
} else |
2411 |
|
|
getcwd_status = 1; |
2412 |
|
|
} |
2413 |
|
|
|
2414 |
|
|
/* |
2415 |
|
|
* We are leaving the old base directory. |
2416 |
|
|
* Do not use it any longer, not even for messages. |
2417 |
|
|
*/ |
2418 |
|
|
*basedir = '\0'; |
2419 |
|
|
|
2420 |
|
|
/* |
2421 |
|
|
* If and only if the directory was changed earlier and |
2422 |
|
|
* the next directory to process is given as a relative path, |
2423 |
|
|
* first go back, or bail out if that is impossible. |
2424 |
|
|
*/ |
2425 |
|
|
if (chdir_status && '/' != *targetdir) { |
2426 |
|
|
if (2 == getcwd_status) { |
2427 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2428 |
|
|
say("", "getcwd: %s", startdir); |
2429 |
|
|
return 0; |
2430 |
|
|
} |
2431 |
|
|
if (-1 == chdir(startdir)) { |
2432 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2433 |
|
|
say("", "&chdir %s", startdir); |
2434 |
|
|
return 0; |
2435 |
|
|
} |
2436 |
|
|
} |
2437 |
|
|
|
2438 |
|
|
/* |
2439 |
|
|
* Always resolve basedir to the canonicalized absolute |
2440 |
|
|
* pathname and append a trailing slash, such that |
2441 |
|
|
* we can reliably check whether files are inside. |
2442 |
|
|
*/ |
2443 |
|
|
if (NULL == realpath(targetdir, basedir)) { |
2444 |
|
|
if (report_baddir || errno != ENOENT) { |
2445 |
|
|
exitcode = (int)MANDOCLEVEL_BADARG; |
2446 |
|
|
say("", "&%s: realpath", targetdir); |
2447 |
|
|
} |
2448 |
|
|
return 0; |
2449 |
|
|
} else if (-1 == chdir(basedir)) { |
2450 |
|
|
if (report_baddir || errno != ENOENT) { |
2451 |
|
|
exitcode = (int)MANDOCLEVEL_BADARG; |
2452 |
|
|
say("", "&chdir"); |
2453 |
|
|
} |
2454 |
|
|
return 0; |
2455 |
|
|
} |
2456 |
|
|
chdir_status = 1; |
2457 |
|
|
cp = strchr(basedir, '\0'); |
2458 |
|
|
if ('/' != cp[-1]) { |
2459 |
|
|
if (cp - basedir >= PATH_MAX - 1) { |
2460 |
|
|
exitcode = (int)MANDOCLEVEL_SYSERR; |
2461 |
|
|
say("", "Filename too long"); |
2462 |
|
|
return 0; |
2463 |
|
|
} |
2464 |
|
|
*cp++ = '/'; |
2465 |
|
|
*cp = '\0'; |
2466 |
|
|
} |
2467 |
|
|
return 1; |
2468 |
|
|
} |
2469 |
|
|
|
2470 |
|
|
static void |
2471 |
|
|
say(const char *file, const char *format, ...) |
2472 |
|
|
{ |
2473 |
|
|
va_list ap; |
2474 |
|
|
int use_errno; |
2475 |
|
|
|
2476 |
|
|
if ('\0' != *basedir) |
2477 |
|
|
fprintf(stderr, "%s", basedir); |
2478 |
|
|
if ('\0' != *basedir && '\0' != *file) |
2479 |
|
|
fputc('/', stderr); |
2480 |
|
|
if ('\0' != *file) |
2481 |
|
|
fprintf(stderr, "%s", file); |
2482 |
|
|
|
2483 |
|
|
use_errno = 1; |
2484 |
|
|
if (NULL != format) { |
2485 |
|
|
switch (*format) { |
2486 |
|
|
case '&': |
2487 |
|
|
format++; |
2488 |
|
|
break; |
2489 |
|
|
case '\0': |
2490 |
|
|
format = NULL; |
2491 |
|
|
break; |
2492 |
|
|
default: |
2493 |
|
|
use_errno = 0; |
2494 |
|
|
break; |
2495 |
|
|
} |
2496 |
|
|
} |
2497 |
|
|
if (NULL != format) { |
2498 |
|
|
if ('\0' != *basedir || '\0' != *file) |
2499 |
|
|
fputs(": ", stderr); |
2500 |
|
|
va_start(ap, format); |
2501 |
|
|
vfprintf(stderr, format, ap); |
2502 |
|
|
va_end(ap); |
2503 |
|
|
} |
2504 |
|
|
if (use_errno) { |
2505 |
|
|
if ('\0' != *basedir || '\0' != *file || NULL != format) |
2506 |
|
|
fputs(": ", stderr); |
2507 |
|
|
perror(NULL); |
2508 |
|
|
} else |
2509 |
|
|
fputc('\n', stderr); |
2510 |
|
|
} |