Nuke unused macro and comment
[dragonfly.git] / usr.bin / printf / printf.c
blobf077eeb1db043908081a57d9bc2fd37aa52ab6fe
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
33 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)printf.c 8.1 (Berkeley) 7/20/93
35 * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/usr.bin/printf/printf.c,v 1.27 2004/03/07 22:22:13 cperciva Exp $
36 * $DragonFly: src/usr.bin/printf/printf.c,v 1.7 2007/01/21 00:24:55 swildner Exp $
39 #include <sys/types.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
49 #ifdef SHELL
50 #define main printfcmd
51 #include "bltin/bltin.h"
52 #include "memalloc.h"
53 #else
54 #define warnx1(a, b, c) warnx(a)
55 #define warnx2(a, b, c) warnx(a, b)
56 #define warnx3(a, b, c) warnx(a, b, c)
57 #endif
59 #ifndef BUILTIN
60 #include <locale.h>
61 #endif
63 #define PF(f, func) do { \
64 char *b = NULL; \
65 if (fieldwidth) \
66 if (precision) \
67 asprintf(&b, f, fieldwidth, precision, func); \
68 else \
69 asprintf(&b, f, fieldwidth, func); \
70 else if (precision) \
71 asprintf(&b, f, precision, func); \
72 else \
73 asprintf(&b, f, func); \
74 if (b) { \
75 fputs(b, stdout); \
76 free(b); \
77 } \
78 } while (0)
80 static int asciicode(void);
81 static int escape(char *);
82 static int getchr(void);
83 static int getdouble(double *);
84 static int getint(int *);
85 static int getlonglongs(long long *, unsigned long long *, int);
86 static const char
87 *getstr(void);
88 static char *mkquad(char *, int);
89 static void usage(void);
91 static char **gargv;
93 int
94 #ifdef BUILTIN
95 progprintf(int argc, char **argv)
96 #else
97 main(int argc, char **argv)
98 #endif
100 static const char *skip1, *skip2;
101 int ch, chopped, end, fieldwidth, precision, rval;
102 char convch, nextch, *format, *fmt, *start;
104 #ifndef BUILTIN
105 setlocale(LC_NUMERIC, "");
106 #endif
107 while ((ch = getopt(argc, argv, "")) != -1)
108 switch (ch) {
109 case '?':
110 default:
111 usage();
112 return (1);
114 argc -= optind;
115 argv += optind;
117 if (argc < 1) {
118 usage();
119 return (1);
123 * Basic algorithm is to scan the format string for conversion
124 * specifications -- once one is found, find out if the field
125 * width or precision is a '*'; if it is, gather up value. Note,
126 * format strings are reused as necessary to use up the provided
127 * arguments, arguments of zero/null string are provided to use
128 * up the format string.
130 skip1 = "#-+ 0";
131 skip2 = "0123456789";
133 chopped = escape(fmt = format = *argv); /* backslash interpretation */
134 rval = 0;
135 gargv = ++argv;
136 for (;;) {
137 end = 0;
138 /* find next format specification */
139 next: for (start = fmt;; ++fmt) {
140 if (!*fmt) {
141 /* avoid infinite loop */
142 if (chopped) {
143 printf("%s", start);
144 return (rval);
146 if (end == 1) {
147 warnx1("missing format character",
148 NULL, NULL);
149 return (1);
151 end = 1;
152 if (fmt > start)
153 printf("%s", start);
154 if (!*gargv)
155 return (rval);
156 fmt = format;
157 goto next;
159 /* %% prints a % */
160 if (*fmt == '%') {
161 if (*++fmt != '%')
162 break;
163 *fmt++ = '\0';
164 printf("%s", start);
165 goto next;
169 /* skip to field width */
170 for (; strchr(skip1, *fmt); ++fmt);
171 if (*fmt == '*') {
172 if (getint(&fieldwidth))
173 return (1);
174 ++fmt;
175 } else {
176 fieldwidth = 0;
178 /* skip to possible '.', get following precision */
179 for (; strchr(skip2, *fmt); ++fmt);
181 if (*fmt == '.') {
182 /* precision present? */
183 ++fmt;
184 if (*fmt == '*') {
185 if (getint(&precision))
186 return (1);
187 ++fmt;
188 } else {
189 precision = 0;
191 /* skip to conversion char */
192 for (; strchr(skip2, *fmt); ++fmt);
194 } else
195 precision = 0;
196 if (!*fmt) {
197 warnx1("missing format character", NULL, NULL);
198 return (1);
201 convch = *fmt;
202 nextch = *++fmt;
203 *fmt = '\0';
204 switch(convch) {
205 case 'b': {
206 char *p;
207 int getout;
209 #ifdef SHELL
210 p = savestr(getstr());
211 #else
212 p = strdup(getstr());
213 #endif
214 if (p == NULL) {
215 warnx2("%s", strerror(ENOMEM), NULL);
216 return (1);
218 getout = escape(p);
219 *(fmt - 1) = 's';
220 PF(start, p);
221 *(fmt - 1) = 'b';
222 #ifdef SHELL
223 ckfree(p);
224 #else
225 free(p);
226 #endif
227 if (getout)
228 return (rval);
229 break;
231 case 'c': {
232 char p;
234 p = getchr();
235 PF(start, p);
236 break;
238 case 's': {
239 const char *p;
241 p = getstr();
242 PF(start, p);
243 break;
245 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': {
246 char *f;
247 long long val;
248 unsigned long long uval;
249 int signedconv;
251 signedconv = (convch == 'd' || convch == 'i');
252 if ((f = mkquad(start, convch)) == NULL)
253 return (1);
254 if (getlonglongs(&val, &uval, signedconv))
255 rval = 1;
256 if (signedconv)
257 PF(f, val);
258 else
259 PF(f, uval);
260 break;
262 case 'e': case 'E': case 'f': case 'g': case 'G': {
263 double p;
265 if (getdouble(&p))
266 rval = 1;
267 PF(start, p);
268 break;
270 default:
271 warnx2("illegal format character %c", convch, NULL);
272 return (1);
274 *fmt = nextch;
276 /* NOTREACHED */
279 static char *
280 mkquad(char *str, int ch)
282 static char *copy;
283 static size_t copy_size;
284 char *newcopy;
285 size_t len, newlen;
287 len = strlen(str) + 2;
288 if (len > copy_size) {
289 newlen = ((len + 1023) >> 10) << 10;
290 #ifdef SHELL
291 if ((newcopy = ckrealloc(copy, newlen)) == NULL)
292 #else
293 if ((newcopy = realloc(copy, newlen)) == NULL)
294 #endif
296 warnx2("%s", strerror(ENOMEM), NULL);
297 return (NULL);
299 copy = newcopy;
300 copy_size = newlen;
303 memmove(copy, str, len - 3);
304 copy[len - 3] = 'q';
305 copy[len - 2] = ch;
306 copy[len - 1] = '\0';
307 return (copy);
310 static int
311 escape(char *fmt)
313 char *store;
314 int value, c;
316 for (store = fmt; (c = *fmt); ++fmt, ++store) {
317 if (c != '\\') {
318 *store = c;
319 continue;
321 switch (*++fmt) {
322 case '\0': /* EOS, user error */
323 *store = '\\';
324 *++store = '\0';
325 return (0);
326 case '\\': /* backslash */
327 case '\'': /* single quote */
328 *store = *fmt;
329 break;
330 case 'a': /* bell/alert */
331 *store = '\7';
332 break;
333 case 'b': /* backspace */
334 *store = '\b';
335 break;
336 case 'c':
337 *store = '\0';
338 return (1);
339 case 'f': /* form-feed */
340 *store = '\f';
341 break;
342 case 'n': /* newline */
343 *store = '\n';
344 break;
345 case 'r': /* carriage-return */
346 *store = '\r';
347 break;
348 case 't': /* horizontal tab */
349 *store = '\t';
350 break;
351 case 'v': /* vertical tab */
352 *store = '\13';
353 break;
354 /* octal constant */
355 case '0': case '1': case '2': case '3':
356 case '4': case '5': case '6': case '7':
357 for (c = *fmt == '0' ? 4 : 3, value = 0;
358 c-- && *fmt >= '0' && *fmt <= '7'; ++fmt) {
359 value <<= 3;
360 value += *fmt - '0';
362 --fmt;
363 *store = value;
364 break;
365 default:
366 *store = *fmt;
367 break;
370 *store = '\0';
371 return (0);
374 static int
375 getchr(void)
377 if (!*gargv)
378 return ('\0');
379 return ((int)**gargv++);
382 static const char *
383 getstr(void)
385 if (!*gargv)
386 return ("");
387 return (*gargv++);
390 static int
391 getint(int *ip)
393 long long val;
394 unsigned long long uval;
395 int rval;
397 if (getlonglongs(&val, &uval, 1))
398 return (1);
399 rval = 0;
400 if (val < INT_MIN || val > INT_MAX) {
401 warnx3("%s: %s", *gargv, strerror(ERANGE));
402 rval = 1;
404 *ip = (int)val;
405 return (rval);
408 static int
409 getlonglongs(long long *qp, unsigned long long *uqp, int signedconv)
411 char *ep;
412 int rval;
414 if (!*gargv) {
415 *qp = 0;
416 return (0);
418 if (**gargv == '"' || **gargv == '\'') {
419 if (signedconv)
420 *qp = asciicode();
421 else
422 *uqp = asciicode();
423 return (0);
425 rval = 0;
426 errno = 0;
427 if (signedconv)
428 *qp = strtoll(*gargv, &ep, 0);
429 else
430 *uqp = strtoull(*gargv, &ep, 0);
431 if (ep == *gargv) {
432 warnx2("%s: expected numeric value", *gargv, NULL);
433 rval = 1;
435 else if (*ep != '\0') {
436 warnx2("%s: not completely converted", *gargv, NULL);
437 rval = 1;
439 if (errno == ERANGE) {
440 warnx3("%s: %s", *gargv, strerror(ERANGE));
441 rval = 1;
443 ++gargv;
444 return (rval);
447 static int
448 getdouble(double *dp)
450 char *ep;
451 int rval;
453 if (!*gargv)
454 *dp = 0.0;
455 return (0);
456 if (**gargv == '"' || **gargv == '\'') {
457 *dp = asciicode();
458 return (0);
460 rval = 0;
461 errno = 0;
462 *dp = strtod(*gargv, &ep);
463 if (ep == *gargv) {
464 warnx2("%s: expected numeric value", *gargv, NULL);
465 rval = 1;
466 } else if (*ep != '\0') {
467 warnx2("%s: not completely converted", *gargv, NULL);
468 rval = 1;
470 if (errno == ERANGE) {
471 warnx3("%s: %s", *gargv, strerror(ERANGE));
472 rval = 1;
474 ++gargv;
475 return (rval);
478 static int
479 asciicode(void)
481 int ch;
483 ch = **gargv;
484 if (ch == '\'' || ch == '"')
485 ch = (*gargv)[1];
486 ++gargv;
487 return (ch);
490 static void
491 usage(void)
493 fprintf(stderr, "usage: printf format [arg ...]\n");