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