1 /******************************************************************************
3 * Module Name: ascase - Source conversion - lower/upper case utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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 == '\\')
250 * Check for translation escape string. It means to ignore
251 * blocks of code during this code conversion.
253 if ((SubBuffer[0] == '/') &&
254 (SubBuffer[1] == '*') &&
255 (SubBuffer[2] == '!'))
257 SubBuffer = strstr (SubBuffer, "!*/");
260 printf ("Found an unterminated translation escape!, line %u: %s\n",
261 LineNumber, Filename);
268 /* Ignore anything that starts with a number (0-9) */
270 if (isdigit ((int) *SubBuffer))
272 /* Ignore hex constants */
274 if ((SubBuffer[0] == '0') &&
275 ((SubBuffer[1] == 'x') || (SubBuffer[1] == 'X')))
280 /* Skip over all digits, both decimal and hex */
282 while (isxdigit ((int) *SubBuffer))
291 * Check for fully upper case identifiers. These are usually macros
292 * or defines. Allow decimal digits and embedded underscores.
294 if (isupper ((int) *SubBuffer))
296 SubString = SubBuffer + 1;
297 while ((isupper ((int) *SubString)) ||
298 (isdigit ((int) *SubString)) ||
305 * For the next character, anything other than a lower case
306 * means that the identifier has terminated, and contains
307 * exclusively Uppers/Digits/Underscores. Ignore the entire
310 if (!islower ((int) *SubString))
312 SubBuffer = SubString + 1;
318 * These forms may indicate an identifier that can be converted:
319 * <UpperCase><LowerCase> (Ax)
320 * <UpperCase><Number> (An)
322 if (isupper ((int) SubBuffer[0]) &&
323 ((islower ((int) SubBuffer[1])) || isdigit ((int) SubBuffer[1])))
325 TokenStart = SubBuffer;
330 /* Walk over the lower case letters and decimal digits */
332 while (islower ((int) *SubBuffer) ||
333 isdigit ((int) *SubBuffer))
338 /* Check for end of line or end of token */
340 if (*SubBuffer == '\n')
346 if (*SubBuffer == ' ')
348 /* Check for form "Axx - " in a parameter header description */
350 while (*SubBuffer == ' ')
356 if ((SubBuffer[1] == '-') &&
357 (SubBuffer[2] == ' '))
361 *TokenStart = (char) tolower ((int) *TokenStart);
368 * Ignore these combinations:
369 * <Letter><Digit><UpperCase>
370 * <Digit><Digit><UpperCase>
371 * <Underscore><Digit><UpperCase>
373 if (isdigit ((int) *SubBuffer))
375 if (isalnum ((int) *(SubBuffer-1)) ||
376 *(SubBuffer-1) == '_')
382 /* Ignore token if next character is not uppercase or digit */
384 if (!isupper ((int) *SubBuffer) &&
385 !isdigit ((int) *SubBuffer))
391 * Form <UpperCase><LowerCaseLetters><UpperCase> (AxxB):
392 * Convert leading character of the token to lower case
396 *TokenStart = (char) tolower ((int) *TokenStart);
400 /* Find the end of this identifier (token) */
402 TokenEnd = SubBuffer - 1;
403 while ((isalnum ((int) *TokenEnd)) ||
409 SubString = TokenEnd;
412 while (*SubString != '\n')
415 * If we have at least two trailing spaces, we can get rid of
416 * one to make up for the newly inserted underscore. This will
417 * help preserve the alignment of the text
419 if ((SubString[0] == ' ') &&
420 (SubString[1] == ' '))
422 Length = SubString - SubBuffer - 1;
431 Length = strlen (&SubBuffer[0]);
435 * Within this identifier, convert this pair of letters that
438 * <LowerCase><UpperCase>
440 * <LowerCase><Underscore><LowerCase>
442 Gbl_MadeChanges = TRUE;
444 /* Insert the underscore */
446 memmove (&SubBuffer[1], &SubBuffer[0], Length + 1);
450 * If we have <UpperCase><UpperCase>, leave them as-is
451 * Enables transforms like:
452 * LocalFADT -> local_FADT
454 if (isupper ((int) SubBuffer[2]))
460 /* Lower case the original upper case letter */
462 SubBuffer[1] = (char) tolower ((int) SubBuffer[1]);
472 /******************************************************************************
474 * FUNCTION: AsLowerCaseIdentifiers
476 * DESCRIPTION: Converts mixed case identifiers to lower case. Leaves comments,
477 * quoted strings, and all-upper-case macros alone.
479 ******************************************************************************/
482 AsLowerCaseIdentifiers (
485 char *SubBuffer = Buffer;
491 * Check for translation escape string -- means to ignore
492 * blocks of code while replacing
494 if ((SubBuffer[0] == '/') &&
495 (SubBuffer[1] == '*') &&
496 (SubBuffer[2] == '!'))
498 SubBuffer = strstr (SubBuffer, "!*/");
505 /* Ignore comments */
507 if ((SubBuffer[0] == '/') &&
508 (SubBuffer[1] == '*'))
510 SubBuffer = strstr (SubBuffer, "*/");
519 /* Ignore quoted strings */
521 if ((SubBuffer[0] == '\"') && (SubBuffer[1] != '\''))
525 /* Find the closing quote */
529 /* Ignore escaped quote characters */
531 if (SubBuffer[0] == '\\')
535 else if (SubBuffer[0] == '\"')
551 * Only lower case if we have an upper followed by a lower
552 * This leaves the all-uppercase things (macros, etc.) intact
554 if ((isupper ((int) SubBuffer[0])) &&
555 (islower ((int) SubBuffer[1])))
557 Gbl_MadeChanges = TRUE;
558 *SubBuffer = (char) tolower ((int) *SubBuffer);
566 /******************************************************************************
568 * FUNCTION: AsUppercaseTokens
570 * DESCRIPTION: Force to uppercase all tokens that begin with the prefix string.
571 * used to convert mixed-case macros and constants to uppercase.
573 ******************************************************************************/
591 SubBuffer = strstr (SubBuffer, PrefixString);
594 TokenEnd = SubBuffer;
595 while ((isalnum ((int) *TokenEnd)) || (*TokenEnd == '_'))
600 for (i = 0; i < (TokenEnd - SubBuffer); i++)
602 if ((islower ((int) SubBuffer[i])) &&
603 (isupper ((int) SubBuffer[i+1])))
606 SubString = TokenEnd;
609 while (*SubString != '\n')
611 if ((SubString[0] == ' ') &&
612 (SubString[1] == ' '))
614 Length = SubString - &SubBuffer[i] - 2;
623 Length = strlen (&SubBuffer[i+1]);
626 memmove (&SubBuffer[i+2], &SubBuffer[i+1], (Length+1));
627 SubBuffer[i+1] = '_';
633 for (i = 0; i < (TokenEnd - SubBuffer); i++)
635 SubBuffer[i] = (char) toupper ((int) SubBuffer[i]);
638 SubBuffer = TokenEnd;