
/*
 * smatch.c -- match a switch
 *
 * $Id$
 */

#include <h/mh.h>


int
smatch(char *string, struct swit *swp)
{
    register char *sp, *tcp;
    struct swit *tp;
    int firstone, len;

    firstone = UNKWNSW;

    if (!string)
	return firstone;

    for (len = strlen(string), tp = swp; tcp = tp->sw; tp++) {
	if (len < abs(tp->minchars))
	    continue;		/* no match */
	for (sp = string; *sp == *tcp++;) {
	    if (*sp++ == 0)
		return (tp - swp);/* exact match */
	}
	if (*sp) {
	    if (*sp != ' ')
		continue;	/* no match */
	    if (*--tcp == 0)
		return (tp - swp);/* exact match */
	}
	if (firstone == UNKWNSW)
	    firstone = tp - swp;
	else
	    firstone = AMBIGSW;
    }

    return (firstone);
}
