2007-04-28 23:47:03 +00:00
|
|
|
/*
|
2011-01-04 01:06:56 +00:00
|
|
|
* Copyright 2005-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
|
2007-04-28 23:47:03 +00:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "external_commands.h"
|
|
|
|
|
2011-01-04 01:06:56 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2007-04-28 23:47:03 +00:00
|
|
|
|
|
|
|
|
2011-01-04 01:06:56 +00:00
|
|
|
static FILE*
|
|
|
|
get_input()
|
2007-04-28 23:47:03 +00:00
|
|
|
{
|
2011-01-04 01:06:56 +00:00
|
|
|
static FILE* sInput = fdopen(3, "r");
|
|
|
|
return sInput;
|
2007-04-28 23:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-04 01:06:56 +00:00
|
|
|
static FILE*
|
|
|
|
get_output()
|
2007-04-28 23:47:03 +00:00
|
|
|
{
|
2011-01-04 01:06:56 +00:00
|
|
|
static FILE* sOutput = fdopen(4, "w");
|
|
|
|
return sOutput;
|
2007-04-28 23:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-04 01:06:56 +00:00
|
|
|
bool
|
|
|
|
FSShell::get_external_command(char* buffer, int size)
|
2007-04-28 23:47:03 +00:00
|
|
|
{
|
2011-01-04 01:06:56 +00:00
|
|
|
// get the input stream
|
|
|
|
FILE* in = get_input();
|
|
|
|
if (in == NULL) {
|
|
|
|
fprintf(stderr, "Error: Failed to open command input: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return false;
|
2007-04-28 23:47:03 +00:00
|
|
|
}
|
|
|
|
|
2011-01-04 01:06:56 +00:00
|
|
|
while (true) {
|
|
|
|
// read a command line
|
|
|
|
if (fgets(buffer, size, in) != NULL)
|
|
|
|
return true;
|
2007-04-28 23:47:03 +00:00
|
|
|
|
2011-01-04 01:06:56 +00:00
|
|
|
// when interrupted, try again
|
|
|
|
if (errno != EINTR)
|
2007-04-28 23:47:03 +00:00
|
|
|
return false;
|
2011-01-04 01:06:56 +00:00
|
|
|
}
|
2007-04-28 23:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
FSShell::reply_to_external_command(int result)
|
|
|
|
{
|
2011-01-04 01:06:56 +00:00
|
|
|
// get the output stream
|
|
|
|
FILE* out = get_output();
|
|
|
|
if (out == NULL) {
|
|
|
|
fprintf(stderr, "Error: Failed to open command output: %s\n",
|
|
|
|
strerror(errno));
|
2007-04-28 23:47:03 +00:00
|
|
|
return;
|
2011-01-04 01:06:56 +00:00
|
|
|
}
|
2007-04-28 23:47:03 +00:00
|
|
|
|
2011-01-04 01:06:56 +00:00
|
|
|
if (fprintf(out, "%d\n", result) < 0 || fflush(out) == EOF) {
|
|
|
|
fprintf(stderr, "Error: Failed to write command reply to output reply: "
|
|
|
|
"%s\n", strerror(errno));
|
|
|
|
}
|
2007-04-28 23:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
FSShell::external_command_cleanup()
|
|
|
|
{
|
2011-01-04 01:06:56 +00:00
|
|
|
// The file will be closed automatically when the team exits.
|
2007-04-28 23:47:03 +00:00
|
|
|
}
|