1 /* casemod.c -- functions to change case of strings */
3 /* Copyright (C) 2008,2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 #if defined (HAVE_CONFIG_H)
25 #if defined (HAVE_UNISTD_H)
27 #endif /* HAVE_UNISTD_H */
33 #include <bashtypes.h>
40 #include <chartypes.h>
42 #include <glob/strmatch.h>
44 #define _to_wupper(wc) (iswlower (wc) ? towupper (wc) : (wc))
45 #define _to_wlower(wc) (iswupper (wc) ? towlower (wc) : (wc))
47 #if !defined (HANDLE_MULTIBYTE)
48 # define cval(s, i) ((s)[(i)])
49 # define iswalnum(c) (isalnum(c))
50 # define TOGGLE(x) (ISUPPER (x) ? tolower (x) : (TOUPPER (x)))
52 # define TOGGLE(x) (iswupper (x) ? towlower (x) : (_to_wupper(x)))
55 /* These must agree with the defines in externs.h */
56 #define CASE_NOOP 0x0000
57 #define CASE_LOWER 0x0001
58 #define CASE_UPPER 0x0002
59 #define CASE_CAPITALIZE 0x0004
60 #define CASE_UNCAP 0x0008
61 #define CASE_TOGGLE 0x0010
62 #define CASE_TOGGLEALL 0x0020
63 #define CASE_UPFIRST 0x0040
64 #define CASE_LOWFIRST 0x0080
66 #define CASE_USEWORDS 0x1000 /* modify behavior to act on words in passed string */
68 extern char *substring __P((char *, int, int));
70 #if defined (HANDLE_MULTIBYTE)
82 return ((wchar_t)s[i]);
85 return ((wchar_t)s[i]);
86 memset (&mps, 0, sizeof (mbstate_t));
87 tmp = mbrtowc (&wc, s + i, l - i, &mps);
88 if (MB_INVALIDCH (tmp) || MB_NULLWCH (tmp))
89 return ((wchar_t)s[i]);
94 /* Modify the case of characters in STRING matching PAT based on the value of
95 FLAGS. If PAT is null, modify the case of each character */
97 sh_modcase (string, pat, flags)
102 int start, next, end;
103 int inword, c, nc, nop, match, usewords;
106 #if defined (HANDLE_MULTIBYTE)
108 char mb[MB_LEN_MAX+1];
113 #if defined (HANDLE_MULTIBYTE)
114 memset (&state, 0, sizeof (mbstate_t));
118 end = strlen (string);
120 ret = (char *)xmalloc (end + 1);
121 strcpy (ret, string);
123 /* See if we are supposed to split on alphanumerics and operate on each word */
124 usewords = (flags & CASE_USEWORDS);
125 flags &= ~CASE_USEWORDS;
130 wc = cval (ret, start);
132 if (iswalnum (wc) == 0)
135 ADVANCE_CHAR (ret, end, start);
142 ADVANCE_CHAR (ret, end, next);
143 s = substring (ret, start, next);
144 match = strmatch (pat, s, FNM_EXTMATCH) != FNM_NOMATCH;
154 /* XXX - for now, the toggling operators work on the individual
155 words in the string, breaking on alphanumerics. Should I
156 leave the capitalization operators to do that also? */
157 if (flags == CASE_CAPITALIZE)
160 nop = inword ? CASE_LOWER : CASE_UPPER;
162 nop = (start > 0) ? CASE_LOWER : CASE_UPPER;
165 else if (flags == CASE_UNCAP)
168 nop = inword ? CASE_UPPER : CASE_LOWER;
170 nop = (start > 0) ? CASE_UPPER : CASE_LOWER;
173 else if (flags == CASE_UPFIRST)
176 nop = inword ? CASE_NOOP : CASE_UPPER;
178 nop = (start > 0) ? CASE_NOOP : CASE_UPPER;
181 else if (flags == CASE_LOWFIRST)
184 nop = inword ? CASE_NOOP : CASE_LOWER;
186 nop = (start > 0) ? CASE_NOOP : CASE_LOWER;
189 else if (flags == CASE_TOGGLE)
191 nop = inword ? CASE_NOOP : CASE_TOGGLE;
197 if (MB_CUR_MAX == 1 || isascii (wc))
202 case CASE_NOOP: nc = wc; break;
203 case CASE_UPPER: nc = TOUPPER (wc); break;
204 case CASE_LOWER: nc = TOLOWER (wc); break;
206 case CASE_TOGGLE: nc = TOGGLE (wc); break;
210 #if defined (HANDLE_MULTIBYTE)
213 mbrtowc (&wc, string + start, end - start, &state);
217 case CASE_NOOP: nwc = wc; break;
218 case CASE_UPPER: nwc = TOUPPER (wc); break;
219 case CASE_LOWER: nwc = TOLOWER (wc); break;
221 case CASE_TOGGLE: nwc = TOGGLE (wc); break;
223 if (nwc != wc) /* just skip unchanged characters */
225 mlen = wcrtomb (mb, nwc, &state);
228 /* Assume the same width */
229 strncpy (ret + start, mb, mlen);
234 /* This assumes that the upper and lower case versions are the same width. */
235 ADVANCE_CHAR (ret, end, start);