xref: /dragonfly/usr.bin/printf/printf.c (revision bcb3e04d)
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  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)printf.c	8.1 (Berkeley) 7/20/93
35  * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/usr.bin/printf/printf.c,v 1.27 2004/03/07 22:22:13 cperciva Exp $
36  * $DragonFly: src/usr.bin/printf/printf.c,v 1.7 2007/01/21 00:24:55 swildner Exp $
37  */
38 
39 #include <sys/types.h>
40 
41 #include <err.h>
42 #include <errno.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #ifdef SHELL
50 #define main printfcmd
51 #include "bltin/bltin.h"
52 #include "memalloc.h"
53 #else
54 #define	warnx1(a, b, c)		warnx(a)
55 #define	warnx2(a, b, c)		warnx(a, b)
56 #define	warnx3(a, b, c)		warnx(a, b, c)
57 #endif
58 
59 #ifndef BUILTIN
60 #include <locale.h>
61 #endif
62 
63 #define PF(f, func) do { \
64 	char *b = NULL; \
65 	if (fieldwidth) \
66 		if (precision) \
67 			asprintf(&b, f, fieldwidth, precision, func); \
68 		else \
69 			asprintf(&b, f, fieldwidth, func); \
70 	else if (precision) \
71 		asprintf(&b, f, precision, func); \
72 	else \
73 		asprintf(&b, f, func); \
74 	if (b) { \
75 		fputs(b, stdout); \
76 		free(b); \
77 	} \
78 } while (0)
79 
80 static int	 asciicode(void);
81 static int	 escape(char *);
82 static int	 getchr(void);
83 static int	 getdouble(double *);
84 static int	 getint(int *);
85 static int	 getlonglongs(long long *, unsigned long long *, int);
86 static const char
87 		*getstr(void);
88 static char	*mkquad(char *, int);
89 static void	 usage(void);
90 
91 static char **gargv;
92 
93 int
94 #ifdef BUILTIN
95 progprintf(int argc, char **argv)
96 #else
97 main(int argc, char **argv)
98 #endif
99 {
100 	static const char *skip1, *skip2;
101 	int chopped, end, fieldwidth, precision, rval;
102 	char convch, nextch, *format, *fmt, *start;
103 
104 #ifndef BUILTIN
105 	setlocale(LC_NUMERIC, "");
106 #endif
107 
108 	/*
109 	 * We may not use getopt(3) because calling
110 	 * "printf -f%s oo" may not result in an invalid
111 	 * option error.
112 	 * However common usage and other implementations seem
113 	 * to indicate that we need to allow -- as a discardable
114 	 * option separator.
115 	 */
116 	if (argc > 1 && strcmp(argv[1], "--") == 0) {
117 		argc--;
118 		argv++;
119 	}
120 
121 	if (argc < 2) {
122 		usage();
123 		return (1);
124 	}
125 
126 	argv++;
127 
128 	/*
129 	 * Basic algorithm is to scan the format string for conversion
130 	 * specifications -- once one is found, find out if the field
131 	 * width or precision is a '*'; if it is, gather up value.  Note,
132 	 * format strings are reused as necessary to use up the provided
133 	 * arguments, arguments of zero/null string are provided to use
134 	 * up the format string.
135 	 */
136 	skip1 = "#-+ 0";
137 	skip2 = "0123456789";
138 
139 	chopped = escape(fmt = format = *argv);	/* backslash interpretation */
140 	rval = 0;
141 	gargv = ++argv;
142 	for (;;) {
143 		end = 0;
144 		/* find next format specification */
145 next:		for (start = fmt;; ++fmt) {
146 			if (!*fmt) {
147 				/* avoid infinite loop */
148 				if (chopped) {
149 					printf("%s", start);
150 					return (rval);
151 				}
152 				if (end == 1) {
153 					warnx1("missing format character",
154 					    NULL, NULL);
155 					return (1);
156 				}
157 				end = 1;
158 				if (fmt > start)
159 					printf("%s", start);
160 				if (!*gargv)
161 					return (rval);
162 				fmt = format;
163 				goto next;
164 			}
165 			/* %% prints a % */
166 			if (*fmt == '%') {
167 				if (*++fmt != '%')
168 					break;
169 				*fmt++ = '\0';
170 				printf("%s", start);
171 				goto next;
172 			}
173 		}
174 
175 		/* skip to field width */
176 		for (; strchr(skip1, *fmt); ++fmt);
177 		if (*fmt == '*') {
178 			if (getint(&fieldwidth))
179 				return (1);
180 			++fmt;
181 		} else {
182 			fieldwidth = 0;
183 
184 			/* skip to possible '.', get following precision */
185 			for (; strchr(skip2, *fmt); ++fmt);
186 		}
187 		if (*fmt == '.') {
188 			/* precision present? */
189 			++fmt;
190 			if (*fmt == '*') {
191 				if (getint(&precision))
192 					return (1);
193 				++fmt;
194 			} else {
195 				precision = 0;
196 
197 				/* skip to conversion char */
198 				for (; strchr(skip2, *fmt); ++fmt);
199 			}
200 		} else
201 			precision = 0;
202 		if (!*fmt) {
203 			warnx1("missing format character", NULL, NULL);
204 			return (1);
205 		}
206 
207 		convch = *fmt;
208 		nextch = *++fmt;
209 		*fmt = '\0';
210 		switch(convch) {
211 		case 'b': {
212 			char *p;
213 			int getout;
214 
215 #ifdef SHELL
216 			p = savestr(getstr());
217 #else
218 			p = strdup(getstr());
219 #endif
220 			if (p == NULL) {
221 				warnx2("%s", strerror(ENOMEM), NULL);
222 				return (1);
223 			}
224 			getout = escape(p);
225 			*(fmt - 1) = 's';
226 			PF(start, p);
227 			*(fmt - 1) = 'b';
228 #ifdef SHELL
229 			ckfree(p);
230 #else
231 			free(p);
232 #endif
233 			if (getout)
234 				return (rval);
235 			break;
236 		}
237 		case 'c': {
238 			char p;
239 
240 			p = getchr();
241 			PF(start, p);
242 			break;
243 		}
244 		case 's': {
245 			const char *p;
246 
247 			p = getstr();
248 			PF(start, p);
249 			break;
250 		}
251 		case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
252 			char *f;
253 			long long val;
254 			unsigned long long uval;
255 			int signedconv;
256 
257 			signedconv = (convch == 'd' || convch == 'i');
258 			if ((f = mkquad(start, convch)) == NULL)
259 				return (1);
260 			if (getlonglongs(&val, &uval, signedconv))
261 				rval = 1;
262 			if (signedconv)
263 				PF(f, val);
264 			else
265 				PF(f, uval);
266 			break;
267 		}
268 		case 'e': case 'E': case 'f': case 'g': case 'G': {
269 			double p;
270 
271 			if (getdouble(&p))
272 				rval = 1;
273 			PF(start, p);
274 			break;
275 		}
276 		default:
277 			warnx2("illegal format character %c", convch, NULL);
278 			return (1);
279 		}
280 		*fmt = nextch;
281 	}
282 	/* NOTREACHED */
283 }
284 
285 static char *
286 mkquad(char *str, int ch)
287 {
288 	static char *copy;
289 	static size_t copy_size;
290 	char *newcopy;
291 	size_t len, newlen;
292 
293 	len = strlen(str) + 2;
294 	if (len > copy_size) {
295 		newlen = ((len + 1023) >> 10) << 10;
296 #ifdef SHELL
297 		if ((newcopy = ckrealloc(copy, newlen)) == NULL)
298 #else
299 		if ((newcopy = realloc(copy, newlen)) == NULL)
300 #endif
301 		{
302 			warnx2("%s", strerror(ENOMEM), NULL);
303 			return (NULL);
304 		}
305 		copy = newcopy;
306 		copy_size = newlen;
307 	}
308 
309 	memmove(copy, str, len - 3);
310 	copy[len - 3] = 'q';
311 	copy[len - 2] = ch;
312 	copy[len - 1] = '\0';
313 	return (copy);
314 }
315 
316 static int
317 escape(char *fmt)
318 {
319 	char *store;
320 	int value, c;
321 
322 	for (store = fmt; (c = *fmt); ++fmt, ++store) {
323 		if (c != '\\') {
324 			*store = c;
325 			continue;
326 		}
327 		switch (*++fmt) {
328 		case '\0':		/* EOS, user error */
329 			*store = '\\';
330 			*++store = '\0';
331 			return (0);
332 		case '\\':		/* backslash */
333 		case '\'':		/* single quote */
334 			*store = *fmt;
335 			break;
336 		case 'a':		/* bell/alert */
337 			*store = '\7';
338 			break;
339 		case 'b':		/* backspace */
340 			*store = '\b';
341 			break;
342 		case 'c':
343 			*store = '\0';
344 			return (1);
345 		case 'f':		/* form-feed */
346 			*store = '\f';
347 			break;
348 		case 'n':		/* newline */
349 			*store = '\n';
350 			break;
351 		case 'r':		/* carriage-return */
352 			*store = '\r';
353 			break;
354 		case 't':		/* horizontal tab */
355 			*store = '\t';
356 			break;
357 		case 'v':		/* vertical tab */
358 			*store = '\13';
359 			break;
360 					/* octal constant */
361 		case '0': case '1': case '2': case '3':
362 		case '4': case '5': case '6': case '7':
363 			for (c = *fmt == '0' ? 4 : 3, value = 0;
364 			    c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
365 				value <<= 3;
366 				value += *fmt - '0';
367 			}
368 			--fmt;
369 			*store = value;
370 			break;
371 		default:
372 			*store = *fmt;
373 			break;
374 		}
375 	}
376 	*store = '\0';
377 	return (0);
378 }
379 
380 static int
381 getchr(void)
382 {
383 	if (!*gargv)
384 		return ('\0');
385 	return ((int)**gargv++);
386 }
387 
388 static const char *
389 getstr(void)
390 {
391 	if (!*gargv)
392 		return ("");
393 	return (*gargv++);
394 }
395 
396 static int
397 getint(int *ip)
398 {
399 	long long val;
400 	unsigned long long uval;
401 	int rval;
402 
403 	if (getlonglongs(&val, &uval, 1))
404 		return (1);
405 	rval = 0;
406 	if (val < INT_MIN || val > INT_MAX) {
407 		warnx3("%s: %s", *gargv, strerror(ERANGE));
408 		rval = 1;
409 	}
410 	*ip = (int)val;
411 	return (rval);
412 }
413 
414 static int
415 getlonglongs(long long *qp, unsigned long long *uqp, int signedconv)
416 {
417 	char *ep;
418 	int rval;
419 
420 	if (!*gargv) {
421 		*qp = 0;
422 		return (0);
423 	}
424 	if (**gargv == '"' || **gargv == '\'') {
425 		if (signedconv)
426 			*qp = asciicode();
427 		else
428 			*uqp = asciicode();
429 		return (0);
430 	}
431 	rval = 0;
432 	errno = 0;
433 	if (signedconv)
434 		*qp = strtoll(*gargv, &ep, 0);
435 	else
436 		*uqp = strtoull(*gargv, &ep, 0);
437 	if (ep == *gargv) {
438 		warnx2("%s: expected numeric value", *gargv, NULL);
439 		rval = 1;
440 	}
441 	else if (*ep != '\0') {
442 		warnx2("%s: not completely converted", *gargv, NULL);
443 		rval = 1;
444 	}
445 	if (errno == ERANGE) {
446 		warnx3("%s: %s", *gargv, strerror(ERANGE));
447 		rval = 1;
448 	}
449 	++gargv;
450 	return (rval);
451 }
452 
453 static int
454 getdouble(double *dp)
455 {
456 	char *ep;
457 	int rval;
458 
459 	if (!*gargv) {
460 		*dp = 0.0;
461 		return (0);
462 	}
463 	if (**gargv == '"' || **gargv == '\'') {
464 		*dp = asciicode();
465 		return (0);
466 	}
467 	rval = 0;
468 	errno = 0;
469 	*dp = strtod(*gargv, &ep);
470 	if (ep == *gargv) {
471 		warnx2("%s: expected numeric value", *gargv, NULL);
472 		rval = 1;
473 	} else if (*ep != '\0') {
474 		warnx2("%s: not completely converted", *gargv, NULL);
475 		rval = 1;
476 	}
477 	if (errno == ERANGE) {
478 		warnx3("%s: %s", *gargv, strerror(ERANGE));
479 		rval = 1;
480 	}
481 	++gargv;
482 	return (rval);
483 }
484 
485 static int
486 asciicode(void)
487 {
488 	int ch;
489 
490 	ch = **gargv;
491 	if (ch == '\'' || ch == '"')
492 		ch = (*gargv)[1];
493 	++gargv;
494 	return (ch);
495 }
496 
497 static void
498 usage(void)
499 {
500 	fprintf(stderr, "usage: printf format [arg ...]\n");
501 }
502