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