summaryrefslogtreecommitdiff
path: root/gcc/ada/gprcmd.adb
blob: 14798fb4f49b2b14f470495ffa859b458360abce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
57
58
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
94
95
96
97
98
99
100
101
102
103
104
105
106
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                               G P R C M D                                --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--         Copyright (C) 2002-2004 Free Software Foundation, Inc.           --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with GNAT;  see file COPYING.  If not, write --
-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
-- MA 02111-1307, USA.                                                      --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

--  A utility used by Makefile.generic to handle multi-language builds.
--  gprcmd provides a set of commands so that the makefiles do not need
--  to depend on unix utilities not available on all targets.

--  The list of commands recognized by gprcmd are:

--    pwd          display current directory
--    to_lower     display next argument in lower case
--    to_absolute  convert pathnames to absolute directories when needed
--    cat          dump contents of a given file
--    extend       handle recursive directories ("/**" notation)
--    deps         post process dependency makefiles
--    stamp        copy file time stamp from file1 to file2
--    prefix       get the prefix of the GNAT installation
--    path         convert a list of directories to a path list, inserting a
--                 path separator after each directory, including the last one
--    ignore       do nothing

with Gnatvsn;
with Osint;   use Osint;
with Namet;   use Namet;

with Ada.Characters.Handling;   use Ada.Characters.Handling;
with Ada.Command_Line;          use Ada.Command_Line;
with Ada.Text_IO;               use Ada.Text_IO;
with GNAT.OS_Lib;               use GNAT.OS_Lib;
with GNAT.Directory_Operations; use GNAT.Directory_Operations;
with GNAT.Regpat;               use GNAT.Regpat;


procedure Gprcmd is

   --  ??? comments are thin throughout this unit


   procedure Cat (File : String);
   --  Print the contents of file on standard output.
   --  If the file cannot be read, exit the process with an error code.

   procedure Check_Args (Condition : Boolean);
   --  If Condition is false, print command invoked, then the usage,
   --  and exit the process.

   procedure Deps (Objext : String; File : String; GCC : Boolean);
   --  Process $(CC) dependency file. If GCC is True, add a rule so that make
   --  will not complain when a file is removed/added. If GCC is False, add a
   --  rule to recompute the dependency file when needed

   procedure Extend (Dir : String);
   --  If Dir ends with /**, Put all subdirs recursively on standard output,
   --  otherwise put Dir.

   procedure Usage;
   --  Display the command line options and exit the process.

   procedure Copy_Time_Stamp (From, To : String);
   --  Copy file time stamp from file From to file To.

   ---------
   -- Cat --
   ---------

   procedure Cat (File : String) is
      FD     : File_Descriptor;
      Buffer : String_Access;
      Length : Integer;

   begin
      FD := Open_Read (File, Fmode => Binary);

      if FD = Invalid_FD then
         OS_Exit (2);
      end if;

      Length := Integer (File_Length (FD));
      Buffer := new String (1 .. Length);
      Length := Read (FD, Buffer.all'Address, Length);
      Close (FD);
      Put (Buffer.all);
      Free (Buffer);
   end Cat;

   ----------------
   -- Check_Args --
   ----------------

   procedure Check_Args (Condition : Boolean) is
   begin
      if not Condition then
         Put_Line
           (Standard_Error,
            "bad call to gprcmd with" & Argument_Count'Img & " arguments.");

         for J in 0 .. Argument_Count loop
            Put (Standard_Error, Argument (J) & " ");
         end loop;

         New_Line (Standard_Error);

         Usage;
      end if;
   end Check_Args;

   ---------------------
   -- Copy_Time_Stamp --
   ---------------------

   procedure Copy_Time_Stamp (From, To : String) is
      function Copy_Attributes
        (From, To : String;
         Mode     : Integer) return Integer;
      pragma Import (C, Copy_Attributes, "__gnat_copy_attribs");
      --  Mode = 0 - copy only time stamps.
      --  Mode = 1 - copy time stamps and read/write/execute attributes

      FD : File_Descriptor;

   begin
      if not Is_Regular_File (From) then
         return;
      end if;

      FD := Create_File (To, Fmode => Binary);

      if FD = Invalid_FD then
         OS_Exit (2);
      end if;

      Close (FD);

      if Copy_Attributes (From & ASCII.NUL, To & ASCII.NUL, 0) /= 0 then
         OS_Exit (2);
      end if;
   end Copy_Time_Stamp;

   ----------
   -- Deps --
   ----------

   procedure Deps (Objext : String; File : String; GCC : Boolean) is
      Colon      : constant String := ':' & ASCII.LF;
      NL         : constant String := (1 => ASCII.LF);
      Base       : constant String := ' ' & Base_Name (File) & ": ";
      FD         : File_Descriptor;
      Buffer     : String_Access;
      Length     : Integer;
      Obj_Regexp : constant Pattern_Matcher :=
                     Compile ("^.*\" & Objext & ": ");
      Matched    : Match_Array (0 .. 0);
      Start      : Natural;
      First      : Natural;
      Last       : Natural;

   begin
      FD := Open_Read_Write (File, Fmode => Binary);

      if FD = Invalid_FD then
         return;
      end if;

      Length := Integer (File_Length (FD));
      Buffer := new String (1 .. Length);
      Length := Read (FD, Buffer.all'Address, Length);

      if GCC then
         Lseek (FD, 0, Seek_End);
      else
         Close (FD);
         FD := Create_File (File, Fmode => Binary);
      end if;

      Start := Buffer'First;

      while Start <= Buffer'Last loop

         --  Parse Buffer line by line

         while Start < Buffer'Last
           and then (Buffer (Start) = ASCII.CR
                     or else Buffer (Start) = ASCII.LF)
         loop
            Start := Start + 1;
         end loop;

         Last := Start;

         while Last < Buffer'Last
           and then Buffer (Last + 1) /= ASCII.CR
           and then Buffer (Last + 1) /= ASCII.LF
         loop
            Last := Last + 1;
         end loop;

         Match (Obj_Regexp, Buffer (Start .. Last), Matched);

         if GCC then
            if Matched (0) = No_Match then
               First := Start;
            else
               First := Matched (0).Last + 1;
            end if;

            Length := Write (FD, Buffer (First)'Address, Last - First + 1);

            if Start = Last or else Buffer (Last) = '\' then
               Length := Write (FD, NL (1)'Address, NL'Length);
            else
               Length := Write (FD, Colon (1)'Address, Colon'Length);
            end if;

         else
            if Matched (0) = No_Match then
               First := Start;
            else
               Length :=
                 Write (FD, Buffer (Start)'Address,
                        Matched (0).Last - Start - 1);
               Length := Write (FD, Base (Base'First)'Address, Base'Length);
               First := Matched (0).Last + 1;
            end if;

            Length := Write (FD, Buffer (First)'Address, Last - First + 1);
            Length := Write (FD, NL (1)'Address, NL'Length);
         end if;

         Start := Last + 1;
      end loop;

      Close (FD);
      Free (Buffer);
   end Deps;

   ------------
   -- Extend --
   ------------

   procedure Extend (Dir : String) is

      procedure Recursive_Extend (D : String);
      --  Recursively display all subdirectories of D

      ----------------------
      -- Recursive_Extend --
      ----------------------

      procedure Recursive_Extend (D : String) is
         Iter   : Dir_Type;
         Buffer : String (1 .. 8192);
         Last   : Natural;

      begin
         Open (Iter, D);

         loop
            Read (Iter, Buffer, Last);

            exit when Last = 0;

            if Buffer (1 .. Last) /= "."
              and then Buffer (1 .. Last) /= ".."
            then
               declare
                  Abs_Dir : constant String := D & Buffer (1 .. Last);

               begin
                  if Is_Directory (Abs_Dir)
                    and then not Is_Symbolic_Link (Abs_Dir)
                  then
                     Put (' ' & Abs_Dir);
                     Recursive_Extend (Abs_Dir & '/');
                  end if;
               end;
            end if;
         end loop;

         Close (Iter);

      exception
         when Directory_Error =>
            null;
      end Recursive_Extend;

   --  Start of processing for Extend

   begin
      if Dir'Length < 3
        or else (Dir (Dir'Last - 2) /= '/'
                 and then Dir (Dir'Last - 2) /= Directory_Separator)
        or else Dir (Dir'Last - 1 .. Dir'Last) /= "**"
      then
         Put (Dir);
         return;
      end if;

      declare
         D : constant String := Dir (Dir'First .. Dir'Last - 2);
      begin
         Put (D);
         Recursive_Extend (D);
      end;
   end Extend;

   -----------
   -- Usage --
   -----------

   procedure Usage is
   begin
      Put_Line (Standard_Error, "usage: gprcmd cmd [arguments]");
      Put_Line (Standard_Error, "where cmd is one of the following commands:");
      Put_Line (Standard_Error, "  pwd         " &
                                "display current directory");
      Put_Line (Standard_Error, "  to_lower    " &
                                "display next argument in lower case");
      Put_Line (Standard_Error, "  to_absolute " &
                                "convert pathnames to absolute " &
                                "directories when needed");
      Put_Line (Standard_Error, "  cat         " &
                                "dump contents of a given file");
      Put_Line (Standard_Error, "  extend      " &
                                "handle recursive directories " &
                                "(""/**"" notation)");
      Put_Line (Standard_Error, "  deps        " &
                                "post process dependency makefiles");
      Put_Line (Standard_Error, "  stamp       " &
                                "copy file time stamp from file1 to file2");
      Put_Line (Standard_Error, "  prefix      " &
                                "get the prefix of the GNAT installation");
      Put_Line (Standard_Error, "  path        " &
                                "convert a directory list into a path list");
      Put_Line (Standard_Error, "  ignore      " &
                                "do nothing");
      OS_Exit (1);
   end Usage;

--  Start of processing for Gprcmd

begin
   Check_Args (Argument_Count > 0);

   declare
      Cmd : constant String := Argument (1);

   begin
      if Cmd = "-v" then

         --  Output on standard error, because only returned values should
         --  go to standard output.

         Put (Standard_Error, "GPRCMD ");
         Put (Standard_Error, Gnatvsn.Gnat_Version_String);
         Put_Line (Standard_Error,
                   " Copyright 2002-2004, Free Software Fundation, Inc.");
         Usage;

      elsif Cmd = "pwd" then
         Put (Format_Pathname (Get_Current_Dir, UNIX));

      elsif Cmd = "cat" then
         Check_Args (Argument_Count = 2);
         Cat (Argument (2));

      elsif Cmd = "to_lower" then
         Check_Args (Argument_Count >= 2);

         for J in 2 .. Argument_Count loop
            Put (To_Lower (Argument (J)));

            if J < Argument_Count then
               Put (' ');
            end if;
         end loop;

      elsif Cmd = "to_absolute" then
         Check_Args (Argument_Count > 2);

         declare
            Dir : constant String := Argument (2);

         begin
            for J in 3 .. Argument_Count loop
               if Is_Absolute_Path (Argument (J)) then
                  Put (Format_Pathname (Argument (J), UNIX));
               else
                  Put (Format_Pathname (Normalize_Pathname (Argument (J), Dir),
                                        UNIX));
               end if;

               if J < Argument_Count then
                  Put (' ');
               end if;
            end loop;
         end;

      elsif Cmd = "extend" then
         Check_Args (Argument_Count >= 2);

         declare
            Dir : constant String := Argument (2);

         begin
            for J in 3 .. Argument_Count loop
               if Is_Absolute_Path (Argument (J)) then
                  Extend (Format_Pathname (Argument (J), UNIX));
               else
                  Extend
                    (Format_Pathname (Normalize_Pathname (Argument (J), Dir),
                                      UNIX));
               end if;

               if J < Argument_Count then
                  Put (' ');
               end if;
            end loop;
         end;

      elsif Cmd = "deps" then
         Check_Args (Argument_Count in 3 .. 4);
         Deps (Argument (2), Argument (3), GCC => Argument_Count = 4);

      elsif Cmd = "stamp" then
         Check_Args (Argument_Count = 3);
         Copy_Time_Stamp (Argument (2), Argument (3));

      elsif Cmd = "prefix" then

         --  Find the GNAT prefix. gprcmd is found in <prefix>/bin.
         --  So we find the full path of gprcmd, verify that it is in a
         --  subdirectory "bin", and return the <prefix> if it is the case.
         --  Otherwise, nothing is returned.

         Find_Program_Name;

         declare
            Path  : constant String_Access :=
                      Locate_Exec_On_Path (Name_Buffer (1 .. Name_Len));
            Index : Natural;

         begin
            if Path /= null then
               Index := Path'Last;

               while Index >= Path'First + 4 loop
                  exit when Path (Index) = Directory_Separator;
                  Index := Index - 1;
               end loop;

               if Index > Path'First + 5
                 and then Path (Index - 3 .. Index - 1) = "bin"
                 and then Path (Index - 4) = Directory_Separator
               then
                  --  We have found the <prefix>, return it

                  Put (Path (Path'First .. Index - 5));
               end if;
            end if;
         end;

      --  For "path" just add path separator after each directory argument

      elsif Cmd = "path" then
         for J in 2 .. Argument_Count loop
            Put (Argument (J));
            Put (Path_Separator);
         end loop;

      --  For "ignore" do nothing

      elsif Cmd = "ignore" then
         null;

      --  Unknown command

      else
         Check_Args (False);
      end if;
   end;
end Gprcmd;