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