diff options
author | Douglas Gilbert <dgilbert@interlog.com> | 2022-05-06 01:27:03 +0000 |
---|---|---|
committer | Douglas Gilbert <dgilbert@interlog.com> | 2022-05-06 01:27:03 +0000 |
commit | 3e056f675f51f14a5209efe1b491ba897e7873ce (patch) | |
tree | 8843997030eac9382a2d817c44b96a5cbe2d391f /include | |
parent | d7f5140b8b84eb9e27509a1c0fb6872a782b1fe8 (diff) | |
download | sg3_utils-3e056f675f51f14a5209efe1b491ba897e7873ce.tar.gz |
sg_inq: add support for Hitachi/HP open-v ldev names; sg_rep_zone: more json work
git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@951 6180dd3e-e324-4e3e-922d-17de1ae2f315
Diffstat (limited to 'include')
-rw-r--r-- | include/sg_pr2serr.h | 225 |
1 files changed, 136 insertions, 89 deletions
diff --git a/include/sg_pr2serr.h b/include/sg_pr2serr.h index b13432fc..b29c7e34 100644 --- a/include/sg_pr2serr.h +++ b/include/sg_pr2serr.h @@ -10,12 +10,6 @@ * SPDX-License-Identifier: BSD-2-Clause */ -/* These are convenience functions that replace the somewhat long-winded - * fprintf(stderr, ....). The second form (i.e. pr2ws() ) is for internal - * library use and may place its output somewhere other than stderr; it - * depends on the external variable sg_warnings_strm which can be set - * with sg_set_warnings_strm(). By default it uses stderr. */ - #include <inttypes.h> #include <stdio.h> #include <stdbool.h> @@ -24,40 +18,11 @@ extern "C" { #endif -#if 1 -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 - -typedef void * sg_json_opaque_p; - -typedef struct sg_json_state_t { - bool pr_as_json; - bool pr_pretty; - bool pr_header; - bool pr_sorted; - bool pr_output; - bool pr_implemented; - bool pr_unimplemented; - char pr_format; - int pr_indent_size; - int first_bad_char; - int verbose; - /* the following hold state information */ - sg_json_opaque_p basep; /* base JSON object pointer */ - sg_json_opaque_p outputp; /* 'output' named JSON array pointer */ - sg_json_opaque_p userp; /* for temporary usage */ -} sg_json_state; - +/* pr2serr and pr2ws are convenience functions that replace the somewhat + * long-winded fprintf(stderr, ....). The second form (i.e. pr2ws() ) is for + * internal library use and may place its output somewhere other than stderr; + * it depends on the external variable sg_warnings_strm which can be set + * with sg_set_warnings_strm(). By default it uses stderr. */ #if __USE_MINGW_ANSI_STDIO -0 == 1 #define __printf(a, b) __attribute__((__format__(gnu_printf, a, b))) @@ -80,66 +45,148 @@ int pr2ws(const char * fmt, ...) __printf(1, 2); * 0). Linux kernel has a similar function called scnprintf(). */ int sg_scnpr(char * cp, int cp_max_len, const char * fmt, ...) __printf(3, 4); -void sgj_pr_hr(sg_json_state * jsp, const char * fmt, ...) __printf(2, 3); - -bool sgj_init_state(sg_json_state * jstp, const char * j_optarg); - -sg_json_opaque_p sgj_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_pr_simple_vs(sg_json_state * jsp, sg_json_opaque_p jop, - int leadin_sp, const char * name, - enum sg_json_separator_t sep, const char * value); - -void sgj_pr_simple_vi(sg_json_state * jsp, sg_json_opaque_p jop, - int leadin_sp, const char * name, - enum sg_json_separator_t sep, int64_t value); - -void sgj_pr_simple_vb(sg_json_state * jsp, sg_json_opaque_p jop, - int leadin_sp, const char * name, - enum sg_json_separator_t sep, bool value); +/* JSON support functions and structures follow. The prefix "sgj_" is used + * for sg3_utils JSON functions, types and values. */ + +enum sgj_separator_t { + SGJ_SEP_NONE = 0, + SGJ_SEP_SPACE_1, + SGJ_SEP_SPACE_2, + SGJ_SEP_SPACE_3, + SGJ_SEP_SPACE_4, + SGJ_SEP_EQUAL_NO_SPACE, + SGJ_SEP_EQUAL_1_SPACE, + SGJ_SEP_COLON_NO_SPACE, + SGJ_SEP_COLON_1_SPACE, +}; -void sgj_add_name_pair_ihex(sg_json_state * jsp, sg_json_opaque_p jop, +typedef void * sgj_opaque_p; + +/* Apart from the pointers at the end the other fields are initialized + * from the argument given to --json= . If there is no argument then + * they initialized as shown. */ +typedef struct sgj_state_t { + bool pr_as_json; /* = false */ + bool pr_pretty; /* = true */ + bool pr_header; /* = true */ + bool pr_sorted; /* = false (ignored) */ + bool pr_output; /* = false */ + bool pr_implemented; + bool pr_unimplemented; + char pr_format; /* = '\0' */ + int pr_indent_size; /* = 4 */ + int first_bad_char; /* = '\0' */ + int verbose; /* = 0 */ + /* the following hold state information */ + sgj_opaque_p basep; /* base JSON object pointer */ + sgj_opaque_p outputp; /* 'output' named JSON array pointer */ + sgj_opaque_p userp; /* for temporary usage */ +} sgj_state; + +/* Prints to stdout like printf(fmt, ...). Further if --json=o option is + * given that output line is also placed in the JSON 'output' array. */ +void sgj_pr_hr(sgj_state * jsp, const char * fmt, ...) __printf(2, 3); + +/* Initializes the state object pointed to by jsp based on the argument + * given to the right of --json= pointed to by j_optarg. If it is NULL + * then state object gets its default values. Returns true if argument + * to --json= is decoded properly, else returns false and places the + * first "bad" character in jsp->first_bad_char . Note that no JSON + * in-core tree needs to exist when this function is called. */ +bool sgj_init_state(sgj_state * jsp, const char * j_optarg); + +/* sgj_start() creates a JSON in-core tree and returns a pointer to it (or + * NULL if the associated heap allocation fails). It should be paired with + * sgj_finish() to clean up (i.e. remove all heap allocations) all the + * elements (i.e. JSON objects and arrays) that have been placed in that + * in-core tree. If jsp is NULL nothing further happens. Otherwise the pointer + * to be returned is placed in jsp->basep. If jsp->pr_header is true and + * util_name is non-NULL then a "utility_invoked" JSON object is made with + * "name", and "version_date" object fields. If the jsp->pr_output field is + * true a named array called "output" is added to the "utility_invoked" object + * (creating it in the case when jsp->pr_header is false) and a pointer to + * that array object is placed in jsp->objectp . The returned pointer is not + * usually needed but if it is NULL then a heap allocation has failed. */ +sgj_opaque_p sgj_start(const char * util_name, const char * ver_str, + int argc, char *argv[], sgj_state * jsp); + +/* These are low level functions returning a pointer to a newly created JSON + * object or array. If jsp is NULL or jsp->pr_as_json is false nothing happens + * and NULL is returned. Note that this JSON object is _not_ placed in the + * in-core tree controlled by jsp (jsp->basep); it may be added later as the + * third argument to sgj_add_array_element(), for example. */ +sgj_opaque_p sgj_new_unattached_object(sgj_state * jsp); +sgj_opaque_p sgj_new_unattached_array(sgj_state * jsp); + +/* If jsp is NULL or jsp->pr_as_json is false nothing happens and NULL is + * returned. Otherwise it creates a new named object (whose name is what + * 'name' points to) at 'jop' with an empty object as its value; a pointer + * to that empty object is returned. If 'jop' is NULL then jsp->basep is + * used instead. The returned value should always be checked (for NULL) + * and if not, used. */ +sgj_opaque_p sgj_new_named_object(sgj_state * jsp, sgj_opaque_p jop, + const char * name); + +/* If jsp is NULL or jsp->pr_as_json is false nothing happens and NULL is + * returned. Otherwise it creates a new named object (whose name is what + * 'name' points to) at 'jop' with an empty array as its value; a pointer + * to that empty array is returned. If 'jop' is NULL then jsp->basep is + * used instead. The returned value should always * be checked (for NULL) + * and if not, used. */ +sgj_opaque_p sgj_new_named_array(sgj_state * jsp, sgj_opaque_p jop, + const char * name); + +/* If jsp is NULL or jsp->pr_as_json is false or ua_jop is NULL nothing + * happens and NULL is returned. Otherwise it adds a new array element + * (ua_jop) to the array ('jap') that was returned by sgj_new_named_array(). + * ua_jop is assumed to not been part of the main JSON in-core tree before + * this call, and it is after this call. This means that ua_jop must have + * been created by sgj_new_unattached_object() or similar. */ +sgj_opaque_p sgj_add_array_element(sgj_state * jsp, sgj_opaque_p jap, + sgj_opaque_p ua_jop); + +sgj_opaque_p sgj_add_name_vs(sgj_state * jsp, sgj_opaque_p jop, + const char * name, const char * value); + +sgj_opaque_p sgj_add_name_vi(sgj_state * jsp, sgj_opaque_p jop, + const char * name, int64_t value); + +sgj_opaque_p sgj_add_name_vb(sgj_state * jsp, sgj_opaque_p jop, + const char * name, bool value); + +sgj_opaque_p sgj_add_name_obj(sgj_state * jsp, sgj_opaque_p jop, + const char * name, sgj_opaque_p ua_jop); + +void sgj_pr_twin_vs(sgj_state * jsp, sgj_opaque_p jop, int leadin_sp, + const char * name, enum sgj_separator_t sep, + const char * value); + +void sgj_pr_twin_vi(sgj_state * jsp, sgj_opaque_p jop, int leadin_sp, + const char * name, enum sgj_separator_t sep, + int64_t value); + +void sgj_pr_twin_vb(sgj_state * jsp, sgj_opaque_p jop, int leadin_sp, + const char * name, enum sgj_separator_t sep, bool value); + +void sgj_add_name_pair_ihex(sgj_state * jsp, sgj_opaque_p jop, const char * name, uint64_t value); -void sgj_add_name_pair_istr(sg_json_state * jsp, sg_json_opaque_p jop, - const char * name, int64_t value, - const char * str); +void sgj_add_name_pair_istr(sgj_state * jsp, sgj_opaque_p jop, + const char * name, int64_t val_i, + const char * val_s); #if 0 -void sgj_pr_hr_line_vs(sg_json_state * jsp, sg_json_opaque_p jop, +void sgj_pr_hr_line_vs(sgj_state * jsp, sgj_opaque_p jop, const char * hr_line, const char * name, const char * value); #endif -void sgj_pr2file(sg_json_state * jsp, sg_json_opaque_p jop, int exit_status, +void sgj_pr2file(sgj_state * jsp, sgj_opaque_p jop, int exit_status, FILE * fp); -void sgj_finish(sg_json_state * jstp); +void sgj_free_unattached(sgj_opaque_p jop); + +void sgj_finish(sgj_state * jsp); #ifdef __cplusplus |