aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Gilbert <dgilbert@interlog.com>2022-04-17 14:15:24 +0000
committerDouglas Gilbert <dgilbert@interlog.com>2022-04-17 14:15:24 +0000
commitf26bb872bf4f043a1b817c808a6947fcb03ecda4 (patch)
tree132ab4d71c41c26fe05e12a57fc9e684ffbd08ec
parent5e7262973c7b0bf753e5e560956c176d9d12424d (diff)
downloadsg3_utils-f26bb872bf4f043a1b817c808a6947fcb03ecda4.tar.gz
sg_lib: add hex2fp(); sg_rtpg: https://github.com/hreinecke/sg3_utils/pull/79 applies; json_builder work
git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@944 6180dd3e-e324-4e3e-922d-17de1ae2f315
-rw-r--r--ChangeLog5
-rw-r--r--archive/sg_json_writer.c360
-rw-r--r--archive/sg_json_writer.h101
-rw-r--r--doc/scsi_logging_level.811
-rw-r--r--doc/sg3_utils.82
-rw-r--r--doc/sg_rep_zones.86
-rw-r--r--include/sg_lib.h4
-rw-r--r--include/sg_pr2serr.h23
-rw-r--r--lib/Makefile.am4
-rw-r--r--lib/Makefile.in44
-rw-r--r--lib/sg_json_builder.c995
-rw-r--r--lib/sg_json_builder.h324
-rw-r--r--lib/sg_lib.c18
-rw-r--r--src/sg_logs.c154
-rw-r--r--src/sg_opcodes.c2
-rw-r--r--src/sg_rep_zones.c13
-rw-r--r--src/sg_rtpg.c28
-rw-r--r--testing/Makefile10
-rw-r--r--testing/sg_mrq_dd.cpp28
-rw-r--r--testing/sg_tst_json_builder.c157
20 files changed, 2132 insertions, 157 deletions
diff --git a/ChangeLog b/ChangeLog
index 3975faa6..bd6145af 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,11 +2,13 @@ Each utility has its own version number, date of last change and
some description at the top of its ".c" file. All utilities in the main
directory have their own "man" pages. There is also a sg3_utils man page.
-Changelog for pre-release sg3_utils-1.48 [20220309] [svn: r943]
+Changelog for pre-release sg3_utils-1.48 [20220417] [svn: r944]
- sg_z_act_query: new utility for sending either a
Zone activate or Zone query command
- sg_rep_density: new utility for decoding the response of
Report density support command [ssc (tape)]
+ - sg_rtpg: https://github.com/hreinecke/sg3_utils/pull/79
+ applied
- rescan-scsi-bus.sh: with '-r' it crashed due to change in
rev 867 in sg_inq.c from Device_type= to PDT= .
Change script to use either
@@ -45,6 +47,7 @@ Changelog for pre-release sg3_utils-1.48 [20220309] [svn: r943]
log (sub)pages
- sg_lib: add sg_pdt_s_eq() to cope with ZBC disks which may
be either PDT_ZBC (if host managed) or PDT_DISK
+ - add hex2fp(), similar to hex2str() but outputs to FILE
- cleanup masks for PDT [0x1f] and group_number [0x3f]
- initialize all sense buffers to 0
- rework main README file
diff --git a/archive/sg_json_writer.c b/archive/sg_json_writer.c
new file mode 100644
index 00000000..bc1c7b16
--- /dev/null
+++ b/archive/sg_json_writer.c
@@ -0,0 +1,360 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Simple streaming JSON writer
+ *
+ * This takes care of the annoying bits of JSON syntax like the commas
+ * after elements
+ *
+ * Authors: Stephen Hemminger <stephen@networkplumber.org>
+ *
+ * Borrowed from Linux kernel [5.17.0]: tools/bpf/bpftool/json_writer.[hc]
+ */
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdarg.h>
+#include <assert.h>
+#include <malloc.h>
+#include <inttypes.h>
+#include <stdint.h>
+
+#include "sg_json_writer.h"
+
+struct json_writer {
+ FILE *out; /* output file */
+ unsigned depth; /* nesting */
+ bool pretty; /* optional whitepace */
+ char sep; /* either nul or comma */
+};
+
+/* indentation for pretty print */
+static void jsonw_indent(json_writer_t *self)
+{
+ unsigned i;
+ for (i = 0; i < self->depth; ++i)
+ fputs(" ", self->out);
+}
+
+/* end current line and indent if pretty printing */
+static void jsonw_eol(json_writer_t *self)
+{
+ if (!self->pretty)
+ return;
+
+ putc('\n', self->out);
+ jsonw_indent(self);
+}
+
+/* If current object is not empty print a comma */
+static void jsonw_eor(json_writer_t *self)
+{
+ if (self->sep != '\0')
+ putc(self->sep, self->out);
+ self->sep = ',';
+}
+
+
+/* Output JSON encoded string */
+/* Handles C escapes, does not do Unicode */
+static void jsonw_puts(json_writer_t *self, const char *str)
+{
+ putc('"', self->out);
+ for (; *str; ++str)
+ switch (*str) {
+ case '\t':
+ fputs("\\t", self->out);
+ break;
+ case '\n':
+ fputs("\\n", self->out);
+ break;
+ case '\r':
+ fputs("\\r", self->out);
+ break;
+ case '\f':
+ fputs("\\f", self->out);
+ break;
+ case '\b':
+ fputs("\\b", self->out);
+ break;
+ case '\\':
+ fputs("\\n", self->out);
+ break;
+ case '"':
+ fputs("\\\"", self->out);
+ break;
+ case '\'':
+ fputs("\\\'", self->out);
+ break;
+ default:
+ putc(*str, self->out);
+ }
+ putc('"', self->out);
+}
+
+/* Create a new JSON stream */
+json_writer_t *jsonw_new(FILE *f)
+{
+ json_writer_t *self = malloc(sizeof(*self));
+ if (self) {
+ self->out = f;
+ self->depth = 0;
+ self->pretty = false;
+ self->sep = '\0';
+ }
+ return self;
+}
+
+/* End output to JSON stream */
+void jsonw_destroy(json_writer_t **self_p)
+{
+ json_writer_t *self = *self_p;
+
+ assert(self->depth == 0);
+ fputs("\n", self->out);
+ fflush(self->out);
+ free(self);
+ *self_p = NULL;
+}
+
+void jsonw_pretty(json_writer_t *self, bool on)
+{
+ self->pretty = on;
+}
+
+void jsonw_reset(json_writer_t *self)
+{
+ assert(self->depth == 0);
+ self->sep = '\0';
+}
+
+/* Basic blocks */
+static void jsonw_begin(json_writer_t *self, int c)
+{
+ jsonw_eor(self);
+ putc(c, self->out);
+ ++self->depth;
+ self->sep = '\0';
+}
+
+static void jsonw_end(json_writer_t *self, int c)
+{
+ assert(self->depth > 0);
+
+ --self->depth;
+ if (self->sep != '\0')
+ jsonw_eol(self);
+ putc(c, self->out);
+ self->sep = ',';
+}
+
+
+/* Add a JSON property name */
+void jsonw_name(json_writer_t *self, const char *name)
+{
+ jsonw_eor(self);
+ jsonw_eol(self);
+ self->sep = '\0';
+ jsonw_puts(self, name);
+ putc(':', self->out);
+ if (self->pretty)
+ putc(' ', self->out);
+}
+
+void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap)
+{
+ jsonw_eor(self);
+ putc('"', self->out);
+ vfprintf(self->out, fmt, ap);
+ putc('"', self->out);
+}
+
+void jsonw_printf(json_writer_t *self, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ jsonw_eor(self);
+ vfprintf(self->out, fmt, ap);
+ va_end(ap);
+}
+
+/* Collections */
+void jsonw_start_object(json_writer_t *self)
+{
+ jsonw_begin(self, '{');
+}
+
+void jsonw_end_object(json_writer_t *self)
+{
+ jsonw_end(self, '}');
+}
+
+void jsonw_start_array(json_writer_t *self)
+{
+ jsonw_begin(self, '[');
+}
+
+void jsonw_end_array(json_writer_t *self)
+{
+ jsonw_end(self, ']');
+}
+
+/* JSON value types */
+void jsonw_string(json_writer_t *self, const char *value)
+{
+ jsonw_eor(self);
+ jsonw_puts(self, value);
+}
+
+void jsonw_bool(json_writer_t *self, bool val)
+{
+ jsonw_printf(self, "%s", val ? "true" : "false");
+}
+
+void jsonw_null(json_writer_t *self)
+{
+ jsonw_printf(self, "null");
+}
+
+void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num)
+{
+ jsonw_printf(self, fmt, num);
+}
+
+// #ifdef notused
+void jsonw_float(json_writer_t *self, double num)
+{
+ jsonw_printf(self, "%g", num);
+}
+// #endif
+
+void jsonw_hu(json_writer_t *self, unsigned short num)
+{
+ jsonw_printf(self, "%hu", num);
+}
+
+void jsonw_uint(json_writer_t *self, uint64_t num)
+{
+ jsonw_printf(self, "%"PRIu64, num);
+}
+
+void jsonw_lluint(json_writer_t *self, unsigned long long int num)
+{
+ jsonw_printf(self, "%llu", num);
+}
+
+void jsonw_int(json_writer_t *self, int64_t num)
+{
+ jsonw_printf(self, "%"PRId64, num);
+}
+
+/* Basic name/value objects */
+void jsonw_string_field(json_writer_t *self, const char *prop, const char *val)
+{
+ jsonw_name(self, prop);
+ jsonw_string(self, val);
+}
+
+void jsonw_bool_field(json_writer_t *self, const char *prop, bool val)
+{
+ jsonw_name(self, prop);
+ jsonw_bool(self, val);
+}
+
+// #ifdef notused
+void jsonw_float_field(json_writer_t *self, const char *prop, double val)
+{
+ jsonw_name(self, prop);
+ jsonw_float(self, val);
+}
+// #endif
+
+void jsonw_float_field_fmt(json_writer_t *self,
+ const char *prop,
+ const char *fmt,
+ double val)
+{
+ jsonw_name(self, prop);
+ jsonw_float_fmt(self, fmt, val);
+}
+
+void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num)
+{
+ jsonw_name(self, prop);
+ jsonw_uint(self, num);
+}
+
+void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num)
+{
+ jsonw_name(self, prop);
+ jsonw_hu(self, num);
+}
+
+void jsonw_lluint_field(json_writer_t *self,
+ const char *prop,
+ unsigned long long int num)
+{
+ jsonw_name(self, prop);
+ jsonw_lluint(self, num);
+}
+
+void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num)
+{
+ jsonw_name(self, prop);
+ jsonw_int(self, num);
+}
+
+void jsonw_null_field(json_writer_t *self, const char *prop)
+{
+ jsonw_name(self, prop);
+ jsonw_null(self);
+}
+
+#ifdef TEST
+int main(int argc, char **argv)
+{
+ json_writer_t *wr = jsonw_new(stdout);
+
+ jsonw_start_object(wr);
+ jsonw_pretty(wr, true);
+ jsonw_name(wr, "Vyatta");
+ jsonw_start_object(wr);
+ jsonw_string_field(wr, "url", "http://vyatta.com");
+ jsonw_uint_field(wr, "downloads", 2000000ul);
+ jsonw_float_field(wr, "stock", 8.16);
+
+ jsonw_name(wr, "ARGV");
+ jsonw_start_array(wr);
+ while (--argc)
+ jsonw_string(wr, *++argv);
+ jsonw_end_array(wr);
+
+ jsonw_name(wr, "empty");
+ jsonw_start_array(wr);
+ jsonw_end_array(wr);
+
+ jsonw_name(wr, "NIL");
+ jsonw_start_object(wr);
+ jsonw_end_object(wr);
+
+ jsonw_null_field(wr, "my_null");
+
+ jsonw_name(wr, "special chars");
+ jsonw_start_array(wr);
+ jsonw_string_field(wr, "slash", "/");
+ jsonw_string_field(wr, "newline", "\n");
+ jsonw_string_field(wr, "tab", "\t");
+ jsonw_string_field(wr, "ff", "\f");
+ jsonw_string_field(wr, "quote", "\"");
+ jsonw_string_field(wr, "tick", "\'");
+ jsonw_string_field(wr, "backslash", "\\");
+ jsonw_end_array(wr);
+
+ jsonw_end_object(wr);
+
+ jsonw_end_object(wr);
+ jsonw_destroy(&wr);
+ return 0;
+}
+
+#endif
diff --git a/archive/sg_json_writer.h b/archive/sg_json_writer.h
new file mode 100644
index 00000000..c751ade8
--- /dev/null
+++ b/archive/sg_json_writer.h
@@ -0,0 +1,101 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+/*
+ * Simple streaming JSON writer
+ *
+ * This takes care of the annoying bits of JSON syntax like the commas
+ * after elements
+ *
+ * Authors: Stephen Hemminger <stephen@networkplumber.org>
+ *
+ * Borrowed from Linux kernel [5.17.0]: tools/bpf/bpftool/json_writer.[hc]
+ */
+
+#ifndef SG_JSON_WRITER_H_
+#define SG_JSON_WRITER_H_
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdarg.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Need to resolve __printf(a, b) macro calls
+#if defined(__GNUC__) || defined(__clang__)
+#ifdef SG_LIB_MINGW
+/* MinGW uses Microsoft's printf */
+
+#define __printf(a, b)
+
+#else /* GNU/clang other than MinGW */
+
+#define __printf(a, b) __attribute__ ((format (printf, a, b)))
+
+#endif
+
+#else /* not GNU (and not clang) */
+
+#define __printf(a, b)
+
+#endif
+
+/* Opaque class structure */
+typedef struct json_writer json_writer_t;
+
+/* Create a new JSON stream */
+json_writer_t *jsonw_new(FILE *f);
+/* End output to JSON stream */
+void jsonw_destroy(json_writer_t **self_p);
+
+/* Cause output to have pretty whitespace */
+void jsonw_pretty(json_writer_t *self, bool on);
+
+/* Reset separator to create new JSON */
+void jsonw_reset(json_writer_t *self);
+
+/* Add property name */
+void jsonw_name(json_writer_t *self, const char *name);
+
+/* Add value */
+void __printf(2, 0) jsonw_vprintf_enquote(json_writer_t *self, const char *fmt,
+ va_list ap);
+void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...);
+void jsonw_string(json_writer_t *self, const char *value);
+void jsonw_bool(json_writer_t *self, bool value);
+void jsonw_float(json_writer_t *self, double number);
+void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num);
+void jsonw_uint(json_writer_t *self, uint64_t number);
+void jsonw_hu(json_writer_t *self, unsigned short number);
+void jsonw_int(json_writer_t *self, int64_t number);
+void jsonw_null(json_writer_t *self);
+void jsonw_lluint(json_writer_t *self, unsigned long long int num);
+
+/* Useful Combinations of name and value */
+void jsonw_string_field(json_writer_t *self, const char *prop, const char *val);
+void jsonw_bool_field(json_writer_t *self, const char *prop, bool value);
+void jsonw_float_field(json_writer_t *self, const char *prop, double num);
+void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num);
+void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num);
+void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num);
+void jsonw_null_field(json_writer_t *self, const char *prop);
+void jsonw_lluint_field(json_writer_t *self, const char *prop,
+ unsigned long long int num);
+void jsonw_float_field_fmt(json_writer_t *self, const char *prop,
+ const char *fmt, double val);
+
+/* Collections */
+void jsonw_start_object(json_writer_t *self);
+void jsonw_end_object(json_writer_t *self);
+
+void jsonw_start_array(json_writer_t *self);
+void jsonw_end_array(json_writer_t *self);
+
+/* Override default exception handling */
+typedef void (jsonw_err_handler_fn)(const char *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* SG_JSON_WRITER_H_ */
diff --git a/doc/scsi_logging_level.8 b/doc/scsi_logging_level.8
index 062d3f4b..57a5cf4b 100644
--- a/doc/scsi_logging_level.8
+++ b/doc/scsi_logging_level.8
@@ -1,4 +1,4 @@
-.TH SCSI_LOGGING_LEVEL "8" "September 2018" "sg3_utils\-1.45" SG3_UTILS
+.TH SCSI_LOGGING_LEVEL "8" "April 2022" "sg3_utils\-1.48" SG3_UTILS
.SH NAME
scsi_logging_level \- access Linux SCSI logging level information
.SH SYNOPSIS
@@ -104,6 +104,15 @@ the sg driver use:
The output from the sg driver caused by this will go to the system
logs (e.g. /var/log/syslog). To reduce the amount of output use a number
lower than 7. Using 0 will turn off the tracing and debug.
+.PP
+To turn on maximum SCSI subsystem logging use:
+.PP
+ scsi_logging_level \-s \-a 7
+.PP
+That is probably best done on a system that does not use a SCSI command
+device to hold the root file system, or the file system that holds the
+system log. Note that SATA disks and USB attached storage nearly always
+use the SCSI subsystem.
.SH AUTHORS
Written by IBM. Small alterations by Douglas Gilbert.
.SH "REPORTING BUGS"
diff --git a/doc/sg3_utils.8 b/doc/sg3_utils.8
index c1254219..3fe20021 100644
--- a/doc/sg3_utils.8
+++ b/doc/sg3_utils.8
@@ -1,4 +1,4 @@
-.TH SG3_UTILS "8" "March 2022" "sg3_utils\-1.48" SG3_UTILS
+.TH SG3_UTILS "8" "April 2022" "sg3_utils\-1.48" SG3_UTILS
.SH NAME
sg3_utils \- a package of utilities for sending SCSI commands
.SH SYNOPSIS
diff --git a/doc/sg_rep_zones.8 b/doc/sg_rep_zones.8
index 0f4c2c4c..68fecee2 100644
--- a/doc/sg_rep_zones.8
+++ b/doc/sg_rep_zones.8
@@ -1,4 +1,4 @@
-.TH SG_REP_ZONES "8" "February 2022" "sg3_utils\-1.48" SG3_UTILS
+.TH SG_REP_ZONES "8" "April 2022" "sg3_utils\-1.48" SG3_UTILS
.SH NAME
sg_rep_zones \- send SCSI REPORT ZONES, REALMS or ZONE DOMAINS command
.SH SYNOPSIS
@@ -162,8 +162,8 @@ condition of closed; 5 for list zones with a zone condition of full; 6 for
list zones with a zone condition of read only; 7 for list zones with a zone
condition of offline. Other values are 0x10 for list zones with 'RWP
recommended' set to true; 0x11 for list zones with non\-sequential write
-resource active set to true and 0x3f for list zones with a zone condition
-of 'not write pointer'.
+resource active set to true, 0x3e for list zones apart from GAP zones, and
+0x3f for list zones with a zone condition of 'not write pointer'.
.TP
\fB\-s\fR, \fB\-\-start\fR=\fILBA\fR
where \fILBA\fR is at the start or within the first zone to be reported. The
diff --git a/include/sg_lib.h b/include/sg_lib.h
index 16e6fc15..383e9072 100644
--- a/include/sg_lib.h
+++ b/include/sg_lib.h
@@ -622,6 +622,10 @@ int dStrHexStr(const char * str, int len, const char * leadin, int format,
int hex2str(const uint8_t * b_str, int len, const char * leadin, int format,
int cb_len, char * cbp);
+/* Similar to hex2str() but outputs to file pointed to be fp */
+void hex2fp(const uint8_t * b_str, int len, const char * leadin, int format,
+ FILE * fp);
+
/* The following 2 functions are equivalent to dStrHex() and dStrHexErr()
* respectively. The difference is only the type of the first of argument:
* uint8_t instead of char. The name of the argument is changed to b_str to
diff --git a/include/sg_pr2serr.h b/include/sg_pr2serr.h
index 351b3e31..70592781 100644
--- a/include/sg_pr2serr.h
+++ b/include/sg_pr2serr.h
@@ -32,6 +32,20 @@
extern "C" {
#endif
+#if 0
+enum sg_json_separator_t {
+ SG_JSON_SEP_NONE = 0,
+ SG_JSON_SEP_SPACE_1,
+ SG_JSON_SEP_SPACE_2,
+ SG_JSON_SEP_SPACE_3,
+ SG_JSON_SEP_SPACE_4,
+ SG_JSON_SEP_EQUAL_NO_SPACE,
+ SG_JSON_SEP_EQUAL_1_SPACE,
+ SG_JSON_SEP_COLON_NO_SPACE,
+ SG_JSON_SEP_COLON_1_SPACE,
+};
+#endif
+
#if defined(__GNUC__) || defined(__clang__)
#ifdef SG_LIB_MINGW
@@ -64,6 +78,15 @@ int sg_scnpr(char * cp, int cp_max_len, const char * fmt, ...);
#endif
+#if 0
+/* Print function for normal and/or json output. "hr" stands for human
+ * readable (only); "j" for JSON (only). */
+void pr_j_simple(int leadin_sp, const char * name,
+ enum sg_json_separator_t sep, const char * value);
+void pr_j_hr_line(const char * hr_line, const char * jname,
+ const char * jvalue);
+#endif
+
#ifdef __cplusplus
}
diff --git a/lib/Makefile.am b/lib/Makefile.am
index ec3932ed..b481816d 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -6,7 +6,8 @@ libsgutils2_la_SOURCES = \
sg_cmds_basic2.c \
sg_cmds_extra.c \
sg_cmds_mmc.c \
- sg_pt_common.c
+ sg_pt_common.c \
+ sg_json_builder.c
if OS_LINUX
if PT_DUMMY
@@ -100,4 +101,3 @@ libsgutils2_la_LDFLAGS = -version-info 2:0:0 -no-undefined -release ${PACKAGE_VE
libsgutils2_la_LIBADD = @GETOPT_O_FILES@
libsgutils2_la_DEPENDENCIES = @GETOPT_O_FILES@
-
diff --git a/lib/Makefile.in b/lib/Makefile.in
index 3b07a603..1a6dbeee 100644
--- a/lib/Makefile.in
+++ b/lib/Makefile.in
@@ -146,9 +146,10 @@ am__installdirs = "$(DESTDIR)$(libdir)"
LTLIBRARIES = $(lib_LTLIBRARIES)
am__libsgutils2_la_SOURCES_DIST = sg_lib.c sg_lib_data.c \
sg_lib_names.c sg_cmds_basic.c sg_cmds_basic2.c \
- sg_cmds_extra.c sg_cmds_mmc.c sg_pt_common.c sg_pt_dummy.c \
- sg_pt_linux.c sg_io_linux.c sg_pt_linux_nvme.c sg_pt_win32.c \
- sg_pt_freebsd.c sg_pt_solaris.c sg_pt_osf1.c sg_pt_haiku.c
+ sg_cmds_extra.c sg_cmds_mmc.c sg_pt_common.c sg_json_builder.c \
+ sg_pt_dummy.c sg_pt_linux.c sg_io_linux.c sg_pt_linux_nvme.c \
+ sg_pt_win32.c sg_pt_freebsd.c sg_pt_solaris.c sg_pt_osf1.c \
+ sg_pt_haiku.c
@OS_LINUX_TRUE@@PT_DUMMY_TRUE@am__objects_1 = sg_pt_dummy.lo
@OS_LINUX_TRUE@@PT_DUMMY_FALSE@am__objects_2 = sg_pt_linux.lo \
@OS_LINUX_TRUE@@PT_DUMMY_FALSE@ sg_io_linux.lo \
@@ -166,11 +167,12 @@ am__libsgutils2_la_SOURCES_DIST = sg_lib.c sg_lib_data.c \
@OS_OTHER_TRUE@am__objects_13 = sg_pt_dummy.lo
am_libsgutils2_la_OBJECTS = sg_lib.lo sg_lib_data.lo sg_lib_names.lo \
sg_cmds_basic.lo sg_cmds_basic2.lo sg_cmds_extra.lo \
- sg_cmds_mmc.lo sg_pt_common.lo $(am__objects_1) \
- $(am__objects_2) $(am__objects_3) $(am__objects_4) \
- $(am__objects_5) $(am__objects_6) $(am__objects_7) \
- $(am__objects_8) $(am__objects_9) $(am__objects_10) \
- $(am__objects_11) $(am__objects_12) $(am__objects_13)
+ sg_cmds_mmc.lo sg_pt_common.lo sg_json_builder.lo \
+ $(am__objects_1) $(am__objects_2) $(am__objects_3) \
+ $(am__objects_4) $(am__objects_5) $(am__objects_6) \
+ $(am__objects_7) $(am__objects_8) $(am__objects_9) \
+ $(am__objects_10) $(am__objects_11) $(am__objects_12) \
+ $(am__objects_13)
libsgutils2_la_OBJECTS = $(am_libsgutils2_la_OBJECTS)
AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
@@ -198,12 +200,13 @@ am__maybe_remake_depfiles = depfiles
am__depfiles_remade = ./$(DEPDIR)/sg_cmds_basic.Plo \
./$(DEPDIR)/sg_cmds_basic2.Plo ./$(DEPDIR)/sg_cmds_extra.Plo \
./$(DEPDIR)/sg_cmds_mmc.Plo ./$(DEPDIR)/sg_io_linux.Plo \
- ./$(DEPDIR)/sg_lib.Plo ./$(DEPDIR)/sg_lib_data.Plo \
- ./$(DEPDIR)/sg_lib_names.Plo ./$(DEPDIR)/sg_pt_common.Plo \
- ./$(DEPDIR)/sg_pt_dummy.Plo ./$(DEPDIR)/sg_pt_freebsd.Plo \
- ./$(DEPDIR)/sg_pt_haiku.Plo ./$(DEPDIR)/sg_pt_linux.Plo \
- ./$(DEPDIR)/sg_pt_linux_nvme.Plo ./$(DEPDIR)/sg_pt_osf1.Plo \
- ./$(DEPDIR)/sg_pt_solaris.Plo ./$(DEPDIR)/sg_pt_win32.Plo
+ ./$(DEPDIR)/sg_json_builder.Plo ./$(DEPDIR)/sg_lib.Plo \
+ ./$(DEPDIR)/sg_lib_data.Plo ./$(DEPDIR)/sg_lib_names.Plo \
+ ./$(DEPDIR)/sg_pt_common.Plo ./$(DEPDIR)/sg_pt_dummy.Plo \
+ ./$(DEPDIR)/sg_pt_freebsd.Plo ./$(DEPDIR)/sg_pt_haiku.Plo \
+ ./$(DEPDIR)/sg_pt_linux.Plo ./$(DEPDIR)/sg_pt_linux_nvme.Plo \
+ ./$(DEPDIR)/sg_pt_osf1.Plo ./$(DEPDIR)/sg_pt_solaris.Plo \
+ ./$(DEPDIR)/sg_pt_win32.Plo
am__mv = mv -f
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
@@ -376,11 +379,11 @@ top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
libsgutils2_la_SOURCES = sg_lib.c sg_lib_data.c sg_lib_names.c \
sg_cmds_basic.c sg_cmds_basic2.c sg_cmds_extra.c sg_cmds_mmc.c \
- sg_pt_common.c $(am__append_1) $(am__append_2) $(am__append_3) \
- $(am__append_4) $(am__append_5) $(am__append_6) \
- $(am__append_7) $(am__append_8) $(am__append_9) \
- $(am__append_10) $(am__append_11) $(am__append_12) \
- $(am__append_13)
+ sg_pt_common.c sg_json_builder.c $(am__append_1) \
+ $(am__append_2) $(am__append_3) $(am__append_4) \
+ $(am__append_5) $(am__append_6) $(am__append_7) \
+ $(am__append_8) $(am__append_9) $(am__append_10) \
+ $(am__append_11) $(am__append_12) $(am__append_13)
@DEBUG_FALSE@DBG_CFLAGS =
# This is active if --enable-debug given to ./configure
@@ -489,6 +492,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sg_cmds_extra.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sg_cmds_mmc.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sg_io_linux.Plo@am__quote@ # am--include-marker
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sg_json_builder.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sg_lib.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sg_lib_data.Plo@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sg_lib_names.Plo@am__quote@ # am--include-marker
@@ -667,6 +671,7 @@ distclean: distclean-am
-rm -f ./$(DEPDIR)/sg_cmds_extra.Plo
-rm -f ./$(DEPDIR)/sg_cmds_mmc.Plo
-rm -f ./$(DEPDIR)/sg_io_linux.Plo
+ -rm -f ./$(DEPDIR)/sg_json_builder.Plo
-rm -f ./$(DEPDIR)/sg_lib.Plo
-rm -f ./$(DEPDIR)/sg_lib_data.Plo
-rm -f ./$(DEPDIR)/sg_lib_names.Plo
@@ -729,6 +734,7 @@ maintainer-clean: maintainer-clean-am
-rm -f ./$(DEPDIR)/sg_cmds_extra.Plo
-rm -f ./$(DEPDIR)/sg_cmds_mmc.Plo
-rm -f ./$(DEPDIR)/sg_io_linux.Plo
+ -rm -f ./$(DEPDIR)/sg_json_builder.Plo
-rm -f ./$(DEPDIR)/sg_lib.Plo
-rm -f ./$(DEPDIR)/sg_lib_data.Plo
-rm -f ./$(DEPDIR)/sg_lib_names.Plo
diff --git a/lib/sg_json_builder.c b/lib/sg_json_builder.c
new file mode 100644
index 00000000..4d616989
--- /dev/null
+++ b/lib/sg_json_builder.c
@@ -0,0 +1,995 @@
+
+/* vim: set et ts=3 sw=3 sts=3 ft=c:
+ *
+ * Copyright (C) 2014 James McLaughlin. All rights reserved.
+ * https://github.com/udp/json-builder
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "sg_json_builder.h"
+
+#include <string.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#ifdef _MSC_VER
+ #define snprintf _snprintf
+#endif
+
+static const json_serialize_opts default_opts =
+{
+ json_serialize_mode_single_line,
+ 0,
+ 3 /* indent_size */
+};
+
+typedef struct json_builder_value
+{
+ json_value value;
+
+ int is_builder_value;
+
+ size_t additional_length_allocated;
+ size_t length_iterated;
+
+} json_builder_value;
+
+static int builderize (json_value * value)
+{
+ if (((json_builder_value *) value)->is_builder_value)
+ return 1;
+
+ if (value->type == json_object)
+ {
+ unsigned int i;
+
+ /* Values straight out of the parser have the names of object entries
+ * allocated in the same allocation as the values array itself. This is
+ * not desirable when manipulating values because the names would be easy
+ * to clobber.
+ */
+ for (i = 0; i < value->u.object.length; ++ i)
+ {
+ json_char * name_copy;
+ json_object_entry * entry = &value->u.object.values [i];
+
+ if (! (name_copy = (json_char *) malloc ((entry->name_length + 1) * sizeof (json_char))))
+ return 0;
+
+ memcpy (name_copy, entry->name, entry->name_length + 1);
+ entry->name = name_copy;
+ }
+ }
+
+ ((json_builder_value *) value)->is_builder_value = 1;
+
+ return 1;
+}
+
+const size_t json_builder_extra = sizeof(json_builder_value) - sizeof(json_value);
+
+/* These flags are set up from the opts before serializing to make the
+ * serializer conditions simpler.
+ */
+const int f_spaces_around_brackets = (1 << 0);
+const int f_spaces_after_commas = (1 << 1);
+const int f_spaces_after_colons = (1 << 2);
+const int f_tabs = (1 << 3);
+
+static int get_serialize_flags (json_serialize_opts opts)
+{
+ int flags = 0;
+
+ if (opts.mode == json_serialize_mode_packed)
+ return 0;
+
+ if (opts.mode == json_serialize_mode_multiline)
+ {
+ if (opts.opts & json_serialize_opt_use_tabs)
+ flags |= f_tabs;
+ }
+ else
+ {
+ if (! (opts.opts & json_serialize_opt_pack_brackets))
+ flags |= f_spaces_around_brackets;
+
+ if (! (opts.opts & json_serialize_opt_no_space_after_comma))
+ flags |= f_spaces_after_commas;
+ }
+
+ if (! (opts.opts & json_serialize_opt_no_space_after_colon))
+ flags |= f_spaces_after_colons;
+
+ return flags;
+}
+
+json_value * json_array_new (size_t length)
+{
+ json_value * value = (json_value *) calloc (1, sizeof (json_builder_value));
+
+ if (!value)
+ return NULL;
+
+ ((json_builder_value *) value)->is_builder_value = 1;
+
+ value->type = json_array;
+
+ if (! (value->u.array.values = (json_value **) malloc (length * sizeof (json_value *))))
+ {
+ free (value);
+ return NULL;
+ }
+
+ ((json_builder_value *) value)->additional_length_allocated = length;
+
+ return value;
+}
+
+json_value * json_array_push (json_value * array, json_value * value)
+{
+ assert (array->type == json_array);
+
+ if (!builderize (array) || !builderize (value))
+ return NULL;
+
+ if (((json_builder_value *) array)->additional_length_allocated > 0)
+ {
+ -- ((json_builder_value *) array)->additional_length_allocated;
+ }
+ else
+ {
+ json_value ** values_new = (json_value **) realloc
+ (array->u.array.values, sizeof (json_value *) * (array->u.array.length + 1));
+
+ if (!values_new)
+ return NULL;
+
+ array->u.array.values = values_new;
+ }
+
+ array->u.array.values [array->u.array.length] = value;
+ ++ array->u.array.length;
+
+ value->parent = array;
+
+ return value;
+}
+
+json_value * json_object_new (size_t length)
+{
+ json_value * value = (json_value *) calloc (1, sizeof (json_builder_value));
+
+ if (!value)
+ return NULL;
+
+ ((json_builder_value *) value)->is_builder_value = 1;
+
+ value->type = json_object;
+
+ if (! (value->u.object.values = (json_object_entry *) calloc
+ (length, sizeof (*value->u.object.values))))
+ {
+ free (value);
+ return NULL;
+ }
+
+ ((json_builder_value *) value)->additional_length_allocated = length;
+
+ return value;
+}
+
+json_value * json_object_push (json_value * object,
+ const json_char * name,
+ json_value * value)
+{
+ return json_object_push_length (object, strlen (name), name, value);
+}
+
+json_value * json_object_push_length (json_value * object,
+ unsigned int name_length, const json_char * name,
+ json_value * value)
+{
+ json_char * name_copy;
+
+ assert (object->type == json_object);
+
+ if (! (name_copy = (json_char *) malloc ((name_length + 1) * sizeof (json_char))))
+ return NULL;
+
+ memcpy (name_copy, name, name_length * sizeof (json_char));
+ name_copy [name_length] = 0;
+
+ if (!json_object_push_nocopy (object, name_length, name_copy, value))
+ {
+ free (name_copy);
+ return NULL;
+ }
+
+ return value;
+}
+
+json_value * json_object_push_nocopy (json_value * object,
+ unsigned int name_length, json_char * name,
+ json_value * value)
+{
+ json_object_entry * entry;
+
+ assert (object->type == json_object);
+
+ if (!builderize (object) || !builderize (value))
+ return NULL;
+
+ if (((json_builder_value *) object)->additional_length_allocated > 0)
+ {
+ -- ((json_builder_value *) object)->additional_length_allocated;
+ }
+ else
+ {
+ json_object_entry * values_new = (json_object_entry *)
+ realloc (object->u.object.values, sizeof (*object->u.object.values)
+ * (object->u.object.length + 1));
+
+ if (!values_new)
+ return NULL;
+
+ object->u.object.values = values_new;
+ }
+
+ entry = object->u.object.values + object->u.object.length;
+
+ entry->name_length = name_length;
+ entry->name = name;
+ entry->value = value;
+
+ ++ object->u.object.length;
+
+ value->parent = object;
+
+ return value;
+}
+
+json_value * json_string_new (const json_char * buf)
+{
+ return json_string_new_length (strlen (buf), buf);
+}
+
+json_value * json_string_new_length (unsigned int length, const json_char * buf)
+{
+ json_value * value;
+ json_char * copy = (json_char *) malloc ((length + 1) * sizeof (json_char));
+
+ if (!copy)
+ return NULL;
+
+ memcpy (copy, buf, length * sizeof (json_char));
+ copy [length] = 0;
+
+ if (! (value = json_string_new_nocopy (length, copy)))
+ {
+ free (copy);
+ return NULL;
+ }
+
+ return value;
+}
+
+json_value * json_string_new_nocopy (unsigned int length, json_char * buf)
+{
+ json_value * value = (json_value *) calloc (1, sizeof (json_builder_value));
+
+ if (!value)
+ return NULL;
+
+ ((json_builder_value *) value)->is_builder_value = 1;
+
+ value->type = json_string;
+ value->u.string.length = length;
+ value->u.string.ptr = buf;
+
+ return value;
+}
+
+json_value * json_integer_new (json_int_t integer)
+{
+ json_value * value = (json_value *) calloc (1, sizeof (json_builder_value));
+
+ if (!value)
+ return NULL;
+
+ ((json_builder_value *) value)->is_builder_value = 1;
+
+ value->type = json_integer;
+ value->u.integer = integer;
+
+ return value;
+}
+
+json_value * json_double_new (double dbl)
+{
+ json_value * value = (json_value *) calloc (1, sizeof (json_builder_value));
+
+ if (!value)
+ return NULL;
+
+ ((json_builder_value *) value)->is_builder_value = 1;
+
+ value->type = json_double;
+ value->u.dbl = dbl;
+
+ return value;
+}
+
+json_value * json_boolean_new (int b)
+{
+ json_value * value = (json_value *) calloc (1, sizeof (json_builder_value));
+
+ if (!value)
+ return NULL;
+
+ ((json_builder_value *) value)->is_builder_value = 1;
+
+ value->type = json_boolean;
+ value->u.boolean = b;
+
+ return value;
+}
+
+json_value * json_null_new (void)
+{
+ json_value * value = (json_value *) calloc (1, sizeof (json_builder_value));
+
+ if (!value)
+ return NULL;
+
+ ((json_builder_value *) value)->is_builder_value = 1;
+
+ value->type = json_null;
+
+ return value;
+}
+
+void json_object_sort (json_value * object, json_value * proto)
+{
+ unsigned int i, out_index = 0;
+
+ if (!builderize (object))
+ return; /* TODO error */
+
+ assert (object->type == json_object);
+ assert (proto->type == json_object);
+
+ for (i = 0; i < proto->u.object.length; ++ i)
+ {
+ unsigned int j;
+ json_object_entry proto_entry = proto->u.object.values [i];
+
+ for (j = 0; j < object->u.object.length; ++ j)
+ {
+ json_object_entry entry = object->u.object.values [j];
+
+ if (entry.name_length != proto_entry.name_length)
+ continue;
+
+ if (memcmp (entry.name, proto_entry.name, entry.name_length) != 0)
+ continue;
+
+ object->u.object.values [j] = object->u.object.values [out_index];
+ object->u.object.values [out_index] = entry;
+
+ ++ out_index;
+ }
+ }
+}
+
+json_value * json_object_merge (json_value * objectA, json_value * objectB)
+{
+ unsigned int i;
+
+ assert (objectA->type == json_object);
+ assert (objectB->type == json_object);
+ assert (objectA != objectB);
+
+ if (!builderize (objectA) || !builderize (objectB))
+ return NULL;
+
+ if (objectB->u.object.length <=
+ ((json_builder_value *) objectA)->additional_length_allocated)
+ {
+ ((json_builder_value *) objectA)->additional_length_allocated
+ -= objectB->u.object.length;
+ }
+ else
+ {
+ json_object_entry * values_new;
+
+ unsigned int alloc =
+ objectA->u.object.length
+ + ((json_builder_value *) objectA)->additional_length_allocated
+ + objectB->u.object.length;
+
+ if (! (values_new = (json_object_entry *)
+ realloc (objectA->u.object.values, sizeof (json_object_entry) * alloc)))
+ {
+ return NULL;
+ }
+
+ objectA->u.object.values = values_new;
+ }
+
+ for (i = 0; i < objectB->u.object.length; ++ i)
+ {
+ json_object_entry * entry = &objectA->u.object.values[objectA->u.object.length + i];
+
+ *entry = objectB->u.object.values[i];
+ entry->value->parent = objectA;
+ }
+
+ objectA->u.object.length += objectB->u.object.length;
+
+ free (objectB->u.object.values);
+ free (objectB);
+
+ return objectA;
+}
+
+static size_t measure_string (unsigned int length,
+ const json_char * str)
+{
+ unsigned int i;
+ size_t measured_length = 0;
+
+ for(i = 0; i < length; ++ i)
+ {
+ json_char c = str [i];
+
+ switch (c)
+ {
+ case '"':
+ case '\\':
+ case '\b':
+ case '\f':
+ case '\n':
+ case '\r':
+ case '\t':
+
+ measured_length += 2;
+ break;
+
+ default:
+
+ ++ measured_length;
+ break;
+ };
+ };
+
+ return measured_length;
+}
+
+#define PRINT_ESCAPED(c) do { \
+ *buf ++ = '\\'; \
+ *buf ++ = (c); \
+} while(0); \
+
+static size_t serialize_string (json_char * buf,
+ unsigned int length,
+ const json_char * str)
+{
+ json_char * orig_buf = buf;
+ unsigned int i;
+
+ for(i = 0; i < length; ++ i)
+ {
+ json_char c = str [i];
+
+ switch (c)
+ {
+ case '"': PRINT_ESCAPED ('\"'); continue;
+ case '\\': PRINT_ESCAPED ('\\'); continue;
+ case '\b': PRINT_ESCAPED ('b'); continue;
+ case '\f': PRINT_ESCAPED ('f'); continue;
+ case '\n': PRINT_ESCAPED ('n'); continue;
+ case '\r': PRINT_ESCAPED ('r'); continue;
+ case '\t': PRINT_ESCAPED ('t'); continue;
+
+ default:
+
+ *buf ++ = c;
+ break;
+ };
+ };
+
+ return buf - orig_buf;
+}
+
+size_t json_measure (json_value * value)
+{
+ return json_measure_ex (value, default_opts);
+}
+
+#define MEASURE_NEWLINE() do { \
+ ++ newlines; \
+ indents += depth; \
+} while(0); \
+
+size_t json_measure_ex (json_value * value, json_serialize_opts opts)
+{
+ size_t total = 1; /* null terminator */
+ size_t newlines = 0;
+ size_t depth = 0;
+ size_t indents = 0;
+ int flags;
+ int bracket_size, comma_size, colon_size;
+
+ flags = get_serialize_flags (opts);
+
+ /* to reduce branching
+ */
+ bracket_size = flags & f_spaces_around_brackets ? 2 : 1;
+ comma_size = flags & f_spaces_after_commas ? 2 : 1;
+ colon_size = flags & f_spaces_after_colons ? 2 : 1;
+
+ while (value)
+ {
+ json_int_t integer;
+ json_object_entry * entry;
+
+ switch (value->type)
+ {
+ case json_array:
+
+ if (((json_builder_value *) value)->length_iterated == 0)
+ {
+ if (value->u.array.length == 0)
+ {
+ total += 2; /* `[]` */
+ break;
+ }
+
+ total += bracket_size; /* `[` */
+
+ ++ depth;
+ MEASURE_NEWLINE(); /* \n after [ */
+ }
+
+ if (((json_builder_value *) value)->length_iterated == value->u.array.length)
+ {
+ -- depth;
+ MEASURE_NEWLINE();
+ total += bracket_size; /* `]` */
+
+ ((json_builder_value *) value)->length_iterated = 0;
+ break;
+ }
+
+ if (((json_builder_value *) value)->length_iterated > 0)
+ {
+ total += comma_size; /* `, ` */
+
+ MEASURE_NEWLINE();
+ }
+
+ ((json_builder_value *) value)->length_iterated++;
+ value = value->u.array.values [((json_builder_value *) value)->length_iterated - 1];
+ continue;
+
+ case json_object:
+
+ if (((json_builder_value *) value)->length_iterated == 0)
+ {
+ if (value->u.object.length == 0)
+ {
+ total += 2; /* `{}` */
+ break;
+ }
+
+ total += bracket_size; /* `{` */
+
+ ++ depth;
+ MEASURE_NEWLINE(); /* \n after { */
+ }
+
+ if (((json_builder_value *) value)->length_iterated == value->u.object.length)
+ {
+ -- depth;
+ MEASURE_NEWLINE();
+ total += bracket_size; /* `}` */
+
+ ((json_builder_value *) value)->length_iterated = 0;
+ break;
+ }
+
+ if (((json_builder_value *) value)->length_iterated > 0)
+ {
+ total += comma_size; /* `, ` */
+ MEASURE_NEWLINE();
+ }
+
+ entry = value->u.object.values + (((json_builder_value *) value)->length_iterated ++);
+
+ total += 2 + colon_size; /* `"": ` */
+ total += measure_string (entry->name_length, entry->name);
+
+ value = entry->value;
+ continue;
+
+ case json_string:
+
+ total += 2; /* `""` */
+ total += measure_string (value->u.string.length, value->u.string.ptr);
+ break;
+
+ case json_integer:
+
+ integer = value->u.integer;
+
+ if (integer < 0)
+ {
+ total += 1; /* `-` */
+ integer = - integer;
+ }
+
+ ++ total; /* first digit */
+
+ while (integer >= 10)
+ {
+ ++ total; /* another digit */
+ integer /= 10;
+ }
+
+ break;
+
+ case json_double:
+
+ total += snprintf (NULL, 0, "%g", value->u.dbl);
+
+ /* Because sometimes we need to add ".0" if sprintf does not do it
+ * for us. Downside is that we allocate more bytes than strictly
+ * needed for serialization.
+ */
+ total += 2;
+
+ break;
+
+ case json_boolean:
+
+ total += value->u.boolean ?
+ 4: /* `true` */
+ 5; /* `false` */
+
+ break;
+
+ case json_null:
+
+ total += 4; /* `null` */
+ break;
+
+ default:
+ break;
+ };
+
+ value = value->parent;
+ }
+
+ if (opts.mode == json_serialize_mode_multiline)
+ {
+ total += newlines * (((opts.opts & json_serialize_opt_CRLF) ? 2 : 1) + opts.indent_size);
+ total += indents * opts.indent_size;
+ }
+
+ return total;
+}
+
+void json_serialize (json_char * buf, json_value * value)
+{
+ json_serialize_ex (buf, value, default_opts);
+}
+
+#define PRINT_NEWLINE() do { \
+ if (opts.mode == json_serialize_mode_multiline) { \
+ if (opts.opts & json_serialize_opt_CRLF) \
+ *buf ++ = '\r'; \
+ *buf ++ = '\n'; \
+ for(i = 0; i < indent; ++ i) \
+ *buf ++ = indent_char; \
+ } \
+} while(0); \
+
+#define PRINT_OPENING_BRACKET(c) do { \
+ *buf ++ = (c); \
+ if (flags & f_spaces_around_brackets) \
+ *buf ++ = ' '; \
+} while(0); \
+
+#define PRINT_CLOSING_BRACKET(c) do { \
+ if (flags & f_spaces_around_brackets) \
+ *buf ++ = ' '; \
+ *buf ++ = (c); \
+} while(0); \
+
+void json_serialize_ex (json_char * buf, json_value * value, json_serialize_opts opts)
+{
+ json_int_t integer, orig_integer;
+ json_object_entry * entry;
+ json_char * ptr, * dot;
+ int indent = 0;
+ char indent_char;
+ int i;
+ int flags;
+
+ flags = get_serialize_flags (opts);
+
+ indent_char = flags & f_tabs ? '\t' : ' ';
+
+ while (value)
+ {
+ switch (value->type)
+ {
+ case json_array:
+
+ if (((json_builder_value *) value)->length_iterated == 0)
+ {
+ if (value->u.array.length == 0)
+ {
+ *buf ++ = '[';
+ *buf ++ = ']';
+
+ break;
+ }
+
+ PRINT_OPENING_BRACKET ('[');
+
+ indent += opts.indent_size;
+ PRINT_NEWLINE();
+ }
+
+ if (((json_builder_value *) value)->length_iterated == value->u.array.length)
+ {
+ indent -= opts.indent_size;
+ PRINT_NEWLINE();
+ PRINT_CLOSING_BRACKET (']');
+
+ ((json_builder_value *) value)->length_iterated = 0;
+ break;
+ }
+
+ if (((json_builder_value *) value)->length_iterated > 0)
+ {
+ *buf ++ = ',';
+
+ if (flags & f_spaces_after_commas)
+ *buf ++ = ' ';
+
+ PRINT_NEWLINE();
+ }
+
+ ((json_builder_value *) value)->length_iterated++;
+ value = value->u.array.values [((json_builder_value *) value)->length_iterated - 1];
+ continue;
+
+ case json_object:
+
+ if (((json_builder_value *) value)->length_iterated == 0)
+ {
+ if (value->u.object.length == 0)
+ {
+ *buf ++ = '{';
+ *buf ++ = '}';
+
+ break;
+ }
+
+ PRINT_OPENING_BRACKET ('{');
+
+ indent += opts.indent_size;
+ PRINT_NEWLINE();
+ }
+
+ if (((json_builder_value *) value)->length_iterated == value->u.object.length)
+ {
+ indent -= opts.indent_size;
+ PRINT_NEWLINE();
+ PRINT_CLOSING_BRACKET ('}');
+
+ ((json_builder_value *) value)->length_iterated = 0;
+ break;
+ }
+
+ if (((json_builder_value *) value)->length_iterated > 0)
+ {
+ *buf ++ = ',';
+
+ if (flags & f_spaces_after_commas)
+ *buf ++ = ' ';
+
+ PRINT_NEWLINE();
+ }
+
+ entry = value->u.object.values + (((json_builder_value *) value)->length_iterated ++);
+
+ *buf ++ = '\"';
+ buf += serialize_string (buf, entry->name_length, entry->name);
+ *buf ++ = '\"';
+ *buf ++ = ':';
+
+ if (flags & f_spaces_after_colons)
+ *buf ++ = ' ';
+
+ value = entry->value;
+ continue;
+
+ case json_string:
+
+ *buf ++ = '\"';
+ buf += serialize_string (buf, value->u.string.length, value->u.string.ptr);
+ *buf ++ = '\"';
+ break;
+
+ case json_integer:
+
+ integer = value->u.integer;
+
+ if (integer < 0)
+ {
+ *buf ++ = '-';
+ integer = - integer;
+ }
+
+ orig_integer = integer;
+
+ ++ buf;
+
+ while (integer >= 10)
+ {
+ ++ buf;
+ integer /= 10;
+ }
+
+ integer = orig_integer;
+ ptr = buf;
+
+ do
+ {
+ *-- ptr = "0123456789"[integer % 10];
+
+ } while ((integer /= 10) > 0);
+
+ break;
+
+ case json_double:
+
+ ptr = buf;
+
+ buf += sprintf (buf, "%g", value->u.dbl);
+
+ if ((dot = strchr (ptr, ',')))
+ {
+ *dot = '.';
+ }
+ else if (!strchr (ptr, '.') && !strchr (ptr, 'e'))
+ {
+ *buf ++ = '.';
+ *buf ++ = '0';
+ }
+
+ break;
+
+ case json_boolean:
+
+ if (value->u.boolean)
+ {
+ memcpy (buf, "true", 4);
+ buf += 4;
+ }
+ else
+ {
+ memcpy (buf, "false", 5);
+ buf += 5;
+ }
+
+ break;
+
+ case json_null:
+
+ memcpy (buf, "null", 4);
+ buf += 4;
+ break;
+
+ default:
+ break;
+ };
+
+ value = value->parent;
+ }
+
+ *buf = 0;
+}
+
+void json_builder_free (json_value * value)
+{
+ json_value * cur_value;
+
+ if (!value)
+ return;
+
+ value->parent = 0;
+
+ while (value)
+ {
+ switch (value->type)
+ {
+ case json_array:
+
+ if (!value->u.array.length)
+ {
+ free (value->u.array.values);
+ break;
+ }
+
+ value = value->u.array.values [-- value->u.array.length];
+ continue;
+
+ case json_object:
+
+ if (!value->u.object.length)
+ {
+ free (value->u.object.values);
+ break;
+ }
+
+ -- value->u.object.length;
+
+ if (((json_builder_value *) value)->is_builder_value)
+ {
+ /* Names are allocated separately for builder values. In parser
+ * values, they are part of the same allocation as the values array
+ * itself.
+ */
+ free (value->u.object.values [value->u.object.length].name);
+ }
+
+ value = value->u.object.values [value->u.object.length].value;
+ continue;
+
+ case json_string:
+
+ free (value->u.string.ptr);
+ break;
+
+ default:
+ break;
+ };
+
+ cur_value = value;
+ value = value->parent;
+ free (cur_value);
+ }
+}
+
+
+
+
+
+
diff --git a/lib/sg_json_builder.h b/lib/sg_json_builder.h
new file mode 100644
index 00000000..9d83b026
--- /dev/null
+++ b/lib/sg_json_builder.h
@@ -0,0 +1,324 @@
+
+/* vim: set et ts=3 sw=3 sts=3 ft=c:
+ *
+ * Copyright (C) 2014 James McLaughlin. All rights reserved.
+ * https://github.com/udp/json-builder
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _JSON_BUILDER_H
+#define _JSON_BUILDER_H
+
+/*
+ * Used to require json.h from json-parser but what was need as been
+ * included in this header.
+ * https://github.com/udp/json-parser
+ */
+/* #include "json.h" */
+
+#ifndef json_char
+ #define json_char char
+#endif
+
+#ifndef json_int_t
+ #undef JSON_INT_T_OVERRIDDEN
+ #if defined(_MSC_VER)
+ #define json_int_t __int64
+ #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__cplusplus) && __cplusplus >= 201103L)
+ /* C99 and C++11 */
+ #include <stdint.h>
+ #define json_int_t int_fast64_t
+ #else
+ /* C89 */
+ #define json_int_t long
+ #endif
+#else
+ #define JSON_INT_T_OVERRIDDEN 1
+#endif
+
+#include <stddef.h>
+
+#ifdef __cplusplus
+
+ #include <string.h>
+
+ extern "C"
+ {
+
+#endif
+
+typedef struct
+{
+ unsigned long max_memory; /* should be size_t, but would modify the API */
+ int settings;
+
+ /* Custom allocator support (leave null to use malloc/free)
+ */
+
+ void * (* mem_alloc) (size_t, int zero, void * user_data);
+ void (* mem_free) (void *, void * user_data);
+
+ void * user_data; /* will be passed to mem_alloc and mem_free */
+
+ size_t value_extra; /* how much extra space to allocate for values? */
+
+} json_settings;
+
+#define json_enable_comments 0x01
+
+typedef enum
+{
+ json_none,
+ json_object,
+ json_array,
+ json_integer,
+ json_double,
+ json_string,
+ json_boolean,
+ json_null
+
+} json_type;
+
+extern const struct _json_value json_value_none;
+
+typedef struct _json_object_entry
+{
+ json_char * name;
+ unsigned int name_length;
+
+ struct _json_value * value;
+
+} json_object_entry;
+
+typedef struct _json_value
+{
+ struct _json_value * parent;
+
+ json_type type;
+
+ union
+ {
+ int boolean;
+ json_int_t integer;
+ double dbl;
+
+ struct
+ {
+ unsigned int length;
+ json_char * ptr; /* null terminated */
+
+ } string;
+
+ struct
+ {
+ unsigned int length;
+
+ json_object_entry * values;
+
+ #if defined(__cplusplus)
+ json_object_entry * begin () const
+ { return values;
+ }
+ json_object_entry * end () const
+ { return values + length;
+ }
+ #endif
+
+ } object;
+
+ struct
+ {
+ unsigned int length;
+ struct _json_value ** values;
+
+ #if defined(__cplusplus)
+ _json_value ** begin () const
+ { return values;
+ }
+ _json_value ** end () const
+ { return values + length;
+ }
+ #endif
+
+ } array;
+
+ } u;
+
+ union
+ {
+ struct _json_value * next_alloc;
+ void * object_mem;
+
+ } _reserved;
+
+ #ifdef JSON_TRACK_SOURCE
+
+ /* Location of the value in the source JSON
+ */
+ unsigned int line, col;
+
+ #endif
+
+
+ /* C++ operator sugar removed */
+
+} json_value;
+
+#if 0
+#define json_error_max 128
+json_value * json_parse_ex (json_settings * settings,
+ const json_char * json,
+ size_t length,
+ char * error);
+
+void json_value_free (json_value *);
+
+
+/* Not usually necessary, unless you used a custom mem_alloc and now want to
+ * use a custom mem_free.
+ */
+void json_value_free_ex (json_settings * settings,
+ json_value *);
+#endif
+
+/* <<< end of code from json-parser's json.h >>> */
+
+
+/* IMPORTANT NOTE: If you want to use json-builder functions with values
+ * allocated by json-parser as part of the parsing process, you must pass
+ * json_builder_extra as the value_extra setting in json_settings when
+ * parsing. Otherwise there will not be room for the extra state and
+ * json-builder WILL invoke undefined behaviour.
+ *
+ * Also note that unlike json-parser, json-builder does not currently support
+ * custom allocators (for no particular reason other than that it doesn't have
+ * any settings or global state.)
+ */
+extern const size_t json_builder_extra;
+
+
+/*** Arrays
+ ***
+ * Note that all of these length arguments are just a hint to allow for
+ * pre-allocation - passing 0 is fine.
+ */
+json_value * json_array_new (size_t length);
+json_value * json_array_push (json_value * array, json_value *);
+
+
+/*** Objects
+ ***/
+json_value * json_object_new (size_t length);
+
+json_value * json_object_push (json_value * object,
+ const json_char * name,
+ json_value *);
+
+/* Same as json_object_push, but doesn't call strlen() for you.
+ */
+json_value * json_object_push_length (json_value * object,
+ unsigned int name_length, const json_char * name,
+ json_value *);
+
+/* Same as json_object_push_length, but doesn't copy the name buffer before
+ * storing it in the value. Use this micro-optimisation at your own risk.
+ */
+json_value * json_object_push_nocopy (json_value * object,
+ unsigned int name_length, json_char * name,
+ json_value *);
+
+/* Merges all entries from objectB into objectA and destroys objectB.
+ */
+json_value * json_object_merge (json_value * objectA, json_value * objectB);
+
+/* Sort the entries of an object based on the order in a prototype object.
+ * Helpful when reading JSON and writing it again to preserve user order.
+ */
+void json_object_sort (json_value * object, json_value * proto);
+
+
+
+/*** Strings
+ ***/
+json_value * json_string_new (const json_char *);
+json_value * json_string_new_length (unsigned int length, const json_char *);
+json_value * json_string_new_nocopy (unsigned int length, json_char *);
+
+
+/*** Everything else
+ ***/
+json_value * json_integer_new (json_int_t);
+json_value * json_double_new (double);
+json_value * json_boolean_new (int);
+json_value * json_null_new (void);
+
+
+/*** Serializing
+ ***/
+#define json_serialize_mode_multiline 0
+#define json_serialize_mode_single_line 1
+#define json_serialize_mode_packed 2
+
+#define json_serialize_opt_CRLF (1 << 1)
+#define json_serialize_opt_pack_brackets (1 << 2)
+#define json_serialize_opt_no_space_after_comma (1 << 3)
+#define json_serialize_opt_no_space_after_colon (1 << 4)
+#define json_serialize_opt_use_tabs (1 << 5)
+
+typedef struct json_serialize_opts
+{
+ int mode;
+ int opts;
+ int indent_size;
+
+} json_serialize_opts;
+
+
+/* Returns a length in characters that is at least large enough to hold the
+ * value in its serialized form, including a null terminator.
+ */
+size_t json_measure (json_value *);
+size_t json_measure_ex (json_value *, json_serialize_opts);
+
+
+/* Serializes a JSON value into the buffer given (which must already be
+ * allocated with a length of at least json_measure(value, opts))
+ */
+void json_serialize (json_char * buf, json_value *);
+void json_serialize_ex (json_char * buf, json_value *, json_serialize_opts);
+
+
+/*** Cleaning up
+ ***/
+void json_builder_free (json_value *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+
+
diff --git a/lib/sg_lib.c b/lib/sg_lib.c
index c4fe1d7d..36af6e6f 100644
--- a/lib/sg_lib.c
+++ b/lib/sg_lib.c
@@ -3049,6 +3049,24 @@ hex2str(const uint8_t * b_str, int len, const char * leadin, int format,
return dStrHexStr((const char *)b_str, len, leadin, format, b_len, b);
}
+void
+hex2fp(const uint8_t * b_str, int len, const char * leadin, int format,
+ FILE * fp)
+{
+ int k, num;
+ char b[800]; /* allow for 4 lines of 16 bytes (in hex) each */
+
+ if (leadin && (strlen(leadin) > 118)) {
+ fprintf(fp, ">>> leadin parameter is too large\n");
+ return;
+ }
+ for (k = 0; k < len; k += num) {
+ num = ((k + 64) < len) ? 64 : (len - k);
+ hex2str(b_str + k, num, leadin, format, sizeof(b), b);
+ fprintf(fp, "%s", b);
+ }
+}
+
/* Returns true when executed on big endian machine; else returns false.
* Useful for displaying ATA identify words (which need swapping on a
* big endian machine). */
diff --git a/src/sg_logs.c b/src/sg_logs.c
index 9c44d6be..051ea7fc 100644
--- a/src/sg_logs.c
+++ b/src/sg_logs.c
@@ -37,7 +37,7 @@
#include "sg_unaligned.h"
#include "sg_pr2serr.h"
-static const char * version_str = "1.97 20220309"; /* spc6r06 + sbc5r01 */
+static const char * version_str = "1.98 20220310"; /* spc6r06 + sbc5r01 */
#define MX_ALLOC_LEN (0xfffc)
#define MX_INLEN_ALLOC_LEN (0x800000)
@@ -1575,7 +1575,7 @@ show_supported_pgs_page(const uint8_t * resp, int len,
int pg_code = bp[k];
const struct log_elem * lep;
- snprintf(b, sizeof(b) - 1, " 0x%02x ", pg_code);
+ snprintf(b, sizeof(b) - 1, " 0x%02x ", pg_code);
lep = pg_subpg_pdt_search(pg_code, 0, op->dev_pdt, -1);
if (lep) {
if (op->do_brief > 1)
@@ -1622,9 +1622,9 @@ show_supported_pgs_sub_page(const uint8_t * resp, int len,
/* formerly ignored [pg, 0xff] when pg > 0, don't know why */
if (NOT_SPG_SUBPG == subpg_code)
- snprintf(b, sizeof(b) - 1, " 0x%02x ", pg_code);
+ snprintf(b, sizeof(b) - 1, " 0x%02x ", pg_code);
else
- snprintf(b, sizeof(b) - 1, " 0x%02x,0x%02x ", pg_code,
+ snprintf(b, sizeof(b) - 1, " 0x%02x,0x%02x ", pg_code,
subpg_code);
if ((pg_code > 0) && (subpg_code == 0xff))
printf("%s\n", b);
@@ -3196,13 +3196,13 @@ show_app_client_page(const uint8_t * resp, int len, const struct opts_t * op)
if (op->verbose || ((! op->do_raw) && (op->do_hex == 0)))
printf("Application client page [0xf]\n");
if (0 == op->filter_given) {
- if ((len > 128) && (0 == op->do_hex)) {
- hex2stdout(resp, 64, op->dstrhex_no_ascii);
- printf(" ..... [truncated after 64 of %d bytes (use '-H' to "
+ if ((len > 128) && (0 == op->do_hex) && (0 == op->undefined_hex)) {
+ hex2fp(resp, 64, " ", 0 != op->dstrhex_no_ascii, stdout);
+ printf(" ..... [truncated after 64 of %d bytes (use '-H' to "
"see the rest)]\n", len);
}
else
- hex2stdout(resp, len, op->dstrhex_no_ascii);
+ hex2fp(resp, len, " ", 0 != op->dstrhex_no_ascii, stdout);
return true;
}
/* only here if filter_given set */
@@ -4329,8 +4329,7 @@ show_format_status_page(const uint8_t * resp, int len,
default:
printf(" Unknown Format parameter code = 0x%x\n", pc);
is_count = false;
- hex2str(bp, pl, " ", 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, pl, " ", 0 != op->dstrhex_no_ascii, stdout);
break;
}
if (is_count) {
@@ -4785,9 +4784,7 @@ show_dt_device_status_page(const uint8_t * resp, int len,
pl);
break;
}
- hex2str(bp + 4, 8, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp + 4, 8, " ", 0 != op->dstrhex_no_ascii, stdout);
break;
case 0x3:
printf(" Key management error data (hex only now):\n");
@@ -4800,9 +4797,7 @@ show_dt_device_status_page(const uint8_t * resp, int len,
pl);
break;
}
- hex2str(bp + 4, 12, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp + 4, 12, " ", 0 != op->dstrhex_no_ascii, stdout);
break;
default:
if ((pc >= 0x101) && (pc <= 0x1ff)) {
@@ -4819,9 +4814,8 @@ show_dt_device_status_page(const uint8_t * resp, int len,
sg_get_unaligned_be64(bp + 8));
} else {
printf(" non-SAS transport, in hex:\n");
- hex2str(bp + 4, ((pl < num) ? pl : num) - 4, " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp + 4, ((pl < num) ? pl : num) - 4, " ",
+ 0 != op->dstrhex_no_ascii, stdout);
}
} else if (pc >= 0x8000) {
if (op->exclude_vendor) {
@@ -4833,15 +4827,13 @@ show_dt_device_status_page(const uint8_t * resp, int len,
}
} else {
printf(" Vendor specific [parameter_code=0x%x]:\n", pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
}
} else {
printf(" Reserved [parameter_code=0x%x]:\n", pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
}
break;
}
@@ -4866,7 +4858,6 @@ show_tapealert_response_page(const uint8_t * resp, int len,
int num, pl, pc, k, mod, div;
const uint8_t * bp;
char str[PCB_STR_LEN];
- char b[512];
if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
printf("TapeAlert response page (ssc-3, adc-3) [0x12]\n");
@@ -4907,9 +4898,8 @@ show_tapealert_response_page(const uint8_t * resp, int len,
default:
if (pc <= 0x8000) {
printf(" Reserved [parameter_code=0x%x]:\n", pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
} else {
if (op->exclude_vendor) {
if ((op->verbose > 0) && (0 == op->do_brief) &&
@@ -4920,9 +4910,8 @@ show_tapealert_response_page(const uint8_t * resp, int len,
}
} else {
printf(" Vendor specific [parameter_code=0x%x]:\n", pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
}
}
break;
@@ -4971,7 +4960,6 @@ show_requested_recovery_page(const uint8_t * resp, int len,
int num, pl, pc, j, k;
const uint8_t * bp;
char str[PCB_STR_LEN];
- char b[512];
if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
printf("Requested recovery page (ssc-3) [0x13]\n");
@@ -5007,9 +4995,8 @@ show_requested_recovery_page(const uint8_t * resp, int len,
default:
if (pc <= 0x8000) {
printf(" Reserved [parameter_code=0x%x]:\n", pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
} else {
if (op->exclude_vendor) {
if ((op->verbose > 0) && (0 == op->do_brief) &&
@@ -5020,9 +5007,8 @@ show_requested_recovery_page(const uint8_t * resp, int len,
}
} else {
printf(" Vendor specific [parameter_code=0x%x]:\n", pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
}
}
break;
@@ -5048,7 +5034,6 @@ show_ata_pt_results_page(const uint8_t * resp, int len,
const uint8_t * bp;
const uint8_t * dp;
char str[PCB_STR_LEN];
- char b[512];
if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
printf("ATA pass-through results page (sat-2) [0x16]\n");
@@ -5086,15 +5071,13 @@ show_ata_pt_results_page(const uint8_t * resp, int len,
printf(" device=0x%x status=0x%x\n", dp[12], dp[13]);
} else if (pl > 17) {
printf(" Reserved [parameter_code=0x%x]:\n", pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
} else {
printf(" short parameter length: %d [parameter_code=0x%x]:\n",
pl, pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
}
if (op->do_pcb)
printf(" <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
@@ -5145,7 +5128,6 @@ show_background_scan_results_page(const uint8_t * resp, int len,
int j, m, num, pl, pc;
const uint8_t * bp;
char str[PCB_STR_LEN];
- char b[512];
if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
printf("Background scan results page [0x15]\n");
@@ -5223,11 +5205,9 @@ show_background_scan_results_page(const uint8_t * resp, int len,
"reserved\n", pc, pc);
if (skip_out)
skip_out = false;
- else {
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
- }
+ else
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
break;
} else
printf(" Medium scan parameter # %d [0x%x]\n", pc, pc);
@@ -5291,7 +5271,6 @@ show_zoned_block_dev_stats(const uint8_t * resp, int len,
int num, pl, pc;
const uint8_t * bp;
char str[PCB_STR_LEN];
- char b[512];
if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
printf("Zoned block device statistics page [0x14,0x1]\n");
@@ -5438,9 +5417,8 @@ show_zoned_block_dev_stats(const uint8_t * resp, int len,
break;
default:
printf(" Reserved [parameter_code=0x%x]:\n", pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
break;
}
if (trunc)
@@ -5545,7 +5523,6 @@ show_background_op_page(const uint8_t * resp, int len,
int num, pl, pc;
const uint8_t * bp;
char str[PCB_STR_LEN];
- char b[512];
if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
printf("Background operation page [0x15,0x2]\n");
@@ -5581,9 +5558,8 @@ show_background_op_page(const uint8_t * resp, int len,
break;
default:
printf(" Reserved [parameter_code=0x%x]:\n", pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
break;
}
if (op->do_pcb)
@@ -5607,7 +5583,6 @@ show_lps_misalignment_page(const uint8_t * resp, int len,
int num, pl, pc;
const uint8_t * bp;
char str[PCB_STR_LEN];
- char b[512];
if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
printf("LPS misalignment page [0x15,0x3]\n");
@@ -5647,9 +5622,8 @@ show_lps_misalignment_page(const uint8_t * resp, int len,
pc, bp[4]);
} else {
printf("<unexpected pc=0x%x>\n", pc);
- hex2str(bp, ((pl < num) ? pl : num), " ",
- 0 != op->dstrhex_no_ascii, sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp, ((pl < num) ? pl : num), " ",
+ 0 != op->dstrhex_no_ascii, stdout);
}
break;
}
@@ -5674,7 +5648,6 @@ show_service_buffer_info_page(const uint8_t * resp, int len,
int num, pl, pc;
const uint8_t * bp;
char str[PCB_STR_LEN];
- char b[512];
if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
printf("Service buffer information page (adc-3) [0x15]\n");
@@ -5705,9 +5678,7 @@ show_service_buffer_info_page(const uint8_t * resp, int len,
} else if (pc < 0x8000) {
printf(" parameter_code=0x%x, Reserved, parameter in hex:\n",
pc);
- hex2str(bp + 4, pl - 4, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp + 4, pl - 4, " ", 0 != op->dstrhex_no_ascii, stdout);
} else {
if (op->exclude_vendor) {
if ((op->verbose > 0) && (0 == op->do_brief) &&
@@ -5719,9 +5690,8 @@ show_service_buffer_info_page(const uint8_t * resp, int len,
} else {
printf(" parameter_code=0x%x, Vendor-specific, parameter in "
"hex:\n", pc);
- hex2str(bp + 4, pl - 4, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp + 4, pl - 4, " ", 0 != op->dstrhex_no_ascii,
+ stdout);
}
}
if (op->do_pcb)
@@ -5869,7 +5839,6 @@ show_device_stats_page(const uint8_t * resp, int len,
int num, pl, pc;
const uint8_t * bp;
char str[PCB_STR_LEN];
- char b[512];
if (op->verbose || ((! op->do_raw) && (0 == op->do_hex)))
printf("Device statistics page (ssc-3 and adc)\n");
@@ -6001,9 +5970,8 @@ show_device_stats_page(const uint8_t * resp, int len,
default:
vl_num = false;
printf(" Reserved parameter code [0x%x] data in hex:\n", pc);
- hex2str(bp + 4, pl - 4, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp + 4, pl - 4, " ", 0 != op->dstrhex_no_ascii,
+ stdout);
break;
}
if (vl_num)
@@ -6034,9 +6002,8 @@ show_device_stats_page(const uint8_t * resp, int len,
"hex:\n", pc);
} else {
printf(" Reserved parameter [0x%x], dump in hex:\n", pc);
- hex2str(bp + 4, pl - 4, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp + 4, pl - 4, " ", 0 != op->dstrhex_no_ascii,
+ stdout);
}
break;
}
@@ -6405,16 +6372,12 @@ show_mchanger_diag_data_page(const uint8_t * resp, int len,
printf(" Destination address: 0x%x\n", v);
if (pl > 91) {
printf(" Volume tag information:\n");
- hex2str(bp + 56, 36, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp + 56, 36, " ", 0 != op->dstrhex_no_ascii, stdout);
}
if (pl > 99) {
printf(" Timestamp origin: 0x%x\n", bp[92] & 0xf);
printf(" Timestamp:\n");
- hex2str(bp + 94, 6, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
+ hex2fp(bp + 94, 6, " ", 0 != op->dstrhex_no_ascii, stdout);
}
if (op->do_pcb)
printf(" <%s>\n", get_pcb_str(bp[2], str, sizeof(str)));
@@ -6733,11 +6696,9 @@ show_volume_stats_pages(const uint8_t * resp, int len,
pc);
if (skip_out)
skip_out = false;
- else {
- hex2str(bp + 4, pl - 4, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
- }
+ else
+ hex2fp(bp + 4, pl - 4, " ", 0 != op->dstrhex_no_ascii,
+ stdout);
break;
}
if (op->do_pcb)
@@ -7078,7 +7039,6 @@ decode_page_contents(const uint8_t * resp, int len, struct opts_t * op)
bool spf;
bool done = false;
const struct log_elem * lep;
- char b[512];
if (len < 3) {
pr2serr("%s: response has bad length: %d\n", __func__, len);
@@ -7114,17 +7074,13 @@ decode_page_contents(const uint8_t * resp, int len, struct opts_t * op)
printf("%s%x, here is hex:\n", unable_s, pg_code);
}
if ((len > 128) && (0 == op->do_hex)) {
- hex2str(resp, 64, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
- printf(" ..... [truncated after 64 of %d bytes (use '-H' to "
+ hex2fp(resp, 64, " ", 0 != op->dstrhex_no_ascii, stdout);
+ printf(" ..... [truncated after 64 of %d bytes (use '-H' to "
"see the rest)]\n", len);
} else {
- if (0 == op->do_hex) {
- hex2str(resp, len, " ", 0 != op->dstrhex_no_ascii,
- sizeof(b), b);
- printf("%s", b);
- } else
+ if (0 == op->do_hex)
+ hex2fp(resp, len, " ", 0 != op->dstrhex_no_ascii, stdout);
+ else
hex2stdout(resp, len, op->dstrhex_no_ascii);
}
}
diff --git a/src/sg_opcodes.c b/src/sg_opcodes.c
index 944a3e76..55530511 100644
--- a/src/sg_opcodes.c
+++ b/src/sg_opcodes.c
@@ -33,7 +33,7 @@
#include "sg_pt.h"
-static const char * version_str = "0.73 20220127"; /* spc6r06 */
+static const char * version_str = "0.74 20220312"; /* spc6r06 */
#define SENSE_BUFF_LEN 64 /* Arbitrary, could be larger */
diff --git a/src/sg_rep_zones.c b/src/sg_rep_zones.c
index 1eca3a8d..93001b50 100644
--- a/src/sg_rep_zones.c
+++ b/src/sg_rep_zones.c
@@ -37,10 +37,10 @@
*
* This program issues the SCSI REPORT ZONES, REPORT ZONE DOMAINS or REPORT
* REALMS command to the given SCSI device and decodes the response.
- * Based on zbc2r10.pdf
+ * Based on zbc2r12.pdf
*/
-static const char * version_str = "1.33 20220224";
+static const char * version_str = "1.34 20220416";
#define WILD_RZONES_BUFF_LEN (1 << 28)
#define MAX_RZONES_BUFF_LEN (2 * 1024 * 1024)
@@ -117,6 +117,7 @@ static struct option long_options[] = {
{0, 0, 0, 0},
};
+/* Zone types */
static struct zt_num2abbrev_t zt_num2abbrev[] = {
{0, "none"},
{1, "c"}, /* conventionial */
@@ -157,11 +158,11 @@ usage(int h)
"[--hex]\n"
" [--inhex=FN] [--locator=LBA] "
"[--maxlen=LEN]\n"
- " [--partial] [--only] [--raw] "
+ " [--num=NUM] [--partial] [--raw] "
"[--readonly]\n"
" [--realm] [--report=OPT] [--start=LBA] "
- "[--verbose]\n"
- " [--version] DEVICE\n");
+ "[--statistics]\n"
+ " [--verbose] [--version] [--wp] DEVICE\n");
pr2serr(" where:\n"
" --domain|-d sends a REPORT ZONE DOMAINS command\n"
" --find=ZT|-F ZT find first zone with ZT zone type, "
@@ -183,8 +184,6 @@ usage(int h)
" (def: 0 -> 8192 bytes)\n"
" --num=NUM|-n NUM number of zones to output (def: 0 -> "
"all)\n"
- " --only|-o output header and starting LBA of "
- "next\n"
" --partial|-p sets PARTIAL bit in cdb (def: 0 -> "
"zone list\n"
" length not altered by allocation length "
diff --git a/src/sg_rtpg.c b/src/sg_rtpg.c
index 48ee4a02..d83bf363 100644
--- a/src/sg_rtpg.c
+++ b/src/sg_rtpg.c
@@ -153,11 +153,11 @@ main(int argc, char * argv[])
bool extended = false;
bool verbose_given = false;
bool version_given = false;
- int k, j, off, res, c, report_len, tgt_port_count;
+ int k, j, off, res, c, report_len, buff_len, tgt_port_count;
int sg_fd = -1;
int ret = 0;
int verbose = 0;
- uint8_t reportTgtGrpBuff[REPORT_TGT_GRP_BUFF_LEN];
+ uint8_t * reportTgtGrpBuff = NULL;
uint8_t * bp;
const char * device_name = NULL;
@@ -255,20 +255,26 @@ main(int argc, char * argv[])
goto err_out;
}
- memset(reportTgtGrpBuff, 0x0, sizeof(reportTgtGrpBuff));
- /* trunc = 0; */
+ buff_len = REPORT_TGT_GRP_BUFF_LEN;
+
+retry:
+ reportTgtGrpBuff = (uint8_t *)malloc(buff_len);
+ if (NULL == reportTgtGrpBuff) {
+ pr2serr(" Out of memory (ram)\n");
+ goto err_out;
+ }
+ memset(reportTgtGrpBuff, 0x0, buff_len);
res = sg_ll_report_tgt_prt_grp2(sg_fd, reportTgtGrpBuff,
- sizeof(reportTgtGrpBuff),
+ buff_len,
extended, true, verbose);
ret = res;
if (0 == res) {
report_len = sg_get_unaligned_be32(reportTgtGrpBuff + 0) + 4;
- if (report_len > (int)sizeof(reportTgtGrpBuff)) {
- /* trunc = 1; */
- pr2serr(" <<report too long for internal buffer, output "
- "truncated\n");
- report_len = (int)sizeof(reportTgtGrpBuff);
+ if (report_len > buff_len) {
+ free(reportTgtGrpBuff);
+ buff_len = report_len;
+ goto retry;
}
if (raw) {
dStrRaw(reportTgtGrpBuff, report_len);
@@ -354,6 +360,8 @@ err_out:
ret = sg_convert_errno(-res);
}
}
+ if (reportTgtGrpBuff)
+ free(reportTgtGrpBuff);
if (0 == verbose) {
if (! sg_if_can2stderr("sg_rtpg failed: ", ret))
pr2serr("Some error occurred, try again with '-v' "
diff --git a/testing/Makefile b/testing/Makefile
index 46d00a2e..5d05ad5a 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -7,7 +7,7 @@ MANDIR=$(DESTDIR)/$(PREFIX)/man
EXECS = sg_sense_test sg_queue_tst bsg_queue_tst sg_chk_asc sg_tst_nvme \
sg_tst_ioctl sg_tst_bidi tst_sg_lib sgs_dd sg_tst_excl \
sg_tst_excl2 sg_tst_excl3 sg_tst_context sg_tst_async sgh_dd \
- sg_mrq_dd sg_iovec_tst sg_take_snap
+ sg_mrq_dd sg_iovec_tst sg_take_snap sg_tst_json_builder
EXTRAS =
@@ -49,7 +49,8 @@ LIBFILESOLD = ../lib/sg_lib.o ../lib/sg_lib_data.o ../lib/sg_io_linux.o
LIBFILESNEW = ../lib/sg_pt_linux_nvme.o ../lib/sg_lib.o ../lib/sg_lib_data.o \
../lib/sg_pt_linux.o ../lib/sg_io_linux.o \
../lib/sg_pt_common.o ../lib/sg_cmds_basic.o \
- ../lib/sg_cmds_basic2.o ../lib/sg_lib_names.o
+ ../lib/sg_cmds_basic2.o ../lib/sg_lib_names.o \
+ ../lib/sg_json_builder.o
all: $(EXECS)
@@ -63,7 +64,7 @@ depend dep:
done > .depend
clean:
- /bin/rm -f *.o $(EXECS) $(EXTRAS) $(BSG_EXTRAS) core .depend
+ /bin/rm -f *.o $(EXECS) $(EXTRAS) $(BSG_EXTRAS) json_writer core .depend
sg_sense_test: sg_sense_test.o $(LIBFILESOLD)
$(LD) -o $@ $(LDFLAGS) $^
@@ -120,6 +121,9 @@ sg_iovec_tst: sg_iovec_tst.o sg_scat_gath.o $(LIBFILESNEW)
sg_take_snap: sg_take_snap.o $(LIBFILESNEW)
$(LD) -o $@ $(LDFLAGS) $^
+sg_tst_json_builder: sg_tst_json_builder.o $(LIBFILESNEW)
+ $(LD) -o $@ $(LDFLAGS) $^
+
install: $(EXECS)
install -d $(INSTDIR)
diff --git a/testing/sg_mrq_dd.cpp b/testing/sg_mrq_dd.cpp
index 7b81a63a..9f17146c 100644
--- a/testing/sg_mrq_dd.cpp
+++ b/testing/sg_mrq_dd.cpp
@@ -30,7 +30,7 @@
*
*/
-static const char * version_str = "1.40 20220118";
+static const char * version_str = "1.41 20220413";
#define _XOPEN_SOURCE 600
#ifndef _GNU_SOURCE
@@ -390,6 +390,7 @@ static atomic<int> num_ebusy(0);
static atomic<int> num_start_eagain(0);
static atomic<int> num_fin_eagain(0);
static atomic<int> num_miscompare(0);
+static atomic<int> num_fallthru_sigusr2(0);
static atomic<bool> vb_first_time(true);
static sigset_t signal_set;
@@ -1072,17 +1073,16 @@ siginfo_handler(int sig)
print_stats(" ");
}
-#if 0
+/* Usually this signal (SIGUSR2) will be caught by the timed wait in the
+ * sig_listen_thread thread but some might slip through while the timed
+ * wait is being re-armed or after that thread is finished. This handler
+ * acts as a backstop. */
static void
siginfo2_handler(int sig)
{
if (sig) { ; } /* unused, dummy to suppress warning */
- pr2serr("Progress report, continuing ...\n");
- if (do_time > 0)
- calc_duration_throughput(1);
- print_stats(" ");
+ ++num_fallthru_sigusr2;
}
-#endif
static void
install_handler(int sig_num, void (*sig_handler) (int sig))
@@ -4247,7 +4247,7 @@ main(int argc, char * argv[])
install_handler(SIGQUIT, interrupt_handler);
install_handler(SIGPIPE, interrupt_handler);
install_handler(SIGUSR1, siginfo_handler);
- // install_handler(SIGUSR2, siginfo2_handler);
+ install_handler(SIGUSR2, siginfo2_handler);
num_ifiles = clp->inf_v.size();
num_ofiles = clp->outf_v.size();
@@ -4590,9 +4590,11 @@ jump:
}
std::this_thread::yield(); // not enough it seems
{ /* allow time for SIGUSR2 signal to get through */
- struct timespec tspec = {0, 400000}; /* 400 usecs */
+ struct timespec tspec = {0, 1000000}; /* 1 msec */
+ struct timespec rem;
- nanosleep(&tspec, NULL);
+ while ((nanosleep(&tspec, &rem) < 0) && (EINTR == errno))
+ tspec = rem;
}
}
@@ -4645,5 +4647,11 @@ fini:
if (0 == res)
res = clp->reason_res.load();
sigprocmask(SIG_SETMASK, &orig_signal_set, NULL);
+ if (clp->verbose) {
+ int num_sigusr2 = num_fallthru_sigusr2.load();
+ if (num_sigusr2 > 0)
+ pr2serr("Number of fall-through SIGUSR2 signals caught: %d\n",
+ num_sigusr2);
+ }
return (res >= 0) ? res : SG_LIB_CAT_OTHER;
}
diff --git a/testing/sg_tst_json_builder.c b/testing/sg_tst_json_builder.c
new file mode 100644
index 00000000..f91e600a
--- /dev/null
+++ b/testing/sg_tst_json_builder.c
@@ -0,0 +1,157 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Simple streaming JSON writer
+ *
+ * This takes care of the annoying bits of JSON syntax like the commas
+ * after elements
+ *
+ * Authors: Stephen Hemminger <stephen@networkplumber.org>
+ *
+ * Borrowed from Linux kernel [5.17.0]: tools/bpf/bpftool/json_writer.[hc]
+ */
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdarg.h>
+#include <assert.h>
+#include <malloc.h>
+#include <inttypes.h>
+#include <stdint.h>
+
+#include "../lib/sg_json_builder.h"
+
+
+
+static json_serialize_opts out_settings = {
+ json_serialize_mode_multiline,
+ 0,
+ 4
+};
+
+int
+main(int arnum, char * argv[])
+{
+ size_t len;
+ json_value * jv1p;
+ json_value * jv2p;
+ json_value * jv3p = json_object_new(0);
+ json_value * jvp = json_object_new(0);
+ json_value * jv4p;
+ json_value * jv5p;
+ json_value * ja1p = json_array_new(0);
+ json_value * ja2p;
+ json_value * ja3p;
+ json_value * jsp = json_string_new("hello world 1");
+ json_value * js2p = json_string_new("hello world 2");
+ json_value * js3p = json_string_new("hello world 3");
+ json_value * js10 = json_string_new("good-bye world");
+ json_value * js11 = json_string_new("good-bye world 2");
+ json_value * js12 = json_string_new("duplicate name 1");
+ char b[8192];
+
+ jv1p = json_object_push(jvp, "contents", jsp);
+
+ if (jvp == jv1p)
+ printf("jvp == jv1p\n");
+ else
+ printf("jvp != jv1p\n");
+
+#if 1
+ json_array_push(ja1p, js2p);
+ jv2p = json_object_push(jvp, "extra", js3p);
+ if (jv2p)
+ printf("jv2p->type=%d\n", jv2p->type);
+ else
+ printf("jv2p is NULL\n");
+ ja2p = json_array_push(ja1p, json_string_new("hello world 99"));
+ if (ja2p)
+ printf("ja2p->type=%d\n", ja2p->type);
+ else
+ printf("ja2p is NULL\n");
+ // json_object_push(ja2p, "boo", json_string_new("hello world 88"));
+ json_object_push(jvp, "a_array", ja1p);
+ jv4p = json_object_push(jvp, "a_object", jv3p);
+ if (jv4p)
+ printf("jv4p->type=%d\n", jv4p->type);
+ else
+ printf("jv4p is NULL\n");
+ json_object_push(jv4p, "test", js10);
+ json_object_push(jv4p, "test2", js11);
+ json_object_push(jv4p, "test", js12);
+ // ja3p = json_array_push(ja2p, json_string_new("good-bye"));
+ // jv4p = json_object_push(jvp, "a_array", ja2p);
+ // jv5p = json_object_merge(jvp, ja1p);
+#endif
+ jv5p = jvp;
+
+ jv1p = json_string_new("boo");
+
+ len = json_measure_ex(jv5p, out_settings);
+ printf("jvp length: %zu bytes\n", len);
+ if (len < sizeof(b)) {
+ json_serialize_ex(b, jv5p, out_settings);
+ printf("json serialized:\n");
+ printf("%s\n", b);
+ } else
+ printf("since json output length [%zu] > 8192, skip outputting\n",
+ len);
+
+ json_builder_free(jvp);
+ return 0;
+}
+
+
+#if 0
+int main(int argc, char **argv)
+{
+ json_writer_t *wr = jsonw_new(stdout);
+
+ jsonw_start_object(wr);
+ jsonw_pretty(wr, true);
+ jsonw_name(wr, "Vyatta");
+ jsonw_start_object(wr);
+ jsonw_string_field(wr, "url", "http://vyatta.com");
+ jsonw_uint_field(wr, "downloads", 2000000ul);
+ jsonw_float_field(wr, "stock", 8.16);
+
+ jsonw_name(wr, "ARGV");
+ jsonw_start_array(wr);
+ while (--argc)
+ jsonw_string(wr, *++argv);
+ jsonw_end_array(wr);
+
+ jsonw_name(wr, "empty");
+ jsonw_start_array(wr);
+ jsonw_end_array(wr);
+
+ jsonw_name(wr, "NIL");
+ jsonw_start_object(wr);
+ jsonw_end_object(wr);
+
+ jsonw_null_field(wr, "my_null");
+
+ jsonw_name(wr, "special chars");
+ jsonw_start_array(wr);
+ jsonw_string_field(wr, "slash", "/");
+ jsonw_string_field(wr, "newline", "\n");
+ jsonw_string_field(wr, "tab", "\t");
+ jsonw_string_field(wr, "ff", "\f");
+ jsonw_string_field(wr, "quote", "\"");
+ jsonw_string_field(wr, "tick", "\'");
+ jsonw_string_field(wr, "backslash", "\\");
+ jsonw_end_array(wr);
+
+jsonw_name(wr, "ARGV");
+jsonw_start_array(wr);
+jsonw_string(wr, "boo: appended or new entry?");
+jsonw_end_array(wr);
+
+ jsonw_end_object(wr);
+
+ jsonw_end_object(wr);
+ jsonw_destroy(&wr);
+ return 0;
+}
+
+#endif
+