envset.cmd 4.1 KB

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