158 lines
3.5 KiB
C++
158 lines
3.5 KiB
C++
#include <array>
|
|
#include <charconv>
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
namespace
|
|
{
|
|
|
|
bool parse_i64(std::string_view text, std::int64_t& out) noexcept
|
|
{
|
|
if (text.empty()) {
|
|
return false;
|
|
}
|
|
const char* begin = text.data();
|
|
const char* end = text.data() + text.size();
|
|
auto [ptr, ec] = std::from_chars(begin, end, out, 10);
|
|
return ec == std::errc{} && ptr == end;
|
|
}
|
|
|
|
int usage_error(std::string_view msg)
|
|
{
|
|
std::cerr << "VoidProcessHelper: " << msg << '\n';
|
|
return 2;
|
|
}
|
|
|
|
int mode_echo(int argc, char** argv)
|
|
{
|
|
for (int i = 2; i < argc; ++i) {
|
|
if (i > 2) {
|
|
std::cout.put(' ');
|
|
}
|
|
std::cout << argv[i];
|
|
}
|
|
std::cout.put('\n');
|
|
std::cout.flush();
|
|
return 0;
|
|
}
|
|
|
|
int mode_cat()
|
|
{
|
|
std::array<char, 4096> buf{};
|
|
while (std::cin) {
|
|
std::cin.read(buf.data(), static_cast<std::streamsize>(buf.size()));
|
|
const std::streamsize got = std::cin.gcount();
|
|
if (got > 0) {
|
|
std::cout.write(buf.data(), got);
|
|
}
|
|
}
|
|
std::cout.flush();
|
|
return std::cout.good() ? 0 : 1;
|
|
}
|
|
|
|
int mode_seq(int argc, char** argv)
|
|
{
|
|
if (argc != 4) {
|
|
return usage_error("seq requires <start> <end>");
|
|
}
|
|
std::int64_t start = 0;
|
|
std::int64_t finish = 0;
|
|
if (!parse_i64(argv[2], start) || !parse_i64(argv[3], finish)) {
|
|
return usage_error("seq arguments must be integers");
|
|
}
|
|
if (start <= finish) {
|
|
for (std::int64_t v = start;; ++v) {
|
|
std::cout << v << '\n';
|
|
if (v == finish) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
std::cout.flush();
|
|
return 0;
|
|
}
|
|
|
|
int mode_streams()
|
|
{
|
|
std::cout << "to_stdout\n" << std::flush;
|
|
std::cerr << "to_stderr\n" << std::flush;
|
|
return 0;
|
|
}
|
|
|
|
int mode_exit(int argc, char** argv)
|
|
{
|
|
if (argc != 3) {
|
|
return usage_error("exit requires <code>");
|
|
}
|
|
std::int64_t code = 0;
|
|
if (!parse_i64(argv[2], code)) {
|
|
return usage_error("exit code must be an integer");
|
|
}
|
|
return static_cast<int>(code);
|
|
}
|
|
|
|
int mode_lines(int argc, char** argv)
|
|
{
|
|
bool no_newline = false;
|
|
bool to_stderr = false;
|
|
int idx = 2;
|
|
while (idx < argc) {
|
|
const std::string_view arg{argv[idx]};
|
|
if (arg == "--no-newline") {
|
|
no_newline = true;
|
|
++idx;
|
|
} else if (arg == "--stderr") {
|
|
to_stderr = true;
|
|
++idx;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
std::ostream& os = to_stderr ? std::cerr : std::cout;
|
|
const int count = argc - idx;
|
|
for (int i = idx; i < argc; ++i) {
|
|
os << argv[i];
|
|
const bool is_last = (i == argc - 1);
|
|
if (!is_last || !no_newline) {
|
|
os.put('\n');
|
|
}
|
|
}
|
|
if (count == 0 && !no_newline) {
|
|
os.put('\n');
|
|
}
|
|
os.flush();
|
|
return 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
std::ios::sync_with_stdio(false);
|
|
if (argc < 2) {
|
|
return usage_error("missing mode");
|
|
}
|
|
const std::string_view mode{argv[1]};
|
|
if (mode == "echo") {
|
|
return mode_echo(argc, argv);
|
|
}
|
|
if (mode == "cat") {
|
|
return mode_cat();
|
|
}
|
|
if (mode == "seq") {
|
|
return mode_seq(argc, argv);
|
|
}
|
|
if (mode == "streams") {
|
|
return mode_streams();
|
|
}
|
|
if (mode == "exit") {
|
|
return mode_exit(argc, argv);
|
|
}
|
|
if (mode == "lines") {
|
|
return mode_lines(argc, argv);
|
|
}
|
|
return usage_error("unknown mode");
|
|
}
|