1 static char Sccsid[] = "@(#)bal.c 1.2 02/15/87"; 2 3 /* 4 Function to find the position, in str, of the first of the char- 5 acters in end occurring outside a balanced string. A balanced string 6 contains matched occurrences of any character in open and the corres- 7 ponding character in clos. Balanced strings may be nested. The null 8 at the end of str is considered to belong to end. Unmatched members 9 of open or clos result in an error return. 10 */ 11 12 #define ifany(x) for (p=x; *p; p++) if (c == *p) 13 #define matching_clos clos[p-open] 14 #define error -1 15 #define position s-str-1 16 17 balbrk(str,open,clos,end) 18 char *str,*open,*clos,*end; 19 { 20 register char *p, *s, c; 21 char opp[2]; 22 opp[1] = '\0'; 23 for (s = str; c = *s++; ) { 24 ifany(end) return position; 25 ifany(clos) return error; 26 ifany(open) { 27 opp[0] = matching_clos; 28 s += balbrk(s,open,clos,opp); 29 if (*s++ != matching_clos) return error; 30 break; 31 } 32 } 33 return position; 34 } 35