version 1.21, 2011/11/28 09:44:05 |
version 1.31, 2012/06/08 10:44:52 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2012 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 |
|
|
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
#include "config.h" |
#include "config.h" |
#endif |
#endif |
|
#include <sys/param.h> |
|
|
#include <assert.h> |
#include <assert.h> |
#include <getopt.h> |
#include <getopt.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
#include <unistd.h> |
|
|
#include "apropos_db.h" |
|
#include "mandoc.h" |
|
#include "manpath.h" |
#include "manpath.h" |
|
#include "mansearch.h" |
|
|
static int cmp(const void *, const void *); |
|
static void list(struct res *, size_t, void *); |
|
static void usage(void); |
|
|
|
static char *progname; |
|
|
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
int ch, rc, whatis; |
int ch; |
|
size_t i, sz; |
|
struct manpage *res; |
|
char *conf_file, *defpaths, *auxpaths, |
|
*arch, *sec; |
struct manpaths paths; |
struct manpaths paths; |
size_t terms; |
char *progname; |
struct opts opts; |
|
struct expr *e; |
|
char *defpaths, *auxpaths; |
|
extern int optind; |
|
extern char *optarg; |
extern char *optarg; |
|
extern int optind; |
|
|
progname = strrchr(argv[0], '/'); |
progname = strrchr(argv[0], '/'); |
if (progname == NULL) |
if (progname == NULL) |
Line 53 main(int argc, char *argv[]) |
|
Line 48 main(int argc, char *argv[]) |
|
else |
else |
++progname; |
++progname; |
|
|
whatis = 0 == strncmp(progname, "whatis", 6); |
auxpaths = defpaths = conf_file = arch = sec = NULL; |
|
|
memset(&paths, 0, sizeof(struct manpaths)); |
memset(&paths, 0, sizeof(struct manpaths)); |
memset(&opts, 0, sizeof(struct opts)); |
|
|
|
auxpaths = defpaths = NULL; |
while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:"))) |
e = NULL; |
|
|
|
while (-1 != (ch = getopt(argc, argv, "M:m:S:s:"))) |
|
switch (ch) { |
switch (ch) { |
|
case ('C'): |
|
conf_file = optarg; |
|
break; |
case ('M'): |
case ('M'): |
defpaths = optarg; |
defpaths = optarg; |
break; |
break; |
Line 70 main(int argc, char *argv[]) |
|
Line 63 main(int argc, char *argv[]) |
|
auxpaths = optarg; |
auxpaths = optarg; |
break; |
break; |
case ('S'): |
case ('S'): |
opts.arch = optarg; |
arch = optarg; |
break; |
break; |
case ('s'): |
case ('s'): |
opts.cat = optarg; |
sec = optarg; |
break; |
break; |
default: |
default: |
usage(); |
goto usage; |
return(EXIT_FAILURE); |
|
} |
} |
|
|
argc -= optind; |
argc -= optind; |
argv += optind; |
argv += optind; |
|
|
if (0 == argc) |
if (0 == argc) |
return(EXIT_SUCCESS); |
goto usage; |
|
|
rc = 0; |
manpath_parse(&paths, conf_file, defpaths, auxpaths); |
|
ch = mansearch(&paths, arch, sec, argc, argv, &res, &sz); |
|
manpath_free(&paths); |
|
|
manpath_parse(&paths, defpaths, auxpaths); |
if (0 == ch) |
|
goto usage; |
|
|
e = whatis ? termcomp(argc, argv, &terms) : |
for (i = 0; i < sz; i++) { |
exprcomp(argc, argv, &terms); |
printf("%s - %s\n", res[i].file, res[i].desc); |
|
free(res[i].desc); |
if (NULL == e) { |
|
fprintf(stderr, "%s: Bad expression\n", progname); |
|
goto out; |
|
} |
} |
|
|
rc = apropos_search |
free(res); |
(paths.sz, paths.paths, |
return(sz ? EXIT_SUCCESS : EXIT_FAILURE); |
&opts, e, terms, NULL, list); |
usage: |
|
fprintf(stderr, "usage: %s [-C conf] " |
if (0 == rc) |
"[-M paths] " |
fprintf(stderr, "%s: Error reading " |
"[-m paths] " |
"manual database\n", progname); |
"[-S arch] " |
|
"[-s section] " |
out: |
"expr ...\n", |
manpath_free(&paths); |
progname); |
exprfree(e); |
return(EXIT_FAILURE); |
|
|
return(rc ? EXIT_SUCCESS : EXIT_FAILURE); |
|
} |
|
|
|
/* ARGSUSED */ |
|
static void |
|
list(struct res *res, size_t sz, void *arg) |
|
{ |
|
int i; |
|
|
|
qsort(res, sz, sizeof(struct res), cmp); |
|
|
|
for (i = 0; i < (int)sz; i++) |
|
printf("%s(%s%s%s) - %s\n", res[i].title, |
|
res[i].cat, |
|
*res[i].arch ? "/" : "", |
|
*res[i].arch ? res[i].arch : "", |
|
res[i].desc); |
|
} |
|
|
|
static int |
|
cmp(const void *p1, const void *p2) |
|
{ |
|
|
|
return(strcmp(((const struct res *)p1)->title, |
|
((const struct res *)p2)->title)); |
|
} |
|
|
|
static void |
|
usage(void) |
|
{ |
|
|
|
fprintf(stderr, "usage: %s " |
|
"[-M path] " |
|
"[-m path] " |
|
"[-S arch] " |
|
"[-s section] " |
|
"expression...\n", |
|
progname); |
|
} |
} |