Skip to content

Commit 8402207

Browse files
committed
add --limit as an alternative way for controlling the length of outputs
1 parent 21acf67 commit 8402207

File tree

8 files changed

+166
-0
lines changed

8 files changed

+166
-0
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
of limiting the maximum number of shown entries in queries
88
- Summary image output would segmentation fault when database existed but
99
didn't yet contain any daily or monthly data for the selected interface
10+
- New
11+
- Add --limit as an alternative way for controlling the length of outputs
1012

1113

1214
2.5 / 14-Jan-2020

man/vnstat.1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ vnstat \- a console-based network traffic monitor
3333
.RB [ \-\-json
3434
.RI [ mode ]
3535
.RI [ limit ]]
36+
.RB [ \-\-limit
37+
.IR limit ]
3638
.RB [ \-\-live
3739
.RI [ mode ]]
3840
.RB [ \-\-locale
@@ -229,6 +231,24 @@ names or structures of previously existing content gets changed. In comparison,
229231
.B "vnstatversion"
230232
field exists only as extra information.
231233

234+
.TP
235+
.BI "--limit " limit
236+
Set the maximum number of entries in list outputs to
237+
.IR limit .
238+
Usage of
239+
.B "--limit"
240+
overrides the default list entry limit values and the optional
241+
.I limit
242+
parameter given directly for a list query. All entries stored in the database will be shown if
243+
.I limit
244+
is set to 0.
245+
.B "--limit"
246+
can also be used to control the length of
247+
.B "--json"
248+
and
249+
.B "--xml"
250+
outputs.
251+
232252
.TP
233253
.BI "-l, --live " [mode]
234254
Display current transfer rate for the selected interface in real time

man/vnstati.1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ vnstati \- png image output support for vnStat
3535
.IR interface ]
3636
.RB [ \-\-iface
3737
.IR interface ]
38+
.RB [ \-\-limit
39+
.IR limit ]
3840
.RB [ \-\-locale
3941
.IR locale ]
4042
.RB [ \-\-months
@@ -191,6 +193,18 @@ information of two or more interfaces using the
191193
syntax. All provided interfaces must be unique and must exist in the database
192194
when the merge syntax is used.
193195

196+
.TP
197+
.BI "--limit " limit
198+
Set the maximum number of entries in list outputs to
199+
.IR limit .
200+
Usage of
201+
.B "--limit"
202+
overrides the default list entry limit values and the optional
203+
.I limit
204+
parameter given directly for a list query. All entries stored in the database will be shown if
205+
.I limit
206+
is set to 0.
207+
194208
.TP
195209
.BI "--locale " locale
196210
Use

src/vnstat_func.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void initparams(PARAMS *p)
2929
p->removeiface = 0;
3030
p->renameiface = 0;
3131
p->livemode = 0;
32+
p->limit = -1;
3233
p->ifacelist = NULL;
3334
p->interface[0] = '\0';
3435
p->alias[0] = '\0';
@@ -113,6 +114,7 @@ void showlonghelp(PARAMS *p)
113114
printf(" -tr, --traffic [time] calculate traffic\n");
114115
printf(" -l, --live [mode] show transfer rate in real time\n");
115116
printf(" -ru, --rateunit [mode] swap configured rate unit\n");
117+
printf(" --limit <limit> set output entry limit\n");
116118
printf(" --style <mode> select output style (0-4)\n");
117119
printf(" --iflist show list of available interfaces\n");
118120
printf(" --dbdir <directory> select database directory\n");
@@ -454,6 +456,18 @@ void parseargs(PARAMS *p, int argc, char **argv)
454456
}
455457
free(p->ifacelist);
456458
exit(EXIT_SUCCESS);
459+
} else if (strcmp(argv[currentarg], "--limit") == 0) {
460+
if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) {
461+
p->limit = atoi(argv[currentarg + 1]);
462+
if (p->limit < 0) {
463+
printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg + 1], argv[currentarg]);
464+
exit(EXIT_FAILURE);
465+
}
466+
currentarg++;
467+
} else {
468+
printf("Error: Invalid or missing parameter for %s.\n", argv[currentarg]);
469+
exit(EXIT_FAILURE);
470+
}
457471
} else if ((strcmp(argv[currentarg], "-v") == 0) || (strcmp(argv[currentarg], "--version") == 0)) {
458472
printf("vnStat %s by Teemu Toivola <tst at iki dot fi>\n", getversion());
459473
exit(EXIT_SUCCESS);
@@ -462,6 +476,10 @@ void parseargs(PARAMS *p, int argc, char **argv)
462476
exit(EXIT_FAILURE);
463477
}
464478
}
479+
480+
if (p->limit != -1) {
481+
cfg.listfivemins = cfg.listhours = cfg.listdays = cfg.listmonths = cfg.listyears = cfg.listtop = cfg.listjsonxml = p->limit;
482+
}
465483
}
466484

467485
void handleremoveinterface(PARAMS *p)

src/vnstat_func.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ typedef struct {
55
int query, setalias;
66
int addiface, force, traffic;
77
int livetraffic, defaultiface, removeiface, renameiface, livemode;
8+
int32_t limit;
89
uint64_t dbifcount;
910
char interface[32], alias[32], newifname[32], filename[512];
1011
char definterface[32], cfgfile[512], *ifacelist, jsonmode, xmlmode;

src/vnstati.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void initiparams(IPARAMS *p)
9393
p->cfgfile[0] = '\0';
9494
p->cache = 0;
9595
p->help = 0;
96+
p->limit = -1;
9697
}
9798

9899
void showihelp(IPARAMS *p)
@@ -125,6 +126,7 @@ void showihelp(IPARAMS *p)
125126
printf(" -?, --help this help\n");
126127
printf(" -D, --debug show some additional debug information\n");
127128
printf(" -v, --version show version\n");
129+
printf(" --limit <limit> set list entry limit\n");
128130
printf(" --dbdir <directory> select database directory\n");
129131
printf(" --style <mode> select output style (0-3)\n");
130132
printf(" --locale <locale> set locale\n");
@@ -362,6 +364,18 @@ void parseargs(IPARAMS *p, IMAGECONTENT *ic, int argc, char **argv)
362364
printf("Error: Date of format YYYY-MM-DD HH:MM or YYYY-MM-DD for %s missing.\n", argv[currentarg]);
363365
exit(EXIT_FAILURE);
364366
}
367+
} else if (strcmp(argv[currentarg], "--limit") == 0) {
368+
if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) {
369+
p->limit = atoi(argv[currentarg + 1]);
370+
if (p->limit < 0) {
371+
printf("Error: Invalid limit parameter \"%s\" for %s. Only a zero and positive numbers are allowed.\n", argv[currentarg + 1], argv[currentarg]);
372+
exit(EXIT_FAILURE);
373+
}
374+
currentarg++;
375+
} else {
376+
printf("Error: Invalid or missing parameter for %s.\n", argv[currentarg]);
377+
exit(EXIT_FAILURE);
378+
}
365379
} else if ((strcmp(argv[currentarg], "-v") == 0) || (strcmp(argv[currentarg], "--version")) == 0) {
366380
printf("vnStat image output %s by Teemu Toivola <tst at iki dot fi>\n", getversion());
367381
exit(EXIT_SUCCESS);
@@ -375,6 +389,10 @@ void parseargs(IPARAMS *p, IMAGECONTENT *ic, int argc, char **argv)
375389
showihelp(p);
376390
exit(EXIT_SUCCESS);
377391
}
392+
393+
if (p->limit != -1) {
394+
cfg.listfivemins = cfg.listhours = cfg.listdays = cfg.listmonths = cfg.listyears = cfg.listtop = cfg.listjsonxml = p->limit;
395+
}
378396
}
379397

380398
void validateinput(IPARAMS *p)

src/vnstati.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
typedef struct {
55
int cache, help;
6+
int32_t limit;
67
char interface[32], filename[512], cfgfile[512];
78
FILE *pngout;
89
} IPARAMS;

tests/parseargs_tests.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,93 @@ START_TEST(vnstat_parseargs_rename_requires_parameter)
481481
}
482482
END_TEST
483483

484+
START_TEST(vnstat_parseargs_limit_requires_parameter)
485+
{
486+
PARAMS p;
487+
char *argv[] = {"vnstat", "--limit", NULL};
488+
int argc = sizeof(argv) / sizeof(char *) - 1;
489+
490+
initparams(&p);
491+
suppress_output();
492+
parseargs(&p, argc, argv);
493+
}
494+
END_TEST
495+
496+
START_TEST(vnstat_parseargs_limit_cannot_be_negative)
497+
{
498+
PARAMS p;
499+
char *argv[] = {"vnstat", "--limit", "-1", NULL};
500+
int argc = sizeof(argv) / sizeof(char *) - 1;
501+
502+
initparams(&p);
503+
suppress_output();
504+
parseargs(&p, argc, argv);
505+
}
506+
END_TEST
507+
508+
START_TEST(vnstat_parseargs_limit_changes_defaults)
509+
{
510+
PARAMS p;
511+
char *argv[] = {"vnstat", "--limit", "1234", NULL};
512+
int argc = sizeof(argv) / sizeof(char *) - 1;
513+
514+
defaultcfg();
515+
initparams(&p);
516+
suppress_output();
517+
parseargs(&p, argc, argv);
518+
519+
ck_assert_int_eq(cfg.listfivemins, 1234);
520+
ck_assert_int_eq(cfg.listhours, 1234);
521+
ck_assert_int_eq(cfg.listdays, 1234);
522+
ck_assert_int_eq(cfg.listmonths, 1234);
523+
ck_assert_int_eq(cfg.listyears, 1234);
524+
ck_assert_int_eq(cfg.listtop, 1234);
525+
ck_assert_int_eq(cfg.listjsonxml, 1234);
526+
}
527+
END_TEST
528+
529+
START_TEST(vnstat_parseargs_limit_overrides)
530+
{
531+
PARAMS p;
532+
char *argv[] = {"vnstat", "-d", "5", "--limit", "234", NULL};
533+
int argc = sizeof(argv) / sizeof(char *) - 1;
534+
535+
defaultcfg();
536+
initparams(&p);
537+
suppress_output();
538+
parseargs(&p, argc, argv);
539+
540+
ck_assert_int_eq(cfg.listfivemins, 234);
541+
ck_assert_int_eq(cfg.listhours, 234);
542+
ck_assert_int_eq(cfg.listdays, 234);
543+
ck_assert_int_eq(cfg.listmonths, 234);
544+
ck_assert_int_eq(cfg.listyears, 234);
545+
ck_assert_int_eq(cfg.listtop, 234);
546+
ck_assert_int_eq(cfg.listjsonxml, 234);
547+
}
548+
END_TEST
549+
550+
START_TEST(vnstat_parseargs_limit_overrides_regardless_of_position)
551+
{
552+
PARAMS p;
553+
char *argv[] = {"vnstat", "--limit", "345", "-d", "5", NULL};
554+
int argc = sizeof(argv) / sizeof(char *) - 1;
555+
556+
defaultcfg();
557+
initparams(&p);
558+
suppress_output();
559+
parseargs(&p, argc, argv);
560+
561+
ck_assert_int_eq(cfg.listfivemins, 345);
562+
ck_assert_int_eq(cfg.listhours, 345);
563+
ck_assert_int_eq(cfg.listdays, 345);
564+
ck_assert_int_eq(cfg.listmonths, 345);
565+
ck_assert_int_eq(cfg.listyears, 345);
566+
ck_assert_int_eq(cfg.listtop, 345);
567+
ck_assert_int_eq(cfg.listjsonxml, 345);
568+
}
569+
END_TEST
570+
484571
void add_parseargs_tests(Suite *s)
485572
{
486573
TCase *tc_pa = tcase_create("ParseArgs");
@@ -520,5 +607,10 @@ void add_parseargs_tests(Suite *s)
520607
tcase_add_exit_test(tc_pa, vnstat_parseargs_setalias_requires_parameter, 1);
521608
tcase_add_test(tc_pa, vnstat_parseargs_setalias_still_supports_nick);
522609
tcase_add_exit_test(tc_pa, vnstat_parseargs_rename_requires_parameter, 1);
610+
tcase_add_exit_test(tc_pa, vnstat_parseargs_limit_requires_parameter, 1);
611+
tcase_add_exit_test(tc_pa, vnstat_parseargs_limit_cannot_be_negative, 1);
612+
tcase_add_test(tc_pa, vnstat_parseargs_limit_changes_defaults);
613+
tcase_add_test(tc_pa, vnstat_parseargs_limit_overrides);
614+
tcase_add_test(tc_pa, vnstat_parseargs_limit_overrides_regardless_of_position);
523615
suite_add_tcase(s, tc_pa);
524616
}

0 commit comments

Comments
 (0)