asfile.c revision 1.1.1.12 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.11 christos * Copyright (C) 2000 - 2017, 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 jruoho
318 1.1 jruoho
319 1.1 jruoho switch (FileType)
320 1.1 jruoho {
321 1.1 jruoho case FILE_TYPE_SOURCE:
322 1.1.1.3 christos
323 1.1 jruoho Functions = ConversionTable->SourceFunctions;
324 1.1 jruoho StringTable = ConversionTable->SourceStringTable;
325 1.1 jruoho LineTable = ConversionTable->SourceLineTable;
326 1.1 jruoho ConditionalTable = ConversionTable->SourceConditionalTable;
327 1.1 jruoho StructTable = ConversionTable->SourceStructTable;
328 1.1.1.3 christos SpecialMacroTable = ConversionTable->SourceSpecialMacroTable;
329 1.1.1.10 christos break;
330 1.1 jruoho
331 1.1 jruoho case FILE_TYPE_HEADER:
332 1.1.1.3 christos
333 1.1 jruoho Functions = ConversionTable->HeaderFunctions;
334 1.1 jruoho StringTable = ConversionTable->HeaderStringTable;
335 1.1 jruoho LineTable = ConversionTable->HeaderLineTable;
336 1.1 jruoho ConditionalTable = ConversionTable->HeaderConditionalTable;
337 1.1 jruoho StructTable = ConversionTable->HeaderStructTable;
338 1.1.1.3 christos SpecialMacroTable = ConversionTable->HeaderSpecialMacroTable;
339 1.1 jruoho break;
340 1.1 jruoho
341 1.1.1.4 christos case FILE_TYPE_PATCH:
342 1.1.1.4 christos
343 1.1.1.4 christos Functions = ConversionTable->PatchFunctions;
344 1.1.1.4 christos StringTable = ConversionTable->PatchStringTable;
345 1.1.1.4 christos LineTable = ConversionTable->PatchLineTable;
346 1.1.1.4 christos ConditionalTable = ConversionTable->PatchConditionalTable;
347 1.1.1.4 christos StructTable = ConversionTable->PatchStructTable;
348 1.1.1.4 christos SpecialMacroTable = ConversionTable->PatchSpecialMacroTable;
349 1.1.1.4 christos break;
350 1.1.1.4 christos
351 1.1 jruoho default:
352 1.1.1.3 christos
353 1.1 jruoho printf ("Unknown file type, cannot process\n");
354 1.1 jruoho return;
355 1.1 jruoho }
356 1.1 jruoho
357 1.1 jruoho
358 1.1 jruoho Gbl_StructDefs = strstr (FileBuffer, "/* acpisrc:StructDefs");
359 1.1 jruoho Gbl_Files++;
360 1.1.1.2 jruoho VERBOSE_PRINT (("Processing %u bytes\n",
361 1.1.1.2 jruoho (unsigned int) strlen (FileBuffer)));
362 1.1 jruoho
363 1.1.1.3 christos if (Gbl_Cleanup)
364 1.1.1.3 christos {
365 1.1.1.3 christos AsRemoveExtraLines (FileBuffer, Filename);
366 1.1.1.3 christos AsRemoveSpacesAfterPeriod (FileBuffer, Filename);
367 1.1.1.3 christos }
368 1.1.1.3 christos
369 1.1 jruoho if (ConversionTable->LowerCaseTable)
370 1.1 jruoho {
371 1.1 jruoho for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
372 1.1 jruoho {
373 1.1 jruoho AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
374 1.1.1.7 christos FileBuffer);
375 1.1 jruoho }
376 1.1 jruoho }
377 1.1 jruoho
378 1.1 jruoho /* Process all the string replacements */
379 1.1 jruoho
380 1.1 jruoho if (StringTable)
381 1.1 jruoho {
382 1.1 jruoho for (i = 0; StringTable[i].Target; i++)
383 1.1 jruoho {
384 1.1 jruoho AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
385 1.1.1.7 christos StringTable[i].Type, FileBuffer);
386 1.1 jruoho }
387 1.1 jruoho }
388 1.1 jruoho
389 1.1 jruoho if (LineTable)
390 1.1 jruoho {
391 1.1 jruoho for (i = 0; LineTable[i].Identifier; i++)
392 1.1 jruoho {
393 1.1 jruoho AsRemoveLine (FileBuffer, LineTable[i].Identifier);
394 1.1 jruoho }
395 1.1 jruoho }
396 1.1 jruoho
397 1.1 jruoho if (ConditionalTable)
398 1.1 jruoho {
399 1.1 jruoho for (i = 0; ConditionalTable[i].Identifier; i++)
400 1.1 jruoho {
401 1.1 jruoho AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
402 1.1 jruoho }
403 1.1 jruoho }
404 1.1 jruoho
405 1.1.1.6 christos #ifdef _OBSOLETE_FUNCTIONS
406 1.1 jruoho if (MacroTable)
407 1.1 jruoho {
408 1.1 jruoho for (i = 0; MacroTable[i].Identifier; i++)
409 1.1 jruoho {
410 1.1 jruoho AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
411 1.1 jruoho }
412 1.1 jruoho }
413 1.1.1.6 christos #endif
414 1.1 jruoho
415 1.1 jruoho if (StructTable)
416 1.1 jruoho {
417 1.1 jruoho for (i = 0; StructTable[i].Identifier; i++)
418 1.1 jruoho {
419 1.1.1.7 christos AsInsertPrefix (FileBuffer, StructTable[i].Identifier,
420 1.1.1.7 christos StructTable[i].Type);
421 1.1 jruoho }
422 1.1 jruoho }
423 1.1 jruoho
424 1.1.1.3 christos if (SpecialMacroTable)
425 1.1.1.3 christos {
426 1.1.1.3 christos for (i = 0; SpecialMacroTable[i].Identifier; i++)
427 1.1.1.3 christos {
428 1.1.1.3 christos AsCleanupSpecialMacro (FileBuffer, SpecialMacroTable[i].Identifier);
429 1.1.1.3 christos }
430 1.1.1.3 christos }
431 1.1.1.3 christos
432 1.1 jruoho /* Process the function table */
433 1.1 jruoho
434 1.1 jruoho for (i = 0; i < 32; i++)
435 1.1 jruoho {
436 1.1 jruoho /* Decode the function bitmap */
437 1.1 jruoho
438 1.1 jruoho switch ((1 << i) & Functions)
439 1.1 jruoho {
440 1.1 jruoho case 0:
441 1.1.1.3 christos
442 1.1 jruoho /* This function not configured */
443 1.1 jruoho break;
444 1.1 jruoho
445 1.1 jruoho case CVT_COUNT_TABS:
446 1.1 jruoho
447 1.1 jruoho AsCountTabs (FileBuffer, Filename);
448 1.1 jruoho break;
449 1.1 jruoho
450 1.1 jruoho case CVT_COUNT_NON_ANSI_COMMENTS:
451 1.1 jruoho
452 1.1 jruoho AsCountNonAnsiComments (FileBuffer, Filename);
453 1.1 jruoho break;
454 1.1 jruoho
455 1.1 jruoho case CVT_CHECK_BRACES:
456 1.1 jruoho
457 1.1 jruoho AsCheckForBraces (FileBuffer, Filename);
458 1.1 jruoho break;
459 1.1 jruoho
460 1.1 jruoho case CVT_TRIM_LINES:
461 1.1 jruoho
462 1.1 jruoho AsTrimLines (FileBuffer, Filename);
463 1.1 jruoho break;
464 1.1 jruoho
465 1.1 jruoho case CVT_COUNT_LINES:
466 1.1 jruoho
467 1.1 jruoho AsCountSourceLines (FileBuffer, Filename);
468 1.1 jruoho break;
469 1.1 jruoho
470 1.1 jruoho case CVT_BRACES_ON_SAME_LINE:
471 1.1 jruoho
472 1.1 jruoho AsBracesOnSameLine (FileBuffer);
473 1.1 jruoho break;
474 1.1 jruoho
475 1.1 jruoho case CVT_MIXED_CASE_TO_UNDERSCORES:
476 1.1 jruoho
477 1.1.1.3 christos AsMixedCaseToUnderscores (FileBuffer, Filename);
478 1.1 jruoho break;
479 1.1 jruoho
480 1.1 jruoho case CVT_LOWER_CASE_IDENTIFIERS:
481 1.1 jruoho
482 1.1 jruoho AsLowerCaseIdentifiers (FileBuffer);
483 1.1 jruoho break;
484 1.1 jruoho
485 1.1 jruoho case CVT_REMOVE_DEBUG_MACROS:
486 1.1 jruoho
487 1.1 jruoho AsRemoveDebugMacros (FileBuffer);
488 1.1 jruoho break;
489 1.1 jruoho
490 1.1 jruoho case CVT_TRIM_WHITESPACE:
491 1.1 jruoho
492 1.1 jruoho AsTrimWhitespace (FileBuffer);
493 1.1 jruoho break;
494 1.1 jruoho
495 1.1 jruoho case CVT_REMOVE_EMPTY_BLOCKS:
496 1.1 jruoho
497 1.1 jruoho AsRemoveEmptyBlocks (FileBuffer, Filename);
498 1.1 jruoho break;
499 1.1 jruoho
500 1.1 jruoho case CVT_REDUCE_TYPEDEFS:
501 1.1 jruoho
502 1.1 jruoho AsReduceTypedefs (FileBuffer, "typedef union");
503 1.1 jruoho AsReduceTypedefs (FileBuffer, "typedef struct");
504 1.1 jruoho break;
505 1.1 jruoho
506 1.1 jruoho case CVT_SPACES_TO_TABS4:
507 1.1 jruoho
508 1.1 jruoho AsTabify4 (FileBuffer);
509 1.1 jruoho break;
510 1.1 jruoho
511 1.1 jruoho case CVT_SPACES_TO_TABS8:
512 1.1 jruoho
513 1.1 jruoho AsTabify8 (FileBuffer);
514 1.1 jruoho break;
515 1.1 jruoho
516 1.1 jruoho case CVT_COUNT_SHORTMULTILINE_COMMENTS:
517 1.1 jruoho
518 1.1 jruoho #ifdef ACPI_FUTURE_IMPLEMENTATION
519 1.1 jruoho AsTrimComments (FileBuffer, Filename);
520 1.1 jruoho #endif
521 1.1 jruoho break;
522 1.1 jruoho
523 1.1 jruoho default:
524 1.1 jruoho
525 1.1 jruoho printf ("Unknown conversion subfunction opcode\n");
526 1.1 jruoho break;
527 1.1 jruoho }
528 1.1 jruoho }
529 1.1 jruoho
530 1.1 jruoho if (ConversionTable->NewHeader)
531 1.1 jruoho {
532 1.1 jruoho AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
533 1.1 jruoho }
534 1.1 jruoho }
535 1.1 jruoho
536 1.1.1.12 christos /*******************************************************************************
537 1.1.1.12 christos *
538 1.1.1.12 christos * FUNCTION: AsCheckForNonPrintableChars
539 1.1.1.12 christos *
540 1.1.1.12 christos * PARAMETERS: FileBuffer - Buffer with contents of entire file
541 1.1.1.12 christos * FileSize - Size of the file and buffer
542 1.1.1.12 christos *
543 1.1.1.12 christos * RETURN: TRUE if there are no non-printable characters
544 1.1.1.12 christos *
545 1.1.1.12 christos * DESCRIPTION: Scan a file for any non-printable ASCII bytes.
546 1.1.1.12 christos *
547 1.1.1.12 christos ******************************************************************************/
548 1.1.1.12 christos
549 1.1.1.12 christos static BOOLEAN
550 1.1.1.12 christos AsCheckForNonPrintableChars (
551 1.1.1.12 christos char *FileBuffer,
552 1.1.1.12 christos UINT32 FileSize)
553 1.1.1.12 christos {
554 1.1.1.12 christos BOOLEAN Found = TRUE;
555 1.1.1.12 christos UINT8 Byte;
556 1.1.1.12 christos UINT32 i;
557 1.1.1.12 christos
558 1.1.1.12 christos
559 1.1.1.12 christos /* Scan entire file for any non-printable characters */
560 1.1.1.12 christos
561 1.1.1.12 christos for (i = 0; i < FileSize; i++)
562 1.1.1.12 christos {
563 1.1.1.12 christos Byte = FileBuffer[i];
564 1.1.1.12 christos if (!isprint (Byte) && !isspace (Byte))
565 1.1.1.12 christos {
566 1.1.1.12 christos printf ( "Non-printable character (0x%2.2X) "
567 1.1.1.12 christos "at file offset: %8u (0x%X)\n", Byte, i, i);
568 1.1.1.12 christos Found = FALSE;
569 1.1.1.12 christos }
570 1.1.1.12 christos }
571 1.1.1.12 christos
572 1.1.1.12 christos return (Found);
573 1.1.1.12 christos }
574 1.1.1.12 christos
575 1.1 jruoho
576 1.1 jruoho /******************************************************************************
577 1.1 jruoho *
578 1.1 jruoho * FUNCTION: AsProcessOneFile
579 1.1 jruoho *
580 1.1.1.3 christos * DESCRIPTION: Process one source file. The file is opened, read entirely
581 1.1 jruoho * into a buffer, converted, then written to a new file.
582 1.1 jruoho *
583 1.1 jruoho ******************************************************************************/
584 1.1 jruoho
585 1.1 jruoho ACPI_NATIVE_INT
586 1.1 jruoho AsProcessOneFile (
587 1.1 jruoho ACPI_CONVERSION_TABLE *ConversionTable,
588 1.1 jruoho char *SourcePath,
589 1.1 jruoho char *TargetPath,
590 1.1 jruoho int MaxPathLength,
591 1.1 jruoho char *Filename,
592 1.1 jruoho ACPI_NATIVE_INT FileType)
593 1.1 jruoho {
594 1.1 jruoho char *Pathname;
595 1.1.1.9 christos char *OutPathname;
596 1.1.1.9 christos int Status = 0;
597 1.1 jruoho
598 1.1 jruoho
599 1.1 jruoho /* Allocate a file pathname buffer for both source and target */
600 1.1 jruoho
601 1.1 jruoho Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1);
602 1.1 jruoho if (!Pathname)
603 1.1 jruoho {
604 1.1 jruoho printf ("Could not allocate buffer for file pathnames\n");
605 1.1.1.3 christos return (-1);
606 1.1 jruoho }
607 1.1 jruoho
608 1.1 jruoho Gbl_FileType = FileType;
609 1.1 jruoho
610 1.1 jruoho /* Generate the source pathname and read the file */
611 1.1 jruoho
612 1.1 jruoho if (SourcePath)
613 1.1 jruoho {
614 1.1 jruoho strcpy (Pathname, SourcePath);
615 1.1 jruoho strcat (Pathname, "/");
616 1.1 jruoho }
617 1.1 jruoho
618 1.1 jruoho strcat (Pathname, Filename);
619 1.1 jruoho if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize))
620 1.1 jruoho {
621 1.1.1.9 christos Status = -1;
622 1.1.1.9 christos goto Exit1;
623 1.1 jruoho }
624 1.1 jruoho
625 1.1.1.12 christos /* Exit now if simply checking the file for printable ascii chars */
626 1.1.1.12 christos
627 1.1.1.12 christos if (Gbl_CheckAscii)
628 1.1.1.12 christos {
629 1.1.1.12 christos Status = 0;
630 1.1.1.12 christos goto Exit2;
631 1.1.1.12 christos }
632 1.1.1.12 christos
633 1.1 jruoho Gbl_HeaderSize = 0;
634 1.1 jruoho if (strstr (Filename, ".asl"))
635 1.1 jruoho {
636 1.1 jruoho Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */
637 1.1 jruoho }
638 1.1 jruoho else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE))
639 1.1 jruoho {
640 1.1 jruoho Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */
641 1.1 jruoho }
642 1.1 jruoho else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE))
643 1.1 jruoho {
644 1.1 jruoho Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */
645 1.1 jruoho }
646 1.1 jruoho
647 1.1 jruoho /* Process the file in the buffer */
648 1.1 jruoho
649 1.1 jruoho Gbl_MadeChanges = FALSE;
650 1.1 jruoho if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds)
651 1.1 jruoho {
652 1.1 jruoho /*
653 1.1 jruoho * All lone LFs will be converted to CR/LF
654 1.1 jruoho * (when file is written, Windows version only)
655 1.1 jruoho */
656 1.1 jruoho printf ("Converting lone linefeeds\n");
657 1.1 jruoho Gbl_MadeChanges = TRUE;
658 1.1 jruoho }
659 1.1 jruoho
660 1.1 jruoho AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType);
661 1.1 jruoho
662 1.1 jruoho if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
663 1.1 jruoho {
664 1.1 jruoho if (!(Gbl_Overwrite && !Gbl_MadeChanges))
665 1.1 jruoho {
666 1.1 jruoho /* Generate the target pathname and write the file */
667 1.1 jruoho
668 1.1.1.7 christos OutPathname = calloc (MaxPathLength +
669 1.1.1.7 christos strlen (Filename) + 2 + strlen (TargetPath), 1);
670 1.1 jruoho if (!OutPathname)
671 1.1 jruoho {
672 1.1 jruoho printf ("Could not allocate buffer for file pathnames\n");
673 1.1.1.9 christos Status = -1;
674 1.1.1.9 christos goto Exit2;
675 1.1 jruoho }
676 1.1 jruoho
677 1.1 jruoho strcpy (OutPathname, TargetPath);
678 1.1 jruoho if (SourcePath)
679 1.1 jruoho {
680 1.1 jruoho strcat (OutPathname, "/");
681 1.1 jruoho strcat (OutPathname, Filename);
682 1.1 jruoho }
683 1.1 jruoho
684 1.1 jruoho AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags);
685 1.1.1.9 christos free (OutPathname);
686 1.1 jruoho }
687 1.1 jruoho }
688 1.1 jruoho
689 1.1.1.9 christos Exit2:
690 1.1 jruoho free (Gbl_FileBuffer);
691 1.1 jruoho
692 1.1.1.9 christos Exit1:
693 1.1.1.9 christos free (Pathname);
694 1.1.1.9 christos return (Status);
695 1.1 jruoho }
696 1.1 jruoho
697 1.1 jruoho
698 1.1 jruoho /******************************************************************************
699 1.1 jruoho *
700 1.1 jruoho * FUNCTION: AsCheckForDirectory
701 1.1 jruoho *
702 1.1.1.3 christos * DESCRIPTION: Check if the current file is a valid directory. If not,
703 1.1 jruoho * construct the full pathname for the source and target paths.
704 1.1 jruoho * Checks for the dot and dot-dot files (they are ignored)
705 1.1 jruoho *
706 1.1 jruoho ******************************************************************************/
707 1.1 jruoho
708 1.1 jruoho ACPI_NATIVE_INT
709 1.1 jruoho AsCheckForDirectory (
710 1.1 jruoho char *SourceDirPath,
711 1.1 jruoho char *TargetDirPath,
712 1.1 jruoho char *Filename,
713 1.1 jruoho char **SourcePath,
714 1.1 jruoho char **TargetPath)
715 1.1 jruoho {
716 1.1 jruoho char *SrcPath;
717 1.1 jruoho char *TgtPath;
718 1.1 jruoho
719 1.1 jruoho
720 1.1 jruoho if (!(strcmp (Filename, ".")) ||
721 1.1 jruoho !(strcmp (Filename, "..")))
722 1.1 jruoho {
723 1.1.1.3 christos return (-1);
724 1.1 jruoho }
725 1.1 jruoho
726 1.1 jruoho SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1);
727 1.1 jruoho if (!SrcPath)
728 1.1 jruoho {
729 1.1 jruoho printf ("Could not allocate buffer for directory source pathname\n");
730 1.1.1.3 christos return (-1);
731 1.1 jruoho }
732 1.1 jruoho
733 1.1 jruoho TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1);
734 1.1 jruoho if (!TgtPath)
735 1.1 jruoho {
736 1.1 jruoho printf ("Could not allocate buffer for directory target pathname\n");
737 1.1 jruoho free (SrcPath);
738 1.1.1.3 christos return (-1);
739 1.1 jruoho }
740 1.1 jruoho
741 1.1 jruoho strcpy (SrcPath, SourceDirPath);
742 1.1 jruoho strcat (SrcPath, "/");
743 1.1 jruoho strcat (SrcPath, Filename);
744 1.1 jruoho
745 1.1 jruoho strcpy (TgtPath, TargetDirPath);
746 1.1 jruoho strcat (TgtPath, "/");
747 1.1 jruoho strcat (TgtPath, Filename);
748 1.1 jruoho
749 1.1 jruoho *SourcePath = SrcPath;
750 1.1 jruoho *TargetPath = TgtPath;
751 1.1.1.3 christos return (0);
752 1.1 jruoho }
753 1.1 jruoho
754 1.1 jruoho
755 1.1 jruoho /******************************************************************************
756 1.1 jruoho *
757 1.1 jruoho * FUNCTION: AsGetFile
758 1.1 jruoho *
759 1.1 jruoho * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
760 1.1 jruoho *
761 1.1 jruoho ******************************************************************************/
762 1.1 jruoho
763 1.1 jruoho int
764 1.1 jruoho AsGetFile (
765 1.1 jruoho char *Filename,
766 1.1 jruoho char **FileBuffer,
767 1.1 jruoho UINT32 *FileSize)
768 1.1 jruoho {
769 1.1.1.3 christos FILE *File;
770 1.1 jruoho UINT32 Size;
771 1.1 jruoho char *Buffer;
772 1.1.1.3 christos size_t Actual;
773 1.1 jruoho
774 1.1 jruoho
775 1.1 jruoho /* Binary mode leaves CR/LF pairs */
776 1.1 jruoho
777 1.1.1.3 christos File = fopen (Filename, "rb");
778 1.1.1.3 christos if (!File)
779 1.1 jruoho {
780 1.1.1.3 christos printf ("Could not open file %s\n", Filename);
781 1.1.1.3 christos return (-1);
782 1.1 jruoho }
783 1.1 jruoho
784 1.1.1.3 christos /* Need file size to allocate a buffer */
785 1.1.1.3 christos
786 1.1.1.4 christos Size = CmGetFileSize (File);
787 1.1.1.4 christos if (Size == ACPI_UINT32_MAX)
788 1.1 jruoho {
789 1.1.1.3 christos printf ("Could not get file size for %s\n", Filename);
790 1.1 jruoho goto ErrorExit;
791 1.1 jruoho }
792 1.1 jruoho
793 1.1 jruoho /*
794 1.1 jruoho * Create a buffer for the entire file
795 1.1.1.3 christos * Add plenty extra buffer to accommodate string replacements
796 1.1 jruoho */
797 1.1 jruoho Gbl_TotalSize += Size;
798 1.1 jruoho
799 1.1 jruoho Buffer = calloc (Size * 2, 1);
800 1.1 jruoho if (!Buffer)
801 1.1 jruoho {
802 1.1 jruoho printf ("Could not allocate buffer of size %u\n", Size * 2);
803 1.1 jruoho goto ErrorExit;
804 1.1 jruoho }
805 1.1 jruoho
806 1.1 jruoho /* Read the entire file */
807 1.1 jruoho
808 1.1.1.3 christos Actual = fread (Buffer, 1, Size, File);
809 1.1.1.3 christos if (Actual != Size)
810 1.1 jruoho {
811 1.1.1.3 christos printf ("Could not read the input file %s (%u bytes)\n",
812 1.1.1.3 christos Filename, Size);
813 1.1.1.8 christos goto ErrorFree;
814 1.1 jruoho }
815 1.1 jruoho
816 1.1 jruoho Buffer [Size] = 0; /* Null terminate the buffer */
817 1.1.1.3 christos fclose (File);
818 1.1 jruoho
819 1.1.1.12 christos /* This option checks the entire file for non-printable chars */
820 1.1.1.12 christos
821 1.1.1.12 christos if (Gbl_CheckAscii)
822 1.1.1.12 christos {
823 1.1.1.12 christos if (AsCheckForNonPrintableChars (Buffer, Size))
824 1.1.1.12 christos {
825 1.1.1.12 christos printf ("File contains only printable ASCII characters\n");
826 1.1.1.12 christos }
827 1.1.1.12 christos
828 1.1.1.12 christos free (Buffer);
829 1.1.1.12 christos return (0);
830 1.1.1.12 christos }
831 1.1.1.12 christos
832 1.1 jruoho /* Check for unix contamination */
833 1.1 jruoho
834 1.1 jruoho Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer);
835 1.1 jruoho
836 1.1 jruoho /*
837 1.1.1.3 christos * Convert all CR/LF pairs to LF only. We do this locally so that
838 1.1 jruoho * this code is portable across operating systems.
839 1.1 jruoho */
840 1.1 jruoho AsConvertToLineFeeds (Buffer);
841 1.1 jruoho
842 1.1 jruoho *FileBuffer = Buffer;
843 1.1 jruoho *FileSize = Size;
844 1.1.1.3 christos return (0);
845 1.1 jruoho
846 1.1.1.8 christos ErrorFree:
847 1.1.1.8 christos free (Buffer);
848 1.1 jruoho
849 1.1 jruoho ErrorExit:
850 1.1.1.3 christos fclose (File);
851 1.1.1.3 christos return (-1);
852 1.1 jruoho }
853 1.1 jruoho
854 1.1 jruoho
855 1.1 jruoho /******************************************************************************
856 1.1 jruoho *
857 1.1 jruoho * FUNCTION: AsPutFile
858 1.1 jruoho *
859 1.1 jruoho * DESCRIPTION: Create a new output file and write the entire contents of the
860 1.1.1.3 christos * buffer to the new file. Buffer must be a zero terminated string
861 1.1 jruoho *
862 1.1 jruoho ******************************************************************************/
863 1.1 jruoho
864 1.1 jruoho int
865 1.1 jruoho AsPutFile (
866 1.1 jruoho char *Pathname,
867 1.1 jruoho char *FileBuffer,
868 1.1 jruoho UINT32 SystemFlags)
869 1.1 jruoho {
870 1.1.1.3 christos FILE *File;
871 1.1 jruoho UINT32 FileSize;
872 1.1.1.3 christos size_t Actual;
873 1.1.1.3 christos int Status = 0;
874 1.1 jruoho
875 1.1 jruoho
876 1.1 jruoho /* Create the target file */
877 1.1 jruoho
878 1.1 jruoho if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS))
879 1.1 jruoho {
880 1.1 jruoho /* Put back the CR before each LF */
881 1.1 jruoho
882 1.1 jruoho AsInsertCarriageReturns (FileBuffer);
883 1.1 jruoho }
884 1.1 jruoho
885 1.1.1.3 christos File = fopen (Pathname, "w+b");
886 1.1.1.3 christos if (!File)
887 1.1 jruoho {
888 1.1 jruoho perror ("Could not create destination file");
889 1.1 jruoho printf ("Could not create destination file \"%s\"\n", Pathname);
890 1.1.1.3 christos return (-1);
891 1.1 jruoho }
892 1.1 jruoho
893 1.1 jruoho /* Write the buffer to the file */
894 1.1 jruoho
895 1.1 jruoho FileSize = strlen (FileBuffer);
896 1.1.1.3 christos Actual = fwrite (FileBuffer, 1, FileSize, File);
897 1.1.1.3 christos if (Actual != FileSize)
898 1.1.1.3 christos {
899 1.1.1.3 christos printf ("Error writing output file \"%s\"\n", Pathname);
900 1.1.1.3 christos Status = -1;
901 1.1.1.3 christos }
902 1.1 jruoho
903 1.1.1.3 christos fclose (File);
904 1.1.1.3 christos return (Status);
905 1.1 jruoho }
906