version 1.70, 2017/04/17 20:05:08 |
version 1.78, 2018/11/19 19:27:37 |
|
|
/* $OpenBSD$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2013-2017 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2013-2018 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the above |
* purpose with or without fee is hereby granted, provided that the above |
Line 104 mansearch(const struct mansearch *search, |
|
Line 104 mansearch(const struct mansearch *search, |
|
} |
} |
|
|
cur = maxres = 0; |
cur = maxres = 0; |
*res = NULL; |
if (res != NULL) |
|
*res = NULL; |
|
|
outkey = KEY_Nd; |
outkey = KEY_Nd; |
if (search->outkey != NULL) |
if (search->outkey != NULL) |
Line 155 mansearch(const struct mansearch *search, |
|
Line 156 mansearch(const struct mansearch *search, |
|
chdir_status = 1; |
chdir_status = 1; |
|
|
if (dbm_open(MANDOC_DB) == -1) { |
if (dbm_open(MANDOC_DB) == -1) { |
warn("%s/%s", paths->paths[i], MANDOC_DB); |
if (errno != ENOENT) |
|
warn("%s/%s", paths->paths[i], MANDOC_DB); |
continue; |
continue; |
} |
} |
|
|
Line 169 mansearch(const struct mansearch *search, |
|
Line 171 mansearch(const struct mansearch *search, |
|
page = dbm_page_get(rp->page); |
page = dbm_page_get(rp->page); |
|
|
if (lstmatch(search->sec, page->sect) == 0 || |
if (lstmatch(search->sec, page->sect) == 0 || |
lstmatch(search->arch, page->arch) == 0) |
lstmatch(search->arch, page->arch) == 0 || |
|
(search->argmode == ARG_NAME && |
|
rp->bits <= (int32_t)(NAME_SYN & NAME_MASK))) |
continue; |
continue; |
|
|
|
if (res == NULL) { |
|
cur = 1; |
|
break; |
|
} |
if (cur + 1 > maxres) { |
if (cur + 1 > maxres) { |
maxres += 1024; |
maxres += 1024; |
*res = mandoc_reallocarray(*res, |
*res = mandoc_reallocarray(*res, |
Line 180 mansearch(const struct mansearch *search, |
|
Line 188 mansearch(const struct mansearch *search, |
|
mpage = *res + cur; |
mpage = *res + cur; |
mandoc_asprintf(&mpage->file, "%s/%s", |
mandoc_asprintf(&mpage->file, "%s/%s", |
paths->paths[i], page->file + 1); |
paths->paths[i], page->file + 1); |
|
if (access(chdir_status ? page->file + 1 : |
|
mpage->file, R_OK) == -1) { |
|
warn("%s", mpage->file); |
|
warnx("outdated mandoc.db contains " |
|
"bogus %s entry, run makewhatis %s", |
|
page->file + 1, paths->paths[i]); |
|
free(mpage->file); |
|
free(rp); |
|
continue; |
|
} |
mpage->names = buildnames(page); |
mpage->names = buildnames(page); |
mpage->output = buildoutput(outkey, page); |
mpage->output = buildoutput(outkey, page); |
mpage->ipath = i; |
mpage->ipath = i; |
Line 203 mansearch(const struct mansearch *search, |
|
Line 221 mansearch(const struct mansearch *search, |
|
if (cur && search->firstmatch) |
if (cur && search->firstmatch) |
break; |
break; |
} |
} |
qsort(*res, cur, sizeof(struct manpage), manpage_compare); |
if (res != NULL) |
|
qsort(*res, cur, sizeof(struct manpage), manpage_compare); |
if (chdir_status && getcwd_status && chdir(buf) == -1) |
if (chdir_status && getcwd_status && chdir(buf) == -1) |
warn("%s", buf); |
warn("%s", buf); |
exprfree(e); |
exprfree(e); |
*sz = cur; |
*sz = cur; |
return 1; |
return res != NULL || cur; |
} |
} |
|
|
/* |
/* |
|
|
manpage_compare(const void *vp1, const void *vp2) |
manpage_compare(const void *vp1, const void *vp2) |
{ |
{ |
const struct manpage *mp1, *mp2; |
const struct manpage *mp1, *mp2; |
|
const char *cp1, *cp2; |
|
size_t sz1, sz2; |
int diff; |
int diff; |
|
|
mp1 = vp1; |
mp1 = vp1; |
mp2 = vp2; |
mp2 = vp2; |
return (diff = mp2->bits - mp1->bits) ? diff : |
if ((diff = mp2->bits - mp1->bits) || |
(diff = mp1->sec - mp2->sec) ? diff : |
(diff = mp1->sec - mp2->sec)) |
strcasecmp(mp1->names, mp2->names); |
return diff; |
|
|
|
/* Fall back to alphabetic ordering of names. */ |
|
sz1 = strcspn(mp1->names, "("); |
|
sz2 = strcspn(mp2->names, "("); |
|
if (sz1 < sz2) |
|
sz1 = sz2; |
|
if ((diff = strncasecmp(mp1->names, mp2->names, sz1))) |
|
return diff; |
|
|
|
/* For identical names and sections, prefer arch-dependent. */ |
|
cp1 = strchr(mp1->names + sz1, '/'); |
|
cp2 = strchr(mp2->names + sz2, '/'); |
|
return cp1 != NULL && cp2 != NULL ? strcasecmp(cp1, cp2) : |
|
cp1 != NULL ? -1 : cp2 != NULL ? 1 : 0; |
} |
} |
|
|
static char * |
static char * |
Line 429 lstlen(const char *cp, size_t sep) |
|
Line 464 lstlen(const char *cp, size_t sep) |
|
{ |
{ |
size_t sz; |
size_t sz; |
|
|
for (sz = 0;; sz++) { |
for (sz = 0; *cp != '\0'; cp++) { |
if (cp[0] == '\0') { |
|
if (cp[1] == '\0') |
/* Skip names appearing only in the SYNOPSIS. */ |
break; |
if (*cp <= (char)(NAME_SYN & NAME_MASK)) { |
sz += sep - 1; |
while (*cp != '\0') |
} else if (cp[0] < ' ') |
cp++; |
sz--; |
continue; |
cp++; |
} |
|
|
|
/* Skip name class markers. */ |
|
if (*cp < ' ') |
|
cp++; |
|
|
|
/* Print a separator before each but the first string. */ |
|
if (sz) |
|
sz += sep; |
|
|
|
/* Copy one string. */ |
|
while (*cp != '\0') { |
|
sz++; |
|
cp++; |
|
} |
} |
} |
return sz; |
return sz; |
} |
} |
Line 448 lstlen(const char *cp, size_t sep) |
|
Line 497 lstlen(const char *cp, size_t sep) |
|
static void |
static void |
lstcat(char *buf, size_t *i, const char *cp, const char *sep) |
lstcat(char *buf, size_t *i, const char *cp, const char *sep) |
{ |
{ |
const char *s; |
const char *s; |
|
size_t i_start; |
|
|
for (;;) { |
for (i_start = *i; *cp != '\0'; cp++) { |
if (cp[0] == '\0') { |
|
if (cp[1] == '\0') |
/* Skip names appearing only in the SYNOPSIS. */ |
break; |
if (*cp <= (char)(NAME_SYN & NAME_MASK)) { |
|
while (*cp != '\0') |
|
cp++; |
|
continue; |
|
} |
|
|
|
/* Skip name class markers. */ |
|
if (*cp < ' ') |
|
cp++; |
|
|
|
/* Print a separator before each but the first string. */ |
|
if (*i > i_start) { |
s = sep; |
s = sep; |
while (*s != '\0') |
while (*s != '\0') |
buf[(*i)++] = *s++; |
buf[(*i)++] = *s++; |
} else if (cp[0] >= ' ') |
} |
buf[(*i)++] = cp[0]; |
|
cp++; |
/* Copy one string. */ |
|
while (*cp != '\0') |
|
buf[(*i)++] = *cp++; |
} |
} |
|
|
} |
} |
|
|
/* |
/* |
Line 516 buildoutput(size_t im, struct dbm_page *page) |
|
Line 580 buildoutput(size_t im, struct dbm_page *page) |
|
output = mandoc_malloc(sz); |
output = mandoc_malloc(sz); |
i = 0; |
i = 0; |
lstcat(output, &i, input, " # "); |
lstcat(output, &i, input, " # "); |
|
output[i++] = '\0'; |
|
assert(i == sz); |
return output; |
return output; |
} |
} |
|
|
Line 670 exprterm(const struct mansearch *search, int argc, cha |
|
Line 736 exprterm(const struct mansearch *search, int argc, cha |
|
return e; |
return e; |
} |
} |
|
|
|
if (strcmp("-i", argv[*argi]) == 0 && *argi + 1 < argc) { |
|
cs = 0; |
|
++*argi; |
|
} else |
|
cs = 1; |
|
|
e = mandoc_calloc(1, sizeof(*e)); |
e = mandoc_calloc(1, sizeof(*e)); |
e->type = EXPR_TERM; |
e->type = EXPR_TERM; |
e->bits = 0; |
e->bits = 0; |
Line 688 exprterm(const struct mansearch *search, int argc, cha |
|
Line 760 exprterm(const struct mansearch *search, int argc, cha |
|
* If needed, request regular expression handling. |
* If needed, request regular expression handling. |
*/ |
*/ |
|
|
cs = 1; |
|
if (search->argmode == ARG_WORD) { |
if (search->argmode == ARG_WORD) { |
e->bits = TYPE_Nm; |
e->bits = TYPE_Nm; |
e->match.type = DBM_REGEX; |
e->match.type = DBM_REGEX; |
Line 703 exprterm(const struct mansearch *search, int argc, cha |
|
Line 774 exprterm(const struct mansearch *search, int argc, cha |
|
cs = 0; |
cs = 0; |
} else if ((val = strpbrk(argv[*argi], "=~")) == NULL) { |
} else if ((val = strpbrk(argv[*argi], "=~")) == NULL) { |
e->bits = TYPE_Nm | TYPE_Nd; |
e->bits = TYPE_Nm | TYPE_Nd; |
e->match.type = DBM_SUB; |
e->match.type = DBM_REGEX; |
e->match.str = argv[*argi]; |
val = argv[*argi]; |
|
cs = 0; |
} else { |
} else { |
if (val == argv[*argi]) |
if (val == argv[*argi]) |
e->bits = TYPE_Nm | TYPE_Nd; |
e->bits = TYPE_Nm | TYPE_Nd; |