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