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