Home | History | Annotate | Line # | Download | only in acpisrc
asremove.c revision 1.1.1.1
      1 
      2 /******************************************************************************
      3  *
      4  * Module Name: asremove - Source conversion - removal functions
      5  *
      6  *****************************************************************************/
      7 
      8 /******************************************************************************
      9  *
     10  * 1. Copyright Notice
     11  *
     12  * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
     13  * All rights reserved.
     14  *
     15  * 2. License
     16  *
     17  * 2.1. This is your license from Intel Corp. under its intellectual property
     18  * rights.  You may have additional license terms from the party that provided
     19  * you this software, covering your right to use that party's intellectual
     20  * property rights.
     21  *
     22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
     23  * copy of the source code appearing in this file ("Covered Code") an
     24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
     25  * base code distributed originally by Intel ("Original Intel Code") to copy,
     26  * make derivatives, distribute, use and display any portion of the Covered
     27  * Code in any form, with the right to sublicense such rights; and
     28  *
     29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
     30  * license (with the right to sublicense), under only those claims of Intel
     31  * patents that are infringed by the Original Intel Code, to make, use, sell,
     32  * offer to sell, and import the Covered Code and derivative works thereof
     33  * solely to the minimum extent necessary to exercise the above copyright
     34  * license, and in no event shall the patent license extend to any additions
     35  * to or modifications of the Original Intel Code.  No other license or right
     36  * is granted directly or by implication, estoppel or otherwise;
     37  *
     38  * The above copyright and patent license is granted only if the following
     39  * conditions are met:
     40  *
     41  * 3. Conditions
     42  *
     43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
     44  * Redistribution of source code of any substantial portion of the Covered
     45  * Code or modification with rights to further distribute source must include
     46  * the above Copyright Notice, the above License, this list of Conditions,
     47  * and the following Disclaimer and Export Compliance provision.  In addition,
     48  * Licensee must cause all Covered Code to which Licensee contributes to
     49  * contain a file documenting the changes Licensee made to create that Covered
     50  * Code and the date of any change.  Licensee must include in that file the
     51  * documentation of any changes made by any predecessor Licensee.  Licensee
     52  * must include a prominent statement that the modification is derived,
     53  * directly or indirectly, from Original Intel Code.
     54  *
     55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
     56  * Redistribution of source code of any substantial portion of the Covered
     57  * Code or modification without rights to further distribute source must
     58  * include the following Disclaimer and Export Compliance provision in the
     59  * documentation and/or other materials provided with distribution.  In
     60  * addition, Licensee may not authorize further sublicense of source of any
     61  * portion of the Covered Code, and must include terms to the effect that the
     62  * license from Licensee to its licensee is limited to the intellectual
     63  * property embodied in the software Licensee provides to its licensee, and
     64  * not to intellectual property embodied in modifications its licensee may
     65  * make.
     66  *
     67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
     68  * substantial portion of the Covered Code or modification must reproduce the
     69  * above Copyright Notice, and the following Disclaimer and Export Compliance
     70  * provision in the documentation and/or other materials provided with the
     71  * distribution.
     72  *
     73  * 3.4. Intel retains all right, title, and interest in and to the Original
     74  * Intel Code.
     75  *
     76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
     77  * Intel shall be used in advertising or otherwise to promote the sale, use or
     78  * other dealings in products derived from or relating to the Covered Code
     79  * without prior written authorization from Intel.
     80  *
     81  * 4. Disclaimer and Export Compliance
     82  *
     83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
     84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
     85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
     86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
     87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
     88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
     89  * PARTICULAR PURPOSE.
     90  *
     91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
     92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
     93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
     94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
     95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
     96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
     97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
     98  * LIMITED REMEDY.
     99  *
    100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
    101  * software or system incorporating such software without first obtaining any
    102  * required license or other approval from the U. S. Department of Commerce or
    103  * any other agency or department of the United States Government.  In the
    104  * event Licensee exports any such software from the United States or
    105  * re-exports any such software from a foreign destination, Licensee shall
    106  * ensure that the distribution and export/re-export of the software is in
    107  * compliance with all laws, regulations, orders, or other restrictions of the
    108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
    109  * any of its subsidiaries will export/re-export any technical data, process,
    110  * software, or service, directly or indirectly, to any country for which the
    111  * United States government or any agency thereof requires an export license,
    112  * other governmental approval, or letter of assurance, without first obtaining
    113  * such license, approval or letter.
    114  *
    115  *****************************************************************************/
    116 
    117 #include "acpisrc.h"
    118 
    119 /* Local prototypes */
    120 
    121 void
    122 AsRemoveStatement (
    123     char                    *Buffer,
    124     char                    *Keyword,
    125     UINT32                  Type);
    126 
    127 
    128 /******************************************************************************
    129  *
    130  * FUNCTION:    AsRemoveStatement
    131  *
    132  * DESCRIPTION: Remove all statements that contain the given keyword.
    133  *              Limitations:  Removes text from the start of the line that
    134  *              contains the keyword to the next semicolon.  Currently
    135  *              doesn't ignore comments.
    136  *
    137  ******************************************************************************/
    138 
    139 void
    140 AsRemoveStatement (
    141     char                    *Buffer,
    142     char                    *Keyword,
    143     UINT32                  Type)
    144 {
    145     char                    *SubString;
    146     char                    *SubBuffer;
    147     int                     KeywordLength;
    148 
    149 
    150     KeywordLength = strlen (Keyword);
    151     SubBuffer = Buffer;
    152     SubString = Buffer;
    153 
    154 
    155     while (SubString)
    156     {
    157         SubString = strstr (SubBuffer, Keyword);
    158 
    159         if (SubString)
    160         {
    161             SubBuffer = SubString;
    162 
    163             if ((Type == REPLACE_WHOLE_WORD) &&
    164                 (!AsMatchExactWord (SubString, KeywordLength)))
    165             {
    166                 SubBuffer++;
    167                 continue;
    168             }
    169 
    170             /* Find start of this line */
    171 
    172             while (*SubString != '\n')
    173             {
    174                 SubString--;
    175             }
    176             SubString++;
    177 
    178             /* Find end of this statement */
    179 
    180             SubBuffer = AsSkipPastChar (SubBuffer, ';');
    181             if (!SubBuffer)
    182             {
    183                 return;
    184             }
    185 
    186             /* Find end of this line */
    187 
    188             SubBuffer = AsSkipPastChar (SubBuffer, '\n');
    189             if (!SubBuffer)
    190             {
    191                 return;
    192             }
    193 
    194             /* If next line is blank, remove it too */
    195 
    196             if (*SubBuffer == '\n')
    197             {
    198                 SubBuffer++;
    199             }
    200 
    201             /* Remove the lines */
    202 
    203             SubBuffer = AsRemoveData (SubString, SubBuffer);
    204         }
    205     }
    206 }
    207 
    208 
    209 /******************************************************************************
    210  *
    211  * FUNCTION:    AsRemoveConditionalCompile
    212  *
    213  * DESCRIPTION: Remove a "#ifdef" statement, and all text that it encompasses.
    214  *              Limitations: cannot handle nested ifdefs.
    215  *
    216  ******************************************************************************/
    217 
    218 void
    219 AsRemoveConditionalCompile (
    220     char                    *Buffer,
    221     char                    *Keyword)
    222 {
    223     char                    *SubString;
    224     char                    *SubBuffer;
    225     char                    *IfPtr;
    226     char                    *EndifPtr;
    227     char                    *ElsePtr;
    228     char                    *Comment;
    229     int                     KeywordLength;
    230 
    231 
    232     KeywordLength = strlen (Keyword);
    233     SubBuffer = Buffer;
    234     SubString = Buffer;
    235 
    236 
    237     while (SubString)
    238     {
    239         SubBuffer = strstr (SubString, Keyword);
    240         if (!SubBuffer)
    241         {
    242             return;
    243         }
    244 
    245         /*
    246          * Check for translation escape string -- means to ignore
    247          * blocks of code while replacing
    248          */
    249         Comment = strstr (SubString, AS_START_IGNORE);
    250 
    251         if ((Comment) &&
    252             (Comment < SubBuffer))
    253         {
    254             SubString = strstr (Comment, AS_STOP_IGNORE);
    255             if (!SubString)
    256             {
    257                 return;
    258             }
    259 
    260             SubString += 3;
    261             continue;
    262         }
    263 
    264         /* Check for ordinary comment */
    265 
    266         Comment = strstr (SubString, "/*");
    267 
    268         if ((Comment) &&
    269             (Comment < SubBuffer))
    270         {
    271             SubString = strstr (Comment, "*/");
    272             if (!SubString)
    273             {
    274                 return;
    275             }
    276 
    277             SubString += 2;
    278             continue;
    279         }
    280 
    281         SubString = SubBuffer;
    282         if (!AsMatchExactWord (SubString, KeywordLength))
    283         {
    284             SubString++;
    285             continue;
    286         }
    287 
    288         /* Find start of this line */
    289 
    290         while (*SubString != '\n' && (SubString > Buffer))
    291         {
    292             SubString--;
    293         }
    294         SubString++;
    295 
    296         /* Find the "#ifxxxx" */
    297 
    298         IfPtr = strstr (SubString, "#if");
    299         if (!IfPtr)
    300         {
    301             return;
    302         }
    303 
    304         if (IfPtr > SubBuffer)
    305         {
    306             /* Not the right #if */
    307 
    308             SubString = SubBuffer + strlen (Keyword);
    309             continue;
    310         }
    311 
    312         /* Find closing #endif or #else */
    313 
    314         EndifPtr = strstr (SubBuffer, "#endif");
    315         if (!EndifPtr)
    316         {
    317             /* There has to be an #endif */
    318 
    319             return;
    320         }
    321 
    322         ElsePtr = strstr (SubBuffer, "#else");
    323         if ((ElsePtr) &&
    324             (EndifPtr > ElsePtr))
    325         {
    326             /* This #ifdef contains an #else clause */
    327             /* Find end of this line */
    328 
    329             SubBuffer = AsSkipPastChar (ElsePtr, '\n');
    330             if (!SubBuffer)
    331             {
    332                 return;
    333             }
    334 
    335             /* Remove the #ifdef .... #else code */
    336 
    337             AsRemoveData (SubString, SubBuffer);
    338 
    339             /* Next, we will remove the #endif statement */
    340 
    341             EndifPtr = strstr (SubString, "#endif");
    342             if (!EndifPtr)
    343             {
    344                 /* There has to be an #endif */
    345 
    346                 return;
    347             }
    348 
    349             SubString = EndifPtr;
    350         }
    351 
    352         /* Remove the ... #endif part */
    353         /* Find end of this line */
    354 
    355         SubBuffer = AsSkipPastChar (EndifPtr, '\n');
    356         if (!SubBuffer)
    357         {
    358             return;
    359         }
    360 
    361         /* Remove the lines */
    362 
    363         SubBuffer = AsRemoveData (SubString, SubBuffer);
    364     }
    365 }
    366 
    367 
    368 /******************************************************************************
    369  *
    370  * FUNCTION:    AsRemoveMacro
    371  *
    372  * DESCRIPTION: Remove every line that contains the keyword.  Does not
    373  *              skip comments.
    374  *
    375  ******************************************************************************/
    376 
    377 void
    378 AsRemoveMacro (
    379     char                    *Buffer,
    380     char                    *Keyword)
    381 {
    382     char                    *SubString;
    383     char                    *SubBuffer;
    384     int                     NestLevel;
    385 
    386 
    387     SubBuffer = Buffer;
    388     SubString = Buffer;
    389 
    390 
    391     while (SubString)
    392     {
    393         SubString = strstr (SubBuffer, Keyword);
    394 
    395         if (SubString)
    396         {
    397             SubBuffer = SubString;
    398 
    399             /* Find start of the macro parameters */
    400 
    401             while (*SubString != '(')
    402             {
    403                 SubString++;
    404             }
    405             SubString++;
    406 
    407             /* Remove the macro name and opening paren */
    408 
    409             SubString = AsRemoveData (SubBuffer, SubString);
    410 
    411             NestLevel = 1;
    412             while (*SubString)
    413             {
    414                 if (*SubString == '(')
    415                 {
    416                     NestLevel++;
    417                 }
    418                 else if (*SubString == ')')
    419                 {
    420                     NestLevel--;
    421                 }
    422 
    423                 SubString++;
    424 
    425                 if (NestLevel == 0)
    426                 {
    427                     break;
    428                 }
    429             }
    430 
    431             /* Remove the closing paren */
    432 
    433             SubBuffer = AsRemoveData (SubString-1, SubString);
    434         }
    435     }
    436 }
    437 
    438 
    439 /******************************************************************************
    440  *
    441  * FUNCTION:    AsRemoveLine
    442  *
    443  * DESCRIPTION: Remove every line that contains the keyword.  Does not
    444  *              skip comments.
    445  *
    446  ******************************************************************************/
    447 
    448 void
    449 AsRemoveLine (
    450     char                    *Buffer,
    451     char                    *Keyword)
    452 {
    453     char                    *SubString;
    454     char                    *SubBuffer;
    455 
    456 
    457     SubBuffer = Buffer;
    458     SubString = Buffer;
    459 
    460 
    461     while (SubString)
    462     {
    463         SubString = strstr (SubBuffer, Keyword);
    464 
    465         if (SubString)
    466         {
    467             SubBuffer = SubString;
    468 
    469             /* Find start of this line */
    470 
    471             while (*SubString != '\n')
    472             {
    473                 SubString--;
    474             }
    475             SubString++;
    476 
    477             /* Find end of this line */
    478 
    479             SubBuffer = AsSkipPastChar (SubBuffer, '\n');
    480             if (!SubBuffer)
    481             {
    482                 return;
    483             }
    484 
    485             /* Remove the line */
    486 
    487             SubBuffer = AsRemoveData (SubString, SubBuffer);
    488         }
    489     }
    490 }
    491 
    492 
    493 /******************************************************************************
    494  *
    495  * FUNCTION:    AsReduceTypedefs
    496  *
    497  * DESCRIPTION: Eliminate certain typedefs
    498  *
    499  ******************************************************************************/
    500 
    501 void
    502 AsReduceTypedefs (
    503     char                    *Buffer,
    504     char                    *Keyword)
    505 {
    506     char                    *SubString;
    507     char                    *SubBuffer;
    508     int                     NestLevel;
    509 
    510 
    511     SubBuffer = Buffer;
    512     SubString = Buffer;
    513 
    514 
    515     while (SubString)
    516     {
    517         SubString = strstr (SubBuffer, Keyword);
    518 
    519         if (SubString)
    520         {
    521             /* Remove the typedef itself */
    522 
    523             SubBuffer = SubString + strlen ("typedef") + 1;
    524             SubBuffer = AsRemoveData (SubString, SubBuffer);
    525 
    526             /* Find the opening brace of the struct or union */
    527 
    528             while (*SubString != '{')
    529             {
    530                 SubString++;
    531             }
    532             SubString++;
    533 
    534             /* Find the closing brace.  Handles nested braces */
    535 
    536             NestLevel = 1;
    537             while (*SubString)
    538             {
    539                 if (*SubString == '{')
    540                 {
    541                     NestLevel++;
    542                 }
    543                 else if (*SubString == '}')
    544                 {
    545                     NestLevel--;
    546                 }
    547 
    548                 SubString++;
    549 
    550                 if (NestLevel == 0)
    551                 {
    552                     break;
    553                 }
    554             }
    555 
    556             /* Remove an extra line feed if present */
    557 
    558             if (!strncmp (SubString - 3, "\n\n", 2))
    559             {
    560                 *(SubString -2) = '}';
    561                 SubString--;
    562             }
    563 
    564             /* Find the end of the typedef name */
    565 
    566             SubBuffer = AsSkipUntilChar (SubString, ';');
    567 
    568             /* And remove the typedef name */
    569 
    570             SubBuffer = AsRemoveData (SubString, SubBuffer);
    571         }
    572     }
    573 }
    574 
    575 
    576 /******************************************************************************
    577  *
    578  * FUNCTION:    AsRemoveEmptyBlocks
    579  *
    580  * DESCRIPTION: Remove any C blocks (e.g., if {}) that contain no code.  This
    581  *              can happen as a result of removing lines such as DEBUG_PRINT.
    582  *
    583  ******************************************************************************/
    584 
    585 void
    586 AsRemoveEmptyBlocks (
    587     char                    *Buffer,
    588     char                    *Filename)
    589 {
    590     char                    *SubBuffer;
    591     char                    *BlockStart;
    592     BOOLEAN                 EmptyBlock = TRUE;
    593     BOOLEAN                 AnotherPassRequired = TRUE;
    594     UINT32                  BlockCount = 0;
    595 
    596 
    597     while (AnotherPassRequired)
    598     {
    599         SubBuffer = Buffer;
    600         AnotherPassRequired = FALSE;
    601 
    602         while (*SubBuffer)
    603         {
    604             if (*SubBuffer == '{')
    605             {
    606                 BlockStart = SubBuffer;
    607                 EmptyBlock = TRUE;
    608 
    609                 SubBuffer++;
    610                 while (*SubBuffer != '}')
    611                 {
    612                     if ((*SubBuffer != ' ') &&
    613                         (*SubBuffer != '\n'))
    614                     {
    615                         EmptyBlock = FALSE;
    616                         break;
    617                     }
    618                     SubBuffer++;
    619                 }
    620 
    621                 if (EmptyBlock)
    622                 {
    623                     /* Find start of the first line of the block */
    624 
    625                     while (*BlockStart != '\n')
    626                     {
    627                         BlockStart--;
    628                     }
    629 
    630                     /* Find end of the last line of the block */
    631 
    632                     SubBuffer = AsSkipUntilChar (SubBuffer, '\n');
    633                     if (!SubBuffer)
    634                     {
    635                         break;
    636                     }
    637 
    638                     /* Remove the block */
    639 
    640                     SubBuffer = AsRemoveData (BlockStart, SubBuffer);
    641                     BlockCount++;
    642                     AnotherPassRequired = TRUE;
    643                     continue;
    644                 }
    645             }
    646 
    647             SubBuffer++;
    648         }
    649     }
    650 
    651     if (BlockCount)
    652     {
    653         Gbl_MadeChanges = TRUE;
    654         AsPrint ("Code blocks deleted", BlockCount, Filename);
    655     }
    656 }
    657 
    658 
    659 /******************************************************************************
    660  *
    661  * FUNCTION:    AsRemoveDebugMacros
    662  *
    663  * DESCRIPTION: Remove all "Debug" macros -- macros that produce debug output.
    664  *
    665  ******************************************************************************/
    666 
    667 void
    668 AsRemoveDebugMacros (
    669     char                    *Buffer)
    670 {
    671     AsRemoveConditionalCompile (Buffer, "ACPI_DEBUG_OUTPUT");
    672 
    673     AsRemoveStatement (Buffer, "ACPI_DEBUG_PRINT",      REPLACE_WHOLE_WORD);
    674     AsRemoveStatement (Buffer, "ACPI_DEBUG_PRINT_RAW",  REPLACE_WHOLE_WORD);
    675     AsRemoveStatement (Buffer, "DEBUG_EXEC",            REPLACE_WHOLE_WORD);
    676     AsRemoveStatement (Buffer, "FUNCTION_ENTRY",        REPLACE_WHOLE_WORD);
    677     AsRemoveStatement (Buffer, "PROC_NAME",             REPLACE_WHOLE_WORD);
    678     AsRemoveStatement (Buffer, "FUNCTION_TRACE",        REPLACE_SUBSTRINGS);
    679     AsRemoveStatement (Buffer, "DUMP_",                 REPLACE_SUBSTRINGS);
    680 
    681     AsReplaceString ("return_VOID",         "return", REPLACE_WHOLE_WORD, Buffer);
    682     AsReplaceString ("return_PTR",          "return", REPLACE_WHOLE_WORD, Buffer);
    683     AsReplaceString ("return_ACPI_STATUS",  "return", REPLACE_WHOLE_WORD, Buffer);
    684     AsReplaceString ("return_acpi_status",  "return", REPLACE_WHOLE_WORD, Buffer);
    685     AsReplaceString ("return_VALUE",        "return", REPLACE_WHOLE_WORD, Buffer);
    686 }
    687 
    688 
    689