| 388 | } |
| 389 | |
| 390 | static char *escape_value(const tal_t *ctx, |
| 391 | const char *val TAKES, |
| 392 | enum escape_format esc) |
| 393 | { |
| 394 | bool needs_quotes = false; |
| 395 | char *ret, *out; |
| 396 | const char *p; |
| 397 | |
| 398 | switch (esc) { |
| 399 | case REPORT_FMT_NONE: |
| 400 | return tal_strdup(ctx, val); |
| 401 | |
| 402 | case REPORT_FMT_CSV: |
| 403 | for (p = val; *p; p++) { |
| 404 | if (*p == ',' || *p == '"' || *p == '\n' || *p == '\r') { |
| 405 | needs_quotes = true; |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | if (!needs_quotes) |
| 411 | return tal_strdup(ctx, val); |
| 412 | |
| 413 | /* Worst case: doubling length plus " around plus nul term */ |
| 414 | ret = tal_arr(ctx, char, 2 + strlen(val) * 2 + 1); |
| 415 | out = ret; |
| 416 | *(out++) = '"'; |
| 417 | /* Quotes get doubled */ |
| 418 | for (p = val; *p; p++) { |
| 419 | if (*p == '"') |
| 420 | *(out++) = '"'; |
| 421 | *(out++) = *p; |
| 422 | } |
| 423 | *(out++) = '"'; |
| 424 | *(out++) = '\0'; |
| 425 | if (taken(val)) |
| 426 | tal_free(val); |
| 427 | return ret; |
| 428 | } |
| 429 | abort(); |
| 430 | } |
| 431 | |
| 432 | static char *format_event(const tal_t *ctx, |
| 433 | const struct report_format *fmt, |
no test coverage detected