1 | /* $NetBSD: printf.c,v 1.31 2005/03/22 23:55:46 dsl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Copyright (c) 1989, 1993
|
---|
5 | * The Regents of the University of California. All rights reserved.
|
---|
6 | *
|
---|
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.
|
---|
18 | *
|
---|
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.
|
---|
30 | */
|
---|
31 |
|
---|
32 | /*#include <sys/cdefs.h>
|
---|
33 | #ifndef lint
|
---|
34 | #if !defined(BUILTIN) && !defined(SHELL)
|
---|
35 | __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
|
---|
36 | The Regents of the University of California. All rights reserved.\n");
|
---|
37 | #endif
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #ifndef lint
|
---|
41 | #if 0
|
---|
42 | static char sccsid[] = "@(#)printf.c 8.2 (Berkeley) 3/22/95";
|
---|
43 | #else
|
---|
44 | __RCSID("$NetBSD: printf.c,v 1.31 2005/03/22 23:55:46 dsl Exp $");
|
---|
45 | #endif
|
---|
46 | #endif*/ /* not lint */
|
---|
47 |
|
---|
48 | #include "config.h"
|
---|
49 | #include <sys/types.h>
|
---|
50 |
|
---|
51 | #include <ctype.h>
|
---|
52 | #include "err.h"
|
---|
53 | #include <errno.h>
|
---|
54 | #include <inttypes.h>
|
---|
55 | #include <limits.h>
|
---|
56 | #include <locale.h>
|
---|
57 | #include <stdarg.h>
|
---|
58 | #include <stdio.h>
|
---|
59 | #include <stdlib.h>
|
---|
60 | #include <string.h>
|
---|
61 | #include <unistd.h>
|
---|
62 | #include "getopt.h"
|
---|
63 | #ifdef __sun__
|
---|
64 | # include "solfakes.h"
|
---|
65 | #endif
|
---|
66 | #ifdef _MSC_VER
|
---|
67 | # include "mscfakes.h"
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | #include "kmkbuiltin.h"
|
---|
71 |
|
---|
72 |
|
---|
73 | #ifdef __GNUC__
|
---|
74 | #define ESCAPE '\e'
|
---|
75 | #else
|
---|
76 | #define ESCAPE 033
|
---|
77 | #endif
|
---|
78 |
|
---|
79 |
|
---|
80 | static size_t b_length;
|
---|
81 | static char *b_fmt;
|
---|
82 | static int rval;
|
---|
83 | static char **gargv;
|
---|
84 | static struct option long_options[] =
|
---|
85 | {
|
---|
86 | { "help", no_argument, 0, 261 },
|
---|
87 | { "version", no_argument, 0, 262 },
|
---|
88 | { 0, 0, 0, 0 },
|
---|
89 | };
|
---|
90 |
|
---|
91 |
|
---|
92 | static void conv_escape_str(char *, void (*)(int));
|
---|
93 | static char *conv_escape(char *, char *);
|
---|
94 | static char *conv_expand(const char *);
|
---|
95 | static int getchr(void);
|
---|
96 | static double getdouble(void);
|
---|
97 | static int getwidth(void);
|
---|
98 | static intmax_t getintmax(void);
|
---|
99 | static uintmax_t getuintmax(void);
|
---|
100 | static char *getstr(void);
|
---|
101 | static char *mklong(const char *, int);
|
---|
102 | static void check_conversion(const char *, const char *);
|
---|
103 | static int usage(FILE *);
|
---|
104 |
|
---|
105 | static void b_count(int);
|
---|
106 | static void b_output(int);
|
---|
107 |
|
---|
108 | #ifdef BUILTIN /* csh builtin */
|
---|
109 | #define kmk_builtin_printf progprintf
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | #ifdef SHELL /* sh (aka ash) builtin */
|
---|
113 | #define kmk_builtin_printf printfcmd
|
---|
114 | #include "../../bin/sh/bltin/bltin.h"
|
---|
115 | #endif /* SHELL */
|
---|
116 |
|
---|
117 | #define PF(f, func) { \
|
---|
118 | if (fieldwidth != -1) { \
|
---|
119 | if (precision != -1) \
|
---|
120 | (void)printf(f, fieldwidth, precision, func); \
|
---|
121 | else \
|
---|
122 | (void)printf(f, fieldwidth, func); \
|
---|
123 | } else if (precision != -1) \
|
---|
124 | (void)printf(f, precision, func); \
|
---|
125 | else \
|
---|
126 | (void)printf(f, func); \
|
---|
127 | }
|
---|
128 |
|
---|
129 | #define APF(cpp, f, func) { \
|
---|
130 | if (fieldwidth != -1) { \
|
---|
131 | if (precision != -1) \
|
---|
132 | (void)asprintf(cpp, f, fieldwidth, precision, func); \
|
---|
133 | else \
|
---|
134 | (void)asprintf(cpp, f, fieldwidth, func); \
|
---|
135 | } else if (precision != -1) \
|
---|
136 | (void)asprintf(cpp, f, precision, func); \
|
---|
137 | else \
|
---|
138 | (void)asprintf(cpp, f, func); \
|
---|
139 | }
|
---|
140 |
|
---|
141 | int kmk_builtin_printf(int argc, char *argv[], char **envp)
|
---|
142 | {
|
---|
143 | char *fmt, *start;
|
---|
144 | int fieldwidth, precision;
|
---|
145 | char nextch;
|
---|
146 | char *format;
|
---|
147 | int ch;
|
---|
148 |
|
---|
149 | /* kmk: reinitialize globals */
|
---|
150 | b_length = 0;
|
---|
151 | b_fmt = NULL;
|
---|
152 | rval = 0;
|
---|
153 | gargv = NULL;
|
---|
154 |
|
---|
155 | /* kmk: reset getopt and set progname */
|
---|
156 | g_progname = argv[0];
|
---|
157 | opterr = 1;
|
---|
158 | optarg = NULL;
|
---|
159 | optopt = 0;
|
---|
160 | optind = 0; /* init */
|
---|
161 |
|
---|
162 | #if !defined(SHELL) && !defined(BUILTIN) && !defined(kmk_builtin_printf) /* kmk did this already. */
|
---|
163 | (void)setlocale (LC_ALL, "");
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | while ((ch = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
|
---|
167 | switch (ch) {
|
---|
168 | case 261:
|
---|
169 | usage(stdout);
|
---|
170 | return 0;
|
---|
171 | case 262:
|
---|
172 | return kbuild_version(argv[0]);
|
---|
173 | case '?':
|
---|
174 | default:
|
---|
175 | return usage(stderr);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | argc -= optind;
|
---|
179 | argv += optind;
|
---|
180 |
|
---|
181 | if (argc < 1) {
|
---|
182 | return usage(stderr);
|
---|
183 | }
|
---|
184 |
|
---|
185 | format = *argv;
|
---|
186 | gargv = ++argv;
|
---|
187 |
|
---|
188 | #define SKIP1 "#-+ 0"
|
---|
189 | #define SKIP2 "*0123456789"
|
---|
190 | do {
|
---|
191 | /*
|
---|
192 | * Basic algorithm is to scan the format string for conversion
|
---|
193 | * specifications -- once one is found, find out if the field
|
---|
194 | * width or precision is a '*'; if it is, gather up value.
|
---|
195 | * Note, format strings are reused as necessary to use up the
|
---|
196 | * provided arguments, arguments of zero/null string are
|
---|
197 | * provided to use up the format string.
|
---|
198 | */
|
---|
199 |
|
---|
200 | /* find next format specification */
|
---|
201 | for (fmt = format; (ch = *fmt++) != '\0';) {
|
---|
202 | if (ch == '\\') {
|
---|
203 | char c_ch;
|
---|
204 | fmt = conv_escape(fmt, &c_ch);
|
---|
205 | putchar(c_ch);
|
---|
206 | continue;
|
---|
207 | }
|
---|
208 | if (ch != '%' || (*fmt == '%' && ++fmt)) {
|
---|
209 | (void)putchar(ch);
|
---|
210 | continue;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* Ok - we've found a format specification,
|
---|
214 | Save its address for a later printf(). */
|
---|
215 | start = fmt - 1;
|
---|
216 |
|
---|
217 | /* skip to field width */
|
---|
218 | fmt += strspn(fmt, SKIP1);
|
---|
219 | fieldwidth = *fmt == '*' ? getwidth() : -1;
|
---|
220 |
|
---|
221 | /* skip to possible '.', get following precision */
|
---|
222 | fmt += strspn(fmt, SKIP2);
|
---|
223 | if (*fmt == '.')
|
---|
224 | ++fmt;
|
---|
225 | precision = *fmt == '*' ? getwidth() : -1;
|
---|
226 |
|
---|
227 | fmt += strspn(fmt, SKIP2);
|
---|
228 |
|
---|
229 | ch = *fmt;
|
---|
230 | if (!ch) {
|
---|
231 | warnx("missing format character");
|
---|
232 | return (1);
|
---|
233 | }
|
---|
234 | /* null terminate format string to we can use it
|
---|
235 | as an argument to printf. */
|
---|
236 | nextch = fmt[1];
|
---|
237 | fmt[1] = 0;
|
---|
238 | switch (ch) {
|
---|
239 |
|
---|
240 | case 'B': {
|
---|
241 | const char *p = conv_expand(getstr());
|
---|
242 | *fmt = 's';
|
---|
243 | PF(start, p);
|
---|
244 | break;
|
---|
245 | }
|
---|
246 | case 'b': {
|
---|
247 | /* There has to be a better way to do this,
|
---|
248 | * but the string we generate might have
|
---|
249 | * embedded nulls. */
|
---|
250 | static char *a, *t;
|
---|
251 | char *cp = getstr();
|
---|
252 | /* Free on entry in case shell longjumped out */
|
---|
253 | if (a != NULL)
|
---|
254 | free(a);
|
---|
255 | a = NULL;
|
---|
256 | if (t != NULL)
|
---|
257 | free(t);
|
---|
258 | t = NULL;
|
---|
259 | /* Count number of bytes we want to output */
|
---|
260 | b_length = 0;
|
---|
261 | conv_escape_str(cp, b_count);
|
---|
262 | t = malloc(b_length + 1);
|
---|
263 | if (t == NULL)
|
---|
264 | break;
|
---|
265 | memset(t, 'x', b_length);
|
---|
266 | t[b_length] = 0;
|
---|
267 | /* Get printf to calculate the lengths */
|
---|
268 | *fmt = 's';
|
---|
269 | APF(&a, start, t);
|
---|
270 | b_fmt = a;
|
---|
271 | /* Output leading spaces and data bytes */
|
---|
272 | conv_escape_str(cp, b_output);
|
---|
273 | /* Add any trailing spaces */
|
---|
274 | printf("%s", b_fmt);
|
---|
275 | break;
|
---|
276 | }
|
---|
277 | case 'c': {
|
---|
278 | char p = getchr();
|
---|
279 | PF(start, p);
|
---|
280 | break;
|
---|
281 | }
|
---|
282 | case 's': {
|
---|
283 | char *p = getstr();
|
---|
284 | PF(start, p);
|
---|
285 | break;
|
---|
286 | }
|
---|
287 | case 'd':
|
---|
288 | case 'i': {
|
---|
289 | intmax_t p = getintmax();
|
---|
290 | char *f = mklong(start, ch);
|
---|
291 | PF(f, p);
|
---|
292 | break;
|
---|
293 | }
|
---|
294 | case 'o':
|
---|
295 | case 'u':
|
---|
296 | case 'x':
|
---|
297 | case 'X': {
|
---|
298 | uintmax_t p = getuintmax();
|
---|
299 | char *f = mklong(start, ch);
|
---|
300 | PF(f, p);
|
---|
301 | break;
|
---|
302 | }
|
---|
303 | case 'e':
|
---|
304 | case 'E':
|
---|
305 | case 'f':
|
---|
306 | case 'g':
|
---|
307 | case 'G': {
|
---|
308 | double p = getdouble();
|
---|
309 | PF(start, p);
|
---|
310 | break;
|
---|
311 | }
|
---|
312 | default:
|
---|
313 | warnx("%s: invalid directive", start);
|
---|
314 | return 1;
|
---|
315 | }
|
---|
316 | *fmt++ = ch;
|
---|
317 | *fmt = nextch;
|
---|
318 | /* escape if a \c was encountered */
|
---|
319 | if (rval & 0x100)
|
---|
320 | return rval & ~0x100;
|
---|
321 | }
|
---|
322 | } while (gargv != argv && *gargv);
|
---|
323 |
|
---|
324 | return rval;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /* helper functions for conv_escape_str */
|
---|
328 |
|
---|
329 | static void
|
---|
330 | /*ARGSUSED*/
|
---|
331 | b_count(int ch)
|
---|
332 | {
|
---|
333 | b_length++;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /* Output one converted character for every 'x' in the 'format' */
|
---|
337 |
|
---|
338 | static void
|
---|
339 | b_output(int ch)
|
---|
340 | {
|
---|
341 | for (;;) {
|
---|
342 | switch (*b_fmt++) {
|
---|
343 | case 0:
|
---|
344 | b_fmt--;
|
---|
345 | return;
|
---|
346 | case ' ':
|
---|
347 | putchar(' ');
|
---|
348 | break;
|
---|
349 | default:
|
---|
350 | putchar(ch);
|
---|
351 | return;
|
---|
352 | }
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 |
|
---|
357 | /*
|
---|
358 | * Print SysV echo(1) style escape string
|
---|
359 | * Halts processing string if a \c escape is encountered.
|
---|
360 | */
|
---|
361 | static void
|
---|
362 | conv_escape_str(char *str, void (*do_putchar)(int))
|
---|
363 | {
|
---|
364 | int value;
|
---|
365 | int ch;
|
---|
366 | char c;
|
---|
367 |
|
---|
368 | while ((ch = *str++) != '\0') {
|
---|
369 | if (ch != '\\') {
|
---|
370 | do_putchar(ch);
|
---|
371 | continue;
|
---|
372 | }
|
---|
373 |
|
---|
374 | ch = *str++;
|
---|
375 | if (ch == 'c') {
|
---|
376 | /* \c as in SYSV echo - abort all processing.... */
|
---|
377 | rval |= 0x100;
|
---|
378 | break;
|
---|
379 | }
|
---|
380 |
|
---|
381 | /*
|
---|
382 | * %b string octal constants are not like those in C.
|
---|
383 | * They start with a \0, and are followed by 0, 1, 2,
|
---|
384 | * or 3 octal digits.
|
---|
385 | */
|
---|
386 | if (ch == '0') {
|
---|
387 | int octnum = 0, i;
|
---|
388 | for (i = 0; i < 3; i++) {
|
---|
389 | if (!isdigit((unsigned char)*str) || *str > '7')
|
---|
390 | break;
|
---|
391 | octnum = (octnum << 3) | (*str++ - '0');
|
---|
392 | }
|
---|
393 | do_putchar(octnum);
|
---|
394 | continue;
|
---|
395 | }
|
---|
396 |
|
---|
397 | /* \[M][^|-]C as defined by vis(3) */
|
---|
398 | if (ch == 'M' && *str == '-') {
|
---|
399 | do_putchar(0200 | str[1]);
|
---|
400 | str += 2;
|
---|
401 | continue;
|
---|
402 | }
|
---|
403 | if (ch == 'M' && *str == '^') {
|
---|
404 | str++;
|
---|
405 | value = 0200;
|
---|
406 | ch = '^';
|
---|
407 | } else
|
---|
408 | value = 0;
|
---|
409 | if (ch == '^') {
|
---|
410 | ch = *str++;
|
---|
411 | if (ch == '?')
|
---|
412 | value |= 0177;
|
---|
413 | else
|
---|
414 | value |= ch & 037;
|
---|
415 | do_putchar(value);
|
---|
416 | continue;
|
---|
417 | }
|
---|
418 |
|
---|
419 | /* Finally test for sequences valid in the format string */
|
---|
420 | str = conv_escape(str - 1, &c);
|
---|
421 | do_putchar(c);
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | /*
|
---|
426 | * Print "standard" escape characters
|
---|
427 | */
|
---|
428 | static char *
|
---|
429 | conv_escape(char *str, char *conv_ch)
|
---|
430 | {
|
---|
431 | int value;
|
---|
432 | int ch;
|
---|
433 | char num_buf[4], *num_end;
|
---|
434 |
|
---|
435 | ch = *str++;
|
---|
436 |
|
---|
437 | switch (ch) {
|
---|
438 | case '0': case '1': case '2': case '3':
|
---|
439 | case '4': case '5': case '6': case '7':
|
---|
440 | num_buf[0] = ch;
|
---|
441 | ch = str[0];
|
---|
442 | num_buf[1] = ch;
|
---|
443 | num_buf[2] = ch ? str[1] : 0;
|
---|
444 | num_buf[3] = 0;
|
---|
445 | value = strtoul(num_buf, &num_end, 8);
|
---|
446 | str += num_end - (num_buf + 1);
|
---|
447 | break;
|
---|
448 |
|
---|
449 | case 'x':
|
---|
450 | /* Hexadecimal character constants are not required to be
|
---|
451 | supported (by SuS v1) because there is no consistent
|
---|
452 | way to detect the end of the constant.
|
---|
453 | Supporting 2 byte constants is a compromise. */
|
---|
454 | ch = str[0];
|
---|
455 | num_buf[0] = ch;
|
---|
456 | num_buf[1] = ch ? str[1] : 0;
|
---|
457 | num_buf[2] = 0;
|
---|
458 | value = strtoul(num_buf, &num_end, 16);
|
---|
459 | str += num_end - num_buf;
|
---|
460 | break;
|
---|
461 |
|
---|
462 | case '\\': value = '\\'; break; /* backslash */
|
---|
463 | case '\'': value = '\''; break; /* single quote */
|
---|
464 | case '"': value = '"'; break; /* double quote */
|
---|
465 | case 'a': value = '\a'; break; /* alert */
|
---|
466 | case 'b': value = '\b'; break; /* backspace */
|
---|
467 | case 'e': value = ESCAPE; break; /* escape */
|
---|
468 | case 'f': value = '\f'; break; /* form-feed */
|
---|
469 | case 'n': value = '\n'; break; /* newline */
|
---|
470 | case 'r': value = '\r'; break; /* carriage-return */
|
---|
471 | case 't': value = '\t'; break; /* tab */
|
---|
472 | case 'v': value = '\v'; break; /* vertical-tab */
|
---|
473 |
|
---|
474 | default:
|
---|
475 | warnx("unknown escape sequence `\\%c'", ch);
|
---|
476 | rval = 1;
|
---|
477 | value = ch;
|
---|
478 | break;
|
---|
479 | }
|
---|
480 |
|
---|
481 | *conv_ch = value;
|
---|
482 | return str;
|
---|
483 | }
|
---|
484 |
|
---|
485 | /* expand a string so that everything is printable */
|
---|
486 |
|
---|
487 | static char *
|
---|
488 | conv_expand(const char *str)
|
---|
489 | {
|
---|
490 | static char *conv_str;
|
---|
491 | static char no_memory[] = "<no memory>";
|
---|
492 | char *cp;
|
---|
493 | int ch;
|
---|
494 |
|
---|
495 | if (conv_str)
|
---|
496 | free(conv_str);
|
---|
497 | /* get a buffer that is definitely large enough.... */
|
---|
498 | conv_str = malloc(4 * strlen(str) + 1);
|
---|
499 | if (!conv_str)
|
---|
500 | return no_memory;
|
---|
501 | cp = conv_str;
|
---|
502 |
|
---|
503 | while ((ch = *(const unsigned char *)str++) != '\0') {
|
---|
504 | switch (ch) {
|
---|
505 | /* Use C escapes for expected control characters */
|
---|
506 | case '\\': ch = '\\'; break; /* backslash */
|
---|
507 | case '\'': ch = '\''; break; /* single quote */
|
---|
508 | case '"': ch = '"'; break; /* double quote */
|
---|
509 | case '\a': ch = 'a'; break; /* alert */
|
---|
510 | case '\b': ch = 'b'; break; /* backspace */
|
---|
511 | case ESCAPE: ch = 'e'; break; /* escape */
|
---|
512 | case '\f': ch = 'f'; break; /* form-feed */
|
---|
513 | case '\n': ch = 'n'; break; /* newline */
|
---|
514 | case '\r': ch = 'r'; break; /* carriage-return */
|
---|
515 | case '\t': ch = 't'; break; /* tab */
|
---|
516 | case '\v': ch = 'v'; break; /* vertical-tab */
|
---|
517 | default:
|
---|
518 | /* Copy anything printable */
|
---|
519 | if (isprint(ch)) {
|
---|
520 | *cp++ = ch;
|
---|
521 | continue;
|
---|
522 | }
|
---|
523 | /* Use vis(3) encodings for the rest */
|
---|
524 | *cp++ = '\\';
|
---|
525 | if (ch & 0200) {
|
---|
526 | *cp++ = 'M';
|
---|
527 | ch &= ~0200;
|
---|
528 | }
|
---|
529 | if (ch == 0177) {
|
---|
530 | *cp++ = '^';
|
---|
531 | *cp++ = '?';
|
---|
532 | continue;
|
---|
533 | }
|
---|
534 | if (ch < 040) {
|
---|
535 | *cp++ = '^';
|
---|
536 | *cp++ = ch | 0100;
|
---|
537 | continue;
|
---|
538 | }
|
---|
539 | *cp++ = '-';
|
---|
540 | *cp++ = ch;
|
---|
541 | continue;
|
---|
542 | }
|
---|
543 | *cp++ = '\\';
|
---|
544 | *cp++ = ch;
|
---|
545 | }
|
---|
546 |
|
---|
547 | *cp = 0;
|
---|
548 | return conv_str;
|
---|
549 | }
|
---|
550 |
|
---|
551 | static char *
|
---|
552 | mklong(const char *str, int ch)
|
---|
553 | {
|
---|
554 | static char copy[64];
|
---|
555 | size_t len;
|
---|
556 |
|
---|
557 | len = strlen(str) + 2;
|
---|
558 | if (len > sizeof copy) {
|
---|
559 | warnx("format %s too complex\n", str);
|
---|
560 | len = 4;
|
---|
561 | }
|
---|
562 | (void)memmove(copy, str, len - 3);
|
---|
563 | copy[len - 3] = 'j';
|
---|
564 | copy[len - 2] = ch;
|
---|
565 | copy[len - 1] = '\0';
|
---|
566 | return copy;
|
---|
567 | }
|
---|
568 |
|
---|
569 | static int
|
---|
570 | getchr(void)
|
---|
571 | {
|
---|
572 | if (!*gargv)
|
---|
573 | return 0;
|
---|
574 | return (int)**gargv++;
|
---|
575 | }
|
---|
576 |
|
---|
577 | static char *
|
---|
578 | getstr(void)
|
---|
579 | {
|
---|
580 | static char empty[] = "";
|
---|
581 | if (!*gargv)
|
---|
582 | return empty;
|
---|
583 | return *gargv++;
|
---|
584 | }
|
---|
585 |
|
---|
586 | static int
|
---|
587 | getwidth(void)
|
---|
588 | {
|
---|
589 | long val;
|
---|
590 | char *s, *ep;
|
---|
591 |
|
---|
592 | s = *gargv;
|
---|
593 | if (!*gargv)
|
---|
594 | return (0);
|
---|
595 | gargv++;
|
---|
596 |
|
---|
597 | errno = 0;
|
---|
598 | val = strtoul(s, &ep, 0);
|
---|
599 | check_conversion(s, ep);
|
---|
600 |
|
---|
601 | /* Arbitrarily 'restrict' field widths to 1Mbyte */
|
---|
602 | if (val < 0 || val > 1 << 20) {
|
---|
603 | warnx("%s: invalid field width", s);
|
---|
604 | return 0;
|
---|
605 | }
|
---|
606 |
|
---|
607 | return val;
|
---|
608 | }
|
---|
609 |
|
---|
610 | static intmax_t
|
---|
611 | getintmax(void)
|
---|
612 | {
|
---|
613 | intmax_t val;
|
---|
614 | char *cp, *ep;
|
---|
615 |
|
---|
616 | cp = *gargv;
|
---|
617 | if (cp == NULL)
|
---|
618 | return 0;
|
---|
619 | gargv++;
|
---|
620 |
|
---|
621 | if (*cp == '\"' || *cp == '\'')
|
---|
622 | return *(cp+1);
|
---|
623 |
|
---|
624 | errno = 0;
|
---|
625 | val = strtoimax(cp, &ep, 0);
|
---|
626 | check_conversion(cp, ep);
|
---|
627 | return val;
|
---|
628 | }
|
---|
629 |
|
---|
630 | static uintmax_t
|
---|
631 | getuintmax(void)
|
---|
632 | {
|
---|
633 | uintmax_t val;
|
---|
634 | char *cp, *ep;
|
---|
635 |
|
---|
636 | cp = *gargv;
|
---|
637 | if (cp == NULL)
|
---|
638 | return 0;
|
---|
639 | gargv++;
|
---|
640 |
|
---|
641 | if (*cp == '\"' || *cp == '\'')
|
---|
642 | return *(cp + 1);
|
---|
643 |
|
---|
644 | /* strtoumax won't error -ve values */
|
---|
645 | while (isspace(*(unsigned char *)cp))
|
---|
646 | cp++;
|
---|
647 | if (*cp == '-') {
|
---|
648 | warnx("%s: expected positive numeric value", cp);
|
---|
649 | rval = 1;
|
---|
650 | return 0;
|
---|
651 | }
|
---|
652 |
|
---|
653 | errno = 0;
|
---|
654 | val = strtoumax(cp, &ep, 0);
|
---|
655 | check_conversion(cp, ep);
|
---|
656 | return val;
|
---|
657 | }
|
---|
658 |
|
---|
659 | static double
|
---|
660 | getdouble(void)
|
---|
661 | {
|
---|
662 | double val;
|
---|
663 | char *ep;
|
---|
664 |
|
---|
665 | if (!*gargv)
|
---|
666 | return (0.0);
|
---|
667 |
|
---|
668 | if (**gargv == '\"' || **gargv == '\'')
|
---|
669 | return (double) *((*gargv++)+1);
|
---|
670 |
|
---|
671 | errno = 0;
|
---|
672 | val = strtod(*gargv, &ep);
|
---|
673 | check_conversion(*gargv++, ep);
|
---|
674 | return val;
|
---|
675 | }
|
---|
676 |
|
---|
677 | static void
|
---|
678 | check_conversion(const char *s, const char *ep)
|
---|
679 | {
|
---|
680 | if (*ep) {
|
---|
681 | if (ep == s)
|
---|
682 | warnx("%s: expected numeric value", s);
|
---|
683 | else
|
---|
684 | warnx("%s: not completely converted", s);
|
---|
685 | rval = 1;
|
---|
686 | } else if (errno == ERANGE) {
|
---|
687 | warnx("%s: %s", s, strerror(ERANGE));
|
---|
688 | rval = 1;
|
---|
689 | }
|
---|
690 | }
|
---|
691 |
|
---|
692 | static int
|
---|
693 | usage(FILE *pf)
|
---|
694 | {
|
---|
695 | fprintf(pf, "usage: %s format [arg ...]\n"
|
---|
696 | " or: %s --help\n"
|
---|
697 | " or: %s --version\n",
|
---|
698 | g_progname, g_progname, g_progname);
|
---|
699 | return 1;
|
---|
700 | }
|
---|