1 /* $NetBSD: printf.c,v 1.33 2008/07/21 14:19:24 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if !defined(BUILTIN) && !defined(SHELL) 35 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\ 36 The Regents of the University of California. All rights reserved."); 37 #endif 38 #endif 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)printf.c 8.2 (Berkeley) 3/22/95"; 43 #else 44 __RCSID("$NetBSD: printf.c,v 1.33 2008/07/21 14:19:24 lukem Exp $"); 45 #endif 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 50 #include <ctype.h> 51 #include <err.h> 52 #include <errno.h> 53 #include <inttypes.h> 54 #include <limits.h> 55 #include <locale.h> 56 #include <stdarg.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 #ifdef __GNUC__ 63 #define ESCAPE '\e' 64 #else 65 #define ESCAPE 033 66 #endif 67 68 static void conv_escape_str(char *, void (*)(int)); 69 static char *conv_escape(char *, char *); 70 static char *conv_expand(const char *); 71 static int getchr(void); 72 static double getdouble(void); 73 static int getwidth(void); 74 static intmax_t getintmax(void); 75 static uintmax_t getuintmax(void); 76 static char *getstr(void); 77 static char *mklong(const char *, int); 78 static void check_conversion(const char *, const char *); 79 static void usage(void); 80 81 static void b_count(int); 82 static void b_output(int); 83 static size_t b_length; 84 static char *b_fmt; 85 86 static int rval; 87 static char **gargv; 88 89 #ifdef BUILTIN /* csh builtin */ 90 #define main progprintf 91 #endif 92 93 #ifdef SHELL /* sh (aka ash) builtin */ 94 #define main printfcmd 95 #include "../../bin/sh/bltin/bltin.h" 96 #endif /* SHELL */ 97 98 #define PF(f, func) { \ 99 if (fieldwidth != -1) { \ 100 if (precision != -1) \ 101 error = printf(f, fieldwidth, precision, func); \ 102 else \ 103 error = printf(f, fieldwidth, func); \ 104 } else if (precision != -1) \ 105 error = printf(f, precision, func); \ 106 else \ 107 error = printf(f, func); \ 108 } 109 110 #define APF(cpp, f, func) { \ 111 if (fieldwidth != -1) { \ 112 if (precision != -1) \ 113 error = asprintf(cpp, f, fieldwidth, precision, func); \ 114 else \ 115 error = asprintf(cpp, f, fieldwidth, func); \ 116 } else if (precision != -1) \ 117 error = asprintf(cpp, f, precision, func); \ 118 else \ 119 error = asprintf(cpp, f, func); \ 120 } 121 122 #ifdef main 123 int main(int, char *[]); 124 #endif 125 int main(int argc, char *argv[]) 126 { 127 char *fmt, *start; 128 int fieldwidth, precision; 129 char nextch; 130 char *format; 131 int ch; 132 int error; 133 134 #if !defined(SHELL) && !defined(BUILTIN) 135 (void)setlocale (LC_ALL, ""); 136 #endif 137 138 while ((ch = getopt(argc, argv, "")) != -1) { 139 switch (ch) { 140 case '?': 141 default: 142 usage(); 143 return 1; 144 } 145 } 146 argc -= optind; 147 argv += optind; 148 149 if (argc < 1) { 150 usage(); 151 return 1; 152 } 153 154 format = *argv; 155 gargv = ++argv; 156 157 #define SKIP1 "#-+ 0" 158 #define SKIP2 "*0123456789" 159 do { 160 /* 161 * Basic algorithm is to scan the format string for conversion 162 * specifications -- once one is found, find out if the field 163 * width or precision is a '*'; if it is, gather up value. 164 * Note, format strings are reused as necessary to use up the 165 * provided arguments, arguments of zero/null string are 166 * provided to use up the format string. 167 */ 168 169 /* find next format specification */ 170 for (fmt = format; (ch = *fmt++) != '\0';) { 171 if (ch == '\\') { 172 char c_ch; 173 fmt = conv_escape(fmt, &c_ch); 174 putchar(c_ch); 175 continue; 176 } 177 if (ch != '%' || (*fmt == '%' && ++fmt)) { 178 (void)putchar(ch); 179 continue; 180 } 181 182 /* Ok - we've found a format specification, 183 Save its address for a later printf(). */ 184 start = fmt - 1; 185 186 /* skip to field width */ 187 fmt += strspn(fmt, SKIP1); 188 fieldwidth = *fmt == '*' ? getwidth() : -1; 189 190 /* skip to possible '.', get following precision */ 191 fmt += strspn(fmt, SKIP2); 192 if (*fmt == '.') 193 ++fmt; 194 precision = *fmt == '*' ? getwidth() : -1; 195 196 fmt += strspn(fmt, SKIP2); 197 198 ch = *fmt; 199 if (!ch) { 200 warnx("missing format character"); 201 return (1); 202 } 203 /* null terminate format string to we can use it 204 as an argument to printf. */ 205 nextch = fmt[1]; 206 fmt[1] = 0; 207 switch (ch) { 208 209 case 'B': { 210 const char *p = conv_expand(getstr()); 211 if (p == NULL) 212 goto out; 213 *fmt = 's'; 214 PF(start, p); 215 if (error < 0) 216 goto out; 217 break; 218 } 219 case 'b': { 220 /* There has to be a better way to do this, 221 * but the string we generate might have 222 * embedded nulls. */ 223 static char *a, *t; 224 char *cp = getstr(); 225 /* Free on entry in case shell longjumped out */ 226 if (a != NULL) 227 free(a); 228 a = NULL; 229 if (t != NULL) 230 free(t); 231 t = NULL; 232 /* Count number of bytes we want to output */ 233 b_length = 0; 234 conv_escape_str(cp, b_count); 235 t = malloc(b_length + 1); 236 if (t == NULL) 237 goto out; 238 (void)memset(t, 'x', b_length); 239 t[b_length] = 0; 240 /* Get printf to calculate the lengths */ 241 *fmt = 's'; 242 APF(&a, start, t); 243 if (error == -1) 244 goto out; 245 b_fmt = a; 246 /* Output leading spaces and data bytes */ 247 conv_escape_str(cp, b_output); 248 /* Add any trailing spaces */ 249 printf("%s", b_fmt); 250 break; 251 } 252 case 'c': { 253 char p = getchr(); 254 PF(start, p); 255 if (error < 0) 256 goto out; 257 break; 258 } 259 case 's': { 260 char *p = getstr(); 261 PF(start, p); 262 if (error < 0) 263 goto out; 264 break; 265 } 266 case 'd': 267 case 'i': { 268 intmax_t p = getintmax(); 269 char *f = mklong(start, ch); 270 PF(f, p); 271 if (error < 0) 272 goto out; 273 break; 274 } 275 case 'o': 276 case 'u': 277 case 'x': 278 case 'X': { 279 uintmax_t p = getuintmax(); 280 char *f = mklong(start, ch); 281 PF(f, p); 282 if (error < 0) 283 goto out; 284 break; 285 } 286 case 'e': 287 case 'E': 288 case 'f': 289 case 'g': 290 case 'G': { 291 double p = getdouble(); 292 PF(start, p); 293 if (error < 0) 294 goto out; 295 break; 296 } 297 default: 298 warnx("%s: invalid directive", start); 299 return 1; 300 } 301 *fmt++ = ch; 302 *fmt = nextch; 303 /* escape if a \c was encountered */ 304 if (rval & 0x100) 305 return rval & ~0x100; 306 } 307 } while (gargv != argv && *gargv); 308 309 return rval & ~0x100; 310 out: 311 warn("print failed"); 312 return 1; 313 } 314 315 /* helper functions for conv_escape_str */ 316 317 static void 318 /*ARGSUSED*/ 319 b_count(int ch) 320 { 321 b_length++; 322 } 323 324 /* Output one converted character for every 'x' in the 'format' */ 325 326 static void 327 b_output(int ch) 328 { 329 for (;;) { 330 switch (*b_fmt++) { 331 case 0: 332 b_fmt--; 333 return; 334 case ' ': 335 putchar(' '); 336 break; 337 default: 338 putchar(ch); 339 return; 340 } 341 } 342 } 343 344 345 /* 346 * Print SysV echo(1) style escape string 347 * Halts processing string if a \c escape is encountered. 348 */ 349 static void 350 conv_escape_str(char *str, void (*do_putchar)(int)) 351 { 352 int value; 353 int ch; 354 char c; 355 356 while ((ch = *str++) != '\0') { 357 if (ch != '\\') { 358 do_putchar(ch); 359 continue; 360 } 361 362 ch = *str++; 363 if (ch == 'c') { 364 /* \c as in SYSV echo - abort all processing.... */ 365 rval |= 0x100; 366 break; 367 } 368 369 /* 370 * %b string octal constants are not like those in C. 371 * They start with a \0, and are followed by 0, 1, 2, 372 * or 3 octal digits. 373 */ 374 if (ch == '0') { 375 int octnum = 0, i; 376 for (i = 0; i < 3; i++) { 377 if (!isdigit((unsigned char)*str) || *str > '7') 378 break; 379 octnum = (octnum << 3) | (*str++ - '0'); 380 } 381 do_putchar(octnum); 382 continue; 383 } 384 385 /* \[M][^|-]C as defined by vis(3) */ 386 if (ch == 'M' && *str == '-') { 387 do_putchar(0200 | str[1]); 388 str += 2; 389 continue; 390 } 391 if (ch == 'M' && *str == '^') { 392 str++; 393 value = 0200; 394 ch = '^'; 395 } else 396 value = 0; 397 if (ch == '^') { 398 ch = *str++; 399 if (ch == '?') 400 value |= 0177; 401 else 402 value |= ch & 037; 403 do_putchar(value); 404 continue; 405 } 406 407 /* Finally test for sequences valid in the format string */ 408 str = conv_escape(str - 1, &c); 409 do_putchar(c); 410 } 411 } 412 413 /* 414 * Print "standard" escape characters 415 */ 416 static char * 417 conv_escape(char *str, char *conv_ch) 418 { 419 int value; 420 int ch; 421 char num_buf[4], *num_end; 422 423 ch = *str++; 424 425 switch (ch) { 426 case '0': case '1': case '2': case '3': 427 case '4': case '5': case '6': case '7': 428 num_buf[0] = ch; 429 ch = str[0]; 430 num_buf[1] = ch; 431 num_buf[2] = ch ? str[1] : 0; 432 num_buf[3] = 0; 433 value = strtoul(num_buf, &num_end, 8); 434 str += num_end - (num_buf + 1); 435 break; 436 437 case 'x': 438 /* Hexadecimal character constants are not required to be 439 supported (by SuS v1) because there is no consistent 440 way to detect the end of the constant. 441 Supporting 2 byte constants is a compromise. */ 442 ch = str[0]; 443 num_buf[0] = ch; 444 num_buf[1] = ch ? str[1] : 0; 445 num_buf[2] = 0; 446 value = strtoul(num_buf, &num_end, 16); 447 str += num_end - num_buf; 448 break; 449 450 case '\\': value = '\\'; break; /* backslash */ 451 case '\'': value = '\''; break; /* single quote */ 452 case '"': value = '"'; break; /* double quote */ 453 case 'a': value = '\a'; break; /* alert */ 454 case 'b': value = '\b'; break; /* backspace */ 455 case 'e': value = ESCAPE; break; /* escape */ 456 case 'f': value = '\f'; break; /* form-feed */ 457 case 'n': value = '\n'; break; /* newline */ 458 case 'r': value = '\r'; break; /* carriage-return */ 459 case 't': value = '\t'; break; /* tab */ 460 case 'v': value = '\v'; break; /* vertical-tab */ 461 462 default: 463 warnx("unknown escape sequence `\\%c'", ch); 464 rval = 1; 465 value = ch; 466 break; 467 } 468 469 *conv_ch = value; 470 return str; 471 } 472 473 /* expand a string so that everything is printable */ 474 475 static char * 476 conv_expand(const char *str) 477 { 478 static char *conv_str; 479 char *cp; 480 int ch; 481 482 if (conv_str) 483 free(conv_str); 484 /* get a buffer that is definitely large enough.... */ 485 conv_str = malloc(4 * strlen(str) + 1); 486 if (!conv_str) 487 return NULL; 488 cp = conv_str; 489 490 while ((ch = *(const unsigned char *)str++) != '\0') { 491 switch (ch) { 492 /* Use C escapes for expected control characters */ 493 case '\\': ch = '\\'; break; /* backslash */ 494 case '\'': ch = '\''; break; /* single quote */ 495 case '"': ch = '"'; break; /* double quote */ 496 case '\a': ch = 'a'; break; /* alert */ 497 case '\b': ch = 'b'; break; /* backspace */ 498 case ESCAPE: ch = 'e'; break; /* escape */ 499 case '\f': ch = 'f'; break; /* form-feed */ 500 case '\n': ch = 'n'; break; /* newline */ 501 case '\r': ch = 'r'; break; /* carriage-return */ 502 case '\t': ch = 't'; break; /* tab */ 503 case '\v': ch = 'v'; break; /* vertical-tab */ 504 default: 505 /* Copy anything printable */ 506 if (isprint(ch)) { 507 *cp++ = ch; 508 continue; 509 } 510 /* Use vis(3) encodings for the rest */ 511 *cp++ = '\\'; 512 if (ch & 0200) { 513 *cp++ = 'M'; 514 ch &= ~0200; 515 } 516 if (ch == 0177) { 517 *cp++ = '^'; 518 *cp++ = '?'; 519 continue; 520 } 521 if (ch < 040) { 522 *cp++ = '^'; 523 *cp++ = ch | 0100; 524 continue; 525 } 526 *cp++ = '-'; 527 *cp++ = ch; 528 continue; 529 } 530 *cp++ = '\\'; 531 *cp++ = ch; 532 } 533 534 *cp = 0; 535 return conv_str; 536 } 537 538 static char * 539 mklong(const char *str, int ch) 540 { 541 static char copy[64]; 542 size_t len; 543 544 len = strlen(str) + 2; 545 if (len > sizeof copy) { 546 warnx("format %s too complex\n", str); 547 len = 4; 548 } 549 (void)memmove(copy, str, len - 3); 550 copy[len - 3] = 'j'; 551 copy[len - 2] = ch; 552 copy[len - 1] = '\0'; 553 return copy; 554 } 555 556 static int 557 getchr(void) 558 { 559 if (!*gargv) 560 return 0; 561 return (int)**gargv++; 562 } 563 564 static char * 565 getstr(void) 566 { 567 static char empty[] = ""; 568 if (!*gargv) 569 return empty; 570 return *gargv++; 571 } 572 573 static int 574 getwidth(void) 575 { 576 long val; 577 char *s, *ep; 578 579 s = *gargv; 580 if (!*gargv) 581 return (0); 582 gargv++; 583 584 errno = 0; 585 val = strtoul(s, &ep, 0); 586 check_conversion(s, ep); 587 588 /* Arbitrarily 'restrict' field widths to 1Mbyte */ 589 if (val < 0 || val > 1 << 20) { 590 warnx("%s: invalid field width", s); 591 return 0; 592 } 593 594 return val; 595 } 596 597 static intmax_t 598 getintmax(void) 599 { 600 intmax_t val; 601 char *cp, *ep; 602 603 cp = *gargv; 604 if (cp == NULL) 605 return 0; 606 gargv++; 607 608 if (*cp == '\"' || *cp == '\'') 609 return *(cp+1); 610 611 errno = 0; 612 val = strtoimax(cp, &ep, 0); 613 check_conversion(cp, ep); 614 return val; 615 } 616 617 static uintmax_t 618 getuintmax(void) 619 { 620 uintmax_t val; 621 char *cp, *ep; 622 623 cp = *gargv; 624 if (cp == NULL) 625 return 0; 626 gargv++; 627 628 if (*cp == '\"' || *cp == '\'') 629 return *(cp + 1); 630 631 /* strtoumax won't error -ve values */ 632 while (isspace(*(unsigned char *)cp)) 633 cp++; 634 if (*cp == '-') { 635 warnx("%s: expected positive numeric value", cp); 636 rval = 1; 637 return 0; 638 } 639 640 errno = 0; 641 val = strtoumax(cp, &ep, 0); 642 check_conversion(cp, ep); 643 return val; 644 } 645 646 static double 647 getdouble(void) 648 { 649 double val; 650 char *ep; 651 652 if (!*gargv) 653 return (0.0); 654 655 if (**gargv == '\"' || **gargv == '\'') 656 return (double) *((*gargv++)+1); 657 658 errno = 0; 659 val = strtod(*gargv, &ep); 660 check_conversion(*gargv++, ep); 661 return val; 662 } 663 664 static void 665 check_conversion(const char *s, const char *ep) 666 { 667 if (*ep) { 668 if (ep == s) 669 warnx("%s: expected numeric value", s); 670 else 671 warnx("%s: not completely converted", s); 672 rval = 1; 673 } else if (errno == ERANGE) { 674 warnx("%s: %s", s, strerror(ERANGE)); 675 rval = 1; 676 } 677 } 678 679 static void 680 usage(void) 681 { 682 (void)fprintf(stderr, "Usage: %s format [arg ...]\n", getprogname()); 683 } 684