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