* 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
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
//#include "rrd_extra.h"
static PyObject *ErrorObject;
-extern int optind;
-extern int opterr;
/* forward declaration to keep compiler happy */
void initrrdtool(
(*argv)[0] = command;
*argc = element_count + 1;
- /* reset getopt state */
- opterr = optind = 0;
-
return 0;
}
UPD_C_FILES = \
mutex.c \
- rrd_strtod.c \
+ optparse.c \
+ rrd_strtod.c \
rrd_create.c \
hash_32.c \
rrd_parsetime.c \
RRD_C_FILES += rrd_graph.c \
rrd_graph_helper.c \
rrd_xport.c \
- optparse.c \
rrd_gfx.c \
pngsize.c
endif
endif
rrdcgi_SOURCES = rrd_cgi.c
-rrdcgi_LDADD = librrd.la
+rrdcgi_LDADD = librrd.la librrdupd.la
rrdupdate_SOURCES = rrdupdate.c
rrdupdate_LDADD = librrdupd.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
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);
#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;i++)
printf("%d-'%s'\n",i,argv[i]); */
- while (1) {
- int option_index = 0;
- int opt;
-
- opt = getopt_long(argc, argv, "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 'f':
filter = 1;
break;
case '?':
- printf("unknown commandline option '%s'\n", argv[optind - 1]);
+ printf("%s\n", options.errmsg);
return -1;
}
}
/* make sure we have one extra argument,
if there are others, we do not care Apache gives several */
- /* if ( (optind != argc-2
+ /* if ( (options.optind != options.argc-2
&& strstr( getenv("SERVER_SOFTWARE"),"Apache/2") != NULL)
- && optind != argc-1) { */
+ && options.optind != options.argc-1) { */
- if (optind >= 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()) {
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;
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.");
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;
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;
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;
rc = -1;
goto done;
}
- char * optcpy = strdup(optarg);
+ char * optcpy = strdup(options.optarg);
if (optcpy == NULL) {
rrd_set_error("Cannot allocate string");
rc = -1;
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;
}
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) {
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. */
static int read_options (int argc, char **argv) /* {{{ */
{
+ struct optparse options;
int option;
int status = 0;
const char *parsetime_error = NULL;
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)
{
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 */
}
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;
char * ep;
struct passwd *pw;
- uid = strtoul(optarg, &ep, 10);
+ uid = strtoul(options.optarg, &ep, 10);
if (0 == *ep)
{
/* we were passed a number */
}
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;
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)
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 */
}
else
{
- grp = getgrnam(optarg);
+ grp = getgrnam(options.optarg);
}
if (grp)
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);
}
}
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);
}
socket_permission_clear (&default_socket);
- optcopy = strdup (optarg);
+ optcopy = strdup(options.optarg);
dummy = optcopy;
saveptr = NULL;
while ((ptr = strtok_r (dummy, ", ", &saveptr)) != NULL)
{
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;
{
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;
{
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;
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;
}
}
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");
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);
}
{
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");
{
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
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;
}
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;
}
}
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)
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
/* 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.");
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;
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;
#include "rrd_client.h"
#include "rrd_is_thread_safe.h"
+
/* #define DEBUG */
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;
}
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;
}
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;
}
}
*end = end_tmp;
*step = step_tmp;
- if (optind + 1 >= argc) {
- rrd_set_error("Usage: rrdtool %s <file> <CF> [options]", argv[0]);
+ if (options.optind + 1 >= options.argc) {
+ rrd_set_error("Usage: rrdtool %s <file> <CF> [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);
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);
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 <addr>] 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 <addr>] 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);
}
}
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 <addr>] <file>",
- 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 <addr>] <file> [<file> ...]", argv[0]);
- return (-1);
+ rrd_set_error("Usage: rrdtool %s [--daemon|-d <addr>] <file> [<file> ...]",
+ options.argv[0]);
+ return -1;
}
/* try to connect to rrdcached */
}
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);
if (strcmp(#VV, string) == 0) return VVV ;
enum gf_en gf_conv(
- char *string)
+ const char *string)
{
conv_if(PRINT, GF_PRINT);
}
enum gfx_if_en if_conv(
- char *string)
+ const char *string)
{
conv_if(PNG, IF_PNG);
}
enum gfx_type_en type_conv(
- char *string)
+ const char *string)
{
conv_if(TIME , GTYPE_TIME);
conv_if(XY, GTYPE_XY);
}
enum tmt_en tmt_conv(
- char *string)
+ const char *string)
{
conv_if(SECOND, TMT_SECOND);
}
enum grc_en grc_conv(
- char *string)
+ const char *string)
{
conv_if(BACK, GRC_BACK);
}
enum text_prop_en text_prop_conv(
- char *string)
+ const char *string)
{
conv_if(DEFAULT, TEXT_PROP_DEFAULT);
{
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);
}
} /* 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);
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';
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);
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;
#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;
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;
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;
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,
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;
}
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;
}
}
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) {
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
}
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");
}
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");
}
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");
}
break;
case 'w':
- long_tmp = atol(optarg);
+ long_tmp = atol(poptions->optarg);
if (long_tmp < 10) {
rrd_set_error("width below 10 pixels");
return;
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;
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;
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;
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;
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;
}
}
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) {
}
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;
/* 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;
return;
}
- im->daemon_addr = strdup(optarg);
+ im->daemon_addr = strdup(poptions->optarg);
if (im->daemon_addr == NULL)
{
rrd_set_error("strdup failed");
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);
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(
void rrd_graph_options(
int,
char **,
+ struct optparse *,
image_desc_t *);
void rrd_graph_script(
int,
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;
flushfirst = 0;
break;
- default:
- rrd_set_error ("Usage: rrdtool %s [--daemon|-d <addr> [--noflush|-F]] <file>",
- 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 <addr> [--noflush|-F]] <file>",
- 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);
{
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.");
}
break;
- default:
- rrd_set_error ("Usage: rrdtool %s [--daemon|-d <addr>] <file>",
- 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 <addr>] <file>",
- 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);
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.");
}
break;
- default:
- rrd_set_error ("Usage: rrdtool %s [--daemon|-d <addr>] <file>",
- 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 <addr>] <file>",
- 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);
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;
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);
}
#endif
/* local include files -- need to be after the system ones */
-#ifdef HAVE_GETOPT_LONG
-#define _GNU_SOURCE
-#include <getopt.h>
-#else
-#include "rrd_getopt.h"
-#endif
-
+#include "optparse.h"
#include "rrd_format.h"
#ifndef max
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,
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.");
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 ????
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;
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;
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;
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;
}
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;
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;
}
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;
}
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;
}
}
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++)
}
}
- 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;
}
rrd_t *rrd,
enum cf_en cf,
enum rra_par_en rra_par,
- char *arg)
+ const char *arg)
{
double param;
unsigned long i;
rrd_t *rrd,
enum cf_en cf,
enum rra_par_en rra_par,
- char *arg)
+ const char *arg)
{
double param;
unsigned long i;
int set_deltaarg(
rrd_t *rrd,
enum rra_par_en rra_par,
- char *arg)
+ const char *arg)
{
rrd_value_t param;
unsigned long i;
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;
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;
}
}
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;
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':
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.");
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;
}
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 */
{
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",
return 0;
}
#endif
+
+/*
+ * vim: set sw=4 sts=4 ts=8 et fdm=marker :
+ */
#include "rrd_xport.h"
#include "unused.h"
#include "rrd_client.h"
-#include "optparse.h"
#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
#include <io.h>
}
case '?':
- rrd_set_error("%s: %s", argv[0], options.errmsg);
+ rrd_set_error("%s", options.errmsg);
return -1;
}
}
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;