|
28 | 28 | #include <stdlib.h> |
29 | 29 | #include <string.h> |
30 | 30 |
|
| 31 | +#include "xbps/json.h" |
31 | 32 | #include "xbps_api_impl.h" |
32 | 33 | #include "compat.h" |
33 | 34 |
|
@@ -283,7 +284,7 @@ parse_default(const char **pos, struct xbps_fmt *fmt, struct strbuf *buf, |
283 | 284 | } |
284 | 285 |
|
285 | 286 | struct xbps_fmt_conv { |
286 | | - enum { HUMANIZE = 1, STRMODE } type; |
| 287 | + enum { HUMANIZE = 1, STRMODE, JSON } type; |
287 | 288 | union { |
288 | 289 | struct humanize { |
289 | 290 | unsigned width : 8; |
@@ -361,6 +362,10 @@ parse_conversion(const char **pos, struct xbps_fmt *fmt, struct xbps_fmt_conv *c |
361 | 362 | fmt->conv->type = HUMANIZE; |
362 | 363 | *pos += sizeof("humanize"); |
363 | 364 | return parse_humanize(pos, &fmt->conv->humanize); |
| 365 | + } else if (strncmp(*pos + 1, "json", sizeof("json") - 1) == 0) { |
| 366 | + fmt->conv->type = JSON; |
| 367 | + *pos += sizeof("json"); |
| 368 | + return 0; |
364 | 369 | } |
365 | 370 | return -EINVAL; |
366 | 371 | } |
|
641 | 646 | xbps_fmt_print_string(const struct xbps_fmt *fmt, const char *str, size_t len, FILE *fp) |
642 | 647 | { |
643 | 648 | const struct xbps_fmt_spec *spec = fmt->spec; |
| 649 | + |
| 650 | + if (fmt->conv && fmt->conv->type == JSON) { |
| 651 | + struct xbps_json_printer pr = {.file = fp}; |
| 652 | + return xbps_json_print_quote(&pr, str); |
| 653 | + } |
| 654 | + |
644 | 655 | if (len == 0) |
645 | 656 | len = strlen(str); |
646 | 657 | if (spec && spec->align == '>' && spec->width > (unsigned)len) { |
@@ -700,6 +711,7 @@ xbps_fmt_print_number(const struct xbps_fmt *fmt, int64_t d, FILE *fp) |
700 | 711 | switch (fmt->conv->type) { |
701 | 712 | case HUMANIZE: return humanize(&fmt->conv->humanize, fmt, d, fp); |
702 | 713 | case STRMODE: return tostrmode(fmt, d, fp); |
| 714 | + case JSON: break; |
703 | 715 | } |
704 | 716 | } |
705 | 717 | if (spec) { |
@@ -732,6 +744,10 @@ xbps_fmt_print_number(const struct xbps_fmt *fmt, int64_t d, FILE *fp) |
732 | 744 | int |
733 | 745 | xbps_fmt_print_object(const struct xbps_fmt *fmt, xbps_object_t obj, FILE *fp) |
734 | 746 | { |
| 747 | + if (fmt->conv && fmt->conv->type == JSON) { |
| 748 | + struct xbps_json_printer pr = {.file = fp}; |
| 749 | + return xbps_json_print_xbps_object(&pr, obj); |
| 750 | + } |
735 | 751 | switch (xbps_object_type(obj)) { |
736 | 752 | case XBPS_TYPE_BOOL: |
737 | 753 | return xbps_fmt_print_string(fmt, xbps_bool_true(obj) ? "true" : "false", 0, fp); |
|
0 commit comments