From 83530d3e43cebc32da157733d35c60bf4bb098da Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Michel=20Vourg=C3=A8re?= Date: Sun, 1 Nov 2015 16:40:45 +0100 Subject: [PATCH] Thread safety: Replace getopt by optparse * Added optparse.h to src/rrd_tool.h. * rrd_graph.[ch]: The 3rd char* parameter of the "*_conv" functions was added a const modifier. * rrd_tune.c: The 3rd char* parameter of the "set_*" functions was added a const modifier. * rrd_graph_options now takes an extra "options*" parameter, so that caller can use options.argc and options.argv * Refresh WIN32 build tips --- WIN32-BUILD-TIPS.txt | 4 +- bindings/python/rrdtoolmodule.c | 5 - src/Makefile.am | 10 +- src/rrd_cgi.c | 31 ++-- src/rrd_create.c | 72 ++++---- src/rrd_daemon.c | 64 +++---- src/rrd_dump.c | 52 +++--- src/rrd_fetch.c | 61 +++---- src/rrd_first.c | 47 ++--- src/rrd_flushcached.c | 54 +++--- src/rrd_graph.c | 298 ++++++++++++++++---------------- src/rrd_graph.h | 13 +- src/rrd_info.c | 58 +++---- src/rrd_last.c | 46 ++--- src/rrd_lastupdate.c | 45 ++--- src/rrd_restore.c | 47 ++--- src/rrd_tool.h | 8 +- src/rrd_tune.c | 141 +++++++-------- src/rrd_update.c | 94 +++++----- src/rrd_xport.c | 5 +- 20 files changed, 511 insertions(+), 644 deletions(-) diff --git a/WIN32-BUILD-TIPS.txt b/WIN32-BUILD-TIPS.txt index c8651bb5..71f258d9 100644 --- a/WIN32-BUILD-TIPS.txt +++ b/WIN32-BUILD-TIPS.txt @@ -204,8 +204,8 @@ enabled by default for MSVC++ projects). MSVC++ 6.0 does not support precompiled headers if #include directives contain MACROS. (2) Compile Release build with Default optimization, not the Maximize Speed optimization. I encountered some strange errors -(related to argument processing for complex commands like graph-- -perhaps the getopt stuff is too blame) with Maximize Speed. +(related to argument processing for complex commands like graph) with +Maximize Speed. (3) libart relies upon config.h (ostensibly generated by the configure script-- but of course not on Win32 platforms). ..\..\confignt (which contains a static Win32 version of config.h) should be on diff --git a/bindings/python/rrdtoolmodule.c b/bindings/python/rrdtoolmodule.c index 2fc4ba24..cd3607d9 100644 --- a/bindings/python/rrdtoolmodule.c +++ b/bindings/python/rrdtoolmodule.c @@ -47,8 +47,6 @@ static const char *__version__ = PACKAGE_VERSION; //#include "rrd_extra.h" static PyObject *ErrorObject; -extern int optind; -extern int opterr; /* forward declaration to keep compiler happy */ void initrrdtool( @@ -114,9 +112,6 @@ static int create_args( (*argv)[0] = command; *argc = element_count + 1; - /* reset getopt state */ - opterr = optind = 0; - return 0; } diff --git a/src/Makefile.am b/src/Makefile.am index c30eb2b7..83aaae49 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,7 +18,8 @@ AM_CFLAGS = @CFLAGS@ -I$(top_srcdir) UPD_C_FILES = \ mutex.c \ - rrd_strtod.c \ + optparse.c \ + rrd_strtod.c \ rrd_create.c \ hash_32.c \ rrd_parsetime.c \ @@ -55,7 +56,6 @@ if BUILD_RRDGRAPH RRD_C_FILES += rrd_graph.c \ rrd_graph_helper.c \ rrd_xport.c \ - optparse.c \ rrd_gfx.c \ pngsize.c endif @@ -130,7 +130,7 @@ bin_PROGRAMS += rrdcached endif rrdcgi_SOURCES = rrd_cgi.c -rrdcgi_LDADD = librrd.la +rrdcgi_LDADD = librrd.la librrdupd.la rrdupdate_SOURCES = rrdupdate.c rrdupdate_LDADD = librrdupd.la @@ -140,9 +140,9 @@ rrdtool_DEPENDENCIES = librrd.la rrdtool_LDADD = librrd.la rrdcached_SOURCES = rrd_daemon.c -rrdcached_DEPENDENCIES = librrd_th.la +rrdcached_DEPENDENCIES = librrd_th.la librrdupd.la rrdcached_CPPFLAGS = -DVERSION='"$(VERSION)"' -DLOCALSTATEDIR='"$(localstatedir)"' -rrdcached_LDADD = librrd_th.la $(ALL_LIBS) +rrdcached_LDADD = librrd_th.la librrdupd.la $(ALL_LIBS) pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = librrd.pc diff --git a/src/rrd_cgi.c b/src/rrd_cgi.c index e9c92b7b..8a80e21e 100644 --- a/src/rrd_cgi.c +++ b/src/rrd_cgi.c @@ -452,10 +452,12 @@ int main( char *buffer; long i; long filter = 0; - struct option long_options[] = { - {"filter", no_argument, 0, 'f'}, - {0, 0, 0, 0} + struct optparse_long longopts[] = { + {"filter", 'f', OPTPARSE_NONE}, + {0}, }; + struct optparse options; + int opt; #ifdef MUST_DISABLE_SIGFPE signal(SIGFPE, SIG_IGN); @@ -463,27 +465,18 @@ int main( #ifdef MUST_DISABLE_FPMASK fpsetmask(0); #endif - optind = 0; - opterr = 0; /* initialize getopt */ /* what do we get for cmdline arguments? for (i=0;i= argc) { + if (options.optind >= options.argc) { fprintf(stderr, "ERROR: expected a filename\n"); exit(1); } else { - readfile(argv[optind], &buffer, 1); + readfile(options.argv[options.optind], &buffer, 1); } if (rrd_test_error()) { diff --git a/src/rrd_create.c b/src/rrd_create.c index 32c9bd65..9e300438 100644 --- a/src/rrd_create.c +++ b/src/rrd_create.c @@ -67,17 +67,17 @@ int rrd_create( int argc, char **argv) { - struct option long_options[] = { - {"start", required_argument, 0, 'b'}, - {"step", required_argument, 0, 's'}, - {"daemon", required_argument, 0, 'd'}, - {"source", required_argument, 0, 'r'}, - {"template", required_argument, 0, 't'}, - {"no-overwrite", no_argument, 0, 'O'}, - {0, 0, 0, 0} + struct optparse_long longopts[] = { + {"start", 'b', OPTPARSE_REQUIRED}, + {"step", 's', OPTPARSE_REQUIRED}, + {"daemon", 'd', OPTPARSE_REQUIRED}, + {"source", 'r', OPTPARSE_REQUIRED}, + {"template", 't', OPTPARSE_REQUIRED}, + {"no-overwrite", 'O', OPTPARSE_NONE}, + {0}, }; - int option_index = 0; - int opt; + struct optparse options; + int opt; time_t last_up = -1; unsigned long pdp_step = 0; rrd_time_value_t last_up_tv; @@ -89,20 +89,13 @@ int rrd_create( const char **sources_array = NULL; char *template = NULL; - optind = 0; - opterr = 0; /* initialize getopt */ - - while (1) { - opt = getopt_long(argc, argv, "b:s:d:r:t:O", long_options, &option_index); - - if (opt == EOF) - break; - + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'd': if (opt_daemon != NULL) - free (opt_daemon); - opt_daemon = strdup (optarg); + free (opt_daemon); + opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); @@ -112,7 +105,7 @@ int rrd_create( break; case 'b': - if ((parsetime_error = rrd_parsetime(optarg, &last_up_tv))) { + if ((parsetime_error = rrd_parsetime(options.optarg, &last_up_tv))) { rrd_set_error("start time: %s", parsetime_error); rc = -1; goto done; @@ -136,7 +129,7 @@ int rrd_create( break; case 's': - if ((parsetime_error = rrd_scaled_duration(optarg, 1, &pdp_step))) { + if ((parsetime_error = rrd_scaled_duration(options.optarg, 1, &pdp_step))) { rrd_set_error("step size: %s", parsetime_error); rc = -1; goto done; @@ -149,18 +142,18 @@ int rrd_create( case 'r': { struct stat st; - if (stat(optarg, &st) != 0) { - rrd_set_error("error checking for source RRD %s: %s", optarg, rrd_strerror(errno)); + if (stat(options.optarg, &st) != 0) { + rrd_set_error("error checking for source RRD %s: %s", options.optarg, rrd_strerror(errno)); rc = -1; goto done; } if (!S_ISREG(st.st_mode)) { - rrd_set_error("Not a regular file: %s", optarg); + rrd_set_error("Not a regular file: %s", options.optarg); rc = -1; goto done; } - char * optcpy = strdup(optarg); + char * optcpy = strdup(options.optarg); if (optcpy == NULL) { rrd_set_error("Cannot allocate string"); rc = -1; @@ -181,7 +174,7 @@ int rrd_create( rc = -1; goto done; } - char * optcpy = strdup(optarg); + char * optcpy = strdup(options.optarg); if (optcpy == NULL) { rrd_set_error("Cannot allocate string"); rc = -1; @@ -192,15 +185,12 @@ int rrd_create( break; } case '?': - if (optopt != 0) - rrd_set_error("unknown option '%c'", optopt); - else - rrd_set_error("unknown option '%s'", argv[optind - 1]); - rc = -1; - goto done; + rrd_set_error("%s", options.errmsg); + rc = -1; + goto done; } } - if (optind == argc) { + if (options.optind == options.argc) { rrd_set_error("need name of an rrd file to create"); rc = -1; goto done; @@ -221,15 +211,17 @@ int rrd_create( } rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) { - rc = rrdc_create_r2(argv[optind], + rc = rrdc_create_r2(options.argv[options.optind], pdp_step, last_up, opt_no_overwrite, sources_array, template, - argc - optind - 1, (const char **) (argv + optind + 1)); + options.argc - options.optind - 1, + (const char **) (options.argv + options.optind + 1)); } else { - rc = rrd_create_r2(argv[optind], + rc = rrd_create_r2(options.argv[options.optind], pdp_step, last_up, opt_no_overwrite, sources_array, template, - argc - optind - 1, (const char **) (argv + optind + 1)); + options.argc - options.optind - 1, + (const char **) (options.argv + options.optind + 1)); } done: if (sources_array != NULL) { @@ -881,7 +873,7 @@ int rrd_create_r2( rrd.stat_head->pdp_step = 300; } - /* optind points to the first non-option command line arg, + /* options.optind points to the first non-option command line arg, * in this case, the file name. */ /* Compute the FNV hash value (used by SEASONAL and DEVSEASONAL * arrays. */ diff --git a/src/rrd_daemon.c b/src/rrd_daemon.c index b98b0b4f..ed9b1035 100644 --- a/src/rrd_daemon.c +++ b/src/rrd_daemon.c @@ -3880,6 +3880,7 @@ static int cleanup (void) /* {{{ */ static int read_options (int argc, char **argv) /* {{{ */ { + struct optparse options; int option; int status = 0; const char *parsetime_error = NULL; @@ -3891,7 +3892,8 @@ static int read_options (int argc, char **argv) /* {{{ */ default_socket.socket_group = (gid_t)-1; default_socket.socket_permissions = (mode_t)-1; - while ((option = getopt(argc, argv, "?a:Bb:Ff:gG:hj:Ll:m:OP:p:Rs:t:U:w:z:")) != -1) + optparse_init(&options, argc, argv); + while ((option = optparse(&options, "?a:Bb:Ff:gG:hj:Ll:m:OP:p:Rs:t:U:w:z:")) != -1) { switch (option) { @@ -3910,7 +3912,7 @@ static int read_options (int argc, char **argv) /* {{{ */ char * ep; struct group *grp; - group_gid = strtoul(optarg, &ep, 10); + group_gid = strtoul(options.optarg, &ep, 10); if (0 == *ep) { /* we were passed a number */ @@ -3918,11 +3920,11 @@ static int read_options (int argc, char **argv) /* {{{ */ } else { - grp = getgrnam(optarg); + grp = getgrnam(options.optarg); } if (NULL == grp) { - fprintf (stderr, "read_options: couldn't map \"%s\" to a group, Sorry\n", optarg); + fprintf (stderr, "read_options: couldn't map \"%s\" to a group, Sorry\n", options.optarg); return (5); } daemon_gid = grp->gr_gid; @@ -3940,7 +3942,7 @@ static int read_options (int argc, char **argv) /* {{{ */ char * ep; struct passwd *pw; - uid = strtoul(optarg, &ep, 10); + uid = strtoul(options.optarg, &ep, 10); if (0 == *ep) { /* we were passed a number */ @@ -3948,11 +3950,11 @@ static int read_options (int argc, char **argv) /* {{{ */ } else { - pw = getpwnam(optarg); + pw = getpwnam(options.optarg); } if (NULL == pw) { - fprintf (stderr, "read_options: couldn't map \"%s\" to a user, Sorry\n", optarg); + fprintf (stderr, "read_options: couldn't map \"%s\" to a user, Sorry\n", options.optarg); return (5); } daemon_uid = pw->pw_uid; @@ -3979,7 +3981,7 @@ static int read_options (int argc, char **argv) /* {{{ */ if ('L' == option) new->addr = strdup(""); else - new->addr = strdup(optarg); + new->addr = strdup(options.optarg); /* Add permissions to the socket {{{ */ if (default_socket.permissions != 0) @@ -4011,7 +4013,7 @@ static int read_options (int argc, char **argv) /* {{{ */ gid_t group_gid; struct group *grp; - group_gid = strtoul(optarg, NULL, 10); + group_gid = strtoul(options.optarg, NULL, 10); if (errno != EINVAL && group_gid>0) { /* we were passed a number */ @@ -4019,7 +4021,7 @@ static int read_options (int argc, char **argv) /* {{{ */ } else { - grp = getgrnam(optarg); + grp = getgrnam(options.optarg); } if (grp) @@ -4029,7 +4031,7 @@ static int read_options (int argc, char **argv) /* {{{ */ else { /* no idea what the user wanted... */ - fprintf (stderr, "read_options: couldn't map \"%s\" to a group, Sorry\n", optarg); + fprintf (stderr, "read_options: couldn't map \"%s\" to a group, Sorry\n", options.optarg); return (5); } } @@ -4041,11 +4043,11 @@ static int read_options (int argc, char **argv) /* {{{ */ long tmp; char *endptr = NULL; - tmp = strtol (optarg, &endptr, 8); - if ((endptr == optarg) || (! endptr) || (*endptr != '\0') + tmp = strtol(options.optarg, &endptr, 8); + if ((endptr == options.optarg) || (! endptr) || (*endptr != '\0') || (tmp > 07777) || (tmp < 0)) { fprintf (stderr, "read_options: Invalid file mode \"%s\".\n", - optarg); + options.optarg); return (5); } @@ -4062,7 +4064,7 @@ static int read_options (int argc, char **argv) /* {{{ */ socket_permission_clear (&default_socket); - optcopy = strdup (optarg); + optcopy = strdup(options.optarg); dummy = optcopy; saveptr = NULL; while ((ptr = strtok_r (dummy, ", ", &saveptr)) != NULL) @@ -4086,8 +4088,8 @@ static int read_options (int argc, char **argv) /* {{{ */ { unsigned long temp; - if ((parsetime_error = rrd_scaled_duration(optarg, 1, &temp))) { - fprintf(stderr, "Invalid flush interval %s: %s\n", optarg, parsetime_error); + if ((parsetime_error = rrd_scaled_duration(options.optarg, 1, &temp))) { + fprintf(stderr, "Invalid flush interval %s: %s\n", options.optarg, parsetime_error); status = 3; } else { config_flush_interval = temp; @@ -4099,8 +4101,8 @@ static int read_options (int argc, char **argv) /* {{{ */ { unsigned long temp; - if ((parsetime_error = rrd_scaled_duration(optarg, 1, &temp))) { - fprintf(stderr, "Invalid write interval %s: %s\n", optarg, parsetime_error); + if ((parsetime_error = rrd_scaled_duration(options.optarg, 1, &temp))) { + fprintf(stderr, "Invalid write interval %s: %s\n", options.optarg, parsetime_error); status = 2; } else { config_write_interval = temp; @@ -4112,8 +4114,8 @@ static int read_options (int argc, char **argv) /* {{{ */ { unsigned long temp; - if ((parsetime_error = rrd_scaled_duration(optarg, 1, &temp))) { - fprintf(stderr, "Invalid write jitter %s: %s\n", optarg, parsetime_error); + if ((parsetime_error = rrd_scaled_duration(options.optarg, 1, &temp))) { + fprintf(stderr, "Invalid write jitter %s: %s\n", options.optarg, parsetime_error); status = 2; } else { config_write_jitter = temp; @@ -4124,12 +4126,12 @@ static int read_options (int argc, char **argv) /* {{{ */ case 't': { int threads; - threads = atoi(optarg); + threads = atoi(options.optarg); if (threads >= 1) config_queue_threads = threads; else { - fprintf (stderr, "Invalid thread count: -t %s\n", optarg); + fprintf (stderr, "Invalid thread count: -t %s\n", options.optarg); return 1; } } @@ -4150,7 +4152,7 @@ static int read_options (int argc, char **argv) /* {{{ */ if (config_base_dir != NULL) free (config_base_dir); - config_base_dir = strdup (optarg); + config_base_dir = strdup(options.optarg); if (config_base_dir == NULL) { fprintf (stderr, "read_options: strdup failed.\n"); @@ -4186,7 +4188,7 @@ static int read_options (int argc, char **argv) /* {{{ */ if (len < 1) { - fprintf (stderr, "Invalid base directory: %s\n", optarg); + fprintf (stderr, "Invalid base directory: %s\n", options.optarg); free(base_realpath); return (4); } @@ -4218,7 +4220,7 @@ static int read_options (int argc, char **argv) /* {{{ */ { if (config_pid_file != NULL) free (config_pid_file); - config_pid_file = strdup (optarg); + config_pid_file = strdup(options.optarg); if (config_pid_file == NULL) { fprintf (stderr, "read_options: strdup failed.\n"); @@ -4235,7 +4237,7 @@ static int read_options (int argc, char **argv) /* {{{ */ { if (journal_dir) free(journal_dir); - journal_dir = realpath((const char *)optarg, NULL); + journal_dir = realpath((const char *)options.optarg, NULL); if (journal_dir) { // if we were able to properly resolve the path, lets have a copy @@ -4254,7 +4256,7 @@ static int read_options (int argc, char **argv) /* {{{ */ return 6; } } else { - fprintf(stderr, "Unable to resolve journal path (%s,%s)\n", optarg, + fprintf(stderr, "Unable to resolve journal path (%s,%s)\n", options.optarg, errno ? rrd_strerror(errno) : ""); return 6; } @@ -4263,12 +4265,12 @@ static int read_options (int argc, char **argv) /* {{{ */ case 'a': { - int temp = atoi(optarg); + int temp = atoi(options.optarg); if (temp > 0) config_alloc_chunk = temp; else { - fprintf(stderr, "Invalid allocation size: %s\n", optarg); + fprintf(stderr, "Invalid allocation size: %s\n", options.optarg); return 10; } } @@ -4319,7 +4321,7 @@ static int read_options (int argc, char **argv) /* {{{ */ status = 1; break; } /* switch (option) */ - } /* while (getopt) */ + } /* while (opt != -1) */ /* advise the user when values are not sane */ if (config_flush_interval < 2 * config_write_interval) diff --git a/src/rrd_dump.c b/src/rrd_dump.c index 4d76580b..17e0966f 100644 --- a/src/rrd_dump.c +++ b/src/rrd_dump.c @@ -517,6 +517,14 @@ int rrd_dump( int argc, char **argv) { + int opt; + struct optparse_long longopts[] = { + {"daemon", 'd', OPTPARSE_REQUIRED}, + {"header", 'h', OPTPARSE_REQUIRED}, + {"no-header", 'n', OPTPARSE_NONE}, + {0}, + }; + struct optparse options; int rc; /** * 0 = no header @@ -528,29 +536,13 @@ int rrd_dump( /* init rrd clean */ - optind = 0; - opterr = 0; /* initialize getopt */ - - while (42) {/* ha ha */ - int opt; - int option_index = 0; - static struct option long_options[] = { - {"daemon", required_argument, 0, 'd'}, - {"header", required_argument, 0, 'h'}, - {"no-header", no_argument, 0, 'n'}, - {0, 0, 0, 0} - }; - - opt = getopt_long(argc, argv, "d:h:n", long_options, &option_index); - - if (opt == EOF) - break; - + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'd': if (opt_daemon != NULL) free (opt_daemon); - opt_daemon = strdup (optarg); + opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); @@ -563,11 +555,11 @@ int rrd_dump( break; case 'h': - if (strcmp(optarg, "dtd") == 0) { + if (strcmp(options.optarg, "dtd") == 0) { opt_header = 1; - } else if (strcmp(optarg, "xsd") == 0) { + } else if (strcmp(options.optarg, "xsd") == 0) { opt_header = 2; - } else if (strcmp(optarg, "none") == 0) { + } else if (strcmp(options.optarg, "none") == 0) { opt_header = 0; } break; @@ -576,28 +568,28 @@ int rrd_dump( rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}]\n" "[--no-header|-n]\n" "[--daemon|-d address]\n" - "file.rrd [file.xml]", argv[0]); + "file.rrd [file.xml]", options.argv[0]); return (-1); break; } - } /* while (42) */ + } /* while (opt != -1) */ - if ((argc - optind) < 1 || (argc - optind) > 2) { + if ((options.argc - options.optind) < 1 || (options.argc - options.optind) > 2) { rrd_set_error("usage rrdtool %s [--header|-h {none,xsd,dtd}]\n" "[--no-header|-n]\n" "[--daemon|-d address]\n" - "file.rrd [file.xml]", argv[0]); + "file.rrd [file.xml]", options.argv[0]); return (-1); } - rc = rrdc_flush_if_daemon(opt_daemon, argv[optind]); + rc = rrdc_flush_if_daemon(opt_daemon, options.argv[options.optind]); if (opt_daemon) free(opt_daemon); if (rc) return (rc); - if ((argc - optind) == 2) { - rc = rrd_dump_opt_r(argv[optind], argv[optind + 1], opt_header); + if ((options.argc - options.optind) == 2) { + rc = rrd_dump_opt_r(options.argv[options.optind], options.argv[options.optind + 1], opt_header); } else { - rc = rrd_dump_opt_r(argv[optind], NULL, opt_header); + rc = rrd_dump_opt_r(options.argv[options.optind], NULL, opt_header); } return rc; diff --git a/src/rrd_fetch.c b/src/rrd_fetch.c index ef8338e7..23515c02 100644 --- a/src/rrd_fetch.c +++ b/src/rrd_fetch.c @@ -56,6 +56,7 @@ #include "rrd_client.h" #include "rrd_is_thread_safe.h" + /* #define DEBUG */ int rrd_fetch( @@ -79,40 +80,32 @@ int rrd_fetch( rrd_time_value_t start_tv, end_tv; const char *parsetime_error = NULL; - struct option long_options[] = { - {"resolution", required_argument, 0, 'r'}, - {"start", required_argument, 0, 's'}, - {"end", required_argument, 0, 'e'}, - {"align-start", no_argument, 0, 'a'}, - {"daemon", required_argument, 0, 'd'}, - {0, 0, 0, 0} + struct optparse_long longopts[] = { + {"resolution", 'r', OPTPARSE_REQUIRED}, + {"start", 's', OPTPARSE_REQUIRED}, + {"end", 'e', OPTPARSE_REQUIRED}, + {"align-start", 'a', OPTPARSE_NONE}, + {"daemon", 'd', OPTPARSE_REQUIRED}, + {0}, }; - - optind = 0; - opterr = 0; /* initialize getopt */ + struct optparse options; + int opt; /* init start and end time */ rrd_parsetime("end-24h", &start_tv); rrd_parsetime("now", &end_tv); - while (1) { - int option_index = 0; - int opt; - - opt = getopt_long(argc, argv, "ar:s:e:d:", long_options, &option_index); - - if (opt == EOF) - break; - + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 's': - if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) { + if ((parsetime_error = rrd_parsetime(options.optarg, &start_tv))) { rrd_set_error("start time: %s", parsetime_error); return -1; } break; case 'e': - if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) { + if ((parsetime_error = rrd_parsetime(options.optarg, &end_tv))) { rrd_set_error("end time: %s", parsetime_error); return -1; } @@ -121,7 +114,7 @@ int rrd_fetch( align_start = 1; break; case 'r': - if ((parsetime_error = rrd_scaled_duration(optarg, 1, &step_tmp))) { + if ((parsetime_error = rrd_scaled_duration(options.optarg, 1, &step_tmp))) { rrd_set_error("resolution: %s", parsetime_error); return -1; } @@ -129,18 +122,18 @@ int rrd_fetch( case 'd': if (opt_daemon != NULL) - free (opt_daemon); - opt_daemon = strdup (optarg); + free (opt_daemon); + opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); - return (-1); + return -1; } break; case '?': - rrd_set_error("unknown option '-%c'", optopt); - return (-1); + rrd_set_error("%s", options.errmsg); + return -1; } } @@ -170,21 +163,21 @@ int rrd_fetch( *end = end_tmp; *step = step_tmp; - if (optind + 1 >= argc) { - rrd_set_error("Usage: rrdtool %s [options]", argv[0]); + if (options.optind + 1 >= options.argc) { + rrd_set_error("Usage: rrdtool %s [options]", options.argv[0]); return -1; } - cf = argv[optind + 1]; + cf = options.argv[options.optind + 1]; rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) - status = rrdc_fetch (argv[optind], cf, start, end, step, - ds_cnt, ds_namv, data); + status = rrdc_fetch (options.argv[options.optind], + cf, start, end, step, ds_cnt, ds_namv, data); else - status = rrd_fetch_r(argv[optind], cf, start, end, step, - ds_cnt, ds_namv, data); + status = rrd_fetch_r(options.argv[options.optind], + cf, start, end, step, ds_cnt, ds_namv, data); if (status != 0) return (-1); diff --git a/src/rrd_first.c b/src/rrd_first.c index 5b3dc28b..d3c31c36 100644 --- a/src/rrd_first.c +++ b/src/rrd_first.c @@ -15,30 +15,22 @@ time_t rrd_first( int argc, char **argv) { + struct optparse_long longopts[] = { + {"rraindex", 129, OPTPARSE_REQUIRED}, + {"daemon", 'd', OPTPARSE_REQUIRED}, + {0}, + }; + struct optparse options; + int opt; int target_rraindex = 0; char *endptr; char *opt_daemon = NULL; - struct option long_options[] = { - {"rraindex", required_argument, 0, 129}, - {"daemon", required_argument, 0, 'd'}, - {0, 0, 0, 0} - }; - - optind = 0; - opterr = 0; /* initialize getopt */ - - while (1) { - int option_index = 0; - int opt; - - opt = getopt_long(argc, argv, "d:", long_options, &option_index); - - if (opt == EOF) - break; + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 129: - target_rraindex = strtol(optarg, &endptr, 0); + target_rraindex = strtol(options.optarg, &endptr, 0); if (target_rraindex < 0) { rrd_set_error("invalid rraindex number"); return (-1); @@ -47,31 +39,30 @@ time_t rrd_first( case 'd': if (opt_daemon != NULL) free (opt_daemon); - opt_daemon = strdup (optarg); + opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); - return (-1); + return -1; } break; - default: - rrd_set_error("usage rrdtool %s [--rraindex number] [--daemon|-d ] file.rrd", - argv[0]); - return (-1); + case '?': + rrd_set_error("%s", options.errmsg); + return -1; } } - if (optind >= argc) { + if (options.optind >= options.argc) { rrd_set_error("usage rrdtool %s [--rraindex number] [--daemon|-d ] file.rrd", - argv[0]); + options.argv[0]); return -1; } rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) { - return (rrdc_first (argv[optind], target_rraindex)); + return rrdc_first(options.argv[options.optind], target_rraindex); } else { - return (rrd_first_r(argv[optind], target_rraindex)); + return rrd_first_r(options.argv[options.optind], target_rraindex); } } diff --git a/src/rrd_flushcached.c b/src/rrd_flushcached.c index a0293398..bdc227e4 100644 --- a/src/rrd_flushcached.c +++ b/src/rrd_flushcached.c @@ -24,52 +24,42 @@ int rrd_flushcached (int argc, char **argv) { + struct optparse_long longopts[] = { + {"daemon", 'd', OPTPARSE_REQUIRED}, + {0}, + }; + struct optparse options; + int opt; char *opt_daemon = NULL; int status; int i; - /* initialize getopt */ - optind = 0; - opterr = 0; - - while (42) - { - int opt; - static struct option long_options[] = - { - {"daemon", required_argument, 0, 'd'}, - {0, 0, 0, 0} - }; - - opt = getopt_long(argc, argv, "d:", long_options, NULL); - - if (opt == -1) - break; - + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'd': if (opt_daemon != NULL) free (opt_daemon); - opt_daemon = strdup (optarg); + opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); - return (-1); + return -1; } break; - default: - rrd_set_error ("Usage: rrdtool %s [--daemon|-d ] ", - argv[0]); - return (-1); + case '?': + rrd_set_error("%s", options.errmsg); + return -1; } - } /* while (42) */ + } /* while (opt!=-1) */ - if ((argc - optind) < 1) + if ((options.argc - options.optind) < 1) { - rrd_set_error ("Usage: rrdtool %s [--daemon|-d ] [ ...]", argv[0]); - return (-1); + rrd_set_error("Usage: rrdtool %s [--daemon|-d ] [ ...]", + options.argv[0]); + return -1; } /* try to connect to rrdcached */ @@ -88,19 +78,19 @@ int rrd_flushcached (int argc, char **argv) } status = 0; - for (i = optind; i < argc; i++) + for (i = options.optind; i < options.argc; i++) { - status = rrdc_flush(argv[i]); + status = rrdc_flush(options.argv[i]); if (status) { char *error; int remaining; error = strdup(rrd_get_error()); - remaining = argc - optind - 1; + remaining = options.argc - options.optind - 1; rrd_set_error("Flushing of file \"%s\" failed: %s. Skipping " - "remaining %i file%s.", argv[i], + "remaining %i file%s.", options.argv[i], ((! error) || (*error == '\0')) ? "unknown error" : error, remaining, (remaining == 1) ? "" : "s"); free(error); diff --git a/src/rrd_graph.c b/src/rrd_graph.c index d6ff8063..9ac3a11b 100644 --- a/src/rrd_graph.c +++ b/src/rrd_graph.c @@ -292,7 +292,7 @@ double ytr( if (strcmp(#VV, string) == 0) return VVV ; enum gf_en gf_conv( - char *string) + const char *string) { conv_if(PRINT, GF_PRINT); @@ -315,7 +315,7 @@ enum gf_en gf_conv( } enum gfx_if_en if_conv( - char *string) + const char *string) { conv_if(PNG, IF_PNG); @@ -340,7 +340,7 @@ enum gfx_if_en if_conv( } enum gfx_type_en type_conv( - char *string) + const char *string) { conv_if(TIME , GTYPE_TIME); conv_if(XY, GTYPE_XY); @@ -348,7 +348,7 @@ enum gfx_type_en type_conv( } enum tmt_en tmt_conv( - char *string) + const char *string) { conv_if(SECOND, TMT_SECOND); @@ -362,7 +362,7 @@ enum tmt_en tmt_conv( } enum grc_en grc_conv( - char *string) + const char *string) { conv_if(BACK, GRC_BACK); @@ -380,7 +380,7 @@ enum grc_en grc_conv( } enum text_prop_en text_prop_conv( - char *string) + const char *string) { conv_if(DEFAULT, TEXT_PROP_DEFAULT); @@ -4562,24 +4562,25 @@ rrd_info_t *rrd_graph_v( { image_desc_t im; rrd_info_t *grinfo; + struct optparse options; rrd_graph_init(&im); /* a dummy surface so that we can measure text sizes for placements */ - rrd_graph_options(argc, argv, &im); + rrd_graph_options(argc, argv, &options, &im); if (rrd_test_error()) { rrd_info_free(im.grinfo); im_free(&im); return NULL; } - if (optind >= argc) { + if (options.optind >= options.argc) { rrd_info_free(im.grinfo); im_free(&im); rrd_set_error("missing filename"); return NULL; } - if (strcmp(argv[optind], "-") != 0) { - im.graphfile = strdup(argv[optind]); + if (strcmp(options.argv[options.optind], "-") != 0) { + im.graphfile = strdup(options.argv[options.optind]); if (im.graphfile == NULL) { rrd_set_error("cannot allocate sufficient memory for filename length"); rrd_info_free(im.grinfo); @@ -4588,7 +4589,7 @@ rrd_info_t *rrd_graph_v( } } /* else we work in memory: im.graphfile==NULL */ - rrd_graph_script(argc, argv, &im, optind+1); + rrd_graph_script(options.argc, options.argv, &im, options.optind+1); if (rrd_test_error()) { rrd_info_free(im.grinfo); @@ -4646,7 +4647,7 @@ rrd_info_t *rrd_graph_v( static void rrd_set_font_desc ( - image_desc_t *im,int prop,char *font, double size ){ + image_desc_t *im, int prop, const char *font, double size ){ if (font){ strncpy(im->text_prop[prop].font, font, sizeof(text_prop[prop].font) - 1); im->text_prop[prop].font[sizeof(text_prop[prop].font) - 1] = '\0'; @@ -4759,7 +4760,7 @@ void rrd_graph_init( for (i = 0; i < DIM(text_prop); i++) { im->text_prop[i].size = -1; im->text_prop[i].font_desc = NULL; - rrd_set_font_desc(im,i, deffont ? deffont : text_prop[i].font,text_prop[i].size); + rrd_set_font_desc(im, i, deffont ? deffont : text_prop[i].font, text_prop[i].size); } mutex_lock(im->fontmap_mutex); @@ -4801,8 +4802,8 @@ void rrd_graph_init( void rrd_graph_options( int argc, char *argv[], - image_desc_t - *im) + struct optparse *poptions, + image_desc_t *im) { int stroff; char *parsetime_error = NULL; @@ -4818,83 +4819,77 @@ void rrd_graph_options( #define LONGOPT_UNITS_SI 255 /* *INDENT-OFF* */ - struct option long_options[] = { - { "alt-autoscale", no_argument, 0, 'A'}, - { "imgformat", required_argument, 0, 'a'}, - { "font-smoothing-threshold", required_argument, 0, 'B'}, - { "base", required_argument, 0, 'b'}, - { "color", required_argument, 0, 'c'}, - { "full-size-mode", no_argument, 0, 'D'}, - { "daemon", required_argument, 0, 'd'}, - { "slope-mode", no_argument, 0, 'E'}, - { "end", required_argument, 0, 'e'}, - { "force-rules-legend", no_argument, 0, 'F'}, - { "imginfo", required_argument, 0, 'f'}, - { "graph-render-mode", required_argument, 0, 'G'}, - { "no-legend", no_argument, 0, 'g'}, - { "height", required_argument, 0, 'h'}, - { "no-minor", no_argument, 0, 'I'}, - { "interlaced", no_argument, 0, 'i'}, - { "alt-autoscale-min", no_argument, 0, 'J'}, - { "only-graph", no_argument, 0, 'j'}, - { "units-length", required_argument, 0, 'L'}, - { "lower-limit", required_argument, 0, 'l'}, - { "alt-autoscale-max", no_argument, 0, 'M'}, - { "zoom", required_argument, 0, 'm'}, - { "no-gridfit", no_argument, 0, 'N'}, - { "font", required_argument, 0, 'n'}, - { "logarithmic", no_argument, 0, 'o'}, - { "pango-markup", no_argument, 0, 'P'}, - { "font-render-mode", required_argument, 0, 'R'}, - { "rigid", no_argument, 0, 'r'}, - { "step", required_argument, 0, 'S'}, - { "start", required_argument, 0, 's'}, - { "tabwidth", required_argument, 0, 'T'}, - { "title", required_argument, 0, 't'}, - { "upper-limit", required_argument, 0, 'u'}, - { "vertical-label", required_argument, 0, 'v'}, - { "watermark", required_argument, 0, 'W'}, - { "width", required_argument, 0, 'w'}, - { "units-exponent", required_argument, 0, 'X'}, - { "x-grid", required_argument, 0, 'x'}, - { "alt-y-grid", no_argument, 0, 'Y'}, - { "y-grid", required_argument, 0, 'y'}, - { "lazy", no_argument, 0, 'z'}, - { "use-nan-for-all-missing-data", no_argument, 0, 'Z'}, - { "units", required_argument, 0, LONGOPT_UNITS_SI}, - { "alt-y-mrtg", no_argument, 0, 1000}, /* this has no effect it is just here to save old apps from crashing when they use it */ - { "disable-rrdtool-tag",no_argument, 0, 1001}, - { "right-axis", required_argument, 0, 1002}, - { "right-axis-label", required_argument, 0, 1003}, - { "right-axis-format", required_argument, 0, 1004}, - { "legend-position", required_argument, 0, 1005}, - { "legend-direction", required_argument, 0, 1006}, - { "border", required_argument, 0, 1007}, - { "grid-dash", required_argument, 0, 1008}, - { "dynamic-labels", no_argument, 0, 1009}, - { "week-fmt", required_argument, 0, 1010}, - { "graph-type", required_argument, 0, 1011}, - { "left-axis-format", required_argument, 0, 1012}, - { "left-axis-formatter",required_argument, 0, 1013}, - { "right-axis-formatter",required_argument,0, 1014}, - { 0, 0, 0, 0} + struct optparse_long longopts[] = { + {"alt-autoscale", 'A', OPTPARSE_NONE}, + {"imgformat", 'a', OPTPARSE_REQUIRED}, + {"font-smoothing-threshold", 'B', OPTPARSE_REQUIRED}, + {"base", 'b', OPTPARSE_REQUIRED}, + {"color", 'c', OPTPARSE_REQUIRED}, + {"full-size-mode", 'D', OPTPARSE_NONE}, + {"daemon", 'd', OPTPARSE_REQUIRED}, + {"slope-mode", 'E', OPTPARSE_NONE}, + {"end", 'e', OPTPARSE_REQUIRED}, + {"force-rules-legend", 'F', OPTPARSE_NONE}, + {"imginfo", 'f', OPTPARSE_REQUIRED}, + {"graph-render-mode", 'G', OPTPARSE_REQUIRED}, + {"no-legend", 'g', OPTPARSE_NONE}, + {"height", 'h', OPTPARSE_REQUIRED}, + {"no-minor", 'I', OPTPARSE_NONE}, + {"interlaced", 'i', OPTPARSE_NONE}, + {"alt-autoscale-min", 'J', OPTPARSE_NONE}, + {"only-graph", 'j', OPTPARSE_NONE}, + {"units-length", 'L', OPTPARSE_REQUIRED}, + {"lower-limit", 'l', OPTPARSE_REQUIRED}, + {"alt-autoscale-max", 'M', OPTPARSE_NONE}, + {"zoom", 'm', OPTPARSE_REQUIRED}, + {"no-gridfit", 'N', OPTPARSE_NONE}, + {"font", 'n', OPTPARSE_REQUIRED}, + {"logarithmic", 'o', OPTPARSE_NONE}, + {"pango-markup", 'P', OPTPARSE_NONE}, + {"font-render-mode", 'R', OPTPARSE_REQUIRED}, + {"rigid", 'r', OPTPARSE_NONE}, + {"step", 'S', OPTPARSE_REQUIRED}, + {"start", 's', OPTPARSE_REQUIRED}, + {"tabwidth", 'T', OPTPARSE_REQUIRED}, + {"title", 't', OPTPARSE_REQUIRED}, + {"upper-limit", 'u', OPTPARSE_REQUIRED}, + {"vertical-label", 'v', OPTPARSE_REQUIRED}, + {"watermark", 'W', OPTPARSE_REQUIRED}, + {"width", 'w', OPTPARSE_REQUIRED}, + {"units-exponent", 'X', OPTPARSE_REQUIRED}, + {"x-grid", 'x', OPTPARSE_REQUIRED}, + {"alt-y-grid", 'Y', OPTPARSE_NONE}, + {"y-grid", 'y', OPTPARSE_REQUIRED}, + {"lazy", 'z', OPTPARSE_NONE}, + {"use-nan-for-all-missing-data", 'Z', OPTPARSE_NONE}, + {"units", LONGOPT_UNITS_SI, OPTPARSE_REQUIRED}, + {"alt-y-mrtg", 1000, OPTPARSE_NONE}, /* this has no effect it is just here to save old apps from crashing when they use it */ + {"disable-rrdtool-tag",1001, OPTPARSE_NONE}, + {"right-axis", 1002, OPTPARSE_REQUIRED}, + {"right-axis-label", 1003, OPTPARSE_REQUIRED}, + {"right-axis-format", 1004, OPTPARSE_REQUIRED}, + {"legend-position", 1005, OPTPARSE_REQUIRED}, + {"legend-direction", 1006, OPTPARSE_REQUIRED}, + {"border", 1007, OPTPARSE_REQUIRED}, + {"grid-dash", 1008, OPTPARSE_REQUIRED}, + {"dynamic-labels", 1009, OPTPARSE_NONE}, + {"week-fmt", 1010, OPTPARSE_REQUIRED}, + {"graph-type", 1011, OPTPARSE_REQUIRED}, + {"left-axis-format", 1012, OPTPARSE_REQUIRED}, + {"left-axis-formatter",1013, OPTPARSE_REQUIRED}, + {"right-axis-formatter",1014, OPTPARSE_REQUIRED}, + {0} }; /* *INDENT-ON* */ + int opt; - optind = 0; - opterr = 0; /* initialize getopt */ rrd_parsetime("end-24h", &start_tv); rrd_parsetime("now", &end_tv); - while (1) { - int option_index = 0; - int opt; + + optparse_init(poptions, argc, argv); + while ((opt = optparse_long(poptions, longopts, NULL)) != -1) { int col_start, col_end; - opt = getopt_long(argc, argv, - "Aa:B:b:c:Dd:Ee:Ff:G:gh:IiJjL:l:Mm:Nn:oPR:rS:s:T:t:u:v:W:w:X:x:Yy:Zz", - long_options, &option_index); - if (opt == EOF) - break; switch (opt) { case 'I': im->extra_flags |= NOMINOR; @@ -4921,28 +4916,28 @@ void rrd_graph_options( im->extra_flags |= ALLOW_MISSING_DS; break; case 1005: - if (strcmp(optarg, "north") == 0) { + if (strcmp(poptions->optarg, "north") == 0) { im->legendposition = NORTH; - } else if (strcmp(optarg, "west") == 0) { + } else if (strcmp(poptions->optarg, "west") == 0) { im->legendposition = WEST; - } else if (strcmp(optarg, "south") == 0) { + } else if (strcmp(poptions->optarg, "south") == 0) { im->legendposition = SOUTH; - } else if (strcmp(optarg, "east") == 0) { + } else if (strcmp(poptions->optarg, "east") == 0) { im->legendposition = EAST; } else { - rrd_set_error("unknown legend-position '%s'", optarg); + rrd_set_error("unknown legend-position '%s'", poptions->optarg); return; } break; case 1006: - if (strcmp(optarg, "topdown") == 0) { + if (strcmp(poptions->optarg, "topdown") == 0) { im->legenddirection = TOP_DOWN; - } else if (strcmp(optarg, "bottomup") == 0) { + } else if (strcmp(poptions->optarg, "bottomup") == 0) { im->legenddirection = BOTTOM_UP; - } else if (strcmp(optarg, "bottomup2") == 0) { + } else if (strcmp(poptions->optarg, "bottomup2") == 0) { im->legenddirection = BOTTOM_UP2; } else { - rrd_set_error("unknown legend-position '%s'", optarg); + rrd_set_error("unknown legend-position '%s'", poptions->optarg); return; } break; @@ -4957,26 +4952,26 @@ void rrd_graph_options( rrd_set_error("--units can only be used once!"); return; } - if (strcmp(optarg, "si") == 0) + if (strcmp(poptions->optarg, "si") == 0) im->extra_flags |= FORCE_UNITS_SI; else { - rrd_set_error("invalid argument for --units: %s", optarg); + rrd_set_error("invalid argument for --units: %s", poptions->optarg); return; } break; case 'X': - im->unitsexponent = atoi(optarg); + im->unitsexponent = atoi(poptions->optarg); break; case 'L': - im->unitslength = atoi(optarg); + im->unitslength = atoi(poptions->optarg); im->forceleftspace = 1; break; case 'T': - if (rrd_strtodbl(optarg, 0, &(im->tabwidth), "option -T") != 2) + if (rrd_strtodbl(poptions->optarg, 0, &(im->tabwidth), "option -T") != 2) return; break; case 'S': - im->step = atoi(optarg); + im->step = atoi(poptions->optarg); break; case 'N': im->gridfit = 0; @@ -4985,23 +4980,23 @@ void rrd_graph_options( im->with_markup = 1; break; case 's': - if ((parsetime_error = rrd_parsetime(optarg, &start_tv))) { + if ((parsetime_error = rrd_parsetime(poptions->optarg, &start_tv)) != 0) { rrd_set_error("start time: %s", parsetime_error); return; } break; case 'e': - if ((parsetime_error = rrd_parsetime(optarg, &end_tv))) { + if ((parsetime_error = rrd_parsetime(poptions->optarg, &end_tv)) != 0) { rrd_set_error("end time: %s", parsetime_error); return; } break; case 'x': - if (strcmp(optarg, "none") == 0) { + if (strcmp(poptions->optarg, "none") == 0) { im->draw_x_grid = 0; break; }; - if (sscanf(optarg, + if (sscanf(poptions->optarg, "%10[A-Z]:%ld:%10[A-Z]:%ld:%10[A-Z]:%ld:%ld:%n", scan_gtm, &im->xlab_user.gridst, @@ -5010,7 +5005,7 @@ void rrd_graph_options( scan_ltm, &im->xlab_user.labst, &im->xlab_user.precis, &stroff) == 7 && stroff != 0) { - im->xlab_form=strdup(optarg + stroff); + im->xlab_form=strdup(poptions->optarg + stroff); if (!im->xlab_form) { rrd_set_error("cannot allocate memory for xlab_form"); return; @@ -5037,11 +5032,11 @@ void rrd_graph_options( } break; case 'y': - if (strcmp(optarg, "none") == 0) { + if (strcmp(poptions->optarg, "none") == 0) { im->draw_y_grid = 0; break; }; - if (sscanf(optarg, "%40[0-9.e+-]:%d", double_str , &im->ylabfact) == 2) { + if (sscanf(poptions->optarg, "%40[0-9.e+-]:%d", double_str , &im->ylabfact) == 2) { if (rrd_strtodbl( double_str, 0, &(im->ygridstep), "option -y") != 2){ return; } @@ -5058,10 +5053,10 @@ void rrd_graph_options( } break; case 1007: - im->draw_3d_border = atoi(optarg); + im->draw_3d_border = atoi(poptions->optarg); break; case 1008: /* grid-dash */ - if(sscanf(optarg, + if(sscanf(poptions->optarg, "%40[0-9.e+-]:%40[0-9.e+-]", double_str, double_str2 ) == 2) { @@ -5079,11 +5074,11 @@ void rrd_graph_options( im->dynamic_labels = 1; break; case 1010: - strncpy(week_fmt,optarg,sizeof week_fmt); + strncpy(week_fmt, poptions->optarg, sizeof week_fmt); week_fmt[(sizeof week_fmt)-1]='\0'; break; case 1002: /* right y axis */ - if(sscanf(optarg, + if(sscanf(poptions->optarg, "%40[0-9.e+-]:%40[0-9.e+-]", double_str, double_str2 ) == 2 @@ -5099,32 +5094,32 @@ void rrd_graph_options( } break; case 1003: - im->second_axis_legend=strdup(optarg); + im->second_axis_legend=strdup(poptions->optarg); if (!im->second_axis_legend) { rrd_set_error("cannot allocate memory for second_axis_legend"); return; } break; case 1004: - im->second_axis_format=strdup(optarg); + im->second_axis_format=strdup(poptions->optarg); if (!im->second_axis_format) { rrd_set_error("cannot allocate memory for second_axis_format"); return; } break; case 1012: - im->primary_axis_format=strdup(optarg); + im->primary_axis_format=strdup(poptions->optarg); if (!im->primary_axis_format) { rrd_set_error("cannot allocate memory for primary_axis_format"); return; } break; case 1013: - if (!strcmp(optarg, "numeric")) { + if (!strcmp(poptions->optarg, "numeric")) { im->primary_axis_formatter = VALUE_FORMATTER_NUMERIC; - } else if (!strcmp(optarg, "timestamp")) { + } else if (!strcmp(poptions->optarg, "timestamp")) { im->primary_axis_formatter = VALUE_FORMATTER_TIMESTAMP; - } else if (!strcmp(optarg, "duration")) { + } else if (!strcmp(poptions->optarg, "duration")) { im->primary_axis_formatter = VALUE_FORMATTER_DURATION; } else { rrd_set_error("Unknown left axis formatter"); @@ -5132,11 +5127,11 @@ void rrd_graph_options( } break; case 1014: - if (!strcmp(optarg, "numeric")) { + if (!strcmp(poptions->optarg, "numeric")) { im->second_axis_formatter = VALUE_FORMATTER_NUMERIC; - } else if (!strcmp(optarg, "timestamp")) { + } else if (!strcmp(poptions->optarg, "timestamp")) { im->second_axis_formatter = VALUE_FORMATTER_TIMESTAMP; - } else if (!strcmp(optarg, "duration")) { + } else if (!strcmp(poptions->optarg, "duration")) { im->second_axis_formatter = VALUE_FORMATTER_DURATION; } else { rrd_set_error("Unknown right axis formatter"); @@ -5144,24 +5139,24 @@ void rrd_graph_options( } break; case 'v': - im->ylegend=strdup(optarg); + im->ylegend=strdup(poptions->optarg); if (!im->ylegend) { rrd_set_error("cannot allocate memory for ylegend"); return; } break; case 'u': - if (rrd_strtodbl(optarg, 0, &(im->maxval), "option -u") != 2){ + if (rrd_strtodbl(poptions->optarg, 0, &(im->maxval), "option -u") != 2){ return; } break; case 'l': - if (rrd_strtodbl(optarg, 0, &(im->minval), "option -l") != 2){ + if (rrd_strtodbl(poptions->optarg, 0, &(im->minval), "option -l") != 2){ return; } break; case 'b': - im->base = atol(optarg); + im->base = atol(poptions->optarg); if (im->base != 1024 && im->base != 1000) { rrd_set_error ("the only sensible value for base apart from 1000 is 1024"); @@ -5169,7 +5164,7 @@ void rrd_graph_options( } break; case 'w': - long_tmp = atol(optarg); + long_tmp = atol(poptions->optarg); if (long_tmp < 10) { rrd_set_error("width below 10 pixels"); return; @@ -5177,7 +5172,7 @@ void rrd_graph_options( im->xsize = long_tmp; break; case 'h': - long_tmp = atol(optarg); + long_tmp = atol(poptions->optarg); if (long_tmp < 10) { rrd_set_error("height below 10 pixels"); return; @@ -5194,19 +5189,19 @@ void rrd_graph_options( im->rigid = 1; break; case 'f': - im->imginfo = optarg; + im->imginfo = poptions->optarg; break; case 'a': if ((int) - (im->imgformat = if_conv(optarg)) == -1) { - rrd_set_error("unsupported graphics format '%s'", optarg); + (im->imgformat = if_conv(poptions->optarg)) == -1) { + rrd_set_error("unsupported graphics format '%s'", poptions->optarg); return; } break; case 1011: if ((int) - (im->graph_type = type_conv(optarg)) == -1) { - rrd_set_error("unsupported graphics type '%s'", optarg); + (im->graph_type = type_conv(poptions->optarg)) == -1) { + rrd_set_error("unsupported graphics type '%s'", poptions->optarg); return; } break; @@ -5220,7 +5215,7 @@ void rrd_graph_options( im->logarithmic = 1; break; case 'c': - if (sscanf(optarg, + if (sscanf(poptions->optarg, "%10[A-Z]#%n%8lx%n", col_nam, &col_start, &color, &col_end) == 2) { int ci; @@ -5270,7 +5265,7 @@ void rrd_graph_options( double size = 1; int end; - if (sscanf(optarg, "%10[A-Z]:%40[0-9.e+-]%n", prop, double_str, &end) >= 2 + if (sscanf(poptions->optarg, "%10[A-Z]:%40[0-9.e+-]%n", prop, double_str, &end) >= 2 && rrd_strtodbl( double_str, 0, &size, NULL) == 2) { int sindex, propidx; @@ -5280,13 +5275,13 @@ void rrd_graph_options( if (size > 0) { rrd_set_font_desc(im,propidx,NULL,size); } - if ((int) strlen(optarg) > end+2) { - if (optarg[end] == ':') { - rrd_set_font_desc(im,propidx,optarg + end + 1,0); + if ((int) strlen(poptions->optarg) > end+2) { + if (poptions->optarg[end] == ':') { + rrd_set_font_desc(im, propidx, poptions->optarg+end+1, 0); } else { rrd_set_error ("expected : after font size in '%s'", - optarg); + poptions->optarg); return; } } @@ -5306,7 +5301,7 @@ void rrd_graph_options( break; } case 'm': - if (rrd_strtodbl(optarg, 0, &(im->zoom), "option -m") != 2){ + if (rrd_strtodbl(poptions->optarg, 0, &(im->zoom), "option -m") != 2){ return; } if (im->zoom <= 0.0) { @@ -5315,40 +5310,40 @@ void rrd_graph_options( } break; case 't': - im->title=strdup(optarg); + im->title=strdup(poptions->optarg); if (!im->title) { rrd_set_error("cannot allocate memory for title"); return; } break; case 'R': - if (strcmp(optarg, "normal") == 0) { + if (strcmp(poptions->optarg, "normal") == 0) { cairo_font_options_set_antialias (im->font_options, CAIRO_ANTIALIAS_GRAY); cairo_font_options_set_hint_style (im->font_options, CAIRO_HINT_STYLE_FULL); - } else if (strcmp(optarg, "light") == 0) { + } else if (strcmp(poptions->optarg, "light") == 0) { cairo_font_options_set_antialias (im->font_options, CAIRO_ANTIALIAS_GRAY); cairo_font_options_set_hint_style (im->font_options, CAIRO_HINT_STYLE_SLIGHT); - } else if (strcmp(optarg, "mono") == 0) { + } else if (strcmp(poptions->optarg, "mono") == 0) { cairo_font_options_set_antialias (im->font_options, CAIRO_ANTIALIAS_NONE); cairo_font_options_set_hint_style (im->font_options, CAIRO_HINT_STYLE_FULL); } else { - rrd_set_error("unknown font-render-mode '%s'", optarg); + rrd_set_error("unknown font-render-mode '%s'", poptions->optarg); return; } break; case 'G': - if (strcmp(optarg, "normal") == 0) + if (strcmp(poptions->optarg, "normal") == 0) im->graph_antialias = CAIRO_ANTIALIAS_GRAY; - else if (strcmp(optarg, "mono") == 0) + else if (strcmp(poptions->optarg, "mono") == 0) im->graph_antialias = CAIRO_ANTIALIAS_NONE; else { - rrd_set_error("unknown graph-render-mode '%s'", optarg); + rrd_set_error("unknown graph-render-mode '%s'", poptions->optarg); return; } break; @@ -5356,7 +5351,7 @@ void rrd_graph_options( /* not supported curently */ break; case 'W': - im->watermark=strdup(optarg); + im->watermark=strdup(poptions->optarg); if (!im->watermark) { rrd_set_error("cannot allocate memory for watermark"); return; @@ -5371,7 +5366,7 @@ void rrd_graph_options( return; } - im->daemon_addr = strdup(optarg); + im->daemon_addr = strdup(poptions->optarg); if (im->daemon_addr == NULL) { rrd_set_error("strdup failed"); @@ -5381,13 +5376,10 @@ void rrd_graph_options( break; } case '?': - if (optopt != 0) - rrd_set_error("unknown option '%c'", optopt); - else - rrd_set_error("unknown option '%s'", argv[optind - 1]); + rrd_set_error("%s", poptions->errmsg); return; } - } /* while (1) */ + } /* while (opt != -1) */ mutex_lock(im->fontmap_mutex); pango_cairo_context_set_font_options(pango_layout_get_context(im->layout), im->font_options); diff --git a/src/rrd_graph.h b/src/rrd_graph.h index 3d7d3a99..b8c7c9b7 100644 --- a/src/rrd_graph.h +++ b/src/rrd_graph.h @@ -358,17 +358,17 @@ double ytr( image_desc_t *, double); enum gf_en gf_conv( - char *); + const char *); enum gfx_if_en if_conv( - char *); + const char *); enum gfx_type_en type_conv( - char *); + const char *); enum tmt_en tmt_conv( - char *); + const char *); enum grc_en grc_conv( - char *); + const char *); enum text_prop_en text_prop_conv( - char *); + const char *); int im_free( image_desc_t *); void auto_scale( @@ -455,6 +455,7 @@ void time_clean( void rrd_graph_options( int, char **, + struct optparse *, image_desc_t *); void rrd_graph_script( int, diff --git a/src/rrd_info.c b/src/rrd_info.c index 29813238..015cc19a 100644 --- a/src/rrd_info.c +++ b/src/rrd_info.c @@ -88,37 +88,29 @@ rrd_info_t *rrd_info( int argc, char **argv) { + struct optparse_long longopts[] = { + {"daemon", 'd', OPTPARSE_REQUIRED}, + {"noflush", 'F', OPTPARSE_NONE}, + {0}, + }; + struct optparse options; + int opt; rrd_info_t *info; char *opt_daemon = NULL; int status; int flushfirst = 1; - optind = 0; - opterr = 0; /* initialize getopt */ - - while (42) { - int opt; - int option_index = 0; - static struct option long_options[] = { - {"daemon", required_argument, 0, 'd'}, - {"noflush", no_argument, 0, 'F'}, - {0, 0, 0, 0} - }; - - opt = getopt_long(argc, argv, "d:F", long_options, &option_index); - - if (opt == EOF) - break; - + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'd': if (opt_daemon != NULL) - free (opt_daemon); - opt_daemon = strdup (optarg); + free (opt_daemon); + opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); - return (NULL); + return NULL; } break; @@ -126,30 +118,28 @@ rrd_info_t *rrd_info( flushfirst = 0; break; - default: - rrd_set_error ("Usage: rrdtool %s [--daemon|-d [--noflush|-F]] ", - argv[0]); - return (NULL); - break; + case '?': + rrd_set_error("%s", options.errmsg); + return NULL; } - } /* while (42) */ + } /* while (opt != -1) */ - if ((argc - optind) != 1) { + if (options.argc - options.optind != 1) { rrd_set_error ("Usage: rrdtool %s [--daemon |-d [--noflush|-F]] ", - argv[0]); - return (NULL); + options.argv[0]); + return NULL; } - if( flushfirst ) { - status = rrdc_flush_if_daemon(opt_daemon, argv[optind]); - if (status) return (NULL); + if (flushfirst) { + status = rrdc_flush_if_daemon(opt_daemon, options.argv[options.optind]); + if (status) return (NULL); } rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) - info = rrdc_info (argv[optind]); + info = rrdc_info(options.argv[options.optind]); else - info = rrd_info_r(argv[optind]); + info = rrd_info_r(options.argv[options.optind]); if (opt_daemon) free(opt_daemon); return (info); diff --git a/src/rrd_last.c b/src/rrd_last.c index dc012a16..9852c289 100644 --- a/src/rrd_last.c +++ b/src/rrd_last.c @@ -15,28 +15,20 @@ time_t rrd_last( { char *opt_daemon = NULL; time_t lastupdate; + struct optparse_long longopts[] = { + {"daemon", 'd', OPTPARSE_REQUIRED}, + {0}, + }; + struct optparse options; + int opt; - optind = 0; - opterr = 0; /* initialize getopt */ - - while (42) { - int opt; - int option_index = 0; - static struct option long_options[] = { - {"daemon", required_argument, 0, 'd'}, - {0, 0, 0, 0} - }; - - opt = getopt_long(argc, argv, "d:", long_options, &option_index); - - if (opt == EOF) - break; - + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'd': if (opt_daemon != NULL) free (opt_daemon); - opt_daemon = strdup (optarg); + opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); @@ -44,26 +36,24 @@ time_t rrd_last( } break; - default: - rrd_set_error ("Usage: rrdtool %s [--daemon|-d ] ", - argv[0]); - return (-1); - break; + case '?': + rrd_set_error("%s", options.errmsg); + return -1; } - } /* while (42) */ + } /* while (opt) */ - if ((argc - optind) != 1) { + if ((options.argc - options.optind) != 1) { rrd_set_error ("Usage: rrdtool %s [--daemon|-d ] ", - argv[0]); - return (-1); + options.argv[0]); + return -1; } rrdc_connect (opt_daemon); if (rrdc_is_connected (opt_daemon)) - lastupdate = rrdc_last (argv[optind]); + lastupdate = rrdc_last(options.argv[options.optind]); else - lastupdate = rrd_last_r(argv[optind]); + lastupdate = rrd_last_r(options.argv[options.optind]); if (opt_daemon) free(opt_daemon); return (lastupdate); diff --git a/src/rrd_lastupdate.c b/src/rrd_lastupdate.c index a3569c15..49b2fa2e 100644 --- a/src/rrd_lastupdate.c +++ b/src/rrd_lastupdate.c @@ -12,35 +12,26 @@ int rrd_lastupdate (int argc, char **argv) { + struct optparse_long longopts[] = { + {"daemon", 'd', OPTPARSE_REQUIRED}, + {0}, + }; + struct optparse options; + int opt; time_t last_update; char **ds_names; char **last_ds; unsigned long ds_count, i; int status; - char *opt_daemon = NULL; - optind = 0; - opterr = 0; /* initialize getopt */ - - while (42) { - int opt; - int option_index = 0; - static struct option long_options[] = { - {"daemon", required_argument, 0, 'd'}, - {0, 0, 0, 0} - }; - - opt = getopt_long (argc, argv, "d:", long_options, &option_index); - - if (opt == EOF) - break; - + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'd': if (opt_daemon != NULL) free (opt_daemon); - opt_daemon = strdup (optarg); + opt_daemon = strdup(options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); @@ -48,25 +39,23 @@ int rrd_lastupdate (int argc, char **argv) } break; - default: - rrd_set_error ("Usage: rrdtool %s [--daemon|-d ] ", - argv[0]); - return (-1); - break; + case '?': + rrd_set_error("%s", options.errmsg); + return -1; } - } /* while (42) */ + } /* while (opt!=-1) */ - if ((argc - optind) != 1) { + if ((options.argc - options.optind) != 1) { rrd_set_error ("Usage: rrdtool %s [--daemon|-d ] ", - argv[0]); + options.argv[0]); return (-1); } - status = rrdc_flush_if_daemon(opt_daemon, argv[optind]); + status = rrdc_flush_if_daemon(opt_daemon, options.argv[options.optind]); if (opt_daemon) free (opt_daemon); if (status) return (-1); - status = rrd_lastupdate_r (argv[optind], + status = rrd_lastupdate_r(options.argv[options.optind], &last_update, &ds_count, &ds_names, &last_ds); if (status != 0) return (status); diff --git a/src/rrd_restore.c b/src/rrd_restore.c index 7292dff0..ca31531e 100644 --- a/src/rrd_restore.c +++ b/src/rrd_restore.c @@ -1383,24 +1383,17 @@ int rrd_restore( int argc, char **argv) { + struct optparse_long longopts[] = { + {"range-check", 'r', OPTPARSE_NONE}, + {"force-overwrite", 'f', OPTPARSE_NONE}, + {0}, + }; + struct optparse options; + int opt; rrd_t *rrd; - /* init rrd clean */ - optind = 0; - opterr = 0; /* initialize getopt */ - while (42) { - int opt; - int option_index = 0; - static struct option long_options[] = { - {"range-check", no_argument, 0, 'r'}, - {"force-overwrite", no_argument, 0, 'f'}, - {0, 0, 0, 0} - }; - - opt = getopt_long(argc, argv, "rf", long_options, &option_index); - - if (opt == EOF) - break; + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'r': opt_range_check = 1; @@ -1410,27 +1403,25 @@ int rrd_restore( opt_force_overwrite = 1; break; - default: - rrd_set_error("usage rrdtool %s [--range-check|-r] " - "[--force-overwrite|-f] file.xml file.rrd", - argv[0]); - return (-1); - break; + case '?': + rrd_set_error("%s", options.errmsg); + return -1; } - } /* while (42) */ + } /* while (opt != -1) */ - if ((argc - optind) != 2) { + if (options.argc - options.optind != 2) { rrd_set_error("usage rrdtool %s [--range-check|-r] " - "[--force-overwrite|-f] file.xml file.rrd", argv[0]); - return (-1); + "[--force-overwrite|-f] file.xml file.rrd", + options.argv[0]); + return -1; } - rrd = parse_file(argv[optind]); + rrd = parse_file(options.argv[options.optind]); if (rrd == NULL) return (-1); - if (write_file(argv[optind + 1], rrd) != 0) { + if (write_file(options.argv[options.optind + 1], rrd) != 0) { local_rrd_free(rrd); return (-1); } diff --git a/src/rrd_tool.h b/src/rrd_tool.h index 400e3e04..0d9ca5d4 100644 --- a/src/rrd_tool.h +++ b/src/rrd_tool.h @@ -46,13 +46,7 @@ extern "C" { #endif /* local include files -- need to be after the system ones */ -#ifdef HAVE_GETOPT_LONG -#define _GNU_SOURCE -#include -#else -#include "rrd_getopt.h" -#endif - +#include "optparse.h" #include "rrd_format.h" #ifndef max diff --git a/src/rrd_tune.c b/src/rrd_tune.c index c7ec443f..0d538cd6 100644 --- a/src/rrd_tune.c +++ b/src/rrd_tune.c @@ -53,21 +53,21 @@ int set_hwarg( rrd_t *rrd, enum cf_en cf, enum rra_par_en rra_par, - char *arg); + const char *arg); int set_deltaarg( rrd_t *rrd, enum rra_par_en rra_par, - char *arg); + const char *arg); int set_windowarg( rrd_t *rrd, enum rra_par_en, - char *arg); + const char *arg); int set_hwsmootharg( rrd_t *rrd, enum cf_en cf, enum rra_par_en rra_par, - char *arg); + const char *arg); int rrd_tune( int argc, @@ -89,50 +89,45 @@ int rrd_tune( char *opt_daemon = NULL; char double_str[ 41 ] = {0}; const char *in_filename = NULL; - struct option long_options[] = { - {"heartbeat", required_argument, 0, 'h'}, - {"minimum", required_argument, 0, 'i'}, - {"maximum", required_argument, 0, 'a'}, - {"data-source-type", required_argument, 0, 'd'}, - {"data-source-rename", required_argument, 0, 'r'}, + struct optparse_long longopts[] = { + {"heartbeat", 'h', OPTPARSE_REQUIRED}, + {"minimum", 'i', OPTPARSE_REQUIRED}, + {"maximum", 'a', OPTPARSE_REQUIRED}, + {"data-source-type", 'd', OPTPARSE_REQUIRED}, + {"data-source-rename", 'r', OPTPARSE_REQUIRED}, /* added parameter tuning options for aberrant behavior detection */ - {"deltapos", required_argument, 0, 'p'}, - {"deltaneg", required_argument, 0, 'n'}, - {"window-length", required_argument, 0, 'w'}, - {"failure-threshold", required_argument, 0, 'f'}, - {"alpha", required_argument, 0, 'x'}, - {"beta", required_argument, 0, 'y'}, - {"gamma", required_argument, 0, 'z'}, - {"gamma-deviation", required_argument, 0, 'v'}, - {"smoothing-window", required_argument, 0, 's'}, - {"smoothing-window-deviation", required_argument, 0, 'S'}, - {"aberrant-reset", required_argument, 0, 'b'}, + {"deltapos", 'p', OPTPARSE_REQUIRED}, + {"deltaneg", 'n', OPTPARSE_REQUIRED}, + {"window-length", 'w', OPTPARSE_REQUIRED}, + {"failure-threshold", 'f', OPTPARSE_REQUIRED}, + {"alpha", 'x', OPTPARSE_REQUIRED}, + {"beta", 'y', OPTPARSE_REQUIRED}, + {"gamma", 'z', OPTPARSE_REQUIRED}, + {"gamma-deviation", 'v', OPTPARSE_REQUIRED}, + {"smoothing-window", 's', OPTPARSE_REQUIRED}, + {"smoothing-window-deviation", 'S', OPTPARSE_REQUIRED}, + {"aberrant-reset", 'b', OPTPARSE_REQUIRED}, // integration of rrd_modify functionality. - {"step", required_argument, 0, 't'}, + {"step", 't', OPTPARSE_REQUIRED}, /* unfortunately, '-d' is already taken */ - {"daemon", required_argument, 0, 'D'}, - {0, 0, 0, 0} + {"daemon", 'D', OPTPARSE_REQUIRED}, + {0} }; - - optind = 0; - opterr = 0; /* initialize getopt */ + struct optparse options; + int opt; /* before we open the input RRD, we should flush it from any caching daemon, because we might totally rewrite it later on */ /* for this, we FIRST have to find the daemon, this means we must parse options twice... */ - while (1) { - int option_index = 0; - int opt = getopt_long(argc, argv, "h:i:a:d:r:p:n:w:f:x:y:z:v:s:S:b:t:D:", - long_options, &option_index); - if (opt == EOF) - break; + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 'D': if (opt_daemon != NULL) free (opt_daemon); - opt_daemon = strdup (optarg); + opt_daemon = strdup (options.optarg); if (opt_daemon == NULL) { rrd_set_error ("strdup failed."); @@ -155,17 +150,17 @@ int rrd_tune( opt_daemon = NULL; } - if (optind < 0 || optind >= argc) { + if (options.optind < 0 || options.optind >= options.argc) { // missing file name... rrd_set_error("missing file name"); goto done; } - /* NOTE: getopt_long reorders argv and places all NON option arguments to + /* NOTE: optparse_long reorders argv and places all NON option arguments to the back, starting with optind. This means the file name has travelled to - argv[optind] */ + options.argv[options.optind] */ - in_filename = argv[optind]; + in_filename = options.argv[options.optind]; if (rrdc_is_any_connected()) { // is it a good idea to just ignore the error ???? @@ -173,30 +168,21 @@ int rrd_tune( rrd_clear_error(); } - optind = 0; - opterr = 0; /* re-initialize getopt */ - rrd_init(&rrd); rrd_file = rrd_open(in_filename, &rrd, RRD_READWRITE | RRD_READAHEAD | RRD_READVALUES); if (rrd_file == NULL) { goto done; } - while (1) { - int option_index = 0; - int opt; + optparse_init(&options, argc, argv); /* re-initialize optparse */ + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { unsigned int strtod_ret_val; - opt = getopt_long(argc, argv, "h:i:a:d:r:p:n:w:f:x:y:z:v:s:S:b:t:D:", - long_options, &option_index); - if (opt == EOF) - break; - optcnt++; switch (opt) { case 'h': if ((matches = - sscanf(optarg, DS_NAM_FMT ":%ld", ds_nam, + sscanf(options.optarg, DS_NAM_FMT ":%ld", ds_nam, &heartbeat)) != 2) { rrd_set_error("invalid arguments for heartbeat"); goto done; @@ -208,7 +194,7 @@ int rrd_tune( break; case 'i': - matches = sscanf(optarg, DS_NAM_FMT ":%40[U0-9.e+-]", ds_nam, double_str); + matches = sscanf(options.optarg, DS_NAM_FMT ":%40[U0-9.e+-]", ds_nam, double_str); if ( matches == 2 ) { if (strcmp(double_str,"U") == 0){ min = DNAN; @@ -232,7 +218,7 @@ int rrd_tune( break; case 'a': - matches = sscanf(optarg, DS_NAM_FMT ":%40[U0-9.e+-]", ds_nam, double_str); + matches = sscanf(options.optarg, DS_NAM_FMT ":%40[U0-9.e+-]", ds_nam, double_str); if ( matches == 2 ) { if (strcmp(double_str,"U") == 0){ max = DNAN; @@ -257,7 +243,7 @@ int rrd_tune( case 'd': if ((matches = - sscanf(optarg, DS_NAM_FMT ":" DST_FMT, ds_nam, dst)) != 2) { + sscanf(options.optarg, DS_NAM_FMT ":" DST_FMT, ds_nam, dst)) != 2) { rrd_set_error("invalid arguments for data source type"); goto done; } @@ -281,7 +267,7 @@ int rrd_tune( break; case 'r': if ((matches = - sscanf(optarg, DS_NAM_FMT ":" DS_NAM_FMT, ds_nam, + sscanf(options.optarg, DS_NAM_FMT ":" DS_NAM_FMT, ds_nam, ds_new)) != 2) { rrd_set_error("invalid arguments for data source type"); goto done; @@ -293,53 +279,53 @@ int rrd_tune( rrd.ds_def[ds].ds_nam[DS_NAM_SIZE - 1] = '\0'; break; case 'p': - if (set_deltaarg(&rrd, RRA_delta_pos, optarg)) { + if (set_deltaarg(&rrd, RRA_delta_pos, options.optarg)) { goto done; } break; case 'n': - if (set_deltaarg(&rrd, RRA_delta_neg, optarg)) { + if (set_deltaarg(&rrd, RRA_delta_neg, options.optarg)) { goto done; } break; case 'f': - if (set_windowarg(&rrd, RRA_failure_threshold, optarg)) { + if (set_windowarg(&rrd, RRA_failure_threshold, options.optarg)) { goto done; } break; case 'w': - if (set_windowarg(&rrd, RRA_window_len, optarg)) { + if (set_windowarg(&rrd, RRA_window_len, options.optarg)) { goto done; } break; case 'x': - if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_alpha, optarg)) { - if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_alpha, optarg)) { + if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_alpha, options.optarg)) { + if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_alpha, options.optarg)) { goto done; } rrd_clear_error(); } break; case 'y': - if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_beta, optarg)) { - if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_beta, optarg)) { + if (set_hwarg(&rrd, CF_HWPREDICT, RRA_hw_beta, options.optarg)) { + if (set_hwarg(&rrd, CF_MHWPREDICT, RRA_hw_beta, options.optarg)) { goto done; } rrd_clear_error(); } break; case 'z': - if (set_hwarg(&rrd, CF_SEASONAL, RRA_seasonal_gamma, optarg)) { + if (set_hwarg(&rrd, CF_SEASONAL, RRA_seasonal_gamma, options.optarg)) { goto done; } break; case 'v': - if (set_hwarg(&rrd, CF_DEVSEASONAL, RRA_seasonal_gamma, optarg)) { + if (set_hwarg(&rrd, CF_DEVSEASONAL, RRA_seasonal_gamma, options.optarg)) { goto done; } break; case 'b': - if (sscanf(optarg, DS_NAM_FMT, ds_nam) != 1) { + if (sscanf(options.optarg, DS_NAM_FMT, ds_nam) != 1) { rrd_set_error("invalid argument for aberrant-reset"); goto done; } @@ -357,7 +343,7 @@ int rrd_tune( strcpy(rrd.stat_head->version, RRD_VERSION4); /* smoothing_window causes Version 4 */ } if (set_hwsmootharg - (&rrd, CF_SEASONAL, RRA_seasonal_smoothing_window, optarg)) { + (&rrd, CF_SEASONAL, RRA_seasonal_smoothing_window, options.optarg)) { goto done; } break; @@ -367,21 +353,18 @@ int rrd_tune( } if (set_hwsmootharg (&rrd, CF_DEVSEASONAL, RRA_seasonal_smoothing_window, - optarg)) { + options.optarg)) { goto done; } break; case 't': - opt_newstep = atoi(optarg); + opt_newstep = atoi(options.optarg); break; case 'D': // ignore, handled in previous argv parsing round break; case '?': - if (optopt != 0) - rrd_set_error("unknown option '%c'", optopt); - else - rrd_set_error("unknown option '%s'", argv[optind - 1]); + rrd_set_error("%s", options.errmsg); goto done; } } @@ -395,7 +378,7 @@ int rrd_tune( sizeof(rra_def_t) * rrd.stat_head->rra_cnt); } - if (optind >= argc) { + if (options.optind >= options.argc) { int i; for (i = 0; i < (int) rrd.stat_head->ds_cnt; i++) @@ -418,8 +401,8 @@ int rrd_tune( } } - optind = handle_modify(&rrd, in_filename, argc, argv, optind + 1, opt_newstep); - if (optind < 0) { + optind = handle_modify(&rrd, in_filename, options.argc, options.argv, options.optind + 1, opt_newstep); + if (options.optind < 0) { goto done; } @@ -449,7 +432,7 @@ int set_hwarg( rrd_t *rrd, enum cf_en cf, enum rra_par_en rra_par, - char *arg) + const char *arg) { double param; unsigned long i; @@ -487,7 +470,7 @@ int set_hwsmootharg( rrd_t *rrd, enum cf_en cf, enum rra_par_en rra_par, - char *arg) + const char *arg) { double param; unsigned long i; @@ -526,7 +509,7 @@ int set_hwsmootharg( int set_deltaarg( rrd_t *rrd, enum rra_par_en rra_par, - char *arg) + const char *arg) { rrd_value_t param; unsigned long i; @@ -563,7 +546,7 @@ int set_deltaarg( int set_windowarg( rrd_t *rrd, enum rra_par_en rra_par, - char *arg) + const char *arg) { unsigned long param; unsigned long i, cdp_idx; diff --git a/src/rrd_update.c b/src/rrd_update.c index 1020d329..39e379fc 100644 --- a/src/rrd_update.c +++ b/src/rrd_update.c @@ -306,37 +306,30 @@ rrd_info_t *rrd_update_v( int argc, char **argv) { - char *tmplt = NULL; + struct optparse_long longopts[] = { + {"template", 't', OPTPARSE_REQUIRED}, + {"skip-past-updates", 's', OPTPARSE_NONE}, + {0}, + }; + struct optparse options; + int opt; + const char *tmplt = NULL; int extra_flags = 0; rrd_info_t *result = NULL; rrd_infoval_t rc; char *opt_daemon = NULL; - struct option long_options[] = { - {"template", required_argument, 0, 't'}, - {"skip-past-updates", no_argument, 0, 's'}, - {0, 0, 0, 0} - }; rc.u_int = -1; - optind = 0; - opterr = 0; /* initialize getopt */ - - while (1) { - int option_index = 0; - int opt; - - opt = getopt_long(argc, argv, "st:", long_options, &option_index); - - if (opt == EOF) - break; + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options, longopts, NULL)) != -1) { switch (opt) { case 't': - tmplt = optarg; + tmplt = options.optarg; break; case '?': - rrd_set_error("unknown option '%s'", argv[optind - 1]); + rrd_set_error("%s", options.errmsg); goto end_tag; } } @@ -346,20 +339,20 @@ rrd_info_t *rrd_update_v( rrd_set_error ("The \"%s\" environment variable is defined, " "but \"%s\" cannot work with rrdcached. Either unset " "the environment variable or use \"update\" instead.", - ENV_RRDCACHED_ADDRESS, argv[0]); + ENV_RRDCACHED_ADDRESS, options.argv[0]); goto end_tag; } /* need at least 2 arguments: filename, data. */ - if (argc - optind < 2) { + if (options.argc - options.optind < 2) { rrd_set_error("Not enough arguments"); goto end_tag; } rc.u_int = 0; result = rrd_info_push(NULL, sprintf_alloc("return_value"), RD_I_INT, rc); - rc.u_int = _rrd_updatex(argv[optind], tmplt,extra_flags, - argc - optind - 1, - (const char **) (argv + optind + 1), result); + rc.u_int = _rrd_updatex(options.argv[options.optind], tmplt,extra_flags, + options.argc - options.optind - 1, + (const char **) (options.argv + options.optind + 1), result); result->value.u_int = rc.u_int; end_tag: return result; @@ -678,31 +671,24 @@ int rrd_update( int argc, char **argv) { - struct option long_options[] = { - {"template", required_argument, 0, 't'}, - {"daemon", required_argument, 0, 'd'}, - {"skip-past-updates", no_argument, 0, 's'}, - {0, 0, 0, 0} + struct optparse_long longopts[] = { + {"template", 't', OPTPARSE_REQUIRED}, + {"daemon", 'd', OPTPARSE_REQUIRED}, + {"skip-past-updates", 's', OPTPARSE_NONE}, + {0}, }; - int option_index = 0; + struct optparse options; int opt; + char *tmplt = NULL; int extra_flags = 0; - char *tmplt = NULL; int rc = -1; char *opt_daemon = NULL; - optind = 0; - opterr = 0; /* initialize getopt */ - - while (1) { - opt = getopt_long(argc, argv, "t:d:s", long_options, &option_index); - - if (opt == EOF) - break; - + optparse_init(&options, argc, argv); + while ((opt = optparse_long(&options,longopts,NULL)) != -1) { switch (opt) { case 't': - tmplt = strdup(optarg); + tmplt = strdup(options.optarg); break; case 's': @@ -712,7 +698,7 @@ int rrd_update( case 'd': if (opt_daemon != NULL) free (opt_daemon); - opt_daemon = strdup (optarg); + opt_daemon = strdup (options.optarg); if (opt_daemon == NULL) { rrd_set_error("strdup failed."); @@ -721,13 +707,13 @@ int rrd_update( break; case '?': - rrd_set_error("unknown option '%s'", argv[optind - 1]); + rrd_set_error("%s", options.errmsg); goto out; } } /* need at least 2 arguments: filename, data. */ - if (argc - optind < 2) { + if (options.argc - options.optind < 2) { rrd_set_error("Not enough arguments"); goto out; } @@ -742,8 +728,8 @@ int rrd_update( if (! rrdc_is_connected(opt_daemon)) { - rc = rrd_updatex_r(argv[optind], tmplt,extra_flags, - argc - optind - 1, (const char **) (argv + optind + 1)); + rc = rrd_updatex_r(options.argv[options.optind], tmplt,extra_flags, + options.argc - options.optind - 1, (const char **) (options.argv + options.optind + 1)); } else /* we are connected */ { @@ -755,16 +741,16 @@ int rrd_update( goto out; } else { rc = rrd_template_update( - argv[optind], /* file */ + options.argv[options.optind], /* file */ tmplt, - argc - optind - 1, /* values_num */ - (const char *const *) (argv + optind + 1)); /* values */ + options.argc - options.optind - 1, /* values_num */ + (const char *const *) (options.argv + options.optind + 1)); /* values */ } } else rc = rrdc_update ( - argv[optind], /* file */ - argc - optind - 1, /* values_num */ - (const char *const *) (argv + optind + 1)); /* values */ + options.argv[options.optind], /* file */ + options.argc - options.optind - 1, /* values_num */ + (const char *const *) (options.argv + options.optind + 1)); /* values */ if (rc > 0) if (!rrd_test_error()) rrd_set_error("Failed sending the values to rrdcached: %s", @@ -2469,3 +2455,7 @@ static int write_changes_to_disk( return 0; } #endif + +/* + * vim: set sw=4 sts=4 ts=8 et fdm=marker : + */ diff --git a/src/rrd_xport.c b/src/rrd_xport.c index 35ed656a..4303af5e 100644 --- a/src/rrd_xport.c +++ b/src/rrd_xport.c @@ -12,7 +12,6 @@ #include "rrd_xport.h" #include "unused.h" #include "rrd_client.h" -#include "optparse.h" #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) #include @@ -152,7 +151,7 @@ int rrd_xport( } case '?': - rrd_set_error("%s: %s", argv[0], options.errmsg); + rrd_set_error("%s", options.errmsg); return -1; } } @@ -177,7 +176,7 @@ int rrd_xport( im.end = end_tmp; im.step = max((long) im.step, (im.end - im.start) / im.xsize); - rrd_graph_script(argc, argv, &im, options.optind); + rrd_graph_script(options.argc, options.argv, &im, options.optind); if (rrd_test_error()) { im_free(&im); return -1; -- 2.47.2