mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-12-15 08:56:38 -05:00
spa: spa-json-dump: read also from stdin, and report syntax errors
Support reading from stdin and report syntax errors.
Also don't do extra spa_json_enter when inserting a dummy "{" when
parsing files with top-level keys. In this case the tokenizer is
already "entered" after spa_json_init, and will give parse error when we
are not inserting the closing "}" for the dummy "{".
This commit is contained in:
parent
3775e4e624
commit
7f5e0f0425
2 changed files with 109 additions and 28 deletions
|
|
@ -4,11 +4,11 @@ SPA JSON to JSON converter
|
||||||
|
|
||||||
# SYNOPSIS
|
# SYNOPSIS
|
||||||
|
|
||||||
**spa-json** *FILE*
|
**spa-json** *[FILE]*
|
||||||
|
|
||||||
# DESCRIPTION
|
# DESCRIPTION
|
||||||
|
|
||||||
Reads a SPA JSON file, and outputs it as standard JSON.
|
Reads a SPA JSON file or stdin, and outputs it as standard JSON.
|
||||||
|
|
||||||
# EXAMPLES
|
# EXAMPLES
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,22 +54,33 @@ static void encode_string(FILE *f, const char *val, int len)
|
||||||
static int dump(FILE *file, int indent, struct spa_json *it, const char *value, int len)
|
static int dump(FILE *file, int indent, struct spa_json *it, const char *value, int len)
|
||||||
{
|
{
|
||||||
struct spa_json sub;
|
struct spa_json sub;
|
||||||
int count = 0;
|
bool toplevel = false;
|
||||||
|
int count = 0, res;
|
||||||
char key[1024];
|
char key[1024];
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
toplevel = true;
|
||||||
|
value = "{";
|
||||||
|
len = 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (spa_json_is_array(value, len)) {
|
if (spa_json_is_array(value, len)) {
|
||||||
fprintf(file, "[");
|
fprintf(file, "[");
|
||||||
spa_json_enter(it, &sub);
|
spa_json_enter(it, &sub);
|
||||||
while ((len = spa_json_next(&sub, &value)) > 0) {
|
while ((len = spa_json_next(&sub, &value)) > 0) {
|
||||||
fprintf(file, "%s\n%*s", count++ > 0 ? "," : "",
|
fprintf(file, "%s\n%*s", count++ > 0 ? "," : "",
|
||||||
indent+2, "");
|
indent+2, "");
|
||||||
dump(file, indent+2, &sub, value, len);
|
if ((res = dump(file, indent+2, &sub, value, len)) < 0)
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
fprintf(file, "%s%*s]", count > 0 ? "\n" : "",
|
fprintf(file, "%s%*s]", count > 0 ? "\n" : "",
|
||||||
count > 0 ? indent : 0, "");
|
count > 0 ? indent : 0, "");
|
||||||
} else if (spa_json_is_object(value, len)) {
|
} else if (spa_json_is_object(value, len)) {
|
||||||
fprintf(file, "{");
|
fprintf(file, "{");
|
||||||
spa_json_enter(it, &sub);
|
if (!toplevel)
|
||||||
|
spa_json_enter(it, &sub);
|
||||||
|
else
|
||||||
|
sub = *it;
|
||||||
while (spa_json_get_string(&sub, key, sizeof(key)) > 0) {
|
while (spa_json_get_string(&sub, key, sizeof(key)) > 0) {
|
||||||
fprintf(file, "%s\n%*s",
|
fprintf(file, "%s\n%*s",
|
||||||
count++ > 0 ? "," : "",
|
count++ > 0 ? "," : "",
|
||||||
|
|
@ -78,8 +89,15 @@ static int dump(FILE *file, int indent, struct spa_json *it, const char *value,
|
||||||
fprintf(file, ": ");
|
fprintf(file, ": ");
|
||||||
if ((len = spa_json_next(&sub, &value)) <= 0)
|
if ((len = spa_json_next(&sub, &value)) <= 0)
|
||||||
break;
|
break;
|
||||||
dump(file, indent+2, &sub, value, len);
|
res = dump(file, indent+2, &sub, value, len);
|
||||||
|
if (res < 0) {
|
||||||
|
if (toplevel)
|
||||||
|
*it = sub;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (toplevel)
|
||||||
|
*it = sub;
|
||||||
fprintf(file, "%s%*s}", count > 0 ? "\n" : "",
|
fprintf(file, "%s%*s}", count > 0 ? "\n" : "",
|
||||||
count > 0 ? indent : 0, "");
|
count > 0 ? indent : 0, "");
|
||||||
} else if (spa_json_is_string(value, len) ||
|
} else if (spa_json_is_string(value, len) ||
|
||||||
|
|
@ -91,21 +109,96 @@ static int dump(FILE *file, int indent, struct spa_json *it, const char *value,
|
||||||
} else {
|
} else {
|
||||||
encode_string(file, value, len);
|
encode_string(file, value, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (spa_json_get_error(it, NULL, NULL, NULL))
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int process_json(const char *filename, void *buf, size_t size)
|
||||||
|
{
|
||||||
|
int len, res;
|
||||||
|
struct spa_json it;
|
||||||
|
const char *value;
|
||||||
|
|
||||||
|
spa_json_init(&it, buf, size);
|
||||||
|
if ((len = spa_json_next(&it, &value)) <= 0) {
|
||||||
|
fprintf(stderr, "not a valid file '%s': %s\n", filename, spa_strerror(len));
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
if (!spa_json_is_container(value, len)) {
|
||||||
|
spa_json_init(&it, buf, size);
|
||||||
|
value = NULL;
|
||||||
|
len = 0;
|
||||||
|
}
|
||||||
|
res = dump(stdout, 0, &it, value, len);
|
||||||
|
if (spa_json_next(&it, &value) < 0)
|
||||||
|
res = -EINVAL;
|
||||||
|
|
||||||
|
fprintf(stdout, "\n");
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
if (res < 0) {
|
||||||
|
int line, col;
|
||||||
|
|
||||||
|
if (spa_json_get_error(&it, buf, &line, &col))
|
||||||
|
fprintf(stderr, "syntax error in file '%s': at line:%d col:%d\n", filename, line, col);
|
||||||
|
else
|
||||||
|
fprintf(stderr, "error parsing file '%s': %s\n", filename, spa_strerror(res));
|
||||||
|
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int process_stdin(void)
|
||||||
|
{
|
||||||
|
uint8_t *buf = NULL, *p;
|
||||||
|
size_t alloc = 0, size = 0, read_size, res;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
do {
|
||||||
|
alloc += 1024 + alloc;
|
||||||
|
p = realloc(buf, alloc);
|
||||||
|
if (!p) {
|
||||||
|
fprintf(stderr, "error: %m\n");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
buf = p;
|
||||||
|
|
||||||
|
read_size = alloc - size;
|
||||||
|
res = fread(buf + size, 1, read_size, stdin);
|
||||||
|
size += res;
|
||||||
|
} while (res == read_size);
|
||||||
|
|
||||||
|
if (ferror(stdin)) {
|
||||||
|
fprintf(stderr, "error: %m\n");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = process_json("-", buf, size);
|
||||||
|
free(buf);
|
||||||
|
|
||||||
|
return (err == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
|
|
||||||
|
error:
|
||||||
|
free(buf);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int fd, len, res, exit_code = EXIT_FAILURE;
|
int fd, res, exit_code = EXIT_FAILURE;
|
||||||
void *data;
|
void *data;
|
||||||
struct stat sbuf;
|
struct stat sbuf;
|
||||||
struct spa_json it;
|
|
||||||
const char *value;
|
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 1) {
|
||||||
fprintf(stderr, "usage: %s <spa-json-file>\n", argv[0]);
|
fprintf(stderr, "usage: %s [spa-json-file]\n", argv[0]);
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
if (argc == 1)
|
||||||
|
return process_stdin();
|
||||||
if ((fd = open(argv[1], O_CLOEXEC | O_RDONLY)) < 0) {
|
if ((fd = open(argv[1], O_CLOEXEC | O_RDONLY)) < 0) {
|
||||||
fprintf(stderr, "error opening file '%s': %m\n", argv[1]);
|
fprintf(stderr, "error opening file '%s': %m\n", argv[1]);
|
||||||
goto error;
|
goto error;
|
||||||
|
|
@ -119,24 +212,12 @@ int main(int argc, char *argv[])
|
||||||
goto error_close;
|
goto error_close;
|
||||||
}
|
}
|
||||||
|
|
||||||
spa_json_init(&it, data, sbuf.st_size);
|
res = process_json(argv[1], data, sbuf.st_size);
|
||||||
if ((len = spa_json_next(&it, &value)) <= 0) {
|
if (res < 0)
|
||||||
fprintf(stderr, "not a valid file '%s': %s\n", argv[1], spa_strerror(len));
|
exit_code = EXIT_FAILURE;
|
||||||
goto error_unmap;
|
else
|
||||||
}
|
exit_code = EXIT_SUCCESS;
|
||||||
if (!spa_json_is_container(value, len)) {
|
|
||||||
spa_json_init(&it, data, sbuf.st_size);
|
|
||||||
value = "{";
|
|
||||||
len = 1;
|
|
||||||
}
|
|
||||||
if ((res = dump(stdout, 0, &it, value, len)) < 0) {
|
|
||||||
fprintf(stderr, "error parsing file '%s': %s\n", argv[1], spa_strerror(res));
|
|
||||||
goto error_unmap;
|
|
||||||
}
|
|
||||||
fprintf(stdout, "\n");
|
|
||||||
exit_code = EXIT_SUCCESS;
|
|
||||||
|
|
||||||
error_unmap:
|
|
||||||
munmap(data, sbuf.st_size);
|
munmap(data, sbuf.st_size);
|
||||||
error_close:
|
error_close:
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue