1 /* $NetBSD: whatis.c,v 1.7 2017/05/23 15:27:54 abhinav 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.7 2017/05/23 15:27:54 abhinav 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 name, section, name_desc" 52 " FROM mandb WHERE name MATCH ? AND name=? COLLATE NOCASE" 53 " UNION" 54 " SELECT b.link AS name, b.section, a.name_desc FROM mandb a" 55 " JOIN" 56 " mandb_links b ON a.name=b.target AND a.section=b.section WHERE b.link=?" 57 " GROUP BY a.name, a.section, a.name_desc ORDER BY section, name"; 58 sqlite3_stmt *stmt = NULL; 59 int retval; 60 61 if (sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL) != SQLITE_OK) 62 errx(EXIT_FAILURE, "Unable to query database"); 63 if (sqlite3_bind_text(stmt, 1, cmd, -1, NULL) != SQLITE_OK) 64 errx(EXIT_FAILURE, "Unable to query database"); 65 if (sqlite3_bind_text(stmt, 2, cmd, -1, NULL) != SQLITE_OK) 66 errx(EXIT_FAILURE, "Unable to query database"); 67 if (sqlite3_bind_text(stmt, 3, cmd, -1, NULL) != SQLITE_OK) 68 errx(EXIT_FAILURE, "Unable to query database"); 69 retval = 1; 70 while (sqlite3_step(stmt) == SQLITE_ROW) { 71 printf("%s(%s) - %s\n", sqlite3_column_text(stmt, 0), 72 sqlite3_column_text(stmt, 1), 73 sqlite3_column_text(stmt, 2)); 74 retval = 0; 75 } 76 sqlite3_finalize(stmt); 77 if (retval) 78 fprintf(stderr, "%s: not found\n", cmd); 79 return retval; 80 } 81 82 int 83 main(int argc, char *argv[]) 84 { 85 sqlite3 *db; 86 int ch, retval; 87 const char *manconf = MANCONF; 88 89 while ((ch = getopt(argc, argv, "C:")) != -1) { 90 switch (ch) { 91 case 'C': 92 manconf = optarg; 93 break; 94 default: 95 usage(); 96 } 97 } 98 argc -= optind; 99 argv += optind; 100 101 if (argc == 0) 102 usage(); 103 104 if ((db = init_db(MANDB_READONLY, manconf)) == NULL) 105 exit(EXIT_FAILURE); 106 107 retval = 0; 108 while (argc--) 109 retval |= whatis(db, *argv++); 110 111 close_db(db); 112 return retval; 113 } 114