1 /******************************************************************************
2  *
3  * Module Name: acpisrc.h - Include file for AcpiSrc utility
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2015, 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 AsCheckForBraces (
265     char                    *Buffer,
266     char                    *Filename);
267 
268 void
269 AsTrimLines (
270     char                    *Buffer,
271     char                    *Filename);
272 
273 void
274 AsMixedCaseToUnderscores (
275     char                    *Buffer,
276     char                    *Filename);
277 
278 void
279 AsCountTabs (
280     char                    *Buffer,
281     char                    *Filename);
282 
283 void
284 AsBracesOnSameLine (
285     char                    *Buffer);
286 
287 void
288 AsLowerCaseIdentifiers (
289     char                    *Buffer);
290 
291 void
292 AsReduceTypedefs (
293     char                    *Buffer,
294     char                    *Keyword);
295 
296 void
297 AsRemoveDebugMacros (
298     char                    *Buffer);
299 
300 void
301 AsRemoveEmptyBlocks (
302     char                    *Buffer,
303     char                    *Filename);
304 
305 void
306 AsCleanupSpecialMacro (
307     char                    *Buffer,
308     char                    *Keyword);
309 
310 void
311 AsCountSourceLines (
312     char                    *Buffer,
313     char                    *Filename);
314 
315 void
316 AsCountNonAnsiComments (
317     char                    *Buffer,
318     char                    *Filename);
319 
320 void
321 AsTrimWhitespace (
322     char                    *Buffer);
323 
324 void
325 AsTabify4 (
326     char                    *Buffer);
327 
328 void
329 AsTabify8 (
330     char                    *Buffer);
331 
332 void
333 AsRemoveConditionalCompile (
334     char                    *Buffer,
335     char                    *Keyword);
336 
337 ACPI_NATIVE_INT
338 AsProcessTree (
339     ACPI_CONVERSION_TABLE   *ConversionTable,
340     char                    *SourcePath,
341     char                    *TargetPath);
342 
343 int
344 AsGetFile (
345     char                    *FileName,
346     char                    **FileBuffer,
347     UINT32                  *FileSize);
348 
349 int
350 AsPutFile (
351     char                    *Pathname,
352     char                    *FileBuffer,
353     UINT32                  SystemFlags);
354 
355 void
356 AsReplaceHeader (
357     char                    *Buffer,
358     char                    *NewHeader);
359 
360 void
361 AsConvertFile (
362     ACPI_CONVERSION_TABLE   *ConversionTable,
363     char                    *FileBuffer,
364     char                    *Filename,
365     ACPI_NATIVE_INT         FileType);
366 
367 ACPI_NATIVE_INT
368 AsProcessOneFile (
369     ACPI_CONVERSION_TABLE   *ConversionTable,
370     char                    *SourcePath,
371     char                    *TargetPath,
372     int                     MaxPathLength,
373     char                    *Filename,
374     ACPI_NATIVE_INT         FileType);
375 
376 ACPI_NATIVE_INT
377 AsCheckForDirectory (
378     char                    *SourceDirPath,
379     char                    *TargetDirPath,
380     char                    *Filename,
381     char                    **SourcePath,
382     char                    **TargetPath);
383 
384 void
385 AsRemoveExtraLines (
386     char                    *FileBuffer,
387     char                    *Filename);
388 
389 void
390 AsRemoveSpacesAfterPeriod (
391     char                    *FileBuffer,
392     char                    *Filename);
393 
394 BOOLEAN
395 AsMatchExactWord (
396     char                    *Word,
397     UINT32                  WordLength);
398 
399 void
400 AsPrint (
401     char                    *Message,
402     UINT32                  Count,
403     char                    *Filename);
404 
405 void
406 AsInsertPrefix (
407     char                    *Buffer,
408     char                    *Keyword,
409     UINT8                   Type);
410 
411 char *
412 AsInsertData (
413     char                    *Buffer,
414     char                    *BufferToAdd,
415     UINT32                  LengthToAdd);
416 
417 char *
418 AsRemoveData (
419     char                    *StartPointer,
420     char                    *EndPointer);
421 
422 void
423 AsInsertCarriageReturns (
424     char                    *Buffer);
425 
426 void
427 AsConvertToLineFeeds (
428     char                    *Buffer);
429