1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1998-2002 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 ------------------------------------------------------------------------------
25 with Xr_Tabls; use Xr_Tabls;
26 with Xref_Lib; use Xref_Lib;
27 with Osint; use Osint;
28 with Types; use Types;
33 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
34 with Ada.Text_IO; use Ada.Text_IO;
35 with GNAT.Command_Line; use GNAT.Command_Line;
43 Output_Ref : Boolean := False;
44 Pattern : Xref_Lib.Search_Pattern;
45 Local_Symbols : Boolean := True;
46 Prj_File : File_Name_String;
47 Prj_File_Length : Natural := 0;
48 Nb_File : Natural := 0;
49 Usage_Error : exception;
50 Full_Path_Name : Boolean := False;
51 Have_Entity : Boolean := False;
52 Wide_Search : Boolean := True;
53 Glob_Mode : Boolean := True;
54 Der_Info : Boolean := False;
55 Type_Tree : Boolean := False;
56 Read_Only : Boolean := False;
57 Source_Lines : Boolean := False;
59 Has_File_In_Entity : Boolean := False;
60 -- Will be true if a file name was specified in the entity
62 procedure Parse_Cmd_Line;
63 -- Parse every switch on the command line
65 procedure Write_Usage;
66 -- Print a small help page for program usage
72 procedure Parse_Cmd_Line is
76 GNAT.Command_Line.Getopt
77 ("a aI: aO: d e f g h I: nostdinc nostdlib p: r s t -RTS=")
83 if GNAT.Command_Line.Full_Switch = "a" then
86 elsif GNAT.Command_Line.Full_Switch = "aI" then
87 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
90 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
100 Full_Path_Name := True;
103 Local_Symbols := False;
109 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
110 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
113 if GNAT.Command_Line.Full_Switch = "nostdinc" then
114 Opt.No_Stdinc := True;
116 elsif GNAT.Command_Line.Full_Switch = "nostlib" then
117 Opt.No_Stdlib := True;
122 S : constant String := GNAT.Command_Line.Parameter;
125 Prj_File_Length := S'Length;
126 Prj_File (1 .. Prj_File_Length) := S;
133 Source_Lines := True;
138 -- Only switch starting with -- recognized is --RTS
141 Opt.No_Stdinc := True;
142 Opt.RTS_Switch := True;
145 Src_Path_Name : String_Ptr :=
147 (GNAT.Command_Line.Parameter, Include);
148 Lib_Path_Name : String_Ptr :=
150 (GNAT.Command_Line.Parameter, Objects);
153 if Src_Path_Name /= null and then Lib_Path_Name /= null then
154 Add_Search_Dirs (Src_Path_Name, Include);
155 Add_Search_Dirs (Lib_Path_Name, Objects);
157 elsif Src_Path_Name = null and then Lib_Path_Name = null then
158 Osint.Fail ("RTS path not valid: missing " &
159 "adainclude and adalib directories");
161 elsif Src_Path_Name = null then
162 Osint.Fail ("RTS path not valid: missing " &
163 "adainclude directory");
165 elsif Lib_Path_Name = null then
166 Osint.Fail ("RTS path not valid: missing " &
176 -- Get the other arguments
180 S : constant String := GNAT.Command_Line.Get_Argument;
183 exit when S'Length = 0;
185 -- First argument is the pattern
187 if not Have_Entity then
188 Add_Entity (Pattern, S, Glob_Mode);
191 if not Has_File_In_Entity
192 and then Index (S, ":") /= 0
194 Has_File_In_Entity := True;
197 -- Next arguments are the files to search
200 Wide_Search := False;
201 Nb_File := Nb_File + 1;
207 when GNAT.Command_Line.Invalid_Switch =>
208 Ada.Text_IO.Put_Line ("Invalid switch : "
209 & GNAT.Command_Line.Full_Switch);
212 when GNAT.Command_Line.Invalid_Parameter =>
213 Ada.Text_IO.Put_Line ("Parameter missing for : "
214 & GNAT.Command_Line.Full_Switch);
217 when Xref_Lib.Invalid_Argument =>
218 Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
226 procedure Write_Usage is
228 Put_Line ("GNATFIND " & Gnatvsn.Gnat_Version_String
229 & " Copyright 1998-2002, Ada Core Technologies Inc.");
230 Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
231 & "[file1 file2 ...]");
233 Put_Line (" pattern Name of the entity to look for (can have "
235 Put_Line (" sourcefile Only find entities referenced from this "
237 Put_Line (" line Only find entities referenced from this line "
239 Put_Line (" column Only find entities referenced from this columns"
241 Put_Line (" file ... Set of Ada source files to search for "
242 & "references. This parameters are optional");
244 Put_Line ("gnatfind switches:");
245 Put_Line (" -a Consider all files, even when the ali file is "
247 Put_Line (" -aIdir Specify source files search path");
248 Put_Line (" -aOdir Specify library/object files search path");
249 Put_Line (" -d Output derived type information");
250 Put_Line (" -e Use the full regular expression set for "
252 Put_Line (" -f Output full path name");
253 Put_Line (" -g Output information only for global symbols");
254 Put_Line (" -Idir Like -aIdir -aOdir");
255 Put_Line (" -nostdinc Don't look for sources in the system default"
257 Put_Line (" -nostdlib Don't look for library files in the system"
258 & " default directory");
259 Put_Line (" --RTS=dir specify the default source and object search"
261 Put_Line (" -p file Use file as the default project file");
262 Put_Line (" -r Find all references (default to find declaration"
264 Put_Line (" -s Print source line");
265 Put_Line (" -t Print type hierarchy");
271 -- Start of processing for Gnatfind
276 if not Have_Entity then
280 -- Special case to speed things up: if the user has a command line of the
281 -- form 'gnatfind entity:file', ie has specified a file and only wants the
282 -- bodies and specs, then we can restrict the search to the .ali file
283 -- associated with 'file'.
285 if Has_File_In_Entity
286 and then not Output_Ref
288 Wide_Search := False;
291 -- Find the project file
293 if Prj_File_Length = 0 then
294 Xr_Tabls.Create_Project_File (Default_Project_File ("."));
296 Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
301 if Type_Tree and then Nb_File > 1 then
302 Ada.Text_IO.Put_Line ("Error: for type hierarchy output you must "
303 & "specify only one file.");
304 Ada.Text_IO.New_Line;
308 Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
309 Der_Info, Type_Tree);
312 Xr_Tabls.Grep_Source_Files;
315 Print_Gnatfind (Output_Ref, Full_Path_Name);