Home | History | Annotate | Line # | Download | only in printf
printf.c revision 1.45
      1 /*	$NetBSD: printf.c,v 1.45 2018/09/04 01:13:50 kre 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.45 2018/09/04 01:13:50 kre 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), int);
     69 static char	*conv_escape(char *, char *, int);
     70 static char	*conv_expand(const char *);
     71 static char	 getchr(void);
     72 static double	 getdouble(void);
     73 static int	 getwidth(void);
     74 static intmax_t	 getintmax(void);
     75 static char	*getstr(void);
     76 static char	*mklong(const char *, char);
     77 static void      check_conversion(const char *, const char *);
     78 static void	 usage(void);
     79 
     80 static void	b_count(int);
     81 static void	b_output(int);
     82 static size_t	b_length;
     83 static char	*b_fmt;
     84 
     85 static int	rval;
     86 static char  **gargv;
     87 
     88 #ifdef BUILTIN		/* csh builtin */
     89 #define main progprintf
     90 #endif
     91 
     92 #ifdef SHELL		/* sh (aka ash) builtin */
     93 #define main printfcmd
     94 #include "../../bin/sh/bltin/bltin.h"
     95 #endif /* SHELL */
     96 
     97 #define PF(f, func) { \
     98 	if (fieldwidth != -1) { \
     99 		if (precision != -1) \
    100 			error = printf(f, fieldwidth, precision, func); \
    101 		else \
    102 			error = printf(f, fieldwidth, func); \
    103 	} else if (precision != -1) \
    104 		error = printf(f, precision, func); \
    105 	else \
    106 		error = printf(f, func); \
    107 }
    108 
    109 #define APF(cpp, f, func) { \
    110 	if (fieldwidth != -1) { \
    111 		if (precision != -1) \
    112 			error = asprintf(cpp, f, fieldwidth, precision, func); \
    113 		else \
    114 			error = asprintf(cpp, f, fieldwidth, func); \
    115 	} else if (precision != -1) \
    116 		error = asprintf(cpp, f, precision, func); \
    117 	else \
    118 		error = asprintf(cpp, f, func); \
    119 }
    120 
    121 #ifdef main
    122 int main(int, char *[]);
    123 #endif
    124 
    125 int
    126 main(int argc, char *argv[])
    127 {
    128 	char *fmt, *start;
    129 	int fieldwidth, precision;
    130 	char nextch;
    131 	char *format;
    132 	char ch;
    133 	int error, o;
    134 
    135 #if !defined(SHELL) && !defined(BUILTIN)
    136 	(void)setlocale (LC_ALL, "");
    137 #endif
    138 
    139 	while ((o = getopt(argc, argv, "")) != -1) {
    140 		switch (o) {
    141 		case '?':
    142 		default:
    143 			usage();
    144 			return 1;
    145 		}
    146 	}
    147 	argc -= optind;
    148 	argv += optind;
    149 
    150 	if (argc < 1) {
    151 		usage();
    152 		return 1;
    153 	}
    154 
    155 	format = *argv;
    156 	gargv = ++argv;
    157 
    158 #define SKIP1	"#-+ 0'"
    159 #define SKIP2	"0123456789"
    160 	do {
    161 		/*
    162 		 * Basic algorithm is to scan the format string for conversion
    163 		 * specifications -- once one is found, find out if the field
    164 		 * width or precision is a '*'; if it is, gather up value.
    165 		 * Note, format strings are reused as necessary to use up the
    166 		 * provided arguments, arguments of zero/null string are
    167 		 * provided to use up the format string.
    168 		 */
    169 
    170 		/* find next format specification */
    171 		for (fmt = format; (ch = *fmt++) != '\0';) {
    172 			if (ch == '\\') {
    173 				char c_ch;
    174 				fmt = conv_escape(fmt, &c_ch, 0);
    175 				putchar(c_ch);
    176 				continue;
    177 			}
    178 			if (ch != '%' || (*fmt == '%' && ++fmt)) {
    179 				(void)putchar(ch);
    180 				continue;
    181 			}
    182 
    183 			/*
    184 			 * Ok - we've found a format specification,
    185 			 * Save its address for a later printf().
    186 			 */
    187 			start = fmt - 1;
    188 
    189 			/* skip to field width */
    190 			fmt += strspn(fmt, SKIP1);
    191 			if (*fmt == '*') {
    192 				fmt++;
    193 				fieldwidth = getwidth();
    194 			} else {
    195 				fieldwidth = -1;
    196 
    197 				/* skip to possible '.' for precision */
    198 				fmt += strspn(fmt, SKIP2);
    199 			}
    200 
    201 			if (*fmt == '.') {
    202 				 /* get following precision */
    203 				fmt++;
    204 				if (*fmt == '*') {
    205 					fmt++;
    206 					precision = getwidth();
    207 				} else {
    208 					precision = -1;
    209 					fmt += strspn(fmt, SKIP2);
    210 				}
    211 			} else
    212 				precision = -1;
    213 
    214 			ch = *fmt;
    215 			if (!ch) {
    216 				warnx("%s: missing format character", start);
    217 				return 1;
    218 			}
    219 
    220 			/*
    221 			 * null terminate format string to we can use it
    222 			 * as an argument to printf.
    223 			 */
    224 			nextch = fmt[1];
    225 			fmt[1] = 0;
    226 
    227 			switch (ch) {
    228 
    229 			case 'B': {
    230 				const char *p = conv_expand(getstr());
    231 
    232 				if (p == NULL)
    233 					goto out;
    234 				*fmt = 's';
    235 				PF(start, p);
    236 				if (error < 0)
    237 					goto out;
    238 				break;
    239 			}
    240 			case 'b': {
    241 				/*
    242 				 * There has to be a better way to do this,
    243 				 * but the string we generate might have
    244 				 * embedded nulls
    245 				 */
    246 				static char *a, *t;
    247 				char *cp = getstr();
    248 
    249 				/* Free on entry in case shell longjumped out */
    250 				if (a != NULL)
    251 					free(a);
    252 				a = NULL;
    253 				if (t != NULL)
    254 					free(t);
    255 				t = NULL;
    256 
    257 				/* Count number of bytes we want to output */
    258 				b_length = 0;
    259 				conv_escape_str(cp, b_count, 0);
    260 				t = malloc(b_length + 1);
    261 				if (t == NULL)
    262 					goto out;
    263 				(void)memset(t, 'x', b_length);
    264 				t[b_length] = 0;
    265 
    266 				/* Get printf to calculate the lengths */
    267 				*fmt = 's';
    268 				APF(&a, start, t);
    269 				if (error == -1)
    270 					goto out;
    271 				b_fmt = a;
    272 
    273 				/* Output leading spaces and data bytes */
    274 				conv_escape_str(cp, b_output, 1);
    275 
    276 				/* Add any trailing spaces */
    277 				printf("%s", b_fmt);
    278 				break;
    279 			}
    280 			case 'c': {
    281 				char p = getchr();
    282 
    283 				PF(start, p);
    284 				if (error < 0)
    285 					goto out;
    286 				break;
    287 			}
    288 			case 's': {
    289 				char *p = getstr();
    290 
    291 				PF(start, p);
    292 				if (error < 0)
    293 					goto out;
    294 				break;
    295 			}
    296 			case 'd':
    297 			case 'i': {
    298 				intmax_t p = getintmax();
    299 				char *f = mklong(start, ch);
    300 
    301 				PF(f, p);
    302 				if (error < 0)
    303 					goto out;
    304 				break;
    305 			}
    306 			case 'o':
    307 			case 'u':
    308 			case 'x':
    309 			case 'X': {
    310 				uintmax_t p = (uintmax_t)getintmax();
    311 				char *f = mklong(start, ch);
    312 
    313 				PF(f, p);
    314 				if (error < 0)
    315 					goto out;
    316 				break;
    317 			}
    318 			case 'a':
    319 			case 'A':
    320 			case 'e':
    321 			case 'E':
    322 			case 'f':
    323 			case 'F':
    324 			case 'g':
    325 			case 'G': {
    326 				double p = getdouble();
    327 
    328 				PF(start, p);
    329 				if (error < 0)
    330 					goto out;
    331 				break;
    332 			}
    333 			case '%':
    334 				/* Don't ask, but this is useful ... */
    335 				if (fieldwidth == 'N' && precision == 'B')
    336 					return 0;
    337 				/* FALLTHROUGH */
    338 			default:
    339 				warnx("%s: invalid directive", start);
    340 				return 1;
    341 			}
    342 			*fmt++ = ch;
    343 			*fmt = nextch;
    344 			/* escape if a \c was encountered */
    345 			if (rval & 0x100)
    346 				return rval & ~0x100;
    347 		}
    348 	} while (gargv != argv && *gargv);
    349 
    350 	return rval & ~0x100;
    351   out:
    352 	warn("print failed");
    353 	return 1;
    354 }
    355 
    356 /* helper functions for conv_escape_str */
    357 
    358 static void
    359 /*ARGSUSED*/
    360 b_count(int ch)
    361 {
    362 	b_length++;
    363 }
    364 
    365 /* Output one converted character for every 'x' in the 'format' */
    366 
    367 static void
    368 b_output(int ch)
    369 {
    370 	for (;;) {
    371 		switch (*b_fmt++) {
    372 		case 0:
    373 			b_fmt--;
    374 			return;
    375 		case ' ':
    376 			putchar(' ');
    377 			break;
    378 		default:
    379 			putchar(ch);
    380 			return;
    381 		}
    382 	}
    383 }
    384 
    385 
    386 /*
    387  * Print SysV echo(1) style escape string
    388  *	Halts processing string if a \c escape is encountered.
    389  */
    390 static void
    391 conv_escape_str(char *str, void (*do_putchar)(int), int quiet)
    392 {
    393 	int value;
    394 	int ch;
    395 	char c;
    396 
    397 	while ((ch = *str++) != '\0') {
    398 		if (ch != '\\') {
    399 			do_putchar(ch);
    400 			continue;
    401 		}
    402 
    403 		ch = *str++;
    404 		if (ch == 'c') {
    405 			/* \c as in SYSV echo - abort all processing.... */
    406 			rval |= 0x100;
    407 			break;
    408 		}
    409 
    410 		/*
    411 		 * %b string octal constants are not like those in C.
    412 		 * They start with a \0, and are followed by 0, 1, 2,
    413 		 * or 3 octal digits.
    414 		 */
    415 		if (ch == '0') {
    416 			int octnum = 0, i;
    417 			for (i = 0; i < 3; i++) {
    418 				if (!isdigit((unsigned char)*str) || *str > '7')
    419 					break;
    420 				octnum = (octnum << 3) | (*str++ - '0');
    421 			}
    422 			do_putchar(octnum);
    423 			continue;
    424 		}
    425 
    426 		/* \[M][^|-]C as defined by vis(3) */
    427 		if (ch == 'M' && *str == '-') {
    428 			do_putchar(0200 | str[1]);
    429 			str += 2;
    430 			continue;
    431 		}
    432 		if (ch == 'M' && *str == '^') {
    433 			str++;
    434 			value = 0200;
    435 			ch = '^';
    436 		} else
    437 			value = 0;
    438 		if (ch == '^') {
    439 			ch = *str++;
    440 			if (ch == '?')
    441 				value |= 0177;
    442 			else
    443 				value |= ch & 037;
    444 			do_putchar(value);
    445 			continue;
    446 		}
    447 
    448 		/* Finally test for sequences valid in the format string */
    449 		str = conv_escape(str - 1, &c, quiet);
    450 		do_putchar(c);
    451 	}
    452 }
    453 
    454 /*
    455  * Print "standard" escape characters
    456  */
    457 static char *
    458 conv_escape(char *str, char *conv_ch, int quiet)
    459 {
    460 	char value;
    461 	char ch;
    462 	char num_buf[4], *num_end;
    463 
    464 	ch = *str++;
    465 
    466 	switch (ch) {
    467 	case '\0':
    468 		if (!quiet)
    469 			warnx("incomplete escape sequence");
    470 		rval = 1;
    471 		value = '\\';
    472 		--str;
    473 		break;
    474 
    475 	case '0': case '1': case '2': case '3':
    476 	case '4': case '5': case '6': case '7':
    477 		num_buf[0] = ch;
    478 		ch = str[0];
    479 		num_buf[1] = ch;
    480 		num_buf[2] = (char)(ch != '\0' ? str[1] : '\0');
    481 		num_buf[3] = '\0';
    482 		value = (char)strtoul(num_buf, &num_end, 8);
    483 		str += num_end  - (num_buf + 1);
    484 		break;
    485 
    486 	case 'x':
    487 		/*
    488 		 * Hexadecimal character constants are not required to be
    489 		 * supported (by SuS v1) because there is no consistent
    490 		 * way to detect the end of the constant.
    491 		 * Supporting 2 byte constants is a compromise.
    492 		 */
    493 		ch = str[0];
    494 		num_buf[0] = ch;
    495 		num_buf[1] = (char)(ch != '\0' ? str[1] : '\0');
    496 		num_buf[2] = '\0';
    497 		value = (char)strtoul(num_buf, &num_end, 16);
    498 		str += num_end - num_buf;
    499 		break;
    500 
    501 	case '\\':	value = '\\';	break;	/* backslash */
    502 	case '\'':	value = '\'';	break;	/* single quote */
    503 	case '"':	value = '"';	break;	/* double quote */
    504 	case 'a':	value = '\a';	break;	/* alert */
    505 	case 'b':	value = '\b';	break;	/* backspace */
    506 	case 'e':	value = ESCAPE;	break;	/* escape */
    507 	case 'E':	value = ESCAPE;	break;	/* escape */
    508 	case 'f':	value = '\f';	break;	/* form-feed */
    509 	case 'n':	value = '\n';	break;	/* newline */
    510 	case 'r':	value = '\r';	break;	/* carriage-return */
    511 	case 't':	value = '\t';	break;	/* tab */
    512 	case 'v':	value = '\v';	break;	/* vertical-tab */
    513 
    514 	default:
    515 		if (!quiet)
    516 			warnx("unknown escape sequence `\\%c'", ch);
    517 		rval = 1;
    518 		value = ch;
    519 		break;
    520 	}
    521 
    522 	*conv_ch = value;
    523 	return str;
    524 }
    525 
    526 /* expand a string so that everything is printable */
    527 
    528 static char *
    529 conv_expand(const char *str)
    530 {
    531 	static char *conv_str;
    532 	char *cp;
    533 	char ch;
    534 
    535 	if (conv_str)
    536 		free(conv_str);
    537 	/* get a buffer that is definitely large enough.... */
    538 	conv_str = malloc(4 * strlen(str) + 1);
    539 	if (!conv_str)
    540 		return NULL;
    541 	cp = conv_str;
    542 
    543 	while ((ch = *(const char *)str++) != '\0') {
    544 		switch (ch) {
    545 		/* Use C escapes for expected control characters */
    546 		case '\\':	ch = '\\';	break;	/* backslash */
    547 		case '\'':	ch = '\'';	break;	/* single quote */
    548 		case '"':	ch = '"';	break;	/* double quote */
    549 		case '\a':	ch = 'a';	break;	/* alert */
    550 		case '\b':	ch = 'b';	break;	/* backspace */
    551 		case ESCAPE:	ch = 'e';	break;	/* escape */
    552 		case '\f':	ch = 'f';	break;	/* form-feed */
    553 		case '\n':	ch = 'n';	break;	/* newline */
    554 		case '\r':	ch = 'r';	break;	/* carriage-return */
    555 		case '\t':	ch = 't';	break;	/* tab */
    556 		case '\v':	ch = 'v';	break;	/* vertical-tab */
    557 		default:
    558 			/* Copy anything printable */
    559 			if (isprint((unsigned char)ch)) {
    560 				*cp++ = ch;
    561 				continue;
    562 			}
    563 			/* Use vis(3) encodings for the rest */
    564 			*cp++ = '\\';
    565 			if (ch & 0200) {
    566 				*cp++ = 'M';
    567 				ch &= (char)~0200;
    568 			}
    569 			if (ch == 0177) {
    570 				*cp++ = '^';
    571 				*cp++ = '?';
    572 				continue;
    573 			}
    574 			if (ch < 040) {
    575 				*cp++ = '^';
    576 				*cp++ = ch | 0100;
    577 				continue;
    578 			}
    579 			*cp++ = '-';
    580 			*cp++ = ch;
    581 			continue;
    582 		}
    583 		*cp++ = '\\';
    584 		*cp++ = ch;
    585 	}
    586 
    587 	*cp = 0;
    588 	return conv_str;
    589 }
    590 
    591 static char *
    592 mklong(const char *str, char ch)
    593 {
    594 	static char copy[64];
    595 	size_t len;
    596 
    597 	len = strlen(str) + 2;
    598 	if (len > sizeof copy) {
    599 		warnx("format %s too complex", str);
    600 		len = 4;
    601 	}
    602 	(void)memmove(copy, str, len - 3);
    603 	copy[len - 3] = 'j';
    604 	copy[len - 2] = ch;
    605 	copy[len - 1] = '\0';
    606 	return copy;
    607 }
    608 
    609 static char
    610 getchr(void)
    611 {
    612 	if (!*gargv)
    613 		return 0;
    614 	return **gargv++;
    615 }
    616 
    617 static char *
    618 getstr(void)
    619 {
    620 	static char empty[] = "";
    621 	if (!*gargv)
    622 		return empty;
    623 	return *gargv++;
    624 }
    625 
    626 static int
    627 getwidth(void)
    628 {
    629 	unsigned long val;
    630 	char *s, *ep;
    631 
    632 	s = *gargv;
    633 	if (s == NULL)
    634 		return 0;
    635 	gargv++;
    636 
    637 	errno = 0;
    638 	val = strtoul(s, &ep, 0);
    639 	check_conversion(s, ep);
    640 
    641 	/* Arbitrarily 'restrict' field widths to 1Mbyte */
    642 	if (val > 1 << 20) {
    643 		warnx("%s: invalid field width", s);
    644 		return 0;
    645 	}
    646 
    647 	return (int)val;
    648 }
    649 
    650 static intmax_t
    651 getintmax(void)
    652 {
    653 	intmax_t val;
    654 	char *cp, *ep;
    655 
    656 	cp = *gargv;
    657 	if (cp == NULL)
    658 		return 0;
    659 	gargv++;
    660 
    661 	if (*cp == '\"' || *cp == '\'')
    662 		return *(cp + 1);
    663 
    664 	errno = 0;
    665 	val = strtoimax(cp, &ep, 0);
    666 	check_conversion(cp, ep);
    667 	return val;
    668 }
    669 
    670 static double
    671 getdouble(void)
    672 {
    673 	double val;
    674 	char *ep;
    675 
    676 	if (!*gargv)
    677 		return 0.0;
    678 
    679 	if (**gargv == '\"' || **gargv == '\'')
    680 		return (double) *((*gargv++)+1);
    681 
    682 	errno = 0;
    683 	val = strtod(*gargv, &ep);
    684 	check_conversion(*gargv++, ep);
    685 	return val;
    686 }
    687 
    688 static void
    689 check_conversion(const char *s, const char *ep)
    690 {
    691 	if (*ep) {
    692 		if (ep == s)
    693 			warnx("%s: expected numeric value", s);
    694 		else
    695 			warnx("%s: not completely converted", s);
    696 		rval = 1;
    697 	} else if (errno == ERANGE) {
    698 		warnx("%s: %s", s, strerror(ERANGE));
    699 		rval = 1;
    700 	}
    701 }
    702 
    703 static void
    704 usage(void)
    705 {
    706 	(void)fprintf(stderr, "Usage: %s format [arg ...]\n", getprogname());
    707 }
    708