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