Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* printf - format and print data |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 3 | |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 4 | Copyright 1999 Dave Cinege |
| 5 | Portions copyright (C) 1990-1996 Free Software Foundation, Inc. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 6 | |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 7 | Licensed under GPL v2 or later, see file LICENSE in this tarball for details. |
| 8 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 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> */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 37 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | |
| 39 | // 19990508 Busy Boxed! Dave Cinege |
| 40 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 41 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 43 | static int print_formatted(char *format, int argc, char **argv); |
| 44 | static void print_direc(char *start, size_t length, |
"Vladimir N. Oleynik" | 5c2b238 | 2006-02-28 10:15:42 +0000 | [diff] [blame] | 45 | int field_width, int precision, char *argument); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 46 | |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 47 | typedef int (*converter)(char *arg, void *result); |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 48 | |
Bernhard Reutner-Fischer | 101a470 | 2006-04-03 15:46:14 +0000 | [diff] [blame] | 49 | static void multiconvert(char *arg, void *result, converter convert) |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 50 | { |
| 51 | char s[16]; |
| 52 | if (*arg == '"' || *arg == '\'') { |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 53 | sprintf(s, "%d", (unsigned)arg[1]); |
| 54 | arg = s; |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 55 | } |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 56 | if (convert(arg, result)) |
| 57 | fputs(arg, stderr); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 58 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 59 | |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 60 | static unsigned long xstrtoul(char *arg) |
| 61 | { |
| 62 | unsigned long result; |
| 63 | |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 64 | multiconvert(arg, &result, (converter)safe_strtoul); |
Rob Landley | 251161f | 2006-01-06 20:28:05 +0000 | [diff] [blame] | 65 | return result; |
| 66 | } |
| 67 | |
| 68 | static long xstrtol(char *arg) |
| 69 | { |
| 70 | long result; |
| 71 | multiconvert(arg, &result, (converter)safe_strtol); |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | static double xstrtod(char *arg) |
| 76 | { |
| 77 | double result; |
| 78 | multiconvert(arg, &result, (converter)safe_strtod); |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | static void print_esc_string(char *str) |
| 83 | { |
| 84 | for (; *str; str++) { |
| 85 | if (*str == '\\') { |
| 86 | str++; |
| 87 | putchar(bb_process_escape_sequence((const char **)&str)); |
| 88 | } else { |
| 89 | putchar(*str); |
| 90 | } |
| 91 | |
| 92 | } |
| 93 | } |
| 94 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 95 | int printf_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 96 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 97 | char *format; |
| 98 | int args_used; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 99 | |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 100 | if (argc <= 1 || argv[1][0] == '-') { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 102 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 103 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | format = argv[1]; |
| 105 | argc -= 2; |
| 106 | argv += 2; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 107 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 108 | do { |
| 109 | args_used = print_formatted(format, argc, argv); |
| 110 | argc -= args_used; |
| 111 | argv += args_used; |
| 112 | } |
| 113 | while (args_used > 0 && argc > 0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 114 | |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 115 | /* if (argc > 0) |
| 116 | fprintf(stderr, "excess args ignored"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 117 | */ |
| 118 | |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 119 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* Print the text in FORMAT, using ARGV (with ARGC elements) for |
| 123 | arguments to any `%' directives. |
| 124 | Return the number of elements of ARGV used. */ |
| 125 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 126 | static int print_formatted(char *format, int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 127 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 128 | int save_argc = argc; /* Preserve original value. */ |
| 129 | char *f; /* Pointer into `format'. */ |
| 130 | char *direc_start; /* Start of % directive. */ |
| 131 | size_t direc_length; /* Length of % directive. */ |
| 132 | int field_width; /* Arg to first '*', or -1 if none. */ |
| 133 | int precision; /* Arg to second '*', or -1 if none. */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 134 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 135 | for (f = format; *f; ++f) { |
| 136 | switch (*f) { |
| 137 | case '%': |
| 138 | direc_start = f++; |
| 139 | direc_length = 1; |
| 140 | field_width = precision = -1; |
| 141 | if (*f == '%') { |
| 142 | putchar('%'); |
| 143 | break; |
| 144 | } |
| 145 | if (*f == 'b') { |
| 146 | if (argc > 0) { |
| 147 | print_esc_string(*argv); |
| 148 | ++argv; |
| 149 | --argc; |
| 150 | } |
| 151 | break; |
| 152 | } |
| 153 | if (strchr("-+ #", *f)) { |
| 154 | ++f; |
| 155 | ++direc_length; |
| 156 | } |
| 157 | if (*f == '*') { |
| 158 | ++f; |
| 159 | ++direc_length; |
| 160 | if (argc > 0) { |
| 161 | field_width = xstrtoul(*argv); |
| 162 | ++argv; |
| 163 | --argc; |
| 164 | } else |
| 165 | field_width = 0; |
| 166 | } else |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 167 | while (isdigit(*f)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 168 | ++f; |
| 169 | ++direc_length; |
| 170 | } |
| 171 | if (*f == '.') { |
| 172 | ++f; |
| 173 | ++direc_length; |
| 174 | if (*f == '*') { |
| 175 | ++f; |
| 176 | ++direc_length; |
| 177 | if (argc > 0) { |
| 178 | precision = xstrtoul(*argv); |
| 179 | ++argv; |
| 180 | --argc; |
| 181 | } else |
| 182 | precision = 0; |
| 183 | } else |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 184 | while (isdigit(*f)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 185 | ++f; |
| 186 | ++direc_length; |
| 187 | } |
| 188 | } |
| 189 | if (*f == 'l' || *f == 'L' || *f == 'h') { |
| 190 | ++f; |
| 191 | ++direc_length; |
| 192 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 193 | /* |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 194 | if (!strchr ("diouxXfeEgGcs", *f)) |
| 195 | fprintf(stderr, "%%%c: invalid directive", *f); |
| 196 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 197 | ++direc_length; |
| 198 | if (argc > 0) { |
| 199 | print_direc(direc_start, direc_length, field_width, |
| 200 | precision, *argv); |
| 201 | ++argv; |
| 202 | --argc; |
| 203 | } else |
| 204 | print_direc(direc_start, direc_length, field_width, |
| 205 | precision, ""); |
| 206 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 207 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 208 | case '\\': |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 209 | if (*++f == 'c') |
| 210 | exit(0); |
| 211 | putchar(bb_process_escape_sequence((const char **)&f)); |
| 212 | f--; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 213 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 214 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 215 | default: |
| 216 | putchar(*f); |
| 217 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 218 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 219 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 220 | return save_argc - argc; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 223 | static void |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 224 | print_direc(char *start, size_t length, int field_width, int precision, |
| 225 | char *argument) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 226 | { |
Denis Vlasenko | c290563 | 2006-09-23 16:01:09 +0000 | [diff] [blame] | 227 | char *p; /* Null-terminated copy of % directive. */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 228 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 229 | p = xmalloc((unsigned) (length + 1)); |
| 230 | strncpy(p, start, length); |
| 231 | p[length] = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 232 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 233 | switch (p[length - 1]) { |
| 234 | case 'd': |
| 235 | case 'i': |
| 236 | if (field_width < 0) { |
| 237 | if (precision < 0) |
| 238 | printf(p, xstrtol(argument)); |
| 239 | else |
| 240 | printf(p, precision, xstrtol(argument)); |
| 241 | } else { |
| 242 | if (precision < 0) |
| 243 | printf(p, field_width, xstrtol(argument)); |
| 244 | else |
| 245 | printf(p, field_width, precision, xstrtol(argument)); |
| 246 | } |
| 247 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 248 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 249 | case 'o': |
| 250 | case 'u': |
| 251 | case 'x': |
| 252 | case 'X': |
| 253 | if (field_width < 0) { |
| 254 | if (precision < 0) |
| 255 | printf(p, xstrtoul(argument)); |
| 256 | else |
| 257 | printf(p, precision, xstrtoul(argument)); |
| 258 | } else { |
| 259 | if (precision < 0) |
| 260 | printf(p, field_width, xstrtoul(argument)); |
| 261 | else |
| 262 | printf(p, field_width, precision, xstrtoul(argument)); |
| 263 | } |
| 264 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 265 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 266 | case 'f': |
| 267 | case 'e': |
| 268 | case 'E': |
| 269 | case 'g': |
| 270 | case 'G': |
| 271 | if (field_width < 0) { |
| 272 | if (precision < 0) |
| 273 | printf(p, xstrtod(argument)); |
| 274 | else |
| 275 | printf(p, precision, xstrtod(argument)); |
| 276 | } else { |
| 277 | if (precision < 0) |
| 278 | printf(p, field_width, xstrtod(argument)); |
| 279 | else |
| 280 | printf(p, field_width, precision, xstrtod(argument)); |
| 281 | } |
| 282 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 283 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 284 | case 'c': |
| 285 | printf(p, *argument); |
| 286 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 287 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 288 | case 's': |
| 289 | if (field_width < 0) { |
| 290 | if (precision < 0) |
| 291 | printf(p, argument); |
| 292 | else |
| 293 | printf(p, precision, argument); |
| 294 | } else { |
| 295 | if (precision < 0) |
| 296 | printf(p, field_width, argument); |
| 297 | else |
| 298 | printf(p, field_width, precision, argument); |
| 299 | } |
| 300 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 301 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 302 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 303 | free(p); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 304 | } |