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