OSDN Git Service

PR c++/9704
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnatkr.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                               G N A T K R                                --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                                                                          --
10 --          Copyright (C) 1992-2001 Free Software Foundation, Inc.          --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- GNAT was originally developed  by the GNAT team at  New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
25 --                                                                          --
26 ------------------------------------------------------------------------------
27
28 with Ada.Characters.Handling; use Ada.Characters.Handling;
29 with Ada.Command_Line;        use Ada.Command_Line;
30 with Krunch;
31 with System.IO; use System.IO;
32
33 procedure Gnatkr is
34
35    Count        : Natural;
36    Maxlen       : Integer;
37    Exit_Program : exception;
38
39    function Get_Maximum_File_Name_Length return Integer;
40    pragma Import (C, Get_Maximum_File_Name_Length,
41                  "__gnat_get_maximum_file_name_length");
42
43 begin
44    Count := Argument_Count;
45
46    if Count < 1 or else Count > 2 then
47       Put_Line ("Usage: gnatkr  filename[.extension]  [krunch-count]");
48       raise Exit_Program;
49
50    else
51       --  If the length (krunch-count) argument is omitted use the system
52       --  default if there is one, otherwise use 8.
53
54       if Count = 1 then
55          Maxlen := Get_Maximum_File_Name_Length;
56
57          if Maxlen = -1 then
58             Maxlen := 8;
59          end if;
60
61       else
62          Maxlen := 0;
63
64          for J in Argument (2)'Range loop
65             if Argument (2) (J) /= ' ' then
66                if Argument (2) (J) not in '0' .. '9' then
67                   Put_Line ("Illegal argument for krunch-count");
68                   raise Exit_Program;
69                else
70                   Maxlen := Maxlen * 10 +
71                     Character'Pos (Argument (2) (J)) - Character'Pos ('0');
72                end if;
73             end if;
74          end loop;
75
76          --  Zero means crunch only system files
77
78          if Maxlen = 0 then
79             Maxlen := Natural'Last;
80          end if;
81
82       end if;
83
84       declare
85          Fname : String  := Argument (1);
86          Klen  : Natural := Fname'Length;
87
88          Extp : Boolean := False;
89          --  True if extension is present
90
91          Ext : Natural := 0;
92          --  If extension is present, points to it (init to prevent warning)
93
94       begin
95          --  Remove .adb or .ads extension if present (recognized only if the
96          --  name is all lower case and contains no other instances of dots)
97
98          if Klen > 4
99            and then Fname (Klen - 3 .. Klen - 1) = ".ad"
100            and then (Fname (Klen) = 's' or else Fname (Klen) = 'b')
101          then
102             Extp := True;
103
104             for J in 1 .. Klen - 4 loop
105                if Is_Upper (Fname (J)) or else Fname (J) = '.' then
106                   Extp := False;
107                end if;
108             end loop;
109
110             if Extp then
111                Klen := Klen - 4;
112                Ext := Klen + 1;
113             end if;
114
115          else
116             Extp := False;
117          end if;
118
119          --  Fold to lower case and replace dots by dashes
120
121          for J in 1 .. Klen loop
122             Fname (J) := To_Lower (Fname (J));
123
124             if Fname (J) = '.' then
125                Fname (J) := '-';
126             end if;
127          end loop;
128
129          Krunch (Fname, Klen, Maxlen, False);
130
131          Put (Fname (1 .. Klen));
132
133          if Extp then
134             Put (Fname (Ext .. Fname'Length));
135          end if;
136
137          New_Line;
138       end;
139    end if;
140
141    Set_Exit_Status (Success);
142
143 exception
144    when Exit_Program =>
145       Set_Exit_Status (Failure);
146
147 end Gnatkr;