diff mbox series

[iproute2-next,04/15] lib: Add parse_flag_on_off(), set_flag()

Message ID 56315f7e2213456852b021997b974eff5e45174d.1603154867.git.me@pmachata.org (mailing list archive)
State Not Applicable
Headers show
Series Add a tool for configuration of DCB | expand

Commit Message

Petr Machata Oct. 20, 2020, 12:58 a.m. UTC
Some iplink code makes a heavy use of code that sets or unsets a certain
flag depending on whether "on" or "off" of specified. Extract this logic
into a new function, parse_flag_on_off(). The bit that sets or clears a
flag will be useful separately, so add it to a named function, set_flag().

Signed-off-by: Petr Machata <me@pmachata.org>
---
 include/utils.h | 10 ++++++++++
 lib/utils.c     | 11 +++++++++++
 2 files changed, 21 insertions(+)
diff mbox series

Patch

diff --git a/include/utils.h b/include/utils.h
index bd62cdcd7122..681110fcf8af 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -325,8 +325,18 @@  char *sprint_time64(__s64 time, char *buf);
 int do_batch(const char *name, bool force,
 	     int (*cmd)(int argc, char *argv[], void *user), void *user);
 
+static inline void set_flag(unsigned int *p_flags, unsigned int flag, bool on)
+{
+	if (on)
+		*p_flags |= flag;
+	else
+		*p_flags &= ~flag;
+}
+
 int parse_one_of(const char *msg, const char *realval, const char * const *list,
 		 size_t len, int *p_err);
 int parse_on_off(const char *msg, const char *realval, int *p_err);
+void parse_flag_on_off(const char *msg, const char *realval,
+		       unsigned int *p_flags, unsigned int flag, int *p_ret);
 
 #endif /* __UTILS_H__ */
diff --git a/lib/utils.c b/lib/utils.c
index 930877ae0f0d..fb25c64d36ff 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1763,3 +1763,14 @@  int parse_on_off(const char *msg, const char *realval, int *p_err)
 
 	return parse_one_of(msg, realval, values_on_off, ARRAY_SIZE(values_on_off), p_err);
 }
+
+void parse_flag_on_off(const char *msg, const char *realval,
+		       unsigned int *p_flags, unsigned int flag, int *p_ret)
+{
+	int on_off = parse_on_off(msg, realval, p_ret);
+
+	if (*p_ret)
+		return;
+
+	set_flag(p_flags, flag, on_off);
+}