1/* $NetBSD: whatis.c,v 1.2 2012/02/07 19:17:16 joerg 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.2 2012/02/07 19:17:16 joerg 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 42usage(void) 43{ 44 fprintf(stderr, "%s ...\n", "whatis"); 45 exit(EXIT_FAILURE); 46} 47 48static int 49whatis(sqlite3 *db, const char *cmd) 50{ 51 static const char sqlstr[] = "SELECT name, section, name_desc" 52 " FROM mandb WHERE name MATCH ? AND name=?" 53 " ORDER BY section, name"; 54 sqlite3_stmt *stmt = NULL; 55 int retval; 56 57 if (sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL) != SQLITE_OK) 58 errx(EXIT_FAILURE, "Unable to query database"); 59 if (sqlite3_bind_text(stmt, 1, cmd, -1, NULL) != SQLITE_OK) 60 errx(EXIT_FAILURE, "Unable to query database"); 61 if (sqlite3_bind_text(stmt, 2, cmd, -1, NULL) != SQLITE_OK) 62 errx(EXIT_FAILURE, "Unable to query database"); 63 retval = 1; 64 while (sqlite3_step(stmt) == SQLITE_ROW) { 65 printf("%s(%s) - %s\n", sqlite3_column_text(stmt, 0), 66 sqlite3_column_text(stmt, 1), 67 sqlite3_column_text(stmt, 2)); 68 retval = 0; 69 } 70 sqlite3_finalize(stmt); 71 if (retval) 72 fprintf(stderr, "%s: not found\n", cmd); 73 return retval; 74} 75 76int 77main(int argc, char *argv[]) 78{ 79 sqlite3 *db; 80 int ch, retval; 81 82 while ((ch = getopt(argc, argv, "")) != -1) { 83 switch (ch) { 84 default: 85 usage(); 86 } 87 } 88 argc -= optind; 89 argv += optind; 90 91 if (argc == 0) 92 usage(); 93 94 if ((db = init_db(MANDB_READONLY)) == NULL) 95 exit(EXIT_FAILURE); 96 97 retval = 0; 98 while (argc--) 99 retval |= whatis(db, *argv++); 100 101 close_db(db); 102 return retval; 103} 104