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