From 8cd2200ba1758cd127a1b95ab1808cde4f56713c Mon Sep 17 00:00:00 2001 From: Douglas Gilbert Date: Thu, 21 Apr 2022 02:37:15 +0000 Subject: sg_opcodes: add --inhex=FN to process earlier -HHH, add experimental --json[=JO] option; sg_turs: change nanosleep() to Sleep() in MinGW git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@945 6180dd3e-e324-4e3e-922d-17de1ae2f315 --- include/sg_pr2serr.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/sg_pr2serr.h b/include/sg_pr2serr.h index 70592781..afac7c09 100644 --- a/include/sg_pr2serr.h +++ b/include/sg_pr2serr.h @@ -27,12 +27,13 @@ #include +#include #ifdef __cplusplus extern "C" { #endif -#if 0 +#if 1 enum sg_json_separator_t { SG_JSON_SEP_NONE = 0, SG_JSON_SEP_SPACE_1, @@ -46,6 +47,23 @@ enum sg_json_separator_t { }; #endif +typedef void * sg_json_opaque_p; + +typedef struct sg_json_state_t { + bool pr_as_json; + bool pr_pretty; + bool pr_sorted; + bool pr_output; + bool pr_implemented; + bool pr_unimplemented; + char pr_format; + int pr_indent_size; + int verbose; + sg_json_opaque_p basep; + sg_json_opaque_p outputp; + sg_json_opaque_p userp; +} sg_json_state; + #if defined(__GNUC__) || defined(__clang__) #ifdef SG_LIB_MINGW @@ -54,8 +72,17 @@ int pr2serr(const char * fmt, ...); int pr2ws(const char * fmt, ...); +/* Want safer, 'n += snprintf(b + n, blen - n, ...)' style sequence of + * functions. Returns number of chars placed in cp excluding the trailing null + * char. So for cp_max_len > 0 the return value is always < cp_max_len; for + * cp_max_len <= 1 the return value is 0 and no chars are written to cp. Note + * this means that when cp_max_len = 1, this function assumes that cp[0] is + * the null character and does nothing (and returns 0). Linux kernel has a + * similar function called scnprintf(). */ int sg_scnpr(char * cp, int cp_max_len, const char * fmt, ...); +void sgj_pr_hr(sg_json_state * jsp, const char * fmt, ...); + #else /* GNU/clang other than MinGW */ int pr2serr(const char * fmt, ...) @@ -66,6 +93,9 @@ int pr2ws(const char * fmt, ...) int sg_scnpr(char * cp, int cp_max_len, const char * fmt, ...) __attribute__ ((format (printf, 3, 4))); + +void sgj_pr_hr(sg_json_state * jsp, const char * fmt, ...) + __attribute__ ((format (printf, 2, 3))); #endif #else /* not GNU (and not clang) */ @@ -76,15 +106,51 @@ int pr2ws(const char * fmt, ...); int sg_scnpr(char * cp, int cp_max_len, const char * fmt, ...); +void sgj_pr_hr(sg_json_state * jsp, const char * fmt, ...); + #endif -#if 0 +void sg_json_init_state(sg_json_state * jstp); + +sg_json_opaque_p sg_json_start(const char * util_name, const char * ver_str, + int argc, char *argv[], sg_json_state * jstp); + +/* Newly created object is un-attached */ +sg_json_opaque_p sgj_new_object(sg_json_state * jsp); + +sg_json_opaque_p sgj_new_named_object(sg_json_state * jsp, + sg_json_opaque_p jop, + const char * name); + +sg_json_opaque_p sgj_add_array_element(sg_json_state * jsp, + sg_json_opaque_p jap, + sg_json_opaque_p ejop); + +sg_json_opaque_p sgj_new_named_array(sg_json_state * jsp, + sg_json_opaque_p jop, + const char * name); + +sg_json_opaque_p sgj_add_name_vs(sg_json_state * jsp, sg_json_opaque_p jop, + const char * name, const char * value); + +sg_json_opaque_p sgj_add_name_vi(sg_json_state * jsp, sg_json_opaque_p jop, + const char * name, int64_t value); + +sg_json_opaque_p sgj_add_name_vb(sg_json_state * jsp, sg_json_opaque_p jop, + const char * name, bool value); + +void sgj_pr2file(sg_json_opaque_p jop, sg_json_state * jstp, FILE * fp); + +void sg_json_free(sg_json_opaque_p jop); + +#if 1 /* 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, +void pr_j_simple(sg_json_opaque_p jop, 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); +void pr_j_hr_line(sg_json_opaque_p jop, const char * hr_line, + const char * name, const char * value); + #endif -- cgit v1.2.3