]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - lib/json_print.c
libnetlink: Convert GETMDB dumps to use rtnl_mdbdump_req
[mirror_iproute2.git] / lib / json_print.c
index 93b4119dff9907c901ec2c9200520e4b9a8a04ec..eed109c56401c8db629dd5bb2893cbc74d4cc275 100644 (file)
 #include "json_print.h"
 
 static json_writer_t *_jw;
-static FILE *_fp;
 
 #define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw)
 #define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY))
 
-void new_json_obj(int json, FILE *fp)
+void new_json_obj(int json)
 {
        if (json) {
-               _jw = jsonw_new(fp);
+               _jw = jsonw_new(stdout);
                if (!_jw) {
                        perror("json object");
                        exit(1);
                }
-               jsonw_pretty(_jw, true);
+               if (pretty)
+                       jsonw_pretty(_jw, true);
                jsonw_start_array(_jw);
        }
-       set_current_fp(fp);
 }
 
 void delete_json_obj(void)
@@ -48,15 +47,6 @@ bool is_json_context(void)
        return _jw != NULL;
 }
 
-void set_current_fp(FILE *fp)
-{
-       if (!fp) {
-               fprintf(stderr, "Error: invalid file pointer.\n");
-               exit(1);
-       }
-       _fp = fp;
-}
-
 json_writer_t *get_json_writer(void)
 {
        return _jw;
@@ -89,7 +79,7 @@ void open_json_array(enum output_type type, const char *str)
                        jsonw_name(_jw, str);
                jsonw_start_array(_jw);
        } else if (_IS_FP_CONTEXT(type)) {
-               fprintf(_fp, "%s", str);
+               printf("%s", str);
        }
 }
 
@@ -99,11 +89,9 @@ void open_json_array(enum output_type type, const char *str)
 void close_json_array(enum output_type type, const char *str)
 {
        if (_IS_JSON_CONTEXT(type)) {
-               jsonw_pretty(_jw, false);
                jsonw_end_array(_jw);
-               jsonw_pretty(_jw, true);
        } else if (_IS_FP_CONTEXT(type)) {
-               fprintf(_fp, "%s", str);
+               printf("%s", str);
        }
 }
 
@@ -112,6 +100,7 @@ void close_json_array(enum output_type type, const char *str)
  * functions handling different types
  */
 #define _PRINT_FUNC(type_name, type)                                   \
+       __attribute__((format(printf, 4, 0)))                           \
        void print_color_##type_name(enum output_type t,                \
                                     enum color_attr color,             \
                                     const char *key,                   \
@@ -124,13 +113,17 @@ void close_json_array(enum output_type type, const char *str)
                        else                                            \
                                jsonw_##type_name##_field(_jw, key, value); \
                } else if (_IS_FP_CONTEXT(t)) {                         \
-                       color_fprintf(_fp, color, fmt, value);          \
+                       color_fprintf(stdout, color, fmt, value);          \
                }                                                       \
        }
 _PRINT_FUNC(int, int);
+_PRINT_FUNC(s64, int64_t);
 _PRINT_FUNC(hu, unsigned short);
-_PRINT_FUNC(uint, uint64_t);
+_PRINT_FUNC(uint, unsigned int);
+_PRINT_FUNC(u64, uint64_t);
+_PRINT_FUNC(luint, unsigned long int);
 _PRINT_FUNC(lluint, unsigned long long int);
+_PRINT_FUNC(float, double);
 #undef _PRINT_FUNC
 
 void print_color_string(enum output_type type,
@@ -147,7 +140,7 @@ void print_color_string(enum output_type type,
                else
                        jsonw_string_field(_jw, key, value);
        } else if (_IS_FP_CONTEXT(type)) {
-               color_fprintf(_fp, color, fmt, value);
+               color_fprintf(stdout, color, fmt, value);
        }
 }
 
@@ -168,7 +161,7 @@ void print_color_bool(enum output_type type,
                else
                        jsonw_bool(_jw, value);
        } else if (_IS_FP_CONTEXT(type)) {
-               color_fprintf(_fp, color, fmt, value ? "true" : "false");
+               color_fprintf(stdout, color, fmt, value ? "true" : "false");
        }
 }
 
@@ -187,7 +180,7 @@ void print_color_0xhex(enum output_type type,
                snprintf(b1, sizeof(b1), "%#x", hex);
                print_string(PRINT_JSON, key, NULL, b1);
        } else if (_IS_FP_CONTEXT(type)) {
-               color_fprintf(_fp, color, fmt, hex);
+               color_fprintf(stdout, color, fmt, hex);
        }
 }
 
@@ -206,7 +199,7 @@ void print_color_hex(enum output_type type,
                else
                        jsonw_string(_jw, b1);
        } else if (_IS_FP_CONTEXT(type)) {
-               color_fprintf(_fp, color, fmt, hex);
+               color_fprintf(stdout, color, fmt, hex);
        }
 }
 
@@ -226,6 +219,13 @@ void print_color_null(enum output_type type,
                else
                        jsonw_null(_jw);
        } else if (_IS_FP_CONTEXT(type)) {
-               color_fprintf(_fp, color, fmt, value);
+               color_fprintf(stdout, color, fmt, value);
        }
 }
+
+/* Print line seperator (if not in JSON mode) */
+void print_nl(void)
+{
+       if (!_jw)
+               printf("%s", _SL_);
+}