1 /******************************************************************************
3 * Module Name: aslfileio - File I/O support
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
46 #define _COMPONENT ACPI_COMPILER
47 ACPI_MODULE_NAME ("aslfileio")
50 /*******************************************************************************
58 * DESCRIPTION: Dump the error log and abort the compiler. Used for serious
61 ******************************************************************************/
68 AePrintErrorLog (ASL_FILE_STDERR);
71 /* Print error summary to stdout also */
73 AePrintErrorLog (ASL_FILE_STDOUT);
80 /*******************************************************************************
82 * FUNCTION: FlFileError
84 * PARAMETERS: FileId - Index into file info array
85 * ErrorId - Index into error message array
89 * DESCRIPTION: Decode errno to an error message and add the entire error
92 ******************************************************************************/
100 sprintf (MsgBuffer, "\"%s\" (%s)", Gbl_Files[FileId].Filename,
102 AslCommonError (ASL_ERROR, ErrorId, 0, 0, 0, 0, NULL, MsgBuffer);
106 /*******************************************************************************
108 * FUNCTION: FlOpenFile
110 * PARAMETERS: FileId - Index into file info array
111 * Filename - file pathname to open
112 * Mode - Open mode for fopen
116 * DESCRIPTION: Open a file.
117 * NOTE: Aborts compiler on any error.
119 ******************************************************************************/
130 File = fopen (Filename, Mode);
133 FlFileError (FileId, ASL_MSG_OPEN);
137 Gbl_Files[FileId].Filename = Filename;
138 Gbl_Files[FileId].Handle = File;
142 /*******************************************************************************
144 * FUNCTION: FlGetFileSize
146 * PARAMETERS: FileId - Index into file info array
150 * DESCRIPTION: Get current file size. Uses seek-to-EOF. File must be open.
152 ******************************************************************************/
163 fp = Gbl_Files[FileId].Handle;
166 fseek (fp, 0, SEEK_END);
167 FileSize = (UINT32) ftell (fp);
169 /* Restore file pointer */
171 fseek (fp, Offset, SEEK_SET);
176 /*******************************************************************************
178 * FUNCTION: FlReadFile
180 * PARAMETERS: FileId - Index into file info array
181 * Buffer - Where to place the data
182 * Length - Amount to read
184 * RETURN: Status. AE_ERROR indicates EOF.
186 * DESCRIPTION: Read data from an open file.
187 * NOTE: Aborts compiler on any error.
189 ******************************************************************************/
200 /* Read and check for error */
202 Actual = fread (Buffer, 1, Length, Gbl_Files[FileId].Handle);
205 if (feof (Gbl_Files[FileId].Handle))
207 /* End-of-file, just return error */
212 FlFileError (FileId, ASL_MSG_READ);
220 /*******************************************************************************
222 * FUNCTION: FlWriteFile
224 * PARAMETERS: FileId - Index into file info array
225 * Buffer - Data to write
226 * Length - Amount of data to write
230 * DESCRIPTION: Write data to an open file.
231 * NOTE: Aborts compiler on any error.
233 ******************************************************************************/
244 /* Write and check for error */
246 Actual = fwrite ((char *) Buffer, 1, Length, Gbl_Files[FileId].Handle);
247 if (Actual != Length)
249 FlFileError (FileId, ASL_MSG_WRITE);
255 /*******************************************************************************
257 * FUNCTION: FlPrintFile
259 * PARAMETERS: FileId - Index into file info array
260 * Format - Printf format string
261 * ... - Printf arguments
265 * DESCRIPTION: Formatted write to an open file.
266 * NOTE: Aborts compiler on any error.
268 ******************************************************************************/
280 va_start (Args, Format);
282 Actual = vfprintf (Gbl_Files[FileId].Handle, Format, Args);
287 FlFileError (FileId, ASL_MSG_WRITE);
293 /*******************************************************************************
295 * FUNCTION: FlSeekFile
297 * PARAMETERS: FileId - Index into file info array
298 * Offset - Absolute byte offset in file
302 * DESCRIPTION: Seek to absolute offset.
303 * NOTE: Aborts compiler on any error.
305 ******************************************************************************/
315 Error = fseek (Gbl_Files[FileId].Handle, Offset, SEEK_SET);
318 FlFileError (FileId, ASL_MSG_SEEK);
324 /*******************************************************************************
326 * FUNCTION: FlCloseFile
328 * PARAMETERS: FileId - Index into file info array
332 * DESCRIPTION: Close an open file. Aborts compiler on error
334 ******************************************************************************/
343 if (!Gbl_Files[FileId].Handle)
348 Error = fclose (Gbl_Files[FileId].Handle);
351 FlFileError (FileId, ASL_MSG_CLOSE);
355 Gbl_Files[FileId].Handle = NULL;
360 /*******************************************************************************
362 * FUNCTION: FlDeleteFile
364 * PARAMETERS: FileId - Index into file info array
368 * DESCRIPTION: Delete a file.
370 ******************************************************************************/
376 ASL_FILE_INFO *Info = &Gbl_Files[FileId];
384 if (remove (Info->Filename))
386 printf ("%s (%s file) ",
387 Info->Filename, Info->Description);
388 perror ("Could not delete");
391 Info->Filename = NULL;