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