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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453 | ------------------------------------------------------------------------------
-- Templates Parser --
-- --
-- Copyright (C) 2004-2014, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify --
-- it under terms of the GNU General Public License as published by the --
-- Free Software Foundation; either version 3, or (at your option) any --
-- later version. This library is distributed in the hope that it will be --
-- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are --
-- granted additional permissions described in the GCC Runtime Library --
-- Exception, version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- As a special exception, if other files instantiate generics from this --
-- unit, or you link this unit with other files to produce an executable, --
-- this unit does not by itself cause the resulting executable to be --
-- covered by the GNU General Public License. This exception does not --
-- however invalidate any other reasons why the executable file might be --
-- covered by the GNU Public License. --
------------------------------------------------------------------------------
with Ada.Command_Line;
with Ada.Directories;
with Ada.Strings.Fixed;
with Ada.Strings.Maps.Constants;
package body Templates_Parser.Utils is
--------------------------
-- Executable_Extension --
--------------------------
function Executable_Extension return String is
begin
if Is_Windows then
return "exe";
else
return "";
end if;
end Executable_Extension;
---------------------------
-- Get_Program_Directory --
---------------------------
function Get_Program_Directory return String is
function Locate_On_Path (Filename : String) return String;
-- Returns the full pathname for filename or the empty string if not
-- found.
function Is_Full_Pathname (Filename : String) return Boolean;
-- Returns True is Filename is a full pathname
function Get_Command_Name return String;
-- Returns the normalized command string
function Containing_Directory (Filename : String) return String;
-- Containing directory without directory separator, this can happen
-- with GNAT when returning the current working directory?
--------------------------
-- Containing_Directory --
--------------------------
function Containing_Directory (Filename : String) return String is
CD : constant String := Directories.Containing_Directory (Filename);
begin
if CD (CD'Last) = Directory_Separator then
return CD (CD'First .. CD'Last - 1);
else
return CD;
end if;
end Containing_Directory;
----------------------
-- Get_Command_Name --
----------------------
function Get_Command_Name return String is
N : constant String := Command_Line.Command_Name;
E : constant String := Executable_Extension;
begin
if N'Length > E'Length
and then N (N'Last - E'Length + 1 .. N'Last) = E
then
return N;
else
return N & E;
end if;
end Get_Command_Name;
----------------------
-- Is_Full_Pathname --
----------------------
function Is_Full_Pathname (Filename : String) return Boolean is
F : String renames Filename;
begin
return F (F'First) = Directory_Separator
or else
(F'Length > 2
and then (F (F'First) in 'a' .. 'z'
or else F (F'First) in 'A' .. 'Z')
and then F (F'First + 1) = ':'
and then F (F'First + 2) = Directory_Separator);
end Is_Full_Pathname;
--------------------
-- Locate_On_Path --
--------------------
function Locate_On_Path (Filename : String) return String is
PATH : constant String := Environment_Variables.Value ("PATH");
First, Last : Natural;
Idx : Natural;
begin
First := PATH'First;
loop
Last := Strings.Fixed.Index
(PATH, String'(1 => Path_Separator), From => First);
if Last = 0 then
Idx := PATH'Last;
else
Idx := Last - 1;
end if;
declare
Full_Pathname : constant String := Directories.Compose
(PATH (First .. Idx) & Directory_Separator, Filename);
begin
if Directories.Exists (Full_Pathname) then
return Full_Pathname;
end if;
end;
First := Last + 1;
exit when Last = 0 or else First > PATH'Last;
end loop;
return "";
end Locate_On_Path;
Command_Name : constant String := Get_Command_Name;
begin
if Command_Name = "" then
-- Command name can be empty on OS not supporting command line
-- options like VxWorks.
return ".";
else
declare
Dir : constant String := Containing_Directory (Command_Name);
begin
-- On UNIX command_name doesn't include the directory name
-- when the command was found on the PATH. On Windows using
-- the standard shell, the command is never passed using a
-- full pathname. In such a case, which check on the PATH
-- ourselves to find it.
if Directories.Exists (Command_Name) then
-- Command is found
if Is_Full_Pathname (Command_Name)
or else Is_Full_Pathname (Dir)
then
-- And we have a full pathname use it
return Dir & Directory_Separator;
else
-- A relative pathname, catenate the current directory
return Directories.Current_Directory
& Directory_Separator & Dir & Directory_Separator;
end if;
else
-- Command does not exist, try checkin it on the PATH
declare
Full_Pathname : constant String :=
Locate_On_Path
(Directories.Simple_Name (Command_Name));
begin
if Full_Pathname = "" then
-- Not found on the PATH, nothing we can do
return Dir;
else
return Directories.Containing_Directory (Full_Pathname)
& Directory_Separator;
end if;
end;
end if;
end;
end if;
end Get_Program_Directory;
-----------
-- Image --
-----------
function Image (T : Tag) return String is
function Quote (Str : String) return String with Inline;
-- Quote Str and double quote inside Str if needed
-----------
-- Quote --
-----------
function Quote (Str : String) return String is
Result : Unbounded_String;
begin
Append (Result, """");
for K in Str'Range loop
if Str (K) = '"' then
Append (Result, """""");
else
Append (Result, Str (K));
end if;
end loop;
Append (Result, """");
return To_String (Result);
end Quote;
Result : Unbounded_String;
N : Tag_Node_Access := T.Data.Head;
begin
while N /= null loop
if N.Kind = Value then
if Result /= Null_Unbounded_String then
Append (Result, ",");
end if;
Append (Result, Quote (To_String (N.V)));
else
Append (Result, Image (N.VS.all));
end if;
N := N.Next;
end loop;
return "(" & To_String (Result) & ")";
end Image;
function Image (N : Integer) return String is
N_Img : constant String := Integer'Image (N);
begin
if N_Img (N_Img'First) = '-' then
return N_Img;
else
return N_Img (N_Img'First + 1 .. N_Img'Last);
end if;
end Image;
---------------
-- Is_Number --
---------------
function Is_Number (S : String) return Boolean is
begin
return S'Length /= 0
and then
Strings.Fixed.Index
(S,
Strings.Maps.Constants.Decimal_Digit_Set,
Test => Strings.Outside) = 0;
end Is_Number;
-----------
-- Value --
-----------
function Value (T : String) return Tag is
function Value (T : String) return Tag;
-- Returns the Tag for T string
function Clear_Quote (Str : String) return String with Inline;
-- Removes double quote in Str
-----------------
-- Clear_Quote --
-----------------
function Clear_Quote (Str : String) return String is
Result : Unbounded_String;
begin
for K in Str'Range loop
if Str (K) /= '"'
or else (K < Str'Last and then Str (K + 1) /= '"')
then
Append (Result, Str (K));
end if;
end loop;
return To_String (Result);
end Clear_Quote;
-----------
-- Value --
-----------
function Value (T : String) return Tag is
Result : Tag;
K : Natural := T'First;
N : Natural;
Last : Natural;
begin
while K < T'Last loop
if T (K) = '(' then
-- This is a nested Tag, Look for corresponding closing parent
Last := K + 1;
N := 0;
Nested_Tag : loop
if T (Last) = ')' then
if N = 0 then
-- Matching parent found, add it to the result
Result := Result & Value (T (K + 1 .. Last));
K := Last;
-- and leave this loop
exit Nested_Tag;
else
N := N - 1;
end if;
elsif T (Last) = '(' then
N := N + 1;
end if;
if Last = T'Last then
-- Matching parent not found
raise Constraint_Error;
else
Last := Last + 1;
end if;
end loop Nested_Tag;
elsif T (K) = '"' then
-- This is a value, Look for corresponding closing quote
Last := K + 1;
Quoted_Value : loop
if T (Last) = '"'
and then Last < T'Last
and then T (Last + 1) = '"'
then
-- Skip this quote
Last := Last + 1;
elsif T (Last) = '"'
and then (Last = T'Last or else T (Last + 1) /= '"')
then
-- Found matching quote, add this value
Result := Result & Clear_Quote (T (K + 1 .. Last - 1));
K := Last;
-- and leave loop
exit Quoted_Value;
elsif Last = T'Last then
-- No matching quote
raise Constraint_Error;
end if;
Last := Last + 1;
end loop Quoted_Value;
-- Here we must have either a ',' or ")"
if Last /= T'Last
and then T (Last + 1) /= ','
and then T (Last + 1) /= ')'
then
raise Constraint_Error;
end if;
end if;
K := K + 1;
end loop;
return Result;
end Value;
begin
if T'Length > 1 and then T (T'First) = '(' and then T (T'Last) = ')' then
return Value (T (T'First + 1 .. T'Last - 1));
else
raise Constraint_Error;
end if;
end Value;
----------------
-- Web_Escape --
----------------
function Web_Escape (S : String) return String is
Result : Unbounded_String;
Last : Integer := S'First;
procedure Append_To_Result (Str : String; From, To : Integer);
-- Append S (From .. To) to Result if not empty concatenated with Str
-- and update Last.
----------------------
-- Append_To_Result --
----------------------
procedure Append_To_Result (Str : String; From, To : Integer) is
begin
if From <= To then
Append (Result, S (From .. To) & Str);
else
Append (Result, Str);
end if;
Last := To + 2;
end Append_To_Result;
begin
for I in S'Range loop
case S (I) is
when '&' =>
Append_To_Result ("&", Last, I - 1);
when '>' =>
Append_To_Result (">", Last, I - 1);
when '<' =>
Append_To_Result ("<", Last, I - 1);
when '"' =>
Append_To_Result (""", Last, I - 1);
when others =>
null;
end case;
end loop;
if Last <= S'Last then
Append (Result, S (Last .. S'Last));
end if;
return To_String (Result);
end Web_Escape;
end Templates_Parser.Utils;
|