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))
259 vbuf = xmalloc (vbsize = 16);
266 sh_invalidid (vname);
275 list = loptend; /* skip over possible `--' */
283 if (list->word->word == 0 || list->word->word[0] == '\0')
284 return (EXECUTION_SUCCESS);
286 format = list->word->word;
289 garglist = list->next;
291 /* If the format string is empty after preprocessing, return immediately. */
292 if (format == 0 || *format == 0)
293 return (EXECUTION_SUCCESS);
295 terminate_immediately++;
297 /* Basic algorithm is to scan the format string for conversion
298 specifications -- once one is found, find out if the field
299 width or precision is a '*'; if it is, gather up value. Note,
300 format strings are reused as necessary to use up the provided
301 arguments, arguments of zero/null string are provided to use
302 up the format string. */
306 /* find next format specification */
307 for (fmt = format; *fmt; fmt++)
309 precision = fieldwidth = 0;
310 have_fieldwidth = have_precision = 0;
315 /* A NULL third argument to tescape means to bypass the
316 special processing for arguments to %b. */
317 #if defined (HANDLE_MULTIBYTE)
318 /* Accommodate possible use of \u or \U, which can result in
319 multibyte characters */
320 memset (mbch, '\0', sizeof (mbch));
321 fmt += tescape (fmt, mbch, &mblen, (int *)NULL);
322 for (mbind = 0; mbind < mblen; mbind++)
325 fmt += tescape (fmt, &nextch, (int *)NULL, (int *)NULL);
328 fmt--; /* for loop will increment it for us again */
338 /* ASSERT(*fmt == '%') */
341 if (*fmt == '%') /* %% prints a % */
347 /* found format specification, skip to field width */
348 for (; *fmt && strchr(SKIP1, *fmt); ++fmt)
351 /* Skip optional field width. */
356 fieldwidth = getint ();
362 /* Skip optional '.' and precision */
370 precision = getint ();
374 /* Negative precisions are allowed but treated as if the
375 precision were missing; I would like to allow a leading
376 `+' in the precision number as an extension, but lots
377 of asprintf/fprintf implementations get this wrong. */
379 if (*fmt == '-' || *fmt == '+')
389 /* skip possible format modifiers */
391 while (*fmt && strchr (LENMODS, *fmt))
396 builtin_error (_("`%s': missing format character"), start);
397 PRETURN (EXECUTION_FAILURE);
401 thisch = modstart[0];
402 nextch = modstart[1];
403 modstart[0] = convch;
428 char *timefmt, timebuf[128], *t;
434 modstart[1] = nextch; /* restore char after left paren */
435 timefmt = xmalloc (strlen (fmt) + 3);
436 fmt++; /* skip over left paren */
437 for (t = timefmt, n = 1; *fmt; )
441 else if (*fmt == ')')
450 builtin_warning (_("`%c': invalid time format specification"), *fmt);
456 if (timefmt[0] == '\0')
459 timefmt[1] = 'X'; /* locale-specific current time - should we use `+'? */
462 /* argument is seconds since the epoch with special -1 and -2 */
465 secs = NOW; /* roughly date +%s */
467 secs = shell_start_time; /* roughly $SECONDS */
470 #if defined (HAVE_TZSET)
471 sv_tz ("TZ"); /* XXX -- just make sure */
473 tm = localtime (&secs);
474 n = strftime (timebuf, sizeof (timebuf), timefmt, tm);
479 timebuf[sizeof(timebuf) - 1] = '\0';
480 /* convert to %s format that preserves fieldwidth and precision */
483 n = printstr (start, timebuf, strlen (timebuf), fieldwidth, precision); /* XXX - %s for now */
488 PRETURN (EXECUTION_FAILURE);
500 if (legal_identifier (var))
501 bind_var_to_int (var, tw);
505 PRETURN (EXECUTION_FAILURE);
511 case 'b': /* expand escapes in argument */
518 xp = bexpand (p, strlen (p), &ch, &rlen);
522 /* Have to use printstr because of possible NUL bytes
523 in XP -- printf does not handle that well. */
524 r = printstr (start, xp, rlen, fieldwidth, precision);
529 retval = EXECUTION_FAILURE;
539 case 'q': /* print with shell quoting */
546 if (p && *p == 0) /* XXX - getstr never returns null */
547 xp = savestring ("''");
548 else if (ansic_shouldquote (p))
549 xp = ansic_quote (p, 0, (int *)0);
551 xp = sh_backslash_quote (p);
554 /* Use printstr to get fieldwidth and precision right. */
555 r = printstr (start, xp, strlen (xp), fieldwidth, precision);
565 PRETURN (EXECUTION_FAILURE);
576 p = pp = getintmax ();
579 f = mklong (start, PRIdMAX, sizeof (PRIdMAX) - 2);
584 /* Optimize the common case where the integer fits
585 in "long". This also works around some long
586 long and/or intmax_t library bugs in the common
587 case, e.g. glibc 2.2 x86. */
588 f = mklong (start, "l", 1);
603 p = pp = getuintmax ();
606 f = mklong (start, PRIdMAX, sizeof (PRIdMAX) - 2);
611 f = mklong (start, "l", 1);
623 #if defined (HAVE_PRINTF_A_FORMAT)
632 f = mklong (start, FLOATMAX_CONV, sizeof(FLOATMAX_CONV) - 1);
637 /* We don't output unrecognized format characters; we print an
638 error message and return a failure exit status. */
640 builtin_error (_("`%c': invalid format character"), convch);
641 PRETURN (EXECUTION_FAILURE);
644 modstart[0] = thisch;
645 modstart[1] = nextch;
652 PRETURN (EXECUTION_FAILURE);
655 while (garglist && garglist != list->next);
657 if (conversion_error)
658 retval = EXECUTION_FAILURE;
667 builtin_error (_("warning: %s: %s"), s, strerror(ERANGE));
670 /* We duplicate a lot of what printf(3) does here. */
672 printstr (fmt, string, len, fieldwidth, precision)
673 char *fmt; /* format */
674 char *string; /* expanded string argument */
675 int len; /* length of expanded string */
676 int fieldwidth; /* argument for width of `*' */
677 int precision; /* argument for precision of `*' */
682 int padlen, nc, ljust, i;
683 int fw, pr; /* fieldwidth and precision */
686 if (string == 0 || *string == '\0')
688 if (string == 0 || len == 0)
702 while (strchr (SKIP1, *fmt))
709 /* get fieldwidth, if present */
720 else if (DIGIT (*fmt))
724 fw = (fw * 10) + (*fmt++ - '0');
727 /* get precision, if present */
736 else if (DIGIT (*fmt))
740 pr = (pr * 10) + (*fmt++ - '0');
745 /* If we remove this, get rid of `s'. */
746 if (*fmt != 'b' && *fmt != 'q')
748 internal_error ("format parsing problem: %s", s);
753 /* chars from string to print */
754 nc = (pr >= 0 && pr <= len) ? pr : len;
762 /* leading pad characters */
763 for (; padlen > 0; padlen--)
766 /* output NC characters from STRING */
767 for (i = 0; i < nc; i++)
770 /* output any necessary trailing padding */
771 for (; padlen < 0; padlen++)
774 return (ferror (stdout) ? -1 : 0);
777 /* Convert STRING by expanding the escape sequences specified by the
778 POSIX standard for printf's `%b' format string. If SAWC is non-null,
779 perform the processing appropriate for %b arguments. In particular,
780 recognize `\c' and use that as a string terminator. If we see \c, set
781 *SAWC to 1 before returning. LEN is the length of STRING. */
783 /* Translate a single backslash-escape sequence starting at ESTART (the
784 character after the backslash) and return the number of characters
785 consumed by the sequence. CP is the place to return the translated
786 value. *SAWC is set to 1 if the escape sequence was \c, since that means
787 to short-circuit the rest of the processing. If SAWC is null, we don't
788 do the \c short-circuiting, and \c is treated as an unrecognized escape
789 sequence; we also bypass the other processing specific to %b arguments. */
791 tescape (estart, cp, lenp, sawc)
798 unsigned long uvalue;
806 #if defined (__STDC__)
807 case 'a': *cp = '\a'; break;
809 case 'a': *cp = '\007'; break;
812 case 'b': *cp = '\b'; break;
815 case 'E': *cp = '\033'; break; /* ESC -- non-ANSI */
817 case 'f': *cp = '\f'; break;
819 case 'n': *cp = '\n'; break;
821 case 'r': *cp = '\r'; break;
823 case 't': *cp = '\t'; break;
825 case 'v': *cp = '\v'; break;
827 /* The octal escape sequences are `\0' followed by up to three octal
828 digits (if SAWC), or `\' followed by up to three octal digits (if
829 !SAWC). As an extension, we allow the latter form even if SAWC. */
830 case '0': case '1': case '2': case '3':
831 case '4': case '5': case '6': case '7':
832 evalue = OCTVALUE (c);
833 for (temp = 2 + (!evalue && !!sawc); ISOCTAL (*p) && temp--; p++)
834 evalue = (evalue * 8) + OCTVALUE (*p);
838 /* And, as another extension, we allow \xNN, where each N is a
841 for (temp = 2, evalue = 0; ISXDIGIT ((unsigned char)*p) && temp--; p++)
842 evalue = (evalue * 16) + HEXVALUE (*p);
845 builtin_error (_("missing hex digit for \\x"));
852 #if defined (HANDLE_MULTIBYTE)
855 temp = (c == 'u') ? 4 : 8; /* \uNNNN \UNNNNNNNN */
856 for (uvalue = 0; ISXDIGIT ((unsigned char)*p) && temp--; p++)
857 uvalue = (uvalue * 16) + HEXVALUE (*p);
860 builtin_error (_("missing unicode digit for \\%c"), c);
864 if (uvalue <= UCHAR_MAX)
868 temp = u32cconv (uvalue, cp);
876 case '\\': /* \\ -> \ */
880 /* SAWC == 0 means that \', \", and \? are recognized as escape
881 sequences, though the only processing performed is backslash
883 case '\'': case '"': case '?':
899 /* other backslash escapes are passed through unaltered */
908 bexpand (string, len, sawc, lenp)
910 int len, *sawc, *lenp;
913 char *ret, *r, *s, c;
914 #if defined (HANDLE_MULTIBYTE)
919 if (string == 0 || len == 0)
925 return ((char *)NULL);
928 ret = (char *)xmalloc (len + 1);
929 for (r = ret, s = string; s && *s; )
932 if (c != '\\' || *s == '\0')
938 #if defined (HANDLE_MULTIBYTE)
939 memset (mbch, '\0', sizeof (mbch));
940 s += tescape (s, mbch, &mblen, &temp);
942 s += tescape (s, &c, (int *)NULL, &temp);
951 #if defined (HANDLE_MULTIBYTE)
952 for (mbind = 0; mbind < mblen; mbind++)
972 nlen = vblen + blen + 1;
975 vbsize = ((nlen + 63) >> 6) << 6;
976 vbuf = (char *)xrealloc (vbuf, vbsize);
980 vbuf[vblen++] = buf[0];
983 FASTCOPY (buf, vbuf + vblen, blen);
989 if (strlen (vbuf) != vblen)
990 internal_error ("printf:vbadd: vblen (%d) != strlen (vbuf) (%d)", vblen, (int)strlen (vbuf));
997 #if defined (PREFER_STDARG)
998 vbprintf (const char *format, ...)
1000 vbprintf (format, va_alist)
1009 SH_VA_START (args, format);
1010 blen = vsnprintf (vbuf + vblen, vbsize - vblen, format, args);
1013 nlen = vblen + blen + 1;
1016 vbsize = ((nlen + 63) >> 6) << 6;
1017 vbuf = (char *)xrealloc (vbuf, vbsize);
1018 SH_VA_START (args, format);
1019 blen = vsnprintf (vbuf + vblen, vbsize - vblen, format, args);
1027 if (strlen (vbuf) != vblen)
1028 internal_error ("printf:vbadd: vblen (%d) != strlen (vbuf) (%d)", vblen, (int)strlen (vbuf));
1035 mklong (str, modifiers, mlen)
1042 slen = strlen (str);
1043 len = slen + mlen + 1;
1045 if (len > conv_bufsize)
1047 conv_bufsize = (((len + 1023) >> 10) << 10);
1048 conv_buf = (char *)xrealloc (conv_buf, conv_bufsize);
1051 FASTCOPY (str, conv_buf, slen - 1);
1052 FASTCOPY (modifiers, conv_buf + slen - 1, mlen);
1054 conv_buf[len - 2] = str[slen - 1];
1055 conv_buf[len - 1] = '\0';
1067 ret = (int)garglist->word->word[0];
1068 garglist = garglist->next;
1080 ret = garglist->word->word;
1081 garglist = garglist->next;
1094 printf_erange (garglist->word->word);
1097 else if (ret < INT_MIN)
1099 printf_erange (garglist->word->word);
1115 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1116 return asciicode ();
1119 ret = strtoimax (garglist->word->word, &ep, 0);
1123 sh_invalidnum (garglist->word->word);
1124 /* POSIX.2 says ``...a diagnostic message shall be written to standard
1125 error, and the utility shall not exit with a zero exit status, but
1126 shall continue processing any remaining operands and shall write the
1127 value accumulated at the time the error was detected to standard
1130 ret = 0; /* return partially-converted value from strtoimax */
1132 conversion_error = 1;
1134 else if (errno == ERANGE)
1135 printf_erange (garglist->word->word);
1137 garglist = garglist->next;
1150 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1151 return asciicode ();
1154 ret = strtoumax (garglist->word->word, &ep, 0);
1158 sh_invalidnum (garglist->word->word);
1159 /* Same POSIX.2 conversion error requirements as getintmax(). */
1161 conversion_error = 1;
1163 else if (errno == ERANGE)
1164 printf_erange (garglist->word->word);
1166 garglist = garglist->next;
1179 if (garglist->word->word[0] == '\'' || garglist->word->word[0] == '"')
1180 return asciicode ();
1183 ret = strtofltmax (garglist->word->word, &ep);
1187 sh_invalidnum (garglist->word->word);
1188 /* Same thing about POSIX.2 conversion error requirements. */
1190 conversion_error = 1;
1192 else if (errno == ERANGE)
1193 printf_erange (garglist->word->word);
1195 garglist = garglist->next;
1199 /* NO check is needed for garglist here. */
1203 register intmax_t ch;
1204 #if defined (HANDLE_MULTIBYTE)
1206 size_t mblength, slen;
1210 #if defined (HANDLE_MULTIBYTE)
1211 slen = strlen (garglist->word->word+1);
1212 mblength = MBLEN (garglist->word->word+1, slen);
1215 mblength = mbtowc (&wc, garglist->word->word+1, slen);
1220 ch = (unsigned char)garglist->word->word[1];
1222 garglist = garglist->next;
1227 bind_printf_variable (name, value, flags)
1232 #if defined (ARRAY_VARS)
1233 if (valid_array_reference (name) == 0)
1234 return (bind_variable (name, value, flags));
1236 return (assign_array_element (name, value, flags));
1237 #else /* !ARRAY_VARS */
1238 return bind_variable (name, value, flags);
1239 #endif /* !ARRAY_VARS */