output: add support for uppercase hex numbers

... via "%X" format string.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-id: 20200930111222.6020-2-kraxel@redhat.com
diff --git a/src/output.c b/src/output.c
index 3c335cb..0184444 100644
--- a/src/output.c
+++ b/src/output.c
@@ -160,10 +160,12 @@
 
 // Output a single digit hex character.
 static inline void
-putsinglehex(struct putcinfo *action, u32 val)
+putsinglehex(struct putcinfo *action, u32 val, int uc)
 {
     if (val <= 9)
         val = '0' + val;
+    else if (uc)
+        val = 'A' + val - 10;
     else
         val = 'a' + val - 10;
     putc(action, val);
@@ -171,23 +173,23 @@
 
 // Output an integer in hexadecimal with a specified width.
 static void
-puthex(struct putcinfo *action, u32 val, int width)
+puthex(struct putcinfo *action, u32 val, int width, int uc)
 {
     switch (width) {
-    default: putsinglehex(action, (val >> 28) & 0xf);
-    case 7:  putsinglehex(action, (val >> 24) & 0xf);
-    case 6:  putsinglehex(action, (val >> 20) & 0xf);
-    case 5:  putsinglehex(action, (val >> 16) & 0xf);
-    case 4:  putsinglehex(action, (val >> 12) & 0xf);
-    case 3:  putsinglehex(action, (val >> 8) & 0xf);
-    case 2:  putsinglehex(action, (val >> 4) & 0xf);
-    case 1:  putsinglehex(action, (val >> 0) & 0xf);
+    default: putsinglehex(action, (val >> 28) & 0xf, uc);
+    case 7:  putsinglehex(action, (val >> 24) & 0xf, uc);
+    case 6:  putsinglehex(action, (val >> 20) & 0xf, uc);
+    case 5:  putsinglehex(action, (val >> 16) & 0xf, uc);
+    case 4:  putsinglehex(action, (val >> 12) & 0xf, uc);
+    case 3:  putsinglehex(action, (val >> 8) & 0xf, uc);
+    case 2:  putsinglehex(action, (val >> 4) & 0xf, uc);
+    case 1:  putsinglehex(action, (val >> 0) & 0xf, uc);
     }
 }
 
 // Output an integer in hexadecimal with a minimum width.
 static void
-putprettyhex(struct putcinfo *action, u32 val, int width, char padchar)
+putprettyhex(struct putcinfo *action, u32 val, int width, char padchar, int uc)
 {
     u32 tmp = val;
     int count = 1;
@@ -196,18 +198,18 @@
     width -= count;
     while (width-- > 0)
         putc(action, padchar);
-    puthex(action, val, count);
+    puthex(action, val, count, uc);
 }
 
 // Output 'struct pci_device' BDF as %02x:%02x.%x
 static void
 put_pci_device(struct putcinfo *action, struct pci_device *pci)
 {
-    puthex(action, pci_bdf_to_bus(pci->bdf), 2);
+    puthex(action, pci_bdf_to_bus(pci->bdf), 2, 0);
     putc(action, ':');
-    puthex(action, pci_bdf_to_dev(pci->bdf), 2);
+    puthex(action, pci_bdf_to_dev(pci->bdf), 2, 0);
     putc(action, '.');
-    puthex(action, pci_bdf_to_fn(pci->bdf), 1);
+    puthex(action, pci_bdf_to_fn(pci->bdf), 1, 0);
 }
 
 static inline int
@@ -220,6 +222,7 @@
 bvprintf(struct putcinfo *action, const char *fmt, va_list args)
 {
     const char *s = fmt;
+    int uc;
     for (;; s++) {
         char c = GET_GLOBAL(*(u8*)s);
         if (!c)
@@ -284,19 +287,21 @@
             }
             putc(action, '0');
             putc(action, 'x');
-            puthex(action, val, 8);
+            puthex(action, val, 8, 0);
             break;
+        case 'X':
         case 'x':
+            uc = (c == 'X');
             val = va_arg(args, s32);
             if (is64) {
                 u32 upper = va_arg(args, s32);
                 if (upper) {
-                    putprettyhex(action, upper, field_width - 8, padchar);
-                    puthex(action, val, 8);
+                    putprettyhex(action, upper, field_width - 8, padchar, uc);
+                    puthex(action, val, 8, uc);
                     break;
                 }
             }
-            putprettyhex(action, val, field_width, padchar);
+            putprettyhex(action, val, field_width, padchar, uc);
             break;
         case 'c':
             val = va_arg(args, int);
@@ -348,7 +353,7 @@
         if (cur != &MainThread) {
             // Show "thread id" for this debug message.
             debug_putc(&debuginfo, '|');
-            puthex(&debuginfo, (u32)cur, 8);
+            puthex(&debuginfo, (u32)cur, 8, 0);
             debug_putc(&debuginfo, '|');
             debug_putc(&debuginfo, ' ');
         }
@@ -450,12 +455,12 @@
     while (len > 0) {
         if (count % 8 == 0) {
             putc(&debuginfo, '\n');
-            puthex(&debuginfo, count*4, 8);
+            puthex(&debuginfo, count*4, 8, 0);
             putc(&debuginfo, ':');
         } else {
             putc(&debuginfo, ' ');
         }
-        puthex(&debuginfo, *(u32*)d, 8);
+        puthex(&debuginfo, *(u32*)d, 8, 0);
         count++;
         len-=4;
         d+=4;