• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nickg / nvc / 22274574463

22 Feb 2026 09:32AM UTC coverage: 92.515% (-0.03%) from 92.546%
22274574463

push

github

nickg
Fix quoting problems in install scripts

Issue #1419

77027 of 83259 relevant lines covered (92.51%)

442605.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.28
/src/printf.c
1
//
2
//  Copyright (C) 2025-2026  Nick Gasson
3
//
4
//  This program is free software: you can redistribute it and/or modify
5
//  it under the terms of the GNU General Public License as published by
6
//  the Free Software Foundation, either version 3 of the License, or
7
//  (at your option) any later version.
8
//
9
//  This program is distributed in the hope that it will be useful,
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
//  GNU General Public License for more details.
13
//
14
//  You should have received a copy of the GNU General Public License
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

18
#include "util.h"
19
#include "ident.h"
20
#include "mir/mir-node.h"
21
#include "object.h"
22
#include "printf.h"
23
#include "thread.h"
24
#include "type.h"
25

26
#include <assert.h>
27
#include <stdio.h>
28
#include <string.h>
29
#include <stdlib.h>
30
#include <unistd.h>
31
#include <limits.h>
32

33
#define ANSI_RESET      0
34
#define ANSI_BOLD       1
35
#define ANSI_FG_BLACK   30
36
#define ANSI_FG_RED     31
37
#define ANSI_FG_GREEN   32
38
#define ANSI_FG_YELLOW  33
39
#define ANSI_FG_BLUE    34
40
#define ANSI_FG_MAGENTA 35
41
#define ANSI_FG_CYAN    36
42
#define ANSI_FG_WHITE   37
43

44
#define MAX_ARGS 25
45

46
typedef struct _printf_state printf_state_t;
47
typedef struct _printf_arg printf_arg_t;
48

49
typedef union {
50
   long long  ll;
51
   long       l;
52
   size_t     z;
53
   double     f;
54
   int        i;
55
   void      *p;
56
} printf_value_t;
57

58
typedef int (*fmt_fn_t)(ostream_t *, printf_state_t *, printf_arg_t *);
59

60
typedef struct _printf_arg {
61
   const char     *start;
62
   size_t          len;
63
   fmt_fn_t        fn;
64
   printf_value_t  value;
65
   int             precision;
66
} printf_arg_t;
67

68
typedef struct _printf_state {
69
   printf_arg_t args[MAX_ARGS];
70
   unsigned     nargs;
71
   unsigned     pos;
72
} printf_state_t;
73

74
typedef struct {
75
   const char *name;
76
   int         value;
77
} color_escape_t;
78

79
static const color_escape_t escapes[] = {
80
   { "",        ANSI_RESET },
81
   { "bold",    ANSI_BOLD },
82
   { "black",   ANSI_FG_BLACK },
83
   { "red",     ANSI_FG_RED },
84
   { "green",   ANSI_FG_GREEN },
85
   { "yellow",  ANSI_FG_YELLOW },
86
   { "blue",    ANSI_FG_BLUE },
87
   { "magenta", ANSI_FG_MAGENTA },
88
   { "cyan",    ANSI_FG_CYAN },
89
   { "white",   ANSI_FG_WHITE },
90
};
91

92
static int printf_interpret(ostream_t *os, printf_state_t *state,
93
                            const char *fmt, const char *end);
94

95
int ostream_write(ostream_t *os, const char *buf, size_t len)
1,600,691✔
96
{
97
   (*os->callback)(buf, len, os->context);
1,600,691✔
98
   return len;
1,600,691✔
99
}
100

101
int ostream_putc(ostream_t *os, char ch)
597,566✔
102
{
103
   char buf[1] = { ch };
597,566✔
104
   return ostream_write(os, buf, 1);
597,566✔
105
}
106

107
int ostream_puts(ostream_t *os, const char *str)
7,414✔
108
{
109
   if (str == NULL)
7,414✔
110
      return ostream_write(os, "(null)", 6);
×
111
   else
112
      return ostream_write(os, str, strlen(str));
7,414✔
113
}
114

115
static int format_ident(ostream_t *os, printf_state_t *state, printf_arg_t *arg)
56✔
116
{
117
   ident_t id = arg->value.p;
56✔
118
   return ostream_write(os, istr(id), ident_len(id));
56✔
119
}
120

121
static int format_ident_toupper(ostream_t *os, printf_state_t *state,
191✔
122
                                printf_arg_t *arg)
123
{
124
   ident_t id = arg->value.p;
191✔
125
   size_t len = ident_len(id);
191✔
126
   const char *str = istr(id);
191✔
127
   int nchars = 0;
191✔
128
   char chunk[32];
191✔
129

130
   for (int i = 0; i < len; i += sizeof(chunk)) {
382✔
131
      const int tocopy = MIN(sizeof(chunk), len - i);
191✔
132
      for (int j = 0; j < tocopy; j++)
838✔
133
         chunk[j] = toupper_iso88591(str[i + j]);
647✔
134

135
      nchars += ostream_write(os, chunk, tocopy);
191✔
136
   }
137

138
   return nchars;
191✔
139
}
140

141
static int format_type(ostream_t *os, printf_state_t *state, printf_arg_t *arg)
216✔
142
{
143
   type_t type = arg->value.p;
216✔
144

145
   // Print a fully qualified name if there is another type in the
146
   // argument list with the same simple name
147

148
   for (int i = 0; i < state->nargs; i++) {
415✔
149
      const printf_arg_t *other = &(state->args[i]);
333✔
150
      if (other != arg && other->fn == format_type)
333✔
151
         return ostream_puts(os, type_pp2(type, other->value.p));
134✔
152
   }
153

154
   return ostream_puts(os, type_pp(type));
82✔
155
}
156

157
static int format_object_kind(ostream_t *os, printf_state_t *state,
×
158
                              printf_arg_t *arg)
159
{
160
   object_t *obj = arg->value.p;
×
161
   return ostream_puts(os, object_kind_str(obj));
×
162
}
163

164
static int format_mir(ostream_t *os, printf_state_t *state, printf_arg_t *arg)
9✔
165
{
166
   mir_value_t *value = arg->value.p;
9✔
167
   char buf[64];
9✔
168

169
   switch (value->tag) {
9✔
170
   case MIR_TAG_NODE:
×
171
      checked_sprintf(buf, sizeof(buf), "%%%d", value->id);
×
172
      break;
×
173
   case MIR_TAG_CONST:
9✔
174
      checked_sprintf(buf, sizeof(buf), "#%d",
9✔
175
                      value->id - (1 << (_MIR_ID_BITS - 1)));
9✔
176
      break;
9✔
177
   default:
×
178
      checked_sprintf(buf, sizeof(buf), "{tag:%d, id:%x}",
×
179
                      value->tag, value->id);
×
180
      break;
×
181
   }
182

183
   return ostream_puts(os, buf);
9✔
184
}
185

186
static int delegate(ostream_t *os, printf_state_t *s, printf_arg_t *arg, ...)
509,951✔
187
{
188
   char spec[32];
509,951✔
189
   assert(arg->len + 1 < sizeof(spec));
509,951✔
190
   memcpy(spec, arg->start, arg->len);
509,951✔
191
   spec[arg->len] = '\0';
509,951✔
192

193
   va_list ap, ap2;
509,951✔
194
   va_start(ap, arg);
509,951✔
195
   va_copy(ap2, ap);
509,951✔
196

197
   char small[64];
509,951✔
198
   int req = vsnprintf(small, sizeof(small), spec, ap);
509,951✔
199

200
   if (req + 1 > sizeof(small)) {
509,951✔
201
      char *large = xmalloc(req + 1);
650✔
202
      vsnprintf(large, req + 1, spec, ap2);
650✔
203
      ostream_write(os, large, req);
650✔
204
      free(large);
650✔
205
   }
206
   else
207
      ostream_write(os, small, req);
509,301✔
208

209
   va_end(ap);
509,951✔
210
   va_end(ap2);
509,951✔
211
   return req;
509,951✔
212
}
213

214
static fmt_fn_t get_pointer_formatter(char ch)
476✔
215
{
216
   switch (ch) {
476✔
217
   case 'i': return format_ident;
218
   case 'I': return format_ident_toupper;
191✔
219
   case 'T': return format_type;
216✔
220
   case 'K': return format_object_kind;
×
221
   case 'M': return format_mir;
9✔
222
   default: return NULL;
4✔
223
   }
224
}
225

226
static int ansi_escape(ostream_t *os, printf_state_t *state, const char **fmt)
152,228✔
227
{
228
   bool has_format = false;
152,228✔
229
   const char *start = *fmt;
152,228✔
230
   do {
799,537✔
231
      has_format |= (**fmt == '%');
799,537✔
232
      (*fmt)++;
799,537✔
233
   } while (**fmt != '\0' && **fmt != '$');
799,537✔
234

235
   if (**fmt == '\0')
152,228✔
236
      return ostream_write(os, start, *fmt - start);
73✔
237

238
   const char *end = *fmt;
152,155✔
239
   (*fmt)++;   // Advance past final '$'
152,155✔
240

241
   size_t len = len = *fmt - start - 2;
152,155✔
242
   const char *e = e = start + 1;
152,155✔
243
   LOCAL_TEXT_BUF tb = NULL;
304,310✔
244

245
   if (has_format) {
152,155✔
246
      // Expand any embedded formatting inside the ANSI escape
247
      tb = tb_new();
16,408✔
248

249
      ostream_t aos = {
16,408✔
250
         tb_ostream_write,
251
         tb,
252
         os->charset,
16,408✔
253
         false,
254
      };
255

256
      printf_interpret(&aos, state, start + 1, end);
16,408✔
257

258
      e = tb_get(tb);
16,408✔
259
      len = tb_len(tb);
16,408✔
260
   }
261

262
   bool bold;
152,155✔
263
   if ((bold = (*e == '!')))
152,155✔
264
      ++e, --len;
5✔
265

266
   bool bright;
152,155✔
267
   if ((bright = (*e == '+')))
152,155✔
268
      ++e, --len;
29✔
269

270
   if (*e == '#') {
152,155✔
271
      char *eptr;
1✔
272
      int code = strtoul(e + 1, &eptr, 10);
1✔
273
      if (eptr == e + len) {
1✔
274
         char buf[16];
1✔
275
         if (bold)
1✔
276
            checked_sprintf(buf, sizeof(buf), "\033[1;38;5;%dm", code);
×
277
         else
278
            checked_sprintf(buf, sizeof(buf), "\033[38;5;%dm", code);
1✔
279

280
         if (os->terminal)
1✔
281
            ostream_puts(os, buf);
1✔
282

283
         return 0;
1✔
284
      }
285
   }
286

287
   if (strncmp(e, "link:", 5) == 0) {
152,154✔
288
      const char *bel = strchr(e, '\07');
16,409✔
289

290
#ifndef __MINGW32__    // Winpty doesn't recognise these
291
      if (os->terminal) {
16,409✔
292
         ostream_puts(os, "\033]8;;");
2✔
293
         ostream_write(os, e + 5, len - 5);
2✔
294
         ostream_puts(os, "\033]8;;\07");
2✔
295
         return e + len - bel - 1;
2✔
296
      }
297
#endif
298

299
      return ostream_write(os, bel + 1, e + len - bel - 1);
16,407✔
300
   }
301

302
   for (int i = 0; i < ARRAY_LEN(escapes); i++) {
513,650✔
303
      if (strncmp(e, escapes[i].name, len) == 0) {
513,648✔
304
         int code = escapes[i].value + (bright ? 60 : 0);
135,743✔
305
         char buf[16];
135,743✔
306
         if (bold)
135,743✔
307
            checked_sprintf(buf, sizeof(buf), "\033[1;%dm", code);
4✔
308
         else
309
            checked_sprintf(buf, sizeof(buf), "\033[%dm", code);
135,739✔
310

311
         if (os->terminal)
135,743✔
312
            ostream_puts(os, buf);
5✔
313

314
         return 0;
135,743✔
315
      }
316
   }
317

318
   return ostream_write(os, start, *fmt - start);
2✔
319
}
320

321
static int format_z(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
121,037✔
322
{
323
   assert(arg->precision == INT_MIN);
121,037✔
324
   return delegate(os, s, arg, arg->value.z);
121,037✔
325
}
326

327
static int format_ll(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
×
328
{
329
   if (arg->precision != INT_MIN)
×
330
      return delegate(os, s, arg, arg->precision, arg->value.ll);
×
331
   else
332
      return delegate(os, s, arg, arg->value.ll);
×
333
}
334

335
static int format_l(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
8,949✔
336
{
337
   if (arg->precision != INT_MIN)
8,949✔
338
      return delegate(os, s, arg, arg->precision, arg->value.l);
6,232✔
339
   else
340
      return delegate(os, s, arg, arg->value.l);
2,717✔
341
}
342

343
static int format_i(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
66,638✔
344
{
345
   if (arg->precision != INT_MIN)
66,638✔
346
      return delegate(os, s, arg, arg->precision, arg->value.i);
10,145✔
347
   else
348
      return delegate(os, s, arg, arg->value.i);
56,493✔
349
}
350

351
static int format_f(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
656✔
352
{
353
   if (arg->precision != INT_MIN)
656✔
354
      return delegate(os, s, arg, arg->precision, arg->value.f);
7✔
355
   else
356
      return delegate(os, s, arg, arg->value.f);
649✔
357
}
358

359
static int format_s(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
312,667✔
360
{
361
   if (arg->precision != INT_MIN)
312,667✔
362
      return delegate(os, s, arg, arg->precision, arg->value.p);
41,213✔
363
   else
364
      return delegate(os, s, arg, arg->value.p);
271,454✔
365
}
366

367
static int format_p(ostream_t *os, printf_state_t *s, printf_arg_t *arg)
4✔
368
{
369
   return delegate(os, s, arg, arg->value.p);
4✔
370
}
371

372
static int printf_interpret(ostream_t *os, printf_state_t *state,
438,874✔
373
                            const char *fmt, const char *end)
374
{
375
   int nchars = 0;
438,874✔
376
   for (const char *p = fmt;;) {
438,874✔
377
      const char *start = p;
1,109,429✔
378
      while (p < end && *p != '%' && *p != '$')
2,643,490✔
379
         p++;
1,534,061✔
380

381
      if (start < p)
1,109,429✔
382
         nchars += ostream_write(os, start, p - start);
438,693✔
383

384
      if (p == end)
1,109,429✔
385
         return nchars;
438,874✔
386
      else if (*p == '$') {
670,555✔
387
         nchars += ansi_escape(os, state, &p);
152,228✔
388
         continue;
152,228✔
389
      }
390
      else if (*p == '%' && *(p + 1) == '%') {
518,327✔
391
         nchars += ostream_write(os, "%", 1);
7,904✔
392
         p += 2;
7,904✔
393
         continue;
7,904✔
394
      }
395

396
      assert(state->pos < state->nargs);
510,423✔
397

398
      printf_arg_t *arg = &(state->args[state->pos]);
510,423✔
399
      nchars += (*arg->fn)(os, state, arg);
510,423✔
400
      p += arg->len;
510,423✔
401

402
      state->pos++;
510,423✔
403
   }
404
}
405

406
int nvc_vfprintf(ostream_t *os, const char *fmt, va_list ap)
422,466✔
407
{
408
   printf_state_t state = {};
422,466✔
409

410
   const char *end = NULL;
422,466✔
411
   for (const char *p = fmt;;) {
422,466✔
412
      while (*p != '\0' && *p != '%')
3,049,175✔
413
         p++;
2,108,382✔
414

415
      if (*p == '\0') {
940,793✔
416
         end = p;
422,466✔
417
         break;
422,466✔
418
      }
419

420
      printf_arg_t arg = { .start = p, .precision = INT_MIN };
518,327✔
421
      bool z_mod = false;
518,327✔
422
      int l_mod = 0;
518,327✔
423
   again:
724,275✔
424
      switch (*++p) {
724,275✔
425
      case 'l':
8,951✔
426
         l_mod++;
8,951✔
427
         goto again;
8,951✔
428
      case 'z':
121,037✔
429
         z_mod = true;
121,037✔
430
         goto again;
121,037✔
431
      case '-':
18,363✔
432
      case '+':
433
      case '.':
434
      case '0'...'9':
435
         goto again;
18,363✔
436
      case '*':
57,597✔
437
         arg.precision = va_arg(ap, int);
57,597✔
438
         goto again;
57,597✔
439
      case 'd':
195,567✔
440
      case 'i':
441
      case 'x':
442
      case 'u':
443
         if (z_mod) {
195,567✔
444
            arg.value.z = va_arg(ap, size_t);
121,037✔
445
            arg.fn = format_z;
121,037✔
446
         }
447
         else if (l_mod >= 2) {
74,530✔
448
            arg.value.ll = va_arg(ap, long long);
×
449
            arg.fn = format_ll;
×
450
         }
451
         else if (l_mod == 1) {
74,530✔
452
            arg.value.l = va_arg(ap, long);
8,949✔
453
            arg.fn = format_l;
8,949✔
454
         }
455
         else {
456
            arg.value.i = va_arg(ap, int);
65,581✔
457
            arg.fn = format_i;
65,581✔
458
         }
459
         break;
460
      case 'c':
1,057✔
461
         arg.value.i = va_arg(ap, int);
1,057✔
462
         arg.fn = format_i;
1,057✔
463
         break;
1,057✔
464
      case 'e':
656✔
465
      case 'f':
466
      case 'g':
467
         arg.value.f = va_arg(ap, double);
656✔
468
         arg.fn = format_f;
656✔
469
         break;
656✔
470
      case 's':
312,667✔
471
         arg.value.p = va_arg(ap, char *);
312,667✔
472
         arg.fn = format_s;
312,667✔
473
         break;
312,667✔
474
      case 'p':
476✔
475
         arg.value.p = va_arg(ap, void *);
476✔
476
         if ((arg.fn = get_pointer_formatter(p[1])))
476✔
477
            p++;
472✔
478
         else
479
            arg.fn = format_p;
480
         break;
481
      case '%':
7,904✔
482
         p++;
7,904✔
483
         continue;
7,904✔
484
      default:
×
485
         fatal_trace("unhandled character '%c' in format", *p);
486
      }
487

488
      arg.len = ++p - arg.start;
510,423✔
489

490
      if (state.nargs == MAX_ARGS)
510,423✔
491
         fatal_trace("maximum of %d printf arguments supported", MAX_ARGS);
492
      else
493
         state.args[state.nargs++] = arg;
510,423✔
494
   }
495

496
   return printf_interpret(os, &state, fmt, end);
844,932✔
497
}
498

499
int nvc_vprintf(const char *fmt, va_list ap)
10✔
500
{
501
   return nvc_vfprintf(nvc_stdout(), fmt, ap);
10✔
502
}
503

504
int nvc_printf(const char *fmt, ...)
10✔
505
{
506
   va_list ap;
10✔
507
   va_start(ap, fmt);
10✔
508
   const int nchars = nvc_vprintf(fmt, ap);
10✔
509
   va_end(ap);
10✔
510
   return nchars;
10✔
511
}
512

513
int nvc_fprintf(ostream_t *os, const char *fmt, ...)
127,861✔
514
{
515
   va_list ap;
127,861✔
516
   va_start(ap, fmt);
127,861✔
517
   const int nchars = nvc_vfprintf(os, fmt, ap);
127,861✔
518
   va_end(ap);
127,861✔
519
   return nchars;
127,861✔
520
}
521

522
void stdio_ostream_write(const char *buf, size_t len, void *ctx)
867,491✔
523
{
524
   FILE *f = ctx;
867,491✔
525
   fwrite(buf, len, 1, f);
867,491✔
526
}
867,491✔
527

528
static void init_terminal_ostream(ostream_t *os, FILE *f)
7✔
529
{
530
   os->callback = stdio_ostream_write;
7✔
531
   os->context = f;
7✔
532

533
   if (isatty(fileno(f))) {
7✔
534
      os->charset = utf8_terminal() ? CHARSET_UTF8 : CHARSET_ISO88591;
×
535
      os->terminal = color_terminal();
×
536
   }
537
   else {
538
      os->charset = CHARSET_ISO88591;
7✔
539
      os->terminal = false;
7✔
540
   }
541
}
7✔
542

543
ostream_t *nvc_stdout(void)
16✔
544
{
545
   static ostream_t os;
16✔
546
   INIT_ONCE(init_terminal_ostream(&os, stdout));
16✔
547
   return &os;
16✔
548
}
549

550
ostream_t *nvc_stderr(void)
51✔
551
{
552
   static ostream_t os;
51✔
553
   INIT_ONCE(init_terminal_ostream(&os, stderr));
51✔
554
   return &os;
51✔
555
}
556

557
char *color_asprintf(const char *fmt, ...)
23✔
558
{
559
   LOCAL_TEXT_BUF tb = tb_new();
46✔
560
   ostream_t os = { tb_ostream_write, tb, CHARSET_ISO88591, color_terminal() };
23✔
561

562
   va_list ap;
23✔
563
   va_start(ap, fmt);
23✔
564
   nvc_vfprintf(&os, fmt, ap);
23✔
565
   va_end(ap);
23✔
566

567
   return tb_claim(tb);
23✔
568
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc