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)';
};
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 = {
name = grub-menulst2cfg;
mansection = 1;

View file

@ -637,10 +637,13 @@ grub_lltoa (char *str, int c, unsigned long long n)
}
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;
grub_size_t n = 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_str (const char *s);
auto void write_fill (const char ch, int n);
@ -659,18 +662,166 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list ar
write_char (*s++);
}
void write_fill (const char ch, int n)
void write_fill (const char ch, int count_fill)
{
int i;
for (i = 0; i < n; i++)
for (i = 0; i < count_fill; i++)
write_char (ch);
}
fmt = fmt0;
while ((c = *fmt++) != 0)
{
if (c != '%')
write_char (c);
continue;
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')
{
c = *fmt++;
if (c == 'l')
c = *fmt++;
}
switch (c)
{
case 'p':
case 'x':
case 'u':
case 'd':
case 'c':
case 'C':
case 's':
count_args++;
break;
}
}
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')
{
c = *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;
}
}
for (n = 0; n < count_args; n++)
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;
@ -678,10 +829,20 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list ar
unsigned int format2 = ~ 0U;
char zerofill = ' ';
int rightfill = 0;
int n;
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 =='-')
{
@ -721,6 +882,17 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list ar
fmt = p;
}
}
if (*fmt == '$')
{
curn = format1 - 1;
fmt++;
format1 = 0;
format2 = ~ 0U;
zerofill = ' ';
rightfill = 0;
goto rescan;
}
c = *fmt++;
if (c == 'l')
@ -734,6 +906,9 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list ar
}
}
if (curn >= count_args)
continue;
switch (c)
{
case 'p':
@ -747,32 +922,15 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list ar
/* Fall through. */
case 'd':
if (longlongfmt)
{
long long ll;
ll = va_arg (args, long long);
grub_lltoa (tmp, c, ll);
}
grub_lltoa (tmp, c, args[curn].ll);
else if (longfmt && unsig)
{
unsigned long l = va_arg (args, unsigned long);
grub_lltoa (tmp, c, l);
}
grub_lltoa (tmp, c, (unsigned long) args[curn].l);
else if (longfmt)
{
long l = va_arg (args, long);
grub_lltoa (tmp, c, l);
}
grub_lltoa (tmp, c, args[curn].l);
else if (unsig)
{
unsigned u = va_arg (args, unsigned);
grub_lltoa (tmp, c, u);
}
grub_lltoa (tmp, c, (unsigned) args[curn].i);
else
{
n = va_arg (args, int);
grub_lltoa (tmp, c, n);
}
grub_lltoa (tmp, c, args[curn].i);
if (! rightfill && grub_strlen (tmp) < format1)
write_fill (zerofill, format1 - grub_strlen (tmp));
write_str (tmp);
@ -781,13 +939,12 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list ar
break;
case 'c':
n = va_arg (args, int);
write_char (n & 0xff);
write_char (args[curn].i & 0xff);
break;
case 'C':
{
grub_uint32_t code = va_arg (args, grub_uint32_t);
grub_uint32_t code = args[curn].w;
int shift;
unsigned mask;
@ -836,7 +993,7 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list ar
break;
case 's':
p = va_arg (args, char *);
p = args[curn].p;
if (p)
{
grub_size_t len = 0;
@ -863,7 +1020,6 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt, va_list ar
break;
}
}
}
*str = '\0';

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);