xref: /netbsd-src/usr.sbin/makemandb/makemandb.c (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
1 /*	$NetBSD: makemandb.c,v 1.37 2016/04/13 11:48:29 christos 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.37 2016/04/13 11:48:29 christos 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 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 				name_desc[sz] = 0;
1405 				rec->name = emalloc(sz + 1);
1406 				memcpy(rec->name, name_desc, sz + 1);
1407 				name_desc += sz + 1;
1408 				continue;
1409 			}
1410 
1411 			/*
1412 			 * Once rec->name is set, rest of the names
1413 			 * are to be treated as links or aliases.
1414 			 */
1415 			if (rec->name && has_alias) {
1416 				if (name_desc[sz] != ',') {
1417 					/* No more commas left -->
1418 					 * no more aliases to take out
1419 					 */
1420 					has_alias = 0;
1421 				}
1422 				name_desc[sz] = 0;
1423 				concat2(&rec->links, name_desc, sz);
1424 				name_desc += sz + 1;
1425 				continue;
1426 			}
1427 			break;
1428 		}
1429 
1430 		/* Parse any escape sequences that might be there */
1431 		char *temp = parse_escape(name_desc);
1432 		free(rec->name_desc);
1433 		rec->name_desc = temp;
1434 		temp = parse_escape(rec->name);
1435 		free(rec->name);
1436 		rec->name = temp;
1437 		return;
1438 	}
1439 
1440 	/* The RETURN VALUE section might be specified in multiple ways */
1441 	if (strcmp(head->string, "RETURN") == 0 &&
1442 	    head->next != NULL && head->next->type == MAN_TEXT &&
1443 	    (strcmp(head->next->string, "VALUE") == 0 ||
1444 	    strcmp(head->next->string, "VALUES") == 0)) {
1445 		man_parse_section(MANSEC_RETURN_VALUES, n, rec);
1446 		return;
1447 	}
1448 
1449 	/*
1450 	 * EXIT STATUS section can also be specified all on one line or on two
1451 	 * separate lines.
1452 	 */
1453 	if (strcmp(head->string, "EXIT") == 0 &&
1454 	    head->next != NULL && head->next->type == MAN_TEXT &&
1455 	    strcmp(head->next->string, "STATUS") == 0) {
1456 		man_parse_section(MANSEC_EXIT_STATUS, n, rec);
1457 		return;
1458 	}
1459 
1460 	/* Store the rest of the content in desc. */
1461 	man_parse_section(MANSEC_NONE, n, rec);
1462 }
1463 
1464 /*
1465  * pman_parse_node --
1466  *  Generic function to iterate through a node. Usually called from
1467  *  man_parse_section to parse a particular section of the man page.
1468  */
1469 static void
1470 pman_parse_node(const struct man_node *n, secbuff *s)
1471 {
1472 	if (n == NULL)
1473 		return;
1474 
1475 	if (n->type == MAN_TEXT)
1476 		append(s, n->string);
1477 
1478 	pman_parse_node(n->child, s);
1479 	pman_parse_node(n->next, s);
1480 }
1481 
1482 /*
1483  * man_parse_section --
1484  *  Takes two parameters:
1485  *   sec: Tells which section we are present in
1486  *   n: Is the present node of the AST.
1487  * Depending on the section, we call pman_parse_node to parse that section and
1488  * concatenate the content from that section into the buffer for that section.
1489  */
1490 static void
1491 man_parse_section(enum man_sec sec, const struct man_node *n, mandb_rec *rec)
1492 {
1493 	/*
1494 	 * If the user sepecified the 'l' flag then just parse
1495 	 * the NAME section, ignore the rest.
1496 	 */
1497 	if (mflags.limit)
1498 		return;
1499 
1500 	switch (sec) {
1501 	case MANSEC_LIBRARY:
1502 		pman_parse_node(n, &rec->lib);
1503 		break;
1504 	case MANSEC_RETURN_VALUES:
1505 		pman_parse_node(n, &rec->return_vals);
1506 		break;
1507 	case MANSEC_ENVIRONMENT:
1508 		pman_parse_node(n, &rec->env);
1509 		break;
1510 	case MANSEC_FILES:
1511 		pman_parse_node(n, &rec->files);
1512 		break;
1513 	case MANSEC_EXIT_STATUS:
1514 		pman_parse_node(n, &rec->exit_status);
1515 		break;
1516 	case MANSEC_DIAGNOSTICS:
1517 		pman_parse_node(n, &rec->diagnostics);
1518 		break;
1519 	case MANSEC_ERRORS:
1520 		pman_parse_node(n, &rec->errors);
1521 		break;
1522 	case MANSEC_NAME:
1523 	case MANSEC_SYNOPSIS:
1524 	case MANSEC_EXAMPLES:
1525 	case MANSEC_STANDARDS:
1526 	case MANSEC_HISTORY:
1527 	case MANSEC_BUGS:
1528 	case MANSEC_AUTHORS:
1529 	case MANSEC_COPYRIGHT:
1530 		break;
1531 	default:
1532 		pman_parse_node(n, &rec->desc);
1533 		break;
1534 	}
1535 
1536 }
1537 
1538 /*
1539  * insert_into_db --
1540  *  Inserts the parsed data of the man page in the Sqlite databse.
1541  *  If any of the values is NULL, then we cleanup and return -1 indicating
1542  *  an error.
1543  *  Otherwise, store the data in the database and return 0.
1544  */
1545 static int
1546 insert_into_db(sqlite3 *db, mandb_rec *rec)
1547 {
1548 	int rc = 0;
1549 	int idx = -1;
1550 	const char *sqlstr = NULL;
1551 	sqlite3_stmt *stmt = NULL;
1552 	char *ln = NULL;
1553 	char *errmsg = NULL;
1554 	long int mandb_rowid;
1555 
1556 	/*
1557 	 * At the very minimum we want to make sure that we store
1558 	 * the following data:
1559 	 *   Name, one line description, and the MD5 hash
1560 	 */
1561 	if (rec->name == NULL || rec->name_desc == NULL ||
1562 	    rec->md5_hash == NULL) {
1563 		cleanup(rec);
1564 		return -1;
1565 	}
1566 
1567 	/* Write null byte at the end of all the sec_buffs */
1568 	rec->desc.data[rec->desc.offset] = 0;
1569 	rec->lib.data[rec->lib.offset] = 0;
1570 	rec->env.data[rec->env.offset] = 0;
1571 	rec->return_vals.data[rec->return_vals.offset] = 0;
1572 	rec->exit_status.data[rec->exit_status.offset] = 0;
1573 	rec->files.data[rec->files.offset] = 0;
1574 	rec->diagnostics.data[rec->diagnostics.offset] = 0;
1575 	rec->errors.data[rec->errors.offset] = 0;
1576 
1577 	/*
1578 	 * In case of a mdoc page: (sorry, no better place to put this code)
1579 	 * parse the comma separated list of names of man pages,
1580 	 * the first name will be stored in the mandb table, rest will be
1581 	 * treated as links and put in the mandb_links table.
1582 	 */
1583 	if (rec->page_type == MDOC) {
1584 		char *tmp;
1585 		rec->links = estrdup(rec->name);
1586 		free(rec->name);
1587 		size_t sz = strcspn(rec->links, " \0");
1588 		rec->name = emalloc(sz + 1);
1589 		memcpy(rec->name, rec->links, sz);
1590 		if(rec->name[sz - 1] == ',')
1591 			rec->name[sz - 1] = 0;
1592 		else
1593 			rec->name[sz] = 0;
1594 		while (rec->links[sz] == ' ')
1595 			++sz;
1596 		tmp = estrdup(rec->links + sz);
1597 		free(rec->links);
1598 		rec->links = tmp;
1599 	}
1600 
1601 /*------------------------ Populate the mandb table---------------------------*/
1602 	sqlstr = "INSERT INTO mandb VALUES (:section, :name, :name_desc, :desc,"
1603 		 " :lib, :return_vals, :env, :files, :exit_status,"
1604 		 " :diagnostics, :errors, :md5_hash, :machine)";
1605 
1606 	rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
1607 	if (rc != SQLITE_OK)
1608 		goto Out;
1609 
1610 	idx = sqlite3_bind_parameter_index(stmt, ":name");
1611 	rc = sqlite3_bind_text(stmt, idx, rec->name, -1, NULL);
1612 	if (rc != SQLITE_OK) {
1613 		sqlite3_finalize(stmt);
1614 		goto Out;
1615 	}
1616 
1617 	idx = sqlite3_bind_parameter_index(stmt, ":section");
1618 	rc = sqlite3_bind_text(stmt, idx, rec->section, -1, NULL);
1619 	if (rc != SQLITE_OK) {
1620 		sqlite3_finalize(stmt);
1621 		goto Out;
1622 	}
1623 
1624 	idx = sqlite3_bind_parameter_index(stmt, ":name_desc");
1625 	rc = sqlite3_bind_text(stmt, idx, rec->name_desc, -1, NULL);
1626 	if (rc != SQLITE_OK) {
1627 		sqlite3_finalize(stmt);
1628 		goto Out;
1629 	}
1630 
1631 	idx = sqlite3_bind_parameter_index(stmt, ":desc");
1632 	rc = sqlite3_bind_text(stmt, idx, rec->desc.data,
1633 	                       rec->desc.offset + 1, NULL);
1634 	if (rc != SQLITE_OK) {
1635 		sqlite3_finalize(stmt);
1636 		goto Out;
1637 	}
1638 
1639 	idx = sqlite3_bind_parameter_index(stmt, ":lib");
1640 	rc = sqlite3_bind_text(stmt, idx, rec->lib.data,
1641 	    rec->lib.offset + 1, NULL);
1642 	if (rc != SQLITE_OK) {
1643 		sqlite3_finalize(stmt);
1644 		goto Out;
1645 	}
1646 
1647 	idx = sqlite3_bind_parameter_index(stmt, ":return_vals");
1648 	rc = sqlite3_bind_text(stmt, idx, rec->return_vals.data,
1649 	                      rec->return_vals.offset + 1, NULL);
1650 	if (rc != SQLITE_OK) {
1651 		sqlite3_finalize(stmt);
1652 		goto Out;
1653 	}
1654 
1655 	idx = sqlite3_bind_parameter_index(stmt, ":env");
1656 	rc = sqlite3_bind_text(stmt, idx, rec->env.data,
1657 	    rec->env.offset + 1, NULL);
1658 	if (rc != SQLITE_OK) {
1659 		sqlite3_finalize(stmt);
1660 		goto Out;
1661 	}
1662 
1663 	idx = sqlite3_bind_parameter_index(stmt, ":files");
1664 	rc = sqlite3_bind_text(stmt, idx, rec->files.data,
1665 	                       rec->files.offset + 1, NULL);
1666 	if (rc != SQLITE_OK) {
1667 		sqlite3_finalize(stmt);
1668 		goto Out;
1669 	}
1670 
1671 	idx = sqlite3_bind_parameter_index(stmt, ":exit_status");
1672 	rc = sqlite3_bind_text(stmt, idx, rec->exit_status.data,
1673 	                       rec->exit_status.offset + 1, NULL);
1674 	if (rc != SQLITE_OK) {
1675 		sqlite3_finalize(stmt);
1676 		goto Out;
1677 	}
1678 
1679 	idx = sqlite3_bind_parameter_index(stmt, ":diagnostics");
1680 	rc = sqlite3_bind_text(stmt, idx, rec->diagnostics.data,
1681 	                       rec->diagnostics.offset + 1, NULL);
1682 	if (rc != SQLITE_OK) {
1683 		sqlite3_finalize(stmt);
1684 		goto Out;
1685 	}
1686 
1687 	idx = sqlite3_bind_parameter_index(stmt, ":errors");
1688 	rc = sqlite3_bind_text(stmt, idx, rec->errors.data,
1689 	                       rec->errors.offset + 1, NULL);
1690 	if (rc != SQLITE_OK) {
1691 		sqlite3_finalize(stmt);
1692 		goto Out;
1693 	}
1694 
1695 	idx = sqlite3_bind_parameter_index(stmt, ":md5_hash");
1696 	rc = sqlite3_bind_text(stmt, idx, rec->md5_hash, -1, NULL);
1697 	if (rc != SQLITE_OK) {
1698 		sqlite3_finalize(stmt);
1699 		goto Out;
1700 	}
1701 
1702 	idx = sqlite3_bind_parameter_index(stmt, ":machine");
1703 	if (rec->machine)
1704 		rc = sqlite3_bind_text(stmt, idx, rec->machine, -1, NULL);
1705 	else
1706 		rc = sqlite3_bind_null(stmt, idx);
1707 	if (rc != SQLITE_OK) {
1708 		sqlite3_finalize(stmt);
1709 		goto Out;
1710 	}
1711 
1712 	rc = sqlite3_step(stmt);
1713 	if (rc != SQLITE_DONE) {
1714 		sqlite3_finalize(stmt);
1715 		goto Out;
1716 	}
1717 
1718 	sqlite3_finalize(stmt);
1719 
1720 	/* Get the row id of the last inserted row */
1721 	mandb_rowid = sqlite3_last_insert_rowid(db);
1722 
1723 /*------------------------Populate the mandb_meta table-----------------------*/
1724 	sqlstr = "INSERT INTO mandb_meta VALUES (:device, :inode, :mtime,"
1725 		 " :file, :md5_hash, :id)";
1726 	rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
1727 	if (rc != SQLITE_OK)
1728 		goto Out;
1729 
1730 	idx = sqlite3_bind_parameter_index(stmt, ":device");
1731 	rc = sqlite3_bind_int64(stmt, idx, rec->device);
1732 	if (rc != SQLITE_OK) {
1733 		sqlite3_finalize(stmt);
1734 		goto Out;
1735 	}
1736 
1737 	idx = sqlite3_bind_parameter_index(stmt, ":inode");
1738 	rc = sqlite3_bind_int64(stmt, idx, rec->inode);
1739 	if (rc != SQLITE_OK) {
1740 		sqlite3_finalize(stmt);
1741 		goto Out;
1742 	}
1743 
1744 	idx = sqlite3_bind_parameter_index(stmt, ":mtime");
1745 	rc = sqlite3_bind_int64(stmt, idx, rec->mtime);
1746 	if (rc != SQLITE_OK) {
1747 		sqlite3_finalize(stmt);
1748 		goto Out;
1749 	}
1750 
1751 	idx = sqlite3_bind_parameter_index(stmt, ":file");
1752 	rc = sqlite3_bind_text(stmt, idx, rec->file_path, -1, NULL);
1753 	if (rc != SQLITE_OK) {
1754 		sqlite3_finalize(stmt);
1755 		goto Out;
1756 	}
1757 
1758 	idx = sqlite3_bind_parameter_index(stmt, ":md5_hash");
1759 	rc = sqlite3_bind_text(stmt, idx, rec->md5_hash, -1, NULL);
1760 	if (rc != SQLITE_OK) {
1761 		sqlite3_finalize(stmt);
1762 		goto Out;
1763 	}
1764 
1765 	idx = sqlite3_bind_parameter_index(stmt, ":id");
1766 	rc = sqlite3_bind_int64(stmt, idx, mandb_rowid);
1767 	if (rc != SQLITE_OK) {
1768 		sqlite3_finalize(stmt);
1769 		goto Out;
1770 	}
1771 
1772 	rc = sqlite3_step(stmt);
1773 	sqlite3_finalize(stmt);
1774 	if (rc == SQLITE_CONSTRAINT_UNIQUE) {
1775 		/* The *most* probable reason for reaching here is that
1776 		 * the UNIQUE contraint on the file column of the mandb_meta
1777 		 * table was violated.
1778 		 * This can happen when a file was updated/modified.
1779 		 * To fix this we need to do two things:
1780 		 * 1. Delete the row for the older version of this file
1781 		 *    from mandb table.
1782 		 * 2. Run an UPDATE query to update the row for this file
1783 		 *    in the mandb_meta table.
1784 		 */
1785 		warnx("Trying to update index for %s", rec->file_path);
1786 		char *sql = sqlite3_mprintf("DELETE FROM mandb "
1787 					    "WHERE rowid = (SELECT id"
1788 					    "  FROM mandb_meta"
1789 					    "  WHERE file = %Q)",
1790 					    rec->file_path);
1791 		sqlite3_exec(db, sql, NULL, NULL, &errmsg);
1792 		sqlite3_free(sql);
1793 		if (errmsg != NULL) {
1794 			if (mflags.verbosity)
1795 				warnx("%s", errmsg);
1796 			free(errmsg);
1797 		}
1798 		sqlstr = "UPDATE mandb_meta SET device = :device,"
1799 			 " inode = :inode, mtime = :mtime, id = :id,"
1800 			 " md5_hash = :md5 WHERE file = :file";
1801 		rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
1802 		if (rc != SQLITE_OK) {
1803 			if (mflags.verbosity)
1804 				warnx("Update failed with error: %s",
1805 			    sqlite3_errmsg(db));
1806 			close_db(db);
1807 			cleanup(rec);
1808 			errx(EXIT_FAILURE,
1809 			    "Consider running makemandb with -f option");
1810 		}
1811 
1812 		idx = sqlite3_bind_parameter_index(stmt, ":device");
1813 		sqlite3_bind_int64(stmt, idx, rec->device);
1814 		idx = sqlite3_bind_parameter_index(stmt, ":inode");
1815 		sqlite3_bind_int64(stmt, idx, rec->inode);
1816 		idx = sqlite3_bind_parameter_index(stmt, ":mtime");
1817 		sqlite3_bind_int64(stmt, idx, rec->mtime);
1818 		idx = sqlite3_bind_parameter_index(stmt, ":id");
1819 		sqlite3_bind_int64(stmt, idx, mandb_rowid);
1820 		idx = sqlite3_bind_parameter_index(stmt, ":md5");
1821 		sqlite3_bind_text(stmt, idx, rec->md5_hash, -1, NULL);
1822 		idx = sqlite3_bind_parameter_index(stmt, ":file");
1823 		sqlite3_bind_text(stmt, idx, rec->file_path, -1, NULL);
1824 		rc = sqlite3_step(stmt);
1825 		sqlite3_finalize(stmt);
1826 
1827 		if (rc != SQLITE_DONE) {
1828 			if (mflags.verbosity)
1829 				warnx("%s", sqlite3_errmsg(db));
1830 			close_db(db);
1831 			cleanup(rec);
1832 			errx(EXIT_FAILURE,
1833 			    "Consider running makemandb with -f option");
1834 		}
1835 	} else if (rc != SQLITE_DONE) {
1836 		/* Otherwise make this error fatal */
1837 		warnx("Failed at %s\n%s", rec->file_path, sqlite3_errmsg(db));
1838 		cleanup(rec);
1839 		close_db(db);
1840 		exit(EXIT_FAILURE);
1841 	}
1842 
1843 /*------------------------ Populate the mandb_links table---------------------*/
1844 	char *str = NULL;
1845 	char *links;
1846 	if (rec->links && strlen(rec->links)) {
1847 		links = rec->links;
1848 		for(ln = strtok(links, " "); ln; ln = strtok(NULL, " ")) {
1849 			if (ln[0] == ',')
1850 				ln++;
1851 			if(ln[strlen(ln) - 1] == ',')
1852 				ln[strlen(ln) - 1] = 0;
1853 
1854 			str = sqlite3_mprintf("INSERT INTO mandb_links"
1855 					      " VALUES (%Q, %Q, %Q, %Q, %Q)",
1856 					      ln, rec->name, rec->section,
1857 					      rec->machine, rec->md5_hash);
1858 			sqlite3_exec(db, str, NULL, NULL, &errmsg);
1859 			sqlite3_free(str);
1860 			if (errmsg != NULL) {
1861 				warnx("%s", errmsg);
1862 				cleanup(rec);
1863 				free(errmsg);
1864 				return -1;
1865 			}
1866 		}
1867 	}
1868 
1869 	cleanup(rec);
1870 	return 0;
1871 
1872   Out:
1873 	if (mflags.verbosity)
1874 		warnx("%s", sqlite3_errmsg(db));
1875 	cleanup(rec);
1876 	return -1;
1877 }
1878 
1879 /*
1880  * check_md5--
1881  *  Generates the md5 hash of the file and checks if it already doesn't exist
1882  *  in the table (passed as the 3rd parameter).
1883  *  This function is being used to avoid hardlinks.
1884  *  On successful completion it will also set the value of the fourth parameter
1885  *  to the md5 hash of the file (computed previously). It is the responsibility
1886  *  of the caller to free this buffer.
1887  *  Return values:
1888  *  -1: If an error occurs somewhere and sets the md5 return buffer to NULL.
1889  *  0: If the md5 hash does not exist in the table.
1890  *  1: If the hash exists in the database.
1891  */
1892 static int
1893 check_md5(const char *file, sqlite3 *db, const char *table, char **md5sum,
1894     void *buf, size_t buflen)
1895 {
1896 	int rc = 0;
1897 	int idx = -1;
1898 	char *sqlstr = NULL;
1899 	sqlite3_stmt *stmt = NULL;
1900 
1901 	assert(file != NULL);
1902 	*md5sum = MD5Data(buf, buflen, NULL);
1903 	if (*md5sum == NULL) {
1904 		if (mflags.verbosity)
1905 			warn("md5 failed: %s", file);
1906 		return -1;
1907 	}
1908 
1909 	easprintf(&sqlstr, "SELECT * FROM %s WHERE md5_hash = :md5_hash",
1910 	    table);
1911 	rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
1912 	if (rc != SQLITE_OK) {
1913 		free(sqlstr);
1914 		free(*md5sum);
1915 		*md5sum = NULL;
1916 		return -1;
1917 	}
1918 
1919 	idx = sqlite3_bind_parameter_index(stmt, ":md5_hash");
1920 	rc = sqlite3_bind_text(stmt, idx, *md5sum, -1, NULL);
1921 	if (rc != SQLITE_OK) {
1922 		if (mflags.verbosity)
1923 			warnx("%s", sqlite3_errmsg(db));
1924 		sqlite3_finalize(stmt);
1925 		free(sqlstr);
1926 		free(*md5sum);
1927 		*md5sum = NULL;
1928 		return -1;
1929 	}
1930 
1931 	if (sqlite3_step(stmt) == SQLITE_ROW) {
1932 		sqlite3_finalize(stmt);
1933 		free(sqlstr);
1934 		return 0;
1935 	}
1936 
1937 	sqlite3_finalize(stmt);
1938 	free(sqlstr);
1939 	return 1;
1940 }
1941 
1942 /* Optimize the index for faster search */
1943 static void
1944 optimize(sqlite3 *db)
1945 {
1946 	const char *sqlstr;
1947 	char *errmsg = NULL;
1948 
1949 	if (mflags.verbosity == 2)
1950 		printf("Optimizing the database index\n");
1951 	sqlstr = "INSERT INTO mandb(mandb) VALUES (\'optimize\');"
1952 		 "VACUUM";
1953 	sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
1954 	if (errmsg != NULL) {
1955 		if (mflags.verbosity)
1956 			warnx("%s", errmsg);
1957 		free(errmsg);
1958 		return;
1959 	}
1960 }
1961 
1962 /*
1963  * cleanup --
1964  *  cleans up the global buffers
1965  */
1966 static void
1967 cleanup(mandb_rec *rec)
1968 {
1969 	rec->desc.offset = 0;
1970 	rec->lib.offset = 0;
1971 	rec->return_vals.offset = 0;
1972 	rec->env.offset = 0;
1973 	rec->exit_status.offset = 0;
1974 	rec->diagnostics.offset = 0;
1975 	rec->errors.offset = 0;
1976 	rec->files.offset = 0;
1977 
1978 	free(rec->machine);
1979 	rec->machine = NULL;
1980 
1981 	free(rec->links);
1982 	rec->links = NULL;
1983 
1984 	free(rec->file_path);
1985 	rec->file_path = NULL;
1986 
1987 	free(rec->name);
1988 	rec->name = NULL;
1989 
1990 	free(rec->name_desc);
1991 	rec->name_desc = NULL;
1992 
1993 	free(rec->md5_hash);
1994 	rec->md5_hash = NULL;
1995 
1996 	free(rec->section);
1997 	rec->section = NULL;
1998 }
1999 
2000 /*
2001  * init_secbuffs--
2002  *  Sets the value of buflen for all the sec_buff field of rec. And then
2003  *  allocate memory to each sec_buff member of rec.
2004  */
2005 static void
2006 init_secbuffs(mandb_rec *rec)
2007 {
2008 	/*
2009 	 * Some sec_buff might need more memory, for example desc,
2010 	 * which stores the data of the DESCRIPTION section,
2011 	 * while some might need very small amount of memory.
2012 	 * Therefore explicitly setting the value of buflen field for
2013 	 * each sec_buff.
2014 	 */
2015 	rec->desc.buflen = 10 * BUFLEN;
2016 	rec->desc.data = emalloc(rec->desc.buflen);
2017 	rec->desc.offset = 0;
2018 
2019 	rec->lib.buflen = BUFLEN / 2;
2020 	rec->lib.data = emalloc(rec->lib.buflen);
2021 	rec->lib.offset = 0;
2022 
2023 	rec->return_vals.buflen = BUFLEN;
2024 	rec->return_vals.data = emalloc(rec->return_vals.buflen);
2025 	rec->return_vals.offset = 0;
2026 
2027 	rec->exit_status.buflen = BUFLEN;
2028 	rec->exit_status.data = emalloc(rec->exit_status.buflen);
2029 	rec->exit_status.offset = 0;
2030 
2031 	rec->env.buflen = BUFLEN;
2032 	rec->env.data = emalloc(rec->env.buflen);
2033 	rec->env.offset = 0;
2034 
2035 	rec->files.buflen = BUFLEN;
2036 	rec->files.data = emalloc(rec->files.buflen);
2037 	rec->files.offset = 0;
2038 
2039 	rec->diagnostics.buflen = BUFLEN;
2040 	rec->diagnostics.data = emalloc(rec->diagnostics.buflen);
2041 	rec->diagnostics.offset = 0;
2042 
2043 	rec->errors.buflen = BUFLEN;
2044 	rec->errors.data = emalloc(rec->errors.buflen);
2045 	rec->errors.offset = 0;
2046 }
2047 
2048 /*
2049  * free_secbuffs--
2050  *  This function should be called at the end, when all the pages have been
2051  *  parsed.
2052  *  It frees the memory allocated to sec_buffs by init_secbuffs in the starting.
2053  */
2054 static void
2055 free_secbuffs(mandb_rec *rec)
2056 {
2057 	free(rec->desc.data);
2058 	free(rec->lib.data);
2059 	free(rec->return_vals.data);
2060 	free(rec->exit_status.data);
2061 	free(rec->env.data);
2062 	free(rec->files.data);
2063 	free(rec->diagnostics.data);
2064 	free(rec->errors.data);
2065 }
2066 
2067 static void
2068 replace_hyph(char *str)
2069 {
2070 	char *iter = str;
2071 	while ((iter = strchr(iter, ASCII_HYPH)) != NULL)
2072 		*iter = '-';
2073 
2074 	iter = str;
2075 	while ((iter = strchr(iter, ASCII_NBRSP)) != NULL)
2076 		*iter = '-';
2077 }
2078 
2079 static char *
2080 parse_escape(const char *str)
2081 {
2082 	const char *backslash, *last_backslash;
2083 	char *result, *iter;
2084 	size_t len;
2085 
2086 	assert(str);
2087 
2088 	last_backslash = str;
2089 	backslash = strchr(str, '\\');
2090 	if (backslash == NULL) {
2091 		result = estrdup(str);
2092 		replace_hyph(result);
2093 		return result;
2094 	}
2095 
2096 	result = emalloc(strlen(str) + 1);
2097 	iter = result;
2098 
2099 	do {
2100 		len = backslash - last_backslash;
2101 		memcpy(iter, last_backslash, len);
2102 		iter += len;
2103 		if (backslash[1] == '-' || backslash[1] == ' ') {
2104 			*iter++ = backslash[1];
2105 			last_backslash = backslash + 2;
2106 			backslash = strchr(backslash + 2, '\\');
2107 		} else {
2108 			++backslash;
2109 			mandoc_escape(&backslash, NULL, NULL);
2110 			last_backslash = backslash;
2111 			if (backslash == NULL)
2112 				break;
2113 			backslash = strchr(last_backslash, '\\');
2114 		}
2115 	} while (backslash != NULL);
2116 	if (last_backslash != NULL)
2117 		strcpy(iter, last_backslash);
2118 
2119 	replace_hyph(result);
2120 	return result;
2121 }
2122 
2123 /*
2124  * append--
2125  *  Concatenates a space and src at the end of sbuff->data (much like concat in
2126  *  apropos-utils.c).
2127  *  Rather than reallocating space for writing data, it uses the value of the
2128  *  offset field of sec_buff to write new data at the free space left in the
2129  *  buffer.
2130  *  In case the size of the data to be appended exceeds the number of bytes left
2131  *  in the buffer, it reallocates buflen number of bytes and then continues.
2132  *  Value of offset field should be adjusted as new data is written.
2133  *
2134  *  NOTE: This function does not write the null byte at the end of the buffers,
2135  *  write a null byte at the position pointed to by offset before inserting data
2136  *  in the db.
2137  */
2138 static void
2139 append(secbuff *sbuff, const char *src)
2140 {
2141 	short flag = 0;
2142 	size_t srclen, newlen;
2143 	char *temp;
2144 
2145 	assert(src != NULL);
2146 	temp = parse_escape(src);
2147 	srclen = strlen(temp);
2148 
2149 	if (sbuff->data == NULL) {
2150 		sbuff->data = emalloc(sbuff->buflen);
2151 		sbuff->offset = 0;
2152 	}
2153 
2154 	newlen = sbuff->offset + srclen + 2;
2155 	if (newlen >= sbuff->buflen) {
2156 		while (sbuff->buflen < newlen)
2157 			sbuff->buflen += sbuff->buflen;
2158 		sbuff->data = erealloc(sbuff->data, sbuff->buflen);
2159 		flag = 1;
2160 	}
2161 
2162 	/* Append a space at the end of the buffer. */
2163 	if (sbuff->offset || flag)
2164 		sbuff->data[sbuff->offset++] = ' ';
2165 	/* Now, copy src at the end of the buffer. */
2166 	memcpy(sbuff->data + sbuff->offset, temp, srclen);
2167 	sbuff->offset += srclen;
2168 	free(temp);
2169 }
2170 
2171 static void
2172 usage(void)
2173 {
2174 	fprintf(stderr, "Usage: %s [-floQqv] [-C path]\n", getprogname());
2175 	exit(1);
2176 }
2177