1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                               G N A T K R                                --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 1992-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 Ada.Characters.Handling; use Ada.Characters.Handling;
28with Ada.Command_Line;        use Ada.Command_Line;
29with Gnatvsn;
30with Krunch;
31with System.IO; use System.IO;
32
33procedure Gnatkr is
34   pragma Ident (Gnatvsn.Gnat_Static_Version_String);
35
36   Count        : Natural;
37   Maxlen       : Integer;
38   Exit_Program : exception;
39
40   function Get_Maximum_File_Name_Length return Integer;
41   pragma Import (C, Get_Maximum_File_Name_Length,
42                 "__gnat_get_maximum_file_name_length");
43
44begin
45   Count := Argument_Count;
46
47   if Count < 1 or else Count > 2 then
48      Put_Line ("Usage: gnatkr  filename[.extension]  [krunch-count]");
49      raise Exit_Program;
50
51   else
52      --  If the length (krunch-count) argument is omitted use the system
53      --  default if there is one, otherwise use 8.
54
55      if Count = 1 then
56         Maxlen := Get_Maximum_File_Name_Length;
57
58         if Maxlen = -1 then
59            Maxlen := 8;
60         end if;
61
62      else
63         Maxlen := 0;
64
65         for J in Argument (2)'Range loop
66            if Argument (2) (J) /= ' ' then
67               if Argument (2) (J) not in '0' .. '9' then
68                  Put_Line ("Illegal argument for krunch-count");
69                  raise Exit_Program;
70               else
71                  Maxlen := Maxlen * 10 +
72                    Character'Pos (Argument (2) (J)) - Character'Pos ('0');
73               end if;
74            end if;
75         end loop;
76
77         --  Zero means crunch only system files
78
79         if Maxlen = 0 then
80            Maxlen := Natural'Last;
81         end if;
82
83      end if;
84
85      declare
86         Fname : String  := Argument (1);
87         Klen  : Natural := Fname'Length;
88
89         Extp : Boolean := False;
90         --  True if extension is present
91
92         Ext : Natural := 0;
93         --  If extension is present, points to it (init to prevent warning)
94
95      begin
96         --  Remove extension if present (an extension is defined as the
97         --  section of the file name after the last dot in the name. If
98         --  there is no dot in the name, then
99         --  name is all lower case and contains no other instances of dots)
100
101         for J in reverse 1 .. Klen loop
102            if Fname (J) = '.' then
103               Extp := True;
104               Ext := J;
105               Klen := J - 1;
106               exit;
107            end if;
108         end loop;
109
110         --  Fold to lower case and replace dots by dashes
111
112         for J in 1 .. Klen loop
113            Fname (J) := To_Lower (Fname (J));
114
115            if Fname (J) = '.' then
116               Fname (J) := '-';
117            end if;
118         end loop;
119
120         Krunch (Fname, Klen, Maxlen, False);
121
122         Put (Fname (1 .. Klen));
123
124         if Extp then
125            Put (Fname (Ext .. Fname'Length));
126         end if;
127
128         New_Line;
129      end;
130   end if;
131
132   Set_Exit_Status (Success);
133
134exception
135   when Exit_Program =>
136      Set_Exit_Status (Failure);
137
138end Gnatkr;
139