1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
// SPDX-License-Identifier: ISC
/*
* Copyright (c) 2019 David Lamparter, for NetDEF, Inc.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "compiler.h"
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "printfrr.h"
#include "monotime.h"
printfrr_ext_autoreg_p("HX", printfrr_hexdump);
static ssize_t printfrr_hexdump(struct fbuf *buf, struct printfrr_eargs *ea,
const void *ptr)
{
ssize_t ret = 0;
ssize_t input_len = printfrr_ext_len(ea);
char sep = ' ';
const uint8_t *pos, *end;
if (ea->fmt[0] == 'c') {
ea->fmt++;
sep = ':';
} else if (ea->fmt[0] == 'n') {
ea->fmt++;
sep = '\0';
}
if (input_len < 0)
return 0;
for (pos = ptr, end = pos + input_len; pos < end; pos++) {
if (sep && pos != ptr)
ret += bputch(buf, sep);
ret += bputhex(buf, *pos);
}
return ret;
}
/* string analog for hexdumps / the "this." in ("74 68 69 73 0a |this.|") */
printfrr_ext_autoreg_p("HS", printfrr_hexdstr);
static ssize_t printfrr_hexdstr(struct fbuf *buf, struct printfrr_eargs *ea,
const void *ptr)
{
ssize_t ret = 0;
ssize_t input_len = printfrr_ext_len(ea);
const uint8_t *pos, *end;
if (input_len < 0)
return 0;
for (pos = ptr, end = pos + input_len; pos < end; pos++) {
if (*pos >= 0x20 && *pos < 0x7f)
ret += bputch(buf, *pos);
else
ret += bputch(buf, '.');
}
return ret;
}
enum escape_flags {
ESC_N_R_T = (1 << 0), /* use \n \r \t instead of \x0a ...*/
ESC_SPACE = (1 << 1), /* \ */
ESC_BACKSLASH = (1 << 2), /* \\ */
ESC_DBLQUOTE = (1 << 3), /* \" */
ESC_SGLQUOTE = (1 << 4), /* \' */
ESC_BACKTICK = (1 << 5), /* \` */
ESC_DOLLAR = (1 << 6), /* \$ */
ESC_CLBRACKET = (1 << 7), /* \] for RFC5424 syslog */
ESC_OTHER = (1 << 8), /* remaining non-alpha */
ESC_ALL = ESC_N_R_T | ESC_SPACE | ESC_BACKSLASH | ESC_DBLQUOTE
| ESC_SGLQUOTE | ESC_DOLLAR | ESC_OTHER,
ESC_QUOTSTRING = ESC_N_R_T | ESC_BACKSLASH | ESC_DBLQUOTE,
/* if needed: ESC_SHELL = ... */
};
static ssize_t bquote(struct fbuf *buf, const uint8_t *pos, size_t len,
unsigned int flags)
{
ssize_t ret = 0;
const uint8_t *end = pos + len;
for (; pos < end; pos++) {
/* here's to hoping this might be a bit faster... */
if (__builtin_expect(!!isalnum(*pos), 1)) {
ret += bputch(buf, *pos);
continue;
}
switch (*pos) {
case '%':
case '+':
case ',':
case '-':
case '.':
case '/':
case ':':
case '@':
case '_':
ret += bputch(buf, *pos);
continue;
case '\r':
if (!(flags & ESC_N_R_T))
break;
ret += bputch(buf, '\\');
ret += bputch(buf, 'r');
continue;
case '\n':
if (!(flags & ESC_N_R_T))
break;
ret += bputch(buf, '\\');
ret += bputch(buf, 'n');
continue;
case '\t':
if (!(flags & ESC_N_R_T))
break;
ret += bputch(buf, '\\');
ret += bputch(buf, 't');
continue;
case ' ':
if (flags & ESC_SPACE)
ret += bputch(buf, '\\');
ret += bputch(buf, *pos);
continue;
case '\\':
if (flags & ESC_BACKSLASH)
ret += bputch(buf, '\\');
ret += bputch(buf, *pos);
continue;
case '"':
if (flags & ESC_DBLQUOTE)
ret += bputch(buf, '\\');
ret += bputch(buf, *pos);
continue;
case '\'':
if (flags & ESC_SGLQUOTE)
ret += bputch(buf, '\\');
ret += bputch(buf, *pos);
continue;
case '`':
if (flags & ESC_BACKTICK)
ret += bputch(buf, '\\');
ret += bputch(buf, *pos);
continue;
case '$':
if (flags & ESC_DOLLAR)
ret += bputch(buf, '\\');
ret += bputch(buf, *pos);
continue;
case ']':
if (flags & ESC_CLBRACKET)
ret += bputch(buf, '\\');
ret += bputch(buf, *pos);
continue;
/* remaining: !#&'()*;<=>?[^{|}~ */
default:
if (*pos >= 0x20 && *pos < 0x7f) {
if (flags & ESC_OTHER)
ret += bputch(buf, '\\');
ret += bputch(buf, *pos);
continue;
}
}
ret += bputch(buf, '\\');
ret += bputch(buf, 'x');
ret += bputhex(buf, *pos);
}
return ret;
}
printfrr_ext_autoreg_p("SE", printfrr_escape);
static ssize_t printfrr_escape(struct fbuf *buf, struct printfrr_eargs *ea,
const void *vptr)
{
ssize_t len = printfrr_ext_len(ea);
const uint8_t *ptr = vptr;
bool null_is_empty = false;
if (ea->fmt[0] == 'n') {
null_is_empty = true;
ea->fmt++;
}
if (!ptr) {
if (null_is_empty)
return 0;
return bputs(buf, "(null)");
}
if (len < 0)
len = strlen((const char *)ptr);
return bquote(buf, ptr, len, ESC_ALL);
}
printfrr_ext_autoreg_p("SQ", printfrr_quote);
static ssize_t printfrr_quote(struct fbuf *buf, struct printfrr_eargs *ea,
const void *vptr)
{
ssize_t len = printfrr_ext_len(ea);
const uint8_t *ptr = vptr;
ssize_t ret = 0;
bool null_is_empty = false;
bool do_quotes = false;
unsigned int flags = ESC_QUOTSTRING;
while (ea->fmt[0]) {
switch (ea->fmt[0]) {
case 'n':
null_is_empty = true;
ea->fmt++;
continue;
case 'q':
do_quotes = true;
ea->fmt++;
continue;
case 's':
flags |= ESC_CLBRACKET;
flags &= ~ESC_N_R_T;
ea->fmt++;
continue;
}
break;
}
if (!ptr) {
if (null_is_empty)
return bputs(buf, do_quotes ? "\"\"" : "");
return bputs(buf, "(null)");
}
if (len < 0)
len = strlen((const char *)ptr);
if (do_quotes)
ret += bputch(buf, '"');
ret += bquote(buf, ptr, len, flags);
if (do_quotes)
ret += bputch(buf, '"');
return ret;
}
static ssize_t printfrr_abstime(struct fbuf *buf, struct printfrr_eargs *ea,
const struct timespec *ts, unsigned int flags);
static ssize_t printfrr_reltime(struct fbuf *buf, struct printfrr_eargs *ea,
const struct timespec *ts, unsigned int flags);
ssize_t printfrr_time(struct fbuf *buf, struct printfrr_eargs *ea,
const struct timespec *ts, unsigned int flags)
{
bool have_abs, have_anchor;
if (!(flags & TIMEFMT_PRESELECT)) {
switch (ea->fmt[0]) {
case 'I':
/* no bit set */
break;
case 'M':
flags |= TIMEFMT_MONOTONIC;
break;
case 'R':
flags |= TIMEFMT_REALTIME;
break;
default:
return bputs(buf,
"{invalid time format input specifier}");
}
ea->fmt++;
if (ea->fmt[0] == 's') {
flags |= TIMEFMT_SINCE;
ea->fmt++;
} else if (ea->fmt[0] == 'u') {
flags |= TIMEFMT_UNTIL;
ea->fmt++;
}
}
have_abs = !!(flags & TIMEFMT_ABSOLUTE);
have_anchor = !!(flags & TIMEFMT_ANCHORS);
if (have_abs ^ have_anchor)
return printfrr_abstime(buf, ea, ts, flags);
else
return printfrr_reltime(buf, ea, ts, flags);
}
static ssize_t do_subsec(struct fbuf *buf, const struct timespec *ts,
int precision, unsigned int flags)
{
unsigned long long frac;
if (precision <= 0 || (flags & TIMEFMT_SECONDS))
return 0;
frac = ts->tv_nsec;
if (precision > 9)
precision = 9;
for (int i = precision; i < 9; i++)
frac /= 10;
return bprintfrr(buf, ".%0*llu", precision, frac);
}
static ssize_t printfrr_abstime(struct fbuf *buf, struct printfrr_eargs *ea,
const struct timespec *ts, unsigned int flags)
{
struct timespec real_ts[1];
struct tm tm;
char cbuf[32] = ""; /* manpage says 26 for ctime_r */
ssize_t ret = 0;
int precision = ea->precision;
while (ea->fmt[0]) {
char ch = *ea->fmt++;
switch (ch) {
case 'p':
flags |= TIMEFMT_SPACE;
continue;
case 'i':
flags |= TIMEFMT_ISO8601;
continue;
}
ea->fmt--;
break;
}
if (flags & TIMEFMT_SKIP)
return 0;
if (!ts)
return bputch(buf, '-');
if (flags & TIMEFMT_REALTIME)
*real_ts = *ts;
else if (flags & TIMEFMT_MONOTONIC) {
struct timespec mono_now[1];
clock_gettime(CLOCK_REALTIME, real_ts);
clock_gettime(CLOCK_MONOTONIC, mono_now);
timespecsub(real_ts, mono_now, real_ts);
timespecadd(real_ts, ts, real_ts);
} else {
clock_gettime(CLOCK_REALTIME, real_ts);
if (flags & TIMEFMT_SINCE)
timespecsub(real_ts, ts, real_ts);
else /* flags & TIMEFMT_UNTIL */
timespecadd(real_ts, ts, real_ts);
}
localtime_r(&real_ts->tv_sec, &tm);
if (flags & TIMEFMT_ISO8601) {
if (flags & TIMEFMT_SPACE)
strftime(cbuf, sizeof(cbuf), "%Y-%m-%d %H:%M:%S", &tm);
else
strftime(cbuf, sizeof(cbuf), "%Y-%m-%dT%H:%M:%S", &tm);
ret += bputs(buf, cbuf);
if (precision == -1)
precision = 3;
ret += do_subsec(buf, real_ts, precision, flags);
} else {
size_t len;
asctime_r(&tm, cbuf);
len = strlen(cbuf);
if (!len)
/* WTF. */
return 0;
if (cbuf[len - 1] == '\n')
cbuf[len - 1] = '\0';
ret += bputs(buf, cbuf);
}
return ret;
}
static ssize_t printfrr_reltime(struct fbuf *buf, struct printfrr_eargs *ea,
const struct timespec *ts, unsigned int flags)
{
struct timespec real_ts[1];
ssize_t ret = 0;
const char *space = "";
const char *dashes = "-";
int precision = ea->precision;
while (ea->fmt[0]) {
char ch = *ea->fmt++;
switch (ch) {
case 'p':
flags |= TIMEFMT_SPACE;
space = " ";
continue;
case 't':
flags |= TIMEFMT_BASIC;
continue;
case 'd':
flags |= TIMEFMT_DECIMAL;
continue;
case 'm':
flags |= TIMEFMT_MMSS;
dashes = "--:--";
continue;
case 'h':
flags |= TIMEFMT_HHMMSS;
dashes = "--:--:--";
continue;
case 'x':
flags |= TIMEFMT_DASHES;
continue;
}
ea->fmt--;
break;
}
if (flags & TIMEFMT_SKIP)
return 0;
if (!ts)
return bputch(buf, '-');
if (flags & TIMEFMT_ABSOLUTE) {
struct timespec anchor[1];
if (flags & TIMEFMT_REALTIME)
clock_gettime(CLOCK_REALTIME, anchor);
else
clock_gettime(CLOCK_MONOTONIC, anchor);
if (flags & TIMEFMT_UNTIL)
timespecsub(ts, anchor, real_ts);
else /* flags & TIMEFMT_SINCE */
timespecsub(anchor, ts, real_ts);
} else
*real_ts = *ts;
if (real_ts->tv_sec == 0 && real_ts->tv_nsec == 0 &&
(flags & TIMEFMT_DASHES))
return bputs(buf, dashes);
if (real_ts->tv_sec < 0) {
if (flags & TIMEFMT_DASHES)
return bputs(buf, dashes);
/* -0.3s is { -1s + 700ms } */
real_ts->tv_sec = -real_ts->tv_sec - 1;
real_ts->tv_nsec = 1000000000L - real_ts->tv_nsec;
if (real_ts->tv_nsec >= 1000000000L) {
real_ts->tv_sec++;
real_ts->tv_nsec -= 1000000000L;
}
/* all formats have a - make sense in front */
ret += bputch(buf, '-');
}
if (flags & TIMEFMT_DECIMAL) {
ret += bprintfrr(buf, "%lld", (long long)real_ts->tv_sec);
if (precision == -1)
precision = 3;
ret += do_subsec(buf, real_ts, precision, flags);
return ret;
}
/* these divisions may be slow on embedded boxes, hence only do the
* ones we need, plus the ?: zero check to hopefully skip zeros fast
*/
lldiv_t min_sec = lldiv(real_ts->tv_sec, 60);
if (flags & TIMEFMT_MMSS) {
ret += bprintfrr(buf, "%02lld:%02lld", min_sec.quot,
min_sec.rem);
ret += do_subsec(buf, real_ts, precision, flags);
return ret;
}
lldiv_t hour_min = min_sec.quot ? lldiv(min_sec.quot, 60) : (lldiv_t){};
if (flags & TIMEFMT_HHMMSS) {
ret += bprintfrr(buf, "%02lld:%02lld:%02lld", hour_min.quot,
hour_min.rem, min_sec.rem);
ret += do_subsec(buf, real_ts, precision, flags);
return ret;
}
lldiv_t day_hour =
hour_min.quot ? lldiv(hour_min.quot, 24) : (lldiv_t){};
lldiv_t week_day =
day_hour.quot ? lldiv(day_hour.quot, 7) : (lldiv_t){};
/* if sub-second precision is not supported, return */
if (flags & TIMEFMT_BASIC) {
/* match frrtime_to_interval (without space flag) */
if (week_day.quot)
ret += bprintfrr(buf, "%lldw%s%lldd%s%02lldh",
week_day.quot, space, week_day.rem,
space, day_hour.rem);
else if (day_hour.quot)
ret += bprintfrr(buf, "%lldd%s%02lldh%s%02lldm",
day_hour.quot, space, day_hour.rem,
space, hour_min.rem);
else
ret += bprintfrr(buf, "%02lld:%02lld:%02lld",
hour_min.quot, hour_min.rem,
min_sec.rem);
/* no sub-seconds here */
return ret;
}
/* default format */
if (week_day.quot)
ret += bprintfrr(buf, "%lldw%s", week_day.quot, space);
if (week_day.rem || week_day.quot)
ret += bprintfrr(buf, "%lldd%s", week_day.rem, space);
ret += bprintfrr(buf, "%02lld:%02lld:%02lld", day_hour.rem,
hour_min.rem, min_sec.rem);
if (precision == -1)
precision = 3;
ret += do_subsec(buf, real_ts, precision, flags);
return ret;
}
printfrr_ext_autoreg_p("TS", printfrr_ts);
static ssize_t printfrr_ts(struct fbuf *buf, struct printfrr_eargs *ea,
const void *vptr)
{
const struct timespec *ts = vptr;
return printfrr_time(buf, ea, ts, 0);
}
printfrr_ext_autoreg_p("TV", printfrr_tv);
static ssize_t printfrr_tv(struct fbuf *buf, struct printfrr_eargs *ea,
const void *vptr)
{
const struct timeval *tv = vptr;
struct timespec ts;
if (!tv)
return printfrr_time(buf, ea, NULL, 0);
ts.tv_sec = tv->tv_sec;
ts.tv_nsec = tv->tv_usec * 1000;
return printfrr_time(buf, ea, &ts, 0);
}
printfrr_ext_autoreg_p("TT", printfrr_tt);
static ssize_t printfrr_tt(struct fbuf *buf, struct printfrr_eargs *ea,
const void *vptr)
{
const time_t *tt = vptr;
struct timespec ts;
if (!tt)
return printfrr_time(buf, ea, NULL, TIMEFMT_SECONDS);
ts.tv_sec = *tt;
ts.tv_nsec = 0;
return printfrr_time(buf, ea, &ts, TIMEFMT_SECONDS);
}
|