| 1 | This file is printf.def, from which is created printf.c.
|
|---|
| 2 | It implements the builtin "printf" in Bash.
|
|---|
| 3 |
|
|---|
| 4 | Copyright (C) 1997-2005 Free Software Foundation, Inc.
|
|---|
| 5 |
|
|---|
| 6 | This file is part of GNU Bash, the Bourne Again SHell.
|
|---|
| 7 |
|
|---|
| 8 | Bash is free software; you can redistribute it and/or modify it under
|
|---|
| 9 | the terms of the GNU General Public License as published by the Free
|
|---|
| 10 | Software Foundation; either version 2, or (at your option) any later
|
|---|
| 11 | version.
|
|---|
| 12 |
|
|---|
| 13 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|---|
| 15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|---|
| 16 | for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License along
|
|---|
| 19 | with Bash; see the file COPYING. If not, write to the Free Software
|
|---|
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
|
|---|
| 21 |
|
|---|
| 22 | $PRODUCES printf.c
|
|---|
| 23 |
|
|---|
| 24 | $BUILTIN printf
|
|---|
| 25 | $FUNCTION printf_builtin
|
|---|
| 26 | $SHORT_DOC printf [-v var] format [arguments]
|
|---|
| 27 | printf formats and prints ARGUMENTS under control of the FORMAT. FORMAT
|
|---|
| 28 | is a character string which contains three types of objects: plain
|
|---|
| 29 | characters, which are simply copied to standard output, character escape
|
|---|
| 30 | sequences which are converted and copied to the standard output, and
|
|---|
| 31 | format specifications, each of which causes printing of the next successive
|
|---|
| 32 | argument. In addition to the standard printf(1) formats, %b means to
|
|---|
| 33 | expand backslash escape sequences in the corresponding argument, and %q
|
|---|
| 34 | means to quote the argument in a way that can be reused as shell input.
|
|---|
| 35 | If the -v option is supplied, the output is placed into the value of the
|
|---|
| 36 | shell variable VAR rather than being sent to the standard output.
|
|---|
| 37 | $END
|
|---|
| 38 |
|
|---|
| 39 | #include <config.h>
|
|---|
| 40 |
|
|---|
| 41 | #include "../bashtypes.h"
|
|---|
| 42 |
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #if defined (HAVE_LIMITS_H)
|
|---|
| 45 | # include <limits.h>
|
|---|
| 46 | #else
|
|---|
| 47 | /* Assume 32-bit ints. */
|
|---|
| 48 | # define INT_MAX 2147483647
|
|---|
| 49 | # define INT_MIN (-2147483647-1)
|
|---|
| 50 | #endif
|
|---|
| 51 |
|
|---|
| 52 | #include <stdio.h>
|
|---|
| 53 | #include <chartypes.h>
|
|---|
| 54 |
|
|---|
| 55 | #ifdef HAVE_INTTYPES_H
|
|---|
| 56 | # include <inttypes.h>
|
|---|
| 57 | #endif
|
|---|
| 58 |
|
|---|
| 59 | #include "../bashansi.h"
|
|---|
| 60 | #include "../bashintl.h"
|
|---|
| 61 |
|
|---|
| 62 | #include "../shell.h"
|
|---|
| 63 | #include "stdc.h"
|
|---|
| 64 | #include "bashgetopt.h"
|
|---|
| 65 | #include "common.h"
|
|---|
| 66 |
|
|---|
| 67 | #if !defined (PRIdMAX)
|
|---|
| 68 | # if HAVE_LONG_LONG
|
|---|
| 69 | # define PRIdMAX "lld"
|
|---|
| 70 | # else
|
|---|
| 71 | # define PRIdMAX "ld"
|
|---|
| 72 | # endif
|
|---|
| 73 | #endif
|
|---|
| 74 |
|
|---|
| 75 | #if !defined (errno)
|
|---|
| 76 | extern int errno;
|
|---|
| 77 | #endif
|
|---|
| 78 |
|
|---|
| 79 | #define PC(c) \
|
|---|
| 80 | do { \
|
|---|
| 81 | char b[2]; \
|
|---|
| 82 | tw++; \
|
|---|
| 83 | b[0] = c; b[1] = '\0'; \
|
|---|
| 84 | if (vflag) \
|
|---|
| 85 | vbadd (b, 1); \
|
|---|
| 86 | else \
|
|---|
| 87 | putchar (c); \
|
|---|
| 88 | } while (0)
|
|---|
| 89 |
|
|---|
| 90 | #define PF(f, func) \
|
|---|
| 91 | do { \
|
|---|
| 92 | char *b = 0; \
|
|---|
| 93 | int nw; \
|
|---|
| 94 | if (have_fieldwidth && have_precision) \
|
|---|
| 95 | nw = asprintf(&b, f, fieldwidth, precision, func); \
|
|---|
| 96 | else if (have_fieldwidth) \
|
|---|
| 97 | nw = asprintf(&b, f, fieldwidth, func); \
|
|---|
| 98 | else if (have_precision) \
|
|---|
| 99 | nw = asprintf(&b, f, precision, func); \
|
|---|
| 100 | else \
|
|---|
| 101 | nw = asprintf(&b, f, func); \
|
|---|
| 102 | tw += nw; \
|
|---|
| 103 | if (b) \
|
|---|
| 104 | { \
|
|---|
| 105 | if (vflag) \
|
|---|
| 106 | (void)vbadd (b, nw); \
|
|---|
| 107 | else \
|
|---|
| 108 | (void)fputs (b, stdout); \
|
|---|
| 109 | free (b); \
|
|---|
| 110 | } \
|
|---|
| 111 | } while (0)
|
|---|
| 112 |
|
|---|
| 113 | /* We free the buffer used by mklong() if it's `too big'. */
|
|---|
| 114 | #define PRETURN(value) \
|
|---|
| 115 | do \
|
|---|
| 116 | { \
|
|---|
| 117 | if (vflag) \
|
|---|
| 118 | { \
|
|---|
| 119 | bind_variable (vname, vbuf, 0); \
|
|---|
| 120 | stupidly_hack_special_variables (vname); \
|
|---|
| 121 | } \
|
|---|
| 122 | if (conv_bufsize > 4096 ) \
|
|---|
| 123 | { \
|
|---|
| 124 | free (conv_buf); \
|
|---|
| 125 | conv_bufsize = 0; \
|
|---|
| 126 | conv_buf = 0; \
|
|---|
| 127 | } \
|
|---|
| 128 | if (vbsize > 4096) \
|
|---|
| 129 | { \
|
|---|
| 130 | free (vbuf); \
|
|---|
| 131 | vbsize = 0; \
|
|---|
| 132 | vbuf = 0; \
|
|---|
| 133 | } \
|
|---|
| 134 | fflush (stdout); \
|
|---|
| 135 | return (value); \
|
|---|
| 136 | } \
|
|---|
|
|---|