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