Skip to content

Commit 6c03876

Browse files
committed
lib/format.c: add json value conversion
1 parent ae521bb commit 6c03876

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

bin/xbps-query/xbps-query.1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ Format strings are parsed by the following EBNF:
312312
| "true" | "false" -- default boolean.
313313
| ('"' (("\\" (escape|'"')) | [^"])* '"') -- default string.
314314

315-
<conversion> ::= humanize | strmode
315+
<conversion> ::= humanize | strmode | json
316316

317-
-- Convert inode status information into a symbolic string
317+
-- Convert inode status information into a symbolic string.
318318
<strmode> ::= "strmode"
319319

320320
-- Format a number into a human readable form, the default is:`humanize .8Ki`:
@@ -334,6 +334,9 @@ Format strings are parsed by the following EBNF:
334334
| "E" -- exa
335335
<i> ::= "i" -- Use IEEE/IEC (and now also SI) power of two prefixes.
336336

337+
-- Format value as json value.
338+
<json> ::= "json"
339+
337340
<format> ::= [[fill] align] [sign] [width] ["." precision] [type]
338341
<fill> ::= <any char> -- The character to use when aligning the output.
339342
<align> ::= "<" -- Left align.

lib/format.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdlib.h>
2929
#include <string.h>
3030

31+
#include "xbps/json.h"
3132
#include "xbps_api_impl.h"
3233
#include "compat.h"
3334

@@ -283,7 +284,7 @@ parse_default(const char **pos, struct xbps_fmt *fmt, struct strbuf *buf,
283284
}
284285

285286
struct xbps_fmt_conv {
286-
enum { HUMANIZE = 1, STRMODE } type;
287+
enum { HUMANIZE = 1, STRMODE, JSON } type;
287288
union {
288289
struct humanize {
289290
unsigned width : 8;
@@ -361,6 +362,10 @@ parse_conversion(const char **pos, struct xbps_fmt *fmt, struct xbps_fmt_conv *c
361362
fmt->conv->type = HUMANIZE;
362363
*pos += sizeof("humanize");
363364
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;
364369
}
365370
return -EINVAL;
366371
}
@@ -641,6 +646,12 @@ int
641646
xbps_fmt_print_string(const struct xbps_fmt *fmt, const char *str, size_t len, FILE *fp)
642647
{
643648
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+
644655
if (len == 0)
645656
len = strlen(str);
646657
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)
700711
switch (fmt->conv->type) {
701712
case HUMANIZE: return humanize(&fmt->conv->humanize, fmt, d, fp);
702713
case STRMODE: return tostrmode(fmt, d, fp);
714+
case JSON: break;
703715
}
704716
}
705717
if (spec) {
@@ -732,6 +744,10 @@ xbps_fmt_print_number(const struct xbps_fmt *fmt, int64_t d, FILE *fp)
732744
int
733745
xbps_fmt_print_object(const struct xbps_fmt *fmt, xbps_object_t obj, FILE *fp)
734746
{
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+
}
735751
switch (xbps_object_type(obj)) {
736752
case XBPS_TYPE_BOOL:
737753
return xbps_fmt_print_string(fmt, xbps_bool_true(obj) ? "true" : "false", 0, fp);

0 commit comments

Comments
 (0)