Built win.arm64 against r3658
[kbuild-mirror.git] / kBuild / envos2.cmd
blob23002084af8dfff3258525e7a47e1c75c45175bd
1 /*
2 echo this is a rexx script!
3 cancel & quit & exit
4 */
5 /* $Id$ */
6 /** @file
7 * Environment setup script for OS/2.
8 */
12 * Copyright (c) 1999-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
14 * This file is part of kBuild.
16 * kBuild is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * kBuild is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with kBuild; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 * Setup the usual suspects.
36 Address CMD '@echo off';
37 signal on novalue name NoValueHandler
38 if (RxFuncQuery('SysLoadFuncs') = 1) then
40 call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs';
41 call SysLoadFuncs;
42 end
45 * Apply the CMD.EXE workaround.
47 call FixCMDEnv;
50 * Globals
52 skBuildPath = EnvGet("KBUILD_PATH");
53 skBuildBinPath = EnvGet("KBUILD_BIN_PATH");
54 skBuildType = EnvGet("KBUILD_TYPE");
55 skBuildTarget = EnvGet("KBUILD_TARGET");
56 skBuildTargetArch = EnvGet("KBUILD_TARGET_ARCH");
57 skBuildTargetCpu = EnvGet("KBUILD_TARGET_CPU");
58 skBuildHost = EnvGet("KBUILD_HOST");
59 skBuildHostArch = EnvGet("KBUILD_HOST_ARCH");
60 skBuildHostCpu = EnvGet("KBUILD_HOST_CPU");
63 * Process arguments.
65 fOptFull = 0
66 fOptLegacy = 0
67 fOptDbg = 0
68 fOptQuiet = 0
69 sOptVars = ""
70 fOptValueOnly = 0
71 sShowVarPrefix = "";
72 fOptOverrideAll = 0
73 fOptOverrideType = 0;
74 fSetType = 0;
75 fOptOverrideTarget = 0;
76 fOptOverrideTargetArch = 0;
77 fOptDefault = 0;
79 parse arg sArgs
80 do while (sArgs <> '')
81 parse value sArgs with sArg sRest
82 say 'sArgs='sArgs';'
83 say ' sArg='sArg';'
84 say 'sRest='sRest';'
86 select
87 when (sArg = "--debug-script") then do
88 fOptDbg = 1;
89 end
90 when (sArg = "--no-debug-script") then do
91 fOptDbg = 0;
92 end
93 when (sArg = "--quiet") then do
94 fOptQuiet = 1;
95 end
96 when (sArg = "--verbose") then do
97 fOptQuiet = 0;
98 end
99 when (sArg = "--full") then do
100 fOptFull = 1;
102 when (sArg = "--normal") then do
103 fOptFull = 0;
105 when (sArg = "--legacy") then do
106 fOptLegacy = 1;
108 when (sArg = "--no-legacy") then do
109 fOptLegacy = 0;
111 when (sArg = "--eval") then do
112 say "error: --eval is not supported on OS/2."
114 when (sArg = "--var") then do
115 parse value sRest with sVar sRest2
116 sRest = sRest2;
117 if (sVar = '') then do
118 say "syntax error: --var is missing the variable name";
119 call SysSleep 1
120 exit 1;
122 if (sVar = "all" | sOptVars = "all") then
123 sOptVars = "all";
124 else
125 sOptVars = sOptVars || " " || sVar;
127 when (sArg = "--set") then do
128 sShowVarPrefix = "SET ";
130 when (sArg = "--no-set") then do
131 sShowVarPrefix = "";
133 when (sArg = "--value-only") then do
134 fOptValueOnly = 1;
136 when (sArg = "--name-and-value") then do
137 fOptValueOnly = 0;
139 when (sArg = "--release") then do
140 fOptOverrideType = 1;
141 fSetType = 1;
142 skBuildType = 'release';
144 when (sArg = "--debug") then do
145 fOptOverrideType = 1;
146 fSetType = 1;
147 skBuildType = 'debug';
149 when (sArg = "--profile") then do
150 fOptOverrideType = 1;
151 fSetType = 1;
152 skBuildType = 'profile';
154 when (sArg = "--defaults") then do
155 fOptOverrideAll = 1;
156 skBuildType = "";
157 skBuildTarget = "";
158 skBuildTargetArch = "";
159 skBuildTargetCpu = "";
160 skBuildHost = "";
161 skBuildHostArch = "";
162 skBuildHostCpu = "";
163 skBuildPath = "";
164 skBuildBinPath = "";
167 when (sArg = "--help" | sArg = "-h" | sArg = "-?" | sArg = "/?" | sArg = "/h") then do
168 say "kBuild Environment Setup Script, v0.1.4"
169 say ""
170 say "syntax: envos2.cmd [options] [command [args]]"
171 say " or: envos2.cmd [options] --var <varname>"
172 say ""
173 say "The first form will execute the command, or if no command is given"
174 say "modify the current invoking shell."
175 say "The second form will print the specfified variable(s)."
176 say ""
177 say "Options:"
178 say " --debug, --release, --profile"
179 say " Alternative way of specifying KBUILD_TYPE."
180 say " --defaults"
181 say " Enforce defaults for all the KBUILD_* values."
182 say " --debug-script, --no-debug-script"
183 say " Controls debug output. Default: --no-debug-script"
184 say " --quiet, --verbose"
185 say " Controls informational output. Default: --verbose"
186 say " --full, --normal"
187 say " Controls the variable set. Default: --normal"
188 say " --legacy, --no-legacy"
189 say " Include legacy variables in result. Default: --no-legacy"
190 say " --value-only, --name-and-value"
191 say " Controls what the result of a --var query. Default: --name-and-value"
192 say " --set, --no-set"
193 say " Whether to prefix the variable output with 'SET'."
194 say " Default: --no-set"
195 say ""
196 exit 1
199 when (sArg = "--") then do
200 sArgs = sRest;
201 leave;
204 when (left(sArg, 2) = '--') then do
205 say 'syntax error: unknown option: '||sArg
206 call SysSleep 1
207 exit 1
210 otherwise do
211 leave
214 sArgs = sRest;
216 sCommand = strip(sArgs);
219 * Deal with legacy environment variables.
221 if (\fOptOverrideAll) then do
222 if (EnvGet("PATH_KBUILD") <> '') then do
223 if (skBuildPath <> '' & skBuildPath <> EnvGet("PATH_KBUILD")) then do
224 say "error: KBUILD_PATH ("||skBuildPath||") and PATH_KBUILD ("||EnvGet("PATH_KBUILD")||") disagree."
225 call SysSleep 1;
226 exit 1;
228 skBuildPath = EnvGet("PATH_KBUILD");
231 if (EnvGet("PATH_KBUILD_BIN") <> '') then do
232 if (skBuildPath <> '' & skBuildBinPath <> EnvGet("PATH_KBUILD_BIN")) then do
233 say "error: KBUILD_BIN_PATH ("||skBuildBinPath||") and PATH_KBUILD_BIN ("||EnvGet("PATH_KBUILD_BIN")||") disagree."
234 call SysSleep 1;
235 exit 1;
237 skBuildBinPath = EnvGet("PATH_KBUILD_BIN");
240 if (EnvGet("BUILD_TYPE") <> '') then do
241 if (skBuildType <> '' & skBuildType <> EnvGet("BUILD_TYPE")) then do
242 say "error: KBUILD_TYPE ("||skBuildType||") and BUILD_TYPE ("||EnvGet("BUILD_TYPE")||") disagree."
243 call SysSleep 1;
244 exit 1;
246 skBuildType = EnvGet("BUILD_TYPE");
249 if (EnvGet("BUILD_TARGET") <> '') then do
250 if (skBuildTarget <> '' & skBuildTarget <> EnvGet("BUILD_TARGET")) then do
251 say "error: KBUILD_TARGET ("||skBuildTarget||") and BUILD_TARGET ("||EnvGet("BUILD_TARGET")||") disagree."
252 call SysSleep 1;
253 exit 1;
255 skBuildTarget = EnvGet("BUILD_TARGET");
258 if (EnvGet("BUILD_TARGET_ARCH") <> '') then do
259 if (skBuildTargetArch <> '' & skBuildTargetArch <> EnvGet("BUILD_TARGET_ARCH")) then do
260 say "error: KBUILD_TARGET_ARCH ("||skBuildTargetArch ||") and BUILD_TARGET_ARCH ("||EnvGet("BUILD_TARGET_ARCH")||") disagree."
261 call SysSleep 1;
262 exit 1;
264 skBuildTargetArch = EnvGet("BUILD_TARGET_ARCH");
267 if (EnvGet("BUILD_TARGET_CPU") <> '') then do
268 if (skBuildTargetCpu <> '' & skBuildTargetCpu <> EnvGet("BUILD_TARGET_CPU")) then do
269 say "error: KBUILD_TARGET_CPU ("||skBuildTargetCpu ||") and BUILD_TARGET_CPU ("||EnvGet("BUILD_TARGET_CPU")||") disagree."
270 call SysSleep 1;
271 exit 1;
273 skBuildTargetCpu = EnvGet("BUILD_TARGET_CPU");
276 if (EnvGet("BUILD_PLATFORM") <> '') then do
277 if (skBuildHost <> '' & skBuildHost <> EnvGet("BUILD_PLATFORM")) then do
278 say "error: KBUILD_HOST ("||skBuildHost||") and BUILD_PLATFORM ("||EnvGet("BUILD_PLATFORM")||") disagree."
279 call SysSleep 1;
280 exit 1;
282 if (skBuildHost = '' & EnvGet("BUILD_PLATFORM") = "OS2") then do
283 say "error: BUILD_PLATFORM=OS2! Please unset it or change it to 'os2'."
284 call SysSleep 1;
285 exit 1;
287 skBuildHost = EnvGet("BUILD_PLATFORM");
290 if (EnvGet("BUILD_PLATFORM_ARCH") <> '') then do
291 if (skBuildHostArch <> '' & skBuildHostArch <> EnvGet("BUILD_PLATFORM_ARCH")) then do
292 say "error: KBUILD_HOST_ARCH ("||skBuildHostArch ||") and BUILD_PLATFORM_ARCH ("||EnvGet("BUILD_PLATFORM_ARCH")||") disagree."
293 call SysSleep 1;
294 exit 1;
296 skBuildHostArch = EnvGet("BUILD_PLATFORM_ARCH");
299 if (EnvGet("BUILD_PLATFORM_CPU") <> '') then do
300 if (skBuildHostCpu <> '' & skBuildHostCpu <> EnvGet("BUILD_PLATFORM_CPU")) then do
301 say "error: KBUILD_HOST_CPU ("||skBuildHostCpu ||") and BUILD_PLATFORM_CPU ("||EnvGet("BUILD_PLATFORM_CPU")||") disagree."
302 call SysSleep 1;
303 exit 1;
305 skBuildHostCpu = EnvGet("BUILD_PLATFORM_CPU");
310 * Set default build type.
312 if (skBuildType = '') then
313 skBuildType = 'release';
314 if (fOptDbg <> 0) then say "dbg: KBUILD_TYPE="||skBuildType
317 * Determin the host platform (OS/2)
319 if (skBuildHost = '') then
320 skBuildHost = 'os2';
321 if (fOptDbg <> 0) then say "dbg: KBUILD_HOST="||skBuildHost
323 if (skBuildHostArch = '') then do
324 select
325 when (skBuildHostCpu = 'i386',
326 | skBuildHostCpu = 'i486',
327 | skBuildHostCpu = 'i586',
328 | skBuildHostCpu = 'i686',
329 | skBuildHostCpu = 'i786',
330 | skBuildHostCpu = 'i886',
331 | skBuildHostCpu = 'i986') then do
332 skBuildHostArch = "x86";
334 otherwise do
335 skBuildHostArch = "x86";
339 if (fOptDbg <> 0) then say "dbg: KBUILD_HOST_ARCH="||skBuildHostArch
341 if (skBuildHostCpu = '') then
342 skBuildHostCpu = 'blend';
343 if (fOptDbg <> 0) then say "dbg: KBUILD_HOST_CPU="||skBuildHostCpu
347 * The target platform.
348 * Defaults to the host when not specified.
350 if (skBuildTarget = '') then
351 skBuildTarget = skBuildHost;
352 if (fOptDbg <> 0) then say "dbg: KBUILD_TARGET="||skBuildTarget
354 if (skBuildTargetArch = '') then
355 skBuildTargetArch = skBuildHostArch;
356 if (fOptDbg <> 0) then say "dbg: KBUILD_TARGET_ARCH="||skBuildTargetArch
358 if (skBuildTargetCpu = '') then do
359 if (skBuildTargetArch = skBuildHostArch) then
360 skBuildTargetCpu = skBuildHostCpu;
361 else
362 skBuildTargetCpu = "blend";
364 if (fOptDbg <> 0) then say "dbg: KBUILD_TARGET_CPU="||skBuildTargetCpu
368 * Determin KBUILD_PATH from the script location and calc KBUILD_BIN_PATH from there.
370 if (skBuildPath = '') then do
371 skBuildPath = GetScriptDir()
373 skBuildPath = translate(skBuildPath, '/', '\')
374 if ( FileExists(skBuildPath||"/footer.kmk") = 0,
375 | FileExists(skBuildPath||"/header.kmk") = 0,
376 | FileExists(skBuildPath||"/rules.kmk") = 0) then do
377 say "error: KBUILD_PATH ("skBuildPath||") is not pointing to a popluated kBuild directory."
378 call SysSleep 1
379 exit 1
381 if (fOptDbg <> 0) then say "dbg: KBUILD_PATH="||skBuildPath;
383 if (skBuildBinPath = '') then do
384 skBuildBinPath = skBuildPath||'/bin/'||skBuildHost||'.'||skBuildHostArch;
386 skBuildBinPath = translate(skBuildBinPath, '/', '\')
387 if (fOptDbg <> 0) then say "dbg: KBUILD_BIN_PATH="||skBuildBinPath;
390 * Add the bin/x.y/ directory to the PATH and BEGINLIBPATH.
391 * NOTE! Once bootstrapped this is the only thing that is actually necessary.
393 sOldPath = EnvGet("PATH");
394 call EnvAddFront 0, "PATH", translate(skBuildBinPath, '\', '/');
395 sNewPath = EnvGet("PATH");
396 call EnvSet 0, "PATH", sOldPath;
397 if (fOptDbg <> 0) then say "dbg: PATH="||sNewPath;
399 sOldBeginLibPath = EnvGet("BEGINLIBPATH");
400 call EnvAddFront 0, "BEGINLIBPATH", translate(skBuildBinPath, '\', '/');
401 sNewBeginLibPath = EnvGet("BEGINLIBPATH");
402 call EnvSet 0, "BEGINLIBPATH", sOldBeginLibPath;
403 if (fOptDbg <> 0) then say "dbg: BEGINLIBPATH="||sNewBeginLibPath;
406 * Sanity check
408 if (DirExists(skBuildBinPath) = 0) then
409 say "warning: The bin directory for the build platform doesn't exist. ("||skBuildBinPath||")";
410 else
412 sPrograms = "kmk kDepPre kDepIDB kmk_append kmk_ash kmk_cat kmk_cp kmk_echo kmk_install kmk_ln kmk_mkdir kmk_mv kmk_redirect kmk_rm kmk_rmdir kmk_sed";
413 do i = 1 to words(sPrograms)
414 sProgram = word(sPrograms, i);
415 if (FileExists(skBuildBinPath||"\"||sProgram||".exe") = 0) then
416 say "warning: The "||sProgram||" program doesn't exit for this platform. ("||skBuildBinPath||")";
422 * The environment is in place, now take the requested action.
424 iRc = 0;
425 if (sOptVars <> '') then
427 if (sOptVars = "all") then
428 sOptVars = "KBUILD_PATH KBUILD_BIN_PATH KBUILD_TYPE ",
429 || "KBUILD_TARGET KBUILD_TARGET_ARCH KBUILD_TARGET_CPU ",
430 || "KBUILD_HOST KBUILD_HOST_ARCH KBUILD_HOST_CPU ";
432 /* Echo variable values or variable export statements. */
433 do i = 1 to words(sOptVars)
434 sVar = word(sOptVars, i)
435 sVal = '';
436 select
437 when (sVar = "PATH") then sVal = sNewPath;
438 when (sVar = "BEGINLIBPATH") then sVal = sNewBeginLibPath;
439 when (sVar = "KBUILD_PATH") then sVal = skBuildPath;
440 when (sVar = "KBUILD_BIN_PATH") then sVal = skBuildBinPath;
441 when (sVar = "KBUILD_TYPE") then sVal = skBuildType;
442 when (sVar = "KBUILD_HOST") then sVal = skBuildHost;
443 when (sVar = "KBUILD_HOST_ARCH") then sVal = skBuildHostArch;
444 when (sVar = "KBUILD_HOST_CPU") then sVal = skBuildHostCpu;
445 when (sVar = "KBUILD_TARGET") then sVal = skBuildTarget;
446 when (sVar = "KBUILD_TARGET_ARCH") then sVal = skBuildTargetArch;
447 when (sVar = "KBUILD_TARGET_CPU") then sVal = skBuildTargetCpu;
448 otherwise do
449 say "error: Unknown variable "||sVar||" specified in --var request."
450 call SysSleep 1
451 exit 1
455 if (fOptValueOnly <> 0) then
456 say sVal
457 else
458 say sShowVarPrefix||sVar||"="||sVal;
461 else
463 /* Wipe out all variables - legacy included - with --default. */
464 if (fOptOverrideAll <> 0) then do
465 call EnvSet 0, KBUILD_PATH, ''
466 call EnvSet 0, KBUILD_BIN_PATH, ''
467 call EnvSet 0, KBUILD_HOST, ''
468 call EnvSet 0, KBUILD_HOST_ARCH, ''
469 call EnvSet 0, KBUILD_HOST_CPU, ''
470 call EnvSet 0, KBUILD_TARGET, ''
471 call EnvSet 0, KBUILD_TARGET_ARCH, ''
472 call EnvSet 0, KBUILD_TARGET_CPU, ''
474 call EnvSet 0, PATH_KBUILD, ''
475 call EnvSet 0, PATH_KBUILD_BIN, ''
476 call EnvSet 0, BUILD_PLATFORM, ''
477 call EnvSet 0, BUILD_PLATFORM_ARCH, ''
478 call EnvSet 0, BUILD_PLATFORM_CPU, ''
479 call EnvSet 0, BUILD_TARGET, ''
480 call EnvSet 0, BUILD_TARGET_ARCH, ''
481 call EnvSet 0, BUILD_TARGET_CPU, ''
484 /* Export the variables. */
485 call EnvSet 0, "PATH", sNewPath
486 call EnvSet 0, "BEGINLIBPATH", sNewBeginLibPath
487 if (fOptOverrideType <> 0) then call EnvSet 0, "KBUILD_TYPE", skBuildType
488 if (fOptFull <> 0) then do
489 call EnvSet 0, KBUILD_PATH, skBuildPath
490 call EnvSet 0, KBUILD_HOST, skBuildHost
491 call EnvSet 0, KBUILD_HOST_ARCH, skBuildHostArch
492 call EnvSet 0, KBUILD_HOST_CPU, skBuildHostCpu
493 call EnvSet 0, KBUILD_TARGET, skBuildTarget
494 call EnvSet 0, KBUILD_TARGET_ARCH, skBuildTargetArch
495 call EnvSet 0, KBUILD_TARGET_CPU, skBuildTargetCpu
497 if (fOptLegacy <> 0) then do
498 call EnvSet 0, PATH_KBUILD, skBuildPath
499 call EnvSet 0, BUILD_PLATFORM, skBuildHost
500 call EnvSet 0, BUILD_PLATFORM_ARCH, skBuildHostArch
501 call EnvSet 0, BUILD_PLATFORM_CPU, skBuildHostCpu
502 call EnvSet 0, BUILD_TARGET, skBuildTarget
503 call EnvSet 0, BUILD_TARGET_ARCH, skBuildTargetArch
504 call EnvSet 0, BUILD_TARGET_CPU, skBuildTargetCpu
509 * Execute left over arguments.
511 if (strip(sCommand) <> '') then do
512 if (fOptQuiet <> 0) then say "info: Executing command: "|| sCommand
513 address CMD sCommand
514 iRc = rc;
515 if (fOptQuiet <> 0 & iRc <> 0) then say "info: rc="||iRc||": "|| sCommand
518 if (fOptDbg <> 0) then say "dbg: finished (rc="||rc||")"
519 exit (iRc);
522 /*******************************************************************************
523 * Procedure Section *
524 *******************************************************************************/
527 * Give the script syntax
529 syntax: procedure
530 say 'syntax: envos2.cmd [command to be executed and its arguments]'
531 say ''
532 return 0;
536 * No value handler
538 NoValueHandler:
539 say 'NoValueHandler: line 'SIGL;
540 exit(16);
545 * Add sToAdd in front of sEnvVar.
546 * Note: sToAdd now is allowed to be alist!
548 * Known features: Don't remove sToAdd from original value if sToAdd
549 * is at the end and don't end with a ';'.
551 EnvAddFront: procedure
552 parse arg fRM, sEnvVar, sToAdd, sSeparator
554 /* sets default separator if not specified. */
555 if (sSeparator = '') then sSeparator = ';';
557 /* checks that sToAdd ends with an ';'. Adds one if not. */
558 if (substr(sToAdd, length(sToAdd), 1) <> sSeparator) then
559 sToAdd = sToAdd || sSeparator;
561 /* check and evt. remove ';' at start of sToAdd */
562 if (substr(sToAdd, 1, 1) = ';') then
563 sToAdd = substr(sToAdd, 2);
565 /* loop thru sToAdd */
566 rc = 0;
567 i = length(sToAdd);
568 do while i > 1 & rc = 0
569 j = lastpos(sSeparator, sToAdd, i-1);
570 rc = EnvAddFront2(fRM, sEnvVar, substr(sToAdd, j+1, i - j), sSeparator);
571 i = j;
574 return rc;
577 * Add sToAdd in front of sEnvVar.
579 * Known features: Don't remove sToAdd from original value if sToAdd
580 * is at the end and don't end with a ';'.
582 EnvAddFront2: procedure
583 parse arg fRM, sEnvVar, sToAdd, sSeparator
585 /* sets default separator if not specified. */
586 if (sSeparator = '') then sSeparator = ';';
588 /* checks that sToAdd ends with a separator. Adds one if not. */
589 if (substr(sToAdd, length(sToAdd), 1) <> sSeparator) then
590 sToAdd = sToAdd || sSeparator;
592 /* check and evt. remove the separator at start of sToAdd */
593 if (substr(sToAdd, 1, 1) = sSeparator) then
594 sToAdd = substr(sToAdd, 2);
596 /* Get original variable value */
597 sOrgEnvVar = EnvGet(sEnvVar);
599 /* Remove previously sToAdd if exists. (Changing sOrgEnvVar). */
600 i = pos(translate(sToAdd), translate(sOrgEnvVar));
601 if (i > 0) then
602 sOrgEnvVar = substr(sOrgEnvVar, 1, i-1) || substr(sOrgEnvVar, i + length(sToAdd));
604 /* set environment */
605 if (fRM) then
606 return EnvSet(0, sEnvVar, sOrgEnvVar);
607 return EnvSet(0, sEnvVar, sToAdd||sOrgEnvVar);
611 * Add sToAdd as the end of sEnvVar.
612 * Note: sToAdd now is allowed to be alist!
614 * Known features: Don't remove sToAdd from original value if sToAdd
615 * is at the end and don't end with a ';'.
617 EnvAddEnd: procedure
618 parse arg fRM, sEnvVar, sToAdd, sSeparator
620 /* sets default separator if not specified. */
621 if (sSeparator = '') then sSeparator = ';';
623 /* checks that sToAdd ends with a separator. Adds one if not. */
624 if (substr(sToAdd, length(sToAdd), 1) <> sSeparator) then
625 sToAdd = sToAdd || sSeparator;
627 /* check and evt. remove ';' at start of sToAdd */
628 if (substr(sToAdd, 1, 1) = sSeparator) then
629 sToAdd = substr(sToAdd, 2);
631 /* loop thru sToAdd */
632 rc = 0;
633 i = length(sToAdd);
634 do while i > 1 & rc = 0
635 j = lastpos(sSeparator, sToAdd, i-1);
636 rc = EnvAddEnd2(fRM, sEnvVar, substr(sToAdd, j+1, i - j), sSeparator);
637 i = j;
640 return rc;
644 * Add sToAdd as the end of sEnvVar.
646 * Known features: Don't remove sToAdd from original value if sToAdd
647 * is at the end and don't end with a ';'.
649 EnvAddEnd2: procedure
650 parse arg fRM, sEnvVar, sToAdd, sSeparator
652 /* sets default separator if not specified. */
653 if (sSeparator = '') then sSeparator = ';';
655 /* checks that sToAdd ends with a separator. Adds one if not. */
656 if (substr(sToAdd, length(sToAdd), 1) <> sSeparator) then
657 sToAdd = sToAdd || sSeparator;
659 /* check and evt. remove separator at start of sToAdd */
660 if (substr(sToAdd, 1, 1) = sSeparator) then
661 sToAdd = substr(sToAdd, 2);
663 /* Get original variable value */
664 sOrgEnvVar = EnvGet(sEnvVar);
666 if (sOrgEnvVar <> '') then
668 /* Remove previously sToAdd if exists. (Changing sOrgEnvVar). */
669 i = pos(translate(sToAdd), translate(sOrgEnvVar));
670 if (i > 0) then
671 sOrgEnvVar = substr(sOrgEnvVar, 1, i-1) || substr(sOrgEnvVar, i + length(sToAdd));
673 /* checks that sOrgEnvVar ends with a separator. Adds one if not. */
674 if (sOrgEnvVar = '') then
675 if (right(sOrgEnvVar,1) <> sSeparator) then
676 sOrgEnvVar = sOrgEnvVar || sSeparator;
679 /* set environment */
680 if (fRM) then return EnvSet(0, sEnvVar, sOrgEnvVar);
681 return EnvSet(0, sEnvVar, sOrgEnvVar||sToAdd);
685 * Sets sEnvVar to sValue.
687 EnvSet: procedure
688 parse arg fRM, sEnvVar, sValue
690 /* if we're to remove this, make valuestring empty! */
691 if (fRM) then
692 sValue = '';
693 sEnvVar = translate(sEnvVar);
696 * Begin/EndLibpath fix:
697 * We'll have to set internal these using both commandline 'SET'
698 * and internal VALUE in order to export it and to be able to
699 * get it (with EnvGet) again.
701 if ((sEnvVar = 'BEGINLIBPATH') | (sEnvVar = 'ENDLIBPATH')) then
703 if (length(sValue) >= 1024) then
704 say 'Warning: 'sEnvVar' is too long,' length(sValue)' char.';
705 return SysSetExtLibPath(sValue, substr(sEnvVar, 1, 1));
708 if (length(sValue) >= 1024) then
710 say 'Warning: 'sEnvVar' is too long,' length(sValue)' char.';
711 say ' This may make CMD.EXE unstable after a SET operation to print the environment.';
713 sRc = VALUE(sEnvVar, sValue, 'OS2ENVIRONMENT');
714 return 0;
718 * Gets the value of sEnvVar.
720 EnvGet: procedure
721 parse arg sEnvVar
722 if ((translate(sEnvVar) = 'BEGINLIBPATH') | (translate(sEnvVar) = 'ENDLIBPATH')) then
723 return SysQueryExtLibPath(substr(sEnvVar, 1, 1));
724 return value(sEnvVar,, 'OS2ENVIRONMENT');
728 * Checks if a file exists.
729 * @param sFile Name of the file to look for.
730 * @param sComplain Complaint text. Complain if non empty and not found.
731 * @returns TRUE if file exists.
732 * FALSE if file doesn't exists.
734 FileExists: procedure
735 parse arg sFile, sComplain
736 rc = stream(sFile, 'c', 'query exist');
737 if ((rc = '') & (sComplain <> '')) then
738 say sComplain ''''sFile'''.';
739 return rc <> '';
743 * Checks if a directory exists.
744 * @param sDir Name of the directory to look for.
745 * @param sComplain Complaint text. Complain if non empty and not found.
746 * @returns TRUE if file exists.
747 * FALSE if file doesn't exists.
749 DirExists: procedure
750 parse arg sDir, sComplain
751 rc = SysFileTree(sDir, 'sDirs', 'DO');
752 if (rc = 0 & sDirs.0 = 1) then
753 return 1;
754 if (sComplain <> '') then
755 say sComplain ''''sDir'''.';
756 return 0;
760 * Workaround for bug in CMD.EXE.
761 * It messes up when REXX have expanded the environment.
763 FixCMDEnv: procedure
764 /* do this anyway
765 /* check for 4OS2 first */
766 Address CMD 'set 4os2test_env=%@eval[2 + 2]';
767 if (value('4os2test_env',, 'OS2ENVIRONMENT') = '4') then
768 return 0;
771 /* force environment expansion by setting a lot of variables and freeing them.
772 * ~6600 (bytes) */
773 do i = 1 to 100
774 Address CMD '@set dummyenvvar'||i'=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
776 do i = 1 to 100
777 Address CMD '@set dummyenvvar'||i'=';
779 return 0;
783 * Translate a string to lower case.
785 ToLower: procedure
786 parse arg sString
787 return translate(sString, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
791 * Gets the script directory.
793 GetScriptDir: procedure
795 * Assuming this script is in the root directory, we can determing
796 * the abs path to it by using the 'parse source' feature in rexx.
798 parse source . . sScript
799 sScriptDir = filespec('drive', sScript) || strip(filespec('path', sScript), 'T', '\');
800 return ToLower(sScriptDir);