]>
git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/tr/str.c
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/usr.bin/tr/str.c,v 1.24 2004/11/14 05:15:25 jkh Exp $");
39 static const char sccsid
[] = "@(#)str.c 8.2 (Berkeley) 4/28/95";
42 #include <sys/cdefs.h>
43 #include <sys/types.h>
58 static int backslash(STR
*, int *);
59 static int bracket(STR
*);
60 static void genclass(STR
*);
61 static void genequiv(STR
*);
62 static int genrange(STR
*, int);
63 static void genseq(STR
*);
66 * Using libc internal function __collate_lookup_l for character
69 void __collate_lookup_l(const __darwin_wchar_t
*, int *, int *, int *,
72 * Cache for primary collation weight of each single byte character
73 * used in static void genequiv(s)
75 int collation_weight_cache
[NCHARS_SB
];
76 int is_weight_cached
= 0;
92 switch (ch
= (u_char
)*s
->str
) {
95 * force at least one postive return so setup() will
96 * process lastch of a sequence like [a*]; but change
97 * state so it won't get stuck in a while(next(s)) loop
101 #endif /* __APPLE__ */
109 s
->lastch
= backslash(s
, &is_octal
);
116 clen
= mbrtowc(&wch
, s
->str
, MB_LEN_MAX
, NULL
);
117 if (clen
== (size_t)-1 || clen
== (size_t)-2 ||
119 errc(1, EILSEQ
, NULL
);
126 /* We can start a range at any time. */
127 if (s
->str
[0] == '-' && genrange(s
, is_octal
))
147 ch
= nextwctype(s
->lastch
, s
->cclass
);
155 if ((ch
= s
->set
[s
->cnt
++]) == OOBCH
) {
174 case ':': /* "[:class:]" */
175 if ((p
= strchr(s
->str
+ 2, ']')) == NULL
)
177 if (*(p
- 1) != ':' || p
- s
->str
< 4)
184 case '=': /* "[=equiv=]" */
185 if ((p
= strchr(s
->str
+ 2, ']')) == NULL
)
187 if (*(p
- 1) != '=' || p
- s
->str
< 4)
192 default: /* "[\###*n]" or "[#*n]" */
194 if ((p
= strpbrk(s
->str
+ 2, "*]")) == NULL
)
196 if (p
[0] != '*' || index(p
, ']') == NULL
)
210 if ((s
->cclass
= wctype(s
->str
)) == 0)
211 errx(1, "unknown class %s", s
->str
);
213 s
->lastch
= -1; /* incremented before check in next() */
214 if (strcmp(s
->str
, "upper") == 0)
215 s
->state
= CCLASS_UPPER
;
216 else if (strcmp(s
->str
, "lower") == 0)
217 s
->state
= CCLASS_LOWER
;
230 if (*s
->str
== '\\') {
231 s
->equiv
[0] = backslash(s
, NULL
);
233 errx(1, "misplaced equivalence equals sign");
236 clen
= mbrtowc(&wc
, s
->str
, MB_LEN_MAX
, NULL
);
237 if (clen
== (size_t)-1 || clen
== (size_t)-2 || clen
== 0)
238 errc(1, EILSEQ
, NULL
);
240 if (s
->str
[clen
] != '=')
241 errx(1, "misplaced equivalence equals sign");
246 * Partially supporting multi-byte locales; only finds equivalent
247 * characters within the first NCHARS_SB entries of the
252 __collate_lookup_l(s
->equiv
, &len
, &tprim
, &tsec
, LC_GLOBAL_LOCALE
);
255 for (p
= 1, i
= 1; i
< NCHARS_SB
; i
++) {
257 if (is_weight_cached
) {
259 * retrieve primary weight from cache
261 cprim
= collation_weight_cache
[i
];
264 * perform lookup of primary weight and fill cache
267 __collate_lookup_l((__darwin_wchar_t
*)&i
, &len
, &cprim
, &csec
, LC_GLOBAL_LOCALE
);
268 collation_weight_cache
[i
] = cprim
;
272 * If a character does not exist in the collation
273 * table, just skip it
280 * Only compare primary weights to determine multi-byte
281 * character equivalence
283 if (cprim
== tprim
) {
289 if (!is_weight_cached
) {
290 is_weight_cached
= 1;
300 genrange(STR
*s
, int was_octal
)
310 if (*++s
->str
== '\\')
311 stopval
= backslash(s
, &octal
);
313 clen
= mbrtowc(&wc
, s
->str
, MB_LEN_MAX
, NULL
);
314 if (clen
== (size_t)-1 || clen
== (size_t)-2)
315 errc(1, EILSEQ
, NULL
);
320 * XXX Characters are not ordered according to collating sequence in
323 if (octal
|| was_octal
|| MB_CUR_MAX
> 1) {
324 if (stopval
< s
->lastch
) {
328 s
->cnt
= stopval
- s
->lastch
+ 1;
333 if (charcoll((const void *)&stopval
, (const void *)&(s
->lastch
)) < 0) {
337 if ((s
->set
= p
= malloc((NCHARS_SB
+ 1) * sizeof(int))) == NULL
)
338 err(1, "genrange() malloc");
339 for (cnt
= 0; cnt
< NCHARS_SB
; cnt
++)
340 if (charcoll((const void *)&cnt
, (const void *)&(s
->lastch
)) >= 0 &&
341 charcoll((const void *)&cnt
, (const void *)&stopval
) <= 0)
349 mergesort(s
->set
, n
, sizeof(*(s
->set
)), charcoll
);
362 if (s
->which
== STRING1
)
363 errx(1, "sequences only valid in string2");
364 #endif /* !__APPLE__ */
367 s
->lastch
= backslash(s
, NULL
);
369 clen
= mbrtowc(&wc
, s
->str
, MB_LEN_MAX
, NULL
);
370 if (clen
== (size_t)-1 || clen
== (size_t)-2)
371 errc(1, EILSEQ
, NULL
);
376 errx(1, "misplaced sequence asterisk");
380 s
->cnt
= backslash(s
, NULL
);
387 if (isdigit((u_char
)*s
->str
)) {
388 s
->cnt
= strtol(s
->str
, &ep
, 0);
394 errx(1, "illegal sequence count");
398 s
->state
= s
->cnt
? SEQUENCE
: INFINITE
;
402 * Translate \??? into a character. Up to 3 octal digits, if no digits either
403 * an escape code or a literal character.
406 backslash(STR
*s
, int *is_octal
)
410 if (is_octal
!= NULL
)
412 for (cnt
= val
= 0;;) {
413 ch
= (u_char
)*++s
->str
;
414 if (!isdigit(ch
) || ch
> '7')
416 val
= val
* 8 + ch
- '0';
423 if (is_octal
!= NULL
)
430 case 'a': /* escape characters */
444 case '\0': /* \" -> \ */
447 default: /* \x" -> x */