checkdirs() was being passed the wrong mount point, resulting in a panic
[dragonfly.git] / sys / contrib / dev / acpica-unix-20050309 / tools / acpisrc / asfile.c
blob69638c06a4cd1d9d366d6e43a01f80e93cde1546
2 /******************************************************************************
4 * Module Name: asfile - Main module for the acpi source processor utility
5 * $Revision: 34 $
7 *****************************************************************************/
9 /******************************************************************************
11 * 1. Copyright Notice
13 * Some or all of this work - Copyright (c) 1999 - 2005, Intel Corp.
14 * All rights reserved.
16 * 2. License
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights. You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
21 * property rights.
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code. No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
39 * The above copyright and patent license is granted only if the following
40 * conditions are met:
42 * 3. Conditions
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision. In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change. Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee. Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution. In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
66 * make.
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
72 * distribution.
74 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * Intel Code.
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
82 * 4. Disclaimer and Export Compliance
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * PARTICULAR PURPOSE.
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * LIMITED REMEDY.
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government. In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
116 *****************************************************************************/
118 #include "acpisrc.h"
121 /******************************************************************************
123 * FUNCTION: AsDoWildcard
125 * DESCRIPTION: Process files via wildcards
127 ******************************************************************************/
129 void
130 AsDoWildcard (
131 ACPI_CONVERSION_TABLE *ConversionTable,
132 char *SourcePath,
133 char *TargetPath,
134 int MaxPathLength,
135 int FileType,
136 char *WildcardSpec)
138 void *DirInfo;
139 char *Filename;
140 char *SourceDirPath;
141 char *TargetDirPath;
142 char RequestedFileType;
145 if (FileType == FILE_TYPE_DIRECTORY)
147 RequestedFileType = REQUEST_DIR_ONLY;
149 else
151 RequestedFileType = REQUEST_FILE_ONLY;
154 VERBOSE_PRINT (("Checking for %s source files in directory \"%s\"\n",
155 WildcardSpec, SourcePath));
157 /* Open the directory for wildcard search */
159 DirInfo = AcpiOsOpenDirectory (SourcePath, WildcardSpec, RequestedFileType);
160 if (DirInfo)
163 * Get all of the files that match both the
164 * wildcard and the requested file type
166 while ((Filename = AcpiOsGetNextFilename (DirInfo)))
168 /* Looking for directory files, must check file type */
170 switch (RequestedFileType)
172 case REQUEST_DIR_ONLY:
174 /* If we actually have a dir, process the subtree */
176 if (!AsCheckForDirectory (SourcePath, TargetPath, Filename,
177 &SourceDirPath, &TargetDirPath))
179 VERBOSE_PRINT (("Subdirectory: %s\n", Filename));
181 AsProcessTree (ConversionTable, SourceDirPath, TargetDirPath);
182 free (SourceDirPath);
183 free (TargetDirPath);
185 break;
187 case REQUEST_FILE_ONLY:
189 /* Otherwise, this is a file, not a directory */
191 VERBOSE_PRINT (("File: %s\n", Filename));
193 AsProcessOneFile (ConversionTable, SourcePath, TargetPath,
194 MaxPathLength, Filename, FileType);
195 break;
199 /* Cleanup */
201 AcpiOsCloseDirectory (DirInfo);
206 /******************************************************************************
208 * FUNCTION: AsProcessTree
210 * DESCRIPTION: Process the directory tree. Files with the extension ".C" and
211 * ".H" are processed as the tree is traversed.
213 ******************************************************************************/
215 ACPI_NATIVE_INT
216 AsProcessTree (
217 ACPI_CONVERSION_TABLE *ConversionTable,
218 char *SourcePath,
219 char *TargetPath)
221 int MaxPathLength;
224 MaxPathLength = max (strlen (SourcePath), strlen (TargetPath));
226 if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
228 if (ConversionTable->Flags & FLG_LOWERCASE_DIRNAMES)
230 strlwr (TargetPath);
233 VERBOSE_PRINT (("Creating Directory \"%s\"\n", TargetPath));
234 if (mkdir (TargetPath))
236 if (errno != EEXIST)
238 printf ("Could not create target directory\n");
239 return -1;
244 /* Do the C source files */
246 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
247 FILE_TYPE_SOURCE, "*.c");
249 /* Do the C header files */
251 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
252 FILE_TYPE_HEADER, "*.h");
254 /* Do the Lex file(s) */
256 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
257 FILE_TYPE_SOURCE, "*.l");
259 /* Do the yacc file(s) */
261 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
262 FILE_TYPE_SOURCE, "*.y");
264 /* Do any subdirectories */
266 AsDoWildcard (ConversionTable, SourcePath, TargetPath, MaxPathLength,
267 FILE_TYPE_DIRECTORY, "*");
269 return 0;
273 /******************************************************************************
275 * FUNCTION: AsDetectLoneLineFeeds
277 * DESCRIPTION: Find LF without CR.
279 ******************************************************************************/
281 BOOLEAN
282 AsDetectLoneLineFeeds (
283 char *Filename,
284 char *Buffer)
286 UINT32 i = 1;
287 UINT32 LfCount = 0;
290 if (!Buffer[0])
292 return FALSE;
295 while (Buffer[i])
297 if (Buffer[i] == 0x0A)
299 if (Buffer[i-1] != 0x0D)
301 LfCount++;
305 i++;
308 if (LfCount)
310 printf ("****UNIX CONTAMINATION DETECTED****\n");
311 printf ("%d lone linefeeds in file %s\n", LfCount, Filename);
312 return TRUE;
315 return (FALSE);
319 /******************************************************************************
321 * FUNCTION: AsConvertFile
323 * DESCRIPTION: Perform the requested transforms on the file buffer (as
324 * determined by the ConversionTable and the FileType).
326 ******************************************************************************/
328 void
329 AsConvertFile (
330 ACPI_CONVERSION_TABLE *ConversionTable,
331 char *FileBuffer,
332 char *Filename,
333 ACPI_NATIVE_INT FileType)
335 UINT32 i;
336 UINT32 Functions;
337 ACPI_STRING_TABLE *StringTable;
338 ACPI_IDENTIFIER_TABLE *ConditionalTable;
339 ACPI_IDENTIFIER_TABLE *LineTable;
340 ACPI_IDENTIFIER_TABLE *MacroTable;
341 ACPI_TYPED_IDENTIFIER_TABLE *StructTable;
344 switch (FileType)
346 case FILE_TYPE_SOURCE:
347 Functions = ConversionTable->SourceFunctions;
348 StringTable = ConversionTable->SourceStringTable;
349 LineTable = ConversionTable->SourceLineTable;
350 ConditionalTable = ConversionTable->SourceConditionalTable;
351 MacroTable = ConversionTable->SourceMacroTable;
352 StructTable = ConversionTable->SourceStructTable;
353 break;
355 case FILE_TYPE_HEADER:
356 Functions = ConversionTable->HeaderFunctions;
357 StringTable = ConversionTable->HeaderStringTable;
358 LineTable = ConversionTable->HeaderLineTable;
359 ConditionalTable = ConversionTable->HeaderConditionalTable;
360 MacroTable = ConversionTable->HeaderMacroTable;
361 StructTable = ConversionTable->HeaderStructTable;
362 break;
364 default:
365 printf ("Unknown file type, cannot process\n");
366 return;
369 Gbl_Files++;
370 VERBOSE_PRINT (("Processing %d bytes\n", strlen (FileBuffer)));
372 if (ConversionTable->LowerCaseTable)
374 for (i = 0; ConversionTable->LowerCaseTable[i].Identifier; i++)
376 AsLowerCaseString (ConversionTable->LowerCaseTable[i].Identifier,
377 FileBuffer);
381 /* Process all the string replacements */
383 if (StringTable)
385 for (i = 0; StringTable[i].Target; i++)
387 AsReplaceString (StringTable[i].Target, StringTable[i].Replacement,
388 StringTable[i].Type, FileBuffer);
392 if (LineTable)
394 for (i = 0; LineTable[i].Identifier; i++)
396 AsRemoveLine (FileBuffer, LineTable[i].Identifier);
400 if (ConditionalTable)
402 for (i = 0; ConditionalTable[i].Identifier; i++)
404 AsRemoveConditionalCompile (FileBuffer, ConditionalTable[i].Identifier);
408 if (MacroTable)
410 for (i = 0; MacroTable[i].Identifier; i++)
412 AsRemoveMacro (FileBuffer, MacroTable[i].Identifier);
416 if (StructTable)
418 for (i = 0; StructTable[i].Identifier; i++)
420 AsInsertPrefix (FileBuffer, StructTable[i].Identifier, StructTable[i].Type);
424 /* Process the function table */
426 for (i = 0; i < 32; i++)
428 /* Decode the function bitmap */
430 switch ((1 << i) & Functions)
432 case 0:
433 /* This function not configured */
434 break;
437 case CVT_COUNT_TABS:
439 AsCountTabs (FileBuffer, Filename);
440 break;
443 case CVT_COUNT_NON_ANSI_COMMENTS:
445 AsCountNonAnsiComments (FileBuffer, Filename);
446 break;
449 case CVT_CHECK_BRACES:
451 AsCheckForBraces (FileBuffer, Filename);
452 break;
455 case CVT_TRIM_LINES:
457 AsTrimLines (FileBuffer, Filename);
458 break;
461 case CVT_COUNT_LINES:
463 AsCountSourceLines (FileBuffer, Filename);
464 break;
467 case CVT_BRACES_ON_SAME_LINE:
469 AsBracesOnSameLine (FileBuffer);
470 break;
473 case CVT_MIXED_CASE_TO_UNDERSCORES:
475 AsMixedCaseToUnderscores (FileBuffer);
476 break;
479 case CVT_LOWER_CASE_IDENTIFIERS:
481 AsLowerCaseIdentifiers (FileBuffer);
482 break;
485 case CVT_REMOVE_DEBUG_MACROS:
487 AsRemoveDebugMacros (FileBuffer);
488 break;
491 case CVT_TRIM_WHITESPACE:
493 AsTrimWhitespace (FileBuffer);
494 break;
497 case CVT_REMOVE_EMPTY_BLOCKS:
499 AsRemoveEmptyBlocks (FileBuffer, Filename);
500 break;
503 case CVT_REDUCE_TYPEDEFS:
505 AsReduceTypedefs (FileBuffer, "typedef union");
506 AsReduceTypedefs (FileBuffer, "typedef struct");
507 break;
510 case CVT_SPACES_TO_TABS4:
512 AsTabify4 (FileBuffer);
513 break;
516 case CVT_SPACES_TO_TABS8:
518 AsTabify8 (FileBuffer);
519 break;
522 default:
524 printf ("Unknown conversion subfunction opcode\n");
525 break;
529 if (ConversionTable->NewHeader)
531 AsReplaceHeader (FileBuffer, ConversionTable->NewHeader);
536 /******************************************************************************
538 * FUNCTION: AsProcessOneFile
540 * DESCRIPTION: Process one source file. The file is opened, read entirely
541 * into a buffer, converted, then written to a new file.
543 ******************************************************************************/
545 ACPI_NATIVE_INT
546 AsProcessOneFile (
547 ACPI_CONVERSION_TABLE *ConversionTable,
548 char *SourcePath,
549 char *TargetPath,
550 int MaxPathLength,
551 char *Filename,
552 ACPI_NATIVE_INT FileType)
554 char *Pathname;
555 char *OutPathname = NULL;
558 /* Allocate a file pathname buffer for both source and target */
560 Pathname = calloc (MaxPathLength + strlen (Filename) + 2, 1);
561 if (!Pathname)
563 printf ("Could not allocate buffer for file pathnames\n");
564 return -1;
567 Gbl_FileType = FileType;
569 /* Generate the source pathname and read the file */
571 if (SourcePath)
573 strcpy (Pathname, SourcePath);
574 strcat (Pathname, "/");
577 strcat (Pathname, Filename);
579 if (AsGetFile (Pathname, &Gbl_FileBuffer, &Gbl_FileSize))
581 return -1;
584 /* Process the file in the buffer */
586 Gbl_MadeChanges = FALSE;
587 AsConvertFile (ConversionTable, Gbl_FileBuffer, Pathname, FileType);
589 if (!(ConversionTable->Flags & FLG_NO_FILE_OUTPUT))
591 if (!(Gbl_Overwrite && !Gbl_MadeChanges))
593 /* Generate the target pathname and write the file */
595 OutPathname = calloc (MaxPathLength + strlen (Filename) + 2 + strlen (TargetPath), 1);
596 if (!OutPathname)
598 printf ("Could not allocate buffer for file pathnames\n");
599 return -1;
602 strcpy (OutPathname, TargetPath);
603 if (SourcePath)
605 strcat (OutPathname, "/");
606 strcat (OutPathname, Filename);
609 AsPutFile (OutPathname, Gbl_FileBuffer, ConversionTable->Flags);
613 free (Gbl_FileBuffer);
614 free (Pathname);
615 if (OutPathname)
617 free (OutPathname);
620 return 0;
624 /******************************************************************************
626 * FUNCTION: AsCheckForDirectory
628 * DESCRIPTION: Check if the current file is a valid directory. If not,
629 * construct the full pathname for the source and target paths.
630 * Checks for the dot and dot-dot files (they are ignored)
632 ******************************************************************************/
634 ACPI_NATIVE_INT
635 AsCheckForDirectory (
636 char *SourceDirPath,
637 char *TargetDirPath,
638 char *Filename,
639 char **SourcePath,
640 char **TargetPath)
642 char *SrcPath;
643 char *TgtPath;
646 if (!(strcmp (Filename, ".")) ||
647 !(strcmp (Filename, "..")))
649 return -1;
652 SrcPath = calloc (strlen (SourceDirPath) + strlen (Filename) + 2, 1);
653 if (!SrcPath)
655 printf ("Could not allocate buffer for directory source pathname\n");
656 return -1;
659 TgtPath = calloc (strlen (TargetDirPath) + strlen (Filename) + 2, 1);
660 if (!TgtPath)
662 printf ("Could not allocate buffer for directory target pathname\n");
663 free (SrcPath);
664 return -1;
667 strcpy (SrcPath, SourceDirPath);
668 strcat (SrcPath, "/");
669 strcat (SrcPath, Filename);
671 strcpy (TgtPath, TargetDirPath);
672 strcat (TgtPath, "/");
673 strcat (TgtPath, Filename);
675 *SourcePath = SrcPath;
676 *TargetPath = TgtPath;
677 return 0;
681 /******************************************************************************
683 * FUNCTION: AsGetFile
685 * DESCRIPTION: Open a file and read it entirely into a an allocated buffer
687 ******************************************************************************/
690 AsGetFile (
691 char *Filename,
692 char **FileBuffer,
693 UINT32 *FileSize)
696 int FileHandle;
697 UINT32 Size;
698 char *Buffer;
701 /* Binary mode leaves CR/LF pairs */
703 FileHandle = open (Filename, O_BINARY | O_RDONLY);
704 if (!FileHandle)
706 printf ("Could not open %s\n", Filename);
707 return -1;
710 if (fstat (FileHandle, &Gbl_StatBuf))
712 printf ("Could not get file status for %s\n", Filename);
713 goto ErrorExit;
717 * Create a buffer for the entire file
718 * Add plenty extra buffer to accomodate string replacements
720 Size = Gbl_StatBuf.st_size;
721 Buffer = calloc (Size * 2, 1);
722 if (!Buffer)
724 printf ("Could not allocate buffer of size %d\n", Size * 2);
725 goto ErrorExit;
728 /* Read the entire file */
730 Size = read (FileHandle, Buffer, Size);
731 if (Size == -1)
733 printf ("Could not read the input file %s\n", Filename);
734 goto ErrorExit;
737 Buffer [Size] = 0; /* Null terminate the buffer */
738 close (FileHandle);
740 /* Check for unix contamination */
742 if (!Gbl_IgnoreLoneLineFeeds && AsDetectLoneLineFeeds (Filename, Buffer))
744 return -1;
748 * Convert all CR/LF pairs to LF only. We do this locally so that
749 * this code is portable across operating systems.
751 AsConvertToLineFeeds (Buffer);
753 *FileBuffer = Buffer;
754 *FileSize = Size;
756 return 0;
759 ErrorExit:
761 close (FileHandle);
762 return -1;
766 /******************************************************************************
768 * FUNCTION: AsPutFile
770 * DESCRIPTION: Create a new output file and write the entire contents of the
771 * buffer to the new file. Buffer must be a zero terminated string
773 ******************************************************************************/
776 AsPutFile (
777 char *Pathname,
778 char *FileBuffer,
779 UINT32 SystemFlags)
781 UINT32 FileSize;
782 int DestHandle;
783 int OpenFlags;
786 /* Create the target file */
788 OpenFlags = O_TRUNC | O_CREAT | O_WRONLY | O_BINARY;
790 if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS))
792 /* Put back the CR before each LF */
794 AsInsertCarriageReturns (FileBuffer);
797 DestHandle = open (Pathname, OpenFlags, S_IREAD | S_IWRITE);
798 if (DestHandle == -1)
800 perror ("Could not create destination file");
801 printf ("Could not create destination file \"%s\"\n", Pathname);
802 return -1;
805 /* Write the buffer to the file */
807 FileSize = strlen (FileBuffer);
808 write (DestHandle, FileBuffer, FileSize);
810 close (DestHandle);
812 return 0;