Skip to content

Commit

Permalink
Add command line options, output cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
i-ky committed Feb 25, 2023
1 parent 1b8beda commit 5a52b76
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/basset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,55 @@
#include <csignal>
#include <fstream>
#include <iostream>
#include <ostream>
#include <string>

using std::cerr;
using std::cout;
using std::ifstream;
using std::ostream;
using std::string;
using std::to_string;
using std::literals::string_literals::operator""s;

int main(int argc, char *argv[]) {
const string progname{*argv++};

auto usage = [progname](ostream &stream) {
stream << progname << " [options] -- ...\n";
};

bool verbose{false};

while (*argv != nullptr) {
if (*argv == "--"s) {
break;
}

if (*argv == "--help"s) {
usage(cout);
return 0;
}

if (*argv == "--verbose"s) {
verbose = true;
} else if (*argv == "--no-verbose"s) {
verbose = false;
} else {
cerr << "unsupported option: " << *argv << '\n';
usage(cerr);
return -1;
}

argv++;
}

if (*argv == nullptr) {
cerr << "unexpected end of arguments\n";
usage(cerr);
return -1;
}

argv++;

if (auto pid = fork()) {
Expand Down Expand Up @@ -61,9 +102,9 @@ int main(int argc, char *argv[]) {
}

if (WIFEXITED(wstatus) || WIFSIGNALED(wstatus)) {
cerr << pid << " exited/terminated by signal\n";
verbose &&cerr << pid << " exited/terminated by signal\n";
} else if (WIFSTOPPED(wstatus)) {
cerr << pid << " stopped\n";
verbose &&cerr << pid << " stopped\n";

if (WSTOPSIG(wstatus) == SIGTRAP) {
switch (wstatus >> 16) {
Expand Down Expand Up @@ -98,6 +139,7 @@ int main(int argc, char *argv[]) {

if (!cmdline.eof()) {
cerr << "failed to read /proc/[pid]/cmdline\n";
return -1;
}

break;
Expand All @@ -111,15 +153,15 @@ int main(int argc, char *argv[]) {
return -1;
}
} else {
cerr << "got signal: " << WSTOPSIG(wstatus) << '\n';
verbose &&cerr << "got signal: " << WSTOPSIG(wstatus) << '\n';
}

if (ptrace(PTRACE_CONT, pid, nullptr, nullptr) == -1) {
perror("cannot ptrace(PTRACE_CONT)");
return -1;
}
} else if (WIFCONTINUED(wstatus)) {
cerr << pid << " continued\n";
verbose &&cerr << pid << " continued\n";
} else {
cerr << "unexpected wait status: " << wstatus << '\n';
return -1;
Expand Down

0 comments on commit 5a52b76

Please sign in to comment.