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