11.10Srin/*	$NetBSD: whatis.c,v 1.10 2023/08/03 07:49:23 rin Exp $	*/
21.1Sjoerg/*-
31.1Sjoerg * Copyright (c) 2012 Joerg Sonnenberger <joerg@NetBSD.org>
41.1Sjoerg * All rights reserved.
51.1Sjoerg *
61.1Sjoerg * Redistribution and use in source and binary forms, with or without
71.1Sjoerg * modification, are permitted provided that the following conditions
81.1Sjoerg * are met:
91.1Sjoerg *
101.1Sjoerg * 1. Redistributions of source code must retain the above copyright
111.1Sjoerg *    notice, this list of conditions and the following disclaimer.
121.1Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjoerg *    notice, this list of conditions and the following disclaimer in
141.1Sjoerg *    the documentation and/or other materials provided with the
151.1Sjoerg *    distribution.
161.1Sjoerg *
171.1Sjoerg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181.1Sjoerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191.1Sjoerg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
201.1Sjoerg * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
211.1Sjoerg * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
221.1Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
231.1Sjoerg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
241.1Sjoerg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
251.1Sjoerg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
261.1Sjoerg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
271.1Sjoerg * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281.1Sjoerg * SUCH DAMAGE.
291.1Sjoerg */
301.1Sjoerg
311.1Sjoerg#include <sys/cdefs.h>
321.10Srin__RCSID("$NetBSD: whatis.c,v 1.10 2023/08/03 07:49:23 rin Exp $");
331.1Sjoerg
341.1Sjoerg#include <err.h>
351.1Sjoerg#include <stdio.h>
361.1Sjoerg#include <stdlib.h>
371.1Sjoerg#include <unistd.h>
381.1Sjoerg
391.1Sjoerg#include "apropos-utils.h"
401.1Sjoerg
411.1Sjoerg__dead static void
421.1Sjoergusage(void)
431.1Sjoerg{
441.5Sabhinav	fprintf(stderr, "%s [-C path] ...\n", "whatis");
451.1Sjoerg	exit(EXIT_FAILURE);
461.1Sjoerg}
471.1Sjoerg
481.1Sjoergstatic int
491.1Sjoergwhatis(sqlite3 *db, const char *cmd)
501.1Sjoerg{
511.8Sabhinav	static const char sqlstr[] = "SELECT link AS name, section, name_desc"
521.10Srin		" FROM mandb_links WHERE link=?"
531.9Sleot		" UNION"
541.9Sleot		" SELECT name, section, name_desc"
551.9Sleot		" FROM mandb WHERE name MATCH ? AND name=? COLLATE NOCASE"
561.9Sleot		" ORDER BY section";
571.1Sjoerg	sqlite3_stmt *stmt = NULL;
581.1Sjoerg	int retval;
591.8Sabhinav	int i;
601.8Sabhinav	if (sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL) != SQLITE_OK)
611.8Sabhinav		errx(EXIT_FAILURE, "%s", sqlite3_errmsg(db));
621.8Sabhinav
631.8Sabhinav	for (i = 0; i < 3; i++) {
641.8Sabhinav		if (sqlite3_bind_text(stmt, i + 1, cmd, -1, NULL) != SQLITE_OK)
651.8Sabhinav			errx(EXIT_FAILURE, "%s", sqlite3_errmsg(db));
661.8Sabhinav	}
671.1Sjoerg
681.1Sjoerg	retval = 1;
691.1Sjoerg	while (sqlite3_step(stmt) == SQLITE_ROW) {
701.1Sjoerg		printf("%s(%s) - %s\n", sqlite3_column_text(stmt, 0),
711.1Sjoerg		    sqlite3_column_text(stmt, 1),
721.1Sjoerg		    sqlite3_column_text(stmt, 2));
731.1Sjoerg		retval = 0;
741.1Sjoerg	}
751.1Sjoerg	sqlite3_finalize(stmt);
761.1Sjoerg	if (retval)
771.1Sjoerg		fprintf(stderr, "%s: not found\n", cmd);
781.1Sjoerg	return retval;
791.1Sjoerg}
801.1Sjoerg
811.1Sjoergint
821.1Sjoergmain(int argc, char *argv[])
831.1Sjoerg{
841.1Sjoerg	sqlite3 *db;
851.1Sjoerg	int ch, retval;
861.5Sabhinav	const char *manconf = MANCONF;
871.1Sjoerg
881.5Sabhinav	while ((ch = getopt(argc, argv, "C:")) != -1) {
891.1Sjoerg		switch (ch) {
901.5Sabhinav		case 'C':
911.5Sabhinav			manconf = optarg;
921.5Sabhinav			break;
931.1Sjoerg		default:
941.1Sjoerg			usage();
951.1Sjoerg		}
961.1Sjoerg	}
971.1Sjoerg	argc -= optind;
981.1Sjoerg	argv += optind;
991.1Sjoerg
1001.1Sjoerg	if (argc == 0)
1011.1Sjoerg		usage();
1021.1Sjoerg
1031.5Sabhinav	if ((db = init_db(MANDB_READONLY, manconf)) == NULL)
1041.1Sjoerg		exit(EXIT_FAILURE);
1051.1Sjoerg
1061.1Sjoerg	retval = 0;
1071.1Sjoerg	while (argc--)
1081.1Sjoerg		retval |= whatis(db, *argv++);
1091.1Sjoerg
1101.1Sjoerg	close_db(db);
1111.1Sjoerg	return retval;
1121.1Sjoerg}
113