ATLAS Offline Software
ShellExec.cxx
Go to the documentation of this file.
1 //
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 
6 // Please feel free to contact me (krumnack@iastate.edu) for bug
7 // reports, feature suggestions, praise and complaints.
8 
9 
10 //
11 // includes
12 //
13 
15 
16 #include <cstdio>
17 #include <vector>
18 #include <TSystem.h>
19 #include <RootCoreUtils/ThrowMsg.h>
20 
21 //
22 // method implementations
23 //
24 
25 namespace RCU
26 {
27  namespace Shell
28  {
29  void exec (const std::string& cmd)
30  {
31  if (gSystem->Exec (cmd.c_str()) != 0)
32  RCU_THROW_MSG ("command failed: " + cmd);
33  }
34 
35 
36 
37  std::string exec_read (const std::string& cmd)
38  {
39  int rc = 0;
40  std::string result = exec_read (cmd, rc);
41  if (rc != 0)
42  RCU_THROW_MSG ("command failed: " + cmd + "\nwith output:\n" + result);
43  return result;
44  }
45 
46 
47 
48  std::string exec_read (const std::string& cmd, int& rc)
49  {
50  std::string result;
51  FILE *pipe = 0;
52  try
53  {
54  std::vector<char> buffer (1024);
55  size_t read;
56 
57  pipe = popen (cmd.c_str(), "r");
58  while ((read = fread (&buffer[0], 1, buffer.size(), pipe)) > 0)
59  {
60  result.append (&buffer[0], read);
61  }
62  rc = pclose (pipe);
63  pipe = nullptr;
64  return result;
65  } catch (...)
66  {
67  if (pipe)
68  rc = pclose (pipe);
69  throw;
70  }
71  }
72 
73 
74 
75  std::string quote (const std::string& name)
76  {
77  std::string result;
78  for (std::string::const_iterator iter = name.begin(),
79  end = name.end(); iter != end; ++ iter)
80  {
81  if (!isalnum (*iter) && *iter != '/' && *iter != '.' &&
82  *iter != '-')
83  result += '\\';
84  result += *iter;
85  };
86  if (result.empty())
87  result = "\"\"";
88  return result;
89  }
90  }
91 }
read
IovVectorMap_t read(const Folder &theFolder, const SelectionCriterion &choice, const unsigned int limit=10)
Definition: openCoraCool.cxx:569
get_generator_info.result
result
Definition: get_generator_info.py:21
rerun_display.cmd
string cmd
Definition: rerun_display.py:67
RCU
This module defines a variety of assert style macros.
Definition: Assert.cxx:26
ShellExec.h
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:12
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ThrowMsg.h
RCU::Shell::exec_read
std::string exec_read(const std::string &cmd)
effects: execute the given command and return the output returns: the output of the command guarantee...
Definition: ShellExec.cxx:37
RCU::Shell::exec
void exec(const std::string &cmd)
effects: execute the given command guarantee: strong failures: out of memory II failures: system fail...
Definition: ShellExec.cxx:29
RCU_THROW_MSG
#define RCU_THROW_MSG(message)
Definition: PrintMsg.h:58
RCU::Shell::quote
std::string quote(const std::string &name)
effects: quote the given name to protect it from the shell returns: the quoted name guarantee: strong...
Definition: ShellExec.cxx:75