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