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