=================================================================== RCS file: /cvs/mandoc/dbm.c,v retrieving revision 1.4 retrieving revision 1.7 diff -u -p -r1.4 -r1.7 --- mandoc/dbm.c 2016/08/30 22:01:07 1.4 +++ mandoc/dbm.c 2019/07/01 22:56:24 1.7 @@ -1,4 +1,4 @@ -/* $Id: dbm.c,v 1.4 2016/08/30 22:01:07 schwarze Exp $ */ +/* $Id: dbm.c,v 1.7 2019/07/01 22:56:24 schwarze Exp $ */ /* * Copyright (c) 2016 Ingo Schwarze * @@ -151,17 +151,17 @@ dbm_page_get(int32_t ip) assert(ip < npages); res.name = dbm_get(pages[ip].name); if (res.name == NULL) - res.name = "(NULL)"; + res.name = "(NULL)\0"; res.sect = dbm_get(pages[ip].sect); if (res.sect == NULL) - res.sect = "(NULL)"; + res.sect = "(NULL)\0"; res.arch = pages[ip].arch ? dbm_get(pages[ip].arch) : NULL; res.desc = dbm_get(pages[ip].desc); if (res.desc == NULL) res.desc = "(NULL)"; res.file = dbm_get(pages[ip].file); if (res.file == NULL) - res.file = " (NULL)"; + res.file = " (NULL)\0"; res.addr = dbm_addr(pages + ip); return &res; } @@ -233,7 +233,7 @@ static struct dbm_res page_bytitle(enum iter arg_iter, const struct dbm_match *arg_match) { static const struct dbm_match *match; - static const char *cp; + static const char *cp; static int32_t ip; struct dbm_res res = {-1, 0}; @@ -315,7 +315,7 @@ page_byarch(const struct dbm_match *arg_match) static const struct dbm_match *match; struct dbm_res res = {-1, 0}; static int32_t ip; - const char *cp; + const char *cp; /* Initialize for a new iteration. */ @@ -346,41 +346,45 @@ page_byarch(const struct dbm_match *arg_match) } static struct dbm_res -page_bymacro(int32_t im, const struct dbm_match *match) +page_bymacro(int32_t arg_im, const struct dbm_match *arg_match) { - static const int32_t *pp; - struct dbm_res res = {-1, 0}; - const char *cp; - int32_t iv; + static const struct dbm_match *match; + static const int32_t *pp; + static const char *cp; + static int32_t im, iv; + struct dbm_res res = {-1, 0}; assert(im >= 0); assert(im < MACRO_MAX); /* Initialize for a new iteration. */ - if (match != NULL) { + if (arg_match != NULL) { iteration = ITER_MACRO; + match = arg_match; + im = arg_im; cp = nvals[im] ? dbm_get(macros[im]->value) : NULL; - for (iv = 0; iv < nvals[im]; iv++) { - if (dbm_match(match, cp)) - break; - cp = strchr(cp, '\0') + 1; - } - pp = iv == nvals[im] ? NULL : dbm_get(macros[im][iv].pages); + pp = NULL; + iv = -1; return res; } if (iteration != ITER_MACRO) return res; - /* No more matches. */ + /* Find the next matching macro value. */ - if (pp == NULL || *pp == 0) { - iteration = ITER_NONE; - pp = NULL; - return res; + while (pp == NULL || *pp == 0) { + if (++iv == nvals[im]) { + iteration = ITER_NONE; + return res; + } + if (iv) + cp = strchr(cp, '\0') + 1; + if (dbm_match(match, cp)) + pp = dbm_get(macros[im][iv].pages); } - /* Found a match. */ + /* Found a matching page. */ res.page = (struct page *)dbm_get(*pp++) - pages; return res;