gnoga_2.1.2_5f127c56/deps/zanyblue/src/zbtest/commands/zbtest-commands-getenv_command.adb

  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
--  -*- coding: utf-8 -*-
--
--  ZanyBlue, an Ada library and framework for finite element analysis.
--
--  Copyright (c) 2012, 2017, Michael Rohan <mrohan@zanyblue.com>
--  All rights reserved.
--
--  Redistribution and use in source and binary forms, with or without
--  modification, are permitted provided that the following conditions
--  are met:
--
--    * Redistributions of source code must retain the above copyright
--      notice, this list of conditions and the following disclaimer.
--
--    * Redistributions in binary form must reproduce the above copyright
--      notice, this list of conditions and the following disclaimer in the
--      documentation and/or other materials provided with the distribution.
--
--    * Neither the name of ZanyBlue nor the names of its contributors may
--      be used to endorse or promote products derived from this software
--      without specific prior written permission.
--
--  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
--  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
--  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
--  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
--  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
--  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
--  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
--  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
--  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
--  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--

--  @usage getenv [ -l | -s | -p | -a ] name [ parameter ]
--  @summary define a parameter based on an environment variable
--  @start-doc
--  Define an internal parameter based on the value of an environment
--  variable.  For example,::
--
--      ZBTest> print HOME
--      The parameter "HOME" is not defined
--      ZBTest> getenv HOME
--      ZBTest> print HOME
--      /u/mrohan
--
--  The options available are
--
--  -l
--     Define an internal list parameter by splitting on the pathsep
--
--  -s
--     Define a simple scalar (string) parameter (default)
--
--  -a
--     Append the values (implies the -l option)
--
--  -p
--     Prepend the values (implies the -l option)
--
--  If the target is not given then import to name.
--

with Ada.Environment_Variables;

separate (ZBTest.Commands)
procedure Getenv_Command
  (State : in out State_Type;
   Args  :        List_Type)
is

   use Ada.Environment_Variables;

   procedure Get_List_Value
     (State         : in out State_Type;
      Source        :        String;
      Target        :        String;
      Append_Values :        Boolean);
   --  Get an environment variable as a list value.

   --------------------
   -- Get_List_Value --
   --------------------

   procedure Get_List_Value
     (State         : in out State_Type;
      Source        :        String;
      Target        :        String;
      Append_Values :        Boolean)
   is

      procedure Parse_Values
        (List_Values : in out List_Type;
         Definition  :        String);

      ------------------
      -- Parse_Values --
      ------------------

      procedure Parse_Values
        (List_Values : in out List_Type;
         Definition  :        String)
      is

         PSep  : constant Unicode_Character := State.Get_Character ("_pathsep");
         Start : Positive                := Definition'First;
         Next  : Positive                := Definition'First;

      begin
         while Next <= Definition'Last loop
            if Definition (Next) = PSep then
               Append (List_Values, Definition (Start .. Next - 1));
               Start := Next + 1;
            end if;
            Next := Next + 1;
         end loop;
         Append (List_Values, Definition (Start .. Next - 1));
      end Parse_Values;

      List_Values : List_Type;

   begin  -- Get_List_Value
      if Exists (Wide_To_UTF8 (Source)) then
         Parse_Values
           (List_Values, To_Wide_String (Value (Wide_To_UTF8 (Source))));
         if Append_Values then
            for I in 1 .. Length (List_Values) loop
               State.Append (Target, Value (List_Values, I));
            end loop;
         else
            for I in reverse 1 .. Length (List_Values) loop
               State.Prepend (Target, Value (List_Values, I));
            end loop;
         end if;
      else
         Print_10007 (+Source);
      end if;
   end Get_List_Value;

   Get_To_List   : Boolean := False;
   Append_Values : Boolean := True;
   Source_Idx    : Natural := 0;
   Target_Idx    : Natural := 0;

begin
   for I in 2 .. Length (Args) loop
      if Value (Args, I) = "-l" then
         Get_To_List := True;
      elsif Value (Args, I) = "-s" then
         Get_To_List := False;
      elsif Value (Args, I) = "-a" then
         Get_To_List   := True;
         Append_Values := True;
      elsif Value (Args, I) = "-p" then
         Get_To_List   := True;
         Append_Values := False;
      elsif Source_Idx = 0 then
         Source_Idx := I;
      elsif Target_Idx = 0 then
         Target_Idx := I;
      else
         raise Command_Usage_Error;
      end if;
   end loop;
   if Source_Idx = 0 then
      raise Command_Usage_Error;
   end if;
   if Target_Idx = 0 then
      Target_Idx := Source_Idx;
   end if;
   if Get_To_List then
      Get_List_Value
        (State, Value (Args, Source_Idx), Value (Args, Target_Idx),
         Append_Values);
   else
      State.Set_String
        (Value (Args, Target_Idx),
         To_Wide_String (Value (Wide_To_UTF8 (Value (Args, Source_Idx)))));
   end if;
end Getenv_Command;