printf.c revision 1.57 1 1.57 kre /* $NetBSD: printf.c,v 1.57 2024/08/06 14:55:48 kre 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.29 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.16 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.17 mrg #if !defined(BUILTIN) && !defined(SHELL)
35 1.33 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
36 1.33 lukem The Regents of the University of California. All rights reserved.");
37 1.17 mrg #endif
38 1.5 jtc #endif
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.16 christos #if 0
42 1.17 mrg static char sccsid[] = "@(#)printf.c 8.2 (Berkeley) 3/22/95";
43 1.16 christos #else
44 1.57 kre __RCSID("$NetBSD: printf.c,v 1.57 2024/08/06 14:55:48 kre Exp $");
45 1.16 christos #endif
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.17 mrg #include <sys/types.h>
49 1.17 mrg
50 1.4 jtc #include <ctype.h>
51 1.19 perry #include <err.h>
52 1.19 perry #include <errno.h>
53 1.22 kleink #include <inttypes.h>
54 1.19 perry #include <limits.h>
55 1.19 perry #include <locale.h>
56 1.23 wiz #include <stdarg.h>
57 1.1 cgd #include <stdio.h>
58 1.4 jtc #include <stdlib.h>
59 1.2 mycroft #include <string.h>
60 1.19 perry #include <unistd.h>
61 1.4 jtc
62 1.25 christos #ifdef __GNUC__
63 1.25 christos #define ESCAPE '\e'
64 1.25 christos #else
65 1.25 christos #define ESCAPE 033
66 1.25 christos #endif
67 1.5 jtc
68 1.39 kre static void conv_escape_str(char *, void (*)(int), int);
69 1.39 kre static char *conv_escape(char *, char *, int);
70 1.25 christos static char *conv_expand(const char *);
71 1.57 kre static wchar_t getchr(void);
72 1.57 kre static long double getdouble(void);
73 1.25 christos static int getwidth(void);
74 1.23 wiz static intmax_t getintmax(void);
75 1.23 wiz static char *getstr(void);
76 1.57 kre static char *mklong(const char *, char, char);
77 1.57 kre static intmax_t wide_char(const char *, int);
78 1.23 wiz static void check_conversion(const char *, const char *);
79 1.41 kre static void usage(void);
80 1.25 christos
81 1.26 dsl static void b_count(int);
82 1.26 dsl static void b_output(int);
83 1.30 christos static size_t b_length;
84 1.26 dsl static char *b_fmt;
85 1.26 dsl
86 1.5 jtc static int rval;
87 1.5 jtc static char **gargv;
88 1.57 kre static int long_double;
89 1.4 jtc
90 1.25 christos #ifdef BUILTIN /* csh builtin */
91 1.25 christos #define main progprintf
92 1.16 christos #endif
93 1.16 christos
94 1.25 christos #ifdef SHELL /* sh (aka ash) builtin */
95 1.5 jtc #define main printfcmd
96 1.5 jtc #include "../../bin/sh/bltin/bltin.h"
97 1.5 jtc #endif /* SHELL */
98 1.5 jtc
99 1.1 cgd #define PF(f, func) { \
100 1.25 christos if (fieldwidth != -1) { \
101 1.25 christos if (precision != -1) \
102 1.32 christos error = printf(f, fieldwidth, precision, func); \
103 1.1 cgd else \
104 1.32 christos error = printf(f, fieldwidth, func); \
105 1.25 christos } else if (precision != -1) \
106 1.32 christos error = printf(f, precision, func); \
107 1.1 cgd else \
108 1.32 christos error = printf(f, func); \
109 1.1 cgd }
110 1.1 cgd
111 1.26 dsl #define APF(cpp, f, func) { \
112 1.26 dsl if (fieldwidth != -1) { \
113 1.26 dsl if (precision != -1) \
114 1.32 christos error = asprintf(cpp, f, fieldwidth, precision, func); \
115 1.26 dsl else \
116 1.32 christos error = asprintf(cpp, f, fieldwidth, func); \
117 1.26 dsl } else if (precision != -1) \
118 1.32 christos error = asprintf(cpp, f, precision, func); \
119 1.26 dsl else \
120 1.32 christos error = asprintf(cpp, f, func); \
121 1.26 dsl }
122 1.26 dsl
123 1.51 christos #define isodigit(c) ((c) >= '0' && (c) <= '7')
124 1.52 christos #define octtobin(c) ((c) - '0')
125 1.52 christos #define check(c, a) (c) >= (a) && (c) <= (a) + 5 ? (c) - (a) + 10
126 1.52 christos #define hextobin(c) (check(c, 'a') : check(c, 'A') : (c) - '0')
127 1.32 christos #ifdef main
128 1.32 christos int main(int, char *[]);
129 1.32 christos #endif
130 1.44 kre
131 1.44 kre int
132 1.44 kre main(int argc, char *argv[])
133 1.1 cgd {
134 1.18 lukem char *fmt, *start;
135 1.18 lukem int fieldwidth, precision;
136 1.25 christos char nextch;
137 1.4 jtc char *format;
138 1.36 christos char ch;
139 1.49 kre int error;
140 1.4 jtc
141 1.5 jtc #if !defined(SHELL) && !defined(BUILTIN)
142 1.15 cgd (void)setlocale (LC_ALL, "");
143 1.5 jtc #endif
144 1.1 cgd
145 1.46 kre rval = 0; /* clear for builtin versions (avoid holdover) */
146 1.57 kre long_double = 0;
147 1.53 kre clearerr(stdout); /* for the builtin version */
148 1.46 kre
149 1.50 kre if (argc > 2 && strchr(argv[1], '%') == NULL) {
150 1.50 kre int o;
151 1.50 kre
152 1.50 kre /*
153 1.56 kre * We only do this for argc > 2, as:
154 1.56 kre *
155 1.56 kre * for argc <= 1
156 1.56 kre * at best we have a bare "printf" so there cannot be
157 1.56 kre * any options, thus getopts() would be a waste of time.
158 1.56 kre * The usage() below is assured.
159 1.56 kre *
160 1.56 kre * for argc == 2
161 1.56 kre * There is only one arg (argv[1]) which logically must
162 1.56 kre * be intended to be the (required) format string for
163 1.56 kre * printf, without which we can do nothing so rather
164 1.56 kre * than usage() if it happens to start with a '-' we
165 1.56 kre * just avoid getopts() and treat it as a format string.
166 1.56 kre *
167 1.56 kre * Then, for argc > 2, we also skip this if there is a '%'
168 1.56 kre * anywhere in argv[1] as it is likely that would be intended
169 1.56 kre * to be the format string, rather than options, even if it
170 1.56 kre * starts with a '-' so we skip getopts() in that case as well.
171 1.56 kre *
172 1.56 kre * Note that this would fail should there ever be an option
173 1.56 kre * which takes an arbitrary string value, which could be given
174 1.56 kre * as -Oabc%def so should that ever become possible, remove
175 1.56 kre * the strchr() test above.
176 1.50 kre */
177 1.50 kre
178 1.57 kre while ((o = getopt(argc, argv, "L")) != -1) {
179 1.50 kre switch (o) {
180 1.57 kre case 'L':
181 1.57 kre long_double = 1;
182 1.57 kre break;
183 1.50 kre case '?':
184 1.50 kre default:
185 1.50 kre usage();
186 1.50 kre return 1;
187 1.50 kre }
188 1.5 jtc }
189 1.50 kre argc -= optind;
190 1.50 kre argv += optind;
191 1.50 kre } else {
192 1.50 kre argc -= 1; /* drop argv[0] (the program name) */
193 1.50 kre argv += 1;
194 1.1 cgd }
195 1.1 cgd
196 1.56 kre if (argc < 1) { /* Nothing left at all? */
197 1.5 jtc usage();
198 1.30 christos return 1;
199 1.5 jtc }
200 1.5 jtc
201 1.56 kre format = *argv; /* First remaining arg is the format string */
202 1.56 kre gargv = ++argv; /* remaining args are for that to consume */
203 1.4 jtc
204 1.35 christos #define SKIP1 "#-+ 0'"
205 1.34 christos #define SKIP2 "0123456789"
206 1.4 jtc do {
207 1.6 jtc /*
208 1.6 jtc * Basic algorithm is to scan the format string for conversion
209 1.6 jtc * specifications -- once one is found, find out if the field
210 1.41 kre * width or precision is a '*'; if it is, gather up value.
211 1.6 jtc * Note, format strings are reused as necessary to use up the
212 1.41 kre * provided arguments, arguments of zero/null string are
213 1.6 jtc * provided to use up the format string.
214 1.6 jtc */
215 1.6 jtc
216 1.6 jtc /* find next format specification */
217 1.30 christos for (fmt = format; (ch = *fmt++) != '\0';) {
218 1.25 christos if (ch == '\\') {
219 1.25 christos char c_ch;
220 1.39 kre fmt = conv_escape(fmt, &c_ch, 0);
221 1.25 christos putchar(c_ch);
222 1.25 christos continue;
223 1.25 christos }
224 1.25 christos if (ch != '%' || (*fmt == '%' && ++fmt)) {
225 1.25 christos (void)putchar(ch);
226 1.25 christos continue;
227 1.25 christos }
228 1.25 christos
229 1.41 kre /*
230 1.41 kre * Ok - we've found a format specification,
231 1.41 kre * Save its address for a later printf().
232 1.41 kre */
233 1.25 christos start = fmt - 1;
234 1.25 christos
235 1.25 christos /* skip to field width */
236 1.25 christos fmt += strspn(fmt, SKIP1);
237 1.34 christos if (*fmt == '*') {
238 1.34 christos fmt++;
239 1.34 christos fieldwidth = getwidth();
240 1.44 kre } else {
241 1.34 christos fieldwidth = -1;
242 1.25 christos
243 1.44 kre /* skip to possible '.' for precision */
244 1.44 kre fmt += strspn(fmt, SKIP2);
245 1.44 kre }
246 1.44 kre
247 1.34 christos if (*fmt == '.') {
248 1.44 kre /* get following precision */
249 1.34 christos fmt++;
250 1.34 christos if (*fmt == '*') {
251 1.34 christos fmt++;
252 1.34 christos precision = getwidth();
253 1.44 kre } else {
254 1.34 christos precision = -1;
255 1.44 kre fmt += strspn(fmt, SKIP2);
256 1.44 kre }
257 1.34 christos } else
258 1.34 christos precision = -1;
259 1.25 christos
260 1.25 christos ch = *fmt;
261 1.25 christos if (!ch) {
262 1.44 kre warnx("%s: missing format character", start);
263 1.42 kre return 1;
264 1.25 christos }
265 1.44 kre
266 1.41 kre /*
267 1.41 kre * null terminate format string to we can use it
268 1.41 kre * as an argument to printf.
269 1.41 kre */
270 1.25 christos nextch = fmt[1];
271 1.25 christos fmt[1] = 0;
272 1.44 kre
273 1.25 christos switch (ch) {
274 1.25 christos
275 1.25 christos case 'B': {
276 1.25 christos const char *p = conv_expand(getstr());
277 1.41 kre
278 1.32 christos if (p == NULL)
279 1.32 christos goto out;
280 1.25 christos *fmt = 's';
281 1.25 christos PF(start, p);
282 1.32 christos if (error < 0)
283 1.32 christos goto out;
284 1.6 jtc break;
285 1.25 christos }
286 1.25 christos case 'b': {
287 1.41 kre /*
288 1.41 kre * There has to be a better way to do this,
289 1.26 dsl * but the string we generate might have
290 1.41 kre * embedded nulls
291 1.41 kre */
292 1.26 dsl static char *a, *t;
293 1.26 dsl char *cp = getstr();
294 1.41 kre
295 1.26 dsl /* Free on entry in case shell longjumped out */
296 1.26 dsl if (a != NULL)
297 1.26 dsl free(a);
298 1.26 dsl a = NULL;
299 1.26 dsl if (t != NULL)
300 1.26 dsl free(t);
301 1.26 dsl t = NULL;
302 1.41 kre
303 1.26 dsl /* Count number of bytes we want to output */
304 1.26 dsl b_length = 0;
305 1.39 kre conv_escape_str(cp, b_count, 0);
306 1.26 dsl t = malloc(b_length + 1);
307 1.26 dsl if (t == NULL)
308 1.32 christos goto out;
309 1.32 christos (void)memset(t, 'x', b_length);
310 1.26 dsl t[b_length] = 0;
311 1.41 kre
312 1.26 dsl /* Get printf to calculate the lengths */
313 1.25 christos *fmt = 's';
314 1.26 dsl APF(&a, start, t);
315 1.32 christos if (error == -1)
316 1.32 christos goto out;
317 1.26 dsl b_fmt = a;
318 1.41 kre
319 1.26 dsl /* Output leading spaces and data bytes */
320 1.39 kre conv_escape_str(cp, b_output, 1);
321 1.41 kre
322 1.26 dsl /* Add any trailing spaces */
323 1.26 dsl printf("%s", b_fmt);
324 1.25 christos break;
325 1.25 christos }
326 1.57 kre case 'C': {
327 1.57 kre wchar_t p = (wchar_t)getintmax();
328 1.57 kre char *f = mklong(start, 'c', 'l');
329 1.57 kre
330 1.57 kre PF(f, p);
331 1.57 kre if (error < 0)
332 1.57 kre goto out;
333 1.57 kre break;
334 1.57 kre }
335 1.25 christos case 'c': {
336 1.57 kre wchar_t p = getchr();
337 1.57 kre char *f = mklong(start, ch, 'l');
338 1.41 kre
339 1.57 kre PF(f, p);
340 1.32 christos if (error < 0)
341 1.32 christos goto out;
342 1.25 christos break;
343 1.25 christos }
344 1.25 christos case 's': {
345 1.25 christos char *p = getstr();
346 1.41 kre
347 1.25 christos PF(start, p);
348 1.32 christos if (error < 0)
349 1.32 christos goto out;
350 1.25 christos break;
351 1.25 christos }
352 1.25 christos case 'd':
353 1.25 christos case 'i': {
354 1.25 christos intmax_t p = getintmax();
355 1.57 kre char *f = mklong(start, ch, 'j');
356 1.41 kre
357 1.25 christos PF(f, p);
358 1.32 christos if (error < 0)
359 1.32 christos goto out;
360 1.25 christos break;
361 1.25 christos }
362 1.25 christos case 'o':
363 1.25 christos case 'u':
364 1.25 christos case 'x':
365 1.25 christos case 'X': {
366 1.43 kre uintmax_t p = (uintmax_t)getintmax();
367 1.57 kre char *f = mklong(start, ch, 'j');
368 1.41 kre
369 1.25 christos PF(f, p);
370 1.32 christos if (error < 0)
371 1.32 christos goto out;
372 1.25 christos break;
373 1.25 christos }
374 1.40 kre case 'a':
375 1.40 kre case 'A':
376 1.25 christos case 'e':
377 1.25 christos case 'E':
378 1.25 christos case 'f':
379 1.40 kre case 'F':
380 1.25 christos case 'g':
381 1.25 christos case 'G': {
382 1.57 kre long double p = getdouble();
383 1.41 kre
384 1.57 kre if (long_double) {
385 1.57 kre char * f = mklong(start, ch, 'L');
386 1.57 kre PF(f, p);
387 1.57 kre } else {
388 1.57 kre double pp = (double)p;
389 1.57 kre PF(start, pp);
390 1.57 kre }
391 1.32 christos if (error < 0)
392 1.32 christos goto out;
393 1.4 jtc break;
394 1.25 christos }
395 1.44 kre case '%':
396 1.44 kre /* Don't ask, but this is useful ... */
397 1.44 kre if (fieldwidth == 'N' && precision == 'B')
398 1.44 kre return 0;
399 1.44 kre /* FALLTHROUGH */
400 1.6 jtc default:
401 1.25 christos warnx("%s: invalid directive", start);
402 1.30 christos return 1;
403 1.4 jtc }
404 1.25 christos *fmt++ = ch;
405 1.25 christos *fmt = nextch;
406 1.25 christos /* escape if a \c was encountered */
407 1.25 christos if (rval & 0x100)
408 1.53 kre goto done;
409 1.6 jtc }
410 1.25 christos } while (gargv != argv && *gargv);
411 1.1 cgd
412 1.53 kre done:
413 1.53 kre (void)fflush(stdout);
414 1.53 kre if (ferror(stdout)) {
415 1.53 kre clearerr(stdout);
416 1.53 kre err(1, "write error");
417 1.53 kre }
418 1.32 christos return rval & ~0x100;
419 1.41 kre out:
420 1.32 christos warn("print failed");
421 1.32 christos return 1;
422 1.4 jtc }
423 1.4 jtc
424 1.26 dsl /* helper functions for conv_escape_str */
425 1.26 dsl
426 1.26 dsl static void
427 1.30 christos /*ARGSUSED*/
428 1.26 dsl b_count(int ch)
429 1.26 dsl {
430 1.26 dsl b_length++;
431 1.26 dsl }
432 1.26 dsl
433 1.26 dsl /* Output one converted character for every 'x' in the 'format' */
434 1.26 dsl
435 1.26 dsl static void
436 1.26 dsl b_output(int ch)
437 1.26 dsl {
438 1.26 dsl for (;;) {
439 1.26 dsl switch (*b_fmt++) {
440 1.26 dsl case 0:
441 1.26 dsl b_fmt--;
442 1.26 dsl return;
443 1.26 dsl case ' ':
444 1.26 dsl putchar(' ');
445 1.26 dsl break;
446 1.26 dsl default:
447 1.26 dsl putchar(ch);
448 1.26 dsl return;
449 1.26 dsl }
450 1.26 dsl }
451 1.26 dsl }
452 1.26 dsl
453 1.4 jtc
454 1.4 jtc /*
455 1.41 kre * Print SysV echo(1) style escape string
456 1.25 christos * Halts processing string if a \c escape is encountered.
457 1.4 jtc */
458 1.26 dsl static void
459 1.39 kre conv_escape_str(char *str, void (*do_putchar)(int), int quiet)
460 1.4 jtc {
461 1.4 jtc int value;
462 1.25 christos int ch;
463 1.26 dsl char c;
464 1.25 christos
465 1.30 christos while ((ch = *str++) != '\0') {
466 1.25 christos if (ch != '\\') {
467 1.26 dsl do_putchar(ch);
468 1.25 christos continue;
469 1.25 christos }
470 1.25 christos
471 1.25 christos ch = *str++;
472 1.25 christos if (ch == 'c') {
473 1.25 christos /* \c as in SYSV echo - abort all processing.... */
474 1.25 christos rval |= 0x100;
475 1.25 christos break;
476 1.25 christos }
477 1.25 christos
478 1.41 kre /*
479 1.25 christos * %b string octal constants are not like those in C.
480 1.41 kre * They start with a \0, and are followed by 0, 1, 2,
481 1.41 kre * or 3 octal digits.
482 1.25 christos */
483 1.25 christos if (ch == '0') {
484 1.30 christos int octnum = 0, i;
485 1.30 christos for (i = 0; i < 3; i++) {
486 1.30 christos if (!isdigit((unsigned char)*str) || *str > '7')
487 1.30 christos break;
488 1.31 dsl octnum = (octnum << 3) | (*str++ - '0');
489 1.30 christos }
490 1.30 christos do_putchar(octnum);
491 1.25 christos continue;
492 1.25 christos }
493 1.25 christos
494 1.25 christos /* \[M][^|-]C as defined by vis(3) */
495 1.25 christos if (ch == 'M' && *str == '-') {
496 1.26 dsl do_putchar(0200 | str[1]);
497 1.25 christos str += 2;
498 1.25 christos continue;
499 1.25 christos }
500 1.25 christos if (ch == 'M' && *str == '^') {
501 1.4 jtc str++;
502 1.25 christos value = 0200;
503 1.25 christos ch = '^';
504 1.25 christos } else
505 1.25 christos value = 0;
506 1.25 christos if (ch == '^') {
507 1.25 christos ch = *str++;
508 1.25 christos if (ch == '?')
509 1.25 christos value |= 0177;
510 1.25 christos else
511 1.25 christos value |= ch & 037;
512 1.26 dsl do_putchar(value);
513 1.25 christos continue;
514 1.1 cgd }
515 1.25 christos
516 1.25 christos /* Finally test for sequences valid in the format string */
517 1.39 kre str = conv_escape(str - 1, &c, quiet);
518 1.26 dsl do_putchar(c);
519 1.4 jtc }
520 1.4 jtc }
521 1.4 jtc
522 1.4 jtc /*
523 1.41 kre * Print "standard" escape characters
524 1.4 jtc */
525 1.25 christos static char *
526 1.39 kre conv_escape(char *str, char *conv_ch, int quiet)
527 1.4 jtc {
528 1.52 christos int value = 0;
529 1.51 christos char ch, *begin;
530 1.51 christos int c;
531 1.4 jtc
532 1.25 christos ch = *str++;
533 1.4 jtc
534 1.25 christos switch (ch) {
535 1.38 kre case '\0':
536 1.39 kre if (!quiet)
537 1.39 kre warnx("incomplete escape sequence");
538 1.38 kre rval = 1;
539 1.38 kre value = '\\';
540 1.38 kre --str;
541 1.38 kre break;
542 1.38 kre
543 1.4 jtc case '0': case '1': case '2': case '3':
544 1.4 jtc case '4': case '5': case '6': case '7':
545 1.51 christos str--;
546 1.51 christos for (c = 3; c-- && isodigit(*str); str++) {
547 1.51 christos value <<= 3;
548 1.51 christos value += octtobin(*str);
549 1.51 christos }
550 1.25 christos break;
551 1.4 jtc
552 1.4 jtc case 'x':
553 1.41 kre /*
554 1.41 kre * Hexadecimal character constants are not required to be
555 1.41 kre * supported (by SuS v1) because there is no consistent
556 1.41 kre * way to detect the end of the constant.
557 1.41 kre * Supporting 2 byte constants is a compromise.
558 1.41 kre */
559 1.51 christos begin = str;
560 1.51 christos for (c = 2; c-- && isxdigit((unsigned char)*str); str++) {
561 1.51 christos value <<= 4;
562 1.52 christos value += hextobin(*str);
563 1.51 christos }
564 1.51 christos if (str == begin) {
565 1.51 christos if (!quiet)
566 1.51 christos warnx("\\x%s: missing hexadecimal number "
567 1.51 christos "in escape", begin);
568 1.51 christos rval = 1;
569 1.51 christos }
570 1.25 christos break;
571 1.25 christos
572 1.25 christos case '\\': value = '\\'; break; /* backslash */
573 1.25 christos case '\'': value = '\''; break; /* single quote */
574 1.25 christos case '"': value = '"'; break; /* double quote */
575 1.25 christos case 'a': value = '\a'; break; /* alert */
576 1.25 christos case 'b': value = '\b'; break; /* backspace */
577 1.25 christos case 'e': value = ESCAPE; break; /* escape */
578 1.45 kre case 'E': value = ESCAPE; break; /* escape */
579 1.25 christos case 'f': value = '\f'; break; /* form-feed */
580 1.25 christos case 'n': value = '\n'; break; /* newline */
581 1.25 christos case 'r': value = '\r'; break; /* carriage-return */
582 1.25 christos case 't': value = '\t'; break; /* tab */
583 1.25 christos case 'v': value = '\v'; break; /* vertical-tab */
584 1.4 jtc
585 1.25 christos default:
586 1.39 kre if (!quiet)
587 1.39 kre warnx("unknown escape sequence `\\%c'", ch);
588 1.25 christos rval = 1;
589 1.26 dsl value = ch;
590 1.4 jtc break;
591 1.25 christos }
592 1.4 jtc
593 1.52 christos *conv_ch = (char)value;
594 1.25 christos return str;
595 1.25 christos }
596 1.4 jtc
597 1.25 christos /* expand a string so that everything is printable */
598 1.4 jtc
599 1.25 christos static char *
600 1.25 christos conv_expand(const char *str)
601 1.25 christos {
602 1.25 christos static char *conv_str;
603 1.25 christos char *cp;
604 1.36 christos char ch;
605 1.4 jtc
606 1.25 christos if (conv_str)
607 1.25 christos free(conv_str);
608 1.25 christos /* get a buffer that is definitely large enough.... */
609 1.25 christos conv_str = malloc(4 * strlen(str) + 1);
610 1.25 christos if (!conv_str)
611 1.32 christos return NULL;
612 1.25 christos cp = conv_str;
613 1.4 jtc
614 1.36 christos while ((ch = *(const char *)str++) != '\0') {
615 1.25 christos switch (ch) {
616 1.25 christos /* Use C escapes for expected control characters */
617 1.25 christos case '\\': ch = '\\'; break; /* backslash */
618 1.25 christos case '\'': ch = '\''; break; /* single quote */
619 1.25 christos case '"': ch = '"'; break; /* double quote */
620 1.25 christos case '\a': ch = 'a'; break; /* alert */
621 1.25 christos case '\b': ch = 'b'; break; /* backspace */
622 1.25 christos case ESCAPE: ch = 'e'; break; /* escape */
623 1.25 christos case '\f': ch = 'f'; break; /* form-feed */
624 1.25 christos case '\n': ch = 'n'; break; /* newline */
625 1.25 christos case '\r': ch = 'r'; break; /* carriage-return */
626 1.25 christos case '\t': ch = 't'; break; /* tab */
627 1.25 christos case '\v': ch = 'v'; break; /* vertical-tab */
628 1.25 christos default:
629 1.25 christos /* Copy anything printable */
630 1.36 christos if (isprint((unsigned char)ch)) {
631 1.25 christos *cp++ = ch;
632 1.25 christos continue;
633 1.25 christos }
634 1.25 christos /* Use vis(3) encodings for the rest */
635 1.25 christos *cp++ = '\\';
636 1.25 christos if (ch & 0200) {
637 1.25 christos *cp++ = 'M';
638 1.36 christos ch &= (char)~0200;
639 1.25 christos }
640 1.25 christos if (ch == 0177) {
641 1.25 christos *cp++ = '^';
642 1.25 christos *cp++ = '?';
643 1.25 christos continue;
644 1.25 christos }
645 1.25 christos if (ch < 040) {
646 1.25 christos *cp++ = '^';
647 1.25 christos *cp++ = ch | 0100;
648 1.25 christos continue;
649 1.25 christos }
650 1.25 christos *cp++ = '-';
651 1.25 christos *cp++ = ch;
652 1.25 christos continue;
653 1.25 christos }
654 1.25 christos *cp++ = '\\';
655 1.25 christos *cp++ = ch;
656 1.1 cgd }
657 1.4 jtc
658 1.25 christos *cp = 0;
659 1.25 christos return conv_str;
660 1.1 cgd }
661 1.1 cgd
662 1.5 jtc static char *
663 1.57 kre mklong(const char *str, char ch, char longer)
664 1.1 cgd {
665 1.5 jtc static char copy[64];
666 1.15 cgd size_t len;
667 1.1 cgd
668 1.1 cgd len = strlen(str) + 2;
669 1.25 christos if (len > sizeof copy) {
670 1.53 kre warnx("format \"%s\" too complex", str);
671 1.25 christos len = 4;
672 1.53 kre rval = 1;
673 1.25 christos }
674 1.15 cgd (void)memmove(copy, str, len - 3);
675 1.57 kre copy[len - 3] = longer;
676 1.1 cgd copy[len - 2] = ch;
677 1.1 cgd copy[len - 1] = '\0';
678 1.30 christos return copy;
679 1.1 cgd }
680 1.1 cgd
681 1.57 kre static wchar_t
682 1.23 wiz getchr(void)
683 1.1 cgd {
684 1.1 cgd if (!*gargv)
685 1.30 christos return 0;
686 1.57 kre return (wchar_t)wide_char(*gargv++, 0);
687 1.1 cgd }
688 1.1 cgd
689 1.5 jtc static char *
690 1.23 wiz getstr(void)
691 1.1 cgd {
692 1.30 christos static char empty[] = "";
693 1.1 cgd if (!*gargv)
694 1.30 christos return empty;
695 1.30 christos return *gargv++;
696 1.1 cgd }
697 1.1 cgd
698 1.5 jtc static int
699 1.25 christos getwidth(void)
700 1.1 cgd {
701 1.36 christos unsigned long val;
702 1.25 christos char *s, *ep;
703 1.25 christos
704 1.25 christos s = *gargv;
705 1.44 kre if (s == NULL)
706 1.42 kre return 0;
707 1.25 christos gargv++;
708 1.4 jtc
709 1.25 christos errno = 0;
710 1.25 christos val = strtoul(s, &ep, 0);
711 1.25 christos check_conversion(s, ep);
712 1.4 jtc
713 1.25 christos /* Arbitrarily 'restrict' field widths to 1Mbyte */
714 1.36 christos if (val > 1 << 20) {
715 1.25 christos warnx("%s: invalid field width", s);
716 1.25 christos return 0;
717 1.25 christos }
718 1.25 christos
719 1.36 christos return (int)val;
720 1.1 cgd }
721 1.1 cgd
722 1.22 kleink static intmax_t
723 1.23 wiz getintmax(void)
724 1.1 cgd {
725 1.22 kleink intmax_t val;
726 1.25 christos char *cp, *ep;
727 1.1 cgd
728 1.25 christos cp = *gargv;
729 1.25 christos if (cp == NULL)
730 1.25 christos return 0;
731 1.25 christos gargv++;
732 1.4 jtc
733 1.25 christos if (*cp == '\"' || *cp == '\'')
734 1.57 kre return wide_char(cp, 1);
735 1.4 jtc
736 1.11 jtc errno = 0;
737 1.25 christos val = strtoimax(cp, &ep, 0);
738 1.25 christos check_conversion(cp, ep);
739 1.11 jtc return val;
740 1.11 jtc }
741 1.11 jtc
742 1.57 kre static long double
743 1.23 wiz getdouble(void)
744 1.1 cgd {
745 1.57 kre long double val;
746 1.4 jtc char *ep;
747 1.1 cgd
748 1.1 cgd if (!*gargv)
749 1.42 kre return 0.0;
750 1.4 jtc
751 1.53 kre /* This is a NetBSD extension, not required by POSIX (it is useless) */
752 1.53 kre if (*(ep = *gargv) == '\"' || *ep == '\'')
753 1.57 kre return (long double)wide_char(ep, 1);
754 1.1 cgd
755 1.11 jtc errno = 0;
756 1.57 kre val = strtold(*gargv, &ep);
757 1.12 jtc check_conversion(*gargv++, ep);
758 1.12 jtc return val;
759 1.12 jtc }
760 1.12 jtc
761 1.53 kre /*
762 1.57 kre * Fetch a wide character from the string given
763 1.53 kre *
764 1.57 kre * if all that character must consume the entire string
765 1.57 kre * after an initial leading byte (ascii char) is ignored,
766 1.57 kre * (used for parsing intger args using the 'X syntax)
767 1.53 kre *
768 1.57 kre * if !all then there is no requirement that the whole
769 1.57 kre * string be consumed (remaining characters are just ignored)
770 1.57 kre * but the character is to start at *p.
771 1.57 kre * (used for fetching the first chartacter of a string arg for %c)
772 1.53 kre */
773 1.53 kre static intmax_t
774 1.57 kre wide_char(const char *p, int all)
775 1.53 kre {
776 1.57 kre wchar_t wch;
777 1.57 kre size_t len;
778 1.57 kre int n;
779 1.57 kre
780 1.57 kre (void)mbtowc(NULL, NULL, 0);
781 1.57 kre n = mbtowc(&wch, p + all, len = strlen(p + all));
782 1.57 kre if (n < 0) {
783 1.57 kre warn("%s", p);
784 1.57 kre rval = -1;
785 1.57 kre } else if (all && (size_t)n != len) {
786 1.54 christos warnx("%s: not completely converted", p);
787 1.53 kre rval = 1;
788 1.53 kre }
789 1.53 kre
790 1.57 kre return (intmax_t) wch;
791 1.53 kre }
792 1.53 kre
793 1.12 jtc static void
794 1.23 wiz check_conversion(const char *s, const char *ep)
795 1.12 jtc {
796 1.4 jtc if (*ep) {
797 1.12 jtc if (ep == s)
798 1.25 christos warnx("%s: expected numeric value", s);
799 1.11 jtc else
800 1.25 christos warnx("%s: not completely converted", s);
801 1.11 jtc rval = 1;
802 1.11 jtc } else if (errno == ERANGE) {
803 1.25 christos warnx("%s: %s", s, strerror(ERANGE));
804 1.4 jtc rval = 1;
805 1.4 jtc }
806 1.5 jtc }
807 1.5 jtc
808 1.5 jtc static void
809 1.23 wiz usage(void)
810 1.5 jtc {
811 1.57 kre (void)fprintf(stderr,
812 1.57 kre "Usage: %s [-L] format [arg ...]\n", getprogname());
813 1.1 cgd }
814