From 3f85360c644b79535360bf230638eabab3a2e685 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Sun, 20 May 2018 12:03:07 +0100 Subject: [PATCH] main: move output_format to state, and rename color_format Signed-off-by: Sami Kerola --- src/dhcpd-pools.c | 21 +++++++++------------ src/dhcpd-pools.h | 5 +++-- src/output.c | 18 +++++++++--------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/dhcpd-pools.c b/src/dhcpd-pools.c index 9e1ced7..dd52fe9 100644 --- a/src/dhcpd-pools.c +++ b/src/dhcpd-pools.c @@ -133,7 +133,7 @@ static void skip_arg_parse(struct conf_t *state, char *optarg) } /*! \brief Command line options parser. */ -static char parse_command_line_opts(struct conf_t *state, int argc, char **argv) +static void parse_command_line_opts(struct conf_t *state, int argc, char **argv) { enum { OPT_SNET_ALARMS = CHAR_MAX + 1, @@ -172,7 +172,6 @@ static char parse_command_line_opts(struct conf_t *state, int argc, char **argv) {"ip-version", required_argument, NULL, OPT_SET_IPV}, {NULL, 0, NULL, 0} }; - char output_format = '\0'; int alarming = 0; while (1) { @@ -192,7 +191,7 @@ static char parse_command_line_opts(struct conf_t *state, int argc, char **argv) break; case 'f': /* Output format */ - output_format = optarg[0]; + state->output_format = optarg[0]; break; case 's': { @@ -231,7 +230,7 @@ static char parse_command_line_opts(struct conf_t *state, int argc, char **argv) case OPT_MUSTACH: #ifdef BUILD_MUSTACH state->mustach_template = optarg; - output_format = 'm'; + state->output_format = 'm'; #else error(EXIT_FAILURE, 0, "compiled without mustach support"); #endif @@ -312,19 +311,18 @@ static char parse_command_line_opts(struct conf_t *state, int argc, char **argv) } /* Output format is not defined, if alarm thresholds are then it's alarming, else use the * default. */ - if (output_format == '\0') { + if (state->output_format == '\0') { if (alarming == 1) - output_format = 'a'; + state->output_format = 'a'; else { const char *const default_format = OUTPUT_FORMAT; - output_format = default_format[0]; + state->output_format = default_format[0]; } } - if (output_format == 'X' || output_format == 'J') { + if (state->output_format == 'X' || state->output_format == 'J') { state->print_mac_addreses = 1; } - return output_format; } /*!\brief Start of execution. This will mostly call other functions one @@ -347,14 +345,13 @@ int main(int argc, char **argv) .ip_version = IPvUNKNOWN, 0 }; - char output_format; int ret_val; atexit(close_stdout); set_program_name(argv[0]); prepare_memory(&state); set_ipv_functions(&state, IPvUNKNOWN); - output_format = parse_command_line_opts(&state, argc, argv); + parse_command_line_opts(&state, argc, argv); /* Do the job */ parse_config(&state, 1, state.dhcpdconf_file, state.shared_net_root); @@ -365,7 +362,7 @@ int main(int argc, char **argv) mergesort_ranges(&state, state.ranges, state.num_ranges, NULL, 1); if (state.reverse_order == 1) flip_ranges(&state); - ret_val = output_analysis(&state, output_format); + ret_val = output_analysis(&state); clean_up(&state); return (ret_val); } diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 1a77b1f..4556e06 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -214,9 +214,10 @@ struct conf_t { enum dhcp_version ip_version; /*!< Designator if the dhcpd is running in IPv4 or IPv6 mode. */ const char *dhcpdconf_file; /*!< Path to dhcpd.conf file. */ const char *dhcpdlease_file; /*!< Path to dhcpd.leases file. */ - int output_format; /*!< Column to use in color_tags array. */ + int color_format; /*!< Column to use in color_tags array. */ struct output_sort *sorts; /*!< Linked list how to sort ranges. */ const char *output_file; /*!< Output file path. */ + char output_format; /*!< Output format, such as text, json, xml, .... */ const char *mustach_template; /*!< Mustach template file path. */ double warning; /*!< Warning percent threshold. */ double critical; /*!< Critical percent threshold. */ @@ -314,7 +315,7 @@ extern int range_output_helper(struct conf_t *state, struct output_helper_t *oh, struct range_t *range_p); extern int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh, struct shared_network_t *shared_p); -extern int output_analysis(struct conf_t *state, const char output_format); +extern int output_analysis(struct conf_t *state); /* sort.c */ extern void mergesort_ranges(struct conf_t *state, diff --git a/src/output.c b/src/output.c index b723397..264a970 100644 --- a/src/output.c +++ b/src/output.c @@ -182,7 +182,7 @@ static int start_color(struct conf_t *state, struct output_helper_t *oh, FILE *o if (oh->status == STATUS_OK) { return 0; } - fputs(color_tags[oh->status][state->output_format], outfile); + fputs(color_tags[oh->status][state->color_format], outfile); return 1; } @@ -282,7 +282,7 @@ static int output_txt(struct conf_t *state) fprintf(outfile, "%7g %8.3f", range_p->backups, oh.bup); } if (color_set) - fputs(color_tags[COLOR_RESET][state->output_format], outfile); + fputs(color_tags[COLOR_RESET][state->color_format], outfile); fprintf(outfile, "\n"); range_p++; } @@ -320,7 +320,7 @@ static int output_txt(struct conf_t *state) fprintf(outfile, "%7g %8.3f", shared_p->backups, oh.bup); } if (color_set) - fputs(color_tags[COLOR_RESET][state->output_format], outfile); + fputs(color_tags[COLOR_RESET][state->color_format], outfile); fprintf(outfile, "\n"); } } @@ -356,7 +356,7 @@ static int output_txt(struct conf_t *state) fprintf(outfile, "%7g %8.3f", state->shared_net_root->backups, oh.bup); } if (color_set) - fputs(color_tags[COLOR_RESET][state->output_format], outfile); + fputs(color_tags[COLOR_RESET][state->color_format], outfile); fprintf(outfile, "\n"); } close_outfile(outfile); @@ -1180,13 +1180,13 @@ static int output_alarming(struct conf_t *state) } /*! \brief Return output_format_names enum based on single char input. */ -int output_analysis(struct conf_t *state, const char output_format) +int output_analysis(struct conf_t *state) { int ret = 1; - switch (output_format) { + switch (state->output_format) { case 't': - state->output_format = OUT_FORM_TEXT; + state->color_format = OUT_FORM_TEXT; ret = output_txt(state); break; case 'a': @@ -1196,7 +1196,7 @@ int output_analysis(struct conf_t *state, const char output_format) error(EXIT_FAILURE, 0, "html table only output format is deprecated"); break; case 'H': - state->output_format = OUT_FORM_HTML; + state->color_format = OUT_FORM_HTML; ret = output_html(state); break; case 'x': @@ -1218,7 +1218,7 @@ int output_analysis(struct conf_t *state, const char output_format) break; #endif default: - error(EXIT_FAILURE, 0, "unknown output format: '%c'", output_format); + error(EXIT_FAILURE, 0, "unknown output format: '%c'", state->output_format); } return ret; }