1/*
2
3   Licensed to the Apache Software Foundation (ASF) under one or more
4   contributor license agreements.  See the NOTICE file distributed with
5   this work for additional information regarding copyright ownership.
6   The ASF licenses this file to You under the Apache License, Version 2.0
7   (the "License"); you may not use this file except in compliance with
8   the License.  You may obtain a copy of the License at
9
10       http://www.apache.org/licenses/LICENSE-2.0
11
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17
18SET environment variables
19First optional parameter:
20   ;     parameters are considered parts of a path variable, semicolons are
21         appended to each element if not already present
22   -D    parameters are properties for Java or Makefile etc., -D will be
23         prepended and the parameters will be separated by a space
24   =D    the same as above but equal sign is not required
25   ,     parameters should be comma separated in the environment variable
26   -     parameters should be separated by the next parameter
27   Other values mean that the first parameter is missing and the environment
28   variable will be set to the space separated parameters
29
30Second parameter: name of the environment variable
31
32Next parameters: values
33; implies that the equal sign is considered a part of the parameter and is
34not interpreted
35
36-D requires parameters in the form name=value. If the equal sign is not found,
37the parameters are changed to name=expanded_name
38
39Other options have optional equal sign. If it is found, only the part after
40the equal sign will be oprionally expanded.
41
42If the parameter is the minus sign, the next parameter will not be expanded.
43If the parameter is a single dot, it will be replaced with the value of the
44environment variable as it existed before envset was invoked.
45
46For other parameters the batch looks for the environment variable with the
47same name (in uppercase). If it is found, it forms the expanded_name. If
48the environment variable with such a name does not exist, the expanded_name
49will hold the parameter name without case conversion.
50*/
51
52parse arg mode envar args
53
54equal = 0
55sep = ' '
56
57/* Parse command line parameters */
58select
59  when mode='-' then do
60    sep = envar
61    parse var args envar args
62  end
63  when mode=';' then do
64    sep = ''
65    equal = -1
66  end
67  when mode='-D' then equal = 1
68  when mode='=D' then mode = '-D'
69  when mode=',' then sep = ','
70otherwise
71  args = envar args
72  envar = mode
73  mode = ''
74end
75
76env = 'OS2ENVIRONMENT'
77envar = translate(envar)
78orig = value(envar,,env)
79newval = ''
80expand = 1
81
82/* for each parameter... */
83do i = 1 to words(args)
84  if expand > 0 & word(args, i) = '-' then expand = 0
85  else call addval word(args, i)
86end
87
88/* Optionally enclose path variable by quotes */
89if mode = ';' & pos(' ', newval) > 0 then newval = '"' || newval || '"'
90
91/* Set the new value, 'SET' cannot be used since it does not allow '=' */
92x = value(envar, newval, env)
93exit 0
94
95addval: procedure expose sep equal orig expand newval mode env
96parse arg var
97
98if var = '.' then expvar = orig
99else do
100  if equal >= 0 then do
101    parse var var name '=' val
102    if val = '' then var = name
103    else var = val
104  end
105  if expand = 0 then expvar = var
106  else expvar = value(translate(var),,env)
107  if expvar = '' then expvar = var
108  if equal >= 0 then do
109    if val = '' then do
110      parse var expvar key '=' val
111      if val <> '' then name = key
112      else do
113        if equal > 0 then val = key
114        else name = key
115      end
116    end
117    else val = expvar
118    if pos(' ', val) > 0 | pos('=', val) > 0 then val = '"' || val || '"'
119    if val = '' then expvar = name
120    else expvar = name || '=' || val
121  end
122  if mode = '-D' then expvar = '-D' || expvar
123  if mode = ';' then do
124    if right(expvar, 1) <> ';' then expvar = expvar || ';'
125  end
126end
127
128if newval = '' then newval = expvar
129else newval = newval || sep || expvar
130expand = 1
131return
132