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