mirror of
git://git.code.sf.net/p/dhcpd-pools/code
synced 2025-12-17 00:06:59 +00:00
simplify output format selection, and passing
Use of enum is a lot more readable than passing strings, and comparing characters, around. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
b524296016
commit
98bcdf9378
4 changed files with 28 additions and 12 deletions
|
|
@ -100,6 +100,7 @@ int main(int argc, char **argv)
|
|||
int i;
|
||||
int option_index = 0;
|
||||
char const *tmp;
|
||||
char *output_format_tmp;
|
||||
struct range_t *tmp_ranges;
|
||||
enum {
|
||||
OPT_SNET_ALARMS = CHAR_MAX + 1,
|
||||
|
|
@ -154,7 +155,7 @@ int main(int argc, char **argv)
|
|||
tmp++;
|
||||
config.number_limit = (*tmp - '0');
|
||||
/* Make sure some output format is selected by default */
|
||||
strncpy(config.output_format, OUTPUT_FORMAT, (size_t)1);
|
||||
output_format_tmp = OUTPUT_FORMAT;
|
||||
/* Default sort order is by IPs small to big */
|
||||
config.reverse_order = false;
|
||||
config.backups_found = false;
|
||||
|
|
@ -177,7 +178,7 @@ int main(int argc, char **argv)
|
|||
break;
|
||||
case 'f':
|
||||
/* Output format */
|
||||
strncpy(config.output_format, optarg, (size_t)1);
|
||||
output_format_tmp = optarg;
|
||||
break;
|
||||
case 's':
|
||||
{
|
||||
|
|
@ -215,19 +216,19 @@ int main(int argc, char **argv)
|
|||
config.snet_alarms = true;
|
||||
break;
|
||||
case OPT_WARN:
|
||||
strcpy(config.output_format, "a");
|
||||
output_format_tmp = "a";
|
||||
config.warning = strtod_or_err(optarg, "illegal argument");
|
||||
break;
|
||||
case OPT_CRIT:
|
||||
strcpy(config.output_format, "a");
|
||||
output_format_tmp = "a";
|
||||
config.critical = strtod_or_err(optarg, "illegal argument");
|
||||
break;
|
||||
case OPT_WARN_COUNT:
|
||||
strcpy(config.output_format, "a");
|
||||
output_format_tmp = "a";
|
||||
config.warn_count = strtod_or_err(optarg, "illegal argument");
|
||||
break;
|
||||
case OPT_CRIT_COUNT:
|
||||
strcpy(config.output_format, "a");
|
||||
output_format_tmp = "a";
|
||||
config.crit_count = strtod_or_err(optarg, "illegal argument");
|
||||
break;
|
||||
case OPT_MINSIZE:
|
||||
|
|
@ -245,37 +246,45 @@ int main(int argc, char **argv)
|
|||
}
|
||||
}
|
||||
/* Output function selection */
|
||||
switch (config.output_format[0]) {
|
||||
switch (output_format_tmp[0]) {
|
||||
case 't':
|
||||
output_analysis = output_txt;
|
||||
config.output_format = OUTPUT_SHORT;
|
||||
break;
|
||||
case 'a':
|
||||
output_analysis = output_alarming;
|
||||
config.output_format = OUTPUT_SHORT;
|
||||
break;
|
||||
case 'h':
|
||||
error(EXIT_FAILURE, 0, "html table only output format is deprecated");
|
||||
break;
|
||||
case 'H':
|
||||
output_analysis = output_html;
|
||||
config.output_format = OUTPUT_SHORT;
|
||||
break;
|
||||
case 'x':
|
||||
output_analysis = output_xml;
|
||||
config.output_format = OUTPUT_SHORT;
|
||||
break;
|
||||
case 'X':
|
||||
output_analysis = output_xml;
|
||||
config.output_format = OUTPUT_ETHERNETS;
|
||||
break;
|
||||
case 'j':
|
||||
output_analysis = output_json;
|
||||
config.output_format = OUTPUT_SHORT;
|
||||
break;
|
||||
case 'J':
|
||||
output_analysis = output_json;
|
||||
config.output_format = OUTPUT_ETHERNETS;
|
||||
break;
|
||||
case 'c':
|
||||
output_analysis = output_csv;
|
||||
config.output_format = OUTPUT_SHORT;
|
||||
break;
|
||||
default:
|
||||
clean_up();
|
||||
error(EXIT_FAILURE, 0, "main: unknown output format `%c'", config.output_format[0]);
|
||||
error(EXIT_FAILURE, 0, "main: unknown output format '%c'", output_format_tmp[0]);
|
||||
}
|
||||
/* Do the job */
|
||||
set_ipv_functions(VERSION_UNKNOWN);
|
||||
|
|
|
|||
|
|
@ -114,6 +114,13 @@ enum prefix_t {
|
|||
PREFIX_HARDWARE_ETHERNET,
|
||||
NUM_OF_PREFIX
|
||||
};
|
||||
/*! \enum dhcp_version
|
||||
* \brief Indicator which IP version is in use.
|
||||
*/
|
||||
enum output_formats {
|
||||
OUTPUT_SHORT,
|
||||
OUTPUT_ETHERNETS
|
||||
};
|
||||
/*! \struct shared_network_t
|
||||
* \brief Counters for an individual shared network.
|
||||
*/
|
||||
|
|
@ -195,7 +202,6 @@ struct configuration_t {
|
|||
enum dhcp_version dhcp_version;
|
||||
char *dhcpdconf_file;
|
||||
char *dhcpdlease_file;
|
||||
char output_format[2];
|
||||
struct output_sort *sorts;
|
||||
char *output_file;
|
||||
double warning;
|
||||
|
|
@ -207,6 +213,7 @@ struct configuration_t {
|
|||
reverse_order:1,
|
||||
backups_found:1,
|
||||
snet_alarms:1,
|
||||
output_format:1,
|
||||
header_limit:3,
|
||||
number_limit:3;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ int parse_leases(void)
|
|||
line[0] = '\0';
|
||||
ipstring = xmalloc(sizeof(char) * MAXLEN);
|
||||
ipstring[0] = '\0';
|
||||
if (config.output_format[0] == 'X' || config.output_format[0] == 'J')
|
||||
if (config.output_format == OUTPUT_ETHERNETS)
|
||||
ethernets = true;
|
||||
while (!feof(dhcpd_leases)) {
|
||||
if (!fgets(line, MAXLEN, dhcpd_leases) && ferror(dhcpd_leases))
|
||||
|
|
|
|||
|
|
@ -237,7 +237,7 @@ int output_xml(void)
|
|||
|
||||
fprintf(outfile, "<dhcpstatus>\n");
|
||||
|
||||
if (config.output_format[0] == 'X' || config.output_format[0] == 'J') {
|
||||
if (config.output_format == OUTPUT_ETHERNETS) {
|
||||
struct leases_t *l;
|
||||
|
||||
for (l = leases; l != NULL; l = l->hh.next) {
|
||||
|
|
@ -342,7 +342,7 @@ int output_json(void)
|
|||
|
||||
fprintf(outfile, "{\n");
|
||||
|
||||
if (config.output_format[0] == 'X' || config.output_format[0] == 'J') {
|
||||
if (config.output_format == OUTPUT_ETHERNETS) {
|
||||
struct leases_t *l;
|
||||
|
||||
fprintf(outfile, " \"active_leases\": [");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue