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