You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

envset.cmd 3.9KB

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