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