]>
git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
1 /* $Id: mansearch.c,v 1.80 2018/12/13 11:55:46 schwarze Exp $ */
3 * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013-2018 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>
39 #include "mandoc_aux.h"
40 #include "mandoc_ohash.h"
42 #include "mansearch.h"
47 struct dbm_match match
; /* Match type and expression. */
48 uint64_t bits
; /* Type mask. */
49 /* Used for OR and AND groups: */
50 struct expr
*next
; /* Next child in the parent group. */
51 struct expr
*child
; /* First child in this group. */
52 enum { EXPR_TERM
, EXPR_OR
, EXPR_AND
} type
;
55 const char *const mansearch_keynames
[KEY_MAX
] = {
56 "arch", "sec", "Xr", "Ar", "Fa", "Fl", "Dv", "Fn",
57 "Ic", "Pa", "Cm", "Li", "Em", "Cd", "Va", "Ft",
58 "Tn", "Er", "Ev", "Sy", "Sh", "In", "Ss", "Ox",
59 "An", "Mt", "St", "Bx", "At", "Nx", "Fx", "Lk",
60 "Ms", "Bsx", "Dx", "Rs", "Vt", "Lb", "Nm", "Nd"
64 static struct ohash
*manmerge(struct expr
*, struct ohash
*);
65 static struct ohash
*manmerge_term(struct expr
*, struct ohash
*);
66 static struct ohash
*manmerge_or(struct expr
*, struct ohash
*);
67 static struct ohash
*manmerge_and(struct expr
*, struct ohash
*);
68 static char *buildnames(const struct dbm_page
*);
69 static char *buildoutput(size_t, struct dbm_page
*);
70 static size_t lstlen(const char *, size_t);
71 static void lstcat(char *, size_t *, const char *, const char *);
72 static int lstmatch(const char *, const char *);
73 static struct expr
*exprcomp(const struct mansearch
*,
74 int, char *[], int *);
75 static struct expr
*expr_and(const struct mansearch
*,
76 int, char *[], int *);
77 static struct expr
*exprterm(const struct mansearch
*,
78 int, char *[], int *);
79 static void exprfree(struct expr
*);
80 static int manpage_compare(const void *, const void *);
84 mansearch(const struct mansearch
*search
,
85 const struct manpaths
*paths
,
86 int argc
, char *argv
[],
87 struct manpage
**res
, size_t *sz
)
92 struct dbm_page
*page
;
93 struct manpage
*mpage
;
95 size_t cur
, i
, maxres
, outkey
;
97 int argi
, chdir_status
, getcwd_status
, im
;
100 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 ||
174 (search
->argmode
== ARG_NAME
&&
175 rp
->bits
<= (int32_t)(NAME_SYN
& NAME_MASK
)))
182 if (cur
+ 1 > maxres
) {
184 *res
= mandoc_reallocarray(*res
,
185 maxres
, sizeof(**res
));
188 mandoc_asprintf(&mpage
->file
, "%s/%s",
189 paths
->paths
[i
], page
->file
+ 1);
190 if (access(chdir_status
? page
->file
+ 1 :
191 mpage
->file
, R_OK
) == -1) {
192 warn("%s", mpage
->file
);
193 warnx("outdated mandoc.db contains "
194 "bogus %s entry, run makewhatis %s",
195 page
->file
+ 1, paths
->paths
[i
]);
200 mpage
->names
= buildnames(page
);
201 mpage
->output
= buildoutput(outkey
, page
);
203 mpage
->sec
= *page
->sect
- '0';
204 if (mpage
->sec
< 0 || mpage
->sec
> 9)
206 mpage
->form
= *page
->file
;
215 * In man(1) mode, prefer matches in earlier trees
216 * over matches in later trees.
219 if (cur
&& search
->firstmatch
)
223 qsort(*res
, cur
, sizeof(struct manpage
), manpage_compare
);
224 if (chdir_status
&& getcwd_status
&& chdir(buf
) == -1)
228 return res
!= NULL
|| cur
;
232 * Merge the results for the expression tree rooted at e
233 * into the the result list htab.
235 static struct ohash
*
236 manmerge(struct expr
*e
, struct ohash
*htab
)
240 return manmerge_term(e
, htab
);
242 return manmerge_or(e
->child
, htab
);
244 return manmerge_and(e
->child
, htab
);
250 static struct ohash
*
251 manmerge_term(struct expr
*e
, struct ohash
*htab
)
253 struct dbm_res res
, *rp
;
259 htab
= mandoc_malloc(sizeof(*htab
));
260 mandoc_ohash_init(htab
, 4, offsetof(struct dbm_res
, page
));
263 for (im
= 0, ib
= 1; im
< KEY_MAX
; im
++, ib
<<= 1) {
264 if ((e
->bits
& ib
) == 0)
269 dbm_page_byarch(&e
->match
);
272 dbm_page_bysect(&e
->match
);
275 dbm_page_byname(&e
->match
);
278 dbm_page_bydesc(&e
->match
);
281 dbm_page_bymacro(im
- 2, &e
->match
);
286 * When hashing for deduplication, use the unique
287 * page ID itself instead of a hash function;
288 * that is quite efficient.
292 res
= dbm_page_next();
295 slot
= ohash_lookup_memory(htab
,
296 (char *)&res
, sizeof(res
.page
), res
.page
);
297 if ((rp
= ohash_find(htab
, slot
)) != NULL
)
299 rp
= mandoc_malloc(sizeof(*rp
));
301 ohash_insert(htab
, slot
, rp
);
307 static struct ohash
*
308 manmerge_or(struct expr
*e
, struct ohash
*htab
)
311 htab
= manmerge(e
, htab
);
317 static struct ohash
*
318 manmerge_and(struct expr
*e
, struct ohash
*htab
)
320 struct ohash
*hand
, *h1
, *h2
;
322 unsigned int slot1
, slot2
;
324 /* Evaluate the first term of the AND clause. */
326 hand
= manmerge(e
, NULL
);
328 while ((e
= e
->next
) != NULL
) {
330 /* Evaluate the next term and prepare for ANDing. */
332 h2
= manmerge(e
, NULL
);
333 if (ohash_entries(h2
) < ohash_entries(hand
)) {
338 hand
= mandoc_malloc(sizeof(*hand
));
339 mandoc_ohash_init(hand
, 4, offsetof(struct dbm_res
, page
));
341 /* Keep all pages that are in both result sets. */
343 for (res
= ohash_first(h1
, &slot1
); res
!= NULL
;
344 res
= ohash_next(h1
, &slot1
)) {
345 if (ohash_find(h2
, ohash_lookup_memory(h2
,
346 (char *)res
, sizeof(res
->page
),
350 ohash_insert(hand
, ohash_lookup_memory(hand
,
351 (char *)res
, sizeof(res
->page
),
355 /* Discard the merged results. */
357 for (res
= ohash_first(h2
, &slot2
); res
!= NULL
;
358 res
= ohash_next(h2
, &slot2
))
366 /* Merge the result of the AND into htab. */
371 for (res
= ohash_first(hand
, &slot1
); res
!= NULL
;
372 res
= ohash_next(hand
, &slot1
)) {
373 slot2
= ohash_lookup_memory(htab
,
374 (char *)res
, sizeof(res
->page
), res
->page
);
375 if (ohash_find(htab
, slot2
) == NULL
)
376 ohash_insert(htab
, slot2
, res
);
381 /* Discard the merged result. */
389 mansearch_free(struct manpage
*res
, size_t sz
)
393 for (i
= 0; i
< sz
; i
++) {
402 manpage_compare(const void *vp1
, const void *vp2
)
404 const struct manpage
*mp1
, *mp2
;
405 const char *cp1
, *cp2
;
411 if ((diff
= mp1
->sec
- mp2
->sec
))
414 /* Fall back to alphabetic ordering of names. */
415 sz1
= strcspn(mp1
->names
, "(");
416 sz2
= strcspn(mp2
->names
, "(");
419 if ((diff
= strncasecmp(mp1
->names
, mp2
->names
, sz1
)))
422 /* For identical names and sections, prefer arch-dependent. */
423 cp1
= strchr(mp1
->names
+ sz1
, '/');
424 cp2
= strchr(mp2
->names
+ sz2
, '/');
425 return cp1
!= NULL
&& cp2
!= NULL
? strcasecmp(cp1
, cp2
) :
426 cp1
!= NULL
? -1 : cp2
!= NULL
? 1 : 0;
430 buildnames(const struct dbm_page
*page
)
435 sz
= lstlen(page
->name
, 2) + 1 + lstlen(page
->sect
, 2) +
436 (page
->arch
== NULL
? 0 : 1 + lstlen(page
->arch
, 2)) + 2;
437 buf
= mandoc_malloc(sz
);
439 lstcat(buf
, &i
, page
->name
, ", ");
441 lstcat(buf
, &i
, page
->sect
, ", ");
442 if (page
->arch
!= NULL
) {
444 lstcat(buf
, &i
, page
->arch
, ", ");
453 * Count the buffer space needed to print the NUL-terminated
454 * list of NUL-terminated strings, when printing sep separator
455 * characters between strings.
458 lstlen(const char *cp
, size_t sep
)
462 for (sz
= 0; *cp
!= '\0'; cp
++) {
464 /* Skip names appearing only in the SYNOPSIS. */
465 if (*cp
<= (char)(NAME_SYN
& NAME_MASK
)) {
471 /* Skip name class markers. */
475 /* Print a separator before each but the first string. */
479 /* Copy one string. */
480 while (*cp
!= '\0') {
489 * Print the NUL-terminated list of NUL-terminated strings
490 * into the buffer, seperating strings with sep.
493 lstcat(char *buf
, size_t *i
, const char *cp
, const char *sep
)
498 for (i_start
= *i
; *cp
!= '\0'; cp
++) {
500 /* Skip names appearing only in the SYNOPSIS. */
501 if (*cp
<= (char)(NAME_SYN
& NAME_MASK
)) {
507 /* Skip name class markers. */
511 /* Print a separator before each but the first string. */
518 /* Copy one string. */
526 * Return 1 if the string *want occurs in any of the strings
527 * in the NUL-terminated string list *have, or 0 otherwise.
528 * If either argument is NULL or empty, assume no filtering
529 * is desired and return 1.
532 lstmatch(const char *want
, const char *have
)
534 if (want
== NULL
|| have
== NULL
|| *have
== '\0')
536 while (*have
!= '\0') {
537 if (strcasestr(have
, want
) != NULL
)
539 have
= strchr(have
, '\0') + 1;
545 * Build a list of values taken by the macro im in the manual page.
548 buildoutput(size_t im
, struct dbm_page
*page
)
550 const char *oldoutput
, *sep
, *input
;
551 char *output
, *newoutput
, *value
;
556 return mandoc_strdup(page
->desc
);
574 sz
= lstlen(input
, 3) + 1;
575 output
= mandoc_malloc(sz
);
577 lstcat(output
, &i
, input
, " # ");
584 dbm_macro_bypage(im
- 2, page
->addr
);
585 while ((value
= dbm_macro_next()) != NULL
) {
586 if (output
== NULL
) {
593 mandoc_asprintf(&newoutput
, "%s%s%s", oldoutput
, sep
, value
);
601 * Compile a set of string tokens into an expression.
602 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
603 * "(", "foo=bar", etc.).
606 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
608 struct expr
*parent
, *child
;
609 int needterm
, nested
;
611 if ((nested
= *argi
) == argc
)
614 parent
= child
= NULL
;
615 while (*argi
< argc
) {
616 if (strcmp(")", argv
[*argi
]) == 0) {
618 warnx("missing term "
619 "before closing parenthesis");
623 warnx("ignoring unmatched right parenthesis");
627 if (strcmp("-o", argv
[*argi
]) == 0) {
630 warnx("ignoring -o after %s",
633 warnx("ignoring initial -o");
641 child
= expr_and(search
, argc
, argv
, argi
);
644 if (parent
== NULL
) {
645 parent
= mandoc_calloc(1, sizeof(*parent
));
646 parent
->type
= EXPR_OR
;
648 parent
->child
= child
;
650 child
->next
= expr_and(search
, argc
, argv
, argi
);
653 if (needterm
&& *argi
)
654 warnx("ignoring trailing %s", argv
[*argi
- 1]);
655 return parent
== NULL
? child
: parent
;
659 expr_and(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
661 struct expr
*parent
, *child
;
665 parent
= child
= NULL
;
666 while (*argi
< argc
) {
667 if (strcmp(")", argv
[*argi
]) == 0) {
669 warnx("missing term "
670 "before closing parenthesis");
674 if (strcmp("-o", argv
[*argi
]) == 0)
676 if (strcmp("-a", argv
[*argi
]) == 0) {
679 warnx("ignoring -a after %s",
682 warnx("ignoring initial -a");
691 child
= exprterm(search
, argc
, argv
, argi
);
697 if (parent
== NULL
) {
698 parent
= mandoc_calloc(1, sizeof(*parent
));
699 parent
->type
= EXPR_AND
;
701 parent
->child
= child
;
703 child
->next
= exprterm(search
, argc
, argv
, argi
);
704 if (child
->next
!= NULL
) {
709 if (needterm
&& *argi
)
710 warnx("ignoring trailing %s", argv
[*argi
- 1]);
711 return parent
== NULL
? child
: parent
;
715 exprterm(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
723 if (strcmp("(", argv
[*argi
]) == 0) {
725 e
= exprcomp(search
, argc
, argv
, argi
);
727 assert(strcmp(")", argv
[*argi
]) == 0);
730 warnx("unclosed parenthesis");
734 if (strcmp("-i", argv
[*argi
]) == 0 && *argi
+ 1 < argc
) {
740 e
= mandoc_calloc(1, sizeof(*e
));
746 if (search
->argmode
== ARG_NAME
) {
748 e
->match
.type
= DBM_EXACT
;
749 e
->match
.str
= argv
[(*argi
)++];
754 * Separate macro keys from search string.
755 * If needed, request regular expression handling.
758 if (search
->argmode
== ARG_WORD
) {
760 e
->match
.type
= DBM_REGEX
;
762 mandoc_asprintf(&val
, "[[:<:]]%s[[:>:]]", argv
[*argi
]);
764 mandoc_asprintf(&val
, "\\<%s\\>", argv
[*argi
]);
766 mandoc_asprintf(&val
,
767 "(^|[^a-zA-Z01-9_])%s([^a-zA-Z01-9_]|$)", argv
[*argi
]);
770 } else if ((val
= strpbrk(argv
[*argi
], "=~")) == NULL
) {
771 e
->bits
= TYPE_Nm
| TYPE_Nd
;
772 e
->match
.type
= DBM_REGEX
;
776 if (val
== argv
[*argi
])
777 e
->bits
= TYPE_Nm
| TYPE_Nd
;
779 e
->match
.type
= DBM_SUB
;
780 e
->match
.str
= val
+ 1;
782 e
->match
.type
= DBM_REGEX
;
784 if (strstr(argv
[*argi
], "arch") != NULL
)
788 /* Compile regular expressions. */
790 if (e
->match
.type
== DBM_REGEX
) {
791 e
->match
.re
= mandoc_malloc(sizeof(*e
->match
.re
));
792 irc
= regcomp(e
->match
.re
, val
,
793 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
));
795 regerror(irc
, e
->match
.re
, errbuf
, sizeof(errbuf
));
796 warnx("regcomp /%s/: %s", val
, errbuf
);
798 if (search
->argmode
== ARG_WORD
)
814 * Parse out all possible fields.
815 * If the field doesn't resolve, bail.
818 while (NULL
!= (key
= strsep(&argv
[*argi
], ","))) {
821 for (i
= 0, iterbit
= 1; i
< KEY_MAX
; i
++, iterbit
<<= 1) {
822 if (0 == strcasecmp(key
, mansearch_keynames
[i
])) {
828 if (strcasecmp(key
, "any"))
829 warnx("treating unknown key "
830 "\"%s\" as \"any\"", key
);
840 exprfree(struct expr
*e
)
844 if (e
->child
!= NULL
)