]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-print-json: Change d() output to use d_json()
authorTokunori Ikegami <ikegami.t@gmail.com>
Thu, 2 Nov 2023 18:20:26 +0000 (03:20 +0900)
committerDaniel Wagner <wagi@monom.org>
Thu, 16 Nov 2023 10:05:54 +0000 (11:05 +0100)
Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
nvme-print-json.c
nvme-print-stdout.c
nvme-print.c
nvme-print.h

index dbe06f9ce39b6f6b36b67f5aa878069ae5fb627e..c0fbbbd90ab641d4686165f867d63047a715e6c0 100644 (file)
@@ -1930,23 +1930,22 @@ static void json_ctrl_registers(void *bar, bool fabrics)
        json_print(root);
 }
 
-static void d_json(unsigned char *buf, int len, int width, int group,
-           struct json_object *array)
+static void d_json(unsigned char *buf, int len, int width, int group, struct json_object *array)
 {
-       int i, line_done = 0;
-       char ascii[32 + 1];
+       int i;
+       char ascii[32 + 1] = { 0 };
+
        assert(width < sizeof(ascii));
 
        for (i = 0; i < len; i++) {
-               line_done = 0;
                ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.';
-               if (((i + 1) % width) == 0) {
-                       ascii[i % width + 1] = '\0';
+               if (!((i + 1) % width)) {
                        json_array_add_value_string(array, ascii);
-                       line_done = 1;
+                       memset(ascii, 0, sizeof(ascii));
                }
        }
-       if (!line_done) {
+
+       if (strlen(ascii)) {
                ascii[i % width + 1] = '\0';
                json_array_add_value_string(array, ascii);
        }
@@ -2958,6 +2957,17 @@ static void json_feature_show_fields(enum nvme_features_id fid, unsigned int res
        }
 }
 
+void json_d(unsigned char *buf, int len, int width, int group)
+{
+       struct json_object *root = json_create_object();
+       struct json_object *data = json_create_array();
+
+       d_json(buf, len, width, group, data);
+       json_object_add_value_array(root, "data", data);
+
+       json_print(root);
+}
+
 static void json_nvme_list_ctrl(struct nvme_ctrl_list *ctrl_list)
 {
        __u16 num = le16_to_cpu(ctrl_list->num);
@@ -3825,6 +3835,7 @@ static struct print_ops json_print_ops = {
        .id_ctrl_rpmbs                  = NULL,
        .lba_range                      = NULL,
        .lba_status_info                = NULL,
+       .d                              = json_d,
 
        /* libnvme tree print functions */
        .list_item                      = NULL,
index a0f5dd1c8a47188b88146463100904aadccc6ea9..2f0c8a4bbdda426c455a677d30f20661235f59a4 100644 (file)
@@ -4323,6 +4323,41 @@ static void stdout_lba_status_info(__u32 result)
        printf("\tLBA Status Information Report Interval (LSIRI): %u\n", result & 0xffff);
 }
 
+void stdout_d(unsigned char *buf, int len, int width, int group)
+{
+       int i, offset = 0;
+       char ascii[32 + 1] = { 0 };
+
+       assert(width < sizeof(ascii));
+
+       printf("     ");
+
+       for (i = 0; i <= 15; i++)
+               printf("%3x", i);
+
+       for (i = 0; i < len; i++) {
+               if (!(i % width))
+                       printf( "\n%04x:", offset);
+               if (i % group)
+                       printf( "%02x", buf[i]);
+               else
+                       printf( " %02x", buf[i]);
+               ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.';
+               if (!((i + 1) % width)) {
+                       printf( " \"%.*s\"", width, ascii);
+                       offset += width;
+                       memset(ascii, 0, sizeof(ascii));
+               }
+       }
+
+       if (strlen(ascii)) {
+               unsigned b = width - (i % width);
+               printf( " %*s \"%.*s\"", 2 * b + b / group + (b % group ? 1 : 0), "", width, ascii);
+       }
+
+       printf( "\n");
+}
+
 static void stdout_plm_config(struct nvme_plm_config *plmcfg)
 {
        printf("\tEnable Event          :%04x\n", le16_to_cpu(plmcfg->ee));
@@ -5139,6 +5174,7 @@ static struct print_ops stdout_print_ops = {
        .id_ctrl_rpmbs                  = stdout_id_ctrl_rpmbs,
        .lba_range                      = stdout_lba_range,
        .lba_status_info                = stdout_lba_status_info,
+       .d                              = stdout_d,
 
        /* libnvme tree print functions */
        .list_item                      = stdout_list_item,
index f4c3cc81dd6e621b2380afd1e0e2e3a3aff6ac64..362ad6ffe293ca063cc951472449d9d7291be3ca 100644 (file)
@@ -382,32 +382,7 @@ void nvme_show_relatives(const char *name)
 
 void d(unsigned char *buf, int len, int width, int group)
 {
-       int i, offset = 0;
-       char ascii[32 + 1] = { 0 };
-
-       assert(width < sizeof(ascii));
-       printf("     ");
-       for (i = 0; i <= 15; i++)
-               printf("%3x", i);
-       for (i = 0; i < len; i++) {
-               if (i % width == 0)
-                       printf( "\n%04x:", offset);
-               if (i % group == 0)
-                       printf( " %02x", buf[i]);
-               else
-                       printf( "%02x", buf[i]);
-               ascii[i % width] = (buf[i] >= '!' && buf[i] <= '~') ? buf[i] : '.';
-               if (((i + 1) % width) == 0) {
-                       printf( " \"%.*s\"", width, ascii);
-                       offset += width;
-                       memset(ascii, 0, sizeof(ascii));
-               }
-       }
-       if (strlen(ascii)) {
-               unsigned b = width - (i % width);
-               printf( " %*s \"%.*s\"", 2 * b + b / group + (b % group ? 1 : 0), "", width, ascii);
-       }
-       printf( "\n");
+       nvme_print(d, 0, buf, len, width, group);
 }
 
 void d_raw(unsigned char *buf, unsigned len)
index 88ff9ed08c388be4e90ff1d48f87f402dd386588..c05576b786c9f26dfc07718d3bd67f6f152e011a 100644 (file)
@@ -82,6 +82,7 @@ struct print_ops {
        void (*id_ctrl_rpmbs)(__le32 ctrl_rpmbs);
        void (*lba_range)(struct nvme_lba_range_type *lbrt, int nr_ranges);
        void (*lba_status_info)(__u32 result);
+       void (*d)(unsigned char *buf, int len, int width, int group);
 
        /* libnvme tree print functions */
        void (*list_item)(nvme_ns_t n);