asmain.c revision 1.1.1.10       1 /******************************************************************************
      2  *
      3  * Module Name: asmain - Main module for the acpi source processor utility
      4  *
      5  *****************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2017, Intel Corp.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     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.
     25  *
     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.
     29  *
     30  * NO WARRANTY
     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.
     42  */
     43 
     44 #include "acpisrc.h"
     45 
     46 /* Local prototypes */
     47 
     48 int
     49 AsExaminePaths (
     50     ACPI_CONVERSION_TABLE   *ConversionTable,
     51     char                    *Source,
     52     char                    *Target,
     53     UINT32                  *SourceFileType);
     54 
     55 void
     56 AsDisplayStats (
     57     void);
     58 
     59 void
     60 AsDisplayUsage (
     61     void);
     62 
     63 /* Globals */
     64 
     65 UINT32                  Gbl_Tabs = 0;
     66 UINT32                  Gbl_MissingBraces = 0;
     67 UINT32                  Gbl_NonAnsiComments = 0;
     68 UINT32                  Gbl_Files = 0;
     69 UINT32                  Gbl_WhiteLines = 0;
     70 UINT32                  Gbl_CommentLines = 0;
     71 UINT32                  Gbl_SourceLines = 0;
     72 UINT32                  Gbl_LongLines = 0;
     73 UINT32                  Gbl_TotalLines = 0;
     74 UINT32                  Gbl_TotalSize = 0;
     75 UINT32                  Gbl_HeaderLines = 0;
     76 UINT32                  Gbl_HeaderSize = 0;
     77 void                    *Gbl_StructDefs = NULL;
     78 
     79 struct stat             Gbl_StatBuf;
     80 char                    *Gbl_FileBuffer;
     81 UINT32                  Gbl_FileSize;
     82 UINT32                  Gbl_FileType;
     83 BOOLEAN                 Gbl_CheckAscii = FALSE;
     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;
     95 
     96 #define AS_UTILITY_NAME             "ACPI Source Code Conversion Utility"
     97 #define AS_SUPPORTED_OPTIONS        "acdhilqsuv^y"
     98 
     99 
    100 /******************************************************************************
    101  *
    102  * FUNCTION:    AsExaminePaths
    103  *
    104  * DESCRIPTION: Source and Target pathname verification and handling
    105  *
    106  ******************************************************************************/
    107 
    108 int
    109 AsExaminePaths (
    110     ACPI_CONVERSION_TABLE   *ConversionTable,
    111     char                    *Source,
    112     char                    *Target,
    113     UINT32                  *SourceFileType)
    114 {
    115     int                     Status;
    116     int                     Response;
    117 
    118 
    119     Status = stat (Source, &Gbl_StatBuf);
    120     if (Status)
    121     {
    122         printf ("Source path \"%s\" does not exist\n", Source);
    123         return (-1);
    124     }
    125 
    126     /* Return the filetype -- file or a directory */
    127 
    128     *SourceFileType = 0;
    129     if (Gbl_StatBuf.st_mode & S_IFDIR)
    130     {
    131         *SourceFileType = S_IFDIR;
    132     }
    133 
    134     /*
    135      * If we are in no-output mode or in batch mode, we are done
    136      */
    137     if ((ConversionTable->Flags & FLG_NO_FILE_OUTPUT) ||
    138         (Gbl_BatchMode))
    139     {
    140         return (0);
    141     }
    142 
    143     if (!AcpiUtStricmp (Source, Target))
    144     {
    145         printf ("Target path is the same as the source path, overwrite?\n");
    146         Response = getchar ();
    147 
    148         /* Check response */
    149 
    150         if (Response != 'y')
    151         {
    152             return (-1);
    153         }
    154 
    155         Gbl_Overwrite = TRUE;
    156     }
    157     else
    158     {
    159         Status = stat (Target, &Gbl_StatBuf);
    160         if (!Status)
    161         {
    162             printf ("Target path already exists, overwrite?\n");
    163             Response = getchar ();
    164 
    165             /* Check response */
    166 
    167             if (Response != 'y')
    168             {
    169                 return (-1);
    170             }
    171         }
    172     }
    173 
    174     return (0);
    175 }
    176 
    177 
    178 /******************************************************************************
    179  *
    180  * FUNCTION:    AsDisplayStats
    181  *
    182  * DESCRIPTION: Display global statistics gathered during translation
    183  *
    184  ******************************************************************************/
    185 
    186 void
    187 AsDisplayStats (
    188     void)
    189 {
    190 
    191     if (Gbl_QuietMode)
    192     {
    193         return;
    194     }
    195 
    196     printf ("\nAcpiSrc statistics:\n\n");
    197     printf ("%8u Files processed\n", Gbl_Files);
    198 
    199     if (!Gbl_Files)
    200     {
    201         return;
    202     }
    203 
    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);
    214 
    215     if (Gbl_WhiteLines > 0)
    216     {
    217         printf ("%8.1f Ratio of code to whitespace\n",
    218             ((float) Gbl_SourceLines / (float) Gbl_WhiteLines));
    219     }
    220 
    221     if ((Gbl_CommentLines + Gbl_NonAnsiComments) > 0)
    222     {
    223         printf ("%8.1f Ratio of code to comments\n",
    224             ((float) Gbl_SourceLines /
    225             (float) (Gbl_CommentLines + Gbl_NonAnsiComments)));
    226     }
    227 
    228     if (!Gbl_TotalLines)
    229     {
    230         return;
    231     }
    232 
    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);
    238     return;
    239 }
    240 
    241 
    242 /******************************************************************************
    243  *
    244  * FUNCTION:    AsDisplayUsage
    245  *
    246  * DESCRIPTION: Usage message
    247  *
    248  ******************************************************************************/
    249 
    250 void
    251 AsDisplayUsage (
    252     void)
    253 {
    254 
    255     ACPI_USAGE_HEADER ("acpisrc [-c|l|u] [-dsvy] <SourceDir> <DestinationDir>");
    256 
    257     ACPI_OPTION ("-a <file>",   "Check entire file for non-printable characters");
    258     ACPI_OPTION ("-c",          "Generate cleaned version of the source");
    259     ACPI_OPTION ("-h",          "Insert dual-license header into all modules");
    260     ACPI_OPTION ("-i",          "Cleanup macro indentation");
    261     ACPI_OPTION ("-l",          "Generate Linux version of the source");
    262     ACPI_OPTION ("-u",          "Generate Custom source translation");
    263 
    264     ACPI_USAGE_TEXT ("\n");
    265     ACPI_OPTION ("-d",          "Leave debug statements in code");
    266     ACPI_OPTION ("-s",          "Generate source statistics only");
    267     ACPI_OPTION ("-v",          "Display version information");
    268     ACPI_OPTION ("-vb",         "Verbose mode");
    269     ACPI_OPTION ("-y",          "Suppress file overwrite prompts");
    270 }
    271 
    272 
    273 /******************************************************************************
    274  *
    275  * FUNCTION:    main
    276  *
    277  * DESCRIPTION: C main function
    278  *
    279  ******************************************************************************/
    280 
    281 int ACPI_SYSTEM_XFACE
    282 main (
    283     int                     argc,
    284     char                    *argv[])
    285 {
    286     int                     j;
    287     ACPI_CONVERSION_TABLE   *ConversionTable = NULL;
    288     char                    *SourcePath;
    289     char                    *TargetPath;
    290     UINT32                  FileType;
    291 
    292 
    293     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
    294     AcpiOsInitialize ();
    295     printf (ACPI_COMMON_SIGNON (AS_UTILITY_NAME));
    296 
    297     if (argc < 2)
    298     {
    299         AsDisplayUsage ();
    300         return (0);
    301     }
    302 
    303     /* Command line options */
    304 
    305     while ((j = AcpiGetopt (argc, argv, AS_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch(j)
    306     {
    307     case 'l':
    308 
    309         /* Linux code generation */
    310 
    311         printf ("Creating Linux source code\n");
    312         ConversionTable = &LinuxConversionTable;
    313         Gbl_WidenDeclarations = TRUE;
    314         Gbl_IgnoreLoneLineFeeds = TRUE;
    315         break;
    316 
    317     case 'c':
    318 
    319         /* Cleanup code */
    320 
    321         printf ("Code cleanup\n");
    322         ConversionTable = &CleanupConversionTable;
    323         Gbl_Cleanup = TRUE;
    324         break;
    325 
    326     case 'h':
    327 
    328         /* Inject Dual-license header */
    329 
    330         printf ("Inserting Dual-license header to all modules\n");
    331         ConversionTable = &LicenseConversionTable;
    332         break;
    333 
    334     case 'i':
    335 
    336         /* Cleanup wrong indent result */
    337 
    338         printf ("Cleaning up macro indentation\n");
    339         ConversionTable = &IndentConversionTable;
    340         Gbl_IgnoreLoneLineFeeds = TRUE;
    341         Gbl_IgnoreTranslationEscapes = TRUE;
    342         break;
    343 
    344     case 's':
    345 
    346         /* Statistics only */
    347 
    348         break;
    349 
    350     case 'u':
    351 
    352         /* custom conversion  */
    353 
    354         printf ("Custom source translation\n");
    355         ConversionTable = &CustomConversionTable;
    356         break;
    357 
    358     case 'v':
    359 
    360         switch (AcpiGbl_Optarg[0])
    361         {
    362         case '^':  /* -v: (Version): signon already emitted, just exit */
    363 
    364             exit (0);
    365 
    366         case 'b':
    367 
    368             /* Verbose mode */
    369 
    370             Gbl_VerboseMode = TRUE;
    371             break;
    372 
    373         default:
    374 
    375             printf ("Unknown option: -v%s\n", AcpiGbl_Optarg);
    376             return (-1);
    377         }
    378 
    379         break;
    380 
    381     case 'y':
    382 
    383         /* Batch mode */
    384 
    385         Gbl_BatchMode = TRUE;
    386         break;
    387 
    388     case 'd':
    389 
    390         /* Leave debug statements in */
    391 
    392         Gbl_DebugStatementsMode = TRUE;
    393         break;
    394 
    395     case 'q':
    396 
    397         /* Quiet mode */
    398 
    399         Gbl_QuietMode = TRUE;
    400         break;
    401 
    402     case 'a':
    403 
    404         Gbl_CheckAscii = TRUE;
    405         break;
    406 
    407     default:
    408 
    409         AsDisplayUsage ();
    410         return (-1);
    411     }
    412 
    413 
    414     SourcePath = argv[AcpiGbl_Optind];
    415     if (!SourcePath)
    416     {
    417         printf ("Missing source path\n");
    418         AsDisplayUsage ();
    419         return (-1);
    420     }
    421 
    422     /* This option checks the entire file for printable ascii chars */
    423 
    424     if (Gbl_CheckAscii)
    425     {
    426         AsProcessOneFile (NULL, NULL, NULL, 0, SourcePath, FILE_TYPE_SOURCE);
    427         return (0);
    428     }
    429 
    430     TargetPath = argv[AcpiGbl_Optind+1];
    431 
    432     if (!ConversionTable)
    433     {
    434         /* Just generate statistics. Ignore target path */
    435 
    436         TargetPath = SourcePath;
    437 
    438         printf ("Source code statistics only\n");
    439         ConversionTable = &StatsConversionTable;
    440     }
    441     else if (!TargetPath)
    442     {
    443         TargetPath = SourcePath;
    444     }
    445 
    446     if (Gbl_DebugStatementsMode)
    447     {
    448         ConversionTable->SourceFunctions &= ~CVT_REMOVE_DEBUG_MACROS;
    449     }
    450 
    451     /* Check source and target paths and files */
    452 
    453     if (AsExaminePaths (ConversionTable, SourcePath, TargetPath, &FileType))
    454     {
    455         return (-1);
    456     }
    457 
    458     /* Source/target can be either directories or a files */
    459 
    460     if (FileType == S_IFDIR)
    461     {
    462         /* Process the directory tree */
    463 
    464         AsProcessTree (ConversionTable, SourcePath, TargetPath);
    465     }
    466     else
    467     {
    468         if (Gbl_CheckAscii)
    469         {
    470             AsProcessOneFile (NULL, NULL, NULL, 0,
    471                 SourcePath, FILE_TYPE_SOURCE);
    472             return (0);
    473         }
    474 
    475         /* Process a single file */
    476 
    477         /* Differentiate between source and header files */
    478 
    479         if (strstr (SourcePath, ".h"))
    480         {
    481             AsProcessOneFile (ConversionTable, NULL, TargetPath, 0,
    482                 SourcePath, FILE_TYPE_HEADER);
    483         }
    484         else if (strstr (SourcePath, ".c"))
    485         {
    486             AsProcessOneFile (ConversionTable, NULL, TargetPath, 0,
    487                 SourcePath, FILE_TYPE_SOURCE);
    488         }
    489         else if (strstr (SourcePath, ".patch"))
    490         {
    491             AsProcessOneFile (ConversionTable, NULL, TargetPath, 0,
    492                 SourcePath, FILE_TYPE_PATCH);
    493         }
    494         else
    495         {
    496             printf ("Unknown file type - %s\n", SourcePath);
    497         }
    498     }
    499 
    500     /* Always display final summary and stats */
    501 
    502     AsDisplayStats ();
    503     return (0);
    504 }
    505