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