]> diplodocus.org Git - nmh/blob - docs/historical/SRI-NOSC/subs.dir/smatch.c
expect this commit to be force-replaced periodically
[nmh] / docs / historical / SRI-NOSC / subs.dir / smatch.c
1 #include "mh.h"
2
3 /* switch match, or any unambiguous abbreviation */
4 /* exact match always wins, even if shares same root */
5 /* returns subscript in zero-terminated tbl[] of strings */
6 /* returns -1 if no match, -2 if ambiguous */
7
8 smatch(string, swp)
9 char *string;
10 struct swit *swp;
11 {
12 register char *sp, *tcp;
13 struct swit *tp;
14 int firstone, strlen;
15
16 firstone = -1;
17
18 for (strlen = length(string), tp = swp; tcp = tp->sw; tp++) {
19 if(strlen < abs(tp->minchars)) continue; /* no match */
20 for (sp = string; *sp == *tcp++; ) {
21 if (*sp++ == 0) return(tp-swp); /* exact match */
22 }
23 if (*sp != 0) {
24 if (*sp != ' ') continue; /* no match */
25 if (*--tcp == 0) return(tp-swp); /* exact match */
26 }
27 if (firstone == -1) firstone = tp-swp; /* possible match */
28 else firstone = -2; /* ambiguous */
29 }
30
31 return (firstone);
32 }
33
34 abs(i)
35 {
36 return(i < 0 ? -i : i);
37 }