naccyde / rpms / iproute

Forked from rpms/iproute 7 months ago
Clone

Blame SOURCES/0017-lib-introduce-print_nl.patch

930fb9
From 1934af7ac7bbd967f56d0cf5fa12ec0423bd5683 Mon Sep 17 00:00:00 2001
930fb9
From: Phil Sutter <psutter@redhat.com>
930fb9
Date: Wed, 19 Sep 2018 20:23:03 +0200
930fb9
Subject: [PATCH] lib: introduce print_nl
930fb9
930fb9
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1625500
930fb9
Upstream Status: iproute2.git commit b85076cd74e77
930fb9
930fb9
commit b85076cd74e77538918d35992b1a9cd17ff86af8
930fb9
Author: Stephen Hemminger <stephen@networkplumber.org>
930fb9
Date:   Tue Sep 11 08:29:33 2018 -0700
930fb9
930fb9
    lib: introduce print_nl
930fb9
930fb9
    Common pattern in iproute commands is to print a line seperator
930fb9
    in non-json mode. Make that a simple function.
930fb9
930fb9
    Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
930fb9
---
930fb9
 bridge/bridge.c      |  1 -
930fb9
 include/json_print.h |  2 ++
930fb9
 ip/ip.c              |  1 -
930fb9
 ip/ipaddress.c       | 12 ++++++------
930fb9
 ip/ipila.c           |  2 +-
930fb9
 ip/ipl2tp.c          | 14 +++++++-------
930fb9
 ip/ipmacsec.c        |  2 +-
930fb9
 ip/ipmaddr.c         |  2 +-
930fb9
 ip/ipmroute.c        |  2 +-
930fb9
 ip/ipntable.c        | 16 ++++++++--------
930fb9
 lib/json_print.c     |  7 +++++++
930fb9
 lib/utils.c          |  1 +
930fb9
 tc/tc.c              |  1 -
930fb9
 13 files changed, 35 insertions(+), 28 deletions(-)
930fb9
930fb9
diff --git a/bridge/bridge.c b/bridge/bridge.c
930fb9
index 28e1381..7221c0b 100644
930fb9
--- a/bridge/bridge.c
930fb9
+++ b/bridge/bridge.c
930fb9
@@ -30,7 +30,6 @@ int json;
930fb9
 int timestamp;
930fb9
 char *batch_file;
930fb9
 int force;
930fb9
-const char *_SL_;
930fb9
 
930fb9
 static void usage(void) __attribute__((noreturn));
930fb9
 
930fb9
diff --git a/include/json_print.h b/include/json_print.h
930fb9
index 218fedc..78a6c83 100644
930fb9
--- a/include/json_print.h
930fb9
+++ b/include/json_print.h
930fb9
@@ -41,6 +41,8 @@ void close_json_object(void);
930fb9
 void open_json_array(enum output_type type, const char *delim);
930fb9
 void close_json_array(enum output_type type, const char *delim);
930fb9
 
930fb9
+void print_nl(void);
930fb9
+
930fb9
 #define _PRINT_FUNC(type_name, type)					\
930fb9
 	void print_color_##type_name(enum output_type t,		\
930fb9
 				     enum color_attr color,		\
930fb9
diff --git a/ip/ip.c b/ip/ip.c
930fb9
index bb964f3..2ca55e3 100644
930fb9
--- a/ip/ip.c
930fb9
+++ b/ip/ip.c
930fb9
@@ -33,7 +33,6 @@ int oneline;
930fb9
 int brief;
930fb9
 int json;
930fb9
 int timestamp;
930fb9
-const char *_SL_;
930fb9
 int force;
930fb9
 int max_flush_loops = 10;
930fb9
 int batch_mode;
930fb9
diff --git a/ip/ipaddress.c b/ip/ipaddress.c
930fb9
index b7b78f6..f315a81 100644
930fb9
--- a/ip/ipaddress.c
930fb9
+++ b/ip/ipaddress.c
930fb9
@@ -240,7 +240,7 @@ static void print_linktype(FILE *fp, struct rtattr *tb)
930fb9
 		const char *kind
930fb9
 			= rta_getattr_str(linkinfo[IFLA_INFO_KIND]);
930fb9
 
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 		print_string(PRINT_ANY, "info_kind", "    %s ", kind);
930fb9
 
930fb9
 		lu = get_link_kind(kind);
930fb9
@@ -269,7 +269,7 @@ static void print_linktype(FILE *fp, struct rtattr *tb)
930fb9
 		const char *slave_kind
930fb9
 			= rta_getattr_str(linkinfo[IFLA_INFO_SLAVE_KIND]);
930fb9
 
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 		print_string(PRINT_ANY,
930fb9
 			     "info_slave_kind",
930fb9
 			     "    %s_slave ",
930fb9
@@ -749,7 +749,7 @@ static void print_link_stats(FILE *fp, struct nlmsghdr *n)
930fb9
 	parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi),
930fb9
 		     n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
930fb9
 	__print_link_stats(fp, tb);
930fb9
-	fprintf(fp, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 }
930fb9
 
930fb9
 static int print_linkinfo_brief(FILE *fp, const char *name,
930fb9
@@ -913,7 +913,7 @@ int print_linkinfo(const struct sockaddr_nl *who,
930fb9
 		print_link_event(fp, rta_getattr_u32(tb[IFLA_EVENT]));
930fb9
 
930fb9
 	if (!filter.family || filter.family == AF_PACKET || show_details) {
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 		print_string(PRINT_ANY,
930fb9
 			     "link_type",
930fb9
 			     "    link/%s ",
930fb9
@@ -1064,7 +1064,7 @@ int print_linkinfo(const struct sockaddr_nl *who,
930fb9
 		xdp_dump(fp, tb[IFLA_XDP], true, true);
930fb9
 
930fb9
 	if (do_link && show_stats) {
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 		__print_link_stats(fp, tb);
930fb9
 	}
930fb9
 
930fb9
@@ -1392,7 +1392,7 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
930fb9
 	if (rta_tb[IFA_CACHEINFO]) {
930fb9
 		struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
930fb9
 
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 		print_string(PRINT_FP, NULL, "       valid_lft ", NULL);
930fb9
 
930fb9
 		if (ci->ifa_valid == INFINITY_LIFE_TIME) {
930fb9
diff --git a/ip/ipila.c b/ip/ipila.c
930fb9
index 370385c..895fe0c 100644
930fb9
--- a/ip/ipila.c
930fb9
+++ b/ip/ipila.c
930fb9
@@ -128,7 +128,7 @@ static int print_ila_mapping(const struct sockaddr_nl *who,
930fb9
 	else
930fb9
 		print_string(PRINT_FP, NULL, "%s", "-");
930fb9
 
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 	close_json_object();
930fb9
 
930fb9
 	return 0;
930fb9
diff --git a/ip/ipl2tp.c b/ip/ipl2tp.c
930fb9
index 05e9638..414a9eb 100644
930fb9
--- a/ip/ipl2tp.c
930fb9
+++ b/ip/ipl2tp.c
930fb9
@@ -228,7 +228,7 @@ static void print_tunnel(const struct l2tp_data *data)
930fb9
 	print_string(PRINT_ANY, "encap", " encap %s",
930fb9
 		     p->encap == L2TP_ENCAPTYPE_UDP ? "UDP" :
930fb9
 		     p->encap == L2TP_ENCAPTYPE_IP ? "IP" : "??");
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 
930fb9
 	print_string(PRINT_ANY, "local", "  From %s ",
930fb9
 		     inet_ntop(p->local_ip.family, p->local_ip.data,
930fb9
@@ -236,11 +236,11 @@ static void print_tunnel(const struct l2tp_data *data)
930fb9
 	print_string(PRINT_ANY, "peer", "to %s",
930fb9
 		     inet_ntop(p->peer_ip.family, p->peer_ip.data,
930fb9
 			       buf, sizeof(buf)));
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 
930fb9
 	print_uint(PRINT_ANY, "peer_tunnel", "  Peer tunnel %u",
930fb9
 		   p->peer_tunnel_id);
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 
930fb9
 	if (p->encap == L2TP_ENCAPTYPE_UDP) {
930fb9
 		print_string(PRINT_FP, NULL,
930fb9
@@ -250,7 +250,7 @@ static void print_tunnel(const struct l2tp_data *data)
930fb9
 			   p->local_udp_port);
930fb9
 		print_uint(PRINT_ANY, "peer_port", "/%hu",
930fb9
 			   p->peer_udp_port);
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 
930fb9
 		switch (p->local_ip.family) {
930fb9
 		case AF_INET:
930fb9
@@ -292,18 +292,18 @@ static void print_session(struct l2tp_data *data)
930fb9
 
930fb9
 	print_uint(PRINT_ANY, "session_id", "Session %u", p->session_id);
930fb9
 	print_uint(PRINT_ANY, "tunnel_id",  " in tunnel %u", p->tunnel_id);
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 
930fb9
 	print_uint(PRINT_ANY, "peer_session_id",
930fb9
 		     "  Peer session %u,", p->peer_session_id);
930fb9
 	print_uint(PRINT_ANY, "peer_tunnel_id",
930fb9
 		     " tunnel %u",  p->peer_tunnel_id);
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 
930fb9
 	if (p->ifname != NULL) {
930fb9
 		print_color_string(PRINT_ANY, COLOR_IFNAME,
930fb9
 				   "interface", "  interface name: %s" , p->ifname);
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 	}
930fb9
 
930fb9
 	/* Show offsets only for plain console output (for legacy scripts) */
930fb9
diff --git a/ip/ipmacsec.c b/ip/ipmacsec.c
930fb9
index 4e4e158..fa56e0e 100644
930fb9
--- a/ip/ipmacsec.c
930fb9
+++ b/ip/ipmacsec.c
930fb9
@@ -627,7 +627,7 @@ static void print_attrs(struct rtattr *attrs[])
930fb9
 	if (attrs[MACSEC_SECY_ATTR_CIPHER_SUITE]) {
930fb9
 		__u64 cid = rta_getattr_u64(attrs[MACSEC_SECY_ATTR_CIPHER_SUITE]);
930fb9
 
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 		print_string(PRINT_ANY, "cipher_suite",
930fb9
 			     "    cipher suite: %s,", cs_id_to_name(cid));
930fb9
 	}
930fb9
diff --git a/ip/ipmaddr.c b/ip/ipmaddr.c
930fb9
index a484990..992b212 100644
930fb9
--- a/ip/ipmaddr.c
930fb9
+++ b/ip/ipmaddr.c
930fb9
@@ -240,7 +240,7 @@ static void print_mlist(FILE *fp, struct ma_info *list)
930fb9
 			print_uint(PRINT_ANY, "ifindex", "%d:", list->index);
930fb9
 			print_color_string(PRINT_ANY, COLOR_IFNAME,
930fb9
 					   "ifname", "\t%s", list->name);
930fb9
-			print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+			print_nl();
930fb9
 			cur_index = list->index;
930fb9
 
930fb9
 			open_json_array(PRINT_JSON, "maddr");
930fb9
diff --git a/ip/ipmroute.c b/ip/ipmroute.c
930fb9
index cdb4d89..bc23cfe 100644
930fb9
--- a/ip/ipmroute.c
930fb9
+++ b/ip/ipmroute.c
930fb9
@@ -181,7 +181,7 @@ int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
930fb9
 	if (show_stats && tb[RTA_MFC_STATS]) {
930fb9
 		struct rta_mfc_stats *mfcs = RTA_DATA(tb[RTA_MFC_STATS]);
930fb9
 
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 		print_u64(PRINT_ANY, "packets", "  %"PRIu64" packets,",
930fb9
 			   mfcs->mfcs_packets);
930fb9
 		print_u64(PRINT_ANY, "bytes", " %"PRIu64" bytes", mfcs->mfcs_bytes);
930fb9
diff --git a/ip/ipntable.c b/ip/ipntable.c
930fb9
index 4fae181..dd4f7c2 100644
930fb9
--- a/ip/ipntable.c
930fb9
+++ b/ip/ipntable.c
930fb9
@@ -346,7 +346,7 @@ static void print_ndtconfig(const struct ndt_config *ndtc)
930fb9
 		   "entry_size %u ", ndtc->ndtc_entry_size);
930fb9
 	print_uint(PRINT_ANY, "entries", "entries %u ", ndtc->ndtc_entries);
930fb9
 
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 
930fb9
 	print_string(PRINT_ANY, "last_flush",
930fb9
 		     "        last_flush %s ",
930fb9
@@ -355,7 +355,7 @@ static void print_ndtconfig(const struct ndt_config *ndtc)
930fb9
 		     "last_rand %s ",
930fb9
 		     ntable_strtime_delta(ndtc->ndtc_last_rand));
930fb9
 
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 
930fb9
 	print_uint(PRINT_ANY, "hash_rnd",
930fb9
 		   "        hash_rnd %u ", ndtc->ndtc_hash_rnd);
930fb9
@@ -367,7 +367,7 @@ static void print_ndtconfig(const struct ndt_config *ndtc)
930fb9
 	print_uint(PRINT_ANY, "proxy_qlen",
930fb9
 		   "proxy_qlen %u ", ndtc->ndtc_proxy_qlen);
930fb9
 
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 }
930fb9
 
930fb9
 static void print_ndtparams(struct rtattr *tpb[])
930fb9
@@ -379,7 +379,7 @@ static void print_ndtparams(struct rtattr *tpb[])
930fb9
 		print_string(PRINT_FP, NULL, "    dev ", NULL);
930fb9
 		print_color_string(PRINT_ANY, COLOR_IFNAME,
930fb9
 				   "dev", "%s ", ll_index_to_name(ifindex));
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 	}
930fb9
 
930fb9
 	print_string(PRINT_FP, NULL, "    ", NULL);
930fb9
@@ -482,7 +482,7 @@ static void print_ndtparams(struct rtattr *tpb[])
930fb9
 		print_u64(PRINT_ANY, "locktime", "locktime %llu ", locktime);
930fb9
 	}
930fb9
 
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 }
930fb9
 
930fb9
 static void print_ndtstats(const struct ndt_stats *ndts)
930fb9
@@ -517,7 +517,7 @@ static void print_ndtstats(const struct ndt_stats *ndts)
930fb9
 	print_u64(PRINT_ANY, "forced_gc_runs", "forced_gc_runs %llu ",
930fb9
 		   ndts->ndts_forced_gc_runs);
930fb9
 
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 }
930fb9
 
930fb9
 static int print_ntable(const struct sockaddr_nl *who,
930fb9
@@ -579,7 +579,7 @@ static int print_ntable(const struct sockaddr_nl *who,
930fb9
 		print_string(PRINT_ANY, "name", "%s ", name);
930fb9
 	}
930fb9
 
930fb9
-	print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+	print_nl();
930fb9
 
930fb9
 	ret = (tb[NDTA_THRESH1] || tb[NDTA_THRESH2] || tb[NDTA_THRESH3] ||
930fb9
 	       tb[NDTA_GC_INTERVAL]);
930fb9
@@ -611,7 +611,7 @@ static int print_ntable(const struct sockaddr_nl *who,
930fb9
 	}
930fb9
 
930fb9
 	if (ret)
930fb9
-		print_string(PRINT_FP, NULL, "%s", _SL_);
930fb9
+		print_nl();
930fb9
 
930fb9
 	if (tb[NDTA_CONFIG] && show_stats)
930fb9
 		print_ndtconfig(RTA_DATA(tb[NDTA_CONFIG]));
930fb9
diff --git a/lib/json_print.c b/lib/json_print.c
930fb9
index 5dc41bf..09e51d0 100644
930fb9
--- a/lib/json_print.c
930fb9
+++ b/lib/json_print.c
930fb9
@@ -221,3 +221,10 @@ void print_color_null(enum output_type type,
930fb9
 		color_fprintf(stdout, color, fmt, value);
930fb9
 	}
930fb9
 }
930fb9
+
930fb9
+/* Print line seperator (if not in JSON mode) */
930fb9
+void print_nl(void)
930fb9
+{
930fb9
+	if (!_jw)
930fb9
+		printf("%s", _SL_);
930fb9
+}
930fb9
diff --git a/lib/utils.c b/lib/utils.c
930fb9
index 02ce677..e87ecf3 100644
930fb9
--- a/lib/utils.c
930fb9
+++ b/lib/utils.c
930fb9
@@ -42,6 +42,7 @@
930fb9
 int resolve_hosts;
930fb9
 int timestamp_short;
930fb9
 int pretty;
930fb9
+const char *_SL_ = "\n";
930fb9
 
930fb9
 int read_prop(const char *dev, char *prop, long *value)
930fb9
 {
930fb9
diff --git a/tc/tc.c b/tc/tc.c
930fb9
index 3bb5910..2e97f2b 100644
930fb9
--- a/tc/tc.c
930fb9
+++ b/tc/tc.c
930fb9
@@ -43,7 +43,6 @@ bool use_names;
930fb9
 int json;
930fb9
 int color;
930fb9
 int oneline;
930fb9
-const char *_SL_;
930fb9
 
930fb9
 static char *conf_file;
930fb9
 
930fb9
-- 
930fb9
1.8.3.1
930fb9