1 /* $NetBSD: whatis.c,v 1.10 2023/08/03 07:49:23 rin Exp $ */ 2 /*- 3 * Copyright (c) 2012 Joerg Sonnenberger <joerg@NetBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __RCSID("$NetBSD: whatis.c,v 1.10 2023/08/03 07:49:23 rin Exp $"); 33 34 #include <err.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <unistd.h> 38 39 #include "apropos-utils.h" 40 41 __dead static void 42 usage(void) 43 { 44 fprintf(stderr, "%s [-C path] ...\n", "whatis"); 45 exit(EXIT_FAILURE); 46 } 47 48 static int 49 whatis(sqlite3 *db, const char *cmd) 50 { 51 static const char sqlstr[] = "SELECT link AS name, section, name_desc" 52 " FROM mandb_links WHERE link=?" 53 " UNION" 54 " SELECT name, section, name_desc" 55 " FROM mandb WHERE name MATCH ? AND name=? COLLATE NOCASE" 56 " ORDER BY section"; 57 sqlite3_stmt *stmt = NULL; 58 int retval; 59 int i; 60 if (sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL) != SQLITE_OK) 61 errx(EXIT_FAILURE, "%s", sqlite3_errmsg(db)); 62 63 for (i = 0; i < 3; i++) { 64 if (sqlite3_bind_text(stmt, i + 1, cmd, -1, NULL) != SQLITE_OK) 65 errx(EXIT_FAILURE, "%s", sqlite3_errmsg(db)); 66 } 67 68 retval = 1; 69 while (sqlite3_step(stmt) == SQLITE_ROW) { 70 printf("%s(%s) - %s\n", sqlite3_column_text(stmt, 0), 71 sqlite3_column_text(stmt, 1), 72 sqlite3_column_text(stmt, 2)); 73 retval = 0; 74 } 75 sqlite3_finalize(stmt); 76 if (retval) 77 fprintf(stderr, "%s: not found\n", cmd); 78 return retval; 79 } 80 81 int 82 main(int argc, char *argv[]) 83 { 84 sqlite3 *db; 85 int ch, retval; 86 const char *manconf = MANCONF; 87 88 while ((ch = getopt(argc, argv, "C:")) != -1) { 89 switch (ch) { 90 case 'C': 91 manconf = optarg; 92 break; 93 default: 94 usage(); 95 } 96 } 97 argc -= optind; 98 argv += optind; 99 100 if (argc == 0) 101 usage(); 102 103 if ((db = init_db(MANDB_READONLY, manconf)) == NULL) 104 exit(EXIT_FAILURE); 105 106 retval = 0; 107 while (argc--) 108 retval |= whatis(db, *argv++); 109 110 close_db(db); 111 return retval; 112 } 113