Util: inherited 'execute' from Taskwarrior.

This commit is contained in:
Paul Beckingham 2015-05-24 11:03:38 -04:00
parent 6f9a4de3bf
commit 4be49a2d72
3 changed files with 221 additions and 1 deletions

View file

@ -11,7 +11,8 @@ set (tasksh_SRCS diag.cpp
Path.cpp Path.h
File.cpp File.h
Directory.cpp Directory.h
text.cpp text.h)
text.cpp text.h
util.cpp util.h)
add_executable (tasksh_executable main.cpp ${tasksh_SRCS})
target_link_libraries (tasksh_executable ${TASKSH_LIBRARIES})

185
src/util.cpp Normal file
View file

@ -0,0 +1,185 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <vector>
#include <string>
#include <unistd.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <util.h>
////////////////////////////////////////////////////////////////////////////////
// Run a binary with args, capturing output.
int execute (
const std::string& executable,
const std::vector <std::string>& args,
const std::string& input,
std::string& output)
{
pid_t pid;
int pin[2], pout[2];
fd_set rfds, wfds;
struct timeval tv;
int select_retval, read_retval, write_retval;
char buf[16384];
unsigned int written;
const char* input_cstr = input.c_str ();
if (signal (SIGPIPE, SIG_IGN) == SIG_ERR) // Handled locally with EPIPE.
throw std::string (std::strerror (errno));
if (pipe (pin) == -1)
throw std::string (std::strerror (errno));
if (pipe (pout) == -1)
throw std::string (std::strerror (errno));
if ((pid = fork ()) == -1)
throw std::string (std::strerror (errno));
if (pid == 0)
{
// This is only reached in the child
close (pin[1]); // Close the write end of the input pipe.
close (pout[0]); // Close the read end of the output pipe.
// Parent writes to pin[1]. Set read end pin[0] as STDIN for child.
if (dup2 (pin[0], STDIN_FILENO) == -1)
throw std::string (std::strerror (errno));
close (pin[0]);
// Parent reads from pout[0]. Set write end pout[1] as STDOUT for child.
if (dup2 (pout[1], STDOUT_FILENO) == -1)
throw std::string (std::strerror (errno));
close (pout[1]);
// Add executable as argv[0] and NULL-terminate the array for execvp().
char** argv = new char* [args.size () + 2];
argv[0] = (char*) executable.c_str ();
for (unsigned int i = 0; i < args.size (); ++i)
argv[i+1] = (char*) args[i].c_str ();
argv[args.size () + 1] = NULL;
_exit (execvp (executable.c_str (), argv));
}
// This is only reached in the parent
close (pin[0]); // Close the read end of the input pipe.
close (pout[1]); // Close the write end of the output pipe.
if (input.size () == 0)
{
// Nothing to send to the child, close the pipe early.
close (pin[1]);
}
read_retval = -1;
written = 0;
while (read_retval != 0 || input.size () != written)
{
FD_ZERO (&rfds);
if (read_retval != 0)
FD_SET (pout[0], &rfds);
FD_ZERO (&wfds);
if (input.size () != written)
FD_SET (pin[1], &wfds);
// On Linux, tv may be overwritten by select(). Reset it each time.
// NOTE: Timeout chosen arbitrarily - we don't time out execute() calls.
// select() is run over and over again unless the child exits or closes
// its pipes.
tv.tv_sec = 5;
tv.tv_usec = 0;
select_retval = select (std::max (pout[0], pin[1]) + 1, &rfds, &wfds, NULL, &tv);
if (select_retval == -1)
throw std::string (std::strerror (errno));
// Write data to child's STDIN
if (FD_ISSET (pin[1], &wfds))
{
write_retval = write (pin[1], input_cstr + written, input.size () - written);
if (write_retval == -1)
{
if (errno == EPIPE)
{
// Child died (or closed the pipe) before reading all input.
// We don't really care; pretend we wrote it all.
write_retval = input.size () - written;
}
else
{
throw std::string (std::strerror (errno));
}
}
written += write_retval;
if (written == input.size ())
{
// Let the child know that no more input is coming by closing the pipe.
close (pin[1]);
}
}
// Read data from child's STDOUT
if (FD_ISSET (pout[0], &rfds))
{
read_retval = read (pout[0], &buf, sizeof (buf) - 1);
if (read_retval == -1)
throw std::string (std::strerror (errno));
buf[read_retval] = '\0';
output += buf;
}
}
close (pout[0]); // Close the read end of the output pipe.
int status = -1;
if (wait (&status) == -1)
throw std::string (std::strerror (errno));
if (WIFEXITED (status))
{
status = WEXITSTATUS (status);
}
else
{
throw std::string ("Error: Could not get exit status!");
}
if (signal (SIGPIPE, SIG_DFL) == SIG_ERR) // We're done, return to default.
throw std::string (std::strerror (errno));
return status;
}
////////////////////////////////////////////////////////////////////////////////

34
src/util.h Normal file
View file

@ -0,0 +1,34 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_UTIL
#define INCLUDED_UTIL
// util.cpp
int execute (const std::string&, const std::vector <std::string>&, const std::string&, std::string&);
#endif
////////////////////////////////////////////////////////////////////////////////