diff --git a/include/match.h b/include/match.h index dc9fa45f..4985926b 100644 --- a/include/match.h +++ b/include/match.h @@ -34,6 +34,8 @@ #ifndef _MATCH_H_ #define _MATCH_H_ +#include + /* returned by stmtch() */ #define M_IGNORE -3 #define M_NOTUNIQUE -2 @@ -45,4 +47,7 @@ #define ME_PARTIAL 1 #define ME_EXACT 2 +extern int stmtch(char *, void *, ptrdiff_t, size_t); +extern int mineq(char *, char *); + #endif /* _MATCH_H_ */ diff --git a/include/prototypes.h b/include/prototypes.h index f9eabc67..f84396f9 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -174,8 +174,7 @@ extern void snxtsct_all(struct nstr_sect *); extern void snxtsct_rewind(struct nstr_sect *); extern void snxtsct_dist(register struct nstr_sect *, coord, coord, int); /* stmtch.c */ -extern int stmtch(register s_char *, s_char *, int, int); -extern int mineq(register s_char *, register s_char *); +/* in match.h */ /* type.c */ extern int typematch(s_char *, int); /* vlist.c */ diff --git a/src/lib/common/stmtch.c b/src/lib/common/stmtch.c index bcb4fefe..35f0753e 100644 --- a/src/lib/common/stmtch.c +++ b/src/lib/common/stmtch.c @@ -40,30 +40,36 @@ #include "common.h" /* - * find a matching string from a member string pointer - * in a structure "str". Pointer is incremented by - * the (passed) size of the structure. + * Find NEEDLE in array HAYSTACK[], return its index. + * Return M_NOTFOUND if there are no matches, M_NOTUNIQUE if there are + * several. + * Each array element has a pointer to its name stored at offset OFFS. + * Search stops when this name is a null pointer or empty. NEEDLE is + * compared to element names with mineq(). + * ELT_SIZE gives the size of an array element. */ int -stmtch(register s_char *obj, s_char *base, int off, int size) +stmtch(char *needle, void *haystack, ptrdiff_t offs, size_t elt_size) { - register s_char *str; - register int stat2; - register int i; - register int n; +#define ELT_NAME(i) (*(char **)((char *)haystack + (i)*elt_size + offs)) + int i, res; - stat2 = M_NOTFOUND; - str = base + off; - for (i = 0; *(s_char **)str; i++, str += size) { - if ((n = mineq(obj, *(s_char **)str)) == ME_MISMATCH) - continue; - if (n == ME_EXACT) + res = M_NOTFOUND; + for (i = 0; ELT_NAME(i) && ELT_NAME(i)[0] != 0; ++i) { + switch (mineq(needle, ELT_NAME(i))) { + case ME_MISMATCH: + break; + case ME_PARTIAL: + if (res != M_NOTFOUND) + return M_NOTUNIQUE; + res = i; + break; + case ME_EXACT: return i; - if (stat2 != M_NOTFOUND) - return M_NOTUNIQUE; - stat2 = i; + } } - return stat2; + return res; +#undef ELT_NAME } /* @@ -75,7 +81,7 @@ stmtch(register s_char *obj, s_char *base, int off, int size) * terminating 0. */ int -mineq(register s_char *a, register s_char *b) +mineq(char *a, char *b) { int i;