Thread (24 messages) 24 messages, 4 authors, 2020-10-22
STALE2071d
Revisions (4)
  1. v1 current
  2. v2 [diff vs current]
  3. v4 [diff vs current]
  4. v5 [diff vs current]

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

From: Petr Machata <hidden>
Date: 2020-10-20 00:58:55
Subsystem: library code, the rest · Maintainers: Andrew Morton, Linus Torvalds

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 <redacted>
---
 include/utils.h | 10 ++++++++++
 lib/utils.c     | 11 +++++++++++
 2 files changed, 21 insertions(+)
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);
+}
-- 
2.25.1
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help