blob: 924499b29511b5e25e7429584717376f8841fe42 [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
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112int printf_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000113{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114 char *format;
115 int args_used;
Eric Andersencc8ed391999-10-05 16:24:54 +0000116
Denis Vlasenkoc2905632006-09-23 16:01:09 +0000117 if (argc <= 1 || argv[1][0] == '-') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000120
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 format = argv[1];
122 argc -= 2;
123 argv += 2;
Eric Andersencc8ed391999-10-05 16:24:54 +0000124
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125 do {
126 args_used = print_formatted(format, argc, argv);
127 argc -= args_used;
128 argv += args_used;
129 }
130 while (args_used > 0 && argc > 0);
Eric Andersencc8ed391999-10-05 16:24:54 +0000131
Denis Vlasenkoc2905632006-09-23 16:01:09 +0000132/* if (argc > 0)
133 fprintf(stderr, "excess args ignored");
Eric Andersencc8ed391999-10-05 16:24:54 +0000134*/
135
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000136 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000137}
138
139/* Print the text in FORMAT, using ARGV (with ARGC elements) for
Denis Vlasenko13858992006-10-08 12:49:22 +0000140 arguments to any '%' directives.
Eric Andersencc8ed391999-10-05 16:24:54 +0000141 Return the number of elements of ARGV used. */
142
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143static int print_formatted(char *format, int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +0000144{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 int save_argc = argc; /* Preserve original value. */
Denis Vlasenko13858992006-10-08 12:49:22 +0000146 char *f; /* Pointer into 'format'. */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 char *direc_start; /* Start of % directive. */
148 size_t direc_length; /* Length of % directive. */
149 int field_width; /* Arg to first '*', or -1 if none. */
150 int precision; /* Arg to second '*', or -1 if none. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000151
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 for (f = format; *f; ++f) {
153 switch (*f) {
154 case '%':
155 direc_start = f++;
156 direc_length = 1;
157 field_width = precision = -1;
158 if (*f == '%') {
159 putchar('%');
160 break;
161 }
162 if (*f == 'b') {
163 if (argc > 0) {
164 print_esc_string(*argv);
165 ++argv;
166 --argc;
167 }
168 break;
169 }
170 if (strchr("-+ #", *f)) {
171 ++f;
172 ++direc_length;
173 }
174 if (*f == '*') {
175 ++f;
176 ++direc_length;
177 if (argc > 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000178 field_width = my_xstrtoul(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179 ++argv;
180 --argc;
181 } else
182 field_width = 0;
183 } else
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000184 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 ++f;
186 ++direc_length;
187 }
188 if (*f == '.') {
189 ++f;
190 ++direc_length;
191 if (*f == '*') {
192 ++f;
193 ++direc_length;
194 if (argc > 0) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000195 precision = my_xstrtoul(*argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 ++argv;
197 --argc;
198 } else
199 precision = 0;
200 } else
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000201 while (isdigit(*f)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 ++f;
203 ++direc_length;
204 }
205 }
206 if (*f == 'l' || *f == 'L' || *f == 'h') {
207 ++f;
208 ++direc_length;
209 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000210 /*
Denis Vlasenkoc2905632006-09-23 16:01:09 +0000211 if (!strchr ("diouxXfeEgGcs", *f))
212 fprintf(stderr, "%%%c: invalid directive", *f);
213 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 ++direc_length;
215 if (argc > 0) {
216 print_direc(direc_start, direc_length, field_width,
217 precision, *argv);
218 ++argv;
219 --argc;
220 } else
221 print_direc(direc_start, direc_length, field_width,
222 precision, "");
223 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 case '\\':
Glenn L McGrath240a91d2004-09-15 02:05:23 +0000226 if (*++f == 'c')
227 exit(0);
228 putchar(bb_process_escape_sequence((const char **)&f));
229 f--;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000231
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232 default:
233 putchar(*f);
234 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000235 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000236
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237 return save_argc - argc;
Eric Andersencc8ed391999-10-05 16:24:54 +0000238}
239
Eric Andersencc8ed391999-10-05 16:24:54 +0000240static void
Erik Andersene49d5ec2000-02-08 19:58:47 +0000241print_direc(char *start, size_t length, int field_width, int precision,
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000242 const char *argument)
Eric Andersencc8ed391999-10-05 16:24:54 +0000243{
Denis Vlasenkoc2905632006-09-23 16:01:09 +0000244 char *p; /* Null-terminated copy of % directive. */
Eric Andersencc8ed391999-10-05 16:24:54 +0000245
Erik Andersene49d5ec2000-02-08 19:58:47 +0000246 p = xmalloc((unsigned) (length + 1));
247 strncpy(p, start, length);
248 p[length] = 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000249
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 switch (p[length - 1]) {
251 case 'd':
252 case 'i':
253 if (field_width < 0) {
254 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000255 printf(p, my_xstrtol(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000256 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000257 printf(p, precision, my_xstrtol(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000258 } else {
259 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000260 printf(p, field_width, my_xstrtol(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000262 printf(p, field_width, precision, my_xstrtol(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263 }
264 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000265
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 case 'o':
267 case 'u':
268 case 'x':
269 case 'X':
270 if (field_width < 0) {
271 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000272 printf(p, my_xstrtoul(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000274 printf(p, precision, my_xstrtoul(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275 } else {
276 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000277 printf(p, field_width, my_xstrtoul(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000278 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000279 printf(p, field_width, precision, my_xstrtoul(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280 }
281 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000282
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 case 'f':
284 case 'e':
285 case 'E':
286 case 'g':
287 case 'G':
288 if (field_width < 0) {
289 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000290 printf(p, my_xstrtod(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000292 printf(p, precision, my_xstrtod(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 } else {
294 if (precision < 0)
Denis Vlasenko13858992006-10-08 12:49:22 +0000295 printf(p, field_width, my_xstrtod(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 else
Denis Vlasenko13858992006-10-08 12:49:22 +0000297 printf(p, field_width, precision, my_xstrtod(argument));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298 }
299 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000300
Erik Andersene49d5ec2000-02-08 19:58:47 +0000301 case 'c':
302 printf(p, *argument);
303 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000304
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305 case 's':
306 if (field_width < 0) {
307 if (precision < 0)
308 printf(p, argument);
309 else
310 printf(p, precision, argument);
311 } else {
312 if (precision < 0)
313 printf(p, field_width, argument);
314 else
315 printf(p, field_width, precision, argument);
316 }
317 break;
Eric Andersencc8ed391999-10-05 16:24:54 +0000318 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000319
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 free(p);
Eric Andersencc8ed391999-10-05 16:24:54 +0000321}