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