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