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 BOOLEAN
171 AsCheckForNonPrintableChars (
172     char                    *FileBuffer,
173     UINT32                  FileSize);
174 
175 static ACPI_INLINE int
176 AsMaxInt (int a, int b)
177 {
178     return (a > b ? a : b);
179 }
180 
181 
182 /******************************************************************************
183  *
184  * FUNCTION:    AsDoWildcard
185  *
186  * DESCRIPTION: Process files via wildcards
187  *
188  ******************************************************************************/
189 
190 void
191 AsDoWildcard (
192     ACPI_CONVERSION_TABLE   *ConversionTable,
193     char                    *SourcePath,
194     char                    *TargetPath,
195     int                     MaxPathLength,
196     int                     FileType,
197     char                    *WildcardSpec)
198 {
199     void                    *DirInfo;
200     char                    *Filename;
201     char                    *SourceDirPath;
202     char                    *TargetDirPath;
203     char                    RequestedFileType;
204 
205 
206     if (FileType == FILE_TYPE_DIRECTORY)
207     {
208         RequestedFileType = REQUEST_DIR_ONLY;
209     }
210     else
211     {
212         RequestedFileType = REQUEST_FILE_ONLY;
213     }
214 
215     VERBOSE_PRINT (("Checking for %s source files in directory \"%s\"\n",
216             WildcardSpec, SourcePath));
217 
218     /* Open the directory for wildcard search */
219 
220     DirInfo = AcpiOsOpenDirectory (SourcePath, WildcardSpec, RequestedFileType);
221     if (DirInfo)
222     {
223         /*
224          * Get all of the files that match both the
225          * wildcard and the requested file type
226          */
227         while ((Filename = AcpiOsGetNextFilename (DirInfo)))
228         {
229             /* Looking for directory files, must check file type */
230 
231             switch (RequestedFileType)
232             {
233             case REQUEST_DIR_ONLY:
234 
235                 /* If we actually have a dir, process the subtree */
236 
237                 if (!AsCheckForDirectory (SourcePath, TargetPath, Filename,
238                     &SourceDirPath, &TargetDirPath))
239                 {
240                     VERBOSE_PRINT (("Subdirectory: %s\n", Filename));
241 
242                     AsProcessTree (ConversionTable, SourceDirPath, TargetDirPath);
243                     free (SourceDirPath);
244                     free (TargetDirPath);
245                 }
246                 break;
247 
248             case REQUEST_FILE_ONLY:
249 
250                 /* Otherwise, this is a file, not a directory */
251 
252                 VERBOSE_PRINT (("File: %s\n", Filename));
253 
254                 AsProcessOneFile (ConversionTable, SourcePath, TargetPath,
255                     MaxPathLength, Filename, FileType);
256                 break;
257 
258             default:
259 
260                 break;
261             }
262         }
263 
264         /* Cleanup */
265 
266         AcpiOsCloseDirectory (DirInfo);
267     }
268 }
269 
270 
271 /******************************************************************************
272  *
273  * FUNCTION:    AsProcessTree
274  *
275  * DESCRIPTION: Process the directory tree. Files with the extension ".C" and
276  *              ".H" are processed as the tree is traversed.
277  *
278  ******************************************************************************/
279 
280 ACPI_NATIVE_INT
281 AsProcessTree (
282     ACPI_CONVERSION_TABLE   *ConversionTable,
283     char                    *SourcePath,
284     char                    *TargetPath)
285 {
286     int                     MaxPathLength;
287 
288 
289     MaxPathLength = AsMaxInt (strlen (SourcePath), strlen (TargetPath));
290 
291     if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
292     {
293         if (ConversionTable->Flags & FLG_LOWERCASE_DIRNAMES)
294         {
295             AcpiUtStrlwr (TargetPath);
296         }
297 
298         VERBOSE_PRINT (("Creating Directory \"%s\"\n", TargetPath));
299         if (mkdir (TargetPath))
300         {
301             if (errno != EEXIST)
302             {
303                 printf ("Could not create target directory\n");
304                 return (-1);
305             }
306         }
307     }
308 
309     /* Do the C source files */
310 
311     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
312         FILE_TYPE_SOURCE, "*.c");
313 
314     /* Do the C header files */
315 
316     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
317         FILE_TYPE_HEADER, "*.h");
318 
319     /* Do the Lex file(s) */
320 
321     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
322         FILE_TYPE_SOURCE, "*.l");
323 
324     /* Do the yacc file(s) */
325 
326     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
327         FILE_TYPE_SOURCE, "*.y");
328 
329     /* Do any ASL files */
330 
331     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
332         FILE_TYPE_HEADER, "*.asl");
333 
334     /* Do any subdirectories */
335 
336     AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
337         FILE_TYPE_DIRECTORY, "*");
338 
339     return (0);
340 }
341 
342 
343 /******************************************************************************
344  *
345  * FUNCTION:    AsDetectLoneLineFeeds
346  *
347  * DESCRIPTION: Find LF without CR.
348  *
349  ******************************************************************************/
350 
351 BOOLEAN
352 AsDetectLoneLineFeeds (
353     char                    *Filename,
354     char                    *Buffer)
355 {
356     UINT32                  i = 1;
357     UINT32                  LfCount = 0;
358     UINT32                  LineCount = 0;
359 
360 
361     if (!Buffer[0])
362     {
363         return (FALSE);
364     }
365 
366     while (Buffer[i])
367     {
368         if (Buffer[i] == 0x0A)
369         {
370             if (Buffer[i-1] != 0x0D)
371             {
372                 LfCount++;
373             }
374 
375             LineCount++;
376         }
377         i++;
378     }
379 
380     if (LfCount)
381     {
382         if (LineCount == LfCount)
383         {
384             if (!Gbl_IgnoreLoneLineFeeds)
385             {
386                 printf ("%s: ****File has UNIX format**** (LF only, not CR/LF) %u lines\n",
387                     Filename, LfCount);
388             }
389         }
390         else
391         {
392             printf ("%s: %u lone linefeeds in file\n", Filename, LfCount);
393         }
394 
395         return (TRUE);
396     }
397 
398     return (FALSE);
399 }
400 
401 
402 /******************************************************************************
403  *
404  * FUNCTION:    AsConvertFile
405  *
406  * DESCRIPTION: Perform the requested transforms on the file buffer (as
407  *              determined by the ConversionTable and the FileType).
408  *
409  ******************************************************************************/
410 
411 void
412 AsConvertFile (
413     ACPI_CONVERSION_TABLE   *ConversionTable,
414     char                    *FileBuffer,
415     char                    *Filename,
416     ACPI_NATIVE_INT         FileType)
417 {
418     UINT32                  i;
419     UINT32                  Functions;
420     ACPI_STRING_TABLE       *StringTable;
421     ACPI_IDENTIFIER_TABLE   *ConditionalTable;
422     ACPI_IDENTIFIER_TABLE   *LineTable;
423     ACPI_TYPED_IDENTIFIER_TABLE *StructTable;
424     ACPI_IDENTIFIER_TABLE   *SpecialMacroTable;
425 
426 
427     switch (FileType)
428     {
429     case FILE_TYPE_SOURCE:
430 
431         Functions           = ConversionTable->SourceFunctions;
432         StringTable         = ConversionTable->SourceStringTable;
433         LineTable           = ConversionTable->SourceLineTable;
434         ConditionalTable    = ConversionTable->SourceConditionalTable;
435         StructTable         = ConversionTable->SourceStructTable;
436         SpecialMacroTable   = ConversionTable->SourceSpecialMacroTable;
437         break;
438 
439     case FILE_TYPE_HEADER:
440 
441         Functions           = ConversionTable->HeaderFunctions;
442         StringTable         = ConversionTable->HeaderStringTable;
443         LineTable           = ConversionTable->HeaderLineTable;
444         ConditionalTable    = ConversionTable->HeaderConditionalTable;
445         StructTable         = ConversionTable->HeaderStructTable;
446         SpecialMacroTable   = ConversionTable->HeaderSpecialMacroTable;
447         break;
448 
449     case FILE_TYPE_PATCH:
450 
451         Functions           = ConversionTable->PatchFunctions;
452         StringTable         = ConversionTable->PatchStringTable;
453         LineTable           = ConversionTable->PatchLineTable;
454         ConditionalTable    = ConversionTable->PatchConditionalTable;
455         StructTable         = ConversionTable->PatchStructTable;
456         SpecialMacroTable   = ConversionTable->PatchSpecialMacroTable;
457         break;
458 
459     default:
460 
461         printf ("Unknown file type, cannot process\n");
462         return;
463     }
464 
465 
466     Gbl_StructDefs = strstr (FileBuffer, "/* acpisrc:StructDefs");
467     Gbl_Files++;
468     VERBOSE_PRINT (("Processing %u bytes\n",
469         (unsigned int) strlen (FileBuffer)));
470 
471     if (Gbl_Cleanup)
472     {
473         AsRemoveExtraLines (FileBuffer, Filename);
474         AsRemoveSpacesAfterPeriod (FileBuffer, Filename);
475     }
476 
477     if (ConversionTable->LowerCaseTable)
478     {
479         for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
480         {
481             AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
482                 FileBuffer);
483         }
484     }
485 
486     /* Process all the string replacements */
487 
488     if (StringTable)
489     {
490         for (i = 0; StringTable[i].Target; i++)
491         {
492             AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
493                 StringTable[i].Type, FileBuffer);
494         }
495     }
496 
497     if (LineTable)
498     {
499         for (i = 0; LineTable[i].Identifier; i++)
500         {
501             AsRemoveLine (FileBuffer, LineTable[i].Identifier);
502         }
503     }
504 
505     if (ConditionalTable)
506     {
507         for (i = 0; ConditionalTable[i].Identifier; i++)
508         {
509             AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
510         }
511     }
512 
513 #ifdef _OBSOLETE_FUNCTIONS
514     if (MacroTable)
515     {
516         for (i = 0; MacroTable[i].Identifier; i++)
517         {
518             AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
519         }
520     }
521 #endif
522 
523     if (StructTable)
524     {
525         for (i = 0; StructTable[i].Identifier; i++)
526         {
527             AsInsertPrefix (FileBuffer, StructTable[i].Identifier,
528                 StructTable[i].Type);
529         }
530     }
531 
532     if (SpecialMacroTable)
533     {
534         for (i = 0; SpecialMacroTable[i].Identifier; i++)
535         {
536             AsCleanupSpecialMacro (FileBuffer, SpecialMacroTable[i].Identifier);
537         }
538     }
539 
540     /* Process the function table */
541 
542     for (i = 0; i < 32; i++)
543     {
544         /* Decode the function bitmap */
545 
546         switch ((1 << i) & Functions)
547         {
548         case 0:
549 
550             /* This function not configured */
551             break;
552 
553         case CVT_COUNT_TABS:
554 
555             AsCountTabs (FileBuffer, Filename);
556             break;
557 
558         case CVT_COUNT_NON_ANSI_COMMENTS:
559 
560             AsCountNonAnsiComments (FileBuffer, Filename);
561             break;
562 
563         case CVT_CHECK_BRACES:
564 
565             AsCheckForBraces (FileBuffer, Filename);
566             break;
567 
568         case CVT_TRIM_LINES:
569 
570             AsTrimLines (FileBuffer, Filename);
571             break;
572 
573         case CVT_COUNT_LINES:
574 
575             AsCountSourceLines (FileBuffer, Filename);
576             break;
577 
578         case CVT_BRACES_ON_SAME_LINE:
579 
580             AsBracesOnSameLine (FileBuffer);
581             break;
582 
583         case CVT_MIXED_CASE_TO_UNDERSCORES:
584 
585             AsMixedCaseToUnderscores (FileBuffer, Filename);
586             break;
587 
588         case CVT_LOWER_CASE_IDENTIFIERS:
589 
590             AsLowerCaseIdentifiers (FileBuffer);
591             break;
592 
593         case CVT_REMOVE_DEBUG_MACROS:
594 
595             AsRemoveDebugMacros (FileBuffer);
596             break;
597 
598         case CVT_TRIM_WHITESPACE:
599 
600             AsTrimWhitespace (FileBuffer);
601             break;
602 
603         case CVT_REMOVE_EMPTY_BLOCKS:
604 
605             AsRemoveEmptyBlocks (FileBuffer, Filename);
606             break;
607 
608         case CVT_REDUCE_TYPEDEFS:
609 
610             AsReduceTypedefs (FileBuffer, "typedef union");
611             AsReduceTypedefs (FileBuffer, "typedef struct");
612             break;
613 
614         case CVT_SPACES_TO_TABS4:
615 
616             AsTabify4 (FileBuffer);
617             break;
618 
619         case CVT_SPACES_TO_TABS8:
620 
621             AsTabify8 (FileBuffer);
622             break;
623 
624         case CVT_COUNT_SHORTMULTILINE_COMMENTS:
625 
626 #ifdef ACPI_FUTURE_IMPLEMENTATION
627             AsTrimComments (FileBuffer, Filename);
628 #endif
629             break;
630 
631         default:
632 
633             printf ("Unknown conversion subfunction opcode\n");
634             break;
635         }
636     }
637 
638     if (ConversionTable->NewHeader)
639     {
640         AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
641     }
642 }
643 
644 /*******************************************************************************
645  *
646  * FUNCTION:    AsCheckForNonPrintableChars
647  *
648  * PARAMETERS:  FileBuffer              - Buffer with contents of entire file
649  *              FileSize                - Size of the file and buffer
650  *
651  * RETURN:      TRUE if there are no non-printable characters
652  *
653  * DESCRIPTION: Scan a file for any non-printable ASCII bytes.
654  *
655  ******************************************************************************/
656 
657 static BOOLEAN
658 AsCheckForNonPrintableChars (
659     char                    *FileBuffer,
660     UINT32                  FileSize)
661 {
662     BOOLEAN                 Found = TRUE;
663     UINT8                   Byte;
664     UINT32                  i;
665 
666 
667     /* Scan entire file for any non-printable characters */
668 
669     for (i = 0; i < FileSize; i++)
670     {
671         Byte = FileBuffer[i];
672         if (!isprint (Byte) && !isspace (Byte))
673         {
674             printf ( "Non-printable character (0x%2.2X) "
675                 "at file offset: %8u (0x%X)\n", Byte, i, i);
676             Found = FALSE;
677         }
678     }
679 
680     return (Found);
681 }
682 
683 
684 /******************************************************************************
685  *
686  * FUNCTION:    AsProcessOneFile
687  *
688  * DESCRIPTION: Process one source file. The file is opened, read entirely
689  *              into a buffer, converted, then written to a new file.
690  *
691  ******************************************************************************/
692 
693 ACPI_NATIVE_INT
694 AsProcessOneFile (
695     ACPI_CONVERSION_TABLE   *ConversionTable,
696     char                    *SourcePath,
697     char                    *TargetPath,
698     int                     MaxPathLength,
699     char                    *Filename,
700     ACPI_NATIVE_INT         FileType)
701 {
702     char                    *Pathname;
703     char                    *OutPathname;
704     int                     Status = 0;
705 
706 
707     /* Allocate a file pathname buffer for both source and target */
708 
709     Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1);
710     if (!Pathname)
711     {
712         printf ("Could not allocate buffer for file pathnames\n");
713         return (-1);
714     }
715 
716     Gbl_FileType = FileType;
717 
718     /* Generate the source pathname and read the file */
719 
720     if (SourcePath)
721     {
722         strcpy (Pathname, SourcePath);
723         strcat (Pathname, "/");
724     }
725 
726     strcat (Pathname, Filename);
727     if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize))
728     {
729         Status = -1;
730         goto Exit1;
731     }
732 
733     /* Exit now if simply checking the file for printable ascii chars */
734 
735     if (Gbl_CheckAscii)
736     {
737         Status = 0;
738         goto Exit2;
739     }
740 
741     Gbl_HeaderSize = 0;
742     if (strstr (Filename, ".asl"))
743     {
744         Gbl_HeaderSize = LINES_IN_ASL_HEADER; /* Lines in default ASL header */
745     }
746     else if (strstr (Gbl_FileBuffer, LEGAL_HEADER_SIGNATURE))
747     {
748         Gbl_HeaderSize = LINES_IN_LEGAL_HEADER; /* Normal C file and H header */
749     }
750     else if (strstr (Gbl_FileBuffer, LINUX_HEADER_SIGNATURE))
751     {
752         Gbl_HeaderSize = LINES_IN_LINUX_HEADER; /* Linuxized C file and H header */
753     }
754 
755     /* Process the file in the buffer */
756 
757     Gbl_MadeChanges = FALSE;
758     if (!Gbl_IgnoreLoneLineFeeds && Gbl_HasLoneLineFeeds)
759     {
760         /*
761          * All lone LFs will be converted to CR/LF
762          * (when file is written, Windows version only)
763          */
764         printf ("Converting lone linefeeds\n");
765         Gbl_MadeChanges = TRUE;
766     }
767 
768     AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType);
769 
770     if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
771     {
772         if (!(Gbl_Overwrite && !Gbl_MadeChanges))
773         {
774             /* Generate the target pathname and write the file */
775 
776             OutPathname = calloc (MaxPathLength +
777                 strlen (Filename) + 2 + strlen (TargetPath), 1);
778             if (!OutPathname)
779             {
780                 printf ("Could not allocate buffer for file pathnames\n");
781                 Status = -1;
782                 goto Exit2;
783             }
784 
785             strcpy (OutPathname, TargetPath);
786             if (SourcePath)
787             {
788                 strcat (OutPathname, "/");
789                 strcat (OutPathname, Filename);
790             }
791 
792             AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags);
793             free (OutPathname);
794         }
795     }
796 
797 Exit2:
798     free (Gbl_FileBuffer);
799 
800 Exit1:
801     free (Pathname);
802     return (Status);
803 }
804 
805 
806 /******************************************************************************
807  *
808  * FUNCTION:    AsCheckForDirectory
809  *
810  * DESCRIPTION: Check if the current file is a valid directory. If not,
811  *              construct the full pathname for the source and target paths.
812  *              Checks for the dot and dot-dot files (they are ignored)
813  *
814  ******************************************************************************/
815 
816 ACPI_NATIVE_INT
817 AsCheckForDirectory (
818     char                    *SourceDirPath,
819     char                    *TargetDirPath,
820     char                    *Filename,
821     char                    **SourcePath,
822     char                    **TargetPath)
823 {
824     char                    *SrcPath;
825     char                    *TgtPath;
826 
827 
828     if (!(strcmp (Filename, ".")) ||
829         !(strcmp (Filename, "..")))
830     {
831         return (-1);
832     }
833 
834     SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1);
835     if (!SrcPath)
836     {
837         printf ("Could not allocate buffer for directory source pathname\n");
838         return (-1);
839     }
840 
841     TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1);
842     if (!TgtPath)
843     {
844         printf ("Could not allocate buffer for directory target pathname\n");
845         free (SrcPath);
846         return (-1);
847     }
848 
849     strcpy (SrcPath, SourceDirPath);
850     strcat (SrcPath, "/");
851     strcat (SrcPath, Filename);
852 
853     strcpy (TgtPath, TargetDirPath);
854     strcat (TgtPath, "/");
855     strcat (TgtPath, Filename);
856 
857     *SourcePath = SrcPath;
858     *TargetPath = TgtPath;
859     return (0);
860 }
861 
862 
863 /******************************************************************************
864  *
865  * FUNCTION:    AsGetFile
866  *
867  * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
868  *
869  ******************************************************************************/
870 
871 int
872 AsGetFile (
873     char                    *Filename,
874     char                    **FileBuffer,
875     UINT32                  *FileSize)
876 {
877     FILE                    *File;
878     UINT32                  Size;
879     char                    *Buffer;
880     size_t                  Actual;
881 
882 
883     /* Binary mode leaves CR/LF pairs */
884 
885     File = fopen (Filename, "rb");
886     if (!File)
887     {
888         printf ("Could not open file %s\n", Filename);
889         return (-1);
890     }
891 
892     /* Need file size to allocate a buffer */
893 
894     Size = CmGetFileSize (File);
895     if (Size == ACPI_UINT32_MAX)
896     {
897         printf ("Could not get file size for %s\n", Filename);
898         goto ErrorExit;
899     }
900 
901     /*
902      * Create a buffer for the entire file
903      * Add plenty extra buffer to accommodate string replacements
904      */
905     Gbl_TotalSize += Size;
906 
907     Buffer = calloc (Size * 2, 1);
908     if (!Buffer)
909     {
910         printf ("Could not allocate buffer of size %u\n", Size * 2);
911         goto ErrorExit;
912     }
913 
914     /* Read the entire file */
915 
916     Actual = fread (Buffer, 1, Size, File);
917     if (Actual != Size)
918     {
919         printf ("Could not read the input file %s (%u bytes)\n",
920             Filename, Size);
921         goto ErrorFree;
922     }
923 
924     Buffer [Size] = 0;         /* Null terminate the buffer */
925     fclose (File);
926 
927     /* This option checks the entire file for non-printable chars */
928 
929     if (Gbl_CheckAscii)
930     {
931         if (AsCheckForNonPrintableChars (Buffer, Size))
932         {
933             printf ("File contains only printable ASCII characters\n");
934         }
935 
936         free (Buffer);
937         return (0);
938     }
939 
940     /* Check for unix contamination */
941 
942     Gbl_HasLoneLineFeeds = AsDetectLoneLineFeeds (Filename, Buffer);
943 
944     /*
945      * Convert all CR/LF pairs to LF only. We do this locally so that
946      * this code is portable across operating systems.
947      */
948     AsConvertToLineFeeds (Buffer);
949 
950     *FileBuffer = Buffer;
951     *FileSize = Size;
952     return (0);
953 
954 ErrorFree:
955     free (Buffer);
956 
957 ErrorExit:
958     fclose (File);
959     return (-1);
960 }
961 
962 
963 /******************************************************************************
964  *
965  * FUNCTION:    AsPutFile
966  *
967  * DESCRIPTION: Create a new output file and write the entire contents of the
968  *              buffer to the new file. Buffer must be a zero terminated string
969  *
970  ******************************************************************************/
971 
972 int
973 AsPutFile (
974     char                    *Pathname,
975     char                    *FileBuffer,
976     UINT32                  SystemFlags)
977 {
978     FILE                    *File;
979     UINT32                  FileSize;
980     size_t                  Actual;
981     int                     Status = 0;
982 
983 
984     /* Create the target file */
985 
986     if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS))
987     {
988         /* Put back the CR before each LF */
989 
990         AsInsertCarriageReturns (FileBuffer);
991     }
992 
993     File = fopen (Pathname, "w+b");
994     if (!File)
995     {
996         perror ("Could not create destination file");
997         printf ("Could not create destination file \"%s\"\n", Pathname);
998         return (-1);
999     }
1000 
1001     /* Write the buffer to the file */
1002 
1003     FileSize = strlen (FileBuffer);
1004     Actual = fwrite (FileBuffer, 1, FileSize, File);
1005     if (Actual != FileSize)
1006     {
1007         printf ("Error writing output file \"%s\"\n", Pathname);
1008         Status = -1;
1009     }
1010 
1011     fclose (File);
1012     return (Status);
1013 }
1014