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