version 1.7, 2011/11/18 01:10:03 |
version 1.8, 2011/11/26 16:41:35 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
|
* Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
*/ |
*/ |
#include <assert.h> |
#include <assert.h> |
#include <getopt.h> |
#include <getopt.h> |
#include <limits.h> |
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
#include "apropos_db.h" |
#include "apropos_db.h" |
#include "man_conf.h" |
|
#include "mandoc.h" |
#include "mandoc.h" |
|
#include "manpath.h" |
|
|
static int cmp(const void *, const void *); |
static int cmp(const void *, const void *); |
static void list(struct res *, size_t, void *); |
static void list(struct res *, size_t, void *); |
|
|
int |
int |
apropos(int argc, char *argv[]) |
apropos(int argc, char *argv[]) |
{ |
{ |
struct man_conf dirs; |
int ch, rc; |
int ch, use_man_conf; |
struct manpaths paths; |
size_t terms; |
size_t terms; |
struct opts opts; |
struct opts opts; |
struct expr *e; |
struct expr *e; |
|
char *defpaths, *auxpaths; |
extern int optind; |
extern int optind; |
extern char *optarg; |
extern char *optarg; |
|
|
memset(&dirs, 0, sizeof(struct man_conf)); |
|
memset(&opts, 0, sizeof(struct opts)); |
|
use_man_conf = 1; |
|
|
|
progname = strrchr(argv[0], '/'); |
progname = strrchr(argv[0], '/'); |
if (progname == NULL) |
if (progname == NULL) |
progname = argv[0]; |
progname = argv[0]; |
else |
else |
++progname; |
++progname; |
|
|
while (-1 != (ch = getopt(argc, argv, "M:m:S:s:"))) |
memset(&paths, 0, sizeof(struct manpaths)); |
|
memset(&opts, 0, sizeof(struct opts)); |
|
|
|
auxpaths = defpaths = NULL; |
|
e = NULL; |
|
rc = 0; |
|
|
|
while (-1 != (ch = getopt(argc, argv, "M:m:S:s:"))) |
switch (ch) { |
switch (ch) { |
case ('M'): |
case ('M'): |
use_man_conf = 0; |
defpaths = optarg; |
/* FALLTHROUGH */ |
break; |
case ('m'): |
case ('m'): |
manpath_parse(&dirs, optarg); |
auxpaths = optarg; |
break; |
break; |
case ('S'): |
case ('S'): |
opts.arch = optarg; |
opts.arch = optarg; |
|
|
break; |
break; |
default: |
default: |
usage(); |
usage(); |
return(EXIT_FAILURE); |
goto out; |
} |
} |
|
|
argc -= optind; |
argc -= optind; |
argv += optind; |
argv += optind; |
|
|
if (0 == argc) |
if (0 == argc) { |
return(EXIT_SUCCESS); |
rc = 1; |
|
goto out; |
|
} |
|
|
|
manpath_parse(&paths, defpaths, auxpaths); |
|
|
if (NULL == (e = exprcomp(argc, argv, &terms))) { |
if (NULL == (e = exprcomp(argc, argv, &terms))) { |
fprintf(stderr, "Bad expression\n"); |
fprintf(stderr, "%s: Bad expression\n", progname); |
return(EXIT_FAILURE); |
goto out; |
} |
} |
|
|
/* |
rc = apropos_search |
* Configure databases. |
(paths.sz, paths.paths, |
* The keyword database is a btree that allows for duplicate |
&opts, e, terms, NULL, list); |
* entries. |
|
* The index database is a recno. |
|
*/ |
|
|
|
if (use_man_conf) |
if (0 == rc) |
man_conf_parse(&dirs); |
fprintf(stderr, "%s: Error reading " |
ch = apropos_search(dirs.argc, dirs.argv, &opts, |
"manual database\n", progname); |
e, terms, NULL, list); |
|
|
|
man_conf_free(&dirs); |
out: |
|
manpath_free(&paths); |
exprfree(e); |
exprfree(e); |
if (0 == ch) |
|
fprintf(stderr, "%s: Database error\n", progname); |
return(rc ? EXIT_SUCCESS : EXIT_FAILURE); |
return(ch ? EXIT_SUCCESS : EXIT_FAILURE); |
|
} |
} |
|
|
/* ARGSUSED */ |
/* ARGSUSED */ |
|
|
qsort(res, sz, sizeof(struct res), cmp); |
qsort(res, sz, sizeof(struct res), cmp); |
|
|
for (i = 0; i < (int)sz; i++) |
for (i = 0; i < (int)sz; i++) |
printf("%s(%s%s%s) - %s\n", res[i].title, |
printf("%s(%s%s%s) - %s\n", res[i].title, |
res[i].cat, |
res[i].cat, |
*res[i].arch ? "/" : "", |
*res[i].arch ? "/" : "", |
*res[i].arch ? res[i].arch : "", |
*res[i].arch ? res[i].arch : "", |
res[i].desc); |
res[i].desc); |
|
|
"[-m path] " |
"[-m path] " |
"[-S arch] " |
"[-S arch] " |
"[-s section] " |
"[-s section] " |
"expression...\n", |
"expression...\n", |
progname); |
progname); |
} |
} |