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