Home | History | Annotate | Line # | Download | only in acpisrc
asfile.c revision 1.1
      1 
      2 /******************************************************************************
      3  *
      4  * Module Name: asfile - Main module for the acpi source processor utility
      5  *
      6  *****************************************************************************/
      7 
      8 /******************************************************************************
      9  *
     10  * 1. Copyright Notice
     11  *
     12  * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
     13  * All rights reserved.
     14  *
     15  * 2. License
     16  *
     17  * 2.1. This is your license from Intel Corp. under its intellectual property
     18  * rights.  You may have additional license terms from the party that provided
     19  * you this software, covering your right to use that party's intellectual
     20  * property rights.
     21  *
     22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
     23  * copy of the source code appearing in this file ("Covered Code") an
     24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
     25  * base code distributed originally by Intel ("Original Intel Code") to copy,
     26  * make derivatives, distribute, use and display any portion of the Covered
     27  * Code in any form, with the right to sublicense such rights; and
     28  *
     29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
     30  * license (with the right to sublicense), under only those claims of Intel
     31  * patents that are infringed by the Original Intel Code, to make, use, sell,
     32  * offer to sell, and import the Covered Code and derivative works thereof
     33  * solely to the minimum extent necessary to exercise the above copyright
     34  * license, and in no event shall the patent license extend to any additions
     35  * to or modifications of the Original Intel Code.  No other license or right
     36  * is granted directly or by implication, estoppel or otherwise;
     37  *
     38  * The above copyright and patent license is granted only if the following
     39  * conditions are met:
     40  *
     41  * 3. Conditions
     42  *
     43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
     44  * Redistribution of source code of any substantial portion of the Covered
     45  * Code or modification with rights to further distribute source must include
     46  * the above Copyright Notice, the above License, this list of Conditions,
     47  * and the following Disclaimer and Export Compliance provision.  In addition,
     48  * Licensee must cause all Covered Code to which Licensee contributes to
     49  * contain a file documenting the changes Licensee made to create that Covered
     50  * Code and the date of any change.  Licensee must include in that file the
     51  * documentation of any changes made by any predecessor Licensee.  Licensee
     52  * must include a prominent statement that the modification is derived,
     53  * directly or indirectly, from Original Intel Code.
     54  *
     55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
     56  * Redistribution of source code of any substantial portion of the Covered
     57  * Code or modification without rights to further distribute source must
     58  * include the following Disclaimer and Export Compliance provision in the
     59  * documentation and/or other materials provided with distribution.  In
     60  * addition, Licensee may not authorize further sublicense of source of any
     61  * portion of the Covered Code, and must include terms to the effect that the
     62  * license from Licensee to its licensee is limited to the intellectual
     63  * property embodied in the software Licensee provides to its licensee, and
     64  * not to intellectual property embodied in modifications its licensee may
     65  * make.
     66  *
     67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
     68  * substantial portion of the Covered Code or modification must reproduce the
     69  * above Copyright Notice, and the following Disclaimer and Export Compliance
     70  * provision in the documentation and/or other materials provided with the
     71  * distribution.
     72  *
     73  * 3.4. Intel retains all right, title, and interest in and to the Original
     74  * Intel Code.
     75  *
     76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
     77  * Intel shall be used in advertising or otherwise to promote the sale, use or
     78  * other dealings in products derived from or relating to the Covered Code
     79  * without prior written authorization from Intel.
     80  *
     81  * 4. Disclaimer and Export Compliance
     82  *
     83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
     84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
     85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
     86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
     87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
     88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
     89  * PARTICULAR PURPOSE.
     90  *
     91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
     92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
     93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
     94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
     95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
     96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
     97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
     98  * LIMITED REMEDY.
     99  *
    100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
    101  * software or system incorporating such software without first obtaining any
    102  * required license or other approval from the U. S. Department of Commerce or
    103  * any other agency or department of the United States Government.  In the
    104  * event Licensee exports any such software from the United States or
    105  * re-exports any such software from a foreign destination, Licensee shall
    106  * ensure that the distribution and export/re-export of the software is in
    107  * compliance with all laws, regulations, orders, or other restrictions of the
    108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
    109  * any of its subsidiaries will export/re-export any technical data, process,
    110  * software, or service, directly or indirectly, to any country for which the
    111  * United States government or any agency thereof requires an export license,
    112  * other governmental approval, or letter of assurance, without first obtaining
    113  * such license, approval or letter.
    114  *
    115  *****************************************************************************/
    116 
    117 #include "acpisrc.h"
    118 
    119 /* Local prototypes */
    120 
    121 void
    122 AsDoWildcard (
    123     ACPI_CONVERSION_TABLE   *ConversionTable,
    124     char                    *SourcePath,
    125     char                    *TargetPath,
    126     int                     MaxPathLength,
    127     int                     FileType,
    128     char                    *WildcardSpec);
    129 
    130 BOOLEAN
    131 AsDetectLoneLineFeeds (
    132     char                    *Filename,
    133     char                    *Buffer);
    134 
    135 static inline int
    136 AsMaxInt (int a, int b)
    137 {
    138     return (a > b ? a : b);
    139 }
    140 
    141 
    142 /******************************************************************************
    143  *
    144  * FUNCTION:    AsDoWildcard
    145  *
    146  * DESCRIPTION: Process files via wildcards
    147  *
    148  ******************************************************************************/
    149 
    150 void
    151 AsDoWildcard (
    152     ACPI_CONVERSION_TABLE   *ConversionTable,
    153     char                    *SourcePath,
    154     char                    *TargetPath,
    155     int                     MaxPathLength,
    156     int                     FileType,
    157     char                    *WildcardSpec)
    158 {
    159     void                    *DirInfo;
    160     char                    *Filename;
    161     char                    *SourceDirPath;
    162     char                    *TargetDirPath;
    163     char                    RequestedFileType;
    164 
    165 
    166     if (FileType == FILE_TYPE_DIRECTORY)
    167     {
    168         RequestedFileType = REQUEST_DIR_ONLY;
    169     }
    170     else
    171     {
    172         RequestedFileType = REQUEST_FILE_ONLY;
    173     }
    174 
    175     VERBOSE_PRINT (("Checking for %s source files in directory \"%s\"\n",
    176             WildcardSpec, SourcePath));
    177 
    178     /* Open the directory for wildcard search */
    179 
    180     DirInfo = AcpiOsOpenDirectory (SourcePath, WildcardSpec, RequestedFileType);
    181     if (DirInfo)
    182     {
    183         /*
    184          * Get all of the files that match both the
    185          * wildcard and the requested file type
    186          */
    187         while ((Filename = AcpiOsGetNextFilename (DirInfo)))
    188         {
    189             /* Looking for directory files, must check file type */
    190 
    191             switch (RequestedFileType)
    192             {
    193             case REQUEST_DIR_ONLY:
    194 
    195                 /* If we actually have a dir, process the subtree */
    196 
    197                 if (!AsCheckForDirectory (SourcePath, TargetPath, Filename,
    198                         &SourceDirPath, &TargetDirPath))
    199                 {
    200                     VERBOSE_PRINT (("Subdirectory: %s\n", Filename));
    201 
    202                     AsProcessTree (ConversionTable, SourceDirPath, TargetDirPath);
    203                     free (SourceDirPath);
    204                     free (TargetDirPath);
    205                 }
    206                 break;
    207 
    208             case REQUEST_FILE_ONLY:
    209 
    210                 /* Otherwise, this is a file, not a directory */
    211 
    212                 VERBOSE_PRINT (("File: %s\n", Filename));
    213 
    214                 AsProcessOneFile (ConversionTable, SourcePath, TargetPath,
    215                         MaxPathLength, Filename, FileType);
    216                 break;
    217 
    218             default:
    219                 break;
    220             }
    221         }
    222 
    223         /* Cleanup */
    224 
    225         AcpiOsCloseDirectory (DirInfo);
    226     }
    227 }
    228 
    229 
    230 /******************************************************************************
    231  *
    232  * FUNCTION:    AsProcessTree
    233  *
    234  * DESCRIPTION: Process the directory tree.  Files with the extension ".C" and
    235  *              ".H" are processed as the tree is traversed.
    236  *
    237  ******************************************************************************/
    238 
    239 ACPI_NATIVE_INT
    240 AsProcessTree (
    241     ACPI_CONVERSION_TABLE   *ConversionTable,
    242     char                    *SourcePath,
    243     char                    *TargetPath)
    244 {
    245     int                     MaxPathLength;
    246 
    247 
    248     MaxPathLength = AsMaxInt (strlen (SourcePath), strlen (TargetPath));
    249 
    250     if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
    251     {
    252         if (ConversionTable->Flags & FLG_LOWERCASE_DIRNAMES)
    253         {
    254             strlwr (TargetPath);
    255         }
    256 
    257         VERBOSE_PRINT (("Creating Directory \"%s\"\n", TargetPath));
    258         if (mkdir (TargetPath))
    259         {
    260             if (errno != EEXIST)
    261             {
    262                 printf ("Could not create target directory\n");
    263                 return -1;
    264             }
    265         }
    266     }
    267 
    268     /* Do the C source files */
    269 
    270     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    271             FILE_TYPE_SOURCE, "*.c");
    272 
    273     /* Do the C header files */
    274 
    275     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    276             FILE_TYPE_HEADER, "*.h");
    277 
    278     /* Do the Lex file(s) */
    279 
    280     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    281             FILE_TYPE_SOURCE, "*.l");
    282 
    283     /* Do the yacc file(s) */
    284 
    285     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    286             FILE_TYPE_SOURCE, "*.y");
    287 
    288     /* Do any ASL files */
    289 
    290     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    291             FILE_TYPE_HEADER, "*.asl");
    292 
    293     /* Do any subdirectories */
    294 
    295     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
    296             FILE_TYPE_DIRECTORY, "*");
    297 
    298     return 0;
    299 }
    300 
    301 
    302 /******************************************************************************
    303  *
    304  * FUNCTION:    AsDetectLoneLineFeeds
    305  *
    306  * DESCRIPTION: Find LF without CR.
    307  *
    308  ******************************************************************************/
    309 
    310 BOOLEAN
    311 AsDetectLoneLineFeeds (
    312     char                    *Filename,
    313     char                    *Buffer)
    314 {
    315     UINT32                  i = 1;
    316     UINT32                  LfCount = 0;
    317     UINT32                  LineCount = 0;
    318 
    319 
    320     if (!Buffer[0])
    321     {
    322         return FALSE;
    323     }
    324 
    325     while (Buffer[i])
    326     {
    327         if (Buffer[i] == 0x0A)
    328         {
    329             if (Buffer[i-1] != 0x0D)
    330             {
    331                 LfCount++;
    332             }
    333             LineCount++;
    334         }
    335         i++;
    336     }
    337 
    338     if (LfCount)
    339     {
    340         if (LineCount == LfCount)
    341         {
    342             if (!Gbl_IgnoreLoneLineFeeds)
    343             {
    344                 printf ("%s: ****File has UNIX format**** (LF only, not CR/LF) %u lines\n",
    345                     Filename, LfCount);
    346             }
    347         }
    348         else
    349         {
    350             printf ("%s: %u lone linefeeds in file\n", Filename, LfCount);
    351         }
    352         return TRUE;
    353     }
    354 
    355     return (FALSE);
    356 }
    357 
    358 
    359 /******************************************************************************
    360  *
    361  * FUNCTION:    AsConvertFile
    362  *
    363  * DESCRIPTION: Perform the requested transforms on the file buffer (as
    364  *              determined by the ConversionTable and the FileType).
    365  *
    366  ******************************************************************************/
    367 
    368 void
    369 AsConvertFile (
    370     ACPI_CONVERSION_TABLE   *ConversionTable,
    371     char                    *FileBuffer,
    372     char                    *Filename,
    373     ACPI_NATIVE_INT         FileType)
    374 {
    375     UINT32                  i;
    376     UINT32                  Functions;
    377     ACPI_STRING_TABLE       *StringTable;
    378     ACPI_IDENTIFIER_TABLE   *ConditionalTable;
    379     ACPI_IDENTIFIER_TABLE   *LineTable;
    380     ACPI_IDENTIFIER_TABLE   *MacroTable;
    381     ACPI_TYPED_IDENTIFIER_TABLE *StructTable;
    382 
    383 
    384     switch (FileType)
    385     {
    386     case FILE_TYPE_SOURCE:
    387         Functions           = ConversionTable->SourceFunctions;
    388         StringTable         = ConversionTable->SourceStringTable;
    389         LineTable           = ConversionTable->SourceLineTable;
    390         ConditionalTable    = ConversionTable->SourceConditionalTable;
    391         MacroTable          = ConversionTable->SourceMacroTable;
    392         StructTable         = ConversionTable->SourceStructTable;
    393        break;
    394 
    395     case FILE_TYPE_HEADER:
    396         Functions           = ConversionTable->HeaderFunctions;
    397         StringTable         = ConversionTable->HeaderStringTable;
    398         LineTable           = ConversionTable->HeaderLineTable;
    399         ConditionalTable    = ConversionTable->HeaderConditionalTable;
    400         MacroTable          = ConversionTable->HeaderMacroTable;
    401         StructTable         = ConversionTable->HeaderStructTable;
    402         break;
    403 
    404     default:
    405         printf ("Unknown file type, cannot process\n");
    406         return;
    407     }
    408 
    409 
    410     Gbl_StructDefs = strstr (FileBuffer, "/* acpisrc:StructDefs");
    411     Gbl_Files++;
    412     VERBOSE_PRINT (("Processing %u bytes\n", strlen (FileBuffer)));
    413 
    414     if (ConversionTable->LowerCaseTable)
    415     {
    416         for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
    417         {
    418             AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
    419                                 FileBuffer);
    420         }
    421     }
    422 
    423     /* Process all the string replacements */
    424 
    425     if (StringTable)
    426     {
    427         for (i = 0; StringTable[i].Target; i++)
    428         {
    429             AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
    430                     StringTable[i].Type, FileBuffer);
    431         }
    432     }
    433 
    434     if (LineTable)
    435     {
    436         for (i = 0; LineTable[i].Identifier; i++)
    437         {
    438             AsRemoveLine (FileBuffer, LineTable[i].Identifier);
    439         }
    440     }
    441 
    442     if (ConditionalTable)
    443     {
    444         for (i = 0; ConditionalTable[i].Identifier; i++)
    445         {
    446             AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
    447         }
    448     }
    449 
    450     if (MacroTable)
    451     {
    452         for (i = 0; MacroTable[i].Identifier; i++)
    453         {
    454             AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
    455         }
    456     }
    457 
    458     if (StructTable)
    459     {
    460         for (i = 0; StructTable[i].Identifier; i++)
    461         {
    462             AsInsertPrefix (FileBuffer, StructTable[i].Identifier, StructTable[i].Type);
    463         }
    464     }
    465 
    466     /* Process the function table */
    467 
    468     for (i = 0; i < 32; i++)
    469     {
    470         /* Decode the function bitmap */
    471 
    472         switch ((1 << i) & Functions)
    473         {
    474         case 0:
    475             /* This function not configured */
    476             break;
    477 
    478 
    479         case CVT_COUNT_TABS:
    480 
    481             AsCountTabs (FileBuffer, Filename);
    482             break;
    483 
    484 
    485         case CVT_COUNT_NON_ANSI_COMMENTS:
    486 
    487             AsCountNonAnsiComments (FileBuffer, Filename);
    488             break;
    489 
    490 
    491         case CVT_CHECK_BRACES:
    492 
    493             AsCheckForBraces (FileBuffer, Filename);
    494             break;
    495 
    496 
    497         case CVT_TRIM_LINES:
    498 
    499             AsTrimLines (FileBuffer, Filename);
    500             break;
    501 
    502 
    503         case CVT_COUNT_LINES:
    504 
    505             AsCountSourceLines (FileBuffer, Filename);
    506             break;
    507 
    508 
    509         case CVT_BRACES_ON_SAME_LINE:
    510 
    511             AsBracesOnSameLine (FileBuffer);
    512             break;
    513 
    514 
    515         case CVT_MIXED_CASE_TO_UNDERSCORES:
    516 
    517             AsMixedCaseToUnderscores (FileBuffer);
    518             break;
    519 
    520 
    521         case CVT_LOWER_CASE_IDENTIFIERS:
    522 
    523             AsLowerCaseIdentifiers (FileBuffer);
    524             break;
    525 
    526 
    527         case CVT_REMOVE_DEBUG_MACROS:
    528 
    529             AsRemoveDebugMacros (FileBuffer);
    530             break;
    531 
    532 
    533         case CVT_TRIM_WHITESPACE:
    534 
    535             AsTrimWhitespace (FileBuffer);
    536             break;
    537 
    538 
    539         case CVT_REMOVE_EMPTY_BLOCKS:
    540 
    541             AsRemoveEmptyBlocks (FileBuffer, Filename);
    542             break;
    543 
    544 
    545         case CVT_REDUCE_TYPEDEFS:
    546 
    547             AsReduceTypedefs (FileBuffer, "typedef union");
    548             AsReduceTypedefs (FileBuffer, "typedef struct");
    549             break;
    550 
    551 
    552         case CVT_SPACES_TO_TABS4:
    553 
    554             AsTabify4 (FileBuffer);
    555             break;
    556 
    557 
    558         case CVT_SPACES_TO_TABS8:
    559 
    560             AsTabify8 (FileBuffer);
    561             break;
    562 
    563         case CVT_COUNT_SHORTMULTILINE_COMMENTS:
    564 
    565 #ifdef ACPI_FUTURE_IMPLEMENTATION
    566             AsTrimComments (FileBuffer, Filename);
    567 #endif
    568             break;
    569 
    570         default:
    571 
    572             printf ("Unknown conversion subfunction opcode\n");
    573             break;
    574         }
    575     }
    576 
    577     if (ConversionTable->NewHeader)
    578     {
    579         AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
    580     }
    581 }
    582 
    583 
    584 /******************************************************************************
    585  *
    586  * FUNCTION:    AsProcessOneFile
    587  *
    588  * DESCRIPTION: Process one source file.  The file is opened, read entirely
    589  *              into a buffer, converted, then written to a new file.
    590  *
    591  ******************************************************************************/
    592 
    593 ACPI_NATIVE_INT
    594 AsProcessOneFile (
    595     ACPI_CONVERSION_TABLE   *ConversionTable,
    596     char                    *SourcePath,
    597     char                    *TargetPath,
    598     int                     MaxPathLength,
    599     char                    *Filename,
    600     ACPI_NATIVE_INT         FileType)
    601 {
    602     char                    *Pathname;
    603     char                    *OutPathname = NULL;
    604 
    605 
    606     /* Allocate a file pathname buffer for both source and target */
    607 
    608     Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1);
    609     if (!Pathname)
    610     {
    611         printf ("Could not allocate buffer for file pathnames\n");
    612         return -1;
    613     }
    614 
    615     Gbl_FileType = FileType;
    616 
    617     /* Generate the source pathname and read the file */
    618 
    619     if (SourcePath)
    620     {
    621         strcpy (Pathname, SourcePath);
    622         strcat (Pathname, "/");
    623     }
    624 
    625     strcat (Pathname, Filename);
    626 
    627     if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize))
    628     {
    629         return -1;
    630     }
    631 
    632     Gbl_HeaderSize = 0;
    633     if (strstr (Filename, ".asl"))
    634     {
    635         Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */
    636     }
    637     else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE))
    638     {
    639         Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */
    640     }
    641     else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE))
    642     {
    643         Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */
    644     }
    645 
    646     /* Process the file in the buffer */
    647 
    648     Gbl_MadeChanges = FALSE;
    649     if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds)
    650     {
    651         /*
    652          * All lone LFs will be converted to CR/LF
    653          * (when file is written, Windows version only)
    654          */
    655         printf ("Converting lone linefeeds\n");
    656         Gbl_MadeChanges = TRUE;
    657     }
    658 
    659     AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType);
    660 
    661     if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
    662     {
    663         if (!(Gbl_Overwrite && !Gbl_MadeChanges))
    664         {
    665             /* Generate the target pathname and write the file */
    666 
    667             OutPathname = calloc (MaxPathLength + strlen (Filename) + 2 + strlen (TargetPath), 1);
    668             if (!OutPathname)
    669             {
    670                 printf ("Could not allocate buffer for file pathnames\n");
    671                 return -1;
    672             }
    673 
    674             strcpy (OutPathname, TargetPath);
    675             if (SourcePath)
    676             {
    677                 strcat (OutPathname, "/");
    678                 strcat (OutPathname, Filename);
    679             }
    680 
    681             AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags);
    682         }
    683     }
    684 
    685     free (Gbl_FileBuffer);
    686     free (Pathname);
    687     if (OutPathname)
    688     {
    689         free (OutPathname);
    690     }
    691 
    692     return 0;
    693 }
    694 
    695 
    696 /******************************************************************************
    697  *
    698  * FUNCTION:    AsCheckForDirectory
    699  *
    700  * DESCRIPTION: Check if the current file is a valid directory.  If not,
    701  *              construct the full pathname for the source and target paths.
    702  *              Checks for the dot and dot-dot files (they are ignored)
    703  *
    704  ******************************************************************************/
    705 
    706 ACPI_NATIVE_INT
    707 AsCheckForDirectory (
    708     char                    *SourceDirPath,
    709     char                    *TargetDirPath,
    710     char                    *Filename,
    711     char                    **SourcePath,
    712     char                    **TargetPath)
    713 {
    714     char                    *SrcPath;
    715     char                    *TgtPath;
    716 
    717 
    718     if (!(strcmp (Filename, ".")) ||
    719         !(strcmp (Filename, "..")))
    720     {
    721         return -1;
    722     }
    723 
    724     SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1);
    725     if (!SrcPath)
    726     {
    727         printf ("Could not allocate buffer for directory source pathname\n");
    728         return -1;
    729     }
    730 
    731     TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1);
    732     if (!TgtPath)
    733     {
    734         printf ("Could not allocate buffer for directory target pathname\n");
    735         free (SrcPath);
    736         return -1;
    737     }
    738 
    739     strcpy (SrcPath, SourceDirPath);
    740     strcat (SrcPath, "/");
    741     strcat (SrcPath, Filename);
    742 
    743     strcpy (TgtPath, TargetDirPath);
    744     strcat (TgtPath, "/");
    745     strcat (TgtPath, Filename);
    746 
    747     *SourcePath = SrcPath;
    748     *TargetPath = TgtPath;
    749     return 0;
    750 }
    751 
    752 
    753 /******************************************************************************
    754  *
    755  * FUNCTION:    AsGetFile
    756  *
    757  * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
    758  *
    759  ******************************************************************************/
    760 
    761 int
    762 AsGetFile (
    763     char                    *Filename,
    764     char                    **FileBuffer,
    765     UINT32                  *FileSize)
    766 {
    767 
    768     int                     FileHandle;
    769     UINT32                  Size;
    770     char                    *Buffer;
    771 
    772 
    773     /* Binary mode leaves CR/LF pairs */
    774 
    775     FileHandle = open (Filename, O_BINARY | O_RDONLY);
    776     if (!FileHandle)
    777     {
    778         printf ("Could not open %s\n", Filename);
    779         return -1;
    780     }
    781 
    782     if (fstat (FileHandle, &Gbl_StatBuf))
    783     {
    784         printf ("Could not get file status for %s\n", Filename);
    785         goto ErrorExit;
    786     }
    787 
    788     /*
    789      * Create a buffer for the entire file
    790      * Add plenty extra buffer to accomodate string replacements
    791      */
    792     Size = Gbl_StatBuf.st_size;
    793     Gbl_TotalSize += Size;
    794 
    795     Buffer = calloc (Size * 2, 1);
    796     if (!Buffer)
    797     {
    798         printf ("Could not allocate buffer of size %u\n", Size * 2);
    799         goto ErrorExit;
    800     }
    801 
    802     /* Read the entire file */
    803 
    804     Size = read (FileHandle, Buffer, Size);
    805     if (Size == -1)
    806     {
    807         printf ("Could not read the input file %s\n", Filename);
    808         goto ErrorExit;
    809     }
    810 
    811     Buffer [Size] = 0;         /* Null terminate the buffer */
    812     close (FileHandle);
    813 
    814     /* Check for unix contamination */
    815 
    816     Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer);
    817 
    818     /*
    819      * Convert all CR/LF pairs to LF only.  We do this locally so that
    820      * this code is portable across operating systems.
    821      */
    822     AsConvertToLineFeeds (Buffer);
    823 
    824     *FileBuffer = Buffer;
    825     *FileSize = Size;
    826 
    827     return 0;
    828 
    829 
    830 ErrorExit:
    831 
    832     close (FileHandle);
    833     return -1;
    834 }
    835 
    836 
    837 /******************************************************************************
    838  *
    839  * FUNCTION:    AsPutFile
    840  *
    841  * DESCRIPTION: Create a new output file and write the entire contents of the
    842  *              buffer to the new file.  Buffer must be a zero terminated string
    843  *
    844  ******************************************************************************/
    845 
    846 int
    847 AsPutFile (
    848     char                    *Pathname,
    849     char                    *FileBuffer,
    850     UINT32                  SystemFlags)
    851 {
    852     UINT32                  FileSize;
    853     int                     DestHandle;
    854     int                     OpenFlags;
    855 
    856 
    857     /* Create the target file */
    858 
    859     OpenFlags = O_TRUNC | O_CREAT | O_WRONLY | O_BINARY;
    860 
    861     if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS))
    862     {
    863         /* Put back the CR before each LF */
    864 
    865         AsInsertCarriageReturns (FileBuffer);
    866     }
    867 
    868     DestHandle = open (Pathname, OpenFlags, S_IREAD | S_IWRITE);
    869     if (DestHandle == -1)
    870     {
    871         perror ("Could not create destination file");
    872         printf ("Could not create destination file \"%s\"\n", Pathname);
    873         return -1;
    874     }
    875 
    876     /* Write the buffer to the file */
    877 
    878     FileSize = strlen (FileBuffer);
    879     write (DestHandle, FileBuffer, FileSize);
    880 
    881     close (DestHandle);
    882 
    883     return 0;
    884 }
    885 
    886 
    887