main: move output_format to state, and rename color_format

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2018-05-20 12:03:07 +01:00
parent e8e9d49ebb
commit 3f85360c64
No known key found for this signature in database
GPG key ID: A9553245FDE9B739
3 changed files with 21 additions and 23 deletions

View file

@ -133,7 +133,7 @@ static void skip_arg_parse(struct conf_t *state, char *optarg)
} }
/*! \brief Command line options parser. */ /*! \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 { enum {
OPT_SNET_ALARMS = CHAR_MAX + 1, 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}, {"ip-version", required_argument, NULL, OPT_SET_IPV},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
char output_format = '\0';
int alarming = 0; int alarming = 0;
while (1) { while (1) {
@ -192,7 +191,7 @@ static char parse_command_line_opts(struct conf_t *state, int argc, char **argv)
break; break;
case 'f': case 'f':
/* Output format */ /* Output format */
output_format = optarg[0]; state->output_format = optarg[0];
break; break;
case 's': case 's':
{ {
@ -231,7 +230,7 @@ static char parse_command_line_opts(struct conf_t *state, int argc, char **argv)
case OPT_MUSTACH: case OPT_MUSTACH:
#ifdef BUILD_MUSTACH #ifdef BUILD_MUSTACH
state->mustach_template = optarg; state->mustach_template = optarg;
output_format = 'm'; state->output_format = 'm';
#else #else
error(EXIT_FAILURE, 0, "compiled without mustach support"); error(EXIT_FAILURE, 0, "compiled without mustach support");
#endif #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 /* Output format is not defined, if alarm thresholds are then it's alarming, else use the
* default. */ * default. */
if (output_format == '\0') { if (state->output_format == '\0') {
if (alarming == 1) if (alarming == 1)
output_format = 'a'; state->output_format = 'a';
else { else {
const char *const default_format = OUTPUT_FORMAT; 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; state->print_mac_addreses = 1;
} }
return output_format;
} }
/*!\brief Start of execution. This will mostly call other functions one /*!\brief Start of execution. This will mostly call other functions one
@ -347,14 +345,13 @@ int main(int argc, char **argv)
.ip_version = IPvUNKNOWN, .ip_version = IPvUNKNOWN,
0 0
}; };
char output_format;
int ret_val; int ret_val;
atexit(close_stdout); atexit(close_stdout);
set_program_name(argv[0]); set_program_name(argv[0]);
prepare_memory(&state); prepare_memory(&state);
set_ipv_functions(&state, IPvUNKNOWN); set_ipv_functions(&state, IPvUNKNOWN);
output_format = parse_command_line_opts(&state, argc, argv); parse_command_line_opts(&state, argc, argv);
/* Do the job */ /* Do the job */
parse_config(&state, 1, state.dhcpdconf_file, state.shared_net_root); 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); mergesort_ranges(&state, state.ranges, state.num_ranges, NULL, 1);
if (state.reverse_order == 1) if (state.reverse_order == 1)
flip_ranges(&state); flip_ranges(&state);
ret_val = output_analysis(&state, output_format); ret_val = output_analysis(&state);
clean_up(&state); clean_up(&state);
return (ret_val); return (ret_val);
} }

View file

@ -214,9 +214,10 @@ struct conf_t {
enum dhcp_version ip_version; /*!< Designator if the dhcpd is running in IPv4 or IPv6 mode. */ 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 *dhcpdconf_file; /*!< Path to dhcpd.conf file. */
const char *dhcpdlease_file; /*!< Path to dhcpd.leases 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. */ struct output_sort *sorts; /*!< Linked list how to sort ranges. */
const char *output_file; /*!< Output file path. */ 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. */ const char *mustach_template; /*!< Mustach template file path. */
double warning; /*!< Warning percent threshold. */ double warning; /*!< Warning percent threshold. */
double critical; /*!< Critical 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); struct range_t *range_p);
extern int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh, extern int shnet_output_helper(struct conf_t *state, struct output_helper_t *oh,
struct shared_network_t *shared_p); 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 */ /* sort.c */
extern void mergesort_ranges(struct conf_t *state, extern void mergesort_ranges(struct conf_t *state,

View file

@ -182,7 +182,7 @@ static int start_color(struct conf_t *state, struct output_helper_t *oh, FILE *o
if (oh->status == STATUS_OK) { if (oh->status == STATUS_OK) {
return 0; return 0;
} }
fputs(color_tags[oh->status][state->output_format], outfile); fputs(color_tags[oh->status][state->color_format], outfile);
return 1; return 1;
} }
@ -282,7 +282,7 @@ static int output_txt(struct conf_t *state)
fprintf(outfile, "%7g %8.3f", range_p->backups, oh.bup); fprintf(outfile, "%7g %8.3f", range_p->backups, oh.bup);
} }
if (color_set) if (color_set)
fputs(color_tags[COLOR_RESET][state->output_format], outfile); fputs(color_tags[COLOR_RESET][state->color_format], outfile);
fprintf(outfile, "\n"); fprintf(outfile, "\n");
range_p++; range_p++;
} }
@ -320,7 +320,7 @@ static int output_txt(struct conf_t *state)
fprintf(outfile, "%7g %8.3f", shared_p->backups, oh.bup); fprintf(outfile, "%7g %8.3f", shared_p->backups, oh.bup);
} }
if (color_set) if (color_set)
fputs(color_tags[COLOR_RESET][state->output_format], outfile); fputs(color_tags[COLOR_RESET][state->color_format], outfile);
fprintf(outfile, "\n"); 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); fprintf(outfile, "%7g %8.3f", state->shared_net_root->backups, oh.bup);
} }
if (color_set) if (color_set)
fputs(color_tags[COLOR_RESET][state->output_format], outfile); fputs(color_tags[COLOR_RESET][state->color_format], outfile);
fprintf(outfile, "\n"); fprintf(outfile, "\n");
} }
close_outfile(outfile); 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. */ /*! \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; int ret = 1;
switch (output_format) { switch (state->output_format) {
case 't': case 't':
state->output_format = OUT_FORM_TEXT; state->color_format = OUT_FORM_TEXT;
ret = output_txt(state); ret = output_txt(state);
break; break;
case 'a': 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"); error(EXIT_FAILURE, 0, "html table only output format is deprecated");
break; break;
case 'H': case 'H':
state->output_format = OUT_FORM_HTML; state->color_format = OUT_FORM_HTML;
ret = output_html(state); ret = output_html(state);
break; break;
case 'x': case 'x':
@ -1218,7 +1218,7 @@ int output_analysis(struct conf_t *state, const char output_format)
break; break;
#endif #endif
default: default:
error(EXIT_FAILURE, 0, "unknown output format: '%c'", output_format); error(EXIT_FAILURE, 0, "unknown output format: '%c'", state->output_format);
} }
return ret; return ret;
} }