1 /****************************************************************************** 2 * 3 * Module Name: acpisrc.h - Include file for AcpiSrc utility 4 * 5 *****************************************************************************/ 6 7 /* 8 * Copyright (C) 2000 - 2014, 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 #include "acpi.h" 45 #include "accommon.h" 46 47 #include <stdio.h> 48 #include <sys/stat.h> 49 #include <errno.h> 50 51 /* mkdir support */ 52 53 #ifdef WIN32 54 #include <direct.h> 55 #else 56 #define mkdir(x) mkdir(x, 0770) 57 #endif 58 59 60 /* Constants */ 61 62 #define LINES_IN_LEGAL_HEADER 105 /* See above */ 63 #define LEGAL_HEADER_SIGNATURE " * 2.1. This is your license from Intel Corp. under its intellectual property" 64 #define LINES_IN_LINUX_HEADER 34 65 #define LINUX_HEADER_SIGNATURE " * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS" 66 #define LINES_IN_ASL_HEADER 29 /* Header as output from disassembler */ 67 68 #define ASRC_MAX_FILE_SIZE (1024 * 100) 69 70 #define FILE_TYPE_SOURCE 1 71 #define FILE_TYPE_HEADER 2 72 #define FILE_TYPE_DIRECTORY 3 73 #define FILE_TYPE_PATCH 4 74 75 #define CVT_COUNT_TABS 0x00000001 76 #define CVT_COUNT_NON_ANSI_COMMENTS 0x00000002 77 #define CVT_TRIM_LINES 0x00000004 78 #define CVT_CHECK_BRACES 0x00000008 79 #define CVT_COUNT_LINES 0x00000010 80 #define CVT_BRACES_ON_SAME_LINE 0x00000020 81 #define CVT_MIXED_CASE_TO_UNDERSCORES 0x00000040 82 #define CVT_LOWER_CASE_IDENTIFIERS 0x00000080 83 #define CVT_REMOVE_DEBUG_MACROS 0x00000100 84 #define CVT_TRIM_WHITESPACE 0x00000200 /* Should be after all line removal */ 85 #define CVT_REMOVE_EMPTY_BLOCKS 0x00000400 /* Should be after trimming lines */ 86 #define CVT_REDUCE_TYPEDEFS 0x00000800 87 #define CVT_COUNT_SHORTMULTILINE_COMMENTS 0x00001000 88 #define CVT_SPACES_TO_TABS4 0x40000000 /* Tab conversion should be last */ 89 #define CVT_SPACES_TO_TABS8 0x80000000 /* Tab conversion should be last */ 90 91 #define FLG_DEFAULT_FLAGS 0x00000000 92 #define FLG_NO_CARRIAGE_RETURNS 0x00000001 93 #define FLG_NO_FILE_OUTPUT 0x00000002 94 #define FLG_LOWERCASE_DIRNAMES 0x00000004 95 96 #define AS_START_IGNORE "/*!" 97 #define AS_STOP_IGNORE "!*/" 98 99 100 /* Globals */ 101 102 extern UINT32 Gbl_Files; 103 extern UINT32 Gbl_MissingBraces; 104 extern UINT32 Gbl_Tabs; 105 extern UINT32 Gbl_NonAnsiComments; 106 extern UINT32 Gbl_SourceLines; 107 extern UINT32 Gbl_WhiteLines; 108 extern UINT32 Gbl_CommentLines; 109 extern UINT32 Gbl_LongLines; 110 extern UINT32 Gbl_TotalLines; 111 extern UINT32 Gbl_HeaderSize; 112 extern UINT32 Gbl_HeaderLines; 113 extern struct stat Gbl_StatBuf; 114 extern char *Gbl_FileBuffer; 115 extern UINT32 Gbl_TotalSize; 116 extern UINT32 Gbl_FileSize; 117 extern UINT32 Gbl_FileType; 118 extern BOOLEAN Gbl_VerboseMode; 119 extern BOOLEAN Gbl_QuietMode; 120 extern BOOLEAN Gbl_BatchMode; 121 extern BOOLEAN Gbl_MadeChanges; 122 extern BOOLEAN Gbl_Overwrite; 123 extern BOOLEAN Gbl_WidenDeclarations; 124 extern BOOLEAN Gbl_IgnoreLoneLineFeeds; 125 extern BOOLEAN Gbl_HasLoneLineFeeds; 126 extern BOOLEAN Gbl_Cleanup; 127 extern BOOLEAN Gbl_IgnoreTranslationEscapes; 128 extern void *Gbl_StructDefs; 129 130 #define PARAM_LIST(pl) pl 131 #define TERSE_PRINT(a) if (!Gbl_VerboseMode) printf PARAM_LIST(a) 132 #define VERBOSE_PRINT(a) if (Gbl_VerboseMode) printf PARAM_LIST(a) 133 134 #define REPLACE_WHOLE_WORD 0x00 135 #define REPLACE_SUBSTRINGS 0x01 136 #define REPLACE_MASK 0x01 137 138 #define EXTRA_INDENT_C 0x02 139 140 141 /* Conversion table structs */ 142 143 typedef struct acpi_string_table 144 { 145 char *Target; 146 char *Replacement; 147 UINT8 Type; 148 149 } ACPI_STRING_TABLE; 150 151 152 typedef struct acpi_typed_identifier_table 153 { 154 char *Identifier; 155 UINT8 Type; 156 157 } ACPI_TYPED_IDENTIFIER_TABLE; 158 159 #define SRC_TYPE_SIMPLE 0 160 #define SRC_TYPE_STRUCT 1 161 #define SRC_TYPE_UNION 2 162 163 164 typedef struct acpi_identifier_table 165 { 166 char *Identifier; 167 168 } ACPI_IDENTIFIER_TABLE; 169 170 typedef struct acpi_conversion_table 171 { 172 char *NewHeader; 173 UINT32 Flags; 174 175 ACPI_TYPED_IDENTIFIER_TABLE *LowerCaseTable; 176 177 ACPI_STRING_TABLE *SourceStringTable; 178 ACPI_IDENTIFIER_TABLE *SourceLineTable; 179 ACPI_IDENTIFIER_TABLE *SourceConditionalTable; 180 ACPI_IDENTIFIER_TABLE *SourceMacroTable; 181 ACPI_TYPED_IDENTIFIER_TABLE *SourceStructTable; 182 ACPI_IDENTIFIER_TABLE *SourceSpecialMacroTable; 183 UINT32 SourceFunctions; 184 185 ACPI_STRING_TABLE *HeaderStringTable; 186 ACPI_IDENTIFIER_TABLE *HeaderLineTable; 187 ACPI_IDENTIFIER_TABLE *HeaderConditionalTable; 188 ACPI_IDENTIFIER_TABLE *HeaderMacroTable; 189 ACPI_TYPED_IDENTIFIER_TABLE *HeaderStructTable; 190 ACPI_IDENTIFIER_TABLE *HeaderSpecialMacroTable; 191 UINT32 HeaderFunctions; 192 193 ACPI_STRING_TABLE *PatchStringTable; 194 ACPI_IDENTIFIER_TABLE *PatchLineTable; 195 ACPI_IDENTIFIER_TABLE *PatchConditionalTable; 196 ACPI_IDENTIFIER_TABLE *PatchMacroTable; 197 ACPI_TYPED_IDENTIFIER_TABLE *PatchStructTable; 198 ACPI_IDENTIFIER_TABLE *PatchSpecialMacroTable; 199 UINT32 PatchFunctions; 200 201 } ACPI_CONVERSION_TABLE; 202 203 204 /* Conversion tables */ 205 206 extern ACPI_CONVERSION_TABLE LinuxConversionTable; 207 extern ACPI_CONVERSION_TABLE CleanupConversionTable; 208 extern ACPI_CONVERSION_TABLE StatsConversionTable; 209 extern ACPI_CONVERSION_TABLE CustomConversionTable; 210 extern ACPI_CONVERSION_TABLE LicenseConversionTable; 211 extern ACPI_CONVERSION_TABLE IndentConversionTable; 212 213 typedef 214 char * (*AS_SCAN_CALLBACK) ( 215 char *Buffer, 216 char *Filename, 217 UINT32 LineNumber); 218 219 typedef struct as_brace_info 220 { 221 char *Operator; 222 UINT32 Length; 223 224 } AS_BRACE_INFO; 225 226 227 /* Prototypes */ 228 229 char * 230 AsSkipUntilChar ( 231 char *Buffer, 232 char Target); 233 234 char * 235 AsSkipPastChar ( 236 char *Buffer, 237 char Target); 238 239 char * 240 AsReplaceData ( 241 char *Buffer, 242 UINT32 LengthToRemove, 243 char *BufferToAdd, 244 UINT32 LengthToAdd); 245 246 int 247 AsReplaceString ( 248 char *Target, 249 char *Replacement, 250 UINT8 Type, 251 char *Buffer); 252 253 int 254 AsLowerCaseString ( 255 char *Target, 256 char *Buffer); 257 258 void 259 AsRemoveLine ( 260 char *Buffer, 261 char *Keyword); 262 263 void 264 AsRemoveMacro ( 265 char *Buffer, 266 char *Keyword); 267 268 void 269 AsCheckForBraces ( 270 char *Buffer, 271 char *Filename); 272 273 void 274 AsTrimLines ( 275 char *Buffer, 276 char *Filename); 277 278 void 279 AsMixedCaseToUnderscores ( 280 char *Buffer, 281 char *Filename); 282 283 void 284 AsCountTabs ( 285 char *Buffer, 286 char *Filename); 287 288 void 289 AsBracesOnSameLine ( 290 char *Buffer); 291 292 void 293 AsLowerCaseIdentifiers ( 294 char *Buffer); 295 296 void 297 AsReduceTypedefs ( 298 char *Buffer, 299 char *Keyword); 300 301 void 302 AsRemoveDebugMacros ( 303 char *Buffer); 304 305 void 306 AsRemoveEmptyBlocks ( 307 char *Buffer, 308 char *Filename); 309 310 void 311 AsCleanupSpecialMacro ( 312 char *Buffer, 313 char *Keyword); 314 315 void 316 AsCountSourceLines ( 317 char *Buffer, 318 char *Filename); 319 320 void 321 AsCountNonAnsiComments ( 322 char *Buffer, 323 char *Filename); 324 325 void 326 AsTrimWhitespace ( 327 char *Buffer); 328 329 void 330 AsTabify4 ( 331 char *Buffer); 332 333 void 334 AsTabify8 ( 335 char *Buffer); 336 337 void 338 AsRemoveConditionalCompile ( 339 char *Buffer, 340 char *Keyword); 341 342 ACPI_NATIVE_INT 343 AsProcessTree ( 344 ACPI_CONVERSION_TABLE *ConversionTable, 345 char *SourcePath, 346 char *TargetPath); 347 348 int 349 AsGetFile ( 350 char *FileName, 351 char **FileBuffer, 352 UINT32 *FileSize); 353 354 int 355 AsPutFile ( 356 char *Pathname, 357 char *FileBuffer, 358 UINT32 SystemFlags); 359 360 void 361 AsReplaceHeader ( 362 char *Buffer, 363 char *NewHeader); 364 365 void 366 AsConvertFile ( 367 ACPI_CONVERSION_TABLE *ConversionTable, 368 char *FileBuffer, 369 char *Filename, 370 ACPI_NATIVE_INT FileType); 371 372 ACPI_NATIVE_INT 373 AsProcessOneFile ( 374 ACPI_CONVERSION_TABLE *ConversionTable, 375 char *SourcePath, 376 char *TargetPath, 377 int MaxPathLength, 378 char *Filename, 379 ACPI_NATIVE_INT FileType); 380 381 ACPI_NATIVE_INT 382 AsCheckForDirectory ( 383 char *SourceDirPath, 384 char *TargetDirPath, 385 char *Filename, 386 char **SourcePath, 387 char **TargetPath); 388 389 void 390 AsRemoveExtraLines ( 391 char *FileBuffer, 392 char *Filename); 393 394 void 395 AsRemoveSpacesAfterPeriod ( 396 char *FileBuffer, 397 char *Filename); 398 399 BOOLEAN 400 AsMatchExactWord ( 401 char *Word, 402 UINT32 WordLength); 403 404 void 405 AsPrint ( 406 char *Message, 407 UINT32 Count, 408 char *Filename); 409 410 void 411 AsInsertPrefix ( 412 char *Buffer, 413 char *Keyword, 414 UINT8 Type); 415 416 char * 417 AsInsertData ( 418 char *Buffer, 419 char *BufferToAdd, 420 UINT32 LengthToAdd); 421 422 char * 423 AsRemoveData ( 424 char *StartPointer, 425 char *EndPointer); 426 427 void 428 AsInsertCarriageReturns ( 429 char *Buffer); 430 431 void 432 AsConvertToLineFeeds ( 433 char *Buffer); 434 435 void 436 AsStrlwr ( 437 char *SrcString); 438