diff options
Diffstat (limited to 'usr.bin/printf')
-rw-r--r-- | usr.bin/printf/Makefile | 6 | ||||
-rw-r--r-- | usr.bin/printf/printf.1 | 5 | ||||
-rw-r--r-- | usr.bin/printf/printf.c | 39 | ||||
-rw-r--r-- | usr.bin/printf/tests/Makefile | 22 | ||||
-rw-r--r-- | usr.bin/printf/tests/legacy_test.sh | 6 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.b.out | 1 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.d.out | 1 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.f.out | 1 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.l1.out | 1 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.l2.out | 1 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.m1.out | bin | 0 -> 7 bytes | |||
-rw-r--r-- | usr.bin/printf/tests/regress.m2.out | 1 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.m3.out | 4 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.m4.out | 1 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.m5.out | 1 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.s.out | 1 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.sh | 23 | ||||
-rw-r--r-- | usr.bin/printf/tests/regress.zero.out | 1 |
18 files changed, 112 insertions, 3 deletions
diff --git a/usr.bin/printf/Makefile b/usr.bin/printf/Makefile index c91c4e3..31047c5 100644 --- a/usr.bin/printf/Makefile +++ b/usr.bin/printf/Makefile @@ -1,6 +1,12 @@ # @(#)Makefile 8.1 (Berkeley) 6/6/93 # $FreeBSD$ +.include <bsd.own.mk> + PROG= printf +.if ${MK_TESTS} != "no" +SUBDIR+= tests +.endif + .include <bsd.prog.mk> diff --git a/usr.bin/printf/printf.1 b/usr.bin/printf/printf.1 index c5bba97..7256b53d 100644 --- a/usr.bin/printf/printf.1 +++ b/usr.bin/printf/printf.1 @@ -31,7 +31,7 @@ .\" @(#)printf.1 8.1 (Berkeley) 6/6/93 .\" $FreeBSD$ .\" -.Dd May 28, 2011 +.Dd April 21, 2014 .Dt PRINTF 1 .Os .Sh NAME @@ -290,6 +290,9 @@ octal escapes are .Cm \e0 Ns Ar num instead of .Cm \e Ns Ar num . +.It Cm n$ +Allows reordering of the output according to +.Ar argument . .It Cm \&% Print a `%'; no argument is used. .El diff --git a/usr.bin/printf/printf.c b/usr.bin/printf/printf.c index 2368f184..7ed0c21 100644 --- a/usr.bin/printf/printf.c +++ b/usr.bin/printf/printf.c @@ -1,4 +1,5 @@ /*- + * Copyright 2010 Nexenta Systems, Inc. All rights reserved. * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * @@ -61,14 +62,19 @@ static const char rcsid[] = #include <wchar.h> #ifdef SHELL -#define main printfcmd +#define main printfcmd #include "bltin/bltin.h" #include "error.h" #include "options.h" #endif -#define PF(f, func) do { \ +#define PF(f, func) do { \ char *b = NULL; \ + int dollar = 0; \ + if (*f == '$') { \ + dollar++; \ + *f = '%'; \ + } \ if (havewidth) \ if (haveprec) \ (void)asprintf(&b, f, fieldwidth, precision, func); \ @@ -82,6 +88,8 @@ static const char rcsid[] = (void)fputs(b, stdout); \ free(b); \ } \ + if (dollar) \ + *f = '$'; \ } while (0) static int asciicode(void); @@ -96,6 +104,8 @@ static const char static char *mknum(char *, char); static void usage(void); +static int myargc; +static char **myargv; static char **gargv; int @@ -146,7 +156,13 @@ main(int argc, char *argv[]) chopped = escape(fmt, 1, &len); /* backslash interpretation */ rval = end = 0; gargv = ++argv; + for (;;) { + char **maxargv = gargv; + + myargv = gargv; + for (myargc = 0; gargv[myargc]; myargc++) + /* nop */; start = fmt; while (fmt < format + len) { if (fmt[0] == '%') { @@ -168,7 +184,10 @@ main(int argc, char *argv[]) start = fmt; } else fmt++; + if (gargv > maxargv) + maxargv = gargv; } + gargv = maxargv; if (end == 1) { warnx("missing format character"); @@ -202,6 +221,22 @@ printf_doformat(char *start, int *rval) char convch, nextch; fmt = start + 1; + + /* look for "n$" field index specifier */ + fmt += strspn(fmt, skip2); + if ((*fmt == '$') && (fmt != (start + 1))) { + int idx = atoi(start + 1); + if (idx <= myargc) { + gargv = &myargv[idx - 1]; + } else { + gargv = &myargv[myargc]; + } + start = fmt; + fmt++; + } else { + fmt = start + 1; + } + /* skip to field width */ fmt += strspn(fmt, skip1); if (*fmt == '*') { diff --git a/usr.bin/printf/tests/Makefile b/usr.bin/printf/tests/Makefile new file mode 100644 index 0000000..da3f533 --- /dev/null +++ b/usr.bin/printf/tests/Makefile @@ -0,0 +1,22 @@ +# $FreeBSD$ + +TESTSDIR= ${TESTSBASE}/usr.bin/printf + +TAP_TESTS_SH= legacy_test + +FILESDIR= ${TESTSDIR} +FILES= regress.b.out +FILES+= regress.d.out +FILES+= regress.f.out +FILES+= regress.l1.out +FILES+= regress.l2.out +FILES+= regress.m1.out +FILES+= regress.m2.out +FILES+= regress.m3.out +FILES+= regress.m4.out +FILES+= regress.m5.out +FILES+= regress.s.out +FILES+= regress.sh +FILES+= regress.zero.out + +.include <bsd.test.mk> diff --git a/usr.bin/printf/tests/legacy_test.sh b/usr.bin/printf/tests/legacy_test.sh new file mode 100644 index 0000000..1b6b806 --- /dev/null +++ b/usr.bin/printf/tests/legacy_test.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# $FreeBSD$ + +SRCDIR="$(dirname "${0}")"; export SRCDIR + +m4 "${SRCDIR}/../regress.m4" "${SRCDIR}/regress.sh" | sh diff --git a/usr.bin/printf/tests/regress.b.out b/usr.bin/printf/tests/regress.b.out new file mode 100644 index 0000000..0373d93 --- /dev/null +++ b/usr.bin/printf/tests/regress.b.out @@ -0,0 +1 @@ +abcdef diff --git a/usr.bin/printf/tests/regress.d.out b/usr.bin/printf/tests/regress.d.out new file mode 100644 index 0000000..da83c08 --- /dev/null +++ b/usr.bin/printf/tests/regress.d.out @@ -0,0 +1 @@ +123, 123,00123,00123,00123 diff --git a/usr.bin/printf/tests/regress.f.out b/usr.bin/printf/tests/regress.f.out new file mode 100644 index 0000000..127cb8a --- /dev/null +++ b/usr.bin/printf/tests/regress.f.out @@ -0,0 +1 @@ +42.250000,-42.250 ,inf,nan diff --git a/usr.bin/printf/tests/regress.l1.out b/usr.bin/printf/tests/regress.l1.out new file mode 100644 index 0000000..9be0dc9 --- /dev/null +++ b/usr.bin/printf/tests/regress.l1.out @@ -0,0 +1 @@ +228 diff --git a/usr.bin/printf/tests/regress.l2.out b/usr.bin/printf/tests/regress.l2.out new file mode 100644 index 0000000..9be0dc9 --- /dev/null +++ b/usr.bin/printf/tests/regress.l2.out @@ -0,0 +1 @@ +228 diff --git a/usr.bin/printf/tests/regress.m1.out b/usr.bin/printf/tests/regress.m1.out Binary files differnew file mode 100644 index 0000000..a9e063e --- /dev/null +++ b/usr.bin/printf/tests/regress.m1.out diff --git a/usr.bin/printf/tests/regress.m2.out b/usr.bin/printf/tests/regress.m2.out new file mode 100644 index 0000000..8baef1b --- /dev/null +++ b/usr.bin/printf/tests/regress.m2.out @@ -0,0 +1 @@ +abc diff --git a/usr.bin/printf/tests/regress.m3.out b/usr.bin/printf/tests/regress.m3.out new file mode 100644 index 0000000..bbe4e70 --- /dev/null +++ b/usr.bin/printf/tests/regress.m3.out @@ -0,0 +1,4 @@ +%abc +%def +%ghi +%jkl diff --git a/usr.bin/printf/tests/regress.m4.out b/usr.bin/printf/tests/regress.m4.out new file mode 100644 index 0000000..5e0b5d4 --- /dev/null +++ b/usr.bin/printf/tests/regress.m4.out @@ -0,0 +1 @@ +0,0.000000,, diff --git a/usr.bin/printf/tests/regress.m5.out b/usr.bin/printf/tests/regress.m5.out new file mode 100644 index 0000000..2838468 --- /dev/null +++ b/usr.bin/printf/tests/regress.m5.out @@ -0,0 +1 @@ +-d diff --git a/usr.bin/printf/tests/regress.s.out b/usr.bin/printf/tests/regress.s.out new file mode 100644 index 0000000..3d572b9 --- /dev/null +++ b/usr.bin/printf/tests/regress.s.out @@ -0,0 +1 @@ +abc,abc diff --git a/usr.bin/printf/tests/regress.sh b/usr.bin/printf/tests/regress.sh new file mode 100644 index 0000000..84e183b --- /dev/null +++ b/usr.bin/printf/tests/regress.sh @@ -0,0 +1,23 @@ +# $FreeBSD$ + +REGRESSION_START($1) + +echo '1..15' + +REGRESSION_TEST(`b', `printf "abc%b%b" "def\n" "\cghi"') +REGRESSION_TEST(`d', `printf "%d,%5d,%.5d,%0*d,%.*d\n" 123 123 123 5 123 5 123') +REGRESSION_TEST(`f', `printf "%f,%-8.3f,%f,%f\n" +42.25 -42.25 inf nan') +REGRESSION_TEST(`l1', `LC_ALL=en_US.ISO8859-1 printf "%d\n" $(printf \"\\344)') +REGRESSION_TEST(`l2', `LC_ALL=en_US.UTF-8 printf "%d\n" $(printf \"\\303\\244)') +REGRESSION_TEST(`m1', `printf "%c%%%d\0\045\n" abc \"abc') +REGRESSION_TEST(`m2', `printf "abc\n\cdef"') +REGRESSION_TEST(`m3', `printf "%%%s\n" abc def ghi jkl') +REGRESSION_TEST(`m4', `printf "%d,%f,%c,%s\n"') +REGRESSION_TEST(`m5', `printf -- "-d\n"') +REGRESSION_TEST(`s', `printf "%.3s,%-5s\n" abcd abc') +REGRESSION_TEST('zero', `printf "%u%u\n" 15') +REGRESSION_TEST('zero', `printf "%d%d\n" 15') +REGRESSION_TEST('zero', `printf "%d%u\n" 15') +REGRESSION_TEST('zero', `printf "%u%d\n" 15') + +REGRESSION_END() diff --git a/usr.bin/printf/tests/regress.zero.out b/usr.bin/printf/tests/regress.zero.out new file mode 100644 index 0000000..fa8f08c --- /dev/null +++ b/usr.bin/printf/tests/regress.zero.out @@ -0,0 +1 @@ +150 |