All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] SeaBIOS: Fix bvprintf() to respect padding for hex printing.
@ 2010-06-14 12:04 Jes.Sorensen
  2010-06-17  2:42 ` [Qemu-devel] " Kevin O'Connor
  0 siblings, 1 reply; 3+ messages in thread
From: Jes.Sorensen @ 2010-06-14 12:04 UTC (permalink / raw)
  To: kevin; +Cc: Jes Sorensen, seabios, qemu-devel

From: Jes Sorensen <Jes.Sorensen@redhat.com>

Fix bvprintf to respect space padding when printing hex numbers
and the caller specifies alignment without zero padding, eg. %2x
as opposed to %02x

Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>
---
 src/output.c |   27 +++++++++++++++++++++------
 1 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/src/output.c b/src/output.c
index 3de565a..a0d07e3 100644
--- a/src/output.c
+++ b/src/output.c
@@ -195,7 +195,7 @@ putsinglehex(struct putcinfo *action, u32 val)
 
 // Output an integer in hexadecimal.
 static void
-puthex(struct putcinfo *action, u32 val, int width)
+puthex(struct putcinfo *action, u32 val, int width, int spacepad)
 {
     if (!width) {
         u32 tmp = val;
@@ -210,6 +210,17 @@ puthex(struct putcinfo *action, u32 val, int width)
         }
         if (tmp > 0xf)
             width += 1;
+    } else if (spacepad)  {
+        int count = 1;
+        u32 tmp = val;
+        while (tmp >>= 4) {
+            count++;
+        }
+        count = width - count;
+        width -= count;
+        while (count--) {
+            putc(action, ' ');
+        }
     }
 
     switch (width) {
@@ -244,11 +255,15 @@ bvprintf(struct putcinfo *action, const char *fmt, va_list args)
         }
         const char *n = s+1;
         int field_width = 0;
+        int spacepad = 1;
         for (;;) {
             c = GET_GLOBAL(*(u8*)n);
             if (!isdigit(c))
                 break;
-            field_width = field_width * 10 + c - '0';
+            if (!field_width && (c == '0'))
+                spacepad = 0;
+            else
+                field_width = field_width * 10 + c - '0';
             n++;
         }
         if (c == 'l') {
@@ -281,7 +296,7 @@ bvprintf(struct putcinfo *action, const char *fmt, va_list args)
             field_width = 8;
         case 'x':
             val = va_arg(args, s32);
-            puthex(action, val, field_width);
+            puthex(action, val, field_width, spacepad);
             break;
         case 'c':
             val = va_arg(args, int);
@@ -333,7 +348,7 @@ __dprintf(const char *fmt, ...)
         if (cur != &MainThread) {
             // Show "thread id" for this debug message.
             putc_debug(&debuginfo, '|');
-            puthex(&debuginfo, (u32)cur, 8);
+            puthex(&debuginfo, (u32)cur, 8, 0);
             putc_debug(&debuginfo, '|');
             putc_debug(&debuginfo, ' ');
         }
@@ -411,12 +426,12 @@ hexdump(const void *d, int len)
     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;
-- 
1.6.5.2

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [Qemu-devel] Re: [PATCH] SeaBIOS: Fix bvprintf() to respect padding for hex printing.
  2010-06-14 12:04 [Qemu-devel] [PATCH] SeaBIOS: Fix bvprintf() to respect padding for hex printing Jes.Sorensen
@ 2010-06-17  2:42 ` Kevin O'Connor
  2010-06-17  7:11   ` Jes Sorensen
  0 siblings, 1 reply; 3+ messages in thread
From: Kevin O'Connor @ 2010-06-17  2:42 UTC (permalink / raw)
  To: Jes.Sorensen; +Cc: seabios, qemu-devel

On Mon, Jun 14, 2010 at 02:04:31PM +0200, Jes.Sorensen@redhat.com wrote:
> From: Jes Sorensen <Jes.Sorensen@redhat.com>
> 
> Fix bvprintf to respect space padding when printing hex numbers
> and the caller specifies alignment without zero padding, eg. %2x
> as opposed to %02x

Hi Jes,

I thought your patch would increase stack space in 16bit mode, but
oddly it seems to actually reduce stack space (at least on gcc4.4.4).

So, the patch looks good, but I think you missed the case where the
length given is smaller than the actual number, and %p needs to use
zero padding.  How about the below instead.

-Kevin


diff --git a/src/output.c b/src/output.c
index 50d54ce..936d3d8 100644
--- a/src/output.c
+++ b/src/output.c
@@ -195,21 +195,24 @@ putsinglehex(struct putcinfo *action, u32 val)
 
 // Output an integer in hexadecimal.
 static void
-puthex(struct putcinfo *action, u32 val, int width)
+puthex(struct putcinfo *action, u32 val, int width, int spacepad)
 {
     if (!width) {
         u32 tmp = val;
         width = 1;
-        if (tmp > 0xffff) {
-            width += 4;
-            tmp >>= 16;
-        }
-        if (tmp > 0xff) {
-            width += 2;
-            tmp >>= 8;
+        while (tmp >>= 4)
+            width++;
+    } else if (spacepad)  {
+        u32 tmp = val;
+        u32 count = 1;
+        while (tmp >>= 4)
+            count++;
+        if (width > count) {
+            count = width - count;
+            width -= count;
+            while (count--)
+                putc(action, ' ');
         }
-        if (tmp > 0xf)
-            width += 1;
     }
 
     switch (width) {
@@ -244,11 +247,15 @@ bvprintf(struct putcinfo *action, const char *fmt, va_list args)
         }
         const char *n = s+1;
         int field_width = 0;
+        int spacepad = 1;
         for (;;) {
             c = GET_GLOBAL(*(u8*)n);
             if (!isdigit(c))
                 break;
-            field_width = field_width * 10 + c - '0';
+            if (!field_width && (c == '0'))
+                spacepad = 0;
+            else
+                field_width = field_width * 10 + c - '0';
             n++;
         }
         if (c == 'l') {
@@ -279,9 +286,10 @@ bvprintf(struct putcinfo *action, const char *fmt, va_list args)
             putc(action, '0');
             putc(action, 'x');
             field_width = 8;
+            spacepad = 0;
         case 'x':
             val = va_arg(args, s32);
-            puthex(action, val, field_width);
+            puthex(action, val, field_width, spacepad);
             break;
         case 'c':
             val = va_arg(args, int);
@@ -333,7 +341,7 @@ __dprintf(const char *fmt, ...)
         if (cur != &MainThread) {
             // Show "thread id" for this debug message.
             putc_debug(&debuginfo, '|');
-            puthex(&debuginfo, (u32)cur, 8);
+            puthex(&debuginfo, (u32)cur, 8, 0);
             putc_debug(&debuginfo, '|');
             putc_debug(&debuginfo, ' ');
         }
@@ -411,12 +419,12 @@ hexdump(const void *d, int len)
     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;

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [Qemu-devel] Re: [PATCH] SeaBIOS: Fix bvprintf() to respect padding for hex printing.
  2010-06-17  2:42 ` [Qemu-devel] " Kevin O'Connor
@ 2010-06-17  7:11   ` Jes Sorensen
  0 siblings, 0 replies; 3+ messages in thread
From: Jes Sorensen @ 2010-06-17  7:11 UTC (permalink / raw)
  To: Kevin O'Connor; +Cc: seabios, qemu-devel

On 06/17/10 04:42, Kevin O'Connor wrote:
> On Mon, Jun 14, 2010 at 02:04:31PM +0200, Jes.Sorensen@redhat.com wrote:
>> From: Jes Sorensen <Jes.Sorensen@redhat.com>
>>
>> Fix bvprintf to respect space padding when printing hex numbers
>> and the caller specifies alignment without zero padding, eg. %2x
>> as opposed to %02x
> 
> I thought your patch would increase stack space in 16bit mode, but
> oddly it seems to actually reduce stack space (at least on gcc4.4.4).
> 
> So, the patch looks good, but I think you missed the case where the
> length given is smaller than the actual number, and %p needs to use
> zero padding.  How about the below instead.

Hi Kevin,

DOH, you're right! Your patch looks good to me so
Signed-off-by: Jes Sorensen <Jes.Sorensen@redhat.com>

Thanks for catching this.

Cheers,
Jes

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-06-17  7:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-14 12:04 [Qemu-devel] [PATCH] SeaBIOS: Fix bvprintf() to respect padding for hex printing Jes.Sorensen
2010-06-17  2:42 ` [Qemu-devel] " Kevin O'Connor
2010-06-17  7:11   ` Jes Sorensen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.