151396Sbostic /*- 251396Sbostic * Copyright (c) 1991 The Regents of the University of California. 351396Sbostic * All rights reserved. 451396Sbostic * 551396Sbostic * %sccs.include.redist.c% 651396Sbostic */ 751396Sbostic 851396Sbostic #ifndef lint 9*51407Sbostic static char sccsid[] = "@(#)str.c 5.3 (Berkeley) 10/27/91"; 1051396Sbostic #endif /* not lint */ 1151396Sbostic 1251396Sbostic #include <sys/cdefs.h> 1351396Sbostic #include <sys/types.h> 1451396Sbostic #include <errno.h> 1551396Sbostic #include <stdio.h> 1651396Sbostic #include <stddef.h> 1751396Sbostic #include <stdlib.h> 1851396Sbostic #include <string.h> 1951396Sbostic #include "extern.h" 2051396Sbostic 2151396Sbostic static int backslash __P((STR *)); 2251396Sbostic static int bracket __P((STR *)); 2351396Sbostic static int c_class __P((const void *, const void *)); 2451396Sbostic static void genclass __P((STR *)); 2551396Sbostic static void genequiv __P((STR *)); 2651396Sbostic static int genrange __P((STR *)); 2751396Sbostic static void genseq __P((STR *)); 2851396Sbostic 2951396Sbostic int 3051396Sbostic next(s) 3151396Sbostic register STR *s; 3251396Sbostic { 3351396Sbostic register int ch; 3451396Sbostic 3551396Sbostic switch (s->state) { 3651396Sbostic case EOS: 3751396Sbostic return (0); 3851396Sbostic case INFINITE: 3951396Sbostic return (1); 4051396Sbostic case NORMAL: 4151399Sbostic switch (ch = *s->str) { 4251396Sbostic case '\0': 4351396Sbostic s->state = EOS; 4451396Sbostic return (0); 4551396Sbostic case '\\': 4651396Sbostic s->lastch = backslash(s); 4751396Sbostic break; 4851396Sbostic case '[': 4951396Sbostic if (bracket(s)) 5051396Sbostic return (next(s)); 5151396Sbostic /* FALLTHROUGH */ 5251396Sbostic default: 5351399Sbostic ++s->str; 5451396Sbostic s->lastch = ch; 5551396Sbostic break; 5651396Sbostic } 5751396Sbostic 5851396Sbostic /* We can start a range at any time. */ 5951396Sbostic if (s->str[0] == '-' && genrange(s)) 6051396Sbostic return (next(s)); 6151396Sbostic return (1); 6251396Sbostic case RANGE: 6351396Sbostic if (s->cnt-- == 0) { 6451396Sbostic s->state = NORMAL; 6551396Sbostic return (next(s)); 6651396Sbostic } 6751396Sbostic ++s->lastch; 6851396Sbostic return (1); 6951396Sbostic case SEQUENCE: 7051396Sbostic if (s->cnt-- == 0) { 7151396Sbostic s->state = NORMAL; 7251396Sbostic return (next(s)); 7351396Sbostic } 7451396Sbostic return (1); 7551396Sbostic case SET: 7651396Sbostic if ((s->lastch = s->set[s->cnt++]) == OOBCH) { 7751396Sbostic s->state = NORMAL; 7851396Sbostic return (next(s)); 7951396Sbostic } 8051396Sbostic return (1); 8151396Sbostic } 8251396Sbostic /* NOTREACHED */ 8351396Sbostic } 8451396Sbostic 8551396Sbostic static int 8651396Sbostic bracket(s) 8751396Sbostic register STR *s; 8851396Sbostic { 8951396Sbostic register char *p; 9051396Sbostic 9151399Sbostic switch (*++s->str) { 9251396Sbostic case ':': /* "[:class:]" */ 9351396Sbostic if ((p = strpbrk(s->str + 1, ":]")) == NULL) 9451396Sbostic return (0); 9551396Sbostic if (p[0] != ':' || p[1] != ']') 9651396Sbostic return (0); 9751396Sbostic *p = '\0'; 9851396Sbostic ++s->str; 9951396Sbostic genclass(s); 10051396Sbostic s->str = p + 2; 10151396Sbostic return (1); 10251396Sbostic case '=': /* "[=equiv=]" */ 10351396Sbostic if ((p = strpbrk(s->str + 1, "=]")) == NULL) 10451396Sbostic return (0); 10551396Sbostic if (p[0] != '=' || p[1] != ']') 10651396Sbostic return (0); 10751396Sbostic genequiv(s); 10851396Sbostic return (1); 10951396Sbostic default: /* "[\###*]" or "[#*]" */ 11051396Sbostic if ((p = strpbrk(s->str + 1, "*]")) == NULL) 11151396Sbostic return (0); 11251396Sbostic if (p[0] != '*' || index(p, ']') == NULL) 11351396Sbostic return (0); 11451396Sbostic genseq(s); 11551396Sbostic return (1); 11651396Sbostic } 11751396Sbostic /* NOTREACHED */ 11851396Sbostic } 11951396Sbostic 12051396Sbostic int isalnum __P((int)), 12151396Sbostic isalpha __P((int)), 12251396Sbostic isblank __P((int)), 12351396Sbostic isspace __P((int)), 12451396Sbostic iscntrl __P((int)), 12551396Sbostic isdigit __P((int)), 12651396Sbostic isgraph __P((int)), 12751396Sbostic islower __P((int)), 12851396Sbostic isprint __P((int)), 12951396Sbostic ispunct __P((int)), 13051396Sbostic isupper __P((int)), 13151396Sbostic isxdigit __P((int)); 13251396Sbostic 13351396Sbostic typedef struct { 13451396Sbostic char *name; 13551396Sbostic int (*func) __P((int)); 13651396Sbostic int *set; 13751396Sbostic } CLASS; 13851396Sbostic 13951396Sbostic static CLASS classes[] = { 140*51407Sbostic { "alnum", isalnum, }, 141*51407Sbostic { "alpha", isalpha, }, 142*51407Sbostic { "blank", isblank, }, 143*51407Sbostic { "cntrl", iscntrl, }, 144*51407Sbostic { "digit", isdigit, }, 145*51407Sbostic { "graph", isgraph, }, 146*51407Sbostic { "lower", islower, }, 147*51407Sbostic { "print", isupper, }, 148*51407Sbostic { "punct", ispunct, }, 149*51407Sbostic { "space", isspace, }, 150*51407Sbostic { "upper", isupper, }, 151*51407Sbostic { "xdigit", isxdigit, }, 15251396Sbostic }; 15351396Sbostic 15451396Sbostic static void 15551396Sbostic genclass(s) 15651396Sbostic STR *s; 15751396Sbostic { 15851396Sbostic register int cnt, (*func) __P((int)); 15951396Sbostic CLASS *cp, tmp; 16051396Sbostic int *p; 16151396Sbostic 16251396Sbostic tmp.name = s->str; 16351396Sbostic if ((cp = (CLASS *)bsearch(&tmp, classes, sizeof(classes) / 16451396Sbostic sizeof(CLASS), sizeof(CLASS), c_class)) == NULL) 16551396Sbostic err("unknown class %s", s->str); 16651396Sbostic 16751396Sbostic if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL) 16851396Sbostic err("%s", strerror(errno)); 169*51407Sbostic bzero(p, (NCHARS + 1) * sizeof(int)); 17051396Sbostic for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt) 17151396Sbostic if ((func)(cnt)) 17251396Sbostic *p++ = cnt; 17351396Sbostic *p = OOBCH; 17451396Sbostic 17551396Sbostic s->cnt = 0; 176*51407Sbostic s->state = SET; 17751396Sbostic s->set = cp->set; 17851396Sbostic } 17951396Sbostic 18051396Sbostic static int 18151396Sbostic c_class(a, b) 18251396Sbostic const void *a, *b; 18351396Sbostic { 18451396Sbostic return (strcmp(((CLASS *)a)->name, ((CLASS *)b)->name)); 18551396Sbostic } 18651396Sbostic 18751396Sbostic /* 18851396Sbostic * English doesn't have any equivalence classes, so for now 18951396Sbostic * we just syntax check and grab the character. 19051396Sbostic */ 19151396Sbostic static void 19251396Sbostic genequiv(s) 19351396Sbostic STR *s; 19451396Sbostic { 19551396Sbostic if (*++s->str == '\\') { 196*51407Sbostic s->equiv[0] = backslash(s); 19751396Sbostic if (*s->str != '=') 19851396Sbostic err("misplaced equivalence equals sign"); 19951396Sbostic } else { 200*51407Sbostic s->equiv[0] = s->str[0]; 20151396Sbostic if (s->str[1] != '=') 20251396Sbostic err("misplaced equivalence equals sign"); 20351396Sbostic } 20451396Sbostic s->str += 2; 20551396Sbostic s->cnt = 0; 20651396Sbostic s->state = SET; 207*51407Sbostic s->set = s->equiv; 20851396Sbostic } 20951396Sbostic 21051396Sbostic static int 21151396Sbostic genrange(s) 21251396Sbostic STR *s; 21351396Sbostic { 21451396Sbostic int stopval; 21551396Sbostic char *savestart; 21651396Sbostic 21751396Sbostic savestart = s->str; 21851396Sbostic stopval = *++s->str == '\\' ? backslash(s) : *s->str; 21951396Sbostic if (stopval < s->lastch) { 22051396Sbostic s->str = savestart; 22151396Sbostic return (0); 22251396Sbostic } 22351396Sbostic s->cnt = stopval - s->lastch + 1; 22451396Sbostic s->state = RANGE; 22551396Sbostic --s->lastch; 22651396Sbostic return (1); 22751396Sbostic } 22851396Sbostic 22951396Sbostic static void 23051396Sbostic genseq(s) 23151396Sbostic STR *s; 23251396Sbostic { 23351396Sbostic char *ep; 23451396Sbostic 235*51407Sbostic if (s->which == STRING1) 236*51407Sbostic err("sequences only valid in string2"); 23751396Sbostic 23851396Sbostic if (*s->str == '\\') 23951396Sbostic s->lastch = backslash(s); 24051396Sbostic else 24151396Sbostic s->lastch = *s->str++; 24251396Sbostic if (*s->str != '*') 24351396Sbostic err("misplaced sequence asterisk"); 24451396Sbostic 24551396Sbostic switch (*++s->str) { 24651396Sbostic case '\\': 24751396Sbostic s->cnt = backslash(s); 24851396Sbostic break; 24951396Sbostic case ']': 25051396Sbostic s->cnt = 0; 25151396Sbostic ++s->str; 25251396Sbostic break; 25351396Sbostic default: 25451396Sbostic if (isdigit(*s->str)) { 25551396Sbostic s->cnt = strtol(s->str, &ep, 0); 25651396Sbostic if (*ep == ']') { 25751396Sbostic s->str = ep + 1; 25851396Sbostic break; 25951396Sbostic } 26051396Sbostic } 26151396Sbostic err("illegal sequence count"); 26251396Sbostic /* NOTREACHED */ 26351396Sbostic } 26451396Sbostic 26551396Sbostic s->state = s->cnt ? SEQUENCE : INFINITE; 26651396Sbostic } 26751396Sbostic 268*51407Sbostic /* Use the #defines isXXX() here, DON'T use them above. */ 26951396Sbostic #include <ctype.h> 27051396Sbostic 27151396Sbostic /* 27251396Sbostic * Translate \??? into a character. Up to 3 octal digits, if no digits either 27351396Sbostic * an escape code or a literal character. 27451396Sbostic */ 27551396Sbostic static int 27651396Sbostic backslash(s) 27751396Sbostic register STR *s; 27851396Sbostic { 27951396Sbostic register int ch, cnt, val; 28051396Sbostic 28151396Sbostic for (cnt = val = 0;;) { 28251396Sbostic ch = *++s->str; 28351396Sbostic if (!isascii(ch) || !isdigit(ch)) 28451396Sbostic break; 28551396Sbostic val = val * 8 + ch - '0'; 28651396Sbostic if (++cnt == 3) 28751396Sbostic break; 28851396Sbostic } 28951396Sbostic if (cnt) 29051396Sbostic return (val); 29151396Sbostic ++s->str; 29251396Sbostic switch (ch) { 29351396Sbostic case 'a': /* escape characters */ 29451396Sbostic return ('\7'); 29551396Sbostic case 'b': 29651396Sbostic return ('\b'); 29751396Sbostic case 'f': 29851396Sbostic return ('\f'); 29951396Sbostic case 'n': 30051396Sbostic return ('\n'); 30151396Sbostic case 'r': 30251396Sbostic return ('\r'); 30351396Sbostic case 't': 30451396Sbostic return ('\t'); 30551396Sbostic case 'v': 30651396Sbostic return ('\13'); 30751396Sbostic case '\0': /* \" -> \ */ 30851396Sbostic s->state = EOS; 30951396Sbostic return ('\\'); 31051396Sbostic default: /* \x" -> x */ 31151396Sbostic return (ch); 31251396Sbostic } 31351396Sbostic } 314