suppress warnings about easch <applet>_main() having
[platform/upstream/busybox.git] / coreutils / printf.c
1 /* vi: set sw=4 ts=4: */
2 /* printf - format and print data
3
4    Copyright 1999 Dave Cinege
5    Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
6
7    Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
8 */
9
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
33    The 'format' argument is re-used as many times as necessary
34    to convert all of the given arguments.
35
36    David MacKenzie <djm@gnu.ai.mit.edu> */
37
38
39 //   19990508 Busy Boxed! Dave Cinege
40
41 #include "busybox.h"
42
43 static int print_formatted(char *format, int argc, char **argv);
44 static void print_direc(char *start, size_t length,
45                 int field_width, int precision, const char *argument);
46
47 typedef void (*converter)(const char *arg, void *result);
48
49 static void multiconvert(const char *arg, void *result, converter convert)
50 {
51         char s[16];
52         if (*arg == '"' || *arg == '\'') {
53                 sprintf(s, "%d", (unsigned char)arg[1]);
54                 arg = s;
55         }
56         convert(arg, result);
57         if (errno) /* Huh, looks strange... bug? */
58                 fputs(arg, stderr);
59 }
60
61 static void conv_strtoul(const char *arg, void *result)
62 {
63         *(unsigned long*)result = bb_strtoul(arg, NULL, 10);
64 }
65 static void conv_strtol(const char *arg, void *result)
66 {
67         *(long*)result = bb_strtol(arg, NULL, 10);
68 }
69 static void conv_strtod(const char *arg, void *result)
70 {
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
78 static unsigned long my_xstrtoul(const char *arg)
79 {
80         unsigned long result;
81         multiconvert(arg, &result, conv_strtoul);
82         return result;
83 }
84
85 static long my_xstrtol(const char *arg)
86 {
87         long result;
88         multiconvert(arg, &result, conv_strtol);
89         return result;
90 }
91
92 static double my_xstrtod(const char *arg)
93 {
94         double result;
95         multiconvert(arg, &result, conv_strtod);
96         return result;
97 }
98
99 static 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
112 int printf_main(int argc, char **argv);
113 int printf_main(int argc, char **argv)
114 {
115         char *format;
116         int args_used;
117
118         if (argc <= 1 || argv[1][0] == '-') {
119                 bb_show_usage();
120         }
121
122         format = argv[1];
123         argc -= 2;
124         argv += 2;
125
126         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);
132
133 /*      if (argc > 0)
134                 fprintf(stderr, "excess args ignored");
135 */
136
137         return EXIT_SUCCESS;
138 }
139
140 /* Print the text in FORMAT, using ARGV (with ARGC elements) for
141    arguments to any '%' directives.
142    Return the number of elements of ARGV used.  */
143
144 static int print_formatted(char *format, int argc, char **argv)
145 {
146         int save_argc = argc;           /* Preserve original value.  */
147         char *f;                                        /* Pointer into 'format'.  */
148         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.  */
152
153         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) {
179                                         field_width = my_xstrtoul(*argv);
180                                         ++argv;
181                                         --argc;
182                                 } else
183                                         field_width = 0;
184                         } else
185                                 while (isdigit(*f)) {
186                                         ++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) {
196                                                 precision = my_xstrtoul(*argv);
197                                                 ++argv;
198                                                 --argc;
199                                         } else
200                                                 precision = 0;
201                                 } else
202                                         while (isdigit(*f)) {
203                                                 ++f;
204                                                 ++direc_length;
205                                         }
206                         }
207                         if (*f == 'l' || *f == 'L' || *f == 'h') {
208                                 ++f;
209                                 ++direc_length;
210                         }
211                         /*
212                         if (!strchr ("diouxXfeEgGcs", *f))
213                         fprintf(stderr, "%%%c: invalid directive", *f);
214                         */
215                         ++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;
225
226                 case '\\':
227                         if (*++f == 'c')
228                                 exit(0);
229                         putchar(bb_process_escape_sequence((const char **)&f));
230                         f--;
231                         break;
232
233                 default:
234                         putchar(*f);
235                 }
236         }
237
238         return save_argc - argc;
239 }
240
241 static void
242 print_direc(char *start, size_t length, int field_width, int precision,
243                 const char *argument)
244 {
245         char *p;                /* Null-terminated copy of % directive. */
246
247         p = xmalloc((unsigned) (length + 1));
248         strncpy(p, start, length);
249         p[length] = 0;
250
251         switch (p[length - 1]) {
252         case 'd':
253         case 'i':
254                 if (field_width < 0) {
255                         if (precision < 0)
256                                 printf(p, my_xstrtol(argument));
257                         else
258                                 printf(p, precision, my_xstrtol(argument));
259                 } else {
260                         if (precision < 0)
261                                 printf(p, field_width, my_xstrtol(argument));
262                         else
263                                 printf(p, field_width, precision, my_xstrtol(argument));
264                 }
265                 break;
266
267         case 'o':
268         case 'u':
269         case 'x':
270         case 'X':
271                 if (field_width < 0) {
272                         if (precision < 0)
273                                 printf(p, my_xstrtoul(argument));
274                         else
275                                 printf(p, precision, my_xstrtoul(argument));
276                 } else {
277                         if (precision < 0)
278                                 printf(p, field_width, my_xstrtoul(argument));
279                         else
280                                 printf(p, field_width, precision, my_xstrtoul(argument));
281                 }
282                 break;
283
284         case 'f':
285         case 'e':
286         case 'E':
287         case 'g':
288         case 'G':
289                 if (field_width < 0) {
290                         if (precision < 0)
291                                 printf(p, my_xstrtod(argument));
292                         else
293                                 printf(p, precision, my_xstrtod(argument));
294                 } else {
295                         if (precision < 0)
296                                 printf(p, field_width, my_xstrtod(argument));
297                         else
298                                 printf(p, field_width, precision, my_xstrtod(argument));
299                 }
300                 break;
301
302         case 'c':
303                 printf(p, *argument);
304                 break;
305
306         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;
319         }
320
321         free(p);
322 }