libifupdown: inline the tokenization functions

This commit is contained in:
Ariadne Conill 2020-07-24 04:12:42 -06:00
parent 8bc39aea7c
commit 5c75cdcab0
4 changed files with 50 additions and 44 deletions

View file

@ -18,24 +18,7 @@
#include <string.h> #include <string.h>
#include "libifupdown/interface-file.h" #include "libifupdown/interface-file.h"
#include "libifupdown/fgetline.h" #include "libifupdown/fgetline.h"
#include "libifupdown/tokenize.h"
static char *
next_token(char **buf)
{
char *out = *buf;
while (*out && isspace(*out))
out++;
char *end = out;
while (*end && !isspace(*end))
end++;
*end++ = '\0';
*buf = end;
return out;
}
bool bool
lif_interface_file_parse(struct lif_dict *collection, const char *filename) lif_interface_file_parse(struct lif_dict *collection, const char *filename)
@ -51,14 +34,14 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename)
while (lif_fgetline(linebuf, sizeof linebuf, f) != NULL) while (lif_fgetline(linebuf, sizeof linebuf, f) != NULL)
{ {
char *bufp = linebuf; char *bufp = linebuf;
char *token = next_token(&bufp); char *token = lif_next_token(&bufp);
if (!*token || !isalpha(*token)) if (!*token || !isalpha(*token))
continue; continue;
if (!strcmp(token, "source")) if (!strcmp(token, "source"))
{ {
char *source_filename = next_token(&bufp); char *source_filename = lif_next_token(&bufp);
if (!*source_filename) if (!*source_filename)
goto parse_error; goto parse_error;
@ -73,7 +56,7 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename)
} }
else if (!strcmp(token, "auto")) else if (!strcmp(token, "auto"))
{ {
char *ifname = next_token(&bufp); char *ifname = lif_next_token(&bufp);
if (!*ifname && cur_iface == NULL) if (!*ifname && cur_iface == NULL)
goto parse_error; goto parse_error;
else else
@ -87,7 +70,7 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename)
} }
else if (!strcmp(token, "iface")) else if (!strcmp(token, "iface"))
{ {
char *ifname = next_token(&bufp); char *ifname = lif_next_token(&bufp);
if (!*ifname) if (!*ifname)
goto parse_error; goto parse_error;
@ -99,11 +82,11 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename)
* or "inet dhcp" or such to designate hints. lets pick up * or "inet dhcp" or such to designate hints. lets pick up
* those hints here. * those hints here.
*/ */
char *inet_type = next_token(&bufp); char *inet_type = lif_next_token(&bufp);
if (!*inet_type) if (!*inet_type)
continue; continue;
char *hint = next_token(&bufp); char *hint = lif_next_token(&bufp);
if (!*hint) if (!*hint)
continue; continue;
@ -114,7 +97,7 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename)
} }
else if (!strcmp(token, "address")) else if (!strcmp(token, "address"))
{ {
char *addr = next_token(&bufp); char *addr = lif_next_token(&bufp);
if (cur_iface == NULL) if (cur_iface == NULL)
{ {

View file

@ -26,5 +26,6 @@
#include "libifupdown/environment.h" #include "libifupdown/environment.h"
#include "libifupdown/execute.h" #include "libifupdown/execute.h"
#include "libifupdown/lifecycle.h" #include "libifupdown/lifecycle.h"
#include "libifupdown/tokenize.h"
#endif #endif

View file

@ -21,6 +21,7 @@
#include "libifupdown/interface.h" #include "libifupdown/interface.h"
#include "libifupdown/lifecycle.h" #include "libifupdown/lifecycle.h"
#include "libifupdown/state.h" #include "libifupdown/state.h"
#include "libifupdown/tokenize.h"
static bool static bool
handle_commands_for_phase(const struct lif_execute_opts *opts, char *const envp[], struct lif_interface *iface, const char *lifname, const char *phase) handle_commands_for_phase(const struct lif_execute_opts *opts, char *const envp[], struct lif_interface *iface, const char *lifname, const char *phase)
@ -264,24 +265,6 @@ on_error:
return false; return false;
} }
static char *
next_token(char **buf)
{
char *out = *buf;
while (*out && isspace(*out))
out++;
char *end = out;
while (*end && !isspace(*end))
end++;
*end++ = '\0';
*buf = end;
return out;
}
static bool static bool
handle_dependents(const struct lif_execute_opts *opts, struct lif_interface *parent, struct lif_dict *collection, struct lif_dict *state, bool up) handle_dependents(const struct lif_execute_opts *opts, struct lif_interface *parent, struct lif_dict *collection, struct lif_dict *state, bool up)
{ {
@ -295,7 +278,7 @@ handle_dependents(const struct lif_execute_opts *opts, struct lif_interface *par
strlcpy(require_ifs, requires->data, sizeof require_ifs); strlcpy(require_ifs, requires->data, sizeof require_ifs);
char *bufp = require_ifs; char *bufp = require_ifs;
for (char *tokenp = next_token(&bufp); *tokenp; tokenp = next_token(&bufp)) for (char *tokenp = lif_next_token(&bufp); *tokenp; tokenp = lif_next_token(&bufp))
{ {
struct lif_interface *iface = lif_interface_collection_find(collection, tokenp); struct lif_interface *iface = lif_interface_collection_find(collection, tokenp);

39
libifupdown/tokenize.h Normal file
View file

@ -0,0 +1,39 @@
/*
* libifupdown/tokenize.h
* Purpose: tokenization helper
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#ifndef LIBIFUPDOWN_TOKENIZE_H__GUARD
#define LIBIFUPDOWN_TOKENIZE_H__GUARD
#include <ctype.h>
static inline char *
lif_next_token(char **buf)
{
char *out = *buf;
while (*out && isspace(*out))
out++;
char *end = out;
while (*end && !isspace(*end))
end++;
*end++ = '\0';
*buf = end;
return out;
}
#endif