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