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