From: Teemu Toivola Date: Mon, 19 Aug 2019 17:40:06 +0000 (+0300) Subject: refactor vnstati.c X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=288df13d991ad1b9adf8b1f8052f63228dc5ed43;p=vnstat refactor vnstati.c --- diff --git a/src/vnstati.c b/src/vnstati.c index c3458fa..24798b7 100644 --- a/src/vnstati.c +++ b/src/vnstati.c @@ -63,55 +63,133 @@ int main(int argc, char *argv[]) strncpy_nt(p.interface, cfg.iface, 32); ic.current = time(NULL); + parseargs(&p, &ic, argc, argv); + validateinput(&p); + handlecaching(&p, &ic); + handledatabase(&p, &ic); + openoutput(&p); + + if (debug) + printf("Qmode: %d\n", cfg.qmode); + + drawimage(&ic); + db_close(); + writeoutput(&p, &ic); + + if (debug) + printf("all done\n"); + + return 0; +} + +void initiparams(IPARAMS *p) +{ + db = NULL; + noexit = 0; /* allow functions to exit in case of error */ + debug = 0; /* debug disabled by default */ + disableprints = 0; /* let prints be visible */ + + p->interface[0] = '\0'; + p->filename[0] = '\0'; + p->cfgfile[0] = '\0'; + p->cache = 0; + p->help = 0; +} + +void showihelp(IPARAMS *p) +{ + printf("vnStat image output %s by Teemu Toivola \n\n", getversion()); + + printf(" -5, --fiveminutes [count] output 5 minutes\n"); + printf(" -h, --hours [count] output hours\n"); + printf(" -hg, --hoursgraph output hours graph\n"); + printf(" -d, --days [count] output days\n"); + printf(" -m, --months [count] output months\n"); + printf(" -y, --years [count] output years\n"); + printf(" -t, --top [count] output top days\n"); + printf(" -s, --summary output summary\n"); + printf(" -hs, --hsummary output horizontal summary with hours\n"); + printf(" -vs, --vsummary output vertical summary with hours\n"); + printf(" -nh, --noheader remove header from output\n"); + printf(" -ne, --noedge remove edge from output\n"); + printf(" -nl, --nolegend remove legend from output\n"); + printf(" -ru, --rateunit [mode] swap configured rate unit\n"); + printf(" -o, --output select output filename\n"); + printf(" -c, --cache update output only when too old\n"); + printf(" -i, --iface select interface"); + if (strlen(p->interface)) { + printf(" (default: %s)", p->interface); + } + printf("\n"); + printf(" -b, --begin set list begin date\n"); + printf(" -e, --end set list end date\n"); + printf(" -?, --help this help\n"); + printf(" -D, --debug show some additional debug information\n"); + printf(" -v, --version show version\n"); + printf(" --dbdir select database directory\n"); + printf(" --style select output style (0-3)\n"); + printf(" --locale set locale\n"); + printf(" --config select config file\n"); + printf(" --altdate use alternative date location\n"); + printf(" --headertext specify header text string\n"); + printf(" --transparent [enabled] toggle background transparency\n\n"); + + printf("See also \"man vnstati\".\n"); +} + +void parseargs(IPARAMS *p, IMAGECONTENT *ic, int argc, char **argv) +{ + int currentarg; + /* parse parameters */ for (currentarg = 1; currentarg < argc; currentarg++) { if (debug) printf("arg %d: \"%s\"\n", currentarg, argv[currentarg]); if ((strcmp(argv[currentarg], "-?") == 0) || (strcmp(argv[currentarg], "--help")) == 0) { - p.help = 1; + p->help = 1; } else if ((strcmp(argv[currentarg], "-i") == 0) || (strcmp(argv[currentarg], "--iface")) == 0) { if (currentarg + 1 < argc) { if (strlen(argv[currentarg + 1]) > 31) { printf("Error: Interface name is limited to 31 characters.\n"); - return 1; + exit(EXIT_FAILURE); } - strncpy_nt(p.interface, argv[currentarg + 1], 32); + strncpy_nt(p->interface, argv[currentarg + 1], 32); if (debug) - printf("Used interface: \"%s\"\n", p.interface); + printf("Used interface: \"%s\"\n", p->interface); currentarg++; continue; } else { printf("Error: Interface for -i missing.\n"); - return 1; + exit(EXIT_FAILURE); } } else if ((strcmp(argv[currentarg], "-o") == 0) || (strcmp(argv[currentarg], "--output")) == 0) { if (currentarg + 1 < argc) { - strncpy_nt(p.filename, argv[currentarg + 1], 512); + strncpy_nt(p->filename, argv[currentarg + 1], 512); if (debug) - printf("Output file: \"%s\"\n", p.filename); + printf("Output file: \"%s\"\n", p->filename); currentarg++; continue; } else { printf("Error: Filename for -o missing.\n"); - return 1; + exit(EXIT_FAILURE); } } else if ((strcmp(argv[currentarg], "-c") == 0) || (strcmp(argv[currentarg], "--cache")) == 0) { if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) { - p.cache = atoi(argv[currentarg + 1]); + p->cache = atoi(argv[currentarg + 1]); if (debug) - printf("Cache time: %d minutes\n", p.cache); + printf("Cache time: %d minutes\n", p->cache); currentarg++; continue; } else { printf("Error: Parameter for -c missing or invalid.\n"); - return 1; + exit(EXIT_FAILURE); } } else if ((strcmp(argv[currentarg], "--style")) == 0) { if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) { cfg.ostyle = atoi(argv[currentarg + 1]); if (cfg.ostyle > 3 || cfg.ostyle < 0) { printf("Error: Invalid style parameter \"%d\" for --style.\n", cfg.ostyle); - return 1; + exit(EXIT_FAILURE); } if (debug) printf("Style changed: %d\n", cfg.ostyle); @@ -119,14 +197,14 @@ int main(int argc, char *argv[]) continue; } else { printf("Error: Style parameter for --style missing.\n"); - return 1; + exit(EXIT_FAILURE); } } else if ((strcmp(argv[currentarg], "--transparent")) == 0) { if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) { cfg.transbg = atoi(argv[currentarg + 1]); if (cfg.transbg > 1 || cfg.transbg < 0) { printf("Error: Invalid parameter \"%d\" for --transparent.\n", cfg.transbg); - return 1; + exit(EXIT_FAILURE); } if (debug) printf("Transparency changed: %d\n", cfg.transbg); @@ -146,7 +224,7 @@ int main(int argc, char *argv[]) continue; } else { printf("Error: Directory for --dbdir missing.\n"); - return 1; + exit(EXIT_FAILURE); } } else if ((strcmp(argv[currentarg], "--locale")) == 0) { if (currentarg + 1 < argc) { @@ -157,7 +235,7 @@ int main(int argc, char *argv[]) continue; } else { printf("Error: Locale for --locale missing.\n"); - return 1; + exit(EXIT_FAILURE); } } else if (strcmp(argv[currentarg], "--config") == 0) { /* config has already been parsed earlier so nothing to do here */ @@ -165,17 +243,17 @@ int main(int argc, char *argv[]) continue; } else if ((strcmp(argv[currentarg], "--headertext")) == 0) { if (currentarg + 1 < argc) { - strncpy_nt(ic.headertext, argv[currentarg + 1], 65); + strncpy_nt(ic->headertext, argv[currentarg + 1], 65); if (debug) - printf("Header text: \"%s\"\n", ic.headertext); + printf("Header text: \"%s\"\n", ic->headertext); currentarg++; continue; } else { printf("Error: Text string parameter for --headertext missing.\n"); - return 1; + exit(EXIT_FAILURE); } } else if (strcmp(argv[currentarg], "--altdate") == 0) { - ic.altdate = 1; + ic->altdate = 1; } else if ((strcmp(argv[currentarg], "-D") == 0) || (strcmp(argv[currentarg], "--debug")) == 0) { debug = 1; } else if ((strcmp(argv[currentarg], "-d") == 0) || (strcmp(argv[currentarg], "--days")) == 0) { @@ -184,7 +262,7 @@ int main(int argc, char *argv[]) cfg.listdays = atoi(argv[currentarg + 1]); if (cfg.listdays < 0) { printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg + 1], argv[currentarg]); - return 1; + exit(EXIT_FAILURE); } currentarg++; } @@ -194,7 +272,7 @@ int main(int argc, char *argv[]) cfg.listmonths = atoi(argv[currentarg + 1]); if (cfg.listmonths < 0) { printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg + 1], argv[currentarg]); - return 1; + exit(EXIT_FAILURE); } currentarg++; } @@ -204,7 +282,7 @@ int main(int argc, char *argv[]) cfg.listtop = atoi(argv[currentarg + 1]); if (cfg.listtop < 0) { printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg + 1], argv[currentarg]); - return 1; + exit(EXIT_FAILURE); } currentarg++; } @@ -214,7 +292,7 @@ int main(int argc, char *argv[]) cfg.listyears = atoi(argv[currentarg + 1]); if (cfg.listyears < 0) { printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg + 1], argv[currentarg]); - return 1; + exit(EXIT_FAILURE); } currentarg++; } @@ -224,7 +302,7 @@ int main(int argc, char *argv[]) cfg.listhours = atoi(argv[currentarg + 1]); if (cfg.listhours < 0) { printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg + 1], argv[currentarg]); - return 1; + exit(EXIT_FAILURE); } currentarg++; } @@ -234,7 +312,7 @@ int main(int argc, char *argv[]) cfg.listfivemins = atoi(argv[currentarg + 1]); if (cfg.listfivemins < 0) { printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg + 1], argv[currentarg]); - return 1; + exit(EXIT_FAILURE); } currentarg++; } @@ -247,11 +325,11 @@ int main(int argc, char *argv[]) } else if ((strcmp(argv[currentarg], "-vs") == 0) || (strcmp(argv[currentarg], "--vsummary")) == 0) { cfg.qmode = 52; } else if ((strcmp(argv[currentarg], "-nh") == 0) || (strcmp(argv[currentarg], "--noheader")) == 0) { - ic.showheader = 0; + ic->showheader = 0; } else if ((strcmp(argv[currentarg], "-ne") == 0) || (strcmp(argv[currentarg], "--noedge")) == 0) { - ic.showedge = 0; + ic->showedge = 0; } else if ((strcmp(argv[currentarg], "-nl") == 0) || (strcmp(argv[currentarg], "--nolegend")) == 0) { - ic.showlegend = 0; + ic->showlegend = 0; } else if ((strcmp(argv[currentarg], "-ru") == 0) || (strcmp(argv[currentarg], "--rateunit")) == 0) { if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) { cfg.rateunit = atoi(argv[currentarg + 1]); @@ -260,7 +338,7 @@ int main(int argc, char *argv[]) printf(" Valid parameters:\n"); printf(" 0 - bytes\n"); printf(" 1 - bits\n"); - return 1; + exit(EXIT_FAILURE); } if (debug) printf("Rateunit changed: %d\n", cfg.rateunit); @@ -275,111 +353,39 @@ int main(int argc, char *argv[]) if (currentarg + 1 < argc) { if (!validatedatetime(argv[currentarg + 1])) { printf("Error: Invalid date format, expected YYYY-MM-DD HH:MM or YYYY-MM-DD.\n"); - return 1; + exit(EXIT_FAILURE); } - strncpy_nt(ic.databegin, argv[currentarg + 1], 18); + strncpy_nt(ic->databegin, argv[currentarg + 1], 18); currentarg++; } else { printf("Error: Date of format YYYY-MM-DD HH:MM or YYYY-MM-DD for %s missing.\n", argv[currentarg]); - return 1; + exit(EXIT_FAILURE); } } else if ((strcmp(argv[currentarg], "-e") == 0) || (strcmp(argv[currentarg], "--end") == 0)) { if (currentarg + 1 < argc) { if (!validatedatetime(argv[currentarg + 1])) { printf("Error: Invalid date format, expected YYYY-MM-DD HH:MM or YYYY-MM-DD.\n"); - return 1; + exit(EXIT_FAILURE); } - strncpy_nt(ic.dataend, argv[currentarg + 1], 18); + strncpy_nt(ic->dataend, argv[currentarg + 1], 18); currentarg++; } else { printf("Error: Date of format YYYY-MM-DD HH:MM or YYYY-MM-DD for %s missing.\n", argv[currentarg]); - return 1; + exit(EXIT_FAILURE); } } else if ((strcmp(argv[currentarg], "-v") == 0) || (strcmp(argv[currentarg], "--version")) == 0) { printf("vnStat image output %s by Teemu Toivola \n", getversion()); - return 0; + exit(EXIT_SUCCESS); } else { printf("Unknown arg \"%s\". Use --help for help.\n", argv[currentarg]); - return 1; + exit(EXIT_FAILURE); } } - if (p.help || argc == 1) { - showihelp(&p); - return 0; + if (p->help || argc == 1) { + showihelp(p); + exit(EXIT_SUCCESS); } - - validateinput(&p); - handlecaching(&p, &ic); - handledatabase(&p, &ic); - openoutput(&p); - - if (debug) - printf("Qmode: %d\n", cfg.qmode); - - drawimage(&ic); - db_close(); - writeoutput(&p, &ic); - - if (debug) - printf("all done\n"); - - return 0; -} - -void initiparams(IPARAMS *p) -{ - db = NULL; - noexit = 0; /* allow functions to exit in case of error */ - debug = 0; /* debug disabled by default */ - disableprints = 0; /* let prints be visible */ - - p->interface[0] = '\0'; - p->filename[0] = '\0'; - p->cfgfile[0] = '\0'; - p->cache = 0; - p->help = 0; -} - -void showihelp(IPARAMS *p) -{ - printf("vnStat image output %s by Teemu Toivola \n\n", getversion()); - - printf(" -5, --fiveminutes [count] output 5 minutes\n"); - printf(" -h, --hours [count] output hours\n"); - printf(" -hg, --hoursgraph output hours graph\n"); - printf(" -d, --days [count] output days\n"); - printf(" -m, --months [count] output months\n"); - printf(" -y, --years [count] output years\n"); - printf(" -t, --top [count] output top days\n"); - printf(" -s, --summary output summary\n"); - printf(" -hs, --hsummary output horizontal summary with hours\n"); - printf(" -vs, --vsummary output vertical summary with hours\n"); - printf(" -nh, --noheader remove header from output\n"); - printf(" -ne, --noedge remove edge from output\n"); - printf(" -nl, --nolegend remove legend from output\n"); - printf(" -ru, --rateunit [mode] swap configured rate unit\n"); - printf(" -o, --output select output filename\n"); - printf(" -c, --cache update output only when too old\n"); - printf(" -i, --iface select interface"); - if (strlen(p->interface)) { - printf(" (default: %s)", p->interface); - } - printf("\n"); - printf(" -b, --begin set list begin date\n"); - printf(" -e, --end set list end date\n"); - printf(" -?, --help this help\n"); - printf(" -D, --debug show some additional debug information\n"); - printf(" -v, --version show version\n"); - printf(" --dbdir select database directory\n"); - printf(" --style select output style (0-3)\n"); - printf(" --locale set locale\n"); - printf(" --config select config file\n"); - printf(" --altdate use alternative date location\n"); - printf(" --headertext specify header text string\n"); - printf(" --transparent [enabled] toggle background transparency\n\n"); - - printf("See also \"man vnstati\".\n"); } void validateinput(IPARAMS *p) diff --git a/src/vnstati.h b/src/vnstati.h index 96be4e3..05b80b7 100644 --- a/src/vnstati.h +++ b/src/vnstati.h @@ -9,6 +9,7 @@ typedef struct { void initiparams(IPARAMS *p); void showihelp(IPARAMS *p); +void parseargs(IPARAMS *p, IMAGECONTENT *ic, int argc, char **argv); void validateinput(IPARAMS *p); void handlecaching(IPARAMS *p, IMAGECONTENT *ic); void handledatabase(IPARAMS *p, IMAGECONTENT *ic);