1 This file is printf.def
, from which is created printf.c.
2 It implements the builtin
"printf" in Bash.
4 Copyright (C
) 1997-2010 Free Software Foundation
, Inc.
6 This file is part of GNU Bash
, the Bourne Again SHell.
8 Bash is free software
: you can redistribute it and
/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation
, either version
3 of the License
, or
11 (at your option
) any later version.
13 Bash is distributed in the hope that it will be useful
,
14 but WITHOUT ANY WARRANTY
; without even the implied warranty of
15 MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not
, see
<http
://www.gnu.org
/licenses
/>.
24 $FUNCTION printf_builtin
25 $SHORT_DOC printf
[-v var
] format
[arguments
]
26 Formats and prints ARGUMENTS under control of the FORMAT.
29 -v var assign the output to shell variable
VAR rather than
30 display it on the standard output
32 FORMAT is a character string which contains three types of objects
: plain
33 characters
, which are simply copied to standard output
; character escape
34 sequences
, which are converted and copied to the standard output
; and
35 format specifications
, each of which causes printing of the next successive
38 In addition to the standard format specifications described in
printf(1)
39 and
printf(3), printf interprets
:
41 %b expand backslash escape sequences in the corresponding argument
42 %q quote the argument in a way that can be reused as shell input
43 %(fmt
)T output the date
-time string resulting from using FMT as a format
44 string for
strftime(3)
47 Returns success unless an invalid option is given or a write or assignment
53 #include
"../bashtypes.h"
56 #if
defined (HAVE_LIMITS_H
)
59 /* Assume
32-bit ints.
*/
60 # define INT_MAX
2147483647
61 # define
INT_MIN (-2147483647-1)
64 #if
defined (PREFER_STDARG
)
71 #include
<chartypes.h
>
73 #ifdef HAVE_INTTYPES_H
74 # include
<inttypes.h
>
77 #include
"posixtime.h"
78 #include
"../bashansi.h"
79 #include
"../bashintl.h"
81 #define NEED_STRFTIME_DECL
86 #include
"bashgetopt.h"
89 #if
defined (PRI_MACROS_BROKEN
)
93 #if
!defined (PRIdMAX
)
95 # define PRIdMAX
"lld"
109 b
[0] = c
; b
[1] = '\0'; \
116 #define
PF(f
, func
) \
120 if (have_fieldwidth
&& have_precision
) \
121 nw
= vflag ?
vbprintf (f
, fieldwidth
, precision
, func
) : printf (f
, fieldwidth
, precision
, func
); \
122 else
if (have_fieldwidth
) \
123 nw
= vflag ?
vbprintf (f
, fieldwidth
, func
) : printf (f
, fieldwidth
, func
); \
124 else
if (have_precision
) \
125 nw
= vflag ?
vbprintf (f
, precision
, func
) : printf (f
, precision
, func
); \
127 nw
= vflag ?
vbprintf (f
, func
) : printf (f
, func
); \
129 if (ferror (stdout
)) \
133 return (EXECUTION_FAILURE
); \
137 /* We free the buffer used by
mklong() if it
's `too big'.
*/
138 #define
PRETURN(value
) \
143 bind_printf_variable (vname
, vbuf
, 0); \
144 stupidly_hack_special_variables (vname
); \
146 if (conv_bufsize
> 4096 ) \
160 terminate_immediately
--; \
162 if (ferror (stdout
)) \
166 return (EXECUTION_FAILURE
); \
172 #define SKIP1
"#'-+ 0"
173 #define LENMODS
"hjlLtz"
175 extern time_t shell_start_time
;
178 extern int asprintf
__P((char
**, const char
*, ...
)) __attribute__((__format__ (printf
, 2, 3)));
182 extern int vsnprintf
__P((char
*, size_t
, const char
*, va_list
)) __attribute__((__format__ (printf
, 3, 0)));
185 static void printf_erange
__P((char *)
);
186 static int printstr
__P((char
*, char
*, int
, int
, int
));
187 static int tescape
__P((char
*, char
*, int
*, int *)
);
188 static char
*bexpand
__P((char
*, int
, int
*, int *)
);
189 static char
*vbadd
__P((char
*, int
));
190 static int vbprintf
__P((const char
*, ...
)) __attribute__((__format__ (printf
, 1, 2)));
191 static char
*mklong
__P((char
*, char
*, size_t
));
192 static int getchr
__P((void
));
193 static char
*getstr
__P((void
));
194 static int getint
__P((void
));
195 static intmax_t getintmax
__P((void
));
196 static uintmax_t getuintmax
__P((void
));
197 static SHELL_VAR
*bind_printf_variable
__P((char
*, char
*, int
));
199 #if
defined (HAVE_LONG_DOUBLE
) && HAVE_DECL_STRTOLD
&& !defined(STRTOLD_BROKEN
)
200 typedef long double floatmax_t
;
201 # define FLOATMAX_CONV
"L"
202 # define strtofltmax strtold
204 typedef double floatmax_t
;
205 # define FLOATMAX_CONV
""
206 # define strtofltmax strtod
208 static floatmax_t getfloatmax
__P((void
));
210 static intmax_t asciicode
__P((void
));
212 static WORD_LIST
*garglist
;
214 static int conversion_error
;
216 /* printf
-v var support
*/
217 static int vflag
= 0;
218 static char
*vbuf
, *vname
;
219 static size_t vbsize
;
224 static char
*conv_buf
;
225 static size_t conv_bufsize
;
228 printf_builtin (list
)
231 int ch
, fieldwidth
, precision
;
232 int have_fieldwidth
, have_precision
;
233 char convch
, thisch
, nextch
, *format
, *modstart
, *fmt
, *start
;
234 #if
defined (HANDLE_MULTIBYTE
)
235 char mbch
[25]; /* 25 > MB_LEN_MAX
, plus can handle
4-byte UTF
-8 and large Unicode characters
*/
239 conversion_error
= 0;
240 retval
= EXECUTION_SUCCESS
;
244 reset_internal_getopt ();
245 while ((ch
= internal_getopt (list
, "v:")) != -1)
251 #if
defined (ARRAY_VARS
)
252 if (legal_identifier (vname
) ||
valid_array_reference (vname
))
254 if (legal_identifier (vname
))
264 sh_invalidid (vname
);
273 list
= loptend
; /* skip over possible `
--' */
281 if (list->word->word == 0 || list->word->word[0] == '\
0')
282 return (EXECUTION_SUCCESS);
284 format = list->word->word;
287 garglist = list->next;
289 /* If the format string is empty after preprocessing, return immediately. */
290 if (format == 0 || *format == 0)
291 return (EXECUTION_SUCCESS);
293 terminate_immediately++;
295 /* Basic algorithm is to scan the format string for conversion
296 specifications -- once one is found, find out if the field
297 width or precision is a '*'; if it is, gather up value. Note,
298 format strings are reused as necessary to use up the provided
299 arguments, arguments of zero/null string are provided to use
300 up the format string. */
304 /* find next format specification */
305 for (fmt = format; *fmt; fmt++)
307 precision = fieldwidth = 0;
308 have_fieldwidth = have_precision = 0;
313 /* A NULL third argument to tescape means to bypass the
314 special processing for arguments to %b. */
315 #if defined (HANDLE_MULTIBYTE)
316 /* Accommodate possible use of \u or \U, which can result in
317 multibyte characters */
318 memset (mbch, '\
0', sizeof (mbch));
319 fmt += tescape (fmt, mbch, &mblen, (int *)NULL);
320 for (mbind = 0; mbind < mblen; mbind++)
323 fmt += tescape (fmt, &nextch, (int *)NULL, (int *)NULL);
326 fmt--; /* for loop will increment it for us again */
336 /* ASSERT(*fmt == '%') */
339 if (*fmt == '%') /* %% prints a % */
345 /* found format specification, skip to field width */
346 for (; *fmt && strchr(SKIP1, *fmt); ++fmt)
349 /* Skip optional field width. */
354 fieldwidth = getint ();
360 /* Skip optional '.
' and precision */
368 precision = getint ();
372 /* Negative precisions are allowed but treated as if the
373 precision were missing; I would like to allow a leading
374 `+' in the precision number as an extension
, but lots
375 of asprintf
/fprintf implementations get this wrong.
*/
377 if (*fmt == '-' || *fmt == '+')
387 /* skip possible format modifiers */
389 while (*fmt && strchr (LENMODS, *fmt))
394 builtin_error (_("`%s': missing format character"), start);
395 PRETURN (EXECUTION_FAILURE);
399 thisch = modstart[0];
400 nextch = modstart[1];
401 modstart[0] = convch;
426 char *timefmt, timebuf[128], *t;
432 modstart[1] = nextch; /* restore char after left paren */
433 timefmt = xmalloc (strlen (fmt) + 3);
434 fmt++; /* skip over left paren */
435 for (t = timefmt, n = 1; *fmt; )
439 else if (*fmt == ')')
448 builtin_warning (_("`%c': invalid time format specification"), *fmt);
454 if (timefmt[0] == '\0')
457 timefmt[1] = 'X'; /* locale-specific current time - should we use `+'? */
460 /* argument is seconds since the epoch with special -1 and -2 */
463 secs = NOW; /* roughly date +%s */
465 secs = shell_start_time; /* roughly $SECONDS */
468 tm = localtime (&secs);
469 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
474 timebuf[sizeof(timebuf) - 1] = '\0';
475 /* convert to %s format that preserves fieldwidth and precision */
478 n = printstr (start, timebuf, strlen (timebuf), fieldwidth, precision); /* XXX - %s for now */
483 PRETURN (EXECUTION_FAILURE);
495 if (legal_identifier (var))
496 bind_var_to_int (var, tw);
500 PRETURN (EXECUTION_FAILURE);
506 case 'b': /* expand escapes in argument */
513 xp = bexpand (p, strlen (p), &ch, &rlen);
517 /* Have to use printstr because of possible NUL bytes
518 in XP -- printf does not handle that well. */
519 r = printstr (start, xp, rlen, fieldwidth, precision);
524 retval = EXECUTION_FAILURE;
534 case 'q': /* print with shell quoting */
541 if (p && *p == 0) /* XXX - getstr never returns null */
542 xp = savestring ("''");
543 else if (ansic_shouldquote (p))
544 xp = ansic_quote (p, 0, (int *)0);
546 xp
= sh_backslash_quote (p
);
549 /* Use printstr to get fieldwidth and precision right.
*/
550 r
= printstr (start
, xp
, strlen (xp
), fieldwidth
, precision
);
560 PRETURN (EXECUTION_FAILURE
);
571 p
= pp
= getintmax ();
574 f
= mklong (start
, PRIdMAX
, sizeof (PRIdMAX
) - 2);
579 /* Optimize the common case where the integer fits
580 in
"long". This also works around some long
581 long and
/or intmax_t library bugs in the common
582 case
, e.g. glibc
2.2 x86.
*/
583 f
= mklong (start
, "l", 1);
598 p
= pp
= getuintmax ();
601 f
= mklong (start
, PRIdMAX
, sizeof (PRIdMAX
) - 2);
606 f
= mklong (start
, "l", 1);
618 #if
defined (HAVE_PRINTF_A_FORMAT
)
627 f
= mklong (start
, FLOATMAX_CONV
, sizeof(FLOATMAX_CONV
) - 1);
632 /* We don
't output unrecognized format characters; we print an
633 error message and return a failure exit status. */
635 builtin_error (_("`%c': invalid format character
"), convch);
636 PRETURN (EXECUTION_FAILURE);
639 modstart[0] = thisch;
640 modstart[1] = nextch;
647 PRETURN (EXECUTION_FAILURE);
650 while (garglist && garglist != list->next);
652 if (conversion_error)
653 retval = EXECUTION_FAILURE;
662 builtin_error (_("warning
: %s
: %s
"), s, strerror(ERANGE));
665 /* We duplicate a lot of what printf(3) does here. */
667 printstr (fmt, string, len, fieldwidth, precision)
668 char *fmt; /* format */
669 char *string; /* expanded string argument */
670 int len; /* length of expanded string */
671 int fieldwidth; /* argument for width of `*' */
672 int precision; /* argument for precision of `*' */
677 int padlen, nc, ljust, i;
678 int fw, pr; /* fieldwidth and precision */
681 if (string == 0 || *string == '\0')
683 if (string == 0 || len == 0)
697 while (strchr (SKIP1, *fmt))
704 /* get fieldwidth, if present */
715 else if (DIGIT (*fmt))
719 fw = (fw * 10) + (*fmt++ - '0');
722 /* get precision, if present */
731 else if (DIGIT (*fmt))
735 pr = (pr * 10) + (*fmt++ - '0');
740 /* If we remove this, get rid of `s'. */
741 if (*fmt != 'b' && *fmt != 'q')
743 internal_error ("format parsing problem
: %s
", s);
748 /* chars from string to print */
749 nc = (pr >= 0 && pr <= len) ? pr : len;
757 /* leading pad characters */
758 for (; padlen > 0; padlen--)
761 /* output NC characters from STRING */
762 for (i = 0; i < nc; i++)
765 /* output any necessary trailing padding */
766 for (; padlen < 0; padlen++)
769 return (ferror (stdout) ? -1 : 0);
772 /* Convert STRING by expanding the escape sequences specified by the
773 POSIX standard for printf's `%b' format string. If SAWC is non-null,
774 perform the processing appropriate for %b arguments. In particular,
775 recognize `\c' and use that as a string terminator. If we see \c, set
776 *SAWC to 1 before returning. LEN is the length of STRING. */
778 /* Translate a single backslash-escape sequence starting at ESTART (the
779 character after the backslash) and return the number of characters
780 consumed by the sequence. CP is the place to return the translated
781 value. *SAWC is set to 1 if the escape sequence was \c, since that means
782 to short-circuit the rest of the processing. If SAWC is null, we don't
783 do the \c short-circuiting, and \c is treated as an unrecognized escape
784 sequence; we also bypass the other processing specific to %b arguments. */
786 tescape (estart, cp, lenp, sawc)
793 unsigned long uvalue;
801 #if defined (__STDC__)
802 case 'a': *cp = '\a'; break;
804 case 'a': *cp = '\007'; break;
807 case 'b': *cp = '\b'; break;
810 case 'E': *cp = '\033'; break; /* ESC -- non-ANSI */
812 case 'f': *cp = '\f'; break;
814 case 'n': *cp = '\n'; break;
816 case 'r': *cp = '\r'; break;
818 case 't': *cp = '\t'; break;
820 case 'v': *cp = '\v'; break;
822 /* The octal escape sequences are `\0' followed by up to three octal
823 digits (if SAWC), or `\' followed by up to three octal digits (if
824 !SAWC). As an extension, we allow the latter form even if SAWC. */
825 case '0': case '1': case '2': case '3':
826 case '4': case '5': case '6': case '7':
827 evalue = OCTVALUE (c);
828 for (temp = 2 + (!evalue && !!sawc); ISOCTAL (*p) && temp--; p++)
829 evalue = (evalue * 8) + OCTVALUE (*p);
833 /* And, as another extension, we allow \xNN, where each N is a
836 for (temp = 2, evalue = 0; ISXDIGIT ((unsigned char)*p) && temp--; p++)
837 evalue = (evalue * 16) + HEXVALUE (*p);
840 builtin_error (_("missing hex digit for
\\x
"));
847 #if defined (HANDLE_MULTIBYTE)
850 temp = (c == 'u') ? 4 : 8; /* \uNNNN \UNNNNNNNN */
851 for (uvalue = 0; ISXDIGIT ((unsigned char)*p) && temp--; p++)
852 uvalue = (uvalue * 16) + HEXVALUE (*p);
855 builtin_error (_("missing unicode digit for
\\%c
"), c);
859 if (uvalue <= UCHAR_MAX)
863 temp = u32cconv (uvalue, cp);
871 case '\\': /* \\ -> \ */
875 /* SAWC == 0 means that \', \", and \? are recognized as escape
876 sequences, though the only processing performed is backslash
878 case '\'': case '"': case '?
':
894 /* other backslash escapes are passed through unaltered */
903 bexpand (string, len, sawc, lenp)
905 int len, *sawc, *lenp;
908 char *ret, *r, *s, c;
909 #if defined (HANDLE_MULTIBYTE)
914 if (string == 0 || len == 0)
920 return ((char *)NULL);
923 ret = (char *)xmalloc (len + 1);
924 for (r = ret, s = string; s && *s; )
927 if (c != '\\' || *s == '\
0')
933 #if defined (HANDLE_MULTIBYTE)
934 memset (mbch, '\
0', sizeof (mbch));
935 s += tescape (s, mbch, &mblen, &temp);
937 s += tescape (s, &c, (int *)NULL, &temp);
946 #if defined (HANDLE_MULTIBYTE)
947 for (mbind = 0; mbind < mblen; mbind++)
967 nlen = vblen + blen + 1;
970 vbsize = ((nlen + 63) >> 6) << 6;
971 vbuf = (char *)xrealloc (vbuf, vbsize);
975 vbuf[vblen++] = buf[0];
978 FASTCOPY (buf, vbuf + vblen, blen);
984 if (strlen (vbuf) != vblen)
985 internal_error ("printf:vbadd: vblen (%d) != strlen (vbuf) (%d)", vblen, (int)strlen (vbuf));
992 #if defined (PREFER_STDARG)
993 vbprintf (const char *format, ...)
995 vbprintf (format, va_alist)
1004 SH_VA_START (args, format);
1005 blen = vsnprintf (vbuf + vblen, vbsize - vblen, format, args);
1008 nlen = vblen + blen + 1;
1011 vbsize = ((nlen + 63) >> 6) << 6;
1012 vbuf = (char *)xrealloc (vbuf, vbsize);
1013 SH_VA_START (args, format);
1014 blen = vsnprintf (vbuf + vblen, vbsize - vblen, format, args);
1022 if (strlen (vbuf) != vblen)
1023 internal_error ("printf:vbadd: vblen (%d) != strlen (vbuf) (%d)", vblen, (int)strlen (vbuf));
1030 mklong (str, modifiers, mlen)
1037 slen = strlen (str);
1038 len = slen + mlen + 1;
1040 if (len > conv_bufsize)
1042 conv_bufsize = (((len + 1023) >> 10) << 10);
1043 conv_buf = (char *)xrealloc (conv_buf, conv_bufsize);
1046 FASTCOPY (str, conv_buf, slen - 1);
1047 FASTCOPY (modifiers, conv_buf + slen - 1, mlen);
1049 conv_buf[len - 2] = str[slen - 1];
1050 conv_buf[len - 1] = '\
0';
1062 ret = (int)garglist->word->word[0];
1063 garglist = garglist->next;
1075 ret = garglist->word->word;
1076 garglist = garglist->next;
1089 printf_erange (garglist->word->word);
1092 else if (ret < INT_MIN)
1094 printf_erange (garglist->word->word);
1110 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1111 return asciicode ();
1114 ret = strtoimax (garglist->word->word, &ep, 0);
1118 sh_invalidnum (garglist->word->word);
1119 /* POSIX.2 says ``...a diagnostic message shall be written to standard
1120 error, and the utility shall not exit with a zero exit status, but
1121 shall continue processing any remaining operands and shall write the
1122 value accumulated at the time the error was detected to standard
1125 ret = 0; /* return partially-converted value from strtoimax */
1127 conversion_error = 1;
1129 else if (errno == ERANGE)
1130 printf_erange (garglist->word->word);
1132 garglist = garglist->next;
1145 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1146 return asciicode ();
1149 ret = strtoumax (garglist->word->word, &ep, 0);
1153 sh_invalidnum (garglist->word->word);
1154 /* Same POSIX.2 conversion error requirements as getintmax(). */
1156 conversion_error = 1;
1158 else if (errno == ERANGE)
1159 printf_erange (garglist->word->word);
1161 garglist = garglist->next;
1174 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1175 return asciicode ();
1178 ret = strtofltmax (garglist->word->word, &ep);
1182 sh_invalidnum (garglist->word->word);
1183 /* Same thing about POSIX.2 conversion error requirements. */
1185 conversion_error = 1;
1187 else if (errno == ERANGE)
1188 printf_erange (garglist->word->word);
1190 garglist = garglist->next;
1194 /* NO check is needed for garglist here. */
1198 register intmax_t ch;
1199 #if defined (HANDLE_MULTIBYTE)
1201 size_t mblength, slen;
1205 #if defined (HANDLE_MULTIBYTE)
1206 slen = strlen (garglist->word->word+1);
1207 mblength = MBLEN (garglist->word->word+1, slen);
1210 mblength = mbtowc (&wc, garglist->word->word+1, slen);
1215 ch = (unsigned char)garglist->word->word[1];
1217 garglist = garglist->next;
1222 bind_printf_variable (name, value, flags)
1227 #if defined (ARRAY_VARS)
1228 if (valid_array_reference (name) == 0)
1229 return (bind_variable (name, value, flags));
1231 return (assign_array_element (name, value, flags));
1232 #else /* !ARRAY_VARS */
1233 return bind_variable (name, value, flags);
1234 #endif /* !ARRAY_VARS */