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