Home | History | Annotate | Line # | Download | only in acpisrc
asfile.c revision 1.1.1.4.2.2
      1          1.1    jruoho /******************************************************************************
      2          1.1    jruoho  *
      3          1.1    jruoho  * Module Name: asfile - Main module for the acpi source processor utility
      4          1.1    jruoho  *
      5          1.1    jruoho  *****************************************************************************/
      6          1.1    jruoho 
      7      1.1.1.2    jruoho /*
      8  1.1.1.4.2.1     skrll  * Copyright (C) 2000 - 2015, Intel Corp.
      9          1.1    jruoho  * All rights reserved.
     10          1.1    jruoho  *
     11      1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12      1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13      1.1.1.2    jruoho  * are met:
     14      1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15      1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16      1.1.1.2    jruoho  *    without modification.
     17      1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18      1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19      1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20      1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21      1.1.1.2    jruoho  *    binary redistribution.
     22      1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23      1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24      1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25      1.1.1.2    jruoho  *
     26      1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27      1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28      1.1.1.2    jruoho  * Software Foundation.
     29      1.1.1.2    jruoho  *
     30      1.1.1.2    jruoho  * NO WARRANTY
     31      1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32      1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33      1.1.1.2    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34      1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35      1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36      1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37      1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38      1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39      1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40      1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41      1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42      1.1.1.2    jruoho  */
     43          1.1    jruoho 
     44          1.1    jruoho #include "acpisrc.h"
     45      1.1.1.4  christos #include "acapps.h"
     46          1.1    jruoho 
     47          1.1    jruoho /* Local prototypes */
     48          1.1    jruoho 
     49          1.1    jruoho void
     50          1.1    jruoho AsDoWildcard (
     51          1.1    jruoho     ACPI_CONVERSION_TABLE   *ConversionTable,
     52          1.1    jruoho     char                    *SourcePath,
     53          1.1    jruoho     char                    *TargetPath,
     54          1.1    jruoho     int                     MaxPathLength,
     55          1.1    jruoho     int                     FileType,
     56          1.1    jruoho     char                    *WildcardSpec);
     57          1.1    jruoho 
     58          1.1    jruoho BOOLEAN
     59          1.1    jruoho AsDetectLoneLineFeeds (
     60          1.1    jruoho     char                    *Filename,
     61          1.1    jruoho     char                    *Buffer);
     62          1.1    jruoho 
     63      1.1.1.2    jruoho static ACPI_INLINE int
     64          1.1    jruoho AsMaxInt (int a, int b)
     65          1.1    jruoho {
     66          1.1    jruoho     return (a > b ? a : b);
     67          1.1    jruoho }
     68          1.1    jruoho 
     69          1.1    jruoho 
     70          1.1    jruoho /******************************************************************************
     71          1.1    jruoho  *
     72          1.1    jruoho  * FUNCTION:    AsDoWildcard
     73          1.1    jruoho  *
     74          1.1    jruoho  * DESCRIPTION: Process files via wildcards
     75          1.1    jruoho  *
     76          1.1    jruoho  ******************************************************************************/
     77          1.1    jruoho 
     78          1.1    jruoho void
     79          1.1    jruoho AsDoWildcard (
     80          1.1    jruoho     ACPI_CONVERSION_TABLE   *ConversionTable,
     81          1.1    jruoho     char                    *SourcePath,
     82          1.1    jruoho     char                    *TargetPath,
     83          1.1    jruoho     int                     MaxPathLength,
     84          1.1    jruoho     int                     FileType,
     85          1.1    jruoho     char                    *WildcardSpec)
     86          1.1    jruoho {
     87          1.1    jruoho     void                    *DirInfo;
     88          1.1    jruoho     char                    *Filename;
     89          1.1    jruoho     char                    *SourceDirPath;
     90          1.1    jruoho     char                    *TargetDirPath;
     91          1.1    jruoho     char                    RequestedFileType;
     92          1.1    jruoho 
     93          1.1    jruoho 
     94          1.1    jruoho     if (FileType == FILE_TYPE_DIRECTORY)
     95          1.1    jruoho     {
     96          1.1    jruoho         RequestedFileType = REQUEST_DIR_ONLY;
     97          1.1    jruoho     }
     98          1.1    jruoho     else
     99          1.1    jruoho     {
    100          1.1    jruoho         RequestedFileType = REQUEST_FILE_ONLY;
    101          1.1    jruoho     }
    102          1.1    jruoho 
    103          1.1    jruoho     VERBOSE_PRINT (("Checking for %s source files in directory \"%s\"\n",
    104          1.1    jruoho             WildcardSpec, SourcePath));
    105          1.1    jruoho 
    106          1.1    jruoho     /* Open the directory for wildcard search */
    107          1.1    jruoho 
    108          1.1    jruoho     DirInfo = AcpiOsOpenDirectory (SourcePath, WildcardSpec, RequestedFileType);
    109          1.1    jruoho     if (DirInfo)
    110          1.1    jruoho     {
    111          1.1    jruoho         /*
    112          1.1    jruoho          * Get all of the files that match both the
    113          1.1    jruoho          * wildcard and the requested file type
    114          1.1    jruoho          */
    115          1.1    jruoho         while ((Filename = AcpiOsGetNextFilename (DirInfo)))
    116          1.1    jruoho         {
    117          1.1    jruoho             /* Looking for directory files, must check file type */
    118          1.1    jruoho 
    119          1.1    jruoho             switch (RequestedFileType)
    120          1.1    jruoho             {
    121          1.1    jruoho             case REQUEST_DIR_ONLY:
    122          1.1    jruoho 
    123          1.1    jruoho                 /* If we actually have a dir, process the subtree */
    124          1.1    jruoho 
    125          1.1    jruoho                 if (!AsCheckForDirectory (SourcePath, TargetPath, Filename,
    126          1.1    jruoho                         &SourceDirPath, &TargetDirPath))
    127          1.1    jruoho                 {
    128          1.1    jruoho                     VERBOSE_PRINT (("Subdirectory: %s\n", Filename));
    129          1.1    jruoho 
    130          1.1    jruoho                     AsProcessTree (ConversionTable, SourceDirPath, TargetDirPath);
    131          1.1    jruoho                     free (SourceDirPath);
    132          1.1    jruoho                     free (TargetDirPath);
    133          1.1    jruoho                 }
    134          1.1    jruoho                 break;
    135          1.1    jruoho 
    136          1.1    jruoho             case REQUEST_FILE_ONLY:
    137          1.1    jruoho 
    138          1.1    jruoho                 /* Otherwise, this is a file, not a directory */
    139          1.1    jruoho 
    140          1.1    jruoho                 VERBOSE_PRINT (("File: %s\n", Filename));
    141          1.1    jruoho 
    142          1.1    jruoho                 AsProcessOneFile (ConversionTable, SourcePath, TargetPath,
    143          1.1    jruoho                         MaxPathLength, Filename, FileType);
    144          1.1    jruoho                 break;
    145          1.1    jruoho 
    146          1.1    jruoho             default:
    147      1.1.1.3  christos 
    148          1.1    jruoho                 break;
    149          1.1    jruoho             }
    150          1.1    jruoho         }
    151          1.1    jruoho 
    152          1.1    jruoho         /* Cleanup */
    153          1.1    jruoho 
    154          1.1    jruoho         AcpiOsCloseDirectory (DirInfo);
    155          1.1    jruoho     }
    156          1.1    jruoho }
    157          1.1    jruoho 
    158          1.1    jruoho 
    159          1.1    jruoho /******************************************************************************
    160          1.1    jruoho  *
    161          1.1    jruoho  * FUNCTION:    AsProcessTree
    162          1.1    jruoho  *
    163      1.1.1.3  christos  * DESCRIPTION: Process the directory tree. Files with the extension ".C" and
    164          1.1    jruoho  *              ".H" are processed as the tree is traversed.
    165          1.1    jruoho  *
    166          1.1    jruoho  ******************************************************************************/
    167          1.1    jruoho 
    168          1.1    jruoho ACPI_NATIVE_INT
    169          1.1    jruoho AsProcessTree (
    170          1.1    jruoho     ACPI_CONVERSION_TABLE   *ConversionTable,
    171          1.1    jruoho     char                    *SourcePath,
    172          1.1    jruoho     char                    *TargetPath)
    173          1.1    jruoho {
    174          1.1    jruoho     int                     MaxPathLength;
    175          1.1    jruoho 
    176          1.1    jruoho 
    177          1.1    jruoho     MaxPathLength = AsMaxInt (strlen (SourcePath), strlen (TargetPath));
    178          1.1    jruoho 
    179          1.1    jruoho     if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
    180          1.1    jruoho     {
    181          1.1    jruoho         if (ConversionTable->Flags & FLG_LOWERCASE_DIRNAMES)
    182          1.1    jruoho         {
    183  1.1.1.4.2.2     skrll             AcpiUtStrlwr (TargetPath);
    184          1.1    jruoho         }
    185          1.1    jruoho 
    186          1.1    jruoho         VERBOSE_PRINT (("Creating Directory \"%s\"\n", TargetPath));
    187          1.1    jruoho         if (mkdir (TargetPath))
    188          1.1    jruoho         {
    189          1.1    jruoho             if (errno != EEXIST)
    190          1.1    jruoho             {
    191          1.1    jruoho                 printf ("Could not create target directory\n");
    192      1.1.1.3  christos                 return (-1);
    193          1.1    jruoho             }
    194          1.1    jruoho         }
    195          1.1    jruoho     }
    196          1.1    jruoho 
    197          1.1    jruoho     /* Do the C source files */
    198          1.1    jruoho 
    199          1.1    jruoho     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    200          1.1    jruoho             FILE_TYPE_SOURCE, "*.c");
    201          1.1    jruoho 
    202          1.1    jruoho     /* Do the C header files */
    203          1.1    jruoho 
    204          1.1    jruoho     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    205          1.1    jruoho             FILE_TYPE_HEADER, "*.h");
    206          1.1    jruoho 
    207          1.1    jruoho     /* Do the Lex file(s) */
    208          1.1    jruoho 
    209          1.1    jruoho     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    210          1.1    jruoho             FILE_TYPE_SOURCE, "*.l");
    211          1.1    jruoho 
    212          1.1    jruoho     /* Do the yacc file(s) */
    213          1.1    jruoho 
    214          1.1    jruoho     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    215          1.1    jruoho             FILE_TYPE_SOURCE, "*.y");
    216          1.1    jruoho 
    217          1.1    jruoho     /* Do any ASL files */
    218          1.1    jruoho 
    219          1.1    jruoho     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    220          1.1    jruoho             FILE_TYPE_HEADER, "*.asl");
    221          1.1    jruoho 
    222          1.1    jruoho     /* Do any subdirectories */
    223          1.1    jruoho 
    224          1.1    jruoho     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    225          1.1    jruoho             FILE_TYPE_DIRECTORY, "*");
    226          1.1    jruoho 
    227      1.1.1.3  christos     return (0);
    228          1.1    jruoho }
    229          1.1    jruoho 
    230          1.1    jruoho 
    231          1.1    jruoho /******************************************************************************
    232          1.1    jruoho  *
    233          1.1    jruoho  * FUNCTION:    AsDetectLoneLineFeeds
    234          1.1    jruoho  *
    235          1.1    jruoho  * DESCRIPTION: Find LF without CR.
    236          1.1    jruoho  *
    237          1.1    jruoho  ******************************************************************************/
    238          1.1    jruoho 
    239          1.1    jruoho BOOLEAN
    240          1.1    jruoho AsDetectLoneLineFeeds (
    241          1.1    jruoho     char                    *Filename,
    242          1.1    jruoho     char                    *Buffer)
    243          1.1    jruoho {
    244          1.1    jruoho     UINT32                  i = 1;
    245          1.1    jruoho     UINT32                  LfCount = 0;
    246          1.1    jruoho     UINT32                  LineCount = 0;
    247          1.1    jruoho 
    248          1.1    jruoho 
    249          1.1    jruoho     if (!Buffer[0])
    250          1.1    jruoho     {
    251      1.1.1.3  christos         return (FALSE);
    252          1.1    jruoho     }
    253          1.1    jruoho 
    254          1.1    jruoho     while (Buffer[i])
    255          1.1    jruoho     {
    256          1.1    jruoho         if (Buffer[i] == 0x0A)
    257          1.1    jruoho         {
    258          1.1    jruoho             if (Buffer[i-1] != 0x0D)
    259          1.1    jruoho             {
    260          1.1    jruoho                 LfCount++;
    261          1.1    jruoho             }
    262          1.1    jruoho             LineCount++;
    263          1.1    jruoho         }
    264          1.1    jruoho         i++;
    265          1.1    jruoho     }
    266          1.1    jruoho 
    267          1.1    jruoho     if (LfCount)
    268          1.1    jruoho     {
    269          1.1    jruoho         if (LineCount == LfCount)
    270          1.1    jruoho         {
    271          1.1    jruoho             if (!Gbl_IgnoreLoneLineFeeds)
    272          1.1    jruoho             {
    273          1.1    jruoho                 printf ("%s: ****File has UNIX format**** (LF only, not CR/LF) %u lines\n",
    274          1.1    jruoho                     Filename, LfCount);
    275          1.1    jruoho             }
    276          1.1    jruoho         }
    277          1.1    jruoho         else
    278          1.1    jruoho         {
    279          1.1    jruoho             printf ("%s: %u lone linefeeds in file\n", Filename, LfCount);
    280          1.1    jruoho         }
    281      1.1.1.3  christos         return (TRUE);
    282          1.1    jruoho     }
    283          1.1    jruoho 
    284          1.1    jruoho     return (FALSE);
    285          1.1    jruoho }
    286          1.1    jruoho 
    287          1.1    jruoho 
    288          1.1    jruoho /******************************************************************************
    289          1.1    jruoho  *
    290          1.1    jruoho  * FUNCTION:    AsConvertFile
    291          1.1    jruoho  *
    292          1.1    jruoho  * DESCRIPTION: Perform the requested transforms on the file buffer (as
    293          1.1    jruoho  *              determined by the ConversionTable and the FileType).
    294          1.1    jruoho  *
    295          1.1    jruoho  ******************************************************************************/
    296          1.1    jruoho 
    297          1.1    jruoho void
    298          1.1    jruoho AsConvertFile (
    299          1.1    jruoho     ACPI_CONVERSION_TABLE   *ConversionTable,
    300          1.1    jruoho     char                    *FileBuffer,
    301          1.1    jruoho     char                    *Filename,
    302          1.1    jruoho     ACPI_NATIVE_INT         FileType)
    303          1.1    jruoho {
    304          1.1    jruoho     UINT32                  i;
    305          1.1    jruoho     UINT32                  Functions;
    306          1.1    jruoho     ACPI_STRING_TABLE       *StringTable;
    307          1.1    jruoho     ACPI_IDENTIFIER_TABLE   *ConditionalTable;
    308          1.1    jruoho     ACPI_IDENTIFIER_TABLE   *LineTable;
    309          1.1    jruoho     ACPI_TYPED_IDENTIFIER_TABLE *StructTable;
    310      1.1.1.3  christos     ACPI_IDENTIFIER_TABLE   *SpecialMacroTable;
    311          1.1    jruoho 
    312          1.1    jruoho 
    313          1.1    jruoho     switch (FileType)
    314          1.1    jruoho     {
    315          1.1    jruoho     case FILE_TYPE_SOURCE:
    316      1.1.1.3  christos 
    317          1.1    jruoho         Functions           = ConversionTable->SourceFunctions;
    318          1.1    jruoho         StringTable         = ConversionTable->SourceStringTable;
    319          1.1    jruoho         LineTable           = ConversionTable->SourceLineTable;
    320          1.1    jruoho         ConditionalTable    = ConversionTable->SourceConditionalTable;
    321          1.1    jruoho         StructTable         = ConversionTable->SourceStructTable;
    322      1.1.1.3  christos         SpecialMacroTable   = ConversionTable->SourceSpecialMacroTable;
    323          1.1    jruoho        break;
    324          1.1    jruoho 
    325          1.1    jruoho     case FILE_TYPE_HEADER:
    326      1.1.1.3  christos 
    327          1.1    jruoho         Functions           = ConversionTable->HeaderFunctions;
    328          1.1    jruoho         StringTable         = ConversionTable->HeaderStringTable;
    329          1.1    jruoho         LineTable           = ConversionTable->HeaderLineTable;
    330          1.1    jruoho         ConditionalTable    = ConversionTable->HeaderConditionalTable;
    331          1.1    jruoho         StructTable         = ConversionTable->HeaderStructTable;
    332      1.1.1.3  christos         SpecialMacroTable   = ConversionTable->HeaderSpecialMacroTable;
    333          1.1    jruoho         break;
    334          1.1    jruoho 
    335      1.1.1.4  christos     case FILE_TYPE_PATCH:
    336      1.1.1.4  christos 
    337      1.1.1.4  christos         Functions           = ConversionTable->PatchFunctions;
    338      1.1.1.4  christos         StringTable         = ConversionTable->PatchStringTable;
    339      1.1.1.4  christos         LineTable           = ConversionTable->PatchLineTable;
    340      1.1.1.4  christos         ConditionalTable    = ConversionTable->PatchConditionalTable;
    341      1.1.1.4  christos         StructTable         = ConversionTable->PatchStructTable;
    342      1.1.1.4  christos         SpecialMacroTable   = ConversionTable->PatchSpecialMacroTable;
    343      1.1.1.4  christos         break;
    344      1.1.1.4  christos 
    345          1.1    jruoho     default:
    346      1.1.1.3  christos 
    347          1.1    jruoho         printf ("Unknown file type, cannot process\n");
    348          1.1    jruoho         return;
    349          1.1    jruoho     }
    350          1.1    jruoho 
    351          1.1    jruoho 
    352          1.1    jruoho     Gbl_StructDefs = strstr (FileBuffer, "/* acpisrc:StructDefs");
    353          1.1    jruoho     Gbl_Files++;
    354      1.1.1.2    jruoho     VERBOSE_PRINT (("Processing %u bytes\n",
    355      1.1.1.2    jruoho         (unsigned int) strlen (FileBuffer)));
    356          1.1    jruoho 
    357      1.1.1.3  christos     if (Gbl_Cleanup)
    358      1.1.1.3  christos     {
    359      1.1.1.3  christos         AsRemoveExtraLines (FileBuffer, Filename);
    360      1.1.1.3  christos         AsRemoveSpacesAfterPeriod (FileBuffer, Filename);
    361      1.1.1.3  christos     }
    362      1.1.1.3  christos 
    363          1.1    jruoho     if (ConversionTable->LowerCaseTable)
    364          1.1    jruoho     {
    365          1.1    jruoho         for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
    366          1.1    jruoho         {
    367          1.1    jruoho             AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
    368          1.1    jruoho                                 FileBuffer);
    369          1.1    jruoho         }
    370          1.1    jruoho     }
    371          1.1    jruoho 
    372          1.1    jruoho     /* Process all the string replacements */
    373          1.1    jruoho 
    374          1.1    jruoho     if (StringTable)
    375          1.1    jruoho     {
    376          1.1    jruoho         for (i = 0; StringTable[i].Target; i++)
    377          1.1    jruoho         {
    378          1.1    jruoho             AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
    379          1.1    jruoho                     StringTable[i].Type, FileBuffer);
    380          1.1    jruoho         }
    381          1.1    jruoho     }
    382          1.1    jruoho 
    383          1.1    jruoho     if (LineTable)
    384          1.1    jruoho     {
    385          1.1    jruoho         for (i = 0; LineTable[i].Identifier; i++)
    386          1.1    jruoho         {
    387          1.1    jruoho             AsRemoveLine (FileBuffer, LineTable[i].Identifier);
    388          1.1    jruoho         }
    389          1.1    jruoho     }
    390          1.1    jruoho 
    391          1.1    jruoho     if (ConditionalTable)
    392          1.1    jruoho     {
    393          1.1    jruoho         for (i = 0; ConditionalTable[i].Identifier; i++)
    394          1.1    jruoho         {
    395          1.1    jruoho             AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
    396          1.1    jruoho         }
    397          1.1    jruoho     }
    398          1.1    jruoho 
    399  1.1.1.4.2.2     skrll #ifdef _OBSOLETE_FUNCTIONS
    400          1.1    jruoho     if (MacroTable)
    401          1.1    jruoho     {
    402          1.1    jruoho         for (i = 0; MacroTable[i].Identifier; i++)
    403          1.1    jruoho         {
    404          1.1    jruoho             AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
    405          1.1    jruoho         }
    406          1.1    jruoho     }
    407  1.1.1.4.2.2     skrll #endif
    408          1.1    jruoho 
    409          1.1    jruoho     if (StructTable)
    410          1.1    jruoho     {
    411          1.1    jruoho         for (i = 0; StructTable[i].Identifier; i++)
    412          1.1    jruoho         {
    413          1.1    jruoho             AsInsertPrefix (FileBuffer, StructTable[i].Identifier, StructTable[i].Type);
    414          1.1    jruoho         }
    415          1.1    jruoho     }
    416          1.1    jruoho 
    417      1.1.1.3  christos     if (SpecialMacroTable)
    418      1.1.1.3  christos     {
    419      1.1.1.3  christos         for (i = 0; SpecialMacroTable[i].Identifier; i++)
    420      1.1.1.3  christos         {
    421      1.1.1.3  christos             AsCleanupSpecialMacro (FileBuffer, SpecialMacroTable[i].Identifier);
    422      1.1.1.3  christos         }
    423      1.1.1.3  christos     }
    424      1.1.1.3  christos 
    425          1.1    jruoho     /* Process the function table */
    426          1.1    jruoho 
    427          1.1    jruoho     for (i = 0; i < 32; i++)
    428          1.1    jruoho     {
    429          1.1    jruoho         /* Decode the function bitmap */
    430          1.1    jruoho 
    431          1.1    jruoho         switch ((1 << i) & Functions)
    432          1.1    jruoho         {
    433          1.1    jruoho         case 0:
    434      1.1.1.3  christos 
    435          1.1    jruoho             /* This function not configured */
    436          1.1    jruoho             break;
    437          1.1    jruoho 
    438          1.1    jruoho         case CVT_COUNT_TABS:
    439          1.1    jruoho 
    440          1.1    jruoho             AsCountTabs (FileBuffer, Filename);
    441          1.1    jruoho             break;
    442          1.1    jruoho 
    443          1.1    jruoho         case CVT_COUNT_NON_ANSI_COMMENTS:
    444          1.1    jruoho 
    445          1.1    jruoho             AsCountNonAnsiComments (FileBuffer, Filename);
    446          1.1    jruoho             break;
    447          1.1    jruoho 
    448          1.1    jruoho         case CVT_CHECK_BRACES:
    449          1.1    jruoho 
    450          1.1    jruoho             AsCheckForBraces (FileBuffer, Filename);
    451          1.1    jruoho             break;
    452          1.1    jruoho 
    453          1.1    jruoho         case CVT_TRIM_LINES:
    454          1.1    jruoho 
    455          1.1    jruoho             AsTrimLines (FileBuffer, Filename);
    456          1.1    jruoho             break;
    457          1.1    jruoho 
    458          1.1    jruoho         case CVT_COUNT_LINES:
    459          1.1    jruoho 
    460          1.1    jruoho             AsCountSourceLines (FileBuffer, Filename);
    461          1.1    jruoho             break;
    462          1.1    jruoho 
    463          1.1    jruoho         case CVT_BRACES_ON_SAME_LINE:
    464          1.1    jruoho 
    465          1.1    jruoho             AsBracesOnSameLine (FileBuffer);
    466          1.1    jruoho             break;
    467          1.1    jruoho 
    468          1.1    jruoho         case CVT_MIXED_CASE_TO_UNDERSCORES:
    469          1.1    jruoho 
    470      1.1.1.3  christos             AsMixedCaseToUnderscores (FileBuffer, Filename);
    471          1.1    jruoho             break;
    472          1.1    jruoho 
    473          1.1    jruoho         case CVT_LOWER_CASE_IDENTIFIERS:
    474          1.1    jruoho 
    475          1.1    jruoho             AsLowerCaseIdentifiers (FileBuffer);
    476          1.1    jruoho             break;
    477          1.1    jruoho 
    478          1.1    jruoho         case CVT_REMOVE_DEBUG_MACROS:
    479          1.1    jruoho 
    480          1.1    jruoho             AsRemoveDebugMacros (FileBuffer);
    481          1.1    jruoho             break;
    482          1.1    jruoho 
    483          1.1    jruoho         case CVT_TRIM_WHITESPACE:
    484          1.1    jruoho 
    485          1.1    jruoho             AsTrimWhitespace (FileBuffer);
    486          1.1    jruoho             break;
    487          1.1    jruoho 
    488          1.1    jruoho         case CVT_REMOVE_EMPTY_BLOCKS:
    489          1.1    jruoho 
    490          1.1    jruoho             AsRemoveEmptyBlocks (FileBuffer, Filename);
    491          1.1    jruoho             break;
    492          1.1    jruoho 
    493          1.1    jruoho         case CVT_REDUCE_TYPEDEFS:
    494          1.1    jruoho 
    495          1.1    jruoho             AsReduceTypedefs (FileBuffer, "typedef union");
    496          1.1    jruoho             AsReduceTypedefs (FileBuffer, "typedef struct");
    497          1.1    jruoho             break;
    498          1.1    jruoho 
    499          1.1    jruoho         case CVT_SPACES_TO_TABS4:
    500          1.1    jruoho 
    501          1.1    jruoho             AsTabify4 (FileBuffer);
    502          1.1    jruoho             break;
    503          1.1    jruoho 
    504          1.1    jruoho         case CVT_SPACES_TO_TABS8:
    505          1.1    jruoho 
    506          1.1    jruoho             AsTabify8 (FileBuffer);
    507          1.1    jruoho             break;
    508          1.1    jruoho 
    509          1.1    jruoho         case CVT_COUNT_SHORTMULTILINE_COMMENTS:
    510          1.1    jruoho 
    511          1.1    jruoho #ifdef ACPI_FUTURE_IMPLEMENTATION
    512          1.1    jruoho             AsTrimComments (FileBuffer, Filename);
    513          1.1    jruoho #endif
    514          1.1    jruoho             break;
    515          1.1    jruoho 
    516          1.1    jruoho         default:
    517          1.1    jruoho 
    518          1.1    jruoho             printf ("Unknown conversion subfunction opcode\n");
    519          1.1    jruoho             break;
    520          1.1    jruoho         }
    521          1.1    jruoho     }
    522          1.1    jruoho 
    523          1.1    jruoho     if (ConversionTable->NewHeader)
    524          1.1    jruoho     {
    525          1.1    jruoho         AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
    526          1.1    jruoho     }
    527          1.1    jruoho }
    528          1.1    jruoho 
    529          1.1    jruoho 
    530          1.1    jruoho /******************************************************************************
    531          1.1    jruoho  *
    532          1.1    jruoho  * FUNCTION:    AsProcessOneFile
    533          1.1    jruoho  *
    534      1.1.1.3  christos  * DESCRIPTION: Process one source file. The file is opened, read entirely
    535          1.1    jruoho  *              into a buffer, converted, then written to a new file.
    536          1.1    jruoho  *
    537          1.1    jruoho  ******************************************************************************/
    538          1.1    jruoho 
    539          1.1    jruoho ACPI_NATIVE_INT
    540          1.1    jruoho AsProcessOneFile (
    541          1.1    jruoho     ACPI_CONVERSION_TABLE   *ConversionTable,
    542          1.1    jruoho     char                    *SourcePath,
    543          1.1    jruoho     char                    *TargetPath,
    544          1.1    jruoho     int                     MaxPathLength,
    545          1.1    jruoho     char                    *Filename,
    546          1.1    jruoho     ACPI_NATIVE_INT         FileType)
    547          1.1    jruoho {
    548          1.1    jruoho     char                    *Pathname;
    549          1.1    jruoho     char                    *OutPathname = NULL;
    550          1.1    jruoho 
    551          1.1    jruoho 
    552          1.1    jruoho     /* Allocate a file pathname buffer for both source and target */
    553          1.1    jruoho 
    554          1.1    jruoho     Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1);
    555          1.1    jruoho     if (!Pathname)
    556          1.1    jruoho     {
    557          1.1    jruoho         printf ("Could not allocate buffer for file pathnames\n");
    558      1.1.1.3  christos         return (-1);
    559          1.1    jruoho     }
    560          1.1    jruoho 
    561          1.1    jruoho     Gbl_FileType = FileType;
    562          1.1    jruoho 
    563          1.1    jruoho     /* Generate the source pathname and read the file */
    564          1.1    jruoho 
    565          1.1    jruoho     if (SourcePath)
    566          1.1    jruoho     {
    567          1.1    jruoho         strcpy (Pathname, SourcePath);
    568          1.1    jruoho         strcat (Pathname, "/");
    569          1.1    jruoho     }
    570          1.1    jruoho 
    571          1.1    jruoho     strcat (Pathname, Filename);
    572          1.1    jruoho 
    573          1.1    jruoho     if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize))
    574          1.1    jruoho     {
    575      1.1.1.3  christos         return (-1);
    576          1.1    jruoho     }
    577          1.1    jruoho 
    578          1.1    jruoho     Gbl_HeaderSize = 0;
    579          1.1    jruoho     if (strstr (Filename, ".asl"))
    580          1.1    jruoho     {
    581          1.1    jruoho         Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */
    582          1.1    jruoho     }
    583          1.1    jruoho     else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE))
    584          1.1    jruoho     {
    585          1.1    jruoho         Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */
    586          1.1    jruoho     }
    587          1.1    jruoho     else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE))
    588          1.1    jruoho     {
    589          1.1    jruoho         Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */
    590          1.1    jruoho     }
    591          1.1    jruoho 
    592          1.1    jruoho     /* Process the file in the buffer */
    593          1.1    jruoho 
    594          1.1    jruoho     Gbl_MadeChanges = FALSE;
    595          1.1    jruoho     if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds)
    596          1.1    jruoho     {
    597          1.1    jruoho         /*
    598          1.1    jruoho          * All lone LFs will be converted to CR/LF
    599          1.1    jruoho          * (when file is written, Windows version only)
    600          1.1    jruoho          */
    601          1.1    jruoho         printf ("Converting lone linefeeds\n");
    602          1.1    jruoho         Gbl_MadeChanges = TRUE;
    603          1.1    jruoho     }
    604          1.1    jruoho 
    605          1.1    jruoho     AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType);
    606          1.1    jruoho 
    607          1.1    jruoho     if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
    608          1.1    jruoho     {
    609          1.1    jruoho         if (!(Gbl_Overwrite && !Gbl_MadeChanges))
    610          1.1    jruoho         {
    611          1.1    jruoho             /* Generate the target pathname and write the file */
    612          1.1    jruoho 
    613          1.1    jruoho             OutPathname = calloc (MaxPathLength + strlen (Filename) + 2 + strlen (TargetPath), 1);
    614          1.1    jruoho             if (!OutPathname)
    615          1.1    jruoho             {
    616          1.1    jruoho                 printf ("Could not allocate buffer for file pathnames\n");
    617      1.1.1.3  christos                 return (-1);
    618          1.1    jruoho             }
    619          1.1    jruoho 
    620          1.1    jruoho             strcpy (OutPathname, TargetPath);
    621          1.1    jruoho             if (SourcePath)
    622          1.1    jruoho             {
    623          1.1    jruoho                 strcat (OutPathname, "/");
    624          1.1    jruoho                 strcat (OutPathname, Filename);
    625          1.1    jruoho             }
    626          1.1    jruoho 
    627          1.1    jruoho             AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags);
    628          1.1    jruoho         }
    629          1.1    jruoho     }
    630          1.1    jruoho 
    631          1.1    jruoho     free (Gbl_FileBuffer);
    632          1.1    jruoho     free (Pathname);
    633          1.1    jruoho     if (OutPathname)
    634          1.1    jruoho     {
    635          1.1    jruoho         free (OutPathname);
    636          1.1    jruoho     }
    637          1.1    jruoho 
    638      1.1.1.3  christos     return (0);
    639          1.1    jruoho }
    640          1.1    jruoho 
    641          1.1    jruoho 
    642          1.1    jruoho /******************************************************************************
    643          1.1    jruoho  *
    644          1.1    jruoho  * FUNCTION:    AsCheckForDirectory
    645          1.1    jruoho  *
    646      1.1.1.3  christos  * DESCRIPTION: Check if the current file is a valid directory. If not,
    647          1.1    jruoho  *              construct the full pathname for the source and target paths.
    648          1.1    jruoho  *              Checks for the dot and dot-dot files (they are ignored)
    649          1.1    jruoho  *
    650          1.1    jruoho  ******************************************************************************/
    651          1.1    jruoho 
    652          1.1    jruoho ACPI_NATIVE_INT
    653          1.1    jruoho AsCheckForDirectory (
    654          1.1    jruoho     char                    *SourceDirPath,
    655          1.1    jruoho     char                    *TargetDirPath,
    656          1.1    jruoho     char                    *Filename,
    657          1.1    jruoho     char                    **SourcePath,
    658          1.1    jruoho     char                    **TargetPath)
    659          1.1    jruoho {
    660          1.1    jruoho     char                    *SrcPath;
    661          1.1    jruoho     char                    *TgtPath;
    662          1.1    jruoho 
    663          1.1    jruoho 
    664          1.1    jruoho     if (!(strcmp (Filename, ".")) ||
    665          1.1    jruoho         !(strcmp (Filename, "..")))
    666          1.1    jruoho     {
    667      1.1.1.3  christos         return (-1);
    668          1.1    jruoho     }
    669          1.1    jruoho 
    670          1.1    jruoho     SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1);
    671          1.1    jruoho     if (!SrcPath)
    672          1.1    jruoho     {
    673          1.1    jruoho         printf ("Could not allocate buffer for directory source pathname\n");
    674      1.1.1.3  christos         return (-1);
    675          1.1    jruoho     }
    676          1.1    jruoho 
    677          1.1    jruoho     TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1);
    678          1.1    jruoho     if (!TgtPath)
    679          1.1    jruoho     {
    680          1.1    jruoho         printf ("Could not allocate buffer for directory target pathname\n");
    681          1.1    jruoho         free (SrcPath);
    682      1.1.1.3  christos         return (-1);
    683          1.1    jruoho     }
    684          1.1    jruoho 
    685          1.1    jruoho     strcpy (SrcPath, SourceDirPath);
    686          1.1    jruoho     strcat (SrcPath, "/");
    687          1.1    jruoho     strcat (SrcPath, Filename);
    688          1.1    jruoho 
    689          1.1    jruoho     strcpy (TgtPath, TargetDirPath);
    690          1.1    jruoho     strcat (TgtPath, "/");
    691          1.1    jruoho     strcat (TgtPath, Filename);
    692          1.1    jruoho 
    693          1.1    jruoho     *SourcePath = SrcPath;
    694          1.1    jruoho     *TargetPath = TgtPath;
    695      1.1.1.3  christos     return (0);
    696          1.1    jruoho }
    697          1.1    jruoho 
    698          1.1    jruoho 
    699          1.1    jruoho /******************************************************************************
    700          1.1    jruoho  *
    701          1.1    jruoho  * FUNCTION:    AsGetFile
    702          1.1    jruoho  *
    703          1.1    jruoho  * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
    704          1.1    jruoho  *
    705          1.1    jruoho  ******************************************************************************/
    706          1.1    jruoho 
    707          1.1    jruoho int
    708          1.1    jruoho AsGetFile (
    709          1.1    jruoho     char                    *Filename,
    710          1.1    jruoho     char                    **FileBuffer,
    711          1.1    jruoho     UINT32                  *FileSize)
    712          1.1    jruoho {
    713      1.1.1.3  christos     FILE                    *File;
    714          1.1    jruoho     UINT32                  Size;
    715          1.1    jruoho     char                    *Buffer;
    716      1.1.1.3  christos     size_t                  Actual;
    717          1.1    jruoho 
    718          1.1    jruoho 
    719          1.1    jruoho     /* Binary mode leaves CR/LF pairs */
    720          1.1    jruoho 
    721      1.1.1.3  christos     File = fopen (Filename, "rb");
    722      1.1.1.3  christos     if (!File)
    723          1.1    jruoho     {
    724      1.1.1.3  christos         printf ("Could not open file %s\n", Filename);
    725      1.1.1.3  christos         return (-1);
    726          1.1    jruoho     }
    727          1.1    jruoho 
    728      1.1.1.3  christos     /* Need file size to allocate a buffer */
    729      1.1.1.3  christos 
    730      1.1.1.4  christos     Size = CmGetFileSize (File);
    731      1.1.1.4  christos     if (Size == ACPI_UINT32_MAX)
    732          1.1    jruoho     {
    733      1.1.1.3  christos         printf ("Could not get file size for %s\n", Filename);
    734          1.1    jruoho         goto ErrorExit;
    735          1.1    jruoho     }
    736          1.1    jruoho 
    737          1.1    jruoho     /*
    738          1.1    jruoho      * Create a buffer for the entire file
    739      1.1.1.3  christos      * Add plenty extra buffer to accommodate string replacements
    740          1.1    jruoho      */
    741          1.1    jruoho     Gbl_TotalSize += Size;
    742          1.1    jruoho 
    743          1.1    jruoho     Buffer = calloc (Size * 2, 1);
    744          1.1    jruoho     if (!Buffer)
    745          1.1    jruoho     {
    746          1.1    jruoho         printf ("Could not allocate buffer of size %u\n", Size * 2);
    747          1.1    jruoho         goto ErrorExit;
    748          1.1    jruoho     }
    749          1.1    jruoho 
    750          1.1    jruoho     /* Read the entire file */
    751          1.1    jruoho 
    752      1.1.1.3  christos     Actual = fread (Buffer, 1, Size, File);
    753      1.1.1.3  christos     if (Actual != Size)
    754          1.1    jruoho     {
    755      1.1.1.3  christos         printf ("Could not read the input file %s (%u bytes)\n",
    756      1.1.1.3  christos             Filename, Size);
    757          1.1    jruoho         goto ErrorExit;
    758          1.1    jruoho     }
    759          1.1    jruoho 
    760          1.1    jruoho     Buffer [Size] = 0;         /* Null terminate the buffer */
    761      1.1.1.3  christos     fclose (File);
    762          1.1    jruoho 
    763          1.1    jruoho     /* Check for unix contamination */
    764          1.1    jruoho 
    765          1.1    jruoho     Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer);
    766          1.1    jruoho 
    767          1.1    jruoho     /*
    768      1.1.1.3  christos      * Convert all CR/LF pairs to LF only. We do this locally so that
    769          1.1    jruoho      * this code is portable across operating systems.
    770          1.1    jruoho      */
    771          1.1    jruoho     AsConvertToLineFeeds (Buffer);
    772          1.1    jruoho 
    773          1.1    jruoho     *FileBuffer = Buffer;
    774          1.1    jruoho     *FileSize = Size;
    775      1.1.1.3  christos     return (0);
    776          1.1    jruoho 
    777          1.1    jruoho 
    778          1.1    jruoho ErrorExit:
    779          1.1    jruoho 
    780      1.1.1.3  christos     fclose (File);
    781      1.1.1.3  christos     return (-1);
    782          1.1    jruoho }
    783          1.1    jruoho 
    784          1.1    jruoho 
    785          1.1    jruoho /******************************************************************************
    786          1.1    jruoho  *
    787          1.1    jruoho  * FUNCTION:    AsPutFile
    788          1.1    jruoho  *
    789          1.1    jruoho  * DESCRIPTION: Create a new output file and write the entire contents of the
    790      1.1.1.3  christos  *              buffer to the new file. Buffer must be a zero terminated string
    791          1.1    jruoho  *
    792          1.1    jruoho  ******************************************************************************/
    793          1.1    jruoho 
    794          1.1    jruoho int
    795          1.1    jruoho AsPutFile (
    796          1.1    jruoho     char                    *Pathname,
    797          1.1    jruoho     char                    *FileBuffer,
    798          1.1    jruoho     UINT32                  SystemFlags)
    799          1.1    jruoho {
    800      1.1.1.3  christos     FILE                    *File;
    801          1.1    jruoho     UINT32                  FileSize;
    802      1.1.1.3  christos     size_t                  Actual;
    803      1.1.1.3  christos     int                     Status = 0;
    804          1.1    jruoho 
    805          1.1    jruoho 
    806          1.1    jruoho     /* Create the target file */
    807          1.1    jruoho 
    808          1.1    jruoho     if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS))
    809          1.1    jruoho     {
    810          1.1    jruoho         /* Put back the CR before each LF */
    811          1.1    jruoho 
    812          1.1    jruoho         AsInsertCarriageReturns (FileBuffer);
    813          1.1    jruoho     }
    814          1.1    jruoho 
    815      1.1.1.3  christos     File = fopen (Pathname, "w+b");
    816      1.1.1.3  christos     if (!File)
    817          1.1    jruoho     {
    818          1.1    jruoho         perror ("Could not create destination file");
    819          1.1    jruoho         printf ("Could not create destination file \"%s\"\n", Pathname);
    820      1.1.1.3  christos         return (-1);
    821          1.1    jruoho     }
    822          1.1    jruoho 
    823          1.1    jruoho     /* Write the buffer to the file */
    824          1.1    jruoho 
    825          1.1    jruoho     FileSize = strlen (FileBuffer);
    826      1.1.1.3  christos     Actual = fwrite (FileBuffer, 1, FileSize, File);
    827      1.1.1.3  christos     if (Actual != FileSize)
    828      1.1.1.3  christos     {
    829      1.1.1.3  christos         printf ("Error writing output file \"%s\"\n", Pathname);
    830      1.1.1.3  christos         Status = -1;
    831      1.1.1.3  christos     }
    832          1.1    jruoho 
    833      1.1.1.3  christos     fclose (File);
    834      1.1.1.3  christos     return (Status);
    835          1.1    jruoho }
    836