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