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 | -----------------------------------------------------------------------
-- util-commands -- Support to make command line tools
-- Copyright (C) 2017, 2018, 2022, 2023 Stephane Carrez
-- Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
-----------------------------------------------------------------------
with Ada.Command_Line;
with Ada.Characters.Handling;
with Util.Systems.IO;
with Util.Systems.Os;
package body Util.Commands is
-- ------------------------------
-- Get the number of arguments available.
-- ------------------------------
overriding
function Get_Count (List : in Default_Argument_List) return Natural is
Count : constant Natural := Ada.Command_Line.Argument_Count;
begin
if Count > List.Offset then
return Count - List.Offset;
else
return 0;
end if;
end Get_Count;
-- ------------------------------
-- Get the argument at the given position.
-- ------------------------------
overriding
function Get_Argument (List : in Default_Argument_List;
Pos : in Positive) return String is
begin
return Ada.Command_Line.Argument (Pos + List.Offset);
end Get_Argument;
-- ------------------------------
-- Get the command name.
-- ------------------------------
overriding
function Get_Command_Name (List : in Default_Argument_List) return String is
pragma Unreferenced (List);
begin
return Ada.Command_Line.Command_Name;
end Get_Command_Name;
-- ------------------------------
-- Set the argument list to the given string and split the arguments.
-- ------------------------------
procedure Initialize (List : in out String_Argument_List;
Line : in String) is
First : Natural := Line'First;
begin
List.Length := Line'Length;
List.Line (1 .. Line'Length) := Line;
List.Count := 0;
loop
while First <= Line'Length
and then Ada.Characters.Handling.Is_Space (List.Line (First)) loop
First := First + 1;
end loop;
exit when First > Line'Length;
List.Start_Pos (List.Count) := First;
while First <= Line'Length
and then not Ada.Characters.Handling.Is_Space (List.Line (First)) loop
First := First + 1;
end loop;
List.End_Pos (List.Count) := First - 1;
List.Count := List.Count + 1;
end loop;
if List.Count > 0 then
List.Count := List.Count - 1;
end if;
end Initialize;
-- ------------------------------
-- Get the number of arguments available.
-- ------------------------------
overriding
function Get_Count (List : in String_Argument_List) return Natural is
begin
return List.Count;
end Get_Count;
-- ------------------------------
-- Get the argument at the given position.
-- ------------------------------
overriding
function Get_Argument (List : in String_Argument_List;
Pos : in Positive) return String is
begin
return List.Line (List.Start_Pos (Pos) .. List.End_Pos (Pos));
end Get_Argument;
-- ------------------------------
-- Get the command name.
-- ------------------------------
overriding
function Get_Command_Name (List : in String_Argument_List) return String is
begin
return List.Line (List.Start_Pos (0) .. List.End_Pos (0));
end Get_Command_Name;
-- ------------------------------
-- Get the number of arguments available.
-- ------------------------------
overriding
function Get_Count (List : in Dynamic_Argument_List) return Natural is
begin
return Natural (List.List.Length);
end Get_Count;
-- ------------------------------
-- Get the argument at the given position.
-- ------------------------------
overriding
function Get_Argument (List : in Dynamic_Argument_List;
Pos : in Positive) return String is
begin
return List.List.Element (Pos);
end Get_Argument;
-- ------------------------------
-- Get the command name.
-- ------------------------------
overriding
function Get_Command_Name (List : in Dynamic_Argument_List) return String is
begin
return Ada.Strings.Unbounded.To_String (List.Name);
end Get_Command_Name;
procedure Put_Raw (Content : in String) is
begin
Util.Systems.IO.Put_Raw (Util.Systems.IO.STDOUT_FILENO, Content);
end Put_Raw;
procedure Put_Raw_Line (Content : in String) is
begin
Util.Systems.IO.Put_Raw (Util.Systems.IO.STDOUT_FILENO,
Content & Util.Systems.Os.Line_Separator);
end Put_Raw_Line;
procedure New_Line_Raw (Count : in Positive) is
begin
for I in 1 .. Count loop
Util.Systems.IO.Put_Raw (Util.Systems.IO.STDOUT_FILENO,
Util.Systems.Os.Line_Separator);
end loop;
end New_Line_Raw;
end Util.Commands;
|