1
2/******************************************************************************
3 *
4 * Module Name: ascase - Source conversion - lower/upper case utilities
5 *
6 *****************************************************************************/
7
8/*
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions, and the following disclaimer,
17 *    without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 *    substantially similar to the "NO WARRANTY" disclaimer below
20 *    ("Disclaimer") and any redistribution must be conditioned upon
21 *    including a substantially similar Disclaimer requirement for further
22 *    binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 *    of any contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
45#include "acpisrc.h"
46
47/* Local prototypes */
48
49void
50AsUppercaseTokens (
51    char                    *Buffer,
52    char                    *PrefixString);
53
54
55/******************************************************************************
56 *
57 * FUNCTION:    AsLowerCaseString
58 *
59 * DESCRIPTION: LowerCase all instances of a target string with a replacement
60 *              string.  Returns count of the strings replaced.
61 *
62 ******************************************************************************/
63
64int
65AsLowerCaseString (
66    char                    *Target,
67    char                    *Buffer)
68{
69    char                    *SubString1;
70    char                    *SubString2;
71    char                    *SubBuffer;
72    int                     TargetLength;
73    int                     LowerCaseCount = 0;
74    int                     i;
75
76
77    TargetLength = strlen (Target);
78
79    SubBuffer = Buffer;
80    SubString1 = Buffer;
81
82    while (SubString1)
83    {
84        /* Find the target string */
85
86        SubString1 = strstr (SubBuffer, Target);
87        if (!SubString1)
88        {
89            return LowerCaseCount;
90        }
91
92        /*
93         * Check for translation escape string -- means to ignore
94         * blocks of code while replacing
95         */
96        SubString2 = strstr (SubBuffer, AS_START_IGNORE);
97
98        if ((SubString2) &&
99            (SubString2 < SubString1))
100        {
101            /* Find end of the escape block starting at "Substring2" */
102
103            SubString2 = strstr (SubString2, AS_STOP_IGNORE);
104            if (!SubString2)
105            {
106                /* Didn't find terminator */
107
108                return LowerCaseCount;
109            }
110
111            /* Move buffer to end of escape block and continue */
112
113            SubBuffer = SubString2;
114        }
115
116        /* Do the actual replace if the target was found */
117
118        else
119        {
120            if (!AsMatchExactWord (SubString1, TargetLength))
121            {
122                SubBuffer = SubString1 + 1;
123                continue;
124            }
125
126            for (i = 0; i < TargetLength; i++)
127            {
128                SubString1[i] = (char) tolower ((int) SubString1[i]);
129            }
130
131            SubBuffer = SubString1 + TargetLength;
132
133            if ((Gbl_WidenDeclarations) && (!Gbl_StructDefs))
134            {
135                if ((SubBuffer[0] == ' ') && (SubBuffer[1] == ' '))
136                {
137                    AsInsertData (SubBuffer, "        ", 8);
138                }
139            }
140
141            LowerCaseCount++;
142        }
143    }
144
145    return LowerCaseCount;
146}
147
148
149/******************************************************************************
150 *
151 * FUNCTION:    AsMixedCaseToUnderscores
152 *
153 * DESCRIPTION: Converts mixed case identifiers to underscored identifiers.
154 *              for example,
155 *
156 *              ThisUsefullyNamedIdentifier   becomes:
157 *
158 *              this_usefully_named_identifier
159 *
160 ******************************************************************************/
161
162void
163AsMixedCaseToUnderscores (
164    char                    *Buffer)
165{
166    UINT32                  Length;
167    char                    *SubBuffer = Buffer;
168    char                    *TokenEnd;
169    char                    *TokenStart = NULL;
170    char                    *SubString;
171    BOOLEAN                 HasLowerCase = FALSE;
172
173
174    while (*SubBuffer)
175    {
176        /* Ignore whitespace */
177
178        if (*SubBuffer == ' ')
179        {
180            while (*SubBuffer == ' ')
181            {
182                SubBuffer++;
183            }
184            TokenStart = NULL;
185            HasLowerCase = FALSE;
186            continue;
187        }
188
189        /* Ignore commas */
190
191        if ((*SubBuffer == ',') ||
192            (*SubBuffer == '>') ||
193            (*SubBuffer == ')'))
194        {
195            SubBuffer++;
196            TokenStart = NULL;
197            HasLowerCase = FALSE;
198            continue;
199        }
200
201        /* Check for quoted string -- ignore */
202
203        if (*SubBuffer == '"')
204        {
205            SubBuffer++;
206            while (*SubBuffer != '"')
207            {
208                if (!*SubBuffer)
209                {
210                    return;
211                }
212
213                /* Handle embedded escape sequences */
214
215                if (*SubBuffer == '\\')
216                {
217                    SubBuffer++;
218                }
219                SubBuffer++;
220            }
221            SubBuffer++;
222            continue;
223        }
224
225        if (islower ((int) *SubBuffer))
226        {
227            HasLowerCase = TRUE;
228        }
229
230        /*
231         * Check for translation escape string -- means to ignore
232         * blocks of code while replacing
233         */
234        if ((SubBuffer[0] == '/') &&
235            (SubBuffer[1] == '*') &&
236            (SubBuffer[2] == '!'))
237        {
238            SubBuffer = strstr (SubBuffer, "!*/");
239            if (!SubBuffer)
240            {
241                return;
242            }
243            continue;
244        }
245
246        /* Ignore hex constants */
247
248        if (SubBuffer[0] == '0')
249        {
250            if ((SubBuffer[1] == 'x') ||
251                (SubBuffer[1] == 'X'))
252            {
253                SubBuffer += 2;
254                while (isxdigit ((int) *SubBuffer))
255                {
256                    SubBuffer++;
257                }
258                continue;
259            }
260        }
261
262/* OBSOLETE CODE, all quoted strings now completely ignored. */
263#if 0
264        /* Ignore format specification fields */
265
266        if (SubBuffer[0] == '%')
267        {
268            SubBuffer++;
269
270            while ((isalnum (*SubBuffer)) || (*SubBuffer == '.'))
271            {
272                SubBuffer++;
273            }
274
275            continue;
276        }
277#endif
278
279        /* Ignore standard escape sequences (\n, \r, etc.)  Not Hex or Octal escapes */
280
281        if (SubBuffer[0] == '\\')
282        {
283            SubBuffer += 2;
284            continue;
285        }
286
287        /*
288         * Ignore identifiers that already contain embedded underscores
289         * These are typically C macros or defines (all upper case)
290         * Note: there are some cases where identifiers have underscores
291         * AcpiGbl_* for example. HasLowerCase flag handles these.
292         */
293        if ((*SubBuffer == '_') && (!HasLowerCase) && (TokenStart))
294        {
295            /* Check the rest of the identifier for any lower case letters */
296
297            SubString = SubBuffer;
298            while ((isalnum ((int) *SubString)) || (*SubString == '_'))
299            {
300                if (islower ((int) *SubString))
301                {
302                    HasLowerCase = TRUE;
303                }
304                SubString++;
305            }
306
307            /* If no lower case letters, we can safely ignore the entire token */
308
309            if (!HasLowerCase)
310            {
311                SubBuffer = SubString;
312                continue;
313            }
314        }
315
316        /* A capital letter may indicate the start of a token; save it */
317
318        if (isupper ((int) SubBuffer[0]))
319        {
320            TokenStart = SubBuffer;
321        }
322
323        /*
324         * Convert each pair of letters that matches the form:
325         *
326         *      <LowerCase><UpperCase>
327         * to
328         *      <LowerCase><Underscore><LowerCase>
329         */
330        else if ((islower ((int) SubBuffer[0]) || isdigit ((int) SubBuffer[0])) &&
331                 (isupper ((int) SubBuffer[1])))
332        {
333            if (isdigit ((int) SubBuffer[0]))
334            {
335                /* Ignore <UpperCase><Digit><UpperCase> */
336                /* Ignore <Underscore><Digit><UpperCase> */
337
338                if (isupper ((int) *(SubBuffer-1)) ||
339                    *(SubBuffer-1) == '_')
340                {
341                    SubBuffer++;
342                    continue;
343                }
344            }
345
346            /*
347             * Matched the pattern.
348             * Find the end of this identifier (token)
349             */
350            TokenEnd = SubBuffer;
351            while ((isalnum ((int) *TokenEnd)) || (*TokenEnd == '_'))
352            {
353                TokenEnd++;
354            }
355
356            /* Force the UpperCase letter (#2) to lower case */
357
358            Gbl_MadeChanges = TRUE;
359            SubBuffer[1] = (char) tolower ((int) SubBuffer[1]);
360
361            SubString = TokenEnd;
362            Length = 0;
363
364            while (*SubString != '\n')
365            {
366                /*
367                 * If we have at least two trailing spaces, we can get rid of
368                 * one to make up for the newly inserted underscore.  This will
369                 * help preserve the alignment of the text
370                 */
371                if ((SubString[0] == ' ') &&
372                    (SubString[1] == ' '))
373                {
374                    Length = SubString - SubBuffer - 2;
375                    break;
376                }
377
378                SubString++;
379            }
380
381            if (!Length)
382            {
383                Length = strlen (&SubBuffer[1]);
384            }
385
386            memmove (&SubBuffer[2], &SubBuffer[1], Length + 1);
387            SubBuffer[1] = '_';
388            SubBuffer +=2;
389
390            /* Lower case the leading character of the token */
391
392            if (TokenStart)
393            {
394                *TokenStart = (char) tolower ((int) *TokenStart);
395                TokenStart = NULL;
396            }
397        }
398
399        SubBuffer++;
400    }
401}
402
403
404/******************************************************************************
405 *
406 * FUNCTION:    AsLowerCaseIdentifiers
407 *
408 * DESCRIPTION: Converts mixed case identifiers to lower case.  Leaves comments,
409 *              quoted strings, and all-upper-case macros alone.
410 *
411 ******************************************************************************/
412
413void
414AsLowerCaseIdentifiers (
415    char                    *Buffer)
416{
417    char                    *SubBuffer = Buffer;
418
419
420    while (*SubBuffer)
421    {
422        /*
423         * Check for translation escape string -- means to ignore
424         * blocks of code while replacing
425         */
426        if ((SubBuffer[0] == '/') &&
427            (SubBuffer[1] == '*') &&
428            (SubBuffer[2] == '!'))
429        {
430            SubBuffer = strstr (SubBuffer, "!*/");
431            if (!SubBuffer)
432            {
433                return;
434            }
435        }
436
437        /* Ignore comments */
438
439        if ((SubBuffer[0] == '/') &&
440            (SubBuffer[1] == '*'))
441        {
442            SubBuffer = strstr (SubBuffer, "*/");
443            if (!SubBuffer)
444            {
445                return;
446            }
447
448            SubBuffer += 2;
449        }
450
451        /* Ignore quoted strings */
452
453        if ((SubBuffer[0] == '\"') && (SubBuffer[1] != '\''))
454        {
455            SubBuffer++;
456
457            /* Find the closing quote */
458
459            while (SubBuffer[0])
460            {
461                /* Ignore escaped quote characters */
462
463                if (SubBuffer[0] == '\\')
464                {
465                    SubBuffer++;
466                }
467                else if (SubBuffer[0] == '\"')
468                {
469                    SubBuffer++;
470                    break;
471                }
472                SubBuffer++;
473            }
474        }
475
476        if (!SubBuffer[0])
477        {
478            return;
479        }
480
481        /*
482         * Only lower case if we have an upper followed by a lower
483         * This leaves the all-uppercase things (macros, etc.) intact
484         */
485        if ((isupper ((int) SubBuffer[0])) &&
486            (islower ((int) SubBuffer[1])))
487        {
488            Gbl_MadeChanges = TRUE;
489            *SubBuffer = (char) tolower ((int) *SubBuffer);
490        }
491
492        SubBuffer++;
493    }
494}
495
496
497/******************************************************************************
498 *
499 * FUNCTION:    AsUppercaseTokens
500 *
501 * DESCRIPTION: Force to uppercase all tokens that begin with the prefix string.
502 *              used to convert mixed-case macros and constants to uppercase.
503 *
504 ******************************************************************************/
505
506void
507AsUppercaseTokens (
508    char                    *Buffer,
509    char                    *PrefixString)
510{
511    char                    *SubBuffer;
512    char                    *TokenEnd;
513    char                    *SubString;
514    int                     i;
515    UINT32                  Length;
516
517
518    SubBuffer = Buffer;
519
520    while (SubBuffer)
521    {
522        SubBuffer = strstr (SubBuffer, PrefixString);
523        if (SubBuffer)
524        {
525            TokenEnd = SubBuffer;
526            while ((isalnum ((int) *TokenEnd)) || (*TokenEnd == '_'))
527            {
528                TokenEnd++;
529            }
530
531            for (i = 0; i < (TokenEnd - SubBuffer); i++)
532            {
533                if ((islower ((int) SubBuffer[i])) &&
534                    (isupper ((int) SubBuffer[i+1])))
535                {
536
537                    SubString = TokenEnd;
538                    Length = 0;
539
540                    while (*SubString != '\n')
541                    {
542                        if ((SubString[0] == ' ') &&
543                            (SubString[1] == ' '))
544                        {
545                            Length = SubString - &SubBuffer[i] - 2;
546                            break;
547                        }
548
549                        SubString++;
550                    }
551
552                    if (!Length)
553                    {
554                        Length = strlen (&SubBuffer[i+1]);
555                    }
556
557                    memmove (&SubBuffer[i+2], &SubBuffer[i+1], (Length+1));
558                    SubBuffer[i+1] = '_';
559                    i +=2;
560                    TokenEnd++;
561                }
562            }
563
564            for (i = 0; i < (TokenEnd - SubBuffer); i++)
565            {
566                SubBuffer[i] = (char) toupper ((int) SubBuffer[i]);
567            }
568
569            SubBuffer = TokenEnd;
570        }
571    }
572}
573
574
575