1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             S I N P U T . C                              --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 1992-2019, 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 3,  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 COPYING3.  If not, go to --
19-- http://www.gnu.org/licenses for a complete copy of the license.          --
20--                                                                          --
21-- GNAT was originally developed  by the GNAT team at  New York University. --
22-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23--                                                                          --
24------------------------------------------------------------------------------
25
26with Debug;  use Debug;
27with Opt;    use Opt;
28with Output; use Output;
29with System; use System;
30
31pragma Warnings (Off);
32--  This package is used also by gnatcoll
33with System.OS_Lib; use System.OS_Lib;
34pragma Warnings (On);
35
36package body Sinput.C is
37
38   ---------------
39   -- Load_File --
40   ---------------
41
42   function Load_File (Path : String) return Source_File_Index is
43      Src  : Source_Buffer_Ptr;
44      X    : Source_File_Index;
45      Lo   : Source_Ptr;
46      Hi   : Source_Ptr;
47
48      Source_File_FD : File_Descriptor;
49      --  The file descriptor for the current source file. A negative value
50      --  indicates failure to open the specified source file.
51
52      Len : Integer;
53      --  Length of file (assume no more than 2 gigabytes of source)
54
55      Actual_Len : Integer;
56
57      Path_Id : File_Name_Type;
58      File_Id : File_Name_Type;
59
60   begin
61      if Path = "" then
62         return No_Source_File;
63      end if;
64
65      Source_File.Increment_Last;
66      X := Source_File.Last;
67
68      if Debug_Flag_L then
69         Write_Str ("Sinput.C.Load_File: created source ");
70         Write_Int (Int (X));
71         Write_Str (" for ");
72         Write_Str (Path);
73         Write_Line ("");
74      end if;
75
76      if X = Source_File.First then
77         Lo := First_Source_Ptr;
78      else
79         Lo := ((Source_File.Table (X - 1).Source_Last + Source_Align) /
80                  Source_Align) * Source_Align;
81      end if;
82
83      Name_Len := Path'Length;
84      Name_Buffer (1 .. Name_Len) := Path;
85      Path_Id := Name_Find;
86      Name_Buffer (Name_Len + 1) := ASCII.NUL;
87
88      --  Open the source FD, note that we open in binary mode, because as
89      --  documented in the spec, the caller is expected to handle either
90      --  DOS or Unix mode files, and there is no point in wasting time on
91      --  text translation when it is not required.
92
93      Source_File_FD := Open_Read (Name_Buffer'Address, Binary);
94
95      if Source_File_FD = Invalid_FD then
96         Source_File.Decrement_Last;
97         return No_Source_File;
98
99      end if;
100
101      Len := Integer (File_Length (Source_File_FD));
102
103      --  Set Hi so that length is one more than the physical length, allowing
104      --  for the extra EOF character at the end of the buffer
105
106      Hi := Lo + Source_Ptr (Len);
107
108      --  Do the actual read operation
109
110      declare
111         Var_Ptr : constant Source_Buffer_Ptr_Var :=
112           new Source_Buffer (Lo .. Hi);
113         --  Allocate source buffer, allowing extra character at end for EOF
114
115      begin
116         --  Some systems have file types that require one read per line,
117         --  so read until we get the Len bytes or until there are no more
118         --  characters.
119
120         Hi := Lo;
121         loop
122            Actual_Len := Read (Source_File_FD, Var_Ptr (Hi)'Address, Len);
123            Hi := Hi + Source_Ptr (Actual_Len);
124            exit when Actual_Len = Len or else Actual_Len <= 0;
125         end loop;
126
127         Var_Ptr (Hi) := EOF;
128         Src := Var_Ptr.all'Access;
129      end;
130
131      --  Read is complete, close the file and we are done (no need to test
132      --  status from close, since we have successfully read the file).
133
134      Close (Source_File_FD);
135
136      --  Get the file name, without path information
137
138      declare
139         Index : Positive := Path'Last;
140
141      begin
142         while Index > Path'First loop
143            exit when Path (Index - 1) = '/';
144            exit when Path (Index - 1) = Directory_Separator;
145            Index := Index - 1;
146         end loop;
147
148         Name_Len := Path'Last - Index + 1;
149         Name_Buffer (1 .. Name_Len) := Path (Index .. Path'Last);
150         File_Id := Name_Find;
151      end;
152
153      declare
154         S : Source_File_Record renames Source_File.Table (X);
155
156      begin
157         S := (Debug_Source_Name   => File_Id,
158               File_Name           => File_Id,
159               File_Type           => Config,
160               First_Mapped_Line   => No_Line_Number,
161               Full_Debug_Name     => Path_Id,
162               Full_File_Name      => Path_Id,
163               Full_Ref_Name       => Path_Id,
164               Instance            => No_Instance_Id,
165               Identifier_Casing   => Unknown,
166               Inlined_Call        => No_Location,
167               Inlined_Body        => False,
168               Inherited_Pragma    => False,
169               Keyword_Casing      => Unknown,
170               Last_Source_Line    => 1,
171               License             => Unknown,
172               Lines_Table         => null,
173               Lines_Table_Max     => 1,
174               Logical_Lines_Table => null,
175               Num_SRef_Pragmas    => 0,
176               Reference_Name      => File_Id,
177               Sloc_Adjust         => 0,
178               Source_Checksum     => 0,
179               Source_First        => Lo,
180               Source_Last         => Hi,
181               Source_Text         => Src,
182               Template            => No_Source_File,
183               Unit                => No_Unit,
184               Time_Stamp          => Empty_Time_Stamp,
185               Index               => X);
186
187         Alloc_Line_Tables (S, Opt.Table_Factor * Alloc.Lines_Initial);
188         S.Lines_Table (1) := Lo;
189      end;
190
191      Set_Source_File_Index_Table (X);
192      return X;
193   end Load_File;
194
195end Sinput.C;
196