]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/subs/smatch.c
Replaced use of snprintf() with memcpy()/memmove().
[nmh] / docs / historical / mh-jun-1982 / subs / smatch.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 #include "../mh.h"
9
10 /* switch match, or any unambiguous abbreviation */
11 /* exact match always wins, even if shares same root */
12 /* returns subscript in zero-terminated tbl[] of strings */
13 /* returns -1 if no match, -2 if ambiguous */
14
15 #define abs(i) (i < 0 ? -i : i)
16
17 smatch(string, swp)
18 char *string;
19 struct swit *swp;
20 {
21 register char *sp, *tcp;
22 struct swit *tp;
23 int firstone, stringlen;
24
25 firstone = -1;
26
27 for (stringlen = strlen(string), tp = swp; tcp = tp->sw; tp++) {
28 if(stringlen < abs(tp->minchars)) continue; /* no match */
29 for (sp = string; *sp == *tcp++; ) {
30 if (*sp++ == 0) return(tp-swp); /* exact match */
31 }
32 if (*sp != 0) {
33 if (*sp != ' ') continue; /* no match */
34 if (*--tcp == 0) return(tp-swp); /* exact match */
35 }
36 if (firstone == -1) firstone = tp-swp; /* possible match */
37 else firstone = -2; /* ambiguous */
38 }
39
40 return (first