diff --git a/applications/test/foamEnv/Make/files b/applications/test/foamEnv/Make/files new file mode 100644 index 0000000000..b67805d076 --- /dev/null +++ b/applications/test/foamEnv/Make/files @@ -0,0 +1,3 @@ +Test-foamEnv.C + +EXE = $(FOAM_USER_APPBIN)/Test-foamEnv diff --git a/applications/test/foamEnv/Make/options b/applications/test/foamEnv/Make/options new file mode 100644 index 0000000000..18e6fe47af --- /dev/null +++ b/applications/test/foamEnv/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = */ +/* EXE_LIBS = */ diff --git a/applications/test/foamEnv/Test-foamEnv.C b/applications/test/foamEnv/Test-foamEnv.C new file mode 100644 index 0000000000..0555c8be11 --- /dev/null +++ b/applications/test/foamEnv/Test-foamEnv.C @@ -0,0 +1,79 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Application + Test-etcFiles + +Description + Test etcFiles functionality. + Similar to foamEtcFile script, but automatically prunes nonexistent + directories from the list. + +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "OSspecific.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noBanner(); + argList::noParallel(); + argList::noFunctionObjects(); + argList::removeOption("case"); + + argList::addArgument("env..."); + + argList::addNote + ( + "Simple test/report OpenFOAM environment" + ); + + argList args(argc, argv, false, true); + + for (int argi = 1; argi < args.size(); ++argi) + { + const std::string envName(args[argi]); + + if (hasEnv(envName)) + { + Info<<"Have env " << envName.c_str() << "=" << getEnv(envName) + << nl; + } + else + { + Info<<"No env " << envName.c_str()<< nl; + } + } + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OSspecific/MSwindows/MSwindows.C b/src/OSspecific/MSwindows/MSwindows.C index 6afa4e03f2..46f2ae99bb 100644 --- a/src/OSspecific/MSwindows/MSwindows.C +++ b/src/OSspecific/MSwindows/MSwindows.C @@ -7,7 +7,7 @@ ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation Copyright (C) 2011 Symscape - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -360,7 +360,7 @@ pid_t Foam::pgid() } -bool Foam::env(const std::string& envName) +bool Foam::hasEnv(const std::string& envName) { // An empty envName => always false return !envName.empty() && diff --git a/src/OSspecific/POSIX/POSIX.C b/src/OSspecific/POSIX/POSIX.C index e32a27c1fa..2ce70e3718 100644 --- a/src/OSspecific/POSIX/POSIX.C +++ b/src/OSspecific/POSIX/POSIX.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -263,7 +263,7 @@ pid_t Foam::pgid() } -bool Foam::env(const std::string& envName) +bool Foam::hasEnv(const std::string& envName) { // An empty envName => always false return !envName.empty() && ::getenv(envName.c_str()) != nullptr; diff --git a/src/OpenFOAM/db/error/IOerror.C b/src/OpenFOAM/db/error/IOerror.C index c0cacb3185..9ec6377dff 100644 --- a/src/OpenFOAM/db/error/IOerror.C +++ b/src/OpenFOAM/db/error/IOerror.C @@ -187,7 +187,7 @@ void Foam::IOerror::exitOrAbort(const int, const bool isAbort) } } - if (env("FOAM_ABORT")) + if (hasEnv("FOAM_ABORT")) { Perr<< nl << *this << nl << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; @@ -246,7 +246,7 @@ void Foam::IOerror::exitOrAbort(const int, const bool isAbort) void Foam::IOerror::exit(const int) { - exitOrAbort(1, env("FOAM_ABORT")); + exitOrAbort(1, hasEnv("FOAM_ABORT")); } diff --git a/src/OpenFOAM/db/error/error.C b/src/OpenFOAM/db/error/error.C index dff61cfac9..897b80a4ca 100644 --- a/src/OpenFOAM/db/error/error.C +++ b/src/OpenFOAM/db/error/error.C @@ -238,7 +238,7 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort) } } - if (env("FOAM_ABORT")) + if (hasEnv("FOAM_ABORT")) { Perr<< nl << *this << nl << "\nFOAM aborting (FOAM_ABORT set)\n" << endl; @@ -297,7 +297,7 @@ void Foam::error::exitOrAbort(const int errNo, const bool isAbort) void Foam::error::exit(const int errNo) { - exitOrAbort(errNo, env("FOAM_ABORT")); + exitOrAbort(errNo, hasEnv("FOAM_ABORT")); } diff --git a/src/OpenFOAM/include/OSspecific.H b/src/OpenFOAM/include/OSspecific.H index 0c4fc94c55..6c2a96f399 100644 --- a/src/OpenFOAM/include/OSspecific.H +++ b/src/OpenFOAM/include/OSspecific.H @@ -66,16 +66,21 @@ pid_t pgid(); //- True if environment variable of given name is defined. // Using an empty name is a no-op and always returns false. -bool env(const std::string& envName); +bool hasEnv(const std::string& envName); //- Get environment value for given envName. -// Return string() if the environment is undefined or envName is empty. +// \return empty string if environment is undefined or envName is empty. string getEnv(const std::string& envName); //- Set an environment variable, return true on success. // Using an empty name is a no-op and always returns false. bool setEnv(const word& name, const std::string& value, const bool overwrite); +//- Deprecated(2020-05) check for existence of environment variable +// \deprecated(2020-05) - use hasEnv() function +FOAM_DEPRECATED_FOR(2020-05, "hasEnv() function") +inline bool env(const std::string& envName) { return Foam::hasEnv(envName); } + //- Return the system's host name, as per hostname(1) // Optionally with the full name (as per the '-f' option) string hostName(const bool full=false);