tools/hexnet: replace exit codes with macros to improve readability
parent
6a5096dfa3
commit
310011a6da
|
@ -20,6 +20,11 @@
|
||||||
|
|
||||||
#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] <<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
|
||||||
|
|
||||||
int main(int argc, char* argv[]){
|
int main(int argc, char* argv[]){
|
||||||
|
|
||||||
// Argument parsing ################################################
|
// Argument parsing ################################################
|
||||||
|
@ -31,7 +36,7 @@ int main(int argc, char* argv[]){
|
||||||
uint16_t port;
|
uint16_t port;
|
||||||
if (argc<2) {
|
if (argc<2) {
|
||||||
usage;
|
usage;
|
||||||
return 1;
|
return EXIT_USAGE;
|
||||||
}
|
}
|
||||||
for (int i=1; i<argc; i++) {
|
for (int i=1; i<argc; i++) {
|
||||||
std::string argument(argv[i]);
|
std::string argument(argv[i]);
|
||||||
|
@ -61,16 +66,16 @@ int main(int argc, char* argv[]){
|
||||||
}
|
}
|
||||||
if (argument=="-h") {
|
if (argument=="-h") {
|
||||||
usage;
|
usage;
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
if (argument=="--help") {
|
if (argument=="--help") {
|
||||||
usage;
|
usage;
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
if (!server && i==argc-2) {
|
if (!server && i==argc-2) {
|
||||||
//TODO: check if valid host
|
//TODO: check if valid host
|
||||||
std::cerr << "Host argument parsing is not yet implemented." << std::endl;
|
std::cerr << "Host argument parsing is not yet implemented." << std::endl;
|
||||||
return 3;
|
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") {
|
||||||
|
@ -81,13 +86,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 1;
|
return EXIT_USAGE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 1;
|
return EXIT_USAGE;
|
||||||
}
|
}
|
||||||
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") {
|
||||||
|
@ -96,13 +101,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 1;
|
return EXIT_USAGE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 1;
|
return EXIT_USAGE;
|
||||||
}
|
}
|
||||||
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 {
|
||||||
|
@ -111,7 +116,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 1;
|
return EXIT_USAGE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// using unsigned long here because that's how stoul()
|
// using unsigned long here because that's how stoul()
|
||||||
|
@ -120,14 +125,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 1;
|
return EXIT_USAGE;
|
||||||
}
|
}
|
||||||
port = (uint16_t) argumentNumber;
|
port = (uint16_t) argumentNumber;
|
||||||
}
|
}
|
||||||
break; // last argument anyway
|
break; // last argument anyway
|
||||||
}
|
}
|
||||||
usage;
|
usage;
|
||||||
return 1;
|
return EXIT_USAGE;
|
||||||
}
|
}
|
||||||
// Argument parsing end ############################################
|
// Argument parsing end ############################################
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue