headers_po_hi.adb 11.2 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                          PolyORB HI COMPONENTS                           --
--                                                                          --
--                        H E A D E R S _ P O _ H I                         --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
yoogx's avatar
yoogx committed
9
--    Copyright (C) 2006-2009 Telecom ParisTech, 2010-2015 ESA & ISAE.      --
10
--                                                                          --
yoogx's avatar
yoogx committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- PolyORB-HI is free software; you can redistribute it and/or modify under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. PolyORB-HI is distributed in the hope that it will be useful, but  --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
--              PolyORB-HI/Ada is maintained by the TASTE project           --
28
--                      (taste-users@lists.tuxfamily.org)                   --
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
--                                                                          --
------------------------------------------------------------------------------

with Ada.Calendar;            use Ada.Calendar;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Command_Line;
with Ada.Directories;         use Ada.Directories;
with Ada.Exceptions;
with Ada.Strings.Unbounded;   use Ada.Strings.Unbounded;
with Ada.Text_IO;             use Ada.Text_IO;
with Ada.Unchecked_Deallocation;

with GNAT.Regpat; use GNAT.Regpat;

procedure Headers_PO_HI is

   subtype Line_Type is String (1 .. 256);

   type Kind_Type is (None, Unit_Spec, Unit_Body, Project_File);

   Add_Header : Boolean := True;

   Header_Template : constant String :=
     "/*" & ASCII.LF &
     " * This is a part of PolyORB-HI-C distribution, a minimal" & ASCII.LF &
     " * middleware written for generated code from AADL models." & ASCII.LF &
     " * You should use it with the Ocarina toolsuite." & ASCII.LF &
     " *" & ASCII.LF &
yoogx's avatar
yoogx committed
57
     " * For more informations, please visit http://taste.tuxfamily.org/wiki"
58
     & ASCII.LF & " *" & ASCII.LF & "@COPYRIGHT@" & " */" & ASCII.LF;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

   -------------------------
   -- Utility subprograms --
   -------------------------

   function Copyright_Line (First_Year, Last_Year : Year_Number) return String;
   --  Return copyright notice for the specified year range

   function Doublespace (S : String) return String;
   --  Return S with double spacing inserted if short enough to fit the header
   --  comment box; otherwise return S unchanged.

   function Has_Prefix (Prefix : String; S : String) return Boolean;
   --  True iff S starts with Prefix

   function Image (Year : Year_Number) return String;
   --  Return the string image of Year (with no leading space)

   procedure Update_Header (Filename : String);
   --  Output the contents of Filename with updated header

   --------------------
   -- Copyright_Line --
   --------------------

   function Copyright_Line
     (First_Year, Last_Year : Year_Number) return String
   is
      Range_Image : constant String :=
        Image (First_Year) & "-" & Image (Last_Year);
      Last : Positive := Range_Image'Last;
   begin
      if First_Year = Last_Year then
         Last := Range_Image'First + 3;
      end if;
94
95
96
97
98
99
100
101
102
103
104
105
106
      if First_Year < 2009 then
         return "Copyright (C) " &
           Image (First_Year) & "-2009 Telecom ParisTech, "
           & "2010-" & Image (last_year) & " ESA & ISAE.";
      elsif First_Year = 2009 then
         return "Copyright (C) " &
           Image (First_Year) & " Telecom ParisTech, "
           & "2010-" & Image (last_year) & " ESA & ISAE.";

      else
         return "Copyright (C) " & Range_Image (Range_Image'First .. Last)
           & " ESA & ISAE.";
      end if;
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
   end Copyright_Line;

   -----------------
   -- Doublespace --
   -----------------

   function Doublespace (S : String) return String is
   begin
      if S'Length > 35 then
         return S;
      else
         declare
            Res : String (2 * S'First .. 2 * S'Last) := (others => ' ');
         begin
            for J in S'Range loop
               Res (2 * J) := S (J);
            end loop;
            return Res;
         end;
      end if;
   end Doublespace;

   ----------------
   -- Has_Prefix --
   ----------------

   function Has_Prefix (Prefix : String; S : String) return Boolean is
   begin
      return S'Length >= Prefix'Length
        and then S (S'First .. S'First + Prefix'Length - 1) = Prefix;
   end Has_Prefix;

   -----------
   -- Image --
   -----------

   function Image (Year : Year_Number) return String is
      Res : constant String := Year'Img;
   begin
      return Res (Res'First + 1 .. Res'Last);
   end Image;

   -------------------
   -- Update_Header --
   -------------------

   procedure Update_Header (Filename : String) is

      Ofilename : constant String := Filename & ".UHN";

      ----------------------
      -- Global variables --
      ----------------------

      UName : Unbounded_String;
      UKind : Kind_Type := None;

      Last_Copyright_Year  : Year_Number := Year (Clock);
      First_Copyright_Year : Year_Number := Last_Copyright_Year;

      type Substs is (Copyright);
      type String_Access is access all String;

      procedure Free is new Ada.Unchecked_Deallocation (String, String_Access);

      procedure Output_Header (Outf : File_Type);
      --  Output header templates with appropriate substitutions

      procedure Output_Header (Outf : File_Type) is
         Pattern : Unbounded_String;

         function "+" (S : String) return Unbounded_String is
         begin
            return To_Unbounded_String (" * " & S & ASCII.LF);
         end "+";

         Subst_Strings : array (Substs) of Unbounded_String :=
           (Copyright        =>
              +Copyright_Line (First_Copyright_Year, Last_Copyright_Year));

      begin
         Pattern := To_Unbounded_String ("@(");
         for J in Substs loop
            Append (Pattern, J'Img);
            if J /= Substs'Last then
               Append (Pattern, '|');
            end if;
         end loop;
         Append (Pattern, ")@");

         declare
            Matcher : constant Pattern_Matcher :=
                        Compile (To_String (Pattern), Single_Line);
            Matches : Match_Array (0 .. Paren_Count (Matcher));
            Start   : Positive := Header_Template'First;
         begin
            while Start <= Header_Template'Last loop
               Match (Matcher,
                      Header_Template (Start .. Header_Template'Last), Matches);

               if Matches (0) = No_Match then
                  Put (Outf, Header_Template (Start .. Header_Template'Last));
                  exit;
               end if;
               declare
                  Loc_Token  : Match_Location renames Matches (1);
               begin
                  Put (Outf, Header_Template (Start .. Loc_Token.First - 2));
                  Put (Outf,
                    To_String (Subst_Strings
                      (Substs'Value (Header_Template (Loc_Token.First
                                                   .. Loc_Token.Last)))));
                  Start := Loc_Token.Last + 2;
               end;

            end loop;
         end;
      end Output_Header;

      Line : Line_Type;
      Last : Natural;

      Copyright_Matcher : constant Pattern_Matcher :=
        Compile ("Copyright \([cC]\) ([0-9]+)");
      Copyright_Matches : Match_Array (0 .. Paren_Count (Copyright_Matcher));

      F    : File_Type;
      Outf : File_Type;

      In_Header : Boolean := True;
237
      Got_Copyright : Boolean := False;
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
      Buf : Unbounded_String;

      Basename : constant String := Base_Name (Filename);
   begin
      Open   (F, In_File, Filename);
      Create (Outf, Out_File, Ofilename);

      begin
         loop
            Get_Line (F, Line, Last);
            if Last = Line'Last then
               raise Constraint_Error with "line too long";
            end if;
            if Last < 2
              or else (Line (1 .. 2) /= "/*"
                         and then Line (1 ..2) /= " *"
                         and then Line (1 ..2) /= "*/")
            then
               In_Header := False;
               exit;
            end if;

            if In_Header then
               Match (Copyright_Matcher, Line (1 .. Last), Copyright_Matches);
262
263
264
               if (not Got_Copyright)
                 and then Copyright_Matches (0) /= No_Match
               then
265
266
267
                  First_Copyright_Year :=
                    Year_Number'Value (Line (Copyright_Matches (1).First
                                          .. Copyright_Matches (1).Last));
268
                  Got_Copyright := True;
269
270
271
272
               end if;
            end if;
         end loop;

273
274
275
276
         if not Got_Copyright then
            Reset (F);
         end if;

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
         if Add_Header then
            Output_Header (Outf);
            Put_Line (To_String (Buf));
            New_Line (Outf);
         end if;

         Put (Outf, To_String (Buf));

         while not End_Of_File (F) loop
            Get_Line (F, Line, Last);
            if Last = Line'Last then
               raise Constraint_Error with "line too long";
            end if;
            Put_Line (Outf, Line (1 .. Last));
         end loop;

         Close (F);
         Close (Outf);
         Delete_File (Filename);
         Rename (Ofilename, Filename);
      exception
         when E : others =>
            Put_Line (Standard_Error, "Update of " & Filename & " failed:");
            Put_Line (Ada.Exceptions.Exception_Information (E));
            Delete_File (Ofilename);
      end;

   end Update_Header;

   --  Start of processing for Headers_PO_HI

begin
   if Ada.Command_Line.Argument_Count = 2 then
      if Ada.Command_Line.Argument (1) = "-noh" then
         Add_Header := False;
      end if;
      Put_Line ("Removing header from " &  Ada.Command_Line.Argument (2));
      Update_Header (Ada.Command_Line.Argument (2));
   else

      Put_Line ("Adding header to " &  Ada.Command_Line.Argument (1));
      Update_Header (Ada.Command_Line.Argument (1));
   end if;
end Headers_PO_HI;