Compare commits

..

No commits in common. "310011a6da8b27d6aa09749658a83c8461db686b" and "b3cd8709fb813367c980ef54e43d4feda7f7a3e8" have entirely different histories.

1 changed files with 11 additions and 26 deletions

View File

@ -18,12 +18,7 @@
#include <cstdint>
#include <cctype>
#define usage std::cerr << "Usage: " << argv[0] << " [-4|-6] [-t|-u] <<host> <port> | -l <port>>" << std::endl << "<port> may be hexadecimal (prefixed with 0x) or binary (prefixed with 0b)." << std::endl
#define EXIT_SUCCESS 0
#define EXIT_RUNTIME 1
#define EXIT_USAGE 2
#define EXIT_UNIMPLEMENTED 3
#define usage std::cerr << "Usage: " << argv[0] << " [-4|-6] [-t|-u] <port>" << std::endl << "<port> may be hexadecimal (prefixed with 0x) or binary (prefixed with 0b)." << std::endl
int main(int argc, char* argv[]){
@ -32,11 +27,10 @@ int main(int argc, char* argv[]){
bool ipv6 = true;
bool tcp = true;
bool udp = true;
bool server = false;
uint16_t port;
if (argc<2) {
usage;
return EXIT_USAGE;
return 1;
}
for (int i=1; i<argc; i++) {
std::string argument(argv[i]);
@ -60,22 +54,13 @@ int main(int argc, char* argv[]){
udp = true;
continue;
}
if (argument=="-l") {
server = true;
continue;
}
if (argument=="-h") {
usage;
return EXIT_SUCCESS;
return 0;
}
if (argument=="--help") {
usage;
return EXIT_SUCCESS;
}
if (!server && i==argc-2) {
//TODO: check if valid host
std::cerr << "Host argument parsing is not yet implemented." << std::endl;
return EXIT_UNIMPLEMENTED;
return 0;
}
if (i==argc-1) {
if (argument.substr(0, 2)=="0b" || argument.substr(0, 2)=="0B") {
@ -86,13 +71,13 @@ int main(int argc, char* argv[]){
} else {
std::cerr << argument << " is not a valid port." << std::endl;
usage;
return EXIT_USAGE;
return 1;
}
}
if (argument.length()>18) {
std::cerr << argument << " is too big for a valid port." << std::endl;
usage;
return EXIT_USAGE;
return 1;
}
port = (uint16_t) std::stoul(argument.substr(2, argument.length()), nullptr, 2);
} else if (argument.substr(0, 2)=="0x" || argument.substr(0, 2)=="0X") {
@ -101,13 +86,13 @@ int main(int argc, char* argv[]){
if (std::isxdigit(digit)==0) {
std::cerr << argument << " is not a valid port." << digit << std::endl;
usage;
return EXIT_USAGE;
return 1;
}
}
if (argument.length()>6) {
std::cerr << argument << " is too big for a valid port." << std::endl;
usage;
return EXIT_USAGE;
return 1;
}
port = (uint16_t) std::stoul(argument.substr(2, argument.length()), nullptr, 16);
} else {
@ -116,7 +101,7 @@ int main(int argc, char* argv[]){
if (std::isdigit(digit)==0) {
std::cerr << argument << " is not a valid port." << std::endl;
usage;
return EXIT_USAGE;
return 1;
}
}
// using unsigned long here because that's how stoul()
@ -125,14 +110,14 @@ int main(int argc, char* argv[]){
if (argumentNumber > 65535) {
std::cerr << argument << " is too big for a valid port." << std::endl;
usage;
return EXIT_USAGE;
return 1;
}
port = (uint16_t) argumentNumber;
}
break; // last argument anyway
}
usage;
return EXIT_USAGE;
return 1;
}
// Argument parsing end ############################################