]>
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>
38 #include "mandoc_aux.h"
39 #include "mandoc_ohash.h"
41 #include "mansearch.h"
46 struct dbm_match match
; /* Match type and expression. */
47 uint64_t bits
; /* Type mask. */
48 /* Used for OR and AND groups: */
49 struct expr
*next
; /* Next child in the parent group. */
50 struct expr
*child
; /* First child in this group. */
51 enum { EXPR_TERM
, EXPR_OR
, EXPR_AND
} type
;
54 const char *const mansearch_keynames
[KEY_MAX
] = {
55 "arch", "sec", "Xr", "Ar", "Fa", "Fl", "Dv", "Fn",
56 "Ic", "Pa", "Cm", "Li", "Em", "Cd", "Va", "Ft",
57 "Tn", "Er", "Ev", "Sy", "Sh", "In", "Ss", "Ox",
58 "An", "Mt", "St", "Bx", "At", "Nx", "Fx", "Lk",
59 "Ms", "Bsx", "Dx", "Rs", "Vt", "Lb", "Nm", "Nd"
63 static struct ohash
*manmerge(struct expr
*, struct ohash
*);
64 static struct ohash
*manmerge_term(struct expr
*, struct ohash
*);
65 static struct ohash
*manmerge_or(struct expr
*, struct ohash
*);
66 static struct ohash
*manmerge_and(struct expr
*, struct ohash
*);
67 static char *buildnames(const struct dbm_page
*);
68 static char *buildoutput(size_t, int32_t);
69 static size_t lstlen(const char *);
70 static void lstcat(char *, size_t *, const char *);
71 static int lstmatch(const char *, const char *);
72 static struct expr
*exprcomp(const struct mansearch
*,
73 int, char *[], int *);
74 static struct expr
*expr_and(const struct mansearch
*,
75 int, char *[], int *);
76 static struct expr
*exprterm(const struct mansearch
*,
77 int, char *[], int *);
78 static void exprfree(struct expr
*);
79 static int manpage_compare(const void *, const void *);
83 mansearch(const struct mansearch
*search
,
84 const struct manpaths
*paths
,
85 int argc
, char *argv
[],
86 struct manpage
**res
, size_t *sz
)
91 struct dbm_page
*page
;
92 struct manpage
*mpage
;
94 size_t cur
, i
, maxres
, outkey
;
96 int argi
, chdir_status
, getcwd_status
, im
;
99 if ((e
= exprcomp(search
, argc
, argv
, &argi
)) == NULL
) {
108 if (search
->outkey
!= NULL
)
109 for (im
= 0; im
< KEY_MAX
; im
++)
110 if (0 == strcasecmp(search
->outkey
,
111 mansearch_keynames
[im
])) {
117 * Remember the original working directory, if possible.
118 * This will be needed if the second or a later directory
119 * is given as a relative path.
120 * Do not error out if the current directory is not
121 * searchable: Maybe it won't be needed after all.
124 if (getcwd(buf
, PATH_MAX
) == NULL
) {
126 (void)strlcpy(buf
, strerror(errno
), sizeof(buf
));
131 * Loop over the directories (containing databases) for us to
133 * Don't let missing/bad databases/directories phase us.
134 * In each, try to open the resident database and, if it opens,
135 * scan it for our match expression.
139 for (i
= 0; i
< paths
->sz
; i
++) {
140 if (chdir_status
&& paths
->paths
[i
][0] != '/') {
141 if ( ! getcwd_status
) {
142 warnx("%s: getcwd: %s", paths
->paths
[i
], buf
);
144 } else if (chdir(buf
) == -1) {
149 if (chdir(paths
->paths
[i
]) == -1) {
150 warn("%s", paths
->paths
[i
]);
155 if (dbm_open(MANDOC_DB
) == -1) {
156 warn("%s/%s", paths
->paths
[i
], MANDOC_DB
);
160 if ((htab
= manmerge(e
, NULL
)) == NULL
) {
165 for (rp
= ohash_first(htab
, &slot
); rp
!= NULL
;
166 rp
= ohash_next(htab
, &slot
)) {
167 page
= dbm_page_get(rp
->page
);
169 if (lstmatch(search
->sec
, page
->sect
) == 0 ||
170 lstmatch(search
->arch
, page
->arch
) == 0)
173 if (cur
+ 1 > maxres
) {
175 *res
= mandoc_reallocarray(*res
,
176 maxres
, sizeof(**res
));
179 mandoc_asprintf(&mpage
->file
, "%s/%s",
180 paths
->paths
[i
], page
->file
+ 1);
181 mpage
->names
= buildnames(page
);
182 mpage
->output
= (int)outkey
== KEY_Nd
?
183 mandoc_strdup(page
->desc
) :
184 buildoutput(outkey
, page
->addr
);
186 mpage
->bits
= rp
->bits
;
187 mpage
->sec
= *page
->sect
- '0';
188 if (mpage
->sec
< 0 || mpage
->sec
> 9)
190 mpage
->form
= *page
->file
;
199 * In man(1) mode, prefer matches in earlier trees
200 * over matches in later trees.
203 if (cur
&& search
->firstmatch
)
206 qsort(*res
, cur
, sizeof(struct manpage
), manpage_compare
);
207 if (chdir_status
&& getcwd_status
&& chdir(buf
) == -1)
215 * Merge the results for the expression tree rooted at e
216 * into the the result list htab.
218 static struct ohash
*
219 manmerge(struct expr
*e
, struct ohash
*htab
)
223 return manmerge_term(e
, htab
);
225 return manmerge_or(e
->child
, htab
);
227 return manmerge_and(e
->child
, htab
);
233 static struct ohash
*
234 manmerge_term(struct expr
*e
, struct ohash
*htab
)
236 struct dbm_res res
, *rp
;
242 htab
= mandoc_malloc(sizeof(*htab
));
243 mandoc_ohash_init(htab
, 4, offsetof(struct dbm_res
, page
));
246 for (im
= 0, ib
= 1; im
< KEY_MAX
; im
++, ib
<<= 1) {
247 if ((e
->bits
& ib
) == 0)
252 dbm_page_byarch(&e
->match
);
255 dbm_page_bysect(&e
->match
);
258 dbm_page_byname(&e
->match
);
261 dbm_page_bydesc(&e
->match
);
264 dbm_page_bymacro(im
- 2, &e
->match
);
269 * When hashing for deduplication, use the unique
270 * page ID itself instead of a hash function;
271 * that is quite efficient.
275 res
= dbm_page_next();
278 slot
= ohash_lookup_memory(htab
,
279 (char *)&res
, sizeof(res
.page
), res
.page
);
280 if ((rp
= ohash_find(htab
, slot
)) != NULL
) {
281 rp
->bits
|= res
.bits
;
284 rp
= mandoc_malloc(sizeof(*rp
));
286 ohash_insert(htab
, slot
, rp
);
292 static struct ohash
*
293 manmerge_or(struct expr
*e
, struct ohash
*htab
)
296 htab
= manmerge(e
, htab
);
302 static struct ohash
*
303 manmerge_and(struct expr
*e
, struct ohash
*htab
)
305 struct ohash
*hand
, *h1
, *h2
;
307 unsigned int slot1
, slot2
;
309 /* Evaluate the first term of the AND clause. */
311 hand
= manmerge(e
, NULL
);
313 while ((e
= e
->next
) != NULL
) {
315 /* Evaluate the next term and prepare for ANDing. */
317 h2
= manmerge(e
, NULL
);
318 if (ohash_entries(h2
) < ohash_entries(hand
)) {
323 hand
= mandoc_malloc(sizeof(*hand
));
324 mandoc_ohash_init(hand
, 4, offsetof(struct dbm_res
, page
));
326 /* Keep all pages that are in both result sets. */
328 for (res
= ohash_first(h1
, &slot1
); res
!= NULL
;
329 res
= ohash_next(h1
, &slot1
)) {
330 if (ohash_find(h2
, ohash_lookup_memory(h2
,
331 (char *)res
, sizeof(res
->page
),
335 ohash_insert(hand
, ohash_lookup_memory(hand
,
336 (char *)res
, sizeof(res
->page
),
340 /* Discard the merged results. */
342 for (res
= ohash_first(h2
, &slot2
); res
!= NULL
;
343 res
= ohash_next(h2
, &slot2
))
351 /* Merge the result of the AND into htab. */
356 for (res
= ohash_first(hand
, &slot1
); res
!= NULL
;
357 res
= ohash_next(hand
, &slot1
)) {
358 slot2
= ohash_lookup_memory(htab
,
359 (char *)res
, sizeof(res
->page
), res
->page
);
360 if (ohash_find(htab
, slot2
) == NULL
)
361 ohash_insert(htab
, slot2
, res
);
366 /* Discard the merged result. */
374 mansearch_free(struct manpage
*res
, size_t sz
)
378 for (i
= 0; i
< sz
; i
++) {
387 manpage_compare(const void *vp1
, const void *vp2
)
389 const struct manpage
*mp1
, *mp2
;
394 return (diff
= mp2
->bits
- mp1
->bits
) ? diff
:
395 (diff
= mp1
->sec
- mp2
->sec
) ? diff
:
396 strcasecmp(mp1
->names
, mp2
->names
);
400 buildnames(const struct dbm_page
*page
)
405 sz
= lstlen(page
->name
) + 1 + lstlen(page
->sect
) +
406 (page
->arch
== NULL
? 0 : 1 + lstlen(page
->arch
)) + 2;
407 buf
= mandoc_malloc(sz
);
409 lstcat(buf
, &i
, page
->name
);
411 lstcat(buf
, &i
, page
->sect
);
412 if (page
->arch
!= NULL
) {
414 lstcat(buf
, &i
, page
->arch
);
423 * Count the buffer space needed to print the NUL-terminated
424 * list of NUL-terminated strings, when printing two separator
425 * characters between strings.
428 lstlen(const char *cp
)
432 for (sz
= 0;; sz
++) {
437 } else if (cp
[0] < ' ')
445 * Print the NUL-terminated list of NUL-terminated strings
446 * into the buffer, seperating strings with a comma and a blank.
449 lstcat(char *buf
, size_t *i
, const char *cp
)
457 } else if (cp
[0] >= ' ')
464 * Return 1 if the string *want occurs in any of the strings
465 * in the NUL-terminated string list *have, or 0 otherwise.
466 * If either argument is NULL or empty, assume no filtering
467 * is desired and return 1.
470 lstmatch(const char *want
, const char *have
)
472 if (want
== NULL
|| have
== NULL
|| *have
== '\0')
474 while (*have
!= '\0') {
475 if (strcasestr(have
, want
) != NULL
)
477 have
= strchr(have
, '\0') + 1;
483 * Build a list of values taken by the macro im
484 * in the manual page with big-endian address addr.
487 buildoutput(size_t im
, int32_t addr
)
489 const char *oldoutput
, *sep
;
490 char *output
, *newoutput
, *value
;
493 dbm_macro_bypage(im
- 2, addr
);
494 while ((value
= dbm_macro_next()) != NULL
) {
495 if (output
== NULL
) {
502 mandoc_asprintf(&newoutput
, "%s%s%s", oldoutput
, sep
, value
);
510 * Compile a set of string tokens into an expression.
511 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
512 * "(", "foo=bar", etc.).
515 exprcomp(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
517 struct expr
*parent
, *child
;
518 int needterm
, nested
;
520 if ((nested
= *argi
) == argc
)
523 parent
= child
= NULL
;
524 while (*argi
< argc
) {
525 if (strcmp(")", argv
[*argi
]) == 0) {
527 warnx("missing term "
528 "before closing parenthesis");
532 warnx("ignoring unmatched right parenthesis");
536 if (strcmp("-o", argv
[*argi
]) == 0) {
539 warnx("ignoring -o after %s",
542 warnx("ignoring initial -o");
550 child
= expr_and(search
, argc
, argv
, argi
);
553 if (parent
== NULL
) {
554 parent
= mandoc_calloc(1, sizeof(*parent
));
555 parent
->type
= EXPR_OR
;
557 parent
->child
= child
;
559 child
->next
= expr_and(search
, argc
, argv
, argi
);
562 if (needterm
&& *argi
)
563 warnx("ignoring trailing %s", argv
[*argi
- 1]);
564 return parent
== NULL
? child
: parent
;
568 expr_and(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
570 struct expr
*parent
, *child
;
574 parent
= child
= NULL
;
575 while (*argi
< argc
) {
576 if (strcmp(")", argv
[*argi
]) == 0) {
578 warnx("missing term "
579 "before closing parenthesis");
583 if (strcmp("-o", argv
[*argi
]) == 0)
585 if (strcmp("-a", argv
[*argi
]) == 0) {
588 warnx("ignoring -a after %s",
591 warnx("ignoring initial -a");
600 child
= exprterm(search
, argc
, argv
, argi
);
606 if (parent
== NULL
) {
607 parent
= mandoc_calloc(1, sizeof(*parent
));
608 parent
->type
= EXPR_AND
;
610 parent
->child
= child
;
612 child
->next
= exprterm(search
, argc
, argv
, argi
);
613 if (child
->next
!= NULL
) {
618 if (needterm
&& *argi
)
619 warnx("ignoring trailing %s", argv
[*argi
- 1]);
620 return parent
== NULL
? child
: parent
;
624 exprterm(const struct mansearch
*search
, int argc
, char *argv
[], int *argi
)
632 if (strcmp("(", argv
[*argi
]) == 0) {
634 e
= exprcomp(search
, argc
, argv
, argi
);
636 assert(strcmp(")", argv
[*argi
]) == 0);
639 warnx("unclosed parenthesis");
643 e
= mandoc_calloc(1, sizeof(*e
));
649 if (search
->argmode
== ARG_NAME
) {
651 e
->match
.type
= DBM_EXACT
;
652 e
->match
.str
= argv
[(*argi
)++];
657 * Separate macro keys from search string.
658 * If needed, request regular expression handling.
661 if (search
->argmode
== ARG_WORD
) {
663 e
->match
.type
= DBM_REGEX
;
665 mandoc_asprintf(&val
, "[[:<:]]%s[[:>:]]", argv
[*argi
]);
667 mandoc_asprintf(&val
, "\\<%s\\>", argv
[*argi
]);
669 mandoc_asprintf(&val
,
670 "(^|[^a-zA-Z01-9_])%s([^a-zA-Z01-9_]|$)", argv
[*argi
]);
673 } else if ((val
= strpbrk(argv
[*argi
], "=~")) == NULL
) {
674 e
->bits
= TYPE_Nm
| TYPE_Nd
;
675 e
->match
.type
= DBM_SUB
;
676 e
->match
.str
= argv
[*argi
];
678 if (val
== argv
[*argi
])
679 e
->bits
= TYPE_Nm
| TYPE_Nd
;
681 e
->match
.type
= DBM_SUB
;
682 e
->match
.str
= val
+ 1;
684 e
->match
.type
= DBM_REGEX
;
686 if (strstr(argv
[*argi
], "arch") != NULL
)
690 /* Compile regular expressions. */
692 if (e
->match
.type
== DBM_REGEX
) {
693 e
->match
.re
= mandoc_malloc(sizeof(*e
->match
.re
));
694 irc
= regcomp(e
->match
.re
, val
,
695 REG_EXTENDED
| REG_NOSUB
| (cs
? 0 : REG_ICASE
));
697 regerror(irc
, e
->match
.re
, errbuf
, sizeof(errbuf
));
698 warnx("regcomp /%s/: %s", val
, errbuf
);
700 if (search
->argmode
== ARG_WORD
)
716 * Parse out all possible fields.
717 * If the field doesn't resolve, bail.
720 while (NULL
!= (key
= strsep(&argv
[*argi
], ","))) {
723 for (i
= 0, iterbit
= 1; i
< KEY_MAX
; i
++, iterbit
<<= 1) {
724 if (0 == strcasecmp(key
, mansearch_keynames
[i
])) {
730 if (strcasecmp(key
, "any"))
731 warnx("treating unknown key "
732 "\"%s\" as \"any\"", key
);
742 exprfree(struct expr
*e
)
746 if (e
->child
!= NULL
)