Annotation of mandoc/mandocdb.c, Revision 1.81
1.81 ! schwarze 1: /* $Id: mandocdb.c,v 1.80 2013/12/26 23:35:59 schwarze Exp $ */
1.1 kristaps 2: /*
1.48 schwarze 3: * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.56 schwarze 4: * Copyright (c) 2011, 2012, 2013 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:
1.50 kristaps 22: #include <sys/stat.h>
1.1 kristaps 23:
24: #include <assert.h>
1.43 kristaps 25: #include <ctype.h>
1.63 schwarze 26: #include <errno.h>
1.1 kristaps 27: #include <fcntl.h>
1.50 kristaps 28: #include <fts.h>
1.1 kristaps 29: #include <getopt.h>
1.58 schwarze 30: #include <limits.h>
1.50 kristaps 31: #include <stddef.h>
1.59 schwarze 32: #include <stdio.h>
1.1 kristaps 33: #include <stdint.h>
34: #include <stdlib.h>
35: #include <string.h>
1.17 schwarze 36: #include <unistd.h>
1.1 kristaps 37:
1.53 kristaps 38: #ifdef HAVE_OHASH
1.50 kristaps 39: #include <ohash.h>
1.53 kristaps 40: #else
41: #include "compat_ohash.h"
42: #endif
1.50 kristaps 43: #include <sqlite3.h>
1.1 kristaps 44:
1.50 kristaps 45: #include "mdoc.h"
1.1 kristaps 46: #include "man.h"
47: #include "mandoc.h"
1.10 kristaps 48: #include "manpath.h"
1.55 kristaps 49: #include "mansearch.h"
1.1 kristaps 50:
1.52 kristaps 51: #define SQL_EXEC(_v) \
52: if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
53: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
54: #define SQL_BIND_TEXT(_s, _i, _v) \
55: if (SQLITE_OK != sqlite3_bind_text \
56: ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
57: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
58: #define SQL_BIND_INT(_s, _i, _v) \
59: if (SQLITE_OK != sqlite3_bind_int \
60: ((_s), (_i)++, (_v))) \
61: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
62: #define SQL_BIND_INT64(_s, _i, _v) \
63: if (SQLITE_OK != sqlite3_bind_int64 \
64: ((_s), (_i)++, (_v))) \
65: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
66: #define SQL_STEP(_s) \
67: if (SQLITE_DONE != sqlite3_step((_s))) \
68: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
69:
1.50 kristaps 70: enum op {
71: OP_DEFAULT = 0, /* new dbs from dir list or default config */
72: OP_CONFFILE, /* new databases from custom config file */
73: OP_UPDATE, /* delete/add entries in existing database */
74: OP_DELETE, /* delete entries from existing database */
75: OP_TEST /* change no databases, report potential problems */
1.38 schwarze 76: };
77:
1.50 kristaps 78: enum form {
79: FORM_SRC, /* format is -man or -mdoc */
80: FORM_CAT, /* format is cat */
81: FORM_NONE /* format is unknown */
82: };
1.38 schwarze 83:
1.50 kristaps 84: struct str {
85: char *utf8; /* key in UTF-8 form */
1.78 schwarze 86: const struct mpage *mpage; /* if set, the owning parse */
1.50 kristaps 87: uint64_t mask; /* bitmask in sequence */
1.51 kristaps 88: char key[]; /* the string itself */
1.38 schwarze 89: };
90:
1.79 schwarze 91: struct inodev {
92: ino_t st_ino;
93: dev_t st_dev;
1.50 kristaps 94: };
1.5 kristaps 95:
1.78 schwarze 96: struct mpage {
1.79 schwarze 97: struct inodev inodev; /* used for hashing routine */
1.50 kristaps 98: enum form dform; /* path-cued form */
99: enum form sform; /* suffix-cued form */
1.58 schwarze 100: char file[PATH_MAX]; /* filename rel. to manpath */
1.66 schwarze 101: char *desc; /* parsed description */
102: char *name; /* name (from filename) (not empty) */
103: char *sec; /* suffix-cued section (or empty) */
104: char *dsec; /* path-cued section (or empty) */
105: char *arch; /* path-cued arch. (or empty) */
1.3 kristaps 106: };
107:
1.69 schwarze 108: struct title {
109: char *title; /* name(sec/arch) given inside the file */
110: char *file; /* file name in case of mismatch */
111: };
112:
1.50 kristaps 113: enum stmt {
1.81 ! schwarze 114: STMT_DELETE_PAGE = 0, /* delete mpage */
! 115: STMT_INSERT_PAGE, /* insert mpage */
! 116: STMT_INSERT_LINK, /* insert mlink */
! 117: STMT_INSERT_KEY, /* insert parsed key */
1.50 kristaps 118: STMT__MAX
1.1 kristaps 119: };
120:
1.78 schwarze 121: typedef int (*mdoc_fp)(struct mpage *, const struct mdoc_node *);
1.1 kristaps 122:
1.50 kristaps 123: struct mdoc_handler {
124: mdoc_fp fp; /* optional handler */
125: uint64_t mask; /* set unless handler returns 0 */
1.1 kristaps 126: };
127:
1.59 schwarze 128: static void dbclose(int);
1.78 schwarze 129: static void dbindex(struct mchars *, int, const struct mpage *);
1.59 schwarze 130: static int dbopen(int);
131: static void dbprune(void);
1.78 schwarze 132: static void fileadd(struct mpage *);
1.50 kristaps 133: static int filecheck(const char *);
1.59 schwarze 134: static void filescan(const char *);
1.50 kristaps 135: static void *hash_alloc(size_t, void *);
136: static void hash_free(void *, size_t, void *);
137: static void *hash_halloc(size_t, void *);
1.78 schwarze 138: static void inoadd(const struct stat *, struct mpage *);
1.50 kristaps 139: static int inocheck(const struct stat *);
1.59 schwarze 140: static void ofadd(int, const char *, const char *, const char *,
141: const char *, const char *, const struct stat *);
1.78 schwarze 142: static void mpages_free(void);
143: static void mpages_merge(struct mchars *, struct mparse *, int);
144: static void parse_cat(struct mpage *);
145: static void parse_man(struct mpage *, const struct man_node *);
146: static void parse_mdoc(struct mpage *, const struct mdoc_node *);
147: static int parse_mdoc_body(struct mpage *, const struct mdoc_node *);
148: static int parse_mdoc_head(struct mpage *, const struct mdoc_node *);
149: static int parse_mdoc_Fd(struct mpage *, const struct mdoc_node *);
150: static int parse_mdoc_Fn(struct mpage *, const struct mdoc_node *);
151: static int parse_mdoc_In(struct mpage *, const struct mdoc_node *);
152: static int parse_mdoc_Nd(struct mpage *, const struct mdoc_node *);
153: static int parse_mdoc_Nm(struct mpage *, const struct mdoc_node *);
154: static int parse_mdoc_Sh(struct mpage *, const struct mdoc_node *);
155: static int parse_mdoc_St(struct mpage *, const struct mdoc_node *);
156: static int parse_mdoc_Xr(struct mpage *, const struct mdoc_node *);
157: static void putkey(const struct mpage *,
1.50 kristaps 158: const char *, uint64_t);
1.78 schwarze 159: static void putkeys(const struct mpage *,
1.64 schwarze 160: const char *, size_t, uint64_t);
1.78 schwarze 161: static void putmdockey(const struct mpage *,
1.50 kristaps 162: const struct mdoc_node *, uint64_t);
1.59 schwarze 163: static void say(const char *, const char *, ...);
1.80 schwarze 164: static int set_basedir(const char *);
1.59 schwarze 165: static int treescan(void);
1.50 kristaps 166: static size_t utf8(unsigned int, char [7]);
167: static void utf8key(struct mchars *, struct str *);
168:
169: static char *progname;
170: static int use_all; /* use all found files */
171: static int nodb; /* no database changes */
172: static int verb; /* print what we're doing */
173: static int warnings; /* warn about crap */
1.59 schwarze 174: static int exitcode; /* to be returned by main */
1.50 kristaps 175: static enum op op; /* operational mode */
1.59 schwarze 176: static char basedir[PATH_MAX]; /* current base directory */
1.78 schwarze 177: static struct ohash mpages; /* table of distinct manual pages */
1.50 kristaps 178: static struct ohash filenames; /* table of filenames */
179: static struct ohash strings; /* table of all strings */
180: static sqlite3 *db = NULL; /* current database */
181: static sqlite3_stmt *stmts[STMT__MAX]; /* current statements */
1.25 schwarze 182:
183: static const struct mdoc_handler mdocs[MDOC_MAX] = {
1.68 schwarze 184: { NULL, 0 }, /* Ap */
185: { NULL, 0 }, /* Dd */
186: { NULL, 0 }, /* Dt */
187: { NULL, 0 }, /* Os */
188: { parse_mdoc_Sh, TYPE_Sh }, /* Sh */
189: { parse_mdoc_head, TYPE_Ss }, /* Ss */
190: { NULL, 0 }, /* Pp */
191: { NULL, 0 }, /* D1 */
192: { NULL, 0 }, /* Dl */
193: { NULL, 0 }, /* Bd */
194: { NULL, 0 }, /* Ed */
195: { NULL, 0 }, /* Bl */
196: { NULL, 0 }, /* El */
197: { NULL, 0 }, /* It */
198: { NULL, 0 }, /* Ad */
199: { NULL, TYPE_An }, /* An */
200: { NULL, TYPE_Ar }, /* Ar */
201: { NULL, TYPE_Cd }, /* Cd */
202: { NULL, TYPE_Cm }, /* Cm */
203: { NULL, TYPE_Dv }, /* Dv */
204: { NULL, TYPE_Er }, /* Er */
205: { NULL, TYPE_Ev }, /* Ev */
206: { NULL, 0 }, /* Ex */
207: { NULL, TYPE_Fa }, /* Fa */
208: { parse_mdoc_Fd, 0 }, /* Fd */
209: { NULL, TYPE_Fl }, /* Fl */
210: { parse_mdoc_Fn, 0 }, /* Fn */
211: { NULL, TYPE_Ft }, /* Ft */
212: { NULL, TYPE_Ic }, /* Ic */
213: { parse_mdoc_In, TYPE_In }, /* In */
214: { NULL, TYPE_Li }, /* Li */
215: { parse_mdoc_Nd, TYPE_Nd }, /* Nd */
216: { parse_mdoc_Nm, TYPE_Nm }, /* Nm */
217: { NULL, 0 }, /* Op */
218: { NULL, 0 }, /* Ot */
219: { NULL, TYPE_Pa }, /* Pa */
220: { NULL, 0 }, /* Rv */
221: { parse_mdoc_St, 0 }, /* St */
222: { NULL, TYPE_Va }, /* Va */
223: { parse_mdoc_body, TYPE_Va }, /* Vt */
224: { parse_mdoc_Xr, 0 }, /* Xr */
225: { NULL, 0 }, /* %A */
226: { NULL, 0 }, /* %B */
227: { NULL, 0 }, /* %D */
228: { NULL, 0 }, /* %I */
229: { NULL, 0 }, /* %J */
230: { NULL, 0 }, /* %N */
231: { NULL, 0 }, /* %O */
232: { NULL, 0 }, /* %P */
233: { NULL, 0 }, /* %R */
234: { NULL, 0 }, /* %T */
235: { NULL, 0 }, /* %V */
236: { NULL, 0 }, /* Ac */
237: { NULL, 0 }, /* Ao */
238: { NULL, 0 }, /* Aq */
239: { NULL, TYPE_At }, /* At */
240: { NULL, 0 }, /* Bc */
241: { NULL, 0 }, /* Bf */
242: { NULL, 0 }, /* Bo */
243: { NULL, 0 }, /* Bq */
244: { NULL, TYPE_Bsx }, /* Bsx */
245: { NULL, TYPE_Bx }, /* Bx */
246: { NULL, 0 }, /* Db */
247: { NULL, 0 }, /* Dc */
248: { NULL, 0 }, /* Do */
249: { NULL, 0 }, /* Dq */
250: { NULL, 0 }, /* Ec */
251: { NULL, 0 }, /* Ef */
252: { NULL, TYPE_Em }, /* Em */
253: { NULL, 0 }, /* Eo */
254: { NULL, TYPE_Fx }, /* Fx */
255: { NULL, TYPE_Ms }, /* Ms */
256: { NULL, 0 }, /* No */
257: { NULL, 0 }, /* Ns */
258: { NULL, TYPE_Nx }, /* Nx */
259: { NULL, TYPE_Ox }, /* Ox */
260: { NULL, 0 }, /* Pc */
261: { NULL, 0 }, /* Pf */
262: { NULL, 0 }, /* Po */
263: { NULL, 0 }, /* Pq */
264: { NULL, 0 }, /* Qc */
265: { NULL, 0 }, /* Ql */
266: { NULL, 0 }, /* Qo */
267: { NULL, 0 }, /* Qq */
268: { NULL, 0 }, /* Re */
269: { NULL, 0 }, /* Rs */
270: { NULL, 0 }, /* Sc */
271: { NULL, 0 }, /* So */
272: { NULL, 0 }, /* Sq */
273: { NULL, 0 }, /* Sm */
274: { NULL, 0 }, /* Sx */
275: { NULL, TYPE_Sy }, /* Sy */
276: { NULL, TYPE_Tn }, /* Tn */
277: { NULL, 0 }, /* Ux */
278: { NULL, 0 }, /* Xc */
279: { NULL, 0 }, /* Xo */
280: { parse_mdoc_head, 0 }, /* Fo */
281: { NULL, 0 }, /* Fc */
282: { NULL, 0 }, /* Oo */
283: { NULL, 0 }, /* Oc */
284: { NULL, 0 }, /* Bk */
285: { NULL, 0 }, /* Ek */
286: { NULL, 0 }, /* Bt */
287: { NULL, 0 }, /* Hf */
288: { NULL, 0 }, /* Fr */
289: { NULL, 0 }, /* Ud */
290: { NULL, TYPE_Lb }, /* Lb */
291: { NULL, 0 }, /* Lp */
292: { NULL, TYPE_Lk }, /* Lk */
293: { NULL, TYPE_Mt }, /* Mt */
294: { NULL, 0 }, /* Brq */
295: { NULL, 0 }, /* Bro */
296: { NULL, 0 }, /* Brc */
297: { NULL, 0 }, /* %C */
298: { NULL, 0 }, /* Es */
299: { NULL, 0 }, /* En */
300: { NULL, TYPE_Dx }, /* Dx */
301: { NULL, 0 }, /* %Q */
302: { NULL, 0 }, /* br */
303: { NULL, 0 }, /* sp */
304: { NULL, 0 }, /* %U */
305: { NULL, 0 }, /* Ta */
1.1 kristaps 306: };
307:
308: int
309: main(int argc, char *argv[])
310: {
1.59 schwarze 311: int ch, i;
1.50 kristaps 312: size_t j, sz;
1.59 schwarze 313: const char *path_arg;
1.50 kristaps 314: struct mchars *mc;
315: struct manpaths dirs;
316: struct mparse *mp;
1.78 schwarze 317: struct ohash_info mpages_info, filename_info;
1.50 kristaps 318:
319: memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *));
320: memset(&dirs, 0, sizeof(struct manpaths));
321:
1.78 schwarze 322: mpages_info.alloc = filename_info.alloc = hash_alloc;
323: mpages_info.halloc = filename_info.halloc = hash_halloc;
324: mpages_info.hfree = filename_info.hfree = hash_free;
1.50 kristaps 325:
1.79 schwarze 326: mpages_info.key_offset = offsetof(struct mpage, inodev);
1.78 schwarze 327: filename_info.key_offset = offsetof(struct mpage, file);
1.1 kristaps 328:
329: progname = strrchr(argv[0], '/');
330: if (progname == NULL)
331: progname = argv[0];
332: else
333: ++progname;
334:
1.50 kristaps 335: /*
336: * We accept a few different invocations.
337: * The CHECKOP macro makes sure that invocation styles don't
338: * clobber each other.
339: */
340: #define CHECKOP(_op, _ch) do \
341: if (OP_DEFAULT != (_op)) { \
342: fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
343: goto usage; \
344: } while (/*CONSTCOND*/0)
1.10 kristaps 345:
1.59 schwarze 346: path_arg = NULL;
1.38 schwarze 347: op = OP_DEFAULT;
1.1 kristaps 348:
1.50 kristaps 349: while (-1 != (ch = getopt(argc, argv, "aC:d:ntu:vW")))
1.1 kristaps 350: switch (ch) {
1.12 schwarze 351: case ('a'):
352: use_all = 1;
353: break;
1.34 schwarze 354: case ('C'):
1.50 kristaps 355: CHECKOP(op, ch);
1.59 schwarze 356: path_arg = optarg;
1.38 schwarze 357: op = OP_CONFFILE;
1.34 schwarze 358: break;
1.5 kristaps 359: case ('d'):
1.50 kristaps 360: CHECKOP(op, ch);
1.59 schwarze 361: path_arg = optarg;
1.5 kristaps 362: op = OP_UPDATE;
363: break;
1.50 kristaps 364: case ('n'):
365: nodb = 1;
366: break;
1.38 schwarze 367: case ('t'):
1.50 kristaps 368: CHECKOP(op, ch);
1.38 schwarze 369: dup2(STDOUT_FILENO, STDERR_FILENO);
370: op = OP_TEST;
1.50 kristaps 371: nodb = warnings = 1;
1.38 schwarze 372: break;
1.5 kristaps 373: case ('u'):
1.50 kristaps 374: CHECKOP(op, ch);
1.59 schwarze 375: path_arg = optarg;
1.5 kristaps 376: op = OP_DELETE;
377: break;
378: case ('v'):
379: verb++;
380: break;
1.38 schwarze 381: case ('W'):
382: warnings = 1;
383: break;
1.1 kristaps 384: default:
1.38 schwarze 385: goto usage;
1.1 kristaps 386: }
387:
388: argc -= optind;
389: argv += optind;
390:
1.38 schwarze 391: if (OP_CONFFILE == op && argc > 0) {
1.50 kristaps 392: fprintf(stderr, "-C: Too many arguments\n");
1.38 schwarze 393: goto usage;
394: }
395:
1.59 schwarze 396: exitcode = (int)MANDOCLEVEL_OK;
1.50 kristaps 397: mp = mparse_alloc(MPARSE_AUTO,
398: MANDOCLEVEL_FATAL, NULL, NULL, NULL);
399: mc = mchars_alloc();
400:
1.78 schwarze 401: ohash_init(&mpages, 6, &mpages_info);
1.50 kristaps 402: ohash_init(&filenames, 6, &filename_info);
403:
404: if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
405: /*
406: * Force processing all files.
407: */
408: use_all = 1;
1.59 schwarze 409:
1.50 kristaps 410: /*
411: * All of these deal with a specific directory.
412: * Jump into that directory then collect files specified
413: * on the command-line.
414: */
1.59 schwarze 415: if (0 == set_basedir(path_arg))
1.50 kristaps 416: goto out;
417: for (i = 0; i < argc; i++)
1.59 schwarze 418: filescan(argv[i]);
419: if (0 == dbopen(1))
1.50 kristaps 420: goto out;
421: if (OP_TEST != op)
1.59 schwarze 422: dbprune();
1.50 kristaps 423: if (OP_DELETE != op)
1.78 schwarze 424: mpages_merge(mc, mp, 0);
1.59 schwarze 425: dbclose(1);
1.50 kristaps 426: } else {
427: /*
428: * If we have arguments, use them as our manpaths.
429: * If we don't, grok from manpath(1) or however else
430: * manpath_parse() wants to do it.
431: */
432: if (argc > 0) {
433: dirs.paths = mandoc_calloc
434: (argc, sizeof(char *));
435: dirs.sz = (size_t)argc;
436: for (i = 0; i < argc; i++)
437: dirs.paths[i] = mandoc_strdup(argv[i]);
438: } else
1.59 schwarze 439: manpath_parse(&dirs, path_arg, NULL, NULL);
1.50 kristaps 440:
441: /*
1.69 schwarze 442: * First scan the tree rooted at a base directory, then
443: * build a new database and finally move it into place.
1.50 kristaps 444: * Ignore zero-length directories and strip trailing
445: * slashes.
446: */
447: for (j = 0; j < dirs.sz; j++) {
448: sz = strlen(dirs.paths[j]);
449: if (sz && '/' == dirs.paths[j][sz - 1])
450: dirs.paths[j][--sz] = '\0';
451: if (0 == sz)
452: continue;
1.66 schwarze 453:
454: if (j) {
1.78 schwarze 455: ohash_init(&mpages, 6, &mpages_info);
1.66 schwarze 456: ohash_init(&filenames, 6, &filename_info);
457: }
458:
1.59 schwarze 459: if (0 == set_basedir(dirs.paths[j]))
1.50 kristaps 460: goto out;
1.59 schwarze 461: if (0 == treescan())
1.50 kristaps 462: goto out;
1.59 schwarze 463: if (0 == set_basedir(dirs.paths[j]))
1.50 kristaps 464: goto out;
1.59 schwarze 465: if (0 == dbopen(0))
1.50 kristaps 466: goto out;
1.52 kristaps 467:
1.78 schwarze 468: mpages_merge(mc, mp, warnings && !use_all);
1.59 schwarze 469: dbclose(0);
1.66 schwarze 470:
471: if (j + 1 < dirs.sz) {
1.80 schwarze 472: mpages_free();
1.78 schwarze 473: ohash_delete(&mpages);
1.66 schwarze 474: ohash_delete(&filenames);
475: }
1.50 kristaps 476: }
477: }
478: out:
1.59 schwarze 479: set_basedir(NULL);
1.50 kristaps 480: manpath_free(&dirs);
481: mchars_free(mc);
482: mparse_free(mp);
1.80 schwarze 483: mpages_free();
1.78 schwarze 484: ohash_delete(&mpages);
1.50 kristaps 485: ohash_delete(&filenames);
1.59 schwarze 486: return(exitcode);
1.50 kristaps 487: usage:
488: fprintf(stderr, "usage: %s [-anvW] [-C file]\n"
489: " %s [-anvW] dir ...\n"
490: " %s [-nvW] -d dir [file ...]\n"
491: " %s [-nvW] -u dir [file ...]\n"
492: " %s -t file ...\n",
493: progname, progname, progname,
494: progname, progname);
495:
1.59 schwarze 496: return((int)MANDOCLEVEL_BADARG);
1.50 kristaps 497: }
498:
499: /*
1.59 schwarze 500: * Scan a directory tree rooted at "basedir" for manpages.
1.50 kristaps 501: * We use fts(), scanning directory parts along the way for clues to our
502: * section and architecture.
503: *
504: * If use_all has been specified, grok all files.
505: * If not, sanitise paths to the following:
506: *
507: * [./]man*[/<arch>]/<name>.<section>
508: * or
509: * [./]cat<section>[/<arch>]/<name>.0
510: *
511: * TODO: accomodate for multi-language directories.
512: */
513: static int
1.59 schwarze 514: treescan(void)
1.50 kristaps 515: {
516: FTS *f;
517: FTSENT *ff;
518: int dform;
519: char *sec;
1.66 schwarze 520: const char *dsec, *arch, *cp, *path;
1.50 kristaps 521: const char *argv[2];
522:
523: argv[0] = ".";
524: argv[1] = (char *)NULL;
525:
526: /*
527: * Walk through all components under the directory, using the
528: * logical descent of files.
529: */
530: f = fts_open((char * const *)argv, FTS_LOGICAL, NULL);
531: if (NULL == f) {
1.59 schwarze 532: exitcode = (int)MANDOCLEVEL_SYSERR;
533: say("", NULL);
1.50 kristaps 534: return(0);
535: }
536:
537: dsec = arch = NULL;
538: dform = FORM_NONE;
539:
540: while (NULL != (ff = fts_read(f))) {
541: path = ff->fts_path + 2;
542: /*
1.78 schwarze 543: * If we're a regular file, add an mpage by using the
1.50 kristaps 544: * stored directory data and handling the filename.
545: * Disallow duplicate (hard-linked) files.
546: */
547: if (FTS_F == ff->fts_info) {
1.60 schwarze 548: if (0 == strcmp(path, MANDOC_DB))
549: continue;
1.50 kristaps 550: if ( ! use_all && ff->fts_level < 2) {
1.56 schwarze 551: if (warnings)
1.59 schwarze 552: say(path, "Extraneous file");
1.50 kristaps 553: continue;
554: } else if (inocheck(ff->fts_statp)) {
1.56 schwarze 555: if (warnings)
1.59 schwarze 556: say(path, "Duplicate file");
1.50 kristaps 557: continue;
1.60 schwarze 558: } else if (NULL == (sec =
559: strrchr(ff->fts_name, '.'))) {
560: if ( ! use_all) {
1.56 schwarze 561: if (warnings)
1.60 schwarze 562: say(path,
563: "No filename suffix");
1.50 kristaps 564: continue;
565: }
1.60 schwarze 566: } else if (0 == strcmp(++sec, "html")) {
567: if (warnings)
568: say(path, "Skip html");
569: continue;
570: } else if (0 == strcmp(sec, "gz")) {
571: if (warnings)
572: say(path, "Skip gz");
573: continue;
574: } else if (0 == strcmp(sec, "ps")) {
575: if (warnings)
576: say(path, "Skip ps");
577: continue;
578: } else if (0 == strcmp(sec, "pdf")) {
579: if (warnings)
580: say(path, "Skip pdf");
581: continue;
582: } else if ( ! use_all &&
583: ((FORM_SRC == dform && strcmp(sec, dsec)) ||
584: (FORM_CAT == dform && strcmp(sec, "0")))) {
585: if (warnings)
586: say(path, "Wrong filename suffix");
587: continue;
1.66 schwarze 588: } else
1.60 schwarze 589: sec[-1] = '\0';
1.66 schwarze 590: ofadd(dform, path, ff->fts_name, dsec, sec,
591: arch, ff->fts_statp);
1.50 kristaps 592: continue;
593: } else if (FTS_D != ff->fts_info &&
1.60 schwarze 594: FTS_DP != ff->fts_info) {
595: if (warnings)
596: say(path, "Not a regular file");
1.50 kristaps 597: continue;
1.60 schwarze 598: }
1.1 kristaps 599:
1.50 kristaps 600: switch (ff->fts_level) {
601: case (0):
602: /* Ignore the root directory. */
603: break;
604: case (1):
605: /*
606: * This might contain manX/ or catX/.
607: * Try to infer this from the name.
608: * If we're not in use_all, enforce it.
609: */
610: dsec = NULL;
611: dform = FORM_NONE;
612: cp = ff->fts_name;
613: if (FTS_DP == ff->fts_info)
614: break;
1.1 kristaps 615:
1.50 kristaps 616: if (0 == strncmp(cp, "man", 3)) {
617: dform = FORM_SRC;
1.66 schwarze 618: dsec = cp + 3;
1.50 kristaps 619: } else if (0 == strncmp(cp, "cat", 3)) {
620: dform = FORM_CAT;
1.66 schwarze 621: dsec = cp + 3;
1.50 kristaps 622: }
1.1 kristaps 623:
1.50 kristaps 624: if (NULL != dsec || use_all)
625: break;
1.1 kristaps 626:
1.56 schwarze 627: if (warnings)
1.59 schwarze 628: say(path, "Unknown directory part");
1.50 kristaps 629: fts_set(f, ff, FTS_SKIP);
630: break;
631: case (2):
632: /*
633: * Possibly our architecture.
634: * If we're descending, keep tabs on it.
635: */
636: arch = NULL;
637: if (FTS_DP != ff->fts_info && NULL != dsec)
1.66 schwarze 638: arch = ff->fts_name;
1.50 kristaps 639: break;
640: default:
641: if (FTS_DP == ff->fts_info || use_all)
642: break;
1.56 schwarze 643: if (warnings)
1.59 schwarze 644: say(path, "Extraneous directory part");
1.50 kristaps 645: fts_set(f, ff, FTS_SKIP);
646: break;
1.5 kristaps 647: }
1.50 kristaps 648: }
649:
650: fts_close(f);
651: return(1);
652: }
1.5 kristaps 653:
1.50 kristaps 654: /*
655: * Add a file to the file vector.
656: * Do not verify that it's a "valid" looking manpage (we'll do that
657: * later).
658: *
659: * Try to infer the manual section, architecture, and page name from the
660: * path, assuming it looks like
661: *
662: * [./]man*[/<arch>]/<name>.<section>
663: * or
664: * [./]cat<section>[/<arch>]/<name>.0
665: *
1.78 schwarze 666: * Stuff this information directly into the mpage vector.
1.50 kristaps 667: * See treescan() for the fts(3) version of this.
668: */
669: static void
1.59 schwarze 670: filescan(const char *file)
1.50 kristaps 671: {
1.59 schwarze 672: char buf[PATH_MAX];
1.50 kristaps 673: const char *sec, *arch, *name, *dsec;
1.59 schwarze 674: char *p, *start;
1.50 kristaps 675: int dform;
676: struct stat st;
1.5 kristaps 677:
1.50 kristaps 678: assert(use_all);
1.5 kristaps 679:
1.50 kristaps 680: if (0 == strncmp(file, "./", 2))
681: file += 2;
1.5 kristaps 682:
1.59 schwarze 683: if (NULL == realpath(file, buf)) {
684: exitcode = (int)MANDOCLEVEL_BADARG;
685: say(file, NULL);
686: return;
1.72 schwarze 687: } else if (OP_TEST != op && strstr(buf, basedir) != buf) {
1.59 schwarze 688: exitcode = (int)MANDOCLEVEL_BADARG;
689: say("", "%s: outside base directory", buf);
690: return;
691: } else if (-1 == stat(buf, &st)) {
692: exitcode = (int)MANDOCLEVEL_BADARG;
693: say(file, NULL);
1.50 kristaps 694: return;
695: } else if ( ! (S_IFREG & st.st_mode)) {
1.59 schwarze 696: exitcode = (int)MANDOCLEVEL_BADARG;
697: say(file, "Not a regular file");
1.50 kristaps 698: return;
699: } else if (inocheck(&st)) {
1.56 schwarze 700: if (warnings)
1.59 schwarze 701: say(file, "Duplicate file");
1.50 kristaps 702: return;
703: }
1.59 schwarze 704: start = buf + strlen(basedir);
1.50 kristaps 705: sec = arch = name = dsec = NULL;
706: dform = FORM_NONE;
1.17 schwarze 707:
1.50 kristaps 708: /*
709: * First try to guess our directory structure.
710: * If we find a separator, try to look for man* or cat*.
711: * If we find one of these and what's underneath is a directory,
712: * assume it's an architecture.
713: */
714: if (NULL != (p = strchr(start, '/'))) {
715: *p++ = '\0';
716: if (0 == strncmp(start, "man", 3)) {
717: dform = FORM_SRC;
718: dsec = start + 3;
719: } else if (0 == strncmp(start, "cat", 3)) {
720: dform = FORM_CAT;
721: dsec = start + 3;
1.17 schwarze 722: }
1.5 kristaps 723:
1.50 kristaps 724: start = p;
725: if (NULL != dsec && NULL != (p = strchr(start, '/'))) {
726: *p++ = '\0';
727: arch = start;
728: start = p;
729: }
730: }
731:
732: /*
733: * Now check the file suffix.
734: * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
735: */
736: p = strrchr(start, '\0');
737: while (p-- > start && '/' != *p && '.' != *p)
738: /* Loop. */ ;
739:
740: if ('.' == *p) {
741: *p++ = '\0';
742: sec = p;
1.5 kristaps 743: }
744:
1.10 kristaps 745: /*
1.50 kristaps 746: * Now try to parse the name.
747: * Use the filename portion of the path.
1.10 kristaps 748: */
1.50 kristaps 749: name = start;
750: if (NULL != (p = strrchr(start, '/'))) {
751: name = p + 1;
752: *p = '\0';
753: }
754:
1.59 schwarze 755: ofadd(dform, file, name, dsec, sec, arch, &st);
1.50 kristaps 756: }
757:
758: /*
759: * See fileadd().
760: */
761: static int
762: filecheck(const char *name)
763: {
1.10 kristaps 764:
1.65 schwarze 765: return(NULL != ohash_find(&filenames,
766: ohash_qlookup(&filenames, name)));
1.50 kristaps 767: }
1.10 kristaps 768:
1.50 kristaps 769: /*
770: * Use the standard hashing mechanism (K&R) to see if the given filename
771: * already exists.
772: */
773: static void
1.78 schwarze 774: fileadd(struct mpage *mpage)
1.50 kristaps 775: {
1.65 schwarze 776: unsigned int slot;
1.1 kristaps 777:
1.78 schwarze 778: slot = ohash_qlookup(&filenames, mpage->file);
1.65 schwarze 779: assert(NULL == ohash_find(&filenames, slot));
1.78 schwarze 780: ohash_insert(&filenames, slot, mpage);
1.50 kristaps 781: }
1.3 kristaps 782:
1.50 kristaps 783: /*
784: * See inoadd().
785: */
786: static int
787: inocheck(const struct stat *st)
788: {
1.79 schwarze 789: struct inodev inodev;
1.50 kristaps 790: uint32_t hash;
791:
1.79 schwarze 792: memset(&inodev, 0, sizeof(inodev));
793: inodev.st_ino = hash = st->st_ino;
794: inodev.st_dev = st->st_dev;
1.13 schwarze 795:
1.78 schwarze 796: return(NULL != ohash_find(&mpages, ohash_lookup_memory(
1.79 schwarze 797: &mpages, (char *)&inodev, sizeof(inodev), hash)));
1.50 kristaps 798: }
1.5 kristaps 799:
1.50 kristaps 800: /*
801: * The hashing function used here is quite simple: simply take the inode
802: * and use uint32_t of its bits.
803: * Then when we do the lookup, use both the inode and device identifier.
804: */
805: static void
1.78 schwarze 806: inoadd(const struct stat *st, struct mpage *mpage)
1.50 kristaps 807: {
1.65 schwarze 808: unsigned int slot;
1.1 kristaps 809:
1.80 schwarze 810: mpage->inodev.st_ino = st->st_ino;
1.79 schwarze 811: mpage->inodev.st_dev = st->st_dev;
1.80 schwarze 812: slot = ohash_lookup_memory(&mpages, (char *)&mpage->inodev,
813: sizeof(struct inodev), st->st_ino);
1.1 kristaps 814:
1.78 schwarze 815: assert(NULL == ohash_find(&mpages, slot));
816: ohash_insert(&mpages, slot, mpage);
1.50 kristaps 817: }
1.35 kristaps 818:
1.50 kristaps 819: static void
1.59 schwarze 820: ofadd(int dform, const char *file, const char *name, const char *dsec,
821: const char *sec, const char *arch, const struct stat *st)
1.50 kristaps 822: {
1.78 schwarze 823: struct mpage *mpage;
1.50 kristaps 824: int sform;
825:
826: assert(NULL != file);
827:
828: if (NULL == name)
829: name = "";
830: if (NULL == sec)
831: sec = "";
832: if (NULL == dsec)
833: dsec = "";
834: if (NULL == arch)
835: arch = "";
836:
1.74 schwarze 837: if ('0' == *sec) {
1.50 kristaps 838: sec = dsec;
839: sform = FORM_CAT;
1.74 schwarze 840: } else if ('1' <= *sec && '9' >= *sec)
841: sform = FORM_SRC;
842: else
843: sform = FORM_NONE;
1.50 kristaps 844:
1.78 schwarze 845: mpage = mandoc_calloc(1, sizeof(struct mpage));
846: strlcpy(mpage->file, file, PATH_MAX);
847: mpage->name = mandoc_strdup(name);
848: mpage->sec = mandoc_strdup(sec);
849: mpage->dsec = mandoc_strdup(dsec);
850: mpage->arch = mandoc_strdup(arch);
851: mpage->sform = sform;
852: mpage->dform = dform;
1.3 kristaps 853:
1.50 kristaps 854: /*
855: * Add to unique identifier hash.
856: * Then if it's a source manual and we're going to use source in
857: * favour of catpages, add it to that hash.
858: */
1.78 schwarze 859: inoadd(st, mpage);
860: fileadd(mpage);
1.50 kristaps 861: }
1.3 kristaps 862:
1.50 kristaps 863: static void
1.78 schwarze 864: mpages_free(void)
1.50 kristaps 865: {
1.78 schwarze 866: struct mpage *mpage;
1.80 schwarze 867: unsigned int slot;
1.3 kristaps 868:
1.80 schwarze 869: mpage = ohash_first(&mpages, &slot);
870: while (NULL != mpage) {
1.78 schwarze 871: free(mpage->name);
872: free(mpage->sec);
873: free(mpage->dsec);
874: free(mpage->arch);
875: free(mpage);
1.80 schwarze 876: mpage = ohash_next(&mpages, &slot);
1.50 kristaps 877: }
878: }
1.38 schwarze 879:
1.50 kristaps 880: /*
1.80 schwarze 881: * Run through the files in the global vector "mpages"
882: * and add them to the database specified in "basedir".
1.50 kristaps 883: *
884: * This handles the parsing scheme itself, using the cues of directory
885: * and filename to determine whether the file is parsable or not.
886: */
1.59 schwarze 887: static void
1.78 schwarze 888: mpages_merge(struct mchars *mc, struct mparse *mp, int check_reachable)
1.50 kristaps 889: {
1.69 schwarze 890: struct ohash title_table;
1.71 schwarze 891: struct ohash_info title_info, str_info;
1.69 schwarze 892: char buf[PATH_MAX];
1.78 schwarze 893: struct mpage *mpage;
1.69 schwarze 894: struct mdoc *mdoc;
895: struct man *man;
896: struct title *title_entry;
897: char *bufp, *title_str;
898: const char *msec, *march, *mtitle, *cp;
899: size_t sz;
900: int form;
901: int match;
1.80 schwarze 902: unsigned int pslot, tslot;
1.69 schwarze 903: enum mandoclevel lvl;
904:
1.71 schwarze 905: str_info.alloc = hash_alloc;
906: str_info.halloc = hash_halloc;
907: str_info.hfree = hash_free;
908: str_info.key_offset = offsetof(struct str, key);
909:
1.69 schwarze 910: if (check_reachable) {
911: title_info.alloc = hash_alloc;
912: title_info.halloc = hash_halloc;
913: title_info.hfree = hash_free;
914: title_info.key_offset = offsetof(struct title, title);
915: ohash_init(&title_table, 6, &title_info);
916: }
1.50 kristaps 917:
1.80 schwarze 918: mpage = ohash_first(&mpages, &pslot);
919: while (NULL != mpage) {
1.50 kristaps 920: /*
921: * If we're a catpage (as defined by our path), then see
922: * if a manpage exists by the same name (ignoring the
923: * suffix).
924: * If it does, then we want to use it instead of our
925: * own.
926: */
1.78 schwarze 927: if ( ! use_all && FORM_CAT == mpage->dform) {
928: sz = strlcpy(buf, mpage->file, PATH_MAX);
1.58 schwarze 929: if (sz >= PATH_MAX) {
1.56 schwarze 930: if (warnings)
1.78 schwarze 931: say(mpage->file,
932: "Filename too long");
1.80 schwarze 933: mpage = ohash_next(&mpages, &pslot);
1.50 kristaps 934: continue;
935: }
936: bufp = strstr(buf, "cat");
937: assert(NULL != bufp);
938: memcpy(bufp, "man", 3);
939: if (NULL != (bufp = strrchr(buf, '.')))
940: *++bufp = '\0';
1.78 schwarze 941: strlcat(buf, mpage->dsec, PATH_MAX);
1.50 kristaps 942: if (filecheck(buf)) {
1.56 schwarze 943: if (warnings)
1.78 schwarze 944: say(mpage->file, "Man "
1.56 schwarze 945: "source exists: %s", buf);
1.80 schwarze 946: mpage = ohash_next(&mpages, &pslot);
1.50 kristaps 947: continue;
948: }
949: }
950:
1.71 schwarze 951: ohash_init(&strings, 6, &str_info);
1.50 kristaps 952: mparse_reset(mp);
953: mdoc = NULL;
954: man = NULL;
1.69 schwarze 955: match = 1;
1.14 schwarze 956:
957: /*
1.33 schwarze 958: * Try interpreting the file as mdoc(7) or man(7)
959: * source code, unless it is already known to be
960: * formatted. Fall back to formatted mode.
1.14 schwarze 961: */
1.78 schwarze 962: if (FORM_CAT != mpage->dform || FORM_CAT != mpage->sform) {
963: lvl = mparse_readfd(mp, -1, mpage->file);
1.50 kristaps 964: if (lvl < MANDOCLEVEL_FATAL)
965: mparse_result(mp, &mdoc, &man);
1.76 schwarze 966: }
1.14 schwarze 967:
968: if (NULL != mdoc) {
1.50 kristaps 969: form = 1;
1.14 schwarze 970: msec = mdoc_meta(mdoc)->msec;
1.38 schwarze 971: march = mdoc_meta(mdoc)->arch;
1.14 schwarze 972: mtitle = mdoc_meta(mdoc)->title;
973: } else if (NULL != man) {
1.50 kristaps 974: form = 1;
1.14 schwarze 975: msec = man_meta(man)->msec;
1.78 schwarze 976: march = mpage->arch;
1.14 schwarze 977: mtitle = man_meta(man)->title;
1.76 schwarze 978: } else {
979: form = 0;
1.78 schwarze 980: msec = mpage->dsec;
981: march = mpage->arch;
982: mtitle = mpage->name;
1.76 schwarze 983: }
1.50 kristaps 984:
1.76 schwarze 985: if (NULL == msec)
1.50 kristaps 986: msec = "";
1.76 schwarze 987: if (NULL == march)
1.50 kristaps 988: march = "";
1.76 schwarze 989: if (NULL == mtitle)
1.50 kristaps 990: mtitle = "";
1.1 kristaps 991:
1.12 schwarze 992: /*
1.44 kristaps 993: * Check whether the manual section given in a file
994: * agrees with the directory where the file is located.
995: * Some manuals have suffixes like (3p) on their
996: * section number either inside the file or in the
997: * directory name, some are linked into more than one
998: * section, like encrypt(1) = makekey(8). Do not skip
999: * manuals for such reasons.
1.12 schwarze 1000: */
1.56 schwarze 1001: if (warnings && !use_all && form &&
1.78 schwarze 1002: strcasecmp(msec, mpage->dsec)) {
1.69 schwarze 1003: match = 0;
1.78 schwarze 1004: say(mpage->file, "Section \"%s\" "
1005: "manual in %s directory",
1006: msec, mpage->dsec);
1.69 schwarze 1007: }
1.12 schwarze 1008:
1.42 schwarze 1009: /*
1010: * Manual page directories exist for each kernel
1011: * architecture as returned by machine(1).
1012: * However, many manuals only depend on the
1013: * application architecture as returned by arch(1).
1014: * For example, some (2/ARM) manuals are shared
1015: * across the "armish" and "zaurus" kernel
1016: * architectures.
1017: * A few manuals are even shared across completely
1018: * different architectures, for example fdformat(1)
1019: * on amd64, i386, sparc, and sparc64.
1020: * Thus, warn about architecture mismatches,
1021: * but don't skip manuals for this reason.
1022: */
1.78 schwarze 1023: if (warnings && !use_all && strcasecmp(march, mpage->arch)) {
1.69 schwarze 1024: match = 0;
1.78 schwarze 1025: say(mpage->file, "Architecture \"%s\" "
1.47 schwarze 1026: "manual in \"%s\" directory",
1.78 schwarze 1027: march, mpage->arch);
1.69 schwarze 1028: }
1.78 schwarze 1029: if (warnings && !use_all && strcasecmp(mtitle, mpage->name))
1.69 schwarze 1030: match = 0;
1.12 schwarze 1031:
1.78 schwarze 1032: putkey(mpage, mpage->name, TYPE_Nm);
1.12 schwarze 1033:
1.50 kristaps 1034: if (NULL != mdoc) {
1035: if (NULL != (cp = mdoc_meta(mdoc)->name))
1.78 schwarze 1036: putkey(mpage, cp, TYPE_Nm);
1037: assert(NULL == mpage->desc);
1038: parse_mdoc(mpage, mdoc_node(mdoc));
1039: putkey(mpage, NULL != mpage->desc ?
1040: mpage->desc : mpage->name, TYPE_Nd);
1.50 kristaps 1041: } else if (NULL != man)
1.78 schwarze 1042: parse_man(mpage, man_node(man));
1.50 kristaps 1043: else
1.78 schwarze 1044: parse_cat(mpage);
1.44 kristaps 1045:
1.69 schwarze 1046: /*
1047: * Build a title string for the file. If it matches
1048: * the location of the file, remember the title as
1049: * found; else, remember it as missing.
1050: */
1051:
1052: if (check_reachable) {
1053: if (-1 == asprintf(&title_str, "%s(%s%s%s)", mtitle,
1054: msec, '\0' == *march ? "" : "/", march)) {
1055: perror(NULL);
1056: exit((int)MANDOCLEVEL_SYSERR);
1057: }
1.80 schwarze 1058: tslot = ohash_qlookup(&title_table, title_str);
1059: title_entry = ohash_find(&title_table, tslot);
1.69 schwarze 1060: if (NULL == title_entry) {
1061: title_entry = mandoc_malloc(
1062: sizeof(struct title));
1063: title_entry->title = title_str;
1064: title_entry->file = mandoc_strdup(
1.78 schwarze 1065: match ? "" : mpage->file);
1.80 schwarze 1066: ohash_insert(&title_table, tslot,
1.69 schwarze 1067: title_entry);
1068: } else {
1069: if (match)
1070: *title_entry->file = '\0';
1071: free(title_str);
1072: }
1073: }
1074:
1.78 schwarze 1075: dbindex(mc, form, mpage);
1.66 schwarze 1076: ohash_delete(&strings);
1.80 schwarze 1077: mpage = ohash_next(&mpages, &pslot);
1.69 schwarze 1078: }
1079:
1080: if (check_reachable) {
1.80 schwarze 1081: title_entry = ohash_first(&title_table, &tslot);
1.69 schwarze 1082: while (NULL != title_entry) {
1083: if ('\0' != *title_entry->file)
1084: say(title_entry->file,
1085: "Probably unreachable, title is %s",
1086: title_entry->title);
1087: free(title_entry->title);
1088: free(title_entry->file);
1089: free(title_entry);
1.80 schwarze 1090: title_entry = ohash_next(&title_table, &tslot);
1.69 schwarze 1091: }
1092: ohash_delete(&title_table);
1.50 kristaps 1093: }
1094: }
1.12 schwarze 1095:
1.50 kristaps 1096: static void
1.78 schwarze 1097: parse_cat(struct mpage *mpage)
1.50 kristaps 1098: {
1099: FILE *stream;
1100: char *line, *p, *title;
1101: size_t len, plen, titlesz;
1.12 schwarze 1102:
1.78 schwarze 1103: if (NULL == (stream = fopen(mpage->file, "r"))) {
1.56 schwarze 1104: if (warnings)
1.78 schwarze 1105: say(mpage->file, NULL);
1.50 kristaps 1106: return;
1107: }
1.1 kristaps 1108:
1.50 kristaps 1109: /* Skip to first blank line. */
1.1 kristaps 1110:
1.50 kristaps 1111: while (NULL != (line = fgetln(stream, &len)))
1112: if ('\n' == *line)
1113: break;
1.1 kristaps 1114:
1.50 kristaps 1115: /*
1116: * Assume the first line that is not indented
1117: * is the first section header. Skip to it.
1118: */
1.1 kristaps 1119:
1.50 kristaps 1120: while (NULL != (line = fgetln(stream, &len)))
1121: if ('\n' != *line && ' ' != *line)
1122: break;
1123:
1124: /*
1125: * Read up until the next section into a buffer.
1126: * Strip the leading and trailing newline from each read line,
1127: * appending a trailing space.
1128: * Ignore empty (whitespace-only) lines.
1129: */
1.1 kristaps 1130:
1.50 kristaps 1131: titlesz = 0;
1132: title = NULL;
1.38 schwarze 1133:
1.50 kristaps 1134: while (NULL != (line = fgetln(stream, &len))) {
1135: if (' ' != *line || '\n' != line[len - 1])
1136: break;
1137: while (len > 0 && isspace((unsigned char)*line)) {
1138: line++;
1139: len--;
1140: }
1141: if (1 == len)
1.38 schwarze 1142: continue;
1.50 kristaps 1143: title = mandoc_realloc(title, titlesz + len);
1144: memcpy(title + titlesz, line, len);
1145: titlesz += len;
1146: title[titlesz - 1] = ' ';
1147: }
1.38 schwarze 1148:
1.50 kristaps 1149: /*
1150: * If no page content can be found, or the input line
1151: * is already the next section header, or there is no
1152: * trailing newline, reuse the page title as the page
1153: * description.
1154: */
1.44 kristaps 1155:
1.50 kristaps 1156: if (NULL == title || '\0' == *title) {
1.56 schwarze 1157: if (warnings)
1.78 schwarze 1158: say(mpage->file, "Cannot find NAME section");
1159: assert(NULL == mpage->desc);
1160: mpage->desc = mandoc_strdup(mpage->name);
1161: putkey(mpage, mpage->name, TYPE_Nd);
1.50 kristaps 1162: fclose(stream);
1163: free(title);
1164: return;
1165: }
1.1 kristaps 1166:
1.50 kristaps 1167: title = mandoc_realloc(title, titlesz + 1);
1168: title[titlesz] = '\0';
1.33 schwarze 1169:
1.50 kristaps 1170: /*
1171: * Skip to the first dash.
1172: * Use the remaining line as the description (no more than 70
1173: * bytes).
1174: */
1.33 schwarze 1175:
1.50 kristaps 1176: if (NULL != (p = strstr(title, "- "))) {
1177: for (p += 2; ' ' == *p || '\b' == *p; p++)
1178: /* Skip to next word. */ ;
1179: } else {
1.56 schwarze 1180: if (warnings)
1.78 schwarze 1181: say(mpage->file, "No dash in title line");
1.50 kristaps 1182: p = title;
1183: }
1.38 schwarze 1184:
1.50 kristaps 1185: plen = strlen(p);
1.1 kristaps 1186:
1.50 kristaps 1187: /* Strip backspace-encoding from line. */
1.1 kristaps 1188:
1.50 kristaps 1189: while (NULL != (line = memchr(p, '\b', plen))) {
1190: len = line - p;
1191: if (0 == len) {
1192: memmove(line, line + 1, plen--);
1193: continue;
1194: }
1195: memmove(line - 1, line + 1, plen - len);
1196: plen -= 2;
1197: }
1.1 kristaps 1198:
1.78 schwarze 1199: assert(NULL == mpage->desc);
1200: mpage->desc = mandoc_strdup(p);
1201: putkey(mpage, mpage->desc, TYPE_Nd);
1.50 kristaps 1202: fclose(stream);
1203: free(title);
1204: }
1.1 kristaps 1205:
1.50 kristaps 1206: /*
1207: * Put a type/word pair into the word database for this particular file.
1208: */
1209: static void
1.78 schwarze 1210: putkey(const struct mpage *mpage, const char *value, uint64_t type)
1.50 kristaps 1211: {
1.18 kristaps 1212:
1.50 kristaps 1213: assert(NULL != value);
1.78 schwarze 1214: putkeys(mpage, value, strlen(value), type);
1.3 kristaps 1215: }
1216:
1217: /*
1.50 kristaps 1218: * Grok all nodes at or below a certain mdoc node into putkey().
1.3 kristaps 1219: */
1220: static void
1.78 schwarze 1221: putmdockey(const struct mpage *mpage,
1222: const struct mdoc_node *n, uint64_t m)
1.3 kristaps 1223: {
1.18 kristaps 1224:
1.50 kristaps 1225: for ( ; NULL != n; n = n->next) {
1226: if (NULL != n->child)
1.78 schwarze 1227: putmdockey(mpage, n->child, m);
1.50 kristaps 1228: if (MDOC_TEXT == n->type)
1.78 schwarze 1229: putkey(mpage, n->string, m);
1.50 kristaps 1230: }
1231: }
1.18 kristaps 1232:
1.61 schwarze 1233: static void
1.78 schwarze 1234: parse_man(struct mpage *mpage, const struct man_node *n)
1.50 kristaps 1235: {
1236: const struct man_node *head, *body;
1237: char *start, *sv, *title;
1238: char byte;
1239: size_t sz, titlesz;
1.18 kristaps 1240:
1.50 kristaps 1241: if (NULL == n)
1.61 schwarze 1242: return;
1.18 kristaps 1243:
1.50 kristaps 1244: /*
1245: * We're only searching for one thing: the first text child in
1246: * the BODY of a NAME section. Since we don't keep track of
1247: * sections in -man, run some hoops to find out whether we're in
1248: * the correct section or not.
1249: */
1.18 kristaps 1250:
1.50 kristaps 1251: if (MAN_BODY == n->type && MAN_SH == n->tok) {
1252: body = n;
1253: assert(body->parent);
1254: if (NULL != (head = body->parent->head) &&
1255: 1 == head->nchild &&
1256: NULL != (head = (head->child)) &&
1257: MAN_TEXT == head->type &&
1258: 0 == strcmp(head->string, "NAME") &&
1259: NULL != (body = body->child) &&
1260: MAN_TEXT == body->type) {
1.3 kristaps 1261:
1.50 kristaps 1262: title = NULL;
1263: titlesz = 0;
1.3 kristaps 1264:
1.50 kristaps 1265: /*
1266: * Suck the entire NAME section into memory.
1267: * Yes, we might run away.
1268: * But too many manuals have big, spread-out
1269: * NAME sections over many lines.
1270: */
1.3 kristaps 1271:
1.50 kristaps 1272: for ( ; NULL != body; body = body->next) {
1273: if (MAN_TEXT != body->type)
1274: break;
1275: if (0 == (sz = strlen(body->string)))
1276: continue;
1277: title = mandoc_realloc
1278: (title, titlesz + sz + 1);
1279: memcpy(title + titlesz, body->string, sz);
1280: titlesz += sz + 1;
1281: title[titlesz - 1] = ' ';
1282: }
1283: if (NULL == title)
1.61 schwarze 1284: return;
1.18 kristaps 1285:
1.50 kristaps 1286: title = mandoc_realloc(title, titlesz + 1);
1287: title[titlesz] = '\0';
1.18 kristaps 1288:
1.50 kristaps 1289: /* Skip leading space. */
1.18 kristaps 1290:
1.50 kristaps 1291: sv = title;
1292: while (isspace((unsigned char)*sv))
1293: sv++;
1.18 kristaps 1294:
1.50 kristaps 1295: if (0 == (sz = strlen(sv))) {
1296: free(title);
1.61 schwarze 1297: return;
1.50 kristaps 1298: }
1.1 kristaps 1299:
1.50 kristaps 1300: /* Erase trailing space. */
1.1 kristaps 1301:
1.50 kristaps 1302: start = &sv[sz - 1];
1303: while (start > sv && isspace((unsigned char)*start))
1304: *start-- = '\0';
1.1 kristaps 1305:
1.50 kristaps 1306: if (start == sv) {
1307: free(title);
1.61 schwarze 1308: return;
1.50 kristaps 1309: }
1.1 kristaps 1310:
1.50 kristaps 1311: start = sv;
1.18 kristaps 1312:
1.50 kristaps 1313: /*
1314: * Go through a special heuristic dance here.
1315: * Conventionally, one or more manual names are
1316: * comma-specified prior to a whitespace, then a
1317: * dash, then a description. Try to puzzle out
1318: * the name parts here.
1319: */
1.18 kristaps 1320:
1.50 kristaps 1321: for ( ;; ) {
1322: sz = strcspn(start, " ,");
1323: if ('\0' == start[sz])
1324: break;
1.1 kristaps 1325:
1.50 kristaps 1326: byte = start[sz];
1327: start[sz] = '\0';
1.1 kristaps 1328:
1.78 schwarze 1329: putkey(mpage, start, TYPE_Nm);
1.1 kristaps 1330:
1.50 kristaps 1331: if (' ' == byte) {
1332: start += sz + 1;
1333: break;
1334: }
1.1 kristaps 1335:
1.50 kristaps 1336: assert(',' == byte);
1337: start += sz + 1;
1338: while (' ' == *start)
1339: start++;
1340: }
1.1 kristaps 1341:
1.50 kristaps 1342: if (sv == start) {
1.78 schwarze 1343: putkey(mpage, start, TYPE_Nm);
1.50 kristaps 1344: free(title);
1.61 schwarze 1345: return;
1.50 kristaps 1346: }
1.1 kristaps 1347:
1.50 kristaps 1348: while (isspace((unsigned char)*start))
1349: start++;
1.1 kristaps 1350:
1.50 kristaps 1351: if (0 == strncmp(start, "-", 1))
1352: start += 1;
1353: else if (0 == strncmp(start, "\\-\\-", 4))
1354: start += 4;
1355: else if (0 == strncmp(start, "\\-", 2))
1356: start += 2;
1357: else if (0 == strncmp(start, "\\(en", 4))
1358: start += 4;
1359: else if (0 == strncmp(start, "\\(em", 4))
1360: start += 4;
1.1 kristaps 1361:
1.50 kristaps 1362: while (' ' == *start)
1363: start++;
1.1 kristaps 1364:
1.78 schwarze 1365: assert(NULL == mpage->desc);
1366: mpage->desc = mandoc_strdup(start);
1367: putkey(mpage, mpage->desc, TYPE_Nd);
1.50 kristaps 1368: free(title);
1.61 schwarze 1369: return;
1.50 kristaps 1370: }
1371: }
1.1 kristaps 1372:
1.77 schwarze 1373: for (n = n->child; n; n = n->next) {
1.78 schwarze 1374: if (NULL != mpage->desc)
1.77 schwarze 1375: break;
1.78 schwarze 1376: parse_man(mpage, n);
1.77 schwarze 1377: }
1.1 kristaps 1378: }
1379:
1380: static void
1.78 schwarze 1381: parse_mdoc(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1382: {
1383:
1.50 kristaps 1384: assert(NULL != n);
1385: for (n = n->child; NULL != n; n = n->next) {
1386: switch (n->type) {
1387: case (MDOC_ELEM):
1388: /* FALLTHROUGH */
1389: case (MDOC_BLOCK):
1390: /* FALLTHROUGH */
1391: case (MDOC_HEAD):
1392: /* FALLTHROUGH */
1393: case (MDOC_BODY):
1394: /* FALLTHROUGH */
1395: case (MDOC_TAIL):
1396: if (NULL != mdocs[n->tok].fp)
1.78 schwarze 1397: if (0 == (*mdocs[n->tok].fp)(mpage, n))
1.50 kristaps 1398: break;
1.68 schwarze 1399: if (mdocs[n->tok].mask)
1.78 schwarze 1400: putmdockey(mpage, n->child,
1401: mdocs[n->tok].mask);
1.50 kristaps 1402: break;
1403: default:
1404: assert(MDOC_ROOT != n->type);
1405: continue;
1406: }
1407: if (NULL != n->child)
1.78 schwarze 1408: parse_mdoc(mpage, n);
1.1 kristaps 1409: }
1410: }
1411:
1.25 schwarze 1412: static int
1.78 schwarze 1413: parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1414: {
1415: const char *start, *end;
1416: size_t sz;
1.25 schwarze 1417:
1.50 kristaps 1418: if (SEC_SYNOPSIS != n->sec ||
1419: NULL == (n = n->child) ||
1420: MDOC_TEXT != n->type)
1.25 schwarze 1421: return(0);
1.1 kristaps 1422:
1423: /*
1424: * Only consider those `Fd' macro fields that begin with an
1425: * "inclusion" token (versus, e.g., #define).
1426: */
1.50 kristaps 1427:
1.1 kristaps 1428: if (strcmp("#include", n->string))
1.25 schwarze 1429: return(0);
1.1 kristaps 1430:
1431: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.25 schwarze 1432: return(0);
1.1 kristaps 1433:
1434: /*
1435: * Strip away the enclosing angle brackets and make sure we're
1436: * not zero-length.
1437: */
1438:
1439: start = n->string;
1440: if ('<' == *start || '"' == *start)
1441: start++;
1442:
1443: if (0 == (sz = strlen(start)))
1.25 schwarze 1444: return(0);
1.1 kristaps 1445:
1446: end = &start[(int)sz - 1];
1447: if ('>' == *end || '"' == *end)
1448: end--;
1449:
1.50 kristaps 1450: if (end > start)
1.78 schwarze 1451: putkeys(mpage, start, end - start + 1, TYPE_In);
1.25 schwarze 1452: return(1);
1.1 kristaps 1453: }
1454:
1.25 schwarze 1455: static int
1.78 schwarze 1456: parse_mdoc_In(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1457: {
1458:
1.50 kristaps 1459: if (NULL != n->child && MDOC_TEXT == n->child->type)
1.25 schwarze 1460: return(0);
1.1 kristaps 1461:
1.78 schwarze 1462: putkey(mpage, n->child->string, TYPE_In);
1.25 schwarze 1463: return(1);
1.1 kristaps 1464: }
1465:
1.25 schwarze 1466: static int
1.78 schwarze 1467: parse_mdoc_Fn(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1468: {
1469: const char *cp;
1470:
1.50 kristaps 1471: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.25 schwarze 1472: return(0);
1473:
1.50 kristaps 1474: /*
1475: * Parse: .Fn "struct type *name" "char *arg".
1476: * First strip away pointer symbol.
1477: * Then store the function name, then type.
1478: * Finally, store the arguments.
1479: */
1.1 kristaps 1480:
1.50 kristaps 1481: if (NULL == (cp = strrchr(n->string, ' ')))
1482: cp = n->string;
1.1 kristaps 1483:
1484: while ('*' == *cp)
1485: cp++;
1486:
1.78 schwarze 1487: putkey(mpage, cp, TYPE_Fn);
1.25 schwarze 1488:
1.50 kristaps 1489: if (n->string < cp)
1.78 schwarze 1490: putkeys(mpage, n->string, cp - n->string, TYPE_Ft);
1.25 schwarze 1491:
1.50 kristaps 1492: for (n = n->next; NULL != n; n = n->next)
1493: if (MDOC_TEXT == n->type)
1.78 schwarze 1494: putkey(mpage, n->string, TYPE_Fa);
1.25 schwarze 1495:
1496: return(0);
1.1 kristaps 1497: }
1498:
1.25 schwarze 1499: static int
1.78 schwarze 1500: parse_mdoc_St(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1501: {
1.25 schwarze 1502:
1.1 kristaps 1503: if (NULL == n->child || MDOC_TEXT != n->child->type)
1.25 schwarze 1504: return(0);
1.1 kristaps 1505:
1.78 schwarze 1506: putkey(mpage, n->child->string, TYPE_St);
1.25 schwarze 1507: return(1);
1.1 kristaps 1508: }
1509:
1.25 schwarze 1510: static int
1.78 schwarze 1511: parse_mdoc_Xr(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1512: {
1.67 schwarze 1513: char *cp;
1.1 kristaps 1514:
1515: if (NULL == (n = n->child))
1.25 schwarze 1516: return(0);
1.1 kristaps 1517:
1.67 schwarze 1518: if (NULL == n->next) {
1.78 schwarze 1519: putkey(mpage, n->string, TYPE_Xr);
1.67 schwarze 1520: return(0);
1521: }
1522:
1523: if (-1 == asprintf(&cp, "%s(%s)", n->string, n->next->string)) {
1524: perror(NULL);
1525: exit((int)MANDOCLEVEL_SYSERR);
1526: }
1.78 schwarze 1527: putkey(mpage, cp, TYPE_Xr);
1.67 schwarze 1528: free(cp);
1529: return(0);
1.1 kristaps 1530: }
1531:
1.25 schwarze 1532: static int
1.78 schwarze 1533: parse_mdoc_Nd(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1534: {
1.50 kristaps 1535: size_t sz;
1.1 kristaps 1536:
1537: if (MDOC_BODY != n->type)
1.25 schwarze 1538: return(0);
1.1 kristaps 1539:
1.50 kristaps 1540: /*
1541: * Special-case the `Nd' because we need to put the description
1542: * into the document table.
1543: */
1544:
1545: for (n = n->child; NULL != n; n = n->next) {
1546: if (MDOC_TEXT == n->type) {
1.78 schwarze 1547: if (NULL != mpage->desc) {
1548: sz = strlen(mpage->desc) +
1.66 schwarze 1549: strlen(n->string) + 2;
1.78 schwarze 1550: mpage->desc = mandoc_realloc(
1551: mpage->desc, sz);
1552: strlcat(mpage->desc, " ", sz);
1553: strlcat(mpage->desc, n->string, sz);
1.66 schwarze 1554: } else
1.78 schwarze 1555: mpage->desc = mandoc_strdup(n->string);
1.50 kristaps 1556: }
1557: if (NULL != n->child)
1.78 schwarze 1558: parse_mdoc_Nd(mpage, n);
1.50 kristaps 1559: }
1.25 schwarze 1560: return(1);
1.1 kristaps 1561: }
1562:
1.25 schwarze 1563: static int
1.78 schwarze 1564: parse_mdoc_Nm(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1565: {
1566:
1.25 schwarze 1567: if (SEC_NAME == n->sec)
1568: return(1);
1569: else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
1570: return(0);
1.1 kristaps 1571:
1.25 schwarze 1572: return(1);
1.1 kristaps 1573: }
1574:
1.25 schwarze 1575: static int
1.78 schwarze 1576: parse_mdoc_Sh(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1577: {
1578:
1.25 schwarze 1579: return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1 kristaps 1580: }
1581:
1.50 kristaps 1582: static int
1.78 schwarze 1583: parse_mdoc_head(struct mpage *mpage, const struct mdoc_node *n)
1.1 kristaps 1584: {
1585:
1.50 kristaps 1586: return(MDOC_HEAD == n->type);
1587: }
1.1 kristaps 1588:
1.50 kristaps 1589: static int
1.78 schwarze 1590: parse_mdoc_body(struct mpage *mpage, const struct mdoc_node *n)
1.50 kristaps 1591: {
1.1 kristaps 1592:
1.50 kristaps 1593: return(MDOC_BODY == n->type);
1.1 kristaps 1594: }
1595:
1.50 kristaps 1596: /*
1.66 schwarze 1597: * Add a string to the hash table for the current manual.
1598: * Each string has a bitmask telling which macros it belongs to.
1599: * When we finish the manual, we'll dump the table.
1.50 kristaps 1600: */
1601: static void
1.78 schwarze 1602: putkeys(const struct mpage *mpage,
1603: const char *cp, size_t sz, uint64_t v)
1.50 kristaps 1604: {
1605: struct str *s;
1.65 schwarze 1606: unsigned int slot;
1.50 kristaps 1607: const char *end;
1.25 schwarze 1608:
1.50 kristaps 1609: if (0 == sz)
1610: return;
1.25 schwarze 1611:
1.65 schwarze 1612: end = cp + sz;
1613: slot = ohash_qlookupi(&strings, cp, &end);
1614: s = ohash_find(&strings, slot);
1.25 schwarze 1615:
1.78 schwarze 1616: if (NULL != s && mpage == s->mpage) {
1.50 kristaps 1617: s->mask |= v;
1618: return;
1619: } else if (NULL == s) {
1.51 kristaps 1620: s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
1.50 kristaps 1621: memcpy(s->key, cp, sz);
1.65 schwarze 1622: ohash_insert(&strings, slot, s);
1.1 kristaps 1623: }
1.78 schwarze 1624: s->mpage = mpage;
1.50 kristaps 1625: s->mask = v;
1.1 kristaps 1626: }
1627:
1.50 kristaps 1628: /*
1629: * Take a Unicode codepoint and produce its UTF-8 encoding.
1630: * This isn't the best way to do this, but it works.
1631: * The magic numbers are from the UTF-8 packaging.
1632: * They're not as scary as they seem: read the UTF-8 spec for details.
1633: */
1634: static size_t
1635: utf8(unsigned int cp, char out[7])
1.1 kristaps 1636: {
1.50 kristaps 1637: size_t rc;
1.1 kristaps 1638:
1.50 kristaps 1639: rc = 0;
1640: if (cp <= 0x0000007F) {
1641: rc = 1;
1642: out[0] = (char)cp;
1643: } else if (cp <= 0x000007FF) {
1644: rc = 2;
1645: out[0] = (cp >> 6 & 31) | 192;
1646: out[1] = (cp & 63) | 128;
1647: } else if (cp <= 0x0000FFFF) {
1648: rc = 3;
1649: out[0] = (cp >> 12 & 15) | 224;
1650: out[1] = (cp >> 6 & 63) | 128;
1651: out[2] = (cp & 63) | 128;
1652: } else if (cp <= 0x001FFFFF) {
1653: rc = 4;
1654: out[0] = (cp >> 18 & 7) | 240;
1655: out[1] = (cp >> 12 & 63) | 128;
1656: out[2] = (cp >> 6 & 63) | 128;
1657: out[3] = (cp & 63) | 128;
1658: } else if (cp <= 0x03FFFFFF) {
1659: rc = 5;
1660: out[0] = (cp >> 24 & 3) | 248;
1661: out[1] = (cp >> 18 & 63) | 128;
1662: out[2] = (cp >> 12 & 63) | 128;
1663: out[3] = (cp >> 6 & 63) | 128;
1664: out[4] = (cp & 63) | 128;
1665: } else if (cp <= 0x7FFFFFFF) {
1666: rc = 6;
1667: out[0] = (cp >> 30 & 1) | 252;
1668: out[1] = (cp >> 24 & 63) | 128;
1669: out[2] = (cp >> 18 & 63) | 128;
1670: out[3] = (cp >> 12 & 63) | 128;
1671: out[4] = (cp >> 6 & 63) | 128;
1672: out[5] = (cp & 63) | 128;
1673: } else
1.1 kristaps 1674: return(0);
1675:
1.50 kristaps 1676: out[rc] = '\0';
1677: return(rc);
1678: }
1.1 kristaps 1679:
1.50 kristaps 1680: /*
1681: * Store the UTF-8 version of a key, or alias the pointer if the key has
1682: * no UTF-8 transcription marks in it.
1683: */
1684: static void
1685: utf8key(struct mchars *mc, struct str *key)
1686: {
1687: size_t sz, bsz, pos;
1688: char utfbuf[7], res[5];
1689: char *buf;
1690: const char *seq, *cpp, *val;
1691: int len, u;
1692: enum mandoc_esc esc;
1693:
1694: assert(NULL == key->utf8);
1695:
1696: res[0] = '\\';
1697: res[1] = '\t';
1698: res[2] = ASCII_NBRSP;
1699: res[3] = ASCII_HYPH;
1700: res[4] = '\0';
1.1 kristaps 1701:
1.50 kristaps 1702: val = key->key;
1703: bsz = strlen(val);
1.46 kristaps 1704:
1.50 kristaps 1705: /*
1706: * Pre-check: if we have no stop-characters, then set the
1707: * pointer as ourselvse and get out of here.
1708: */
1709: if (strcspn(val, res) == bsz) {
1710: key->utf8 = key->key;
1711: return;
1712: }
1.46 kristaps 1713:
1.50 kristaps 1714: /* Pre-allocate by the length of the input */
1.46 kristaps 1715:
1.50 kristaps 1716: buf = mandoc_malloc(++bsz);
1717: pos = 0;
1.46 kristaps 1718:
1.50 kristaps 1719: while ('\0' != *val) {
1720: /*
1721: * Halt on the first escape sequence.
1722: * This also halts on the end of string, in which case
1723: * we just copy, fallthrough, and exit the loop.
1724: */
1725: if ((sz = strcspn(val, res)) > 0) {
1726: memcpy(&buf[pos], val, sz);
1727: pos += sz;
1728: val += sz;
1729: }
1.46 kristaps 1730:
1.50 kristaps 1731: if (ASCII_HYPH == *val) {
1732: buf[pos++] = '-';
1733: val++;
1734: continue;
1735: } else if ('\t' == *val || ASCII_NBRSP == *val) {
1736: buf[pos++] = ' ';
1737: val++;
1738: continue;
1739: } else if ('\\' != *val)
1740: break;
1.46 kristaps 1741:
1.50 kristaps 1742: /* Read past the slash. */
1.46 kristaps 1743:
1.50 kristaps 1744: val++;
1745: u = 0;
1.46 kristaps 1746:
1.50 kristaps 1747: /*
1748: * Parse the escape sequence and see if it's a
1749: * predefined character or special character.
1750: */
1751: esc = mandoc_escape
1752: ((const char **)&val, &seq, &len);
1753: if (ESCAPE_ERROR == esc)
1754: break;
1.1 kristaps 1755:
1.50 kristaps 1756: if (ESCAPE_SPECIAL != esc)
1757: continue;
1758: if (0 == (u = mchars_spec2cp(mc, seq, len)))
1759: continue;
1.1 kristaps 1760:
1.50 kristaps 1761: /*
1762: * If we have a Unicode codepoint, try to convert that
1763: * to a UTF-8 byte string.
1764: */
1765: cpp = utfbuf;
1766: if (0 == (sz = utf8(u, utfbuf)))
1767: continue;
1.1 kristaps 1768:
1.50 kristaps 1769: /* Copy the rendered glyph into the stream. */
1.1 kristaps 1770:
1.50 kristaps 1771: sz = strlen(cpp);
1772: bsz += sz;
1.1 kristaps 1773:
1.50 kristaps 1774: buf = mandoc_realloc(buf, bsz);
1.1 kristaps 1775:
1.50 kristaps 1776: memcpy(&buf[pos], cpp, sz);
1777: pos += sz;
1.1 kristaps 1778: }
1779:
1.50 kristaps 1780: buf[pos] = '\0';
1781: key->utf8 = buf;
1.1 kristaps 1782: }
1783:
1.14 schwarze 1784: /*
1.50 kristaps 1785: * Flush the current page's terms (and their bits) into the database.
1786: * Wrap the entire set of additions in a transaction to make sqlite be a
1787: * little faster.
1788: * Also, UTF-8-encode the description at the last possible moment.
1.14 schwarze 1789: */
1790: static void
1.78 schwarze 1791: dbindex(struct mchars *mc, int form, const struct mpage *mpage)
1.14 schwarze 1792: {
1.50 kristaps 1793: struct str *key;
1794: const char *desc;
1795: int64_t recno;
1.52 kristaps 1796: size_t i;
1.66 schwarze 1797: unsigned int slot;
1.14 schwarze 1798:
1.56 schwarze 1799: if (verb)
1.78 schwarze 1800: say(mpage->file, "Adding to index");
1.43 kristaps 1801:
1.50 kristaps 1802: if (nodb)
1.14 schwarze 1803: return;
1.28 kristaps 1804:
1.50 kristaps 1805: desc = "";
1.78 schwarze 1806: if (NULL != mpage->desc && '\0' != *mpage->desc) {
1.65 schwarze 1807: key = ohash_find(&strings,
1.78 schwarze 1808: ohash_qlookup(&strings, mpage->desc));
1.50 kristaps 1809: assert(NULL != key);
1810: if (NULL == key->utf8)
1811: utf8key(mc, key);
1812: desc = key->utf8;
1813: }
1814:
1.52 kristaps 1815: SQL_EXEC("BEGIN TRANSACTION");
1.50 kristaps 1816:
1.52 kristaps 1817: i = 1;
1.81 ! schwarze 1818: /*
! 1819: * XXX The following three lines are obsolete
! 1820: * and only kept for backward compatibility
! 1821: * until apropos(1) and friends have caught up.
! 1822: */
! 1823: SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, mpage->file);
! 1824: SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, mpage->sec);
! 1825: SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, mpage->arch);
! 1826: SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, desc);
! 1827: SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, form);
! 1828: SQL_STEP(stmts[STMT_INSERT_PAGE]);
1.50 kristaps 1829: recno = sqlite3_last_insert_rowid(db);
1.81 ! schwarze 1830: sqlite3_reset(stmts[STMT_INSERT_PAGE]);
! 1831:
! 1832: i = 1;
! 1833: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mpage->sec);
! 1834: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mpage->arch);
! 1835: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mpage->file);
! 1836: SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, recno);
! 1837: SQL_STEP(stmts[STMT_INSERT_LINK]);
! 1838: sqlite3_reset(stmts[STMT_INSERT_LINK]);
1.50 kristaps 1839:
1.66 schwarze 1840: for (key = ohash_first(&strings, &slot); NULL != key;
1841: key = ohash_next(&strings, &slot)) {
1.78 schwarze 1842: assert(key->mpage == mpage);
1.50 kristaps 1843: if (NULL == key->utf8)
1844: utf8key(mc, key);
1.52 kristaps 1845: i = 1;
1846: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);
1847: SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->utf8);
1848: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, recno);
1849: SQL_STEP(stmts[STMT_INSERT_KEY]);
1.50 kristaps 1850: sqlite3_reset(stmts[STMT_INSERT_KEY]);
1.66 schwarze 1851: if (key->utf8 != key->key)
1852: free(key->utf8);
1853: free(key);
1.38 schwarze 1854: }
1.28 kristaps 1855:
1.52 kristaps 1856: SQL_EXEC("END TRANSACTION");
1.14 schwarze 1857: }
1858:
1.5 kristaps 1859: static void
1.59 schwarze 1860: dbprune(void)
1.5 kristaps 1861: {
1.78 schwarze 1862: struct mpage *mpage;
1.52 kristaps 1863: size_t i;
1.80 schwarze 1864: unsigned int slot;
1.5 kristaps 1865:
1.50 kristaps 1866: if (nodb)
1867: return;
1.12 schwarze 1868:
1.80 schwarze 1869: mpage = ohash_first(&mpages, &slot);
1870: while (NULL != mpage) {
1.52 kristaps 1871: i = 1;
1.81 ! schwarze 1872: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE], i, mpage->file);
! 1873: SQL_STEP(stmts[STMT_DELETE_PAGE]);
! 1874: sqlite3_reset(stmts[STMT_DELETE_PAGE]);
1.56 schwarze 1875: if (verb)
1.78 schwarze 1876: say(mpage->file, "Deleted from index");
1.80 schwarze 1877: mpage = ohash_next(&mpages, &slot);
1.5 kristaps 1878: }
1879: }
1880:
1.4 kristaps 1881: /*
1.50 kristaps 1882: * Close an existing database and its prepared statements.
1883: * If "real" is not set, rename the temporary file into the real one.
1.4 kristaps 1884: */
1.35 kristaps 1885: static void
1.59 schwarze 1886: dbclose(int real)
1.4 kristaps 1887: {
1.50 kristaps 1888: size_t i;
1.4 kristaps 1889:
1.50 kristaps 1890: if (nodb)
1.38 schwarze 1891: return;
1.50 kristaps 1892:
1893: for (i = 0; i < STMT__MAX; i++) {
1894: sqlite3_finalize(stmts[i]);
1895: stmts[i] = NULL;
1.4 kristaps 1896: }
1897:
1.50 kristaps 1898: sqlite3_close(db);
1899: db = NULL;
1.12 schwarze 1900:
1.50 kristaps 1901: if (real)
1902: return;
1.12 schwarze 1903:
1.63 schwarze 1904: if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {
1.59 schwarze 1905: exitcode = (int)MANDOCLEVEL_SYSERR;
1906: say(MANDOC_DB, NULL);
1907: }
1.50 kristaps 1908: }
1.14 schwarze 1909:
1.50 kristaps 1910: /*
1911: * This is straightforward stuff.
1912: * Open a database connection to a "temporary" database, then open a set
1913: * of prepared statements we'll use over and over again.
1914: * If "real" is set, we use the existing database; if not, we truncate a
1915: * temporary one.
1916: * Must be matched by dbclose().
1917: */
1918: static int
1.59 schwarze 1919: dbopen(int real)
1.50 kristaps 1920: {
1.63 schwarze 1921: const char *file, *sql;
1.50 kristaps 1922: int rc, ofl;
1.12 schwarze 1923:
1.50 kristaps 1924: if (nodb)
1925: return(1);
1.12 schwarze 1926:
1.63 schwarze 1927: ofl = SQLITE_OPEN_READWRITE;
1928: if (0 == real) {
1929: file = MANDOC_DB "~";
1930: if (-1 == remove(file) && ENOENT != errno) {
1931: exitcode = (int)MANDOCLEVEL_SYSERR;
1932: say(file, NULL);
1933: return(0);
1934: }
1935: ofl |= SQLITE_OPEN_EXCLUSIVE;
1936: } else
1937: file = MANDOC_DB;
1.45 kristaps 1938:
1.50 kristaps 1939: rc = sqlite3_open_v2(file, &db, ofl, NULL);
1940: if (SQLITE_OK == rc)
1.57 schwarze 1941: goto prepare_statements;
1.50 kristaps 1942: if (SQLITE_CANTOPEN != rc) {
1.59 schwarze 1943: exitcode = (int)MANDOCLEVEL_SYSERR;
1944: say(file, NULL);
1.50 kristaps 1945: return(0);
1946: }
1.12 schwarze 1947:
1.50 kristaps 1948: sqlite3_close(db);
1949: db = NULL;
1.12 schwarze 1950:
1.50 kristaps 1951: if (SQLITE_OK != (rc = sqlite3_open(file, &db))) {
1.59 schwarze 1952: exitcode = (int)MANDOCLEVEL_SYSERR;
1953: say(file, NULL);
1.50 kristaps 1954: return(0);
1955: }
1.12 schwarze 1956:
1.81 ! schwarze 1957: /*
! 1958: * XXX The first three columns in table mpages are obsolete
! 1959: * and only kept for backward compatibility
! 1960: * until apropos(1) and friends have caught up.
! 1961: */
! 1962: sql = "CREATE TABLE \"mpages\" (\n"
1.50 kristaps 1963: " \"file\" TEXT NOT NULL,\n"
1964: " \"sec\" TEXT NOT NULL,\n"
1965: " \"arch\" TEXT NOT NULL,\n"
1966: " \"desc\" TEXT NOT NULL,\n"
1967: " \"form\" INTEGER NOT NULL,\n"
1968: " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1969: ");\n"
1970: "\n"
1.81 ! schwarze 1971: "CREATE TABLE \"mlinks\" (\n"
! 1972: " \"sec\" TEXT NOT NULL,\n"
! 1973: " \"arch\" TEXT NOT NULL,\n"
! 1974: " \"name\" TEXT NOT NULL,\n"
! 1975: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
! 1976: "ON DELETE CASCADE,\n"
! 1977: " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
! 1978: ");\n"
! 1979: "\n"
1.50 kristaps 1980: "CREATE TABLE \"keys\" (\n"
1981: " \"bits\" INTEGER NOT NULL,\n"
1982: " \"key\" TEXT NOT NULL,\n"
1.81 ! schwarze 1983: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
! 1984: "ON DELETE CASCADE,\n"
1.50 kristaps 1985: " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1986: ");\n"
1987: "\n"
1988: "CREATE INDEX \"key_index\" ON keys (key);\n";
1.14 schwarze 1989:
1.50 kristaps 1990: if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
1.59 schwarze 1991: exitcode = (int)MANDOCLEVEL_SYSERR;
1992: say(file, "%s", sqlite3_errmsg(db));
1.50 kristaps 1993: return(0);
1994: }
1.4 kristaps 1995:
1.57 schwarze 1996: prepare_statements:
1997: SQL_EXEC("PRAGMA foreign_keys = ON");
1.81 ! schwarze 1998: sql = "DELETE FROM mpages where file=?";
! 1999: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL);
! 2000: sql = "INSERT INTO mpages "
1.50 kristaps 2001: "(file,sec,arch,desc,form) VALUES (?,?,?,?,?)";
1.81 ! schwarze 2002: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL);
! 2003: sql = "INSERT INTO mlinks "
! 2004: "(sec,arch,name,pageid) VALUES (?,?,?,?)";
! 2005: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL);
1.50 kristaps 2006: sql = "INSERT INTO keys "
1.81 ! schwarze 2007: "(bits,key,pageid) VALUES (?,?,?)";
1.50 kristaps 2008: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL);
1.70 schwarze 2009:
2010: #ifndef __APPLE__
2011: /*
2012: * When opening a new database, we can turn off
2013: * synchronous mode for much better performance.
2014: */
2015:
2016: if (real)
2017: SQL_EXEC("PRAGMA synchronous = OFF");
2018: #endif
2019:
1.50 kristaps 2020: return(1);
2021: }
1.5 kristaps 2022:
1.50 kristaps 2023: static void *
2024: hash_halloc(size_t sz, void *arg)
2025: {
1.12 schwarze 2026:
1.50 kristaps 2027: return(mandoc_calloc(sz, 1));
2028: }
1.12 schwarze 2029:
1.50 kristaps 2030: static void *
2031: hash_alloc(size_t sz, void *arg)
2032: {
1.5 kristaps 2033:
1.50 kristaps 2034: return(mandoc_malloc(sz));
2035: }
1.41 kristaps 2036:
1.50 kristaps 2037: static void
2038: hash_free(void *p, size_t sz, void *arg)
2039: {
1.4 kristaps 2040:
1.50 kristaps 2041: free(p);
1.4 kristaps 2042: }
2043:
1.50 kristaps 2044: static int
1.59 schwarze 2045: set_basedir(const char *targetdir)
1.4 kristaps 2046: {
1.59 schwarze 2047: static char startdir[PATH_MAX];
2048: static int fd;
1.4 kristaps 2049:
1.59 schwarze 2050: /*
2051: * Remember where we started by keeping a fd open to the origin
2052: * path component: throughout this utility, we chdir() a lot to
2053: * handle relative paths, and by doing this, we can return to
2054: * the starting point.
2055: */
2056: if ('\0' == *startdir) {
2057: if (NULL == getcwd(startdir, PATH_MAX)) {
2058: exitcode = (int)MANDOCLEVEL_SYSERR;
2059: if (NULL != targetdir)
2060: say(".", NULL);
2061: return(0);
2062: }
2063: if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
2064: exitcode = (int)MANDOCLEVEL_SYSERR;
2065: say(startdir, NULL);
2066: return(0);
2067: }
2068: if (NULL == targetdir)
2069: targetdir = startdir;
2070: } else {
2071: if (-1 == fd)
2072: return(0);
2073: if (-1 == fchdir(fd)) {
2074: close(fd);
2075: basedir[0] = '\0';
2076: exitcode = (int)MANDOCLEVEL_SYSERR;
2077: say(startdir, NULL);
2078: return(0);
2079: }
2080: if (NULL == targetdir) {
2081: close(fd);
2082: return(1);
2083: }
2084: }
2085: if (NULL == realpath(targetdir, basedir)) {
2086: basedir[0] = '\0';
2087: exitcode = (int)MANDOCLEVEL_BADARG;
2088: say(targetdir, NULL);
1.50 kristaps 2089: return(0);
1.59 schwarze 2090: } else if (-1 == chdir(basedir)) {
2091: exitcode = (int)MANDOCLEVEL_BADARG;
2092: say("", NULL);
1.50 kristaps 2093: return(0);
1.4 kristaps 2094: }
1.50 kristaps 2095: return(1);
1.56 schwarze 2096: }
2097:
2098: static void
1.59 schwarze 2099: say(const char *file, const char *format, ...)
1.56 schwarze 2100: {
2101: va_list ap;
2102:
1.59 schwarze 2103: if ('\0' != *basedir)
2104: fprintf(stderr, "%s", basedir);
2105: if ('\0' != *basedir && '\0' != *file)
2106: fputs("//", stderr);
1.56 schwarze 2107: if ('\0' != *file)
1.59 schwarze 2108: fprintf(stderr, "%s", file);
1.56 schwarze 2109: fputs(": ", stderr);
1.59 schwarze 2110:
2111: if (NULL == format) {
2112: perror(NULL);
2113: return;
2114: }
1.56 schwarze 2115:
2116: va_start(ap, format);
2117: vfprintf(stderr, format, ap);
2118: va_end(ap);
2119:
2120: fputc('\n', stderr);
1.1 kristaps 2121: }
CVSweb