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