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