------------------------------------------------------------------------------
--                                                                          --
--                          GNATCHECK COMPONENTS                            --
--                                                                          --
--  A S I S _ U L . E N V I R O N M E N T . S C A N _ P A R A M E T E R S   --
--                                                                          --
--            (adapted for gnatcheck from ASIS Utility Library)             --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2004-2011, AdaCore                     --
--                                                                          --
-- GNATCHECK  is  free  software;  you can redistribute it and/or modify it --
-- under terms of the  GNU  General Public License as published by the Free --
-- Software Foundation;  either version 2, or ( at your option)  any  later --
-- version.  GNATCHECK  is  distributed in the hope that it will be useful, --
-- but  WITHOUT  ANY  WARRANTY;   without  even  the  implied  warranty  of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details.  You should have received a copy of the --
-- GNU  General Public License distributed with GNAT; see file  COPYING. If --
-- not,  write to the  Free Software Foundation,  51 Franklin Street, Fifth --
-- Floor, Boston, MA 02110-1301, USA.                                       --
--                                                                          --
-- GNATCHECK is maintained by AdaCore (http://www.adacore.com).             --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Strings.Fixed;

with ASIS_UL.Options;            use ASIS_UL.Options;
with ASIS_UL.Source_Table;

with Gnatcheck.Categories;
with Gnatcheck.Diagnoses_Old;
with Gnatcheck.Options;          use Gnatcheck.Options;
with Gnatcheck.Output;           use Gnatcheck.Output;
with Gnatcheck.Rules.Rule_Table; use Gnatcheck.Rules.Rule_Table;

separate (ASIS_UL.Environment)
procedure Scan_Parameters is
   Success : Boolean;

   function Get_Coding_Standard_Kind
     (Par  : String)
      return Coding_Standard_Kinds;
   --  Computes the coding standard kind from its parameter. If the parameter
   --  value does not correspond to any coding standard kinds, generates the
   --  corresponding diagnosis and daises Fatal_Error

   function Get_Coding_Standard_Kind
     (Par  : String)
      return Coding_Standard_Kinds
   is
      Result : Coding_Standard_Kinds;
   begin
      Result := Coding_Standard_Kinds'Value (Par);

      if Result = Not_A_Coding_Standard then
         raise Constraint_Error;
      end if;

      return Result;
   exception
      when Constraint_Error =>
         Error ("unknown coding standard kind specified");
         raise Fatal_Error;
   end Get_Coding_Standard_Kind;

   procedure Process_Sections;
   --  Processes option sections. At the moment we have two such sections -
   --  'cargs' for compiler arguments and 'rules' for switching rules ON and
   --  OFF and changing the rule status to feature detection.

   procedure Process_Sections is
   begin

      Process_cargs_Section;

      --  Processing the 'rules' section
      Goto_Section ("rules");

      loop
         case GNAT.Command_Line.Getopt ("* from=") is
         --  We do not want to depend on the set of the currently implemented
         --  rules
            when ASCII.NUL =>
               exit;
            when 'f' =>
               Process_Rule_File (Parameter);

               if not More_Then_One_Rule_File_Set then
                  Rule_File_Name              := new String'(Parameter);
                  More_Then_One_Rule_File_Set := True;
               else
                  Free (Rule_File_Name);
               end if;

            when others =>
               Process_Rule_Option
                 (Ada.Strings.Fixed.Trim (Full_Switch, Ada.Strings.Both));
               --  We use the call to Trim here because there can be a rule
               --  option in quotation marks
               Individual_Rules_Set := True;
         end case;
      end loop;

   end Process_Sections;
begin

   Initialize_Option_Scan
     (Stop_At_First_Non_Switch => False,
      Section_Delimiters       => "cargs rules");

   loop
      case GNAT.Command_Line.Getopt
        ("v q t h hc? hx s? l m? files= a " &
         "d? "                              &   --  debug mode/options

         "log "                             &
         "-include-file= "                  &   --  specify user-defined
                                                --  part of the report file
         "-old-report-format "              &   --  generate report file
                                                --  using the old format
         "-no-column "                      &
         "-exemption "                      &   -- rule exemption ON
         "o= "                              &
         "-write-rules= "                   &   --  template rule file name
--       "-dump-code-standard=  "           &   --  coding standard type
         "gnat05")                              --  Ada 2005 mode
      is

         when ASCII.NUL =>
            exit  when not More_Arguments;
         when 'a' =>

            if Full_Switch = "a" then
               Process_RTL_Units := True;
            end if;

         when 'd' =>

            if Full_Switch = "d" then
               Set_Debug_Options (Parameter);
            end if;

         when 'f' =>

            if Full_Switch = "files" then
               Read_Args_From_File (Parameter, Store_With_No_Check => True);
            end if;

         when 'h' =>
            if Full_Switch = "h" then
               Generate_Rules_Help := True;
            elsif Full_Switch = "hc" then
               Generate_Category_Help := True;
               Gnatcheck.Categories.Process_Category_Help_Parameter
                 (Parameter, Success);

               if not Success then
                  raise Parameter_Error;
               end if;

            elsif Full_Switch = "hx" then
               Generate_XML_Help := True;
            end if;

         when 'g' =>

            if Full_Switch = "gnat05" then
               ASIS_UL.Options.ASIS_2005_Mode := True;
            end if;

         when 'l' =>

            if Full_Switch  = "l" then
               Gnatcheck.Options.Full_Source_Locations := True;
            elsif Full_Switch  = "log" then
               Log_Mode := True;
               Set_Log_File;
            end if;

         when 'm' =>

            begin
               Gnatcheck.Options.Max_Diagnoses := Natural'Value (Parameter);

               if Gnatcheck.Options.Max_Diagnoses > 1000 then
                  Error ("parameter of '-m' option too big, " &
                         "max allowed is 1000");
                  raise Parameter_Error;
               end if;

            exception
               when Constraint_Error =>
               Error ("Wrong parameter of '-m' option: " & Parameter);
               raise Parameter_Error;
            end;

         when 'o' =>

            if Full_Switch = "o" then
               Set_Report_File_Name (Parameter);
            end if;

         when 'q' =>
            Quiet_Mode := True;

         when 's' =>
            Gnatcheck.Diagnoses_Old.Process_Report_File_Format_Parameter
              (Parameter, Success);

            if not Success then
               raise Parameter_Error;
            end if;

         when 't' =>
            Compute_Timing := True;

         when 'v' =>
            Verbose_Mode := True;

            Print_Version_Info (2004);

         when '-' =>

            if Full_Switch = "-dump-code-standard" then
               Coding_Standard_Kind := Get_Coding_Standard_Kind (Parameter);
            elsif Full_Switch = "-no-column" then
               Gnatcheck.Options.No_Column_Num_In_Diagnoses := True;
            elsif Full_Switch = "-exemption" then
               Gnatcheck.Options.Print_Exemption_Section := True;
            elsif Full_Switch = "-include-file" then
               Gnatcheck.Diagnoses_Old.Process_User_Filename (Parameter);
            elsif Full_Switch = "-old-report-format" then
               Gnatcheck.Options.Qualification_Report := False;
            elsif Full_Switch = "-write-rules" then
               Generate_Coding_Standard := True;
               Set_Coding_Standard_File_Name (Parameter);
            end if;

         when others =>
            raise Parameter_Error;
      end case;
   end loop;

   Process_Sections;

exception
   when GNAT.Command_Line.Invalid_Switch =>
      Error ("invalid switch : " & Full_Switch);
      Brief_Help;
      raise Parameter_Error;

   when GNAT.Command_Line.Invalid_Parameter =>
      Error ("missing parameter for: -" & Full_Switch);
      raise Parameter_Error;

end Scan_Parameters;
