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