OpenCoverage

casemod.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/lib/sh/casemod.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* casemod.c -- functions to change case of strings */-
2-
3/* Copyright (C) 2008,2009,2015 Free Software Foundation, Inc.-
4-
5 This file is part of GNU Bash, the Bourne Again SHell.-
6-
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.-
11-
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.-
16-
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/>.-
19*/-
20-
21#if defined (HAVE_CONFIG_H)-
22# include <config.h>-
23#endif-
24-
25#if defined (HAVE_UNISTD_H)-
26# include <unistd.h>-
27#endif /* HAVE_UNISTD_H */-
28-
29#include <stdc.h>-
30-
31#include <bashansi.h>-
32#include <bashintl.h>-
33#include <bashtypes.h>-
34-
35#include <stdio.h>-
36#include <ctype.h>-
37#include <xmalloc.h>-
38-
39#include <shmbchar.h>-
40#include <shmbutil.h>-
41#include <chartypes.h>-
42#include <typemax.h>-
43-
44#include <glob/strmatch.h>-
45-
46#define _to_wupper(wc) (iswlower (wc) ? towupper (wc) : (wc))-
47#define _to_wlower(wc) (iswupper (wc) ? towlower (wc) : (wc))-
48-
49#if !defined (HANDLE_MULTIBYTE)-
50# define cval(s, i) ((s)[(i)])-
51# define iswalnum(c) (isalnum(c))-
52# define TOGGLE(x) (ISUPPER (x) ? tolower ((unsigned char)x) : (TOUPPER (x)))-
53#else-
54# define TOGGLE(x) (iswupper (x) ? towlower (x) : (_to_wupper(x)))-
55#endif-
56-
57/* These must agree with the defines in externs.h */-
58#define CASE_NOOP 0x0000-
59#define CASE_LOWER 0x0001-
60#define CASE_UPPER 0x0002-
61#define CASE_CAPITALIZE 0x0004-
62#define CASE_UNCAP 0x0008-
63#define CASE_TOGGLE 0x0010-
64#define CASE_TOGGLEALL 0x0020-
65#define CASE_UPFIRST 0x0040-
66#define CASE_LOWFIRST 0x0080-
67-
68#define CASE_USEWORDS 0x1000 /* modify behavior to act on words in passed string */-
69-
70extern char *substring __P((char *, int, int));-
71-
72#ifndef UCHAR_MAX-
73# define UCHAR_MAX TYPE_MAXIMUM(unsigned char)-
74#endif-
75-
76#if defined (HANDLE_MULTIBYTE)-
77static wchar_t-
78cval (s, i)-
79 char *s;-
80 int i;-
81{-
82 size_t tmp;-
83 wchar_t wc;-
84 int l;-
85 mbstate_t mps; -
86-
87 if (MB_CUR_MAX == 1 || is_basic (s[i]))
(__ctype_get_m...r_max ()) == 1Description
TRUEnever evaluated
FALSEevaluated 1663 times by 1 test
Evaluated by:
  • Self test
is_basic (s[i])Description
TRUEevaluated 1663 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-1663
88 return ((wchar_t)s[i]);
executed 1663 times by 1 test: return ((wchar_t)s[i]);
Executed by:
  • Self test
1663
89 l = strlen (s);-
90 if (i >= (l - 1))
i >= (l - 1)Description
TRUEnever evaluated
FALSEnever evaluated
0
91 return ((wchar_t)s[i]);
never executed: return ((wchar_t)s[i]);
0
92 memset (&mps, 0, sizeof (mbstate_t));-
93 tmp = mbrtowc (&wc, s + i, l - i, &mps);-
94 if (MB_INVALIDCH (tmp) || MB_NULLWCH (tmp))
(tmp) == (size_t)-1Description
TRUEnever evaluated
FALSEnever evaluated
(tmp) == (size_t)-2Description
TRUEnever evaluated
FALSEnever evaluated
((tmp) == 0)Description
TRUEnever evaluated
FALSEnever evaluated
0
95 return ((wchar_t)s[i]);
never executed: return ((wchar_t)s[i]);
0
96 return wc;
never executed: return wc;
0
97}-
98#endif-
99-
100/* Modify the case of characters in STRING matching PAT based on the value of-
101 FLAGS. If PAT is null, modify the case of each character */-
102char *-
103sh_modcase (string, pat, flags)-
104 const char *string;-
105 char *pat;-
106 int flags;-
107{-
108 int start, next, end, retind;-
109 int inword, c, nc, nop, match, usewords;-
110 char *ret, *s;-
111 wchar_t wc;-
112 int mb_cur_max;-
113#if defined (HANDLE_MULTIBYTE)-
114 wchar_t nwc;-
115 char mb[MB_LEN_MAX+1];-
116 int mlen;-
117 size_t m;-
118 mbstate_t state;-
119#endif-
120-
121 if (string == 0 || *string == 0)
string == 0Description
TRUEnever evaluated
FALSEevaluated 149 times by 1 test
Evaluated by:
  • Self test
*string == 0Description
TRUEevaluated 21 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 128 times by 1 test
Evaluated by:
  • Self test
0-149
122 {-
123 ret = (char *)xmalloc (1);-
124 ret[0] = '\0';-
125 return ret;
executed 21 times by 1 test: return ret;
Executed by:
  • Self test
21
126 }-
127-
128#if defined (HANDLE_MULTIBYTE)-
129 memset (&state, 0, sizeof (mbstate_t));-
130#endif-
131-
132 start = 0;-
133 end = strlen (string);-
134 mb_cur_max = MB_CUR_MAX;-
135-
136 ret = (char *)xmalloc (2*end + 1);-
137 retind = 0;-
138-
139 /* See if we are supposed to split on alphanumerics and operate on each word */-
140 usewords = (flags & CASE_USEWORDS);-
141 flags &= ~CASE_USEWORDS;-
142-
143 inword = 0;-
144 while (start < end)
start < endDescription
TRUEevaluated 1663 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 128 times by 1 test
Evaluated by:
  • Self test
128-1663
145 {-
146 wc = cval ((char *)string, start);-
147-
148 if (iswalnum (wc) == 0)
iswalnum (wc) == 0Description
TRUEevaluated 118 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1545 times by 1 test
Evaluated by:
  • Self test
118-1545
149 inword = 0;
executed 118 times by 1 test: inword = 0;
Executed by:
  • Self test
118
150-
151 if (pat)
patDescription
TRUEevaluated 622 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1041 times by 1 test
Evaluated by:
  • Self test
622-1041
152 {-
153 next = start;-
154 ADVANCE_CHAR (string, end, next);
executed 622 times by 1 test: mblength = 1;
Executed by:
  • Self test
never executed: mblength = 1;
never executed: end of block
never executed: end of block
never executed: (next)++;
executed 622 times by 1 test: (next) += mblength;
Executed by:
  • Self test
never executed: (next)++;
locale_mb_cur_max > 1Description
TRUEevaluated 622 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
_fDescription
TRUEevaluated 622 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
mblength == 0Description
TRUEnever evaluated
FALSEevaluated 622 times by 1 test
Evaluated by:
  • Self test
locale_utf8localeDescription
TRUEnever evaluated
FALSEnever evaluated
(((string)[next] & 0x80) == 0)Description
TRUEnever evaluated
FALSEnever evaluated
mblength == (size_t)-2Description
TRUEnever evaluated
FALSEevaluated 622 times by 1 test
Evaluated by:
  • Self test
mblength == (size_t)-1Description
TRUEnever evaluated
FALSEevaluated 622 times by 1 test
Evaluated by:
  • Self test
0-622
155 s = substring ((char *)string, start, next);-
156 match = strmatch (pat, s, FNM_EXTMATCH) != FNM_NOMATCH;-
157 free (s);-
158 if (match == 0)
match == 0Description
TRUEevaluated 254 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 368 times by 1 test
Evaluated by:
  • Self test
254-368
159 {-
160 /* copy unmatched portion */-
161 memcpy (ret + retind, string + start, next - start);-
162 retind += next - start;-
163 start = next;-
164 inword = 1;-
165 continue;
executed 254 times by 1 test: continue;
Executed by:
  • Self test
254
166 }-
167 }
executed 368 times by 1 test: end of block
Executed by:
  • Self test
368
168-
169 /* XXX - for now, the toggling operators work on the individual-
170 words in the string, breaking on alphanumerics. Should I-
171 leave the capitalization operators to do that also? */-
172 if (flags == CASE_CAPITALIZE)
flags == 0x0004Description
TRUEevaluated 126 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1283 times by 1 test
Evaluated by:
  • Self test
126-1283
173 {-
174 if (usewords)
usewordsDescription
TRUEnever evaluated
FALSEevaluated 126 times by 1 test
Evaluated by:
  • Self test
0-126
175 nop = inword ? CASE_LOWER : CASE_UPPER;
never executed: nop = inword ? 0x0001 : 0x0002;
inwordDescription
TRUEnever evaluated
FALSEnever evaluated
0
176 else-
177 nop = (start > 0) ? CASE_LOWER : CASE_UPPER;
executed 126 times by 1 test: nop = (start > 0) ? 0x0001 : 0x0002;
Executed by:
  • Self test
(start > 0)Description
TRUEevaluated 124 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
2-126
178 inword = 1;-
179 }
executed 126 times by 1 test: end of block
Executed by:
  • Self test
126
180 else if (flags == CASE_UNCAP)
flags == 0x0008Description
TRUEnever evaluated
FALSEevaluated 1283 times by 1 test
Evaluated by:
  • Self test
0-1283
181 {-
182 if (usewords)
usewordsDescription
TRUEnever evaluated
FALSEnever evaluated
0
183 nop = inword ? CASE_UPPER : CASE_LOWER;
never executed: nop = inword ? 0x0002 : 0x0001;
inwordDescription
TRUEnever evaluated
FALSEnever evaluated
0
184 else-
185 nop = (start > 0) ? CASE_UPPER : CASE_LOWER;
never executed: nop = (start > 0) ? 0x0002 : 0x0001;
(start > 0)Description
TRUEnever evaluated
FALSEnever evaluated
0
186 inword = 1;-
187 }
never executed: end of block
0
188 else if (flags == CASE_UPFIRST)
flags == 0x0040Description
TRUEevaluated 297 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 986 times by 1 test
Evaluated by:
  • Self test
297-986
189 {-
190 if (usewords)
usewordsDescription
TRUEnever evaluated
FALSEevaluated 297 times by 1 test
Evaluated by:
  • Self test
0-297
191 nop = inword ? CASE_NOOP : CASE_UPPER;
never executed: nop = inword ? 0x0000 : 0x0002;
inwordDescription
TRUEnever evaluated
FALSEnever evaluated
0
192 else-
193 nop = (start > 0) ? CASE_NOOP : CASE_UPPER;
executed 297 times by 1 test: nop = (start > 0) ? 0x0000 : 0x0002;
Executed by:
  • Self test
(start > 0)Description
TRUEevaluated 274 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 23 times by 1 test
Evaluated by:
  • Self test
23-297
194 inword = 1;-
195 }
executed 297 times by 1 test: end of block
Executed by:
  • Self test
297
196 else if (flags == CASE_LOWFIRST)
flags == 0x0080Description
TRUEevaluated 182 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 804 times by 1 test
Evaluated by:
  • Self test
182-804
197 {-
198 if (usewords)
usewordsDescription
TRUEnever evaluated
FALSEevaluated 182 times by 1 test
Evaluated by:
  • Self test
0-182
199 nop = inword ? CASE_NOOP : CASE_LOWER;
never executed: nop = inword ? 0x0000 : 0x0001;
inwordDescription
TRUEnever evaluated
FALSEnever evaluated
0
200 else-
201 nop = (start > 0) ? CASE_NOOP : CASE_LOWER;
executed 182 times by 1 test: nop = (start > 0) ? 0x0000 : 0x0001;
Executed by:
  • Self test
(start > 0)Description
TRUEevaluated 170 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
12-182
202 inword = 1;-
203 }
executed 182 times by 1 test: end of block
Executed by:
  • Self test
182
204 else if (flags == CASE_TOGGLE)
flags == 0x0010Description
TRUEevaluated 15 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 789 times by 1 test
Evaluated by:
  • Self test
15-789
205 {-
206 nop = inword ? CASE_NOOP : CASE_TOGGLE;
inwordDescription
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
1-14
207 inword = 1;-
208 }
executed 15 times by 1 test: end of block
Executed by:
  • Self test
15
209 else-
210 nop = flags;
executed 789 times by 1 test: nop = flags;
Executed by:
  • Self test
789
211-
212 /* Can't short-circuit, some locales have multibyte upper and lower-
213 case equivalents of single-byte ascii characters (e.g., Turkish) */-
214 if (mb_cur_max == 1)
mb_cur_max == 1Description
TRUEnever evaluated
FALSEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
0-1409
215 {-
216singlebyte:-
217 switch (nop)-
218 {-
219 default:
never executed: default:
0
220 case CASE_NOOP: nc = wc; break;
never executed: break;
never executed: case 0x0000:
0
221 case CASE_UPPER: nc = TOUPPER (wc); break;
never executed: end of block
never executed: __res = toupper ( wc );
never executed: __res = (*__ctype_toupper_loc ())[(int) ( wc )];
never executed: break;
sizeof ( wc ) > 1Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( wc )Description
TRUEnever evaluated
FALSEnever evaluated
never executed: case 0x0002:
((*__ctype_b_l...int) _ISlower)Description
TRUEnever evaluated
FALSEnever evaluated
__c < -128Description
TRUEnever evaluated
FALSEnever evaluated
__c > 255Description
TRUEnever evaluated
FALSEnever evaluated
0
222 case CASE_LOWER: nc = TOLOWER (wc); break;
never executed: end of block
never executed: __res = tolower ( wc );
never executed: __res = (*__ctype_tolower_loc ())[(int) ( wc )];
never executed: break;
sizeof ( wc ) > 1Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( wc )Description
TRUEnever evaluated
FALSEnever evaluated
never executed: case 0x0001:
((*__ctype_b_l...int) _ISupper)Description
TRUEnever evaluated
FALSEnever evaluated
__c < -128Description
TRUEnever evaluated
FALSEnever evaluated
__c > 255Description
TRUEnever evaluated
FALSEnever evaluated
0
223 case CASE_TOGGLEALL:
never executed: case 0x0020:
0
224 case CASE_TOGGLE: nc = TOGGLE (wc); break;
never executed: break;
iswupper (wc)Description
TRUEnever evaluated
FALSEnever evaluated
iswlower (wc)Description
TRUEnever evaluated
FALSEnever evaluated
never executed: case 0x0010:
0
225 }-
226 ret[retind++] = nc;-
227 }
never executed: end of block
0
228#if defined (HANDLE_MULTIBYTE)-
229 else-
230 {-
231 m = mbrtowc (&wc, string + start, end - start, &state);-
232 /* Have to go through wide case conversion even for single-byte-
233 chars, to accommodate single-byte characters where the-
234 corresponding upper or lower case equivalent is multibyte. */-
235 if (MB_INVALIDCH (m))
(m) == (size_t)-1Description
TRUEnever evaluated
FALSEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
(m) == (size_t)-2Description
TRUEnever evaluated
FALSEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
0-1409
236 {-
237 wc = (unsigned char)string[start];-
238 goto singlebyte;
never executed: goto singlebyte;
0
239 }-
240 else if (MB_NULLWCH (m))
((m) == 0)Description
TRUEnever evaluated
FALSEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
0-1409
241 wc = L'\0';
never executed: wc = L'\0';
0
242 switch (nop)-
243 {-
244 default:
never executed: default:
0
245 case CASE_NOOP: nwc = wc; break;
executed 458 times by 1 test: break;
Executed by:
  • Self test
executed 458 times by 1 test: case 0x0000:
Executed by:
  • Self test
458
246 case CASE_UPPER: nwc = _to_wupper (wc); break;
executed 427 times by 1 test: break;
Executed by:
  • Self test
iswlower (wc)Description
TRUEevaluated 377 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 50 times by 1 test
Evaluated by:
  • Self test
executed 427 times by 1 test: case 0x0002:
Executed by:
  • Self test
50-427
247 case CASE_LOWER: nwc = _to_wlower (wc); break;
executed 514 times by 1 test: break;
Executed by:
  • Self test
iswupper (wc)Description
TRUEevaluated 164 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 350 times by 1 test
Evaluated by:
  • Self test
executed 514 times by 1 test: case 0x0001:
Executed by:
  • Self test
164-514
248 case CASE_TOGGLEALL:
executed 9 times by 1 test: case 0x0020:
Executed by:
  • Self test
9
249 case CASE_TOGGLE: nwc = TOGGLE (wc); break;
executed 10 times by 1 test: break;
Executed by:
  • Self test
iswupper (wc)Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
iswlower (wc)Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
executed 1 time by 1 test: case 0x0010:
Executed by:
  • Self test
0-10
250 }-
251-
252 /* We don't have to convert `wide' characters that are in the-
253 unsigned char range back to single-byte `multibyte' characters. */-
254 if ((int)nwc <= UCHAR_MAX && is_basic ((int)nwc))
(int)nwc <= (0x7f * 2 + 1)Description
TRUEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
is_basic ((int)nwc)Description
TRUEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-1409
255 ret[retind++] = nwc;
executed 1409 times by 1 test: ret[retind++] = nwc;
Executed by:
  • Self test
1409
256 else-
257 {-
258 mlen = wcrtomb (mb, nwc, &state);-
259 if (mlen > 0)
mlen > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
260 mb[mlen] = '\0';
never executed: mb[mlen] = '\0';
0
261 /* Don't assume the same width */-
262 strncpy (ret + retind, mb, mlen);-
263 retind += mlen;-
264 }
never executed: end of block
0
265 }-
266#endif-
267-
268 ADVANCE_CHAR (string, end, start);
executed 1409 times by 1 test: mblength = 1;
Executed by:
  • Self test
never executed: mblength = 1;
never executed: end of block
never executed: end of block
never executed: (start)++;
executed 1409 times by 1 test: (start) += mblength;
Executed by:
  • Self test
never executed: (start)++;
locale_mb_cur_max > 1Description
TRUEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
_fDescription
TRUEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
mblength == 0Description
TRUEnever evaluated
FALSEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
locale_utf8localeDescription
TRUEnever evaluated
FALSEnever evaluated
(((string)[sta... & 0x80) == 0)Description
TRUEnever evaluated
FALSEnever evaluated
mblength == (size_t)-2Description
TRUEnever evaluated
FALSEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
mblength == (size_t)-1Description
TRUEnever evaluated
FALSEevaluated 1409 times by 1 test
Evaluated by:
  • Self test
0-1409
269 }
executed 1409 times by 1 test: end of block
Executed by:
  • Self test
1409
270-
271 ret[retind] = '\0';-
272 return ret;
executed 128 times by 1 test: return ret;
Executed by:
  • Self test
128
273}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2