str.c revision 28368
10SN/A/*- 24589SN/A * Copyright (c) 1991, 1993 30SN/A * The Regents of the University of California. All rights reserved. 40SN/A * 50SN/A * Redistribution and use in source and binary forms, with or without 60SN/A * modification, are permitted provided that the following conditions 72362SN/A * are met: 80SN/A * 1. Redistributions of source code must retain the above copyright 92362SN/A * notice, this list of conditions and the following disclaimer. 100SN/A * 2. Redistributions in binary form must reproduce the above copyright 110SN/A * notice, this list of conditions and the following disclaimer in the 120SN/A * documentation and/or other materials provided with the distribution. 130SN/A * 3. All advertising materials mentioning features or use of this software 140SN/A * must display the following acknowledgement: 150SN/A * This product includes software developed by the University of 160SN/A * California, Berkeley and its contributors. 170SN/A * 4. Neither the name of the University nor the names of its contributors 180SN/A * may be used to endorse or promote products derived from this software 190SN/A * without specific prior written permission. 200SN/A * 212362SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 222362SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 232362SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 240SN/A * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 250SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 260SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 270SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 280SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 290SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 300SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 310SN/A * SUCH DAMAGE. 320SN/A */ 330SN/A 340SN/A#ifndef lint 350SN/A#if 0 360SN/Astatic char sccsid[] = "@(#)str.c 8.2 (Berkeley) 4/28/95"; 370SN/A#endif 380SN/Astatic const char rcsid[] = 390SN/A "$Id$"; 400SN/A#endif /* not lint */ 410SN/A 420SN/A#include <sys/cdefs.h> 430SN/A#include <sys/types.h> 440SN/A 454589SN/A#include <ctype.h> 460SN/A#include <err.h> 470SN/A#include <stddef.h> 480SN/A#include <stdio.h> 490SN/A#include <stdlib.h> 500SN/A#include <string.h> 510SN/A 520SN/A#include "extern.h" 530SN/A 540SN/Astatic int backslash __P((STR *)); 550SN/Astatic int bracket __P((STR *)); 5610089SN/Astatic int c_class __P((const void *, const void *)); 570SN/Astatic void genclass __P((STR *)); 580SN/Astatic void genequiv __P((STR *)); 590SN/Astatic int genrange __P((STR *)); 600SN/Astatic void genseq __P((STR *)); 610SN/A 620SN/Aint 630SN/Anext(s) 640SN/A register STR *s; 650SN/A{ 660SN/A register int ch; 670SN/A 680SN/A switch (s->state) { 690SN/A case EOS: 700SN/A return (0); 710SN/A case INFINITE: 720SN/A return (1); 730SN/A case NORMAL: 740SN/A switch (ch = (u_char)*s->str) { 750SN/A case '\0': 760SN/A s->state = EOS; 770SN/A return (0); 780SN/A case '\\': 790SN/A s->lastch = backslash(s); 800SN/A break; 810SN/A case '[': 820SN/A if (bracket(s)) 8310700Sredestad return (next(s)); 840SN/A /* FALLTHROUGH */ 850SN/A default: 8614088Svtewari ++s->str; 870SN/A s->lastch = ch; 880SN/A break; 890SN/A } 900SN/A 910SN/A /* We can start a range at any time. */ 920SN/A if (s->str[0] == '-' && genrange(s)) 930SN/A return (next(s)); 940SN/A return (1); 950SN/A case RANGE: 960SN/A if (s->cnt-- == 0) { 970SN/A s->state = NORMAL; 980SN/A return (next(s)); 990SN/A } 1000SN/A ++s->lastch; 1010SN/A return (1); 1020SN/A case SEQUENCE: 1030SN/A if (s->cnt-- == 0) { 1040SN/A s->state = NORMAL; 1050SN/A return (next(s)); 1060SN/A } 1070SN/A return (1); 1080SN/A case SET: 1090SN/A if ((s->lastch = s->set[s->cnt++]) == OOBCH) { 1100SN/A s->state = NORMAL; 1110SN/A return (next(s)); 1120SN/A } 1130SN/A return (1); 1140SN/A } 1150SN/A /* NOTREACHED */ 1160SN/A} 1170SN/A 1180SN/Astatic int 1190SN/Abracket(s) 1200SN/A register STR *s; 1210SN/A{ 1220SN/A register char *p; 1230SN/A 1244589SN/A switch (s->str[1]) { 1250SN/A case ':': /* "[:class:]" */ 1260SN/A if ((p = strstr(s->str + 2, ":]")) == NULL) 1270SN/A return (0); 1284589SN/A *p = '\0'; 1290SN/A s->str += 2; 1300SN/A genclass(s); 1310SN/A s->str = p + 2; 1320SN/A return (1); 1330SN/A case '=': /* "[=equiv=]" */ 1340SN/A if ((p = strstr(s->str + 2, "=]")) == NULL) 1350SN/A return (0); 1360SN/A s->str += 2; 1370SN/A genequiv(s); 1380SN/A return (1); 1390SN/A default: /* "[\###*n]" or "[#*n]" */ 140 if ((p = strpbrk(s->str + 2, "*]")) == NULL) 141 return (0); 142 if (p[0] != '*' || index(p, ']') == NULL) 143 return (0); 144 s->str += 1; 145 genseq(s); 146 return (1); 147 } 148 /* NOTREACHED */ 149} 150 151typedef struct { 152 char *name; 153 int (*func) __P((int)); 154 int *set; 155} CLASS; 156 157static CLASS classes[] = { 158#undef isalnum 159 { "alnum", isalnum, }, 160#undef isalpha 161 { "alpha", isalpha, }, 162#undef isblank 163 { "blank", isblank, }, 164#undef iscntrl 165 { "cntrl", iscntrl, }, 166#undef isdigit 167 { "digit", isdigit, }, 168#undef isgraph 169 { "graph", isgraph, }, 170#undef islower 171 { "lower", islower, }, 172#undef isprint 173 { "print", isprint, }, 174#undef ispunct 175 { "punct", ispunct, }, 176#undef isspace 177 { "space", isspace, }, 178#undef isupper 179 { "upper", isupper, }, 180#undef isxdigit 181 { "xdigit", isxdigit, }, 182}; 183 184static void 185genclass(s) 186 STR *s; 187{ 188 register int cnt, (*func) __P((int)); 189 CLASS *cp, tmp; 190 int *p; 191 192 tmp.name = s->str; 193 if ((cp = (CLASS *)bsearch(&tmp, classes, sizeof(classes) / 194 sizeof(CLASS), sizeof(CLASS), c_class)) == NULL) 195 errx(1, "unknown class %s", s->str); 196 197 if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL) 198 errx(1, "malloc"); 199 bzero(p, (NCHARS + 1) * sizeof(int)); 200 for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt) 201 if ((func)(cnt)) 202 *p++ = cnt; 203 *p = OOBCH; 204 205 s->cnt = 0; 206 s->state = SET; 207 s->set = cp->set; 208} 209 210static int 211c_class(a, b) 212 const void *a, *b; 213{ 214 return (strcmp(((CLASS *)a)->name, ((CLASS *)b)->name)); 215} 216 217/* 218 * English doesn't have any equivalence classes, so for now 219 * we just syntax check and grab the character. 220 */ 221static void 222genequiv(s) 223 STR *s; 224{ 225 if (*s->str == '\\') { 226 s->equiv[0] = backslash(s); 227 if (*s->str != '=') 228 errx(1, "misplaced equivalence equals sign"); 229 } else { 230 s->equiv[0] = s->str[0]; 231 if (s->str[1] != '=') 232 errx(1, "misplaced equivalence equals sign"); 233 } 234 s->str += 2; 235 s->cnt = 0; 236 s->state = SET; 237 s->set = s->equiv; 238} 239 240static int 241genrange(s) 242 STR *s; 243{ 244 int stopval; 245 char *savestart; 246 247 savestart = s->str; 248 stopval = *++s->str == '\\' ? backslash(s) : (u_char)*s->str++; 249 if (stopval < (u_char)s->lastch) { 250 s->str = savestart; 251 return (0); 252 } 253 s->cnt = stopval - s->lastch + 1; 254 s->state = RANGE; 255 --s->lastch; 256 return (1); 257} 258 259static void 260genseq(s) 261 STR *s; 262{ 263 char *ep; 264 265 if (s->which == STRING1) 266 errx(1, "sequences only valid in string2"); 267 268 if (*s->str == '\\') 269 s->lastch = backslash(s); 270 else 271 s->lastch = *s->str++; 272 if (*s->str != '*') 273 errx(1, "misplaced sequence asterisk"); 274 275 switch (*++s->str) { 276 case '\\': 277 s->cnt = backslash(s); 278 break; 279 case ']': 280 s->cnt = 0; 281 ++s->str; 282 break; 283 default: 284 if (isdigit((u_char)*s->str)) { 285 s->cnt = strtol(s->str, &ep, 0); 286 if (*ep == ']') { 287 s->str = ep + 1; 288 break; 289 } 290 } 291 errx(1, "illegal sequence count"); 292 /* NOTREACHED */ 293 } 294 295 s->state = s->cnt ? SEQUENCE : INFINITE; 296} 297 298/* 299 * Translate \??? into a character. Up to 3 octal digits, if no digits either 300 * an escape code or a literal character. 301 */ 302static int 303backslash(s) 304 register STR *s; 305{ 306 register int ch, cnt, val; 307 308 for (cnt = val = 0;;) { 309 ch = (u_char)*++s->str; 310 if (!isascii(ch) || !isdigit(ch)) 311 break; 312 val = val * 8 + ch - '0'; 313 if (++cnt == 3) { 314 ++s->str; 315 break; 316 } 317 } 318 if (cnt) 319 return (val); 320 if (ch != '\0') 321 ++s->str; 322 switch (ch) { 323 case 'a': /* escape characters */ 324 return ('\7'); 325 case 'b': 326 return ('\b'); 327 case 'f': 328 return ('\f'); 329 case 'n': 330 return ('\n'); 331 case 'r': 332 return ('\r'); 333 case 't': 334 return ('\t'); 335 case 'v': 336 return ('\13'); 337 case '\0': /* \" -> \ */ 338 s->state = EOS; 339 return ('\\'); 340 default: /* \x" -> x */ 341 return (ch); 342 } 343} 344