Annotation of src/usr.bin/mandoc/mandocdb.c, Revision 1.66
1.65 schwarze 1: /* $Id: mandocdb.c,v 1.64 2014/01/06 20:53:36 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);
1.62 schwarze 125: static void dbadd(const struct mpage *, struct mchars *);
1.47 schwarze 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;
1.63 schwarze 695: }
696:
697: if (strstr(buf, basedir) == buf)
698: start = buf + strlen(basedir) + 1;
699: else if (OP_TEST == op)
700: start = buf;
701: else {
1.47 schwarze 702: exitcode = (int)MANDOCLEVEL_BADARG;
703: say("", "%s: outside base directory", buf);
704: return;
1.63 schwarze 705: }
706:
707: if (-1 == stat(buf, &st)) {
1.47 schwarze 708: exitcode = (int)MANDOCLEVEL_BADARG;
709: say(file, NULL);
710: return;
711: } else if ( ! (S_IFREG & st.st_mode)) {
712: exitcode = (int)MANDOCLEVEL_BADARG;
713: say(file, "Not a regular file");
714: return;
1.1 schwarze 715: }
1.63 schwarze 716:
1.47 schwarze 717: mlink = mandoc_calloc(1, sizeof(struct mlink));
718: strlcpy(mlink->file, start, sizeof(mlink->file));
1.1 schwarze 719:
1.10 schwarze 720: /*
1.47 schwarze 721: * First try to guess our directory structure.
722: * If we find a separator, try to look for man* or cat*.
723: * If we find one of these and what's underneath is a directory,
724: * assume it's an architecture.
1.10 schwarze 725: */
1.47 schwarze 726: if (NULL != (p = strchr(start, '/'))) {
727: *p++ = '\0';
728: if (0 == strncmp(start, "man", 3)) {
729: mlink->dform = FORM_SRC;
1.51 schwarze 730: mlink->dsec = start + 3;
1.47 schwarze 731: } else if (0 == strncmp(start, "cat", 3)) {
732: mlink->dform = FORM_CAT;
1.51 schwarze 733: mlink->dsec = start + 3;
1.47 schwarze 734: }
1.10 schwarze 735:
1.47 schwarze 736: start = p;
737: if (NULL != mlink->dsec && NULL != (p = strchr(start, '/'))) {
738: *p++ = '\0';
1.51 schwarze 739: mlink->arch = start;
1.47 schwarze 740: start = p;
1.41 deraadt 741: }
1.47 schwarze 742: }
1.7 schwarze 743:
1.47 schwarze 744: /*
745: * Now check the file suffix.
746: * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
747: */
748: p = strrchr(start, '\0');
749: while (p-- > start && '/' != *p && '.' != *p)
750: /* Loop. */ ;
751:
752: if ('.' == *p) {
753: *p++ = '\0';
1.51 schwarze 754: mlink->fsec = p;
1.47 schwarze 755: }
1.41 deraadt 756:
1.47 schwarze 757: /*
758: * Now try to parse the name.
759: * Use the filename portion of the path.
760: */
761: mlink->name = start;
762: if (NULL != (p = strrchr(start, '/'))) {
763: mlink->name = p + 1;
764: *p = '\0';
765: }
766: mlink_add(mlink, &st);
767: }
1.2 schwarze 768:
1.47 schwarze 769: static void
770: mlink_add(struct mlink *mlink, const struct stat *st)
771: {
772: struct inodev inodev;
773: struct mpage *mpage;
774: unsigned int slot;
775:
776: assert(NULL != mlink->file);
777:
1.51 schwarze 778: mlink->dsec = mandoc_strdup(mlink->dsec ? mlink->dsec : "");
779: mlink->arch = mandoc_strdup(mlink->arch ? mlink->arch : "");
780: mlink->name = mandoc_strdup(mlink->name ? mlink->name : "");
781: mlink->fsec = mandoc_strdup(mlink->fsec ? mlink->fsec : "");
1.47 schwarze 782:
783: if ('0' == *mlink->fsec) {
784: free(mlink->fsec);
785: mlink->fsec = mandoc_strdup(mlink->dsec);
786: mlink->fform = FORM_CAT;
787: } else if ('1' <= *mlink->fsec && '9' >= *mlink->fsec)
788: mlink->fform = FORM_SRC;
789: else
790: mlink->fform = FORM_NONE;
1.1 schwarze 791:
1.47 schwarze 792: slot = ohash_qlookup(&mlinks, mlink->file);
793: assert(NULL == ohash_find(&mlinks, slot));
794: ohash_insert(&mlinks, slot, mlink);
795:
796: inodev.st_ino = st->st_ino;
797: inodev.st_dev = st->st_dev;
798: slot = ohash_lookup_memory(&mpages, (char *)&inodev,
799: sizeof(struct inodev), inodev.st_ino);
800: mpage = ohash_find(&mpages, slot);
801: if (NULL == mpage) {
802: mpage = mandoc_calloc(1, sizeof(struct mpage));
803: mpage->inodev.st_ino = inodev.st_ino;
804: mpage->inodev.st_dev = inodev.st_dev;
805: ohash_insert(&mpages, slot, mpage);
806: } else
807: mlink->next = mpage->mlinks;
808: mpage->mlinks = mlink;
809: }
1.1 schwarze 810:
1.47 schwarze 811: static void
812: mlink_free(struct mlink *mlink)
813: {
1.1 schwarze 814:
1.47 schwarze 815: free(mlink->dsec);
816: free(mlink->arch);
817: free(mlink->name);
818: free(mlink->fsec);
819: free(mlink);
820: }
1.1 schwarze 821:
1.47 schwarze 822: static void
823: mpages_free(void)
824: {
825: struct mpage *mpage;
826: struct mlink *mlink;
827: unsigned int slot;
828:
829: mpage = ohash_first(&mpages, &slot);
830: while (NULL != mpage) {
831: while (NULL != (mlink = mpage->mlinks)) {
832: mpage->mlinks = mlink->next;
833: mlink_free(mlink);
834: }
835: free(mpage->sec);
836: free(mpage->arch);
837: free(mpage->title);
838: free(mpage->desc);
839: free(mpage);
840: mpage = ohash_next(&mpages, &slot);
841: }
842: }
1.1 schwarze 843:
1.47 schwarze 844: /*
845: * For each mlink to the mpage, check whether the path looks like
846: * it is formatted, and if it does, check whether a source manual
847: * exists by the same name, ignoring the suffix.
848: * If both conditions hold, drop the mlink.
849: */
850: static void
851: mlinks_undupe(struct mpage *mpage)
852: {
853: char buf[PATH_MAX];
854: struct mlink **prev;
855: struct mlink *mlink;
856: char *bufp;
857:
858: mpage->form = FORM_CAT;
859: prev = &mpage->mlinks;
860: while (NULL != (mlink = *prev)) {
861: if (FORM_CAT != mlink->dform) {
862: mpage->form = FORM_NONE;
863: goto nextlink;
864: }
865: if (strlcpy(buf, mlink->file, PATH_MAX) >= PATH_MAX) {
866: if (warnings)
867: say(mlink->file, "Filename too long");
868: goto nextlink;
1.26 schwarze 869: }
1.47 schwarze 870: bufp = strstr(buf, "cat");
871: assert(NULL != bufp);
872: memcpy(bufp, "man", 3);
873: if (NULL != (bufp = strrchr(buf, '.')))
874: *++bufp = '\0';
875: strlcat(buf, mlink->dsec, PATH_MAX);
876: if (NULL == ohash_find(&mlinks,
877: ohash_qlookup(&mlinks, buf)))
878: goto nextlink;
879: if (warnings)
880: say(mlink->file, "Man source exists: %s", buf);
881: if (use_all)
882: goto nextlink;
883: *prev = mlink->next;
884: mlink_free(mlink);
885: continue;
886: nextlink:
887: prev = &(*prev)->next;
1.1 schwarze 888: }
1.47 schwarze 889: }
1.1 schwarze 890:
1.50 schwarze 891: static int
892: mlink_check(struct mpage *mpage, struct mlink *mlink)
893: {
894: int match;
895:
896: match = 1;
897:
898: /*
899: * Check whether the manual section given in a file
900: * agrees with the directory where the file is located.
901: * Some manuals have suffixes like (3p) on their
902: * section number either inside the file or in the
903: * directory name, some are linked into more than one
904: * section, like encrypt(1) = makekey(8).
905: */
906:
907: if (FORM_SRC == mpage->form &&
908: strcasecmp(mpage->sec, mlink->dsec)) {
909: match = 0;
910: say(mlink->file, "Section \"%s\" manual in %s directory",
911: mpage->sec, mlink->dsec);
912: }
913:
914: /*
915: * Manual page directories exist for each kernel
916: * architecture as returned by machine(1).
917: * However, many manuals only depend on the
918: * application architecture as returned by arch(1).
919: * For example, some (2/ARM) manuals are shared
920: * across the "armish" and "zaurus" kernel
921: * architectures.
922: * A few manuals are even shared across completely
923: * different architectures, for example fdformat(1)
924: * on amd64, i386, sparc, and sparc64.
925: */
926:
927: if (strcasecmp(mpage->arch, mlink->arch)) {
928: match = 0;
929: say(mlink->file, "Architecture \"%s\" manual in "
930: "\"%s\" directory", mpage->arch, mlink->arch);
931: }
932:
933: if (strcasecmp(mpage->title, mlink->name))
934: match = 0;
935:
936: return(match);
937: }
938:
1.47 schwarze 939: /*
940: * Run through the files in the global vector "mpages"
941: * and add them to the database specified in "basedir".
942: *
943: * This handles the parsing scheme itself, using the cues of directory
944: * and filename to determine whether the file is parsable or not.
945: */
946: static void
1.58 schwarze 947: mpages_merge(struct mchars *mc, struct mparse *mp)
1.47 schwarze 948: {
1.58 schwarze 949: struct ohash_info str_info;
1.47 schwarze 950: struct mpage *mpage;
1.50 schwarze 951: struct mlink *mlink;
1.47 schwarze 952: struct mdoc *mdoc;
953: struct man *man;
954: const char *cp;
955: int match;
1.58 schwarze 956: unsigned int pslot;
1.47 schwarze 957: enum mandoclevel lvl;
958:
959: str_info.alloc = hash_alloc;
960: str_info.halloc = hash_halloc;
961: str_info.hfree = hash_free;
962: str_info.key_offset = offsetof(struct str, key);
963:
1.64 schwarze 964: if (0 == nodb)
965: SQL_EXEC("BEGIN TRANSACTION");
966:
1.47 schwarze 967: mpage = ohash_first(&mpages, &pslot);
968: while (NULL != mpage) {
969: mlinks_undupe(mpage);
970: if (NULL == mpage->mlinks) {
971: mpage = ohash_next(&mpages, &pslot);
972: continue;
973: }
1.1 schwarze 974:
1.47 schwarze 975: ohash_init(&strings, 6, &str_info);
976: mparse_reset(mp);
977: mdoc = NULL;
978: man = NULL;
1.11 schwarze 979:
980: /*
1.24 schwarze 981: * Try interpreting the file as mdoc(7) or man(7)
982: * source code, unless it is already known to be
983: * formatted. Fall back to formatted mode.
1.11 schwarze 984: */
1.47 schwarze 985: if (FORM_CAT != mpage->mlinks->dform ||
986: FORM_CAT != mpage->mlinks->fform) {
987: lvl = mparse_readfd(mp, -1, mpage->mlinks->file);
988: if (lvl < MANDOCLEVEL_FATAL)
989: mparse_result(mp, &mdoc, &man);
990: }
1.11 schwarze 991:
992: if (NULL != mdoc) {
1.47 schwarze 993: mpage->form = FORM_SRC;
994: mpage->sec =
995: mandoc_strdup(mdoc_meta(mdoc)->msec);
996: mpage->arch = mdoc_meta(mdoc)->arch;
997: mpage->arch = mandoc_strdup(
998: NULL == mpage->arch ? "" : mpage->arch);
999: mpage->title =
1000: mandoc_strdup(mdoc_meta(mdoc)->title);
1.11 schwarze 1001: } else if (NULL != man) {
1.47 schwarze 1002: mpage->form = FORM_SRC;
1003: mpage->sec =
1004: mandoc_strdup(man_meta(man)->msec);
1005: mpage->arch =
1006: mandoc_strdup(mpage->mlinks->arch);
1007: mpage->title =
1008: mandoc_strdup(man_meta(man)->title);
1.11 schwarze 1009: } else {
1.47 schwarze 1010: mpage->form = FORM_CAT;
1011: mpage->sec =
1012: mandoc_strdup(mpage->mlinks->dsec);
1013: mpage->arch =
1014: mandoc_strdup(mpage->mlinks->arch);
1015: mpage->title =
1016: mandoc_strdup(mpage->mlinks->name);
1.1 schwarze 1017: }
1.54 schwarze 1018: putkey(mpage, mpage->sec, TYPE_sec);
1.55 schwarze 1019: putkey(mpage, '\0' == *mpage->arch ?
1020: "any" : mpage->arch, TYPE_arch);
1.1 schwarze 1021:
1.54 schwarze 1022: for (mlink = mpage->mlinks; mlink; mlink = mlink->next) {
1023: if ('\0' != *mlink->dsec)
1024: putkey(mpage, mlink->dsec, TYPE_sec);
1025: if ('\0' != *mlink->fsec)
1026: putkey(mpage, mlink->fsec, TYPE_sec);
1.55 schwarze 1027: putkey(mpage, '\0' == *mlink->arch ?
1028: "any" : mlink->arch, TYPE_arch);
1.50 schwarze 1029: putkey(mpage, mlink->name, TYPE_Nm);
1.54 schwarze 1030: }
1.41 deraadt 1031:
1.50 schwarze 1032: if (warnings && !use_all) {
1.47 schwarze 1033: match = 0;
1.50 schwarze 1034: for (mlink = mpage->mlinks; mlink;
1035: mlink = mlink->next)
1036: if (mlink_check(mpage, mlink))
1037: match = 1;
1038: } else
1039: match = 1;
1.6 schwarze 1040:
1.47 schwarze 1041: if (NULL != mdoc) {
1042: if (NULL != (cp = mdoc_meta(mdoc)->name))
1043: putkey(mpage, cp, TYPE_Nm);
1044: assert(NULL == mpage->desc);
1045: parse_mdoc(mpage, mdoc_node(mdoc));
1046: putkey(mpage, NULL != mpage->desc ?
1047: mpage->desc : mpage->mlinks->name, TYPE_Nd);
1048: } else if (NULL != man)
1049: parse_man(mpage, man_node(man));
1050: else
1051: parse_cat(mpage);
1.6 schwarze 1052:
1.62 schwarze 1053: dbadd(mpage, mc);
1.47 schwarze 1054: ohash_delete(&strings);
1055: mpage = ohash_next(&mpages, &pslot);
1056: }
1.64 schwarze 1057:
1058: if (0 == nodb)
1059: SQL_EXEC("END TRANSACTION");
1.47 schwarze 1060: }
1.6 schwarze 1061:
1.47 schwarze 1062: static void
1063: parse_cat(struct mpage *mpage)
1064: {
1065: FILE *stream;
1066: char *line, *p, *title;
1067: size_t len, plen, titlesz;
1.1 schwarze 1068:
1.47 schwarze 1069: if (NULL == (stream = fopen(mpage->mlinks->file, "r"))) {
1070: if (warnings)
1071: say(mpage->mlinks->file, NULL);
1072: return;
1073: }
1.1 schwarze 1074:
1.47 schwarze 1075: /* Skip to first blank line. */
1.1 schwarze 1076:
1.47 schwarze 1077: while (NULL != (line = fgetln(stream, &len)))
1078: if ('\n' == *line)
1079: break;
1.1 schwarze 1080:
1.47 schwarze 1081: /*
1082: * Assume the first line that is not indented
1083: * is the first section header. Skip to it.
1084: */
1.1 schwarze 1085:
1.47 schwarze 1086: while (NULL != (line = fgetln(stream, &len)))
1087: if ('\n' != *line && ' ' != *line)
1088: break;
1089:
1090: /*
1091: * Read up until the next section into a buffer.
1092: * Strip the leading and trailing newline from each read line,
1093: * appending a trailing space.
1094: * Ignore empty (whitespace-only) lines.
1095: */
1.28 schwarze 1096:
1.47 schwarze 1097: titlesz = 0;
1098: title = NULL;
1.38 schwarze 1099:
1.47 schwarze 1100: while (NULL != (line = fgetln(stream, &len))) {
1101: if (' ' != *line || '\n' != line[len - 1])
1102: break;
1103: while (len > 0 && isspace((unsigned char)*line)) {
1104: line++;
1105: len--;
1106: }
1107: if (1 == len)
1108: continue;
1109: title = mandoc_realloc(title, titlesz + len);
1110: memcpy(title + titlesz, line, len);
1111: titlesz += len;
1112: title[titlesz - 1] = ' ';
1113: }
1.28 schwarze 1114:
1.47 schwarze 1115: /*
1116: * If no page content can be found, or the input line
1117: * is already the next section header, or there is no
1118: * trailing newline, reuse the page title as the page
1119: * description.
1120: */
1.1 schwarze 1121:
1.47 schwarze 1122: if (NULL == title || '\0' == *title) {
1123: if (warnings)
1124: say(mpage->mlinks->file,
1125: "Cannot find NAME section");
1126: assert(NULL == mpage->desc);
1127: mpage->desc = mandoc_strdup(mpage->mlinks->name);
1128: putkey(mpage, mpage->mlinks->name, TYPE_Nd);
1129: fclose(stream);
1130: free(title);
1131: return;
1132: }
1.24 schwarze 1133:
1.47 schwarze 1134: title = mandoc_realloc(title, titlesz + 1);
1135: title[titlesz] = '\0';
1.24 schwarze 1136:
1.47 schwarze 1137: /*
1138: * Skip to the first dash.
1139: * Use the remaining line as the description (no more than 70
1140: * bytes).
1141: */
1.28 schwarze 1142:
1.47 schwarze 1143: if (NULL != (p = strstr(title, "- "))) {
1144: for (p += 2; ' ' == *p || '\b' == *p; p++)
1145: /* Skip to next word. */ ;
1146: } else {
1147: if (warnings)
1148: say(mpage->mlinks->file,
1149: "No dash in title line");
1150: p = title;
1151: }
1.1 schwarze 1152:
1.47 schwarze 1153: plen = strlen(p);
1.1 schwarze 1154:
1.47 schwarze 1155: /* Strip backspace-encoding from line. */
1.1 schwarze 1156:
1.47 schwarze 1157: while (NULL != (line = memchr(p, '\b', plen))) {
1158: len = line - p;
1159: if (0 == len) {
1160: memmove(line, line + 1, plen--);
1161: continue;
1162: }
1163: memmove(line - 1, line + 1, plen - len);
1164: plen -= 2;
1165: }
1.1 schwarze 1166:
1.47 schwarze 1167: assert(NULL == mpage->desc);
1168: mpage->desc = mandoc_strdup(p);
1169: putkey(mpage, mpage->desc, TYPE_Nd);
1170: fclose(stream);
1171: free(title);
1172: }
1.16 schwarze 1173:
1.47 schwarze 1174: /*
1175: * Put a type/word pair into the word database for this particular file.
1176: */
1177: static void
1178: putkey(const struct mpage *mpage, const char *value, uint64_t type)
1179: {
1.37 schwarze 1180:
1.47 schwarze 1181: assert(NULL != value);
1182: putkeys(mpage, value, strlen(value), type);
1.2 schwarze 1183: }
1184:
1185: /*
1.47 schwarze 1186: * Grok all nodes at or below a certain mdoc node into putkey().
1.2 schwarze 1187: */
1188: static void
1.47 schwarze 1189: putmdockey(const struct mpage *mpage,
1190: const struct mdoc_node *n, uint64_t m)
1.2 schwarze 1191: {
1.16 schwarze 1192:
1.47 schwarze 1193: for ( ; NULL != n; n = n->next) {
1194: if (NULL != n->child)
1195: putmdockey(mpage, n->child, m);
1196: if (MDOC_TEXT == n->type)
1197: putkey(mpage, n->string, m);
1198: }
1199: }
1.16 schwarze 1200:
1.47 schwarze 1201: static void
1202: parse_man(struct mpage *mpage, const struct man_node *n)
1203: {
1204: const struct man_node *head, *body;
1205: char *start, *sv, *title;
1206: char byte;
1207: size_t sz, titlesz;
1.16 schwarze 1208:
1.47 schwarze 1209: if (NULL == n)
1210: return;
1.16 schwarze 1211:
1.47 schwarze 1212: /*
1213: * We're only searching for one thing: the first text child in
1214: * the BODY of a NAME section. Since we don't keep track of
1215: * sections in -man, run some hoops to find out whether we're in
1216: * the correct section or not.
1217: */
1.16 schwarze 1218:
1.47 schwarze 1219: if (MAN_BODY == n->type && MAN_SH == n->tok) {
1220: body = n;
1221: assert(body->parent);
1222: if (NULL != (head = body->parent->head) &&
1223: 1 == head->nchild &&
1224: NULL != (head = (head->child)) &&
1225: MAN_TEXT == head->type &&
1226: 0 == strcmp(head->string, "NAME") &&
1227: NULL != (body = body->child) &&
1228: MAN_TEXT == body->type) {
1.2 schwarze 1229:
1.47 schwarze 1230: title = NULL;
1231: titlesz = 0;
1.2 schwarze 1232:
1.47 schwarze 1233: /*
1234: * Suck the entire NAME section into memory.
1235: * Yes, we might run away.
1236: * But too many manuals have big, spread-out
1237: * NAME sections over many lines.
1238: */
1.2 schwarze 1239:
1.47 schwarze 1240: for ( ; NULL != body; body = body->next) {
1241: if (MAN_TEXT != body->type)
1242: break;
1243: if (0 == (sz = strlen(body->string)))
1244: continue;
1245: title = mandoc_realloc
1246: (title, titlesz + sz + 1);
1247: memcpy(title + titlesz, body->string, sz);
1248: titlesz += sz + 1;
1249: title[titlesz - 1] = ' ';
1250: }
1251: if (NULL == title)
1252: return;
1.16 schwarze 1253:
1.47 schwarze 1254: title = mandoc_realloc(title, titlesz + 1);
1255: title[titlesz] = '\0';
1.16 schwarze 1256:
1.47 schwarze 1257: /* Skip leading space. */
1.16 schwarze 1258:
1.47 schwarze 1259: sv = title;
1260: while (isspace((unsigned char)*sv))
1261: sv++;
1.16 schwarze 1262:
1.47 schwarze 1263: if (0 == (sz = strlen(sv))) {
1264: free(title);
1265: return;
1266: }
1.1 schwarze 1267:
1.47 schwarze 1268: /* Erase trailing space. */
1.1 schwarze 1269:
1.47 schwarze 1270: start = &sv[sz - 1];
1271: while (start > sv && isspace((unsigned char)*start))
1272: *start-- = '\0';
1.1 schwarze 1273:
1.47 schwarze 1274: if (start == sv) {
1275: free(title);
1276: return;
1277: }
1.1 schwarze 1278:
1.47 schwarze 1279: start = sv;
1.16 schwarze 1280:
1.47 schwarze 1281: /*
1282: * Go through a special heuristic dance here.
1283: * Conventionally, one or more manual names are
1284: * comma-specified prior to a whitespace, then a
1285: * dash, then a description. Try to puzzle out
1286: * the name parts here.
1287: */
1.16 schwarze 1288:
1.47 schwarze 1289: for ( ;; ) {
1290: sz = strcspn(start, " ,");
1291: if ('\0' == start[sz])
1292: break;
1.1 schwarze 1293:
1.47 schwarze 1294: byte = start[sz];
1295: start[sz] = '\0';
1.1 schwarze 1296:
1.47 schwarze 1297: putkey(mpage, start, TYPE_Nm);
1.1 schwarze 1298:
1.47 schwarze 1299: if (' ' == byte) {
1300: start += sz + 1;
1301: break;
1302: }
1.1 schwarze 1303:
1.47 schwarze 1304: assert(',' == byte);
1305: start += sz + 1;
1306: while (' ' == *start)
1307: start++;
1308: }
1.1 schwarze 1309:
1.47 schwarze 1310: if (sv == start) {
1311: putkey(mpage, start, TYPE_Nm);
1312: free(title);
1313: return;
1314: }
1.1 schwarze 1315:
1.47 schwarze 1316: while (isspace((unsigned char)*start))
1317: start++;
1.1 schwarze 1318:
1.47 schwarze 1319: if (0 == strncmp(start, "-", 1))
1320: start += 1;
1321: else if (0 == strncmp(start, "\\-\\-", 4))
1322: start += 4;
1323: else if (0 == strncmp(start, "\\-", 2))
1324: start += 2;
1325: else if (0 == strncmp(start, "\\(en", 4))
1326: start += 4;
1327: else if (0 == strncmp(start, "\\(em", 4))
1328: start += 4;
1.1 schwarze 1329:
1.47 schwarze 1330: while (' ' == *start)
1331: start++;
1.1 schwarze 1332:
1.47 schwarze 1333: assert(NULL == mpage->desc);
1334: mpage->desc = mandoc_strdup(start);
1335: putkey(mpage, mpage->desc, TYPE_Nd);
1336: free(title);
1337: return;
1338: }
1339: }
1.1 schwarze 1340:
1.47 schwarze 1341: for (n = n->child; n; n = n->next) {
1342: if (NULL != mpage->desc)
1343: break;
1344: parse_man(mpage, n);
1.1 schwarze 1345: }
1346: }
1347:
1348: static void
1.47 schwarze 1349: parse_mdoc(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1350: {
1351:
1.47 schwarze 1352: assert(NULL != n);
1353: for (n = n->child; NULL != n; n = n->next) {
1354: switch (n->type) {
1355: case (MDOC_ELEM):
1356: /* FALLTHROUGH */
1357: case (MDOC_BLOCK):
1358: /* FALLTHROUGH */
1359: case (MDOC_HEAD):
1360: /* FALLTHROUGH */
1361: case (MDOC_BODY):
1362: /* FALLTHROUGH */
1363: case (MDOC_TAIL):
1364: if (NULL != mdocs[n->tok].fp)
1365: if (0 == (*mdocs[n->tok].fp)(mpage, n))
1366: break;
1367: if (mdocs[n->tok].mask)
1368: putmdockey(mpage, n->child,
1369: mdocs[n->tok].mask);
1370: break;
1371: default:
1372: assert(MDOC_ROOT != n->type);
1373: continue;
1374: }
1375: if (NULL != n->child)
1376: parse_mdoc(mpage, n);
1.1 schwarze 1377: }
1378: }
1379:
1.19 schwarze 1380: static int
1.47 schwarze 1381: parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1382: {
1383: const char *start, *end;
1384: size_t sz;
1.19 schwarze 1385:
1.47 schwarze 1386: if (SEC_SYNOPSIS != n->sec ||
1387: NULL == (n = n->child) ||
1388: MDOC_TEXT != n->type)
1.19 schwarze 1389: return(0);
1.1 schwarze 1390:
1391: /*
1392: * Only consider those `Fd' macro fields that begin with an
1393: * "inclusion" token (versus, e.g., #define).
1394: */
1.47 schwarze 1395:
1.1 schwarze 1396: if (strcmp("#include", n->string))
1.19 schwarze 1397: return(0);
1.1 schwarze 1398:
1399: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.19 schwarze 1400: return(0);
1.1 schwarze 1401:
1402: /*
1403: * Strip away the enclosing angle brackets and make sure we're
1404: * not zero-length.
1405: */
1406:
1407: start = n->string;
1408: if ('<' == *start || '"' == *start)
1409: start++;
1410:
1411: if (0 == (sz = strlen(start)))
1.19 schwarze 1412: return(0);
1.1 schwarze 1413:
1414: end = &start[(int)sz - 1];
1415: if ('>' == *end || '"' == *end)
1416: end--;
1417:
1.47 schwarze 1418: if (end > start)
1419: putkeys(mpage, start, end - start + 1, TYPE_In);
1.49 schwarze 1420: return(0);
1.1 schwarze 1421: }
1422:
1.19 schwarze 1423: static int
1.47 schwarze 1424: parse_mdoc_Fn(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1425: {
1426: const char *cp;
1427:
1.47 schwarze 1428: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.19 schwarze 1429: return(0);
1430:
1.47 schwarze 1431: /*
1432: * Parse: .Fn "struct type *name" "char *arg".
1433: * First strip away pointer symbol.
1434: * Then store the function name, then type.
1435: * Finally, store the arguments.
1436: */
1.1 schwarze 1437:
1.47 schwarze 1438: if (NULL == (cp = strrchr(n->string, ' ')))
1439: cp = n->string;
1.1 schwarze 1440:
1441: while ('*' == *cp)
1442: cp++;
1443:
1.47 schwarze 1444: putkey(mpage, cp, TYPE_Fn);
1.19 schwarze 1445:
1.47 schwarze 1446: if (n->string < cp)
1447: putkeys(mpage, n->string, cp - n->string, TYPE_Ft);
1.19 schwarze 1448:
1.47 schwarze 1449: for (n = n->next; NULL != n; n = n->next)
1450: if (MDOC_TEXT == n->type)
1451: putkey(mpage, n->string, TYPE_Fa);
1.19 schwarze 1452:
1453: return(0);
1.1 schwarze 1454: }
1455:
1.19 schwarze 1456: static int
1.47 schwarze 1457: parse_mdoc_Xr(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1458: {
1.47 schwarze 1459: char *cp;
1.1 schwarze 1460:
1461: if (NULL == (n = n->child))
1.19 schwarze 1462: return(0);
1.1 schwarze 1463:
1.47 schwarze 1464: if (NULL == n->next) {
1465: putkey(mpage, n->string, TYPE_Xr);
1466: return(0);
1467: }
1.1 schwarze 1468:
1.47 schwarze 1469: if (-1 == asprintf(&cp, "%s(%s)", n->string, n->next->string)) {
1470: perror(NULL);
1471: exit((int)MANDOCLEVEL_SYSERR);
1472: }
1473: putkey(mpage, cp, TYPE_Xr);
1474: free(cp);
1475: return(0);
1.1 schwarze 1476: }
1477:
1.19 schwarze 1478: static int
1.47 schwarze 1479: parse_mdoc_Nd(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1480: {
1.47 schwarze 1481: size_t sz;
1.1 schwarze 1482:
1483: if (MDOC_BODY != n->type)
1.19 schwarze 1484: return(0);
1.1 schwarze 1485:
1.47 schwarze 1486: /*
1487: * Special-case the `Nd' because we need to put the description
1488: * into the document table.
1489: */
1490:
1491: for (n = n->child; NULL != n; n = n->next) {
1492: if (MDOC_TEXT == n->type) {
1493: if (NULL != mpage->desc) {
1494: sz = strlen(mpage->desc) +
1495: strlen(n->string) + 2;
1496: mpage->desc = mandoc_realloc(
1497: mpage->desc, sz);
1498: strlcat(mpage->desc, " ", sz);
1499: strlcat(mpage->desc, n->string, sz);
1500: } else
1501: mpage->desc = mandoc_strdup(n->string);
1502: }
1503: if (NULL != n->child)
1504: parse_mdoc_Nd(mpage, n);
1505: }
1.19 schwarze 1506: return(1);
1.1 schwarze 1507: }
1508:
1.19 schwarze 1509: static int
1.47 schwarze 1510: parse_mdoc_Nm(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1511: {
1512:
1.49 schwarze 1513: return(SEC_NAME == n->sec ||
1514: (SEC_SYNOPSIS == n->sec && MDOC_HEAD == n->type));
1.1 schwarze 1515: }
1516:
1.19 schwarze 1517: static int
1.47 schwarze 1518: parse_mdoc_Sh(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1519: {
1520:
1.19 schwarze 1521: return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1 schwarze 1522: }
1523:
1.47 schwarze 1524: static int
1525: parse_mdoc_head(struct mpage *mpage, const struct mdoc_node *n)
1.1 schwarze 1526: {
1527:
1.47 schwarze 1528: return(MDOC_HEAD == n->type);
1529: }
1.1 schwarze 1530:
1.47 schwarze 1531: static int
1532: parse_mdoc_body(struct mpage *mpage, const struct mdoc_node *n)
1533: {
1.1 schwarze 1534:
1.47 schwarze 1535: return(MDOC_BODY == n->type);
1.1 schwarze 1536: }
1537:
1.47 schwarze 1538: /*
1539: * Add a string to the hash table for the current manual.
1540: * Each string has a bitmask telling which macros it belongs to.
1541: * When we finish the manual, we'll dump the table.
1542: */
1.1 schwarze 1543: static void
1.47 schwarze 1544: putkeys(const struct mpage *mpage,
1545: const char *cp, size_t sz, uint64_t v)
1.1 schwarze 1546: {
1.47 schwarze 1547: struct str *s;
1548: unsigned int slot;
1549: const char *end;
1.1 schwarze 1550:
1.47 schwarze 1551: if (0 == sz)
1552: return;
1553:
1554: end = cp + sz;
1555: slot = ohash_qlookupi(&strings, cp, &end);
1556: s = ohash_find(&strings, slot);
1.1 schwarze 1557:
1.47 schwarze 1558: if (NULL != s && mpage == s->mpage) {
1559: s->mask |= v;
1.1 schwarze 1560: return;
1.47 schwarze 1561: } else if (NULL == s) {
1562: s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
1563: memcpy(s->key, cp, sz);
1564: ohash_insert(&strings, slot, s);
1565: }
1566: s->mpage = mpage;
1567: s->mask = v;
1.1 schwarze 1568: }
1569:
1570: /*
1.47 schwarze 1571: * Take a Unicode codepoint and produce its UTF-8 encoding.
1572: * This isn't the best way to do this, but it works.
1573: * The magic numbers are from the UTF-8 packaging.
1574: * They're not as scary as they seem: read the UTF-8 spec for details.
1.1 schwarze 1575: */
1.47 schwarze 1576: static size_t
1577: utf8(unsigned int cp, char out[7])
1.1 schwarze 1578: {
1.47 schwarze 1579: size_t rc;
1.1 schwarze 1580:
1.47 schwarze 1581: rc = 0;
1582: if (cp <= 0x0000007F) {
1583: rc = 1;
1584: out[0] = (char)cp;
1585: } else if (cp <= 0x000007FF) {
1586: rc = 2;
1587: out[0] = (cp >> 6 & 31) | 192;
1588: out[1] = (cp & 63) | 128;
1589: } else if (cp <= 0x0000FFFF) {
1590: rc = 3;
1591: out[0] = (cp >> 12 & 15) | 224;
1592: out[1] = (cp >> 6 & 63) | 128;
1593: out[2] = (cp & 63) | 128;
1594: } else if (cp <= 0x001FFFFF) {
1595: rc = 4;
1596: out[0] = (cp >> 18 & 7) | 240;
1597: out[1] = (cp >> 12 & 63) | 128;
1598: out[2] = (cp >> 6 & 63) | 128;
1599: out[3] = (cp & 63) | 128;
1600: } else if (cp <= 0x03FFFFFF) {
1601: rc = 5;
1602: out[0] = (cp >> 24 & 3) | 248;
1603: out[1] = (cp >> 18 & 63) | 128;
1604: out[2] = (cp >> 12 & 63) | 128;
1605: out[3] = (cp >> 6 & 63) | 128;
1606: out[4] = (cp & 63) | 128;
1607: } else if (cp <= 0x7FFFFFFF) {
1608: rc = 6;
1609: out[0] = (cp >> 30 & 1) | 252;
1610: out[1] = (cp >> 24 & 63) | 128;
1611: out[2] = (cp >> 18 & 63) | 128;
1612: out[3] = (cp >> 12 & 63) | 128;
1613: out[4] = (cp >> 6 & 63) | 128;
1614: out[5] = (cp & 63) | 128;
1615: } else
1616: return(0);
1.19 schwarze 1617:
1.47 schwarze 1618: out[rc] = '\0';
1619: return(rc);
1.1 schwarze 1620: }
1621:
1.47 schwarze 1622: /*
1.53 schwarze 1623: * Store the rendered version of a key, or alias the pointer
1624: * if the key contains no escape sequences.
1.47 schwarze 1625: */
1626: static void
1.53 schwarze 1627: render_key(struct mchars *mc, struct str *key)
1.1 schwarze 1628: {
1.47 schwarze 1629: size_t sz, bsz, pos;
1630: char utfbuf[7], res[5];
1631: char *buf;
1632: const char *seq, *cpp, *val;
1633: int len, u;
1634: enum mandoc_esc esc;
1635:
1.53 schwarze 1636: assert(NULL == key->rendered);
1.47 schwarze 1637:
1638: res[0] = '\\';
1639: res[1] = '\t';
1640: res[2] = ASCII_NBRSP;
1641: res[3] = ASCII_HYPH;
1642: res[4] = '\0';
1.1 schwarze 1643:
1.47 schwarze 1644: val = key->key;
1645: bsz = strlen(val);
1.1 schwarze 1646:
1647: /*
1.47 schwarze 1648: * Pre-check: if we have no stop-characters, then set the
1649: * pointer as ourselvse and get out of here.
1.1 schwarze 1650: */
1.47 schwarze 1651: if (strcspn(val, res) == bsz) {
1.53 schwarze 1652: key->rendered = key->key;
1.47 schwarze 1653: return;
1654: }
1.1 schwarze 1655:
1.47 schwarze 1656: /* Pre-allocate by the length of the input */
1.39 schwarze 1657:
1.47 schwarze 1658: buf = mandoc_malloc(++bsz);
1659: pos = 0;
1.39 schwarze 1660:
1.47 schwarze 1661: while ('\0' != *val) {
1662: /*
1663: * Halt on the first escape sequence.
1664: * This also halts on the end of string, in which case
1665: * we just copy, fallthrough, and exit the loop.
1666: */
1667: if ((sz = strcspn(val, res)) > 0) {
1668: memcpy(&buf[pos], val, sz);
1669: pos += sz;
1670: val += sz;
1671: }
1.39 schwarze 1672:
1.47 schwarze 1673: if (ASCII_HYPH == *val) {
1674: buf[pos++] = '-';
1675: val++;
1676: continue;
1677: } else if ('\t' == *val || ASCII_NBRSP == *val) {
1678: buf[pos++] = ' ';
1679: val++;
1680: continue;
1681: } else if ('\\' != *val)
1682: break;
1.39 schwarze 1683:
1.47 schwarze 1684: /* Read past the slash. */
1.39 schwarze 1685:
1.47 schwarze 1686: val++;
1.39 schwarze 1687:
1.47 schwarze 1688: /*
1689: * Parse the escape sequence and see if it's a
1690: * predefined character or special character.
1691: */
1.52 schwarze 1692:
1.47 schwarze 1693: esc = mandoc_escape
1694: ((const char **)&val, &seq, &len);
1695: if (ESCAPE_ERROR == esc)
1696: break;
1697: if (ESCAPE_SPECIAL != esc)
1698: continue;
1.39 schwarze 1699:
1.47 schwarze 1700: /*
1.52 schwarze 1701: * Render the special character
1702: * as either UTF-8 or ASCII.
1.47 schwarze 1703: */
1.52 schwarze 1704:
1705: if (write_utf8) {
1706: if (0 == (u = mchars_spec2cp(mc, seq, len)))
1707: continue;
1708: cpp = utfbuf;
1709: if (0 == (sz = utf8(u, utfbuf)))
1710: continue;
1711: sz = strlen(cpp);
1712: } else {
1713: cpp = mchars_spec2str(mc, seq, len, &sz);
1714: if (NULL == cpp)
1715: continue;
1716: if (ASCII_NBRSP == *cpp) {
1717: cpp = " ";
1718: sz = 1;
1719: }
1720: }
1.1 schwarze 1721:
1.47 schwarze 1722: /* Copy the rendered glyph into the stream. */
1.1 schwarze 1723:
1.47 schwarze 1724: bsz += sz;
1725: buf = mandoc_realloc(buf, bsz);
1726: memcpy(&buf[pos], cpp, sz);
1727: pos += sz;
1.1 schwarze 1728: }
1729:
1.47 schwarze 1730: buf[pos] = '\0';
1.53 schwarze 1731: key->rendered = buf;
1.1 schwarze 1732: }
1733:
1.11 schwarze 1734: /*
1.47 schwarze 1735: * Flush the current page's terms (and their bits) into the database.
1736: * Wrap the entire set of additions in a transaction to make sqlite be a
1737: * little faster.
1.53 schwarze 1738: * Also, handle escape sequences at the last possible moment.
1.11 schwarze 1739: */
1740: static void
1.62 schwarze 1741: dbadd(const struct mpage *mpage, struct mchars *mc)
1.11 schwarze 1742: {
1.47 schwarze 1743: struct mlink *mlink;
1744: struct str *key;
1745: int64_t recno;
1746: size_t i;
1747: unsigned int slot;
1748:
1749: if (verb)
1.62 schwarze 1750: say(mpage->mlinks->file, "Adding to database");
1.11 schwarze 1751:
1.47 schwarze 1752: if (nodb)
1.11 schwarze 1753: return;
1.47 schwarze 1754:
1755: i = 1;
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->dsec);
1764: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->arch);
1765: SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->name);
1766: SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, recno);
1767: SQL_STEP(stmts[STMT_INSERT_LINK]);
1768: sqlite3_reset(stmts[STMT_INSERT_LINK]);
1769: }
1770:
1771: for (key = ohash_first(&strings, &slot); NULL != key;
1772: key = ohash_next(&strings, &slot)) {
1773: assert(key->mpage == mpage);
1.53 schwarze 1774: if (NULL == key->rendered)
1775: render_key(mc, key);
1.47 schwarze 1776: i = 1;
1777: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);
1.53 schwarze 1778: SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->rendered);
1.47 schwarze 1779: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, recno);
1780: SQL_STEP(stmts[STMT_INSERT_KEY]);
1781: sqlite3_reset(stmts[STMT_INSERT_KEY]);
1.53 schwarze 1782: if (key->rendered != key->key)
1783: free(key->rendered);
1.47 schwarze 1784: free(key);
1.33 schwarze 1785: }
1.47 schwarze 1786: }
1.41 deraadt 1787:
1.47 schwarze 1788: static void
1789: dbprune(void)
1790: {
1791: struct mpage *mpage;
1792: struct mlink *mlink;
1793: size_t i;
1794: unsigned int slot;
1.11 schwarze 1795:
1.63 schwarze 1796: if (0 == nodb)
1797: SQL_EXEC("BEGIN TRANSACTION");
1.47 schwarze 1798:
1.63 schwarze 1799: for (mpage = ohash_first(&mpages, &slot); NULL != mpage;
1800: mpage = ohash_next(&mpages, &slot)) {
1.47 schwarze 1801: mlink = mpage->mlinks;
1802: if (verb)
1.63 schwarze 1803: say(mlink->file, "Deleting from database");
1804: if (nodb)
1805: continue;
1806: for ( ; NULL != mlink; mlink = mlink->next) {
1807: i = 1;
1808: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
1809: i, mlink->dsec);
1810: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
1811: i, mlink->arch);
1812: SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
1813: i, mlink->name);
1814: SQL_STEP(stmts[STMT_DELETE_PAGE]);
1815: sqlite3_reset(stmts[STMT_DELETE_PAGE]);
1816: }
1.11 schwarze 1817: }
1.63 schwarze 1818:
1819: if (0 == nodb)
1820: SQL_EXEC("END TRANSACTION");
1.47 schwarze 1821: }
1.22 schwarze 1822:
1.47 schwarze 1823: /*
1824: * Close an existing database and its prepared statements.
1825: * If "real" is not set, rename the temporary file into the real one.
1826: */
1827: static void
1828: dbclose(int real)
1829: {
1830: size_t i;
1.11 schwarze 1831:
1.47 schwarze 1832: if (nodb)
1833: return;
1.11 schwarze 1834:
1.47 schwarze 1835: for (i = 0; i < STMT__MAX; i++) {
1836: sqlite3_finalize(stmts[i]);
1837: stmts[i] = NULL;
1.28 schwarze 1838: }
1.22 schwarze 1839:
1.47 schwarze 1840: sqlite3_close(db);
1841: db = NULL;
1.11 schwarze 1842:
1.47 schwarze 1843: if (real)
1844: return;
1.22 schwarze 1845:
1.47 schwarze 1846: if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {
1847: exitcode = (int)MANDOCLEVEL_SYSERR;
1848: say(MANDOC_DB, NULL);
1.22 schwarze 1849: }
1.11 schwarze 1850: }
1851:
1.47 schwarze 1852: /*
1853: * This is straightforward stuff.
1854: * Open a database connection to a "temporary" database, then open a set
1855: * of prepared statements we'll use over and over again.
1856: * If "real" is set, we use the existing database; if not, we truncate a
1857: * temporary one.
1858: * Must be matched by dbclose().
1859: */
1860: static int
1861: dbopen(int real)
1.2 schwarze 1862: {
1.47 schwarze 1863: const char *file, *sql;
1864: int rc, ofl;
1.6 schwarze 1865:
1.47 schwarze 1866: if (nodb)
1867: return(1);
1.6 schwarze 1868:
1.47 schwarze 1869: ofl = SQLITE_OPEN_READWRITE;
1870: if (0 == real) {
1871: file = MANDOC_DB "~";
1872: if (-1 == remove(file) && ENOENT != errno) {
1873: exitcode = (int)MANDOCLEVEL_SYSERR;
1874: say(file, NULL);
1875: return(0);
1.28 schwarze 1876: }
1.47 schwarze 1877: ofl |= SQLITE_OPEN_EXCLUSIVE;
1878: } else
1879: file = MANDOC_DB;
1.6 schwarze 1880:
1.47 schwarze 1881: rc = sqlite3_open_v2(file, &db, ofl, NULL);
1882: if (SQLITE_OK == rc)
1883: goto prepare_statements;
1884: if (SQLITE_CANTOPEN != rc) {
1885: exitcode = (int)MANDOCLEVEL_SYSERR;
1886: say(file, NULL);
1887: return(0);
1888: }
1.6 schwarze 1889:
1.47 schwarze 1890: sqlite3_close(db);
1891: db = NULL;
1.6 schwarze 1892:
1.47 schwarze 1893: if (SQLITE_OK != (rc = sqlite3_open(file, &db))) {
1894: exitcode = (int)MANDOCLEVEL_SYSERR;
1895: say(file, NULL);
1896: return(0);
1.2 schwarze 1897: }
1898:
1.47 schwarze 1899: sql = "CREATE TABLE \"mpages\" (\n"
1900: " \"form\" INTEGER NOT NULL,\n"
1901: " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1902: ");\n"
1903: "\n"
1904: "CREATE TABLE \"mlinks\" (\n"
1905: " \"sec\" TEXT NOT NULL,\n"
1906: " \"arch\" TEXT NOT NULL,\n"
1907: " \"name\" TEXT NOT NULL,\n"
1908: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1.66 ! schwarze 1909: "ON DELETE CASCADE\n"
1.47 schwarze 1910: ");\n"
1911: "\n"
1912: "CREATE TABLE \"keys\" (\n"
1913: " \"bits\" INTEGER NOT NULL,\n"
1914: " \"key\" TEXT NOT NULL,\n"
1915: " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
1.66 ! schwarze 1916: "ON DELETE CASCADE\n"
1.65 schwarze 1917: ");\n";
1.47 schwarze 1918:
1919: if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
1920: exitcode = (int)MANDOCLEVEL_SYSERR;
1921: say(file, "%s", sqlite3_errmsg(db));
1922: return(0);
1.2 schwarze 1923: }
1924:
1.47 schwarze 1925: prepare_statements:
1926: SQL_EXEC("PRAGMA foreign_keys = ON");
1.63 schwarze 1927: sql = "DELETE FROM mpages WHERE id IN "
1928: "(SELECT pageid FROM mlinks WHERE "
1929: "sec=? AND arch=? AND name=?)";
1.47 schwarze 1930: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL);
1931: sql = "INSERT INTO mpages "
1.60 schwarze 1932: "(form) VALUES (?)";
1.47 schwarze 1933: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL);
1934: sql = "INSERT INTO mlinks "
1.61 schwarze 1935: "(sec,arch,name,pageid) VALUES (?,?,?,?)";
1.47 schwarze 1936: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL);
1937: sql = "INSERT INTO keys "
1938: "(bits,key,pageid) VALUES (?,?,?)";
1939: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL);
1.6 schwarze 1940:
1.47 schwarze 1941: /*
1942: * When opening a new database, we can turn off
1943: * synchronous mode for much better performance.
1944: */
1.6 schwarze 1945:
1.47 schwarze 1946: if (real)
1947: SQL_EXEC("PRAGMA synchronous = OFF");
1.11 schwarze 1948:
1.47 schwarze 1949: return(1);
1950: }
1.6 schwarze 1951:
1.47 schwarze 1952: static void *
1953: hash_halloc(size_t sz, void *arg)
1954: {
1.6 schwarze 1955:
1.47 schwarze 1956: return(mandoc_calloc(sz, 1));
1957: }
1.2 schwarze 1958:
1.47 schwarze 1959: static void *
1960: hash_alloc(size_t sz, void *arg)
1961: {
1.28 schwarze 1962:
1.47 schwarze 1963: return(mandoc_malloc(sz));
1964: }
1.6 schwarze 1965:
1.47 schwarze 1966: static void
1967: hash_free(void *p, size_t sz, void *arg)
1968: {
1.26 schwarze 1969:
1.47 schwarze 1970: free(p);
1971: }
1.6 schwarze 1972:
1.47 schwarze 1973: static int
1974: set_basedir(const char *targetdir)
1975: {
1976: static char startdir[PATH_MAX];
1977: static int fd;
1.6 schwarze 1978:
1.47 schwarze 1979: /*
1980: * Remember where we started by keeping a fd open to the origin
1981: * path component: throughout this utility, we chdir() a lot to
1982: * handle relative paths, and by doing this, we can return to
1983: * the starting point.
1984: */
1985: if ('\0' == *startdir) {
1986: if (NULL == getcwd(startdir, PATH_MAX)) {
1987: exitcode = (int)MANDOCLEVEL_SYSERR;
1988: if (NULL != targetdir)
1989: say(".", NULL);
1990: return(0);
1991: }
1992: if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
1993: exitcode = (int)MANDOCLEVEL_SYSERR;
1994: say(startdir, NULL);
1995: return(0);
1.11 schwarze 1996: }
1.47 schwarze 1997: if (NULL == targetdir)
1998: targetdir = startdir;
1999: } else {
2000: if (-1 == fd)
2001: return(0);
2002: if (-1 == fchdir(fd)) {
2003: close(fd);
2004: basedir[0] = '\0';
2005: exitcode = (int)MANDOCLEVEL_SYSERR;
2006: say(startdir, NULL);
2007: return(0);
1.2 schwarze 2008: }
1.47 schwarze 2009: if (NULL == targetdir) {
2010: close(fd);
2011: return(1);
1.2 schwarze 2012: }
2013: }
1.47 schwarze 2014: if (NULL == realpath(targetdir, basedir)) {
2015: basedir[0] = '\0';
2016: exitcode = (int)MANDOCLEVEL_BADARG;
2017: say(targetdir, NULL);
2018: return(0);
2019: } else if (-1 == chdir(basedir)) {
2020: exitcode = (int)MANDOCLEVEL_BADARG;
2021: say("", NULL);
2022: return(0);
2023: }
2024: return(1);
1.2 schwarze 2025: }
2026:
2027: static void
1.47 schwarze 2028: say(const char *file, const char *format, ...)
1.2 schwarze 2029: {
1.47 schwarze 2030: va_list ap;
1.2 schwarze 2031:
1.47 schwarze 2032: if ('\0' != *basedir)
2033: fprintf(stderr, "%s", basedir);
2034: if ('\0' != *basedir && '\0' != *file)
2035: fputs("//", stderr);
2036: if ('\0' != *file)
2037: fprintf(stderr, "%s", file);
2038: fputs(": ", stderr);
1.31 schwarze 2039:
1.47 schwarze 2040: if (NULL == format) {
2041: perror(NULL);
2042: return;
1.2 schwarze 2043: }
1.47 schwarze 2044:
2045: va_start(ap, format);
2046: vfprintf(stderr, format, ap);
2047: va_end(ap);
2048:
2049: fputc('\n', stderr);
1.1 schwarze 2050: }