1 This file is printf.def, from which is created printf.c.
2 It implements the builtin "printf" in Bash.
4 Copyright (C) 1997-2016 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),
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)
46 The format is re-used as necessary to consume all of the arguments. If
47 there are fewer arguments than the format requires, extra format
48 specifications behave as if a zero value or null string, as appropriate,
52 Returns success unless an invalid option is given or a write or assignment
58 #include "../bashtypes.h"
61 #if defined (HAVE_LIMITS_H)
64 /* Assume 32-bit ints. */
65 # define INT_MAX 2147483647
66 # define INT_MIN (-2147483647-1)
69 #if defined (PREFER_STDARG)
76 #include <chartypes.h>
78 #ifdef HAVE_INTTYPES_H
79 # include <inttypes.h>
82 #include "posixtime.h"
83 #include "../bashansi.h"
84 #include "../bashintl.h"
86 #define NEED_STRFTIME_DECL
91 #include "bashgetopt.h"
94 #if defined (PRI_MACROS_BROKEN)
98 #if !defined (PRIdMAX)
100 # define PRIdMAX "lld"
102 # define PRIdMAX "ld"
114 b[0] = c; b[1] = '\0'; \
122 #define PF(f, func) \
126 if (have_fieldwidth && have_precision) \
127 nw = vflag ? vbprintf (f, fieldwidth, precision, func) : printf (f, fieldwidth, precision, func); \
128 else if (have_fieldwidth) \
129 nw = vflag ? vbprintf (f, fieldwidth, func) : printf (f, fieldwidth, func); \
130 else if (have_precision) \
131 nw = vflag ? vbprintf (f, precision, func) : printf (f, precision, func); \
133 nw = vflag ? vbprintf (f, func) : printf (f, func); \
136 if (ferror (stdout)) \
140 return (EXECUTION_FAILURE); \
144 /* We free the buffer used by mklong() if it's `too big'. */
145 #define PRETURN(value) \
151 bind_printf_variable (vname, vbuf, 0); \
152 stupidly_hack_special_variables (vname); \
154 if (conv_bufsize > 4096 ) \
168 if (ferror (stdout) == 0) \
171 if (ferror (stdout)) \
175 return (EXECUTION_FAILURE); \
181 #define SKIP1 "#'-+ 0"
182 #define LENMODS "hjlLtz"
184 extern time_t shell_start_time;
187 extern int asprintf __P((char **, const char *, ...)) __attribute__((__format__ (printf, 2, 3)));
191 extern int vsnprintf __P((char *, size_t, const char *, va_list)) __attribute__((__format__ (printf, 3, 0)));
194 static void printf_erange __P((char *));
195 static int printstr __P((char *, char *, int, int, int));
196 static int tescape __P((char *, char *, int *, int *));
197 static char *bexpand __P((char *, int, int *, int *));
198 static char *vbadd __P((char *, int));
199 static int vbprintf __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
200 static char *mklong __P((char *, char *, size_t));
201 static int getchr __P((void));
202 static char *getstr __P((void));
203 static int getint __P((void));
204 static intmax_t getintmax __P((void));
205 static uintmax_t getuintmax __P((void));
206 static SHELL_VAR *bind_printf_variable __P((char *, char *, int));
208 #if defined (HAVE_LONG_DOUBLE) && HAVE_DECL_STRTOLD && !defined(STRTOLD_BROKEN)
209 typedef long double floatmax_t;
210 # define FLOATMAX_CONV "L"
211 # define strtofltmax strtold
213 typedef double floatmax_t;
214 # define FLOATMAX_CONV ""
215 # define strtofltmax strtod
217 static floatmax_t getfloatmax __P((void));
219 static intmax_t asciicode __P((void));
221 static WORD_LIST *garglist;
223 static int conversion_error;
225 /* printf -v var support */
226 static int vflag = 0;
227 static char *vbuf, *vname;
228 static size_t vbsize;
233 static char *conv_buf;
234 static size_t conv_bufsize;
237 printf_builtin (list)
240 int ch, fieldwidth, precision;
241 int have_fieldwidth, have_precision;
242 char convch, thisch, nextch, *format, *modstart, *fmt, *start;
243 #if defined (HANDLE_MULTIBYTE)
244 char mbch[25]; /* 25 > MB_LEN_MAX, plus can handle 4-byte UTF-8 and large Unicode characters*/
248 conversion_error = 0;
249 retval = EXECUTION_SUCCESS;
253 reset_internal_getopt ();
254 while ((ch = internal_getopt (list, "v:")) != -1)
260 #if defined (ARRAY_VARS)
261 if (legal_identifier (vname) || valid_array_reference (vname, 0))
263 if (legal_identifier (vname))
268 vbuf = xmalloc (vbsize = 16);
275 sh_invalidid (vname);
285 list = loptend; /* skip over possible `--' */
293 /* Allow printf -v var "" to act like var="" */
294 if (vflag && list->word->word && list->word->word[0] == '\0')
296 bind_printf_variable (vname, "", 0);
297 stupidly_hack_special_variables (vname);
298 return (EXECUTION_SUCCESS);
301 if (list->word->word == 0 || list->word->word[0] == '\0')
302 return (EXECUTION_SUCCESS);
304 format = list->word->word;
307 garglist = list->next;
309 /* If the format string is empty after preprocessing, return immediately. */
310 if (format == 0 || *format == 0)
311 return (EXECUTION_SUCCESS);
313 /* Basic algorithm is to scan the format string for conversion
314 specifications -- once one is found, find out if the field
315 width or precision is a '*'; if it is, gather up value. Note,
316 format strings are reused as necessary to use up the provided
317 arguments, arguments of zero/null string are provided to use
318 up the format string. */
322 /* find next format specification */
323 for (fmt = format; *fmt; fmt++)
325 precision = fieldwidth = 0;
326 have_fieldwidth = have_precision = 0;
331 /* A NULL third argument to tescape means to bypass the
332 special processing for arguments to %b. */
333 #if defined (HANDLE_MULTIBYTE)
334 /* Accommodate possible use of \u or \U, which can result in
335 multibyte characters */
336 memset (mbch, '\0', sizeof (mbch));
337 fmt += tescape (fmt, mbch, &mblen, (int *)NULL);
338 for (mbind = 0; mbind < mblen; mbind++)
341 fmt += tescape (fmt, &nextch, (int *)NULL, (int *)NULL);
344 fmt--; /* for loop will increment it for us again */
354 /* ASSERT(*fmt == '%') */
357 if (*fmt == '%') /* %% prints a % */
363 /* found format specification, skip to field width */
364 for (; *fmt && strchr(SKIP1, *fmt); ++fmt)
367 /* Skip optional field width. */
372 fieldwidth = getint ();
378 /* Skip optional '.' and precision */
386 precision = getint ();
390 /* Negative precisions are allowed but treated as if the
391 precision were missing; I would like to allow a leading
392 `+' in the precision number as an extension, but lots
393 of asprintf/fprintf implementations get this wrong. */
395 if (*fmt == '-' || *fmt == '+')
405 /* skip possible format modifiers */
407 while (*fmt && strchr (LENMODS, *fmt))
412 builtin_error (_("`%s': missing format character"), start);
413 PRETURN (EXECUTION_FAILURE);
417 thisch = modstart[0];
418 nextch = modstart[1];
419 modstart[0] = convch;
445 char *timefmt, timebuf[128], *t;
451 modstart[1] = nextch; /* restore char after left paren */
452 timefmt = xmalloc (strlen (fmt) + 3);
453 fmt++; /* skip over left paren */
454 for (t = timefmt, n = 1; *fmt; )
458 else if (*fmt == ')')
467 builtin_warning (_("`%c': invalid time format specification"), *fmt);
473 if (timefmt[0] == '\0')
476 timefmt[1] = 'X'; /* locale-specific current time - should we use `+'? */
479 /* argument is seconds since the epoch with special -1 and -2 */
480 /* default argument is equivalent to -1; special case */
481 arg = garglist ? getintmax () : -1;
483 secs = NOW; /* roughly date +%s */
485 secs = shell_start_time; /* roughly $SECONDS */
488 #if defined (HAVE_TZSET)
489 sv_tz ("TZ"); /* XXX -- just make sure */
491 tm = localtime (&secs);
495 tm = localtime (&secs);
497 n = tm ? strftime (timebuf, sizeof (timebuf), timefmt, tm) : 0;
502 timebuf[sizeof(timebuf) - 1] = '\0';
503 /* convert to %s format that preserves fieldwidth and precision */
506 n = printstr (start, timebuf, strlen (timebuf), fieldwidth, precision); /* XXX - %s for now */
509 if (ferror (stdout) == 0)
514 PRETURN (EXECUTION_FAILURE);
526 if (legal_identifier (var))
527 bind_var_to_int (var, tw);
531 PRETURN (EXECUTION_FAILURE);
537 case 'b': /* expand escapes in argument */
544 xp = bexpand (p, strlen (p), &ch, &rlen);
548 /* Have to use printstr because of possible NUL bytes
549 in XP -- printf does not handle that well. */
550 r = printstr (start, xp, rlen, fieldwidth, precision);
553 if (ferror (stdout) == 0)
558 retval = EXECUTION_FAILURE;
568 case 'q': /* print with shell quoting */
575 if (p && *p == 0) /* XXX - getstr never returns null */
576 xp = savestring ("''");
577 else if (ansic_shouldquote (p))
578 xp = ansic_quote (p, 0, (int *)0);
580 xp = sh_backslash_quote (p, 0, 1);
583 /* Use printstr to get fieldwidth and precision right. */
584 r = printstr (start, xp, strlen (xp), fieldwidth, precision);
594 PRETURN (EXECUTION_FAILURE);
605 p = pp = getintmax ();
608 f = mklong (start, PRIdMAX, sizeof (PRIdMAX) - 2);
613 /* Optimize the common case where the integer fits
614 in "long". This also works around some long
615 long and/or intmax_t library bugs in the common
616 case, e.g. glibc 2.2 x86. */
617 f = mklong (start, "l", 1);
632 p = pp = getuintmax ();
635 f = mklong (start, PRIdMAX, sizeof (PRIdMAX) - 2);
640 f = mklong (start, "l", 1);
652 #if defined (HAVE_PRINTF_A_FORMAT)
661 f = mklong (start, FLOATMAX_CONV, sizeof(FLOATMAX_CONV) - 1);
666 /* We don't output unrecognized format characters; we print an
667 error message and return a failure exit status. */
669 builtin_error (_("`%c': invalid format character"), convch);
670 PRETURN (EXECUTION_FAILURE);
673 modstart[0] = thisch;
674 modstart[1] = nextch;
679 /* PRETURN will print error message. */
680 PRETURN (EXECUTION_FAILURE);
683 while (garglist && garglist != list->next);
685 if (conversion_error)
686 retval = EXECUTION_FAILURE;
695 builtin_error (_("warning: %s: %s"), s, strerror(ERANGE));
698 /* We duplicate a lot of what printf(3) does here. */
700 printstr (fmt, string, len, fieldwidth, precision)
701 char *fmt; /* format */
702 char *string; /* expanded string argument */
703 int len; /* length of expanded string */
704 int fieldwidth; /* argument for width of `*' */
705 int precision; /* argument for precision of `*' */
710 int padlen, nc, ljust, i;
711 int fw, pr; /* fieldwidth and precision */
714 if (string == 0 || len == 0)
729 while (strchr (SKIP1, *fmt))
736 /* get fieldwidth, if present. rely on caller to clamp fieldwidth at INT_MAX */
747 else if (DIGIT (*fmt))
751 mfw = (mfw * 10) + (*fmt++ - '0');
752 /* Error if fieldwidth > INT_MAX here? */
753 fw = (mfw < 0 || mfw > INT_MAX) ? INT_MAX : mfw;
756 /* get precision, if present */
765 else if (DIGIT (*fmt))
769 mpr = (mpr * 10) + (*fmt++ - '0');
770 /* Error if precision > INT_MAX here? */
771 pr = (mpr < 0 || mpr > INT_MAX) ? INT_MAX : mpr;
774 pr = 0; /* "a null digit string is treated as zero" */
778 /* If we remove this, get rid of `s'. */
779 if (*fmt != 'b' && *fmt != 'q')
781 internal_error (_("format parsing problem: %s"), s);
786 /* chars from string to print */
787 nc = (pr >= 0 && pr <= len) ? pr : len;
795 /* leading pad characters */
796 for (; padlen > 0; padlen--)
799 /* output NC characters from STRING */
800 for (i = 0; i < nc; i++)
803 /* output any necessary trailing padding */
804 for (; padlen < 0; padlen++)
807 return (ferror (stdout) ? -1 : 0);
810 /* Convert STRING by expanding the escape sequences specified by the
811 POSIX standard for printf's `%b' format string. If SAWC is non-null,
812 perform the processing appropriate for %b arguments. In particular,
813 recognize `\c' and use that as a string terminator. If we see \c, set
814 *SAWC to 1 before returning. LEN is the length of STRING. */
816 /* Translate a single backslash-escape sequence starting at ESTART (the
817 character after the backslash) and return the number of characters
818 consumed by the sequence. CP is the place to return the translated
819 value. *SAWC is set to 1 if the escape sequence was \c, since that means
820 to short-circuit the rest of the processing. If SAWC is null, we don't
821 do the \c short-circuiting, and \c is treated as an unrecognized escape
822 sequence; we also bypass the other processing specific to %b arguments. */
824 tescape (estart, cp, lenp, sawc)
831 unsigned long uvalue;
839 #if defined (__STDC__)
840 case 'a': *cp = '\a'; break;
842 case 'a': *cp = '\007'; break;
845 case 'b': *cp = '\b'; break;
848 case 'E': *cp = '\033'; break; /* ESC -- non-ANSI */
850 case 'f': *cp = '\f'; break;
852 case 'n': *cp = '\n'; break;
854 case 'r': *cp = '\r'; break;
856 case 't': *cp = '\t'; break;
858 case 'v': *cp = '\v'; break;
860 /* The octal escape sequences are `\0' followed by up to three octal
861 digits (if SAWC), or `\' followed by up to three octal digits (if
862 !SAWC). As an extension, we allow the latter form even if SAWC. */
863 case '0': case '1': case '2': case '3':
864 case '4': case '5': case '6': case '7':
865 evalue = OCTVALUE (c);
866 for (temp = 2 + (!evalue && !!sawc); ISOCTAL (*p) && temp--; p++)
867 evalue = (evalue * 8) + OCTVALUE (*p);
871 /* And, as another extension, we allow \xNN, where each N is a
874 for (temp = 2, evalue = 0; ISXDIGIT ((unsigned char)*p) && temp--; p++)
875 evalue = (evalue * 16) + HEXVALUE (*p);
878 builtin_error (_("missing hex digit for \\x"));
885 #if defined (HANDLE_MULTIBYTE)
888 temp = (c == 'u') ? 4 : 8; /* \uNNNN \UNNNNNNNN */
889 for (uvalue = 0; ISXDIGIT ((unsigned char)*p) && temp--; p++)
890 uvalue = (uvalue * 16) + HEXVALUE (*p);
893 builtin_error (_("missing unicode digit for \\%c"), c);
897 if (uvalue <= 0x7f) /* <= 0x7f translates directly */
901 temp = u32cconv (uvalue, cp);
909 case '\\': /* \\ -> \ */
913 /* SAWC == 0 means that \', \", and \? are recognized as escape
914 sequences, though the only processing performed is backslash
916 case '\'': case '"': case '?':
932 /* other backslash escapes are passed through unaltered */
941 bexpand (string, len, sawc, lenp)
943 int len, *sawc, *lenp;
946 char *ret, *r, *s, c;
947 #if defined (HANDLE_MULTIBYTE)
952 if (string == 0 || len == 0)
958 return ((char *)NULL);
961 ret = (char *)xmalloc (len + 1);
962 for (r = ret, s = string; s && *s; )
965 if (c != '\\' || *s == '\0')
971 #if defined (HANDLE_MULTIBYTE)
972 memset (mbch, '\0', sizeof (mbch));
973 s += tescape (s, mbch, &mblen, &temp);
975 s += tescape (s, &c, (int *)NULL, &temp);
984 #if defined (HANDLE_MULTIBYTE)
985 for (mbind = 0; mbind < mblen; mbind++)
1005 nlen = vblen + blen + 1;
1008 vbsize = ((nlen + 63) >> 6) << 6;
1009 vbuf = (char *)xrealloc (vbuf, vbsize);
1013 vbuf[vblen++] = buf[0];
1016 FASTCOPY (buf, vbuf + vblen, blen);
1022 if (strlen (vbuf) != vblen)
1023 internal_error ("printf:vbadd: vblen (%d) != strlen (vbuf) (%d)", vblen, (int)strlen (vbuf));
1030 #if defined (PREFER_STDARG)
1031 vbprintf (const char *format, ...)
1033 vbprintf (format, va_alist)
1042 SH_VA_START (args, format);
1043 blen = vsnprintf (vbuf + vblen, vbsize - vblen, format, args);
1046 nlen = vblen + blen + 1;
1049 vbsize = ((nlen + 63) >> 6) << 6;
1050 vbuf = (char *)xrealloc (vbuf, vbsize);
1051 SH_VA_START (args, format);
1052 blen = vsnprintf (vbuf + vblen, vbsize - vblen, format, args);
1060 if (strlen (vbuf) != vblen)
1061 internal_error ("printf:vbprintf: vblen (%d) != strlen (vbuf) (%d)", vblen, (int)strlen (vbuf));
1068 mklong (str, modifiers, mlen)
1075 slen = strlen (str);
1076 len = slen + mlen + 1;
1078 if (len > conv_bufsize)
1080 conv_bufsize = (((len + 1023) >> 10) << 10);
1081 conv_buf = (char *)xrealloc (conv_buf, conv_bufsize);
1084 FASTCOPY (str, conv_buf, slen - 1);
1085 FASTCOPY (modifiers, conv_buf + slen - 1, mlen);
1087 conv_buf[len - 2] = str[slen - 1];
1088 conv_buf[len - 1] = '\0';
1100 ret = (int)garglist->word->word[0];
1101 garglist = garglist->next;
1113 ret = garglist->word->word;
1114 garglist = garglist->next;
1130 printf_erange (garglist->word->word);
1133 else if (ret < INT_MIN)
1135 printf_erange (garglist->word->word);
1151 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1152 return asciicode ();
1155 ret = strtoimax (garglist->word->word, &ep, 0);
1159 sh_invalidnum (garglist->word->word);
1160 /* POSIX.2 says ``...a diagnostic message shall be written to standard
1161 error, and the utility shall not exit with a zero exit status, but
1162 shall continue processing any remaining operands and shall write the
1163 value accumulated at the time the error was detected to standard
1166 ret = 0; /* return partially-converted value from strtoimax */
1168 conversion_error = 1;
1170 else if (errno == ERANGE)
1171 printf_erange (garglist->word->word);
1173 garglist = garglist->next;
1186 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1187 return asciicode ();
1190 ret = strtoumax (garglist->word->word, &ep, 0);
1194 sh_invalidnum (garglist->word->word);
1195 /* Same POSIX.2 conversion error requirements as getintmax(). */
1197 conversion_error = 1;
1199 else if (errno == ERANGE)
1200 printf_erange (garglist->word->word);
1202 garglist = garglist->next;
1215 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1216 return asciicode ();
1219 ret = strtofltmax (garglist->word->word, &ep);
1223 sh_invalidnum (garglist->word->word);
1224 /* Same thing about POSIX.2 conversion error requirements. */
1226 conversion_error = 1;
1228 else if (errno == ERANGE)
1229 printf_erange (garglist->word->word);
1231 garglist = garglist->next;
1235 /* NO check is needed for garglist here. */
1239 register intmax_t ch;
1240 #if defined (HANDLE_MULTIBYTE)
1242 size_t mblength, slen;
1246 #if defined (HANDLE_MULTIBYTE)
1247 slen = strlen (garglist->word->word+1);
1248 mblength = MBLEN (garglist->word->word+1, slen);
1251 mblength = mbtowc (&wc, garglist->word->word+1, slen);
1256 ch = (unsigned char)garglist->word->word[1];
1258 garglist = garglist->next;
1263 bind_printf_variable (name, value, flags)
1270 #if defined (ARRAY_VARS)
1271 if (valid_array_reference (name, 0) == 0)
1272 v = bind_variable (name, value, flags);
1274 v = assign_array_element (name, value, flags);
1275 #else /* !ARRAY_VARS */
1276 v = bind_variable (name, value, flags);
1277 #endif /* !ARRAY_VARS */
1279 if (v && readonly_p (v) == 0 && noassign_p (v) == 0)
1280 VUNSETATTR (v, att_invisible);