Home | History | Annotate | Download | only in bin
      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 
     18 SET environment variables
     19 First 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 
     30 Second parameter: name of the environment variable
     31 
     32 Next parameters: values
     33 ; implies that the equal sign is considered a part of the parameter and is
     34 not interpreted
     35 
     36 -D requires parameters in the form name=value. If the equal sign is not found,
     37 the parameters are changed to name=expanded_name
     38 
     39 Other options have optional equal sign. If it is found, only the part after
     40 the equal sign will be oprionally expanded.
     41 
     42 If the parameter is the minus sign, the next parameter will not be expanded.
     43 If the parameter is a single dot, it will be replaced with the value of the
     44 environment variable as it existed before envset was invoked.
     45 
     46 For other parameters the batch looks for the environment variable with the
     47 same name (in uppercase). If it is found, it forms the expanded_name. If
     48 the environment variable with such a name does not exist, the expanded_name
     49 will hold the parameter name without case conversion.
     50 */
     51 
     52 parse arg mode envar args
     53 
     54 equal = 0
     55 sep = ' '
     56 
     57 /* Parse command line parameters */
     58 select
     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 = ','
     70 otherwise
     71   args = envar args
     72   envar = mode
     73   mode = ''
     74 end
     75 
     76 env = 'OS2ENVIRONMENT'
     77 envar = translate(envar)
     78 orig = value(envar,,env)
     79 newval = ''
     80 expand = 1
     81 
     82 /* for each parameter... */
     83 do i = 1 to words(args)
     84   if expand > 0 & word(args, i) = '-' then expand = 0
     85   else call addval word(args, i)
     86 end
     87 
     88 /* Optionally enclose path variable by quotes */
     89 if mode = ';' & pos(' ', newval) > 0 then newval = '"' || newval || '"'
     90 
     91 /* Set the new value, 'SET' cannot be used since it does not allow '=' */
     92 x = value(envar, newval, env)
     93 exit 0
     94 
     95 addval: procedure expose sep equal orig expand newval mode env
     96 parse arg var
     97 
     98 if var = '.' then expvar = orig
     99 else 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
    126 end
    127 
    128 if newval = '' then newval = expvar
    129 else newval = newval || sep || expvar
    130 expand = 1
    131 return
    132