Home | History | Annotate | Line # | Download | only in makemandb
makemandb.c revision 1.46
      1 /*	$NetBSD: makemandb.c,v 1.46 2016/12/19 14:10:57 abhinav Exp $	*/
      2 /*
      3  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay (at) gmail.com>
      4  * Copyright (c) 2011 Kristaps Dzonsons <kristaps (at) bsd.lv>
      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 
     19 #include <sys/cdefs.h>
     20 __RCSID("$NetBSD: makemandb.c,v 1.46 2016/12/19 14:10:57 abhinav Exp $");
     21 
     22 #include <sys/stat.h>
     23 #include <sys/types.h>
     24 
     25 #include <assert.h>
     26 #include <dirent.h>
     27 #include <err.h>
     28 #include <archive.h>
     29 #include <libgen.h>
     30 #include <md5.h>
     31 #include <stdio.h>
     32 #include <stdlib.h>
     33 #include <string.h>
     34 #include <unistd.h>
     35 #include <util.h>
     36 
     37 #include "apropos-utils.h"
     38 #include "dist/man.h"
     39 #include "dist/mandoc.h"
     40 #include "dist/mdoc.h"
     41 #include "dist/roff.h"
     42 
     43 #define BUFLEN 1024
     44 #define MDOC 0	//If the page is of mdoc(7) type
     45 #define MAN 1	//If the page  is of man(7) type
     46 
     47 /*
     48  * A data structure for holding section specific data.
     49  */
     50 typedef struct secbuff {
     51 	char *data;
     52 	size_t buflen;	//Total length of buffer allocated initially
     53 	size_t offset;	// Current offset in the buffer.
     54 } secbuff;
     55 
     56 typedef struct makemandb_flags {
     57 	int optimize;
     58 	int limit;	// limit the indexing to only NAME section
     59 	int recreate;	// Database was created from scratch
     60 	int verbosity;	// 0: quiet, 1: default, 2: verbose
     61 } makemandb_flags;
     62 
     63 typedef struct roff_mandb_rec {
     64 	/* Fields for mandb table */
     65 	char *name;	// for storing the name of the man page
     66 	char *name_desc; // for storing the one line description (.Nd)
     67 	secbuff desc; // for storing the DESCRIPTION section
     68 	secbuff lib; // for the LIBRARY section
     69 	secbuff return_vals; // RETURN VALUES
     70 	secbuff env; // ENVIRONMENT
     71 	secbuff files; // FILES
     72 	secbuff exit_status; // EXIT STATUS
     73 	secbuff diagnostics; // DIAGNOSTICS
     74 	secbuff errors; // ERRORS
     75 	char *section;
     76 
     77 	int xr_found; // To track whether a .Xr was seen when parsing a section
     78 
     79 	/* Fields for mandb_meta table */
     80 	char *md5_hash;
     81 	dev_t device;
     82 	ino_t inode;
     83 	time_t mtime;
     84 
     85 	/* Fields for mandb_links table */
     86 	char *machine;
     87 	char *links; //all the links to a page in a space separated form
     88 	char *file_path;
     89 
     90 	/* Non-db fields */
     91 	int page_type; //Indicates the type of page: mdoc or man
     92 } mandb_rec;
     93 
     94 typedef	void (*proff_nf)(const struct roff_node *n, mandb_rec *);
     95 
     96 static void append(secbuff *sbuff, const char *src);
     97 static void init_secbuffs(mandb_rec *);
     98 static void free_secbuffs(mandb_rec *);
     99 static int check_md5(const char *, sqlite3 *, const char *, char **, void *, size_t);
    100 static void cleanup(mandb_rec *);
    101 static void set_section(const struct roff_man *, mandb_rec *);
    102 static void set_machine(const struct roff_man *, mandb_rec *);
    103 static int insert_into_db(sqlite3 *, mandb_rec *);
    104 static	void begin_parse(const char *, struct mparse *, mandb_rec *,
    105 			 const void *, size_t len);
    106 static void proff_node(const struct roff_node *, mandb_rec *, const proff_nf *);
    107 static void pmdoc_Nm(const struct roff_node *, mandb_rec *);
    108 static void pmdoc_Nd(const struct roff_node *, mandb_rec *);
    109 static void pmdoc_Sh(const struct roff_node *, mandb_rec *);
    110 static void mdoc_parse_Sh(const struct roff_node *, mandb_rec *);
    111 static void pmdoc_Xr(const struct roff_node *, mandb_rec *);
    112 static void pmdoc_Pp(const struct roff_node *, mandb_rec *);
    113 static void pmdoc_macro_handler(const struct roff_node *, mandb_rec *, int);
    114 static void pman_parse_node(const struct roff_node *, secbuff *);
    115 static void pman_parse_name(const struct roff_node *, mandb_rec *);
    116 static void pman_sh(const struct roff_node *, mandb_rec *);
    117 static void pman_block(const struct roff_node *, mandb_rec *);
    118 static void traversedir(const char *, const char *, sqlite3 *, struct mparse *);
    119 static void mdoc_parse_section(enum roff_sec, const char *, mandb_rec *);
    120 static void man_parse_section(enum man_sec, const struct roff_node *, mandb_rec *);
    121 static void build_file_cache(sqlite3 *, const char *, const char *,
    122 			     struct stat *);
    123 static void update_db(sqlite3 *, struct mparse *, mandb_rec *);
    124 __dead static void usage(void);
    125 static void optimize(sqlite3 *);
    126 static char *parse_escape(const char *);
    127 static void replace_hyph(char *);
    128 static makemandb_flags mflags = { .verbosity = 1 };
    129 
    130 static	const proff_nf mdocs[MDOC_MAX + 1] = {
    131 	NULL, /* Ap */
    132 	NULL, /* Dd */
    133 	NULL, /* Dt */
    134 	NULL, /* Os */
    135 
    136 	pmdoc_Sh, /* Sh */
    137 	NULL, /* Ss */
    138 	pmdoc_Pp, /* Pp */
    139 	NULL, /* D1 */
    140 
    141 	NULL, /* Dl */
    142 	NULL, /* Bd */
    143 	NULL, /* Ed */
    144 	NULL, /* Bl */
    145 
    146 	NULL, /* El */
    147 	NULL, /* It */
    148 	NULL, /* Ad */
    149 	NULL, /* An */
    150 
    151 	NULL, /* Ar */
    152 	NULL, /* Cd */
    153 	NULL, /* Cm */
    154 	NULL, /* Dv */
    155 
    156 	NULL, /* Er */
    157 	NULL, /* Ev */
    158 	NULL, /* Ex */
    159 	NULL, /* Fa */
    160 
    161 	NULL, /* Fd */
    162 	NULL, /* Fl */
    163 	NULL, /* Fn */
    164 	NULL, /* Ft */
    165 
    166 	NULL, /* Ic */
    167 	NULL, /* In */
    168 	NULL, /* Li */
    169 	pmdoc_Nd, /* Nd */
    170 
    171 	pmdoc_Nm, /* Nm */
    172 	NULL, /* Op */
    173 	NULL, /* Ot */
    174 	NULL, /* Pa */
    175 
    176 	NULL, /* Rv */
    177 	NULL, /* St */
    178 	NULL, /* Va */
    179 	NULL, /* Vt */
    180 
    181 	pmdoc_Xr, /* Xr */
    182 	NULL, /* %A */
    183 	NULL, /* %B */
    184 	NULL, /* %D */
    185 
    186 	NULL, /* %I */
    187 	NULL, /* %J */
    188 	NULL, /* %N */
    189 	NULL, /* %O */
    190 
    191 	NULL, /* %P */
    192 	NULL, /* %R */
    193 	NULL, /* %T */
    194 	NULL, /* %V */
    195 
    196 	NULL, /* Ac */
    197 	NULL, /* Ao */
    198 	NULL, /* Aq */
    199 	NULL, /* At */
    200 
    201 	NULL, /* Bc */
    202 	NULL, /* Bf */
    203 	NULL, /* Bo */
    204 	NULL, /* Bq */
    205 
    206 	NULL, /* Bsx */
    207 	NULL, /* Bx */
    208 	NULL, /* Db */
    209 	NULL, /* Dc */
    210 
    211 	NULL, /* Do */
    212 	NULL, /* Dq */
    213 	NULL, /* Ec */
    214 	NULL, /* Ef */
    215 
    216 	NULL, /* Em */
    217 	NULL, /* Eo */
    218 	NULL, /* Fx */
    219 	NULL, /* Ms */
    220 
    221 	NULL, /* No */
    222 	NULL, /* Ns */
    223 	NULL, /* Nx */
    224 	NULL, /* Ox */
    225 
    226 	NULL, /* Pc */
    227 	NULL, /* Pf */
    228 	NULL, /* Po */
    229 	NULL, /* Pq */
    230 
    231 	NULL, /* Qc */
    232 	NULL, /* Ql */
    233 	NULL, /* Qo */
    234 	NULL, /* Qq */
    235 
    236 	NULL, /* Re */
    237 	NULL, /* Rs */
    238 	NULL, /* Sc */
    239 	NULL, /* So */
    240 
    241 	NULL, /* Sq */
    242 	NULL, /* Sm */
    243 	NULL, /* Sx */
    244 	NULL, /* Sy */
    245 
    246 	NULL, /* Tn */
    247 	NULL, /* Ux */
    248 	NULL, /* Xc */
    249 	NULL, /* Xo */
    250 
    251 	NULL, /* Fo */
    252 	NULL, /* Fc */
    253 	NULL, /* Oo */
    254 	NULL, /* Oc */
    255 
    256 	NULL, /* Bk */
    257 	NULL, /* Ek */
    258 	NULL, /* Bt */
    259 	NULL, /* Hf */
    260 
    261 	NULL, /* Fr */
    262 	NULL, /* Ud */
    263 	NULL, /* Lb */
    264 	NULL, /* Lp */
    265 
    266 	NULL, /* Lk */
    267 	NULL, /* Mt */
    268 	NULL, /* Brq */
    269 	NULL, /* Bro */
    270 
    271 	NULL, /* Brc */
    272 	NULL, /* %C */
    273 	NULL, /* Es */
    274 	NULL, /* En */
    275 
    276 	NULL, /* Dx */
    277 	NULL, /* %Q */
    278 	NULL, /* br */
    279 	NULL, /* sp */
    280 
    281 	NULL, /* %U */
    282 	NULL, /* Ta */
    283 	NULL, /* ll */
    284 	NULL, /* text */
    285 };
    286 
    287 static	const proff_nf mans[MAN_MAX] = {
    288 	NULL,	//br
    289 	NULL,	//TH
    290 	pman_sh, //SH
    291 	NULL,	//SS
    292 	NULL,	//TP
    293 	NULL,	//LP
    294 	NULL,	//PP
    295 	NULL,	//P
    296 	NULL,	//IP
    297 	NULL,	//HP
    298 	NULL,	//SM
    299 	NULL,	//SB
    300 	NULL,	//BI
    301 	NULL,	//IB
    302 	NULL,	//BR
    303 	NULL,	//RB
    304 	NULL,	//R
    305 	pman_block,	//B
    306 	NULL,	//I
    307 	NULL,	//IR
    308 	NULL,	//RI
    309 	NULL,	//sp
    310 	NULL,	//nf
    311 	NULL,	//fi
    312 	NULL,	//RE
    313 	NULL,	//RS
    314 	NULL,	//DT
    315 	NULL,	//UC
    316 	NULL,	//PD
    317 	NULL,	//AT
    318 	NULL,	//in
    319 	NULL,	//ft
    320 	NULL,	//OP
    321 	NULL,	//EX
    322 	NULL,	//EE
    323 	NULL,	//UR
    324 	NULL,	//UE
    325 	NULL,	//ll
    326 };
    327 
    328 
    329 int
    330 main(int argc, char *argv[])
    331 {
    332 	FILE *file;
    333 	const char *sqlstr, *manconf = NULL;
    334 	char *line, *command, *parent;
    335 	char *errmsg;
    336 	int ch;
    337 	struct mparse *mp;
    338 	sqlite3 *db;
    339 	ssize_t len;
    340 	size_t linesize;
    341 	struct roff_mandb_rec rec;
    342 
    343 	while ((ch = getopt(argc, argv, "C:floQqv")) != -1) {
    344 		switch (ch) {
    345 		case 'C':
    346 			manconf = optarg;
    347 			break;
    348 		case 'f':
    349 			mflags.recreate = 1;
    350 			break;
    351 		case 'l':
    352 			mflags.limit = 1;
    353 			break;
    354 		case 'o':
    355 			mflags.optimize = 1;
    356 			break;
    357 		case 'Q':
    358 			mflags.verbosity = 0;
    359 			break;
    360 		case 'q':
    361 			mflags.verbosity = 1;
    362 			break;
    363 		case 'v':
    364 			mflags.verbosity = 2;
    365 			break;
    366 		default:
    367 			usage();
    368 		}
    369 	}
    370 
    371 	memset(&rec, 0, sizeof(rec));
    372 
    373 	init_secbuffs(&rec);
    374 	mchars_alloc();
    375 	mp = mparse_alloc(0, MANDOCLEVEL_BADARG, NULL, NULL);
    376 
    377 	if (manconf) {
    378 		char *arg;
    379 		size_t command_len = shquote(manconf, NULL, 0) + 1;
    380 		arg = emalloc(command_len);
    381 		shquote(manconf, arg, command_len);
    382 		easprintf(&command, "man -p -C %s", arg);
    383 		free(arg);
    384 	} else {
    385 		command = estrdup("man -p");
    386 		manconf = MANCONF;
    387 	}
    388 
    389 	if (mflags.recreate) {
    390 		char *dbp = get_dbpath(manconf);
    391 		/* No error here, it will fail in init_db in the same call */
    392 		if (dbp != NULL)
    393 			remove(dbp);
    394 	}
    395 
    396 	if ((db = init_db(MANDB_CREATE, manconf)) == NULL)
    397 		exit(EXIT_FAILURE);
    398 
    399 	sqlite3_exec(db, "PRAGMA synchronous = 0", NULL, NULL, 	&errmsg);
    400 	if (errmsg != NULL) {
    401 		warnx("%s", errmsg);
    402 		free(errmsg);
    403 		close_db(db);
    404 		exit(EXIT_FAILURE);
    405 	}
    406 
    407 	sqlite3_exec(db, "ATTACH DATABASE \':memory:\' AS metadb", NULL, NULL,
    408 	    &errmsg);
    409 	if (errmsg != NULL) {
    410 		warnx("%s", errmsg);
    411 		free(errmsg);
    412 		close_db(db);
    413 		exit(EXIT_FAILURE);
    414 	}
    415 
    416 
    417 	/* Call man -p to get the list of man page dirs */
    418 	if ((file = popen(command, "r")) == NULL) {
    419 		close_db(db);
    420 		err(EXIT_FAILURE, "fopen failed");
    421 	}
    422 	free(command);
    423 
    424 	/* Begin the transaction for indexing the pages	*/
    425 	sqlite3_exec(db, "BEGIN", NULL, NULL, &errmsg);
    426 	if (errmsg != NULL) {
    427 		warnx("%s", errmsg);
    428 		free(errmsg);
    429 		close_db(db);
    430 		exit(EXIT_FAILURE);
    431 	}
    432 
    433 	sqlstr = "CREATE TABLE metadb.file_cache(device, inode, mtime, parent,"
    434 		 " file PRIMARY KEY);"
    435 		 "CREATE UNIQUE INDEX metadb.index_file_cache_dev"
    436 		 " ON file_cache (device, inode)";
    437 
    438 	sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
    439 	if (errmsg != NULL) {
    440 		warnx("%s", errmsg);
    441 		free(errmsg);
    442 		close_db(db);
    443 		exit(EXIT_FAILURE);
    444 	}
    445 
    446 	if (mflags.verbosity)
    447 		printf("Building temporary file cache\n");
    448 	line = NULL;
    449 	linesize = 0;
    450 	while ((len = getline(&line, &linesize, file)) != -1) {
    451 		/* Replace the new line character at the end of string with '\0' */
    452 		line[len - 1] = '\0';
    453 		parent = estrdup(line);
    454 		char *pdir = estrdup(dirname(parent));
    455 		free(parent);
    456 		/* Traverse the man page directories and parse the pages */
    457 		traversedir(pdir, line, db, mp);
    458 		free(pdir);
    459 	}
    460 	free(line);
    461 
    462 	if (pclose(file) == -1) {
    463 		close_db(db);
    464 		cleanup(&rec);
    465 		free_secbuffs(&rec);
    466 		err(EXIT_FAILURE, "pclose error");
    467 	}
    468 
    469 	if (mflags.verbosity)
    470 		printf("Performing index update\n");
    471 	update_db(db, mp, &rec);
    472 	mparse_free(mp);
    473 	mchars_free();
    474 	free_secbuffs(&rec);
    475 
    476 	/* Commit the transaction */
    477 	sqlite3_exec(db, "COMMIT", NULL, NULL, &errmsg);
    478 	if (errmsg != NULL) {
    479 		warnx("%s", errmsg);
    480 		free(errmsg);
    481 		close_db(db);
    482 		exit(EXIT_FAILURE);
    483 	}
    484 
    485 	if (mflags.optimize)
    486 		optimize(db);
    487 
    488 	close_db(db);
    489 	return 0;
    490 }
    491 
    492 /*
    493  * traversedir --
    494  *  Traverses the given directory recursively and passes all the man page files
    495  *  in the way to build_file_cache()
    496  */
    497 static void
    498 traversedir(const char *parent, const char *file, sqlite3 *db,
    499             struct mparse *mp)
    500 {
    501 	struct stat sb;
    502 	struct dirent *dirp;
    503 	DIR *dp;
    504 	char *buf;
    505 
    506 	if (stat(file, &sb) < 0) {
    507 		if (mflags.verbosity)
    508 			warn("stat failed: %s", file);
    509 		return;
    510 	}
    511 
    512 	/* If it is a directory, traverse it recursively */
    513 	if (S_ISDIR(sb.st_mode)) {
    514 		if ((dp = opendir(file)) == NULL) {
    515 			if (mflags.verbosity)
    516 				warn("opendir error: %s", file);
    517 			return;
    518 		}
    519 
    520 		while ((dirp = readdir(dp)) != NULL) {
    521 			/* Avoid . and .. entries in a directory */
    522 			if (strncmp(dirp->d_name, ".", 1)) {
    523 				easprintf(&buf, "%s/%s", file, dirp->d_name);
    524 				traversedir(parent, buf, db, mp);
    525 				free(buf);
    526 			}
    527 		}
    528 		closedir(dp);
    529 		return;
    530 	}
    531 
    532 	if (!S_ISREG(sb.st_mode))
    533 		return;
    534 
    535 	if (sb.st_size == 0) {
    536 		if (mflags.verbosity)
    537 			warnx("Empty file: %s", file);
    538 		return;
    539 	}
    540 	build_file_cache(db, parent, file, &sb);
    541 }
    542 
    543 /* build_file_cache --
    544  *   This function generates an md5 hash of the file passed as its 2nd parameter
    545  *   and stores it in a temporary table file_cache along with the full file path.
    546  *   This is done to support incremental updation of the database.
    547  *   The temporary table file_cache is dropped thereafter in the function
    548  *   update_db(), once the database has been updated.
    549  */
    550 static void
    551 build_file_cache(sqlite3 *db, const char *parent, const char *file,
    552 		 struct stat *sb)
    553 {
    554 	const char *sqlstr;
    555 	sqlite3_stmt *stmt = NULL;
    556 	int rc, idx;
    557 	assert(file != NULL);
    558 	dev_t device_cache = sb->st_dev;
    559 	ino_t inode_cache = sb->st_ino;
    560 	time_t mtime_cache = sb->st_mtime;
    561 
    562 	sqlstr = "INSERT INTO metadb.file_cache VALUES (:device, :inode,"
    563 		 " :mtime, :parent, :file)";
    564 	rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
    565 	if (rc != SQLITE_OK) {
    566 		if (mflags.verbosity)
    567 			warnx("%s", sqlite3_errmsg(db));
    568 		return;
    569 	}
    570 
    571 	idx = sqlite3_bind_parameter_index(stmt, ":device");
    572 	rc = sqlite3_bind_int64(stmt, idx, device_cache);
    573 	if (rc != SQLITE_OK) {
    574 		if (mflags.verbosity)
    575 			warnx("%s", sqlite3_errmsg(db));
    576 		sqlite3_finalize(stmt);
    577 		return;
    578 	}
    579 
    580 	idx = sqlite3_bind_parameter_index(stmt, ":inode");
    581 	rc = sqlite3_bind_int64(stmt, idx, inode_cache);
    582 	if (rc != SQLITE_OK) {
    583 		if (mflags.verbosity)
    584 			warnx("%s", sqlite3_errmsg(db));
    585 		sqlite3_finalize(stmt);
    586 		return;
    587 	}
    588 
    589 	idx = sqlite3_bind_parameter_index(stmt, ":mtime");
    590 	rc = sqlite3_bind_int64(stmt, idx, mtime_cache);
    591 	if (rc != SQLITE_OK) {
    592 		if (mflags.verbosity)
    593 			warnx("%s", sqlite3_errmsg(db));
    594 		sqlite3_finalize(stmt);
    595 		return;
    596 	}
    597 
    598 	idx = sqlite3_bind_parameter_index(stmt, ":parent");
    599 	rc = sqlite3_bind_text(stmt, idx, parent, -1, NULL);
    600 	if (rc != SQLITE_OK) {
    601 		if (mflags.verbosity)
    602 			warnx("%s", sqlite3_errmsg(db));
    603 		sqlite3_finalize(stmt);
    604 		return;
    605 	}
    606 
    607 	idx = sqlite3_bind_parameter_index(stmt, ":file");
    608 	rc = sqlite3_bind_text(stmt, idx, file, -1, NULL);
    609 	if (rc != SQLITE_OK) {
    610 		if (mflags.verbosity)
    611 			warnx("%s", sqlite3_errmsg(db));
    612 		sqlite3_finalize(stmt);
    613 		return;
    614 	}
    615 
    616 	sqlite3_step(stmt);
    617 	sqlite3_finalize(stmt);
    618 }
    619 
    620 static void
    621 update_existing_entry(sqlite3 *db, const char *file, const char *hash,
    622     mandb_rec *rec, int *new_count, int *link_count, int *err_count)
    623 {
    624 	int update_count, rc, idx;
    625 	const char *inner_sqlstr;
    626 	sqlite3_stmt *inner_stmt;
    627 
    628 	update_count = sqlite3_total_changes(db);
    629 	inner_sqlstr = "UPDATE mandb_meta SET device = :device,"
    630 		       " inode = :inode, mtime = :mtime WHERE"
    631 		       " md5_hash = :md5 AND file = :file AND"
    632 		       " (device <> :device2 OR inode <> "
    633 		       "  :inode2 OR mtime <> :mtime2)";
    634 	rc = sqlite3_prepare_v2(db, inner_sqlstr, -1, &inner_stmt, NULL);
    635 	if (rc != SQLITE_OK) {
    636 		if (mflags.verbosity)
    637 			warnx("%s", sqlite3_errmsg(db));
    638 		return;
    639 	}
    640 	idx = sqlite3_bind_parameter_index(inner_stmt, ":device");
    641 	sqlite3_bind_int64(inner_stmt, idx, rec->device);
    642 	idx = sqlite3_bind_parameter_index(inner_stmt, ":inode");
    643 	sqlite3_bind_int64(inner_stmt, idx, rec->inode);
    644 	idx = sqlite3_bind_parameter_index(inner_stmt, ":mtime");
    645 	sqlite3_bind_int64(inner_stmt, idx, rec->mtime);
    646 	idx = sqlite3_bind_parameter_index(inner_stmt, ":md5");
    647 	sqlite3_bind_text(inner_stmt, idx, hash, -1, NULL);
    648 	idx = sqlite3_bind_parameter_index(inner_stmt, ":file");
    649 	sqlite3_bind_text(inner_stmt, idx, file, -1, NULL);
    650 	idx = sqlite3_bind_parameter_index(inner_stmt, ":device2");
    651 	sqlite3_bind_int64(inner_stmt, idx, rec->device);
    652 	idx = sqlite3_bind_parameter_index(inner_stmt, ":inode2");
    653 	sqlite3_bind_int64(inner_stmt, idx, rec->inode);
    654 	idx = sqlite3_bind_parameter_index(inner_stmt, ":mtime2");
    655 	sqlite3_bind_int64(inner_stmt, idx, rec->mtime);
    656 
    657 	rc = sqlite3_step(inner_stmt);
    658 	if (rc == SQLITE_DONE) {
    659 		/* Check if an update has been performed. */
    660 		if (update_count != sqlite3_total_changes(db)) {
    661 			if (mflags.verbosity == 2)
    662 				printf("Updated %s\n", file);
    663 			(*new_count)++;
    664 		} else {
    665 			/* Otherwise it was a hardlink. */
    666 			(*link_count)++;
    667 		}
    668 	} else {
    669 		if (mflags.verbosity == 2)
    670 			warnx("Could not update the meta data for %s", file);
    671 		(*err_count)++;
    672 	}
    673 	sqlite3_finalize(inner_stmt);
    674 }
    675 
    676 /* read_and_decompress --
    677  *	Reads the given file into memory. If it is compressed, decompress
    678  *	it before returning to the caller.
    679  */
    680 static int
    681 read_and_decompress(const char *file, void **bufp, size_t *len)
    682 {
    683 	size_t off;
    684 	ssize_t r;
    685 	struct archive *a;
    686 	struct archive_entry *ae;
    687 	char *buf;
    688 
    689 	if ((a = archive_read_new()) == NULL)
    690 		errx(EXIT_FAILURE, "memory allocation failed");
    691 
    692 	*bufp = NULL;
    693 	if (archive_read_support_compression_all(a) != ARCHIVE_OK ||
    694 	    archive_read_support_format_raw(a) != ARCHIVE_OK ||
    695 	    archive_read_open_filename(a, file, 65536) != ARCHIVE_OK ||
    696 	    archive_read_next_header(a, &ae) != ARCHIVE_OK)
    697 		goto archive_error;
    698 	*len = 65536;
    699 	buf = emalloc(*len);
    700 	off = 0;
    701 	for (;;) {
    702 		r = archive_read_data(a, buf + off, *len - off);
    703 		if (r == ARCHIVE_OK) {
    704 			archive_read_finish(a);
    705 			*bufp = buf;
    706 			*len = off;
    707 			return 0;
    708 		}
    709 		if (r <= 0) {
    710 			free(buf);
    711 			break;
    712 		}
    713 		off += r;
    714 		if (off == *len) {
    715 			*len *= 2;
    716 			if (*len < off) {
    717 				if (mflags.verbosity)
    718 					warnx("File too large: %s", file);
    719 				free(buf);
    720 				archive_read_finish(a);
    721 				return -1;
    722 			}
    723 			buf = erealloc(buf, *len);
    724 		}
    725 	}
    726 
    727 archive_error:
    728 	warnx("Error while reading `%s': %s", file, archive_error_string(a));
    729 	archive_read_finish(a);
    730 	return -1;
    731 }
    732 
    733 /* update_db --
    734  *	Does an incremental updation of the database by checking the file_cache.
    735  *	It parses and adds the pages which are present in file_cache,
    736  *	but not in the database.
    737  *	It also removes the pages which are present in the databse,
    738  *	but not in the file_cache.
    739  */
    740 static void
    741 update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)
    742 {
    743 	const char *sqlstr;
    744 	sqlite3_stmt *stmt = NULL;
    745 	char *file;
    746 	char *parent;
    747 	char *errmsg = NULL;
    748 	char *md5sum;
    749 	void *buf;
    750 	size_t buflen;
    751 	struct sql_row {
    752 		struct sql_row *next;
    753 		dev_t device;
    754 		ino_t inode;
    755 		time_t mtime;
    756 		char *parent;
    757 		char *file;
    758 	} *rows, *row;
    759 	int new_count = 0;	/* Counter for newly indexed/updated pages */
    760 	int total_count = 0;	/* Counter for total number of pages */
    761 	int err_count = 0;	/* Counter for number of failed pages */
    762 	int link_count = 0;	/* Counter for number of hard/sym links */
    763 	int md5_status;
    764 	int rc;
    765 
    766 	sqlstr = "SELECT device, inode, mtime, parent, file"
    767 	         " FROM metadb.file_cache fc"
    768 	         " WHERE NOT EXISTS(SELECT 1 FROM mandb_meta WHERE"
    769 	         "  device = fc.device AND inode = fc.inode AND "
    770 	         "  mtime = fc.mtime AND file = fc.file)";
    771 
    772 	rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
    773 	if (rc != SQLITE_OK) {
    774 		if (mflags.verbosity)
    775 			warnx("%s", sqlite3_errmsg(db));
    776 		close_db(db);
    777 		errx(EXIT_FAILURE, "Could not query file cache");
    778 	}
    779 
    780 	buf = NULL;
    781 	rows = NULL;
    782 	while (sqlite3_step(stmt) == SQLITE_ROW) {
    783 		row = emalloc(sizeof(struct sql_row));
    784 		row->device = sqlite3_column_int64(stmt, 0);
    785 		row->inode = sqlite3_column_int64(stmt, 1);
    786 		row->mtime = sqlite3_column_int64(stmt, 2);
    787 		row->parent = estrdup((const char *) sqlite3_column_text(stmt, 3));
    788 		row->file = estrdup((const char *) sqlite3_column_text(stmt, 4));
    789 		row->next = rows;
    790 		rows = row;
    791 		total_count++;
    792 	}
    793 	sqlite3_finalize(stmt);
    794 
    795 	for ( ; rows != NULL; free(parent), free(file), free(buf)) {
    796 		row = rows;
    797 		rows = rows->next;
    798 
    799 		rec->device = row->device;
    800 		rec->inode = row->inode;
    801 		rec->mtime = row->mtime;
    802 		parent = row->parent;
    803 		file = row->file;
    804 		free(row);
    805 
    806 		if (read_and_decompress(file, &buf, &buflen)) {
    807 			err_count++;
    808 			continue;
    809 		}
    810 		md5_status = check_md5(file, db, "mandb_meta", &md5sum, buf,
    811 		    buflen);
    812 		assert(md5sum != NULL);
    813 		if (md5_status == -1) {
    814 			if (mflags.verbosity)
    815 				warnx("An error occurred in checking md5 value"
    816 			      " for file %s", file);
    817 			err_count++;
    818 			continue;
    819 		}
    820 
    821 		if (md5_status == 0) {
    822 			/*
    823 			 * The MD5 hash is already present in the database,
    824 			 * so simply update the metadata.
    825 			 */
    826 			update_existing_entry(db, file, md5sum, rec,
    827 			    &new_count, &link_count, &err_count);
    828 			free(md5sum);
    829 			continue;
    830 		}
    831 
    832 		if (md5_status == 1) {
    833 			/*
    834 			 * The MD5 hash was not present in the database.
    835 			 * This means is either a new file or an updated file.
    836 			 * We should go ahead with parsing.
    837 			 */
    838 			if (chdir(parent) == -1) {
    839 				if (mflags.verbosity)
    840 					warn("chdir failed for `%s', could "
    841 					    "not index `%s'", parent, file);
    842 				err_count++;
    843 				free(md5sum);
    844 				continue;
    845 			}
    846 
    847 			if (mflags.verbosity == 2)
    848 				printf("Parsing: %s\n", file);
    849 			rec->md5_hash = md5sum;
    850 			rec->file_path = estrdup(file);
    851 			// file_path is freed by insert_into_db itself.
    852 			begin_parse(file, mp, rec, buf, buflen);
    853 			if (insert_into_db(db, rec) < 0) {
    854 				if (mflags.verbosity)
    855 					warnx("Error in indexing `%s'", file);
    856 				err_count++;
    857 			} else {
    858 				new_count++;
    859 			}
    860 		}
    861 	}
    862 
    863 	if (mflags.verbosity == 2) {
    864 		printf("Number of new or updated pages encountered: %d\n"
    865 		    "Number of hard links found: %d\n"
    866 		    "Number of pages that were successfully"
    867 		    " indexed or updated: %d\n"
    868 		    "Number of pages that could not be indexed"
    869 		    " due to errors: %d\n",
    870 		    total_count - link_count, link_count, new_count, err_count);
    871 	}
    872 
    873 	if (mflags.recreate)
    874 		return;
    875 
    876 	if (mflags.verbosity == 2)
    877 		printf("Deleting stale index entries\n");
    878 
    879 	sqlstr = "DELETE FROM mandb_meta WHERE file NOT IN"
    880 		 " (SELECT file FROM metadb.file_cache);"
    881 		 "DELETE FROM mandb_links WHERE md5_hash NOT IN"
    882 		 " (SELECT md5_hash from mandb_meta);"
    883 		 "DROP TABLE metadb.file_cache;"
    884 		 "DELETE FROM mandb WHERE rowid NOT IN"
    885 		 " (SELECT id FROM mandb_meta);";
    886 
    887 	sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
    888 	if (errmsg != NULL) {
    889 		warnx("Removing old entries failed: %s", errmsg);
    890 		warnx("Please rebuild database from scratch with -f.");
    891 		free(errmsg);
    892 		return;
    893 	}
    894 }
    895 
    896 /*
    897  * begin_parse --
    898  *  parses the man page using libmandoc
    899  */
    900 static void
    901 begin_parse(const char *file, struct mparse *mp, mandb_rec *rec,
    902     const void *buf, size_t len)
    903 {
    904 	struct roff_man *roff;
    905 	mparse_reset(mp);
    906 
    907 	rec->xr_found = 0;
    908 
    909 	if (mparse_readmem(mp, buf, len, file) >= MANDOCLEVEL_BADARG) {
    910 		/* Printing this warning at verbosity level 2
    911 		 * because some packages from pkgsrc might trigger several
    912 		 * of such warnings.
    913 		 */
    914 		if (mflags.verbosity == 2)
    915 			warnx("%s: Parse failure", file);
    916 		return;
    917 	}
    918 
    919 	mparse_result(mp, &roff, NULL);
    920 	if (roff == NULL) {
    921 		if (mflags.verbosity == 2)
    922 			warnx("Not a roff(7) page");
    923 		return;
    924 	}
    925 
    926 	if (roff->macroset == MACROSET_MDOC) {
    927 		mdoc_validate(roff);
    928 		rec->page_type = MDOC;
    929 		proff_node(roff->first->child, rec, mdocs);
    930 	} else if (roff->macroset == MACROSET_MAN) {
    931 		man_validate(roff);
    932 		rec->page_type = MAN;
    933 		proff_node(roff->first->child, rec, mans);
    934 	} else
    935 		warnx("Unknown macroset %d", roff->macroset);
    936 	set_machine(roff, rec);
    937 	set_section(roff, rec);
    938 }
    939 
    940 /*
    941  * set_section --
    942  *  Extracts the section number and normalizes it to only the numeric part
    943  *  (Which should be the first character of the string).
    944  */
    945 static void
    946 set_section(const struct roff_man *rm, mandb_rec *rec)
    947 {
    948 	if (!rm)
    949 		return;
    950 	const struct roff_meta *rm_meta = &rm->meta;
    951 	const char *s = rm_meta->msec == NULL ? "?" : rm_meta->msec;
    952 	easprintf(&rec->section, "%s", s);
    953 	if (rec->section[0] == '?' && mflags.verbosity == 2)
    954 		warnx("%s: Missing section number", rec->file_path);
    955 }
    956 
    957 /*
    958  * get_machine --
    959  *  Extracts the machine architecture information if available.
    960  */
    961 static void
    962 set_machine(const struct roff_man *rm, mandb_rec *rec)
    963 {
    964 	if (rm == NULL)
    965 		return;
    966 	const struct roff_meta *rm_meta = &rm->meta;
    967 	if (rm_meta->arch)
    968 		rec->machine = estrdup(rm_meta->arch);
    969 }
    970 
    971 /*
    972  * pmdoc_Nm --
    973  *  Extracts the Name of the manual page from the .Nm macro
    974  */
    975 static void
    976 pmdoc_Nm(const struct roff_node *n, mandb_rec *rec)
    977 {
    978 	if (n->sec != SEC_NAME)
    979 		return;
    980 
    981 	for (n = n->child; n; n = n->next) {
    982 		if (n->type == ROFFT_TEXT) {
    983 			char *escaped_name = parse_escape(n->string);
    984 			concat(&rec->name, escaped_name);
    985 			free(escaped_name);
    986 		}
    987 	}
    988 }
    989 
    990 /*
    991  * pmdoc_Nd --
    992  *  Extracts the one line description of the man page from the .Nd macro
    993  */
    994 static void
    995 pmdoc_Nd(const struct roff_node *n, mandb_rec *rec)
    996 {
    997 	if (n->type == ROFFT_BODY)
    998 		deroff(&rec->name_desc, n);
    999 	if (rec->name_desc)
   1000 		replace_hyph(rec->name_desc);
   1001 
   1002 }
   1003 
   1004 /*
   1005  * pmdoc_macro_handler--
   1006  *  This function is a single point of handling all the special macros that we
   1007  *  want to handle especially. For example the .Xr macro for properly parsing
   1008  *  the referenced page name along with the section number, or the .Pp macro
   1009  *  for adding a new line whenever we encounter it.
   1010  */
   1011 static void
   1012 pmdoc_macro_handler(const struct roff_node *n, mandb_rec *rec, int doct)
   1013 {
   1014 	const struct roff_node *sn;
   1015 	assert(n);
   1016 
   1017 	switch (doct) {
   1018 	/*  Parse the man page references.
   1019 	 * Basically the .Xr macros are used like:
   1020 	 *  .Xr ls 1
   1021  	 *  and formatted like this:
   1022 	 *  ls(1)
   1023 	 *  Prepare a buffer to format the data like the above example and call
   1024 	 *  pmdoc_parse_section to append it.
   1025 	 */
   1026 	case MDOC_Xr:
   1027 		n = n->child;
   1028 		while (n->type != ROFFT_TEXT && n->next)
   1029 			n = n->next;
   1030 
   1031 		if (n && n->type != ROFFT_TEXT)
   1032 			return;
   1033 		sn = n;
   1034 		if (n->next)
   1035 			n = n->next;
   1036 
   1037 		while (n->type != ROFFT_TEXT && n->next)
   1038 			n = n->next;
   1039 
   1040 		if (n && n->type == ROFFT_TEXT) {
   1041 			char *buf;
   1042 			easprintf(&buf, "%s(%s)", sn->string, n->string);
   1043 			mdoc_parse_section(n->sec, buf, rec);
   1044 			free(buf);
   1045 		}
   1046 
   1047 		break;
   1048 
   1049 	/* Parse the .Pp macro to add a new line */
   1050 	case MDOC_Pp:
   1051 		if (n->type == ROFFT_TEXT)
   1052 			mdoc_parse_section(n->sec, "\n", rec);
   1053 		break;
   1054 	default:
   1055 		break;
   1056 	}
   1057 
   1058 }
   1059 
   1060 /*
   1061  * pmdoc_Xr, pmdoc_Pp--
   1062  *  Empty stubs.
   1063  *  The parser calls these functions each time it encounters
   1064  *  a .Xr or .Pp macro. We are parsing all the data from
   1065  *  the pmdoc_Sh function, so don't do anything here.
   1066  *  (See if else blocks in pmdoc_Sh.)
   1067  */
   1068 static void
   1069 pmdoc_Xr(const struct roff_node *n, mandb_rec *rec)
   1070 {
   1071 }
   1072 
   1073 static void
   1074 pmdoc_Pp(const struct roff_node *n, mandb_rec *rec)
   1075 {
   1076 }
   1077 
   1078 /*
   1079  * pmdoc_Sh --
   1080  *  Called when a .Sh macro is encountered and tries to parse its body
   1081  */
   1082 static void
   1083 pmdoc_Sh(const struct roff_node *n, mandb_rec *rec)
   1084 {
   1085 	if (n == NULL)
   1086 		return;
   1087 
   1088 	switch (n->sec) {
   1089 	case SEC_NAME:
   1090 	case SEC_SYNOPSIS:
   1091 	case SEC_EXAMPLES:
   1092 	case SEC_STANDARDS:
   1093 	case SEC_HISTORY:
   1094 	case SEC_AUTHORS:
   1095 	case SEC_BUGS:
   1096 		/*
   1097 		 * We don't care about text from these sections
   1098 		 */
   1099 		return;
   1100 	default:
   1101 		break;
   1102 	}
   1103 
   1104 	if (n->type == ROFFT_BLOCK)
   1105 		mdoc_parse_Sh(n->body, rec);
   1106 }
   1107 
   1108 /*
   1109  *  Called from pmdoc_Sh to parse body of a .Sh macro. It calls
   1110  *  mdoc_parse_section to append the data to the section specific buffer.
   1111  *  Two special macros which may occur inside the body of Sh are .Nm and .Xr and
   1112  *  they need special handling, thus the separate if branches for them.
   1113  */
   1114 static void
   1115 mdoc_parse_Sh(const struct roff_node *n, mandb_rec *rec)
   1116 {
   1117 	if (n == NULL || (n->type != ROFFT_TEXT && n->tok == MDOC_MAX))
   1118 		return;
   1119 	int xr_found = 0;
   1120 
   1121 	if (n->type == ROFFT_TEXT) {
   1122 		mdoc_parse_section(n->sec, n->string, rec);
   1123 	} else if (mdocs[n->tok] == pmdoc_Nm && rec->name != NULL) {
   1124 		/*
   1125 		 * When encountering a .Nm macro, substitute it
   1126 		 * with its previously cached value of the argument.
   1127 		 */
   1128 		mdoc_parse_section(n->sec, rec->name, rec);
   1129 	} else if (mdocs[n->tok] == pmdoc_Xr) {
   1130 		/*
   1131 		 * When encountering other inline macros,
   1132 		 * call pmdoc_macro_handler.
   1133 		 */
   1134 		pmdoc_macro_handler(n, rec, MDOC_Xr);
   1135 		xr_found = 1;
   1136 	} else if (mdocs[n->tok] == pmdoc_Pp) {
   1137 		pmdoc_macro_handler(n, rec, MDOC_Pp);
   1138 	}
   1139 
   1140 	/*
   1141 	 * If an Xr macro was encountered then the child node has
   1142 	 * already been explored by pmdoc_macro_handler.
   1143 	 */
   1144 	if (xr_found == 0)
   1145 		mdoc_parse_Sh(n->child, rec);
   1146 	mdoc_parse_Sh(n->next, rec);
   1147 }
   1148 
   1149 /*
   1150  * mdoc_parse_section--
   1151  *  Utility function for parsing sections of the mdoc type pages.
   1152  *  Takes two params:
   1153  *   1. sec is an enum which indicates the section in which we are present
   1154  *   2. string is the string which we need to append to the secbuff for this
   1155  *      particular section.
   1156  *  The function appends string to the global section buffer and returns.
   1157  */
   1158 static void
   1159 mdoc_parse_section(enum roff_sec sec, const char *string, mandb_rec *rec)
   1160 {
   1161 	/*
   1162 	 * If the user specified the 'l' flag, then parse and store only the
   1163 	 * NAME section. Ignore the rest.
   1164 	 */
   1165 	if (mflags.limit)
   1166 		return;
   1167 
   1168 	switch (sec) {
   1169 	case SEC_LIBRARY:
   1170 		append(&rec->lib, string);
   1171 		break;
   1172 	case SEC_RETURN_VALUES:
   1173 		append(&rec->return_vals, string);
   1174 		break;
   1175 	case SEC_ENVIRONMENT:
   1176 		append(&rec->env, string);
   1177 		break;
   1178 	case SEC_FILES:
   1179 		append(&rec->files, string);
   1180 		break;
   1181 	case SEC_EXIT_STATUS:
   1182 		append(&rec->exit_status, string);
   1183 		break;
   1184 	case SEC_DIAGNOSTICS:
   1185 		append(&rec->diagnostics, string);
   1186 		break;
   1187 	case SEC_ERRORS:
   1188 		append(&rec->errors, string);
   1189 		break;
   1190 	default:
   1191 		append(&rec->desc, string);
   1192 		break;
   1193 	}
   1194 }
   1195 
   1196 static void
   1197 proff_node(const struct roff_node *n, mandb_rec *rec, const proff_nf *func)
   1198 {
   1199 	if (n == NULL)
   1200 		return;
   1201 
   1202 	switch (n->type) {
   1203 	case (ROFFT_BODY):
   1204 		/* FALLTHROUGH */
   1205 	case (ROFFT_BLOCK):
   1206 		/* FALLTHROUGH */
   1207 	case (ROFFT_ELEM):
   1208 		if (func[n->tok] != NULL)
   1209 			(*func[n->tok])(n, rec);
   1210 		break;
   1211 	default:
   1212 		break;
   1213 	}
   1214 
   1215 	proff_node(n->child, rec, func);
   1216 	proff_node(n->next, rec, func);
   1217 }
   1218 
   1219 /*
   1220  * pman_parse_name --
   1221  *  Parses the NAME section and puts the complete content in the name_desc
   1222  *  variable.
   1223  */
   1224 static void
   1225 pman_parse_name(const struct roff_node *n, mandb_rec *rec)
   1226 {
   1227 	if (n == NULL)
   1228 		return;
   1229 
   1230 	if (n->type == ROFFT_TEXT) {
   1231 		char *tmp = parse_escape(n->string);
   1232 		concat(&rec->name_desc, tmp);
   1233 		free(tmp);
   1234 	}
   1235 
   1236 	if (n->child)
   1237 		pman_parse_name(n->child, rec);
   1238 
   1239 	if(n->next)
   1240 		pman_parse_name(n->next, rec);
   1241 }
   1242 
   1243 /*
   1244  * A stub function to be able to parse the macros like .B embedded inside
   1245  * a section.
   1246  */
   1247 static void
   1248 pman_block(const struct roff_node *n, mandb_rec *rec)
   1249 {
   1250 }
   1251 
   1252 /*
   1253  * pman_sh --
   1254  * This function does one of the two things:
   1255  *  1. If the present section is NAME, then it will:
   1256  *    (a) Extract the name of the page (in case of multiple comma separated
   1257  *        names, it will pick up the first one).
   1258  *    (b) Build a space spearated list of all the symlinks/hardlinks to
   1259  *        this page and store in the buffer 'links'. These are extracted from
   1260  *        the comma separated list of names in the NAME section as well.
   1261  *    (c) Move on to the one line description section, which is after the list
   1262  *        of names in the NAME section.
   1263  *  2. Otherwise, it will check the section name and call the man_parse_section
   1264  *     function, passing the enum corresponding to that section.
   1265  */
   1266 static void
   1267 pman_sh(const struct roff_node *n, mandb_rec *rec)
   1268 {
   1269 	static const struct {
   1270 		enum man_sec section;
   1271 		const char *header;
   1272 	} mapping[] = {
   1273 	    { MANSEC_DESCRIPTION, "DESCRIPTION" },
   1274 	    { MANSEC_SYNOPSIS, "SYNOPSIS" },
   1275 	    { MANSEC_LIBRARY, "LIBRARY" },
   1276 	    { MANSEC_ERRORS, "ERRORS" },
   1277 	    { MANSEC_FILES, "FILES" },
   1278 	    { MANSEC_RETURN_VALUES, "RETURN VALUE" },
   1279 	    { MANSEC_RETURN_VALUES, "RETURN VALUES" },
   1280 	    { MANSEC_EXIT_STATUS, "EXIT STATUS" },
   1281 	    { MANSEC_EXAMPLES, "EXAMPLES" },
   1282 	    { MANSEC_EXAMPLES, "EXAMPLE" },
   1283 	    { MANSEC_STANDARDS, "STANDARDS" },
   1284 	    { MANSEC_HISTORY, "HISTORY" },
   1285 	    { MANSEC_BUGS, "BUGS" },
   1286 	    { MANSEC_AUTHORS, "AUTHORS" },
   1287 	    { MANSEC_COPYRIGHT, "COPYRIGHT" },
   1288 	};
   1289 	const struct roff_node *head;
   1290 	char *name_desc;
   1291 	size_t sz;
   1292 	size_t i;
   1293 
   1294 	if ((head = n->parent->head) == NULL || (head = head->child) == NULL ||
   1295 	    head->type != ROFFT_TEXT)
   1296 		return;
   1297 
   1298 	/*
   1299 	 * Check if this section should be extracted and
   1300 	 * where it should be stored. Handled the trival cases first.
   1301 	 */
   1302 	for (i = 0; i < sizeof(mapping) / sizeof(mapping[0]); ++i) {
   1303 		if (strcmp(head->string, mapping[i].header) == 0) {
   1304 			man_parse_section(mapping[i].section, n, rec);
   1305 			return;
   1306 		}
   1307 	}
   1308 
   1309 	if (strcmp(head->string, "NAME") == 0) {
   1310 		/*
   1311 		 * We are in the NAME section.
   1312 		 * pman_parse_name will put the complete content in name_desc.
   1313 		 */
   1314 		pman_parse_name(n, rec);
   1315 
   1316 		name_desc = rec->name_desc;
   1317 		if (name_desc == NULL)
   1318 			return;
   1319 
   1320 		/* Remove any leading spaces. */
   1321 		while (name_desc[0] == ' ')
   1322 			name_desc++;
   1323 
   1324 		/* If the line begins with a "\&", avoid those */
   1325 		if (name_desc[0] == '\\' && name_desc[1] == '&')
   1326 			name_desc += 2;
   1327 
   1328 		/* Now name_desc should be left with a comma-space
   1329 		 * separated list of names and the one line description
   1330 		 * of the page:
   1331 		 *     "a, b, c \- sample description"
   1332 		 * Take out the first name, before the first comma
   1333 		 * (or space) and store it in rec->name.
   1334 		 * If the page has aliases then they should be
   1335 		 * in the form of a comma separated list.
   1336 		 * Keep looping while there is a comma in name_desc,
   1337 		 * extract the alias name and store in rec->links.
   1338 		 * When there are no more commas left, break out.
   1339 		 */
   1340 		int has_alias = 0;	// Any more aliases left?
   1341 		while (*name_desc) {
   1342 			/* Remove any leading spaces or hyphens. */
   1343 			if (name_desc[0] == ' ' || name_desc[0] == '-') {
   1344 				name_desc++;
   1345 				continue;
   1346 			}
   1347 			sz = strcspn(name_desc, ", ");
   1348 
   1349 			/* Extract the first term and store it in rec->name. */
   1350 			if (rec->name == NULL) {
   1351 				if (name_desc[sz] == ',')
   1352 					has_alias = 1;
   1353 				rec->name = estrndup(name_desc, sz);
   1354 				/* XXX This would only happen with a poorly
   1355 				 * written man page, maybe warn? */
   1356 				if (name_desc[sz] == '\0')
   1357 					break;
   1358 				name_desc += sz + 1;
   1359 				continue;
   1360 			}
   1361 
   1362 			/*
   1363 			 * Once rec->name is set, rest of the names
   1364 			 * are to be treated as links or aliases.
   1365 			 */
   1366 			if (rec->name && has_alias) {
   1367 				if (name_desc[sz] != ',') {
   1368 					/* No more commas left --> no more
   1369 					 * aliases to take out */
   1370 					has_alias = 0;
   1371 				}
   1372 				concat2(&rec->links, name_desc, sz);
   1373 				/* XXX This would only happen with a poorly
   1374 				 * written man page, maybe warn? */
   1375 				if (name_desc[sz] == '\0')
   1376 					break;
   1377 				name_desc += sz + 1;
   1378 				continue;
   1379 			}
   1380 			break;
   1381 		}
   1382 
   1383 		/* Parse any escape sequences that might be there */
   1384 		char *temp = parse_escape(name_desc);
   1385 		free(rec->name_desc);
   1386 		rec->name_desc = temp;
   1387 		temp = parse_escape(rec->name);
   1388 		free(rec->name);
   1389 		rec->name = temp;
   1390 		return;
   1391 	}
   1392 
   1393 	/* The RETURN VALUE section might be specified in multiple ways */
   1394 	if (strcmp(head->string, "RETURN") == 0 &&
   1395 	    head->next != NULL && head->next->type == ROFFT_TEXT &&
   1396 	    (strcmp(head->next->string, "VALUE") == 0 ||
   1397 	    strcmp(head->next->string, "VALUES") == 0)) {
   1398 		man_parse_section(MANSEC_RETURN_VALUES, n, rec);
   1399 		return;
   1400 	}
   1401 
   1402 	/*
   1403 	 * EXIT STATUS section can also be specified all on one line or on two
   1404 	 * separate lines.
   1405 	 */
   1406 	if (strcmp(head->string, "EXIT") == 0 &&
   1407 	    head->next != NULL && head->next->type == ROFFT_TEXT &&
   1408 	    strcmp(head->next->string, "STATUS") == 0) {
   1409 		man_parse_section(MANSEC_EXIT_STATUS, n, rec);
   1410 		return;
   1411 	}
   1412 
   1413 	/* Store the rest of the content in desc. */
   1414 	man_parse_section(MANSEC_NONE, n, rec);
   1415 }
   1416 
   1417 /*
   1418  * pman_parse_node --
   1419  *  Generic function to iterate through a node. Usually called from
   1420  *  man_parse_section to parse a particular section of the man page.
   1421  */
   1422 static void
   1423 pman_parse_node(const struct roff_node *n, secbuff *s)
   1424 {
   1425 	if (n == NULL)
   1426 		return;
   1427 
   1428 	if (n->type == ROFFT_TEXT)
   1429 		append(s, n->string);
   1430 
   1431 	pman_parse_node(n->child, s);
   1432 	pman_parse_node(n->next, s);
   1433 }
   1434 
   1435 /*
   1436  * man_parse_section --
   1437  *  Takes two parameters:
   1438  *   sec: Tells which section we are present in
   1439  *   n: Is the present node of the AST.
   1440  * Depending on the section, we call pman_parse_node to parse that section and
   1441  * concatenate the content from that section into the buffer for that section.
   1442  */
   1443 static void
   1444 man_parse_section(enum man_sec sec, const struct roff_node *n, mandb_rec *rec)
   1445 {
   1446 	/*
   1447 	 * If the user sepecified the 'l' flag then just parse
   1448 	 * the NAME section, ignore the rest.
   1449 	 */
   1450 	if (mflags.limit)
   1451 		return;
   1452 
   1453 	switch (sec) {
   1454 	case MANSEC_LIBRARY:
   1455 		pman_parse_node(n, &rec->lib);
   1456 		break;
   1457 	case MANSEC_RETURN_VALUES:
   1458 		pman_parse_node(n, &rec->return_vals);
   1459 		break;
   1460 	case MANSEC_ENVIRONMENT:
   1461 		pman_parse_node(n, &rec->env);
   1462 		break;
   1463 	case MANSEC_FILES:
   1464 		pman_parse_node(n, &rec->files);
   1465 		break;
   1466 	case MANSEC_EXIT_STATUS:
   1467 		pman_parse_node(n, &rec->exit_status);
   1468 		break;
   1469 	case MANSEC_DIAGNOSTICS:
   1470 		pman_parse_node(n, &rec->diagnostics);
   1471 		break;
   1472 	case MANSEC_ERRORS:
   1473 		pman_parse_node(n, &rec->errors);
   1474 		break;
   1475 	case MANSEC_NAME:
   1476 	case MANSEC_SYNOPSIS:
   1477 	case MANSEC_EXAMPLES:
   1478 	case MANSEC_STANDARDS:
   1479 	case MANSEC_HISTORY:
   1480 	case MANSEC_BUGS:
   1481 	case MANSEC_AUTHORS:
   1482 	case MANSEC_COPYRIGHT:
   1483 		break;
   1484 	default:
   1485 		pman_parse_node(n, &rec->desc);
   1486 		break;
   1487 	}
   1488 
   1489 }
   1490 
   1491 /*
   1492  * insert_into_db --
   1493  *  Inserts the parsed data of the man page in the Sqlite databse.
   1494  *  If any of the values is NULL, then we cleanup and return -1 indicating
   1495  *  an error.
   1496  *  Otherwise, store the data in the database and return 0.
   1497  */
   1498 static int
   1499 insert_into_db(sqlite3 *db, mandb_rec *rec)
   1500 {
   1501 	int rc = 0;
   1502 	int idx = -1;
   1503 	const char *sqlstr = NULL;
   1504 	sqlite3_stmt *stmt = NULL;
   1505 	char *ln = NULL;
   1506 	char *errmsg = NULL;
   1507 	long int mandb_rowid;
   1508 
   1509 	/*
   1510 	 * At the very minimum we want to make sure that we store
   1511 	 * the following data:
   1512 	 *   Name, one line description, and the MD5 hash
   1513 	 */
   1514 	if (rec->name == NULL || rec->name_desc == NULL ||
   1515 	    rec->md5_hash == NULL) {
   1516 		cleanup(rec);
   1517 		return -1;
   1518 	}
   1519 
   1520 	/* Write null byte at the end of all the sec_buffs */
   1521 	rec->desc.data[rec->desc.offset] = 0;
   1522 	rec->lib.data[rec->lib.offset] = 0;
   1523 	rec->env.data[rec->env.offset] = 0;
   1524 	rec->return_vals.data[rec->return_vals.offset] = 0;
   1525 	rec->exit_status.data[rec->exit_status.offset] = 0;
   1526 	rec->files.data[rec->files.offset] = 0;
   1527 	rec->diagnostics.data[rec->diagnostics.offset] = 0;
   1528 	rec->errors.data[rec->errors.offset] = 0;
   1529 
   1530 	/*
   1531 	 * In case of a mdoc page: (sorry, no better place to put this code)
   1532 	 * parse the comma separated list of names of man pages,
   1533 	 * the first name will be stored in the mandb table, rest will be
   1534 	 * treated as links and put in the mandb_links table.
   1535 	 */
   1536 	if (rec->page_type == MDOC) {
   1537 		char *tmp;
   1538 		rec->links = estrdup(rec->name);
   1539 		free(rec->name);
   1540 		size_t sz = strcspn(rec->links, " \0");
   1541 		rec->name = emalloc(sz + 1);
   1542 		memcpy(rec->name, rec->links, sz);
   1543 		if(rec->name[sz - 1] == ',')
   1544 			rec->name[sz - 1] = 0;
   1545 		else
   1546 			rec->name[sz] = 0;
   1547 		while (rec->links[sz] == ' ')
   1548 			++sz;
   1549 		tmp = estrdup(rec->links + sz);
   1550 		free(rec->links);
   1551 		rec->links = tmp;
   1552 	}
   1553 
   1554 /*------------------------ Populate the mandb table---------------------------*/
   1555 	sqlstr = "INSERT INTO mandb VALUES (:section, :name, :name_desc, :desc,"
   1556 		 " :lib, :return_vals, :env, :files, :exit_status,"
   1557 		 " :diagnostics, :errors, :md5_hash, :machine)";
   1558 
   1559 	rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
   1560 	if (rc != SQLITE_OK)
   1561 		goto Out;
   1562 
   1563 	idx = sqlite3_bind_parameter_index(stmt, ":name");
   1564 	rc = sqlite3_bind_text(stmt, idx, rec->name, -1, NULL);
   1565 	if (rc != SQLITE_OK) {
   1566 		sqlite3_finalize(stmt);
   1567 		goto Out;
   1568 	}
   1569 
   1570 	idx = sqlite3_bind_parameter_index(stmt, ":section");
   1571 	rc = sqlite3_bind_text(stmt, idx, rec->section, -1, NULL);
   1572 	if (rc != SQLITE_OK) {
   1573 		sqlite3_finalize(stmt);
   1574 		goto Out;
   1575 	}
   1576 
   1577 	idx = sqlite3_bind_parameter_index(stmt, ":name_desc");
   1578 	rc = sqlite3_bind_text(stmt, idx, rec->name_desc, -1, NULL);
   1579 	if (rc != SQLITE_OK) {
   1580 		sqlite3_finalize(stmt);
   1581 		goto Out;
   1582 	}
   1583 
   1584 	idx = sqlite3_bind_parameter_index(stmt, ":desc");
   1585 	rc = sqlite3_bind_text(stmt, idx, rec->desc.data,
   1586 	                       rec->desc.offset + 1, NULL);
   1587 	if (rc != SQLITE_OK) {
   1588 		sqlite3_finalize(stmt);
   1589 		goto Out;
   1590 	}
   1591 
   1592 	idx = sqlite3_bind_parameter_index(stmt, ":lib");
   1593 	rc = sqlite3_bind_text(stmt, idx, rec->lib.data,
   1594 	    rec->lib.offset + 1, NULL);
   1595 	if (rc != SQLITE_OK) {
   1596 		sqlite3_finalize(stmt);
   1597 		goto Out;
   1598 	}
   1599 
   1600 	idx = sqlite3_bind_parameter_index(stmt, ":return_vals");
   1601 	rc = sqlite3_bind_text(stmt, idx, rec->return_vals.data,
   1602 	                      rec->return_vals.offset + 1, NULL);
   1603 	if (rc != SQLITE_OK) {
   1604 		sqlite3_finalize(stmt);
   1605 		goto Out;
   1606 	}
   1607 
   1608 	idx = sqlite3_bind_parameter_index(stmt, ":env");
   1609 	rc = sqlite3_bind_text(stmt, idx, rec->env.data,
   1610 	    rec->env.offset + 1, NULL);
   1611 	if (rc != SQLITE_OK) {
   1612 		sqlite3_finalize(stmt);
   1613 		goto Out;
   1614 	}
   1615 
   1616 	idx = sqlite3_bind_parameter_index(stmt, ":files");
   1617 	rc = sqlite3_bind_text(stmt, idx, rec->files.data,
   1618 	                       rec->files.offset + 1, NULL);
   1619 	if (rc != SQLITE_OK) {
   1620 		sqlite3_finalize(stmt);
   1621 		goto Out;
   1622 	}
   1623 
   1624 	idx = sqlite3_bind_parameter_index(stmt, ":exit_status");
   1625 	rc = sqlite3_bind_text(stmt, idx, rec->exit_status.data,
   1626 	                       rec->exit_status.offset + 1, NULL);
   1627 	if (rc != SQLITE_OK) {
   1628 		sqlite3_finalize(stmt);
   1629 		goto Out;
   1630 	}
   1631 
   1632 	idx = sqlite3_bind_parameter_index(stmt, ":diagnostics");
   1633 	rc = sqlite3_bind_text(stmt, idx, rec->diagnostics.data,
   1634 	                       rec->diagnostics.offset + 1, NULL);
   1635 	if (rc != SQLITE_OK) {
   1636 		sqlite3_finalize(stmt);
   1637 		goto Out;
   1638 	}
   1639 
   1640 	idx = sqlite3_bind_parameter_index(stmt, ":errors");
   1641 	rc = sqlite3_bind_text(stmt, idx, rec->errors.data,
   1642 	                       rec->errors.offset + 1, NULL);
   1643 	if (rc != SQLITE_OK) {
   1644 		sqlite3_finalize(stmt);
   1645 		goto Out;
   1646 	}
   1647 
   1648 	idx = sqlite3_bind_parameter_index(stmt, ":md5_hash");
   1649 	rc = sqlite3_bind_text(stmt, idx, rec->md5_hash, -1, NULL);
   1650 	if (rc != SQLITE_OK) {
   1651 		sqlite3_finalize(stmt);
   1652 		goto Out;
   1653 	}
   1654 
   1655 	idx = sqlite3_bind_parameter_index(stmt, ":machine");
   1656 	if (rec->machine)
   1657 		rc = sqlite3_bind_text(stmt, idx, rec->machine, -1, NULL);
   1658 	else
   1659 		rc = sqlite3_bind_null(stmt, idx);
   1660 	if (rc != SQLITE_OK) {
   1661 		sqlite3_finalize(stmt);
   1662 		goto Out;
   1663 	}
   1664 
   1665 	rc = sqlite3_step(stmt);
   1666 	if (rc != SQLITE_DONE) {
   1667 		sqlite3_finalize(stmt);
   1668 		goto Out;
   1669 	}
   1670 
   1671 	sqlite3_finalize(stmt);
   1672 
   1673 	/* Get the row id of the last inserted row */
   1674 	mandb_rowid = sqlite3_last_insert_rowid(db);
   1675 
   1676 /*------------------------Populate the mandb_meta table-----------------------*/
   1677 	sqlstr = "INSERT INTO mandb_meta VALUES (:device, :inode, :mtime,"
   1678 		 " :file, :md5_hash, :id)";
   1679 	rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
   1680 	if (rc != SQLITE_OK)
   1681 		goto Out;
   1682 
   1683 	idx = sqlite3_bind_parameter_index(stmt, ":device");
   1684 	rc = sqlite3_bind_int64(stmt, idx, rec->device);
   1685 	if (rc != SQLITE_OK) {
   1686 		sqlite3_finalize(stmt);
   1687 		goto Out;
   1688 	}
   1689 
   1690 	idx = sqlite3_bind_parameter_index(stmt, ":inode");
   1691 	rc = sqlite3_bind_int64(stmt, idx, rec->inode);
   1692 	if (rc != SQLITE_OK) {
   1693 		sqlite3_finalize(stmt);
   1694 		goto Out;
   1695 	}
   1696 
   1697 	idx = sqlite3_bind_parameter_index(stmt, ":mtime");
   1698 	rc = sqlite3_bind_int64(stmt, idx, rec->mtime);
   1699 	if (rc != SQLITE_OK) {
   1700 		sqlite3_finalize(stmt);
   1701 		goto Out;
   1702 	}
   1703 
   1704 	idx = sqlite3_bind_parameter_index(stmt, ":file");
   1705 	rc = sqlite3_bind_text(stmt, idx, rec->file_path, -1, NULL);
   1706 	if (rc != SQLITE_OK) {
   1707 		sqlite3_finalize(stmt);
   1708 		goto Out;
   1709 	}
   1710 
   1711 	idx = sqlite3_bind_parameter_index(stmt, ":md5_hash");
   1712 	rc = sqlite3_bind_text(stmt, idx, rec->md5_hash, -1, NULL);
   1713 	if (rc != SQLITE_OK) {
   1714 		sqlite3_finalize(stmt);
   1715 		goto Out;
   1716 	}
   1717 
   1718 	idx = sqlite3_bind_parameter_index(stmt, ":id");
   1719 	rc = sqlite3_bind_int64(stmt, idx, mandb_rowid);
   1720 	if (rc != SQLITE_OK) {
   1721 		sqlite3_finalize(stmt);
   1722 		goto Out;
   1723 	}
   1724 
   1725 	rc = sqlite3_step(stmt);
   1726 	sqlite3_finalize(stmt);
   1727 	if (rc == SQLITE_CONSTRAINT_UNIQUE) {
   1728 		/* The *most* probable reason for reaching here is that
   1729 		 * the UNIQUE contraint on the file column of the mandb_meta
   1730 		 * table was violated.
   1731 		 * This can happen when a file was updated/modified.
   1732 		 * To fix this we need to do two things:
   1733 		 * 1. Delete the row for the older version of this file
   1734 		 *    from mandb table.
   1735 		 * 2. Run an UPDATE query to update the row for this file
   1736 		 *    in the mandb_meta table.
   1737 		 */
   1738 		warnx("Trying to update index for %s", rec->file_path);
   1739 		char *sql = sqlite3_mprintf("DELETE FROM mandb "
   1740 					    "WHERE rowid = (SELECT id"
   1741 					    "  FROM mandb_meta"
   1742 					    "  WHERE file = %Q)",
   1743 					    rec->file_path);
   1744 		sqlite3_exec(db, sql, NULL, NULL, &errmsg);
   1745 		sqlite3_free(sql);
   1746 		if (errmsg != NULL) {
   1747 			if (mflags.verbosity)
   1748 				warnx("%s", errmsg);
   1749 			free(errmsg);
   1750 		}
   1751 		sqlstr = "UPDATE mandb_meta SET device = :device,"
   1752 			 " inode = :inode, mtime = :mtime, id = :id,"
   1753 			 " md5_hash = :md5 WHERE file = :file";
   1754 		rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
   1755 		if (rc != SQLITE_OK) {
   1756 			if (mflags.verbosity)
   1757 				warnx("Update failed with error: %s",
   1758 			    sqlite3_errmsg(db));
   1759 			close_db(db);
   1760 			cleanup(rec);
   1761 			errx(EXIT_FAILURE,
   1762 			    "Consider running makemandb with -f option");
   1763 		}
   1764 
   1765 		idx = sqlite3_bind_parameter_index(stmt, ":device");
   1766 		sqlite3_bind_int64(stmt, idx, rec->device);
   1767 		idx = sqlite3_bind_parameter_index(stmt, ":inode");
   1768 		sqlite3_bind_int64(stmt, idx, rec->inode);
   1769 		idx = sqlite3_bind_parameter_index(stmt, ":mtime");
   1770 		sqlite3_bind_int64(stmt, idx, rec->mtime);
   1771 		idx = sqlite3_bind_parameter_index(stmt, ":id");
   1772 		sqlite3_bind_int64(stmt, idx, mandb_rowid);
   1773 		idx = sqlite3_bind_parameter_index(stmt, ":md5");
   1774 		sqlite3_bind_text(stmt, idx, rec->md5_hash, -1, NULL);
   1775 		idx = sqlite3_bind_parameter_index(stmt, ":file");
   1776 		sqlite3_bind_text(stmt, idx, rec->file_path, -1, NULL);
   1777 		rc = sqlite3_step(stmt);
   1778 		sqlite3_finalize(stmt);
   1779 
   1780 		if (rc != SQLITE_DONE) {
   1781 			if (mflags.verbosity)
   1782 				warnx("%s", sqlite3_errmsg(db));
   1783 			close_db(db);
   1784 			cleanup(rec);
   1785 			errx(EXIT_FAILURE,
   1786 			    "Consider running makemandb with -f option");
   1787 		}
   1788 	} else if (rc != SQLITE_DONE) {
   1789 		/* Otherwise make this error fatal */
   1790 		warnx("Failed at %s\n%s", rec->file_path, sqlite3_errmsg(db));
   1791 		cleanup(rec);
   1792 		close_db(db);
   1793 		exit(EXIT_FAILURE);
   1794 	}
   1795 
   1796 /*------------------------ Populate the mandb_links table---------------------*/
   1797 	char *str = NULL;
   1798 	char *links;
   1799 	if (rec->links && strlen(rec->links)) {
   1800 		links = rec->links;
   1801 		for(ln = strtok(links, " "); ln; ln = strtok(NULL, " ")) {
   1802 			if (ln[0] == ',')
   1803 				ln++;
   1804 			if(ln[strlen(ln) - 1] == ',')
   1805 				ln[strlen(ln) - 1] = 0;
   1806 
   1807 			str = sqlite3_mprintf("INSERT INTO mandb_links"
   1808 					      " VALUES (%Q, %Q, %Q, %Q, %Q)",
   1809 					      ln, rec->name, rec->section,
   1810 					      rec->machine, rec->md5_hash);
   1811 			sqlite3_exec(db, str, NULL, NULL, &errmsg);
   1812 			sqlite3_free(str);
   1813 			if (errmsg != NULL) {
   1814 				warnx("%s", errmsg);
   1815 				cleanup(rec);
   1816 				free(errmsg);
   1817 				return -1;
   1818 			}
   1819 		}
   1820 	}
   1821 
   1822 	cleanup(rec);
   1823 	return 0;
   1824 
   1825   Out:
   1826 	if (mflags.verbosity)
   1827 		warnx("%s", sqlite3_errmsg(db));
   1828 	cleanup(rec);
   1829 	return -1;
   1830 }
   1831 
   1832 /*
   1833  * check_md5--
   1834  *  Generates the md5 hash of the file and checks if it already doesn't exist
   1835  *  in the table (passed as the 3rd parameter).
   1836  *  This function is being used to avoid hardlinks.
   1837  *  On successful completion it will also set the value of the fourth parameter
   1838  *  to the md5 hash of the file (computed previously). It is the responsibility
   1839  *  of the caller to free this buffer.
   1840  *  Return values:
   1841  *  -1: If an error occurs somewhere and sets the md5 return buffer to NULL.
   1842  *  0: If the md5 hash does not exist in the table.
   1843  *  1: If the hash exists in the database.
   1844  */
   1845 static int
   1846 check_md5(const char *file, sqlite3 *db, const char *table, char **md5sum,
   1847     void *buf, size_t buflen)
   1848 {
   1849 	int rc = 0;
   1850 	int idx = -1;
   1851 	char *sqlstr = NULL;
   1852 	sqlite3_stmt *stmt = NULL;
   1853 
   1854 	assert(file != NULL);
   1855 	*md5sum = MD5Data(buf, buflen, NULL);
   1856 	if (*md5sum == NULL) {
   1857 		if (mflags.verbosity)
   1858 			warn("md5 failed: %s", file);
   1859 		return -1;
   1860 	}
   1861 
   1862 	easprintf(&sqlstr, "SELECT * FROM %s WHERE md5_hash = :md5_hash",
   1863 	    table);
   1864 	rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
   1865 	if (rc != SQLITE_OK) {
   1866 		free(sqlstr);
   1867 		free(*md5sum);
   1868 		*md5sum = NULL;
   1869 		return -1;
   1870 	}
   1871 
   1872 	idx = sqlite3_bind_parameter_index(stmt, ":md5_hash");
   1873 	rc = sqlite3_bind_text(stmt, idx, *md5sum, -1, NULL);
   1874 	if (rc != SQLITE_OK) {
   1875 		if (mflags.verbosity)
   1876 			warnx("%s", sqlite3_errmsg(db));
   1877 		sqlite3_finalize(stmt);
   1878 		free(sqlstr);
   1879 		free(*md5sum);
   1880 		*md5sum = NULL;
   1881 		return -1;
   1882 	}
   1883 
   1884 	if (sqlite3_step(stmt) == SQLITE_ROW) {
   1885 		sqlite3_finalize(stmt);
   1886 		free(sqlstr);
   1887 		return 0;
   1888 	}
   1889 
   1890 	sqlite3_finalize(stmt);
   1891 	free(sqlstr);
   1892 	return 1;
   1893 }
   1894 
   1895 /* Optimize the index for faster search */
   1896 static void
   1897 optimize(sqlite3 *db)
   1898 {
   1899 	const char *sqlstr;
   1900 	char *errmsg = NULL;
   1901 
   1902 	if (mflags.verbosity == 2)
   1903 		printf("Optimizing the database index\n");
   1904 	sqlstr = "INSERT INTO mandb(mandb) VALUES (\'optimize\');"
   1905 		 "VACUUM";
   1906 	sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
   1907 	if (errmsg != NULL) {
   1908 		if (mflags.verbosity)
   1909 			warnx("%s", errmsg);
   1910 		free(errmsg);
   1911 		return;
   1912 	}
   1913 }
   1914 
   1915 /*
   1916  * cleanup --
   1917  *  cleans up the global buffers
   1918  */
   1919 static void
   1920 cleanup(mandb_rec *rec)
   1921 {
   1922 	rec->desc.offset = 0;
   1923 	rec->lib.offset = 0;
   1924 	rec->return_vals.offset = 0;
   1925 	rec->env.offset = 0;
   1926 	rec->exit_status.offset = 0;
   1927 	rec->diagnostics.offset = 0;
   1928 	rec->errors.offset = 0;
   1929 	rec->files.offset = 0;
   1930 
   1931 	free(rec->machine);
   1932 	rec->machine = NULL;
   1933 
   1934 	free(rec->links);
   1935 	rec->links = NULL;
   1936 
   1937 	free(rec->file_path);
   1938 	rec->file_path = NULL;
   1939 
   1940 	free(rec->name);
   1941 	rec->name = NULL;
   1942 
   1943 	free(rec->name_desc);
   1944 	rec->name_desc = NULL;
   1945 
   1946 	free(rec->md5_hash);
   1947 	rec->md5_hash = NULL;
   1948 
   1949 	free(rec->section);
   1950 	rec->section = NULL;
   1951 }
   1952 
   1953 /*
   1954  * init_secbuffs--
   1955  *  Sets the value of buflen for all the sec_buff field of rec. And then
   1956  *  allocate memory to each sec_buff member of rec.
   1957  */
   1958 static void
   1959 init_secbuffs(mandb_rec *rec)
   1960 {
   1961 	/*
   1962 	 * Some sec_buff might need more memory, for example desc,
   1963 	 * which stores the data of the DESCRIPTION section,
   1964 	 * while some might need very small amount of memory.
   1965 	 * Therefore explicitly setting the value of buflen field for
   1966 	 * each sec_buff.
   1967 	 */
   1968 	rec->desc.buflen = 10 * BUFLEN;
   1969 	rec->desc.data = emalloc(rec->desc.buflen);
   1970 	rec->desc.offset = 0;
   1971 
   1972 	rec->lib.buflen = BUFLEN / 2;
   1973 	rec->lib.data = emalloc(rec->lib.buflen);
   1974 	rec->lib.offset = 0;
   1975 
   1976 	rec->return_vals.buflen = BUFLEN;
   1977 	rec->return_vals.data = emalloc(rec->return_vals.buflen);
   1978 	rec->return_vals.offset = 0;
   1979 
   1980 	rec->exit_status.buflen = BUFLEN;
   1981 	rec->exit_status.data = emalloc(rec->exit_status.buflen);
   1982 	rec->exit_status.offset = 0;
   1983 
   1984 	rec->env.buflen = BUFLEN;
   1985 	rec->env.data = emalloc(rec->env.buflen);
   1986 	rec->env.offset = 0;
   1987 
   1988 	rec->files.buflen = BUFLEN;
   1989 	rec->files.data = emalloc(rec->files.buflen);
   1990 	rec->files.offset = 0;
   1991 
   1992 	rec->diagnostics.buflen = BUFLEN;
   1993 	rec->diagnostics.data = emalloc(rec->diagnostics.buflen);
   1994 	rec->diagnostics.offset = 0;
   1995 
   1996 	rec->errors.buflen = BUFLEN;
   1997 	rec->errors.data = emalloc(rec->errors.buflen);
   1998 	rec->errors.offset = 0;
   1999 }
   2000 
   2001 /*
   2002  * free_secbuffs--
   2003  *  This function should be called at the end, when all the pages have been
   2004  *  parsed.
   2005  *  It frees the memory allocated to sec_buffs by init_secbuffs in the starting.
   2006  */
   2007 static void
   2008 free_secbuffs(mandb_rec *rec)
   2009 {
   2010 	free(rec->desc.data);
   2011 	free(rec->lib.data);
   2012 	free(rec->return_vals.data);
   2013 	free(rec->exit_status.data);
   2014 	free(rec->env.data);
   2015 	free(rec->files.data);
   2016 	free(rec->diagnostics.data);
   2017 	free(rec->errors.data);
   2018 }
   2019 
   2020 static void
   2021 replace_hyph(char *str)
   2022 {
   2023 	char *iter = str;
   2024 	while ((iter = strchr(iter, ASCII_HYPH)) != NULL)
   2025 		*iter = '-';
   2026 
   2027 	iter = str;
   2028 	while ((iter = strchr(iter, ASCII_NBRSP)) != NULL)
   2029 		*iter = '-';
   2030 }
   2031 
   2032 static char *
   2033 parse_escape(const char *str)
   2034 {
   2035 	const char *backslash, *last_backslash;
   2036 	char *result, *iter;
   2037 	size_t len;
   2038 
   2039 	assert(str);
   2040 
   2041 	last_backslash = str;
   2042 	backslash = strchr(str, '\\');
   2043 	if (backslash == NULL) {
   2044 		result = estrdup(str);
   2045 		replace_hyph(result);
   2046 		return result;
   2047 	}
   2048 
   2049 	result = emalloc(strlen(str) + 1);
   2050 	iter = result;
   2051 
   2052 	do {
   2053 		len = backslash - last_backslash;
   2054 		memcpy(iter, last_backslash, len);
   2055 		iter += len;
   2056 		if (backslash[1] == '-' || backslash[1] == ' ') {
   2057 			*iter++ = backslash[1];
   2058 			last_backslash = backslash + 2;
   2059 			backslash = strchr(last_backslash, '\\');
   2060 		} else {
   2061 			++backslash;
   2062 			mandoc_escape(&backslash, NULL, NULL);
   2063 			last_backslash = backslash;
   2064 			if (backslash == NULL)
   2065 				break;
   2066 			backslash = strchr(last_backslash, '\\');
   2067 		}
   2068 	} while (backslash != NULL);
   2069 	if (last_backslash != NULL)
   2070 		strcpy(iter, last_backslash);
   2071 
   2072 	replace_hyph(result);
   2073 	return result;
   2074 }
   2075 
   2076 /*
   2077  * append--
   2078  *  Concatenates a space and src at the end of sbuff->data (much like concat in
   2079  *  apropos-utils.c).
   2080  *  Rather than reallocating space for writing data, it uses the value of the
   2081  *  offset field of sec_buff to write new data at the free space left in the
   2082  *  buffer.
   2083  *  In case the size of the data to be appended exceeds the number of bytes left
   2084  *  in the buffer, it reallocates buflen number of bytes and then continues.
   2085  *  Value of offset field should be adjusted as new data is written.
   2086  *
   2087  *  NOTE: This function does not write the null byte at the end of the buffers,
   2088  *  write a null byte at the position pointed to by offset before inserting data
   2089  *  in the db.
   2090  */
   2091 static void
   2092 append(secbuff *sbuff, const char *src)
   2093 {
   2094 	short flag = 0;
   2095 	size_t srclen, newlen;
   2096 	char *temp;
   2097 
   2098 	assert(src != NULL);
   2099 	temp = parse_escape(src);
   2100 	srclen = strlen(temp);
   2101 
   2102 	if (sbuff->data == NULL) {
   2103 		sbuff->data = emalloc(sbuff->buflen);
   2104 		sbuff->offset = 0;
   2105 	}
   2106 
   2107 	newlen = sbuff->offset + srclen + 2;
   2108 	if (newlen >= sbuff->buflen) {
   2109 		while (sbuff->buflen < newlen)
   2110 			sbuff->buflen += sbuff->buflen;
   2111 		sbuff->data = erealloc(sbuff->data, sbuff->buflen);
   2112 		flag = 1;
   2113 	}
   2114 
   2115 	/* Append a space at the end of the buffer. */
   2116 	if (sbuff->offset || flag)
   2117 		sbuff->data[sbuff->offset++] = ' ';
   2118 	/* Now, copy src at the end of the buffer. */
   2119 	memcpy(sbuff->data + sbuff->offset, temp, srclen);
   2120 	sbuff->offset += srclen;
   2121 	free(temp);
   2122 }
   2123 
   2124 static void
   2125 usage(void)
   2126 {
   2127 	fprintf(stderr, "Usage: %s [-floQqv] [-C path]\n", getprogname());
   2128 	exit(1);
   2129 }
   2130