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