]>
git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
1 /* $Id: mansearch.c,v 1.82 2019/07/01 22:56:24 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
);
202 mpage
->bits
= search
->firstmatch
? rp
->bits
: 0;
204 mpage
->sec
= *page
->sect
- '0';
205 if (mpage
->sec
< 0 || mpage
->sec
> 9)
207 mpage
->form
= *page
->file
;
216 * In man(1) mode, prefer matches in earlier trees
217 * over matches in later trees.
220 if (cur
&& search
->firstmatch
)
224 qsort(*res
, cur
, sizeof(struct manpage
), manpage_compare
);
225 if (chdir_status
&& getcwd_status
&& chdir(buf
) == -1)
229 return res
!= NULL
|| cur
;
233 * Merge the results for the expression tree rooted at e
234 * into the the result list htab.
236 static struct ohash
*
237 manmerge(struct expr
*e
, struct ohash
*htab
)
241 return manmerge_term(e
, htab
);
243 return manmerge_or(e
->child
, htab
);
245 return manmerge_and(e
->child
, htab
);
251 static struct ohash
*
252 manmerge_term(struct expr
*e
, struct ohash
*htab
)
254 struct dbm_res res
, *rp
;
260 htab
= mandoc_malloc(sizeof(*htab
));
261 mandoc_ohash_init(htab
, 4, offsetof(struct dbm_res
, page
));
264 for (im
= 0, ib
= 1; im
< KEY_MAX
; im
++, ib
<<= 1) {
265 if ((e
->bits
& ib
) == 0)
270 dbm_page_byarch(&e
->match
);
273 dbm_page_bysect(&e
->match
);
276 dbm_page_byname(&e
->match
);
279 dbm_page_bydesc(&e
->match
);
282 dbm_page_bymacro(im
- 2, &e
->match
);
287 * When hashing for deduplication, use the unique
288 * page ID itself instead of a hash function;
289 * that is quite efficient.
293 res
= dbm_page_next();
296 slot
= ohash_lookup_memory(htab
,
297 (char *)&res
, sizeof(res
.page
), res
.page
);
298 if ((rp
= ohash_find(htab
, slot
)) != NULL
) {
299 rp
->bits
|= res
.bits
;
302 rp
= mandoc_malloc(sizeof(*rp
));
304 ohash_insert(htab
, slot
, rp
);
310 static struct ohash
*
311 manmerge_or(struct expr
*e
, struct ohash
*htab
)
314 htab
= manmerge(e
, htab
);
320 static struct ohash
*
321 manmerge_and(struct expr
*e
, struct ohash
*htab
)
323 struct ohash
*hand
, *h1
, *h2
;
325 unsigned int slot1
, slot2
;
327 /* Evaluate the first term of the AND clause. */
329 hand
= manmerge(e
, NULL
);
331 while ((e
= e
->next
) != NULL
) {
333 /* Evaluate the next term and prepare for ANDing. */
335 h2
= manmerge(e
, NULL
);
336 if (ohash_entries(h2
) < ohash_entries(hand
)) {
341 hand
= mandoc_malloc(sizeof(*hand
));
342 mandoc_ohash_init(hand
, 4, offsetof(struct dbm_res
, page
));
344 /* Keep all pages that are in both result sets. */
346 for (res
= ohash_first(h1
, &slot1
); res
!= NULL
;
347 res
= ohash_next(h1
, &slot1
)) {
348 if (ohash_find(h2
, ohash_lookup_memory(h2
,
349 (char *)res
, sizeof(res
->page
),
353 ohash_insert(hand
, ohash_lookup_memory(hand
,
354 (char *)res
, sizeof(res
->page
),
358 /* Discard the merged results. */
360 for (res
= ohash_first(h2
, &slot2
); res
!= NULL
;
361 res
= ohash_next(h2
, &slot2
))
369 /* Merge the result of the AND into htab. */
374 for (res
= ohash_first(hand
, &slot1
); res
!= NULL
;
375 res
= ohash_next(hand
, &slot1
)) {
376 slot2
= ohash_lookup_memory(htab
,
377 (char *)res
, sizeof(res
->page
), res
->page
);
378 if (ohash_find(htab
, slot2
) == NULL
)
379 ohash_insert(htab
, slot2
, res
);
384 /* Discard the merged result. */
392 mansearch_free(struct manpage
*res
, size_t sz
)
396 for (i
= 0; i
< sz
; i
++) {
405 manpage_compare(const void *vp1
, const void *vp2
)
407 const struct manpage
*mp1
, *mp2
;
408 const char *cp1
, *cp2
;
414 if ((diff
= mp2
->bits
- mp1
->bits
) ||
415 (diff
= mp1
->sec
- mp2
->sec
))
418 /* Fall back to alphabetic ordering of names. */
419 sz1
= strcspn(mp1
->names
, "(");
420 sz2
= strcspn(mp2
->names
, "(");
423 if ((diff
= strncasecmp(mp1
->names
, mp2
->names
, sz1
)))
426 /* For identical names and sections, prefer arch-dependent. */
427 cp1
= strchr(mp1
->names
+ sz1
, '/');
428 cp2
= strchr(mp2
->names
+ sz2
, '/');
429 return cp1
!= NULL
&& cp2
!= NULL
? strcasecmp(cp1
, cp2
) :
430 cp1
!= NULL
? -1 : cp2
!= NULL
? 1 : 0;
434 buildnames(const struct dbm_page
*page
)
439 sz
= lstlen(page
->name
, 2) + 1 + lstlen(page
->sect
, 2) +
440 (page
->arch
== NULL
? 0 : 1 + lstlen(page
->arch
, 2)) + 2;
441 buf
= mandoc_malloc(sz
);
443 lstcat(buf
, &i
, page
->name
, ", ");
445 lstcat(buf
, &i
, page
->sect
, ", ");
446 if (page
->arch
!= NULL
) {
448 lstcat(buf
, &i
, page
->arch
, ", ");
457 * Count the buffer space needed to print the NUL-terminated
458 * list of NUL-terminated strings, when printing sep separator
459 * characters between strings.
462 lstlen(const char *cp
, size_t sep
)
466 for (sz
= 0; *cp
!= '\0'; cp
++) {
468 /* Skip names appearing only in the SYNOPSIS. */
469 if (*cp
<= (char)(NAME_SYN
& NAME_MASK
)) {
475 /* Skip name class markers. */
479 /* Print a separator before each but the first string. */
483 /* Copy one string. */
484 while (*cp
!= '\0') {
493 * Print the NUL-terminated list of NUL-terminated strings
494 * into the buffer, seperating strings with sep.
497 lstcat(char *buf
, size_t *i
, const char *cp
, const char *sep
)
502 for (i_start
= *i
; *cp
!= '\0'; cp
++) {
504 /* Skip names appearing only in the SYNOPSIS. */
505 if (*cp
<= (char)(NAME_SYN
& NAME_MASK
)) {
511 /* Skip name class markers. */
515 /* Print a separator before each but the first string. */
522 /* Copy one string. */
530 * Return 1 if the string *want occurs in any of the strings
531 * in the NUL-terminated string list *have, or 0 otherwise.
532 * If either argument is NULL or empty, assume no filtering
533 * is desired and return 1.
536 lstmatch(const char *want
, const char *have
)
538 if (want
== NULL
|| have
== NULL
|| *have
== '\0')
540 while (*have
!= '\0') {
541 if (strcasestr(have
, want
) != NULL
)
543 have
= strchr(have
, '\0') + 1;
549 * Build a list of values taken by the macro im in the manual page.
552 buildoutput(size_t im
, struct dbm_page
*page
)
554 const char *oldoutput
, *sep
, *input
;
555 char *output
, *newoutput
, *value
;
560 return mandoc_strdup(page
->desc
);
578 sz
= lstlen(input
, 3) + 1;
579 output
= mandoc_malloc(sz
);
581 lstcat(output
, &i
, input
, " # ");
588 dbm_macro_bypage(im
- 2, page
->addr
);
589 while ((value
= dbm_macro_next()) != NULL
) {
590 if (output
== NULL
) {
597 mandoc_asprintf(&newoutput
, "%s%s%s", oldoutput
, sep
, value
);
605 * Compile a set of string tokens into an expression.
606 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
607 * "(", "foo=bar", etc.).
610 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
612 struct expr
*parent
, *child
;
613 int needterm
, nested
;
615 if ((nested
= *argi
) == argc
)
618 parent
= child
= NULL
;
619 while (*argi
< argc
) {
620 if (strcmp(")", argv
[*argi
]) == 0) {
622 warnx("missing term "
623 "before closing parenthesis");
627 warnx("ignoring unmatched right parenthesis");
631 if (strcmp("-o", argv
[*argi
]) == 0) {
634 warnx("ignoring -o after %s",
637 warnx("ignoring initial -o");
645 child
= expr_and(search
, argc
, argv
, argi
);
648 if (parent
== NULL
) {
649 parent
= mandoc_calloc(1, sizeof(*parent
));
650 parent
->type
= EXPR_OR
;
652 parent
->child
= child
;
654 child
->next
= expr_and(search
, argc
, argv
, argi
);
657 if (needterm
&& *argi
)
658 warnx("ignoring trailing %s", argv
[*argi
- 1]);
659 return parent
== NULL
? child
: parent
;
663 expr_and(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
665 struct expr
*parent
, *child
;
669 parent
= child
= NULL
;
670 while (*argi
< argc
) {
671 if (strcmp(")", argv
[*argi
]) == 0) {
673 warnx("missing term "
674 "before closing parenthesis");
678 if (strcmp("-o", argv
[*argi
]) == 0)
680 if (strcmp("-a", argv
[*argi
]) == 0) {
683 warnx("ignoring -a after %s",
686 warnx("ignoring initial -a");
695 child
= exprterm(search
, argc
, argv
, argi
);
701 if (parent
== NULL
) {
702 parent
= mandoc_calloc(1, sizeof(*parent
));
703 parent
->type
= EXPR_AND
;
705 parent
->child
= child
;
707 child
->next
= exprterm(search
, argc
, argv
, argi
);
708 if (child
->next
!= NULL
) {
713 if (needterm
&& *argi
)
714 warnx("ignoring trailing %s", argv
[*argi
- 1]);
715 return parent
== NULL
? child
: parent
;
719 exprterm(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
727 if (strcmp("(", argv
[*argi
]) == 0) {
729 e
= exprcomp(search
, argc
, argv
, argi
);
731 assert(strcmp(")", argv
[*argi
]) == 0);
734 warnx("unclosed parenthesis");
738 if (strcmp("-i", argv
[*argi
]) == 0 && *argi
+ 1 < argc
) {
744 e
= mandoc_calloc(1, sizeof(*e
));
750 if (search
->argmode
== ARG_NAME
) {
752 e
->match
.type
= DBM_EXACT
;
753 e
->match
.str
= argv
[(*argi
)++];
758 * Separate macro keys from search string.
759 * If needed, request regular expression handling.
762 if (search
->argmode
== ARG_WORD
) {
764 e
->match
.type
= DBM_REGEX
;
766 mandoc_asprintf(&val
, "[[:<:]]%s[[:>:]]", argv
[*argi
]);
768 mandoc_asprintf(&val
, "\\<%s\\>", argv
[*argi
]);
770 mandoc_asprintf(&val
,
771 "(^|[^a-zA-Z01-9_])%s([^a-zA-Z01-9_]|$)", argv
[*argi
]);
774 } else if ((val
= strpbrk(argv
[*argi
], "=~")) == NULL
) {
775 e
->bits
= TYPE_Nm
| TYPE_Nd
;
776 e
->match
.type
= DBM_REGEX
;
780 if (val
== argv
[*argi
])
781 e
->bits
= TYPE_Nm
| TYPE_Nd
;
783 e
->match
.type
= DBM_SUB
;
784 e
->match
.str
= val
+ 1;
786 e
->match
.type
= DBM_REGEX
;
788 if (strstr(argv
[*argi
], "arch") != NULL
)
792 /* Compile regular expressions. */
794 if (e
->match
.type
== DBM_REGEX
) {
795 e
->match
.re
= mandoc_malloc(sizeof(*e
->match
.re
));
796 irc
= regcomp(e
->match
.re
, val
,
797 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
));
799 regerror(irc
, e
->match
.re
, errbuf
, sizeof(errbuf
));
800 warnx("regcomp /%s/: %s", val
, errbuf
);
802 if (search
->argmode
== ARG_WORD
)
818 * Parse out all possible fields.
819 * If the field doesn't resolve, bail.
822 while (NULL
!= (key
= strsep(&argv
[*argi
], ","))) {
825 for (i
= 0, iterbit
= 1; i
< KEY_MAX
; i
++, iterbit
<<= 1) {
826 if (0 == strcasecmp(key
, mansearch_keynames
[i
])) {
832 if (strcasecmp(key
, "any"))
833 warnx("treating unknown key "
834 "\"%s\" as \"any\"", key
);
844 exprfree(struct expr
*e
)
848 if (e
->child
!= NULL
)