1 /******************************************************************************
3 * Module Name: ascase - Source conversion - lower/upper case utilities
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2020, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
154 /* Local prototypes */
162 /******************************************************************************
164 * FUNCTION: AsLowerCaseString
166 * DESCRIPTION: LowerCase all instances of a target string with a replacement
167 * string. Returns count of the strings replaced.
169 ******************************************************************************/
180 int LowerCaseCount = 0;
184 TargetLength = strlen (Target);
191 /* Find the target string */
193 SubString1 = strstr (SubBuffer, Target);
196 return (LowerCaseCount);
200 * Check for translation escape string -- means to ignore
201 * blocks of code while replacing
203 if (Gbl_IgnoreTranslationEscapes)
209 SubString2 = strstr (SubBuffer, AS_START_IGNORE);
213 (SubString2 < SubString1))
215 /* Find end of the escape block starting at "Substring2" */
217 SubString2 = strstr (SubString2, AS_STOP_IGNORE);
220 /* Didn't find terminator */
222 return (LowerCaseCount);
225 /* Move buffer to end of escape block and continue */
227 SubBuffer = SubString2;
230 /* Do the actual replace if the target was found */
234 if (!AsMatchExactWord (SubString1, TargetLength))
236 SubBuffer = SubString1 + 1;
240 for (i = 0; i < TargetLength; i++)
242 SubString1[i] = (char) tolower ((int) SubString1[i]);
245 SubBuffer = SubString1 + TargetLength;
247 if ((Gbl_WidenDeclarations) && (!Gbl_StructDefs))
249 if ((SubBuffer[0] == ' ') && (SubBuffer[1] == ' '))
251 AsInsertData (SubBuffer, " ", 8);
259 return (LowerCaseCount);
263 /******************************************************************************
265 * FUNCTION: AsMixedCaseToUnderscores
267 * DESCRIPTION: Converts mixed case identifiers to underscored identifiers.
270 * ThisUsefullyNamedIdentifier becomes:
272 * this_usefully_named_identifier
274 ******************************************************************************/
277 AsMixedCaseToUnderscores (
282 char *SubBuffer = Buffer;
284 char *TokenStart = NULL;
286 UINT32 LineNumber = 1;
291 * Examine the entire buffer (contains the entire file)
292 * We are only interested in these tokens:
293 * Escape sequences - ignore entire sequence
294 * Single-quoted constants - ignore
295 * Quoted strings - ignore entire string
296 * Translation escape - starts with /,*,!
297 * Decimal and hex numeric constants - ignore entire token
298 * Entire uppercase token - ignore, it is a macro or define
299 * Starts with underscore, then a lowercase or digit: convert
303 if (*SubBuffer == '\n')
310 /* Ignore standard escape sequences (\n, \r, etc.) Not Hex or Octal escapes */
312 if (*SubBuffer == '\\')
318 /* Ignore single-quoted characters */
320 if (*SubBuffer == '\'')
326 /* Ignore standard double-quoted strings */
328 if (*SubBuffer == '"')
332 while (*SubBuffer != '"')
338 printf ("Found an unterminated quoted string!, line %u: %s\n",
339 LineNumber, Filename);
343 /* Handle escape sequences */
345 if (*SubBuffer == '\\')
358 * Check for translation escape string. It means to ignore
359 * blocks of code during this code conversion.
361 if ((SubBuffer[0] == '/') &&
362 (SubBuffer[1] == '*') &&
363 (SubBuffer[2] == '!'))
365 SubBuffer = strstr (SubBuffer, "!*/");
368 printf ("Found an unterminated translation escape!, line %u: %s\n",
369 LineNumber, Filename);
376 /* Ignore anything that starts with a number (0-9) */
378 if (isdigit ((int) *SubBuffer))
380 /* Ignore hex constants */
382 if ((SubBuffer[0] == '0') &&
383 ((SubBuffer[1] == 'x') || (SubBuffer[1] == 'X')))
388 /* Skip over all digits, both decimal and hex */
390 while (isxdigit ((int) *SubBuffer))
399 * Check for fully upper case identifiers. These are usually macros
400 * or defines. Allow decimal digits and embedded underscores.
402 if (isupper ((int) *SubBuffer))
404 SubString = SubBuffer + 1;
405 while ((isupper ((int) *SubString)) ||
406 (isdigit ((int) *SubString)) ||
413 * For the next character, anything other than a lower case
414 * means that the identifier has terminated, and contains
415 * exclusively Uppers/Digits/Underscores. Ignore the entire
418 if (!islower ((int) *SubString))
420 SubBuffer = SubString + 1;
426 * These forms may indicate an identifier that can be converted:
427 * <UpperCase><LowerCase> (Ax)
428 * <UpperCase><Number> (An)
430 if (isupper ((int) SubBuffer[0]) &&
431 ((islower ((int) SubBuffer[1])) || isdigit ((int) SubBuffer[1])))
433 TokenStart = SubBuffer;
438 /* Walk over the lower case letters and decimal digits */
440 while (islower ((int) *SubBuffer) ||
441 isdigit ((int) *SubBuffer))
446 /* Check for end of line or end of token */
448 if (*SubBuffer == '\n')
454 if (*SubBuffer == ' ')
456 /* Check for form "Axx - " in a parameter header description */
458 while (*SubBuffer == ' ')
464 if ((SubBuffer[1] == '-') &&
465 (SubBuffer[2] == ' '))
469 *TokenStart = (char) tolower ((int) *TokenStart);
476 * Ignore these combinations:
477 * <Letter><Digit><UpperCase>
478 * <Digit><Digit><UpperCase>
479 * <Underscore><Digit><UpperCase>
481 if (isdigit ((int) *SubBuffer))
483 if (isalnum ((int) *(SubBuffer-1)) ||
484 *(SubBuffer-1) == '_')
490 /* Ignore token if next character is not uppercase or digit */
492 if (!isupper ((int) *SubBuffer) &&
493 !isdigit ((int) *SubBuffer))
499 * Form <UpperCase><LowerCaseLetters><UpperCase> (AxxB):
500 * Convert leading character of the token to lower case
504 *TokenStart = (char) tolower ((int) *TokenStart);
508 /* Find the end of this identifier (token) */
510 TokenEnd = SubBuffer - 1;
511 while ((isalnum ((int) *TokenEnd)) ||
517 SubString = TokenEnd;
520 while (*SubString != '\n')
523 * If we have at least two trailing spaces, we can get rid of
524 * one to make up for the newly inserted underscore. This will
525 * help preserve the alignment of the text
527 if ((SubString[0] == ' ') &&
528 (SubString[1] == ' '))
530 Length = SubString - SubBuffer - 1;
539 Length = strlen (&SubBuffer[0]);
543 * Within this identifier, convert this pair of letters that
546 * <LowerCase><UpperCase>
548 * <LowerCase><Underscore><LowerCase>
550 Gbl_MadeChanges = TRUE;
552 /* Insert the underscore */
554 memmove (&SubBuffer[1], &SubBuffer[0], Length + 1);
558 * If we have <UpperCase><UpperCase>, leave them as-is
559 * Enables transforms like:
560 * LocalFADT -> local_FADT
562 if (isupper ((int) SubBuffer[2]))
568 /* Lower case the original upper case letter */
570 SubBuffer[1] = (char) tolower ((int) SubBuffer[1]);
580 /******************************************************************************
582 * FUNCTION: AsLowerCaseIdentifiers
584 * DESCRIPTION: Converts mixed case identifiers to lower case. Leaves comments,
585 * quoted strings, and all-upper-case macros alone.
587 ******************************************************************************/
590 AsLowerCaseIdentifiers (
593 char *SubBuffer = Buffer;
599 * Check for translation escape string -- means to ignore
600 * blocks of code while replacing
602 if ((SubBuffer[0] == '/') &&
603 (SubBuffer[1] == '*') &&
604 (SubBuffer[2] == '!'))
606 SubBuffer = strstr (SubBuffer, "!*/");
613 /* Ignore comments */
615 if ((SubBuffer[0] == '/') &&
616 (SubBuffer[1] == '*'))
618 SubBuffer = strstr (SubBuffer, "*/");
627 /* Ignore quoted strings */
629 if ((SubBuffer[0] == '\"') && (SubBuffer[1] != '\''))
633 /* Find the closing quote */
637 /* Ignore escaped quote characters */
639 if (SubBuffer[0] == '\\')
643 else if (SubBuffer[0] == '\"')
659 * Only lower case if we have an upper followed by a lower
660 * This leaves the all-uppercase things (macros, etc.) intact
662 if ((isupper ((int) SubBuffer[0])) &&
663 (islower ((int) SubBuffer[1])))
665 Gbl_MadeChanges = TRUE;
666 *SubBuffer = (char) tolower ((int) *SubBuffer);
674 /******************************************************************************
676 * FUNCTION: AsUppercaseTokens
678 * DESCRIPTION: Force to uppercase all tokens that begin with the prefix string.
679 * used to convert mixed-case macros and constants to uppercase.
681 ******************************************************************************/
699 SubBuffer = strstr (SubBuffer, PrefixString);
702 TokenEnd = SubBuffer;
703 while ((isalnum ((int) *TokenEnd)) || (*TokenEnd == '_'))
708 for (i = 0; i < (TokenEnd - SubBuffer); i++)
710 if ((islower ((int) SubBuffer[i])) &&
711 (isupper ((int) SubBuffer[i+1])))
714 SubString = TokenEnd;
717 while (*SubString != '\n')
719 if ((SubString[0] == ' ') &&
720 (SubString[1] == ' '))
722 Length = SubString - &SubBuffer[i] - 2;
731 Length = strlen (&SubBuffer[i+1]);
734 memmove (&SubBuffer[i+2], &SubBuffer[i+1], (Length+1));
735 SubBuffer[i+1] = '_';
741 for (i = 0; i < (TokenEnd - SubBuffer); i++)
743 SubBuffer[i] = (char) toupper ((int) SubBuffer[i]);
746 SubBuffer = TokenEnd;