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