1 /******************************************************************************
3 * Module Name: ascase - Source conversion - lower/upper case utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
46 /* Local prototypes */
54 /******************************************************************************
56 * FUNCTION: AsLowerCaseString
58 * DESCRIPTION: LowerCase all instances of a target string with a replacement
59 * string. Returns count of the strings replaced.
61 ******************************************************************************/
72 int LowerCaseCount
= 0;
76 TargetLength
= strlen (Target
);
83 /* Find the target string */
85 SubString1
= strstr (SubBuffer
, Target
);
88 return (LowerCaseCount
);
92 * Check for translation escape string -- means to ignore
93 * blocks of code while replacing
95 if (Gbl_IgnoreTranslationEscapes
)
101 SubString2
= strstr (SubBuffer
, AS_START_IGNORE
);
105 (SubString2
< SubString1
))
107 /* Find end of the escape block starting at "Substring2" */
109 SubString2
= strstr (SubString2
, AS_STOP_IGNORE
);
112 /* Didn't find terminator */
114 return (LowerCaseCount
);
117 /* Move buffer to end of escape block and continue */
119 SubBuffer
= SubString2
;
122 /* Do the actual replace if the target was found */
126 if (!AsMatchExactWord (SubString1
, TargetLength
))
128 SubBuffer
= SubString1
+ 1;
132 for (i
= 0; i
< TargetLength
; i
++)
134 SubString1
[i
] = (char) tolower ((int) SubString1
[i
]);
137 SubBuffer
= SubString1
+ TargetLength
;
139 if ((Gbl_WidenDeclarations
) && (!Gbl_StructDefs
))
141 if ((SubBuffer
[0] == ' ') && (SubBuffer
[1] == ' '))
143 AsInsertData (SubBuffer
, " ", 8);
151 return (LowerCaseCount
);
155 /******************************************************************************
157 * FUNCTION: AsMixedCaseToUnderscores
159 * DESCRIPTION: Converts mixed case identifiers to underscored identifiers.
162 * ThisUsefullyNamedIdentifier becomes:
164 * this_usefully_named_identifier
166 ******************************************************************************/
169 AsMixedCaseToUnderscores (
174 char *SubBuffer
= Buffer
;
176 char *TokenStart
= NULL
;
178 UINT32 LineNumber
= 1;
183 * Examine the entire buffer (contains the entire file)
184 * We are only interested in these tokens:
185 * Escape sequences - ignore entire sequence
186 * Single-quoted constants - ignore
187 * Quoted strings - ignore entire string
188 * Translation escape - starts with /,*,!
189 * Decimal and hex numeric constants - ignore entire token
190 * Entire uppercase token - ignore, it is a macro or define
191 * Starts with underscore, then a lowercase or digit: convert
195 if (*SubBuffer
== '\n')
202 /* Ignore standard escape sequences (\n, \r, etc.) Not Hex or Octal escapes */
204 if (*SubBuffer
== '\\')
210 /* Ignore single-quoted characters */
212 if (*SubBuffer
== '\'')
218 /* Ignore standard double-quoted strings */
220 if (*SubBuffer
== '"')
224 while (*SubBuffer
!= '"')
230 printf ("Found an unterminated quoted string!, line %u: %s\n",
231 LineNumber
, Filename
);
235 /* Handle escape sequences */
237 if (*SubBuffer
== '\\')
249 * Check for translation escape string. It means to ignore
250 * blocks of code during this code conversion.
252 if ((SubBuffer
[0] == '/') &&
253 (SubBuffer
[1] == '*') &&
254 (SubBuffer
[2] == '!'))
256 SubBuffer
= strstr (SubBuffer
, "!*/");
259 printf ("Found an unterminated translation escape!, line %u: %s\n",
260 LineNumber
, Filename
);
266 /* Ignore anything that starts with a number (0-9) */
268 if (isdigit ((int) *SubBuffer
))
270 /* Ignore hex constants */
272 if ((SubBuffer
[0] == '0') &&
273 ((SubBuffer
[1] == 'x') || (SubBuffer
[1] == 'X')))
278 /* Skip over all digits, both decimal and hex */
280 while (isxdigit ((int) *SubBuffer
))
289 * Check for fully upper case identifiers. These are usually macros
290 * or defines. Allow decimal digits and embedded underscores.
292 if (isupper ((int) *SubBuffer
))
294 SubString
= SubBuffer
+ 1;
295 while ((isupper ((int) *SubString
)) ||
296 (isdigit ((int) *SubString
)) ||
303 * For the next character, anything other than a lower case
304 * means that the identifier has terminated, and contains
305 * exclusively Uppers/Digits/Underscores. Ignore the entire
308 if (!islower ((int) *SubString
))
310 SubBuffer
= SubString
+ 1;
316 * These forms may indicate an identifier that can be converted:
317 * <UpperCase><LowerCase> (Ax)
318 * <UpperCase><Number> (An)
320 if (isupper ((int) SubBuffer
[0]) &&
321 ((islower ((int) SubBuffer
[1])) || isdigit ((int) SubBuffer
[1])))
323 TokenStart
= SubBuffer
;
328 /* Walk over the lower case letters and decimal digits */
330 while (islower ((int) *SubBuffer
) ||
331 isdigit ((int) *SubBuffer
))
336 /* Check for end of line or end of token */
338 if (*SubBuffer
== '\n')
344 if (*SubBuffer
== ' ')
346 /* Check for form "Axx - " in a parameter header description */
348 while (*SubBuffer
== ' ')
354 if ((SubBuffer
[1] == '-') &&
355 (SubBuffer
[2] == ' '))
359 *TokenStart
= (char) tolower ((int) *TokenStart
);
366 * Ignore these combinations:
367 * <Letter><Digit><UpperCase>
368 * <Digit><Digit><UpperCase>
369 * <Underscore><Digit><UpperCase>
371 if (isdigit ((int) *SubBuffer
))
373 if (isalnum ((int) *(SubBuffer
-1)) ||
374 *(SubBuffer
-1) == '_')
380 /* Ignore token if next character is not uppercase or digit */
382 if (!isupper ((int) *SubBuffer
) &&
383 !isdigit ((int) *SubBuffer
))
389 * Form <UpperCase><LowerCaseLetters><UpperCase> (AxxB):
390 * Convert leading character of the token to lower case
394 *TokenStart
= (char) tolower ((int) *TokenStart
);
398 /* Find the end of this identifier (token) */
400 TokenEnd
= SubBuffer
- 1;
401 while ((isalnum ((int) *TokenEnd
)) ||
407 SubString
= TokenEnd
;
410 while (*SubString
!= '\n')
413 * If we have at least two trailing spaces, we can get rid of
414 * one to make up for the newly inserted underscore. This will
415 * help preserve the alignment of the text
417 if ((SubString
[0] == ' ') &&
418 (SubString
[1] == ' '))
420 Length
= SubString
- SubBuffer
- 1;
429 Length
= strlen (&SubBuffer
[0]);
433 * Within this identifier, convert this pair of letters that
436 * <LowerCase><UpperCase>
438 * <LowerCase><Underscore><LowerCase>
440 Gbl_MadeChanges
= TRUE
;
442 /* Insert the underscore */
444 memmove (&SubBuffer
[1], &SubBuffer
[0], Length
+ 1);
448 * If we have <UpperCase><UpperCase>, leave them as-is
449 * Enables transforms like:
450 * LocalFADT -> local_FADT
452 if (isupper ((int) SubBuffer
[2]))
458 /* Lower case the original upper case letter */
460 SubBuffer
[1] = (char) tolower ((int) SubBuffer
[1]);
470 /******************************************************************************
472 * FUNCTION: AsLowerCaseIdentifiers
474 * DESCRIPTION: Converts mixed case identifiers to lower case. Leaves comments,
475 * quoted strings, and all-upper-case macros alone.
477 ******************************************************************************/
480 AsLowerCaseIdentifiers (
483 char *SubBuffer
= Buffer
;
489 * Check for translation escape string -- means to ignore
490 * blocks of code while replacing
492 if ((SubBuffer
[0] == '/') &&
493 (SubBuffer
[1] == '*') &&
494 (SubBuffer
[2] == '!'))
496 SubBuffer
= strstr (SubBuffer
, "!*/");
503 /* Ignore comments */
505 if ((SubBuffer
[0] == '/') &&
506 (SubBuffer
[1] == '*'))
508 SubBuffer
= strstr (SubBuffer
, "*/");
517 /* Ignore quoted strings */
519 if ((SubBuffer
[0] == '\"') && (SubBuffer
[1] != '\''))
523 /* Find the closing quote */
527 /* Ignore escaped quote characters */
529 if (SubBuffer
[0] == '\\')
533 else if (SubBuffer
[0] == '\"')
548 * Only lower case if we have an upper followed by a lower
549 * This leaves the all-uppercase things (macros, etc.) intact
551 if ((isupper ((int) SubBuffer
[0])) &&
552 (islower ((int) SubBuffer
[1])))
554 Gbl_MadeChanges
= TRUE
;
555 *SubBuffer
= (char) tolower ((int) *SubBuffer
);
563 /******************************************************************************
565 * FUNCTION: AsUppercaseTokens
567 * DESCRIPTION: Force to uppercase all tokens that begin with the prefix string.
568 * used to convert mixed-case macros and constants to uppercase.
570 ******************************************************************************/
588 SubBuffer
= strstr (SubBuffer
, PrefixString
);
591 TokenEnd
= SubBuffer
;
592 while ((isalnum ((int) *TokenEnd
)) || (*TokenEnd
== '_'))
597 for (i
= 0; i
< (TokenEnd
- SubBuffer
); i
++)
599 if ((islower ((int) SubBuffer
[i
])) &&
600 (isupper ((int) SubBuffer
[i
+1])))
603 SubString
= TokenEnd
;
606 while (*SubString
!= '\n')
608 if ((SubString
[0] == ' ') &&
609 (SubString
[1] == ' '))
611 Length
= SubString
- &SubBuffer
[i
] - 2;
620 Length
= strlen (&SubBuffer
[i
+1]);
623 memmove (&SubBuffer
[i
+2], &SubBuffer
[i
+1], (Length
+1));
624 SubBuffer
[i
+1] = '_';
630 for (i
= 0; i
< (TokenEnd
- SubBuffer
); i
++)
632 SubBuffer
[i
] = (char) toupper ((int) SubBuffer
[i
]);
635 SubBuffer
= TokenEnd
;