PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/sys/contrib/dev/acpica/common/adfile.c

https://github.com/blacklion/GEOM-Events
C | 333 lines | 147 code | 60 blank | 126 comment | 15 complexity | d62d103e2ccc0baf6889647ebc6c6590 MD5 | raw file
  1. /******************************************************************************
  2. *
  3. * Module Name: adfile - Application-level disassembler file support routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2011, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <contrib/dev/acpica/include/acpi.h>
  43. #include <contrib/dev/acpica/include/accommon.h>
  44. #include <contrib/dev/acpica/include/acapps.h>
  45. #include <stdio.h>
  46. #define _COMPONENT ACPI_TOOLS
  47. ACPI_MODULE_NAME ("adfile")
  48. /* Local prototypes */
  49. static INT32
  50. AdWriteBuffer (
  51. char *Filename,
  52. char *Buffer,
  53. UINT32 Length);
  54. static char FilenameBuf[20];
  55. /******************************************************************************
  56. *
  57. * FUNCTION: AfGenerateFilename
  58. *
  59. * PARAMETERS: Prefix - prefix string
  60. * TableId - The table ID
  61. *
  62. * RETURN: Pointer to the completed string
  63. *
  64. * DESCRIPTION: Build an output filename from an ACPI table ID string
  65. *
  66. ******************************************************************************/
  67. char *
  68. AdGenerateFilename (
  69. char *Prefix,
  70. char *TableId)
  71. {
  72. UINT32 i;
  73. UINT32 j;
  74. for (i = 0; Prefix[i]; i++)
  75. {
  76. FilenameBuf[i] = Prefix[i];
  77. }
  78. FilenameBuf[i] = '_';
  79. i++;
  80. for (j = 0; j < 8 && (TableId[j] != ' ') && (TableId[j] != 0); i++, j++)
  81. {
  82. FilenameBuf[i] = TableId[j];
  83. }
  84. FilenameBuf[i] = 0;
  85. strcat (FilenameBuf, ACPI_TABLE_FILE_SUFFIX);
  86. return FilenameBuf;
  87. }
  88. /******************************************************************************
  89. *
  90. * FUNCTION: AfWriteBuffer
  91. *
  92. * PARAMETERS: Filename - name of file
  93. * Buffer - data to write
  94. * Length - length of data
  95. *
  96. * RETURN: Actual number of bytes written
  97. *
  98. * DESCRIPTION: Open a file and write out a single buffer
  99. *
  100. ******************************************************************************/
  101. static INT32
  102. AdWriteBuffer (
  103. char *Filename,
  104. char *Buffer,
  105. UINT32 Length)
  106. {
  107. FILE *fp;
  108. ACPI_SIZE Actual;
  109. fp = fopen (Filename, "wb");
  110. if (!fp)
  111. {
  112. printf ("Couldn't open %s\n", Filename);
  113. return (-1);
  114. }
  115. Actual = fwrite (Buffer, (size_t) Length, 1, fp);
  116. fclose (fp);
  117. return ((INT32) Actual);
  118. }
  119. /******************************************************************************
  120. *
  121. * FUNCTION: AfWriteTable
  122. *
  123. * PARAMETERS: Table - pointer to the ACPI table
  124. * Length - length of the table
  125. * TableName - the table signature
  126. * OemTableID - from the table header
  127. *
  128. * RETURN: None
  129. *
  130. * DESCRIPTION: Dump the loaded tables to a file (or files)
  131. *
  132. ******************************************************************************/
  133. void
  134. AdWriteTable (
  135. ACPI_TABLE_HEADER *Table,
  136. UINT32 Length,
  137. char *TableName,
  138. char *OemTableId)
  139. {
  140. char *Filename;
  141. Filename = AdGenerateFilename (TableName, OemTableId);
  142. AdWriteBuffer (Filename, (char *) Table, Length);
  143. AcpiOsPrintf ("Table [%s] written to \"%s\"\n", TableName, Filename);
  144. }
  145. /*******************************************************************************
  146. *
  147. * FUNCTION: FlGenerateFilename
  148. *
  149. * PARAMETERS: InputFilename - Original ASL source filename
  150. * Suffix - New extension.
  151. *
  152. * RETURN: New filename containing the original base + the new suffix
  153. *
  154. * DESCRIPTION: Generate a new filename from the ASL source filename and a new
  155. * extension. Used to create the *.LST, *.TXT, etc. files.
  156. *
  157. ******************************************************************************/
  158. char *
  159. FlGenerateFilename (
  160. char *InputFilename,
  161. char *Suffix)
  162. {
  163. char *Position;
  164. char *NewFilename;
  165. /*
  166. * Copy the original filename to a new buffer. Leave room for the worst case
  167. * where we append the suffix, an added dot and the null terminator.
  168. */
  169. NewFilename = ACPI_ALLOCATE_ZEROED ((ACPI_SIZE)
  170. strlen (InputFilename) + strlen (Suffix) + 2);
  171. strcpy (NewFilename, InputFilename);
  172. /* Try to find the last dot in the filename */
  173. Position = strrchr (NewFilename, '.');
  174. if (Position)
  175. {
  176. /* Tack on the new suffix */
  177. Position++;
  178. *Position = 0;
  179. strcat (Position, Suffix);
  180. }
  181. else
  182. {
  183. /* No dot, add one and then the suffix */
  184. strcat (NewFilename, ".");
  185. strcat (NewFilename, Suffix);
  186. }
  187. return NewFilename;
  188. }
  189. /*******************************************************************************
  190. *
  191. * FUNCTION: FlStrdup
  192. *
  193. * DESCRIPTION: Local strdup function
  194. *
  195. ******************************************************************************/
  196. static char *
  197. FlStrdup (
  198. char *String)
  199. {
  200. char *NewString;
  201. NewString = ACPI_ALLOCATE ((ACPI_SIZE) strlen (String) + 1);
  202. if (!NewString)
  203. {
  204. return (NULL);
  205. }
  206. strcpy (NewString, String);
  207. return (NewString);
  208. }
  209. /*******************************************************************************
  210. *
  211. * FUNCTION: FlSplitInputPathname
  212. *
  213. * PARAMETERS: InputFilename - The user-specified ASL source file to be
  214. * compiled
  215. * OutDirectoryPath - Where the directory path prefix is
  216. * returned
  217. * OutFilename - Where the filename part is returned
  218. *
  219. * RETURN: Status
  220. *
  221. * DESCRIPTION: Split the input path into a directory and filename part
  222. * 1) Directory part used to open include files
  223. * 2) Filename part used to generate output filenames
  224. *
  225. ******************************************************************************/
  226. ACPI_STATUS
  227. FlSplitInputPathname (
  228. char *InputPath,
  229. char **OutDirectoryPath,
  230. char **OutFilename)
  231. {
  232. char *Substring;
  233. char *DirectoryPath;
  234. char *Filename;
  235. *OutDirectoryPath = NULL;
  236. *OutFilename = NULL;
  237. if (!InputPath)
  238. {
  239. return (AE_OK);
  240. }
  241. /* Get the path to the input filename's directory */
  242. DirectoryPath = FlStrdup (InputPath);
  243. if (!DirectoryPath)
  244. {
  245. return (AE_NO_MEMORY);
  246. }
  247. Substring = strrchr (DirectoryPath, '\\');
  248. if (!Substring)
  249. {
  250. Substring = strrchr (DirectoryPath, '/');
  251. if (!Substring)
  252. {
  253. Substring = strrchr (DirectoryPath, ':');
  254. }
  255. }
  256. if (!Substring)
  257. {
  258. DirectoryPath[0] = 0;
  259. Filename = FlStrdup (InputPath);
  260. }
  261. else
  262. {
  263. Filename = FlStrdup (Substring + 1);
  264. *(Substring+1) = 0;
  265. }
  266. if (!Filename)
  267. {
  268. return (AE_NO_MEMORY);
  269. }
  270. *OutDirectoryPath = DirectoryPath;
  271. *OutFilename = Filename;
  272. return (AE_OK);
  273. }