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