OSDN Git Service

2007-08-30 Vincent Celier <celier@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / switch.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                               S W I T C H                                --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Osint;  use Osint;
28 with Output; use Output;
29
30 package body Switch is
31
32    ----------------
33    -- Bad_Switch --
34    ----------------
35
36    procedure Bad_Switch (Switch : Character) is
37    begin
38       Osint.Fail ("invalid switch: ", (1 => Switch));
39    end Bad_Switch;
40
41    procedure Bad_Switch (Switch : String) is
42    begin
43       Osint.Fail ("invalid switch: ", Switch);
44    end Bad_Switch;
45
46    ----------------------------
47    -- Check_Version_And_Help --
48    ----------------------------
49
50    procedure Check_Version_And_Help
51      (Tool_Name      : String;
52       Initial_Year   : String;
53       Usage          : Procedure_Ptr;
54       Version_String : String := Gnatvsn.Gnat_Version_String)
55    is
56       Version_Switch_Present : Boolean := False;
57       Help_Switch_Present    : Boolean := False;
58       Next_Arg               : Natural;
59
60    begin
61       --  First check for --version or --help
62
63       Next_Arg := 1;
64       while Next_Arg < Arg_Count loop
65          declare
66             Next_Argv : String (1 .. Len_Arg (Next_Arg));
67          begin
68             Fill_Arg (Next_Argv'Address, Next_Arg);
69
70             if Next_Argv = Version_Switch then
71                Version_Switch_Present := True;
72
73             elsif Next_Argv = Help_Switch then
74                Help_Switch_Present := True;
75             end if;
76
77             Next_Arg := Next_Arg + 1;
78          end;
79       end loop;
80
81       --  If --version was used, display version and exit
82
83       if Version_Switch_Present then
84          Set_Standard_Output;
85          Display_Version (Tool_Name, Initial_Year, Version_String);
86          Write_Str (Gnatvsn.Gnat_Free_Software);
87          Write_Eol;
88          Write_Eol;
89          Exit_Program (E_Success);
90       end if;
91
92       --  If --help was used, display help and exit
93
94       if Help_Switch_Present then
95          Set_Standard_Output;
96          Usage.all;
97          Write_Eol;
98          Write_Line ("Report bugs to report@adacore.com");
99          Exit_Program (E_Success);
100       end if;
101    end Check_Version_And_Help;
102
103    ---------------------
104    -- Display_Version --
105    ---------------------
106
107    procedure Display_Version
108      (Tool_Name      : String;
109       Initial_Year   : String;
110       Version_String : String := Gnatvsn.Gnat_Version_String)
111    is
112    begin
113       Write_Str (Tool_Name);
114       Write_Char (' ');
115       Write_Str (Version_String);
116       Write_Eol;
117
118       Write_Str ("Copyright (C) ");
119       Write_Str (Initial_Year);
120       Write_Char ('-');
121       Write_Str (Gnatvsn.Current_Year);
122       Write_Str (", ");
123       Write_Str (Gnatvsn.Copyright_Holder);
124       Write_Eol;
125    end Display_Version;
126
127    -------------------------
128    -- Is_Front_End_Switch --
129    -------------------------
130
131    function Is_Front_End_Switch (Switch_Chars : String) return Boolean is
132       Ptr : constant Positive := Switch_Chars'First;
133    begin
134       return Is_Switch (Switch_Chars)
135         and then
136           (Switch_Chars (Ptr + 1) = 'I'
137             or else (Switch_Chars'Length >= 5
138                       and then Switch_Chars (Ptr + 1 .. Ptr + 4) = "gnat")
139             or else (Switch_Chars'Length >= 5
140                       and then Switch_Chars (Ptr + 2 .. Ptr + 4) = "RTS"));
141    end Is_Front_End_Switch;
142
143    ---------------
144    -- Is_Switch --
145    ---------------
146
147    function Is_Switch (Switch_Chars : String) return Boolean is
148    begin
149       return Switch_Chars'Length > 1
150         and then Switch_Chars (Switch_Chars'First) = '-';
151    end Is_Switch;
152
153    --------------
154    -- Scan_Nat --
155    --------------
156
157    procedure Scan_Nat
158      (Switch_Chars : String;
159       Max          : Integer;
160       Ptr          : in out Integer;
161       Result       : out Nat;
162       Switch       : Character)
163    is
164    begin
165       Result := 0;
166
167       if Ptr > Max or else Switch_Chars (Ptr) not in '0' .. '9' then
168          Osint.Fail ("missing numeric value for switch: ", (1 => Switch));
169
170       else
171          while Ptr <= Max and then Switch_Chars (Ptr) in '0' .. '9' loop
172             Result := Result * 10 +
173               Character'Pos (Switch_Chars (Ptr)) - Character'Pos ('0');
174             Ptr := Ptr + 1;
175
176             if Result > Switch_Max_Value then
177                Osint.Fail
178                  ("numeric value out of range for switch: ", (1 => Switch));
179             end if;
180          end loop;
181       end if;
182    end Scan_Nat;
183
184    --------------
185    -- Scan_Pos --
186    --------------
187
188    procedure Scan_Pos
189      (Switch_Chars : String;
190       Max          : Integer;
191       Ptr          : in out Integer;
192       Result       : out Pos;
193       Switch       : Character)
194    is
195       Temp : Nat;
196
197    begin
198       Scan_Nat (Switch_Chars, Max, Ptr, Temp, Switch);
199
200       if Temp = 0 then
201          Osint.Fail ("numeric value out of range for switch: ", (1 => Switch));
202       end if;
203
204       Result := Temp;
205    end Scan_Pos;
206
207 end Switch;