blob: 0180d0e5a64d5c58b01790e7debedb9e10b8f538 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/* printf - format and print data
Eric Andersencc8ed391999-10-05 16:24:54 +00003
Rob Landley251161f2006-01-06 20:28:05 +00004 Copyright 1999 Dave Cinege
5 Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
Eric Andersencc8ed391999-10-05 16:24:54 +00006
Rob Landley251161f2006-01-06 20:28:05 +00007 Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
8*/
Eric Andersencc8ed391999-10-05 16:24:54 +00009
10/* Usage: printf format [argument...]
11
12 A front end to the printf function that lets it be used from the shell.
13
14 Backslash escapes:
15
16 \" = double quote
17 \\ = backslash
18 \a = alert (bell)
19 \b = backspace
20 \c = produce no further output
21 \f = form feed
22 \n = new line
23 \r = carriage return
24 \t = horizontal tab
25 \v = vertical tab
26 \0ooo = octal number (ooo is 0 to 3 digits)
27 \xhhh = hexadecimal number (hhh is 1 to 3 digits)
28
29 Additional directive:
30
31 %b = print an argument string, interpreting backslash escapes
32
Denis Vlasenko13858992006-10-08 12:49:22 +000033 The 'format' argument is re-used as many times as necessary
Eric Andersencc8ed391999-10-05 16:24:54 +000034 to convert all of the given arguments.
35
36 David MacKenzie <djm@gnu.ai.mit.edu> */
Erik Andersene49d5ec2000-02-08 19:58:47 +000037
Eric Andersencc8ed391999-10-05 16:24:54 +000038
39// 19990508 Busy Boxed! Dave Cinege
40
Eric Andersencbe31da2001-02-20 06:14:08 +000041#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000042
Denis Vlasenkoc2905632006-09-23 16:01:09 +000043static int print_formatted(char *format, int argc, char **argv);
44static void print_direc(char *start, size_t length,
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000045 int field_width, int precision, const char *argument);
Eric Andersencc8ed391999-10-05 16:24:54 +000046
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000047typedef void (*converter)(const char *arg, void *result);
Denis Vlasenkoc2905632006-09-23 16:01:09 +000048
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000049static void multiconvert(const char *arg, void *result, converter convert)
Rob Landley251161f2006-01-06 20:28:05 +000050{
51 char s[16];
52 if (*arg == '"' || *arg == '\'') {
Denis Vlasenkod686a042006-11-27 14:43:21 +000053 sprintf(s, "%d", (unsigned char)arg[1]);
Denis Vlasenkoc2905632006-09-23 16:01:09 +000054 arg = s;
Rob Landley251161f2006-01-06 20:28:05 +000055 }
Denis Vlasenkod686a042006-11-27 14:43:21 +000056 convert(arg, result);
57 if (errno) /* Huh, looks strange... bug? */
Denis Vlasenkoc2905632006-09-23 16:01:09 +000058 fputs(arg, stderr);
Rob Landley251161f2006-01-06 20:28:05 +000059}
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000060
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000061static void conv_strtoul(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000062{
63 *(unsigned long*)result = bb_strtoul(arg, NULL, 10);
64}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000065static void conv_strtol(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000066{
67 *(long*)result = bb_strtol(arg, NULL, 10);
68}
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000069static void conv_strtod(const char *arg, void *result)
Denis Vlasenkod686a042006-11-27 14:43:21 +000070{
71 char *end;
72 /* Well, this one allows leading whitespace... so what */
73 /* What I like much less is that "-" is accepted too! :( */
74 *(double*)result = strtod(arg, &end);
75 if (end[0]) errno = ERANGE;
76}
77
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000078static unsigned long my_xstrtoul(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +000079{
80 unsigned long result;
Denis Vlasenkod686a042006-11-27 14:43:21 +000081 multiconvert(arg, &result, conv_strtoul);
Rob Landley251161f2006-01-06 20:28:05 +000082 return result;
83}
84
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000085static long my_xstrtol(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +000086{
87 long result;
Denis Vlasenkod686a042006-11-27 14:43:21 +000088 multiconvert(arg, &result, conv_strtol);
Rob Landley251161f2006-01-06 20:28:05 +000089 return result;
90}
91
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000092static double my_xstrtod(const char *arg)
Rob Landley251161f2006-01-06 20:28:05 +000093{
94 double result;
Denis Vlasenkod686a042006-11-27 14:43:21 +000095 multiconvert(arg, &result, conv_strtod);
Rob Landley251161f2006-01-06 20:28:05 +000096 return result;
97}
98
99static void print_esc_string(char *str)
100{
101 for (; *str; str++) {
102 if (*str == '\\') {
103 str++;
104 putchar(bb_process_escape_sequence((const char **)&str));
105 } else {
106 putchar(*str);
107 }
108
109 }
110}
111
Denis Vlasenko06af2162007-02-03 17:28:39 +0000112int printf_main(int argc, char **argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113int printf_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000114{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115 char *format;
116 int args_used;
Eric Andersencc8ed391999-10-05 16:24:54 +0000117
Denis Vlasenkoc2905632006-09-23 16:01:09 +0000118 if (argc <= 1 || argv[1][0] == '-') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000121
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 format = argv[1];
123 argc -= 2;
124 argv += 2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000125
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126 do {
127 args_used = print_formatted(format, argc, argv);
128 argc -= args_used;
129 argv += args_used;
130 }
131 while (args_used > 0 && argc > 0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000132
Denis Vlasenkoc2905632006-09-23 16:01:09 +0000133/* if (argc > 0)
134 fprintf(stderr, "excess args ignored");
Eric Andersencc8ed391999-10-05 16:24:54 +0000135*/
136
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000137 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000138}
139
140/* Print the text in FORMAT, using ARGV (with ARGC elements) for
Denis Vlasenko13858992006-10-08 12:49:22 +0000141 arguments to any '%' directives.
Eric Andersencc8ed391999-10-05 16:24:54 +0000142 Return the number of elements of ARGV used. */
143
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144static int print_formatted(char *format, int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000145{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 int save_argc = argc; /* Preserve original value. */
Denis Vlasenko13858992006-10-08 12:49:22 +0000147 char *f; /* Pointer into 'format'. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 char *direc_start; /* Start of % directive. */
149 size_t direc_length; /* Length of % directive. */
150 int field_width; /* Arg to first '*', or -1 if none. */
151 int precision; /* Arg to second '*', or -1 if none. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000152
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 for (f = format; *f; ++f) {
154 switch (*f) {
155 case '%':
156 direc_start = f++;
157 direc_length = 1;
158 field_width = precision = -1;
159 if (*f == '%') {
160 putchar('%');
161 break;
162 }
163 if (*f == 'b') {
164 if (argc > 0) {
165 print_esc_string(*argv);
166 ++argv;
167 --argc;
168 }
169 break;
170 }
171 if (strchr("-+ #", *f)) {
172 ++f;
173 ++direc_length;
174 }
175 if (*f == '*') {
176 ++f;
177 ++direc_length;
178 if (argc > 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000179 field_width = my_xstrtoul(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000180 ++argv;
181 --argc;
182 } else
183 field_width = 0;
184 } else
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000185 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000186 ++f;
187 ++direc_length;
188 }
189 if (*f == '.') {
190 ++f;
191 ++direc_length;
192 if (*f == '*') {
193 ++f;
194 ++direc_length;
195 if (argc > 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000196 precision = my_xstrtoul(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000197 ++argv;
198 --argc;
199 } else
200 precision = 0;
201 } else
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000202 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000203 ++f;
204 ++direc_length;
205 }
206 }
207 if (*f == 'l' || *f == 'L' || *f == 'h') {
208 ++f;
209 ++direc_length;
210 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000211 /*
Denis Vlasenkoc2905632006-09-23 16:01:09 +0000212 if (!strchr ("diouxXfeEgGcs", *f))
213 fprintf(stderr, "%%%c: invalid directive", *f);
214 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 ++direc_length;
216 if (argc > 0) {
217 print_direc(direc_start, direc_length, field_width,
218 precision, *argv);
219 ++argv;
220 --argc;
221 } else
222 print_direc(direc_start, direc_length, field_width,
223 precision, "");
224 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000225
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226 case '\\':
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000227 if (*++f == 'c')
228 exit(0);
229 putchar(bb_process_escape_sequence((const char **)&f));
230 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000232
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 default:
234 putchar(*f);
235 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000236 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000237
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 return save_argc - argc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000239}
240
Eric Andersencc8ed391999-10-05 16:24:54 +0000241static void
Erik Andersene49d5ec2000-02-08 19:58:47 +0000242print_direc(char *start, size_t length, int field_width, int precision,
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000243 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000244{
Denis Vlasenkoc2905632006-09-23 16:01:09 +0000245 char *p; /* Null-terminated copy of % directive. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000246
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247 p = xmalloc((unsigned) (length + 1));
248 strncpy(p, start, length);
249 p[length] = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000250
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251 switch (p[length - 1]) {
252 case 'd':
253 case 'i':
254 if (field_width < 0) {
255 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000256 printf(p, my_xstrtol(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000258 printf(p, precision, my_xstrtol(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259 } else {
260 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000261 printf(p, field_width, my_xstrtol(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000263 printf(p, field_width, precision, my_xstrtol(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264 }
265 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000266
Erik Andersene49d5ec2000-02-08 19:58:47 +0000267 case 'o':
268 case 'u':
269 case 'x':
270 case 'X':
271 if (field_width < 0) {
272 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000273 printf(p, my_xstrtoul(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000275 printf(p, precision, my_xstrtoul(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 } else {
277 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000278 printf(p, field_width, my_xstrtoul(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000280 printf(p, field_width, precision, my_xstrtoul(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281 }
282 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000283
Erik Andersene49d5ec2000-02-08 19:58:47 +0000284 case 'f':
285 case 'e':
286 case 'E':
287 case 'g':
288 case 'G':
289 if (field_width < 0) {
290 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000291 printf(p, my_xstrtod(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000293 printf(p, precision, my_xstrtod(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 } else {
295 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000296 printf(p, field_width, my_xstrtod(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000298 printf(p, field_width, precision, my_xstrtod(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000299 }
300 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000301
Erik Andersene49d5ec2000-02-08 19:58:47 +0000302 case 'c':
303 printf(p, *argument);
304 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000305
Erik Andersene49d5ec2000-02-08 19:58:47 +0000306 case 's':
307 if (field_width < 0) {
308 if (precision < 0)
309 printf(p, argument);
310 else
311 printf(p, precision, argument);
312 } else {
313 if (precision < 0)
314 printf(p, field_width, argument);
315 else
316 printf(p, field_width, precision, argument);
317 }
318 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000319 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000320
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321 free(p);
Eric Andersencc8ed391999-10-05 16:24:54 +0000322}