1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             G N A T X R E F                              --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--         Copyright (C) 1998-2004 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,  59 Temple Place - Suite 330,  Boston, --
20-- MA 02111-1307, 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
27with Xr_Tabls; use Xr_Tabls;
28with Xref_Lib; use Xref_Lib;
29with Osint;    use Osint;
30with Types;    use Types;
31
32with Gnatvsn;
33with Opt;
34
35with Ada.Strings.Fixed; use Ada.Strings.Fixed;
36with Ada.Text_IO;       use Ada.Text_IO;
37with GNAT.Command_Line; use GNAT.Command_Line;
38with GNAT.Strings;      use GNAT.Strings;
39
40procedure Gnatxref is
41   Search_Unused   : Boolean := False;
42   Local_Symbols   : Boolean := True;
43   Prj_File        : File_Name_String;
44   Prj_File_Length : Natural := 0;
45   Usage_Error     : exception;
46   Full_Path_Name  : Boolean := False;
47   Vi_Mode         : Boolean := False;
48   Read_Only       : Boolean := False;
49   Have_File       : Boolean := False;
50   Der_Info        : Boolean := False;
51
52   RTS_Specified : String_Access := null;
53   --  Used to detect multiple use of --RTS= switch
54
55   procedure Parse_Cmd_Line;
56   --  Parse every switch on the command line
57
58   procedure Write_Usage;
59   --  Print a small help page for program usage
60
61   --------------------
62   -- Parse_Cmd_Line --
63   --------------------
64
65   procedure Parse_Cmd_Line is
66   begin
67      loop
68         case
69           GNAT.Command_Line.Getopt
70             ("a aI: aO: d f g h I: nostdinc nostdlib p: u v -RTS=")
71         is
72            when ASCII.NUL =>
73               exit;
74
75            when 'a' =>
76               if GNAT.Command_Line.Full_Switch = "a" then
77                  Read_Only := True;
78
79               elsif GNAT.Command_Line.Full_Switch = "aI" then
80                  Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
81
82               else
83                  Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
84               end if;
85
86            when 'd' =>
87               Der_Info := True;
88
89            when 'f' =>
90               Full_Path_Name := True;
91
92            when 'g' =>
93               Local_Symbols := False;
94
95            when 'h' =>
96               Write_Usage;
97
98            when 'I' =>
99               Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
100               Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
101
102            when 'n' =>
103               if GNAT.Command_Line.Full_Switch = "nostdinc" then
104                  Opt.No_Stdinc := True;
105               elsif GNAT.Command_Line.Full_Switch = "nostlib" then
106                  Opt.No_Stdlib := True;
107               end if;
108
109            when 'p' =>
110               declare
111                  S : constant String := GNAT.Command_Line.Parameter;
112               begin
113                  Prj_File_Length := S'Length;
114                  Prj_File (1 .. Prj_File_Length) := S;
115               end;
116
117            when 'u' =>
118               Search_Unused := True;
119               Vi_Mode := False;
120
121            when 'v' =>
122               Vi_Mode := True;
123               Search_Unused := False;
124
125            --  The only switch starting with -- recognized is --RTS
126
127            when '-' =>
128
129               --  Check that it is the first time we see this switch
130
131               if RTS_Specified = null then
132                  RTS_Specified := new String'(GNAT.Command_Line.Parameter);
133
134               elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
135                  Osint.Fail ("--RTS cannot be specified multiple times");
136               end if;
137
138               Opt.No_Stdinc  := True;
139               Opt.RTS_Switch := True;
140
141               declare
142                  Src_Path_Name : constant String_Ptr :=
143                                    Get_RTS_Search_Dir
144                                      (GNAT.Command_Line.Parameter, Include);
145
146                  Lib_Path_Name : constant String_Ptr :=
147                                    Get_RTS_Search_Dir
148                                      (GNAT.Command_Line.Parameter, Objects);
149
150               begin
151                  if Src_Path_Name /= null and then Lib_Path_Name /= null then
152                     Add_Search_Dirs (Src_Path_Name, Include);
153                     Add_Search_Dirs (Lib_Path_Name, Objects);
154
155                  elsif Src_Path_Name = null and then Lib_Path_Name = null then
156                     Osint.Fail ("RTS path not valid: missing " &
157                                 "adainclude and adalib directories");
158
159                  elsif Src_Path_Name = null then
160                     Osint.Fail ("RTS path not valid: missing " &
161                                 "adainclude directory");
162
163                  elsif  Lib_Path_Name = null then
164                     Osint.Fail ("RTS path not valid: missing " &
165                                 "adalib directory");
166                  end if;
167               end;
168
169            when others =>
170               Write_Usage;
171         end case;
172      end loop;
173
174      --  Get the other arguments
175
176      loop
177         declare
178            S : constant String := GNAT.Command_Line.Get_Argument;
179
180         begin
181            exit when S'Length = 0;
182
183            if Ada.Strings.Fixed.Index (S, ":") /= 0 then
184               Ada.Text_IO.Put_Line
185                 ("Only file names are allowed on the command line");
186               Write_Usage;
187            end if;
188
189            Add_Xref_File (S);
190            Have_File := True;
191         end;
192      end loop;
193
194   exception
195      when GNAT.Command_Line.Invalid_Switch =>
196         Ada.Text_IO.Put_Line ("Invalid switch : "
197                               & GNAT.Command_Line.Full_Switch);
198         Write_Usage;
199
200      when GNAT.Command_Line.Invalid_Parameter =>
201         Ada.Text_IO.Put_Line ("Parameter missing for : "
202                               & GNAT.Command_Line.Full_Switch);
203         Write_Usage;
204   end Parse_Cmd_Line;
205
206   -----------------
207   -- Write_Usage --
208   -----------------
209
210   procedure Write_Usage is
211   begin
212      Put_Line ("GNATXREF " & Gnatvsn.Gnat_Version_String
213                & " Copyright 1998-2004, Ada Core Technologies Inc.");
214      Put_Line ("Usage: gnatxref [switches] file1 file2 ...");
215      New_Line;
216      Put_Line ("  file ... list of source files to xref, " &
217                "including with'ed units");
218      New_Line;
219      Put_Line ("gnatxref switches:");
220      Put_Line ("   -a        Consider all files, even when the ali file is"
221                & " readonly");
222      Put_Line ("   -aIdir    Specify source files search path");
223      Put_Line ("   -aOdir    Specify library/object files search path");
224      Put_Line ("   -d        Output derived type information");
225      Put_Line ("   -f        Output full path name");
226      Put_Line ("   -g        Output information only for global symbols");
227      Put_Line ("   -Idir     Like -aIdir -aOdir");
228      Put_Line ("   -nostdinc Don't look for sources in the system default"
229                & " directory");
230      Put_Line ("   -nostdlib Don't look for library files in the system"
231                & " default directory");
232      Put_Line ("   --RTS=dir specify the default source and object search"
233                & " path");
234      Put_Line ("   -p file   Use file as the default project file");
235      Put_Line ("   -u        List unused entities");
236      Put_Line ("   -v        Print a 'tags' file for vi");
237      New_Line;
238
239      raise Usage_Error;
240   end Write_Usage;
241
242begin
243   Parse_Cmd_Line;
244
245   if not Have_File then
246      Write_Usage;
247   end if;
248
249   Xr_Tabls.Set_Default_Match (True);
250
251   --  Find the project file
252
253   if Prj_File_Length = 0 then
254      Xr_Tabls.Create_Project_File
255        (Default_Project_File (Osint.To_Host_Dir_Spec (".", False).all));
256   else
257      Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
258   end if;
259
260   --  Fill up the table
261
262   Search_Xref (Local_Symbols, Read_Only, Der_Info);
263
264   if Search_Unused then
265      Print_Unused (Full_Path_Name);
266   elsif Vi_Mode then
267      Print_Vi (Full_Path_Name);
268   else
269      Print_Xref (Full_Path_Name);
270   end if;
271
272exception
273   when Usage_Error =>
274      null;
275end Gnatxref;
276