iso9660fs: initialize buffer cache
[minix.git] / commands / printf / printf.c
blob532f64dab4a88db89bfbad7404bf8120843014e6
1 /* $NetBSD: printf.c,v 1.33.8.1 2009/10/14 18:37:30 sborrill Exp $ */
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 #include <sys/types.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <inttypes.h>
40 #include <limits.h>
41 #include <locale.h>
42 #include <stdarg.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <inttypes.h>
49 #ifdef __GNUC__
50 #define ESCAPE '\e'
51 #else
52 #define ESCAPE 033
53 #endif
55 static void conv_escape_str(char *, void (*)(int));
56 static char *conv_escape(char *, char *);
57 static char *conv_expand(const char *);
58 static int getchr(void);
59 static double getdouble(void);
60 static int getwidth(void);
61 static intmax_t getintmax(void);
62 static uintmax_t getuintmax(void);
63 static char *getstr(void);
64 static char *mklong(const char *, int);
65 static void check_conversion(const char *, const char *);
66 static void usage(void);
68 static void b_count(int);
69 static void b_output(int);
70 static size_t b_length;
71 static char *b_fmt;
73 static int rval;
74 static char **gargv;
76 #ifdef BUILTIN /* csh builtin */
77 #define main progprintf
78 #endif
80 #ifdef SHELL /* sh (aka ash) builtin */
81 #define main printfcmd
82 #include "../../bin/sh/bltin/bltin.h"
83 #endif /* SHELL */
85 #define PF(f, func) { \
86 if (fieldwidth != -1) { \
87 if (precision != -1) \
88 error = printf(f, fieldwidth, precision, func); \
89 else \
90 error = printf(f, fieldwidth, func); \
91 } else if (precision != -1) \
92 error = printf(f, precision, func); \
93 else \
94 error = printf(f, func); \
97 #if 0
98 #define APF(cpp, f, func) { \
99 if (fieldwidth != -1) { \
100 if (precision != -1) \
101 error = asprintf(cpp, f, fieldwidth, precision, func); \
102 else \
103 error = asprintf(cpp, f, fieldwidth, func); \
104 } else if (precision != -1) \
105 error = asprintf(cpp, f, precision, func); \
106 else \
107 error = asprintf(cpp, f, func); \
109 #endif
111 #ifdef main
112 int main(int, char *[]);
113 #endif
114 int main(int argc, char *argv[])
116 char *fmt, *start;
117 int fieldwidth, precision;
118 char nextch;
119 char *format;
120 int ch;
121 int error;
123 #if !defined(SHELL) && !defined(BUILTIN)
124 (void)setlocale (LC_ALL, "");
125 #endif
127 while ((ch = getopt(argc, argv, "")) != -1) {
128 switch (ch) {
129 case '?':
130 default:
131 usage();
132 return 1;
135 argc -= optind;
136 argv += optind;
138 if (argc < 1) {
139 usage();
140 return 1;
143 format = *argv;
144 gargv = ++argv;
146 #define SKIP1 "#-+ 0"
147 #define SKIP2 "0123456789"
148 do {
150 * Basic algorithm is to scan the format string for conversion
151 * specifications -- once one is found, find out if the field
152 * width or precision is a '*'; if it is, gather up value.
153 * Note, format strings are reused as necessary to use up the
154 * provided arguments, arguments of zero/null string are
155 * provided to use up the format string.
158 /* find next format specification */
159 for (fmt = format; (ch = *fmt++) != '\0';) {
160 if (ch == '\\') {
161 char c_ch;
162 fmt = conv_escape(fmt, &c_ch);
163 putchar(c_ch);
164 continue;
166 if (ch != '%' || (*fmt == '%' && ++fmt)) {
167 (void)putchar(ch);
168 continue;
171 /* Ok - we've found a format specification,
172 Save its address for a later printf(). */
173 start = fmt - 1;
175 /* skip to field width */
176 fmt += strspn(fmt, SKIP1);
177 if (*fmt == '*') {
178 fmt++;
179 fieldwidth = getwidth();
180 } else
181 fieldwidth = -1;
183 /* skip to possible '.', get following precision */
184 fmt += strspn(fmt, SKIP2);
185 if (*fmt == '.') {
186 fmt++;
187 if (*fmt == '*') {
188 fmt++;
189 precision = getwidth();
190 } else
191 precision = -1;
192 } else
193 precision = -1;
195 fmt += strspn(fmt, SKIP2);
197 ch = *fmt;
198 if (!ch) {
199 warnx("missing format character");
200 return (1);
202 /* null terminate format string to we can use it
203 as an argument to printf. */
204 nextch = fmt[1];
205 fmt[1] = 0;
206 switch (ch) {
208 case 'B': {
209 const char *p = conv_expand(getstr());
210 if (p == NULL)
211 goto out;
212 *fmt = 's';
213 PF(start, p);
214 if (error < 0)
215 goto out;
216 break;
218 case 'b': {
219 /* There has to be a better way to do this,
220 * but the string we generate might have
221 * embedded nulls. */
222 static char *a, *t;
223 char *cp = getstr();
224 /* Free on entry in case shell longjumped out */
225 if (a != NULL)
226 free(a);
227 a = NULL;
228 if (t != NULL)
229 free(t);
230 t = NULL;
231 /* Count number of bytes we want to output */
232 b_length = 0;
233 conv_escape_str(cp, b_count);
234 t = malloc(b_length + 1);
235 if (t == NULL)
236 goto out;
237 (void)memset(t, 'x', b_length);
238 t[b_length] = 0;
239 /* Get printf to calculate the lengths */
240 *fmt = 's';
241 abort();
242 /* APF(&a, start, t); */
243 if (error == -1)
244 goto out;
245 b_fmt = a;
246 /* Output leading spaces and data bytes */
247 conv_escape_str(cp, b_output);
248 /* Add any trailing spaces */
249 printf("%s", b_fmt);
250 break;
252 case 'c': {
253 char p = getchr();
254 PF(start, p);
255 if (error < 0)
256 goto out;
257 break;
259 case 's': {
260 char *p = getstr();
261 PF(start, p);
262 if (error < 0)
263 goto out;
264 break;
266 case 'd':
267 case 'i': {
268 intmax_t p = getintmax();
269 char *f = mklong(start, ch);
270 PF(f, p);
271 if (error < 0)
272 goto out;
273 break;
275 case 'o':
276 case 'u':
277 case 'x':
278 case 'X': {
279 uintmax_t p = getuintmax();
280 char *f = mklong(start, ch);
281 PF(f, p);
282 if (error < 0)
283 goto out;
284 break;
286 case 'e':
287 case 'E':
288 case 'f':
289 case 'g':
290 case 'G': {
291 double p = getdouble();
292 PF(start, p);
293 if (error < 0)
294 goto out;
295 break;
297 default:
298 warnx("%s: invalid directive", start);
299 return 1;
301 *fmt++ = ch;
302 *fmt = nextch;
303 /* escape if a \c was encountered */
304 if (rval & 0x100)
305 return rval & ~0x100;
307 } while (gargv != argv && *gargv);
309 return rval & ~0x100;
310 out:
311 warn("print failed");
312 return 1;
315 /* helper functions for conv_escape_str */
317 static void
318 /*ARGSUSED*/
319 b_count(int ch)
321 b_length++;
324 /* Output one converted character for every 'x' in the 'format' */
326 static void
327 b_output(int ch)
329 for (;;) {
330 switch (*b_fmt++) {
331 case 0:
332 b_fmt--;
333 return;
334 case ' ':
335 putchar(' ');
336 break;
337 default:
338 putchar(ch);
339 return;
346 * Print SysV echo(1) style escape string
347 * Halts processing string if a \c escape is encountered.
349 static void
350 conv_escape_str(char *str, void (*do_putchar)(int))
352 int value;
353 int ch;
354 char c;
356 while ((ch = *str++) != '\0') {
357 if (ch != '\\') {
358 do_putchar(ch);
359 continue;
362 ch = *str++;
363 if (ch == 'c') {
364 /* \c as in SYSV echo - abort all processing.... */
365 rval |= 0x100;
366 break;
370 * %b string octal constants are not like those in C.
371 * They start with a \0, and are followed by 0, 1, 2,
372 * or 3 octal digits.
374 if (ch == '0') {
375 int octnum = 0, i;
376 for (i = 0; i < 3; i++) {
377 if (!isdigit((unsigned char)*str) || *str > '7')
378 break;
379 octnum = (octnum << 3) | (*str++ - '0');
381 do_putchar(octnum);
382 continue;
385 /* \[M][^|-]C as defined by vis(3) */
386 if (ch == 'M' && *str == '-') {
387 do_putchar(0200 | str[1]);
388 str += 2;
389 continue;
391 if (ch == 'M' && *str == '^') {
392 str++;
393 value = 0200;
394 ch = '^';
395 } else
396 value = 0;
397 if (ch == '^') {
398 ch = *str++;
399 if (ch == '?')
400 value |= 0177;
401 else
402 value |= ch & 037;
403 do_putchar(value);
404 continue;
407 /* Finally test for sequences valid in the format string */
408 str = conv_escape(str - 1, &c);
409 do_putchar(c);
414 * Print "standard" escape characters
416 static char *
417 conv_escape(char *str, char *conv_ch)
419 int value;
420 int ch;
421 char num_buf[4], *num_end;
423 ch = *str++;
425 switch (ch) {
426 case '0': case '1': case '2': case '3':
427 case '4': case '5': case '6': case '7':
428 num_buf[0] = ch;
429 ch = str[0];
430 num_buf[1] = ch;
431 num_buf[2] = ch ? str[1] : 0;
432 num_buf[3] = 0;
433 value = strtoul(num_buf, &num_end, 8);
434 str += num_end - (num_buf + 1);
435 break;
437 case 'x':
438 /* Hexadecimal character constants are not required to be
439 supported (by SuS v1) because there is no consistent
440 way to detect the end of the constant.
441 Supporting 2 byte constants is a compromise. */
442 ch = str[0];
443 num_buf[0] = ch;
444 num_buf[1] = ch ? str[1] : 0;
445 num_buf[2] = 0;
446 value = strtoul(num_buf, &num_end, 16);
447 str += num_end - num_buf;
448 break;
450 case '\\': value = '\\'; break; /* backslash */
451 case '\'': value = '\''; break; /* single quote */
452 case '"': value = '"'; break; /* double quote */
453 case 'a': value = '\a'; break; /* alert */
454 case 'b': value = '\b'; break; /* backspace */
455 case 'e': value = ESCAPE; break; /* escape */
456 case 'f': value = '\f'; break; /* form-feed */
457 case 'n': value = '\n'; break; /* newline */
458 case 'r': value = '\r'; break; /* carriage-return */
459 case 't': value = '\t'; break; /* tab */
460 case 'v': value = '\v'; break; /* vertical-tab */
462 default:
463 warnx("unknown escape sequence `\\%c'", ch);
464 rval = 1;
465 value = ch;
466 break;
469 *conv_ch = value;
470 return str;
473 /* expand a string so that everything is printable */
475 static char *
476 conv_expand(const char *str)
478 static char *conv_str;
479 char *cp;
480 int ch;
482 if (conv_str)
483 free(conv_str);
484 /* get a buffer that is definitely large enough.... */
485 conv_str = malloc(4 * strlen(str) + 1);
486 if (!conv_str)
487 return NULL;
488 cp = conv_str;
490 while ((ch = *(const unsigned char *)str++) != '\0') {
491 switch (ch) {
492 /* Use C escapes for expected control characters */
493 case '\\': ch = '\\'; break; /* backslash */
494 case '\'': ch = '\''; break; /* single quote */
495 case '"': ch = '"'; break; /* double quote */
496 case '\a': ch = 'a'; break; /* alert */
497 case '\b': ch = 'b'; break; /* backspace */
498 case ESCAPE: ch = 'e'; break; /* escape */
499 case '\f': ch = 'f'; break; /* form-feed */
500 case '\n': ch = 'n'; break; /* newline */
501 case '\r': ch = 'r'; break; /* carriage-return */
502 case '\t': ch = 't'; break; /* tab */
503 case '\v': ch = 'v'; break; /* vertical-tab */
504 default:
505 /* Copy anything printable */
506 if (isprint(ch)) {
507 *cp++ = ch;
508 continue;
510 /* Use vis(3) encodings for the rest */
511 *cp++ = '\\';
512 if (ch & 0200) {
513 *cp++ = 'M';
514 ch &= ~0200;
516 if (ch == 0177) {
517 *cp++ = '^';
518 *cp++ = '?';
519 continue;
521 if (ch < 040) {
522 *cp++ = '^';
523 *cp++ = ch | 0100;
524 continue;
526 *cp++ = '-';
527 *cp++ = ch;
528 continue;
530 *cp++ = '\\';
531 *cp++ = ch;
534 *cp = 0;
535 return conv_str;
538 static char *
539 mklong(const char *str, int ch)
541 static char copy[64];
542 size_t len;
544 len = strlen(str) + 2;
545 if (len > sizeof copy) {
546 warnx("format %s too complex\n", str);
547 len = 4;
549 (void)memmove(copy, str, len - 3);
550 copy[len - 3] = 'j';
551 copy[len - 2] = ch;
552 copy[len - 1] = '\0';
553 return copy;
556 static int
557 getchr(void)
559 if (!*gargv)
560 return 0;
561 return (int)**gargv++;
564 static char *
565 getstr(void)
567 static char empty[] = "";
568 if (!*gargv)
569 return empty;
570 return *gargv++;
573 static int
574 getwidth(void)
576 long val;
577 char *s, *ep;
579 s = *gargv;
580 if (!*gargv)
581 return (0);
582 gargv++;
584 errno = 0;
585 val = strtoul(s, &ep, 0);
586 check_conversion(s, ep);
588 /* Arbitrarily 'restrict' field widths to 1Mbyte */
589 if (val < 0 || val > 1 << 20) {
590 warnx("%s: invalid field width", s);
591 return 0;
594 return val;
597 static intmax_t
598 getintmax(void)
600 intmax_t val;
601 char *cp, *ep;
603 cp = *gargv;
604 if (cp == NULL)
605 return 0;
606 gargv++;
608 if (*cp == '\"' || *cp == '\'')
609 return *(cp+1);
611 errno = 0;
612 val = strtol(cp, &ep, 0);
613 check_conversion(cp, ep);
614 return val;
617 static uintmax_t
618 getuintmax(void)
620 uintmax_t val;
621 char *cp, *ep;
623 cp = *gargv;
624 if (cp == NULL)
625 return 0;
626 gargv++;
628 if (*cp == '\"' || *cp == '\'')
629 return *(cp + 1);
631 /* strtoumax won't error -ve values */
632 while (isspace(*(unsigned char *)cp))
633 cp++;
634 if (*cp == '-') {
635 warnx("%s: expected positive numeric value", cp);
636 rval = 1;
637 return 0;
640 errno = 0;
641 val = strtoul(cp, &ep, 0);
642 check_conversion(cp, ep);
643 return val;
646 static double
647 getdouble(void)
649 double val;
650 char *ep;
652 if (!*gargv)
653 return (0.0);
655 if (**gargv == '\"' || **gargv == '\'')
656 return (double) *((*gargv++)+1);
658 errno = 0;
659 val = strtod(*gargv, &ep);
660 check_conversion(*gargv++, ep);
661 return val;
664 static void
665 check_conversion(const char *s, const char *ep)
667 if (*ep) {
668 if (ep == s)
669 warnx("%s: expected numeric value", s);
670 else
671 warnx("%s: not completely converted", s);
672 rval = 1;
673 } else if (errno == ERANGE) {
674 warnx("%s: %s", s, strerror(ERANGE));
675 rval = 1;
679 static void
680 usage(void)
682 (void)fprintf(stderr, "Usage: %s format [arg ...]\n", getprogname());