Home | History | Annotate | Line # | Download | only in acpisrc
asfile.c revision 1.1.1.4.2.3
      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.3     skrll  * Copyright (C) 2000 - 2016, 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.1.4.2.3     skrll                     &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.1.4.2.3     skrll                     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.1.4.2.3     skrll         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.1.4.2.3     skrll         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.1.4.2.3     skrll         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.1.4.2.3     skrll         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.1.4.2.3     skrll         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.1.4.2.3     skrll         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.1.4.2.3     skrll 
    263          1.1    jruoho             LineCount++;
    264          1.1    jruoho         }
    265          1.1    jruoho         i++;
    266          1.1    jruoho     }
    267          1.1    jruoho 
    268          1.1    jruoho     if (LfCount)
    269          1.1    jruoho     {
    270          1.1    jruoho         if (LineCount == LfCount)
    271          1.1    jruoho         {
    272          1.1    jruoho             if (!Gbl_IgnoreLoneLineFeeds)
    273          1.1    jruoho             {
    274          1.1    jruoho                 printf ("%s: ****File has UNIX format**** (LF only, not CR/LF) %u lines\n",
    275          1.1    jruoho                     Filename, LfCount);
    276          1.1    jruoho             }
    277          1.1    jruoho         }
    278          1.1    jruoho         else
    279          1.1    jruoho         {
    280          1.1    jruoho             printf ("%s: %u lone linefeeds in file\n", Filename, LfCount);
    281          1.1    jruoho         }
    282  1.1.1.4.2.3     skrll 
    283      1.1.1.3  christos         return (TRUE);
    284          1.1    jruoho     }
    285          1.1    jruoho 
    286          1.1    jruoho     return (FALSE);
    287          1.1    jruoho }
    288          1.1    jruoho 
    289          1.1    jruoho 
    290          1.1    jruoho /******************************************************************************
    291          1.1    jruoho  *
    292          1.1    jruoho  * FUNCTION:    AsConvertFile
    293          1.1    jruoho  *
    294          1.1    jruoho  * DESCRIPTION: Perform the requested transforms on the file buffer (as
    295          1.1    jruoho  *              determined by the ConversionTable and the FileType).
    296          1.1    jruoho  *
    297          1.1    jruoho  ******************************************************************************/
    298          1.1    jruoho 
    299          1.1    jruoho void
    300          1.1    jruoho AsConvertFile (
    301          1.1    jruoho     ACPI_CONVERSION_TABLE   *ConversionTable,
    302          1.1    jruoho     char                    *FileBuffer,
    303          1.1    jruoho     char                    *Filename,
    304          1.1    jruoho     ACPI_NATIVE_INT         FileType)
    305          1.1    jruoho {
    306          1.1    jruoho     UINT32                  i;
    307          1.1    jruoho     UINT32                  Functions;
    308          1.1    jruoho     ACPI_STRING_TABLE       *StringTable;
    309          1.1    jruoho     ACPI_IDENTIFIER_TABLE   *ConditionalTable;
    310          1.1    jruoho     ACPI_IDENTIFIER_TABLE   *LineTable;
    311          1.1    jruoho     ACPI_TYPED_IDENTIFIER_TABLE *StructTable;
    312      1.1.1.3  christos     ACPI_IDENTIFIER_TABLE   *SpecialMacroTable;
    313          1.1    jruoho 
    314          1.1    jruoho 
    315          1.1    jruoho     switch (FileType)
    316          1.1    jruoho     {
    317          1.1    jruoho     case FILE_TYPE_SOURCE:
    318      1.1.1.3  christos 
    319          1.1    jruoho         Functions           = ConversionTable->SourceFunctions;
    320          1.1    jruoho         StringTable         = ConversionTable->SourceStringTable;
    321          1.1    jruoho         LineTable           = ConversionTable->SourceLineTable;
    322          1.1    jruoho         ConditionalTable    = ConversionTable->SourceConditionalTable;
    323          1.1    jruoho         StructTable         = ConversionTable->SourceStructTable;
    324      1.1.1.3  christos         SpecialMacroTable   = ConversionTable->SourceSpecialMacroTable;
    325          1.1    jruoho        break;
    326          1.1    jruoho 
    327          1.1    jruoho     case FILE_TYPE_HEADER:
    328      1.1.1.3  christos 
    329          1.1    jruoho         Functions           = ConversionTable->HeaderFunctions;
    330          1.1    jruoho         StringTable         = ConversionTable->HeaderStringTable;
    331          1.1    jruoho         LineTable           = ConversionTable->HeaderLineTable;
    332          1.1    jruoho         ConditionalTable    = ConversionTable->HeaderConditionalTable;
    333          1.1    jruoho         StructTable         = ConversionTable->HeaderStructTable;
    334      1.1.1.3  christos         SpecialMacroTable   = ConversionTable->HeaderSpecialMacroTable;
    335          1.1    jruoho         break;
    336          1.1    jruoho 
    337      1.1.1.4  christos     case FILE_TYPE_PATCH:
    338      1.1.1.4  christos 
    339      1.1.1.4  christos         Functions           = ConversionTable->PatchFunctions;
    340      1.1.1.4  christos         StringTable         = ConversionTable->PatchStringTable;
    341      1.1.1.4  christos         LineTable           = ConversionTable->PatchLineTable;
    342      1.1.1.4  christos         ConditionalTable    = ConversionTable->PatchConditionalTable;
    343      1.1.1.4  christos         StructTable         = ConversionTable->PatchStructTable;
    344      1.1.1.4  christos         SpecialMacroTable   = ConversionTable->PatchSpecialMacroTable;
    345      1.1.1.4  christos         break;
    346      1.1.1.4  christos 
    347          1.1    jruoho     default:
    348      1.1.1.3  christos 
    349          1.1    jruoho         printf ("Unknown file type, cannot process\n");
    350          1.1    jruoho         return;
    351          1.1    jruoho     }
    352          1.1    jruoho 
    353          1.1    jruoho 
    354          1.1    jruoho     Gbl_StructDefs = strstr (FileBuffer, "/* acpisrc:StructDefs");
    355          1.1    jruoho     Gbl_Files++;
    356      1.1.1.2    jruoho     VERBOSE_PRINT (("Processing %u bytes\n",
    357      1.1.1.2    jruoho         (unsigned int) strlen (FileBuffer)));
    358          1.1    jruoho 
    359      1.1.1.3  christos     if (Gbl_Cleanup)
    360      1.1.1.3  christos     {
    361      1.1.1.3  christos         AsRemoveExtraLines (FileBuffer, Filename);
    362      1.1.1.3  christos         AsRemoveSpacesAfterPeriod (FileBuffer, Filename);
    363      1.1.1.3  christos     }
    364      1.1.1.3  christos 
    365          1.1    jruoho     if (ConversionTable->LowerCaseTable)
    366          1.1    jruoho     {
    367          1.1    jruoho         for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
    368          1.1    jruoho         {
    369          1.1    jruoho             AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
    370  1.1.1.4.2.3     skrll                 FileBuffer);
    371          1.1    jruoho         }
    372          1.1    jruoho     }
    373          1.1    jruoho 
    374          1.1    jruoho     /* Process all the string replacements */
    375          1.1    jruoho 
    376          1.1    jruoho     if (StringTable)
    377          1.1    jruoho     {
    378          1.1    jruoho         for (i = 0; StringTable[i].Target; i++)
    379          1.1    jruoho         {
    380          1.1    jruoho             AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
    381  1.1.1.4.2.3     skrll                 StringTable[i].Type, FileBuffer);
    382          1.1    jruoho         }
    383          1.1    jruoho     }
    384          1.1    jruoho 
    385          1.1    jruoho     if (LineTable)
    386          1.1    jruoho     {
    387          1.1    jruoho         for (i = 0; LineTable[i].Identifier; i++)
    388          1.1    jruoho         {
    389          1.1    jruoho             AsRemoveLine (FileBuffer, LineTable[i].Identifier);
    390          1.1    jruoho         }
    391          1.1    jruoho     }
    392          1.1    jruoho 
    393          1.1    jruoho     if (ConditionalTable)
    394          1.1    jruoho     {
    395          1.1    jruoho         for (i = 0; ConditionalTable[i].Identifier; i++)
    396          1.1    jruoho         {
    397          1.1    jruoho             AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
    398          1.1    jruoho         }
    399          1.1    jruoho     }
    400          1.1    jruoho 
    401  1.1.1.4.2.2     skrll #ifdef _OBSOLETE_FUNCTIONS
    402          1.1    jruoho     if (MacroTable)
    403          1.1    jruoho     {
    404          1.1    jruoho         for (i = 0; MacroTable[i].Identifier; i++)
    405          1.1    jruoho         {
    406          1.1    jruoho             AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
    407          1.1    jruoho         }
    408          1.1    jruoho     }
    409  1.1.1.4.2.2     skrll #endif
    410          1.1    jruoho 
    411          1.1    jruoho     if (StructTable)
    412          1.1    jruoho     {
    413          1.1    jruoho         for (i = 0; StructTable[i].Identifier; i++)
    414          1.1    jruoho         {
    415  1.1.1.4.2.3     skrll             AsInsertPrefix (FileBuffer, StructTable[i].Identifier,
    416  1.1.1.4.2.3     skrll                 StructTable[i].Type);
    417          1.1    jruoho         }
    418          1.1    jruoho     }
    419          1.1    jruoho 
    420      1.1.1.3  christos     if (SpecialMacroTable)
    421      1.1.1.3  christos     {
    422      1.1.1.3  christos         for (i = 0; SpecialMacroTable[i].Identifier; i++)
    423      1.1.1.3  christos         {
    424      1.1.1.3  christos             AsCleanupSpecialMacro (FileBuffer, SpecialMacroTable[i].Identifier);
    425      1.1.1.3  christos         }
    426      1.1.1.3  christos     }
    427      1.1.1.3  christos 
    428          1.1    jruoho     /* Process the function table */
    429          1.1    jruoho 
    430          1.1    jruoho     for (i = 0; i < 32; i++)
    431          1.1    jruoho     {
    432          1.1    jruoho         /* Decode the function bitmap */
    433          1.1    jruoho 
    434          1.1    jruoho         switch ((1 << i) & Functions)
    435          1.1    jruoho         {
    436          1.1    jruoho         case 0:
    437      1.1.1.3  christos 
    438          1.1    jruoho             /* This function not configured */
    439          1.1    jruoho             break;
    440          1.1    jruoho 
    441          1.1    jruoho         case CVT_COUNT_TABS:
    442          1.1    jruoho 
    443          1.1    jruoho             AsCountTabs (FileBuffer, Filename);
    444          1.1    jruoho             break;
    445          1.1    jruoho 
    446          1.1    jruoho         case CVT_COUNT_NON_ANSI_COMMENTS:
    447          1.1    jruoho 
    448          1.1    jruoho             AsCountNonAnsiComments (FileBuffer, Filename);
    449          1.1    jruoho             break;
    450          1.1    jruoho 
    451          1.1    jruoho         case CVT_CHECK_BRACES:
    452          1.1    jruoho 
    453          1.1    jruoho             AsCheckForBraces (FileBuffer, Filename);
    454          1.1    jruoho             break;
    455          1.1    jruoho 
    456          1.1    jruoho         case CVT_TRIM_LINES:
    457          1.1    jruoho 
    458          1.1    jruoho             AsTrimLines (FileBuffer, Filename);
    459          1.1    jruoho             break;
    460          1.1    jruoho 
    461          1.1    jruoho         case CVT_COUNT_LINES:
    462          1.1    jruoho 
    463          1.1    jruoho             AsCountSourceLines (FileBuffer, Filename);
    464          1.1    jruoho             break;
    465          1.1    jruoho 
    466          1.1    jruoho         case CVT_BRACES_ON_SAME_LINE:
    467          1.1    jruoho 
    468          1.1    jruoho             AsBracesOnSameLine (FileBuffer);
    469          1.1    jruoho             break;
    470          1.1    jruoho 
    471          1.1    jruoho         case CVT_MIXED_CASE_TO_UNDERSCORES:
    472          1.1    jruoho 
    473      1.1.1.3  christos             AsMixedCaseToUnderscores (FileBuffer, Filename);
    474          1.1    jruoho             break;
    475          1.1    jruoho 
    476          1.1    jruoho         case CVT_LOWER_CASE_IDENTIFIERS:
    477          1.1    jruoho 
    478          1.1    jruoho             AsLowerCaseIdentifiers (FileBuffer);
    479          1.1    jruoho             break;
    480          1.1    jruoho 
    481          1.1    jruoho         case CVT_REMOVE_DEBUG_MACROS:
    482          1.1    jruoho 
    483          1.1    jruoho             AsRemoveDebugMacros (FileBuffer);
    484          1.1    jruoho             break;
    485          1.1    jruoho 
    486          1.1    jruoho         case CVT_TRIM_WHITESPACE:
    487          1.1    jruoho 
    488          1.1    jruoho             AsTrimWhitespace (FileBuffer);
    489          1.1    jruoho             break;
    490          1.1    jruoho 
    491          1.1    jruoho         case CVT_REMOVE_EMPTY_BLOCKS:
    492          1.1    jruoho 
    493          1.1    jruoho             AsRemoveEmptyBlocks (FileBuffer, Filename);
    494          1.1    jruoho             break;
    495          1.1    jruoho 
    496          1.1    jruoho         case CVT_REDUCE_TYPEDEFS:
    497          1.1    jruoho 
    498          1.1    jruoho             AsReduceTypedefs (FileBuffer, "typedef union");
    499          1.1    jruoho             AsReduceTypedefs (FileBuffer, "typedef struct");
    500          1.1    jruoho             break;
    501          1.1    jruoho 
    502          1.1    jruoho         case CVT_SPACES_TO_TABS4:
    503          1.1    jruoho 
    504          1.1    jruoho             AsTabify4 (FileBuffer);
    505          1.1    jruoho             break;
    506          1.1    jruoho 
    507          1.1    jruoho         case CVT_SPACES_TO_TABS8:
    508          1.1    jruoho 
    509          1.1    jruoho             AsTabify8 (FileBuffer);
    510          1.1    jruoho             break;
    511          1.1    jruoho 
    512          1.1    jruoho         case CVT_COUNT_SHORTMULTILINE_COMMENTS:
    513          1.1    jruoho 
    514          1.1    jruoho #ifdef ACPI_FUTURE_IMPLEMENTATION
    515          1.1    jruoho             AsTrimComments (FileBuffer, Filename);
    516          1.1    jruoho #endif
    517          1.1    jruoho             break;
    518          1.1    jruoho 
    519          1.1    jruoho         default:
    520          1.1    jruoho 
    521          1.1    jruoho             printf ("Unknown conversion subfunction opcode\n");
    522          1.1    jruoho             break;
    523          1.1    jruoho         }
    524          1.1    jruoho     }
    525          1.1    jruoho 
    526          1.1    jruoho     if (ConversionTable->NewHeader)
    527          1.1    jruoho     {
    528          1.1    jruoho         AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
    529          1.1    jruoho     }
    530          1.1    jruoho }
    531          1.1    jruoho 
    532          1.1    jruoho 
    533          1.1    jruoho /******************************************************************************
    534          1.1    jruoho  *
    535          1.1    jruoho  * FUNCTION:    AsProcessOneFile
    536          1.1    jruoho  *
    537      1.1.1.3  christos  * DESCRIPTION: Process one source file. The file is opened, read entirely
    538          1.1    jruoho  *              into a buffer, converted, then written to a new file.
    539          1.1    jruoho  *
    540          1.1    jruoho  ******************************************************************************/
    541          1.1    jruoho 
    542          1.1    jruoho ACPI_NATIVE_INT
    543          1.1    jruoho AsProcessOneFile (
    544          1.1    jruoho     ACPI_CONVERSION_TABLE   *ConversionTable,
    545          1.1    jruoho     char                    *SourcePath,
    546          1.1    jruoho     char                    *TargetPath,
    547          1.1    jruoho     int                     MaxPathLength,
    548          1.1    jruoho     char                    *Filename,
    549          1.1    jruoho     ACPI_NATIVE_INT         FileType)
    550          1.1    jruoho {
    551          1.1    jruoho     char                    *Pathname;
    552          1.1    jruoho     char                    *OutPathname = NULL;
    553          1.1    jruoho 
    554          1.1    jruoho 
    555          1.1    jruoho     /* Allocate a file pathname buffer for both source and target */
    556          1.1    jruoho 
    557          1.1    jruoho     Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1);
    558          1.1    jruoho     if (!Pathname)
    559          1.1    jruoho     {
    560          1.1    jruoho         printf ("Could not allocate buffer for file pathnames\n");
    561      1.1.1.3  christos         return (-1);
    562          1.1    jruoho     }
    563          1.1    jruoho 
    564          1.1    jruoho     Gbl_FileType = FileType;
    565          1.1    jruoho 
    566          1.1    jruoho     /* Generate the source pathname and read the file */
    567          1.1    jruoho 
    568          1.1    jruoho     if (SourcePath)
    569          1.1    jruoho     {
    570          1.1    jruoho         strcpy (Pathname, SourcePath);
    571          1.1    jruoho         strcat (Pathname, "/");
    572          1.1    jruoho     }
    573          1.1    jruoho 
    574          1.1    jruoho     strcat (Pathname, Filename);
    575          1.1    jruoho 
    576          1.1    jruoho     if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize))
    577          1.1    jruoho     {
    578      1.1.1.3  christos         return (-1);
    579          1.1    jruoho     }
    580          1.1    jruoho 
    581          1.1    jruoho     Gbl_HeaderSize = 0;
    582          1.1    jruoho     if (strstr (Filename, ".asl"))
    583          1.1    jruoho     {
    584          1.1    jruoho         Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */
    585          1.1    jruoho     }
    586          1.1    jruoho     else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE))
    587          1.1    jruoho     {
    588          1.1    jruoho         Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */
    589          1.1    jruoho     }
    590          1.1    jruoho     else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE))
    591          1.1    jruoho     {
    592          1.1    jruoho         Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */
    593          1.1    jruoho     }
    594          1.1    jruoho 
    595          1.1    jruoho     /* Process the file in the buffer */
    596          1.1    jruoho 
    597          1.1    jruoho     Gbl_MadeChanges = FALSE;
    598          1.1    jruoho     if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds)
    599          1.1    jruoho     {
    600          1.1    jruoho         /*
    601          1.1    jruoho          * All lone LFs will be converted to CR/LF
    602          1.1    jruoho          * (when file is written, Windows version only)
    603          1.1    jruoho          */
    604          1.1    jruoho         printf ("Converting lone linefeeds\n");
    605          1.1    jruoho         Gbl_MadeChanges = TRUE;
    606          1.1    jruoho     }
    607          1.1    jruoho 
    608          1.1    jruoho     AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType);
    609          1.1    jruoho 
    610          1.1    jruoho     if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
    611          1.1    jruoho     {
    612          1.1    jruoho         if (!(Gbl_Overwrite && !Gbl_MadeChanges))
    613          1.1    jruoho         {
    614          1.1    jruoho             /* Generate the target pathname and write the file */
    615          1.1    jruoho 
    616  1.1.1.4.2.3     skrll             OutPathname = calloc (MaxPathLength +
    617  1.1.1.4.2.3     skrll                 strlen (Filename) + 2 + strlen (TargetPath), 1);
    618          1.1    jruoho             if (!OutPathname)
    619          1.1    jruoho             {
    620          1.1    jruoho                 printf ("Could not allocate buffer for file pathnames\n");
    621      1.1.1.3  christos                 return (-1);
    622          1.1    jruoho             }
    623          1.1    jruoho 
    624          1.1    jruoho             strcpy (OutPathname, TargetPath);
    625          1.1    jruoho             if (SourcePath)
    626          1.1    jruoho             {
    627          1.1    jruoho                 strcat (OutPathname, "/");
    628          1.1    jruoho                 strcat (OutPathname, Filename);
    629          1.1    jruoho             }
    630          1.1    jruoho 
    631          1.1    jruoho             AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags);
    632          1.1    jruoho         }
    633          1.1    jruoho     }
    634          1.1    jruoho 
    635          1.1    jruoho     free (Gbl_FileBuffer);
    636          1.1    jruoho     free (Pathname);
    637          1.1    jruoho     if (OutPathname)
    638          1.1    jruoho     {
    639          1.1    jruoho         free (OutPathname);
    640          1.1    jruoho     }
    641          1.1    jruoho 
    642      1.1.1.3  christos     return (0);
    643          1.1    jruoho }
    644          1.1    jruoho 
    645          1.1    jruoho 
    646          1.1    jruoho /******************************************************************************
    647          1.1    jruoho  *
    648          1.1    jruoho  * FUNCTION:    AsCheckForDirectory
    649          1.1    jruoho  *
    650      1.1.1.3  christos  * DESCRIPTION: Check if the current file is a valid directory. If not,
    651          1.1    jruoho  *              construct the full pathname for the source and target paths.
    652          1.1    jruoho  *              Checks for the dot and dot-dot files (they are ignored)
    653          1.1    jruoho  *
    654          1.1    jruoho  ******************************************************************************/
    655          1.1    jruoho 
    656          1.1    jruoho ACPI_NATIVE_INT
    657          1.1    jruoho AsCheckForDirectory (
    658          1.1    jruoho     char                    *SourceDirPath,
    659          1.1    jruoho     char                    *TargetDirPath,
    660          1.1    jruoho     char                    *Filename,
    661          1.1    jruoho     char                    **SourcePath,
    662          1.1    jruoho     char                    **TargetPath)
    663          1.1    jruoho {
    664          1.1    jruoho     char                    *SrcPath;
    665          1.1    jruoho     char                    *TgtPath;
    666          1.1    jruoho 
    667          1.1    jruoho 
    668          1.1    jruoho     if (!(strcmp (Filename, ".")) ||
    669          1.1    jruoho         !(strcmp (Filename, "..")))
    670          1.1    jruoho     {
    671      1.1.1.3  christos         return (-1);
    672          1.1    jruoho     }
    673          1.1    jruoho 
    674          1.1    jruoho     SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1);
    675          1.1    jruoho     if (!SrcPath)
    676          1.1    jruoho     {
    677          1.1    jruoho         printf ("Could not allocate buffer for directory source pathname\n");
    678      1.1.1.3  christos         return (-1);
    679          1.1    jruoho     }
    680          1.1    jruoho 
    681          1.1    jruoho     TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1);
    682          1.1    jruoho     if (!TgtPath)
    683          1.1    jruoho     {
    684          1.1    jruoho         printf ("Could not allocate buffer for directory target pathname\n");
    685          1.1    jruoho         free (SrcPath);
    686      1.1.1.3  christos         return (-1);
    687          1.1    jruoho     }
    688          1.1    jruoho 
    689          1.1    jruoho     strcpy (SrcPath, SourceDirPath);
    690          1.1    jruoho     strcat (SrcPath, "/");
    691          1.1    jruoho     strcat (SrcPath, Filename);
    692          1.1    jruoho 
    693          1.1    jruoho     strcpy (TgtPath, TargetDirPath);
    694          1.1    jruoho     strcat (TgtPath, "/");
    695          1.1    jruoho     strcat (TgtPath, Filename);
    696          1.1    jruoho 
    697          1.1    jruoho     *SourcePath = SrcPath;
    698          1.1    jruoho     *TargetPath = TgtPath;
    699      1.1.1.3  christos     return (0);
    700          1.1    jruoho }
    701          1.1    jruoho 
    702          1.1    jruoho 
    703          1.1    jruoho /******************************************************************************
    704          1.1    jruoho  *
    705          1.1    jruoho  * FUNCTION:    AsGetFile
    706          1.1    jruoho  *
    707          1.1    jruoho  * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
    708          1.1    jruoho  *
    709          1.1    jruoho  ******************************************************************************/
    710          1.1    jruoho 
    711          1.1    jruoho int
    712          1.1    jruoho AsGetFile (
    713          1.1    jruoho     char                    *Filename,
    714          1.1    jruoho     char                    **FileBuffer,
    715          1.1    jruoho     UINT32                  *FileSize)
    716          1.1    jruoho {
    717      1.1.1.3  christos     FILE                    *File;
    718          1.1    jruoho     UINT32                  Size;
    719          1.1    jruoho     char                    *Buffer;
    720      1.1.1.3  christos     size_t                  Actual;
    721          1.1    jruoho 
    722          1.1    jruoho 
    723          1.1    jruoho     /* Binary mode leaves CR/LF pairs */
    724          1.1    jruoho 
    725      1.1.1.3  christos     File = fopen (Filename, "rb");
    726      1.1.1.3  christos     if (!File)
    727          1.1    jruoho     {
    728      1.1.1.3  christos         printf ("Could not open file %s\n", Filename);
    729      1.1.1.3  christos         return (-1);
    730          1.1    jruoho     }
    731          1.1    jruoho 
    732      1.1.1.3  christos     /* Need file size to allocate a buffer */
    733      1.1.1.3  christos 
    734      1.1.1.4  christos     Size = CmGetFileSize (File);
    735      1.1.1.4  christos     if (Size == ACPI_UINT32_MAX)
    736          1.1    jruoho     {
    737      1.1.1.3  christos         printf ("Could not get file size for %s\n", Filename);
    738          1.1    jruoho         goto ErrorExit;
    739          1.1    jruoho     }
    740          1.1    jruoho 
    741          1.1    jruoho     /*
    742          1.1    jruoho      * Create a buffer for the entire file
    743      1.1.1.3  christos      * Add plenty extra buffer to accommodate string replacements
    744          1.1    jruoho      */
    745          1.1    jruoho     Gbl_TotalSize += Size;
    746          1.1    jruoho 
    747          1.1    jruoho     Buffer = calloc (Size * 2, 1);
    748          1.1    jruoho     if (!Buffer)
    749          1.1    jruoho     {
    750          1.1    jruoho         printf ("Could not allocate buffer of size %u\n", Size * 2);
    751          1.1    jruoho         goto ErrorExit;
    752          1.1    jruoho     }
    753          1.1    jruoho 
    754          1.1    jruoho     /* Read the entire file */
    755          1.1    jruoho 
    756      1.1.1.3  christos     Actual = fread (Buffer, 1, Size, File);
    757      1.1.1.3  christos     if (Actual != Size)
    758          1.1    jruoho     {
    759      1.1.1.3  christos         printf ("Could not read the input file %s (%u bytes)\n",
    760      1.1.1.3  christos             Filename, Size);
    761          1.1    jruoho         goto ErrorExit;
    762          1.1    jruoho     }
    763          1.1    jruoho 
    764          1.1    jruoho     Buffer [Size] = 0;         /* Null terminate the buffer */
    765      1.1.1.3  christos     fclose (File);
    766          1.1    jruoho 
    767          1.1    jruoho     /* Check for unix contamination */
    768          1.1    jruoho 
    769          1.1    jruoho     Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer);
    770          1.1    jruoho 
    771          1.1    jruoho     /*
    772      1.1.1.3  christos      * Convert all CR/LF pairs to LF only. We do this locally so that
    773          1.1    jruoho      * this code is portable across operating systems.
    774          1.1    jruoho      */
    775          1.1    jruoho     AsConvertToLineFeeds (Buffer);
    776          1.1    jruoho 
    777          1.1    jruoho     *FileBuffer = Buffer;
    778          1.1    jruoho     *FileSize = Size;
    779      1.1.1.3  christos     return (0);
    780          1.1    jruoho 
    781          1.1    jruoho 
    782          1.1    jruoho ErrorExit:
    783          1.1    jruoho 
    784      1.1.1.3  christos     fclose (File);
    785      1.1.1.3  christos     return (-1);
    786          1.1    jruoho }
    787          1.1    jruoho 
    788          1.1    jruoho 
    789          1.1    jruoho /******************************************************************************
    790          1.1    jruoho  *
    791          1.1    jruoho  * FUNCTION:    AsPutFile
    792          1.1    jruoho  *
    793          1.1    jruoho  * DESCRIPTION: Create a new output file and write the entire contents of the
    794      1.1.1.3  christos  *              buffer to the new file. Buffer must be a zero terminated string
    795          1.1    jruoho  *
    796          1.1    jruoho  ******************************************************************************/
    797          1.1    jruoho 
    798          1.1    jruoho int
    799          1.1    jruoho AsPutFile (
    800          1.1    jruoho     char                    *Pathname,
    801          1.1    jruoho     char                    *FileBuffer,
    802          1.1    jruoho     UINT32                  SystemFlags)
    803          1.1    jruoho {
    804      1.1.1.3  christos     FILE                    *File;
    805          1.1    jruoho     UINT32                  FileSize;
    806      1.1.1.3  christos     size_t                  Actual;
    807      1.1.1.3  christos     int                     Status = 0;
    808          1.1    jruoho 
    809          1.1    jruoho 
    810          1.1    jruoho     /* Create the target file */
    811          1.1    jruoho 
    812          1.1    jruoho     if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS))
    813          1.1    jruoho     {
    814          1.1    jruoho         /* Put back the CR before each LF */
    815          1.1    jruoho 
    816          1.1    jruoho         AsInsertCarriageReturns (FileBuffer);
    817          1.1    jruoho     }
    818          1.1    jruoho 
    819      1.1.1.3  christos     File = fopen (Pathname, "w+b");
    820      1.1.1.3  christos     if (!File)
    821          1.1    jruoho     {
    822          1.1    jruoho         perror ("Could not create destination file");
    823          1.1    jruoho         printf ("Could not create destination file \"%s\"\n", Pathname);
    824      1.1.1.3  christos         return (-1);
    825          1.1    jruoho     }
    826          1.1    jruoho 
    827          1.1    jruoho     /* Write the buffer to the file */
    828          1.1    jruoho 
    829          1.1    jruoho     FileSize = strlen (FileBuffer);
    830      1.1.1.3  christos     Actual = fwrite (FileBuffer, 1, FileSize, File);
    831      1.1.1.3  christos     if (Actual != FileSize)
    832      1.1.1.3  christos     {
    833      1.1.1.3  christos         printf ("Error writing output file \"%s\"\n", Pathname);
    834      1.1.1.3  christos         Status = -1;
    835      1.1.1.3  christos     }
    836          1.1    jruoho 
    837      1.1.1.3  christos     fclose (File);
    838      1.1.1.3  christos     return (Status);
    839          1.1    jruoho }
    840