Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1KUmWa-00049E-Pb for mharc-grub-devel@gnu.org; Sun, 17 Aug 2008 13:58:44 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KUmWY-00047U-LK for grub-devel@gnu.org; Sun, 17 Aug 2008 13:58:42 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KUmWY-000470-9d for grub-devel@gnu.org; Sun, 17 Aug 2008 13:58:42 -0400 Received: from [199.232.76.173] (port=59703 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KUmWX-00046n-PS for grub-devel@gnu.org; Sun, 17 Aug 2008 13:58:41 -0400 Received: from ti-out-0910.google.com ([209.85.142.188]:1711) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KUmWW-0005pt-Od for grub-devel@gnu.org; Sun, 17 Aug 2008 13:58:41 -0400 Received: by ti-out-0910.google.com with SMTP id u5so717775tia.10 for ; Sun, 17 Aug 2008 10:58:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type; bh=zfA2aMNF+89YYBAbG4OnYuyR83JG7xpPIHUubDRgKyg=; b=QkjfA/j+s/DG1E3t7YyIqd351QfMb8oHLpRSljHxCe0YI8cM1lAv01QY5qDqt3wG4k OqLjbfqP6RVSSXyrnebDYgdJ+YjT/8N9NtJFMfTH65X5cxVjr3auHC8xzS2G6FbZq0CW dPgs4jVgsuVD7TI8+YiQmF7R1DBcMyg2sRJ8c= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type; b=Op75j6gMdVSOd0uDUmBV4NgNn3xMHvJvdbZNcOkPU9w8W3HPWHbQ8Ajo6EzKpwmOAq 6My45GjOzZdi9Vxt9+7qCiev2pRMdJwShnsDELDWJVVAb7KE13hVzpmSnAyNEn9iKJ9U jZ0XRxCUOr5AH49QXs9Z4WYJUYrTCqbzOVqMc= Received: by 10.110.92.8 with SMTP id p8mr4709480tib.9.1218995918105; Sun, 17 Aug 2008 10:58:38 -0700 (PDT) Received: by 10.110.26.19 with HTTP; Sun, 17 Aug 2008 10:58:38 -0700 (PDT) Message-ID: Date: Sun, 17 Aug 2008 23:28:38 +0530 From: "Deepak Vankadaru" To: grub-devel@gnu.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_33310_13620362.1218995918088" X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 2) Subject: precision formatting in grub_printf X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 17 Aug 2008 17:58:43 -0000 ------=_Part_33310_13620362.1218995918088 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi I have implemented precision formatting support in grub_printf. Following is the diff (of kern/misc.c) Thanks Deepak ###############start of diff *** kern/misc.c 2008-08-18 04:46:36.000000000 +0530 --- /home/deepakv/orig/grub2-1.96+20080724/kern/misc.c 2008-06-16 06:12:48.000000000 +0530 *************** *** 565,572 **** *p++ = (d > 9) ? d + 'a' - 10 : d + '0'; } while (n /= base); - if ( p == str) - *p++ = '0'; *p = 0; grub_reverse (str); --- 565,570 ---- *************** *** 682,693 **** write_char (*s++); } - void write_str_limit (const char *s, int count) - { - while (*s && count--) - write_char (*s++); - } - void write_fill (const char ch, int n) { int i; --- 680,685 ---- *************** *** 704,711 **** char tmp[32]; char *p; unsigned int format1 = 0; ! unsigned int format2 = 1; ! grub_bool_t format2_default = TRUE; char zerofill = ' '; int rightfill = 0; int n; --- 696,702 ---- char tmp[32]; char *p; unsigned int format1 = 0; ! unsigned int format2 = 3; char zerofill = ' '; int rightfill = 0; int n; *************** *** 723,737 **** while (*p && grub_isdigit (*p)) p++; ! if (p > fmt || *p == '.') { char s[p - fmt + 1]; grub_strncpy (s, fmt, p - fmt); s[p - fmt] = 0; if (s[0] == '0') zerofill = '0'; ! if (p - fmt) ! format1 = grub_strtoul (s, 0, 10); fmt = p; if (*p && *p == '.') { --- 714,727 ---- while (*p && grub_isdigit (*p)) p++; ! if (p > fmt) { char s[p - fmt + 1]; grub_strncpy (s, fmt, p - fmt); s[p - fmt] = 0; if (s[0] == '0') zerofill = '0'; ! format1 = grub_strtoul (s, 0, 10); fmt = p; if (*p && *p == '.') { *************** *** 747,755 **** format2 = grub_strtoul (fstr, 0, 10); fmt = p; } - else - format2 = 0; - format2_default = FALSE; } } --- 737,742 ---- *************** *** 790,806 **** n = va_arg (args, int); grub_itoa (tmp, c, n); } ! if ( format2 == 0 && grub_strcmp (tmp, "0") == 0) ! break; ! if ( grub_strlen (tmp) > format2) ! format2 = grub_strlen (tmp); ! if (! rightfill && format2 < format1) ! write_fill (zerofill, format1 - format2); ! if ( grub_strlen (tmp) < format2) ! write_fill ('0', format2 - grub_strlen (tmp)); write_str (tmp); ! if (rightfill && format2 < format1) ! write_fill (zerofill, format1 - format2); break; case 'c': --- 777,787 ---- n = va_arg (args, int); grub_itoa (tmp, c, n); } ! if (! rightfill && grub_strlen (tmp) < format1) ! write_fill (zerofill, format1 - grub_strlen (tmp)); write_str (tmp); ! if (rightfill && grub_strlen (tmp) < format1) ! write_fill (zerofill, format1 - grub_strlen (tmp)); break; case 'c': *************** *** 862,895 **** p = va_arg (args, char *); if (p) { ! if ( format2_default ) ! { ! if (!rightfill && grub_strlen (p) < format1) ! write_fill (' ', format1 - grub_strlen (p)); ! write_str (p); ! if (rightfill && grub_strlen (p) < format1) ! write_fill (' ', format1 - grub_strlen (p)); ! } ! else ! { ! if (!rightfill && format2 < format1) ! write_fill (' ', format1 - format2); ! ! if ( grub_strlen (p) < format2) ! { ! if ( format2 <= format1) ! write_fill (' ', format2 - grub_strlen (p)); ! else ! write_fill (' ', format1 - grub_strlen (p)); ! } ! ! write_str_limit (p, format2); ! ! if (rightfill && format2 < format1) ! write_fill (' ', format1 - format2); ! } } else write_str ("(null)"); --- 843,855 ---- p = va_arg (args, char *); if (p) { ! if (!rightfill && grub_strlen (p) < format1) ! write_fill (zerofill, format1 - grub_strlen (p)); ! write_str (p); ! if (rightfill && grub_strlen (p) < format1) ! write_fill (zerofill, format1 - grub_strlen (p)); } else write_str ("(null)"); ###############end of diff ######start of test case int main() { typedef int (*printf_ptr)(char const *str, ...); printf_ptr p; if(getenv("GRUB_PRINTF")) { printf("using grub_printf\n"); p = grub_printf; } else { printf("using printf\n"); p = printf; } p("##%4.2d##\n",1); p("##%4.2d##\n",12); p("##%4.2d##\n",123); p("##%4.2d##\n",1234); p("##%4.2d##\n",12345); p("##%3.3d##\n",12); p("##%3.3d##\n",123); p("##%3.3d##\n",1234); p("##%2.4d##\n",12345); p("##%2.4d##\n",1234); p("##%2.4d##\n",123); p("##%2.4d##\n",12); p("##%2.4d##\n",1); p("##%.0d##\n",0); p("##%.0d##\n",1); p("##%4.2s##\n","1"); p("##%4.2s##\n","12"); p("##%4.2s##\n","123"); p("##%4.2s##\n","1234"); p("##%4.2s##\n","12345"); p("##%3.3s##\n","12"); p("##%3.3s##\n","123"); p("##%3.3s##\n","1234"); p("##%2.4s##\n","12345"); p("##%2.4s##\n","1234"); p("##%2.4s##\n","123"); p("##%2.4s##\n","12"); p("##%2.4s##\n","1"); return 0; } ------=_Part_33310_13620362.1218995918088 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
Hi

I have implemented precision formatting support in grub_printf. Following is the diff (of kern/misc.c)

Thanks
Deepak

###############start of diff
*** kern/misc.c    2008-08-18 04:46:36.000000000 +0530
--- /home/deepakv/orig/grub2-1.96+20080724/kern/misc.c    2008-06-16 06:12:48.000000000 +0530
***************
*** 565,572 ****
        *p++ = (d > 9) ? d + 'a' - 10 : d + '0';
      }
    while (n /= base);
-   if ( p == str)
-     *p++ = '0';
    *p = 0;
 
    grub_reverse (str);
--- 565,570 ----
***************
*** 682,693 ****
      write_char (*s++);
      }
 
-   void write_str_limit (const char *s, int count)
-     {
-       while (*s && count--)
-     write_char (*s++);
-     }
-
    void write_fill (const char ch, int n)
      {
        int i;
--- 680,685 ----
***************
*** 704,711 ****
        char tmp[32];
        char *p;
        unsigned int format1 = 0;
!       unsigned int format2 = 1;
!       grub_bool_t format2_default = TRUE;
        char zerofill = ' ';
        int rightfill = 0;
        int n;
--- 696,702 ----
        char tmp[32];
        char *p;
        unsigned int format1 = 0;
!       unsigned int format2 = 3;
        char zerofill = ' ';
        int rightfill = 0;
        int n;
***************
*** 723,737 ****
        while (*p && grub_isdigit (*p))
          p++;
 
!       if (p > fmt || *p == '.')
          {
            char s[p - fmt + 1];
            grub_strncpy (s, fmt, p - fmt);
            s[p - fmt] = 0;
            if (s[0] == '0')
          zerofill = '0';
!           if (p - fmt)
!             format1 = grub_strtoul (s, 0, 10);
            fmt = p;
            if (*p && *p == '.')
          {
--- 714,727 ----
        while (*p && grub_isdigit (*p))
          p++;
 
!       if (p > fmt)
          {
            char s[p - fmt + 1];
            grub_strncpy (s, fmt, p - fmt);
            s[p - fmt] = 0;
            if (s[0] == '0')
          zerofill = '0';
!           format1 = grub_strtoul (s, 0, 10);
            fmt = p;
            if (*p && *p == '.')
          {
***************
*** 747,755 ****
                format2 = grub_strtoul (fstr, 0, 10);
                fmt = p;
              }
-           else
-             format2 = 0;
-           format2_default = FALSE;
          }
          }
 
--- 737,742 ----
***************
*** 790,806 ****
              n = va_arg (args, int);
            grub_itoa (tmp, c, n);
          }
!           if ( format2 == 0 && grub_strcmp (tmp, "0") == 0)
!         break;
!           if ( grub_strlen (tmp) > format2)
!             format2 = grub_strlen (tmp);
!           if (! rightfill && format2 < format1)
!         write_fill (zerofill, format1 - format2);
!           if ( grub_strlen (tmp) < format2)
!         write_fill ('0', format2 - grub_strlen (tmp));
            write_str (tmp);
!           if (rightfill && format2 < format1)
!         write_fill (zerofill, format1 - format2);
            break;
           
          case 'c':
--- 777,787 ----
              n = va_arg (args, int);
            grub_itoa (tmp, c, n);
          }
!           if (! rightfill && grub_strlen (tmp) < format1)
!         write_fill (zerofill, format1 - grub_strlen (tmp));
            write_str (tmp);
!           if (rightfill && grub_strlen (tmp) < format1)
!         write_fill (zerofill, format1 - grub_strlen (tmp));
            break;
           
          case 'c':
***************
*** 862,895 ****
            p = va_arg (args, char *);
            if (p)
          {
!           if ( format2_default )
!             {
!               if (!rightfill && grub_strlen (p) < format1)
!                 write_fill (' ', format1 - grub_strlen (p));
           
!               write_str (p);
           
!               if (rightfill && grub_strlen (p) < format1)
!                 write_fill (' ', format1 - grub_strlen (p));
!             }
!             else
!             {
!               if (!rightfill && format2 < format1)
!                 write_fill (' ', format1 - format2);
!          
!               if ( grub_strlen (p) < format2)
!                 {
!                   if ( format2 <= format1)
!                     write_fill (' ', format2 - grub_strlen (p));
!                   else
!                     write_fill (' ', format1 - grub_strlen (p));
!                 }
!
!               write_str_limit (p, format2);
!          
!               if (rightfill && format2 < format1)
!                 write_fill (' ', format1 - format2);
!             }
          }
            else
          write_str ("(null)");
--- 843,855 ----
            p = va_arg (args, char *);
            if (p)
          {
!           if (!rightfill && grub_strlen (p) < format1)
!             write_fill (zerofill, format1 - grub_strlen (p));
           
!           write_str (p);
           
!           if (rightfill && grub_strlen (p) < format1)
!             write_fill (zerofill, format1 - grub_strlen (p));
          }
            else
          write_str ("(null)");

###############end of diff

######start of test case
int main()
{
    typedef int (*printf_ptr)(char const *str, ...);
    printf_ptr p;
    if(getenv("GRUB_PRINTF"))
    {
        printf("using grub_printf\n");
        p = grub_printf;
    }
    else
    {
        printf("using printf\n");
        p = printf;
    }
    p("##%4.2d##\n",1);
    p("##%4.2d##\n",12);
    p("##%4.2d##\n",123);
    p("##%4.2d##\n",1234);
    p("##%4.2d##\n",12345);
    p("##%3.3d##\n",12);
    p("##%3.3d##\n",123);
    p("##%3.3d##\n",1234);
    p("##%2.4d##\n",12345);
    p("##%2.4d##\n",1234);
    p("##%2.4d##\n",123);
    p("##%2.4d##\n",12);
    p("##%2.4d##\n",1);
    p("##%.0d##\n",0);
    p("##%.0d##\n",1);
    p("##%4.2s##\n","1");
    p("##%4.2s##\n","12");
    p("##%4.2s##\n","123");
    p("##%4.2s##\n","1234");
    p("##%4.2s##\n","12345");
    p("##%3.3s##\n","12");
    p("##%3.3s##\n","123");
    p("##%3.3s##\n","1234");
    p("##%2.4s##\n","12345");
    p("##%2.4s##\n","1234");
    p("##%2.4s##\n","123");
    p("##%2.4s##\n","12");
    p("##%2.4s##\n","1");
    return 0;
}
------=_Part_33310_13620362.1218995918088--