]>
git.cameronkatri.com Git - mandoc.git/blob - apropos.c
17435768b4bf3635a6581b9bc9635d2e5d6a3e0b
1 /* $Id: apropos.c,v 1.16 2011/11/20 16:29:50 kristaps Exp $ */
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include "apropos_db.h"
33 * List of paths to be searched for manual databases.
40 static int cmp(const void *, const void *);
41 static void list(struct res
*, size_t, void *);
42 static void manpath_add(struct manpaths
*, const char *);
43 static void manpath_parse(struct manpaths
*, char *);
44 static void usage(void);
46 static char *progname
;
49 main(int argc
, char *argv
[])
52 struct manpaths paths
;
59 progname
= strrchr(argv
[0], '/');
65 memset(&paths
, 0, sizeof(struct manpaths
));
66 memset(&opts
, 0, sizeof(struct opts
));
71 while (-1 != (ch
= getopt(argc
, argv
, "m:S:s:")))
74 manpath_parse(&paths
, optarg
);
96 * Let MANPATH override our default paths.
99 if (NULL
!= getenv("MANPATH"))
100 manpath_add(&paths
, getenv("MANPATH"));
102 manpath_add(&paths
, ".");
104 if (NULL
== (e
= exprcomp(argc
, argv
, &terms
))) {
105 fprintf(stderr
, "%s: Bad expression\n", progname
);
110 (paths
.sz
, paths
.paths
,
111 &opts
, e
, terms
, NULL
, list
);
114 fprintf(stderr
, "%s: Error reading "
115 "manual database\n", progname
);
118 for (i
= 0; i
< paths
.sz
; i
++)
119 free(paths
.paths
[i
]);
124 return(rc
? EXIT_SUCCESS
: EXIT_FAILURE
);
129 list(struct res
*res
, size_t sz
, void *arg
)
133 qsort(res
, sz
, sizeof(struct res
), cmp
);
135 for (i
= 0; i
< (int)sz
; i
++)
136 printf("%s(%s%s%s) - %s\n", res
[i
].title
,
138 *res
[i
].arch
? "/" : "",
139 *res
[i
].arch
? res
[i
].arch
: "",
144 cmp(const void *p1
, const void *p2
)
147 return(strcmp(((const struct res
*)p1
)->title
,
148 ((const struct res
*)p2
)->title
));
155 fprintf(stderr
, "usage: %s "
159 "expression...\n", progname
);
163 * Parse a FULL pathname from a colon-separated list of arrays.
166 manpath_parse(struct manpaths
*dirs
, char *path
)
170 for (dir
= strtok(path
, ":"); dir
; dir
= strtok(NULL
, ":"))
171 manpath_add(dirs
, dir
);
175 * Add a directory to the array, ignoring bad directories.
176 * Grow the array one-by-one for simplicity's sake.
179 manpath_add(struct manpaths
*dirs
, const char *dir
)
184 if (NULL
== (cp
= realpath(dir
, buf
)))
187 dirs
->paths
= mandoc_realloc
189 ((size_t)dirs
->sz
+ 1) * sizeof(char *));
191 dirs
->paths
[dirs
->sz
++] = mandoc_strdup(cp
);