1818#include "sg_pr2serr.h"
1919#include "sg_json_builder.h"
2020
21- /* Comment out next line to remove dependency on sg_lib.h */
21+ /*
22+ * Some users of sg_pr2serr may not need fixed and descriptor sense decoded
23+ * for JSON output. If the following define is commented out the effective
24+ * compile size of this file is reduced by 800 lines plus dependencies on
25+ * other large components of the sg3_utils library.
26+ * Comment out the next line to remove dependency on sg_lib.h and its code.
27+ */
2228#define SG_PRSE_SENSE_DECODE 1
2329
2430#ifdef SG_PRSE_SENSE_DECODE
@@ -65,6 +71,44 @@ pr2serr(const char * fmt, ...)
6571 return n ;
6672}
6773
74+ #ifndef SG_PRSE_SENSE_DECODE
75+
76+ /* Want safe, 'n += snprintf(b + n, blen - n, ...)' style sequence of
77+ * functions. Returns number of chars placed in cp excluding the
78+ * trailing null char. So for cp_max_len > 0 the return value is always
79+ * < cp_max_len; for cp_max_len <= 1 the return value is 0 and no chars are
80+ * written to cp. Note this means that when cp_max_len = 1, this function
81+ * assumes that cp[0] is the null character and does nothing (and returns
82+ * 0). Linux kernel has a similar function called scnprintf(). Public
83+ * declaration in sg_pr2serr.h header */
84+ int
85+ sg_scnpr (char * cp , int cp_max_len , const char * fmt , ...)
86+ {
87+ va_list args ;
88+ int n ;
89+
90+ if (cp_max_len < 2 )
91+ return 0 ;
92+ va_start (args , fmt );
93+ n = vsnprintf (cp , cp_max_len , fmt , args );
94+ va_end (args );
95+ return (n < cp_max_len ) ? n : (cp_max_len - 1 );
96+ }
97+
98+ int
99+ pr2ws (const char * fmt , ...)
100+ {
101+ va_list args ;
102+ int n ;
103+
104+ va_start (args , fmt );
105+ n = vfprintf (stderr , fmt , args );
106+ va_end (args );
107+ return n ;
108+ }
109+
110+ #endif
111+
68112static bool
69113sgj_parse_opts (sgj_state * jsp , const char * j_optarg )
70114{
@@ -359,11 +403,18 @@ sgj_js2file(sgj_state * jsp, sgj_opaque_p jop, int exit_status, FILE * fp)
359403 if ((NULL == jop ) && jsp -> pr_exit_status ) {
360404 char d [80 ];
361405
406+ #ifdef SG_PRSE_SENSE_DECODE
362407 if (sg_exit2str (exit_status , jsp -> verbose , sizeof (d ), d )) {
363408 if (0 == strlen (d ))
364409 strncpy (d , "no errors" , sizeof (d ) - 1 );
365410 } else
366411 strncpy (d , "not available" , sizeof (d ) - 1 );
412+ #else
413+ if (0 == exit_status )
414+ strncpy (d , "no errors" , sizeof (d ) - 1 );
415+ else
416+ snprintf (d , sizeof (d ), "exit_status=%d" , exit_status );
417+ #endif
367418 sgj_js_nv_istr (jsp , jop , "exit_status" , exit_status , NULL , d );
368419 }
369420 memcpy (& out_settings , & def_out_settings , sizeof (out_settings ));
@@ -694,6 +745,28 @@ sgj_js_nv_ihex_nex(sgj_state * jsp, sgj_opaque_p jop, const char * name,
694745 }
695746}
696747
748+ #ifndef SG_PRSE_SENSE_DECODE
749+ static void
750+ h2str (const uint8_t * byte_arr , int num_bytes , char * bp , int blen )
751+ {
752+ int j , k , n ;
753+
754+ for (k = 0 , n = 0 ; (k < num_bytes ) && (n < blen ); ) {
755+ j = sg_scnpr (bp + n , blen - n , "%02x " , byte_arr [k ]);
756+ if (j < 2 )
757+ break ;
758+ n += j ;
759+ ++ k ;
760+ if ((0 == (k % 8 )) && (k < num_bytes ) && (n < blen )) {
761+ bp [n ++ ] = ' ' ;
762+ }
763+ }
764+ j = strlen (bp );
765+ if ((j > 0 ) && (' ' == bp [j - 1 ]))
766+ bp [j - 1 ] = '\0' ; /* chop off trailing space */
767+ }
768+ #endif
769+
697770/* Add hex byte strings irrespective of jsp->pr_hex setting. */
698771void
699772sgj_js_nv_hex_bytes (sgj_state * jsp , sgj_opaque_p jop , const char * name ,
@@ -706,7 +779,11 @@ sgj_js_nv_hex_bytes(sgj_state * jsp, sgj_opaque_p jop, const char * name,
706779 return ;
707780 bp = (char * )calloc (blen + 4 , 1 );
708781 if (bp ) {
782+ #ifdef SG_PRSE_SENSE_DECODE
709783 hex2str (byte_arr , num_bytes , NULL , 2 , blen , bp );
784+ #else
785+ h2str (byte_arr , num_bytes , bp , blen );
786+ #endif
710787 sgj_js_nv_s (jsp , jop , name , bp );
711788 free (bp );
712789 }
@@ -1892,7 +1969,7 @@ sgj_js_sense(sgj_state * jsp, sgj_opaque_p jop, const uint8_t * sbp,
18921969 sgj_js_nv_i (jsp , jop , "extend" , !! (0x80 & sbp [8 ]));
18931970 sgj_js_nv_i (jsp , jop , "count_upper_nonzero" , !! (0x40 & sbp [8 ]));
18941971 sgj_js_nv_i (jsp , jop , "lba_upper_nonzero" , !! (0x20 & sbp [8 ]));
1895- sgj_js_nv_i (jsp , jop , "log_index" , (0x7 & sbp [8 ]));
1972+ sgj_js_nv_i (jsp , jop , "log_index" , (0xf & sbp [8 ]));
18961973 sgj_js_nv_i (jsp , jop , "lba" , sg_get_unaligned_le24 (sbp + 9 ));
18971974 } else if (len > 2 ) { /* fixed format */
18981975 sgj_js_nv_i (jsp , jop , "valid" , valid_info_fld );
0 commit comments