]> git.cameronkatri.com Git - mandoc.git/blob - mandocdb.c
d6b160d50867825f9c4983d2b094aa8a3bbea3fd
[mandoc.git] / mandocdb.c
1 /* $Id: mandocdb.c,v 1.45 2012/03/23 05:45:45 kristaps Exp $ */
2 /*
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <sys/param.h>
23 #include <sys/types.h>
24
25 #include <assert.h>
26 #include <ctype.h>
27 #include <dirent.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <getopt.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #if defined(__linux__)
38 # include <endian.h>
39 # include <db_185.h>
40 #elif defined(__APPLE__)
41 # include <libkern/OSByteOrder.h>
42 # include <db.h>
43 #else
44 # include <db.h>
45 #endif
46
47 #include "man.h"
48 #include "mdoc.h"
49 #include "mandoc.h"
50 #include "mandocdb.h"
51 #include "manpath.h"
52
53 #define MANDOC_BUFSZ BUFSIZ
54 #define MANDOC_SLOP 1024
55
56 #define MANDOC_SRC 0x1
57 #define MANDOC_FORM 0x2
58
59 #define WARNING(_f, _b, _fmt, _args...) \
60 do if (warnings) { \
61 fprintf(stderr, "%s: ", (_b)); \
62 fprintf(stderr, (_fmt), ##_args); \
63 if ('\0' != *(_f)) \
64 fprintf(stderr, ": %s", (_f)); \
65 fprintf(stderr, "\n"); \
66 } while (/* CONSTCOND */ 0)
67
68 /* Access to the mandoc database on disk. */
69
70 struct mdb {
71 char idxn[MAXPATHLEN]; /* index db filename */
72 char dbn[MAXPATHLEN]; /* keyword db filename */
73 DB *idx; /* index recno database */
74 DB *db; /* keyword btree database */
75 };
76
77 /* Stack of temporarily unused index records. */
78
79 struct recs {
80 recno_t *stack; /* pointer to a malloc'ed array */
81 size_t size; /* number of allocated slots */
82 size_t cur; /* current number of empty records */
83 recno_t last; /* last record number in the index */
84 };
85
86 /* Tiny list for files. No need to bring in QUEUE. */
87
88 struct of {
89 char *fname; /* heap-allocated */
90 char *sec;
91 char *arch;
92 char *title;
93 int src_form;
94 struct of *next; /* NULL for last one */
95 struct of *first; /* first in list */
96 };
97
98 /* Buffer for storing growable data. */
99
100 struct buf {
101 char *cp;
102 size_t len; /* current length */
103 size_t size; /* total buffer size */
104 };
105
106 /* Operation we're going to perform. */
107
108 enum op {
109 OP_DEFAULT = 0, /* new dbs from dir list or default config */
110 OP_CONFFILE, /* new databases from custom config file */
111 OP_UPDATE, /* delete/add entries in existing database */
112 OP_DELETE, /* delete entries from existing database */
113 OP_TEST /* change no databases, report potential problems */
114 };
115
116 #define MAN_ARGS DB *hash, \
117 struct buf *buf, \
118 struct buf *dbuf, \
119 const struct man_node *n
120 #define MDOC_ARGS DB *hash, \
121 struct buf *buf, \
122 struct buf *dbuf, \
123 const struct mdoc_node *n, \
124 const struct mdoc_meta *m
125
126 static void buf_appendmdoc(struct buf *,
127 const struct mdoc_node *, int);
128 static void buf_append(struct buf *, const char *);
129 static void buf_appendb(struct buf *,
130 const void *, size_t);
131 static void dbt_put(DB *, const char *, DBT *, DBT *);
132 static void hash_put(DB *, const struct buf *, uint64_t);
133 static void hash_reset(DB **);
134 static void index_merge(const struct of *, struct mparse *,
135 struct buf *, struct buf *, DB *,
136 struct mdb *, struct recs *,
137 const char *);
138 static void index_prune(const struct of *, struct mdb *,
139 struct recs *, const char *);
140 static void ofile_argbuild(int, char *[],
141 struct of **, const char *);
142 static void ofile_dirbuild(const char *, const char *,
143 const char *, int, struct of **, char *);
144 static void ofile_free(struct of *);
145 static void pformatted(DB *, struct buf *, struct buf *,
146 const struct of *, const char *);
147 static int pman_node(MAN_ARGS);
148 static void pmdoc_node(MDOC_ARGS);
149 static int pmdoc_head(MDOC_ARGS);
150 static int pmdoc_body(MDOC_ARGS);
151 static int pmdoc_Fd(MDOC_ARGS);
152 static int pmdoc_In(MDOC_ARGS);
153 static int pmdoc_Fn(MDOC_ARGS);
154 static int pmdoc_Nd(MDOC_ARGS);
155 static int pmdoc_Nm(MDOC_ARGS);
156 static int pmdoc_Sh(MDOC_ARGS);
157 static int pmdoc_St(MDOC_ARGS);
158 static int pmdoc_Xr(MDOC_ARGS);
159
160 #define MDOCF_CHILD 0x01 /* Automatically index child nodes. */
161
162 struct mdoc_handler {
163 int (*fp)(MDOC_ARGS); /* Optional handler. */
164 uint64_t mask; /* Set unless handler returns 0. */
165 int flags; /* For use by pmdoc_node. */
166 };
167
168 static const struct mdoc_handler mdocs[MDOC_MAX] = {
169 { NULL, 0, 0 }, /* Ap */
170 { NULL, 0, 0 }, /* Dd */
171 { NULL, 0, 0 }, /* Dt */
172 { NULL, 0, 0 }, /* Os */
173 { pmdoc_Sh, TYPE_Sh, MDOCF_CHILD }, /* Sh */
174 { pmdoc_head, TYPE_Ss, MDOCF_CHILD }, /* Ss */
175 { NULL, 0, 0 }, /* Pp */
176 { NULL, 0, 0 }, /* D1 */
177 { NULL, 0, 0 }, /* Dl */
178 { NULL, 0, 0 }, /* Bd */
179 { NULL, 0, 0 }, /* Ed */
180 { NULL, 0, 0 }, /* Bl */
181 { NULL, 0, 0 }, /* El */
182 { NULL, 0, 0 }, /* It */
183 { NULL, 0, 0 }, /* Ad */
184 { NULL, TYPE_An, MDOCF_CHILD }, /* An */
185 { NULL, TYPE_Ar, MDOCF_CHILD }, /* Ar */
186 { NULL, TYPE_Cd, MDOCF_CHILD }, /* Cd */
187 { NULL, TYPE_Cm, MDOCF_CHILD }, /* Cm */
188 { NULL, TYPE_Dv, MDOCF_CHILD }, /* Dv */
189 { NULL, TYPE_Er, MDOCF_CHILD }, /* Er */
190 { NULL, TYPE_Ev, MDOCF_CHILD }, /* Ev */
191 { NULL, 0, 0 }, /* Ex */
192 { NULL, TYPE_Fa, MDOCF_CHILD }, /* Fa */
193 { pmdoc_Fd, TYPE_In, 0 }, /* Fd */
194 { NULL, TYPE_Fl, MDOCF_CHILD }, /* Fl */
195 { pmdoc_Fn, 0, 0 }, /* Fn */
196 { NULL, TYPE_Ft, MDOCF_CHILD }, /* Ft */
197 { NULL, TYPE_Ic, MDOCF_CHILD }, /* Ic */
198 { pmdoc_In, TYPE_In, 0 }, /* In */
199 { NULL, TYPE_Li, MDOCF_CHILD }, /* Li */
200 { pmdoc_Nd, TYPE_Nd, MDOCF_CHILD }, /* Nd */
201 { pmdoc_Nm, TYPE_Nm, MDOCF_CHILD }, /* Nm */
202 { NULL, 0, 0 }, /* Op */
203 { NULL, 0, 0 }, /* Ot */
204 { NULL, TYPE_Pa, MDOCF_CHILD }, /* Pa */
205 { NULL, 0, 0 }, /* Rv */
206 { pmdoc_St, TYPE_St, 0 }, /* St */
207 { NULL, TYPE_Va, MDOCF_CHILD }, /* Va */
208 { pmdoc_body, TYPE_Va, MDOCF_CHILD }, /* Vt */
209 { pmdoc_Xr, TYPE_Xr, 0 }, /* Xr */
210 { NULL, 0, 0 }, /* %A */
211 { NULL, 0, 0 }, /* %B */
212 { NULL, 0, 0 }, /* %D */
213 { NULL, 0, 0 }, /* %I */
214 { NULL, 0, 0 }, /* %J */
215 { NULL, 0, 0 }, /* %N */
216 { NULL, 0, 0 }, /* %O */
217 { NULL, 0, 0 }, /* %P */
218 { NULL, 0, 0 }, /* %R */
219 { NULL, 0, 0 }, /* %T */
220 { NULL, 0, 0 }, /* %V */
221 { NULL, 0, 0 }, /* Ac */
222 { NULL, 0, 0 }, /* Ao */
223 { NULL, 0, 0 }, /* Aq */
224 { NULL, TYPE_At, MDOCF_CHILD }, /* At */
225 { NULL, 0, 0 }, /* Bc */
226 { NULL, 0, 0 }, /* Bf */
227 { NULL, 0, 0 }, /* Bo */
228 { NULL, 0, 0 }, /* Bq */
229 { NULL, TYPE_Bsx, MDOCF_CHILD }, /* Bsx */
230 { NULL, TYPE_Bx, MDOCF_CHILD }, /* Bx */
231 { NULL, 0, 0 }, /* Db */
232 { NULL, 0, 0 }, /* Dc */
233 { NULL, 0, 0 }, /* Do */
234 { NULL, 0, 0 }, /* Dq */
235 { NULL, 0, 0 }, /* Ec */
236 { NULL, 0, 0 }, /* Ef */
237 { NULL, TYPE_Em, MDOCF_CHILD }, /* Em */
238 { NULL, 0, 0 }, /* Eo */
239 { NULL, TYPE_Fx, MDOCF_CHILD }, /* Fx */
240 { NULL, TYPE_Ms, MDOCF_CHILD }, /* Ms */
241 { NULL, 0, 0 }, /* No */
242 { NULL, 0, 0 }, /* Ns */
243 { NULL, TYPE_Nx, MDOCF_CHILD }, /* Nx */
244 { NULL, TYPE_Ox, MDOCF_CHILD }, /* Ox */
245 { NULL, 0, 0 }, /* Pc */
246 { NULL, 0, 0 }, /* Pf */
247 { NULL, 0, 0 }, /* Po */
248 { NULL, 0, 0 }, /* Pq */
249 { NULL, 0, 0 }, /* Qc */
250 { NULL, 0, 0 }, /* Ql */
251 { NULL, 0, 0 }, /* Qo */
252 { NULL, 0, 0 }, /* Qq */
253 { NULL, 0, 0 }, /* Re */
254 { NULL, 0, 0 }, /* Rs */
255 { NULL, 0, 0 }, /* Sc */
256 { NULL, 0, 0 }, /* So */
257 { NULL, 0, 0 }, /* Sq */
258 { NULL, 0, 0 }, /* Sm */
259 { NULL, 0, 0 }, /* Sx */
260 { NULL, TYPE_Sy, MDOCF_CHILD }, /* Sy */
261 { NULL, TYPE_Tn, MDOCF_CHILD }, /* Tn */
262 { NULL, 0, 0 }, /* Ux */
263 { NULL, 0, 0 }, /* Xc */
264 { NULL, 0, 0 }, /* Xo */
265 { pmdoc_head, TYPE_Fn, 0 }, /* Fo */
266 { NULL, 0, 0 }, /* Fc */
267 { NULL, 0, 0 }, /* Oo */
268 { NULL, 0, 0 }, /* Oc */
269 { NULL, 0, 0 }, /* Bk */
270 { NULL, 0, 0 }, /* Ek */
271 { NULL, 0, 0 }, /* Bt */
272 { NULL, 0, 0 }, /* Hf */
273 { NULL, 0, 0 }, /* Fr */
274 { NULL, 0, 0 }, /* Ud */
275 { NULL, TYPE_Lb, MDOCF_CHILD }, /* Lb */
276 { NULL, 0, 0 }, /* Lp */
277 { NULL, TYPE_Lk, MDOCF_CHILD }, /* Lk */
278 { NULL, TYPE_Mt, MDOCF_CHILD }, /* Mt */
279 { NULL, 0, 0 }, /* Brq */
280 { NULL, 0, 0 }, /* Bro */
281 { NULL, 0, 0 }, /* Brc */
282 { NULL, 0, 0 }, /* %C */
283 { NULL, 0, 0 }, /* Es */
284 { NULL, 0, 0 }, /* En */
285 { NULL, TYPE_Dx, MDOCF_CHILD }, /* Dx */
286 { NULL, 0, 0 }, /* %Q */
287 { NULL, 0, 0 }, /* br */
288 { NULL, 0, 0 }, /* sp */
289 { NULL, 0, 0 }, /* %U */
290 { NULL, 0, 0 }, /* Ta */
291 };
292
293 static const char *progname;
294 static int use_all; /* Use all directories and files. */
295 static int verb; /* Output verbosity level. */
296 static int warnings; /* Potential problems in manuals. */
297
298 int
299 main(int argc, char *argv[])
300 {
301 struct mparse *mp; /* parse sequence */
302 struct manpaths dirs;
303 struct mdb mdb;
304 struct recs recs;
305 enum op op; /* current operation */
306 const char *dir;
307 int ch, i, flags;
308 char dirbuf[MAXPATHLEN];
309 DB *hash; /* temporary keyword hashtable */
310 BTREEINFO info; /* btree configuration */
311 size_t sz1, sz2;
312 struct buf buf, /* keyword buffer */
313 dbuf; /* description buffer */
314 struct of *of; /* list of files for processing */
315 extern int optind;
316 extern char *optarg;
317
318 progname = strrchr(argv[0], '/');
319 if (progname == NULL)
320 progname = argv[0];
321 else
322 ++progname;
323
324 memset(&dirs, 0, sizeof(struct manpaths));
325 memset(&mdb, 0, sizeof(struct mdb));
326 memset(&recs, 0, sizeof(struct recs));
327
328 of = NULL;
329 mp = NULL;
330 hash = NULL;
331 op = OP_DEFAULT;
332 dir = NULL;
333
334 while (-1 != (ch = getopt(argc, argv, "aC:d:tu:vW")))
335 switch (ch) {
336 case ('a'):
337 use_all = 1;
338 break;
339 case ('C'):
340 if (op) {
341 fprintf(stderr,
342 "-C: conflicting options\n");
343 goto usage;
344 }
345 dir = optarg;
346 op = OP_CONFFILE;
347 break;
348 case ('d'):
349 if (op) {
350 fprintf(stderr,
351 "-d: conflicting options\n");
352 goto usage;
353 }
354 dir = optarg;
355 op = OP_UPDATE;
356 break;
357 case ('t'):
358 dup2(STDOUT_FILENO, STDERR_FILENO);
359 if (op) {
360 fprintf(stderr,
361 "-t: conflicting options\n");
362 goto usage;
363 }
364 op = OP_TEST;
365 use_all = 1;
366 warnings = 1;
367 break;
368 case ('u'):
369 if (op) {
370 fprintf(stderr,
371 "-u: conflicting options\n");
372 goto usage;
373 }
374 dir = optarg;
375 op = OP_DELETE;
376 break;
377 case ('v'):
378 verb++;
379 break;
380 case ('W'):
381 warnings = 1;
382 break;
383 default:
384 goto usage;
385 }
386
387 argc -= optind;
388 argv += optind;
389
390 if (OP_CONFFILE == op && argc > 0) {
391 fprintf(stderr, "-C: too many arguments\n");
392 goto usage;
393 }
394
395 memset(&info, 0, sizeof(BTREEINFO));
396 info.lorder = 4321;
397 info.flags = R_DUP;
398
399 mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
400
401 memset(&buf, 0, sizeof(struct buf));
402 memset(&dbuf, 0, sizeof(struct buf));
403
404 buf.size = dbuf.size = MANDOC_BUFSZ;
405
406 buf.cp = mandoc_malloc(buf.size);
407 dbuf.cp = mandoc_malloc(dbuf.size);
408
409 if (OP_TEST == op) {
410 ofile_argbuild(argc, argv, &of, ".");
411 if (NULL == of)
412 goto out;
413 index_merge(of, mp, &dbuf, &buf,
414 hash, &mdb, &recs, ".");
415 goto out;
416 }
417
418 if (OP_UPDATE == op || OP_DELETE == op) {
419 strlcat(mdb.dbn, dir, MAXPATHLEN);
420 strlcat(mdb.dbn, "/", MAXPATHLEN);
421 sz1 = strlcat(mdb.dbn, MANDOC_DB, MAXPATHLEN);
422
423 strlcat(mdb.idxn, dir, MAXPATHLEN);
424 strlcat(mdb.idxn, "/", MAXPATHLEN);
425 sz2 = strlcat(mdb.idxn, MANDOC_IDX, MAXPATHLEN);
426
427 if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
428 fprintf(stderr, "%s: path too long\n", dir);
429 exit((int)MANDOCLEVEL_BADARG);
430 }
431
432 flags = O_CREAT | O_RDWR;
433 mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info);
434 mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL);
435
436 if (NULL == mdb.db) {
437 perror(mdb.dbn);
438 exit((int)MANDOCLEVEL_SYSERR);
439 } else if (NULL == mdb.idx) {
440 perror(mdb.idxn);
441 exit((int)MANDOCLEVEL_SYSERR);
442 }
443
444 ofile_argbuild(argc, argv, &of, dir);
445
446 if (NULL == of)
447 goto out;
448
449 index_prune(of, &mdb, &recs, dir);
450
451 /*
452 * Go to the root of the respective manual tree.
453 * This must work or no manuals may be found (they're
454 * indexed relative to the root).
455 */
456
457 if (OP_UPDATE == op) {
458 if (-1 == chdir(dir)) {
459 perror(dir);
460 exit((int)MANDOCLEVEL_SYSERR);
461 }
462 index_merge(of, mp, &dbuf, &buf, hash,
463 &mdb, &recs, dir);
464 }
465
466 goto out;
467 }
468
469 /*
470 * Configure the directories we're going to scan.
471 * If we have command-line arguments, use them.
472 * If not, we use man(1)'s method (see mandocdb.8).
473 */
474
475 if (argc > 0) {
476 dirs.paths = mandoc_calloc(argc, sizeof(char *));
477 dirs.sz = argc;
478 for (i = 0; i < argc; i++)
479 dirs.paths[i] = mandoc_strdup(argv[i]);
480 } else
481 manpath_parse(&dirs, dir, NULL, NULL);
482
483 for (i = 0; i < dirs.sz; i++) {
484 /*
485 * Go to the root of the respective manual tree.
486 * This must work or no manuals may be found:
487 * They are indexed relative to the root.
488 */
489
490 if (-1 == chdir(dirs.paths[i])) {
491 perror(dirs.paths[i]);
492 exit((int)MANDOCLEVEL_SYSERR);
493 }
494
495 strlcpy(mdb.dbn, MANDOC_DB, MAXPATHLEN);
496 strlcpy(mdb.idxn, MANDOC_IDX, MAXPATHLEN);
497
498 flags = O_CREAT | O_TRUNC | O_RDWR;
499 mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info);
500 mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL);
501
502 if (NULL == mdb.db) {
503 perror(mdb.dbn);
504 exit((int)MANDOCLEVEL_SYSERR);
505 } else if (NULL == mdb.idx) {
506 perror(mdb.idxn);
507 exit((int)MANDOCLEVEL_SYSERR);
508 }
509
510 /*
511 * Search for manuals and fill the new database.
512 */
513
514 strlcpy(dirbuf, dirs.paths[i], MAXPATHLEN);
515 ofile_dirbuild(".", "", "", 0, &of, dirbuf);
516
517 if (NULL != of) {
518 index_merge(of, mp, &dbuf, &buf, hash,
519 &mdb, &recs, dirs.paths[i]);
520 ofile_free(of);
521 of = NULL;
522 }
523
524 (*mdb.db->close)(mdb.db);
525 (*mdb.idx->close)(mdb.idx);
526 mdb.db = NULL;
527 mdb.idx = NULL;
528 }
529
530 out:
531 if (mdb.db)
532 (*mdb.db->close)(mdb.db);
533 if (mdb.idx)
534 (*mdb.idx->close)(mdb.idx);
535 if (hash)
536 (*hash->close)(hash);
537 if (mp)
538 mparse_free(mp);
539
540 manpath_free(&dirs);
541 ofile_free(of);
542 free(buf.cp);
543 free(dbuf.cp);
544 free(recs.stack);
545
546 return(MANDOCLEVEL_OK);
547
548 usage:
549 fprintf(stderr,
550 "usage: %s [-avvv] [-C file] | dir ... | -t file ...\n"
551 " -d dir [file ...] | "
552 "-u dir [file ...]\n",
553 progname);
554
555 return((int)MANDOCLEVEL_BADARG);
556 }
557
558 void
559 index_merge(const struct of *of, struct mparse *mp,
560 struct buf *dbuf, struct buf *buf, DB *hash,
561 struct mdb *mdb, struct recs *recs,
562 const char *basedir)
563 {
564 recno_t rec;
565 int ch, skip;
566 DBT key, val;
567 DB *files; /* temporary file name table */
568 char emptystring[1] = {'\0'};
569 struct mdoc *mdoc;
570 struct man *man;
571 char *p;
572 const char *fn, *msec, *march, *mtitle;
573 uint64_t mask;
574 size_t sv;
575 unsigned seq;
576 uint64_t vbuf[2];
577 char type;
578
579 if (warnings) {
580 files = NULL;
581 hash_reset(&files);
582 }
583
584 rec = 0;
585 for (of = of->first; of; of = of->next) {
586 fn = of->fname;
587
588 /*
589 * Try interpreting the file as mdoc(7) or man(7)
590 * source code, unless it is already known to be
591 * formatted. Fall back to formatted mode.
592 */
593
594 mparse_reset(mp);
595 mdoc = NULL;
596 man = NULL;
597
598 if ((MANDOC_SRC & of->src_form ||
599 ! (MANDOC_FORM & of->src_form)) &&
600 MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn))
601 mparse_result(mp, &mdoc, &man);
602
603 if (NULL != mdoc) {
604 msec = mdoc_meta(mdoc)->msec;
605 march = mdoc_meta(mdoc)->arch;
606 if (NULL == march)
607 march = "";
608 mtitle = mdoc_meta(mdoc)->title;
609 } else if (NULL != man) {
610 msec = man_meta(man)->msec;
611 march = "";
612 mtitle = man_meta(man)->title;
613 } else {
614 msec = of->sec;
615 march = of->arch;
616 mtitle = of->title;
617 }
618
619 /*
620 * Check whether the manual section given in a file
621 * agrees with the directory where the file is located.
622 * Some manuals have suffixes like (3p) on their
623 * section number either inside the file or in the
624 * directory name, some are linked into more than one
625 * section, like encrypt(1) = makekey(8). Do not skip
626 * manuals for such reasons.
627 */
628
629 skip = 0;
630 assert(of->sec);
631 assert(msec);
632 if (strcasecmp(msec, of->sec))
633 WARNING(fn, basedir, "Section \"%s\" manual "
634 "in \"%s\" directory", msec, of->sec);
635 /*
636 * Manual page directories exist for each kernel
637 * architecture as returned by machine(1).
638 * However, many manuals only depend on the
639 * application architecture as returned by arch(1).
640 * For example, some (2/ARM) manuals are shared
641 * across the "armish" and "zaurus" kernel
642 * architectures.
643 * A few manuals are even shared across completely
644 * different architectures, for example fdformat(1)
645 * on amd64, i386, sparc, and sparc64.
646 * Thus, warn about architecture mismatches,
647 * but don't skip manuals for this reason.
648 */
649
650 assert(of->arch);
651 assert(march);
652 if (strcasecmp(march, of->arch))
653 WARNING(fn, basedir, "Architecture \"%s\" "
654 "manual in \"%s\" directory",
655 march, of->arch);
656
657 /*
658 * By default, skip a file if the title given
659 * in the file disagrees with the file name.
660 * Do not warn, this happens for all MLINKs.
661 */
662
663 assert(of->title);
664 assert(mtitle);
665 if (strcasecmp(mtitle, of->title))
666 skip = 1;
667
668 /*
669 * Build a title string for the file. If it matches
670 * the location of the file, remember the title as
671 * found; else, remember it as missing.
672 */
673
674 if (warnings) {
675 buf->len = 0;
676 buf_appendb(buf, mtitle, strlen(mtitle));
677 buf_appendb(buf, "(", 1);
678 buf_appendb(buf, msec, strlen(msec));
679 if ('\0' != *march) {
680 buf_appendb(buf, "/", 1);
681 buf_appendb(buf, march, strlen(march));
682 }
683 buf_appendb(buf, ")", 2);
684 for (p = buf->cp; '\0' != *p; p++)
685 *p = tolower(*p);
686 key.data = buf->cp;
687 key.size = buf->len;
688 val.data = NULL;
689 val.size = 0;
690 if (0 == skip)
691 val.data = emptystring;
692 else {
693 ch = (*files->get)(files, &key, &val, 0);
694 if (ch < 0) {
695 perror("hash");
696 exit((int)MANDOCLEVEL_SYSERR);
697 } else if (ch > 0) {
698 val.data = (void *)fn;
699 val.size = strlen(fn) + 1;
700 } else
701 val.data = NULL;
702 }
703 if (NULL != val.data &&
704 (*files->put)(files, &key, &val, 0) < 0) {
705 perror("hash");
706 exit((int)MANDOCLEVEL_SYSERR);
707 }
708 }
709
710 if (skip && !use_all)
711 continue;
712
713 /*
714 * The index record value consists of a nil-terminated
715 * filename, a nil-terminated manual section, and a
716 * nil-terminated description. Use the actual
717 * location of the file, such that the user can find
718 * it with man(1). Since the description may not be
719 * set, we set a sentinel to see if we're going to
720 * write a nil byte in its place.
721 */
722
723 dbuf->len = 0;
724 type = mdoc ? 'd' : (man ? 'a' : 'c');
725 buf_appendb(dbuf, &type, 1);
726 buf_appendb(dbuf, fn, strlen(fn) + 1);
727 buf_appendb(dbuf, of->sec, strlen(of->sec) + 1);
728 buf_appendb(dbuf, of->title, strlen(of->title) + 1);
729 buf_appendb(dbuf, of->arch, strlen(of->arch) + 1);
730
731 sv = dbuf->len;
732
733 /*
734 * Collect keyword/mask pairs.
735 * Each pair will become a new btree node.
736 */
737
738 hash_reset(&hash);
739 if (mdoc)
740 pmdoc_node(hash, buf, dbuf,
741 mdoc_node(mdoc), mdoc_meta(mdoc));
742 else if (man)
743 pman_node(hash, buf, dbuf, man_node(man));
744 else
745 pformatted(hash, buf, dbuf, of, basedir);
746
747 /* Test mode, do not access any database. */
748
749 if (NULL == mdb->db || NULL == mdb->idx)
750 continue;
751
752 /*
753 * Make sure the file name is always registered
754 * as an .Nm search key.
755 */
756 buf->len = 0;
757 buf_append(buf, of->title);
758 hash_put(hash, buf, TYPE_Nm);
759
760 /*
761 * Reclaim an empty index record, if available.
762 * Use its record number for all new btree nodes.
763 */
764
765 if (recs->cur > 0) {
766 recs->cur--;
767 rec = recs->stack[(int)recs->cur];
768 } else if (recs->last > 0) {
769 rec = recs->last;
770 recs->last = 0;
771 } else
772 rec++;
773 vbuf[1] = htobe64(rec);
774
775 /*
776 * Copy from the in-memory hashtable of pending
777 * keyword/mask pairs into the database.
778 */
779
780 seq = R_FIRST;
781 while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
782 seq = R_NEXT;
783 assert(sizeof(uint64_t) == val.size);
784 memcpy(&mask, val.data, val.size);
785 vbuf[0] = htobe64(mask);
786 val.size = sizeof(vbuf);
787 val.data = &vbuf;
788 dbt_put(mdb->db, mdb->dbn, &key, &val);
789 }
790 if (ch < 0) {
791 perror("hash");
792 exit((int)MANDOCLEVEL_SYSERR);
793 }
794
795 /*
796 * Apply to the index. If we haven't had a description
797 * set, put an empty one in now.
798 */
799
800 if (dbuf->len == sv)
801 buf_appendb(dbuf, "", 1);
802
803 key.data = &rec;
804 key.size = sizeof(recno_t);
805
806 val.data = dbuf->cp;
807 val.size = dbuf->len;
808
809 if (verb)
810 printf("%s: Adding to index: %s\n", basedir, fn);
811
812 dbt_put(mdb->idx, mdb->idxn, &key, &val);
813 }
814
815 /*
816 * Iterate the remembered file titles and check that
817 * all files can be found by their main title.
818 */
819
820 if (warnings) {
821 seq = R_FIRST;
822 while (0 == (*files->seq)(files, &key, &val, seq)) {
823 seq = R_NEXT;
824 if (val.size)
825 fprintf(stderr, "%s: probably "
826 "unreachable, title is %s\n",
827 (char *)val.data, (char *)key.data);
828 }
829 (*files->close)(files);
830 }
831 }
832
833 /*
834 * Scan through all entries in the index file `idx' and prune those
835 * entries in `ofile'.
836 * Pruning consists of removing from `db', then invalidating the entry
837 * in `idx' (zeroing its value size).
838 */
839 static void
840 index_prune(const struct of *ofile, struct mdb *mdb,
841 struct recs *recs, const char *basedir)
842 {
843 const struct of *of;
844 const char *fn;
845 uint64_t vbuf[2];
846 unsigned seq, sseq;
847 DBT key, val;
848 int ch;
849
850 recs->cur = 0;
851 seq = R_FIRST;
852 while (0 == (ch = (*mdb->idx->seq)(mdb->idx, &key, &val, seq))) {
853 seq = R_NEXT;
854 assert(sizeof(recno_t) == key.size);
855 memcpy(&recs->last, key.data, key.size);
856
857 /* Deleted records are zero-sized. Skip them. */
858
859 if (0 == val.size)
860 goto cont;
861
862 /*
863 * Make sure we're sane.
864 * Read past our mdoc/man/cat type to the next string,
865 * then make sure it's bounded by a NUL.
866 * Failing any of these, we go into our error handler.
867 */
868
869 fn = (char *)val.data + 1;
870 if (NULL == memchr(fn, '\0', val.size - 1))
871 break;
872
873 /*
874 * Search for the file in those we care about.
875 * XXX: build this into a tree. Too slow.
876 */
877
878 for (of = ofile->first; of; of = of->next)
879 if (0 == strcmp(fn, of->fname))
880 break;
881
882 if (NULL == of)
883 continue;
884
885 /*
886 * Search through the keyword database, throwing out all
887 * references to our file.
888 */
889
890 sseq = R_FIRST;
891 while (0 == (ch = (*mdb->db->seq)(mdb->db,
892 &key, &val, sseq))) {
893 sseq = R_NEXT;
894 if (sizeof(vbuf) != val.size)
895 break;
896
897 memcpy(vbuf, val.data, val.size);
898 if (recs->last != betoh64(vbuf[1]))
899 continue;
900
901 if ((ch = (*mdb->db->del)(mdb->db,
902 &key, R_CURSOR)) < 0)
903 break;
904 }
905
906 if (ch < 0) {
907 perror(mdb->dbn);
908 exit((int)MANDOCLEVEL_SYSERR);
909 } else if (1 != ch) {
910 fprintf(stderr, "%s: corrupt database\n",
911 mdb->dbn);
912 exit((int)MANDOCLEVEL_SYSERR);
913 }
914
915 if (verb)
916 printf("%s: Deleting from index: %s\n",
917 basedir, fn);
918
919 val.size = 0;
920 ch = (*mdb->idx->put)(mdb->idx, &key, &val, R_CURSOR);
921
922 if (ch < 0)
923 break;
924 cont:
925 if (recs->cur >= recs->size) {
926 recs->size += MANDOC_SLOP;
927 recs->stack = mandoc_realloc(recs->stack,
928 recs->size * sizeof(recno_t));
929 }
930
931 recs->stack[(int)recs->cur] = recs->last;
932 recs->cur++;
933 }
934
935 if (ch < 0) {
936 perror(mdb->idxn);
937 exit((int)MANDOCLEVEL_SYSERR);
938 } else if (1 != ch) {
939 fprintf(stderr, "%s: corrupt index\n", mdb->idxn);
940 exit((int)MANDOCLEVEL_SYSERR);
941 }
942
943 recs->last++;
944 }
945
946 /*
947 * Grow the buffer (if necessary) and copy in a binary string.
948 */
949 static void
950 buf_appendb(struct buf *buf, const void *cp, size_t sz)
951 {
952
953 /* Overshoot by MANDOC_BUFSZ. */
954
955 while (buf->len + sz >= buf->size) {
956 buf->size = buf->len + sz + MANDOC_BUFSZ;
957 buf->cp = mandoc_realloc(buf->cp, buf->size);
958 }
959
960 memcpy(buf->cp + (int)buf->len, cp, sz);
961 buf->len += sz;
962 }
963
964 /*
965 * Append a nil-terminated string to the buffer.
966 * This can be invoked multiple times.
967 * The buffer string will be nil-terminated.
968 * If invoked multiple times, a space is put between strings.
969 */
970 static void
971 buf_append(struct buf *buf, const char *cp)
972 {
973 size_t sz;
974
975 if (0 == (sz = strlen(cp)))
976 return;
977
978 if (buf->len)
979 buf->cp[(int)buf->len - 1] = ' ';
980
981 buf_appendb(buf, cp, sz + 1);
982 }
983
984 /*
985 * Recursively add all text from a given node.
986 * This is optimised for general mdoc nodes in this context, which do
987 * not consist of subexpressions and having a recursive call for n->next
988 * would be wasteful.
989 * The "f" variable should be 0 unless called from pmdoc_Nd for the
990 * description buffer, which does not start at the beginning of the
991 * buffer.
992 */
993 static void
994 buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
995 {
996
997 for ( ; n; n = n->next) {
998 if (n->child)
999 buf_appendmdoc(buf, n->child, f);
1000
1001 if (MDOC_TEXT == n->type && f) {
1002 f = 0;
1003 buf_appendb(buf, n->string,
1004 strlen(n->string) + 1);
1005 } else if (MDOC_TEXT == n->type)
1006 buf_append(buf, n->string);
1007
1008 }
1009 }
1010
1011 static void
1012 hash_reset(DB **db)
1013 {
1014 DB *hash;
1015
1016 if (NULL != (hash = *db))
1017 (*hash->close)(hash);
1018
1019 *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1020 if (NULL == *db) {
1021 perror("hash");
1022 exit((int)MANDOCLEVEL_SYSERR);
1023 }
1024 }
1025
1026 /* ARGSUSED */
1027 static int
1028 pmdoc_head(MDOC_ARGS)
1029 {
1030
1031 return(MDOC_HEAD == n->type);
1032 }
1033
1034 /* ARGSUSED */
1035 static int
1036 pmdoc_body(MDOC_ARGS)
1037 {
1038
1039 return(MDOC_BODY == n->type);
1040 }
1041
1042 /* ARGSUSED */
1043 static int
1044 pmdoc_Fd(MDOC_ARGS)
1045 {
1046 const char *start, *end;
1047 size_t sz;
1048
1049 if (SEC_SYNOPSIS != n->sec)
1050 return(0);
1051 if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1052 return(0);
1053
1054 /*
1055 * Only consider those `Fd' macro fields that begin with an
1056 * "inclusion" token (versus, e.g., #define).
1057 */
1058 if (strcmp("#include", n->string))
1059 return(0);
1060
1061 if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1062 return(0);
1063
1064 /*
1065 * Strip away the enclosing angle brackets and make sure we're
1066 * not zero-length.
1067 */
1068
1069 start = n->string;
1070 if ('<' == *start || '"' == *start)
1071 start++;
1072
1073 if (0 == (sz = strlen(start)))
1074 return(0);
1075
1076 end = &start[(int)sz - 1];
1077 if ('>' == *end || '"' == *end)
1078 end--;
1079
1080 assert(end >= start);
1081
1082 buf_appendb(buf, start, (size_t)(end - start + 1));
1083 buf_appendb(buf, "", 1);
1084 return(1);
1085 }
1086
1087 /* ARGSUSED */
1088 static int
1089 pmdoc_In(MDOC_ARGS)
1090 {
1091
1092 if (NULL == n->child || MDOC_TEXT != n->child->type)
1093 return(0);
1094
1095 buf_append(buf, n->child->string);
1096 return(1);
1097 }
1098
1099 /* ARGSUSED */
1100 static int
1101 pmdoc_Fn(MDOC_ARGS)
1102 {
1103 struct mdoc_node *nn;
1104 const char *cp;
1105
1106 nn = n->child;
1107
1108 if (NULL == nn || MDOC_TEXT != nn->type)
1109 return(0);
1110
1111 /* .Fn "struct type *name" "char *arg" */
1112
1113 cp = strrchr(nn->string, ' ');
1114 if (NULL == cp)
1115 cp = nn->string;
1116
1117 /* Strip away pointer symbol. */
1118
1119 while ('*' == *cp)
1120 cp++;
1121
1122 /* Store the function name. */
1123
1124 buf_append(buf, cp);
1125 hash_put(hash, buf, TYPE_Fn);
1126
1127 /* Store the function type. */
1128
1129 if (nn->string < cp) {
1130 buf->len = 0;
1131 buf_appendb(buf, nn->string, cp - nn->string);
1132 buf_appendb(buf, "", 1);
1133 hash_put(hash, buf, TYPE_Ft);
1134 }
1135
1136 /* Store the arguments. */
1137
1138 for (nn = nn->next; nn; nn = nn->next) {
1139 if (MDOC_TEXT != nn->type)
1140 continue;
1141 buf->len = 0;
1142 buf_append(buf, nn->string);
1143 hash_put(hash, buf, TYPE_Fa);
1144 }
1145
1146 return(0);
1147 }
1148
1149 /* ARGSUSED */
1150 static int
1151 pmdoc_St(MDOC_ARGS)
1152 {
1153
1154 if (NULL == n->child || MDOC_TEXT != n->child->type)
1155 return(0);
1156
1157 buf_append(buf, n->child->string);
1158 return(1);
1159 }
1160
1161 /* ARGSUSED */
1162 static int
1163 pmdoc_Xr(MDOC_ARGS)
1164 {
1165
1166 if (NULL == (n = n->child))
1167 return(0);
1168
1169 buf_appendb(buf, n->string, strlen(n->string));
1170
1171 if (NULL != (n = n->next)) {
1172 buf_appendb(buf, ".", 1);
1173 buf_appendb(buf, n->string, strlen(n->string) + 1);
1174 } else
1175 buf_appendb(buf, ".", 2);
1176
1177 return(1);
1178 }
1179
1180 /* ARGSUSED */
1181 static int
1182 pmdoc_Nd(MDOC_ARGS)
1183 {
1184
1185 if (MDOC_BODY != n->type)
1186 return(0);
1187
1188 buf_appendmdoc(dbuf, n->child, 1);
1189 return(1);
1190 }
1191
1192 /* ARGSUSED */
1193 static int
1194 pmdoc_Nm(MDOC_ARGS)
1195 {
1196
1197 if (SEC_NAME == n->sec)
1198 return(1);
1199 else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
1200 return(0);
1201
1202 if (NULL == n->child)
1203 buf_append(buf, m->name);
1204
1205 return(1);
1206 }
1207
1208 /* ARGSUSED */
1209 static int
1210 pmdoc_Sh(MDOC_ARGS)
1211 {
1212
1213 return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1214 }
1215
1216 static void
1217 hash_put(DB *db, const struct buf *buf, uint64_t mask)
1218 {
1219 uint64_t oldmask;
1220 DBT key, val;
1221 int rc;
1222
1223 if (buf->len < 2)
1224 return;
1225
1226 key.data = buf->cp;
1227 key.size = buf->len;
1228
1229 if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
1230 perror("hash");
1231 exit((int)MANDOCLEVEL_SYSERR);
1232 } else if (0 == rc) {
1233 assert(sizeof(uint64_t) == val.size);
1234 memcpy(&oldmask, val.data, val.size);
1235 mask |= oldmask;
1236 }
1237
1238 val.data = &mask;
1239 val.size = sizeof(uint64_t);
1240
1241 if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
1242 perror("hash");
1243 exit((int)MANDOCLEVEL_SYSERR);
1244 }
1245 }
1246
1247 static void
1248 dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
1249 {
1250
1251 assert(key->size);
1252 assert(val->size);
1253
1254 if (0 == (*db->put)(db, key, val, 0))
1255 return;
1256
1257 perror(dbn);
1258 exit((int)MANDOCLEVEL_SYSERR);
1259 /* NOTREACHED */
1260 }
1261
1262 /*
1263 * Call out to per-macro handlers after clearing the persistent database
1264 * key. If the macro sets the database key, flush it to the database.
1265 */
1266 static void
1267 pmdoc_node(MDOC_ARGS)
1268 {
1269
1270 if (NULL == n)
1271 return;
1272
1273 switch (n->type) {
1274 case (MDOC_HEAD):
1275 /* FALLTHROUGH */
1276 case (MDOC_BODY):
1277 /* FALLTHROUGH */
1278 case (MDOC_TAIL):
1279 /* FALLTHROUGH */
1280 case (MDOC_BLOCK):
1281 /* FALLTHROUGH */
1282 case (MDOC_ELEM):
1283 buf->len = 0;
1284
1285 /*
1286 * Both NULL handlers and handlers returning true
1287 * request using the data. Only skip the element
1288 * when the handler returns false.
1289 */
1290
1291 if (NULL != mdocs[n->tok].fp &&
1292 0 == (*mdocs[n->tok].fp)(hash, buf, dbuf, n, m))
1293 break;
1294
1295 /*
1296 * For many macros, use the text from all children.
1297 * Set zero flags for macros not needing this.
1298 * In that case, the handler must fill the buffer.
1299 */
1300
1301 if (MDOCF_CHILD & mdocs[n->tok].flags)
1302 buf_appendmdoc(buf, n->child, 0);
1303
1304 /*
1305 * Cover the most common case:
1306 * Automatically stage one string per element.
1307 * Set a zero mask for macros not needing this.
1308 * Additional staging can be done in the handler.
1309 */
1310
1311 if (mdocs[n->tok].mask)
1312 hash_put(hash, buf, mdocs[n->tok].mask);
1313 break;
1314 default:
1315 break;
1316 }
1317
1318 pmdoc_node(hash, buf, dbuf, n->child, m);
1319 pmdoc_node(hash, buf, dbuf, n->next, m);
1320 }
1321
1322 static int
1323 pman_node(MAN_ARGS)
1324 {
1325 const struct man_node *head, *body;
1326 const char *start, *sv;
1327 size_t sz;
1328
1329 if (NULL == n)
1330 return(0);
1331
1332 /*
1333 * We're only searching for one thing: the first text child in
1334 * the BODY of a NAME section. Since we don't keep track of
1335 * sections in -man, run some hoops to find out whether we're in
1336 * the correct section or not.
1337 */
1338
1339 if (MAN_BODY == n->type && MAN_SH == n->tok) {
1340 body = n;
1341 assert(body->parent);
1342 if (NULL != (head = body->parent->head) &&
1343 1 == head->nchild &&
1344 NULL != (head = (head->child)) &&
1345 MAN_TEXT == head->type &&
1346 0 == strcmp(head->string, "NAME") &&
1347 NULL != (body = body->child) &&
1348 MAN_TEXT == body->type) {
1349
1350 assert(body->string);
1351 start = sv = body->string;
1352
1353 /*
1354 * Go through a special heuristic dance here.
1355 * This is why -man manuals are great!
1356 * (I'm being sarcastic: my eyes are bleeding.)
1357 * Conventionally, one or more manual names are
1358 * comma-specified prior to a whitespace, then a
1359 * dash, then a description. Try to puzzle out
1360 * the name parts here.
1361 */
1362
1363 for ( ;; ) {
1364 sz = strcspn(start, " ,");
1365 if ('\0' == start[(int)sz])
1366 break;
1367
1368 buf->len = 0;
1369 buf_appendb(buf, start, sz);
1370 buf_appendb(buf, "", 1);
1371
1372 hash_put(hash, buf, TYPE_Nm);
1373
1374 if (' ' == start[(int)sz]) {
1375 start += (int)sz + 1;
1376 break;
1377 }
1378
1379 assert(',' == start[(int)sz]);
1380 start += (int)sz + 1;
1381 while (' ' == *start)
1382 start++;
1383 }
1384
1385 buf->len = 0;
1386
1387 if (sv == start) {
1388 buf_append(buf, start);
1389 return(1);
1390 }
1391
1392 while (' ' == *start)
1393 start++;
1394
1395 if (0 == strncmp(start, "-", 1))
1396 start += 1;
1397 else if (0 == strncmp(start, "\\-\\-", 4))
1398 start += 4;
1399 else if (0 == strncmp(start, "\\-", 2))
1400 start += 2;
1401 else if (0 == strncmp(start, "\\(en", 4))
1402 start += 4;
1403 else if (0 == strncmp(start, "\\(em", 4))
1404 start += 4;
1405
1406 while (' ' == *start)
1407 start++;
1408
1409 sz = strlen(start) + 1;
1410 buf_appendb(dbuf, start, sz);
1411 buf_appendb(buf, start, sz);
1412
1413 hash_put(hash, buf, TYPE_Nd);
1414 }
1415 }
1416
1417 for (n = n->child; n; n = n->next)
1418 if (pman_node(hash, buf, dbuf, n))
1419 return(1);
1420
1421 return(0);
1422 }
1423
1424 /*
1425 * Parse a formatted manual page.
1426 * By necessity, this involves rather crude guesswork.
1427 */
1428 static void
1429 pformatted(DB *hash, struct buf *buf, struct buf *dbuf,
1430 const struct of *of, const char *basedir)
1431 {
1432 FILE *stream;
1433 char *line, *p, *title;
1434 size_t len, plen, titlesz;
1435
1436 if (NULL == (stream = fopen(of->fname, "r"))) {
1437 WARNING(of->fname, basedir, "%s", strerror(errno));
1438 return;
1439 }
1440
1441 /*
1442 * Always use the title derived from the filename up front,
1443 * do not even try to find it in the file. This also makes
1444 * sure we don't end up with an orphan index record, even if
1445 * the file content turns out to be completely unintelligible.
1446 */
1447
1448 buf->len = 0;
1449 buf_append(buf, of->title);
1450 hash_put(hash, buf, TYPE_Nm);
1451
1452 /* Skip to first blank line. */
1453
1454 while (NULL != (line = fgetln(stream, &len)))
1455 if ('\n' == *line)
1456 break;
1457
1458 /*
1459 * Assume the first line that is not indented
1460 * is the first section header. Skip to it.
1461 */
1462
1463 while (NULL != (line = fgetln(stream, &len)))
1464 if ('\n' != *line && ' ' != *line)
1465 break;
1466
1467 /*
1468 * Read up until the next section into a buffer.
1469 * Strip the leading and trailing newline from each read line,
1470 * appending a trailing space.
1471 * Ignore empty (whitespace-only) lines.
1472 */
1473
1474 titlesz = 0;
1475 title = NULL;
1476
1477 while (NULL != (line = fgetln(stream, &len))) {
1478 if (' ' != *line || '\n' != line[(int)len - 1])
1479 break;
1480 while (len > 0 && isspace((unsigned char)*line)) {
1481 line++;
1482 len--;
1483 }
1484 if (1 == len)
1485 continue;
1486 title = mandoc_realloc(title, titlesz + len);
1487 memcpy(title + titlesz, line, len);
1488 titlesz += len;
1489 title[(int)titlesz - 1] = ' ';
1490 }
1491
1492 /*
1493 * If no page content can be found, or the input line
1494 * is already the next section header, or there is no
1495 * trailing newline, reuse the page title as the page
1496 * description.
1497 */
1498
1499 if (NULL == title || '\0' == *title) {
1500 WARNING(of->fname, basedir,
1501 "Cannot find NAME section");
1502 buf_appendb(dbuf, buf->cp, buf->size);
1503 hash_put(hash, buf, TYPE_Nd);
1504 fclose(stream);
1505 free(title);
1506 return;
1507 }
1508
1509 title = mandoc_realloc(title, titlesz + 1);
1510 title[(int)titlesz] = '\0';
1511
1512 /*
1513 * Skip to the first dash.
1514 * Use the remaining line as the description (no more than 70
1515 * bytes).
1516 */
1517
1518 if (NULL != (p = strstr(title, "- "))) {
1519 for (p += 2; ' ' == *p || '\b' == *p; p++)
1520 /* Skip to next word. */ ;
1521 } else {
1522 WARNING(of->fname, basedir,
1523 "No dash in title line");
1524 p = title;
1525 }
1526
1527 plen = strlen(p);
1528
1529 /* Strip backspace-encoding from line. */
1530
1531 while (NULL != (line = memchr(p, '\b', plen))) {
1532 len = line - p;
1533 if (0 == len) {
1534 memmove(line, line + 1, plen--);
1535 continue;
1536 }
1537 memmove(line - 1, line + 1, plen - len);
1538 plen -= 2;
1539 }
1540
1541 buf_appendb(dbuf, p, plen + 1);
1542 buf->len = 0;
1543 buf_appendb(buf, p, plen + 1);
1544 hash_put(hash, buf, TYPE_Nd);
1545 fclose(stream);
1546 free(title);
1547 }
1548
1549 static void
1550 ofile_argbuild(int argc, char *argv[],
1551 struct of **of, const char *basedir)
1552 {
1553 char buf[MAXPATHLEN];
1554 const char *sec, *arch, *title;
1555 char *p;
1556 int i, src_form;
1557 struct of *nof;
1558
1559 for (i = 0; i < argc; i++) {
1560
1561 /*
1562 * Try to infer the manual section, architecture and
1563 * page title from the path, assuming it looks like
1564 * man*[/<arch>]/<title>.<section> or
1565 * cat<section>[/<arch>]/<title>.0
1566 */
1567
1568 if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {
1569 fprintf(stderr, "%s: Path too long\n", argv[i]);
1570 continue;
1571 }
1572 sec = arch = title = "";
1573 src_form = 0;
1574 p = strrchr(buf, '\0');
1575 while (p-- > buf) {
1576 if ('\0' == *sec && '.' == *p) {
1577 sec = p + 1;
1578 *p = '\0';
1579 if ('0' == *sec)
1580 src_form |= MANDOC_FORM;
1581 else if ('1' <= *sec && '9' >= *sec)
1582 src_form |= MANDOC_SRC;
1583 continue;
1584 }
1585 if ('/' != *p)
1586 continue;
1587 if ('\0' == *title) {
1588 title = p + 1;
1589 *p = '\0';
1590 continue;
1591 }
1592 if (0 == strncmp("man", p + 1, 3))
1593 src_form |= MANDOC_SRC;
1594 else if (0 == strncmp("cat", p + 1, 3))
1595 src_form |= MANDOC_FORM;
1596 else
1597 arch = p + 1;
1598 break;
1599 }
1600 if ('\0' == *title) {
1601 WARNING(argv[i], basedir,
1602 "Cannot deduce title from filename");
1603 title = buf;
1604 }
1605
1606 /*
1607 * Build the file structure.
1608 */
1609
1610 nof = mandoc_calloc(1, sizeof(struct of));
1611 nof->fname = mandoc_strdup(argv[i]);
1612 nof->sec = mandoc_strdup(sec);
1613 nof->arch = mandoc_strdup(arch);
1614 nof->title = mandoc_strdup(title);
1615 nof->src_form = src_form;
1616
1617 /*
1618 * Add the structure to the list.
1619 */
1620
1621 if (NULL == *of) {
1622 *of = nof;
1623 (*of)->first = nof;
1624 } else {
1625 nof->first = (*of)->first;
1626 (*of)->next = nof;
1627 *of = nof;
1628 }
1629 }
1630 }
1631
1632 /*
1633 * Recursively build up a list of files to parse.
1634 * We use this instead of ftw() and so on because I don't want global
1635 * variables hanging around.
1636 * This ignores the mandocdb.db and mandocdb.index files, but assumes that
1637 * everything else is a manual.
1638 * Pass in a pointer to a NULL structure for the first invocation.
1639 */
1640 static void
1641 ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1642 int p_src_form, struct of **of, char *basedir)
1643 {
1644 char buf[MAXPATHLEN];
1645 size_t sz;
1646 DIR *d;
1647 const char *fn, *sec, *arch;
1648 char *p, *q, *suffix;
1649 struct of *nof;
1650 struct dirent *dp;
1651 int src_form;
1652
1653 if (NULL == (d = opendir(dir))) {
1654 WARNING("", dir, "%s", strerror(errno));
1655 return;
1656 }
1657
1658 while (NULL != (dp = readdir(d))) {
1659 fn = dp->d_name;
1660
1661 if ('.' == *fn)
1662 continue;
1663
1664 src_form = p_src_form;
1665
1666 if (DT_DIR == dp->d_type) {
1667 sec = psec;
1668 arch = parch;
1669
1670 /*
1671 * By default, only use directories called:
1672 * man<section>/[<arch>/] or
1673 * cat<section>/[<arch>/]
1674 */
1675
1676 if ('\0' == *sec) {
1677 if(0 == strncmp("man", fn, 3)) {
1678 src_form |= MANDOC_SRC;
1679 sec = fn + 3;
1680 } else if (0 == strncmp("cat", fn, 3)) {
1681 src_form |= MANDOC_FORM;
1682 sec = fn + 3;
1683 } else {
1684 WARNING(fn, basedir, "Bad section");
1685 if (use_all)
1686 sec = fn;
1687 else
1688 continue;
1689 }
1690 } else if ('\0' == *arch) {
1691 if (NULL != strchr(fn, '.')) {
1692 WARNING(fn, basedir, "Bad architecture");
1693 if (0 == use_all)
1694 continue;
1695 }
1696 arch = fn;
1697 } else {
1698 WARNING(fn, basedir, "Excessive subdirectory");
1699 if (0 == use_all)
1700 continue;
1701 }
1702
1703 buf[0] = '\0';
1704 strlcat(buf, dir, MAXPATHLEN);
1705 strlcat(buf, "/", MAXPATHLEN);
1706 strlcat(basedir, "/", MAXPATHLEN);
1707 strlcat(basedir, fn, MAXPATHLEN);
1708 sz = strlcat(buf, fn, MAXPATHLEN);
1709
1710 if (MAXPATHLEN <= sz) {
1711 WARNING(fn, basedir, "Path too long");
1712 continue;
1713 }
1714
1715 ofile_dirbuild(buf, sec, arch,
1716 src_form, of, basedir);
1717
1718 p = strrchr(basedir, '/');
1719 *p = '\0';
1720 continue;
1721 }
1722
1723 if (DT_REG != dp->d_type) {
1724 WARNING(fn, basedir, "Not a regular file");
1725 continue;
1726 }
1727 if (!strcmp(MANDOC_DB, fn) || !strcmp(MANDOC_IDX, fn))
1728 continue;
1729 if ('\0' == *psec) {
1730 WARNING(fn, basedir, "File outside section");
1731 if (0 == use_all)
1732 continue;
1733 }
1734
1735 /*
1736 * By default, skip files where the file name suffix
1737 * does not agree with the section directory
1738 * they are located in.
1739 */
1740
1741 suffix = strrchr(fn, '.');
1742 if (NULL == suffix) {
1743 WARNING(fn, basedir, "No filename suffix");
1744 if (0 == use_all)
1745 continue;
1746 } else if ((MANDOC_SRC & src_form &&
1747 strcmp(suffix + 1, psec)) ||
1748 (MANDOC_FORM & src_form &&
1749 strcmp(suffix + 1, "0"))) {
1750 WARNING(fn, basedir, "Wrong filename suffix");
1751 if (0 == use_all)
1752 continue;
1753 if ('0' == suffix[1])
1754 src_form |= MANDOC_FORM;
1755 else if ('1' <= suffix[1] && '9' >= suffix[1])
1756 src_form |= MANDOC_SRC;
1757 }
1758
1759 /*
1760 * Skip formatted manuals if a source version is
1761 * available. Ignore the age: it is very unlikely
1762 * that people install newer formatted base manuals
1763 * when they used to have source manuals before,
1764 * and in ports, old manuals get removed on update.
1765 */
1766 if (0 == use_all && MANDOC_FORM & src_form &&
1767 '\0' != *psec) {
1768 buf[0] = '\0';
1769 strlcat(buf, dir, MAXPATHLEN);
1770 p = strrchr(buf, '/');
1771 if ('\0' != *parch && NULL != p)
1772 for (p--; p > buf; p--)
1773 if ('/' == *p)
1774 break;
1775 if (NULL == p)
1776 p = buf;
1777 else
1778 p++;
1779 if (0 == strncmp("cat", p, 3))
1780 memcpy(p, "man", 3);
1781 strlcat(buf, "/", MAXPATHLEN);
1782 sz = strlcat(buf, fn, MAXPATHLEN);
1783 if (sz >= MAXPATHLEN) {
1784 WARNING(fn, basedir, "Path too long");
1785 continue;
1786 }
1787 q = strrchr(buf, '.');
1788 if (NULL != q && p < q++) {
1789 *q = '\0';
1790 sz = strlcat(buf, psec, MAXPATHLEN);
1791 if (sz >= MAXPATHLEN) {
1792 WARNING(fn, basedir, "Path too long");
1793 continue;
1794 }
1795 if (0 == access(buf, R_OK))
1796 continue;
1797 }
1798 }
1799
1800 buf[0] = '\0';
1801 assert('.' == dir[0]);
1802 if ('/' == dir[1]) {
1803 strlcat(buf, dir + 2, MAXPATHLEN);
1804 strlcat(buf, "/", MAXPATHLEN);
1805 }
1806 sz = strlcat(buf, fn, MAXPATHLEN);
1807 if (sz >= MAXPATHLEN) {
1808 WARNING(fn, basedir, "Path too long");
1809 continue;
1810 }
1811
1812 nof = mandoc_calloc(1, sizeof(struct of));
1813 nof->fname = mandoc_strdup(buf);
1814 nof->sec = mandoc_strdup(psec);
1815 nof->arch = mandoc_strdup(parch);
1816 nof->src_form = src_form;
1817
1818 /*
1819 * Remember the file name without the extension,
1820 * to be used as the page title in the database.
1821 */
1822
1823 if (NULL != suffix)
1824 *suffix = '\0';
1825 nof->title = mandoc_strdup(fn);
1826
1827 /*
1828 * Add the structure to the list.
1829 */
1830
1831 if (NULL == *of) {
1832 *of = nof;
1833 (*of)->first = nof;
1834 } else {
1835 nof->first = (*of)->first;
1836 (*of)->next = nof;
1837 *of = nof;
1838 }
1839 }
1840
1841 closedir(d);
1842 }
1843
1844 static void
1845 ofile_free(struct of *of)
1846 {
1847 struct of *nof;
1848
1849 if (NULL != of)
1850 of = of->first;
1851
1852 while (NULL != of) {
1853 nof = of->next;
1854 free(of->fname);
1855 free(of->sec);
1856 free(of->arch);
1857 free(of->title);
1858 free(of);
1859 of = nof;
1860 }
1861 }