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