Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / tools / acpisrc / ascase.c
blob919029a2cffb4802337bf28f29c6eea410f42f6b
1 /******************************************************************************
3 * Module Name: ascase - Source conversion - lower/upper case utilities
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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.
44 #include "acpisrc.h"
46 /* Local prototypes */
48 void
49 AsUppercaseTokens (
50 char *Buffer,
51 char *PrefixString);
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 ******************************************************************************/
63 int
64 AsLowerCaseString (
65 char *Target,
66 char *Buffer)
68 char *SubString1;
69 char *SubString2;
70 char *SubBuffer;
71 int TargetLength;
72 int LowerCaseCount = 0;
73 int i;
76 TargetLength = strlen (Target);
78 SubBuffer = Buffer;
79 SubString1 = Buffer;
81 while (SubString1)
83 /* Find the target string */
85 SubString1 = strstr (SubBuffer, Target);
86 if (!SubString1)
88 return (LowerCaseCount);
92 * Check for translation escape string -- means to ignore
93 * blocks of code while replacing
95 if (Gbl_IgnoreTranslationEscapes)
97 SubString2 = NULL;
99 else
101 SubString2 = strstr (SubBuffer, AS_START_IGNORE);
104 if ((SubString2) &&
105 (SubString2 < SubString1))
107 /* Find end of the escape block starting at "Substring2" */
109 SubString2 = strstr (SubString2, AS_STOP_IGNORE);
110 if (!SubString2)
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 */
124 else
126 if (!AsMatchExactWord (SubString1, TargetLength))
128 SubBuffer = SubString1 + 1;
129 continue;
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);
147 LowerCaseCount++;
151 return (LowerCaseCount);
155 /******************************************************************************
157 * FUNCTION: AsMixedCaseToUnderscores
159 * DESCRIPTION: Converts mixed case identifiers to underscored identifiers.
160 * for example,
162 * ThisUsefullyNamedIdentifier becomes:
164 * this_usefully_named_identifier
166 ******************************************************************************/
168 void
169 AsMixedCaseToUnderscores (
170 char *Buffer,
171 char *Filename)
173 UINT32 Length;
174 char *SubBuffer = Buffer;
175 char *TokenEnd;
176 char *TokenStart = NULL;
177 char *SubString;
178 UINT32 LineNumber = 1;
179 UINT32 Count;
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
193 while (*SubBuffer)
195 if (*SubBuffer == '\n')
197 LineNumber++;
198 SubBuffer++;
199 continue;
202 /* Ignore standard escape sequences (\n, \r, etc.) Not Hex or Octal escapes */
204 if (*SubBuffer == '\\')
206 SubBuffer += 2;
207 continue;
210 /* Ignore single-quoted characters */
212 if (*SubBuffer == '\'')
214 SubBuffer += 3;
215 continue;
218 /* Ignore standard double-quoted strings */
220 if (*SubBuffer == '"')
222 SubBuffer++;
223 Count = 0;
224 while (*SubBuffer != '"')
226 Count++;
227 if ((!*SubBuffer) ||
228 (Count > 8192))
230 printf ("Found an unterminated quoted string!, line %u: %s\n",
231 LineNumber, Filename);
232 return;
235 /* Handle escape sequences */
237 if (*SubBuffer == '\\')
239 SubBuffer++;
242 SubBuffer++;
244 SubBuffer++;
245 continue;
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, "!*/");
257 if (!SubBuffer)
259 printf ("Found an unterminated translation escape!, line %u: %s\n",
260 LineNumber, Filename);
261 return;
263 continue;
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')))
275 SubBuffer += 2;
278 /* Skip over all digits, both decimal and hex */
280 while (isxdigit ((int) *SubBuffer))
282 SubBuffer++;
284 TokenStart = NULL;
285 continue;
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)) ||
297 (*SubString == '_'))
299 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
306 * identifier.
308 if (!islower ((int) *SubString))
310 SubBuffer = SubString + 1;
311 continue;
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;
324 SubBuffer++;
326 while (1)
328 /* Walk over the lower case letters and decimal digits */
330 while (islower ((int) *SubBuffer) ||
331 isdigit ((int) *SubBuffer))
333 SubBuffer++;
336 /* Check for end of line or end of token */
338 if (*SubBuffer == '\n')
340 LineNumber++;
341 break;
344 if (*SubBuffer == ' ')
346 /* Check for form "Axx - " in a parameter header description */
348 while (*SubBuffer == ' ')
350 SubBuffer++;
353 SubBuffer--;
354 if ((SubBuffer[1] == '-') &&
355 (SubBuffer[2] == ' '))
357 if (TokenStart)
359 *TokenStart = (char) tolower ((int) *TokenStart);
362 break;
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) == '_')
376 break;
380 /* Ignore token if next character is not uppercase or digit */
382 if (!isupper ((int) *SubBuffer) &&
383 !isdigit ((int) *SubBuffer))
385 break;
389 * Form <UpperCase><LowerCaseLetters><UpperCase> (AxxB):
390 * Convert leading character of the token to lower case
392 if (TokenStart)
394 *TokenStart = (char) tolower ((int) *TokenStart);
395 TokenStart = NULL;
398 /* Find the end of this identifier (token) */
400 TokenEnd = SubBuffer - 1;
401 while ((isalnum ((int) *TokenEnd)) ||
402 (*TokenEnd == '_'))
404 TokenEnd++;
407 SubString = TokenEnd;
408 Length = 0;
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;
421 break;
424 SubString++;
427 if (!Length)
429 Length = strlen (&SubBuffer[0]);
433 * Within this identifier, convert this pair of letters that
434 * matches the form:
436 * <LowerCase><UpperCase>
437 * to
438 * <LowerCase><Underscore><LowerCase>
440 Gbl_MadeChanges = TRUE;
442 /* Insert the underscore */
444 memmove (&SubBuffer[1], &SubBuffer[0], Length + 1);
445 SubBuffer[0] = '_';
448 * If we have <UpperCase><UpperCase>, leave them as-is
449 * Enables transforms like:
450 * LocalFADT -> local_FADT
452 if (isupper ((int) SubBuffer[2]))
454 SubBuffer += 1;
455 break;
458 /* Lower case the original upper case letter */
460 SubBuffer[1] = (char) tolower ((int) SubBuffer[1]);
461 SubBuffer += 2;
465 SubBuffer++;
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 ******************************************************************************/
479 void
480 AsLowerCaseIdentifiers (
481 char *Buffer)
483 char *SubBuffer = Buffer;
486 while (*SubBuffer)
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, "!*/");
497 if (!SubBuffer)
499 return;
503 /* Ignore comments */
505 if ((SubBuffer[0] == '/') &&
506 (SubBuffer[1] == '*'))
508 SubBuffer = strstr (SubBuffer, "*/");
509 if (!SubBuffer)
511 return;
514 SubBuffer += 2;
517 /* Ignore quoted strings */
519 if ((SubBuffer[0] == '\"') && (SubBuffer[1] != '\''))
521 SubBuffer++;
523 /* Find the closing quote */
525 while (SubBuffer[0])
527 /* Ignore escaped quote characters */
529 if (SubBuffer[0] == '\\')
531 SubBuffer++;
533 else if (SubBuffer[0] == '\"')
535 SubBuffer++;
536 break;
538 SubBuffer++;
542 if (!SubBuffer[0])
544 return;
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);
558 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 ******************************************************************************/
572 void
573 AsUppercaseTokens (
574 char *Buffer,
575 char *PrefixString)
577 char *SubBuffer;
578 char *TokenEnd;
579 char *SubString;
580 int i;
581 UINT32 Length;
584 SubBuffer = Buffer;
586 while (SubBuffer)
588 SubBuffer = strstr (SubBuffer, PrefixString);
589 if (SubBuffer)
591 TokenEnd = SubBuffer;
592 while ((isalnum ((int) *TokenEnd)) || (*TokenEnd == '_'))
594 TokenEnd++;
597 for (i = 0; i < (TokenEnd - SubBuffer); i++)
599 if ((islower ((int) SubBuffer[i])) &&
600 (isupper ((int) SubBuffer[i+1])))
603 SubString = TokenEnd;
604 Length = 0;
606 while (*SubString != '\n')
608 if ((SubString[0] == ' ') &&
609 (SubString[1] == ' '))
611 Length = SubString - &SubBuffer[i] - 2;
612 break;
615 SubString++;
618 if (!Length)
620 Length = strlen (&SubBuffer[i+1]);
623 memmove (&SubBuffer[i+2], &SubBuffer[i+1], (Length+1));
624 SubBuffer[i+1] = '_';
625 i +=2;
626 TokenEnd++;
630 for (i = 0; i < (TokenEnd - SubBuffer); i++)
632 SubBuffer[i] = (char) toupper ((int) SubBuffer[i]);
635 SubBuffer = TokenEnd;