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