output: add ethernet address priting support to --mustach

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Sami Kerola 2019-10-26 21:50:48 +01:00
parent b96c6c14d2
commit 544e7ec0a5
No known key found for this signature in database
GPG key ID: 0D46FEF7E61DBB46
4 changed files with 144 additions and 0 deletions

View file

@ -231,6 +231,7 @@ static void parse_command_line_opts(struct conf_t *state, int argc, char **argv)
#ifdef BUILD_MUSTACH
state->mustach_template = optarg;
state->output_format = 'm';
state->print_mac_addreses = 1;
#else
error(EXIT_FAILURE, 0, "compiled without mustach support");
#endif

View file

@ -58,6 +58,7 @@
*/
struct expl {
struct conf_t *state;
struct leases_t *lease_p;
struct range_t *range_p;
struct shared_network_t *shnet_p;
struct output_helper_t oh;
@ -178,6 +179,36 @@ static struct mustach_itf itf = {
.leave = must_leave
};
/*! \brief Mustach active lease aka {{#active_lease}} tag parser and printer. */
static int must_put_active_lease(void *closure, const char *name, int escape
__attribute__((unused)), FILE *file)
{
struct expl *e = closure;
if (!strcmp(name, "ip")) {
fprintf(file, "%s", ntop_ipaddr(&e->lease_p->ip));
return 0;
}
if (!strcmp(name, "macaddress")) {
fprintf(file, "%s", e->lease_p->ethernet);
return 0;
}
error(EXIT_FAILURE, 0, "mustach_dhcpd_pools: fmustach: unexpected tag: %s", name);
return 1;
}
/*! \brief A function to move to next lease when {{/active_lease}} is encountered. */
static int must_next_active_lease(void *closure)
{
struct expl *e = closure;
e->lease_p = e->lease_p->hh.next;
if (e->lease_p == NULL)
return 0;
return 1;
}
/*! \brief Mustach range aka {{#subnets}} tag parser and printer. */
static int must_put_range(void *closure, const char *name, int escape
__attribute__ ((unused)), FILE *file)
@ -359,6 +390,13 @@ static int must_enter(void *closure, const char *name)
{
struct expl *e = closure;
if (!strcmp(name, "active_lease")) {
itf.put = must_put_active_lease;
itf.next = must_next_active_lease;
e->current = 0;
e->lease_p = e->state->leases;
return must_next_active_lease(closure);
}
if (!strcmp(name, "subnets")) {
itf.put = must_put_range;
itf.next = must_next_range;