1167802Sjkim/******************************************************************************
2167802Sjkim *
3167802Sjkim * Module Name: adfile - Application-level disassembler file support routines
4167802Sjkim *
5167802Sjkim *****************************************************************************/
6167802Sjkim
7217365Sjkim/*
8217365Sjkim * Copyright (C) 2000 - 2011, Intel Corp.
9167802Sjkim * All rights reserved.
10167802Sjkim *
11217365Sjkim * Redistribution and use in source and binary forms, with or without
12217365Sjkim * modification, are permitted provided that the following conditions
13217365Sjkim * are met:
14217365Sjkim * 1. Redistributions of source code must retain the above copyright
15217365Sjkim *    notice, this list of conditions, and the following disclaimer,
16217365Sjkim *    without modification.
17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20217365Sjkim *    including a substantially similar Disclaimer requirement for further
21217365Sjkim *    binary redistribution.
22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23217365Sjkim *    of any contributors may be used to endorse or promote products derived
24217365Sjkim *    from this software without specific prior written permission.
25167802Sjkim *
26217365Sjkim * Alternatively, this software may be distributed under the terms of the
27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28217365Sjkim * Software Foundation.
29167802Sjkim *
30217365Sjkim * NO WARRANTY
31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41217365Sjkim * POSSIBILITY OF SUCH DAMAGES.
42217365Sjkim */
43167802Sjkim
44167802Sjkim
45193529Sjkim#include <contrib/dev/acpica/include/acpi.h>
46193529Sjkim#include <contrib/dev/acpica/include/accommon.h>
47193529Sjkim#include <contrib/dev/acpica/include/acapps.h>
48167802Sjkim
49167802Sjkim#include <stdio.h>
50167802Sjkim
51167802Sjkim
52167802Sjkim#define _COMPONENT          ACPI_TOOLS
53167802Sjkim        ACPI_MODULE_NAME    ("adfile")
54167802Sjkim
55193529Sjkim/* Local prototypes */
56167802Sjkim
57212761Sjkimstatic INT32
58193529SjkimAdWriteBuffer (
59193529Sjkim    char                    *Filename,
60193529Sjkim    char                    *Buffer,
61193529Sjkim    UINT32                  Length);
62193529Sjkim
63212761Sjkimstatic char                 FilenameBuf[20];
64167802Sjkim
65198237Sjkim
66167802Sjkim/******************************************************************************
67167802Sjkim *
68167802Sjkim * FUNCTION:    AfGenerateFilename
69167802Sjkim *
70198237Sjkim * PARAMETERS:  Prefix              - prefix string
71198237Sjkim *              TableId             - The table ID
72167802Sjkim *
73167802Sjkim * RETURN:      Pointer to the completed string
74167802Sjkim *
75167802Sjkim * DESCRIPTION: Build an output filename from an ACPI table ID string
76167802Sjkim *
77167802Sjkim ******************************************************************************/
78167802Sjkim
79167802Sjkimchar *
80167802SjkimAdGenerateFilename (
81167802Sjkim    char                    *Prefix,
82167802Sjkim    char                    *TableId)
83167802Sjkim{
84193529Sjkim    UINT32                  i;
85193529Sjkim    UINT32                  j;
86167802Sjkim
87167802Sjkim
88167802Sjkim    for (i = 0; Prefix[i]; i++)
89167802Sjkim    {
90167802Sjkim        FilenameBuf[i] = Prefix[i];
91167802Sjkim    }
92167802Sjkim
93167802Sjkim    FilenameBuf[i] = '_';
94167802Sjkim    i++;
95167802Sjkim
96167802Sjkim    for (j = 0; j < 8 && (TableId[j] != ' ') && (TableId[j] != 0); i++, j++)
97167802Sjkim    {
98167802Sjkim        FilenameBuf[i] = TableId[j];
99167802Sjkim    }
100167802Sjkim
101167802Sjkim    FilenameBuf[i] = 0;
102167802Sjkim    strcat (FilenameBuf, ACPI_TABLE_FILE_SUFFIX);
103167802Sjkim    return FilenameBuf;
104167802Sjkim}
105167802Sjkim
106167802Sjkim
107167802Sjkim/******************************************************************************
108167802Sjkim *
109167802Sjkim * FUNCTION:    AfWriteBuffer
110167802Sjkim *
111198237Sjkim * PARAMETERS:  Filename            - name of file
112198237Sjkim *              Buffer              - data to write
113198237Sjkim *              Length              - length of data
114167802Sjkim *
115167802Sjkim * RETURN:      Actual number of bytes written
116167802Sjkim *
117167802Sjkim * DESCRIPTION: Open a file and write out a single buffer
118167802Sjkim *
119167802Sjkim ******************************************************************************/
120167802Sjkim
121212761Sjkimstatic INT32
122167802SjkimAdWriteBuffer (
123193529Sjkim    char                    *Filename,
124193529Sjkim    char                    *Buffer,
125193529Sjkim    UINT32                  Length)
126167802Sjkim{
127193529Sjkim    FILE                    *fp;
128193529Sjkim    ACPI_SIZE               Actual;
129167802Sjkim
130167802Sjkim
131167802Sjkim    fp = fopen (Filename, "wb");
132167802Sjkim    if (!fp)
133167802Sjkim    {
134167802Sjkim        printf ("Couldn't open %s\n", Filename);
135167802Sjkim        return (-1);
136167802Sjkim    }
137167802Sjkim
138167802Sjkim    Actual = fwrite (Buffer, (size_t) Length, 1, fp);
139167802Sjkim    fclose (fp);
140193529Sjkim    return ((INT32) Actual);
141167802Sjkim}
142167802Sjkim
143167802Sjkim
144167802Sjkim/******************************************************************************
145167802Sjkim *
146167802Sjkim * FUNCTION:    AfWriteTable
147167802Sjkim *
148198237Sjkim * PARAMETERS:  Table               - pointer to the ACPI table
149198237Sjkim *              Length              - length of the table
150198237Sjkim *              TableName           - the table signature
151198237Sjkim *              OemTableID          - from the table header
152167802Sjkim *
153167802Sjkim * RETURN:      None
154167802Sjkim *
155167802Sjkim * DESCRIPTION: Dump the loaded tables to a file (or files)
156167802Sjkim *
157167802Sjkim ******************************************************************************/
158167802Sjkim
159167802Sjkimvoid
160167802SjkimAdWriteTable (
161167802Sjkim    ACPI_TABLE_HEADER       *Table,
162167802Sjkim    UINT32                  Length,
163167802Sjkim    char                    *TableName,
164167802Sjkim    char                    *OemTableId)
165167802Sjkim{
166167802Sjkim    char                    *Filename;
167167802Sjkim
168167802Sjkim
169167802Sjkim    Filename = AdGenerateFilename (TableName, OemTableId);
170167802Sjkim    AdWriteBuffer (Filename, (char *) Table, Length);
171167802Sjkim
172167802Sjkim    AcpiOsPrintf ("Table [%s] written to \"%s\"\n", TableName, Filename);
173167802Sjkim}
174167802Sjkim
175167802Sjkim
176167802Sjkim/*******************************************************************************
177167802Sjkim *
178167802Sjkim * FUNCTION:    FlGenerateFilename
179167802Sjkim *
180167802Sjkim * PARAMETERS:  InputFilename       - Original ASL source filename
181167802Sjkim *              Suffix              - New extension.
182167802Sjkim *
183167802Sjkim * RETURN:      New filename containing the original base + the new suffix
184167802Sjkim *
185167802Sjkim * DESCRIPTION: Generate a new filename from the ASL source filename and a new
186167802Sjkim *              extension.  Used to create the *.LST, *.TXT, etc. files.
187167802Sjkim *
188167802Sjkim ******************************************************************************/
189167802Sjkim
190167802Sjkimchar *
191167802SjkimFlGenerateFilename (
192167802Sjkim    char                    *InputFilename,
193167802Sjkim    char                    *Suffix)
194167802Sjkim{
195167802Sjkim    char                    *Position;
196167802Sjkim    char                    *NewFilename;
197167802Sjkim
198167802Sjkim
199167802Sjkim    /*
200167802Sjkim     * Copy the original filename to a new buffer. Leave room for the worst case
201167802Sjkim     * where we append the suffix, an added dot and the null terminator.
202167802Sjkim     */
203198237Sjkim    NewFilename = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE)
204167802Sjkim        strlen (InputFilename) + strlen (Suffix) + 2);
205167802Sjkim    strcpy (NewFilename, InputFilename);
206167802Sjkim
207167802Sjkim    /* Try to find the last dot in the filename */
208167802Sjkim
209167802Sjkim    Position = strrchr (NewFilename, '.');
210167802Sjkim    if (Position)
211167802Sjkim    {
212167802Sjkim        /* Tack on the new suffix */
213167802Sjkim
214167802Sjkim        Position++;
215167802Sjkim        *Position = 0;
216167802Sjkim        strcat (Position, Suffix);
217167802Sjkim    }
218167802Sjkim    else
219167802Sjkim    {
220167802Sjkim        /* No dot, add one and then the suffix */
221167802Sjkim
222167802Sjkim        strcat (NewFilename, ".");
223167802Sjkim        strcat (NewFilename, Suffix);
224167802Sjkim    }
225167802Sjkim
226167802Sjkim    return NewFilename;
227167802Sjkim}
228167802Sjkim
229167802Sjkim
230167802Sjkim/*******************************************************************************
231167802Sjkim *
232167802Sjkim * FUNCTION:    FlStrdup
233167802Sjkim *
234167802Sjkim * DESCRIPTION: Local strdup function
235167802Sjkim *
236167802Sjkim ******************************************************************************/
237167802Sjkim
238167802Sjkimstatic char *
239167802SjkimFlStrdup (
240167802Sjkim    char                *String)
241167802Sjkim{
242167802Sjkim    char                *NewString;
243167802Sjkim
244167802Sjkim
245198237Sjkim    NewString = ACPI_ALLOCATE ((ACPI_SIZE) strlen (String) + 1);
246167802Sjkim    if (!NewString)
247167802Sjkim    {
248167802Sjkim        return (NULL);
249167802Sjkim    }
250167802Sjkim
251167802Sjkim    strcpy (NewString, String);
252167802Sjkim    return (NewString);
253167802Sjkim}
254167802Sjkim
255167802Sjkim
256167802Sjkim/*******************************************************************************
257167802Sjkim *
258167802Sjkim * FUNCTION:    FlSplitInputPathname
259167802Sjkim *
260167802Sjkim * PARAMETERS:  InputFilename       - The user-specified ASL source file to be
261167802Sjkim *                                    compiled
262167802Sjkim *              OutDirectoryPath    - Where the directory path prefix is
263167802Sjkim *                                    returned
264167802Sjkim *              OutFilename         - Where the filename part is returned
265167802Sjkim *
266167802Sjkim * RETURN:      Status
267167802Sjkim *
268167802Sjkim * DESCRIPTION: Split the input path into a directory and filename part
269167802Sjkim *              1) Directory part used to open include files
270167802Sjkim *              2) Filename part used to generate output filenames
271167802Sjkim *
272167802Sjkim ******************************************************************************/
273167802Sjkim
274167802SjkimACPI_STATUS
275167802SjkimFlSplitInputPathname (
276167802Sjkim    char                    *InputPath,
277167802Sjkim    char                    **OutDirectoryPath,
278167802Sjkim    char                    **OutFilename)
279167802Sjkim{
280167802Sjkim    char                    *Substring;
281167802Sjkim    char                    *DirectoryPath;
282167802Sjkim    char                    *Filename;
283167802Sjkim
284167802Sjkim
285167802Sjkim    *OutDirectoryPath = NULL;
286167802Sjkim    *OutFilename = NULL;
287167802Sjkim
288167802Sjkim    if (!InputPath)
289167802Sjkim    {
290167802Sjkim        return (AE_OK);
291167802Sjkim    }
292167802Sjkim
293167802Sjkim    /* Get the path to the input filename's directory */
294167802Sjkim
295167802Sjkim    DirectoryPath = FlStrdup (InputPath);
296167802Sjkim    if (!DirectoryPath)
297167802Sjkim    {
298167802Sjkim        return (AE_NO_MEMORY);
299167802Sjkim    }
300167802Sjkim
301167802Sjkim    Substring = strrchr (DirectoryPath, '\\');
302167802Sjkim    if (!Substring)
303167802Sjkim    {
304167802Sjkim        Substring = strrchr (DirectoryPath, '/');
305167802Sjkim        if (!Substring)
306167802Sjkim        {
307167802Sjkim            Substring = strrchr (DirectoryPath, ':');
308167802Sjkim        }
309167802Sjkim    }
310167802Sjkim
311167802Sjkim    if (!Substring)
312167802Sjkim    {
313167802Sjkim        DirectoryPath[0] = 0;
314167802Sjkim        Filename = FlStrdup (InputPath);
315167802Sjkim    }
316167802Sjkim    else
317167802Sjkim    {
318167802Sjkim        Filename = FlStrdup (Substring + 1);
319167802Sjkim        *(Substring+1) = 0;
320167802Sjkim    }
321167802Sjkim
322167802Sjkim    if (!Filename)
323167802Sjkim    {
324167802Sjkim        return (AE_NO_MEMORY);
325167802Sjkim    }
326167802Sjkim
327167802Sjkim    *OutDirectoryPath = DirectoryPath;
328167802Sjkim    *OutFilename = Filename;
329167802Sjkim
330167802Sjkim    return (AE_OK);
331167802Sjkim}
332167802Sjkim
333167802Sjkim
334