cmdline parsing of --ipv4/6 plucked into util

This commit is contained in:
Coren[m] 2013-09-11 00:14:20 +02:00
parent 8ba6a5ff5b
commit 4cf0d857bc
8 changed files with 52 additions and 162 deletions

View File

@ -89,33 +89,9 @@ int main(int argc, char *argv[])
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
if (argvoffset < 0)
exit(1);
/* Initialize networking -
Bind to ip 0.0.0.0 / [::] : PORT */

View File

@ -140,33 +140,9 @@ int main(int argc, char *argv[])
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
if (argvoffset < 0)
exit(1);
//memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32);
/* initialize networking */

View File

@ -153,33 +153,9 @@ int main(int argc, char *argv[])
{
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
if (argvoffset < 0)
exit(1);
if (argc < argvoffset + 4) {
printf("Usage: %s [--ipv4|--ipv6] ip port filename\n", argv[0]);

View File

@ -149,33 +149,9 @@ int main(int argc, char *argv[])
{
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
if (argvoffset < 0)
exit(1);
if (argc < argvoffset + 2) {
printf("Usage: %s [--ipv4|--ipv6] filename\n", argv[0]);

View File

@ -97,33 +97,9 @@ int main(int argc, char *argv[])
{
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
if (argvoffset < 0)
exit(1);
/* with optional --ipvx, now it can be 1-4 arguments... */
if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) {

View File

@ -544,33 +544,9 @@ int main(int argc, char *argv[])
/* let user override default by cmdline */
uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
ipv6enabled = 0;
else if (c == '6')
ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
exit(1);
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
exit(1);
}
argvoffset++;
}
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
if (argvoffset < 0)
exit(1);
int on = 0;
int c = 0;

View File

@ -43,6 +43,38 @@ void id_cpy(uint8_t *dest, uint8_t *src)
memcpy(dest, src, CLIENT_ID_SIZE);
}
int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled)
{
int argvoffset = 0, argi;
for(argi = 1; argi < argc; argi++)
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];
if (c == '4')
*ipv6enabled = 0;
else if (c == '6')
*ipv6enabled = 1;
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
return -1;
}
}
else {
printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]);
return -1;
}
if (argvoffset != argi - 1) {
printf("Argument must come first: %s.\n", argv[argi]);
return -1;
}
argvoffset++;
}
return argvoffset;
};
#ifdef LOGGING
char logbuffer[512];
static FILE *logfile = NULL;

View File

@ -16,6 +16,8 @@ uint64_t random_64b();
bool id_eq(uint8_t *dest, uint8_t *src);
void id_cpy(uint8_t *dest, uint8_t *src);
int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled);
#undef LOGGING
/* #define LOGGING */
#ifdef LOGGING