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