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