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