2019-05-21 13:05:17 +02:00
|
|
|
/* This file is part of remwharead.
|
|
|
|
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdlib>
|
2019-09-22 21:59:46 +02:00
|
|
|
#include <cstring>
|
2019-05-21 13:05:17 +02:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::cin;
|
|
|
|
using std::cout;
|
|
|
|
using std::uint32_t;
|
|
|
|
using std::system;
|
|
|
|
|
2019-10-30 07:47:39 +01:00
|
|
|
string read_input();
|
|
|
|
void send_message(const string &message);
|
|
|
|
int launch(const string &args);
|
|
|
|
|
|
|
|
string read_input()
|
2019-05-21 13:05:17 +02:00
|
|
|
{
|
2019-09-22 21:59:46 +02:00
|
|
|
// Read message length.
|
|
|
|
uint32_t length;
|
|
|
|
char buffer[4];
|
|
|
|
|
|
|
|
cin.read(buffer, sizeof(uint32_t));
|
|
|
|
std::memcpy(&length, buffer, 4);
|
|
|
|
|
|
|
|
// Ignore quotes.
|
|
|
|
length -= 2;
|
|
|
|
cin.ignore(1);
|
|
|
|
|
|
|
|
// Read message.
|
2019-05-21 13:05:17 +02:00
|
|
|
string input;
|
|
|
|
char c;
|
|
|
|
|
2019-09-22 21:59:46 +02:00
|
|
|
for (; length > 0; --length)
|
2019-05-21 13:05:17 +02:00
|
|
|
{
|
2019-09-22 21:59:46 +02:00
|
|
|
cin.read(&c, 1);
|
|
|
|
input += c;
|
2019-05-21 13:05:17 +02:00
|
|
|
}
|
2019-09-22 21:59:46 +02:00
|
|
|
|
2019-05-21 13:05:17 +02:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_message(const string &message)
|
|
|
|
{
|
2019-10-30 07:47:39 +01:00
|
|
|
const auto length = static_cast<uint32_t>(message.length() + 2);
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - Necessary.
|
2019-09-22 21:59:46 +02:00
|
|
|
cout.write(reinterpret_cast<const char*>(&length), sizeof(uint32_t));
|
2019-05-21 13:05:17 +02:00
|
|
|
cout << '"' << message << '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
int launch(const string &args)
|
|
|
|
{
|
|
|
|
const string cmd = "remwharead " + args + " 2>/dev/null";
|
2019-10-30 07:47:39 +01:00
|
|
|
// NOLINTNEXTLINE(cert-env33-c) - We get the arguments in a string.
|
2019-05-21 13:05:17 +02:00
|
|
|
int ret = system(cmd.c_str());
|
2019-10-30 07:47:39 +01:00
|
|
|
if (WIFEXITED(ret)) // NOLINT(hicpp-signed-bitwise) - Necessary.
|
2019-05-21 13:05:17 +02:00
|
|
|
{
|
2019-10-30 07:47:39 +01:00
|
|
|
ret = WEXITSTATUS(ret); // NOLINT(hicpp-signed-bitwise) - Necessary.
|
2019-05-21 13:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2019-09-22 20:58:59 +02:00
|
|
|
const string args = read_input();
|
2019-05-26 18:02:47 +02:00
|
|
|
|
2019-09-22 22:05:44 +02:00
|
|
|
const int ret = launch(args);
|
2019-05-21 13:05:17 +02:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
2019-09-22 20:58:59 +02:00
|
|
|
send_message("Command successful.");
|
2019-05-21 13:05:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-22 20:58:59 +02:00
|
|
|
send_message("Command failed with status: "
|
|
|
|
+ std::to_string(ret) + '.');
|
2019-05-21 13:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|