]>
git.cameronkatri.com Git - mandoc.git/blob - apropos.c
1 /* $Id: apropos.c,v 1.13 2011/11/14 10:07:06 kristaps Exp $ */
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 #include "apropos_db.h"
31 static int cmp(const void *, const void *);
32 static void list(struct rec
*, size_t, void *);
33 static void usage(void);
35 static char *progname
;
38 main(int argc
, char *argv
[])
48 memset(&opts
, 0, sizeof(struct opts
));
50 progname
= strrchr(argv
[0], '/');
56 while (-1 != (ch
= getopt(argc
, argv
, "S:s:")))
76 * Collapse expressions into a single string.
77 * First count up the contained strings, adding a space at the
78 * end of each (plus nil-terminator). Then merge.
81 for (sz
= 0, ch
= 0; ch
< argc
; ch
++)
82 sz
+= strlen(argv
[ch
]) + 1;
84 buf
= mandoc_malloc(++sz
);
86 for (*buf
= '\0', ch
= 0; ch
< argc
; ch
++) {
87 strlcat(buf
, argv
[ch
], sz
);
88 strlcat(buf
, " ", sz
);
93 if (NULL
== (e
= exprcomp(buf
))) {
94 fprintf(stderr
, "Bad expression\n");
102 * Configure databases.
103 * The keyword database is a btree that allows for duplicate
105 * The index database is a recno.
108 apropos_search(&opts
, e
, NULL
, list
);
110 return(EXIT_SUCCESS
);
115 list(struct rec
*res
, size_t sz
, void *arg
)
119 qsort(res
, sz
, sizeof(struct rec
), cmp
);
121 for (i
= 0; i
< (int)sz
; i
++)
122 printf("%s(%s%s%s) - %s\n", res
[i
].title
,
124 *res
[i
].arch
? "/" : "",
125 *res
[i
].arch
? res
[i
].arch
: "",
130 cmp(const void *p1
, const void *p2
)
133 return(strcmp(((const struct rec
*)p1
)->title
,
134 ((const struct rec
*)p2
)->title
));
141 fprintf(stderr
, "usage: %s "