1 /******************************************************************************
3 * Module Name: asmain - Main module for the acpi source processor utility
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
47 /* Local prototypes */
51 ACPI_CONVERSION_TABLE *ConversionTable,
54 UINT32 *SourceFileType);
67 UINT32 Gbl_MissingBraces = 0;
68 UINT32 Gbl_NonAnsiComments = 0;
70 UINT32 Gbl_WhiteLines = 0;
71 UINT32 Gbl_CommentLines = 0;
72 UINT32 Gbl_SourceLines = 0;
73 UINT32 Gbl_LongLines = 0;
74 UINT32 Gbl_TotalLines = 0;
75 UINT32 Gbl_TotalSize = 0;
76 UINT32 Gbl_HeaderLines = 0;
77 UINT32 Gbl_HeaderSize = 0;
78 void *Gbl_StructDefs = NULL;
80 struct stat Gbl_StatBuf;
84 BOOLEAN Gbl_VerboseMode = FALSE;
85 BOOLEAN Gbl_QuietMode = FALSE;
86 BOOLEAN Gbl_BatchMode = FALSE;
87 BOOLEAN Gbl_DebugStatementsMode = FALSE;
88 BOOLEAN Gbl_MadeChanges = FALSE;
89 BOOLEAN Gbl_Overwrite = FALSE;
90 BOOLEAN Gbl_WidenDeclarations = FALSE;
91 BOOLEAN Gbl_IgnoreLoneLineFeeds = FALSE;
92 BOOLEAN Gbl_HasLoneLineFeeds = FALSE;
93 BOOLEAN Gbl_Cleanup = FALSE;
94 BOOLEAN Gbl_IgnoreTranslationEscapes = FALSE;
96 #define AS_UTILITY_NAME "ACPI Source Code Conversion Utility"
97 #define AS_SUPPORTED_OPTIONS "cdhilqsuv^y"
100 /******************************************************************************
102 * FUNCTION: AsExaminePaths
104 * DESCRIPTION: Source and Target pathname verification and handling
106 ******************************************************************************/
110 ACPI_CONVERSION_TABLE *ConversionTable,
113 UINT32 *SourceFileType)
119 Status = stat (Source, &Gbl_StatBuf);
122 printf ("Source path \"%s\" does not exist\n", Source);
126 /* Return the filetype -- file or a directory */
129 if (Gbl_StatBuf.st_mode & S_IFDIR)
131 *SourceFileType = S_IFDIR;
135 * If we are in no-output mode or in batch mode, we are done
137 if ((ConversionTable->Flags & FLG_NO_FILE_OUTPUT) ||
143 if (!AcpiUtStricmp (Source, Target))
145 printf ("Target path is the same as the source path, overwrite?\n");
146 Response = getchar ();
155 Gbl_Overwrite = TRUE;
159 Status = stat (Target, &Gbl_StatBuf);
162 printf ("Target path already exists, overwrite?\n");
163 Response = getchar ();
178 /******************************************************************************
180 * FUNCTION: AsDisplayStats
182 * DESCRIPTION: Display global statistics gathered during translation
184 ******************************************************************************/
196 printf ("\nAcpiSrc statistics:\n\n");
197 printf ("%8u Files processed\n", Gbl_Files);
204 printf ("%8u Total bytes (%.1fK/file)\n",
205 Gbl_TotalSize, ((double) Gbl_TotalSize/Gbl_Files)/1024);
206 printf ("%8u Tabs found\n", Gbl_Tabs);
207 printf ("%8u Missing if/else/while braces\n", Gbl_MissingBraces);
208 printf ("%8u Non-ANSI // comments found\n", Gbl_NonAnsiComments);
209 printf ("%8u Total Lines\n", Gbl_TotalLines);
210 printf ("%8u Lines of code\n", Gbl_SourceLines);
211 printf ("%8u Lines of non-comment whitespace\n", Gbl_WhiteLines);
212 printf ("%8u Lines of comments\n", Gbl_CommentLines);
213 printf ("%8u Long lines found\n", Gbl_LongLines);
215 if (Gbl_WhiteLines > 0)
217 printf ("%8.1f Ratio of code to whitespace\n",
218 ((float) Gbl_SourceLines / (float) Gbl_WhiteLines));
221 if ((Gbl_CommentLines + Gbl_NonAnsiComments) > 0)
223 printf ("%8.1f Ratio of code to comments\n",
224 ((float) Gbl_SourceLines /
225 (float) (Gbl_CommentLines + Gbl_NonAnsiComments)));
233 printf (" %u%% code, %u%% comments, %u%% whitespace, %u%% headers\n",
234 (Gbl_SourceLines * 100) / Gbl_TotalLines,
235 (Gbl_CommentLines * 100) / Gbl_TotalLines,
236 (Gbl_WhiteLines * 100) / Gbl_TotalLines,
237 (Gbl_HeaderLines * 100) / Gbl_TotalLines);
242 /******************************************************************************
244 * FUNCTION: AsDisplayUsage
246 * DESCRIPTION: Usage message
248 ******************************************************************************/
255 ACPI_USAGE_HEADER ("acpisrc [-c|l|u] [-dsvy] <SourceDir> <DestinationDir>");
257 ACPI_OPTION ("-c", "Generate cleaned version of the source");
258 ACPI_OPTION ("-h", "Insert dual-license header into all modules");
259 ACPI_OPTION ("-i", "Cleanup macro indentation");
260 ACPI_OPTION ("-l", "Generate Linux version of the source");
261 ACPI_OPTION ("-u", "Generate Custom source translation");
263 ACPI_USAGE_TEXT ("\n");
264 ACPI_OPTION ("-d", "Leave debug statements in code");
265 ACPI_OPTION ("-s", "Generate source statistics only");
266 ACPI_OPTION ("-v", "Display version information");
267 ACPI_OPTION ("-vb", "Verbose mode");
268 ACPI_OPTION ("-y", "Suppress file overwrite prompts");
272 /******************************************************************************
276 * DESCRIPTION: C main function
278 ******************************************************************************/
280 int ACPI_SYSTEM_XFACE
286 ACPI_CONVERSION_TABLE *ConversionTable = NULL;
292 ACPI_DEBUG_INITIALIZE (); /* For debug version only */
294 printf (ACPI_COMMON_SIGNON (AS_UTILITY_NAME));
302 /* Command line options */
304 while ((j = AcpiGetopt (argc, argv, AS_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch(j)
308 /* Linux code generation */
310 printf ("Creating Linux source code\n");
311 ConversionTable = &LinuxConversionTable;
312 Gbl_WidenDeclarations = TRUE;
313 Gbl_IgnoreLoneLineFeeds = TRUE;
320 printf ("Code cleanup\n");
321 ConversionTable = &CleanupConversionTable;
327 /* Inject Dual-license header */
329 printf ("Inserting Dual-license header to all modules\n");
330 ConversionTable = &LicenseConversionTable;
335 /* Cleanup wrong indent result */
337 printf ("Cleaning up macro indentation\n");
338 ConversionTable = &IndentConversionTable;
339 Gbl_IgnoreLoneLineFeeds = TRUE;
340 Gbl_IgnoreTranslationEscapes = TRUE;
345 /* Statistics only */
351 /* custom conversion */
353 printf ("Custom source translation\n");
354 ConversionTable = &CustomConversionTable;
359 switch (AcpiGbl_Optarg[0])
361 case '^': /* -v: (Version): signon already emitted, just exit */
369 Gbl_VerboseMode = TRUE;
374 printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
384 Gbl_BatchMode = TRUE;
389 /* Leave debug statements in */
391 Gbl_DebugStatementsMode = TRUE;
398 Gbl_QuietMode = TRUE;
408 SourcePath = argv[AcpiGbl_Optind];
411 printf ("Missing source path\n");
416 TargetPath = argv[AcpiGbl_Optind+1];
418 if (!ConversionTable)
420 /* Just generate statistics. Ignore target path */
422 TargetPath = SourcePath;
424 printf ("Source code statistics only\n");
425 ConversionTable = &StatsConversionTable;
427 else if (!TargetPath)
429 TargetPath = SourcePath;
432 if (Gbl_DebugStatementsMode)
434 ConversionTable->SourceFunctions &= ~CVT_REMOVE_DEBUG_MACROS;
437 /* Check source and target paths and files */
439 if (AsExaminePaths (ConversionTable, SourcePath, TargetPath, &FileType))
444 /* Source/target can be either directories or a files */
446 if (FileType == S_IFDIR)
448 /* Process the directory tree */
450 AsProcessTree (ConversionTable, SourcePath, TargetPath);
454 /* Process a single file */
456 /* Differentiate between source and header files */
458 if (strstr (SourcePath, ".h"))
460 AsProcessOneFile (ConversionTable, NULL, TargetPath, 0,
461 SourcePath, FILE_TYPE_HEADER);
463 else if (strstr (SourcePath, ".c"))
465 AsProcessOneFile (ConversionTable, NULL, TargetPath, 0,
466 SourcePath, FILE_TYPE_SOURCE);
468 else if (strstr (SourcePath, ".patch"))
470 AsProcessOneFile (ConversionTable, NULL, TargetPath, 0,
471 SourcePath, FILE_TYPE_PATCH);
475 printf ("Unknown file type - %s\n", SourcePath);
479 /* Always display final summary and stats */