printf.c revision 1.23 1 1.23 wiz /* $NetBSD: printf.c,v 1.23 2002/06/14 01:12:15 wiz Exp $ */
2 1.14 tls
3 1.1 cgd /*
4 1.17 mrg * Copyright (c) 1989, 1993
5 1.17 mrg * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.16 christos #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.17 mrg #if !defined(BUILTIN) && !defined(SHELL)
39 1.17 mrg __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
40 1.17 mrg The Regents of the University of California. All rights reserved.\n");
41 1.17 mrg #endif
42 1.5 jtc #endif
43 1.1 cgd
44 1.1 cgd #ifndef lint
45 1.16 christos #if 0
46 1.17 mrg static char sccsid[] = "@(#)printf.c 8.2 (Berkeley) 3/22/95";
47 1.16 christos #else
48 1.23 wiz __RCSID("$NetBSD: printf.c,v 1.23 2002/06/14 01:12:15 wiz Exp $");
49 1.16 christos #endif
50 1.1 cgd #endif /* not lint */
51 1.1 cgd
52 1.17 mrg #include <sys/types.h>
53 1.17 mrg
54 1.4 jtc #include <ctype.h>
55 1.19 perry #include <err.h>
56 1.19 perry #include <errno.h>
57 1.22 kleink #include <inttypes.h>
58 1.19 perry #include <limits.h>
59 1.19 perry #include <locale.h>
60 1.23 wiz #include <stdarg.h>
61 1.1 cgd #include <stdio.h>
62 1.4 jtc #include <stdlib.h>
63 1.2 mycroft #include <string.h>
64 1.19 perry #include <unistd.h>
65 1.4 jtc
66 1.23 wiz static int print_escape_str(const char *);
67 1.23 wiz static size_t print_escape(const char *);
68 1.5 jtc
69 1.23 wiz static int getchr(void);
70 1.23 wiz static double getdouble(void);
71 1.23 wiz static int getint(void);
72 1.23 wiz static intmax_t getintmax(void);
73 1.22 kleink static uintmax_t getuintmax __P ((void));
74 1.23 wiz static char *getstr(void);
75 1.23 wiz static char *mklong(const char *, int);
76 1.23 wiz static void check_conversion(const char *, const char *);
77 1.23 wiz static void usage(void);
78 1.4 jtc
79 1.5 jtc static int rval;
80 1.5 jtc static char **gargv;
81 1.4 jtc
82 1.16 christos #ifdef BUILTIN
83 1.23 wiz int progprintf(int, char **);
84 1.16 christos #else
85 1.23 wiz int main(int, char **);
86 1.16 christos #endif
87 1.16 christos
88 1.4 jtc #define isodigit(c) ((c) >= '0' && (c) <= '7')
89 1.4 jtc #define octtobin(c) ((c) - '0')
90 1.9 jtc #define hextobin(c) ((c) >= 'A' && (c) <= 'F' ? c - 'A' + 10 : (c) >= 'a' && (c) <= 'f' ? c - 'a' + 10 : c - '0')
91 1.1 cgd
92 1.5 jtc #ifdef SHELL
93 1.5 jtc #define main printfcmd
94 1.5 jtc #include "../../bin/sh/bltin/bltin.h"
95 1.5 jtc
96 1.16 christos
97 1.23 wiz static void warnx(const char *fmt, ...);
98 1.5 jtc
99 1.5 jtc static void
100 1.5 jtc warnx(const char *fmt, ...)
101 1.5 jtc {
102 1.5 jtc
103 1.5 jtc char buf[64];
104 1.5 jtc va_list ap;
105 1.5 jtc
106 1.5 jtc va_start(ap, fmt);
107 1.5 jtc vsprintf(buf, fmt, ap);
108 1.5 jtc va_end(ap);
109 1.5 jtc
110 1.5 jtc error(buf);
111 1.5 jtc }
112 1.5 jtc #endif /* SHELL */
113 1.5 jtc
114 1.1 cgd #define PF(f, func) { \
115 1.21 christos if (fieldwidth) { \
116 1.1 cgd if (precision) \
117 1.1 cgd (void)printf(f, fieldwidth, precision, func); \
118 1.1 cgd else \
119 1.1 cgd (void)printf(f, fieldwidth, func); \
120 1.21 christos } else if (precision) \
121 1.1 cgd (void)printf(f, precision, func); \
122 1.1 cgd else \
123 1.1 cgd (void)printf(f, func); \
124 1.1 cgd }
125 1.1 cgd
126 1.4 jtc int
127 1.5 jtc #ifdef BUILTIN
128 1.5 jtc progprintf(argc, argv)
129 1.5 jtc #else
130 1.23 wiz main(int argc, char **argv)
131 1.5 jtc #endif
132 1.1 cgd {
133 1.18 lukem char *fmt, *start;
134 1.18 lukem int fieldwidth, precision;
135 1.6 jtc char convch, nextch;
136 1.4 jtc char *format;
137 1.5 jtc int ch;
138 1.4 jtc
139 1.5 jtc #if !defined(SHELL) && !defined(BUILTIN)
140 1.15 cgd (void)setlocale (LC_ALL, "");
141 1.5 jtc #endif
142 1.1 cgd
143 1.5 jtc while ((ch = getopt(argc, argv, "")) != -1) {
144 1.5 jtc switch (ch) {
145 1.5 jtc case '?':
146 1.5 jtc default:
147 1.5 jtc usage();
148 1.5 jtc return (1);
149 1.5 jtc }
150 1.1 cgd }
151 1.5 jtc argc -= optind;
152 1.5 jtc argv += optind;
153 1.1 cgd
154 1.5 jtc if (argc < 1) {
155 1.5 jtc usage();
156 1.5 jtc return (1);
157 1.5 jtc }
158 1.5 jtc
159 1.5 jtc format = *argv;
160 1.5 jtc gargv = ++argv;
161 1.4 jtc
162 1.7 jtc #define SKIP1 "#-+ 0"
163 1.7 jtc #define SKIP2 "*0123456789"
164 1.4 jtc do {
165 1.6 jtc /*
166 1.6 jtc * Basic algorithm is to scan the format string for conversion
167 1.6 jtc * specifications -- once one is found, find out if the field
168 1.6 jtc * width or precision is a '*'; if it is, gather up value.
169 1.6 jtc * Note, format strings are reused as necessary to use up the
170 1.6 jtc * provided arguments, arguments of zero/null string are
171 1.6 jtc * provided to use up the format string.
172 1.6 jtc */
173 1.6 jtc
174 1.6 jtc /* find next format specification */
175 1.6 jtc for (fmt = format; *fmt; fmt++) {
176 1.6 jtc switch (*fmt) {
177 1.6 jtc case '%':
178 1.6 jtc start = fmt++;
179 1.6 jtc
180 1.6 jtc if (*fmt == '%') {
181 1.15 cgd (void)putchar('%');
182 1.6 jtc break;
183 1.6 jtc } else if (*fmt == 'b') {
184 1.6 jtc char *p = getstr();
185 1.7 jtc if (print_escape_str(p)) {
186 1.7 jtc return (rval);
187 1.7 jtc }
188 1.6 jtc break;
189 1.6 jtc }
190 1.6 jtc
191 1.6 jtc /* skip to field width */
192 1.18 lukem for (; strchr(SKIP1, *fmt); ++fmt) ;
193 1.6 jtc fieldwidth = *fmt == '*' ? getint() : 0;
194 1.6 jtc
195 1.6 jtc /* skip to possible '.', get following precision */
196 1.18 lukem for (; strchr(SKIP2, *fmt); ++fmt) ;
197 1.6 jtc if (*fmt == '.')
198 1.6 jtc ++fmt;
199 1.6 jtc precision = *fmt == '*' ? getint() : 0;
200 1.6 jtc
201 1.18 lukem for (; strchr(SKIP2, *fmt); ++fmt) ;
202 1.6 jtc if (!*fmt) {
203 1.6 jtc warnx ("missing format character");
204 1.10 jtc return(1);
205 1.6 jtc }
206 1.6 jtc
207 1.6 jtc convch = *fmt;
208 1.6 jtc nextch = *(fmt + 1);
209 1.6 jtc *(fmt + 1) = '\0';
210 1.6 jtc switch(convch) {
211 1.6 jtc case 'c': {
212 1.6 jtc char p = getchr();
213 1.6 jtc PF(start, p);
214 1.6 jtc break;
215 1.6 jtc }
216 1.6 jtc case 's': {
217 1.6 jtc char *p = getstr();
218 1.6 jtc PF(start, p);
219 1.6 jtc break;
220 1.6 jtc }
221 1.7 jtc case 'd':
222 1.11 jtc case 'i': {
223 1.11 jtc char *f = mklong(start, convch);
224 1.22 kleink intmax_t p = getintmax();
225 1.11 jtc PF(f, p);
226 1.11 jtc break;
227 1.11 jtc }
228 1.7 jtc case 'o':
229 1.7 jtc case 'u':
230 1.7 jtc case 'x':
231 1.7 jtc case 'X': {
232 1.6 jtc char *f = mklong(start, convch);
233 1.22 kleink uintmax_t p = getuintmax();
234 1.6 jtc PF(f, p);
235 1.6 jtc break;
236 1.6 jtc }
237 1.7 jtc case 'e':
238 1.7 jtc case 'E':
239 1.7 jtc case 'f':
240 1.7 jtc case 'g':
241 1.7 jtc case 'G': {
242 1.6 jtc double p = getdouble();
243 1.6 jtc PF(start, p);
244 1.6 jtc break;
245 1.6 jtc }
246 1.6 jtc default:
247 1.6 jtc warnx ("%s: invalid directive", start);
248 1.10 jtc return(1);
249 1.6 jtc }
250 1.6 jtc *(fmt + 1) = nextch;
251 1.6 jtc break;
252 1.4 jtc
253 1.6 jtc case '\\':
254 1.6 jtc fmt += print_escape(fmt);
255 1.4 jtc break;
256 1.4 jtc
257 1.6 jtc default:
258 1.15 cgd (void)putchar(*fmt);
259 1.4 jtc break;
260 1.4 jtc }
261 1.6 jtc }
262 1.6 jtc } while (gargv > argv && *gargv);
263 1.1 cgd
264 1.6 jtc return (rval);
265 1.4 jtc }
266 1.4 jtc
267 1.4 jtc
268 1.4 jtc /*
269 1.4 jtc * Print SysV echo(1) style escape string
270 1.7 jtc * Halts processing string and returns 1 if a \c escape is encountered.
271 1.4 jtc */
272 1.7 jtc static int
273 1.23 wiz print_escape_str(const char *str)
274 1.4 jtc {
275 1.4 jtc int value;
276 1.4 jtc int c;
277 1.4 jtc
278 1.4 jtc while (*str) {
279 1.4 jtc if (*str == '\\') {
280 1.4 jtc str++;
281 1.4 jtc /*
282 1.4 jtc * %b string octal constants are not like those in C.
283 1.4 jtc * They start with a \0, and are followed by 0, 1, 2,
284 1.4 jtc * or 3 octal digits.
285 1.4 jtc */
286 1.4 jtc if (*str == '0') {
287 1.4 jtc str++;
288 1.4 jtc for (c = 3, value = 0; c-- && isodigit(*str); str++) {
289 1.4 jtc value <<= 3;
290 1.4 jtc value += octtobin(*str);
291 1.4 jtc }
292 1.15 cgd (void)putchar(value);
293 1.4 jtc str--;
294 1.4 jtc } else if (*str == 'c') {
295 1.7 jtc return 1;
296 1.4 jtc } else {
297 1.4 jtc str--;
298 1.4 jtc str += print_escape(str);
299 1.4 jtc }
300 1.4 jtc } else {
301 1.15 cgd (void)putchar(*str);
302 1.1 cgd }
303 1.4 jtc str++;
304 1.4 jtc }
305 1.7 jtc
306 1.7 jtc return 0;
307 1.4 jtc }
308 1.4 jtc
309 1.4 jtc /*
310 1.4 jtc * Print "standard" escape characters
311 1.4 jtc */
312 1.15 cgd static size_t
313 1.23 wiz print_escape(const char *str)
314 1.4 jtc {
315 1.7 jtc const char *start = str;
316 1.9 jtc int value;
317 1.4 jtc int c;
318 1.4 jtc
319 1.4 jtc str++;
320 1.4 jtc
321 1.4 jtc switch (*str) {
322 1.4 jtc case '0': case '1': case '2': case '3':
323 1.4 jtc case '4': case '5': case '6': case '7':
324 1.4 jtc for (c = 3, value = 0; c-- && isodigit(*str); str++) {
325 1.4 jtc value <<= 3;
326 1.4 jtc value += octtobin(*str);
327 1.1 cgd }
328 1.15 cgd (void)putchar(value);
329 1.4 jtc return str - start - 1;
330 1.4 jtc /* NOTREACHED */
331 1.4 jtc
332 1.4 jtc case 'x':
333 1.4 jtc str++;
334 1.21 christos for (value = 0; isxdigit((unsigned char)*str); str++) {
335 1.4 jtc value <<= 4;
336 1.4 jtc value += hextobin(*str);
337 1.1 cgd }
338 1.4 jtc if (value > UCHAR_MAX) {
339 1.4 jtc warnx ("escape sequence out of range for character");
340 1.4 jtc rval = 1;
341 1.1 cgd }
342 1.15 cgd (void)putchar (value);
343 1.4 jtc return str - start - 1;
344 1.4 jtc /* NOTREACHED */
345 1.4 jtc
346 1.4 jtc case '\\': /* backslash */
347 1.15 cgd (void)putchar('\\');
348 1.4 jtc break;
349 1.4 jtc
350 1.4 jtc case '\'': /* single quote */
351 1.15 cgd (void)putchar('\'');
352 1.4 jtc break;
353 1.4 jtc
354 1.4 jtc case '"': /* double quote */
355 1.15 cgd (void)putchar('"');
356 1.4 jtc break;
357 1.4 jtc
358 1.4 jtc case 'a': /* alert */
359 1.15 cgd (void)putchar('\a');
360 1.4 jtc break;
361 1.4 jtc
362 1.4 jtc case 'b': /* backspace */
363 1.15 cgd (void)putchar('\b');
364 1.4 jtc break;
365 1.4 jtc
366 1.4 jtc case 'e': /* escape */
367 1.4 jtc #ifdef __GNUC__
368 1.15 cgd (void)putchar('\e');
369 1.4 jtc #else
370 1.15 cgd (void)putchar(033);
371 1.4 jtc #endif
372 1.4 jtc break;
373 1.4 jtc
374 1.4 jtc case 'f': /* form-feed */
375 1.15 cgd (void)putchar('\f');
376 1.4 jtc break;
377 1.4 jtc
378 1.4 jtc case 'n': /* newline */
379 1.15 cgd (void)putchar('\n');
380 1.4 jtc break;
381 1.4 jtc
382 1.4 jtc case 'r': /* carriage-return */
383 1.15 cgd (void)putchar('\r');
384 1.4 jtc break;
385 1.4 jtc
386 1.4 jtc case 't': /* tab */
387 1.15 cgd (void)putchar('\t');
388 1.4 jtc break;
389 1.4 jtc
390 1.4 jtc case 'v': /* vertical-tab */
391 1.15 cgd (void)putchar('\v');
392 1.4 jtc break;
393 1.4 jtc
394 1.4 jtc default:
395 1.15 cgd (void)putchar(*str);
396 1.4 jtc warnx("unknown escape sequence `\\%c'", *str);
397 1.4 jtc rval = 1;
398 1.15 cgd break;
399 1.1 cgd }
400 1.4 jtc
401 1.4 jtc return 1;
402 1.1 cgd }
403 1.1 cgd
404 1.5 jtc static char *
405 1.23 wiz mklong(const char *str, int ch)
406 1.1 cgd {
407 1.5 jtc static char copy[64];
408 1.15 cgd size_t len;
409 1.1 cgd
410 1.1 cgd len = strlen(str) + 2;
411 1.15 cgd (void)memmove(copy, str, len - 3);
412 1.22 kleink copy[len - 3] = 'j';
413 1.1 cgd copy[len - 2] = ch;
414 1.1 cgd copy[len - 1] = '\0';
415 1.5 jtc return (copy);
416 1.1 cgd }
417 1.1 cgd
418 1.5 jtc static int
419 1.23 wiz getchr(void)
420 1.1 cgd {
421 1.1 cgd if (!*gargv)
422 1.17 mrg return ('\0');
423 1.17 mrg return ((int)**gargv++);
424 1.1 cgd }
425 1.1 cgd
426 1.5 jtc static char *
427 1.23 wiz getstr(void)
428 1.1 cgd {
429 1.1 cgd if (!*gargv)
430 1.17 mrg return ("");
431 1.17 mrg return (*gargv++);
432 1.1 cgd }
433 1.1 cgd
434 1.17 mrg static char *Number = "+-.0123456789";
435 1.5 jtc static int
436 1.23 wiz getint(void)
437 1.1 cgd {
438 1.1 cgd if (!*gargv)
439 1.1 cgd return(0);
440 1.4 jtc
441 1.18 lukem if (strchr(Number, **gargv))
442 1.1 cgd return(atoi(*gargv++));
443 1.4 jtc
444 1.4 jtc return 0;
445 1.1 cgd }
446 1.1 cgd
447 1.22 kleink static intmax_t
448 1.23 wiz getintmax(void)
449 1.1 cgd {
450 1.22 kleink intmax_t val;
451 1.4 jtc char *ep;
452 1.1 cgd
453 1.1 cgd if (!*gargv)
454 1.22 kleink return(INTMAX_C(0));
455 1.4 jtc
456 1.13 jtc if (**gargv == '\"' || **gargv == '\'')
457 1.22 kleink return (intmax_t) *((*gargv++)+1);
458 1.4 jtc
459 1.11 jtc errno = 0;
460 1.22 kleink val = strtoimax (*gargv, &ep, 0);
461 1.12 jtc check_conversion(*gargv++, ep);
462 1.11 jtc return val;
463 1.11 jtc }
464 1.11 jtc
465 1.22 kleink static uintmax_t
466 1.23 wiz getuintmax(void)
467 1.11 jtc {
468 1.22 kleink uintmax_t val;
469 1.11 jtc char *ep;
470 1.11 jtc
471 1.11 jtc if (!*gargv)
472 1.22 kleink return(UINTMAX_C(0));
473 1.11 jtc
474 1.13 jtc if (**gargv == '\"' || **gargv == '\'')
475 1.22 kleink return (uintmax_t) *((*gargv++)+1);
476 1.11 jtc
477 1.11 jtc errno = 0;
478 1.22 kleink val = strtoumax (*gargv, &ep, 0);
479 1.12 jtc check_conversion(*gargv++, ep);
480 1.4 jtc return val;
481 1.1 cgd }
482 1.1 cgd
483 1.5 jtc static double
484 1.23 wiz getdouble(void)
485 1.1 cgd {
486 1.4 jtc double val;
487 1.4 jtc char *ep;
488 1.1 cgd
489 1.1 cgd if (!*gargv)
490 1.4 jtc return(0.0);
491 1.4 jtc
492 1.13 jtc if (**gargv == '\"' || **gargv == '\'')
493 1.13 jtc return (double) *((*gargv++)+1);
494 1.1 cgd
495 1.11 jtc errno = 0;
496 1.4 jtc val = strtod (*gargv, &ep);
497 1.12 jtc check_conversion(*gargv++, ep);
498 1.12 jtc return val;
499 1.12 jtc }
500 1.12 jtc
501 1.12 jtc static void
502 1.23 wiz check_conversion(const char *s, const char *ep)
503 1.12 jtc {
504 1.4 jtc if (*ep) {
505 1.12 jtc if (ep == s)
506 1.12 jtc warnx ("%s: expected numeric value", s);
507 1.11 jtc else
508 1.12 jtc warnx ("%s: not completely converted", s);
509 1.11 jtc rval = 1;
510 1.11 jtc } else if (errno == ERANGE) {
511 1.12 jtc warnx ("%s: %s", s, strerror(ERANGE));
512 1.4 jtc rval = 1;
513 1.4 jtc }
514 1.5 jtc }
515 1.5 jtc
516 1.5 jtc static void
517 1.23 wiz usage(void)
518 1.5 jtc {
519 1.5 jtc (void)fprintf(stderr, "usage: printf format [arg ...]\n");
520 1.1 cgd }
521