xref: /freebsd/usr.bin/printf/printf.c (revision b7a1a0a5ff8895e6a5c9404d02357ac9e2eb8091)
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 #ifdef SHELL
52 #define	EOF	-1
53 #else
54 #include <stdio.h>
55 #endif
56 #include <stdlib.h>
57 #include <string.h>
58 
59 /*
60  * XXX
61  * This *has* to go away.  TK.
62  */
63 #ifdef SHELL
64 #define main printfcmd
65 #define warnx(a, b, c) {						\
66 	char buf[64];							\
67 	(void)sprintf(buf, sizeof(buf), a, b, c);			\
68 	error(buf);							\
69 }
70 #include "../../bin/sh/bltin/bltin.h"
71 #endif
72 
73 #define PF(f, func) { \
74 	if (fieldwidth) \
75 		if (precision) \
76 			(void)printf(f, fieldwidth, precision, func); \
77 		else \
78 			(void)printf(f, fieldwidth, func); \
79 	else if (precision) \
80 		(void)printf(f, precision, func); \
81 	else \
82 		(void)printf(f, func); \
83 }
84 
85 static int	 asciicode __P((void));
86 static void	 escape __P((char *));
87 static int	 getchr __P((void));
88 static double	 getdouble __P((void));
89 static int	 getint __P((int *));
90 static int	 getlong __P((long *));
91 static char	*getstr __P((void));
92 static char	*mklong __P((char *, int));
93 static void	 usage __P((void));
94 
95 static char **gargv;
96 
97 int
98 #ifdef BUILTIN
99 progprintf(argc, argv)
100 #else
101 main(argc, argv)
102 #endif
103 	int argc;
104 	char *argv[];
105 {
106 	extern int optind;
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, "")) != EOF)
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 					warnx("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 		} else
174 			fieldwidth = 0;
175 
176 		/* skip to possible '.', get following precision */
177 		for (; strchr(skip2, *fmt); ++fmt);
178 		if (*fmt == '.')
179 			++fmt;
180 		if (*fmt == '*') {
181 			if (getint(&precision))
182 				return (1);
183 		} else
184 			precision = 0;
185 
186 		/* skip to conversion char */
187 		for (; strchr(skip2, *fmt); ++fmt);
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", NULL, 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