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