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