OSDN Git Service

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