Compare commits
No commits in common. "310011a6da8b27d6aa09749658a83c8461db686b" and "b3cd8709fb813367c980ef54e43d4feda7f7a3e8" have entirely different histories.
310011a6da
...
b3cd8709fb
|
@ -18,12 +18,7 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cctype>
|
#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 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
|
||||||
|
|
||||||
#define EXIT_SUCCESS 0
|
|
||||||
#define EXIT_RUNTIME 1
|
|
||||||
#define EXIT_USAGE 2
|
|
||||||
#define EXIT_UNIMPLEMENTED 3
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]){
|
int main(int argc, char* argv[]){
|
||||||
|
|
||||||
|
@ -32,11 +27,10 @@ int main(int argc, char* argv[]){
|
||||||
bool ipv6 = true;
|
bool ipv6 = true;
|
||||||
bool tcp = true;
|
bool tcp = true;
|
||||||
bool udp = true;
|
bool udp = true;
|
||||||
bool server = false;
|
|
||||||
uint16_t port;
|
uint16_t port;
|
||||||
if (argc<2) {
|
if (argc<2) {
|
||||||
usage;
|
usage;
|
||||||
return EXIT_USAGE;
|
return 1;
|
||||||
}
|
}
|
||||||
for (int i=1; i<argc; i++) {
|
for (int i=1; i<argc; i++) {
|
||||||
std::string argument(argv[i]);
|
std::string argument(argv[i]);
|
||||||
|
@ -60,22 +54,13 @@ int main(int argc, char* argv[]){
|
||||||
udp = true;
|
udp = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (argument=="-l") {
|
|
||||||
server = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (argument=="-h") {
|
if (argument=="-h") {
|
||||||
usage;
|
usage;
|
||||||
return EXIT_SUCCESS;
|
return 0;
|
||||||
}
|
}
|
||||||
if (argument=="--help") {
|
if (argument=="--help") {
|
||||||
usage;
|
usage;
|
||||||
return EXIT_SUCCESS;
|
return 0;
|
||||||
}
|
|
||||||
if (!server && i==argc-2) {
|
|
||||||
//TODO: check if valid host
|
|
||||||
std::cerr << "Host argument parsing is not yet implemented." << std::endl;
|
|
||||||
return EXIT_UNIMPLEMENTED;
|
|
||||||
}
|
}
|
||||||
if (i==argc-1) {
|
if (i==argc-1) {
|
||||||
if (argument.substr(0, 2)=="0b" || argument.substr(0, 2)=="0B") {
|
if (argument.substr(0, 2)=="0b" || argument.substr(0, 2)=="0B") {
|
||||||
|
@ -86,13 +71,13 @@ int main(int argc, char* argv[]){
|
||||||
} else {
|
} else {
|
||||||
std::cerr << argument << " is not a valid port." << std::endl;
|
std::cerr << argument << " is not a valid port." << std::endl;
|
||||||
usage;
|
usage;
|
||||||
return EXIT_USAGE;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (argument.length()>18) {
|
if (argument.length()>18) {
|
||||||
std::cerr << argument << " is too big for a valid port." << std::endl;
|
std::cerr << argument << " is too big for a valid port." << std::endl;
|
||||||
usage;
|
usage;
|
||||||
return EXIT_USAGE;
|
return 1;
|
||||||
}
|
}
|
||||||
port = (uint16_t) std::stoul(argument.substr(2, argument.length()), nullptr, 2);
|
port = (uint16_t) std::stoul(argument.substr(2, argument.length()), nullptr, 2);
|
||||||
} else if (argument.substr(0, 2)=="0x" || argument.substr(0, 2)=="0X") {
|
} 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) {
|
if (std::isxdigit(digit)==0) {
|
||||||
std::cerr << argument << " is not a valid port." << digit << std::endl;
|
std::cerr << argument << " is not a valid port." << digit << std::endl;
|
||||||
usage;
|
usage;
|
||||||
return EXIT_USAGE;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (argument.length()>6) {
|
if (argument.length()>6) {
|
||||||
std::cerr << argument << " is too big for a valid port." << std::endl;
|
std::cerr << argument << " is too big for a valid port." << std::endl;
|
||||||
usage;
|
usage;
|
||||||
return EXIT_USAGE;
|
return 1;
|
||||||
}
|
}
|
||||||
port = (uint16_t) std::stoul(argument.substr(2, argument.length()), nullptr, 16);
|
port = (uint16_t) std::stoul(argument.substr(2, argument.length()), nullptr, 16);
|
||||||
} else {
|
} else {
|
||||||
|
@ -116,7 +101,7 @@ int main(int argc, char* argv[]){
|
||||||
if (std::isdigit(digit)==0) {
|
if (std::isdigit(digit)==0) {
|
||||||
std::cerr << argument << " is not a valid port." << std::endl;
|
std::cerr << argument << " is not a valid port." << std::endl;
|
||||||
usage;
|
usage;
|
||||||
return EXIT_USAGE;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// using unsigned long here because that's how stoul()
|
// using unsigned long here because that's how stoul()
|
||||||
|
@ -125,14 +110,14 @@ int main(int argc, char* argv[]){
|
||||||
if (argumentNumber > 65535) {
|
if (argumentNumber > 65535) {
|
||||||
std::cerr << argument << " is too big for a valid port." << std::endl;
|
std::cerr << argument << " is too big for a valid port." << std::endl;
|
||||||
usage;
|
usage;
|
||||||
return EXIT_USAGE;
|
return 1;
|
||||||
}
|
}
|
||||||
port = (uint16_t) argumentNumber;
|
port = (uint16_t) argumentNumber;
|
||||||
}
|
}
|
||||||
break; // last argument anyway
|
break; // last argument anyway
|
||||||
}
|
}
|
||||||
usage;
|
usage;
|
||||||
return EXIT_USAGE;
|
return 1;
|
||||||
}
|
}
|
||||||
// Argument parsing end ############################################
|
// Argument parsing end ############################################
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue