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