1-----------------------------------------------------------------------------
2--                                                                         --
3--                         ADASOCKETS COMPONENTS                           --
4--                                                                         --
5--                               S P L I T                                 --
6--                                                                         --
7--                                B o d y                                  --
8--                                                                         --
9--          Copyright (C) 1998-2020 Samuel Tardieu <sam@rfc1149.net>       --
10--                 Copyright (C) 1999-2003 Télécom ParisTech               --
11--                                                                         --
12--   AdaSockets is free software; you can  redistribute it and/or modify   --
13--   it  under terms of the GNU  General  Public License as published by   --
14--   the Free Software Foundation; either version 2, or (at your option)   --
15--   any later version.   AdaSockets is distributed  in the hope that it   --
16--   will be useful, but WITHOUT ANY  WARRANTY; without even the implied   --
17--   warranty of MERCHANTABILITY   or FITNESS FOR  A PARTICULAR PURPOSE.   --
18--   See the GNU General Public  License  for more details.  You  should   --
19--   have received a copy of the  GNU General Public License distributed   --
20--   with AdaSockets; see   file COPYING.  If  not,  write  to  the Free   --
21--   Software  Foundation, 59   Temple Place -   Suite  330,  Boston, MA   --
22--   02111-1307, USA.                                                      --
23--                                                                         --
24--   As a special exception, if  other  files instantiate generics  from   --
25--   this unit, or  you link this  unit with other  files to produce  an   --
26--   executable,  this  unit does  not  by  itself cause  the  resulting   --
27--   executable to be  covered by the  GNU General Public License.  This   --
28--   exception does  not  however invalidate any  other reasons  why the   --
29--   executable file might be covered by the GNU Public License.           --
30--                                                                         --
31--   The main repository for this software is located at:                  --
32--       http://www.rfc1149.net/devel/adasockets.html                      --
33--                                                                         --
34--   If you have any question, please use the issues tracker at:           --
35--       https://github.com/samueltardieu/adasockets/issues                --
36--                                                                         --
37-----------------------------------------------------------------------------
38
39with Ada.Command_Line; use Ada.Command_Line;
40with Ada.Text_IO;      use Ada.Text_IO;
41
42procedure Split is
43
44   Column          : Natural  := 0;
45   Max_Column      : constant := 65;
46   Hard_Max_Column : constant := 78;
47   Was_Space       : Boolean  := False;
48
49   procedure Print_Space_Maybe;
50
51   procedure Print_Space_Maybe is
52   begin
53      if Was_Space and then Column /= 0 then
54         Put (' ');
55         Column := Column + 1;
56         Was_Space := False;
57      end if;
58   end Print_Space_Maybe;
59
60begin
61   if Argument_Count /= 1 then
62      Put_Line ("Error, usage: split ""text""");
63      Set_Exit_Status (1);
64   else
65      for I in 1 .. Argument (1) 'Length loop
66         if Argument (1) (I) = ' ' and then Column >= Max_Column then
67            New_Line;
68            Column := 0;
69         elsif Argument (1) (I) = ' ' and then Column = 0 then
70            null;
71         elsif Column >= Hard_Max_Column then
72            New_Line;
73            Put ("--  " & Argument (1) (I));
74            Column := 5;
75         elsif Argument (1) (I) = ' ' then
76            Was_Space := True;
77         else
78            if Column = 0 then
79               Put ("--  ");
80               Column := 4;
81            else
82               Print_Space_Maybe;
83            end if;
84            Put (Argument (1) (I));
85            Column := Column + 1;
86         end if;
87      end loop;
88      if Column > 0 then
89         New_Line;
90      end if;
91   end if;
92end Split;
93