Coverage Report

Created: 2025-12-14 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jq/src/jv_print.c
Line
Count
Source
1
#include <assert.h>
2
#include <stdio.h>
3
#include <float.h>
4
#include <string.h>
5
6
#ifdef WIN32
7
#include <windows.h>
8
#include <io.h>
9
#include <fileapi.h>
10
#endif
11
12
#include "jv.h"
13
#include "jv_dtoa.h"
14
#include "jv_dtoa_tsd.h"
15
#include "jv_unicode.h"
16
#include "jv_alloc.h"
17
#include "jv_private.h"
18
19
#ifndef MAX_PRINT_DEPTH
20
8.19M
#define MAX_PRINT_DEPTH (10000)
21
#endif
22
23
1.74M
#define ESC "\033"
24
#define COL(c) (ESC "[" c "m")
25
1.74M
#define COLRESET (ESC "[0m")
26
27
// Color table. See https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
28
// for how to choose these. The order is same as jv_kind definition, and
29
// the last color is used for object keys.
30
#define DEFAULT_COLORS \
31
  {COL("0;90"),    COL("0;39"),      COL("0;39"),     COL("0;39"),\
32
   COL("0;32"),    COL("1;39"),      COL("1;39"),     COL("1;34")};
33
static const char *const default_colors[] = DEFAULT_COLORS;
34
static const char *colors[] = DEFAULT_COLORS;
35
0
#define COLORS_LEN (sizeof(colors) / sizeof(colors[0]))
36
2.29k
#define FIELD_COLOR (colors[7])
37
38
static char *colors_buf = NULL;
39
0
int jq_set_colors(const char *code_str) {
40
0
  if (code_str == NULL)
41
0
    return 1;
42
43
  // the start of each color code in the env var, and the byte after the end of the last one
44
0
  const char *codes[COLORS_LEN + 1];
45
0
  size_t num_colors;
46
  // must be initialized before `goto default_colors`, used later to loop over every color
47
0
  size_t ci = 0;
48
49
0
  for (num_colors = 0;; num_colors++) {
50
0
    codes[num_colors] = code_str;
51
0
    code_str += strspn(code_str, "0123456789;");
52
0
    if (code_str[0] == '\0' || num_colors + 1 >= COLORS_LEN) {
53
0
      break;
54
0
    } else if (code_str[0] != ':') {
55
0
      return 0; // invalid character
56
0
    }
57
0
    code_str++;
58
0
  }
59
0
  if (codes[num_colors] != code_str) {
60
    // count the last color and store its end (plus one byte for consistency with starts)
61
    // an empty last color would be ignored (for cases like "" and "0:")
62
0
    num_colors++;
63
0
    codes[num_colors] = code_str + 1;
64
0
  } else if (num_colors == 0) {
65
0
    if (colors_buf != NULL) {
66
0
      jv_mem_free(colors_buf);
67
0
      colors_buf = NULL;
68
0
    }
69
0
    goto default_colors;
70
0
  }
71
72
0
  colors_buf = jv_mem_realloc(
73
0
    colors_buf,
74
    // add ESC '[' 'm' to each string
75
    // '\0' is already included in difference of codes
76
0
    codes[num_colors] - codes[0] + 3 * num_colors
77
0
  );
78
0
  char *cb = colors_buf;
79
0
  for (; ci < num_colors; ci++) {
80
0
    colors[ci] = cb;
81
0
    size_t len = codes[ci + 1] - 1 - codes[ci];
82
83
0
    cb[0] = ESC[0];
84
0
    cb[1] = '[';
85
0
    memcpy(cb + 2, codes[ci], len);
86
0
    cb[2 + len] = 'm';
87
0
    cb[3 + len] = '\0';
88
89
0
    cb += len + 4;
90
0
  }
91
0
  default_colors:
92
0
  for (; ci < COLORS_LEN; ci++)
93
0
    colors[ci] = default_colors[ci];
94
0
  return 1;
95
0
}
96
97
1.24G
static void put_buf(const char *s, int len, FILE *fout, jv *strout, int is_tty) {
98
1.24G
  if (strout) {
99
330M
    *strout = jv_string_append_buf(*strout, s, len);
100
910M
  } else {
101
#ifdef WIN32
102
  /* See util.h */
103
  if (is_tty) {
104
    wchar_t *ws;
105
    size_t wl;
106
    if (len == -1)
107
      len = strlen(s);
108
    wl = MultiByteToWideChar(CP_UTF8, 0, s, len, NULL, 0);
109
    ws = jv_mem_calloc(wl + 1, sizeof(*ws));
110
    if (!ws)
111
      return;
112
    wl = MultiByteToWideChar(CP_UTF8, 0, s, len, ws, wl + 1);
113
    ws[wl] = 0;
114
    WriteConsoleW((HANDLE)_get_osfhandle(fileno(fout)), ws, wl, NULL, NULL);
115
    free(ws);
116
  } else
117
    fwrite(s, 1, len, fout);
118
#else
119
910M
  fwrite(s, 1, len, fout);
120
910M
#endif
121
910M
  }
122
1.24G
}
123
124
1.12G
static void put_char(char c, FILE* fout, jv* strout, int T) {
125
1.12G
  put_buf(&c, 1, fout, strout, T);
126
1.12G
}
127
128
26.4M
static void put_str(const char* s, FILE* fout, jv* strout, int T) {
129
26.4M
  put_buf(s, strlen(s), fout, strout, T);
130
26.4M
}
131
132
1.71M
static void put_indent(int n, int flags, FILE* fout, jv* strout, int T) {
133
1.71M
  if (flags & JV_PRINT_TAB) {
134
352M
    while (n--)
135
351M
      put_char('\t', fout, strout, T);
136
891k
  } else {
137
891k
    n *= ((flags & (JV_PRINT_SPACE0 | JV_PRINT_SPACE1 | JV_PRINT_SPACE2)) >> 8);
138
542M
    while (n--)
139
541M
      put_char(' ', fout, strout, T);
140
891k
  }
141
1.71M
}
142
143
903k
static void jvp_dump_string(jv str, int ascii_only, FILE* F, jv* S, int T) {
144
903k
  assert(jv_get_kind(str) == JV_KIND_STRING);
145
903k
  const char* i = jv_string_value(str);
146
903k
  const char* end = i + jv_string_length_bytes(jv_copy(str));
147
903k
  const char* cstart;
148
903k
  int c = 0;
149
903k
  char buf[32];
150
903k
  put_char('"', F, S, T);
151
312M
  while ((i = jvp_utf8_next((cstart = i), end, &c))) {
152
311M
    assert(c != -1);
153
311M
    int unicode_escape = 0;
154
311M
    if (0x20 <= c && c <= 0x7E) {
155
      // printable ASCII
156
202M
      if (c == '"' || c == '\\') {
157
8.41M
        put_char('\\', F, S, T);
158
8.41M
      }
159
202M
      put_char(c, F, S, T);
160
202M
    } else if (c < 0x20 || c == 0x7F) {
161
      // ASCII control character
162
19.9M
      switch (c) {
163
1.08k
      case '\b':
164
1.08k
        put_char('\\', F, S, T);
165
1.08k
        put_char('b', F, S, T);
166
1.08k
        break;
167
14.4k
      case '\t':
168
14.4k
        put_char('\\', F, S, T);
169
14.4k
        put_char('t', F, S, T);
170
14.4k
        break;
171
5.99M
      case '\r':
172
5.99M
        put_char('\\', F, S, T);
173
5.99M
        put_char('r', F, S, T);
174
5.99M
        break;
175
55.0k
      case '\n':
176
55.0k
        put_char('\\', F, S, T);
177
55.0k
        put_char('n', F, S, T);
178
55.0k
        break;
179
283k
      case '\f':
180
283k
        put_char('\\', F, S, T);
181
283k
        put_char('f', F, S, T);
182
283k
        break;
183
13.5M
      default:
184
13.5M
        unicode_escape = 1;
185
13.5M
        break;
186
19.9M
      }
187
89.6M
    } else {
188
89.6M
      if (ascii_only) {
189
2.15M
        unicode_escape = 1;
190
87.5M
      } else {
191
87.5M
        put_buf(cstart, i - cstart, F, S, T);
192
87.5M
      }
193
89.6M
    }
194
311M
    if (unicode_escape) {
195
15.7M
      if (c <= 0xffff) {
196
15.7M
        snprintf(buf, sizeof(buf), "\\u%04x", c);
197
15.7M
      } else {
198
480
        c -= 0x10000;
199
480
        snprintf(buf, sizeof(buf), "\\u%04x\\u%04x",
200
480
                0xD800 | ((c & 0xffc00) >> 10),
201
480
                0xDC00 | (c & 0x003ff));
202
480
      }
203
15.7M
      put_str(buf, F, S, T);
204
15.7M
    }
205
311M
  }
206
903k
  assert(c != -1);
207
903k
  put_char('"', F, S, T);
208
903k
}
209
210
38.4k
static void put_refcnt(struct dtoa_context* C, int refcnt, FILE *F, jv* S, int T){
211
38.4k
  char buf[JVP_DTOA_FMT_MAX_LEN];
212
38.4k
  put_char(' ', F, S, T);
213
38.4k
  put_char('(', F, S, T);
214
38.4k
  put_str(jvp_dtoa_fmt(C, buf, refcnt), F, S, T);
215
38.4k
  put_char(')', F, S, T);
216
38.4k
}
217
218
8.19M
static void jv_dump_term(struct dtoa_context* C, jv x, int flags, int indent, FILE* F, jv* S) {
219
8.19M
  char buf[JVP_DTOA_FMT_MAX_LEN];
220
8.19M
  const char* color = 0;
221
8.19M
  double refcnt = (flags & JV_PRINT_REFCOUNT) ? jv_get_refcnt(x) - 1 : -1;
222
8.19M
  if ((flags & JV_PRINT_COLOR) && jv_get_kind(x) != JV_KIND_INVALID) {
223
872k
    color = colors[(int)jv_get_kind(x) - 1];
224
872k
    put_str(color, F, S, flags & JV_PRINT_ISATTY);
225
872k
  }
226
8.19M
  if (indent > MAX_PRINT_DEPTH) {
227
0
    put_str("<skipped: too deep>", F, S, flags & JV_PRINT_ISATTY);
228
8.19M
  } else switch (jv_get_kind(x)) {
229
0
  default:
230
0
  case JV_KIND_INVALID:
231
0
    if (flags & JV_PRINT_INVALID) {
232
0
      jv msg = jv_invalid_get_msg(jv_copy(x));
233
0
      if (jv_get_kind(msg) == JV_KIND_STRING) {
234
0
        put_str("<invalid:", F, S, flags & JV_PRINT_ISATTY);
235
0
        jvp_dump_string(msg, flags | JV_PRINT_ASCII, F, S, flags & JV_PRINT_ISATTY);
236
0
        put_char('>', F, S, flags & JV_PRINT_ISATTY);
237
0
      } else {
238
0
        put_str("<invalid>", F, S, flags & JV_PRINT_ISATTY);
239
0
      }
240
0
    } else {
241
0
      assert(0 && "Invalid value");
242
0
    }
243
0
    break;
244
2.50k
  case JV_KIND_NULL:
245
2.50k
    put_str("null", F, S, flags & JV_PRINT_ISATTY);
246
2.50k
    break;
247
75.9k
  case JV_KIND_FALSE:
248
75.9k
    put_str("false", F, S, flags & JV_PRINT_ISATTY);
249
75.9k
    break;
250
42.7k
  case JV_KIND_TRUE:
251
42.7k
    put_str("true", F, S, flags & JV_PRINT_ISATTY);
252
42.7k
    break;
253
7.01M
  case JV_KIND_NUMBER: {
254
7.01M
    if (jvp_number_is_nan(x)) {
255
1.94k
      jv_dump_term(C, jv_null(), flags, indent, F, S);
256
7.00M
    } else {
257
7.00M
#ifdef USE_DECNUM
258
7.00M
      const char * literal_data = jv_number_get_literal(x);
259
7.00M
      if (literal_data) {
260
4.53M
        put_str(literal_data, F, S, flags & JV_PRINT_ISATTY);
261
4.53M
      } else {
262
2.47M
#endif
263
2.47M
        double d = jv_number_value(x);
264
2.47M
        if (d != d) {
265
          // JSON doesn't have NaN, so we'll render it as "null"
266
0
          put_str("null", F, S, flags & JV_PRINT_ISATTY);
267
2.47M
        } else {
268
          // Normalise infinities to something we can print in valid JSON
269
2.47M
          if (d > DBL_MAX) d = DBL_MAX;
270
2.47M
          if (d < -DBL_MAX) d = -DBL_MAX;
271
2.47M
          put_str(jvp_dtoa_fmt(C, buf, d), F, S, flags & JV_PRINT_ISATTY);
272
2.47M
        }
273
2.47M
      }
274
7.00M
#ifdef USE_DECNUM
275
7.00M
    }
276
7.01M
#endif
277
7.01M
    break;
278
0
  }
279
883k
  case JV_KIND_STRING:
280
883k
    jvp_dump_string(x, flags & JV_PRINT_ASCII, F, S, flags & JV_PRINT_ISATTY);
281
883k
    if (flags & JV_PRINT_REFCOUNT)
282
731
      put_refcnt(C, refcnt, F, S, flags & JV_PRINT_ISATTY);
283
883k
    break;
284
171k
  case JV_KIND_ARRAY: {
285
171k
    if (jv_array_length(jv_copy(x)) == 0) {
286
10.5k
      put_str("[]", F, S, flags & JV_PRINT_ISATTY);
287
10.5k
      break;
288
10.5k
    }
289
161k
    put_char('[', F, S, flags & JV_PRINT_ISATTY);
290
7.13M
    jv_array_foreach(x, i, elem) {
291
7.13M
      if (i!=0) {
292
6.96M
        if (color) put_str(color, F, S, flags & JV_PRINT_ISATTY);
293
6.96M
        put_char(',', F, S, flags & JV_PRINT_ISATTY);
294
6.96M
      }
295
7.13M
      if (color) put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);
296
7.13M
      if (flags & JV_PRINT_PRETTY) {
297
1.60M
        put_char('\n', F, S, flags & JV_PRINT_ISATTY);
298
1.60M
        put_indent(indent + 1, flags, F, S, flags & JV_PRINT_ISATTY);
299
1.60M
      }
300
7.13M
      jv_dump_term(C, elem, flags, indent + 1, F, S);
301
7.13M
    }
302
161k
    if (flags & JV_PRINT_PRETTY) {
303
105k
      put_char('\n', F, S, flags & JV_PRINT_ISATTY);
304
105k
      put_indent(indent, flags, F, S, flags & JV_PRINT_ISATTY);
305
105k
    }
306
161k
    if (color) put_str(color, F, S, flags & JV_PRINT_ISATTY);
307
161k
    put_char(']', F, S, flags & JV_PRINT_ISATTY);
308
161k
    if (flags & JV_PRINT_REFCOUNT)
309
36.5k
      put_refcnt(C, refcnt, F, S, flags & JV_PRINT_ISATTY);
310
161k
    break;
311
171k
  }
312
7.40k
  case JV_KIND_OBJECT: {
313
7.40k
    if (jv_object_length(jv_copy(x)) == 0) {
314
4.04k
      put_str("{}", F, S, flags & JV_PRINT_ISATTY);
315
4.04k
      break;
316
4.04k
    }
317
3.36k
    put_char('{', F, S, flags & JV_PRINT_ISATTY);
318
3.36k
    int first = 1;
319
3.36k
    int i = 0;
320
3.36k
    jv keyset = jv_null();
321
23.1k
    while (1) {
322
23.1k
      jv key, value;
323
23.1k
      if (flags & JV_PRINT_SORTED) {
324
4.17k
        if (first) {
325
1.13k
          keyset = jv_keys(jv_copy(x));
326
1.13k
          i = 0;
327
3.04k
        } else {
328
3.04k
          i++;
329
3.04k
        }
330
4.17k
        if (i >= jv_array_length(jv_copy(keyset))) {
331
1.13k
          jv_free(keyset);
332
1.13k
          break;
333
1.13k
        }
334
3.04k
        key = jv_array_get(jv_copy(keyset), i);
335
3.04k
        value = jv_object_get(jv_copy(x), jv_copy(key));
336
18.9k
      } else {
337
18.9k
        if (first) {
338
2.23k
          i = jv_object_iter(x);
339
16.6k
        } else {
340
16.6k
          i = jv_object_iter_next(x, i);
341
16.6k
        }
342
18.9k
        if (!jv_object_iter_valid(x, i)) break;
343
16.6k
        key = jv_object_iter_key(x, i);
344
16.6k
        value = jv_object_iter_value(x, i);
345
16.6k
      }
346
347
19.7k
      if (!first) {
348
16.3k
        if (color) put_str(color, F, S, flags & JV_PRINT_ISATTY);
349
16.3k
        put_char(',', F, S, flags & JV_PRINT_ISATTY);
350
16.3k
      }
351
19.7k
      if (color) put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);
352
19.7k
      if (flags & JV_PRINT_PRETTY) {
353
2.56k
        put_char('\n', F, S, flags & JV_PRINT_ISATTY);
354
2.56k
        put_indent(indent + 1, flags, F, S, flags & JV_PRINT_ISATTY);
355
2.56k
      }
356
357
19.7k
      first = 0;
358
19.7k
      if (color) put_str(FIELD_COLOR, F, S, flags & JV_PRINT_ISATTY);
359
19.7k
      jvp_dump_string(key, flags & JV_PRINT_ASCII, F, S, flags & JV_PRINT_ISATTY);
360
19.7k
      jv_free(key);
361
19.7k
      if (color) put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);
362
363
19.7k
      if (color) put_str(color, F, S, flags & JV_PRINT_ISATTY);
364
19.7k
      put_char(':', F, S, flags & JV_PRINT_ISATTY);
365
19.7k
      if (color) put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);
366
19.7k
      if (flags & JV_PRINT_PRETTY) {
367
2.56k
        put_char(' ', F, S, flags & JV_PRINT_ISATTY);
368
2.56k
      }
369
370
19.7k
      jv_dump_term(C, value, flags, indent + 1, F, S);
371
19.7k
    }
372
3.36k
    if (flags & JV_PRINT_PRETTY) {
373
961
      put_char('\n', F, S, flags & JV_PRINT_ISATTY);
374
961
      put_indent(indent, flags, F, S, flags & JV_PRINT_ISATTY);
375
961
    }
376
3.36k
    if (color) put_str(color, F, S, flags & JV_PRINT_ISATTY);
377
3.36k
    put_char('}', F, S, flags & JV_PRINT_ISATTY);
378
3.36k
    if (flags & JV_PRINT_REFCOUNT)
379
1.09k
      put_refcnt(C, refcnt, F, S, flags & JV_PRINT_ISATTY);
380
3.36k
  }
381
8.19M
  }
382
8.19M
  jv_free(x);
383
8.19M
  if (color) {
384
872k
    put_str(COLRESET, F, S, flags & JV_PRINT_ISATTY);
385
872k
  }
386
8.19M
}
387
388
135k
void jv_dumpf(jv x, FILE *f, int flags) {
389
135k
  jv_dump_term(tsd_dtoa_context_get(), x, flags, 0, f, 0);
390
135k
}
391
392
135k
void jv_dump(jv x, int flags) {
393
135k
  jv_dumpf(x, stdout, flags);
394
135k
}
395
396
/* This one is nice for use in debuggers */
397
void jv_show(jv x, int flags) {
398
  if (flags == -1)
399
    flags = JV_PRINT_PRETTY | JV_PRINT_COLOR | JV_PRINT_INDENT_FLAGS(2);
400
  jv_dumpf(jv_copy(x), stderr, flags | JV_PRINT_INVALID);
401
  fflush(stderr);
402
}
403
404
907k
jv jv_dump_string(jv x, int flags) {
405
907k
  jv s = jv_string("");
406
907k
  jv_dump_term(tsd_dtoa_context_get(), x, flags, 0, 0, &s);
407
907k
  return s;
408
907k
}
409
410
728k
char *jv_dump_string_trunc(jv x, char *outbuf, size_t bufsize) {
411
728k
  x = jv_dump_string(x, 0);
412
728k
  const char *str = jv_string_value(x);
413
728k
  const size_t len = strlen(str);
414
728k
  strncpy(outbuf, str, bufsize);
415
728k
  if (len > bufsize - 1 && bufsize >= 4) {
416
    // Indicate truncation with '...' without breaking UTF-8.
417
458k
    const char *s = jvp_utf8_backtrack(outbuf + bufsize - 4, outbuf, NULL);
418
458k
    if (s) bufsize = s + 4 - outbuf;
419
458k
    strcpy(outbuf + bufsize - 4, "...");
420
458k
  } else {
421
270k
    outbuf[bufsize - 1] = '\0';
422
270k
  }
423
728k
  jv_free(x);
424
728k
  return outbuf;
425
728k
}