Support %1$d syntax.

* tests/printf_unit_test.c: New file.
	* Makefile.util.def (printf_test): New test.
	* grub-core/kern/misc.c (grub_vsnprintf_real): Support %1$d syntax.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2011-11-11 21:14:41 +01:00
parent c76b541762
commit 12d4f965cd
3 changed files with 378 additions and 164 deletions

View file

@ -660,6 +660,21 @@ program = {
ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)'; ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
}; };
program = {
testcase;
name = printf_test;
common = tests/printf_unit_test.c;
common = tests/lib/unit_test.c;
common = grub-core/kern/list.c;
common = grub-core/kern/misc.c;
common = grub-core/tests/lib/test.c;
ldadd = libgrubmods.a;
ldadd = libgrubgcry.a;
ldadd = libgrubkern.a;
ldadd = grub-core/gnulib/libgnu.a;
ldadd = '$(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
};
program = { program = {
name = grub-menulst2cfg; name = grub-menulst2cfg;
mansection = 1; mansection = 1;

View file

@ -637,10 +637,13 @@ grub_lltoa (char *str, int c, unsigned long long n)
} }
static int static int
grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list args) grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0, va_list args_in)
{ {
char c; char c;
grub_size_t n = 0;
grub_size_t count = 0; grub_size_t count = 0;
grub_size_t count_args = 0;
const char *fmt;
auto void write_char (unsigned char ch); auto void write_char (unsigned char ch);
auto void write_str (const char *s); auto void write_str (const char *s);
auto void write_fill (const char ch, int n); auto void write_fill (const char ch, int n);
@ -659,209 +662,362 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list ar
write_char (*s++); write_char (*s++);
} }
void write_fill (const char ch, int n) void write_fill (const char ch, int count_fill)
{ {
int i; int i;
for (i = 0; i < n; i++) for (i = 0; i < count_fill; i++)
write_char (ch); write_char (ch);
} }
fmt = fmt0;
while ((c = *fmt++) != 0) while ((c = *fmt++) != 0)
{ {
if (c != '%') if (c != '%')
write_char (c); continue;
else
if (*fmt && *fmt =='-')
fmt++;
while (*fmt && grub_isdigit (*fmt))
fmt++;
if (*fmt && *fmt == '$')
fmt++;
if (*fmt && *fmt =='-')
fmt++;
while (*fmt && grub_isdigit (*fmt))
fmt++;
if (*fmt && *fmt =='.')
fmt++;
while (*fmt && grub_isdigit (*fmt))
fmt++;
c = *fmt++;
if (c == 'l')
{ {
char tmp[32]; c = *fmt++;
char *p; if (c == 'l')
unsigned int format1 = 0; c = *fmt++;
unsigned int format2 = ~ 0U; }
char zerofill = ' '; switch (c)
int rightfill = 0; {
int n; case 'p':
int longfmt = 0; case 'x':
int longlongfmt = 0; case 'u':
int unsig = 0; case 'd':
case 'c':
case 'C':
case 's':
count_args++;
break;
}
}
if (*fmt && *fmt =='-') enum { INT, WCHAR, LONG, LONGLONG, POINTER } types[count_args];
union
{
int i;
grub_uint32_t w;
long l;
long long ll;
void *p;
} args[count_args];
grub_memset (types, 0, sizeof (types));
fmt = fmt0;
n = 0;
while ((c = *fmt++) != 0)
{
int longfmt = 0;
int longlongfmt = 0;
grub_size_t curn;
const char *p;
if (c != '%')
continue;
curn = n++;
if (*fmt && *fmt =='-')
fmt++;
while (*fmt && grub_isdigit (*fmt))
fmt++;
p = fmt;
if (*fmt && *fmt == '$')
{
curn = grub_strtoull (p, 0, 10) - 1;
fmt++;
}
while (*fmt && grub_isdigit (*fmt))
fmt++;
c = *fmt++;
if (c == 'l')
{
c = *fmt++;
longfmt = 1;
if (c == 'l')
{ {
rightfill = 1; c = *fmt++;
fmt++; longlongfmt = 1;
} }
}
if (curn >= count_args)
continue;
switch (c)
{
case 'x':
case 'u':
case 'd':
if (longlongfmt)
types[curn] = LONGLONG;
else if (longfmt)
types[curn] = LONG;
else
types[curn] = INT;
break;
case 'p':
case 's':
types[curn] = POINTER;
break;
case 'c':
types[curn] = INT;
break;
case 'C':
types[curn] = WCHAR;
break;
}
}
p = (char *) fmt; for (n = 0; n < count_args; n++)
/* Read formatting parameters. */ switch (types[n])
{
case WCHAR:
args[n].w = va_arg (args_in, grub_uint32_t);
break;
case POINTER:
args[n].p = va_arg (args_in, void *);
break;
case INT:
args[n].i = va_arg (args_in, int);
break;
case LONG:
args[n].l = va_arg (args_in, long);
break;
case LONGLONG:
args[n].ll = va_arg (args_in, long long);
break;
}
fmt = fmt0;
n = 0;
while ((c = *fmt++) != 0)
{
char tmp[32];
char *p;
unsigned int format1 = 0;
unsigned int format2 = ~ 0U;
char zerofill = ' ';
int rightfill = 0;
int longfmt = 0;
int longlongfmt = 0;
int unsig = 0;
grub_size_t curn;
if (c != '%')
{
write_char (c);
continue;
}
curn = n++;
rescan:;
if (*fmt && *fmt =='-')
{
rightfill = 1;
fmt++;
}
p = (char *) fmt;
/* Read formatting parameters. */
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 == '.')
{
p++;
fmt++;
while (*p && grub_isdigit (*p)) while (*p && grub_isdigit (*p))
p++; p++;
if (p > fmt) if (p > fmt)
{ {
char s[p - fmt + 1]; char fstr[p - fmt + 1];
grub_strncpy (s, fmt, p - fmt); grub_strncpy (fstr, fmt, p - fmt);
s[p - fmt] = 0; fstr[p - fmt] = 0;
if (s[0] == '0') format2 = grub_strtoul (fstr, 0, 10);
zerofill = '0';
format1 = grub_strtoul (s, 0, 10);
fmt = p; fmt = p;
} }
}
if (*fmt == '$')
{
curn = format1 - 1;
fmt++;
format1 = 0;
format2 = ~ 0U;
zerofill = ' ';
rightfill = 0;
if (*p && *p == '.') goto rescan;
{ }
p++;
fmt++;
while (*p && grub_isdigit (*p))
p++;
if (p > fmt)
{
char fstr[p - fmt + 1];
grub_strncpy (fstr, fmt, p - fmt);
fstr[p - fmt] = 0;
format2 = grub_strtoul (fstr, 0, 10);
fmt = p;
}
}
c = *fmt++;
if (c == 'l')
{
longfmt = 1;
c = *fmt++; c = *fmt++;
if (c == 'l') if (c == 'l')
{ {
longfmt = 1; longlongfmt = 1;
c = *fmt++; c = *fmt++;
if (c == 'l')
{
longlongfmt = 1;
c = *fmt++;
}
} }
}
switch (c) if (curn >= count_args)
{ continue;
case 'p':
write_str ("0x");
c = 'x';
longlongfmt |= (sizeof (void *) == sizeof (long long));
/* Fall through. */
case 'x':
case 'u':
unsig = 1;
/* Fall through. */
case 'd':
if (longlongfmt)
{
long long ll;
ll = va_arg (args, long long); switch (c)
grub_lltoa (tmp, c, ll); {
} case 'p':
else if (longfmt && unsig) write_str ("0x");
{ c = 'x';
unsigned long l = va_arg (args, unsigned long); longlongfmt |= (sizeof (void *) == sizeof (long long));
grub_lltoa (tmp, c, l); /* Fall through. */
} case 'x':
else if (longfmt) case 'u':
{ unsig = 1;
long l = va_arg (args, long); /* Fall through. */
grub_lltoa (tmp, c, l); case 'd':
} if (longlongfmt)
else if (unsig) grub_lltoa (tmp, c, args[curn].ll);
{ else if (longfmt && unsig)
unsigned u = va_arg (args, unsigned); grub_lltoa (tmp, c, (unsigned long) args[curn].l);
grub_lltoa (tmp, c, u); else if (longfmt)
} grub_lltoa (tmp, c, args[curn].l);
else else if (unsig)
{ grub_lltoa (tmp, c, (unsigned) args[curn].i);
n = va_arg (args, int); else
grub_lltoa (tmp, c, n); grub_lltoa (tmp, c, args[curn].i);
} if (! rightfill && grub_strlen (tmp) < format1)
if (! rightfill && grub_strlen (tmp) < format1) write_fill (zerofill, format1 - grub_strlen (tmp));
write_fill (zerofill, format1 - grub_strlen (tmp)); write_str (tmp);
write_str (tmp); if (rightfill && grub_strlen (tmp) < format1)
if (rightfill && grub_strlen (tmp) < format1) write_fill (zerofill, format1 - grub_strlen (tmp));
write_fill (zerofill, format1 - grub_strlen (tmp)); break;
break;
case 'c': case 'c':
n = va_arg (args, int); write_char (args[curn].i & 0xff);
write_char (n & 0xff); break;
break;
case 'C': case 'C':
{
grub_uint32_t code = args[curn].w;
int shift;
unsigned mask;
if (code <= 0x7f)
{ {
grub_uint32_t code = va_arg (args, grub_uint32_t); shift = 0;
int shift; mask = 0;
unsigned mask; }
else if (code <= 0x7ff)
if (code <= 0x7f) {
{ shift = 6;
shift = 0; mask = 0xc0;
mask = 0; }
} else if (code <= 0xffff)
else if (code <= 0x7ff) {
{ shift = 12;
shift = 6; mask = 0xe0;
mask = 0xc0; }
} else if (code <= 0x1fffff)
else if (code <= 0xffff) {
{ shift = 18;
shift = 12; mask = 0xf0;
mask = 0xe0; }
} else if (code <= 0x3ffffff)
else if (code <= 0x1fffff) {
{ shift = 24;
shift = 18; mask = 0xf8;
mask = 0xf0; }
} else if (code <= 0x7fffffff)
else if (code <= 0x3ffffff) {
{ shift = 30;
shift = 24; mask = 0xfc;
mask = 0xf8; }
} else
else if (code <= 0x7fffffff) {
{ code = '?';
shift = 30; shift = 0;
mask = 0xfc; mask = 0;
}
else
{
code = '?';
shift = 0;
mask = 0;
}
write_char (mask | (code >> shift));
for (shift -= 6; shift >= 0; shift -= 6)
write_char (0x80 | (0x3f & (code >> shift)));
} }
break;
case 's': write_char (mask | (code >> shift));
p = va_arg (args, char *);
if (p)
{
grub_size_t len = 0;
while (len < format2 && p[len])
len++;
if (!rightfill && len < format1) for (shift -= 6; shift >= 0; shift -= 6)
write_fill (zerofill, format1 - len); write_char (0x80 | (0x3f & (code >> shift)));
}
break;
grub_size_t i; case 's':
for (i = 0; i < len; i++) p = args[curn].p;
write_char (*p++); if (p)
{
grub_size_t len = 0;
while (len < format2 && p[len])
len++;
if (rightfill && len < format1) if (!rightfill && len < format1)
write_fill (zerofill, format1 - len); write_fill (zerofill, format1 - len);
}
else
write_str ("(null)");
break; grub_size_t i;
for (i = 0; i < len; i++)
write_char (*p++);
default: if (rightfill && len < format1)
write_char (c); write_fill (zerofill, format1 - len);
break;
} }
else
write_str ("(null)");
break;
default:
write_char (c);
break;
} }
} }

43
tests/printf_unit_test.c Normal file
View file

@ -0,0 +1,43 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2011 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <grub/test.h>
#include <grub/misc.h>
static void
printf_test (void)
{
char real[512];
char expected[512];
grub_snprintf (real, sizeof (real), "%d %d %d", 1, 2, 3);
snprintf (expected, sizeof (expected), "%d %d %d", 1, 2, 3);
grub_test_assert (strcmp (real, expected) == 0);
grub_snprintf (real, sizeof (real), "%3$d %2$d %1$d", 1, 2, 3);
snprintf (expected, sizeof (expected), "%3$d %2$d %1$d", 1, 2, 3);
grub_test_assert (strcmp (real, expected) == 0);
grub_snprintf (real, sizeof (real), "%d %lld %d", 1, 2LL, 3);
snprintf (expected, sizeof (expected), "%d %lld %d", 1, 2LL, 3);
grub_test_assert (strcmp (real, expected) == 0);
grub_snprintf (real, sizeof (real), "%3$d %2$lld %1$d", 1, 2LL, 3);
snprintf (expected, sizeof (expected), "%3$d %2$lld %1$d", 1, 2LL, 3);
grub_test_assert (strcmp (real, expected) == 0);
}
GRUB_UNIT_TEST ("printf_unit_test", printf_test);