1*a737f1efSrin /* $NetBSD: whatis.c,v 1.10 2023/08/03 07:49:23 rin Exp $ */
2410d0f43Sjoerg /*-
3410d0f43Sjoerg * Copyright (c) 2012 Joerg Sonnenberger <joerg@NetBSD.org>
4410d0f43Sjoerg * All rights reserved.
5410d0f43Sjoerg *
6410d0f43Sjoerg * Redistribution and use in source and binary forms, with or without
7410d0f43Sjoerg * modification, are permitted provided that the following conditions
8410d0f43Sjoerg * are met:
9410d0f43Sjoerg *
10410d0f43Sjoerg * 1. Redistributions of source code must retain the above copyright
11410d0f43Sjoerg * notice, this list of conditions and the following disclaimer.
12410d0f43Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
13410d0f43Sjoerg * notice, this list of conditions and the following disclaimer in
14410d0f43Sjoerg * the documentation and/or other materials provided with the
15410d0f43Sjoerg * distribution.
16410d0f43Sjoerg *
17410d0f43Sjoerg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18410d0f43Sjoerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19410d0f43Sjoerg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20410d0f43Sjoerg * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21410d0f43Sjoerg * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22410d0f43Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
23410d0f43Sjoerg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24410d0f43Sjoerg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25410d0f43Sjoerg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26410d0f43Sjoerg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
27410d0f43Sjoerg * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28410d0f43Sjoerg * SUCH DAMAGE.
29410d0f43Sjoerg */
30410d0f43Sjoerg
31410d0f43Sjoerg #include <sys/cdefs.h>
32*a737f1efSrin __RCSID("$NetBSD: whatis.c,v 1.10 2023/08/03 07:49:23 rin Exp $");
33410d0f43Sjoerg
34410d0f43Sjoerg #include <err.h>
35410d0f43Sjoerg #include <stdio.h>
36410d0f43Sjoerg #include <stdlib.h>
37410d0f43Sjoerg #include <unistd.h>
38410d0f43Sjoerg
39410d0f43Sjoerg #include "apropos-utils.h"
40410d0f43Sjoerg
41410d0f43Sjoerg __dead static void
usage(void)42410d0f43Sjoerg usage(void)
43410d0f43Sjoerg {
44e9287206Sabhinav fprintf(stderr, "%s [-C path] ...\n", "whatis");
45410d0f43Sjoerg exit(EXIT_FAILURE);
46410d0f43Sjoerg }
47410d0f43Sjoerg
48410d0f43Sjoerg static int
whatis(sqlite3 * db,const char * cmd)49410d0f43Sjoerg whatis(sqlite3 *db, const char *cmd)
50410d0f43Sjoerg {
51933b5da2Sabhinav static const char sqlstr[] = "SELECT link AS name, section, name_desc"
5283d90077Sleot " FROM mandb_links WHERE link=?"
5383d90077Sleot " UNION"
5483d90077Sleot " SELECT name, section, name_desc"
5583d90077Sleot " FROM mandb WHERE name MATCH ? AND name=? COLLATE NOCASE"
5683d90077Sleot " ORDER BY section";
57410d0f43Sjoerg sqlite3_stmt *stmt = NULL;
58410d0f43Sjoerg int retval;
59933b5da2Sabhinav int i;
60410d0f43Sjoerg if (sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL) != SQLITE_OK)
61933b5da2Sabhinav errx(EXIT_FAILURE, "%s", sqlite3_errmsg(db));
62933b5da2Sabhinav
63933b5da2Sabhinav for (i = 0; i < 3; i++) {
64933b5da2Sabhinav if (sqlite3_bind_text(stmt, i + 1, cmd, -1, NULL) != SQLITE_OK)
65933b5da2Sabhinav errx(EXIT_FAILURE, "%s", sqlite3_errmsg(db));
66933b5da2Sabhinav }
67933b5da2Sabhinav
68410d0f43Sjoerg retval = 1;
69410d0f43Sjoerg while (sqlite3_step(stmt) == SQLITE_ROW) {
70410d0f43Sjoerg printf("%s(%s) - %s\n", sqlite3_column_text(stmt, 0),
71410d0f43Sjoerg sqlite3_column_text(stmt, 1),
72410d0f43Sjoerg sqlite3_column_text(stmt, 2));
73410d0f43Sjoerg retval = 0;
74410d0f43Sjoerg }
75410d0f43Sjoerg sqlite3_finalize(stmt);
76410d0f43Sjoerg if (retval)
77410d0f43Sjoerg fprintf(stderr, "%s: not found\n", cmd);
78410d0f43Sjoerg return retval;
79410d0f43Sjoerg }
80410d0f43Sjoerg
81410d0f43Sjoerg int
main(int argc,char * argv[])82410d0f43Sjoerg main(int argc, char *argv[])
83410d0f43Sjoerg {
84410d0f43Sjoerg sqlite3 *db;
85410d0f43Sjoerg int ch, retval;
86e9287206Sabhinav const char *manconf = MANCONF;
87410d0f43Sjoerg
88e9287206Sabhinav while ((ch = getopt(argc, argv, "C:")) != -1) {
89410d0f43Sjoerg switch (ch) {
90e9287206Sabhinav case 'C':
91e9287206Sabhinav manconf = optarg;
92e9287206Sabhinav break;
93410d0f43Sjoerg default:
94410d0f43Sjoerg usage();
95410d0f43Sjoerg }
96410d0f43Sjoerg }
97410d0f43Sjoerg argc -= optind;
98410d0f43Sjoerg argv += optind;
99410d0f43Sjoerg
100410d0f43Sjoerg if (argc == 0)
101410d0f43Sjoerg usage();
102410d0f43Sjoerg
103e9287206Sabhinav if ((db = init_db(MANDB_READONLY, manconf)) == NULL)
104410d0f43Sjoerg exit(EXIT_FAILURE);
105410d0f43Sjoerg
106410d0f43Sjoerg retval = 0;
107410d0f43Sjoerg while (argc--)
108410d0f43Sjoerg retval |= whatis(db, *argv++);
109410d0f43Sjoerg
110410d0f43Sjoerg close_db(db);
111410d0f43Sjoerg return retval;
112410d0f43Sjoerg }
113