xref: /freebsd/usr.bin/printf/printf.c (revision 04c9749ff0148ec8f73b150cec8bc2c094a5d31a)
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 static char const sccsid[] = "@(#)printf.c	8.1 (Berkeley) 7/20/93";
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 
50 #include <err.h>
51 #include <errno.h>
52 #include <limits.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #ifdef SHELL
59 #define main printfcmd
60 #include "bltin/bltin.h"
61 #else
62 #define	warnx1(a, b, c)		warnx(a)
63 #define	warnx2(a, b, c)		warnx(a, b)
64 #define	warnx3(a, b, c)		warnx(a, b, c)
65 #endif
66 
67 #define PF(f, func) { \
68 	char *b = NULL; \
69 	if (fieldwidth) \
70 		if (precision) \
71 			(void)asprintf(&b, f, fieldwidth, precision, func); \
72 		else \
73 			(void)asprintf(&b, f, fieldwidth, func); \
74 	else if (precision) \
75 		(void)asprintf(&b, f, precision, func); \
76 	else \
77 		(void)asprintf(&b, f, func); \
78 	if (b) { \
79 		(void)fputs(b, stdout); \
80 		free(b); \
81 	} \
82 }
83 
84 static int	 asciicode __P((void));
85 static void	 escape __P((char *));
86 static int	 getchr __P((void));
87 static double	 getdouble __P((void));
88 static int	 getint __P((int *));
89 static int	 getquad __P((quad_t *));
90 static char	*getstr __P((void));
91 static char	*mklong __P((char *, int));
92 static void	 usage __P((void));
93 
94 static char **gargv;
95 
96 int
97 #ifdef BUILTIN
98 progprintf(argc, argv)
99 #else
100 main(argc, argv)
101 #endif
102 	int argc;
103 	char *argv[];
104 {
105 	static char *skip1, *skip2;
106 	int ch, end, fieldwidth, precision;
107 	char convch, nextch, *format, *fmt, *start;
108 
109 	while ((ch = getopt(argc, argv, "")) != -1)
110 		switch (ch) {
111 		case '?':
112 		default:
113 			usage();
114 			return (1);
115 		}
116 	argc -= optind;
117 	argv += optind;
118 
119 	if (argc < 1) {
120 		usage();
121 		return (1);
122 	}
123 
124 	/*
125 	 * Basic algorithm is to scan the format string for conversion
126 	 * specifications -- once one is found, find out if the field
127 	 * width or precision is a '*'; if it is, gather up value.  Note,
128 	 * format strings are reused as necessary to use up the provided
129 	 * arguments, arguments of zero/null string are provided to use
130 	 * up the format string.
131 	 */
132 	skip1 = "#-+ 0";
133 	skip2 = "0123456789";
134 
135 	escape(fmt = format = *argv);		/* backslash interpretation */
136 	gargv = ++argv;
137 	for (;;) {
138 		end = 0;
139 		/* find next format specification */
140 next:		for (start = fmt;; ++fmt) {
141 			if (!*fmt) {
142 				/* avoid infinite loop */
143 				if (end == 1) {
144 					warnx1("missing format character",
145 					    NULL, NULL);
146 					return (1);
147 				}
148 				end = 1;
149 				if (fmt > start)
150 					(void)printf("%s", start);
151 				if (!*gargv)
152 					return (0);
153 				fmt = format;
154 				goto next;
155 			}
156 			/* %% prints a % */
157 			if (*fmt == '%') {
158 				if (*++fmt != '%')
159 					break;
160 				*fmt++ = '\0';
161 				(void)printf("%s", start);
162 				goto next;
163 			}
164 		}
165 
166 		/* skip to field width */
167 		for (; strchr(skip1, *fmt); ++fmt);
168 		if (*fmt == '*') {
169 			if (getint(&fieldwidth))
170 				return (1);
171 			++fmt;
172 		} else {
173 			fieldwidth = 0;
174 
175 			/* skip to possible '.', get following precision */
176 			for (; strchr(skip2, *fmt); ++fmt);
177 		}
178 		if (*fmt == '.') {
179 			/* precision present? */
180 			++fmt;
181 			if (*fmt == '*') {
182 				if (getint(&precision))
183 					return (1);
184 				++fmt;
185 			} else {
186 				precision = 0;
187 
188 				/* skip to conversion char */
189 				for (; strchr(skip2, *fmt); ++fmt);
190 			}
191 		} else
192 			precision = 0;
193 		if (!*fmt) {
194 			warnx1("missing format character", NULL, NULL);
195 			return (1);
196 		}
197 
198 		convch = *fmt;
199 		nextch = *++fmt;
200 		*fmt = '\0';
201 		switch(convch) {
202 		case 'c': {
203 			char p;
204 
205 			p = getchr();
206 			PF(start, p);
207 			break;
208 		}
209 		case 's': {
210 			char *p;
211 
212 			p = getstr();
213 			PF(start, p);
214 			break;
215 		}
216 		case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
217 			quad_t p;
218 			char *f;
219 
220 			if ((f = mklong(start, convch)) == NULL)
221 				return (1);
222 			if (getquad(&p))
223 				return (1);
224 			PF(f, p);
225 			break;
226 		}
227 		case 'e': case 'E': case 'f': case 'g': case 'G': {
228 			double p;
229 
230 			p = getdouble();
231 			PF(start, p);
232 			break;
233 		}
234 		default:
235 			warnx2("illegal format character %c", convch, NULL);
236 			return (1);
237 		}
238 		*fmt = nextch;
239 	}
240 	/* NOTREACHED */
241 }
242 
243 static char *
244 mklong(str, ch)
245 	char *str;
246 	int ch;
247 {
248 	static char copy[64];
249 	int len;
250 
251 	len = strlen(str) + 2;
252 	if (len > sizeof copy)
253 		return NULL;
254 
255 	memmove(copy, str, len - 3);
256 	copy[len - 3] = 'q';
257 	copy[len - 2] = ch;
258 	copy[len - 1] = '\0';
259 	return (copy);
260 }
261 
262 static void
263 escape(fmt)
264 	register char *fmt;
265 {
266 	register char *store;
267 	register int value, c;
268 
269 	for (store = fmt; (c = *fmt); ++fmt, ++store) {
270 		if (c != '\\') {
271 			*store = c;
272 			continue;
273 		}
274 		switch (*++fmt) {
275 		case '\0':		/* EOS, user error */
276 			*store = '\\';
277 			*++store = '\0';
278 			return;
279 		case '\\':		/* backslash */
280 		case '\'':		/* single quote */
281 			*store = *fmt;
282 			break;
283 		case 'a':		/* bell/alert */
284 			*store = '\7';
285 			break;
286 		case 'b':		/* backspace */
287 			*store = '\b';
288 			break;
289 		case 'f':		/* form-feed */
290 			*store = '\f';
291 			break;
292 		case 'n':		/* newline */
293 			*store = '\n';
294 			break;
295 		case 'r':		/* carriage-return */
296 			*store = '\r';
297 			break;
298 		case 't':		/* horizontal tab */
299 			*store = '\t';
300 			break;
301 		case 'v':		/* vertical tab */
302 			*store = '\13';
303 			break;
304 					/* octal constant */
305 		case '0': case '1': case '2': case '3':
306 		case '4': case '5': case '6': case '7':
307 			for (c = 3, value = 0;
308 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
309 				value <<= 3;
310 				value += *fmt - '0';
311 			}
312 			--fmt;
313 			*store = value;
314 			break;
315 		default:
316 			*store = *fmt;
317 			break;
318 		}
319 	}
320 	*store = '\0';
321 }
322 
323 static int
324 getchr()
325 {
326 	if (!*gargv)
327 		return ('\0');
328 	return ((int)**gargv++);
329 }
330 
331 static char *
332 getstr()
333 {
334 	if (!*gargv)
335 		return ("");
336 	return (*gargv++);
337 }
338 
339 static char *Number = "+-.0123456789";
340 static int
341 getint(ip)
342 	int *ip;
343 {
344 	quad_t val;
345 
346 	if (getquad(&val))
347 		return (1);
348 	if (val < INT_MIN || val > INT_MAX) {
349 		warnx3("%s: %s", *gargv, strerror(ERANGE));
350 		return (1);
351 	}
352 	*ip = (int)val;
353 	return (0);
354 }
355 
356 static int
357 getquad(lp)
358 	quad_t *lp;
359 {
360 	quad_t val;
361 	char *ep;
362 
363 	if (!*gargv) {
364 		*lp = 0;
365 		return (0);
366 	}
367 	if (strchr(Number, **gargv)) {
368 		errno = 0;
369 		val = strtoq(*gargv, &ep, 0);
370 		if (*ep != '\0') {
371 			warnx2("%s: illegal number", *gargv, NULL);
372 			return (1);
373 		}
374 		if (errno == ERANGE)
375 			if (val == QUAD_MAX) {
376 				warnx3("%s: %s", *gargv, strerror(ERANGE));
377 				return (1);
378 			}
379 			if (val == QUAD_MIN) {
380 				warnx3("%s: %s", *gargv, strerror(ERANGE));
381 				return (1);
382 			}
383 
384 		*lp = val;
385 		++gargv;
386 		return (0);
387 	}
388 	*lp =  (long)asciicode();
389 	return (0);
390 }
391 
392 static double
393 getdouble()
394 {
395 	if (!*gargv)
396 		return ((double)0);
397 	if (strchr(Number, **gargv))
398 		return (atof(*gargv++));
399 	return ((double)asciicode());
400 }
401 
402 static int
403 asciicode()
404 {
405 	register int ch;
406 
407 	ch = **gargv;
408 	if (ch == '\'' || ch == '"')
409 		ch = (*gargv)[1];
410 	++gargv;
411 	return (ch);
412 }
413 
414 static void
415 usage()
416 {
417 	(void)fprintf(stderr, "usage: printf format [arg ...]\n");
418 }
419