]> diplodocus.org Git - nmh/blob - sbr/smatch.c
mhbuildsbr.c: Flip logic, moving goto to then-block; no need for else.
[nmh] / sbr / smatch.c
1 /* smatch.c -- match a switch (option)
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9
10
11 int
12 smatch(const char *string, const struct swit *swp)
13 {
14 const char *sp, *tcp;
15 int firstone, len;
16 const struct swit *tp;
17
18 firstone = UNKWNSW;
19
20 if (!string)
21 return firstone;
22 len = strlen(string);
23
24 for (tp = swp; tp->sw; tp++) {
25 tcp = tp->sw;
26 if (len < abs(tp->minchars))
27 continue; /* no match */
28 for (sp = string; *sp == *tcp++;) {
29 if (*sp++ == '\0')
30 return tp->swret; /* exact match */
31 }
32 if (*sp) {
33 if (*sp != ' ')
34 continue; /* no match */
35 if (*--tcp == '\0')
36 return tp->swret; /* exact match */
37 }
38 if (firstone == UNKWNSW)
39 firstone = tp->swret;
40 else
41 firstone = AMBIGSW;
42 }
43
44 return (firstone);
45 }