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