1 1.1 jruoho /****************************************************************************** 2 1.1 jruoho * 3 1.1 jruoho * Module Name: asfile - Main module for the acpi source processor utility 4 1.1 jruoho * 5 1.1 jruoho *****************************************************************************/ 6 1.1 jruoho 7 1.1.1.20 christos /****************************************************************************** 8 1.1.1.20 christos * 9 1.1.1.20 christos * 1. Copyright Notice 10 1.1.1.20 christos * 11 1.1.1.21 christos * Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp. 12 1.1 jruoho * All rights reserved. 13 1.1 jruoho * 14 1.1.1.20 christos * 2. License 15 1.1.1.20 christos * 16 1.1.1.20 christos * 2.1. This is your license from Intel Corp. under its intellectual property 17 1.1.1.20 christos * rights. You may have additional license terms from the party that provided 18 1.1.1.20 christos * you this software, covering your right to use that party's intellectual 19 1.1.1.20 christos * property rights. 20 1.1.1.20 christos * 21 1.1.1.20 christos * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 22 1.1.1.20 christos * copy of the source code appearing in this file ("Covered Code") an 23 1.1.1.20 christos * irrevocable, perpetual, worldwide license under Intel's copyrights in the 24 1.1.1.20 christos * base code distributed originally by Intel ("Original Intel Code") to copy, 25 1.1.1.20 christos * make derivatives, distribute, use and display any portion of the Covered 26 1.1.1.20 christos * Code in any form, with the right to sublicense such rights; and 27 1.1.1.20 christos * 28 1.1.1.20 christos * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 29 1.1.1.20 christos * license (with the right to sublicense), under only those claims of Intel 30 1.1.1.20 christos * patents that are infringed by the Original Intel Code, to make, use, sell, 31 1.1.1.20 christos * offer to sell, and import the Covered Code and derivative works thereof 32 1.1.1.20 christos * solely to the minimum extent necessary to exercise the above copyright 33 1.1.1.20 christos * license, and in no event shall the patent license extend to any additions 34 1.1.1.20 christos * to or modifications of the Original Intel Code. No other license or right 35 1.1.1.20 christos * is granted directly or by implication, estoppel or otherwise; 36 1.1.1.20 christos * 37 1.1.1.20 christos * The above copyright and patent license is granted only if the following 38 1.1.1.20 christos * conditions are met: 39 1.1.1.20 christos * 40 1.1.1.20 christos * 3. Conditions 41 1.1.1.20 christos * 42 1.1.1.20 christos * 3.1. Redistribution of Source with Rights to Further Distribute Source. 43 1.1.1.20 christos * Redistribution of source code of any substantial portion of the Covered 44 1.1.1.20 christos * Code or modification with rights to further distribute source must include 45 1.1.1.20 christos * the above Copyright Notice, the above License, this list of Conditions, 46 1.1.1.20 christos * and the following Disclaimer and Export Compliance provision. In addition, 47 1.1.1.20 christos * Licensee must cause all Covered Code to which Licensee contributes to 48 1.1.1.20 christos * contain a file documenting the changes Licensee made to create that Covered 49 1.1.1.20 christos * Code and the date of any change. Licensee must include in that file the 50 1.1.1.20 christos * documentation of any changes made by any predecessor Licensee. Licensee 51 1.1.1.20 christos * must include a prominent statement that the modification is derived, 52 1.1.1.20 christos * directly or indirectly, from Original Intel Code. 53 1.1.1.20 christos * 54 1.1.1.20 christos * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 55 1.1.1.20 christos * Redistribution of source code of any substantial portion of the Covered 56 1.1.1.20 christos * Code or modification without rights to further distribute source must 57 1.1.1.20 christos * include the following Disclaimer and Export Compliance provision in the 58 1.1.1.20 christos * documentation and/or other materials provided with distribution. In 59 1.1.1.20 christos * addition, Licensee may not authorize further sublicense of source of any 60 1.1.1.20 christos * portion of the Covered Code, and must include terms to the effect that the 61 1.1.1.20 christos * license from Licensee to its licensee is limited to the intellectual 62 1.1.1.20 christos * property embodied in the software Licensee provides to its licensee, and 63 1.1.1.20 christos * not to intellectual property embodied in modifications its licensee may 64 1.1.1.20 christos * make. 65 1.1.1.20 christos * 66 1.1.1.20 christos * 3.3. Redistribution of Executable. Redistribution in executable form of any 67 1.1.1.20 christos * substantial portion of the Covered Code or modification must reproduce the 68 1.1.1.20 christos * above Copyright Notice, and the following Disclaimer and Export Compliance 69 1.1.1.20 christos * provision in the documentation and/or other materials provided with the 70 1.1.1.20 christos * distribution. 71 1.1.1.20 christos * 72 1.1.1.20 christos * 3.4. Intel retains all right, title, and interest in and to the Original 73 1.1.1.20 christos * Intel Code. 74 1.1.1.20 christos * 75 1.1.1.20 christos * 3.5. Neither the name Intel nor any other trademark owned or controlled by 76 1.1.1.20 christos * Intel shall be used in advertising or otherwise to promote the sale, use or 77 1.1.1.20 christos * other dealings in products derived from or relating to the Covered Code 78 1.1.1.20 christos * without prior written authorization from Intel. 79 1.1.1.20 christos * 80 1.1.1.20 christos * 4. Disclaimer and Export Compliance 81 1.1.1.20 christos * 82 1.1.1.20 christos * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 83 1.1.1.20 christos * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 84 1.1.1.20 christos * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 85 1.1.1.20 christos * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 86 1.1.1.20 christos * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 87 1.1.1.20 christos * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 88 1.1.1.20 christos * PARTICULAR PURPOSE. 89 1.1.1.20 christos * 90 1.1.1.20 christos * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 91 1.1.1.20 christos * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 92 1.1.1.20 christos * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 93 1.1.1.20 christos * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 94 1.1.1.20 christos * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 95 1.1.1.20 christos * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 96 1.1.1.20 christos * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 97 1.1.1.20 christos * LIMITED REMEDY. 98 1.1.1.20 christos * 99 1.1.1.20 christos * 4.3. Licensee shall not export, either directly or indirectly, any of this 100 1.1.1.20 christos * software or system incorporating such software without first obtaining any 101 1.1.1.20 christos * required license or other approval from the U. S. Department of Commerce or 102 1.1.1.20 christos * any other agency or department of the United States Government. In the 103 1.1.1.20 christos * event Licensee exports any such software from the United States or 104 1.1.1.20 christos * re-exports any such software from a foreign destination, Licensee shall 105 1.1.1.20 christos * ensure that the distribution and export/re-export of the software is in 106 1.1.1.20 christos * compliance with all laws, regulations, orders, or other restrictions of the 107 1.1.1.20 christos * U.S. Export Administration Regulations. Licensee agrees that neither it nor 108 1.1.1.20 christos * any of its subsidiaries will export/re-export any technical data, process, 109 1.1.1.20 christos * software, or service, directly or indirectly, to any country for which the 110 1.1.1.20 christos * United States government or any agency thereof requires an export license, 111 1.1.1.20 christos * other governmental approval, or letter of assurance, without first obtaining 112 1.1.1.20 christos * such license, approval or letter. 113 1.1.1.20 christos * 114 1.1.1.20 christos ***************************************************************************** 115 1.1.1.20 christos * 116 1.1.1.20 christos * Alternatively, you may choose to be licensed under the terms of the 117 1.1.1.20 christos * following license: 118 1.1.1.20 christos * 119 1.1.1.2 jruoho * Redistribution and use in source and binary forms, with or without 120 1.1.1.2 jruoho * modification, are permitted provided that the following conditions 121 1.1.1.2 jruoho * are met: 122 1.1.1.2 jruoho * 1. Redistributions of source code must retain the above copyright 123 1.1.1.2 jruoho * notice, this list of conditions, and the following disclaimer, 124 1.1.1.2 jruoho * without modification. 125 1.1.1.2 jruoho * 2. Redistributions in binary form must reproduce at minimum a disclaimer 126 1.1.1.2 jruoho * substantially similar to the "NO WARRANTY" disclaimer below 127 1.1.1.2 jruoho * ("Disclaimer") and any redistribution must be conditioned upon 128 1.1.1.2 jruoho * including a substantially similar Disclaimer requirement for further 129 1.1.1.2 jruoho * binary redistribution. 130 1.1.1.2 jruoho * 3. Neither the names of the above-listed copyright holders nor the names 131 1.1.1.2 jruoho * of any contributors may be used to endorse or promote products derived 132 1.1.1.2 jruoho * from this software without specific prior written permission. 133 1.1.1.2 jruoho * 134 1.1.1.2 jruoho * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 135 1.1.1.2 jruoho * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 136 1.1.1.17 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 137 1.1.1.2 jruoho * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 138 1.1.1.20 christos * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 139 1.1.1.20 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 140 1.1.1.20 christos * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 141 1.1.1.20 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 142 1.1.1.20 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 143 1.1.1.20 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 144 1.1.1.20 christos * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 145 1.1.1.20 christos * 146 1.1.1.20 christos * Alternatively, you may choose to be licensed under the terms of the 147 1.1.1.20 christos * GNU General Public License ("GPL") version 2 as published by the Free 148 1.1.1.20 christos * Software Foundation. 149 1.1.1.20 christos * 150 1.1.1.20 christos *****************************************************************************/ 151 1.1 jruoho 152 1.1 jruoho #include "acpisrc.h" 153 1.1 jruoho 154 1.1 jruoho /* Local prototypes */ 155 1.1 jruoho 156 1.1 jruoho void 157 1.1 jruoho AsDoWildcard ( 158 1.1 jruoho ACPI_CONVERSION_TABLE *ConversionTable, 159 1.1 jruoho char *SourcePath, 160 1.1 jruoho char *TargetPath, 161 1.1 jruoho int MaxPathLength, 162 1.1 jruoho int FileType, 163 1.1 jruoho char *WildcardSpec); 164 1.1 jruoho 165 1.1 jruoho BOOLEAN 166 1.1 jruoho AsDetectLoneLineFeeds ( 167 1.1 jruoho char *Filename, 168 1.1 jruoho char *Buffer); 169 1.1 jruoho 170 1.1.1.12 christos static BOOLEAN 171 1.1.1.12 christos AsCheckForNonPrintableChars ( 172 1.1.1.12 christos char *FileBuffer, 173 1.1.1.12 christos UINT32 FileSize); 174 1.1.1.12 christos 175 1.1.1.2 jruoho static ACPI_INLINE int 176 1.1 jruoho AsMaxInt (int a, int b) 177 1.1 jruoho { 178 1.1 jruoho return (a > b ? a : b); 179 1.1 jruoho } 180 1.1 jruoho 181 1.1 jruoho 182 1.1 jruoho /****************************************************************************** 183 1.1 jruoho * 184 1.1 jruoho * FUNCTION: AsDoWildcard 185 1.1 jruoho * 186 1.1 jruoho * DESCRIPTION: Process files via wildcards 187 1.1 jruoho * 188 1.1 jruoho ******************************************************************************/ 189 1.1 jruoho 190 1.1 jruoho void 191 1.1 jruoho AsDoWildcard ( 192 1.1 jruoho ACPI_CONVERSION_TABLE *ConversionTable, 193 1.1 jruoho char *SourcePath, 194 1.1 jruoho char *TargetPath, 195 1.1 jruoho int MaxPathLength, 196 1.1 jruoho int FileType, 197 1.1 jruoho char *WildcardSpec) 198 1.1 jruoho { 199 1.1 jruoho void *DirInfo; 200 1.1 jruoho char *Filename; 201 1.1 jruoho char *SourceDirPath; 202 1.1 jruoho char *TargetDirPath; 203 1.1 jruoho char RequestedFileType; 204 1.1 jruoho 205 1.1 jruoho 206 1.1 jruoho if (FileType == FILE_TYPE_DIRECTORY) 207 1.1 jruoho { 208 1.1 jruoho RequestedFileType = REQUEST_DIR_ONLY; 209 1.1 jruoho } 210 1.1 jruoho else 211 1.1 jruoho { 212 1.1 jruoho RequestedFileType = REQUEST_FILE_ONLY; 213 1.1 jruoho } 214 1.1 jruoho 215 1.1 jruoho VERBOSE_PRINT (("Checking for %s source files in directory \"%s\"\n", 216 1.1 jruoho WildcardSpec, SourcePath)); 217 1.1 jruoho 218 1.1 jruoho /* Open the directory for wildcard search */ 219 1.1 jruoho 220 1.1 jruoho DirInfo = AcpiOsOpenDirectory (SourcePath, WildcardSpec, RequestedFileType); 221 1.1 jruoho if (DirInfo) 222 1.1 jruoho { 223 1.1 jruoho /* 224 1.1 jruoho * Get all of the files that match both the 225 1.1 jruoho * wildcard and the requested file type 226 1.1 jruoho */ 227 1.1 jruoho while ((Filename = AcpiOsGetNextFilename (DirInfo))) 228 1.1 jruoho { 229 1.1 jruoho /* Looking for directory files, must check file type */ 230 1.1 jruoho 231 1.1 jruoho switch (RequestedFileType) 232 1.1 jruoho { 233 1.1 jruoho case REQUEST_DIR_ONLY: 234 1.1 jruoho 235 1.1 jruoho /* If we actually have a dir, process the subtree */ 236 1.1 jruoho 237 1.1 jruoho if (!AsCheckForDirectory (SourcePath, TargetPath, Filename, 238 1.1.1.7 christos &SourceDirPath, &TargetDirPath)) 239 1.1 jruoho { 240 1.1 jruoho VERBOSE_PRINT (("Subdirectory: %s\n", Filename)); 241 1.1 jruoho 242 1.1 jruoho AsProcessTree (ConversionTable, SourceDirPath, TargetDirPath); 243 1.1 jruoho free (SourceDirPath); 244 1.1 jruoho free (TargetDirPath); 245 1.1 jruoho } 246 1.1 jruoho break; 247 1.1 jruoho 248 1.1 jruoho case REQUEST_FILE_ONLY: 249 1.1 jruoho 250 1.1 jruoho /* Otherwise, this is a file, not a directory */ 251 1.1 jruoho 252 1.1 jruoho VERBOSE_PRINT (("File: %s\n", Filename)); 253 1.1 jruoho 254 1.1 jruoho AsProcessOneFile (ConversionTable, SourcePath, TargetPath, 255 1.1.1.7 christos MaxPathLength, Filename, FileType); 256 1.1 jruoho break; 257 1.1 jruoho 258 1.1 jruoho default: 259 1.1.1.3 christos 260 1.1 jruoho break; 261 1.1 jruoho } 262 1.1 jruoho } 263 1.1 jruoho 264 1.1 jruoho /* Cleanup */ 265 1.1 jruoho 266 1.1 jruoho AcpiOsCloseDirectory (DirInfo); 267 1.1 jruoho } 268 1.1 jruoho } 269 1.1 jruoho 270 1.1 jruoho 271 1.1 jruoho /****************************************************************************** 272 1.1 jruoho * 273 1.1 jruoho * FUNCTION: AsProcessTree 274 1.1 jruoho * 275 1.1.1.3 christos * DESCRIPTION: Process the directory tree. Files with the extension ".C" and 276 1.1 jruoho * ".H" are processed as the tree is traversed. 277 1.1 jruoho * 278 1.1 jruoho ******************************************************************************/ 279 1.1 jruoho 280 1.1 jruoho ACPI_NATIVE_INT 281 1.1 jruoho AsProcessTree ( 282 1.1 jruoho ACPI_CONVERSION_TABLE *ConversionTable, 283 1.1 jruoho char *SourcePath, 284 1.1 jruoho char *TargetPath) 285 1.1 jruoho { 286 1.1 jruoho int MaxPathLength; 287 1.1 jruoho 288 1.1 jruoho 289 1.1 jruoho MaxPathLength = AsMaxInt (strlen (SourcePath), strlen (TargetPath)); 290 1.1 jruoho 291 1.1 jruoho if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT)) 292 1.1 jruoho { 293 1.1 jruoho if (ConversionTable->Flags & FLG_LOWERCASE_DIRNAMES) 294 1.1 jruoho { 295 1.1.1.6 christos AcpiUtStrlwr (TargetPath); 296 1.1 jruoho } 297 1.1 jruoho 298 1.1 jruoho VERBOSE_PRINT (("Creating Directory \"%s\"\n", TargetPath)); 299 1.1 jruoho if (mkdir (TargetPath)) 300 1.1 jruoho { 301 1.1 jruoho if (errno != EEXIST) 302 1.1 jruoho { 303 1.1 jruoho printf ("Could not create target directory\n"); 304 1.1.1.3 christos return (-1); 305 1.1 jruoho } 306 1.1 jruoho } 307 1.1 jruoho } 308 1.1 jruoho 309 1.1 jruoho /* Do the C source files */ 310 1.1 jruoho 311 1.1 jruoho AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength, 312 1.1.1.7 christos FILE_TYPE_SOURCE, "*.c"); 313 1.1 jruoho 314 1.1 jruoho /* Do the C header files */ 315 1.1 jruoho 316 1.1 jruoho AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength, 317 1.1.1.7 christos FILE_TYPE_HEADER, "*.h"); 318 1.1 jruoho 319 1.1 jruoho /* Do the Lex file(s) */ 320 1.1 jruoho 321 1.1 jruoho AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength, 322 1.1.1.7 christos FILE_TYPE_SOURCE, "*.l"); 323 1.1 jruoho 324 1.1 jruoho /* Do the yacc file(s) */ 325 1.1 jruoho 326 1.1 jruoho AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength, 327 1.1.1.7 christos FILE_TYPE_SOURCE, "*.y"); 328 1.1 jruoho 329 1.1 jruoho /* Do any ASL files */ 330 1.1 jruoho 331 1.1 jruoho AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength, 332 1.1.1.7 christos FILE_TYPE_HEADER, "*.asl"); 333 1.1 jruoho 334 1.1 jruoho /* Do any subdirectories */ 335 1.1 jruoho 336 1.1 jruoho AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength, 337 1.1.1.7 christos FILE_TYPE_DIRECTORY, "*"); 338 1.1 jruoho 339 1.1.1.3 christos return (0); 340 1.1 jruoho } 341 1.1 jruoho 342 1.1 jruoho 343 1.1 jruoho /****************************************************************************** 344 1.1 jruoho * 345 1.1 jruoho * FUNCTION: AsDetectLoneLineFeeds 346 1.1 jruoho * 347 1.1 jruoho * DESCRIPTION: Find LF without CR. 348 1.1 jruoho * 349 1.1 jruoho ******************************************************************************/ 350 1.1 jruoho 351 1.1 jruoho BOOLEAN 352 1.1 jruoho AsDetectLoneLineFeeds ( 353 1.1 jruoho char *Filename, 354 1.1 jruoho char *Buffer) 355 1.1 jruoho { 356 1.1 jruoho UINT32 i = 1; 357 1.1 jruoho UINT32 LfCount = 0; 358 1.1 jruoho UINT32 LineCount = 0; 359 1.1 jruoho 360 1.1 jruoho 361 1.1 jruoho if (!Buffer[0]) 362 1.1 jruoho { 363 1.1.1.3 christos return (FALSE); 364 1.1 jruoho } 365 1.1 jruoho 366 1.1 jruoho while (Buffer[i]) 367 1.1 jruoho { 368 1.1 jruoho if (Buffer[i] == 0x0A) 369 1.1 jruoho { 370 1.1 jruoho if (Buffer[i-1] != 0x0D) 371 1.1 jruoho { 372 1.1 jruoho LfCount++; 373 1.1 jruoho } 374 1.1.1.7 christos 375 1.1 jruoho LineCount++; 376 1.1 jruoho } 377 1.1 jruoho i++; 378 1.1 jruoho } 379 1.1 jruoho 380 1.1 jruoho if (LfCount) 381 1.1 jruoho { 382 1.1 jruoho if (LineCount == LfCount) 383 1.1 jruoho { 384 1.1 jruoho if (!Gbl_IgnoreLoneLineFeeds) 385 1.1 jruoho { 386 1.1 jruoho printf ("%s: ****File has UNIX format**** (LF only, not CR/LF) %u lines\n", 387 1.1 jruoho Filename, LfCount); 388 1.1 jruoho } 389 1.1 jruoho } 390 1.1 jruoho else 391 1.1 jruoho { 392 1.1 jruoho printf ("%s: %u lone linefeeds in file\n", Filename, LfCount); 393 1.1 jruoho } 394 1.1.1.7 christos 395 1.1.1.3 christos return (TRUE); 396 1.1 jruoho } 397 1.1 jruoho 398 1.1 jruoho return (FALSE); 399 1.1 jruoho } 400 1.1 jruoho 401 1.1 jruoho 402 1.1 jruoho /****************************************************************************** 403 1.1 jruoho * 404 1.1 jruoho * FUNCTION: AsConvertFile 405 1.1 jruoho * 406 1.1 jruoho * DESCRIPTION: Perform the requested transforms on the file buffer (as 407 1.1 jruoho * determined by the ConversionTable and the FileType). 408 1.1 jruoho * 409 1.1 jruoho ******************************************************************************/ 410 1.1 jruoho 411 1.1 jruoho void 412 1.1 jruoho AsConvertFile ( 413 1.1 jruoho ACPI_CONVERSION_TABLE *ConversionTable, 414 1.1 jruoho char *FileBuffer, 415 1.1 jruoho char *Filename, 416 1.1 jruoho ACPI_NATIVE_INT FileType) 417 1.1 jruoho { 418 1.1 jruoho UINT32 i; 419 1.1 jruoho UINT32 Functions; 420 1.1 jruoho ACPI_STRING_TABLE *StringTable; 421 1.1 jruoho ACPI_IDENTIFIER_TABLE *ConditionalTable; 422 1.1 jruoho ACPI_IDENTIFIER_TABLE *LineTable; 423 1.1 jruoho ACPI_TYPED_IDENTIFIER_TABLE *StructTable; 424 1.1.1.3 christos ACPI_IDENTIFIER_TABLE *SpecialMacroTable; 425 1.1.1.13 christos char *SpdxHeader=NULL; 426 1.1 jruoho 427 1.1 jruoho 428 1.1 jruoho switch (FileType) 429 1.1 jruoho { 430 1.1 jruoho case FILE_TYPE_SOURCE: 431 1.1.1.3 christos 432 1.1 jruoho Functions = ConversionTable->SourceFunctions; 433 1.1 jruoho StringTable = ConversionTable->SourceStringTable; 434 1.1 jruoho LineTable = ConversionTable->SourceLineTable; 435 1.1 jruoho ConditionalTable = ConversionTable->SourceConditionalTable; 436 1.1 jruoho StructTable = ConversionTable->SourceStructTable; 437 1.1.1.3 christos SpecialMacroTable = ConversionTable->SourceSpecialMacroTable; 438 1.1.1.13 christos SpdxHeader = ConversionTable->SourceSpdxHeader; 439 1.1.1.10 christos break; 440 1.1 jruoho 441 1.1 jruoho case FILE_TYPE_HEADER: 442 1.1.1.3 christos 443 1.1 jruoho Functions = ConversionTable->HeaderFunctions; 444 1.1 jruoho StringTable = ConversionTable->HeaderStringTable; 445 1.1 jruoho LineTable = ConversionTable->HeaderLineTable; 446 1.1 jruoho ConditionalTable = ConversionTable->HeaderConditionalTable; 447 1.1 jruoho StructTable = ConversionTable->HeaderStructTable; 448 1.1.1.3 christos SpecialMacroTable = ConversionTable->HeaderSpecialMacroTable; 449 1.1.1.13 christos SpdxHeader = ConversionTable->HeaderSpdxHeader; 450 1.1 jruoho break; 451 1.1 jruoho 452 1.1.1.4 christos case FILE_TYPE_PATCH: 453 1.1.1.4 christos 454 1.1.1.4 christos Functions = ConversionTable->PatchFunctions; 455 1.1.1.4 christos StringTable = ConversionTable->PatchStringTable; 456 1.1.1.4 christos LineTable = ConversionTable->PatchLineTable; 457 1.1.1.4 christos ConditionalTable = ConversionTable->PatchConditionalTable; 458 1.1.1.4 christos StructTable = ConversionTable->PatchStructTable; 459 1.1.1.4 christos SpecialMacroTable = ConversionTable->PatchSpecialMacroTable; 460 1.1.1.4 christos break; 461 1.1.1.4 christos 462 1.1 jruoho default: 463 1.1.1.3 christos 464 1.1 jruoho printf ("Unknown file type, cannot process\n"); 465 1.1 jruoho return; 466 1.1 jruoho } 467 1.1 jruoho 468 1.1 jruoho 469 1.1 jruoho Gbl_StructDefs = strstr (FileBuffer, "/* acpisrc:StructDefs"); 470 1.1 jruoho Gbl_Files++; 471 1.1.1.2 jruoho VERBOSE_PRINT (("Processing %u bytes\n", 472 1.1.1.2 jruoho (unsigned int) strlen (FileBuffer))); 473 1.1 jruoho 474 1.1.1.3 christos if (Gbl_Cleanup) 475 1.1.1.3 christos { 476 1.1.1.3 christos AsRemoveExtraLines (FileBuffer, Filename); 477 1.1.1.3 christos AsRemoveSpacesAfterPeriod (FileBuffer, Filename); 478 1.1.1.3 christos } 479 1.1.1.3 christos 480 1.1 jruoho if (ConversionTable->LowerCaseTable) 481 1.1 jruoho { 482 1.1 jruoho for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++) 483 1.1 jruoho { 484 1.1 jruoho AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier, 485 1.1.1.7 christos FileBuffer); 486 1.1 jruoho } 487 1.1 jruoho } 488 1.1 jruoho 489 1.1 jruoho /* Process all the string replacements */ 490 1.1 jruoho 491 1.1 jruoho if (StringTable) 492 1.1 jruoho { 493 1.1 jruoho for (i = 0; StringTable[i].Target; i++) 494 1.1 jruoho { 495 1.1 jruoho AsReplaceString (StringTable[i].Target, StringTable[i].Replacement, 496 1.1.1.7 christos StringTable[i].Type, FileBuffer); 497 1.1 jruoho } 498 1.1 jruoho } 499 1.1 jruoho 500 1.1 jruoho if (LineTable) 501 1.1 jruoho { 502 1.1 jruoho for (i = 0; LineTable[i].Identifier; i++) 503 1.1 jruoho { 504 1.1 jruoho AsRemoveLine (FileBuffer, LineTable[i].Identifier); 505 1.1 jruoho } 506 1.1 jruoho } 507 1.1 jruoho 508 1.1 jruoho if (ConditionalTable) 509 1.1 jruoho { 510 1.1 jruoho for (i = 0; ConditionalTable[i].Identifier; i++) 511 1.1 jruoho { 512 1.1 jruoho AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier); 513 1.1 jruoho } 514 1.1 jruoho } 515 1.1 jruoho 516 1.1.1.6 christos #ifdef _OBSOLETE_FUNCTIONS 517 1.1 jruoho if (MacroTable) 518 1.1 jruoho { 519 1.1 jruoho for (i = 0; MacroTable[i].Identifier; i++) 520 1.1 jruoho { 521 1.1 jruoho AsRemoveMacro (FileBuffer, MacroTable[i].Identifier); 522 1.1 jruoho } 523 1.1 jruoho } 524 1.1.1.6 christos #endif 525 1.1 jruoho 526 1.1 jruoho if (StructTable) 527 1.1 jruoho { 528 1.1 jruoho for (i = 0; StructTable[i].Identifier; i++) 529 1.1 jruoho { 530 1.1.1.7 christos AsInsertPrefix (FileBuffer, StructTable[i].Identifier, 531 1.1.1.7 christos StructTable[i].Type); 532 1.1 jruoho } 533 1.1 jruoho } 534 1.1 jruoho 535 1.1.1.3 christos if (SpecialMacroTable) 536 1.1.1.3 christos { 537 1.1.1.3 christos for (i = 0; SpecialMacroTable[i].Identifier; i++) 538 1.1.1.3 christos { 539 1.1.1.3 christos AsCleanupSpecialMacro (FileBuffer, SpecialMacroTable[i].Identifier); 540 1.1.1.3 christos } 541 1.1.1.3 christos } 542 1.1.1.3 christos 543 1.1 jruoho /* Process the function table */ 544 1.1 jruoho 545 1.1 jruoho for (i = 0; i < 32; i++) 546 1.1 jruoho { 547 1.1 jruoho /* Decode the function bitmap */ 548 1.1 jruoho 549 1.1.1.15 christos switch (((UINT32) 1 << i) & Functions) 550 1.1 jruoho { 551 1.1 jruoho case 0: 552 1.1.1.3 christos 553 1.1 jruoho /* This function not configured */ 554 1.1 jruoho break; 555 1.1 jruoho 556 1.1 jruoho case CVT_COUNT_TABS: 557 1.1 jruoho 558 1.1 jruoho AsCountTabs (FileBuffer, Filename); 559 1.1 jruoho break; 560 1.1 jruoho 561 1.1 jruoho case CVT_COUNT_NON_ANSI_COMMENTS: 562 1.1 jruoho 563 1.1 jruoho AsCountNonAnsiComments (FileBuffer, Filename); 564 1.1 jruoho break; 565 1.1 jruoho 566 1.1 jruoho case CVT_CHECK_BRACES: 567 1.1 jruoho 568 1.1 jruoho AsCheckForBraces (FileBuffer, Filename); 569 1.1 jruoho break; 570 1.1 jruoho 571 1.1 jruoho case CVT_TRIM_LINES: 572 1.1 jruoho 573 1.1 jruoho AsTrimLines (FileBuffer, Filename); 574 1.1 jruoho break; 575 1.1 jruoho 576 1.1 jruoho case CVT_COUNT_LINES: 577 1.1 jruoho 578 1.1 jruoho AsCountSourceLines (FileBuffer, Filename); 579 1.1 jruoho break; 580 1.1 jruoho 581 1.1 jruoho case CVT_BRACES_ON_SAME_LINE: 582 1.1 jruoho 583 1.1 jruoho AsBracesOnSameLine (FileBuffer); 584 1.1 jruoho break; 585 1.1 jruoho 586 1.1 jruoho case CVT_MIXED_CASE_TO_UNDERSCORES: 587 1.1 jruoho 588 1.1.1.3 christos AsMixedCaseToUnderscores (FileBuffer, Filename); 589 1.1 jruoho break; 590 1.1 jruoho 591 1.1 jruoho case CVT_LOWER_CASE_IDENTIFIERS: 592 1.1 jruoho 593 1.1 jruoho AsLowerCaseIdentifiers (FileBuffer); 594 1.1 jruoho break; 595 1.1 jruoho 596 1.1 jruoho case CVT_REMOVE_DEBUG_MACROS: 597 1.1 jruoho 598 1.1 jruoho AsRemoveDebugMacros (FileBuffer); 599 1.1 jruoho break; 600 1.1 jruoho 601 1.1 jruoho case CVT_TRIM_WHITESPACE: 602 1.1 jruoho 603 1.1 jruoho AsTrimWhitespace (FileBuffer); 604 1.1 jruoho break; 605 1.1 jruoho 606 1.1 jruoho case CVT_REMOVE_EMPTY_BLOCKS: 607 1.1 jruoho 608 1.1 jruoho AsRemoveEmptyBlocks (FileBuffer, Filename); 609 1.1 jruoho break; 610 1.1 jruoho 611 1.1 jruoho case CVT_REDUCE_TYPEDEFS: 612 1.1 jruoho 613 1.1 jruoho AsReduceTypedefs (FileBuffer, "typedef union"); 614 1.1 jruoho AsReduceTypedefs (FileBuffer, "typedef struct"); 615 1.1 jruoho break; 616 1.1 jruoho 617 1.1 jruoho case CVT_SPACES_TO_TABS4: 618 1.1 jruoho 619 1.1 jruoho AsTabify4 (FileBuffer); 620 1.1 jruoho break; 621 1.1 jruoho 622 1.1 jruoho case CVT_SPACES_TO_TABS8: 623 1.1 jruoho 624 1.1 jruoho AsTabify8 (FileBuffer); 625 1.1 jruoho break; 626 1.1 jruoho 627 1.1 jruoho case CVT_COUNT_SHORTMULTILINE_COMMENTS: 628 1.1 jruoho 629 1.1 jruoho #ifdef ACPI_FUTURE_IMPLEMENTATION 630 1.1 jruoho AsTrimComments (FileBuffer, Filename); 631 1.1 jruoho #endif 632 1.1 jruoho break; 633 1.1 jruoho 634 1.1 jruoho default: 635 1.1 jruoho 636 1.1 jruoho printf ("Unknown conversion subfunction opcode\n"); 637 1.1 jruoho break; 638 1.1 jruoho } 639 1.1 jruoho } 640 1.1 jruoho 641 1.1 jruoho if (ConversionTable->NewHeader) 642 1.1 jruoho { 643 1.1 jruoho AsReplaceHeader (FileBuffer, ConversionTable->NewHeader); 644 1.1 jruoho } 645 1.1.1.13 christos if (SpdxHeader) 646 1.1.1.13 christos { 647 1.1.1.13 christos AsDoSpdxHeader (FileBuffer, SpdxHeader); 648 1.1.1.13 christos } 649 1.1 jruoho } 650 1.1 jruoho 651 1.1.1.12 christos /******************************************************************************* 652 1.1.1.12 christos * 653 1.1.1.12 christos * FUNCTION: AsCheckForNonPrintableChars 654 1.1.1.12 christos * 655 1.1.1.12 christos * PARAMETERS: FileBuffer - Buffer with contents of entire file 656 1.1.1.12 christos * FileSize - Size of the file and buffer 657 1.1.1.12 christos * 658 1.1.1.12 christos * RETURN: TRUE if there are no non-printable characters 659 1.1.1.12 christos * 660 1.1.1.12 christos * DESCRIPTION: Scan a file for any non-printable ASCII bytes. 661 1.1.1.12 christos * 662 1.1.1.12 christos ******************************************************************************/ 663 1.1.1.12 christos 664 1.1.1.12 christos static BOOLEAN 665 1.1.1.12 christos AsCheckForNonPrintableChars ( 666 1.1.1.12 christos char *FileBuffer, 667 1.1.1.12 christos UINT32 FileSize) 668 1.1.1.12 christos { 669 1.1.1.12 christos BOOLEAN Found = TRUE; 670 1.1.1.12 christos UINT8 Byte; 671 1.1.1.12 christos UINT32 i; 672 1.1.1.12 christos 673 1.1.1.12 christos 674 1.1.1.12 christos /* Scan entire file for any non-printable characters */ 675 1.1.1.12 christos 676 1.1.1.12 christos for (i = 0; i < FileSize; i++) 677 1.1.1.12 christos { 678 1.1.1.12 christos Byte = FileBuffer[i]; 679 1.1.1.12 christos if (!isprint (Byte) && !isspace (Byte)) 680 1.1.1.12 christos { 681 1.1.1.12 christos printf ( "Non-printable character (0x%2.2X) " 682 1.1.1.12 christos "at file offset: %8u (0x%X)\n", Byte, i, i); 683 1.1.1.12 christos Found = FALSE; 684 1.1.1.12 christos } 685 1.1.1.12 christos } 686 1.1.1.12 christos 687 1.1.1.12 christos return (Found); 688 1.1.1.12 christos } 689 1.1.1.12 christos 690 1.1 jruoho 691 1.1 jruoho /****************************************************************************** 692 1.1 jruoho * 693 1.1 jruoho * FUNCTION: AsProcessOneFile 694 1.1 jruoho * 695 1.1.1.3 christos * DESCRIPTION: Process one source file. The file is opened, read entirely 696 1.1 jruoho * into a buffer, converted, then written to a new file. 697 1.1 jruoho * 698 1.1 jruoho ******************************************************************************/ 699 1.1 jruoho 700 1.1 jruoho ACPI_NATIVE_INT 701 1.1 jruoho AsProcessOneFile ( 702 1.1 jruoho ACPI_CONVERSION_TABLE *ConversionTable, 703 1.1 jruoho char *SourcePath, 704 1.1 jruoho char *TargetPath, 705 1.1 jruoho int MaxPathLength, 706 1.1 jruoho char *Filename, 707 1.1 jruoho ACPI_NATIVE_INT FileType) 708 1.1 jruoho { 709 1.1 jruoho char *Pathname; 710 1.1.1.9 christos char *OutPathname; 711 1.1.1.9 christos int Status = 0; 712 1.1 jruoho 713 1.1 jruoho 714 1.1 jruoho /* Allocate a file pathname buffer for both source and target */ 715 1.1 jruoho 716 1.1 jruoho Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1); 717 1.1 jruoho if (!Pathname) 718 1.1 jruoho { 719 1.1 jruoho printf ("Could not allocate buffer for file pathnames\n"); 720 1.1.1.3 christos return (-1); 721 1.1 jruoho } 722 1.1 jruoho 723 1.1 jruoho Gbl_FileType = FileType; 724 1.1 jruoho 725 1.1 jruoho /* Generate the source pathname and read the file */ 726 1.1 jruoho 727 1.1 jruoho if (SourcePath) 728 1.1 jruoho { 729 1.1 jruoho strcpy (Pathname, SourcePath); 730 1.1 jruoho strcat (Pathname, "/"); 731 1.1 jruoho } 732 1.1 jruoho 733 1.1 jruoho strcat (Pathname, Filename); 734 1.1 jruoho if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize)) 735 1.1 jruoho { 736 1.1.1.9 christos Status = -1; 737 1.1.1.9 christos goto Exit1; 738 1.1 jruoho } 739 1.1 jruoho 740 1.1.1.12 christos /* Exit now if simply checking the file for printable ascii chars */ 741 1.1.1.12 christos 742 1.1.1.12 christos if (Gbl_CheckAscii) 743 1.1.1.12 christos { 744 1.1.1.12 christos Status = 0; 745 1.1.1.12 christos goto Exit2; 746 1.1.1.12 christos } 747 1.1.1.12 christos 748 1.1 jruoho Gbl_HeaderSize = 0; 749 1.1 jruoho if (strstr (Filename, ".asl")) 750 1.1 jruoho { 751 1.1 jruoho Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */ 752 1.1 jruoho } 753 1.1 jruoho else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE)) 754 1.1 jruoho { 755 1.1 jruoho Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */ 756 1.1 jruoho } 757 1.1 jruoho else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE)) 758 1.1 jruoho { 759 1.1 jruoho Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */ 760 1.1 jruoho } 761 1.1 jruoho 762 1.1 jruoho /* Process the file in the buffer */ 763 1.1 jruoho 764 1.1 jruoho Gbl_MadeChanges = FALSE; 765 1.1 jruoho if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds) 766 1.1 jruoho { 767 1.1 jruoho /* 768 1.1 jruoho * All lone LFs will be converted to CR/LF 769 1.1 jruoho * (when file is written, Windows version only) 770 1.1 jruoho */ 771 1.1 jruoho printf ("Converting lone linefeeds\n"); 772 1.1 jruoho Gbl_MadeChanges = TRUE; 773 1.1 jruoho } 774 1.1 jruoho 775 1.1 jruoho AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType); 776 1.1 jruoho 777 1.1 jruoho if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT)) 778 1.1 jruoho { 779 1.1 jruoho if (!(Gbl_Overwrite && !Gbl_MadeChanges)) 780 1.1 jruoho { 781 1.1 jruoho /* Generate the target pathname and write the file */ 782 1.1 jruoho 783 1.1.1.7 christos OutPathname = calloc (MaxPathLength + 784 1.1.1.7 christos strlen (Filename) + 2 + strlen (TargetPath), 1); 785 1.1 jruoho if (!OutPathname) 786 1.1 jruoho { 787 1.1 jruoho printf ("Could not allocate buffer for file pathnames\n"); 788 1.1.1.9 christos Status = -1; 789 1.1.1.9 christos goto Exit2; 790 1.1 jruoho } 791 1.1 jruoho 792 1.1 jruoho strcpy (OutPathname, TargetPath); 793 1.1 jruoho if (SourcePath) 794 1.1 jruoho { 795 1.1 jruoho strcat (OutPathname, "/"); 796 1.1 jruoho strcat (OutPathname, Filename); 797 1.1 jruoho } 798 1.1 jruoho 799 1.1 jruoho AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags); 800 1.1.1.9 christos free (OutPathname); 801 1.1 jruoho } 802 1.1 jruoho } 803 1.1 jruoho 804 1.1.1.9 christos Exit2: 805 1.1 jruoho free (Gbl_FileBuffer); 806 1.1 jruoho 807 1.1.1.9 christos Exit1: 808 1.1.1.9 christos free (Pathname); 809 1.1.1.9 christos return (Status); 810 1.1 jruoho } 811 1.1 jruoho 812 1.1 jruoho 813 1.1 jruoho /****************************************************************************** 814 1.1 jruoho * 815 1.1 jruoho * FUNCTION: AsCheckForDirectory 816 1.1 jruoho * 817 1.1.1.3 christos * DESCRIPTION: Check if the current file is a valid directory. If not, 818 1.1 jruoho * construct the full pathname for the source and target paths. 819 1.1 jruoho * Checks for the dot and dot-dot files (they are ignored) 820 1.1 jruoho * 821 1.1 jruoho ******************************************************************************/ 822 1.1 jruoho 823 1.1 jruoho ACPI_NATIVE_INT 824 1.1 jruoho AsCheckForDirectory ( 825 1.1 jruoho char *SourceDirPath, 826 1.1 jruoho char *TargetDirPath, 827 1.1 jruoho char *Filename, 828 1.1 jruoho char **SourcePath, 829 1.1 jruoho char **TargetPath) 830 1.1 jruoho { 831 1.1 jruoho char *SrcPath; 832 1.1 jruoho char *TgtPath; 833 1.1 jruoho 834 1.1 jruoho 835 1.1 jruoho if (!(strcmp (Filename, ".")) || 836 1.1 jruoho !(strcmp (Filename, ".."))) 837 1.1 jruoho { 838 1.1.1.3 christos return (-1); 839 1.1 jruoho } 840 1.1 jruoho 841 1.1 jruoho SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1); 842 1.1 jruoho if (!SrcPath) 843 1.1 jruoho { 844 1.1 jruoho printf ("Could not allocate buffer for directory source pathname\n"); 845 1.1.1.3 christos return (-1); 846 1.1 jruoho } 847 1.1 jruoho 848 1.1 jruoho TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1); 849 1.1 jruoho if (!TgtPath) 850 1.1 jruoho { 851 1.1 jruoho printf ("Could not allocate buffer for directory target pathname\n"); 852 1.1 jruoho free (SrcPath); 853 1.1.1.3 christos return (-1); 854 1.1 jruoho } 855 1.1 jruoho 856 1.1 jruoho strcpy (SrcPath, SourceDirPath); 857 1.1 jruoho strcat (SrcPath, "/"); 858 1.1 jruoho strcat (SrcPath, Filename); 859 1.1 jruoho 860 1.1 jruoho strcpy (TgtPath, TargetDirPath); 861 1.1 jruoho strcat (TgtPath, "/"); 862 1.1 jruoho strcat (TgtPath, Filename); 863 1.1 jruoho 864 1.1 jruoho *SourcePath = SrcPath; 865 1.1 jruoho *TargetPath = TgtPath; 866 1.1.1.3 christos return (0); 867 1.1 jruoho } 868 1.1 jruoho 869 1.1 jruoho 870 1.1 jruoho /****************************************************************************** 871 1.1 jruoho * 872 1.1 jruoho * FUNCTION: AsGetFile 873 1.1 jruoho * 874 1.1 jruoho * DESCRIPTION: Open a file and read it entirely into a an allocated buffer 875 1.1 jruoho * 876 1.1 jruoho ******************************************************************************/ 877 1.1 jruoho 878 1.1 jruoho int 879 1.1 jruoho AsGetFile ( 880 1.1 jruoho char *Filename, 881 1.1 jruoho char **FileBuffer, 882 1.1 jruoho UINT32 *FileSize) 883 1.1 jruoho { 884 1.1.1.3 christos FILE *File; 885 1.1 jruoho UINT32 Size; 886 1.1 jruoho char *Buffer; 887 1.1.1.3 christos size_t Actual; 888 1.1 jruoho 889 1.1 jruoho 890 1.1 jruoho /* Binary mode leaves CR/LF pairs */ 891 1.1 jruoho 892 1.1.1.3 christos File = fopen (Filename, "rb"); 893 1.1.1.3 christos if (!File) 894 1.1 jruoho { 895 1.1.1.3 christos printf ("Could not open file %s\n", Filename); 896 1.1.1.3 christos return (-1); 897 1.1 jruoho } 898 1.1 jruoho 899 1.1.1.3 christos /* Need file size to allocate a buffer */ 900 1.1.1.3 christos 901 1.1.1.4 christos Size = CmGetFileSize (File); 902 1.1.1.4 christos if (Size == ACPI_UINT32_MAX) 903 1.1 jruoho { 904 1.1.1.3 christos printf ("Could not get file size for %s\n", Filename); 905 1.1 jruoho goto ErrorExit; 906 1.1 jruoho } 907 1.1 jruoho 908 1.1 jruoho /* 909 1.1 jruoho * Create a buffer for the entire file 910 1.1.1.3 christos * Add plenty extra buffer to accommodate string replacements 911 1.1 jruoho */ 912 1.1 jruoho Gbl_TotalSize += Size; 913 1.1 jruoho 914 1.1 jruoho Buffer = calloc (Size * 2, 1); 915 1.1 jruoho if (!Buffer) 916 1.1 jruoho { 917 1.1 jruoho printf ("Could not allocate buffer of size %u\n", Size * 2); 918 1.1 jruoho goto ErrorExit; 919 1.1 jruoho } 920 1.1 jruoho 921 1.1 jruoho /* Read the entire file */ 922 1.1 jruoho 923 1.1.1.3 christos Actual = fread (Buffer, 1, Size, File); 924 1.1.1.3 christos if (Actual != Size) 925 1.1 jruoho { 926 1.1.1.3 christos printf ("Could not read the input file %s (%u bytes)\n", 927 1.1.1.3 christos Filename, Size); 928 1.1.1.8 christos goto ErrorFree; 929 1.1 jruoho } 930 1.1 jruoho 931 1.1 jruoho Buffer [Size] = 0; /* Null terminate the buffer */ 932 1.1.1.3 christos fclose (File); 933 1.1 jruoho 934 1.1.1.12 christos /* This option checks the entire file for non-printable chars */ 935 1.1.1.12 christos 936 1.1.1.12 christos if (Gbl_CheckAscii) 937 1.1.1.12 christos { 938 1.1.1.12 christos if (AsCheckForNonPrintableChars (Buffer, Size)) 939 1.1.1.12 christos { 940 1.1.1.12 christos printf ("File contains only printable ASCII characters\n"); 941 1.1.1.12 christos } 942 1.1.1.12 christos 943 1.1.1.12 christos free (Buffer); 944 1.1.1.12 christos return (0); 945 1.1.1.12 christos } 946 1.1.1.12 christos 947 1.1 jruoho /* Check for unix contamination */ 948 1.1 jruoho 949 1.1 jruoho Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer); 950 1.1 jruoho 951 1.1 jruoho /* 952 1.1.1.3 christos * Convert all CR/LF pairs to LF only. We do this locally so that 953 1.1 jruoho * this code is portable across operating systems. 954 1.1 jruoho */ 955 1.1 jruoho AsConvertToLineFeeds (Buffer); 956 1.1 jruoho 957 1.1 jruoho *FileBuffer = Buffer; 958 1.1 jruoho *FileSize = Size; 959 1.1.1.3 christos return (0); 960 1.1 jruoho 961 1.1.1.8 christos ErrorFree: 962 1.1.1.8 christos free (Buffer); 963 1.1 jruoho 964 1.1 jruoho ErrorExit: 965 1.1.1.3 christos fclose (File); 966 1.1.1.3 christos return (-1); 967 1.1 jruoho } 968 1.1 jruoho 969 1.1 jruoho 970 1.1 jruoho /****************************************************************************** 971 1.1 jruoho * 972 1.1 jruoho * FUNCTION: AsPutFile 973 1.1 jruoho * 974 1.1 jruoho * DESCRIPTION: Create a new output file and write the entire contents of the 975 1.1.1.3 christos * buffer to the new file. Buffer must be a zero terminated string 976 1.1 jruoho * 977 1.1 jruoho ******************************************************************************/ 978 1.1 jruoho 979 1.1 jruoho int 980 1.1 jruoho AsPutFile ( 981 1.1 jruoho char *Pathname, 982 1.1 jruoho char *FileBuffer, 983 1.1 jruoho UINT32 SystemFlags) 984 1.1 jruoho { 985 1.1.1.3 christos FILE *File; 986 1.1 jruoho UINT32 FileSize; 987 1.1.1.3 christos size_t Actual; 988 1.1.1.3 christos int Status = 0; 989 1.1 jruoho 990 1.1 jruoho 991 1.1 jruoho /* Create the target file */ 992 1.1 jruoho 993 1.1 jruoho if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS)) 994 1.1 jruoho { 995 1.1 jruoho /* Put back the CR before each LF */ 996 1.1 jruoho 997 1.1 jruoho AsInsertCarriageReturns (FileBuffer); 998 1.1 jruoho } 999 1.1 jruoho 1000 1.1.1.3 christos File = fopen (Pathname, "w+b"); 1001 1.1.1.3 christos if (!File) 1002 1.1 jruoho { 1003 1.1 jruoho perror ("Could not create destination file"); 1004 1.1 jruoho printf ("Could not create destination file \"%s\"\n", Pathname); 1005 1.1.1.3 christos return (-1); 1006 1.1 jruoho } 1007 1.1 jruoho 1008 1.1 jruoho /* Write the buffer to the file */ 1009 1.1 jruoho 1010 1.1 jruoho FileSize = strlen (FileBuffer); 1011 1.1.1.3 christos Actual = fwrite (FileBuffer, 1, FileSize, File); 1012 1.1.1.3 christos if (Actual != FileSize) 1013 1.1.1.3 christos { 1014 1.1.1.3 christos printf ("Error writing output file \"%s\"\n", Pathname); 1015 1.1.1.3 christos Status = -1; 1016 1.1.1.3 christos } 1017 1.1 jruoho 1018 1.1.1.3 christos fclose (File); 1019 1.1.1.3 christos return (Status); 1020 1.1 jruoho } 1021