GNU Octave  8.1.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-2023 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 
35 
38  const std::string& stdout_output)
39 {
41 }
42 
44 process_execution_result::of_error (int status, const std::string& err_msg)
45 {
46  return process_execution_result (status, -1, "", err_msg);
47 }
48 
49 // Execute a shell command, returning results as a C++ object
51 run_command_and_return_output (const std::string& cmd_str)
52 {
53  iprocstream cmd (cmd_str.c_str ());
54 
55  if (! cmd)
56  {
57  std::string msg = "unable to start subprocess for '" + cmd_str + "'";
58 
59  return process_execution_result::of_error (-1, msg);
60  }
61 
62  std::ostringstream output_buf;
63 
64  char ch;
65 
66  for (;;)
67  {
68  if (cmd.get (ch))
69  output_buf.put (ch);
70  else
71  {
72  if (! cmd.eof () && errno == EAGAIN)
73  cmd.clear ();
74  else
75  break;
76  }
77  }
78 
79  int cmd_status = cmd.close ();
80 
81  if (sys::wifexited (cmd_status))
82  cmd_status = sys::wexitstatus (cmd_status);
83  else
84  cmd_status = 127;
85 
86  return process_execution_result::of_success (cmd_status, output_buf.str ());
87 }
88 
OCTAVE_END_NAMESPACE(octave)
int status(void) const
Definition: oct-process.h:57
int exit_status(void) const
Definition: oct-process.h:59
std::string stdout_output(void) const
Definition: oct-process.h:63
static OCTINTERP_API process_execution_result of_success(int exit_status, const std::string &stdout_output)
Definition: oct-process.cc:37
static OCTINTERP_API process_execution_result of_error(int status, const std::string &err_msg)
Definition: oct-process.cc:44
std::string err_msg(void) const
Definition: oct-process.h:61
int close(void)
Definition: procstream.cc:62
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
process_execution_result run_command_and_return_output(const std::string &cmd_str)
Definition: oct-process.cc:51
bool wifexited(int status)
int wexitstatus(int status)