]>
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, 2014, 2015, 2016 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, int32_t);
71 static size_t lstlen(const char *);
72 static void lstcat(char *, size_t *, 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) {
158 warn("%s/%s", paths
->paths
[i
], MANDOC_DB
);
162 if ((htab
= manmerge(e
, NULL
)) == NULL
) {
167 for (rp
= ohash_first(htab
, &slot
); rp
!= NULL
;
168 rp
= ohash_next(htab
, &slot
)) {
169 page
= dbm_page_get(rp
->page
);
171 if (lstmatch(search
->sec
, page
->sect
) == 0 ||
172 lstmatch(search
->arch
, page
->arch
) == 0)
175 if (cur
+ 1 > maxres
) {
177 *res
= mandoc_reallocarray(*res
,
178 maxres
, sizeof(**res
));
181 mandoc_asprintf(&mpage
->file
, "%s/%s",
182 paths
->paths
[i
], page
->file
+ 1);
183 mpage
->names
= buildnames(page
);
184 mpage
->output
= (int)outkey
== KEY_Nd
?
185 mandoc_strdup(page
->desc
) :
186 buildoutput(outkey
, page
->addr
);
188 mpage
->bits
= rp
->bits
;
189 mpage
->sec
= *page
->sect
- '0';
190 if (mpage
->sec
< 0 || mpage
->sec
> 9)
192 mpage
->form
= *page
->file
;
201 * In man(1) mode, prefer matches in earlier trees
202 * over matches in later trees.
205 if (cur
&& search
->firstmatch
)
208 qsort(*res
, cur
, sizeof(struct manpage
), manpage_compare
);
209 if (chdir_status
&& getcwd_status
&& chdir(buf
) == -1)
217 * Merge the results for the expression tree rooted at e
218 * into the the result list htab.
220 static struct ohash
*
221 manmerge(struct expr
*e
, struct ohash
*htab
)
225 return manmerge_term(e
, htab
);
227 return manmerge_or(e
->child
, htab
);
229 return manmerge_and(e
->child
, htab
);
235 static struct ohash
*
236 manmerge_term(struct expr
*e
, struct ohash
*htab
)
238 struct dbm_res res
, *rp
;
244 htab
= mandoc_malloc(sizeof(*htab
));
245 mandoc_ohash_init(htab
, 4, offsetof(struct dbm_res
, page
));
248 for (im
= 0, ib
= 1; im
< KEY_MAX
; im
++, ib
<<= 1) {
249 if ((e
->bits
& ib
) == 0)
254 dbm_page_byarch(&e
->match
);
257 dbm_page_bysect(&e
->match
);
260 dbm_page_byname(&e
->match
);
263 dbm_page_bydesc(&e
->match
);
266 dbm_page_bymacro(im
- 2, &e
->match
);
271 * When hashing for deduplication, use the unique
272 * page ID itself instead of a hash function;
273 * that is quite efficient.
277 res
= dbm_page_next();
280 slot
= ohash_lookup_memory(htab
,
281 (char *)&res
, sizeof(res
.page
), res
.page
);
282 if ((rp
= ohash_find(htab
, slot
)) != NULL
) {
283 rp
->bits
|= res
.bits
;
286 rp
= mandoc_malloc(sizeof(*rp
));
288 ohash_insert(htab
, slot
, rp
);
294 static struct ohash
*
295 manmerge_or(struct expr
*e
, struct ohash
*htab
)
298 htab
= manmerge(e
, htab
);
304 static struct ohash
*
305 manmerge_and(struct expr
*e
, struct ohash
*htab
)
307 struct ohash
*hand
, *h1
, *h2
;
309 unsigned int slot1
, slot2
;
311 /* Evaluate the first term of the AND clause. */
313 hand
= manmerge(e
, NULL
);
315 while ((e
= e
->next
) != NULL
) {
317 /* Evaluate the next term and prepare for ANDing. */
319 h2
= manmerge(e
, NULL
);
320 if (ohash_entries(h2
) < ohash_entries(hand
)) {
325 hand
= mandoc_malloc(sizeof(*hand
));
326 mandoc_ohash_init(hand
, 4, offsetof(struct dbm_res
, page
));
328 /* Keep all pages that are in both result sets. */
330 for (res
= ohash_first(h1
, &slot1
); res
!= NULL
;
331 res
= ohash_next(h1
, &slot1
)) {
332 if (ohash_find(h2
, ohash_lookup_memory(h2
,
333 (char *)res
, sizeof(res
->page
),
337 ohash_insert(hand
, ohash_lookup_memory(hand
,
338 (char *)res
, sizeof(res
->page
),
342 /* Discard the merged results. */
344 for (res
= ohash_first(h2
, &slot2
); res
!= NULL
;
345 res
= ohash_next(h2
, &slot2
))
353 /* Merge the result of the AND into htab. */
358 for (res
= ohash_first(hand
, &slot1
); res
!= NULL
;
359 res
= ohash_next(hand
, &slot1
)) {
360 slot2
= ohash_lookup_memory(htab
,
361 (char *)res
, sizeof(res
->page
), res
->page
);
362 if (ohash_find(htab
, slot2
) == NULL
)
363 ohash_insert(htab
, slot2
, res
);
368 /* Discard the merged result. */
376 mansearch_free(struct manpage
*res
, size_t sz
)
380 for (i
= 0; i
< sz
; i
++) {
389 manpage_compare(const void *vp1
, const void *vp2
)
391 const struct manpage
*mp1
, *mp2
;
396 return (diff
= mp2
->bits
- mp1
->bits
) ? diff
:
397 (diff
= mp1
->sec
- mp2
->sec
) ? diff
:
398 strcasecmp(mp1
->names
, mp2
->names
);
402 buildnames(const struct dbm_page
*page
)
407 sz
= lstlen(page
->name
) + 1 + lstlen(page
->sect
) +
408 (page
->arch
== NULL
? 0 : 1 + lstlen(page
->arch
)) + 2;
409 buf
= mandoc_malloc(sz
);
411 lstcat(buf
, &i
, page
->name
);
413 lstcat(buf
, &i
, page
->sect
);
414 if (page
->arch
!= NULL
) {
416 lstcat(buf
, &i
, page
->arch
);
425 * Count the buffer space needed to print the NUL-terminated
426 * list of NUL-terminated strings, when printing two separator
427 * characters between strings.
430 lstlen(const char *cp
)
434 for (sz
= 0;; sz
++) {
439 } else if (cp
[0] < ' ')
447 * Print the NUL-terminated list of NUL-terminated strings
448 * into the buffer, seperating strings with a comma and a blank.
451 lstcat(char *buf
, size_t *i
, const char *cp
)
459 } else if (cp
[0] >= ' ')
466 * Return 1 if the string *want occurs in any of the strings
467 * in the NUL-terminated string list *have, or 0 otherwise.
468 * If either argument is NULL or empty, assume no filtering
469 * is desired and return 1.
472 lstmatch(const char *want
, const char *have
)
474 if (want
== NULL
|| have
== NULL
|| *have
== '\0')
476 while (*have
!= '\0') {
477 if (strcasestr(have
, want
) != NULL
)
479 have
= strchr(have
, '\0') + 1;
485 * Build a list of values taken by the macro im
486 * in the manual page with big-endian address addr.
489 buildoutput(size_t im
, int32_t addr
)
491 const char *oldoutput
, *sep
;
492 char *output
, *newoutput
, *value
;
495 dbm_macro_bypage(im
- 2, addr
);
496 while ((value
= dbm_macro_next()) != NULL
) {
497 if (output
== NULL
) {
504 mandoc_asprintf(&newoutput
, "%s%s%s", oldoutput
, sep
, value
);
512 * Compile a set of string tokens into an expression.
513 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
514 * "(", "foo=bar", etc.).
517 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
519 struct expr
*parent
, *child
;
520 int needterm
, nested
;
522 if ((nested
= *argi
) == argc
)
525 parent
= child
= NULL
;
526 while (*argi
< argc
) {
527 if (strcmp(")", argv
[*argi
]) == 0) {
529 warnx("missing term "
530 "before closing parenthesis");
534 warnx("ignoring unmatched right parenthesis");
538 if (strcmp("-o", argv
[*argi
]) == 0) {
541 warnx("ignoring -o after %s",
544 warnx("ignoring initial -o");
552 child
= expr_and(search
, argc
, argv
, argi
);
555 if (parent
== NULL
) {
556 parent
= mandoc_calloc(1, sizeof(*parent
));
557 parent
->type
= EXPR_OR
;
559 parent
->child
= child
;
561 child
->next
= expr_and(search
, argc
, argv
, argi
);
564 if (needterm
&& *argi
)
565 warnx("ignoring trailing %s", argv
[*argi
- 1]);
566 return parent
== NULL
? child
: parent
;
570 expr_and(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
572 struct expr
*parent
, *child
;
576 parent
= child
= NULL
;
577 while (*argi
< argc
) {
578 if (strcmp(")", argv
[*argi
]) == 0) {
580 warnx("missing term "
581 "before closing parenthesis");
585 if (strcmp("-o", argv
[*argi
]) == 0)
587 if (strcmp("-a", argv
[*argi
]) == 0) {
590 warnx("ignoring -a after %s",
593 warnx("ignoring initial -a");
602 child
= exprterm(search
, argc
, argv
, argi
);
608 if (parent
== NULL
) {
609 parent
= mandoc_calloc(1, sizeof(*parent
));
610 parent
->type
= EXPR_AND
;
612 parent
->child
= child
;
614 child
->next
= exprterm(search
, argc
, argv
, argi
);
615 if (child
->next
!= NULL
) {
620 if (needterm
&& *argi
)
621 warnx("ignoring trailing %s", argv
[*argi
- 1]);
622 return parent
== NULL
? child
: parent
;
626 exprterm(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
634 if (strcmp("(", argv
[*argi
]) == 0) {
636 e
= exprcomp(search
, argc
, argv
, argi
);
638 assert(strcmp(")", argv
[*argi
]) == 0);
641 warnx("unclosed parenthesis");
645 e
= mandoc_calloc(1, sizeof(*e
));
651 if (search
->argmode
== ARG_NAME
) {
653 e
->match
.type
= DBM_EXACT
;
654 e
->match
.str
= argv
[(*argi
)++];
659 * Separate macro keys from search string.
660 * If needed, request regular expression handling.
664 if (search
->argmode
== ARG_WORD
) {
666 e
->match
.type
= DBM_REGEX
;
668 mandoc_asprintf(&val
, "[[:<:]]%s[[:>:]]", argv
[*argi
]);
670 mandoc_asprintf(&val
, "\\<%s\\>", argv
[*argi
]);
672 mandoc_asprintf(&val
,
673 "(^|[^a-zA-Z01-9_])%s([^a-zA-Z01-9_]|$)", argv
[*argi
]);
676 } else if ((val
= strpbrk(argv
[*argi
], "=~")) == NULL
) {
677 e
->bits
= TYPE_Nm
| TYPE_Nd
;
678 e
->match
.type
= DBM_SUB
;
679 e
->match
.str
= argv
[*argi
];
681 if (val
== argv
[*argi
])
682 e
->bits
= TYPE_Nm
| TYPE_Nd
;
684 e
->match
.type
= DBM_SUB
;
685 e
->match
.str
= val
+ 1;
687 e
->match
.type
= DBM_REGEX
;
689 if (strstr(argv
[*argi
], "arch") != NULL
)
693 /* Compile regular expressions. */
695 if (e
->match
.type
== DBM_REGEX
) {
696 e
->match
.re
= mandoc_malloc(sizeof(*e
->match
.re
));
697 irc
= regcomp(e
->match
.re
, val
,
698 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
));
700 regerror(irc
, e
->match
.re
, errbuf
, sizeof(errbuf
));
701 warnx("regcomp /%s/: %s", val
, errbuf
);
703 if (search
->argmode
== ARG_WORD
)
719 * Parse out all possible fields.
720 * If the field doesn't resolve, bail.
723 while (NULL
!= (key
= strsep(&argv
[*argi
], ","))) {
726 for (i
= 0, iterbit
= 1; i
< KEY_MAX
; i
++, iterbit
<<= 1) {
727 if (0 == strcasecmp(key
, mansearch_keynames
[i
])) {
733 if (strcasecmp(key
, "any"))
734 warnx("treating unknown key "
735 "\"%s\" as \"any\"", key
);
745 exprfree(struct expr
*e
)
749 if (e
->child
!= NULL
)