From 5c75cdcab0b99171fc6c19240a18828937bd80ac Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Fri, 24 Jul 2020 04:12:42 -0600 Subject: [PATCH] libifupdown: inline the tokenization functions --- libifupdown/interface-file.c | 33 ++++++++---------------------- libifupdown/libifupdown.h | 1 + libifupdown/lifecycle.c | 21 ++----------------- libifupdown/tokenize.h | 39 ++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 libifupdown/tokenize.h diff --git a/libifupdown/interface-file.c b/libifupdown/interface-file.c index 9668048..af57250 100644 --- a/libifupdown/interface-file.c +++ b/libifupdown/interface-file.c @@ -18,24 +18,7 @@ #include #include "libifupdown/interface-file.h" #include "libifupdown/fgetline.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; -} +#include "libifupdown/tokenize.h" bool 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) { char *bufp = linebuf; - char *token = next_token(&bufp); + char *token = lif_next_token(&bufp); if (!*token || !isalpha(*token)) continue; if (!strcmp(token, "source")) { - char *source_filename = next_token(&bufp); + char *source_filename = lif_next_token(&bufp); if (!*source_filename) goto parse_error; @@ -73,7 +56,7 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) } else if (!strcmp(token, "auto")) { - char *ifname = next_token(&bufp); + char *ifname = lif_next_token(&bufp); if (!*ifname && cur_iface == NULL) goto parse_error; else @@ -87,7 +70,7 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) } else if (!strcmp(token, "iface")) { - char *ifname = next_token(&bufp); + char *ifname = lif_next_token(&bufp); if (!*ifname) 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 * those hints here. */ - char *inet_type = next_token(&bufp); + char *inet_type = lif_next_token(&bufp); if (!*inet_type) continue; - char *hint = next_token(&bufp); + char *hint = lif_next_token(&bufp); if (!*hint) continue; @@ -114,7 +97,7 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename) } else if (!strcmp(token, "address")) { - char *addr = next_token(&bufp); + char *addr = lif_next_token(&bufp); if (cur_iface == NULL) { diff --git a/libifupdown/libifupdown.h b/libifupdown/libifupdown.h index 4cc65e7..87cc5fd 100644 --- a/libifupdown/libifupdown.h +++ b/libifupdown/libifupdown.h @@ -26,5 +26,6 @@ #include "libifupdown/environment.h" #include "libifupdown/execute.h" #include "libifupdown/lifecycle.h" +#include "libifupdown/tokenize.h" #endif diff --git a/libifupdown/lifecycle.c b/libifupdown/lifecycle.c index 13ec84d..6afa53f 100644 --- a/libifupdown/lifecycle.c +++ b/libifupdown/lifecycle.c @@ -21,6 +21,7 @@ #include "libifupdown/interface.h" #include "libifupdown/lifecycle.h" #include "libifupdown/state.h" +#include "libifupdown/tokenize.h" 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) @@ -264,24 +265,6 @@ on_error: 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 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); 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); diff --git a/libifupdown/tokenize.h b/libifupdown/tokenize.h new file mode 100644 index 0000000..751c2f2 --- /dev/null +++ b/libifupdown/tokenize.h @@ -0,0 +1,39 @@ +/* + * libifupdown/tokenize.h + * Purpose: tokenization helper + * + * Copyright (c) 2020 Ariadne Conill + * + * 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 + +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