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