#include <sys/time.h>
#include "plugin.h"
-#ifdef CONFIG_JSONC
-#include <json.h>
-
-#define json_create_object(o) json_object_new_object(o)
-#define json_create_array(a) json_object_new_array(a)
-#define json_free_object(o) json_object_put(o)
-#define json_free_array(a) json_object_put(a)
-#define json_object_add_value_uint(o, k, v) \
- json_object_object_add(o, k, json_object_new_int(v))
-#define json_object_add_value_int(o, k, v) \
- json_object_object_add(o, k, json_object_new_int(v))
-#ifdef CONFIG_JSONC_14
-#define json_object_add_value_uint64(o, k, v) \
- json_object_object_add(o, k, json_object_new_uint64(v))
-#else
-#define json_object_add_value_uint64(o, k, v) \
- if ((v) > UINT_MAX) { \
- fprintf(stderr, "Value overflow in %s", k); \
- json_object_object_add(o, k, json_object_new_int(-1)); \
- } else \
- json_object_object_add(o, k, json_object_new_int(v))
-#endif
-#define json_object_add_value_float(o, k, v) \
- json_object_object_add(o, k, json_object_new_double(v))
-#define json_object_add_value_string(o, k, v) \
- json_object_object_add(o, k, json_object_new_string(v))
-#define json_object_add_value_array(o, k, v) \
- json_object_object_add(o, k, v)
-#define json_object_add_value_object(o, k, v) \
- json_object_object_add(o, k, v)
-#define json_array_add_value_object(o, k) \
- json_object_array_add(o, k)
-#define json_array_add_value_string(o, v) \
- json_object_array_add(o, json_object_new_string(v))
-#define json_print_object(o, u) \
- printf("%s", json_object_to_json_string_ext(o, \
- JSON_C_TO_STRING_PRETTY | \
- JSON_C_TO_STRING_NOSLASHESCAPE))
-#else
#include "util/json.h"
-#endif
#include "util/argconfig.h"
enum nvme_print_flags {
// SPDX-License-Identifier: GPL-2.0-or-later
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-#include <errno.h>
-#include <stdarg.h>
#include "json.h"
-
-static inline void fail_and_notify(void)
-{
- fprintf(stderr, "Allocation of memory for json object failed, aborting.\n");
- abort();
-}
-
-struct json_object *json_create_object(void)
-{
- void *test = calloc(1, sizeof(struct json_object));
- if (!test)
- fail_and_notify();
- return test;
-}
-
-struct json_object *json_create_array(void)
-{
- void *test = calloc(1, sizeof(struct json_object));
- if (!test)
- fail_and_notify();
- return test;
-}
-
-static struct json_pair *json_create_pair(const char *name, struct json_value *value)
-{
- struct json_pair *pair = malloc(sizeof(struct json_pair));
- if (pair) {
- pair->name = strdup(name);
- pair->value = value;
-
- value->parent_type = JSON_PARENT_TYPE_PAIR;
- value->parent_pair = pair;
- } else
- fail_and_notify();
-
- return pair;
-}
-
-static struct json_value *json_create_value_int(long long number)
-{
- struct json_value *value = malloc(sizeof(struct json_value));
-
- if (value) {
- value->type = JSON_TYPE_INTEGER;
- value->integer_number = number;
- } else
- fail_and_notify();
-
- return value;
-}
-
-static struct json_value *json_create_value_uint(unsigned long long number)
-{
- struct json_value *value = malloc(sizeof(struct json_value));
-
- if (value) {
- value->type = JSON_TYPE_UINT;
- value->uint_number = number;
- } else
- fail_and_notify();
-
- return value;
-}
-
-static struct json_value *json_create_value_float(long double number)
-{
- struct json_value *value = malloc(sizeof(struct json_value));
-
- if (value) {
- value->type = JSON_TYPE_FLOAT;
- value->float_number = number;
- } else
- fail_and_notify();
-
- return value;
-}
-
-static char *strdup_escape(const char *str)
-{
- const char *input = str;
- char *p, *ret;
- int escapes;
-
- if (!strlen(str))
- return NULL;
-
- escapes = 0;
- while ((input = strpbrk(input, "\\\"")) != NULL) {
- escapes++;
- input++;
- }
-
- p = ret = malloc(strlen(str) + escapes + 1);
- if (!ret)
- fail_and_notify();
-
- while (*str) {
- if (*str == '\\' || *str == '\"')
- *p++ = '\\';
- *p++ = *str++;
- }
- *p = '\0';
-
- return ret;
-}
-
-/*
- * Valid JSON strings must escape '"' and '/' with a preceding '/'
- */
-static struct json_value *json_create_value_string(const char *str)
-{
- struct json_value *value = malloc(sizeof(struct json_value));
-
- if (value) {
- value->type = JSON_TYPE_STRING;
- value->string = strdup_escape(str ? str : "(null)");
- if (!value->string) {
- free(value);
- value = NULL;
- return value;
- }
- }
- if (!value)
- fail_and_notify();
-
- return value;
-}
-
-static struct json_value *json_create_value_object(struct json_object *obj)
-{
- struct json_value *value = malloc(sizeof(struct json_value));
-
- if (value) {
- value->type = JSON_TYPE_OBJECT;
- value->object = obj;
- obj->parent = value;
- } else
- fail_and_notify();
-
- return value;
-}
-
-static struct json_value *json_create_value_array(struct json_object *array)
-{
- struct json_value *value = malloc(sizeof(struct json_value));
-
- if (value) {
- value->type = JSON_TYPE_ARRAY;
- value->array = array;
- array->parent = value;
- } else
- fail_and_notify();
-
- return value;
-}
-
-static void json_free_pair(struct json_pair *pair);
-static void json_free_value(struct json_value *value);
-
-void json_free_object(struct json_object *obj)
-{
- int i;
-
- for (i = 0; i < obj->pair_cnt; i++)
- json_free_pair(obj->pairs[i]);
- free(obj->pairs);
- free(obj);
-}
-
-void json_free_array(struct json_object *array)
-{
- int i;
-
- for (i = 0; i < array->value_cnt; i++)
- json_free_value(array->values[i]);
- free(array->values);
- free(array);
-}
-
-static void json_free_pair(struct json_pair *pair)
-{
- json_free_value(pair->value);
- free(pair->name);
- free(pair);
-}
-
-static void json_free_value(struct json_value *value)
-{
- switch (value->type) {
- case JSON_TYPE_STRING:
- free(value->string);
- break;
- case JSON_TYPE_OBJECT:
- json_free_object(value->object);
- break;
- case JSON_TYPE_ARRAY:
- json_free_array(value->array);
- break;
- }
- free(value);
-}
-
-static int json_array_add_value(struct json_object *array, struct json_value *value)
-{
- struct json_value **values = realloc(array->values,
- sizeof(struct json_value *) * (array->value_cnt + 1));
-
- if (!values)
- return ENOMEM;
- values[array->value_cnt] = value;
- array->value_cnt++;
- array->values = values;
-
- value->parent_type = JSON_PARENT_TYPE_ARRAY;
- value->parent_array = array;
- return 0;
-}
-
-static int json_object_add_pair(struct json_object *obj, struct json_pair *pair)
-{
- struct json_pair **pairs = realloc(obj->pairs,
- sizeof(struct json_pair *) * (obj->pair_cnt + 1));
- if (!pairs)
- return ENOMEM;
- pairs[obj->pair_cnt] = pair;
- obj->pair_cnt++;
- obj->pairs = pairs;
-
- pair->parent = obj;
- return 0;
-}
-
-int json_object_add_value_type(struct json_object *obj, const char *name, int type, ...)
-{
- struct json_value *value;
- struct json_pair *pair;
- va_list args;
- int ret;
-
- va_start(args, type);
- if (type == JSON_TYPE_STRING)
- value = json_create_value_string(va_arg(args, char *));
- else if (type == JSON_TYPE_INTEGER)
- value = json_create_value_int(va_arg(args, long long));
- else if (type == JSON_TYPE_UINT)
- value = json_create_value_uint(va_arg(args, unsigned long long));
- else if (type == JSON_TYPE_FLOAT)
- value = json_create_value_float(va_arg(args, long double));
- else if (type == JSON_TYPE_OBJECT)
- value = json_create_value_object(va_arg(args, struct json_object *));
- else
- value = json_create_value_array(va_arg(args, struct json_object *));
- va_end(args);
-
- if (!value)
- return ENOMEM;
-
- pair = json_create_pair(name, value);
- if (!pair) {
- json_free_value(value);
- return ENOMEM;
- }
- ret = json_object_add_pair(obj, pair);
- if (ret) {
- json_free_pair(pair);
- return ENOMEM;
- }
- return 0;
-}
-
-static void json_print_array(struct json_object *array, void *);
-int json_array_add_value_type(struct json_object *array, int type, ...)
-{
- struct json_value *value;
- va_list args;
- int ret;
-
- va_start(args, type);
- if (type == JSON_TYPE_STRING)
- value = json_create_value_string(va_arg(args, char *));
- else if (type == JSON_TYPE_INTEGER)
- value = json_create_value_int(va_arg(args, long long));
- else if (type == JSON_TYPE_UINT)
- value = json_create_value_uint(va_arg(args, unsigned long long));
- else if (type == JSON_TYPE_FLOAT)
- value = json_create_value_float(va_arg(args, double));
- else if (type == JSON_TYPE_OBJECT)
- value = json_create_value_object(va_arg(args, struct json_object *));
- else
- value = json_create_value_array(va_arg(args, struct json_object *));
- va_end(args);
-
- if (!value)
- return ENOMEM;
-
- ret = json_array_add_value(array, value);
- if (ret) {
- json_free_value(value);
- return ENOMEM;
- }
- return 0;
-}
-
-static int json_value_level(struct json_value *value);
-static int json_pair_level(struct json_pair *pair);
-static int json_array_level(struct json_object *array);
-static int json_object_level(struct json_object *object)
-{
- if (object->parent == NULL)
- return 0;
- return json_value_level(object->parent);
-}
-
-static int json_pair_level(struct json_pair *pair)
-{
- return json_object_level(pair->parent) + 1;
-}
-
-static int json_array_level(struct json_object *array)
-{
- return json_value_level(array->parent);
-}
-
-static int json_value_level(struct json_value *value)
-{
- if (value->parent_type == JSON_PARENT_TYPE_PAIR)
- return json_pair_level(value->parent_pair);
- else
- return json_array_level(value->parent_array) + 1;
-}
-
-static void json_print_level(int level, void *out)
-{
- while (level-- > 0)
- printf(" ");
-}
-
-static void json_print_pair(struct json_pair *pair, void *);
-static void json_print_array(struct json_object *array, void *);
-static void json_print_value(struct json_value *value, void *);
-void json_print_object(struct json_object *obj, void *out)
-{
- int i;
-
- printf("{\n");
- for (i = 0; i < obj->pair_cnt; i++) {
- if (i > 0)
- printf(",\n");
- json_print_pair(obj->pairs[i], out);
- }
- printf("\n");
- json_print_level(json_object_level(obj), out);
- printf("}");
-}
-
-static void json_print_pair(struct json_pair *pair, void *out)
-{
- json_print_level(json_pair_level(pair), out);
- printf("\"%s\" : ", pair->name);
- json_print_value(pair->value, out);
-}
-
-static void json_print_array(struct json_object *array, void *out)
-{
- int i;
-
- printf("[\n");
- for (i = 0; i < array->value_cnt; i++) {
- if (i > 0)
- printf(",\n");
- json_print_level(json_value_level(array->values[i]), out);
- json_print_value(array->values[i], out);
- }
- printf("\n");
- json_print_level(json_array_level(array), out);
- printf("]");
-}
-
-static void json_print_value(struct json_value *value, void *out)
-{
- switch (value->type) {
- case JSON_TYPE_STRING:
- printf( "\"%s\"", value->string);
- break;
- case JSON_TYPE_INTEGER:
- printf( "%lld", value->integer_number);
- break;
- case JSON_TYPE_UINT:
- printf( "%llu", value->uint_number);
- break;
- case JSON_TYPE_FLOAT:
- printf( "%.0Lf", value->float_number);
- break;
- case JSON_TYPE_OBJECT:
- json_print_object(value->object, out);
- break;
- case JSON_TYPE_ARRAY:
- json_print_array(value->array, out);
- break;
- }
-}
#ifndef __JSON__H
#define __JSON__H
-struct json_object;
-struct json_pair;
-
-#define JSON_TYPE_STRING 0
-#define JSON_TYPE_INTEGER 1
-#define JSON_TYPE_FLOAT 2
-#define JSON_TYPE_OBJECT 3
-#define JSON_TYPE_ARRAY 4
-#define JSON_TYPE_UINT 5
-#define JSON_PARENT_TYPE_PAIR 0
-#define JSON_PARENT_TYPE_ARRAY 1
-struct json_value {
- int type;
- union {
- long long integer_number;
- unsigned long long uint_number;
- long double float_number;
- char *string;
- struct json_object *object;
- struct json_object *array;
- };
- int parent_type;
- union {
- struct json_pair *parent_pair;
- struct json_object *parent_array;
- };
-};
-
-struct json_object {
- struct json_value **values;
- int value_cnt;
- struct json_pair **pairs;
- int pair_cnt;
- struct json_value *parent;
-};
-
-struct json_pair {
- char *name;
- struct json_value *value;
- struct json_object *parent;
-};
-
-struct json_object *json_create_object(void);
-struct json_object *json_create_array(void);
-
-void json_free_object(struct json_object *obj);
-void json_free_array(struct json_object *array);
-
-int json_object_add_value_type(struct json_object *obj, const char *name, int type, ...);
-#define json_object_add_value_int(obj, name, val) \
- json_object_add_value_type((obj), name, JSON_TYPE_INTEGER, (long long) (val))
-#define json_object_add_value_uint(obj, name, val) \
- json_object_add_value_type((obj), name, JSON_TYPE_UINT, (unsigned long long) (val))
-#define json_object_add_value_uint64 json_object_add_value_uint
-#define json_object_add_value_float(obj, name, val) \
- json_object_add_value_type((obj), name, JSON_TYPE_FLOAT, (val))
-#define json_object_add_value_string(obj, name, val) \
- json_object_add_value_type((obj), name, JSON_TYPE_STRING, (val))
-#define json_object_add_value_object(obj, name, val) \
- json_object_add_value_type((obj), name, JSON_TYPE_OBJECT, (val))
-#define json_object_add_value_array(obj, name, val) \
- json_object_add_value_type((obj), name, JSON_TYPE_ARRAY, (val))
-int json_array_add_value_type(struct json_object *array, int type, ...);
-#define json_array_add_value_int(obj, val) \
- json_array_add_value_type((obj), JSON_TYPE_INTEGER, (val))
-#define json_array_add_value_uint(obj, val) \
- json_array_add_value_type((obj), JSON_TYPE_UINT, (val))
-#define json_array_add_value_float(obj, val) \
- json_array_add_value_type((obj), JSON_TYPE_FLOAT, (val))
-#define json_array_add_value_string(obj, val) \
- json_array_add_value_type((obj), JSON_TYPE_STRING, (val))
-#define json_array_add_value_object(obj, val) \
- json_array_add_value_type((obj), JSON_TYPE_OBJECT, (val))
-#define json_array_add_value_array(obj, val) \
- json_array_add_value_type((obj), JSON_TYPE_ARRAY, (val))
-
-#define json_array_last_value_object(obj) \
- (obj->values[obj->value_cnt - 1]->object)
-
-void json_print_object(struct json_object *obj, void *);
+#include <json.h>
+
+/* Wrappers around json-c's API */
+
+#define json_create_object(o) json_object_new_object(o)
+#define json_create_array(a) json_object_new_array(a)
+#define json_free_object(o) json_object_put(o)
+#define json_free_array(a) json_object_put(a)
+#define json_object_add_value_uint(o, k, v) \
+ json_object_object_add(o, k, json_object_new_int(v))
+#define json_object_add_value_int(o, k, v) \
+ json_object_object_add(o, k, json_object_new_int(v))
+#ifdef CONFIG_JSONC_14
+#define json_object_add_value_uint64(o, k, v) \
+ json_object_object_add(o, k, json_object_new_uint64(v))
+#else
+#define json_object_add_value_uint64(o, k, v) \
+ if ((v) > UINT_MAX) { \
+ fprintf(stderr, "Value overflow in %s", k); \
+ json_object_object_add(o, k, json_object_new_int(-1)); \
+ } else \
+ json_object_object_add(o, k, json_object_new_int(v))
+#endif
+#define json_object_add_value_float(o, k, v) \
+ json_object_object_add(o, k, json_object_new_double(v))
+#define json_object_add_value_string(o, k, v) \
+ json_object_object_add(o, k, json_object_new_string(v))
+#define json_object_add_value_array(o, k, v) \
+ json_object_object_add(o, k, v)
+#define json_object_add_value_object(o, k, v) \
+ json_object_object_add(o, k, v)
+#define json_array_add_value_object(o, k) \
+ json_object_array_add(o, k)
+#define json_array_add_value_string(o, v) \
+ json_object_array_add(o, json_object_new_string(v))
+#define json_print_object(o, u) \
+ printf("%s", json_object_to_json_string_ext(o, \
+ JSON_C_TO_STRING_PRETTY | \
+ JSON_C_TO_STRING_NOSLASHESCAPE))
#endif