1 /* $NetBSD: printf.c,v 1.33 2008/07/21 14:19:24 lukem Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 #if !defined(BUILTIN) && !defined(SHELL)
35 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
36 The Regents of the University of California. All rights reserved.");
42 static char sccsid
[] = "@(#)printf.c 8.2 (Berkeley) 3/22/95";
44 __RCSID("$NetBSD: printf.c,v 1.33 2008/07/21 14:19:24 lukem Exp $");
48 #include <sys/types.h>
68 static void conv_escape_str(char *, void (*)(int));
69 static char *conv_escape(char *, char *);
70 static char *conv_expand(const char *);
71 static int getchr(void);
72 static double getdouble(void);
73 static int getwidth(void);
74 static intmax_t getintmax(void);
75 static uintmax_t getuintmax(void);
76 static char *getstr(void);
77 static char *mklong(const char *, int);
78 static void check_conversion(const char *, const char *);
79 static void usage(void);
81 static void b_count(int);
82 static void b_output(int);
83 static size_t b_length
;
89 #ifdef BUILTIN /* csh builtin */
90 #define main progprintf
93 #ifdef SHELL /* sh (aka ash) builtin */
94 #define main printfcmd
95 #include "../../bin/sh/bltin/bltin.h"
98 #define PF(f, func) { \
99 if (fieldwidth != -1) { \
100 if (precision != -1) \
101 error = printf(f, fieldwidth, precision, func); \
103 error = printf(f, fieldwidth, func); \
104 } else if (precision != -1) \
105 error = printf(f, precision, func); \
107 error = printf(f, func); \
110 #define APF(cpp, f, func) { \
111 if (fieldwidth != -1) { \
112 if (precision != -1) \
113 error = asprintf(cpp, f, fieldwidth, precision, func); \
115 error = asprintf(cpp, f, fieldwidth, func); \
116 } else if (precision != -1) \
117 error = asprintf(cpp, f, precision, func); \
119 error = asprintf(cpp, f, func); \
123 int main(int, char *[]);
125 int main(int argc
, char *argv
[])
128 int fieldwidth
, precision
;
134 #if !defined(SHELL) && !defined(BUILTIN)
135 (void)setlocale (LC_ALL
, "");
138 while ((ch
= getopt(argc
, argv
, "")) != -1) {
157 #define SKIP1 "#-+ 0"
158 #define SKIP2 "0123456789"
161 * Basic algorithm is to scan the format string for conversion
162 * specifications -- once one is found, find out if the field
163 * width or precision is a '*'; if it is, gather up value.
164 * Note, format strings are reused as necessary to use up the
165 * provided arguments, arguments of zero/null string are
166 * provided to use up the format string.
169 /* find next format specification */
170 for (fmt
= format
; (ch
= *fmt
++) != '\0';) {
173 fmt
= conv_escape(fmt
, &c_ch
);
177 if (ch
!= '%' || (*fmt
== '%' && ++fmt
)) {
182 /* Ok - we've found a format specification,
183 Save its address for a later printf(). */
186 /* skip to field width */
187 fmt
+= strspn(fmt
, SKIP1
);
190 fieldwidth
= getwidth();
194 /* skip to possible '.', get following precision */
195 fmt
+= strspn(fmt
, SKIP2
);
200 precision
= getwidth();
206 fmt
+= strspn(fmt
, SKIP2
);
210 warnx("missing format character");
213 /* null terminate format string to we can use it
214 as an argument to printf. */
220 const char *p
= conv_expand(getstr());
230 /* There has to be a better way to do this,
231 * but the string we generate might have
235 /* Free on entry in case shell longjumped out */
242 /* Count number of bytes we want to output */
244 conv_escape_str(cp
, b_count
);
245 t
= malloc(b_length
+ 1);
248 (void)memset(t
, 'x', b_length
);
250 /* Get printf to calculate the lengths */
256 /* Output leading spaces and data bytes */
257 conv_escape_str(cp
, b_output
);
258 /* Add any trailing spaces */
278 intmax_t p
= getintmax();
279 char *f
= mklong(start
, ch
);
289 uintmax_t p
= getuintmax();
290 char *f
= mklong(start
, ch
);
301 double p
= getdouble();
308 warnx("%s: invalid directive", start
);
313 /* escape if a \c was encountered */
315 return rval
& ~0x100;
317 } while (gargv
!= argv
&& *gargv
);
319 return rval
& ~0x100;
321 warn("print failed");
325 /* helper functions for conv_escape_str */
334 /* Output one converted character for every 'x' in the 'format' */
356 * Print SysV echo(1) style escape string
357 * Halts processing string if a \c escape is encountered.
360 conv_escape_str(char *str
, void (*do_putchar
)(int))
366 while ((ch
= *str
++) != '\0') {
374 /* \c as in SYSV echo - abort all processing.... */
380 * %b string octal constants are not like those in C.
381 * They start with a \0, and are followed by 0, 1, 2,
386 for (i
= 0; i
< 3; i
++) {
387 if (!isdigit((unsigned char)*str
) || *str
> '7')
389 octnum
= (octnum
<< 3) | (*str
++ - '0');
395 /* \[M][^|-]C as defined by vis(3) */
396 if (ch
== 'M' && *str
== '-') {
397 do_putchar(0200 | str
[1]);
401 if (ch
== 'M' && *str
== '^') {
417 /* Finally test for sequences valid in the format string */
418 str
= conv_escape(str
- 1, &c
);
424 * Print "standard" escape characters
427 conv_escape(char *str
, char *conv_ch
)
431 char num_buf
[4], *num_end
;
436 case '0': case '1': case '2': case '3':
437 case '4': case '5': case '6': case '7':
441 num_buf
[2] = ch
? str
[1] : 0;
443 value
= strtoul(num_buf
, &num_end
, 8);
444 str
+= num_end
- (num_buf
+ 1);
448 /* Hexadecimal character constants are not required to be
449 supported (by SuS v1) because there is no consistent
450 way to detect the end of the constant.
451 Supporting 2 byte constants is a compromise. */
454 num_buf
[1] = ch
? str
[1] : 0;
456 value
= strtoul(num_buf
, &num_end
, 16);
457 str
+= num_end
- num_buf
;
460 case '\\': value
= '\\'; break; /* backslash */
461 case '\'': value
= '\''; break; /* single quote */
462 case '"': value
= '"'; break; /* double quote */
463 case 'a': value
= '\a'; break; /* alert */
464 case 'b': value
= '\b'; break; /* backspace */
465 case 'e': value
= ESCAPE
; break; /* escape */
466 case 'f': value
= '\f'; break; /* form-feed */
467 case 'n': value
= '\n'; break; /* newline */
468 case 'r': value
= '\r'; break; /* carriage-return */
469 case 't': value
= '\t'; break; /* tab */
470 case 'v': value
= '\v'; break; /* vertical-tab */
473 warnx("unknown escape sequence `\\%c'", ch
);
483 /* expand a string so that everything is printable */
486 conv_expand(const char *str
)
488 static char *conv_str
;
494 /* get a buffer that is definitely large enough.... */
495 conv_str
= malloc(4 * strlen(str
) + 1);
500 while ((ch
= *(const unsigned char *)str
++) != '\0') {
502 /* Use C escapes for expected control characters */
503 case '\\': ch
= '\\'; break; /* backslash */
504 case '\'': ch
= '\''; break; /* single quote */
505 case '"': ch
= '"'; break; /* double quote */
506 case '\a': ch
= 'a'; break; /* alert */
507 case '\b': ch
= 'b'; break; /* backspace */
508 case ESCAPE
: ch
= 'e'; break; /* escape */
509 case '\f': ch
= 'f'; break; /* form-feed */
510 case '\n': ch
= 'n'; break; /* newline */
511 case '\r': ch
= 'r'; break; /* carriage-return */
512 case '\t': ch
= 't'; break; /* tab */
513 case '\v': ch
= 'v'; break; /* vertical-tab */
515 /* Copy anything printable */
520 /* Use vis(3) encodings for the rest */
549 mklong(const char *str
, int ch
)
551 static char copy
[64];
554 len
= strlen(str
) + 2;
555 if (len
> sizeof copy
) {
556 warnx("format %s too complex\n", str
);
559 (void)memmove(copy
, str
, len
- 3);
562 copy
[len
- 1] = '\0';
571 return (int)**gargv
++;
577 static char empty
[] = "";
595 val
= strtoul(s
, &ep
, 0);
596 check_conversion(s
, ep
);
598 /* Arbitrarily 'restrict' field widths to 1Mbyte */
599 if (val
< 0 || val
> 1 << 20) {
600 warnx("%s: invalid field width", s
);
618 if (*cp
== '\"' || *cp
== '\'')
622 val
= strtoimax(cp
, &ep
, 0);
623 check_conversion(cp
, ep
);
638 if (*cp
== '\"' || *cp
== '\'')
641 /* strtoumax won't error -ve values */
642 while (isspace(*(unsigned char *)cp
))
645 warnx("%s: expected positive numeric value", cp
);
651 val
= strtoumax(cp
, &ep
, 0);
652 check_conversion(cp
, ep
);
665 if (**gargv
== '\"' || **gargv
== '\'')
666 return (double) *((*gargv
++)+1);
669 val
= strtod(*gargv
, &ep
);
670 check_conversion(*gargv
++, ep
);
675 check_conversion(const char *s
, const char *ep
)
679 warnx("%s: expected numeric value", s
);
681 warnx("%s: not completely converted", s
);
683 } else if (errno
== ERANGE
) {
684 warnx("%s: %s", s
, strerror(ERANGE
));
692 (void)fprintf(stderr
, "Usage: %s format [arg ...]\n", getprogname());