]> diplodocus.org Git - nmh/blob - sbr/smatch.c
sendsbr.c: Move interface to own file.
[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 #include "smatch.h"
10
11
12 int
13 smatch(const char *string, const struct swit *swp)
14 {
15 const char *sp, *tcp;
16 int firstone, len;
17 const struct swit *tp;
18
19 firstone = UNKWNSW;
20
21 if (!string)
22 return firstone;
23 len = strlen(string);
24
25 for (tp = swp; tp->sw; tp++) {
26 tcp = tp->sw;
27 if (len < abs(tp->minchars))
28 continue; /* no match */
29 for (sp = string; *sp == *tcp++;) {
30 if (*sp++ == '\0')
31 return tp->swret; /* exact match */
32 }
33 if (*sp) {
34 if (*sp != ' ')
35 continue; /* no match */
36 if (*--tcp == '\0')
37 return tp->swret; /* exact match */
38 }
39 if (firstone == UNKWNSW)
40 firstone = tp->swret;
41 else
42 firstone = AMBIGSW;
43 }
44
45 return firstone;
46 }