1 /******************************************************************************
3 * Module Name: asmain - Main module for the acpi source processor utility
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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.
48 /* Local prototypes */
57 ACPI_CONVERSION_TABLE
*ConversionTable
,
60 UINT32
*SourceFileType
);
73 UINT32 Gbl_MissingBraces
= 0;
74 UINT32 Gbl_NonAnsiComments
= 0;
76 UINT32 Gbl_WhiteLines
= 0;
77 UINT32 Gbl_CommentLines
= 0;
78 UINT32 Gbl_SourceLines
= 0;
79 UINT32 Gbl_LongLines
= 0;
80 UINT32 Gbl_TotalLines
= 0;
81 UINT32 Gbl_TotalSize
= 0;
82 UINT32 Gbl_HeaderLines
= 0;
83 UINT32 Gbl_HeaderSize
= 0;
84 void *Gbl_StructDefs
= NULL
;
86 struct stat Gbl_StatBuf
;
90 BOOLEAN Gbl_VerboseMode
= FALSE
;
91 BOOLEAN Gbl_QuietMode
= FALSE
;
92 BOOLEAN Gbl_BatchMode
= FALSE
;
93 BOOLEAN Gbl_DebugStatementsMode
= FALSE
;
94 BOOLEAN Gbl_MadeChanges
= FALSE
;
95 BOOLEAN Gbl_Overwrite
= FALSE
;
96 BOOLEAN Gbl_WidenDeclarations
= FALSE
;
97 BOOLEAN Gbl_IgnoreLoneLineFeeds
= FALSE
;
98 BOOLEAN Gbl_HasLoneLineFeeds
= FALSE
;
99 BOOLEAN Gbl_Cleanup
= FALSE
;
100 BOOLEAN Gbl_IgnoreTranslationEscapes
= FALSE
;
102 #define AS_UTILITY_NAME "ACPI Source Code Conversion Utility"
103 #define AS_SUPPORTED_OPTIONS "cdhilqsuv^y"
106 /******************************************************************************
108 * FUNCTION: AsStricmp
110 * DESCRIPTION: Implementation of the non-ANSI stricmp function (compare
111 * strings with no case sensitivity)
113 ******************************************************************************/
126 c1
= tolower ((int) *String1
);
127 c2
= tolower ((int) *String2
);
132 while ((c1
== c2
) && (c1
));
138 /******************************************************************************
140 * FUNCTION: AsExaminePaths
142 * DESCRIPTION: Source and Target pathname verification and handling
144 ******************************************************************************/
148 ACPI_CONVERSION_TABLE
*ConversionTable
,
151 UINT32
*SourceFileType
)
157 Status
= stat (Source
, &Gbl_StatBuf
);
160 printf ("Source path \"%s\" does not exist\n", Source
);
164 /* Return the filetype -- file or a directory */
167 if (Gbl_StatBuf
.st_mode
& S_IFDIR
)
169 *SourceFileType
= S_IFDIR
;
173 * If we are in no-output mode or in batch mode, we are done
175 if ((ConversionTable
->Flags
& FLG_NO_FILE_OUTPUT
) ||
181 if (!AsStricmp (Source
, Target
))
183 printf ("Target path is the same as the source path, overwrite?\n");
184 Response
= getchar ();
193 Gbl_Overwrite
= TRUE
;
197 Status
= stat (Target
, &Gbl_StatBuf
);
200 printf ("Target path already exists, overwrite?\n");
201 Response
= getchar ();
216 /******************************************************************************
218 * FUNCTION: AsDisplayStats
220 * DESCRIPTION: Display global statistics gathered during translation
222 ******************************************************************************/
234 printf ("\nAcpiSrc statistics:\n\n");
235 printf ("%8u Files processed\n", Gbl_Files
);
242 printf ("%8u Total bytes (%.1fK/file)\n",
243 Gbl_TotalSize
, ((double) Gbl_TotalSize
/Gbl_Files
)/1024);
244 printf ("%8u Tabs found\n", Gbl_Tabs
);
245 printf ("%8u Missing if/else braces\n", Gbl_MissingBraces
);
246 printf ("%8u Non-ANSI comments found\n", Gbl_NonAnsiComments
);
247 printf ("%8u Total Lines\n", Gbl_TotalLines
);
248 printf ("%8u Lines of code\n", Gbl_SourceLines
);
249 printf ("%8u Lines of non-comment whitespace\n", Gbl_WhiteLines
);
250 printf ("%8u Lines of comments\n", Gbl_CommentLines
);
251 printf ("%8u Long lines found\n", Gbl_LongLines
);
253 if (Gbl_WhiteLines
> 0)
255 printf ("%8.1f Ratio of code to whitespace\n",
256 ((float) Gbl_SourceLines
/ (float) Gbl_WhiteLines
));
259 if ((Gbl_CommentLines
+ Gbl_NonAnsiComments
) > 0)
261 printf ("%8.1f Ratio of code to comments\n",
262 ((float) Gbl_SourceLines
/ (float) (Gbl_CommentLines
+ Gbl_NonAnsiComments
)));
270 printf (" %u%% code, %u%% comments, %u%% whitespace, %u%% headers\n",
271 (Gbl_SourceLines
* 100) / Gbl_TotalLines
,
272 (Gbl_CommentLines
* 100) / Gbl_TotalLines
,
273 (Gbl_WhiteLines
* 100) / Gbl_TotalLines
,
274 (Gbl_HeaderLines
* 100) / Gbl_TotalLines
);
279 /******************************************************************************
281 * FUNCTION: AsDisplayUsage
283 * DESCRIPTION: Usage message
285 ******************************************************************************/
292 ACPI_USAGE_HEADER ("acpisrc [-c|l|u] [-dsvy] <SourceDir> <DestinationDir>");
294 ACPI_OPTION ("-c", "Generate cleaned version of the source");
295 ACPI_OPTION ("-h", "Insert dual-license header into all modules");
296 ACPI_OPTION ("-i", "Cleanup macro indentation");
297 ACPI_OPTION ("-l", "Generate Linux version of the source");
298 ACPI_OPTION ("-u", "Generate Custom source translation");
301 ACPI_OPTION ("-d", "Leave debug statements in code");
302 ACPI_OPTION ("-s", "Generate source statistics only");
303 ACPI_OPTION ("-v", "Display version information");
304 ACPI_OPTION ("-vb", "Verbose mode");
305 ACPI_OPTION ("-y", "Suppress file overwrite prompts");
309 /******************************************************************************
313 * DESCRIPTION: C main function
315 ******************************************************************************/
317 int ACPI_SYSTEM_XFACE
323 ACPI_CONVERSION_TABLE
*ConversionTable
= NULL
;
329 ACPI_DEBUG_INITIALIZE (); /* For debug version only */
330 printf (ACPI_COMMON_SIGNON (AS_UTILITY_NAME
));
338 /* Command line options */
340 while ((j
= AcpiGetopt (argc
, argv
, AS_SUPPORTED_OPTIONS
)) != EOF
) switch(j
)
344 /* Linux code generation */
346 printf ("Creating Linux source code\n");
347 ConversionTable
= &LinuxConversionTable
;
348 Gbl_WidenDeclarations
= TRUE
;
349 Gbl_IgnoreLoneLineFeeds
= TRUE
;
356 printf ("Code cleanup\n");
357 ConversionTable
= &CleanupConversionTable
;
363 /* Inject Dual-license header */
365 printf ("Inserting Dual-license header to all modules\n");
366 ConversionTable
= &LicenseConversionTable
;
371 /* Cleanup wrong indent result */
373 printf ("Cleaning up macro indentation\n");
374 ConversionTable
= &IndentConversionTable
;
375 Gbl_IgnoreLoneLineFeeds
= TRUE
;
376 Gbl_IgnoreTranslationEscapes
= TRUE
;
381 /* Statistics only */
387 /* custom conversion */
389 printf ("Custom source translation\n");
390 ConversionTable
= &CustomConversionTable
;
395 switch (AcpiGbl_Optarg
[0])
397 case '^': /* -v: (Version): signon already emitted, just exit */
405 Gbl_VerboseMode
= TRUE
;
410 printf ("Unknown option: -v%s\n", AcpiGbl_Optarg
);
420 Gbl_BatchMode
= TRUE
;
425 /* Leave debug statements in */
427 Gbl_DebugStatementsMode
= TRUE
;
434 Gbl_QuietMode
= TRUE
;
444 SourcePath
= argv
[AcpiGbl_Optind
];
447 printf ("Missing source path\n");
452 TargetPath
= argv
[AcpiGbl_Optind
+1];
454 if (!ConversionTable
)
456 /* Just generate statistics. Ignore target path */
458 TargetPath
= SourcePath
;
460 printf ("Source code statistics only\n");
461 ConversionTable
= &StatsConversionTable
;
463 else if (!TargetPath
)
465 TargetPath
= SourcePath
;
468 if (Gbl_DebugStatementsMode
)
470 ConversionTable
->SourceFunctions
&= ~CVT_REMOVE_DEBUG_MACROS
;
473 /* Check source and target paths and files */
475 if (AsExaminePaths (ConversionTable
, SourcePath
, TargetPath
, &FileType
))
480 /* Source/target can be either directories or a files */
482 if (FileType
== S_IFDIR
)
484 /* Process the directory tree */
486 AsProcessTree (ConversionTable
, SourcePath
, TargetPath
);
490 /* Process a single file */
492 /* Differentiate between source and header files */
494 if (strstr (SourcePath
, ".h"))
496 AsProcessOneFile (ConversionTable
, NULL
, TargetPath
, 0, SourcePath
, FILE_TYPE_HEADER
);
500 AsProcessOneFile (ConversionTable
, NULL
, TargetPath
, 0, SourcePath
, FILE_TYPE_SOURCE
);
504 /* Always display final summary and stats */