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