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 | ------------------------------------------------------------------------------
-- --
-- GPR PROJECT MANAGER --
-- --
-- Copyright (C) 2000-2022, Free Software Foundation, Inc. --
-- --
-- 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 MERCHAN- --
-- TABILITY 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/>. --
-- --
------------------------------------------------------------------------------
with Ada.Unchecked_Deallocation;
with GPR.Names; use GPR.Names;
with GPR.Osint; use GPR.Osint;
package body GPR.Ext is
----------------
-- Initialize --
----------------
procedure Initialize
(Self : out External_References;
Copy_From : External_References := No_External_Refs)
is
N : Name_To_Name_Ptr;
N2 : Name_To_Name_Ptr;
begin
if Self.Refs = null then
Self.Refs := new Name_To_Name_HTable.Instance;
if Copy_From.Refs /= null then
N := Name_To_Name_HTable.Get_First (Copy_From.Refs.all);
while N /= null loop
N2 := new Name_To_Name'
(Key => N.Key,
Value => N.Value,
Source => N.Source,
Next => null);
Name_To_Name_HTable.Set (Self.Refs.all, N2);
N := Name_To_Name_HTable.Get_Next (Copy_From.Refs.all);
end loop;
end if;
end if;
if Self.Context = null then
Self.Context := new Context;
end if;
end Initialize;
---------
-- Add --
---------
procedure Add
(Self : External_References;
External_Name : String;
Value : String;
Source : External_Source := External_Source'First;
Silent : Boolean := False)
is
Key : Name_Id;
N : Name_To_Name_Ptr;
begin
-- For external attribute, set the environment variable
if Source = From_External_Attribute and then External_Name /= "" then
declare
Env_Var : String_Access := Getenv (External_Name);
begin
if Env_Var = null or else Env_Var.all = "" then
Setenv (Name => External_Name, Value => Value);
if not Silent then
Debug_Output
("Environment variable """ & External_Name
& """ = """ & Value & '"');
end if;
elsif not Silent then
Debug_Output
("Not overriding existing environment variable """
& External_Name & """, value is """ & Env_Var.all & '"');
end if;
Free (Env_Var);
end;
end if;
Name_Len := External_Name'Length;
Name_Buffer (1 .. Name_Len) := External_Name;
Canonical_Case_Env_Var_Name (Name_Buffer (1 .. Name_Len));
Key := Name_Find;
-- Check whether the value is already defined, to properly respect the
-- overriding order.
if Source /= External_Source'First then
N := Name_To_Name_HTable.Get (Self.Refs.all, Key);
if N /= null then
if External_Source'Pos (N.Source) <
External_Source'Pos (Source)
then
if not Silent then
Debug_Output
("Not overriding existing external reference '"
& External_Name & "', value was defined in "
& N.Source'Img);
end if;
return;
end if;
end if;
end if;
Name_Len := Value'Length;
Name_Buffer (1 .. Name_Len) := Value;
N := new Name_To_Name'
(Key => Key,
Source => Source,
Value => Name_Find,
Next => null);
if not Silent then
Debug_Output ("Add external (" & External_Name & ") is", N.Value);
end if;
Name_To_Name_HTable.Remove (Self.Refs.all, Key);
Name_To_Name_HTable.Set (Self.Refs.all, N);
end Add;
-----------
-- Check --
-----------
function Check
(Self : External_References;
Declaration : String) return Boolean
is
begin
for Equal_Pos in Declaration'Range loop
if Declaration (Equal_Pos) = '=' then
exit when Equal_Pos = Declaration'First;
Add
(Self => Self,
External_Name =>
Declaration (Declaration'First .. Equal_Pos - 1),
Value =>
Declaration (Equal_Pos + 1 .. Declaration'Last),
Source => From_Command_Line);
return True;
end if;
end loop;
return False;
end Check;
-----------
-- Reset --
-----------
procedure Reset (Self : External_References) is
begin
if Self.Refs /= null then
Debug_Output ("Reset external references");
Name_To_Name_HTable.Reset (Self.Refs.all);
end if;
if Self.Context /= null then
Self.Context.Clear;
end if;
end Reset;
--------------
-- Value_Of --
--------------
function Value_Of
(Self : External_References;
External_Name : Name_Id;
With_Default : Name_Id := No_Name)
return Name_Id
is
Value : Name_To_Name_Ptr;
Val : Name_Id;
Name : String := Get_Name_String (External_Name);
begin
Canonical_Case_Env_Var_Name (Name);
if Self.Refs /= null then
Value := Name_To_Name_HTable.Get (Self.Refs.all, Get_Name_Id (Name));
if Value /= null and then Value.Source <= From_Environment then
Debug_Output ("Value_Of (" & Name & ") is in cache", Value.Value);
return Value.Value;
end if;
end if;
-- Find if it is an environment, if it is, put value in the hash table
declare
Env_Value : String_Access := Getenv (Name);
begin
if Env_Value /= null and then Env_Value'Length > 0 then
Val := Get_Name_Id (Env_Value.all);
if Current_Verbosity = High then
Debug_Output ("Value_Of (" & Name & ") is", Val);
end if;
if Self.Refs /= null then
Add
(Self, Name, Env_Value.all, From_Environment, Silent => True);
end if;
Free (Env_Value);
return Val;
else
if Current_Verbosity = High then
Debug_Output
("Value_Of (" & Name & ") is default", With_Default);
end if;
Free (Env_Value);
return With_Default;
end if;
end;
end Value_Of;
----------
-- Free --
----------
procedure Free (Self : in out External_References) is
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
(Name_To_Name_HTable.Instance, Instance_Access);
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
(Name_To_Name, Name_To_Name_Ptr);
procedure Unchecked_Free is new Ada.Unchecked_Deallocation
(Context, Context_Access);
Ptr : Name_To_Name_Ptr;
Size : Natural := 0;
begin
if Self.Refs /= null then
Ptr := Name_To_Name_HTable.Get_First (Self.Refs.all);
while Ptr /= null loop
Size := Size + 1;
Ptr := Name_To_Name_HTable.Get_Next (Self.Refs.all);
end loop;
declare
Ptr_Array : array (1 .. Size) of Name_To_Name_Ptr;
Idx : Positive := 1;
begin
Ptr := Name_To_Name_HTable.Get_First (Self.Refs.all);
while Ptr /= null loop
Ptr_Array (Idx) := Ptr;
Ptr := Name_To_Name_HTable.Get_Next (Self.Refs.all);
Idx := Idx + 1;
end loop;
for J in Ptr_Array'Range loop
Unchecked_Free (Ptr_Array (J));
end loop;
end;
Reset (Self);
Unchecked_Free (Self.Refs);
Unchecked_Free (Self.Context);
end if;
end Free;
--------------
-- Set_Next --
--------------
procedure Set_Next (E : Name_To_Name_Ptr; Next : Name_To_Name_Ptr) is
begin
E.Next := Next;
end Set_Next;
----------
-- Next --
----------
function Next (E : Name_To_Name_Ptr) return Name_To_Name_Ptr is
begin
return E.Next;
end Next;
-------------
-- Get_Key --
-------------
function Get_Key (E : Name_To_Name_Ptr) return Name_Id is
begin
return E.Key;
end Get_Key;
-----------------
-- Get_Context --
-----------------
function Get_Context (Self : External_References) return Context is
Result : Context;
Cur : Context_Map.Cursor;
use Context_Map;
begin
if Self.Context /= null then
Cur := Self.Context.First;
while Cur /= No_Element loop
Result.Include
(Key (Cur),
Value_Of (Self, Key (Cur), Element (Cur)));
Next (Cur);
end loop;
end if;
return Result;
end Get_Context;
-------------------------
-- Add_Name_To_Context --
-------------------------
procedure Add_Name_To_Context
(Self : External_References;
External_Name : Name_Id;
Default : Name_Id)
is
Name : String := Get_Name_String (External_Name);
begin
if Self.Context /= null then
Canonical_Case_Env_Var_Name (Name);
Self.Context.Include (Get_Name_Id (Name), Default);
end if;
end Add_Name_To_Context;
-------------------
-- Reset_Context --
-------------------
procedure Reset_Context (Self : External_References) is
begin
if Self.Context /= null then
Self.Context.Clear;
end if;
end Reset_Context;
end GPR.Ext;
|