]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
make optparse consider argc
authorTobias Oetiker <tobi@oetiker.ch>
Tue, 28 Jul 2015 14:01:49 +0000 (16:01 +0200)
committerTobias Oetiker <tobi@oetiker.ch>
Tue, 28 Jul 2015 14:01:49 +0000 (16:01 +0200)
bindings/perl-shared/t/base.t
src/optparse.c
src/optparse.h
src/rrd_xport.c

index c30417b8ced07845751fbe579159dfe647e40b74..b9bb5dc35522d2b9d6d448387a143009c27f9aca 100755 (executable)
@@ -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 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n\n";
+print "<xport>\n";
+print "  <meta>\n";
+print "    <start>$start</start>\n";
+print "    <step>$step</step>\n";
+print "    <end>$end</end>\n";
+print "    <rows>", $#$data + 1, "</rows>\n";
+print "    <columns>$col_cnt</columns>\n";
+print "    <legend>\n";
+foreach my $entry (@$legend) {
+    print "      <entry>$entry</entry>\n";
+}
+print "    </legend>\n";
+print "  </meta>\n";
+print "  <data>\n";
+my $row_counter = 0;
+foreach my $row (@$data) {
+    $row_counter++;
+    print "    <row id=\"$row_counter\"><t is=\"", scalar localtime($start), "\">$start</t>";
+    $start += $step;
+    foreach my $val (@$row) {
+        printf ("<v>%1.10e</v>",$val) if $val ne '';
+        print "<v>NaN</v>" if  $val eq '';
+    }
+    print "</row>\n";
+}
+print "  </data>\n";
+print "</xport>\n";
index 8889b9470bc68d77e5f8ef9143a544f30fd57ef5..911a56bef5610e08d385853855a25a29ca70bab8 100644 (file)
@@ -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 '?';
index 3e70dc047d275aa4be39adec7e2a0518dcbc8314..c4b0ec19cc3adcfefa1b475eecbcc8648915c535 100644 (file)
@@ -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.
index f714e8a7f40f336db9020740cbabb12a9a32fb10..76f94b6b9bd7992a22eb907ce80fb3c7834ce192 100644 (file)
@@ -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},