]>
git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
1 /* $OpenBSD: mansearch.c,v 1.50 2016/07/09 15:23:36 schwarze Exp $ */
3 * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013-2017 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 AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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.
21 #include <sys/types.h>
40 #include "mandoc_aux.h"
41 #include "mandoc_ohash.h"
43 #include "mansearch.h"
48 struct dbm_match match
; /* Match type and expression. */
49 uint64_t bits
; /* Type mask. */
50 /* Used for OR and AND groups: */
51 struct expr
*next
; /* Next child in the parent group. */
52 struct expr
*child
; /* First child in this group. */
53 enum { EXPR_TERM
, EXPR_OR
, EXPR_AND
} type
;
56 const char *const mansearch_keynames
[KEY_MAX
] = {
57 "arch", "sec", "Xr", "Ar", "Fa", "Fl", "Dv", "Fn",
58 "Ic", "Pa", "Cm", "Li", "Em", "Cd", "Va", "Ft",
59 "Tn", "Er", "Ev", "Sy", "Sh", "In", "Ss", "Ox",
60 "An", "Mt", "St", "Bx", "At", "Nx", "Fx", "Lk",
61 "Ms", "Bsx", "Dx", "Rs", "Vt", "Lb", "Nm", "Nd"
65 static struct ohash
*manmerge(struct expr
*, struct ohash
*);
66 static struct ohash
*manmerge_term(struct expr
*, struct ohash
*);
67 static struct ohash
*manmerge_or(struct expr
*, struct ohash
*);
68 static struct ohash
*manmerge_and(struct expr
*, struct ohash
*);
69 static char *buildnames(const struct dbm_page
*);
70 static char *buildoutput(size_t, struct dbm_page
*);
71 static size_t lstlen(const char *, size_t);
72 static void lstcat(char *, size_t *, const char *, const char *);
73 static int lstmatch(const char *, const char *);
74 static struct expr
*exprcomp(const struct mansearch
*,
75 int, char *[], int *);
76 static struct expr
*expr_and(const struct mansearch
*,
77 int, char *[], int *);
78 static struct expr
*exprterm(const struct mansearch
*,
79 int, char *[], int *);
80 static void exprfree(struct expr
*);
81 static int manpage_compare(const void *, const void *);
85 mansearch(const struct mansearch
*search
,
86 const struct manpaths
*paths
,
87 int argc
, char *argv
[],
88 struct manpage
**res
, size_t *sz
)
93 struct dbm_page
*page
;
94 struct manpage
*mpage
;
96 size_t cur
, i
, maxres
, outkey
;
98 int argi
, chdir_status
, getcwd_status
, im
;
101 if ((e
= exprcomp(search
, argc
, argv
, &argi
)) == NULL
) {
110 if (search
->outkey
!= NULL
)
111 for (im
= 0; im
< KEY_MAX
; im
++)
112 if (0 == strcasecmp(search
->outkey
,
113 mansearch_keynames
[im
])) {
119 * Remember the original working directory, if possible.
120 * This will be needed if the second or a later directory
121 * is given as a relative path.
122 * Do not error out if the current directory is not
123 * searchable: Maybe it won't be needed after all.
126 if (getcwd(buf
, PATH_MAX
) == NULL
) {
128 (void)strlcpy(buf
, strerror(errno
), sizeof(buf
));
133 * Loop over the directories (containing databases) for us to
135 * Don't let missing/bad databases/directories phase us.
136 * In each, try to open the resident database and, if it opens,
137 * scan it for our match expression.
141 for (i
= 0; i
< paths
->sz
; i
++) {
142 if (chdir_status
&& paths
->paths
[i
][0] != '/') {
143 if ( ! getcwd_status
) {
144 warnx("%s: getcwd: %s", paths
->paths
[i
], buf
);
146 } else if (chdir(buf
) == -1) {
151 if (chdir(paths
->paths
[i
]) == -1) {
152 warn("%s", paths
->paths
[i
]);
157 if (dbm_open(MANDOC_DB
) == -1) {
159 warn("%s/%s", paths
->paths
[i
], MANDOC_DB
);
163 if ((htab
= manmerge(e
, NULL
)) == NULL
) {
168 for (rp
= ohash_first(htab
, &slot
); rp
!= NULL
;
169 rp
= ohash_next(htab
, &slot
)) {
170 page
= dbm_page_get(rp
->page
);
172 if (lstmatch(search
->sec
, page
->sect
) == 0 ||
173 lstmatch(search
->arch
, page
->arch
) == 0)
176 if (cur
+ 1 > maxres
) {
178 *res
= mandoc_reallocarray(*res
,
179 maxres
, sizeof(**res
));
182 mandoc_asprintf(&mpage
->file
, "%s/%s",
183 paths
->paths
[i
], page
->file
+ 1);
184 mpage
->names
= buildnames(page
);
185 mpage
->output
= buildoutput(outkey
, page
);
187 mpage
->bits
= rp
->bits
;
188 mpage
->sec
= *page
->sect
- '0';
189 if (mpage
->sec
< 0 || mpage
->sec
> 9)
191 mpage
->form
= *page
->file
;
200 * In man(1) mode, prefer matches in earlier trees
201 * over matches in later trees.
204 if (cur
&& search
->firstmatch
)
207 qsort(*res
, cur
, sizeof(struct manpage
), manpage_compare
);
208 if (chdir_status
&& getcwd_status
&& chdir(buf
) == -1)
216 * Merge the results for the expression tree rooted at e
217 * into the the result list htab.
219 static struct ohash
*
220 manmerge(struct expr
*e
, struct ohash
*htab
)
224 return manmerge_term(e
, htab
);
226 return manmerge_or(e
->child
, htab
);
228 return manmerge_and(e
->child
, htab
);
234 static struct ohash
*
235 manmerge_term(struct expr
*e
, struct ohash
*htab
)
237 struct dbm_res res
, *rp
;
243 htab
= mandoc_malloc(sizeof(*htab
));
244 mandoc_ohash_init(htab
, 4, offsetof(struct dbm_res
, page
));
247 for (im
= 0, ib
= 1; im
< KEY_MAX
; im
++, ib
<<= 1) {
248 if ((e
->bits
& ib
) == 0)
253 dbm_page_byarch(&e
->match
);
256 dbm_page_bysect(&e
->match
);
259 dbm_page_byname(&e
->match
);
262 dbm_page_bydesc(&e
->match
);
265 dbm_page_bymacro(im
- 2, &e
->match
);
270 * When hashing for deduplication, use the unique
271 * page ID itself instead of a hash function;
272 * that is quite efficient.
276 res
= dbm_page_next();
279 slot
= ohash_lookup_memory(htab
,
280 (char *)&res
, sizeof(res
.page
), res
.page
);
281 if ((rp
= ohash_find(htab
, slot
)) != NULL
) {
282 rp
->bits
|= res
.bits
;
285 rp
= mandoc_malloc(sizeof(*rp
));
287 ohash_insert(htab
, slot
, rp
);
293 static struct ohash
*
294 manmerge_or(struct expr
*e
, struct ohash
*htab
)
297 htab
= manmerge(e
, htab
);
303 static struct ohash
*
304 manmerge_and(struct expr
*e
, struct ohash
*htab
)
306 struct ohash
*hand
, *h1
, *h2
;
308 unsigned int slot1
, slot2
;
310 /* Evaluate the first term of the AND clause. */
312 hand
= manmerge(e
, NULL
);
314 while ((e
= e
->next
) != NULL
) {
316 /* Evaluate the next term and prepare for ANDing. */
318 h2
= manmerge(e
, NULL
);
319 if (ohash_entries(h2
) < ohash_entries(hand
)) {
324 hand
= mandoc_malloc(sizeof(*hand
));
325 mandoc_ohash_init(hand
, 4, offsetof(struct dbm_res
, page
));
327 /* Keep all pages that are in both result sets. */
329 for (res
= ohash_first(h1
, &slot1
); res
!= NULL
;
330 res
= ohash_next(h1
, &slot1
)) {
331 if (ohash_find(h2
, ohash_lookup_memory(h2
,
332 (char *)res
, sizeof(res
->page
),
336 ohash_insert(hand
, ohash_lookup_memory(hand
,
337 (char *)res
, sizeof(res
->page
),
341 /* Discard the merged results. */
343 for (res
= ohash_first(h2
, &slot2
); res
!= NULL
;
344 res
= ohash_next(h2
, &slot2
))
352 /* Merge the result of the AND into htab. */
357 for (res
= ohash_first(hand
, &slot1
); res
!= NULL
;
358 res
= ohash_next(hand
, &slot1
)) {
359 slot2
= ohash_lookup_memory(htab
,
360 (char *)res
, sizeof(res
->page
), res
->page
);
361 if (ohash_find(htab
, slot2
) == NULL
)
362 ohash_insert(htab
, slot2
, res
);
367 /* Discard the merged result. */
375 mansearch_free(struct manpage
*res
, size_t sz
)
379 for (i
= 0; i
< sz
; i
++) {
388 manpage_compare(const void *vp1
, const void *vp2
)
390 const struct manpage
*mp1
, *mp2
;
395 return (diff
= mp2
->bits
- mp1
->bits
) ? diff
:
396 (diff
= mp1
->sec
- mp2
->sec
) ? diff
:
397 strcasecmp(mp1
->names
, mp2
->names
);
401 buildnames(const struct dbm_page
*page
)
406 sz
= lstlen(page
->name
, 2) + 1 + lstlen(page
->sect
, 2) +
407 (page
->arch
== NULL
? 0 : 1 + lstlen(page
->arch
, 2)) + 2;
408 buf
= mandoc_malloc(sz
);
410 lstcat(buf
, &i
, page
->name
, ", ");
412 lstcat(buf
, &i
, page
->sect
, ", ");
413 if (page
->arch
!= NULL
) {
415 lstcat(buf
, &i
, page
->arch
, ", ");
424 * Count the buffer space needed to print the NUL-terminated
425 * list of NUL-terminated strings, when printing sep separator
426 * characters between strings.
429 lstlen(const char *cp
, size_t sep
)
433 for (sz
= 0;; sz
++) {
438 } else if (cp
[0] < ' ')
446 * Print the NUL-terminated list of NUL-terminated strings
447 * into the buffer, seperating strings with sep.
450 lstcat(char *buf
, size_t *i
, const char *cp
, const char *sep
)
461 } else if (cp
[0] >= ' ')
468 * Return 1 if the string *want occurs in any of the strings
469 * in the NUL-terminated string list *have, or 0 otherwise.
470 * If either argument is NULL or empty, assume no filtering
471 * is desired and return 1.
474 lstmatch(const char *want
, const char *have
)
476 if (want
== NULL
|| have
== NULL
|| *have
== '\0')
478 while (*have
!= '\0') {
479 if (strcasestr(have
, want
) != NULL
)
481 have
= strchr(have
, '\0') + 1;
487 * Build a list of values taken by the macro im in the manual page.
490 buildoutput(size_t im
, struct dbm_page
*page
)
492 const char *oldoutput
, *sep
, *input
;
493 char *output
, *newoutput
, *value
;
498 return mandoc_strdup(page
->desc
);
516 sz
= lstlen(input
, 3) + 1;
517 output
= mandoc_malloc(sz
);
519 lstcat(output
, &i
, input
, " # ");
526 dbm_macro_bypage(im
- 2, page
->addr
);
527 while ((value
= dbm_macro_next()) != NULL
) {
528 if (output
== NULL
) {
535 mandoc_asprintf(&newoutput
, "%s%s%s", oldoutput
, sep
, value
);
543 * Compile a set of string tokens into an expression.
544 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
545 * "(", "foo=bar", etc.).
548 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
550 struct expr
*parent
, *child
;
551 int needterm
, nested
;
553 if ((nested
= *argi
) == argc
)
556 parent
= child
= NULL
;
557 while (*argi
< argc
) {
558 if (strcmp(")", argv
[*argi
]) == 0) {
560 warnx("missing term "
561 "before closing parenthesis");
565 warnx("ignoring unmatched right parenthesis");
569 if (strcmp("-o", argv
[*argi
]) == 0) {
572 warnx("ignoring -o after %s",
575 warnx("ignoring initial -o");
583 child
= expr_and(search
, argc
, argv
, argi
);
586 if (parent
== NULL
) {
587 parent
= mandoc_calloc(1, sizeof(*parent
));
588 parent
->type
= EXPR_OR
;
590 parent
->child
= child
;
592 child
->next
= expr_and(search
, argc
, argv
, argi
);
595 if (needterm
&& *argi
)
596 warnx("ignoring trailing %s", argv
[*argi
- 1]);
597 return parent
== NULL
? child
: parent
;
601 expr_and(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
603 struct expr
*parent
, *child
;
607 parent
= child
= NULL
;
608 while (*argi
< argc
) {
609 if (strcmp(")", argv
[*argi
]) == 0) {
611 warnx("missing term "
612 "before closing parenthesis");
616 if (strcmp("-o", argv
[*argi
]) == 0)
618 if (strcmp("-a", argv
[*argi
]) == 0) {
621 warnx("ignoring -a after %s",
624 warnx("ignoring initial -a");
633 child
= exprterm(search
, argc
, argv
, argi
);
639 if (parent
== NULL
) {
640 parent
= mandoc_calloc(1, sizeof(*parent
));
641 parent
->type
= EXPR_AND
;
643 parent
->child
= child
;
645 child
->next
= exprterm(search
, argc
, argv
, argi
);
646 if (child
->next
!= NULL
) {
651 if (needterm
&& *argi
)
652 warnx("ignoring trailing %s", argv
[*argi
- 1]);
653 return parent
== NULL
? child
: parent
;
657 exprterm(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
665 if (strcmp("(", argv
[*argi
]) == 0) {
667 e
= exprcomp(search
, argc
, argv
, argi
);
669 assert(strcmp(")", argv
[*argi
]) == 0);
672 warnx("unclosed parenthesis");
676 if (strcmp("-i", argv
[*argi
]) == 0 && *argi
+ 1 < argc
) {
682 e
= mandoc_calloc(1, sizeof(*e
));
688 if (search
->argmode
== ARG_NAME
) {
690 e
->match
.type
= DBM_EXACT
;
691 e
->match
.str
= argv
[(*argi
)++];
696 * Separate macro keys from search string.
697 * If needed, request regular expression handling.
700 if (search
->argmode
== ARG_WORD
) {
702 e
->match
.type
= DBM_REGEX
;
704 mandoc_asprintf(&val
, "[[:<:]]%s[[:>:]]", argv
[*argi
]);
706 mandoc_asprintf(&val
, "\\<%s\\>", argv
[*argi
]);
708 mandoc_asprintf(&val
,
709 "(^|[^a-zA-Z01-9_])%s([^a-zA-Z01-9_]|$)", argv
[*argi
]);
712 } else if ((val
= strpbrk(argv
[*argi
], "=~")) == NULL
) {
713 e
->bits
= TYPE_Nm
| TYPE_Nd
;
714 e
->match
.type
= DBM_SUB
;
715 e
->match
.str
= argv
[*argi
];
717 if (val
== argv
[*argi
])
718 e
->bits
= TYPE_Nm
| TYPE_Nd
;
720 e
->match
.type
= DBM_SUB
;
721 e
->match
.str
= val
+ 1;
723 e
->match
.type
= DBM_REGEX
;
725 if (strstr(argv
[*argi
], "arch") != NULL
)
729 /* Compile regular expressions. */
731 if (e
->match
.type
== DBM_REGEX
) {
732 e
->match
.re
= mandoc_malloc(sizeof(*e
->match
.re
));
733 irc
= regcomp(e
->match
.re
, val
,
734 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
));
736 regerror(irc
, e
->match
.re
, errbuf
, sizeof(errbuf
));
737 warnx("regcomp /%s/: %s", val
, errbuf
);
739 if (search
->argmode
== ARG_WORD
)
755 * Parse out all possible fields.
756 * If the field doesn't resolve, bail.
759 while (NULL
!= (key
= strsep(&argv
[*argi
], ","))) {
762 for (i
= 0, iterbit
= 1; i
< KEY_MAX
; i
++, iterbit
<<= 1) {
763 if (0 == strcasecmp(key
, mansearch_keynames
[i
])) {
769 if (strcasecmp(key
, "any"))
770 warnx("treating unknown key "
771 "\"%s\" as \"any\"", key
);
781 exprfree(struct expr
*e
)
785 if (e
->child
!= NULL
)