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 
214 /* Prototypes */
215 
216 char *
217 AsSkipUntilChar (
218     char                    *Buffer,
219     char                    Target);
220 
221 char *
222 AsSkipPastChar (
223     char                    *Buffer,
224     char                    Target);
225 
226 char *
227 AsReplaceData (
228     char                    *Buffer,
229     UINT32                  LengthToRemove,
230     char                    *BufferToAdd,
231     UINT32                  LengthToAdd);
232 
233 int
234 AsReplaceString (
235     char                    *Target,
236     char                    *Replacement,
237     UINT8                   Type,
238     char                    *Buffer);
239 
240 int
241 AsLowerCaseString (
242     char                    *Target,
243     char                    *Buffer);
244 
245 void
246 AsRemoveLine (
247     char                    *Buffer,
248     char                    *Keyword);
249 
250 void
251 AsRemoveMacro (
252     char                    *Buffer,
253     char                    *Keyword);
254 
255 void
256 AsCheckForBraces (
257     char                    *Buffer,
258     char                    *Filename);
259 
260 void
261 AsTrimLines (
262     char                    *Buffer,
263     char                    *Filename);
264 
265 void
266 AsMixedCaseToUnderscores (
267     char                    *Buffer,
268     char                    *Filename);
269 
270 void
271 AsCountTabs (
272     char                    *Buffer,
273     char                    *Filename);
274 
275 void
276 AsBracesOnSameLine (
277     char                    *Buffer);
278 
279 void
280 AsLowerCaseIdentifiers (
281     char                    *Buffer);
282 
283 void
284 AsReduceTypedefs (
285     char                    *Buffer,
286     char                    *Keyword);
287 
288 void
289 AsRemoveDebugMacros (
290     char                    *Buffer);
291 
292 void
293 AsRemoveEmptyBlocks (
294     char                    *Buffer,
295     char                    *Filename);
296 
297 void
298 AsCleanupSpecialMacro (
299     char                    *Buffer,
300     char                    *Keyword);
301 
302 void
303 AsCountSourceLines (
304     char                    *Buffer,
305     char                    *Filename);
306 
307 void
308 AsCountNonAnsiComments (
309     char                    *Buffer,
310     char                    *Filename);
311 
312 void
313 AsTrimWhitespace (
314     char                    *Buffer);
315 
316 void
317 AsTabify4 (
318     char                    *Buffer);
319 
320 void
321 AsTabify8 (
322     char                    *Buffer);
323 
324 void
325 AsRemoveConditionalCompile (
326     char                    *Buffer,
327     char                    *Keyword);
328 
329 ACPI_NATIVE_INT
330 AsProcessTree (
331     ACPI_CONVERSION_TABLE   *ConversionTable,
332     char                    *SourcePath,
333     char                    *TargetPath);
334 
335 int
336 AsGetFile (
337     char                    *FileName,
338     char                    **FileBuffer,
339     UINT32                  *FileSize);
340 
341 int
342 AsPutFile (
343     char                    *Pathname,
344     char                    *FileBuffer,
345     UINT32                  SystemFlags);
346 
347 void
348 AsReplaceHeader (
349     char                    *Buffer,
350     char                    *NewHeader);
351 
352 void
353 AsConvertFile (
354     ACPI_CONVERSION_TABLE   *ConversionTable,
355     char                    *FileBuffer,
356     char                    *Filename,
357     ACPI_NATIVE_INT         FileType);
358 
359 ACPI_NATIVE_INT
360 AsProcessOneFile (
361     ACPI_CONVERSION_TABLE   *ConversionTable,
362     char                    *SourcePath,
363     char                    *TargetPath,
364     int                     MaxPathLength,
365     char                    *Filename,
366     ACPI_NATIVE_INT         FileType);
367 
368 ACPI_NATIVE_INT
369 AsCheckForDirectory (
370     char                    *SourceDirPath,
371     char                    *TargetDirPath,
372     char                    *Filename,
373     char                    **SourcePath,
374     char                    **TargetPath);
375 
376 void
377 AsRemoveExtraLines (
378     char                    *FileBuffer,
379     char                    *Filename);
380 
381 void
382 AsRemoveSpacesAfterPeriod (
383     char                    *FileBuffer,
384     char                    *Filename);
385 
386 BOOLEAN
387 AsMatchExactWord (
388     char                    *Word,
389     UINT32                  WordLength);
390 
391 void
392 AsPrint (
393     char                    *Message,
394     UINT32                  Count,
395     char                    *Filename);
396 
397 void
398 AsInsertPrefix (
399     char                    *Buffer,
400     char                    *Keyword,
401     UINT8                   Type);
402 
403 char *
404 AsInsertData (
405     char                    *Buffer,
406     char                    *BufferToAdd,
407     UINT32                  LengthToAdd);
408 
409 char *
410 AsRemoveData (
411     char                    *StartPointer,
412     char                    *EndPointer);
413 
414 void
415 AsInsertCarriageReturns (
416     char                    *Buffer);
417 
418 void
419 AsConvertToLineFeeds (
420     char                    *Buffer);
421 
422 void
423 AsStrlwr (
424     char                    *SrcString);
425