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