GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-process.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2019-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #include <iostream>
27 #include <sstream>
28 
29 #include "oct-process.h"
30 #include "procstream.h"
31 #include "sysdep.h"
32 #include "oct-syscalls.h"
33 
34 namespace octave
35 {
36  OCTINTERP_API
37  process_execution_result
39  const std::string& stdout_output)
40  {
42  }
43 
44  OCTINTERP_API
46  process_execution_result::of_error (int status, const std::string& err_msg)
47  {
48  return process_execution_result (status, -1, "", err_msg);
49  }
50 
51  // Execute a shell command, returning results as a C++ object
52  OCTINTERP_API
54  run_command_and_return_output (const std::string& cmd_str)
55  {
56  iprocstream cmd (cmd_str.c_str ());
57 
58  if (! cmd)
59  {
60  std::string msg = "unable to start subprocess for '" + cmd_str + "'";
61 
62  return process_execution_result::of_error (-1, msg);
63  }
64 
65  std::ostringstream output_buf;
66 
67  char ch;
68 
69  for (;;)
70  {
71  if (cmd.get (ch))
72  output_buf.put (ch);
73  else
74  {
75  if (! cmd.eof () && errno == EAGAIN)
76  cmd.clear ();
77  else
78  break;
79  }
80  }
81 
82  int cmd_status = cmd.close ();
83 
84  if (sys::wifexited (cmd_status))
85  cmd_status = sys::wexitstatus (cmd_status);
86  else
87  cmd_status = 127;
88 
89  return process_execution_result::of_success (cmd_status, output_buf.str ());
90  }
91 }
std::string err_msg(void) const
Definition: oct-process.h:62
static process_execution_result of_error(int status, const std::string &err_msg)
Definition: oct-process.cc:46
static process_execution_result of_success(int exit_status, const std::string &stdout_output)
Definition: oct-process.cc:38
std::string stdout_output(void) const
Definition: oct-process.h:64
int close(void)
Definition: procstream.cc:60
int wexitstatus(int status)
bool wifexited(int status)
OCTINTERP_API process_execution_result run_command_and_return_output(const std::string &cmd_str)
Definition: oct-process.cc:54