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 |
| 3 | Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2, or (at your option) |
| 8 | any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software Foundation, |
| 17 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 18 | |
| 19 | /* Usage: printf format [argument...] |
| 20 | |
| 21 | A front end to the printf function that lets it be used from the shell. |
| 22 | |
| 23 | Backslash escapes: |
| 24 | |
| 25 | \" = double quote |
| 26 | \\ = backslash |
| 27 | \a = alert (bell) |
| 28 | \b = backspace |
| 29 | \c = produce no further output |
| 30 | \f = form feed |
| 31 | \n = new line |
| 32 | \r = carriage return |
| 33 | \t = horizontal tab |
| 34 | \v = vertical tab |
| 35 | \0ooo = octal number (ooo is 0 to 3 digits) |
| 36 | \xhhh = hexadecimal number (hhh is 1 to 3 digits) |
| 37 | |
| 38 | Additional directive: |
| 39 | |
| 40 | %b = print an argument string, interpreting backslash escapes |
| 41 | |
| 42 | The `format' argument is re-used as many times as necessary |
| 43 | to convert all of the given arguments. |
| 44 | |
| 45 | David MacKenzie <djm@gnu.ai.mit.edu> */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 46 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 47 | |
| 48 | // 19990508 Busy Boxed! Dave Cinege |
| 49 | |
| 50 | #include <unistd.h> |
| 51 | #include <stdio.h> |
| 52 | #include <sys/types.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | #include <string.h> |
| 54 | #include <errno.h> |
| 55 | #include <stdlib.h> |
| 56 | #include <fcntl.h> |
| 57 | #include <ctype.h> |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 58 | #include <assert.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 59 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 60 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | static double xstrtod __P((char *s)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | static long xstrtol __P((char *s)); |
| 63 | static unsigned long xstrtoul __P((char *s)); |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 64 | static void print_esc_string __P((char *str)); |
| 65 | static int print_formatted __P((char *format, int argc, char **argv)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 66 | static void print_direc __P( (char *start, size_t length, |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 67 | int field_width, int precision, char *argument)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 68 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | int printf_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | char *format; |
| 72 | int args_used; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 73 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | if (argc <= 1 || **(argv + 1) == '-') { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 77 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 78 | format = argv[1]; |
| 79 | argc -= 2; |
| 80 | argv += 2; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 81 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 82 | do { |
| 83 | args_used = print_formatted(format, argc, argv); |
| 84 | argc -= args_used; |
| 85 | argv += args_used; |
| 86 | } |
| 87 | while (args_used > 0 && argc > 0); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 88 | |
| 89 | /* |
| 90 | if (argc > 0) |
| 91 | fprintf(stderr, "excess args ignored"); |
| 92 | */ |
| 93 | |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 94 | return EXIT_SUCCESS; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* Print the text in FORMAT, using ARGV (with ARGC elements) for |
| 98 | arguments to any `%' directives. |
| 99 | Return the number of elements of ARGV used. */ |
| 100 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 101 | static int print_formatted(char *format, int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 102 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | int save_argc = argc; /* Preserve original value. */ |
| 104 | char *f; /* Pointer into `format'. */ |
| 105 | char *direc_start; /* Start of % directive. */ |
| 106 | size_t direc_length; /* Length of % directive. */ |
| 107 | int field_width; /* Arg to first '*', or -1 if none. */ |
| 108 | int precision; /* Arg to second '*', or -1 if none. */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 109 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | for (f = format; *f; ++f) { |
| 111 | switch (*f) { |
| 112 | case '%': |
| 113 | direc_start = f++; |
| 114 | direc_length = 1; |
| 115 | field_width = precision = -1; |
| 116 | if (*f == '%') { |
| 117 | putchar('%'); |
| 118 | break; |
| 119 | } |
| 120 | if (*f == 'b') { |
| 121 | if (argc > 0) { |
| 122 | print_esc_string(*argv); |
| 123 | ++argv; |
| 124 | --argc; |
| 125 | } |
| 126 | break; |
| 127 | } |
| 128 | if (strchr("-+ #", *f)) { |
| 129 | ++f; |
| 130 | ++direc_length; |
| 131 | } |
| 132 | if (*f == '*') { |
| 133 | ++f; |
| 134 | ++direc_length; |
| 135 | if (argc > 0) { |
| 136 | field_width = xstrtoul(*argv); |
| 137 | ++argv; |
| 138 | --argc; |
| 139 | } else |
| 140 | field_width = 0; |
| 141 | } else |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 142 | while (isdigit(*f)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 143 | ++f; |
| 144 | ++direc_length; |
| 145 | } |
| 146 | if (*f == '.') { |
| 147 | ++f; |
| 148 | ++direc_length; |
| 149 | if (*f == '*') { |
| 150 | ++f; |
| 151 | ++direc_length; |
| 152 | if (argc > 0) { |
| 153 | precision = xstrtoul(*argv); |
| 154 | ++argv; |
| 155 | --argc; |
| 156 | } else |
| 157 | precision = 0; |
| 158 | } else |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 159 | while (isdigit(*f)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 160 | ++f; |
| 161 | ++direc_length; |
| 162 | } |
| 163 | } |
| 164 | if (*f == 'l' || *f == 'L' || *f == 'h') { |
| 165 | ++f; |
| 166 | ++direc_length; |
| 167 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 168 | /* |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | if (!strchr ("diouxXfeEgGcs", *f)) |
| 170 | fprintf(stderr, "%%%c: invalid directive", *f); |
| 171 | */ |
| 172 | ++direc_length; |
| 173 | if (argc > 0) { |
| 174 | print_direc(direc_start, direc_length, field_width, |
| 175 | precision, *argv); |
| 176 | ++argv; |
| 177 | --argc; |
| 178 | } else |
| 179 | print_direc(direc_start, direc_length, field_width, |
| 180 | precision, ""); |
| 181 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 182 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 183 | case '\\': |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 184 | if (*++f == 'c') |
| 185 | exit(0); |
| 186 | putchar(bb_process_escape_sequence((const char **)&f)); |
| 187 | f--; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 188 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 189 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 190 | default: |
| 191 | putchar(*f); |
| 192 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 193 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 194 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 195 | return save_argc - argc; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 198 | static void |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 199 | print_direc(char *start, size_t length, int field_width, int precision, |
| 200 | char *argument) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 201 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 202 | char *p; /* Null-terminated copy of % directive. */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 203 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 204 | p = xmalloc((unsigned) (length + 1)); |
| 205 | strncpy(p, start, length); |
| 206 | p[length] = 0; |
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 | switch (p[length - 1]) { |
| 209 | case 'd': |
| 210 | case 'i': |
| 211 | if (field_width < 0) { |
| 212 | if (precision < 0) |
| 213 | printf(p, xstrtol(argument)); |
| 214 | else |
| 215 | printf(p, precision, xstrtol(argument)); |
| 216 | } else { |
| 217 | if (precision < 0) |
| 218 | printf(p, field_width, xstrtol(argument)); |
| 219 | else |
| 220 | printf(p, field_width, precision, xstrtol(argument)); |
| 221 | } |
| 222 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 223 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 224 | case 'o': |
| 225 | case 'u': |
| 226 | case 'x': |
| 227 | case 'X': |
| 228 | if (field_width < 0) { |
| 229 | if (precision < 0) |
| 230 | printf(p, xstrtoul(argument)); |
| 231 | else |
| 232 | printf(p, precision, xstrtoul(argument)); |
| 233 | } else { |
| 234 | if (precision < 0) |
| 235 | printf(p, field_width, xstrtoul(argument)); |
| 236 | else |
| 237 | printf(p, field_width, precision, xstrtoul(argument)); |
| 238 | } |
| 239 | break; |
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 | case 'f': |
| 242 | case 'e': |
| 243 | case 'E': |
| 244 | case 'g': |
| 245 | case 'G': |
| 246 | if (field_width < 0) { |
| 247 | if (precision < 0) |
| 248 | printf(p, xstrtod(argument)); |
| 249 | else |
| 250 | printf(p, precision, xstrtod(argument)); |
| 251 | } else { |
| 252 | if (precision < 0) |
| 253 | printf(p, field_width, xstrtod(argument)); |
| 254 | else |
| 255 | printf(p, field_width, precision, xstrtod(argument)); |
| 256 | } |
| 257 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 258 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 259 | case 'c': |
| 260 | printf(p, *argument); |
| 261 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 262 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 263 | case 's': |
| 264 | if (field_width < 0) { |
| 265 | if (precision < 0) |
| 266 | printf(p, argument); |
| 267 | else |
| 268 | printf(p, precision, argument); |
| 269 | } else { |
| 270 | if (precision < 0) |
| 271 | printf(p, field_width, argument); |
| 272 | else |
| 273 | printf(p, field_width, precision, argument); |
| 274 | } |
| 275 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 276 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 277 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 278 | free(p); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 281 | static unsigned long xstrtoul(char *arg) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 282 | { |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 283 | unsigned long result; |
Eric Andersen | 2479445 | 2004-03-06 22:11:45 +0000 | [diff] [blame] | 284 | if (safe_strtoul(arg, &result)) |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 285 | fprintf(stderr, "%s", arg); |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 286 | return result; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 289 | static long xstrtol(char *arg) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 290 | { |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 291 | long result; |
Eric Andersen | 2479445 | 2004-03-06 22:11:45 +0000 | [diff] [blame] | 292 | if (safe_strtol(arg, &result)) |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 293 | fprintf(stderr, "%s", arg); |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 294 | return result; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 297 | static double xstrtod(char *arg) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 298 | { |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 299 | double result; |
Eric Andersen | 2479445 | 2004-03-06 22:11:45 +0000 | [diff] [blame] | 300 | if (safe_strtod(arg, &result)) |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 301 | fprintf(stderr, "%s", arg); |
Eric Andersen | a2d1982 | 2003-05-26 18:09:14 +0000 | [diff] [blame] | 302 | return result; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Glenn L McGrath | 240a91d | 2004-09-15 02:05:23 +0000 | [diff] [blame] | 305 | static void print_esc_string(char *str) |
| 306 | { |
| 307 | for (; *str; str++) { |
| 308 | if (*str == '\\') { |
| 309 | str++; |
| 310 | putchar(bb_process_escape_sequence((const char **)&str)); |
| 311 | } else { |
| 312 | putchar(*str); |
| 313 | } |
| 314 | |
| 315 | } |
| 316 | } |