]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
lib: introduce print_nl
authorStephen Hemminger <stephen@networkplumber.org>
Tue, 11 Sep 2018 15:29:33 +0000 (08:29 -0700)
committerStephen Hemminger <stephen@networkplumber.org>
Tue, 11 Sep 2018 15:29:33 +0000 (08:29 -0700)
Common pattern in iproute commands is to print a line seperator
in non-json mode. Make that a simple function.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
13 files changed:
bridge/bridge.c
include/json_print.h
ip/ip.c
ip/ipaddress.c
ip/ipila.c
ip/ipl2tp.c
ip/ipmacsec.c
ip/ipmaddr.c
ip/ipmroute.c
ip/ipntable.c
lib/json_print.c
lib/utils.c
tc/tc.c

index 663a35b2b2e46cd186e96e641fd818699f329ba2..ac4d6a14f510ea7ebd71321c7c3c4bb6070417ac 100644 (file)
@@ -30,7 +30,6 @@ int json;
 int timestamp;
 char *batch_file;
 int force;
-const char *_SL_;
 
 static void usage(void) __attribute__((noreturn));
 
index 218fedc51b0d9d7ed8a8ad00a90f91aa3aad76d3..78a6c83fb516deb2426aed826aebc9d0a6b14dae 100644 (file)
@@ -41,6 +41,8 @@ void close_json_object(void);
 void open_json_array(enum output_type type, const char *delim);
 void close_json_array(enum output_type type, const char *delim);
 
+void print_nl(void);
+
 #define _PRINT_FUNC(type_name, type)                                   \
        void print_color_##type_name(enum output_type t,                \
                                     enum color_attr color,             \
diff --git a/ip/ip.c b/ip/ip.c
index 58c643df8a366a36c82aebde0564b79ffd3571e7..c324120f9fc59991ac82bb9420679c9bc972f993 100644 (file)
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -33,7 +33,6 @@ int oneline;
 int brief;
 int json;
 int timestamp;
-const char *_SL_;
 int force;
 int max_flush_loops = 10;
 int batch_mode;
index c94ddba841a0b528d1c2c1d991f617003fb51789..8dc6c32fd6d99a1bb57a37570bd08964f11a9012 100644 (file)
@@ -240,7 +240,7 @@ static void print_linktype(FILE *fp, struct rtattr *tb)
                const char *kind
                        = rta_getattr_str(linkinfo[IFLA_INFO_KIND]);
 
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
                print_string(PRINT_ANY, "info_kind", "    %s ", kind);
 
                lu = get_link_kind(kind);
@@ -269,7 +269,7 @@ static void print_linktype(FILE *fp, struct rtattr *tb)
                const char *slave_kind
                        = rta_getattr_str(linkinfo[IFLA_INFO_SLAVE_KIND]);
 
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
                print_string(PRINT_ANY,
                             "info_slave_kind",
                             "    %s_slave ",
@@ -765,7 +765,7 @@ static void print_link_stats(FILE *fp, struct nlmsghdr *n)
        parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi),
                     n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
        __print_link_stats(fp, tb);
-       fprintf(fp, "%s", _SL_);
+       print_nl();
 }
 
 static int print_linkinfo_brief(FILE *fp, const char *name,
@@ -929,7 +929,7 @@ int print_linkinfo(const struct sockaddr_nl *who,
                print_link_event(fp, rta_getattr_u32(tb[IFLA_EVENT]));
 
        if (!filter.family || filter.family == AF_PACKET || show_details) {
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
                print_string(PRINT_ANY,
                             "link_type",
                             "    link/%s ",
@@ -1090,7 +1090,7 @@ int print_linkinfo(const struct sockaddr_nl *who,
                xdp_dump(fp, tb[IFLA_XDP], true, true);
 
        if (do_link && show_stats) {
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
                __print_link_stats(fp, tb);
        }
 
@@ -1418,7 +1418,7 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
        if (rta_tb[IFA_CACHEINFO]) {
                struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
 
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
                print_string(PRINT_FP, NULL, "       valid_lft ", NULL);
 
                if (ci->ifa_valid == INFINITY_LIFE_TIME) {
index 370385c0c375ecc44ad58b1a80f19ed5c07f4a01..895fe0cdaf7792028eaec46ebcfcafec0cacb13d 100644 (file)
@@ -128,7 +128,7 @@ static int print_ila_mapping(const struct sockaddr_nl *who,
        else
                print_string(PRINT_FP, NULL, "%s", "-");
 
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
        close_json_object();
 
        return 0;
index 5e7f0390697e68545f3bbab2e8317c9b7351da64..16561eccd4582adf4ab871e603331ea1a474debd 100644 (file)
@@ -219,7 +219,7 @@ static void print_tunnel(const struct l2tp_data *data)
        print_string(PRINT_ANY, "encap", " encap %s",
                     p->encap == L2TP_ENCAPTYPE_UDP ? "UDP" :
                     p->encap == L2TP_ENCAPTYPE_IP ? "IP" : "??");
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 
        print_string(PRINT_ANY, "local", "  From %s ",
                     inet_ntop(p->local_ip.family, p->local_ip.data,
@@ -227,11 +227,11 @@ static void print_tunnel(const struct l2tp_data *data)
        print_string(PRINT_ANY, "peer", "to %s",
                     inet_ntop(p->peer_ip.family, p->peer_ip.data,
                               buf, sizeof(buf)));
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 
        print_uint(PRINT_ANY, "peer_tunnel", "  Peer tunnel %u",
                   p->peer_tunnel_id);
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 
        if (p->encap == L2TP_ENCAPTYPE_UDP) {
                print_string(PRINT_FP, NULL,
@@ -241,7 +241,7 @@ static void print_tunnel(const struct l2tp_data *data)
                           p->local_udp_port);
                print_uint(PRINT_ANY, "peer_port", "/%hu",
                           p->peer_udp_port);
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
 
                switch (p->local_ip.family) {
                case AF_INET:
@@ -283,18 +283,18 @@ static void print_session(struct l2tp_data *data)
 
        print_uint(PRINT_ANY, "session_id", "Session %u", p->session_id);
        print_uint(PRINT_ANY, "tunnel_id",  " in tunnel %u", p->tunnel_id);
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 
        print_uint(PRINT_ANY, "peer_session_id",
                     "  Peer session %u,", p->peer_session_id);
        print_uint(PRINT_ANY, "peer_tunnel_id",
                     " tunnel %u",  p->peer_tunnel_id);
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 
        if (p->ifname != NULL) {
                print_color_string(PRINT_ANY, COLOR_IFNAME,
                                   "interface", "  interface name: %s" , p->ifname);
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
        }
 
        /* Show offsets only for plain console output (for legacy scripts) */
index 4e4e158ea0f238a06f1c3fa335e8edddf013b9db..fa56e0eee774109d4058ae011cdc5de9e55ba630 100644 (file)
@@ -627,7 +627,7 @@ static void print_attrs(struct rtattr *attrs[])
        if (attrs[MACSEC_SECY_ATTR_CIPHER_SUITE]) {
                __u64 cid = rta_getattr_u64(attrs[MACSEC_SECY_ATTR_CIPHER_SUITE]);
 
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
                print_string(PRINT_ANY, "cipher_suite",
                             "    cipher suite: %s,", cs_id_to_name(cid));
        }
index abf83784d0dfbc808f21e9ae667ad48bb2994c32..105b23a8f075a871c31a129a0e44046bd921893a 100644 (file)
@@ -240,7 +240,7 @@ static void print_mlist(FILE *fp, struct ma_info *list)
                        print_uint(PRINT_ANY, "ifindex", "%d:", list->index);
                        print_color_string(PRINT_ANY, COLOR_IFNAME,
                                           "ifname", "\t%s", list->name);
-                       print_string(PRINT_FP, NULL, "%s", _SL_);
+                       print_nl();
                        cur_index = list->index;
 
                        open_json_array(PRINT_JSON, "maddr");
index cdb4d898c43668e215f25ff6e0daf636a8ead4c8..bc23cfea7e8b52b747a9594bb9efde0ab7ca0e9d 100644 (file)
@@ -181,7 +181,7 @@ int print_mroute(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
        if (show_stats && tb[RTA_MFC_STATS]) {
                struct rta_mfc_stats *mfcs = RTA_DATA(tb[RTA_MFC_STATS]);
 
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
                print_u64(PRINT_ANY, "packets", "  %"PRIu64" packets,",
                           mfcs->mfcs_packets);
                print_u64(PRINT_ANY, "bytes", " %"PRIu64" bytes", mfcs->mfcs_bytes);
index 4fae181dd3fa147eeac0d3de1e0ba91b0d2e1cc5..dd4f7c2ee6d91ac688b5d679cc61acb5ff2871a6 100644 (file)
@@ -346,7 +346,7 @@ static void print_ndtconfig(const struct ndt_config *ndtc)
                   "entry_size %u ", ndtc->ndtc_entry_size);
        print_uint(PRINT_ANY, "entries", "entries %u ", ndtc->ndtc_entries);
 
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 
        print_string(PRINT_ANY, "last_flush",
                     "        last_flush %s ",
@@ -355,7 +355,7 @@ static void print_ndtconfig(const struct ndt_config *ndtc)
                     "last_rand %s ",
                     ntable_strtime_delta(ndtc->ndtc_last_rand));
 
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 
        print_uint(PRINT_ANY, "hash_rnd",
                   "        hash_rnd %u ", ndtc->ndtc_hash_rnd);
@@ -367,7 +367,7 @@ static void print_ndtconfig(const struct ndt_config *ndtc)
        print_uint(PRINT_ANY, "proxy_qlen",
                   "proxy_qlen %u ", ndtc->ndtc_proxy_qlen);
 
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 }
 
 static void print_ndtparams(struct rtattr *tpb[])
@@ -379,7 +379,7 @@ static void print_ndtparams(struct rtattr *tpb[])
                print_string(PRINT_FP, NULL, "    dev ", NULL);
                print_color_string(PRINT_ANY, COLOR_IFNAME,
                                   "dev", "%s ", ll_index_to_name(ifindex));
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
        }
 
        print_string(PRINT_FP, NULL, "    ", NULL);
@@ -482,7 +482,7 @@ static void print_ndtparams(struct rtattr *tpb[])
                print_u64(PRINT_ANY, "locktime", "locktime %llu ", locktime);
        }
 
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 }
 
 static void print_ndtstats(const struct ndt_stats *ndts)
@@ -517,7 +517,7 @@ static void print_ndtstats(const struct ndt_stats *ndts)
        print_u64(PRINT_ANY, "forced_gc_runs", "forced_gc_runs %llu ",
                   ndts->ndts_forced_gc_runs);
 
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 }
 
 static int print_ntable(const struct sockaddr_nl *who,
@@ -579,7 +579,7 @@ static int print_ntable(const struct sockaddr_nl *who,
                print_string(PRINT_ANY, "name", "%s ", name);
        }
 
-       print_string(PRINT_FP, NULL, "%s", _SL_);
+       print_nl();
 
        ret = (tb[NDTA_THRESH1] || tb[NDTA_THRESH2] || tb[NDTA_THRESH3] ||
               tb[NDTA_GC_INTERVAL]);
@@ -611,7 +611,7 @@ static int print_ntable(const struct sockaddr_nl *who,
        }
 
        if (ret)
-               print_string(PRINT_FP, NULL, "%s", _SL_);
+               print_nl();
 
        if (tb[NDTA_CONFIG] && show_stats)
                print_ndtconfig(RTA_DATA(tb[NDTA_CONFIG]));
index 77902824a7386bf617bace5be8bec9cfd379f6da..eed109c56401c8db629dd5bb2893cbc74d4cc275 100644 (file)
@@ -222,3 +222,10 @@ void print_color_null(enum output_type type,
                color_fprintf(stdout, color, fmt, value);
        }
 }
+
+/* Print line seperator (if not in JSON mode) */
+void print_nl(void)
+{
+       if (!_jw)
+               printf("%s", _SL_);
+}
index 02ce67721915e14336685a5280c7133dda180773..e87ecf316b2db6f0d959e00cdc9728c1e29171b9 100644 (file)
@@ -42,6 +42,7 @@
 int resolve_hosts;
 int timestamp_short;
 int pretty;
+const char *_SL_ = "\n";
 
 int read_prop(const char *dev, char *prop, long *value)
 {
diff --git a/tc/tc.c b/tc/tc.c
index 4b28e9b182c1d7e4ddd5d6cd0e87acbb5a32e185..c493d5e92e0ddd9b68fa18ca80e585a83731af75 100644 (file)
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -43,7 +43,6 @@ bool use_names;
 int json;
 int color;
 int oneline;
-const char *_SL_;
 
 static char *conf_file;