From: Tobias Oetiker Date: Tue, 28 Jul 2015 14:01:49 +0000 (+0200) Subject: make optparse consider argc X-Git-Tag: v1.5.4~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=86240c5085169a910df70256cc2eee686d77513d;p=thirdparty%2Frrdtool-1.x.git make optparse consider argc --- diff --git a/bindings/perl-shared/t/base.t b/bindings/perl-shared/t/base.t index c30417b8..b9bb5dc3 100755 --- a/bindings/perl-shared/t/base.t +++ b/bindings/perl-shared/t/base.t @@ -1,6 +1,6 @@ #! /usr/bin/perl -BEGIN { $| = 1; print "1..7\n"; } +BEGIN { $| = 1; print "1..8\n"; } END { print "not ok 1\n" unless $loaded; unlink "demo.rrd"; @@ -150,6 +150,9 @@ for (my $i=0;$i<$GRUNS;$i++) { } +ok("graph",!$ERROR); # 3 + + my ($start,$step,$names,$array) = RRDs::fetch $RRD1, "AVERAGE"; $ERROR = RRDs::error; @@ -169,3 +172,48 @@ foreach my $line (@$array){ } print "\n"; } + +my ($start,$end,$step,$col_cnt,$legend,$data) = + RRDs::xport ("-m", 400, + "--start", "now-1day", + "--end", "now", + "DEF:alpha=$RRD1:a:AVERAGE", + "DEF:beta=$RRD1:d:AVERAGE", + "CDEF:calc=alpha,beta,+,2,/,100,*,102,/", + "XPORT:alpha:original ds", + "XPORT:calc:calculated values", + ); + +my $ERROR = RRDs::error; + +ok("xport",!$ERROR); # 3 + +print "\nrrdxport test:\n\n"; +print "\n\n"; +print "\n"; +print " \n"; +print " $start\n"; +print " $step\n"; +print " $end\n"; +print " ", $#$data + 1, "\n"; +print " $col_cnt\n"; +print " \n"; +foreach my $entry (@$legend) { + print " $entry\n"; +} +print " \n"; +print " \n"; +print " \n"; +my $row_counter = 0; +foreach my $row (@$data) { + $row_counter++; + print " $start"; + $start += $step; + foreach my $val (@$row) { + printf ("%1.10e",$val) if $val ne ''; + print "NaN" if $val eq ''; + } + print "\n"; +} +print " \n"; +print "\n"; diff --git a/src/optparse.c b/src/optparse.c index 8889b947..911a56be 100644 --- a/src/optparse.c +++ b/src/optparse.c @@ -4,9 +4,13 @@ #define opterror(options, format, args...) \ snprintf(options->errmsg, sizeof(options->errmsg), format, args); -void optparse_init(struct optparse *options, char **argv) +#define options_argv(i) \ + (i) < options->argc ? options->argv[i] : 0; + +void optparse_init(struct optparse *options, int argc, char **argv) { options->argv = argv; + options->argc = argc; options->permute = 1; options->optind = 1; options->subopt = 0; @@ -60,7 +64,7 @@ int optparse(struct optparse *options, const char *optstring) options->errmsg[0] = '\0'; options->optopt = 0; options->optarg = NULL; - char *option = options->argv[options->optind]; + char *option = options_argv(options->optind); if (option == NULL) { return -1; } else if (is_dashdash(option)) { @@ -81,7 +85,7 @@ int optparse(struct optparse *options, const char *optstring) option += options->subopt + 1; options->optopt = option[0]; int type = argtype(optstring, option[0]); - char *next = options->argv[options->optind + 1]; + char *next = options_argv(options->optind + 1); switch (type) { case -1: opterror(options, "invalid option -- '%c'", option[0]); @@ -206,7 +210,7 @@ optparse_long(struct optparse *options, const struct optparse_long *longopts, int *longindex) { - char *option = options->argv[options->optind]; + char *option = options_argv(options->optind); if (option == NULL) { return -1; } else if (is_shortopt(option)) { @@ -243,7 +247,8 @@ optparse_long(struct optparse *options, } if (arg != NULL) { options->optarg = arg; } else if (longopts[i].argtype == OPTPARSE_REQUIRED) { - options->optarg = options->argv[options->optind++]; + options->optarg = options_argv(options->optind); + options->optind++; if (options->optarg == NULL) { opterror(options, "option requires argument -- '%s'", name); return '?'; diff --git a/src/optparse.h b/src/optparse.h index 3e70dc04..c4b0ec19 100644 --- a/src/optparse.h +++ b/src/optparse.h @@ -45,6 +45,7 @@ struct optparse { char **argv; + int argc; int permute; int optind; int optopt; @@ -64,7 +65,7 @@ struct optparse_long { /** * Initializes the parser state. */ -void optparse_init(struct optparse *options, char **argv); +void optparse_init(struct optparse *options, int argc, char **argv); /** * Read the next option in the argv array. diff --git a/src/rrd_xport.c b/src/rrd_xport.c index f714e8a7..76f94b6b 100644 --- a/src/rrd_xport.c +++ b/src/rrd_xport.c @@ -75,7 +75,7 @@ int rrd_xport( rrd_time_value_t start_tv, end_tv; char *parsetime_error = NULL; struct optparse options; - optparse_init(&options, argv); + optparse_init(&options, argc, argv); struct optparse_long longopts[] = { {"start", 's', OPTPARSE_REQUIRED},