Home | History | Annotate | Line # | Download | only in tr
str.c revision 1.3
      1 /*-
      2  * Copyright (c) 1991 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /*static char sccsid[] = "from: @(#)str.c	5.9 (Berkeley) 3/4/93";*/
     36 static char rcsid[] = "$Id: str.c,v 1.3 1993/08/01 18:04:35 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/cdefs.h>
     40 #include <sys/types.h>
     41 
     42 #include <errno.h>
     43 #include <stddef.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 
     48 #include "extern.h"
     49 
     50 static int	backslash __P((STR *));
     51 static int	bracket __P((STR *));
     52 static int	c_class __P((const void *, const void *));
     53 static void	genclass __P((STR *));
     54 static void	genequiv __P((STR *));
     55 static int	genrange __P((STR *));
     56 static void	genseq __P((STR *));
     57 
     58 int
     59 next(s)
     60 	register STR *s;
     61 {
     62 	register int ch;
     63 
     64 	switch (s->state) {
     65 	case EOS:
     66 		return (0);
     67 	case INFINITE:
     68 		return (1);
     69 	case NORMAL:
     70 		switch (ch = *s->str) {
     71 		case '\0':
     72 			s->state = EOS;
     73 			return (0);
     74 		case '\\':
     75 			s->lastch = backslash(s);
     76 			break;
     77 		case '[':
     78 			if (bracket(s))
     79 				return (next(s));
     80 			/* FALLTHROUGH */
     81 		default:
     82 			++s->str;
     83 			s->lastch = ch;
     84 			break;
     85 		}
     86 
     87 		/* We can start a range at any time. */
     88 		if (s->str[0] == '-' && genrange(s))
     89 			return (next(s));
     90 		return (1);
     91 	case RANGE:
     92 		if (s->cnt-- == 0) {
     93 			s->state = NORMAL;
     94 			return (next(s));
     95 		}
     96 		++s->lastch;
     97 		return (1);
     98 	case SEQUENCE:
     99 		if (s->cnt-- == 0) {
    100 			s->state = NORMAL;
    101 			return (next(s));
    102 		}
    103 		return (1);
    104 	case SET:
    105 		if ((s->lastch = s->set[s->cnt++]) == OOBCH) {
    106 			s->state = NORMAL;
    107 			return (next(s));
    108 		}
    109 		return (1);
    110 	}
    111 	/* NOTREACHED */
    112 }
    113 
    114 static int
    115 bracket(s)
    116 	register STR *s;
    117 {
    118 	register char *p;
    119 
    120 	switch (s->str[1]) {
    121 	case ':':				/* "[:class:]" */
    122 		if ((p = strstr(s->str + 2, ":]")) == NULL)
    123 			return (0);
    124 		*p = '\0';
    125 		s->str += 2;
    126 		genclass(s);
    127 		s->str = p + 2;
    128 		return (1);
    129 	case '=':				/* "[=equiv=]" */
    130 		if ((p = strstr(s->str + 2, "=]")) == NULL)
    131 			return (0);
    132 		s->str += 2;
    133 		genequiv(s);
    134 		return (1);
    135 	default:				/* "[\###*n]" or "[#*n]" */
    136 		if ((p = strpbrk(s->str + 2, "*]")) == NULL)
    137 			return (0);
    138 		if (p[0] != '*' || index(p, ']') == NULL)
    139 			return (0);
    140 		s->str += 1;
    141 		genseq(s);
    142 		return (1);
    143 	}
    144 	/* NOTREACHED */
    145 }
    146 
    147 int isalnum __P((int)),
    148     isalpha __P((int)),
    149     isblank __P((int)),
    150     isspace __P((int)),
    151     iscntrl __P((int)),
    152     isdigit __P((int)),
    153     isgraph __P((int)),
    154     islower __P((int)),
    155     isprint __P((int)),
    156     ispunct __P((int)),
    157     isupper __P((int)),
    158     isxdigit __P((int));
    159 
    160 
    161 static int isblank(x) /* until 4.4 */
    162      int x;
    163 {
    164     if ((x == ' ') || (x== '\t')) return 1;
    165     return 0;
    166 }
    167 
    168 
    169 typedef struct {
    170 	char *name;
    171 	int (*func) __P((int));
    172 	int *set;
    173 } CLASS;
    174 
    175 static CLASS classes[] = {
    176 	{ "alnum",  isalnum,  },
    177 	{ "alpha",  isalpha,  },
    178 	{ "blank",  isblank,  },
    179 	{ "cntrl",  iscntrl,  },
    180 	{ "digit",  isdigit,  },
    181 	{ "graph",  isgraph,  },
    182 	{ "lower",  islower,  },
    183 	{ "print",  isupper,  },
    184 	{ "punct",  ispunct,  },
    185 	{ "space",  isspace,  },
    186 	{ "upper",  isupper,  },
    187 	{ "xdigit", isxdigit, },
    188 };
    189 
    190 static void
    191 genclass(s)
    192 	STR *s;
    193 {
    194 	register int cnt, (*func) __P((int));
    195 	CLASS *cp, tmp;
    196 	int *p;
    197 
    198 	tmp.name = s->str;
    199 	if ((cp = (CLASS *)bsearch(&tmp, classes, sizeof(classes) /
    200 	    sizeof(CLASS), sizeof(CLASS), c_class)) == NULL)
    201 		err("unknown class %s", s->str);
    202 
    203 	if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL)
    204 		err("%s", strerror(errno));
    205 	bzero(p, (NCHARS + 1) * sizeof(int));
    206 	for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt)
    207 		if ((func)(cnt))
    208 			*p++ = cnt;
    209 	*p = OOBCH;
    210 
    211 	s->cnt = 0;
    212 	s->state = SET;
    213 	s->set = cp->set;
    214 }
    215 
    216 static int
    217 c_class(a, b)
    218 	const void *a, *b;
    219 {
    220 	return (strcmp(((CLASS *)a)->name, ((CLASS *)b)->name));
    221 }
    222 
    223 /*
    224  * English doesn't have any equivalence classes, so for now
    225  * we just syntax check and grab the character.
    226  */
    227 static void
    228 genequiv(s)
    229 	STR *s;
    230 {
    231 	if (*s->str == '\\') {
    232 		s->equiv[0] = backslash(s);
    233 		if (*s->str != '=')
    234 			err("misplaced equivalence equals sign");
    235 	} else {
    236 		s->equiv[0] = s->str[0];
    237 		if (s->str[1] != '=')
    238 			err("misplaced equivalence equals sign");
    239 	}
    240 	s->str += 2;
    241 	s->cnt = 0;
    242 	s->state = SET;
    243 	s->set = s->equiv;
    244 }
    245 
    246 static int
    247 genrange(s)
    248 	STR *s;
    249 {
    250 	int stopval;
    251 	char *savestart;
    252 
    253 	savestart = s->str;
    254 	stopval = *++s->str == '\\' ? backslash(s) : *s->str;
    255 	if (stopval < s->lastch) {
    256 		s->str = savestart;
    257 		return (0);
    258 	}
    259 	s->cnt = stopval - s->lastch + 1;
    260 	s->state = RANGE;
    261 	++s->str;
    262 	--s->lastch;
    263 	return (1);
    264 }
    265 
    266 static void
    267 genseq(s)
    268 	STR *s;
    269 {
    270 	char *ep;
    271 
    272 	if (s->which == STRING1)
    273 		err("sequences only valid in string2");
    274 
    275 	if (*s->str == '\\')
    276 		s->lastch = backslash(s);
    277 	else
    278 		s->lastch = *s->str++;
    279 	if (*s->str != '*')
    280 		err("misplaced sequence asterisk");
    281 
    282 	switch (*++s->str) {
    283 	case '\\':
    284 		s->cnt = backslash(s);
    285 		break;
    286 	case ']':
    287 		s->cnt = 0;
    288 		++s->str;
    289 		break;
    290 	default:
    291 		if (isdigit(*s->str)) {
    292 			s->cnt = strtol(s->str, &ep, 0);
    293 			if (*ep == ']') {
    294 				s->str = ep + 1;
    295 				break;
    296 			}
    297 		}
    298 		err("illegal sequence count");
    299 		/* NOTREACHED */
    300 	}
    301 
    302 	s->state = s->cnt ? SEQUENCE : INFINITE;
    303 }
    304 
    305 /* Use the #defines isXXX() here, DON'T use them above. */
    306 #include <ctype.h>
    307 
    308 /*
    309  * Translate \??? into a character.  Up to 3 octal digits, if no digits either
    310  * an escape code or a literal character.
    311  */
    312 static int
    313 backslash(s)
    314 	register STR *s;
    315 {
    316 	register int ch, cnt, val;
    317 
    318 	for (cnt = val = 0;;) {
    319 		ch = *++s->str;
    320 		if (!isascii(ch) || !isdigit(ch))
    321 			break;
    322 		val = val * 8 + ch - '0';
    323 		if (++cnt == 3) {
    324 			++s->str;
    325 			break;
    326 		}
    327 	}
    328 	if (cnt)
    329 		return (val);
    330 	if (ch != '\0')
    331 		++s->str;
    332 	switch (ch) {
    333 		case 'a':			/* escape characters */
    334 			return ('\7');
    335 		case 'b':
    336 			return ('\b');
    337 		case 'f':
    338 			return ('\f');
    339 		case 'n':
    340 			return ('\n');
    341 		case 'r':
    342 			return ('\r');
    343 		case 't':
    344 			return ('\t');
    345 		case 'v':
    346 			return ('\13');
    347 		case '\0':			/*  \" -> \ */
    348 			s->state = EOS;
    349 			return ('\\');
    350 		default:			/* \x" -> x */
    351 			return (ch);
    352 	}
    353 }
    354