]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/osint.adb
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / osint.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- O S I N T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2003 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
27 with Fmap; use Fmap;
28 with Hostparm;
29 with Namet; use Namet;
30 with Opt; use Opt;
31 with Output; use Output;
32 with Sdefault; use Sdefault;
33 with System.Case_Util; use System.Case_Util;
34 with Table;
35
36 with Unchecked_Conversion;
37
38 with GNAT.OS_Lib; use GNAT.OS_Lib;
39 with GNAT.HTable;
40
41 package body Osint is
42
43 Running_Program : Program_Type := Unspecified;
44 -- comment required here ???
45
46 Program_Set : Boolean := False;
47 -- comment required here ???
48
49 Std_Prefix : String_Ptr;
50 -- Standard prefix, computed dynamically the first time Relocate_Path
51 -- is called, and cached for subsequent calls.
52
53 -------------------------------------
54 -- Use of Name_Find and Name_Enter --
55 -------------------------------------
56
57 -- This package creates a number of source, ALI and object file names
58 -- that are used to locate the actual file and for the purpose of
59 -- message construction. These names need not be accessible by Name_Find,
60 -- and can be therefore created by using routine Name_Enter. The files in
61 -- question are file names with a prefix directory (ie the files not
62 -- in the current directory). File names without a prefix directory are
63 -- entered with Name_Find because special values might be attached to
64 -- the various Info fields of the corresponding name table entry.
65
66 -----------------------
67 -- Local Subprograms --
68 -----------------------
69
70 function Append_Suffix_To_File_Name
71 (Name : Name_Id;
72 Suffix : String) return Name_Id;
73 -- Appends Suffix to Name and returns the new name.
74
75 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type;
76 -- Convert OS format time to GNAT format time stamp
77
78 function Concat (String_One : String; String_Two : String) return String;
79 -- Concatenates 2 strings and returns the result of the concatenation
80
81 function Executable_Prefix return String_Ptr;
82 -- Returns the name of the root directory where the executable is stored.
83 -- The executable must be located in a directory called "bin", or
84 -- under root/lib/gcc-lib/..., or under root/libexec/gcc/... Thus, if
85 -- the executable is stored in directory "/foo/bar/bin", this routine
86 -- returns "/foo/bar/". Return "" if the location is not recognized
87 -- as described above.
88
89 function Update_Path (Path : String_Ptr) return String_Ptr;
90 -- Update the specified path to replace the prefix with the location
91 -- where GNAT is installed. See the file prefix.c in GCC for details.
92
93 procedure Write_With_Check (A : Address; N : Integer);
94 -- Writes N bytes from buffer starting at address A to file whose FD is
95 -- stored in Output_FD, and whose file name is stored as a File_Name_Type
96 -- in Output_File_Name. A check is made for disk full, and if this is
97 -- detected, the file being written is deleted, and a fatal error is
98 -- signalled.
99
100 function Locate_File
101 (N : File_Name_Type;
102 T : File_Type;
103 Dir : Natural;
104 Name : String) return File_Name_Type;
105 -- See if the file N whose name is Name exists in directory Dir. Dir is
106 -- an index into the Lib_Search_Directories table if T = Library.
107 -- Otherwise if T = Source, Dir is an index into the
108 -- Src_Search_Directories table. Returns the File_Name_Type of the
109 -- full file name if file found, or No_File if not found.
110
111 function C_String_Length (S : Address) return Integer;
112 -- Returns length of a C string. Returns zero for a null address.
113
114 function To_Path_String_Access
115 (Path_Addr : Address;
116 Path_Len : Integer) return String_Access;
117 -- Converts a C String to an Ada String. Are we doing this to avoid
118 -- withing Interfaces.C.Strings ???
119
120 ------------------------------
121 -- Other Local Declarations --
122 ------------------------------
123
124 EOL : constant Character := ASCII.LF;
125 -- End of line character
126
127 Number_File_Names : Int := 0;
128 -- The total number of file names found on command line and placed in
129 -- File_Names.
130
131 Look_In_Primary_Directory_For_Current_Main : Boolean := False;
132 -- When this variable is True, Find_File will only look in
133 -- the Primary_Directory for the Current_Main file.
134 -- This variable is always True for the compiler.
135 -- It is also True for gnatmake, when the soucr name given
136 -- on the command line has directory information.
137
138 Current_Full_Source_Name : File_Name_Type := No_File;
139 Current_Full_Source_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
140 Current_Full_Lib_Name : File_Name_Type := No_File;
141 Current_Full_Lib_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
142 Current_Full_Obj_Name : File_Name_Type := No_File;
143 Current_Full_Obj_Stamp : Time_Stamp_Type := Empty_Time_Stamp;
144 -- Respectively full name (with directory info) and time stamp of
145 -- the latest source, library and object files opened by Read_Source_File
146 -- and Read_Library_Info.
147
148 ------------------
149 -- Search Paths --
150 ------------------
151
152 Primary_Directory : constant := 0;
153 -- This is index in the tables created below for the first directory to
154 -- search in for source or library information files. This is the
155 -- directory containing the latest main input file (a source file for
156 -- the compiler or a library file for the binder).
157
158 package Src_Search_Directories is new Table.Table (
159 Table_Component_Type => String_Ptr,
160 Table_Index_Type => Natural,
161 Table_Low_Bound => Primary_Directory,
162 Table_Initial => 10,
163 Table_Increment => 100,
164 Table_Name => "Osint.Src_Search_Directories");
165 -- Table of names of directories in which to search for source (Compiler)
166 -- files. This table is filled in the order in which the directories are
167 -- to be searched, and then used in that order.
168
169 package Lib_Search_Directories is new Table.Table (
170 Table_Component_Type => String_Ptr,
171 Table_Index_Type => Natural,
172 Table_Low_Bound => Primary_Directory,
173 Table_Initial => 10,
174 Table_Increment => 100,
175 Table_Name => "Osint.Lib_Search_Directories");
176 -- Table of names of directories in which to search for library (Binder)
177 -- files. This table is filled in the order in which the directories are
178 -- to be searched and then used in that order. The reason for having two
179 -- distinct tables is that we need them both in gnatmake.
180
181 ---------------------
182 -- File Hash Table --
183 ---------------------
184
185 -- The file hash table is provided to free the programmer from any
186 -- efficiency concern when retrieving full file names or time stamps of
187 -- source files. If the programmer calls Source_File_Data (Cache => True)
188 -- he is guaranteed that the price to retrieve the full name (ie with
189 -- directory info) or time stamp of the file will be payed only once,
190 -- the first time the full name is actually searched (or the first time
191 -- the time stamp is actually retrieved). This is achieved by employing
192 -- a hash table that stores as a key the File_Name_Type of the file and
193 -- associates to that File_Name_Type the full file name of the file and its
194 -- time stamp.
195
196 File_Cache_Enabled : Boolean := False;
197 -- Set to true if you want the enable the file data caching mechanism.
198
199 type File_Hash_Num is range 0 .. 1020;
200
201 function File_Hash (F : File_Name_Type) return File_Hash_Num;
202 -- Compute hash index for use by Simple_HTable
203
204 package File_Name_Hash_Table is new GNAT.HTable.Simple_HTable (
205 Header_Num => File_Hash_Num,
206 Element => File_Name_Type,
207 No_Element => No_File,
208 Key => File_Name_Type,
209 Hash => File_Hash,
210 Equal => "=");
211
212 package File_Stamp_Hash_Table is new GNAT.HTable.Simple_HTable (
213 Header_Num => File_Hash_Num,
214 Element => Time_Stamp_Type,
215 No_Element => Empty_Time_Stamp,
216 Key => File_Name_Type,
217 Hash => File_Hash,
218 Equal => "=");
219
220 function Smart_Find_File
221 (N : File_Name_Type;
222 T : File_Type) return File_Name_Type;
223 -- Exactly like Find_File except that if File_Cache_Enabled is True this
224 -- routine looks first in the hash table to see if the full name of the
225 -- file is already available.
226
227 function Smart_File_Stamp
228 (N : File_Name_Type;
229 T : File_Type) return Time_Stamp_Type;
230 -- Takes the same parameter as the routine above (N is a file name
231 -- without any prefix directory information) and behaves like File_Stamp
232 -- except that if File_Cache_Enabled is True this routine looks first in
233 -- the hash table to see if the file stamp of the file is already
234 -- available.
235
236 -----------------------------
237 -- Add_Default_Search_Dirs --
238 -----------------------------
239
240 procedure Add_Default_Search_Dirs is
241 Search_Dir : String_Access;
242 Search_Path : String_Access;
243 Path_File_Name : String_Access;
244
245 procedure Add_Search_Dir
246 (Search_Dir : String;
247 Additional_Source_Dir : Boolean);
248 procedure Add_Search_Dir
249 (Search_Dir : String_Access;
250 Additional_Source_Dir : Boolean);
251 -- Add a source search dir or a library search dir, depending on the
252 -- value of Additional_Source_Dir.
253
254 procedure Get_Dirs_From_File (Additional_Source_Dir : Boolean);
255 -- Open a path file and read the directory to search, one per line
256
257 function Get_Libraries_From_Registry return String_Ptr;
258 -- On Windows systems, get the list of installed standard libraries
259 -- from the registry key:
260 -- HKEY_LOCAL_MACHINE\SOFTWARE\Ada Core Technologies\
261 -- GNAT\Standard Libraries
262 -- Return an empty string on other systems
263
264 --------------------
265 -- Add_Search_Dir --
266 --------------------
267
268 procedure Add_Search_Dir
269 (Search_Dir : String;
270 Additional_Source_Dir : Boolean)
271 is
272 begin
273 if Additional_Source_Dir then
274 Add_Src_Search_Dir (Search_Dir);
275 else
276 Add_Lib_Search_Dir (Search_Dir);
277 end if;
278 end Add_Search_Dir;
279
280 procedure Add_Search_Dir
281 (Search_Dir : String_Access;
282 Additional_Source_Dir : Boolean)
283 is
284 begin
285 if Additional_Source_Dir then
286 Add_Src_Search_Dir (Search_Dir.all);
287 else
288 Add_Lib_Search_Dir (Search_Dir.all);
289 end if;
290 end Add_Search_Dir;
291
292 ------------------------
293 -- Get_Dirs_From_File --
294 ------------------------
295
296 procedure Get_Dirs_From_File (Additional_Source_Dir : Boolean) is
297 File_FD : File_Descriptor;
298 Buffer : String (1 .. Path_File_Name'Length + 1);
299 Len : Natural;
300 Actual_Len : Natural;
301 S : String_Access;
302 Curr : Natural;
303 First : Natural;
304 Ch : Character;
305
306 Status : Boolean;
307 -- For the call to Close
308
309 begin
310 -- Construct a C compatible character string buffer.
311
312 Buffer (1 .. Buffer'Last - 1) := Path_File_Name.all;
313 Buffer (Buffer'Last) := ASCII.NUL;
314
315 File_FD := Open_Read (Buffer'Address, Binary);
316
317 -- If we cannot open the file, we ignore it, we don't fail
318
319 if File_FD = Invalid_FD then
320 return;
321 end if;
322
323 Len := Integer (File_Length (File_FD));
324
325 S := new String (1 .. Len);
326
327 -- Read the file. Note that the loop is not necessary since the
328 -- whole file is read at once except on VMS.
329
330 Curr := 1;
331 Actual_Len := Len;
332 while Curr <= Len and then Actual_Len /= 0 loop
333 Actual_Len := Read (File_FD, S (Curr)'Address, Len);
334 Curr := Curr + Actual_Len;
335 end loop;
336
337 -- We are done with the file, so we close it
338
339 Close (File_FD, Status);
340 -- We ignore any error here, because we have successfully read the
341 -- file.
342
343 -- Now, we read line by line
344
345 First := 1;
346 Curr := 0;
347
348 while Curr < Len loop
349 Ch := S (Curr + 1);
350
351 if Ch = ASCII.CR or else Ch = ASCII.LF
352 or else Ch = ASCII.FF or else Ch = ASCII.VT
353 then
354 if First <= Curr then
355 Add_Search_Dir (S (First .. Curr), Additional_Source_Dir);
356 end if;
357
358 First := Curr + 2;
359 end if;
360
361 Curr := Curr + 1;
362 end loop;
363
364 -- Last line is a special case, if the file does not end with
365 -- an end of line mark.
366
367 if First <= S'Last then
368 Add_Search_Dir (S (First .. S'Last), Additional_Source_Dir);
369 end if;
370 end Get_Dirs_From_File;
371
372 ---------------------------------
373 -- Get_Libraries_From_Registry --
374 ---------------------------------
375
376 function Get_Libraries_From_Registry return String_Ptr is
377 function C_Get_Libraries_From_Registry return Address;
378 pragma Import (C, C_Get_Libraries_From_Registry,
379 "__gnat_get_libraries_from_registry");
380 function Strlen (Str : Address) return Integer;
381 pragma Import (C, Strlen, "strlen");
382 procedure Strncpy (X : Address; Y : Address; Length : Integer);
383 pragma Import (C, Strncpy, "strncpy");
384 Result_Ptr : Address;
385 Result_Length : Integer;
386 Out_String : String_Ptr;
387
388 begin
389 Result_Ptr := C_Get_Libraries_From_Registry;
390 Result_Length := Strlen (Result_Ptr);
391
392 Out_String := new String (1 .. Result_Length);
393 Strncpy (Out_String.all'Address, Result_Ptr, Result_Length);
394 return Out_String;
395 end Get_Libraries_From_Registry;
396
397 -- Start of processing for Add_Default_Search_Dirs
398
399 begin
400 -- After the locations specified on the command line, the next places
401 -- to look for files are the directories specified by the appropriate
402 -- environment variable. Get this value, extract the directory names
403 -- and store in the tables.
404
405 -- On VMS, don't expand the logical name (e.g. environment variable),
406 -- just put it into Unix (e.g. canonical) format. System services
407 -- will handle the expansion as part of the file processing.
408
409 for Additional_Source_Dir in False .. True loop
410
411 if Additional_Source_Dir then
412 Search_Path := Getenv (Ada_Include_Path);
413 if Search_Path'Length > 0 then
414 if Hostparm.OpenVMS then
415 Search_Path := To_Canonical_Path_Spec ("ADA_INCLUDE_PATH:");
416 else
417 Search_Path := To_Canonical_Path_Spec (Search_Path.all);
418 end if;
419 end if;
420 else
421 Search_Path := Getenv (Ada_Objects_Path);
422 if Search_Path'Length > 0 then
423 if Hostparm.OpenVMS then
424 Search_Path := To_Canonical_Path_Spec ("ADA_OBJECTS_PATH:");
425 else
426 Search_Path := To_Canonical_Path_Spec (Search_Path.all);
427 end if;
428 end if;
429 end if;
430
431 Get_Next_Dir_In_Path_Init (Search_Path);
432 loop
433 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
434 exit when Search_Dir = null;
435 Add_Search_Dir (Search_Dir, Additional_Source_Dir);
436 end loop;
437 end loop;
438
439 -- Check for eventual project path file env vars
440
441 Path_File_Name := Getenv (Project_Include_Path_File);
442
443 if Path_File_Name'Length > 0 then
444 Get_Dirs_From_File (Additional_Source_Dir => True);
445 end if;
446
447 Path_File_Name := Getenv (Project_Objects_Path_File);
448
449 if Path_File_Name'Length > 0 then
450 Get_Dirs_From_File (Additional_Source_Dir => False);
451 end if;
452
453 -- For the compiler, if --RTS= was apecified, add the runtime
454 -- directories.
455
456 if RTS_Src_Path_Name /= null and then
457 RTS_Lib_Path_Name /= null
458 then
459 Add_Search_Dirs (RTS_Src_Path_Name, Include);
460 Add_Search_Dirs (RTS_Lib_Path_Name, Objects);
461
462 else
463 if not Opt.No_Stdinc then
464
465 -- For WIN32 systems, look for any system libraries defined in
466 -- the registry. These are added to both source and object
467 -- directories.
468
469 Search_Path := String_Access (Get_Libraries_From_Registry);
470
471 Get_Next_Dir_In_Path_Init (Search_Path);
472 loop
473 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
474 exit when Search_Dir = null;
475 Add_Search_Dir (Search_Dir, False);
476 Add_Search_Dir (Search_Dir, True);
477 end loop;
478
479 -- The last place to look are the defaults
480
481 Search_Path :=
482 Read_Default_Search_Dirs
483 (String_Access (Update_Path (Search_Dir_Prefix)),
484 Include_Search_File,
485 String_Access (Update_Path (Include_Dir_Default_Name)));
486
487 Get_Next_Dir_In_Path_Init (Search_Path);
488 loop
489 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
490 exit when Search_Dir = null;
491 Add_Search_Dir (Search_Dir, True);
492 end loop;
493 end if;
494
495 if not Opt.No_Stdlib and not Opt.RTS_Switch then
496 Search_Path :=
497 Read_Default_Search_Dirs
498 (String_Access (Update_Path (Search_Dir_Prefix)),
499 Objects_Search_File,
500 String_Access (Update_Path (Object_Dir_Default_Name)));
501
502 Get_Next_Dir_In_Path_Init (Search_Path);
503 loop
504 Search_Dir := Get_Next_Dir_In_Path (Search_Path);
505 exit when Search_Dir = null;
506 Add_Search_Dir (Search_Dir, False);
507 end loop;
508 end if;
509 end if;
510 end Add_Default_Search_Dirs;
511
512 --------------
513 -- Add_File --
514 --------------
515
516 procedure Add_File (File_Name : String) is
517 begin
518 Number_File_Names := Number_File_Names + 1;
519
520 -- As Add_File may be called for mains specified inside
521 -- a project file, File_Names may be too short and needs
522 -- to be extended.
523
524 if Number_File_Names > File_Names'Last then
525 File_Names := new File_Name_Array'(File_Names.all & File_Names.all);
526 end if;
527
528 File_Names (Number_File_Names) := new String'(File_Name);
529 end Add_File;
530
531 ------------------------
532 -- Add_Lib_Search_Dir --
533 ------------------------
534
535 procedure Add_Lib_Search_Dir (Dir : String) is
536 begin
537 if Dir'Length = 0 then
538 Fail ("missing library directory name");
539 end if;
540
541 Lib_Search_Directories.Increment_Last;
542 Lib_Search_Directories.Table (Lib_Search_Directories.Last) :=
543 Normalize_Directory_Name (Dir);
544 end Add_Lib_Search_Dir;
545
546 ---------------------
547 -- Add_Search_Dirs --
548 ---------------------
549
550 procedure Add_Search_Dirs
551 (Search_Path : String_Ptr;
552 Path_Type : Search_File_Type)
553 is
554 Current_Search_Path : String_Access;
555
556 begin
557 Get_Next_Dir_In_Path_Init (String_Access (Search_Path));
558 loop
559 Current_Search_Path :=
560 Get_Next_Dir_In_Path (String_Access (Search_Path));
561 exit when Current_Search_Path = null;
562
563 if Path_Type = Include then
564 Add_Src_Search_Dir (Current_Search_Path.all);
565 else
566 Add_Lib_Search_Dir (Current_Search_Path.all);
567 end if;
568 end loop;
569 end Add_Search_Dirs;
570
571 ------------------------
572 -- Add_Src_Search_Dir --
573 ------------------------
574
575 procedure Add_Src_Search_Dir (Dir : String) is
576 begin
577 if Dir'Length = 0 then
578 Fail ("missing source directory name");
579 end if;
580
581 Src_Search_Directories.Increment_Last;
582 Src_Search_Directories.Table (Src_Search_Directories.Last) :=
583 Normalize_Directory_Name (Dir);
584 end Add_Src_Search_Dir;
585
586 --------------------------------
587 -- Append_Suffix_To_File_Name --
588 --------------------------------
589
590 function Append_Suffix_To_File_Name
591 (Name : Name_Id;
592 Suffix : String) return Name_Id
593 is
594 begin
595 Get_Name_String (Name);
596 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
597 Name_Len := Name_Len + Suffix'Length;
598 return Name_Find;
599 end Append_Suffix_To_File_Name;
600
601 ---------------------
602 -- C_String_Length --
603 ---------------------
604
605 function C_String_Length (S : Address) return Integer is
606 function Strlen (S : Address) return Integer;
607 pragma Import (C, Strlen, "strlen");
608
609 begin
610 if S = Null_Address then
611 return 0;
612 else
613 return Strlen (S);
614 end if;
615 end C_String_Length;
616
617 ------------------------------
618 -- Canonical_Case_File_Name --
619 ------------------------------
620
621 -- For now, we only deal with the case of a-z. Eventually we should
622 -- worry about other Latin-1 letters on systems that support this ???
623
624 procedure Canonical_Case_File_Name (S : in out String) is
625 begin
626 if not File_Names_Case_Sensitive then
627 for J in S'Range loop
628 if S (J) in 'A' .. 'Z' then
629 S (J) := Character'Val (
630 Character'Pos (S (J)) +
631 Character'Pos ('a') -
632 Character'Pos ('A'));
633 end if;
634 end loop;
635 end if;
636 end Canonical_Case_File_Name;
637
638 ------------
639 -- Concat --
640 ------------
641
642 function Concat (String_One : String; String_Two : String) return String is
643 Buffer : String (1 .. String_One'Length + String_Two'Length);
644
645 begin
646 Buffer (1 .. String_One'Length) := String_One;
647 Buffer (String_One'Length + 1 .. Buffer'Last) := String_Two;
648 return Buffer;
649 end Concat;
650
651 ---------------------------
652 -- Create_File_And_Check --
653 ---------------------------
654
655 procedure Create_File_And_Check
656 (Fdesc : out File_Descriptor;
657 Fmode : Mode)
658 is
659 begin
660 Output_File_Name := Name_Enter;
661 Fdesc := Create_File (Name_Buffer'Address, Fmode);
662
663 if Fdesc = Invalid_FD then
664 Fail ("Cannot create: ", Name_Buffer (1 .. Name_Len));
665 end if;
666 end Create_File_And_Check;
667
668 --------------------------------
669 -- Current_Library_File_Stamp --
670 --------------------------------
671
672 function Current_Library_File_Stamp return Time_Stamp_Type is
673 begin
674 return Current_Full_Lib_Stamp;
675 end Current_Library_File_Stamp;
676
677 -------------------------------
678 -- Current_Object_File_Stamp --
679 -------------------------------
680
681 function Current_Object_File_Stamp return Time_Stamp_Type is
682 begin
683 return Current_Full_Obj_Stamp;
684 end Current_Object_File_Stamp;
685
686 -------------------------------
687 -- Current_Source_File_Stamp --
688 -------------------------------
689
690 function Current_Source_File_Stamp return Time_Stamp_Type is
691 begin
692 return Current_Full_Source_Stamp;
693 end Current_Source_File_Stamp;
694
695 ----------------------------
696 -- Dir_In_Obj_Search_Path --
697 ----------------------------
698
699 function Dir_In_Obj_Search_Path (Position : Natural) return String_Ptr is
700 begin
701 if Opt.Look_In_Primary_Dir then
702 return
703 Lib_Search_Directories.Table (Primary_Directory + Position - 1);
704 else
705 return Lib_Search_Directories.Table (Primary_Directory + Position);
706 end if;
707 end Dir_In_Obj_Search_Path;
708
709 ----------------------------
710 -- Dir_In_Src_Search_Path --
711 ----------------------------
712
713 function Dir_In_Src_Search_Path (Position : Natural) return String_Ptr is
714 begin
715 if Opt.Look_In_Primary_Dir then
716 return
717 Src_Search_Directories.Table (Primary_Directory + Position - 1);
718 else
719 return Src_Search_Directories.Table (Primary_Directory + Position);
720 end if;
721 end Dir_In_Src_Search_Path;
722
723 ---------------------
724 -- Executable_Name --
725 ---------------------
726
727 function Executable_Name (Name : File_Name_Type) return File_Name_Type is
728 Exec_Suffix : String_Access;
729
730 begin
731 if Name = No_File then
732 return No_File;
733 end if;
734
735 Get_Name_String (Name);
736 Exec_Suffix := Get_Executable_Suffix;
737
738 for J in Exec_Suffix'Range loop
739 Name_Len := Name_Len + 1;
740 Name_Buffer (Name_Len) := Exec_Suffix (J);
741 end loop;
742
743 Free (Exec_Suffix);
744
745 return Name_Enter;
746 end Executable_Name;
747
748 -------------------------
749 -- Executable_Prefix --
750 -------------------------
751
752 function Executable_Prefix return String_Ptr is
753 Exec_Name : String (1 .. Len_Arg (0));
754
755 function Get_Install_Dir (Exec : String) return String_Ptr;
756 -- S is the executable name preceeded by the absolute or relative
757 -- path, e.g. "c:\usr\bin\gcc.exe" or "..\bin\gcc".
758
759 ---------------------
760 -- Get_Install_Dir --
761 ---------------------
762
763 function Get_Install_Dir (Exec : String) return String_Ptr is
764 begin
765 for J in reverse Exec'Range loop
766 if Is_Directory_Separator (Exec (J)) then
767 if J < Exec'Last - 5 then
768 if (To_Lower (Exec (J + 1)) = 'l'
769 and then To_Lower (Exec (J + 2)) = 'i'
770 and then To_Lower (Exec (J + 3)) = 'b')
771 or else
772 (To_Lower (Exec (J + 1)) = 'b'
773 and then To_Lower (Exec (J + 2)) = 'i'
774 and then To_Lower (Exec (J + 3)) = 'n')
775 then
776 return new String'(Exec (Exec'First .. J));
777 end if;
778 end if;
779 end if;
780 end loop;
781
782 return new String'("");
783 end Get_Install_Dir;
784
785 -- Start of processing for Executable_Prefix
786
787 begin
788 Osint.Fill_Arg (Exec_Name'Address, 0);
789
790 -- First determine if a path prefix was placed in front of the
791 -- executable name.
792
793 for J in reverse Exec_Name'Range loop
794 if Is_Directory_Separator (Exec_Name (J)) then
795 return Get_Install_Dir (Exec_Name);
796 end if;
797 end loop;
798
799 -- If we come here, the user has typed the executable name with no
800 -- directory prefix.
801
802 return Get_Install_Dir (GNAT.OS_Lib.Locate_Exec_On_Path (Exec_Name).all);
803 end Executable_Prefix;
804
805 ------------------
806 -- Exit_Program --
807 ------------------
808
809 procedure Exit_Program (Exit_Code : Exit_Code_Type) is
810 begin
811 -- The program will exit with the following status:
812 -- 0 if the object file has been generated (with or without warnings)
813 -- 1 if recompilation was not needed (smart recompilation)
814 -- 2 if gnat1 has been killed by a signal (detected by GCC)
815 -- 4 for a fatal error
816 -- 5 if there were errors
817 -- 6 if no code has been generated (spec)
818 --
819 -- Note that exit code 3 is not used and must not be used as this is
820 -- the code returned by a program aborted via C abort() routine on
821 -- Windows. GCC checks for that case and thinks that the child process
822 -- has been aborted. This code (exit code 3) used to be the code used
823 -- for E_No_Code, but E_No_Code was changed to 6 for this reason.
824
825 case Exit_Code is
826 when E_Success => OS_Exit (0);
827 when E_Warnings => OS_Exit (0);
828 when E_No_Compile => OS_Exit (1);
829 when E_Fatal => OS_Exit (4);
830 when E_Errors => OS_Exit (5);
831 when E_No_Code => OS_Exit (6);
832 when E_Abort => OS_Abort;
833 end case;
834 end Exit_Program;
835
836 ----------
837 -- Fail --
838 ----------
839
840 procedure Fail (S1 : String; S2 : String := ""; S3 : String := "") is
841 begin
842 -- We use Output in case there is a special output set up.
843 -- In this case Set_Standard_Error will have no immediate effect.
844
845 Set_Standard_Error;
846 Osint.Write_Program_Name;
847 Write_Str (": ");
848 Write_Str (S1);
849 Write_Str (S2);
850 Write_Str (S3);
851 Write_Eol;
852
853 Exit_Program (E_Fatal);
854 end Fail;
855
856 ---------------
857 -- File_Hash --
858 ---------------
859
860 function File_Hash (F : File_Name_Type) return File_Hash_Num is
861 begin
862 return File_Hash_Num (Int (F) rem File_Hash_Num'Range_Length);
863 end File_Hash;
864
865 ----------------
866 -- File_Stamp --
867 ----------------
868
869 function File_Stamp (Name : File_Name_Type) return Time_Stamp_Type is
870 begin
871 if Name = No_File then
872 return Empty_Time_Stamp;
873 end if;
874
875 Get_Name_String (Name);
876
877 if not Is_Regular_File (Name_Buffer (1 .. Name_Len)) then
878 return Empty_Time_Stamp;
879 else
880 Name_Buffer (Name_Len + 1) := ASCII.NUL;
881 return OS_Time_To_GNAT_Time (File_Time_Stamp (Name_Buffer));
882 end if;
883 end File_Stamp;
884
885 ---------------
886 -- Find_File --
887 ---------------
888
889 function Find_File
890 (N : File_Name_Type;
891 T : File_Type) return File_Name_Type
892 is
893 begin
894 Get_Name_String (N);
895
896 declare
897 File_Name : String renames Name_Buffer (1 .. Name_Len);
898 File : File_Name_Type := No_File;
899 Last_Dir : Natural;
900
901 begin
902 -- If we are looking for a config file, look only in the current
903 -- directory, i.e. return input argument unchanged. Also look
904 -- only in the current directory if we are looking for a .dg
905 -- file (happens in -gnatD mode)
906
907 if T = Config
908 or else (Debug_Generated_Code
909 and then Name_Len > 3
910 and then
911 (Name_Buffer (Name_Len - 2 .. Name_Len) = ".dg"
912 or else
913 (Hostparm.OpenVMS and then
914 Name_Buffer (Name_Len - 2 .. Name_Len) = "_dg")))
915 then
916 return N;
917
918 -- If we are trying to find the current main file just look in the
919 -- directory where the user said it was.
920
921 elsif Look_In_Primary_Directory_For_Current_Main
922 and then Current_Main = N
923 then
924 return Locate_File (N, T, Primary_Directory, File_Name);
925
926 -- Otherwise do standard search for source file
927
928 else
929 -- Check the mapping of this file name
930
931 File := Mapped_Path_Name (N);
932
933 -- If the file name is mapped to a path name, return the
934 -- corresponding path name
935
936 if File /= No_File then
937 -- For locally removed file, Error_Name is returned; then
938 -- return No_File, indicating the file is not a source.
939
940 if File = Error_Name then
941 return No_File;
942
943 else
944 return File;
945 end if;
946 end if;
947
948 -- First place to look is in the primary directory (i.e. the same
949 -- directory as the source) unless this has been disabled with -I-
950
951 if Opt.Look_In_Primary_Dir then
952 File := Locate_File (N, T, Primary_Directory, File_Name);
953
954 if File /= No_File then
955 return File;
956 end if;
957 end if;
958
959 -- Finally look in directories specified with switches -I/-aI/-aO
960
961 if T = Library then
962 Last_Dir := Lib_Search_Directories.Last;
963 else
964 Last_Dir := Src_Search_Directories.Last;
965 end if;
966
967 for D in Primary_Directory + 1 .. Last_Dir loop
968 File := Locate_File (N, T, D, File_Name);
969
970 if File /= No_File then
971 return File;
972 end if;
973 end loop;
974
975 return No_File;
976 end if;
977 end;
978 end Find_File;
979
980 -----------------------
981 -- Find_Program_Name --
982 -----------------------
983
984 procedure Find_Program_Name is
985 Command_Name : String (1 .. Len_Arg (0));
986 Cindex1 : Integer := Command_Name'First;
987 Cindex2 : Integer := Command_Name'Last;
988
989 begin
990 Fill_Arg (Command_Name'Address, 0);
991
992 -- The program name might be specified by a full path name. However,
993 -- we don't want to print that all out in an error message, so the
994 -- path might need to be stripped away.
995
996 for J in reverse Cindex1 .. Cindex2 loop
997 if Is_Directory_Separator (Command_Name (J)) then
998 Cindex1 := J + 1;
999 exit;
1000 end if;
1001 end loop;
1002
1003 for J in reverse Cindex1 .. Cindex2 loop
1004 if Command_Name (J) = '.' then
1005 Cindex2 := J - 1;
1006 exit;
1007 end if;
1008 end loop;
1009
1010 Name_Len := Cindex2 - Cindex1 + 1;
1011 Name_Buffer (1 .. Name_Len) := Command_Name (Cindex1 .. Cindex2);
1012 end Find_Program_Name;
1013
1014 ------------------------
1015 -- Full_Lib_File_Name --
1016 ------------------------
1017
1018 function Full_Lib_File_Name (N : File_Name_Type) return File_Name_Type is
1019 begin
1020 return Find_File (N, Library);
1021 end Full_Lib_File_Name;
1022
1023 ----------------------------
1024 -- Full_Library_Info_Name --
1025 ----------------------------
1026
1027 function Full_Library_Info_Name return File_Name_Type is
1028 begin
1029 return Current_Full_Lib_Name;
1030 end Full_Library_Info_Name;
1031
1032 ---------------------------
1033 -- Full_Object_File_Name --
1034 ---------------------------
1035
1036 function Full_Object_File_Name return File_Name_Type is
1037 begin
1038 return Current_Full_Obj_Name;
1039 end Full_Object_File_Name;
1040
1041 ----------------------
1042 -- Full_Source_Name --
1043 ----------------------
1044
1045 function Full_Source_Name return File_Name_Type is
1046 begin
1047 return Current_Full_Source_Name;
1048 end Full_Source_Name;
1049
1050 ----------------------
1051 -- Full_Source_Name --
1052 ----------------------
1053
1054 function Full_Source_Name (N : File_Name_Type) return File_Name_Type is
1055 begin
1056 return Smart_Find_File (N, Source);
1057 end Full_Source_Name;
1058
1059 -------------------
1060 -- Get_Directory --
1061 -------------------
1062
1063 function Get_Directory (Name : File_Name_Type) return File_Name_Type is
1064 begin
1065 Get_Name_String (Name);
1066
1067 for J in reverse 1 .. Name_Len loop
1068 if Is_Directory_Separator (Name_Buffer (J)) then
1069 Name_Len := J;
1070 return Name_Find;
1071 end if;
1072 end loop;
1073
1074 Name_Len := Hostparm.Normalized_CWD'Length;
1075 Name_Buffer (1 .. Name_Len) := Hostparm.Normalized_CWD;
1076 return Name_Find;
1077 end Get_Directory;
1078
1079 --------------------------
1080 -- Get_Next_Dir_In_Path --
1081 --------------------------
1082
1083 Search_Path_Pos : Integer;
1084 -- Keeps track of current position in search path. Initialized by the
1085 -- call to Get_Next_Dir_In_Path_Init, updated by Get_Next_Dir_In_Path.
1086
1087 function Get_Next_Dir_In_Path
1088 (Search_Path : String_Access) return String_Access
1089 is
1090 Lower_Bound : Positive := Search_Path_Pos;
1091 Upper_Bound : Positive;
1092
1093 begin
1094 loop
1095 while Lower_Bound <= Search_Path'Last
1096 and then Search_Path.all (Lower_Bound) = Path_Separator
1097 loop
1098 Lower_Bound := Lower_Bound + 1;
1099 end loop;
1100
1101 exit when Lower_Bound > Search_Path'Last;
1102
1103 Upper_Bound := Lower_Bound;
1104 while Upper_Bound <= Search_Path'Last
1105 and then Search_Path.all (Upper_Bound) /= Path_Separator
1106 loop
1107 Upper_Bound := Upper_Bound + 1;
1108 end loop;
1109
1110 Search_Path_Pos := Upper_Bound;
1111 return new String'(Search_Path.all (Lower_Bound .. Upper_Bound - 1));
1112 end loop;
1113
1114 return null;
1115 end Get_Next_Dir_In_Path;
1116
1117 -------------------------------
1118 -- Get_Next_Dir_In_Path_Init --
1119 -------------------------------
1120
1121 procedure Get_Next_Dir_In_Path_Init (Search_Path : String_Access) is
1122 begin
1123 Search_Path_Pos := Search_Path'First;
1124 end Get_Next_Dir_In_Path_Init;
1125
1126 --------------------------------------
1127 -- Get_Primary_Src_Search_Directory --
1128 --------------------------------------
1129
1130 function Get_Primary_Src_Search_Directory return String_Ptr is
1131 begin
1132 return Src_Search_Directories.Table (Primary_Directory);
1133 end Get_Primary_Src_Search_Directory;
1134
1135 -------------------------
1136 -- Get_RTS_Search_Dir --
1137 -------------------------
1138
1139 function Get_RTS_Search_Dir
1140 (Search_Dir : String;
1141 File_Type : Search_File_Type) return String_Ptr
1142 is
1143 procedure Get_Current_Dir
1144 (Dir : System.Address;
1145 Length : System.Address);
1146 pragma Import (C, Get_Current_Dir, "__gnat_get_current_dir");
1147
1148 Max_Path : Integer;
1149 pragma Import (C, Max_Path, "__gnat_max_path_len");
1150 -- Maximum length of a path name
1151
1152 Current_Dir : String_Ptr;
1153 Default_Search_Dir : String_Access;
1154 Default_Suffix_Dir : String_Access;
1155 Local_Search_Dir : String_Access;
1156 Norm_Search_Dir : String_Access;
1157 Result_Search_Dir : String_Access;
1158 Search_File : String_Access;
1159 Temp_String : String_Ptr;
1160
1161 begin
1162 -- Add a directory separator at the end of the directory if necessary
1163 -- so that we can directly append a file to the directory
1164
1165 if Search_Dir (Search_Dir'Last) /= Directory_Separator then
1166 Local_Search_Dir := new String'
1167 (Concat (Search_Dir, String'(1 => Directory_Separator)));
1168 else
1169 Local_Search_Dir := new String'(Search_Dir);
1170 end if;
1171
1172 if File_Type = Include then
1173 Search_File := Include_Search_File;
1174 Default_Suffix_Dir := new String'("adainclude");
1175 else
1176 Search_File := Objects_Search_File;
1177 Default_Suffix_Dir := new String'("adalib");
1178 end if;
1179
1180 Norm_Search_Dir := To_Canonical_Path_Spec (Local_Search_Dir.all);
1181
1182 if Is_Absolute_Path (Norm_Search_Dir.all) then
1183
1184 -- We first verify if there is a directory Include_Search_Dir
1185 -- containing default search directories
1186
1187 Result_Search_Dir
1188 := Read_Default_Search_Dirs (Norm_Search_Dir,
1189 Search_File,
1190 null);
1191 Default_Search_Dir := new String'
1192 (Concat (Norm_Search_Dir.all, Default_Suffix_Dir.all));
1193 Free (Norm_Search_Dir);
1194
1195 if Result_Search_Dir /= null then
1196 return String_Ptr (Result_Search_Dir);
1197 elsif Is_Directory (Default_Search_Dir.all) then
1198 return String_Ptr (Default_Search_Dir);
1199 else
1200 return null;
1201 end if;
1202
1203 else
1204 -- Search in the current directory
1205
1206 -- Get the current directory
1207
1208 declare
1209 Buffer : String (1 .. Max_Path + 2);
1210 Path_Len : Natural := Max_Path;
1211
1212 begin
1213 Get_Current_Dir (Buffer'Address, Path_Len'Address);
1214
1215 if Buffer (Path_Len) /= Directory_Separator then
1216 Path_Len := Path_Len + 1;
1217 Buffer (Path_Len) := Directory_Separator;
1218 end if;
1219
1220 Current_Dir := new String'(Buffer (1 .. Path_Len));
1221 end;
1222
1223 Norm_Search_Dir :=
1224 new String'(Concat (Current_Dir.all, Local_Search_Dir.all));
1225
1226 Result_Search_Dir :=
1227 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1228
1229 Default_Search_Dir :=
1230 new String'
1231 (Concat (Norm_Search_Dir.all, Default_Suffix_Dir.all));
1232
1233 Free (Norm_Search_Dir);
1234
1235 if Result_Search_Dir /= null then
1236 return String_Ptr (Result_Search_Dir);
1237
1238 elsif Is_Directory (Default_Search_Dir.all) then
1239 return String_Ptr (Default_Search_Dir);
1240
1241 else
1242 -- Search in Search_Dir_Prefix/Search_Dir
1243
1244 Norm_Search_Dir :=
1245 new String'
1246 (Concat (Update_Path (Search_Dir_Prefix).all,
1247 Local_Search_Dir.all));
1248
1249 Result_Search_Dir :=
1250 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1251
1252 Default_Search_Dir :=
1253 new String'
1254 (Concat (Norm_Search_Dir.all, Default_Suffix_Dir.all));
1255
1256 Free (Norm_Search_Dir);
1257
1258 if Result_Search_Dir /= null then
1259 return String_Ptr (Result_Search_Dir);
1260
1261 elsif Is_Directory (Default_Search_Dir.all) then
1262 return String_Ptr (Default_Search_Dir);
1263
1264 else
1265 -- We finally search in Search_Dir_Prefix/rts-Search_Dir
1266
1267 Temp_String :=
1268 new String'
1269 (Concat (Update_Path (Search_Dir_Prefix).all, "rts-"));
1270
1271 Norm_Search_Dir :=
1272 new String'(Concat (Temp_String.all, Local_Search_Dir.all));
1273
1274 Result_Search_Dir :=
1275 Read_Default_Search_Dirs (Norm_Search_Dir, Search_File, null);
1276
1277 Default_Search_Dir :=
1278 new String'
1279 (Concat (Norm_Search_Dir.all, Default_Suffix_Dir.all));
1280 Free (Norm_Search_Dir);
1281
1282 if Result_Search_Dir /= null then
1283 return String_Ptr (Result_Search_Dir);
1284
1285 elsif Is_Directory (Default_Search_Dir.all) then
1286 return String_Ptr (Default_Search_Dir);
1287
1288 else
1289 return null;
1290 end if;
1291 end if;
1292 end if;
1293 end if;
1294 end Get_RTS_Search_Dir;
1295
1296 --------------------------------
1297 -- Include_Dir_Default_Prefix --
1298 --------------------------------
1299
1300 function Include_Dir_Default_Prefix return String is
1301 Include_Dir : String_Access :=
1302 String_Access (Update_Path (Include_Dir_Default_Name));
1303
1304 begin
1305 if Include_Dir = null then
1306 return "";
1307
1308 else
1309 declare
1310 Result : constant String := Include_Dir.all;
1311 begin
1312 Free (Include_Dir);
1313 return Result;
1314 end;
1315 end if;
1316 end Include_Dir_Default_Prefix;
1317
1318 ----------------
1319 -- Initialize --
1320 ----------------
1321
1322 procedure Initialize is
1323 begin
1324 Number_File_Names := 0;
1325 Current_File_Name_Index := 0;
1326
1327 Src_Search_Directories.Init;
1328 Lib_Search_Directories.Init;
1329
1330 -- Start off by setting all suppress options to False, these will
1331 -- be reset later (turning some on if -gnato is not specified, and
1332 -- turning all of them on if -gnatp is specified).
1333
1334 Suppress_Options := (others => False);
1335
1336 -- Reserve the first slot in the search paths table. This is the
1337 -- directory of the main source file or main library file and is
1338 -- filled in by each call to Next_Main_Source/Next_Main_Lib_File with
1339 -- the directory specified for this main source or library file. This
1340 -- is the directory which is searched first by default. This default
1341 -- search is inhibited by the option -I- for both source and library
1342 -- files.
1343
1344 Src_Search_Directories.Set_Last (Primary_Directory);
1345 Src_Search_Directories.Table (Primary_Directory) := new String'("");
1346
1347 Lib_Search_Directories.Set_Last (Primary_Directory);
1348 Lib_Search_Directories.Table (Primary_Directory) := new String'("");
1349 end Initialize;
1350
1351 ----------------------------
1352 -- Is_Directory_Separator --
1353 ----------------------------
1354
1355 function Is_Directory_Separator (C : Character) return Boolean is
1356 begin
1357 -- In addition to the default directory_separator allow the '/' to
1358 -- act as separator since this is allowed in MS-DOS, Windows 95/NT,
1359 -- and OS2 ports. On VMS, the situation is more complicated because
1360 -- there are two characters to check for.
1361
1362 return
1363 C = Directory_Separator
1364 or else C = '/'
1365 or else (Hostparm.OpenVMS
1366 and then (C = ']' or else C = ':'));
1367 end Is_Directory_Separator;
1368
1369 -------------------------
1370 -- Is_Readonly_Library --
1371 -------------------------
1372
1373 function Is_Readonly_Library (File : in File_Name_Type) return Boolean is
1374 begin
1375 Get_Name_String (File);
1376
1377 pragma Assert (Name_Buffer (Name_Len - 3 .. Name_Len) = ".ali");
1378
1379 return not Is_Writable_File (Name_Buffer (1 .. Name_Len));
1380 end Is_Readonly_Library;
1381
1382 -------------------
1383 -- Lib_File_Name --
1384 -------------------
1385
1386 function Lib_File_Name
1387 (Source_File : File_Name_Type)
1388 return File_Name_Type
1389 is
1390 Fptr : Natural;
1391 -- Pointer to location to set extension in place
1392
1393 begin
1394 Get_Name_String (Source_File);
1395 Fptr := Name_Len + 1;
1396
1397 for J in reverse 2 .. Name_Len loop
1398 if Name_Buffer (J) = '.' then
1399 Fptr := J;
1400 exit;
1401 end if;
1402 end loop;
1403
1404 Name_Buffer (Fptr) := '.';
1405 Name_Buffer (Fptr + 1 .. Fptr + ALI_Suffix'Length) := ALI_Suffix.all;
1406 Name_Buffer (Fptr + ALI_Suffix'Length + 1) := ASCII.NUL;
1407 Name_Len := Fptr + ALI_Suffix'Length;
1408 return Name_Find;
1409 end Lib_File_Name;
1410
1411 ------------------------
1412 -- Library_File_Stamp --
1413 ------------------------
1414
1415 function Library_File_Stamp (N : File_Name_Type) return Time_Stamp_Type is
1416 begin
1417 return File_Stamp (Find_File (N, Library));
1418 end Library_File_Stamp;
1419
1420 -----------------
1421 -- Locate_File --
1422 -----------------
1423
1424 function Locate_File
1425 (N : File_Name_Type;
1426 T : File_Type;
1427 Dir : Natural;
1428 Name : String) return File_Name_Type
1429 is
1430 Dir_Name : String_Ptr;
1431
1432 begin
1433 if T = Library then
1434 Dir_Name := Lib_Search_Directories.Table (Dir);
1435
1436 else pragma Assert (T /= Config);
1437 Dir_Name := Src_Search_Directories.Table (Dir);
1438 end if;
1439
1440 declare
1441 Full_Name : String (1 .. Dir_Name'Length + Name'Length);
1442
1443 begin
1444 Full_Name (1 .. Dir_Name'Length) := Dir_Name.all;
1445 Full_Name (Dir_Name'Length + 1 .. Full_Name'Length) := Name;
1446
1447 if not Is_Regular_File (Full_Name) then
1448 return No_File;
1449
1450 else
1451 -- If the file is in the current directory then return N itself
1452
1453 if Dir_Name'Length = 0 then
1454 return N;
1455 else
1456 Name_Len := Full_Name'Length;
1457 Name_Buffer (1 .. Name_Len) := Full_Name;
1458 return Name_Enter;
1459 end if;
1460 end if;
1461 end;
1462 end Locate_File;
1463
1464 -------------------------------
1465 -- Matching_Full_Source_Name --
1466 -------------------------------
1467
1468 function Matching_Full_Source_Name
1469 (N : File_Name_Type;
1470 T : Time_Stamp_Type) return File_Name_Type
1471 is
1472 begin
1473 Get_Name_String (N);
1474
1475 declare
1476 File_Name : constant String := Name_Buffer (1 .. Name_Len);
1477 File : File_Name_Type := No_File;
1478 Last_Dir : Natural;
1479
1480 begin
1481 if Opt.Look_In_Primary_Dir then
1482 File := Locate_File (N, Source, Primary_Directory, File_Name);
1483
1484 if File /= No_File and then T = File_Stamp (N) then
1485 return File;
1486 end if;
1487 end if;
1488
1489 Last_Dir := Src_Search_Directories.Last;
1490
1491 for D in Primary_Directory + 1 .. Last_Dir loop
1492 File := Locate_File (N, Source, D, File_Name);
1493
1494 if File /= No_File and then T = File_Stamp (File) then
1495 return File;
1496 end if;
1497 end loop;
1498
1499 return No_File;
1500 end;
1501 end Matching_Full_Source_Name;
1502
1503 ----------------
1504 -- More_Files --
1505 ----------------
1506
1507 function More_Files return Boolean is
1508 begin
1509 return (Current_File_Name_Index < Number_File_Names);
1510 end More_Files;
1511
1512 -------------------------------
1513 -- Nb_Dir_In_Obj_Search_Path --
1514 -------------------------------
1515
1516 function Nb_Dir_In_Obj_Search_Path return Natural is
1517 begin
1518 if Opt.Look_In_Primary_Dir then
1519 return Lib_Search_Directories.Last - Primary_Directory + 1;
1520 else
1521 return Lib_Search_Directories.Last - Primary_Directory;
1522 end if;
1523 end Nb_Dir_In_Obj_Search_Path;
1524
1525 -------------------------------
1526 -- Nb_Dir_In_Src_Search_Path --
1527 -------------------------------
1528
1529 function Nb_Dir_In_Src_Search_Path return Natural is
1530 begin
1531 if Opt.Look_In_Primary_Dir then
1532 return Src_Search_Directories.Last - Primary_Directory + 1;
1533 else
1534 return Src_Search_Directories.Last - Primary_Directory;
1535 end if;
1536 end Nb_Dir_In_Src_Search_Path;
1537
1538 --------------------
1539 -- Next_Main_File --
1540 --------------------
1541
1542 function Next_Main_File return File_Name_Type is
1543 File_Name : String_Ptr;
1544 Dir_Name : String_Ptr;
1545 Fptr : Natural;
1546
1547 begin
1548 pragma Assert (More_Files);
1549
1550 Current_File_Name_Index := Current_File_Name_Index + 1;
1551
1552 -- Get the file and directory name
1553
1554 File_Name := File_Names (Current_File_Name_Index);
1555 Fptr := File_Name'First;
1556
1557 for J in reverse File_Name'Range loop
1558 if File_Name (J) = Directory_Separator
1559 or else File_Name (J) = '/'
1560 then
1561 if J = File_Name'Last then
1562 Fail ("File name missing");
1563 end if;
1564
1565 Fptr := J + 1;
1566 exit;
1567 end if;
1568 end loop;
1569
1570 -- Save name of directory in which main unit resides for use in
1571 -- locating other units
1572
1573 Dir_Name := new String'(File_Name (File_Name'First .. Fptr - 1));
1574
1575 case Running_Program is
1576
1577 when Compiler =>
1578 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
1579 Look_In_Primary_Directory_For_Current_Main := True;
1580
1581 when Make =>
1582 Src_Search_Directories.Table (Primary_Directory) := Dir_Name;
1583
1584 if Fptr > File_Name'First then
1585 Look_In_Primary_Directory_For_Current_Main := True;
1586 end if;
1587
1588 when Binder | Gnatls =>
1589 Dir_Name := Normalize_Directory_Name (Dir_Name.all);
1590 Lib_Search_Directories.Table (Primary_Directory) := Dir_Name;
1591
1592 when Unspecified =>
1593 null;
1594 end case;
1595
1596 Name_Len := File_Name'Last - Fptr + 1;
1597 Name_Buffer (1 .. Name_Len) := File_Name (Fptr .. File_Name'Last);
1598 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1599 Current_Main := File_Name_Type (Name_Find);
1600
1601 -- In the gnatmake case, the main file may have not have the
1602 -- extension. Try ".adb" first then ".ads"
1603
1604 if Running_Program = Make then
1605 declare
1606 Orig_Main : constant File_Name_Type := Current_Main;
1607
1608 begin
1609 if Strip_Suffix (Orig_Main) = Orig_Main then
1610 Current_Main := Append_Suffix_To_File_Name (Orig_Main, ".adb");
1611
1612 if Full_Source_Name (Current_Main) = No_File then
1613 Current_Main :=
1614 Append_Suffix_To_File_Name (Orig_Main, ".ads");
1615
1616 if Full_Source_Name (Current_Main) = No_File then
1617 Current_Main := Orig_Main;
1618 end if;
1619 end if;
1620 end if;
1621 end;
1622 end if;
1623
1624 return Current_Main;
1625 end Next_Main_File;
1626
1627 ------------------------------
1628 -- Normalize_Directory_Name --
1629 ------------------------------
1630
1631 function Normalize_Directory_Name (Directory : String) return String_Ptr is
1632
1633 function Is_Quoted (Path : String) return Boolean;
1634 pragma Inline (Is_Quoted);
1635 -- Returns true if Path is quoted (either double or single quotes)
1636
1637 ---------------
1638 -- Is_Quoted --
1639 ---------------
1640
1641 function Is_Quoted (Path : String) return Boolean is
1642 First : constant Character := Path (Path'First);
1643 Last : constant Character := Path (Path'Last);
1644
1645 begin
1646 if (First = ''' and then Last = ''')
1647 or else
1648 (First = '"' and then Last = '"')
1649 then
1650 return True;
1651 else
1652 return False;
1653 end if;
1654 end Is_Quoted;
1655
1656 Result : String_Ptr;
1657
1658 -- Start of processing for Normalize_Directory_Name
1659
1660 begin
1661 if Directory'Length = 0 then
1662 Result := new String'(Hostparm.Normalized_CWD);
1663
1664 elsif Is_Directory_Separator (Directory (Directory'Last)) then
1665 Result := new String'(Directory);
1666
1667 elsif Is_Quoted (Directory) then
1668
1669 -- This is a quoted string, it certainly means that the directory
1670 -- contains some spaces for example. We can safely remove the quotes
1671 -- here as the OS_Lib.Normalize_Arguments will be called before any
1672 -- spawn routines. This ensure that quotes will be added when needed.
1673
1674 Result := new String (1 .. Directory'Length - 1);
1675 Result (1 .. Directory'Length - 1) :=
1676 Directory (Directory'First + 1 .. Directory'Last - 1);
1677 Result (Result'Last) := Directory_Separator;
1678
1679 else
1680 Result := new String (1 .. Directory'Length + 1);
1681 Result (1 .. Directory'Length) := Directory;
1682 Result (Directory'Length + 1) := Directory_Separator;
1683 end if;
1684
1685 return Result;
1686 end Normalize_Directory_Name;
1687
1688 ---------------------
1689 -- Number_Of_Files --
1690 ---------------------
1691
1692 function Number_Of_Files return Int is
1693 begin
1694 return Number_File_Names;
1695 end Number_Of_Files;
1696
1697 -------------------------------
1698 -- Object_Dir_Default_Prefix --
1699 -------------------------------
1700
1701 function Object_Dir_Default_Prefix return String is
1702 Object_Dir : String_Access :=
1703 String_Access (Update_Path (Object_Dir_Default_Name));
1704
1705 begin
1706 if Object_Dir = null then
1707 return "";
1708
1709 else
1710 declare
1711 Result : constant String := Object_Dir.all;
1712 begin
1713 Free (Object_Dir);
1714 return Result;
1715 end;
1716 end if;
1717 end Object_Dir_Default_Prefix;
1718
1719 ----------------------
1720 -- Object_File_Name --
1721 ----------------------
1722
1723 function Object_File_Name (N : File_Name_Type) return File_Name_Type is
1724 begin
1725 if N = No_File then
1726 return No_File;
1727 end if;
1728
1729 Get_Name_String (N);
1730 Name_Len := Name_Len - ALI_Suffix'Length - 1;
1731
1732 for J in Object_Suffix'Range loop
1733 Name_Len := Name_Len + 1;
1734 Name_Buffer (Name_Len) := Object_Suffix (J);
1735 end loop;
1736
1737 return Name_Enter;
1738 end Object_File_Name;
1739
1740 --------------------------
1741 -- OS_Time_To_GNAT_Time --
1742 --------------------------
1743
1744 function OS_Time_To_GNAT_Time (T : OS_Time) return Time_Stamp_Type is
1745 GNAT_Time : Time_Stamp_Type;
1746
1747 Y : Year_Type;
1748 Mo : Month_Type;
1749 D : Day_Type;
1750 H : Hour_Type;
1751 Mn : Minute_Type;
1752 S : Second_Type;
1753
1754 begin
1755 GM_Split (T, Y, Mo, D, H, Mn, S);
1756 Make_Time_Stamp
1757 (Year => Nat (Y),
1758 Month => Nat (Mo),
1759 Day => Nat (D),
1760 Hour => Nat (H),
1761 Minutes => Nat (Mn),
1762 Seconds => Nat (S),
1763 TS => GNAT_Time);
1764
1765 return GNAT_Time;
1766 end OS_Time_To_GNAT_Time;
1767
1768 ------------------
1769 -- Program_Name --
1770 ------------------
1771
1772 function Program_Name (Nam : String) return String_Access is
1773 Res : String_Access;
1774
1775 begin
1776 -- Get the name of the current program being executed
1777
1778 Find_Program_Name;
1779
1780 -- Find the target prefix if any, for the cross compilation case
1781 -- for instance in "alpha-dec-vxworks-gcc" the target prefix is
1782 -- "alpha-dec-vxworks-"
1783
1784 while Name_Len > 0 loop
1785 if Name_Buffer (Name_Len) = '-' then
1786 exit;
1787 end if;
1788
1789 Name_Len := Name_Len - 1;
1790 end loop;
1791
1792 -- Create the new program name
1793
1794 Res := new String (1 .. Name_Len + Nam'Length);
1795 Res.all (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
1796 Res.all (Name_Len + 1 .. Name_Len + Nam'Length) := Nam;
1797 return Res;
1798 end Program_Name;
1799
1800 ------------------------------
1801 -- Read_Default_Search_Dirs --
1802 ------------------------------
1803
1804 function Read_Default_Search_Dirs
1805 (Search_Dir_Prefix : String_Access;
1806 Search_File : String_Access;
1807 Search_Dir_Default_Name : String_Access) return String_Access
1808 is
1809 Prefix_Len : constant Integer := Search_Dir_Prefix.all'Length;
1810 Buffer : String (1 .. Prefix_Len + Search_File.all'Length + 1);
1811 File_FD : File_Descriptor;
1812 S, S1 : String_Access;
1813 Len : Integer;
1814 Curr : Integer;
1815 Actual_Len : Integer;
1816 J1 : Integer;
1817
1818 Prev_Was_Separator : Boolean;
1819 Nb_Relative_Dir : Integer;
1820
1821 function Is_Relative (S : String; K : Positive) return Boolean;
1822 pragma Inline (Is_Relative);
1823 -- Returns True if a relative directory specification is found
1824 -- in S at position K, False otherwise.
1825
1826 -----------------
1827 -- Is_Relative --
1828 -----------------
1829
1830 function Is_Relative (S : String; K : Positive) return Boolean is
1831 begin
1832 return not Is_Absolute_Path (S (K .. S'Last));
1833 end Is_Relative;
1834
1835 -- Start of processing for Read_Default_Search_Dirs
1836
1837 begin
1838 -- Construct a C compatible character string buffer.
1839
1840 Buffer (1 .. Search_Dir_Prefix.all'Length)
1841 := Search_Dir_Prefix.all;
1842 Buffer (Search_Dir_Prefix.all'Length + 1 .. Buffer'Last - 1)
1843 := Search_File.all;
1844 Buffer (Buffer'Last) := ASCII.NUL;
1845
1846 File_FD := Open_Read (Buffer'Address, Binary);
1847 if File_FD = Invalid_FD then
1848 return Search_Dir_Default_Name;
1849 end if;
1850
1851 Len := Integer (File_Length (File_FD));
1852
1853 -- An extra character for a trailing Path_Separator is allocated
1854
1855 S := new String (1 .. Len + 1);
1856 S (Len + 1) := Path_Separator;
1857
1858 -- Read the file. Note that the loop is not necessary since the
1859 -- whole file is read at once except on VMS.
1860
1861 Curr := 1;
1862 Actual_Len := Len;
1863 while Actual_Len /= 0 loop
1864 Actual_Len := Read (File_FD, S (Curr)'Address, Len);
1865 Curr := Curr + Actual_Len;
1866 end loop;
1867
1868 -- Process the file, translating line and file ending
1869 -- control characters to a path separator character.
1870
1871 Prev_Was_Separator := True;
1872 Nb_Relative_Dir := 0;
1873 for J in 1 .. Len loop
1874 if S (J) in ASCII.NUL .. ASCII.US
1875 or else S (J) = ' '
1876 then
1877 S (J) := Path_Separator;
1878 end if;
1879
1880 if S (J) = Path_Separator then
1881 Prev_Was_Separator := True;
1882 else
1883 if Prev_Was_Separator and then Is_Relative (S.all, J) then
1884 Nb_Relative_Dir := Nb_Relative_Dir + 1;
1885 end if;
1886
1887 Prev_Was_Separator := False;
1888 end if;
1889 end loop;
1890
1891 if Nb_Relative_Dir = 0 then
1892 return S;
1893 end if;
1894
1895 -- Add the Search_Dir_Prefix to all relative paths
1896
1897 S1 := new String (1 .. S'Length + Nb_Relative_Dir * Prefix_Len);
1898 J1 := 1;
1899 Prev_Was_Separator := True;
1900 for J in 1 .. Len + 1 loop
1901 if S (J) = Path_Separator then
1902 Prev_Was_Separator := True;
1903
1904 else
1905 if Prev_Was_Separator and then Is_Relative (S.all, J) then
1906 S1 (J1 .. J1 + Prefix_Len - 1) := Search_Dir_Prefix.all;
1907 J1 := J1 + Prefix_Len;
1908 end if;
1909
1910 Prev_Was_Separator := False;
1911 end if;
1912 S1 (J1) := S (J);
1913 J1 := J1 + 1;
1914 end loop;
1915
1916 Free (S);
1917 return S1;
1918 end Read_Default_Search_Dirs;
1919
1920 -----------------------
1921 -- Read_Library_Info --
1922 -----------------------
1923
1924 function Read_Library_Info
1925 (Lib_File : File_Name_Type;
1926 Fatal_Err : Boolean := False) return Text_Buffer_Ptr
1927 is
1928 Lib_FD : File_Descriptor;
1929 -- The file descriptor for the current library file. A negative value
1930 -- indicates failure to open the specified source file.
1931
1932 Text : Text_Buffer_Ptr;
1933 -- Allocated text buffer.
1934
1935 Status : Boolean;
1936 -- For the calls to Close
1937
1938 begin
1939 Current_Full_Lib_Name := Find_File (Lib_File, Library);
1940 Current_Full_Obj_Name := Object_File_Name (Current_Full_Lib_Name);
1941
1942 if Current_Full_Lib_Name = No_File then
1943 if Fatal_Err then
1944 Fail ("Cannot find: ", Name_Buffer (1 .. Name_Len));
1945 else
1946 Current_Full_Obj_Stamp := Empty_Time_Stamp;
1947 return null;
1948 end if;
1949 end if;
1950
1951 Get_Name_String (Current_Full_Lib_Name);
1952 Name_Buffer (Name_Len + 1) := ASCII.NUL;
1953
1954 -- Open the library FD, note that we open in binary mode, because as
1955 -- documented in the spec, the caller is expected to handle either
1956 -- DOS or Unix mode files, and there is no point in wasting time on
1957 -- text translation when it is not required.
1958
1959 Lib_FD := Open_Read (Name_Buffer'Address, Binary);
1960
1961 if Lib_FD = Invalid_FD then
1962 if Fatal_Err then
1963 Fail ("Cannot open: ", Name_Buffer (1 .. Name_Len));
1964 else
1965 Current_Full_Obj_Stamp := Empty_Time_Stamp;
1966 return null;
1967 end if;
1968 end if;
1969
1970 -- Check for object file consistency if requested
1971
1972 if Opt.Check_Object_Consistency then
1973 Current_Full_Lib_Stamp := File_Stamp (Current_Full_Lib_Name);
1974 Current_Full_Obj_Stamp := File_Stamp (Current_Full_Obj_Name);
1975
1976 if Current_Full_Obj_Stamp (1) = ' ' then
1977
1978 -- When the library is readonly, always assume that
1979 -- the object is consistent.
1980
1981 if Is_Readonly_Library (Current_Full_Lib_Name) then
1982 Current_Full_Obj_Stamp := Current_Full_Lib_Stamp;
1983
1984 elsif Fatal_Err then
1985 Get_Name_String (Current_Full_Obj_Name);
1986 Close (Lib_FD, Status);
1987 -- No need to check the status, we fail anyway
1988
1989 Fail ("Cannot find: ", Name_Buffer (1 .. Name_Len));
1990
1991 else
1992 Current_Full_Obj_Stamp := Empty_Time_Stamp;
1993 Close (Lib_FD, Status);
1994 -- No need to check the status, we return null anyway
1995
1996 return null;
1997 end if;
1998 end if;
1999
2000 -- Object file exists, compare object and ALI time stamps
2001
2002 if Current_Full_Lib_Stamp > Current_Full_Obj_Stamp then
2003 if Fatal_Err then
2004 Get_Name_String (Current_Full_Obj_Name);
2005 Close (Lib_FD, Status);
2006 -- No need to check the status, we fail anyway
2007 Fail ("Bad time stamp: ", Name_Buffer (1 .. Name_Len));
2008 else
2009 Current_Full_Obj_Stamp := Empty_Time_Stamp;
2010 Close (Lib_FD, Status);
2011 -- No need to check the status, we return null anyway
2012
2013 return null;
2014 end if;
2015 end if;
2016 end if;
2017
2018 -- Read data from the file
2019
2020 declare
2021 Len : constant Integer := Integer (File_Length (Lib_FD));
2022 -- Length of source file text. If it doesn't fit in an integer
2023 -- we're probably stuck anyway (>2 gigs of source seems a lot!)
2024
2025 Actual_Len : Integer := 0;
2026
2027 Lo : constant Text_Ptr := 0;
2028 -- Low bound for allocated text buffer
2029
2030 Hi : Text_Ptr := Text_Ptr (Len);
2031 -- High bound for allocated text buffer. Note length is Len + 1
2032 -- which allows for extra EOF character at the end of the buffer.
2033
2034 begin
2035 -- Allocate text buffer. Note extra character at end for EOF
2036
2037 Text := new Text_Buffer (Lo .. Hi);
2038
2039 -- Some systems (e.g. VMS) have file types that require one
2040 -- read per line, so read until we get the Len bytes or until
2041 -- there are no more characters.
2042
2043 Hi := Lo;
2044 loop
2045 Actual_Len := Read (Lib_FD, Text (Hi)'Address, Len);
2046 Hi := Hi + Text_Ptr (Actual_Len);
2047 exit when Actual_Len = Len or Actual_Len <= 0;
2048 end loop;
2049
2050 Text (Hi) := EOF;
2051 end;
2052
2053 -- Read is complete, close file and we are done
2054
2055 Close (Lib_FD, Status);
2056 -- The status should never be False. But, if it is, what can we do?
2057 -- So, we don't test it.
2058
2059 return Text;
2060
2061 end Read_Library_Info;
2062
2063 ----------------------
2064 -- Read_Source_File --
2065 ----------------------
2066
2067 procedure Read_Source_File
2068 (N : File_Name_Type;
2069 Lo : Source_Ptr;
2070 Hi : out Source_Ptr;
2071 Src : out Source_Buffer_Ptr;
2072 T : File_Type := Source)
2073 is
2074 Source_File_FD : File_Descriptor;
2075 -- The file descriptor for the current source file. A negative value
2076 -- indicates failure to open the specified source file.
2077
2078 Len : Integer;
2079 -- Length of file. Assume no more than 2 gigabytes of source!
2080
2081 Actual_Len : Integer;
2082
2083 Status : Boolean;
2084 -- For the call to Close
2085
2086 begin
2087 Current_Full_Source_Name := Find_File (N, T);
2088 Current_Full_Source_Stamp := File_Stamp (Current_Full_Source_Name);
2089
2090 if Current_Full_Source_Name = No_File then
2091
2092 -- If we were trying to access the main file and we could not
2093 -- find it we have an error.
2094
2095 if N = Current_Main then
2096 Get_Name_String (N);
2097 Fail ("Cannot find: ", Name_Buffer (1 .. Name_Len));
2098 end if;
2099
2100 Src := null;
2101 Hi := No_Location;
2102 return;
2103 end if;
2104
2105 Get_Name_String (Current_Full_Source_Name);
2106 Name_Buffer (Name_Len + 1) := ASCII.NUL;
2107
2108 -- Open the source FD, note that we open in binary mode, because as
2109 -- documented in the spec, the caller is expected to handle either
2110 -- DOS or Unix mode files, and there is no point in wasting time on
2111 -- text translation when it is not required.
2112
2113 Source_File_FD := Open_Read (Name_Buffer'Address, Binary);
2114
2115 if Source_File_FD = Invalid_FD then
2116 Src := null;
2117 Hi := No_Location;
2118 return;
2119 end if;
2120
2121 -- Prepare to read data from the file
2122
2123 Len := Integer (File_Length (Source_File_FD));
2124
2125 -- Set Hi so that length is one more than the physical length,
2126 -- allowing for the extra EOF character at the end of the buffer
2127
2128 Hi := Lo + Source_Ptr (Len);
2129
2130 -- Do the actual read operation
2131
2132 declare
2133 subtype Actual_Source_Buffer is Source_Buffer (Lo .. Hi);
2134 -- Physical buffer allocated
2135
2136 type Actual_Source_Ptr is access Actual_Source_Buffer;
2137 -- This is the pointer type for the physical buffer allocated
2138
2139 Actual_Ptr : Actual_Source_Ptr := new Actual_Source_Buffer;
2140 -- And this is the actual physical buffer
2141
2142 begin
2143 -- Allocate source buffer, allowing extra character at end for EOF
2144
2145 -- Some systems (e.g. VMS) have file types that require one
2146 -- read per line, so read until we get the Len bytes or until
2147 -- there are no more characters.
2148
2149 Hi := Lo;
2150 loop
2151 Actual_Len := Read (Source_File_FD, Actual_Ptr (Hi)'Address, Len);
2152 Hi := Hi + Source_Ptr (Actual_Len);
2153 exit when Actual_Len = Len or Actual_Len <= 0;
2154 end loop;
2155
2156 Actual_Ptr (Hi) := EOF;
2157
2158 -- Now we need to work out the proper virtual origin pointer to
2159 -- return. This is exactly Actual_Ptr (0)'Address, but we have
2160 -- to be careful to suppress checks to compute this address.
2161
2162 declare
2163 pragma Suppress (All_Checks);
2164
2165 function To_Source_Buffer_Ptr is new
2166 Unchecked_Conversion (Address, Source_Buffer_Ptr);
2167
2168 begin
2169 Src := To_Source_Buffer_Ptr (Actual_Ptr (0)'Address);
2170 end;
2171 end;
2172
2173 -- Read is complete, get time stamp and close file and we are done
2174
2175 Close (Source_File_FD, Status);
2176 -- The status should never be False. But, if it is, what can we do?
2177 -- So, we don't test it.
2178
2179 end Read_Source_File;
2180
2181 -------------------
2182 -- Relocate_Path --
2183 -------------------
2184
2185 function Relocate_Path
2186 (Prefix : String;
2187 Path : String) return String_Ptr
2188 is
2189 S : String_Ptr;
2190
2191 procedure set_std_prefix (S : String; Len : Integer);
2192 pragma Import (C, set_std_prefix);
2193
2194 begin
2195 if Std_Prefix = null then
2196 Std_Prefix := Executable_Prefix;
2197
2198 if Std_Prefix.all /= "" then
2199 -- Remove trailing directory separator when calling set_std_prefix
2200
2201 set_std_prefix (Std_Prefix.all, Std_Prefix'Length - 1);
2202 end if;
2203 end if;
2204
2205 if Path (Prefix'Range) = Prefix then
2206 if Std_Prefix.all /= "" then
2207 S := new String
2208 (1 .. Std_Prefix'Length + Path'Last - Prefix'Last);
2209 S (1 .. Std_Prefix'Length) := Std_Prefix.all;
2210 S (Std_Prefix'Length + 1 .. S'Last) :=
2211 Path (Prefix'Last + 1 .. Path'Last);
2212 return S;
2213 end if;
2214 end if;
2215
2216 return new String'(Path);
2217 end Relocate_Path;
2218
2219 -----------------
2220 -- Set_Program --
2221 -----------------
2222
2223 procedure Set_Program (P : Program_Type) is
2224 begin
2225 if Program_Set then
2226 Fail ("Set_Program called twice");
2227 end if;
2228
2229 Program_Set := True;
2230 Running_Program := P;
2231 end Set_Program;
2232
2233 ----------------------
2234 -- Smart_File_Stamp --
2235 ----------------------
2236
2237 function Smart_File_Stamp
2238 (N : File_Name_Type;
2239 T : File_Type) return Time_Stamp_Type
2240 is
2241 Time_Stamp : Time_Stamp_Type;
2242
2243 begin
2244 if not File_Cache_Enabled then
2245 return File_Stamp (Find_File (N, T));
2246 end if;
2247
2248 Time_Stamp := File_Stamp_Hash_Table.Get (N);
2249
2250 if Time_Stamp (1) = ' ' then
2251 Time_Stamp := File_Stamp (Smart_Find_File (N, T));
2252 File_Stamp_Hash_Table.Set (N, Time_Stamp);
2253 end if;
2254
2255 return Time_Stamp;
2256 end Smart_File_Stamp;
2257
2258 ---------------------
2259 -- Smart_Find_File --
2260 ---------------------
2261
2262 function Smart_Find_File
2263 (N : File_Name_Type;
2264 T : File_Type) return File_Name_Type
2265 is
2266 Full_File_Name : File_Name_Type;
2267
2268 begin
2269 if not File_Cache_Enabled then
2270 return Find_File (N, T);
2271 end if;
2272
2273 Full_File_Name := File_Name_Hash_Table.Get (N);
2274
2275 if Full_File_Name = No_File then
2276 Full_File_Name := Find_File (N, T);
2277 File_Name_Hash_Table.Set (N, Full_File_Name);
2278 end if;
2279
2280 return Full_File_Name;
2281 end Smart_Find_File;
2282
2283 ----------------------
2284 -- Source_File_Data --
2285 ----------------------
2286
2287 procedure Source_File_Data (Cache : Boolean) is
2288 begin
2289 File_Cache_Enabled := Cache;
2290 end Source_File_Data;
2291
2292 -----------------------
2293 -- Source_File_Stamp --
2294 -----------------------
2295
2296 function Source_File_Stamp (N : File_Name_Type) return Time_Stamp_Type is
2297 begin
2298 return Smart_File_Stamp (N, Source);
2299 end Source_File_Stamp;
2300
2301 ---------------------
2302 -- Strip_Directory --
2303 ---------------------
2304
2305 function Strip_Directory (Name : File_Name_Type) return File_Name_Type is
2306 begin
2307 Get_Name_String (Name);
2308
2309 for J in reverse 1 .. Name_Len - 1 loop
2310 -- If we find the last directory separator
2311
2312 if Is_Directory_Separator (Name_Buffer (J)) then
2313 -- Return the part of Name that follows this last directory
2314 -- separator.
2315
2316 Name_Buffer (1 .. Name_Len - J) := Name_Buffer (J + 1 .. Name_Len);
2317 Name_Len := Name_Len - J;
2318 return Name_Find;
2319 end if;
2320 end loop;
2321
2322 -- There were no directory separator, just return Name
2323
2324 return Name;
2325 end Strip_Directory;
2326
2327 ------------------
2328 -- Strip_Suffix --
2329 ------------------
2330
2331 function Strip_Suffix (Name : File_Name_Type) return File_Name_Type is
2332 begin
2333 Get_Name_String (Name);
2334
2335 for J in reverse 2 .. Name_Len loop
2336
2337 -- If we found the last '.', return the part of Name that precedes
2338 -- this '.'.
2339
2340 if Name_Buffer (J) = '.' then
2341 Name_Len := J - 1;
2342 return Name_Enter;
2343 end if;
2344 end loop;
2345
2346 return Name;
2347 end Strip_Suffix;
2348
2349 ---------------------------
2350 -- To_Canonical_Dir_Spec --
2351 ---------------------------
2352
2353 function To_Canonical_Dir_Spec
2354 (Host_Dir : String;
2355 Prefix_Style : Boolean) return String_Access
2356 is
2357 function To_Canonical_Dir_Spec
2358 (Host_Dir : Address;
2359 Prefix_Flag : Integer) return Address;
2360 pragma Import (C, To_Canonical_Dir_Spec, "__gnat_to_canonical_dir_spec");
2361
2362 C_Host_Dir : String (1 .. Host_Dir'Length + 1);
2363 Canonical_Dir_Addr : Address;
2364 Canonical_Dir_Len : Integer;
2365
2366 begin
2367 C_Host_Dir (1 .. Host_Dir'Length) := Host_Dir;
2368 C_Host_Dir (C_Host_Dir'Last) := ASCII.NUL;
2369
2370 if Prefix_Style then
2371 Canonical_Dir_Addr := To_Canonical_Dir_Spec (C_Host_Dir'Address, 1);
2372 else
2373 Canonical_Dir_Addr := To_Canonical_Dir_Spec (C_Host_Dir'Address, 0);
2374 end if;
2375 Canonical_Dir_Len := C_String_Length (Canonical_Dir_Addr);
2376
2377 if Canonical_Dir_Len = 0 then
2378 return null;
2379 else
2380 return To_Path_String_Access (Canonical_Dir_Addr, Canonical_Dir_Len);
2381 end if;
2382
2383 exception
2384 when others =>
2385 Fail ("erroneous directory spec: ", Host_Dir);
2386 return null;
2387 end To_Canonical_Dir_Spec;
2388
2389 ---------------------------
2390 -- To_Canonical_File_List --
2391 ---------------------------
2392
2393 function To_Canonical_File_List
2394 (Wildcard_Host_File : String;
2395 Only_Dirs : Boolean) return String_Access_List_Access
2396 is
2397 function To_Canonical_File_List_Init
2398 (Host_File : Address;
2399 Only_Dirs : Integer) return Integer;
2400 pragma Import (C, To_Canonical_File_List_Init,
2401 "__gnat_to_canonical_file_list_init");
2402
2403 function To_Canonical_File_List_Next return Address;
2404 pragma Import (C, To_Canonical_File_List_Next,
2405 "__gnat_to_canonical_file_list_next");
2406
2407 procedure To_Canonical_File_List_Free;
2408 pragma Import (C, To_Canonical_File_List_Free,
2409 "__gnat_to_canonical_file_list_free");
2410
2411 Num_Files : Integer;
2412 C_Wildcard_Host_File : String (1 .. Wildcard_Host_File'Length + 1);
2413
2414 begin
2415 C_Wildcard_Host_File (1 .. Wildcard_Host_File'Length) :=
2416 Wildcard_Host_File;
2417 C_Wildcard_Host_File (C_Wildcard_Host_File'Last) := ASCII.NUL;
2418
2419 -- Do the expansion and say how many there are
2420
2421 Num_Files := To_Canonical_File_List_Init
2422 (C_Wildcard_Host_File'Address, Boolean'Pos (Only_Dirs));
2423
2424 declare
2425 Canonical_File_List : String_Access_List (1 .. Num_Files);
2426 Canonical_File_Addr : Address;
2427 Canonical_File_Len : Integer;
2428
2429 begin
2430 -- Retrieve the expanded directoy names and build the list
2431
2432 for J in 1 .. Num_Files loop
2433 Canonical_File_Addr := To_Canonical_File_List_Next;
2434 Canonical_File_Len := C_String_Length (Canonical_File_Addr);
2435 Canonical_File_List (J) := To_Path_String_Access
2436 (Canonical_File_Addr, Canonical_File_Len);
2437 end loop;
2438
2439 -- Free up the storage
2440
2441 To_Canonical_File_List_Free;
2442
2443 return new String_Access_List'(Canonical_File_List);
2444 end;
2445 end To_Canonical_File_List;
2446
2447 ----------------------------
2448 -- To_Canonical_File_Spec --
2449 ----------------------------
2450
2451 function To_Canonical_File_Spec
2452 (Host_File : String) return String_Access
2453 is
2454 function To_Canonical_File_Spec (Host_File : Address) return Address;
2455 pragma Import
2456 (C, To_Canonical_File_Spec, "__gnat_to_canonical_file_spec");
2457
2458 C_Host_File : String (1 .. Host_File'Length + 1);
2459 Canonical_File_Addr : Address;
2460 Canonical_File_Len : Integer;
2461
2462 begin
2463 C_Host_File (1 .. Host_File'Length) := Host_File;
2464 C_Host_File (C_Host_File'Last) := ASCII.NUL;
2465
2466 Canonical_File_Addr := To_Canonical_File_Spec (C_Host_File'Address);
2467 Canonical_File_Len := C_String_Length (Canonical_File_Addr);
2468
2469 if Canonical_File_Len = 0 then
2470 return null;
2471 else
2472 return To_Path_String_Access
2473 (Canonical_File_Addr, Canonical_File_Len);
2474 end if;
2475
2476 exception
2477 when others =>
2478 Fail ("erroneous file spec: ", Host_File);
2479 return null;
2480 end To_Canonical_File_Spec;
2481
2482 ----------------------------
2483 -- To_Canonical_Path_Spec --
2484 ----------------------------
2485
2486 function To_Canonical_Path_Spec
2487 (Host_Path : String) return String_Access
2488 is
2489 function To_Canonical_Path_Spec (Host_Path : Address) return Address;
2490 pragma Import
2491 (C, To_Canonical_Path_Spec, "__gnat_to_canonical_path_spec");
2492
2493 C_Host_Path : String (1 .. Host_Path'Length + 1);
2494 Canonical_Path_Addr : Address;
2495 Canonical_Path_Len : Integer;
2496
2497 begin
2498 C_Host_Path (1 .. Host_Path'Length) := Host_Path;
2499 C_Host_Path (C_Host_Path'Last) := ASCII.NUL;
2500
2501 Canonical_Path_Addr := To_Canonical_Path_Spec (C_Host_Path'Address);
2502 Canonical_Path_Len := C_String_Length (Canonical_Path_Addr);
2503
2504 -- Return a null string (vice a null) for zero length paths, for
2505 -- compatibility with getenv().
2506
2507 return To_Path_String_Access (Canonical_Path_Addr, Canonical_Path_Len);
2508
2509 exception
2510 when others =>
2511 Fail ("erroneous path spec: ", Host_Path);
2512 return null;
2513 end To_Canonical_Path_Spec;
2514
2515 ---------------------------
2516 -- To_Host_Dir_Spec --
2517 ---------------------------
2518
2519 function To_Host_Dir_Spec
2520 (Canonical_Dir : String;
2521 Prefix_Style : Boolean) return String_Access
2522 is
2523 function To_Host_Dir_Spec
2524 (Canonical_Dir : Address;
2525 Prefix_Flag : Integer) return Address;
2526 pragma Import (C, To_Host_Dir_Spec, "__gnat_to_host_dir_spec");
2527
2528 C_Canonical_Dir : String (1 .. Canonical_Dir'Length + 1);
2529 Host_Dir_Addr : Address;
2530 Host_Dir_Len : Integer;
2531
2532 begin
2533 C_Canonical_Dir (1 .. Canonical_Dir'Length) := Canonical_Dir;
2534 C_Canonical_Dir (C_Canonical_Dir'Last) := ASCII.NUL;
2535
2536 if Prefix_Style then
2537 Host_Dir_Addr := To_Host_Dir_Spec (C_Canonical_Dir'Address, 1);
2538 else
2539 Host_Dir_Addr := To_Host_Dir_Spec (C_Canonical_Dir'Address, 0);
2540 end if;
2541 Host_Dir_Len := C_String_Length (Host_Dir_Addr);
2542
2543 if Host_Dir_Len = 0 then
2544 return null;
2545 else
2546 return To_Path_String_Access (Host_Dir_Addr, Host_Dir_Len);
2547 end if;
2548 end To_Host_Dir_Spec;
2549
2550 ----------------------------
2551 -- To_Host_File_Spec --
2552 ----------------------------
2553
2554 function To_Host_File_Spec
2555 (Canonical_File : String) return String_Access
2556 is
2557 function To_Host_File_Spec (Canonical_File : Address) return Address;
2558 pragma Import (C, To_Host_File_Spec, "__gnat_to_host_file_spec");
2559
2560 C_Canonical_File : String (1 .. Canonical_File'Length + 1);
2561 Host_File_Addr : Address;
2562 Host_File_Len : Integer;
2563
2564 begin
2565 C_Canonical_File (1 .. Canonical_File'Length) := Canonical_File;
2566 C_Canonical_File (C_Canonical_File'Last) := ASCII.NUL;
2567
2568 Host_File_Addr := To_Host_File_Spec (C_Canonical_File'Address);
2569 Host_File_Len := C_String_Length (Host_File_Addr);
2570
2571 if Host_File_Len = 0 then
2572 return null;
2573 else
2574 return To_Path_String_Access
2575 (Host_File_Addr, Host_File_Len);
2576 end if;
2577 end To_Host_File_Spec;
2578
2579 ---------------------------
2580 -- To_Path_String_Access --
2581 ---------------------------
2582
2583 function To_Path_String_Access
2584 (Path_Addr : Address;
2585 Path_Len : Integer) return String_Access
2586 is
2587 subtype Path_String is String (1 .. Path_Len);
2588 type Path_String_Access is access Path_String;
2589
2590 function Address_To_Access is new
2591 Unchecked_Conversion (Source => Address,
2592 Target => Path_String_Access);
2593
2594 Path_Access : constant Path_String_Access :=
2595 Address_To_Access (Path_Addr);
2596
2597 Return_Val : String_Access;
2598
2599 begin
2600 Return_Val := new String (1 .. Path_Len);
2601
2602 for J in 1 .. Path_Len loop
2603 Return_Val (J) := Path_Access (J);
2604 end loop;
2605
2606 return Return_Val;
2607 end To_Path_String_Access;
2608
2609 -----------------
2610 -- Update_Path --
2611 -----------------
2612
2613 function Update_Path (Path : String_Ptr) return String_Ptr is
2614
2615 function C_Update_Path (Path, Component : Address) return Address;
2616 pragma Import (C, C_Update_Path, "update_path");
2617
2618 function Strlen (Str : Address) return Integer;
2619 pragma Import (C, Strlen, "strlen");
2620
2621 procedure Strncpy (X : Address; Y : Address; Length : Integer);
2622 pragma Import (C, Strncpy, "strncpy");
2623
2624 In_Length : constant Integer := Path'Length;
2625 In_String : String (1 .. In_Length + 1);
2626 Component_Name : aliased String := "GCC" & ASCII.NUL;
2627 Result_Ptr : Address;
2628 Result_Length : Integer;
2629 Out_String : String_Ptr;
2630
2631 begin
2632 In_String (1 .. In_Length) := Path.all;
2633 In_String (In_Length + 1) := ASCII.NUL;
2634 Result_Ptr := C_Update_Path (In_String'Address,
2635 Component_Name'Address);
2636 Result_Length := Strlen (Result_Ptr);
2637
2638 Out_String := new String (1 .. Result_Length);
2639 Strncpy (Out_String.all'Address, Result_Ptr, Result_Length);
2640 return Out_String;
2641 end Update_Path;
2642
2643 ----------------
2644 -- Write_Info --
2645 ----------------
2646
2647 procedure Write_Info (Info : String) is
2648 begin
2649 Write_With_Check (Info'Address, Info'Length);
2650 Write_With_Check (EOL'Address, 1);
2651 end Write_Info;
2652
2653 ------------------------
2654 -- Write_Program_Name --
2655 ------------------------
2656
2657 procedure Write_Program_Name is
2658 Save_Buffer : constant String (1 .. Name_Len) :=
2659 Name_Buffer (1 .. Name_Len);
2660
2661 begin
2662
2663 Find_Program_Name;
2664
2665 -- Convert the name to lower case so error messages are the same on
2666 -- all systems.
2667
2668 for J in 1 .. Name_Len loop
2669 if Name_Buffer (J) in 'A' .. 'Z' then
2670 Name_Buffer (J) :=
2671 Character'Val (Character'Pos (Name_Buffer (J)) + 32);
2672 end if;
2673 end loop;
2674
2675 Write_Str (Name_Buffer (1 .. Name_Len));
2676
2677 -- Restore Name_Buffer which was clobbered by the call to
2678 -- Find_Program_Name
2679
2680 Name_Len := Save_Buffer'Last;
2681 Name_Buffer (1 .. Name_Len) := Save_Buffer;
2682 end Write_Program_Name;
2683
2684 ----------------------
2685 -- Write_With_Check --
2686 ----------------------
2687
2688 procedure Write_With_Check (A : Address; N : Integer) is
2689 Ignore : Boolean;
2690
2691 begin
2692 if N = Write (Output_FD, A, N) then
2693 return;
2694
2695 else
2696 Write_Str ("error: disk full writing ");
2697 Write_Name_Decoded (Output_File_Name);
2698 Write_Eol;
2699 Name_Len := Name_Len + 1;
2700 Name_Buffer (Name_Len) := ASCII.NUL;
2701 Delete_File (Name_Buffer'Address, Ignore);
2702 Exit_Program (E_Fatal);
2703 end if;
2704 end Write_With_Check;
2705
2706 ----------------------------
2707 -- Package Initialization --
2708 ----------------------------
2709
2710 begin
2711 Initialization : declare
2712
2713 function Get_Default_Identifier_Character_Set return Character;
2714 pragma Import (C, Get_Default_Identifier_Character_Set,
2715 "__gnat_get_default_identifier_character_set");
2716 -- Function to determine the default identifier character set,
2717 -- which is system dependent. See Opt package spec for a list of
2718 -- the possible character codes and their interpretations.
2719
2720 function Get_Maximum_File_Name_Length return Int;
2721 pragma Import (C, Get_Maximum_File_Name_Length,
2722 "__gnat_get_maximum_file_name_length");
2723 -- Function to get maximum file name length for system
2724
2725 begin
2726 Identifier_Character_Set := Get_Default_Identifier_Character_Set;
2727 Maximum_File_Name_Length := Get_Maximum_File_Name_Length;
2728
2729 -- Following should be removed by having above function return
2730 -- Integer'Last as indication of no maximum instead of -1 ???
2731
2732 if Maximum_File_Name_Length = -1 then
2733 Maximum_File_Name_Length := Int'Last;
2734 end if;
2735
2736 Src_Search_Directories.Set_Last (Primary_Directory);
2737 Src_Search_Directories.Table (Primary_Directory) := new String'("");
2738
2739 Lib_Search_Directories.Set_Last (Primary_Directory);
2740 Lib_Search_Directories.Table (Primary_Directory) := new String'("");
2741
2742 Initialize;
2743 end Initialization;
2744
2745 end Osint;