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