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