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 #if defined (HAVE_TZSET)
469 sv_tz ("TZ"); /* XXX -- just make sure */
471 tm = localtime (&secs);
472 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
477 timebuf[sizeof(timebuf) - 1] = '\0';
478 /* convert to %s format that preserves fieldwidth and precision */
481 n = printstr (start, timebuf, strlen (timebuf), fieldwidth, precision); /* XXX - %s for now */
486 PRETURN (EXECUTION_FAILURE);
498 if (legal_identifier (var))
499 bind_var_to_int (var, tw);
503 PRETURN (EXECUTION_FAILURE);
509 case 'b': /* expand escapes in argument */
516 xp = bexpand (p, strlen (p), &ch, &rlen);
520 /* Have to use printstr because of possible NUL bytes
521 in XP -- printf does not handle that well. */
522 r = printstr (start, xp, rlen, fieldwidth, precision);
527 retval = EXECUTION_FAILURE;
537 case 'q': /* print with shell quoting */
544 if (p && *p == 0) /* XXX - getstr never returns null */
545 xp = savestring ("''");
546 else if (ansic_shouldquote (p))
547 xp = ansic_quote (p, 0, (int *)0);
549 xp = sh_backslash_quote (p);
552 /* Use printstr to get fieldwidth and precision right. */
553 r = printstr (start, xp, strlen (xp), fieldwidth, precision);
563 PRETURN (EXECUTION_FAILURE);
574 p = pp = getintmax ();
577 f = mklong (start, PRIdMAX, sizeof (PRIdMAX) - 2);
582 /* Optimize the common case where the integer fits
583 in "long". This also works around some long
584 long and/or intmax_t library bugs in the common
585 case, e.g. glibc 2.2 x86. */
586 f = mklong (start, "l", 1);
601 p = pp = getuintmax ();
604 f = mklong (start, PRIdMAX, sizeof (PRIdMAX) - 2);
609 f = mklong (start, "l", 1);
621 #if defined (HAVE_PRINTF_A_FORMAT)
630 f = mklong (start, FLOATMAX_CONV, sizeof(FLOATMAX_CONV) - 1);
635 /* We don't output unrecognized format characters; we print an
636 error message and return a failure exit status. */
638 builtin_error (_("`%c': invalid format character"), convch);
639 PRETURN (EXECUTION_FAILURE);
642 modstart[0] = thisch;
643 modstart[1] = nextch;
650 PRETURN (EXECUTION_FAILURE);
653 while (garglist && garglist != list->next);
655 if (conversion_error)
656 retval = EXECUTION_FAILURE;
665 builtin_error (_("warning: %s: %s"), s, strerror(ERANGE));
668 /* We duplicate a lot of what printf(3) does here. */
670 printstr (fmt, string, len, fieldwidth, precision)
671 char *fmt; /* format */
672 char *string; /* expanded string argument */
673 int len; /* length of expanded string */
674 int fieldwidth; /* argument for width of `*' */
675 int precision; /* argument for precision of `*' */
680 int padlen, nc, ljust, i;
681 int fw, pr; /* fieldwidth and precision */
684 if (string == 0 || *string == '\0')
686 if (string == 0 || len == 0)
700 while (strchr (SKIP1, *fmt))
707 /* get fieldwidth, if present */
718 else if (DIGIT (*fmt))
722 fw = (fw * 10) + (*fmt++ - '0');
725 /* get precision, if present */
734 else if (DIGIT (*fmt))
738 pr = (pr * 10) + (*fmt++ - '0');
743 /* If we remove this, get rid of `s'. */
744 if (*fmt != 'b' && *fmt != 'q')
746 internal_error ("format parsing problem: %s", s);
751 /* chars from string to print */
752 nc = (pr >= 0 && pr <= len) ? pr : len;
760 /* leading pad characters */
761 for (; padlen > 0; padlen--)
764 /* output NC characters from STRING */
765 for (i = 0; i < nc; i++)
768 /* output any necessary trailing padding */
769 for (; padlen < 0; padlen++)
772 return (ferror (stdout) ? -1 : 0);
775 /* Convert STRING by expanding the escape sequences specified by the
776 POSIX standard for printf's `%b' format string. If SAWC is non-null,
777 perform the processing appropriate for %b arguments. In particular,
778 recognize `\c' and use that as a string terminator. If we see \c, set
779 *SAWC to 1 before returning. LEN is the length of STRING. */
781 /* Translate a single backslash-escape sequence starting at ESTART (the
782 character after the backslash) and return the number of characters
783 consumed by the sequence. CP is the place to return the translated
784 value. *SAWC is set to 1 if the escape sequence was \c, since that means
785 to short-circuit the rest of the processing. If SAWC is null, we don't
786 do the \c short-circuiting, and \c is treated as an unrecognized escape
787 sequence; we also bypass the other processing specific to %b arguments. */
789 tescape (estart, cp, lenp, sawc)
796 unsigned long uvalue;
804 #if defined (__STDC__)
805 case 'a': *cp = '\a'; break;
807 case 'a': *cp = '\007'; break;
810 case 'b': *cp = '\b'; break;
813 case 'E': *cp = '\033'; break; /* ESC -- non-ANSI */
815 case 'f': *cp = '\f'; break;
817 case 'n': *cp = '\n'; break;
819 case 'r': *cp = '\r'; break;
821 case 't': *cp = '\t'; break;
823 case 'v': *cp = '\v'; break;
825 /* The octal escape sequences are `\0' followed by up to three octal
826 digits (if SAWC), or `\' followed by up to three octal digits (if
827 !SAWC). As an extension, we allow the latter form even if SAWC. */
828 case '0': case '1': case '2': case '3':
829 case '4': case '5': case '6': case '7':
830 evalue = OCTVALUE (c);
831 for (temp = 2 + (!evalue && !!sawc); ISOCTAL (*p) && temp--; p++)
832 evalue = (evalue * 8) + OCTVALUE (*p);
836 /* And, as another extension, we allow \xNN, where each N is a
839 for (temp = 2, evalue = 0; ISXDIGIT ((unsigned char)*p) && temp--; p++)
840 evalue = (evalue * 16) + HEXVALUE (*p);
843 builtin_error (_("missing hex digit for \\x"));
850 #if defined (HANDLE_MULTIBYTE)
853 temp = (c == 'u') ? 4 : 8; /* \uNNNN \UNNNNNNNN */
854 for (uvalue = 0; ISXDIGIT ((unsigned char)*p) && temp--; p++)
855 uvalue = (uvalue * 16) + HEXVALUE (*p);
858 builtin_error (_("missing unicode digit for \\%c"), c);
862 if (uvalue <= UCHAR_MAX)
866 temp = u32cconv (uvalue, cp);
874 case '\\': /* \\ -> \ */
878 /* SAWC == 0 means that \', \", and \? are recognized as escape
879 sequences, though the only processing performed is backslash
881 case '\'': case '"': case '?':
897 /* other backslash escapes are passed through unaltered */
906 bexpand (string, len, sawc, lenp)
908 int len, *sawc, *lenp;
911 char *ret, *r, *s, c;
912 #if defined (HANDLE_MULTIBYTE)
917 if (string == 0 || len == 0)
923 return ((char *)NULL);
926 ret = (char *)xmalloc (len + 1);
927 for (r = ret, s = string; s && *s; )
930 if (c != '\\' || *s == '\0')
936 #if defined (HANDLE_MULTIBYTE)
937 memset (mbch, '\0', sizeof (mbch));
938 s += tescape (s, mbch, &mblen, &temp);
940 s += tescape (s, &c, (int *)NULL, &temp);
949 #if defined (HANDLE_MULTIBYTE)
950 for (mbind = 0; mbind < mblen; mbind++)
970 nlen = vblen + blen + 1;
973 vbsize = ((nlen + 63) >> 6) << 6;
974 vbuf = (char *)xrealloc (vbuf, vbsize);
978 vbuf[vblen++] = buf[0];
981 FASTCOPY (buf, vbuf + vblen, blen);
987 if (strlen (vbuf) != vblen)
988 internal_error ("printf:vbadd: vblen (%d) != strlen (vbuf) (%d)", vblen, (int)strlen (vbuf));
995 #if defined (PREFER_STDARG)
996 vbprintf (const char *format, ...)
998 vbprintf (format, va_alist)
1007 SH_VA_START (args, format);
1008 blen = vsnprintf (vbuf + vblen, vbsize - vblen, format, args);
1011 nlen = vblen + blen + 1;
1014 vbsize = ((nlen + 63) >> 6) << 6;
1015 vbuf = (char *)xrealloc (vbuf, vbsize);
1016 SH_VA_START (args, format);
1017 blen = vsnprintf (vbuf + vblen, vbsize - vblen, format, args);
1025 if (strlen (vbuf) != vblen)
1026 internal_error ("printf:vbadd: vblen (%d) != strlen (vbuf) (%d)", vblen, (int)strlen (vbuf));
1033 mklong (str, modifiers, mlen)
1040 slen = strlen (str);
1041 len = slen + mlen + 1;
1043 if (len > conv_bufsize)
1045 conv_bufsize = (((len + 1023) >> 10) << 10);
1046 conv_buf = (char *)xrealloc (conv_buf, conv_bufsize);
1049 FASTCOPY (str, conv_buf, slen - 1);
1050 FASTCOPY (modifiers, conv_buf + slen - 1, mlen);
1052 conv_buf[len - 2] = str[slen - 1];
1053 conv_buf[len - 1] = '\0';
1065 ret = (int)garglist->word->word[0];
1066 garglist = garglist->next;
1078 ret = garglist->word->word;
1079 garglist = garglist->next;
1092 printf_erange (garglist->word->word);
1095 else if (ret < INT_MIN)
1097 printf_erange (garglist->word->word);
1113 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1114 return asciicode ();
1117 ret = strtoimax (garglist->word->word, &ep, 0);
1121 sh_invalidnum (garglist->word->word);
1122 /* POSIX.2 says ``...a diagnostic message shall be written to standard
1123 error, and the utility shall not exit with a zero exit status, but
1124 shall continue processing any remaining operands and shall write the
1125 value accumulated at the time the error was detected to standard
1128 ret = 0; /* return partially-converted value from strtoimax */
1130 conversion_error = 1;
1132 else if (errno == ERANGE)
1133 printf_erange (garglist->word->word);
1135 garglist = garglist->next;
1148 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1149 return asciicode ();
1152 ret = strtoumax (garglist->word->word, &ep, 0);
1156 sh_invalidnum (garglist->word->word);
1157 /* Same POSIX.2 conversion error requirements as getintmax(). */
1159 conversion_error = 1;
1161 else if (errno == ERANGE)
1162 printf_erange (garglist->word->word);
1164 garglist = garglist->next;
1177 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1178 return asciicode ();
1181 ret = strtofltmax (garglist->word->word, &ep);
1185 sh_invalidnum (garglist->word->word);
1186 /* Same thing about POSIX.2 conversion error requirements. */
1188 conversion_error = 1;
1190 else if (errno == ERANGE)
1191 printf_erange (garglist->word->word);
1193 garglist = garglist->next;
1197 /* NO check is needed for garglist here. */
1201 register intmax_t ch;
1202 #if defined (HANDLE_MULTIBYTE)
1204 size_t mblength, slen;
1208 #if defined (HANDLE_MULTIBYTE)
1209 slen = strlen (garglist->word->word+1);
1210 mblength = MBLEN (garglist->word->word+1, slen);
1213 mblength = mbtowc (&wc, garglist->word->word+1, slen);
1218 ch = (unsigned char)garglist->word->word[1];
1220 garglist = garglist->next;
1225 bind_printf_variable (name, value, flags)
1230 #if defined (ARRAY_VARS)
1231 if (valid_array_reference (name) == 0)
1232 return (bind_variable (name, value, flags));
1234 return (assign_array_element (name, value, flags));
1235 #else /* !ARRAY_VARS */
1236 return bind_variable (name, value, flags);
1237 #endif /* !ARRAY_VARS */