OSDN Git Service

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