systemCall.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "systemCall.H"
27 #include "Time.H"
28 #include "dynamicCode.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 namespace functionObjects
36 {
37  defineTypeNameAndDebug(systemCall, 0);
38 
40  (
41  functionObject,
42  systemCall,
43  dictionary
44  );
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
50 
51 Foam::functionObjects::systemCall::systemCall
52 (
53  const word& name,
54  const Time&,
55  const dictionary& dict
56 )
57 :
58  functionObject(name),
59  executeCalls_(),
60  endCalls_(),
61  writeCalls_()
62 {
63  read(dict);
64 }
65 
66 
67 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
68 
70 {}
71 
72 
73 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
74 
76 {
77  dict.readIfPresent("executeCalls", executeCalls_);
78  dict.readIfPresent("endCalls", endCalls_);
79  dict.readIfPresent("writeCalls", writeCalls_);
80 
81  if (executeCalls_.empty() && endCalls_.empty() && writeCalls_.empty())
82  {
84  << "no executeCalls, endCalls or writeCalls defined."
85  << endl;
86  }
88  {
90  << "Executing user-supplied system calls is not enabled by "
91  << "default because of " << nl
92  << "security issues. If you trust the case you can enable this "
93  << "facility by " << nl
94  << "adding to the InfoSwitches setting in the system controlDict:"
95  << nl << nl
96  << " allowSystemOperations 1" << nl << nl
97  << "The system controlDict is either" << nl << nl
98  << " ~/.OpenFOAM/$WM_PROJECT_VERSION/controlDict" << nl << nl
99  << "or" << nl << nl
100  << " $WM_PROJECT_DIR/etc/controlDict" << nl << nl
101  << exit(FatalError);
102  }
103 
104  return true;
105 }
106 
107 
109 {
110  forAll(executeCalls_, callI)
111  {
112  Foam::system(executeCalls_[callI]);
113  }
114 
115  return true;
116 }
117 
118 
120 {
121  forAll(endCalls_, callI)
122  {
123  Foam::system(endCalls_[callI]);
124  }
125 
126  return true;
127 }
128 
129 
131 {
132  forAll(writeCalls_, callI)
133  {
134  Foam::system(writeCalls_[callI]);
135  }
136 
137  return true;
138 }
139 
140 
141 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
virtual ~systemCall()
Destructor.
Definition: systemCall.C:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
virtual bool write()
Write, execute the "writeCalls".
Definition: systemCall.C:130
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
Abstract base-class for Time/database function objects.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Macros for easy insertion into run-time selection tables.
bool read(const char *, int32_t &)
Definition: int32IO.C:85
virtual bool execute()
Execute the "executeCalls" at each time-step.
Definition: systemCall.C:108
A class for handling words, derived from string.
Definition: word.H:59
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:161
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
static const char nl
Definition: Ostream.H:265
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool read(const dictionary &)
Read the system calls.
Definition: systemCall.C:75
virtual bool end()
Execute the "endCalls" at the final time-loop.
Definition: systemCall.C:119
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
addToRunTimeSelectionTable(functionObject, add, dictionary)
Namespace for OpenFOAM.
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1217