xref: /freebsd/usr.bin/printf/printf.c (revision 817420dc8eac7df799c78f5309b75092b7f7cd40)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if !defined(BUILTIN) && !defined(SHELL)
35 #ifndef lint
36 static char const copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 	The Regents of the University of California.  All rights reserved.\n";
39 #endif /* not lint */
40 #endif
41 
42 #ifndef lint
43 #if 0
44 static char const sccsid[] = "@(#)printf.c	8.1 (Berkeley) 7/20/93";
45 #endif
46 static const char rcsid[] =
47   "$FreeBSD$";
48 #endif /* not lint */
49 
50 #include <sys/types.h>
51 
52 #include <err.h>
53 #include <errno.h>
54 #include <limits.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #ifdef SHELL
61 #define main printfcmd
62 #include "bltin/bltin.h"
63 #else
64 #define	warnx1(a, b, c)		warnx(a)
65 #define	warnx2(a, b, c)		warnx(a, b)
66 #define	warnx3(a, b, c)		warnx(a, b, c)
67 #endif
68 
69 #define PF(f, func) { \
70 	char *b = NULL; \
71 	if (fieldwidth) \
72 		if (precision) \
73 			(void)asprintf(&b, f, fieldwidth, precision, func); \
74 		else \
75 			(void)asprintf(&b, f, fieldwidth, func); \
76 	else if (precision) \
77 		(void)asprintf(&b, f, precision, func); \
78 	else \
79 		(void)asprintf(&b, f, func); \
80 	if (b) { \
81 		(void)fputs(b, stdout); \
82 		free(b); \
83 	} \
84 }
85 
86 static int	 asciicode __P((void));
87 static void	 escape __P((char *));
88 static int	 getchr __P((void));
89 static double	 getdouble __P((void));
90 static int	 getint __P((int *));
91 static int	 getquad __P((quad_t *));
92 static char	*getstr __P((void));
93 static char	*mklong __P((char *, int));
94 static void	 usage __P((void));
95 
96 static char **gargv;
97 
98 int
99 #ifdef BUILTIN
100 progprintf(argc, argv)
101 #else
102 main(argc, argv)
103 #endif
104 	int argc;
105 	char *argv[];
106 {
107 	static char *skip1, *skip2;
108 	int ch, end, fieldwidth, precision;
109 	char convch, nextch, *format, *fmt, *start;
110 
111 	while ((ch = getopt(argc, argv, "")) != -1)
112 		switch (ch) {
113 		case '?':
114 		default:
115 			usage();
116 			return (1);
117 		}
118 	argc -= optind;
119 	argv += optind;
120 
121 	if (argc < 1) {
122 		usage();
123 		return (1);
124 	}
125 
126 	/*
127 	 * Basic algorithm is to scan the format string for conversion
128 	 * specifications -- once one is found, find out if the field
129 	 * width or precision is a '*'; if it is, gather up value.  Note,
130 	 * format strings are reused as necessary to use up the provided
131 	 * arguments, arguments of zero/null string are provided to use
132 	 * up the format string.
133 	 */
134 	skip1 = "#-+ 0";
135 	skip2 = "0123456789";
136 
137 	escape(fmt = format = *argv);		/* backslash interpretation */
138 	gargv = ++argv;
139 	for (;;) {
140 		end = 0;
141 		/* find next format specification */
142 next:		for (start = fmt;; ++fmt) {
143 			if (!*fmt) {
144 				/* avoid infinite loop */
145 				if (end == 1) {
146 					warnx1("missing format character",
147 					    NULL, NULL);
148 					return (1);
149 				}
150 				end = 1;
151 				if (fmt > start)
152 					(void)printf("%s", start);
153 				if (!*gargv)
154 					return (0);
155 				fmt = format;
156 				goto next;
157 			}
158 			/* %% prints a % */
159 			if (*fmt == '%') {
160 				if (*++fmt != '%')
161 					break;
162 				*fmt++ = '\0';
163 				(void)printf("%s", start);
164 				goto next;
165 			}
166 		}
167 
168 		/* skip to field width */
169 		for (; strchr(skip1, *fmt); ++fmt);
170 		if (*fmt == '*') {
171 			if (getint(&fieldwidth))
172 				return (1);
173 			++fmt;
174 		} else {
175 			fieldwidth = 0;
176 
177 			/* skip to possible '.', get following precision */
178 			for (; strchr(skip2, *fmt); ++fmt);
179 		}
180 		if (*fmt == '.') {
181 			/* precision present? */
182 			++fmt;
183 			if (*fmt == '*') {
184 				if (getint(&precision))
185 					return (1);
186 				++fmt;
187 			} else {
188 				precision = 0;
189 
190 				/* skip to conversion char */
191 				for (; strchr(skip2, *fmt); ++fmt);
192 			}
193 		} else
194 			precision = 0;
195 		if (!*fmt) {
196 			warnx1("missing format character", NULL, NULL);
197 			return (1);
198 		}
199 
200 		convch = *fmt;
201 		nextch = *++fmt;
202 		*fmt = '\0';
203 		switch(convch) {
204 		case 'c': {
205 			char p;
206 
207 			p = getchr();
208 			PF(start, p);
209 			break;
210 		}
211 		case 's': {
212 			char *p;
213 
214 			p = getstr();
215 			PF(start, p);
216 			break;
217 		}
218 		case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
219 			quad_t p;
220 			char *f;
221 
222 			if ((f = mklong(start, convch)) == NULL)
223 				return (1);
224 			if (getquad(&p))
225 				return (1);
226 			PF(f, p);
227 			break;
228 		}
229 		case 'e': case 'E': case 'f': case 'g': case 'G': {
230 			double p;
231 
232 			p = getdouble();
233 			PF(start, p);
234 			break;
235 		}
236 		default:
237 			warnx2("illegal format character %c", convch, NULL);
238 			return (1);
239 		}
240 		*fmt = nextch;
241 	}
242 	/* NOTREACHED */
243 }
244 
245 static char *
246 mklong(str, ch)
247 	char *str;
248 	int ch;
249 {
250 	static char copy[64];
251 	int len;
252 
253 	len = strlen(str) + 2;
254 	if (len > sizeof copy)
255 		return NULL;
256 
257 	memmove(copy, str, len - 3);
258 	copy[len - 3] = 'q';
259 	copy[len - 2] = ch;
260 	copy[len - 1] = '\0';
261 	return (copy);
262 }
263 
264 static void
265 escape(fmt)
266 	register char *fmt;
267 {
268 	register char *store;
269 	register int value, c;
270 
271 	for (store = fmt; (c = *fmt); ++fmt, ++store) {
272 		if (c != '\\') {
273 			*store = c;
274 			continue;
275 		}
276 		switch (*++fmt) {
277 		case '\0':		/* EOS, user error */
278 			*store = '\\';
279 			*++store = '\0';
280 			return;
281 		case '\\':		/* backslash */
282 		case '\'':		/* single quote */
283 			*store = *fmt;
284 			break;
285 		case 'a':		/* bell/alert */
286 			*store = '\7';
287 			break;
288 		case 'b':		/* backspace */
289 			*store = '\b';
290 			break;
291 		case 'f':		/* form-feed */
292 			*store = '\f';
293 			break;
294 		case 'n':		/* newline */
295 			*store = '\n';
296 			break;
297 		case 'r':		/* carriage-return */
298 			*store = '\r';
299 			break;
300 		case 't':		/* horizontal tab */
301 			*store = '\t';
302 			break;
303 		case 'v':		/* vertical tab */
304 			*store = '\13';
305 			break;
306 					/* octal constant */
307 		case '0': case '1': case '2': case '3':
308 		case '4': case '5': case '6': case '7':
309 			for (c = 3, value = 0;
310 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
311 				value <<= 3;
312 				value += *fmt - '0';
313 			}
314 			--fmt;
315 			*store = value;
316 			break;
317 		default:
318 			*store = *fmt;
319 			break;
320 		}
321 	}
322 	*store = '\0';
323 }
324 
325 static int
326 getchr()
327 {
328 	if (!*gargv)
329 		return ('\0');
330 	return ((int)**gargv++);
331 }
332 
333 static char *
334 getstr()
335 {
336 	if (!*gargv)
337 		return ("");
338 	return (*gargv++);
339 }
340 
341 static char *Number = "+-.0123456789";
342 static int
343 getint(ip)
344 	int *ip;
345 {
346 	quad_t val;
347 
348 	if (getquad(&val))
349 		return (1);
350 	if (val < INT_MIN || val > INT_MAX) {
351 		warnx3("%s: %s", *gargv, strerror(ERANGE));
352 		return (1);
353 	}
354 	*ip = (int)val;
355 	return (0);
356 }
357 
358 static int
359 getquad(lp)
360 	quad_t *lp;
361 {
362 	quad_t val;
363 	char *ep;
364 
365 	if (!*gargv) {
366 		*lp = 0;
367 		return (0);
368 	}
369 	if (strchr(Number, **gargv)) {
370 		errno = 0;
371 		val = strtoq(*gargv, &ep, 0);
372 		if (*ep != '\0') {
373 			warnx2("%s: illegal number", *gargv, NULL);
374 			return (1);
375 		}
376 		if (errno == ERANGE)
377 			if (val == QUAD_MAX) {
378 				warnx3("%s: %s", *gargv, strerror(ERANGE));
379 				return (1);
380 			}
381 			if (val == QUAD_MIN) {
382 				warnx3("%s: %s", *gargv, strerror(ERANGE));
383 				return (1);
384 			}
385 
386 		*lp = val;
387 		++gargv;
388 		return (0);
389 	}
390 	*lp =  (long)asciicode();
391 	return (0);
392 }
393 
394 static double
395 getdouble()
396 {
397 	if (!*gargv)
398 		return ((double)0);
399 	if (strchr(Number, **gargv))
400 		return (atof(*gargv++));
401 	return ((double)asciicode());
402 }
403 
404 static int
405 asciicode()
406 {
407 	register int ch;
408 
409 	ch = **gargv;
410 	if (ch == '\'' || ch == '"')
411 		ch = (*gargv)[1];
412 	++gargv;
413 	return (ch);
414 }
415 
416 static void
417 usage()
418 {
419 	(void)fprintf(stderr, "usage: printf format [arg ...]\n");
420 }
421