printf.c revision 1.15 1 1.15 cgd /* $NetBSD: printf.c,v 1.15 1997/01/14 19:20:09 cgd Exp $ */
2 1.14 tls
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
5 1.1 cgd * 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.1 cgd #ifndef lint
37 1.5 jtc #if !defined(SHELL) && !defined(BUILTIN)
38 1.1 cgd char copyright[] =
39 1.1 cgd "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
40 1.1 cgd All rights reserved.\n";
41 1.5 jtc #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #ifndef lint
45 1.3 mycroft /*static char sccsid[] = "from: @(#)printf.c 5.9 (Berkeley) 6/1/90";*/
46 1.15 cgd static char rcsid[] = "$NetBSD: printf.c,v 1.15 1997/01/14 19:20:09 cgd Exp $";
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.4 jtc #include <ctype.h>
50 1.1 cgd #include <stdio.h>
51 1.4 jtc #include <stdlib.h>
52 1.2 mycroft #include <string.h>
53 1.4 jtc #include <limits.h>
54 1.4 jtc #include <locale.h>
55 1.11 jtc #include <errno.h>
56 1.4 jtc #include <err.h>
57 1.4 jtc
58 1.7 jtc static int print_escape_str __P((const char *));
59 1.15 cgd static size_t print_escape __P((const char *));
60 1.5 jtc
61 1.5 jtc static int getchr __P((void));
62 1.5 jtc static double getdouble __P((void));
63 1.5 jtc static int getint __P((void));
64 1.5 jtc static long getlong __P((void));
65 1.11 jtc static unsigned long getulong __P ((void));
66 1.5 jtc static char *getstr __P((void));
67 1.12 jtc static char *mklong __P((const char *, int));
68 1.12 jtc static void check_conversion __P((const char *, const char *));
69 1.5 jtc static void usage __P((void));
70 1.4 jtc
71 1.5 jtc static int rval;
72 1.5 jtc static char **gargv;
73 1.4 jtc
74 1.4 jtc #define isodigit(c) ((c) >= '0' && (c) <= '7')
75 1.4 jtc #define octtobin(c) ((c) - '0')
76 1.9 jtc #define hextobin(c) ((c) >= 'A' && (c) <= 'F' ? c - 'A' + 10 : (c) >= 'a' && (c) <= 'f' ? c - 'a' + 10 : c - '0')
77 1.1 cgd
78 1.5 jtc #ifdef SHELL
79 1.5 jtc #define main printfcmd
80 1.5 jtc #include "../../bin/sh/bltin/bltin.h"
81 1.5 jtc
82 1.5 jtc #ifdef __STDC__
83 1.5 jtc #include <stdarg.h>
84 1.5 jtc #else
85 1.5 jtc #include <vararg.h>
86 1.5 jtc #endif
87 1.5 jtc
88 1.5 jtc static void
89 1.5 jtc #ifdef __STDC__
90 1.5 jtc warnx(const char *fmt, ...)
91 1.5 jtc #else
92 1.5 jtc warnx(fmt, va_alist)
93 1.5 jtc const char *fmt;
94 1.5 jtc va_dcl
95 1.5 jtc #endif
96 1.5 jtc {
97 1.5 jtc
98 1.5 jtc char buf[64];
99 1.5 jtc va_list ap;
100 1.5 jtc
101 1.5 jtc #ifdef __STDC__
102 1.5 jtc va_start(ap, fmt);
103 1.5 jtc #else
104 1.5 jtc va_start(ap);
105 1.5 jtc #endif
106 1.5 jtc vsprintf(buf, fmt, ap);
107 1.5 jtc va_end(ap);
108 1.5 jtc
109 1.5 jtc error(buf);
110 1.5 jtc }
111 1.5 jtc #endif /* SHELL */
112 1.5 jtc
113 1.1 cgd #define PF(f, func) { \
114 1.1 cgd if (fieldwidth) \
115 1.1 cgd if (precision) \
116 1.1 cgd (void)printf(f, fieldwidth, precision, func); \
117 1.1 cgd else \
118 1.1 cgd (void)printf(f, fieldwidth, func); \
119 1.1 cgd else if (precision) \
120 1.1 cgd (void)printf(f, precision, func); \
121 1.1 cgd else \
122 1.1 cgd (void)printf(f, func); \
123 1.1 cgd }
124 1.1 cgd
125 1.4 jtc int
126 1.5 jtc #ifdef BUILTIN
127 1.5 jtc progprintf(argc, argv)
128 1.5 jtc #else
129 1.1 cgd main(argc, argv)
130 1.5 jtc #endif
131 1.1 cgd int argc;
132 1.1 cgd char **argv;
133 1.1 cgd {
134 1.6 jtc register char *fmt, *start;
135 1.6 jtc register int fieldwidth, precision;
136 1.6 jtc char convch, nextch;
137 1.4 jtc char *format;
138 1.5 jtc int ch;
139 1.4 jtc
140 1.5 jtc #if !defined(SHELL) && !defined(BUILTIN)
141 1.15 cgd (void)setlocale (LC_ALL, "");
142 1.5 jtc #endif
143 1.1 cgd
144 1.5 jtc while ((ch = getopt(argc, argv, "")) != -1) {
145 1.5 jtc switch (ch) {
146 1.5 jtc case '?':
147 1.5 jtc default:
148 1.5 jtc usage();
149 1.5 jtc return (1);
150 1.5 jtc }
151 1.1 cgd }
152 1.5 jtc argc -= optind;
153 1.5 jtc argv += optind;
154 1.1 cgd
155 1.5 jtc if (argc < 1) {
156 1.5 jtc usage();
157 1.5 jtc return (1);
158 1.5 jtc }
159 1.5 jtc
160 1.5 jtc format = *argv;
161 1.5 jtc gargv = ++argv;
162 1.4 jtc
163 1.7 jtc #define SKIP1 "#-+ 0"
164 1.7 jtc #define SKIP2 "*0123456789"
165 1.4 jtc do {
166 1.6 jtc /*
167 1.6 jtc * Basic algorithm is to scan the format string for conversion
168 1.6 jtc * specifications -- once one is found, find out if the field
169 1.6 jtc * width or precision is a '*'; if it is, gather up value.
170 1.6 jtc * Note, format strings are reused as necessary to use up the
171 1.6 jtc * provided arguments, arguments of zero/null string are
172 1.6 jtc * provided to use up the format string.
173 1.6 jtc */
174 1.6 jtc
175 1.6 jtc /* find next format specification */
176 1.6 jtc for (fmt = format; *fmt; fmt++) {
177 1.6 jtc switch (*fmt) {
178 1.6 jtc case '%':
179 1.6 jtc start = fmt++;
180 1.6 jtc
181 1.6 jtc if (*fmt == '%') {
182 1.15 cgd (void)putchar('%');
183 1.6 jtc break;
184 1.6 jtc } else if (*fmt == 'b') {
185 1.6 jtc char *p = getstr();
186 1.7 jtc if (print_escape_str(p)) {
187 1.7 jtc return (rval);
188 1.7 jtc }
189 1.6 jtc break;
190 1.6 jtc }
191 1.6 jtc
192 1.6 jtc /* skip to field width */
193 1.9 jtc for (; index(SKIP1, *fmt); ++fmt) ;
194 1.6 jtc fieldwidth = *fmt == '*' ? getint() : 0;
195 1.6 jtc
196 1.6 jtc /* skip to possible '.', get following precision */
197 1.9 jtc for (; index(SKIP2, *fmt); ++fmt) ;
198 1.6 jtc if (*fmt == '.')
199 1.6 jtc ++fmt;
200 1.6 jtc precision = *fmt == '*' ? getint() : 0;
201 1.6 jtc
202 1.9 jtc for (; index(SKIP2, *fmt); ++fmt) ;
203 1.6 jtc if (!*fmt) {
204 1.6 jtc warnx ("missing format character");
205 1.10 jtc return(1);
206 1.6 jtc }
207 1.6 jtc
208 1.6 jtc convch = *fmt;
209 1.6 jtc nextch = *(fmt + 1);
210 1.6 jtc *(fmt + 1) = '\0';
211 1.6 jtc switch(convch) {
212 1.6 jtc case 'c': {
213 1.6 jtc char p = getchr();
214 1.6 jtc PF(start, p);
215 1.6 jtc break;
216 1.6 jtc }
217 1.6 jtc case 's': {
218 1.6 jtc char *p = getstr();
219 1.6 jtc PF(start, p);
220 1.6 jtc break;
221 1.6 jtc }
222 1.7 jtc case 'd':
223 1.11 jtc case 'i': {
224 1.11 jtc char *f = mklong(start, convch);
225 1.11 jtc long p = getlong();
226 1.11 jtc PF(f, p);
227 1.11 jtc break;
228 1.11 jtc }
229 1.7 jtc case 'o':
230 1.7 jtc case 'u':
231 1.7 jtc case 'x':
232 1.7 jtc case 'X': {
233 1.6 jtc char *f = mklong(start, convch);
234 1.11 jtc unsigned long p = getulong();
235 1.6 jtc PF(f, p);
236 1.6 jtc break;
237 1.6 jtc }
238 1.7 jtc case 'e':
239 1.7 jtc case 'E':
240 1.7 jtc case 'f':
241 1.7 jtc case 'g':
242 1.7 jtc case 'G': {
243 1.6 jtc double p = getdouble();
244 1.6 jtc PF(start, p);
245 1.6 jtc break;
246 1.6 jtc }
247 1.6 jtc default:
248 1.6 jtc warnx ("%s: invalid directive", start);
249 1.10 jtc return(1);
250 1.6 jtc }
251 1.6 jtc *(fmt + 1) = nextch;
252 1.6 jtc break;
253 1.4 jtc
254 1.6 jtc case '\\':
255 1.6 jtc fmt += print_escape(fmt);
256 1.4 jtc break;
257 1.4 jtc
258 1.6 jtc default:
259 1.15 cgd (void)putchar(*fmt);
260 1.4 jtc break;
261 1.4 jtc }
262 1.6 jtc }
263 1.6 jtc } while (gargv > argv && *gargv);
264 1.1 cgd
265 1.6 jtc return (rval);
266 1.4 jtc }
267 1.4 jtc
268 1.4 jtc
269 1.4 jtc /*
270 1.4 jtc * Print SysV echo(1) style escape string
271 1.7 jtc * Halts processing string and returns 1 if a \c escape is encountered.
272 1.4 jtc */
273 1.7 jtc static int
274 1.4 jtc print_escape_str(str)
275 1.7 jtc register const char *str;
276 1.4 jtc {
277 1.4 jtc int value;
278 1.4 jtc int c;
279 1.4 jtc
280 1.4 jtc while (*str) {
281 1.4 jtc if (*str == '\\') {
282 1.4 jtc str++;
283 1.4 jtc /*
284 1.4 jtc * %b string octal constants are not like those in C.
285 1.4 jtc * They start with a \0, and are followed by 0, 1, 2,
286 1.4 jtc * or 3 octal digits.
287 1.4 jtc */
288 1.4 jtc if (*str == '0') {
289 1.4 jtc str++;
290 1.4 jtc for (c = 3, value = 0; c-- && isodigit(*str); str++) {
291 1.4 jtc value <<= 3;
292 1.4 jtc value += octtobin(*str);
293 1.4 jtc }
294 1.15 cgd (void)putchar(value);
295 1.4 jtc str--;
296 1.4 jtc } else if (*str == 'c') {
297 1.7 jtc return 1;
298 1.4 jtc } else {
299 1.4 jtc str--;
300 1.4 jtc str += print_escape(str);
301 1.4 jtc }
302 1.4 jtc } else {
303 1.15 cgd (void)putchar(*str);
304 1.1 cgd }
305 1.4 jtc str++;
306 1.4 jtc }
307 1.7 jtc
308 1.7 jtc return 0;
309 1.4 jtc }
310 1.4 jtc
311 1.4 jtc /*
312 1.4 jtc * Print "standard" escape characters
313 1.4 jtc */
314 1.15 cgd static size_t
315 1.4 jtc print_escape(str)
316 1.7 jtc register const char *str;
317 1.4 jtc {
318 1.7 jtc const char *start = str;
319 1.9 jtc int value;
320 1.4 jtc int c;
321 1.4 jtc
322 1.4 jtc str++;
323 1.4 jtc
324 1.4 jtc switch (*str) {
325 1.4 jtc case '0': case '1': case '2': case '3':
326 1.4 jtc case '4': case '5': case '6': case '7':
327 1.4 jtc for (c = 3, value = 0; c-- && isodigit(*str); str++) {
328 1.4 jtc value <<= 3;
329 1.4 jtc value += octtobin(*str);
330 1.1 cgd }
331 1.15 cgd (void)putchar(value);
332 1.4 jtc return str - start - 1;
333 1.4 jtc /* NOTREACHED */
334 1.4 jtc
335 1.4 jtc case 'x':
336 1.4 jtc str++;
337 1.4 jtc for (value = 0; isxdigit(*str); str++) {
338 1.4 jtc value <<= 4;
339 1.4 jtc value += hextobin(*str);
340 1.1 cgd }
341 1.4 jtc if (value > UCHAR_MAX) {
342 1.4 jtc warnx ("escape sequence out of range for character");
343 1.4 jtc rval = 1;
344 1.1 cgd }
345 1.15 cgd (void)putchar (value);
346 1.4 jtc return str - start - 1;
347 1.4 jtc /* NOTREACHED */
348 1.4 jtc
349 1.4 jtc case '\\': /* backslash */
350 1.15 cgd (void)putchar('\\');
351 1.4 jtc break;
352 1.4 jtc
353 1.4 jtc case '\'': /* single quote */
354 1.15 cgd (void)putchar('\'');
355 1.4 jtc break;
356 1.4 jtc
357 1.4 jtc case '"': /* double quote */
358 1.15 cgd (void)putchar('"');
359 1.4 jtc break;
360 1.4 jtc
361 1.4 jtc case 'a': /* alert */
362 1.4 jtc #ifdef __STDC__
363 1.15 cgd (void)putchar('\a');
364 1.4 jtc #else
365 1.15 cgd (void)putchar(007);
366 1.4 jtc #endif
367 1.4 jtc break;
368 1.4 jtc
369 1.4 jtc case 'b': /* backspace */
370 1.15 cgd (void)putchar('\b');
371 1.4 jtc break;
372 1.4 jtc
373 1.4 jtc case 'e': /* escape */
374 1.4 jtc #ifdef __GNUC__
375 1.15 cgd (void)putchar('\e');
376 1.4 jtc #else
377 1.15 cgd (void)putchar(033);
378 1.4 jtc #endif
379 1.4 jtc break;
380 1.4 jtc
381 1.4 jtc case 'f': /* form-feed */
382 1.15 cgd (void)putchar('\f');
383 1.4 jtc break;
384 1.4 jtc
385 1.4 jtc case 'n': /* newline */
386 1.15 cgd (void)putchar('\n');
387 1.4 jtc break;
388 1.4 jtc
389 1.4 jtc case 'r': /* carriage-return */
390 1.15 cgd (void)putchar('\r');
391 1.4 jtc break;
392 1.4 jtc
393 1.4 jtc case 't': /* tab */
394 1.15 cgd (void)putchar('\t');
395 1.4 jtc break;
396 1.4 jtc
397 1.4 jtc case 'v': /* vertical-tab */
398 1.15 cgd (void)putchar('\v');
399 1.4 jtc break;
400 1.4 jtc
401 1.4 jtc default:
402 1.15 cgd (void)putchar(*str);
403 1.4 jtc warnx("unknown escape sequence `\\%c'", *str);
404 1.4 jtc rval = 1;
405 1.15 cgd break;
406 1.1 cgd }
407 1.4 jtc
408 1.4 jtc return 1;
409 1.1 cgd }
410 1.1 cgd
411 1.5 jtc static char *
412 1.1 cgd mklong(str, ch)
413 1.12 jtc const char *str;
414 1.12 jtc char ch;
415 1.1 cgd {
416 1.5 jtc static char copy[64];
417 1.15 cgd size_t len;
418 1.1 cgd
419 1.1 cgd len = strlen(str) + 2;
420 1.15 cgd (void)memmove(copy, str, len - 3);
421 1.1 cgd copy[len - 3] = 'l';
422 1.1 cgd copy[len - 2] = ch;
423 1.1 cgd copy[len - 1] = '\0';
424 1.5 jtc return (copy);
425 1.1 cgd }
426 1.1 cgd
427 1.5 jtc static int
428 1.1 cgd getchr()
429 1.1 cgd {
430 1.1 cgd if (!*gargv)
431 1.1 cgd return((int)'\0');
432 1.1 cgd return((int)**gargv++);
433 1.1 cgd }
434 1.1 cgd
435 1.5 jtc static char *
436 1.1 cgd getstr()
437 1.1 cgd {
438 1.1 cgd if (!*gargv)
439 1.1 cgd return("");
440 1.1 cgd return(*gargv++);
441 1.1 cgd }
442 1.1 cgd
443 1.1 cgd static char *number = "+-.0123456789";
444 1.5 jtc static int
445 1.1 cgd getint()
446 1.1 cgd {
447 1.1 cgd if (!*gargv)
448 1.1 cgd return(0);
449 1.4 jtc
450 1.1 cgd if (index(number, **gargv))
451 1.1 cgd return(atoi(*gargv++));
452 1.4 jtc
453 1.4 jtc return 0;
454 1.1 cgd }
455 1.1 cgd
456 1.5 jtc static long
457 1.1 cgd getlong()
458 1.1 cgd {
459 1.4 jtc long val;
460 1.4 jtc char *ep;
461 1.1 cgd
462 1.1 cgd if (!*gargv)
463 1.4 jtc return(0L);
464 1.4 jtc
465 1.13 jtc if (**gargv == '\"' || **gargv == '\'')
466 1.13 jtc return (long) *((*gargv++)+1);
467 1.4 jtc
468 1.11 jtc errno = 0;
469 1.4 jtc val = strtol (*gargv, &ep, 0);
470 1.12 jtc check_conversion(*gargv++, ep);
471 1.11 jtc return val;
472 1.11 jtc }
473 1.11 jtc
474 1.11 jtc static unsigned long
475 1.11 jtc getulong()
476 1.11 jtc {
477 1.11 jtc unsigned long val;
478 1.11 jtc char *ep;
479 1.11 jtc
480 1.11 jtc if (!*gargv)
481 1.13 jtc return(0UL);
482 1.11 jtc
483 1.13 jtc if (**gargv == '\"' || **gargv == '\'')
484 1.13 jtc return (unsigned long) *((*gargv++)+1);
485 1.11 jtc
486 1.11 jtc errno = 0;
487 1.11 jtc val = strtoul (*gargv, &ep, 0);
488 1.12 jtc check_conversion(*gargv++, ep);
489 1.4 jtc return val;
490 1.1 cgd }
491 1.1 cgd
492 1.5 jtc static double
493 1.1 cgd getdouble()
494 1.1 cgd {
495 1.4 jtc double val;
496 1.4 jtc char *ep;
497 1.1 cgd
498 1.1 cgd if (!*gargv)
499 1.4 jtc return(0.0);
500 1.4 jtc
501 1.13 jtc if (**gargv == '\"' || **gargv == '\'')
502 1.13 jtc return (double) *((*gargv++)+1);
503 1.1 cgd
504 1.11 jtc errno = 0;
505 1.4 jtc val = strtod (*gargv, &ep);
506 1.12 jtc check_conversion(*gargv++, ep);
507 1.12 jtc return val;
508 1.12 jtc }
509 1.12 jtc
510 1.12 jtc static void
511 1.12 jtc check_conversion(s, ep)
512 1.12 jtc const char *s;
513 1.12 jtc const char *ep;
514 1.12 jtc {
515 1.4 jtc if (*ep) {
516 1.12 jtc if (ep == s)
517 1.12 jtc warnx ("%s: expected numeric value", s);
518 1.11 jtc else
519 1.12 jtc warnx ("%s: not completely converted", s);
520 1.11 jtc rval = 1;
521 1.11 jtc } else if (errno == ERANGE) {
522 1.12 jtc warnx ("%s: %s", s, strerror(ERANGE));
523 1.4 jtc rval = 1;
524 1.4 jtc }
525 1.5 jtc }
526 1.5 jtc
527 1.5 jtc static void
528 1.5 jtc usage()
529 1.5 jtc {
530 1.5 jtc (void)fprintf(stderr, "usage: printf format [arg ...]\n");
531 1.1 cgd }
532