1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                              O S I N T - B                               --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 2001-2003 Free Software Foundation, Inc.          --
10--                                                                          --
11-- GNAT is free software;  you can  redistribute it  and/or modify it under --
12-- terms of the  GNU General Public License as published  by the Free Soft- --
13-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17-- for  more details.  You should have  received  a copy of the GNU General --
18-- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20-- MA 02111-1307, USA.                                                      --
21--                                                                          --
22-- GNAT was originally developed  by the GNAT team at  New York University. --
23-- Extensive contributions were provided by Ada Core Technologies Inc.      --
24--                                                                          --
25------------------------------------------------------------------------------
26
27with Hostparm;
28with Namet;   use Namet;
29with Opt;     use Opt;
30
31package body Osint.B is
32
33   Binder_Output_Time_Stamps_Set : Boolean := False;
34
35   Old_Binder_Output_Time_Stamp  : Time_Stamp_Type;
36   New_Binder_Output_Time_Stamp  : Time_Stamp_Type;
37   Recording_Time_From_Last_Bind : Boolean := False;
38
39   -------------------------
40   -- Close_Binder_Output --
41   -------------------------
42
43   procedure Close_Binder_Output is
44      Status : Boolean;
45   begin
46      Close (Output_FD, Status);
47
48      if not Status then
49         Fail
50           ("error while closing generated file ",
51            Get_Name_String (Output_File_Name));
52      end if;
53
54      if Recording_Time_From_Last_Bind then
55         New_Binder_Output_Time_Stamp  := File_Stamp (Output_File_Name);
56         Binder_Output_Time_Stamps_Set := True;
57      end if;
58   end Close_Binder_Output;
59
60   --------------------------
61   -- Create_Binder_Output --
62   --------------------------
63
64   procedure Create_Binder_Output
65     (Output_File_Name : String;
66      Typ              : Character;
67      Bfile            : out Name_Id)
68   is
69      File_Name : String_Ptr;
70      Findex1   : Natural;
71      Findex2   : Natural;
72      Flength   : Natural;
73
74   begin
75      if Output_File_Name /= "" then
76         Name_Buffer (Output_File_Name'Range) := Output_File_Name;
77         Name_Buffer (Output_File_Name'Last + 1) := ASCII.NUL;
78
79         if Typ = 's' then
80            Name_Buffer (Output_File_Name'Last) := 's';
81         end if;
82
83         Name_Len := Output_File_Name'Last;
84
85      else
86         Name_Buffer (1) := 'b';
87         File_Name := File_Names (Current_File_Name_Index);
88
89         Findex1 := File_Name'First;
90
91         --  The ali file might be specified by a full path name. However,
92         --  the binder generated file should always be created in the
93         --  current directory, so the path might need to be stripped away.
94         --  In addition to the default directory_separator allow the '/' to
95         --  act as separator since this is allowed in MS-DOS and OS2 ports.
96
97         for J in reverse File_Name'Range loop
98            if File_Name (J) = Directory_Separator
99              or else File_Name (J) = '/'
100            then
101               Findex1 := J + 1;
102               exit;
103            end if;
104         end loop;
105
106         Findex2 := File_Name'Last;
107         while File_Name (Findex2) /=  '.' loop
108            Findex2 := Findex2 - 1;
109         end loop;
110
111         Flength := Findex2 - Findex1;
112
113         if Maximum_File_Name_Length > 0 then
114
115            --  Make room for the extra two characters in "b?"
116
117            while Int (Flength) > Maximum_File_Name_Length - 2 loop
118               Findex2 := Findex2 - 1;
119               Flength := Findex2 - Findex1;
120            end loop;
121         end if;
122
123         Name_Buffer (3 .. Flength + 2) := File_Name (Findex1 .. Findex2 - 1);
124         Name_Buffer (Flength + 3) := '.';
125
126         --  C bind file, name is b_xxx.c
127
128         if Typ = 'c' then
129            Name_Buffer (2) := '_';
130            Name_Buffer (Flength + 4) := 'c';
131            Name_Buffer (Flength + 5) := ASCII.NUL;
132            Name_Len := Flength + 4;
133
134         --  Ada bind file, name is b~xxx.adb or b~xxx.ads
135         --  (with $ instead of ~ in VMS)
136
137         else
138            if Hostparm.OpenVMS then
139               Name_Buffer (2) := '$';
140            else
141               Name_Buffer (2) := '~';
142            end if;
143
144            Name_Buffer (Flength + 4) := 'a';
145            Name_Buffer (Flength + 5) := 'd';
146            Name_Buffer (Flength + 6) := Typ;
147            Name_Buffer (Flength + 7) := ASCII.NUL;
148            Name_Len := Flength + 6;
149         end if;
150      end if;
151
152      Bfile := Name_Find;
153
154      if Recording_Time_From_Last_Bind then
155         Old_Binder_Output_Time_Stamp := File_Stamp (Bfile);
156      end if;
157
158      Create_File_And_Check (Output_FD, Text);
159   end Create_Binder_Output;
160
161   --------------------
162   -- More_Lib_Files --
163   --------------------
164
165   function More_Lib_Files return Boolean renames  More_Files;
166
167   ------------------------
168   -- Next_Main_Lib_File --
169   ------------------------
170
171   function Next_Main_Lib_File return File_Name_Type renames Next_Main_File;
172
173   --------------------------------
174   -- Record_Time_From_Last_Bind --
175   --------------------------------
176
177   procedure Record_Time_From_Last_Bind is
178   begin
179      Recording_Time_From_Last_Bind := True;
180   end Record_Time_From_Last_Bind;
181
182   -------------------------
183   -- Time_From_Last_Bind --
184   -------------------------
185
186   function Time_From_Last_Bind return Nat is
187      Old_Y  : Nat;
188      Old_M  : Nat;
189      Old_D  : Nat;
190      Old_H  : Nat;
191      Old_Mi : Nat;
192      Old_S  : Nat;
193      New_Y  : Nat;
194      New_M  : Nat;
195      New_D  : Nat;
196      New_H  : Nat;
197      New_Mi : Nat;
198      New_S  : Nat;
199
200      type Month_Data is array (Int range 1 .. 12) of Int;
201      Cumul : constant Month_Data := (0, 0, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7);
202      --  Represents the difference in days from a period compared to the
203      --  same period if all months had 31 days, i.e:
204      --
205      --    Cumul (m) = 31x(m-1) - (number of days from 01/01 to m/01)
206
207      Res : Int;
208
209   begin
210      if not Recording_Time_From_Last_Bind
211        or else not Binder_Output_Time_Stamps_Set
212        or else Old_Binder_Output_Time_Stamp = Empty_Time_Stamp
213      then
214         return Nat'Last;
215      end if;
216
217      Split_Time_Stamp
218       (Old_Binder_Output_Time_Stamp,
219        Old_Y, Old_M, Old_D, Old_H, Old_Mi, Old_S);
220
221      Split_Time_Stamp
222       (New_Binder_Output_Time_Stamp,
223        New_Y, New_M, New_D, New_H, New_Mi, New_S);
224
225      Res := New_Mi - Old_Mi;
226
227      --  60 minutes in an hour
228
229      Res := Res + 60 * (New_H  - Old_H);
230
231      --  24 hours in a day
232
233      Res := Res + 60 * 24 * (New_D  - Old_D);
234
235      --  Almost 31 days in a month
236
237      Res := Res + 60 * 24 *
238        (31 * (New_M - Old_M) - Cumul (New_M) + Cumul (Old_M));
239
240      --  365 days in a year
241
242      Res := Res + 60 * 24 * 365 * (New_Y - Old_Y);
243
244      return Res;
245   end Time_From_Last_Bind;
246
247   -----------------------
248   -- Write_Binder_Info --
249   -----------------------
250
251   procedure Write_Binder_Info (Info : String) renames Write_Info;
252
253begin
254   Set_Program (Binder);
255end Osint.B;
256