]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/contrib/dev/acpica/common/adfile.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / contrib / dev / acpica / common / adfile.c
1 /******************************************************************************
2  *
3  * Module Name: adfile - Application-level disassembler file support routines
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2011, 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
45 #include <contrib/dev/acpica/include/acpi.h>
46 #include <contrib/dev/acpica/include/accommon.h>
47 #include <contrib/dev/acpica/include/acapps.h>
48
49 #include <stdio.h>
50
51
52 #define _COMPONENT          ACPI_TOOLS
53         ACPI_MODULE_NAME    ("adfile")
54
55 /* Local prototypes */
56
57 static INT32
58 AdWriteBuffer (
59     char                    *Filename,
60     char                    *Buffer,
61     UINT32                  Length);
62
63 static char                 FilenameBuf[20];
64
65
66 /******************************************************************************
67  *
68  * FUNCTION:    AfGenerateFilename
69  *
70  * PARAMETERS:  Prefix              - prefix string
71  *              TableId             - The table ID
72  *
73  * RETURN:      Pointer to the completed string
74  *
75  * DESCRIPTION: Build an output filename from an ACPI table ID string
76  *
77  ******************************************************************************/
78
79 char *
80 AdGenerateFilename (
81     char                    *Prefix,
82     char                    *TableId)
83 {
84     UINT32                  i;
85     UINT32                  j;
86
87
88     for (i = 0; Prefix[i]; i++)
89     {
90         FilenameBuf[i] = Prefix[i];
91     }
92
93     FilenameBuf[i] = '_';
94     i++;
95
96     for (j = 0; j < 8 && (TableId[j] != ' ') && (TableId[j] != 0); i++, j++)
97     {
98         FilenameBuf[i] = TableId[j];
99     }
100
101     FilenameBuf[i] = 0;
102     strcat (FilenameBuf, ACPI_TABLE_FILE_SUFFIX);
103     return FilenameBuf;
104 }
105
106
107 /******************************************************************************
108  *
109  * FUNCTION:    AfWriteBuffer
110  *
111  * PARAMETERS:  Filename            - name of file
112  *              Buffer              - data to write
113  *              Length              - length of data
114  *
115  * RETURN:      Actual number of bytes written
116  *
117  * DESCRIPTION: Open a file and write out a single buffer
118  *
119  ******************************************************************************/
120
121 static INT32
122 AdWriteBuffer (
123     char                    *Filename,
124     char                    *Buffer,
125     UINT32                  Length)
126 {
127     FILE                    *fp;
128     ACPI_SIZE               Actual;
129
130
131     fp = fopen (Filename, "wb");
132     if (!fp)
133     {
134         printf ("Couldn't open %s\n", Filename);
135         return (-1);
136     }
137
138     Actual = fwrite (Buffer, (size_t) Length, 1, fp);
139     fclose (fp);
140     return ((INT32) Actual);
141 }
142
143
144 /******************************************************************************
145  *
146  * FUNCTION:    AfWriteTable
147  *
148  * PARAMETERS:  Table               - pointer to the ACPI table
149  *              Length              - length of the table
150  *              TableName           - the table signature
151  *              OemTableID          - from the table header
152  *
153  * RETURN:      None
154  *
155  * DESCRIPTION: Dump the loaded tables to a file (or files)
156  *
157  ******************************************************************************/
158
159 void
160 AdWriteTable (
161     ACPI_TABLE_HEADER       *Table,
162     UINT32                  Length,
163     char                    *TableName,
164     char                    *OemTableId)
165 {
166     char                    *Filename;
167
168
169     Filename = AdGenerateFilename (TableName, OemTableId);
170     AdWriteBuffer (Filename, (char *) Table, Length);
171
172     AcpiOsPrintf ("Table [%s] written to \"%s\"\n", TableName, Filename);
173 }
174
175
176 /*******************************************************************************
177  *
178  * FUNCTION:    FlGenerateFilename
179  *
180  * PARAMETERS:  InputFilename       - Original ASL source filename
181  *              Suffix              - New extension.
182  *
183  * RETURN:      New filename containing the original base + the new suffix
184  *
185  * DESCRIPTION: Generate a new filename from the ASL source filename and a new
186  *              extension.  Used to create the *.LST, *.TXT, etc. files.
187  *
188  ******************************************************************************/
189
190 char *
191 FlGenerateFilename (
192     char                    *InputFilename,
193     char                    *Suffix)
194 {
195     char                    *Position;
196     char                    *NewFilename;
197
198
199     /*
200      * Copy the original filename to a new buffer. Leave room for the worst case
201      * where we append the suffix, an added dot and the null terminator.
202      */
203     NewFilename = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE)
204         strlen (InputFilename) + strlen (Suffix) + 2);
205     strcpy (NewFilename, InputFilename);
206
207     /* Try to find the last dot in the filename */
208
209     Position = strrchr (NewFilename, '.');
210     if (Position)
211     {
212         /* Tack on the new suffix */
213
214         Position++;
215         *Position = 0;
216         strcat (Position, Suffix);
217     }
218     else
219     {
220         /* No dot, add one and then the suffix */
221
222         strcat (NewFilename, ".");
223         strcat (NewFilename, Suffix);
224     }
225
226     return NewFilename;
227 }
228
229
230 /*******************************************************************************
231  *
232  * FUNCTION:    FlStrdup
233  *
234  * DESCRIPTION: Local strdup function
235  *
236  ******************************************************************************/
237
238 static char *
239 FlStrdup (
240     char                *String)
241 {
242     char                *NewString;
243
244
245     NewString = ACPI_ALLOCATE ((ACPI_SIZE) strlen (String) + 1);
246     if (!NewString)
247     {
248         return (NULL);
249     }
250
251     strcpy (NewString, String);
252     return (NewString);
253 }
254
255
256 /*******************************************************************************
257  *
258  * FUNCTION:    FlSplitInputPathname
259  *
260  * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
261  *                                    compiled
262  *              OutDirectoryPath    - Where the directory path prefix is
263  *                                    returned
264  *              OutFilename         - Where the filename part is returned
265  *
266  * RETURN:      Status
267  *
268  * DESCRIPTION: Split the input path into a directory and filename part
269  *              1) Directory part used to open include files
270  *              2) Filename part used to generate output filenames
271  *
272  ******************************************************************************/
273
274 ACPI_STATUS
275 FlSplitInputPathname (
276     char                    *InputPath,
277     char                    **OutDirectoryPath,
278     char                    **OutFilename)
279 {
280     char                    *Substring;
281     char                    *DirectoryPath;
282     char                    *Filename;
283
284
285     *OutDirectoryPath = NULL;
286     *OutFilename = NULL;
287
288     if (!InputPath)
289     {
290         return (AE_OK);
291     }
292
293     /* Get the path to the input filename's directory */
294
295     DirectoryPath = FlStrdup (InputPath);
296     if (!DirectoryPath)
297     {
298         return (AE_NO_MEMORY);
299     }
300
301     Substring = strrchr (DirectoryPath, '\\');
302     if (!Substring)
303     {
304         Substring = strrchr (DirectoryPath, '/');
305         if (!Substring)
306         {
307             Substring = strrchr (DirectoryPath, ':');
308         }
309     }
310
311     if (!Substring)
312     {
313         DirectoryPath[0] = 0;
314         Filename = FlStrdup (InputPath);
315     }
316     else
317     {
318         Filename = FlStrdup (Substring + 1);
319         *(Substring+1) = 0;
320     }
321
322     if (!Filename)
323     {
324         return (AE_NO_MEMORY);
325     }
326
327     *OutDirectoryPath = DirectoryPath;
328     *OutFilename = Filename;
329
330     return (AE_OK);
331 }
332
333