Patch Detail
get:
Show a patch.
patch:
Update a patch.
put:
Update a patch.
GET /api/patches/816752/?format=api
{ "id": 816752, "url": "http://patchwork.ozlabs.org/api/patches/816752/?format=api", "web_url": "http://patchwork.ozlabs.org/project/netdev/patch/635a857b7b32cd0df79805f8e32b20a2674a675d.1505956723.git.daniel@iogearbox.net/", "project": { "id": 7, "url": "http://patchwork.ozlabs.org/api/projects/7/?format=api", "name": "Linux network development", "link_name": "netdev", "list_id": "netdev.vger.kernel.org", "list_email": "netdev@vger.kernel.org", "web_url": null, "scm_url": null, "webscm_url": null, "list_archive_url": "", "list_archive_url_format": "", "commit_url_format": "" }, "msgid": "<635a857b7b32cd0df79805f8e32b20a2674a675d.1505956723.git.daniel@iogearbox.net>", "list_archive_url": null, "date": "2017-09-21T08:42:28", "name": "[iproute2,master,1/2] json: move json printer to common library", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": true, "hash": "b4921246205dacac93a8a373c57f5e8dcca57cc8", "submitter": { "id": 65705, "url": "http://patchwork.ozlabs.org/api/people/65705/?format=api", "name": "Daniel Borkmann", "email": "daniel@iogearbox.net" }, "delegate": { "id": 389, "url": "http://patchwork.ozlabs.org/api/users/389/?format=api", "username": "shemminger", "first_name": "stephen", "last_name": "hemminger", "email": "shemminger@vyatta.com" }, "mbox": "http://patchwork.ozlabs.org/project/netdev/patch/635a857b7b32cd0df79805f8e32b20a2674a675d.1505956723.git.daniel@iogearbox.net/mbox/", "series": [ { "id": 4331, "url": "http://patchwork.ozlabs.org/api/series/4331/?format=api", "web_url": "http://patchwork.ozlabs.org/project/netdev/list/?series=4331", "date": "2017-09-21T08:42:27", "name": "BPF/XDP json follow-up", "version": 1, "mbox": "http://patchwork.ozlabs.org/series/4331/mbox/" } ], "comments": "http://patchwork.ozlabs.org/api/patches/816752/comments/", "check": "pending", "checks": "http://patchwork.ozlabs.org/api/patches/816752/checks/", "tags": {}, "related": [], "headers": { "Return-Path": "<netdev-owner@vger.kernel.org>", "X-Original-To": "patchwork-incoming@ozlabs.org", "Delivered-To": "patchwork-incoming@ozlabs.org", "Authentication-Results": "ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netdev-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)", "Received": [ "from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xyVVg6yRqz9t3v\n\tfor <patchwork-incoming@ozlabs.org>;\n\tThu, 21 Sep 2017 18:42:51 +1000 (AEST)", "(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1752047AbdIUImu (ORCPT <rfc822;patchwork-incoming@ozlabs.org>);\n\tThu, 21 Sep 2017 04:42:50 -0400", "from www62.your-server.de ([213.133.104.62]:40221 \"EHLO\n\twww62.your-server.de\" rhost-flags-OK-OK-OK-OK) by vger.kernel.org\n\twith ESMTP id S1751847AbdIUImk (ORCPT\n\t<rfc822;netdev@vger.kernel.org>); Thu, 21 Sep 2017 04:42:40 -0400", "from [85.7.161.218] (helo=localhost)\n\tby www62.your-server.de with esmtpsa\n\t(TLSv1.2:DHE-RSA-AES128-GCM-SHA256:128) (Exim 4.85_2)\n\t(envelope-from <daniel@iogearbox.net>)\n\tid 1dux4A-0003ZG-Ey; Thu, 21 Sep 2017 10:42:38 +0200" ], "From": "Daniel Borkmann <daniel@iogearbox.net>", "To": "stephen@networkplumber.org", "Cc": "ast@fb.com, netdev@vger.kernel.org,\n\tDaniel Borkmann <daniel@iogearbox.net>", "Subject": "[PATCH iproute2 master 1/2] json: move json printer to common\n\tlibrary", "Date": "Thu, 21 Sep 2017 10:42:28 +0200", "Message-Id": "<635a857b7b32cd0df79805f8e32b20a2674a675d.1505956723.git.daniel@iogearbox.net>", "X-Mailer": "git-send-email 1.9.3", "In-Reply-To": [ "<cover.1505956723.git.daniel@iogearbox.net>", "<cover.1505956723.git.daniel@iogearbox.net>" ], "References": [ "<cover.1505956723.git.daniel@iogearbox.net>", "<cover.1505956723.git.daniel@iogearbox.net>" ], "X-Authenticated-Sender": "daniel@iogearbox.net", "X-Virus-Scanned": "Clear (ClamAV 0.99.2/23858/Thu Sep 21 02:46:36 2017)", "Sender": "netdev-owner@vger.kernel.org", "Precedence": "bulk", "List-ID": "<netdev.vger.kernel.org>", "X-Mailing-List": "netdev@vger.kernel.org" }, "content": "Move the json printer which is based on json writer into the\niproute2 library, so it can be used by library code and tools\nother than ip. Should probably have been done from the beginning\nlike that given json writer is in the library already anyway.\nNo functional changes.\n\nSigned-off-by: Daniel Borkmann <daniel@iogearbox.net>\n---\n include/json_print.h | 71 ++++++++++++++++\n ip/Makefile | 2 +-\n ip/ip_common.h | 65 ++------------\n ip/ip_print.c | 233 ---------------------------------------------------\n lib/Makefile | 2 +-\n lib/json_print.c | 231 ++++++++++++++++++++++++++++++++++++++++++++++++++\n 6 files changed, 312 insertions(+), 292 deletions(-)\n create mode 100644 include/json_print.h\n delete mode 100644 ip/ip_print.c\n create mode 100644 lib/json_print.c", "diff": "diff --git a/include/json_print.h b/include/json_print.h\nnew file mode 100644\nindex 0000000..44cf5ac\n--- /dev/null\n+++ b/include/json_print.h\n@@ -0,0 +1,71 @@\n+/*\n+ * json_print.h\t\t\"print regular or json output, based on json_writer\".\n+ *\n+ * This program is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU General Public License\n+ * as published by the Free Software Foundation; either version\n+ * 2 of the License, or (at your option) any later version.\n+ *\n+ * Authors: Julien Fortin, <julien@cumulusnetworks.com>\n+ */\n+\n+#ifndef _JSON_PRINT_H_\n+#define _JSON_PRINT_H_\n+\n+#include \"json_writer.h\"\n+#include \"color.h\"\n+\n+json_writer_t *get_json_writer(void);\n+\n+/*\n+ * use:\n+ * - PRINT_ANY for context based output\n+ * - PRINT_FP for non json specific output\n+ * - PRINT_JSON for json specific output\n+ */\n+enum output_type {\n+\tPRINT_FP = 1,\n+\tPRINT_JSON = 2,\n+\tPRINT_ANY = 4,\n+};\n+\n+void new_json_obj(int json, FILE *fp);\n+void delete_json_obj(void);\n+\n+bool is_json_context(void);\n+\n+void set_current_fp(FILE *fp);\n+\n+void fflush_fp(void);\n+\n+void open_json_object(const char *str);\n+void close_json_object(void);\n+void open_json_array(enum output_type type, const char *delim);\n+void close_json_array(enum output_type type, const char *delim);\n+\n+#define _PRINT_FUNC(type_name, type)\t\t\t\t\t\\\n+\tvoid print_color_##type_name(enum output_type t,\t\t\\\n+\t\t\t\t enum color_attr color,\t\t\\\n+\t\t\t\t const char *key,\t\t\t\\\n+\t\t\t\t const char *fmt,\t\t\t\\\n+\t\t\t\t type value);\t\t\t\\\n+\t\t\t\t\t\t\t\t\t\\\n+\tstatic inline void print_##type_name(enum output_type t,\t\\\n+\t\t\t\t\t const char *key,\t\t\\\n+\t\t\t\t\t const char *fmt,\t\t\\\n+\t\t\t\t\t type value)\t\t\\\n+\t{\t\t\t\t\t\t\t\t\\\n+\t\tprint_color_##type_name(t, -1, key, fmt, value);\t\\\n+\t}\n+_PRINT_FUNC(int, int);\n+_PRINT_FUNC(bool, bool);\n+_PRINT_FUNC(null, const char*);\n+_PRINT_FUNC(string, const char*);\n+_PRINT_FUNC(uint, uint64_t);\n+_PRINT_FUNC(hu, unsigned short);\n+_PRINT_FUNC(hex, unsigned int);\n+_PRINT_FUNC(0xhex, unsigned int);\n+_PRINT_FUNC(lluint, unsigned long long int);\n+#undef _PRINT_FUNC\n+\n+#endif /* _JSON_PRINT_H_ */\ndiff --git a/ip/Makefile b/ip/Makefile\nindex 52c9a2e..5a1c7ad 100644\n--- a/ip/Makefile\n+++ b/ip/Makefile\n@@ -9,7 +9,7 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \\\n link_iptnl.o link_gre6.o iplink_bond.o iplink_bond_slave.o iplink_hsr.o \\\n iplink_bridge.o iplink_bridge_slave.o ipfou.o iplink_ipvlan.o \\\n iplink_geneve.o iplink_vrf.o iproute_lwtunnel.o ipmacsec.o ipila.o \\\n- ipvrf.o iplink_xstats.o ipseg6.o ip_print.o\n+ ipvrf.o iplink_xstats.o ipseg6.o\n \n RTMONOBJ=rtmon.o\n \ndiff --git a/ip/ip_common.h b/ip/ip_common.h\nindex efc789c..4b8b0a7 100644\n--- a/ip/ip_common.h\n+++ b/ip/ip_common.h\n@@ -1,3 +1,10 @@\n+#ifndef _IP_COMMON_H_\n+#define _IP_COMMON_H_\n+\n+#include <stdbool.h>\n+\n+#include \"json_print.h\"\n+\n struct link_filter {\n \tint ifindex;\n \tint family;\n@@ -101,8 +108,6 @@ static inline int rtm_get_table(struct rtmsg *r, struct rtattr **tb)\n \n extern struct rtnl_handle rth;\n \n-#include <stdbool.h>\n-\n struct link_util {\n \tstruct link_util\t*next;\n \tconst char\t\t*id;\n@@ -141,58 +146,4 @@ int name_is_vrf(const char *name);\n \n void print_num(FILE *fp, unsigned int width, uint64_t count);\n \n-#include \"json_writer.h\"\n-\n-json_writer_t *get_json_writer(void);\n-/*\n- * use:\n- * - PRINT_ANY for context based output\n- * - PRINT_FP for non json specific output\n- * - PRINT_JSON for json specific output\n- */\n-enum output_type {\n-\tPRINT_FP = 1,\n-\tPRINT_JSON = 2,\n-\tPRINT_ANY = 4,\n-};\n-\n-void new_json_obj(int json, FILE *fp);\n-void delete_json_obj(void);\n-\n-bool is_json_context(void);\n-\n-void set_current_fp(FILE *fp);\n-\n-void fflush_fp(void);\n-\n-void open_json_object(const char *str);\n-void close_json_object(void);\n-void open_json_array(enum output_type type, const char *delim);\n-void close_json_array(enum output_type type, const char *delim);\n-\n-#include \"color.h\"\n-\n-#define _PRINT_FUNC(type_name, type)\t\t\t\t\t\\\n-\tvoid print_color_##type_name(enum output_type t,\t\t\\\n-\t\t\t\t enum color_attr color,\t\t\\\n-\t\t\t\t const char *key,\t\t\t\\\n-\t\t\t\t const char *fmt,\t\t\t\\\n-\t\t\t\t type value);\t\t\t\\\n-\t\t\t\t\t\t\t\t\t\\\n-\tstatic inline void print_##type_name(enum output_type t,\t\\\n-\t\t\t\t\t const char *key,\t\t\\\n-\t\t\t\t\t const char *fmt,\t\t\\\n-\t\t\t\t\t type value)\t\t\\\n-\t{\t\t\t\t\t\t\t\t\\\n-\t\tprint_color_##type_name(t, -1, key, fmt, value);\t\\\n-\t}\n-_PRINT_FUNC(int, int);\n-_PRINT_FUNC(bool, bool);\n-_PRINT_FUNC(null, const char*);\n-_PRINT_FUNC(string, const char*);\n-_PRINT_FUNC(uint, uint64_t);\n-_PRINT_FUNC(hu, unsigned short);\n-_PRINT_FUNC(hex, unsigned int);\n-_PRINT_FUNC(0xhex, unsigned int);\n-_PRINT_FUNC(lluint, unsigned long long int);\n-#undef _PRINT_FUNC\n+#endif /* _IP_COMMON_H_ */\ndiff --git a/ip/ip_print.c b/ip/ip_print.c\ndeleted file mode 100644\nindex 4cd6a0b..0000000\n--- a/ip/ip_print.c\n+++ /dev/null\n@@ -1,233 +0,0 @@\n-/*\n- * ip_print.c \"ip print regular or json output\".\n- *\n- * This program is free software; you can redistribute it and/or\n- * modify it under the terms of the GNU General Public License\n- * as published by the Free Software Foundation; either version\n- * 2 of the License, or (at your option) any later version.\n- *\n- * Authors: Julien Fortin, <julien@cumulusnetworks.com>\n- *\n- */\n-\n-#include <stdarg.h>\n-#include <stdio.h>\n-\n-#include \"utils.h\"\n-#include \"ip_common.h\"\n-#include \"json_writer.h\"\n-\n-static json_writer_t *_jw;\n-static FILE *_fp;\n-\n-#define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw)\n-#define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY))\n-\n-void new_json_obj(int json, FILE *fp)\n-{\n-\tif (json) {\n-\t\t_jw = jsonw_new(fp);\n-\t\tif (!_jw) {\n-\t\t\tperror(\"json object\");\n-\t\t\texit(1);\n-\t\t}\n-\t\tjsonw_pretty(_jw, true);\n-\t\tjsonw_start_array(_jw);\n-\t}\n-\tset_current_fp(fp);\n-}\n-\n-void delete_json_obj(void)\n-{\n-\tif (_jw) {\n-\t\tjsonw_end_array(_jw);\n-\t\tjsonw_destroy(&_jw);\n-\t}\n-}\n-\n-bool is_json_context(void)\n-{\n-\treturn _jw != NULL;\n-}\n-\n-void set_current_fp(FILE *fp)\n-{\n-\tif (!fp) {\n-\t\tfprintf(stderr, \"Error: invalid file pointer.\\n\");\n-\t\texit(1);\n-\t}\n-\t_fp = fp;\n-}\n-\n-json_writer_t *get_json_writer(void)\n-{\n-\treturn _jw;\n-}\n-\n-void open_json_object(const char *str)\n-{\n-\tif (_IS_JSON_CONTEXT(PRINT_JSON)) {\n-\t\tif (str)\n-\t\t\tjsonw_name(_jw, str);\n-\t\tjsonw_start_object(_jw);\n-\t}\n-}\n-\n-void close_json_object(void)\n-{\n-\tif (_IS_JSON_CONTEXT(PRINT_JSON))\n-\t\tjsonw_end_object(_jw);\n-}\n-\n-/*\n- * Start json array or string array using\n- * the provided string as json key (if not null)\n- * or as array delimiter in non-json context.\n- */\n-void open_json_array(enum output_type type, const char *str)\n-{\n-\tif (_IS_JSON_CONTEXT(type)) {\n-\t\tif (str)\n-\t\t\tjsonw_name(_jw, str);\n-\t\tjsonw_start_array(_jw);\n-\t} else if (_IS_FP_CONTEXT(type)) {\n-\t\tfprintf(_fp, \"%s\", str);\n-\t}\n-}\n-\n-/*\n- * End json array or string array\n- */\n-void close_json_array(enum output_type type, const char *str)\n-{\n-\tif (_IS_JSON_CONTEXT(type)) {\n-\t\tjsonw_pretty(_jw, false);\n-\t\tjsonw_end_array(_jw);\n-\t\tjsonw_pretty(_jw, true);\n-\t} else if (_IS_FP_CONTEXT(type)) {\n-\t\tfprintf(_fp, \"%s\", str);\n-\t}\n-}\n-\n-/*\n- * pre-processor directive to generate similar\n- * functions handling different types\n- */\n-#define _PRINT_FUNC(type_name, type)\t\t\t\t\t\\\n-\tvoid print_color_##type_name(enum output_type t,\t\t\\\n-\t\t\t\t enum color_attr color,\t\t\\\n-\t\t\t\t const char *key,\t\t\t\\\n-\t\t\t\t const char *fmt,\t\t\t\\\n-\t\t\t\t type value)\t\t\t\\\n-\t{\t\t\t\t\t\t\t\t\\\n-\t\tif (_IS_JSON_CONTEXT(t)) {\t\t\t\t\\\n-\t\t\tif (!key)\t\t\t\t\t\\\n-\t\t\t\tjsonw_##type_name(_jw, value);\t\t\\\n-\t\t\telse\t\t\t\t\t\t\\\n-\t\t\t\tjsonw_##type_name##_field(_jw, key, value); \\\n-\t\t} else if (_IS_FP_CONTEXT(t)) {\t\t\t\t\\\n-\t\t\tcolor_fprintf(_fp, color, fmt, value); \\\n-\t\t}\t\t\t\t\t\t\t\\\n-\t}\n-_PRINT_FUNC(int, int);\n-_PRINT_FUNC(hu, unsigned short);\n-_PRINT_FUNC(uint, uint64_t);\n-_PRINT_FUNC(lluint, unsigned long long int);\n-#undef _PRINT_FUNC\n-\n-void print_color_string(enum output_type type,\n-\t\t\tenum color_attr color,\n-\t\t\tconst char *key,\n-\t\t\tconst char *fmt,\n-\t\t\tconst char *value)\n-{\n-\tif (_IS_JSON_CONTEXT(type)) {\n-\t\tif (key && !value)\n-\t\t\tjsonw_name(_jw, key);\n-\t\telse if (!key && value)\n-\t\t\tjsonw_string(_jw, value);\n-\t\telse\n-\t\t\tjsonw_string_field(_jw, key, value);\n-\t} else if (_IS_FP_CONTEXT(type)) {\n-\t\tcolor_fprintf(_fp, color, fmt, value);\n-\t}\n-}\n-\n-/*\n- * value's type is bool. When using this function in FP context you can't pass\n- * a value to it, you will need to use \"is_json_context()\" to have different\n- * branch for json and regular output. grep -r \"print_bool\" for example\n- */\n-void print_color_bool(enum output_type type,\n-\t\t enum color_attr color,\n-\t\t const char *key,\n-\t\t const char *fmt,\n-\t\t bool value)\n-{\n-\tif (_IS_JSON_CONTEXT(type)) {\n-\t\tif (key)\n-\t\t\tjsonw_bool_field(_jw, key, value);\n-\t\telse\n-\t\t\tjsonw_bool(_jw, value);\n-\t} else if (_IS_FP_CONTEXT(type)) {\n-\t\tcolor_fprintf(_fp, color, fmt, value ? \"true\" : \"false\");\n-\t}\n-}\n-\n-/*\n- * In JSON context uses hardcode %#x format: 42 -> 0x2a\n- */\n-void print_color_0xhex(enum output_type type,\n-\t\t enum color_attr color,\n-\t\t const char *key,\n-\t\t const char *fmt,\n-\t\t unsigned int hex)\n-{\n-\tif (_IS_JSON_CONTEXT(type)) {\n-\t\tSPRINT_BUF(b1);\n-\n-\t\tsnprintf(b1, sizeof(b1), \"%#x\", hex);\n-\t\tprint_string(PRINT_JSON, key, NULL, b1);\n-\t} else if (_IS_FP_CONTEXT(type)) {\n-\t\tcolor_fprintf(_fp, color, fmt, hex);\n-\t}\n-}\n-\n-void print_color_hex(enum output_type type,\n-\t\t enum color_attr color,\n-\t\t const char *key,\n-\t\t const char *fmt,\n-\t\t unsigned int hex)\n-{\n-\tif (_IS_JSON_CONTEXT(type)) {\n-\t\tSPRINT_BUF(b1);\n-\n-\t\tsnprintf(b1, sizeof(b1), \"%x\", hex);\n-\t\tif (key)\n-\t\t\tjsonw_string_field(_jw, key, b1);\n-\t\telse\n-\t\t\tjsonw_string(_jw, b1);\n-\t} else if (_IS_FP_CONTEXT(type)) {\n-\t\tcolor_fprintf(_fp, color, fmt, hex);\n-\t}\n-}\n-\n-/*\n- * In JSON context we don't use the argument \"value\" we simply call jsonw_null\n- * whereas FP context can use \"value\" to output anything\n- */\n-void print_color_null(enum output_type type,\n-\t\t enum color_attr color,\n-\t\t const char *key,\n-\t\t const char *fmt,\n-\t\t const char *value)\n-{\n-\tif (_IS_JSON_CONTEXT(type)) {\n-\t\tif (key)\n-\t\t\tjsonw_null_field(_jw, key);\n-\t\telse\n-\t\t\tjsonw_null(_jw);\n-\t} else if (_IS_FP_CONTEXT(type)) {\n-\t\tcolor_fprintf(_fp, color, fmt, value);\n-\t}\n-}\ndiff --git a/lib/Makefile b/lib/Makefile\nindex 5e9f72f..0fbdf4c 100644\n--- a/lib/Makefile\n+++ b/lib/Makefile\n@@ -3,7 +3,7 @@ include ../config.mk\n CFLAGS += -fPIC\n \n UTILOBJ = utils.o rt_names.o ll_types.o ll_proto.o ll_addr.o \\\n-\tinet_proto.o namespace.o json_writer.o \\\n+\tinet_proto.o namespace.o json_writer.o json_print.o \\\n \tnames.o color.o bpf.o exec.o fs.o\n \n NLOBJ=libgenl.o ll_map.o libnetlink.o\ndiff --git a/lib/json_print.c b/lib/json_print.c\nnew file mode 100644\nindex 0000000..93b4119\n--- /dev/null\n+++ b/lib/json_print.c\n@@ -0,0 +1,231 @@\n+/*\n+ * json_print.c\t\t\"print regular or json output, based on json_writer\".\n+ *\n+ * This program is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU General Public License\n+ * as published by the Free Software Foundation; either version\n+ * 2 of the License, or (at your option) any later version.\n+ *\n+ * Authors: Julien Fortin, <julien@cumulusnetworks.com>\n+ */\n+\n+#include <stdarg.h>\n+#include <stdio.h>\n+\n+#include \"utils.h\"\n+#include \"json_print.h\"\n+\n+static json_writer_t *_jw;\n+static FILE *_fp;\n+\n+#define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw)\n+#define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY))\n+\n+void new_json_obj(int json, FILE *fp)\n+{\n+\tif (json) {\n+\t\t_jw = jsonw_new(fp);\n+\t\tif (!_jw) {\n+\t\t\tperror(\"json object\");\n+\t\t\texit(1);\n+\t\t}\n+\t\tjsonw_pretty(_jw, true);\n+\t\tjsonw_start_array(_jw);\n+\t}\n+\tset_current_fp(fp);\n+}\n+\n+void delete_json_obj(void)\n+{\n+\tif (_jw) {\n+\t\tjsonw_end_array(_jw);\n+\t\tjsonw_destroy(&_jw);\n+\t}\n+}\n+\n+bool is_json_context(void)\n+{\n+\treturn _jw != NULL;\n+}\n+\n+void set_current_fp(FILE *fp)\n+{\n+\tif (!fp) {\n+\t\tfprintf(stderr, \"Error: invalid file pointer.\\n\");\n+\t\texit(1);\n+\t}\n+\t_fp = fp;\n+}\n+\n+json_writer_t *get_json_writer(void)\n+{\n+\treturn _jw;\n+}\n+\n+void open_json_object(const char *str)\n+{\n+\tif (_IS_JSON_CONTEXT(PRINT_JSON)) {\n+\t\tif (str)\n+\t\t\tjsonw_name(_jw, str);\n+\t\tjsonw_start_object(_jw);\n+\t}\n+}\n+\n+void close_json_object(void)\n+{\n+\tif (_IS_JSON_CONTEXT(PRINT_JSON))\n+\t\tjsonw_end_object(_jw);\n+}\n+\n+/*\n+ * Start json array or string array using\n+ * the provided string as json key (if not null)\n+ * or as array delimiter in non-json context.\n+ */\n+void open_json_array(enum output_type type, const char *str)\n+{\n+\tif (_IS_JSON_CONTEXT(type)) {\n+\t\tif (str)\n+\t\t\tjsonw_name(_jw, str);\n+\t\tjsonw_start_array(_jw);\n+\t} else if (_IS_FP_CONTEXT(type)) {\n+\t\tfprintf(_fp, \"%s\", str);\n+\t}\n+}\n+\n+/*\n+ * End json array or string array\n+ */\n+void close_json_array(enum output_type type, const char *str)\n+{\n+\tif (_IS_JSON_CONTEXT(type)) {\n+\t\tjsonw_pretty(_jw, false);\n+\t\tjsonw_end_array(_jw);\n+\t\tjsonw_pretty(_jw, true);\n+\t} else if (_IS_FP_CONTEXT(type)) {\n+\t\tfprintf(_fp, \"%s\", str);\n+\t}\n+}\n+\n+/*\n+ * pre-processor directive to generate similar\n+ * functions handling different types\n+ */\n+#define _PRINT_FUNC(type_name, type)\t\t\t\t\t\\\n+\tvoid print_color_##type_name(enum output_type t,\t\t\\\n+\t\t\t\t enum color_attr color,\t\t\\\n+\t\t\t\t const char *key,\t\t\t\\\n+\t\t\t\t const char *fmt,\t\t\t\\\n+\t\t\t\t type value)\t\t\t\\\n+\t{\t\t\t\t\t\t\t\t\\\n+\t\tif (_IS_JSON_CONTEXT(t)) {\t\t\t\t\\\n+\t\t\tif (!key)\t\t\t\t\t\\\n+\t\t\t\tjsonw_##type_name(_jw, value);\t\t\\\n+\t\t\telse\t\t\t\t\t\t\\\n+\t\t\t\tjsonw_##type_name##_field(_jw, key, value); \\\n+\t\t} else if (_IS_FP_CONTEXT(t)) {\t\t\t\t\\\n+\t\t\tcolor_fprintf(_fp, color, fmt, value); \\\n+\t\t}\t\t\t\t\t\t\t\\\n+\t}\n+_PRINT_FUNC(int, int);\n+_PRINT_FUNC(hu, unsigned short);\n+_PRINT_FUNC(uint, uint64_t);\n+_PRINT_FUNC(lluint, unsigned long long int);\n+#undef _PRINT_FUNC\n+\n+void print_color_string(enum output_type type,\n+\t\t\tenum color_attr color,\n+\t\t\tconst char *key,\n+\t\t\tconst char *fmt,\n+\t\t\tconst char *value)\n+{\n+\tif (_IS_JSON_CONTEXT(type)) {\n+\t\tif (key && !value)\n+\t\t\tjsonw_name(_jw, key);\n+\t\telse if (!key && value)\n+\t\t\tjsonw_string(_jw, value);\n+\t\telse\n+\t\t\tjsonw_string_field(_jw, key, value);\n+\t} else if (_IS_FP_CONTEXT(type)) {\n+\t\tcolor_fprintf(_fp, color, fmt, value);\n+\t}\n+}\n+\n+/*\n+ * value's type is bool. When using this function in FP context you can't pass\n+ * a value to it, you will need to use \"is_json_context()\" to have different\n+ * branch for json and regular output. grep -r \"print_bool\" for example\n+ */\n+void print_color_bool(enum output_type type,\n+\t\t enum color_attr color,\n+\t\t const char *key,\n+\t\t const char *fmt,\n+\t\t bool value)\n+{\n+\tif (_IS_JSON_CONTEXT(type)) {\n+\t\tif (key)\n+\t\t\tjsonw_bool_field(_jw, key, value);\n+\t\telse\n+\t\t\tjsonw_bool(_jw, value);\n+\t} else if (_IS_FP_CONTEXT(type)) {\n+\t\tcolor_fprintf(_fp, color, fmt, value ? \"true\" : \"false\");\n+\t}\n+}\n+\n+/*\n+ * In JSON context uses hardcode %#x format: 42 -> 0x2a\n+ */\n+void print_color_0xhex(enum output_type type,\n+\t\t enum color_attr color,\n+\t\t const char *key,\n+\t\t const char *fmt,\n+\t\t unsigned int hex)\n+{\n+\tif (_IS_JSON_CONTEXT(type)) {\n+\t\tSPRINT_BUF(b1);\n+\n+\t\tsnprintf(b1, sizeof(b1), \"%#x\", hex);\n+\t\tprint_string(PRINT_JSON, key, NULL, b1);\n+\t} else if (_IS_FP_CONTEXT(type)) {\n+\t\tcolor_fprintf(_fp, color, fmt, hex);\n+\t}\n+}\n+\n+void print_color_hex(enum output_type type,\n+\t\t enum color_attr color,\n+\t\t const char *key,\n+\t\t const char *fmt,\n+\t\t unsigned int hex)\n+{\n+\tif (_IS_JSON_CONTEXT(type)) {\n+\t\tSPRINT_BUF(b1);\n+\n+\t\tsnprintf(b1, sizeof(b1), \"%x\", hex);\n+\t\tif (key)\n+\t\t\tjsonw_string_field(_jw, key, b1);\n+\t\telse\n+\t\t\tjsonw_string(_jw, b1);\n+\t} else if (_IS_FP_CONTEXT(type)) {\n+\t\tcolor_fprintf(_fp, color, fmt, hex);\n+\t}\n+}\n+\n+/*\n+ * In JSON context we don't use the argument \"value\" we simply call jsonw_null\n+ * whereas FP context can use \"value\" to output anything\n+ */\n+void print_color_null(enum output_type type,\n+\t\t enum color_attr color,\n+\t\t const char *key,\n+\t\t const char *fmt,\n+\t\t const char *value)\n+{\n+\tif (_IS_JSON_CONTEXT(type)) {\n+\t\tif (key)\n+\t\t\tjsonw_null_field(_jw, key);\n+\t\telse\n+\t\t\tjsonw_null(_jw);\n+\t} else if (_IS_FP_CONTEXT(type)) {\n+\t\tcolor_fprintf(_fp, color, fmt, value);\n+\t}\n+}\n", "prefixes": [ "iproute2", "master", "1/2" ] }