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