Home | History | Annotate | Line # | Download | only in compiler
aslfileio.c revision 1.5
      1 /******************************************************************************
      2  *
      3  * Module Name: aslfileio - File I/O support
      4  *
      5  *****************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2015, Intel Corp.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions, and the following disclaimer,
     16  *    without modification.
     17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  *    including a substantially similar Disclaimer requirement for further
     21  *    binary redistribution.
     22  * 3. Neither the names of the above-listed copyright holders nor the names
     23  *    of any contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * Alternatively, this software may be distributed under the terms of the
     27  * GNU General Public License ("GPL") version 2 as published by the Free
     28  * Software Foundation.
     29  *
     30  * NO WARRANTY
     31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGES.
     42  */
     43 
     44 #include "aslcompiler.h"
     45 #include "acapps.h"
     46 
     47 #define _COMPONENT          ACPI_COMPILER
     48         ACPI_MODULE_NAME    ("aslfileio")
     49 
     50 
     51 /*******************************************************************************
     52  *
     53  * FUNCTION:    FlFileError
     54  *
     55  * PARAMETERS:  FileId              - Index into file info array
     56  *              ErrorId             - Index into error message array
     57  *
     58  * RETURN:      None
     59  *
     60  * DESCRIPTION: Decode errno to an error message and add the entire error
     61  *              to the error log.
     62  *
     63  ******************************************************************************/
     64 
     65 void
     66 FlFileError (
     67     UINT32                  FileId,
     68     UINT8                   ErrorId)
     69 {
     70 
     71     snprintf (MsgBuffer, sizeof(MsgBuffer), "\"%s\" (%s) - %s", Gbl_Files[FileId].Filename,
     72         Gbl_Files[FileId].Description, strerror (errno));
     73     AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
     74 }
     75 
     76 
     77 /*******************************************************************************
     78  *
     79  * FUNCTION:    FlOpenFile
     80  *
     81  * PARAMETERS:  FileId              - Index into file info array
     82  *              Filename            - file pathname to open
     83  *              Mode                - Open mode for fopen
     84  *
     85  * RETURN:      None
     86  *
     87  * DESCRIPTION: Open a file.
     88  *              NOTE: Aborts compiler on any error.
     89  *
     90  ******************************************************************************/
     91 
     92 void
     93 FlOpenFile (
     94     UINT32                  FileId,
     95     char                    *Filename,
     96     char                    *Mode)
     97 {
     98     FILE                    *File;
     99 
    100 
    101     Gbl_Files[FileId].Filename = Filename;
    102     Gbl_Files[FileId].Handle = NULL;
    103 
    104     File = fopen (Filename, Mode);
    105     if (!File)
    106     {
    107         FlFileError (FileId, ASL_MSG_OPEN);
    108         AslAbort ();
    109     }
    110 
    111     Gbl_Files[FileId].Handle = File;
    112 }
    113 
    114 
    115 /*******************************************************************************
    116  *
    117  * FUNCTION:    FlGetFileSize
    118  *
    119  * PARAMETERS:  FileId              - Index into file info array
    120  *
    121  * RETURN:      File Size
    122  *
    123  * DESCRIPTION: Get current file size. Uses common seek-to-EOF function.
    124  *              File must be open. Aborts compiler on error.
    125  *
    126  ******************************************************************************/
    127 
    128 UINT32
    129 FlGetFileSize (
    130     UINT32                  FileId)
    131 {
    132     UINT32                  FileSize;
    133 
    134 
    135     FileSize = CmGetFileSize (Gbl_Files[FileId].Handle);
    136     if (FileSize == ACPI_UINT32_MAX)
    137     {
    138         AslAbort();
    139     }
    140 
    141     return (FileSize);
    142 }
    143 
    144 
    145 /*******************************************************************************
    146  *
    147  * FUNCTION:    FlReadFile
    148  *
    149  * PARAMETERS:  FileId              - Index into file info array
    150  *              Buffer              - Where to place the data
    151  *              Length              - Amount to read
    152  *
    153  * RETURN:      Status. AE_ERROR indicates EOF.
    154  *
    155  * DESCRIPTION: Read data from an open file.
    156  *              NOTE: Aborts compiler on any error.
    157  *
    158  ******************************************************************************/
    159 
    160 ACPI_STATUS
    161 FlReadFile (
    162     UINT32                  FileId,
    163     void                    *Buffer,
    164     UINT32                  Length)
    165 {
    166     UINT32                  Actual;
    167 
    168 
    169     /* Read and check for error */
    170 
    171     Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
    172     if (Actual < Length)
    173     {
    174         if (feof (Gbl_Files[FileId].Handle))
    175         {
    176             /* End-of-file, just return error */
    177 
    178             return (AE_ERROR);
    179         }
    180 
    181         FlFileError (FileId, ASL_MSG_READ);
    182         AslAbort ();
    183     }
    184 
    185     return (AE_OK);
    186 }
    187 
    188 
    189 /*******************************************************************************
    190  *
    191  * FUNCTION:    FlWriteFile
    192  *
    193  * PARAMETERS:  FileId              - Index into file info array
    194  *              Buffer              - Data to write
    195  *              Length              - Amount of data to write
    196  *
    197  * RETURN:      None
    198  *
    199  * DESCRIPTION: Write data to an open file.
    200  *              NOTE: Aborts compiler on any error.
    201  *
    202  ******************************************************************************/
    203 
    204 void
    205 FlWriteFile (
    206     UINT32                  FileId,
    207     void                    *Buffer,
    208     UINT32                  Length)
    209 {
    210     UINT32                  Actual;
    211 
    212 
    213     /* Write and check for error */
    214 
    215     Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
    216     if (Actual != Length)
    217     {
    218         FlFileError (FileId, ASL_MSG_WRITE);
    219         AslAbort ();
    220     }
    221 
    222     if ((FileId == ASL_FILE_PREPROCESSOR) && Gbl_PreprocessorOutputFlag)
    223     {
    224         /* Duplicate the output to the user preprocessor (.i) file */
    225 
    226         Actual = fwrite ((char *) Buffer, 1, Length,
    227             Gbl_Files[ASL_FILE_PREPROCESSOR_USER].Handle);
    228         if (Actual != Length)
    229         {
    230             FlFileError (FileId, ASL_MSG_WRITE);
    231             AslAbort ();
    232         }
    233     }
    234 }
    235 
    236 
    237 /*******************************************************************************
    238  *
    239  * FUNCTION:    FlPrintFile
    240  *
    241  * PARAMETERS:  FileId              - Index into file info array
    242  *              Format              - Printf format string
    243  *              ...                 - Printf arguments
    244  *
    245  * RETURN:      None
    246  *
    247  * DESCRIPTION: Formatted write to an open file.
    248  *              NOTE: Aborts compiler on any error.
    249  *
    250  ******************************************************************************/
    251 
    252 void
    253 FlPrintFile (
    254     UINT32                  FileId,
    255     char                    *Format,
    256     ...)
    257 {
    258     INT32                   Actual;
    259     va_list                 Args;
    260 
    261 
    262     va_start (Args, Format);
    263     Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
    264     va_end (Args);
    265 
    266     if (Actual == -1)
    267     {
    268         FlFileError (FileId, ASL_MSG_WRITE);
    269         AslAbort ();
    270     }
    271 
    272     if ((FileId == ASL_FILE_PREPROCESSOR) && Gbl_PreprocessorOutputFlag)
    273     {
    274         /*
    275          * Duplicate the output to the user preprocessor (.i) file,
    276          * except: no #line directives.
    277          */
    278         if (!strncmp (Format, "#line", 5))
    279         {
    280             return;
    281         }
    282 
    283         va_start (Args, Format);
    284         Actual = vfprintf (Gbl_Files[ASL_FILE_PREPROCESSOR_USER].Handle,
    285             Format, Args);
    286         va_end (Args);
    287 
    288         if (Actual == -1)
    289         {
    290             FlFileError (FileId, ASL_MSG_WRITE);
    291             AslAbort ();
    292         }
    293     }
    294 
    295 }
    296 
    297 
    298 /*******************************************************************************
    299  *
    300  * FUNCTION:    FlSeekFile
    301  *
    302  * PARAMETERS:  FileId              - Index into file info array
    303  *              Offset              - Absolute byte offset in file
    304  *
    305  * RETURN:      None
    306  *
    307  * DESCRIPTION: Seek to absolute offset.
    308  *              NOTE: Aborts compiler on any error.
    309  *
    310  ******************************************************************************/
    311 
    312 void
    313 FlSeekFile (
    314     UINT32                  FileId,
    315     long                    Offset)
    316 {
    317     int                     Error;
    318 
    319 
    320     Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
    321     if (Error)
    322     {
    323         FlFileError (FileId, ASL_MSG_SEEK);
    324         AslAbort ();
    325     }
    326 }
    327 
    328 
    329 /*******************************************************************************
    330  *
    331  * FUNCTION:    FlCloseFile
    332  *
    333  * PARAMETERS:  FileId              - Index into file info array
    334  *
    335  * RETURN:      None
    336  *
    337  * DESCRIPTION: Close an open file. Aborts compiler on error
    338  *
    339  ******************************************************************************/
    340 
    341 void
    342 FlCloseFile (
    343     UINT32                  FileId)
    344 {
    345     int                     Error;
    346 
    347 
    348     if (!Gbl_Files[FileId].Handle)
    349     {
    350         return;
    351     }
    352 
    353     Error = fclose (Gbl_Files[FileId].Handle);
    354     if (Error)
    355     {
    356         FlFileError (FileId, ASL_MSG_CLOSE);
    357         AslAbort ();
    358     }
    359 
    360     /* Do not clear/free the filename string */
    361 
    362     Gbl_Files[FileId].Handle = NULL;
    363     return;
    364 }
    365 
    366 
    367 /*******************************************************************************
    368  *
    369  * FUNCTION:    FlDeleteFile
    370  *
    371  * PARAMETERS:  FileId              - Index into file info array
    372  *
    373  * RETURN:      None
    374  *
    375  * DESCRIPTION: Delete a file.
    376  *
    377  ******************************************************************************/
    378 
    379 void
    380 FlDeleteFile (
    381     UINT32                  FileId)
    382 {
    383     ASL_FILE_INFO           *Info = &Gbl_Files[FileId];
    384 
    385 
    386     if (!Info->Filename)
    387     {
    388         return;
    389     }
    390 
    391     if (remove (Info->Filename))
    392     {
    393         printf ("%s (%s file) ",
    394             Info->Filename, Info->Description);
    395         perror ("Could not delete");
    396     }
    397 
    398     Info->Filename = NULL;
    399     return;
    400 }
    401