mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	spa-json-dump: add -i and -s options
Add -i option to change the indentation Add -s option to generate simplified SPA JSON
This commit is contained in:
		
							parent
							
								
									a6199c92a4
								
							
						
					
					
						commit
						b67226fa0c
					
				
					 1 changed files with 103 additions and 48 deletions
				
			
		| 
						 | 
					@ -11,21 +11,56 @@
 | 
				
			||||||
#include <sys/mman.h>
 | 
					#include <sys/mman.h>
 | 
				
			||||||
#include <fcntl.h>
 | 
					#include <fcntl.h>
 | 
				
			||||||
#include <errno.h>
 | 
					#include <errno.h>
 | 
				
			||||||
 | 
					#include <getopt.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <spa/utils/result.h>
 | 
					#include <spa/utils/result.h>
 | 
				
			||||||
#include <spa/utils/json.h>
 | 
					#include <spa/utils/json.h>
 | 
				
			||||||
#include <spa/debug/file.h>
 | 
					#include <spa/debug/file.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define REJECT	"\"\\'=:,{}[]()#"
 | 
					#define DEFAULT_INDENT	2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct data {
 | 
					struct data {
 | 
				
			||||||
 | 
						const char *filename;
 | 
				
			||||||
	FILE *file;
 | 
						FILE *file;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						void *data;
 | 
				
			||||||
 | 
						size_t size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	int indent;
 | 
						int indent;
 | 
				
			||||||
	bool simple_string;
 | 
						bool simple_string;
 | 
				
			||||||
	const char *comma;
 | 
						const char *comma;
 | 
				
			||||||
	const char *key_sep;
 | 
						const char *key_sep;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define OPTIONS		"hi:s"
 | 
				
			||||||
 | 
					static const struct option long_options[] = {
 | 
				
			||||||
 | 
						{ "help",	no_argument,		NULL, 'h'},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						{ "indent",	required_argument,	NULL, 'i' },
 | 
				
			||||||
 | 
						{ "spa",	no_argument,		NULL, 's' },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						{ NULL, 0, NULL, 0 }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void show_usage(struct data *d, const char *name, bool is_error)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						FILE *fp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fp = is_error ? stderr : stdout;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fprintf(fp, "%s [options] [spa-json-file]\n", name);
 | 
				
			||||||
 | 
						fprintf(fp,
 | 
				
			||||||
 | 
							"  -h, --help                            Show this help\n"
 | 
				
			||||||
 | 
							"\n");
 | 
				
			||||||
 | 
						fprintf(fp,
 | 
				
			||||||
 | 
							"  -i  --indent                          set indent (default %d)\n"
 | 
				
			||||||
 | 
							"  -s  --spa                             use simplified SPA JSON\n"
 | 
				
			||||||
 | 
							"\n",
 | 
				
			||||||
 | 
							DEFAULT_INDENT);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define REJECT	"\"\\'=:,{}[]()#"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static bool is_simple_string(const char *val, int len)
 | 
					static bool is_simple_string(const char *val, int len)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int i;
 | 
						int i;
 | 
				
			||||||
| 
						 | 
					@ -141,59 +176,46 @@ static int dump(struct data *d, int indent, struct spa_json *it, const char *val
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int process_json(const char *filename, void *buf, size_t size)
 | 
					static int process_json(struct data *d)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct data d;
 | 
					 | 
				
			||||||
	int len, res;
 | 
						int len, res;
 | 
				
			||||||
	struct spa_json it;
 | 
						struct spa_json it;
 | 
				
			||||||
	const char *value;
 | 
						const char *value;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	spa_zero(d);
 | 
						if ((len = spa_json_begin(&it, d->data, d->size, &value)) <= 0) {
 | 
				
			||||||
	if ((len = spa_json_begin(&it, buf, size, &value)) <= 0) {
 | 
							fprintf(stderr, "not a valid file '%s': %s\n", d->filename, spa_strerror(len));
 | 
				
			||||||
                fprintf(stderr, "not a valid file '%s': %s\n", filename, spa_strerror(len));
 | 
					 | 
				
			||||||
		return -EINVAL;
 | 
							return -EINVAL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if (!spa_json_is_container(value, len)) {
 | 
						if (!spa_json_is_container(value, len)) {
 | 
				
			||||||
		spa_json_init(&it, buf, size);
 | 
							spa_json_init(&it, d->data, d->size);
 | 
				
			||||||
		value = NULL;
 | 
							value = NULL;
 | 
				
			||||||
		len = 0;
 | 
							len = 0;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	d.file = stdout;
 | 
					 | 
				
			||||||
#if 1
 | 
					 | 
				
			||||||
	d.simple_string = false;
 | 
					 | 
				
			||||||
	d.comma = ",";
 | 
					 | 
				
			||||||
	d.key_sep = ":";
 | 
					 | 
				
			||||||
	d.indent = 2;
 | 
					 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
	d.simple_string = true;
 | 
					 | 
				
			||||||
	d.comma = "";
 | 
					 | 
				
			||||||
	d.key_sep = " =";
 | 
					 | 
				
			||||||
	d.indent = 4;
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res = dump(&d, 0, &it, value, len);
 | 
						res = dump(d, 0, &it, value, len);
 | 
				
			||||||
	if (spa_json_next(&it, &value) < 0)
 | 
						if (spa_json_next(&it, &value) < 0)
 | 
				
			||||||
		res = -EINVAL;
 | 
							res = -EINVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fprintf(d.file, "\n");
 | 
						fprintf(d->file, "\n");
 | 
				
			||||||
	fflush(d.file);
 | 
						fflush(d->file);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (res < 0) {
 | 
						if (res < 0) {
 | 
				
			||||||
		struct spa_error_location loc;
 | 
							struct spa_error_location loc;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (spa_json_get_error(&it, buf, &loc))
 | 
							if (spa_json_get_error(&it, d->data, &loc))
 | 
				
			||||||
			spa_debug_file_error_location(stderr, &loc,
 | 
								spa_debug_file_error_location(stderr, &loc,
 | 
				
			||||||
					"syntax error in file '%s': %s",
 | 
										"syntax error in file '%s': %s",
 | 
				
			||||||
					filename, loc.reason);
 | 
										d->filename, loc.reason);
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
			fprintf(stderr, "error parsing file '%s': %s\n", filename, spa_strerror(res));
 | 
								fprintf(stderr, "error parsing file '%s': %s\n",
 | 
				
			||||||
 | 
										d->filename, spa_strerror(res));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return -EINVAL;
 | 
							return -EINVAL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int process_stdin(void)
 | 
					static int process_stdin(struct data *d)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	uint8_t *buf = NULL, *p;
 | 
						uint8_t *buf = NULL, *p;
 | 
				
			||||||
	size_t alloc = 0, size = 0, read_size, res;
 | 
						size_t alloc = 0, size = 0, read_size, res;
 | 
				
			||||||
| 
						 | 
					@ -217,8 +239,10 @@ static int process_stdin(void)
 | 
				
			||||||
		fprintf(stderr, "error: %m\n");
 | 
							fprintf(stderr, "error: %m\n");
 | 
				
			||||||
		goto error;
 | 
							goto error;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						d->data = buf;
 | 
				
			||||||
 | 
						d->size = size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err = process_json("-", buf, size);
 | 
						err = process_json(d);
 | 
				
			||||||
	free(buf);
 | 
						free(buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return (err == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 | 
						return (err == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 | 
				
			||||||
| 
						 | 
					@ -230,38 +254,69 @@ error:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main(int argc, char *argv[])
 | 
					int main(int argc, char *argv[])
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						int c;
 | 
				
			||||||
 | 
						int longopt_index = 0;
 | 
				
			||||||
	int fd, res, exit_code = EXIT_FAILURE;
 | 
						int fd, res, exit_code = EXIT_FAILURE;
 | 
				
			||||||
	void *data;
 | 
						struct data d;
 | 
				
			||||||
	struct stat sbuf;
 | 
						struct stat sbuf;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (argc < 1) {
 | 
						spa_zero(d);
 | 
				
			||||||
		fprintf(stderr, "usage: %s [spa-json-file]\n", argv[0]);
 | 
						d.file = stdout;
 | 
				
			||||||
		goto error;
 | 
					
 | 
				
			||||||
	}
 | 
						d.filename = "-";
 | 
				
			||||||
	if (argc == 1)
 | 
						d.simple_string = false;
 | 
				
			||||||
		return process_stdin();
 | 
						d.comma = ",";
 | 
				
			||||||
	if ((fd = open(argv[1],  O_CLOEXEC | O_RDONLY)) < 0)  {
 | 
						d.key_sep = ":";
 | 
				
			||||||
                fprintf(stderr, "error opening file '%s': %m\n", argv[1]);
 | 
						d.indent = DEFAULT_INDENT;
 | 
				
			||||||
		goto error;
 | 
					
 | 
				
			||||||
        }
 | 
						while ((c = getopt_long(argc, argv, OPTIONS, long_options, &longopt_index)) != -1) {
 | 
				
			||||||
        if (fstat(fd, &sbuf) < 0) {
 | 
							switch (c) {
 | 
				
			||||||
                fprintf(stderr, "error statting file '%s': %m\n", argv[1]);
 | 
							case 'h' :
 | 
				
			||||||
                goto error_close;
 | 
								show_usage(&d, argv[0], false);
 | 
				
			||||||
	}
 | 
								return 0;
 | 
				
			||||||
        if ((data = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
 | 
							case 'i':
 | 
				
			||||||
                fprintf(stderr, "error mmapping file '%s': %m\n", argv[1]);
 | 
								d.indent = atoi(optarg);
 | 
				
			||||||
                goto error_close;
 | 
								break;
 | 
				
			||||||
 | 
							case 's':
 | 
				
			||||||
 | 
								d.simple_string = true;
 | 
				
			||||||
 | 
								d.comma = "";
 | 
				
			||||||
 | 
								d.key_sep = " =";
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							default:
 | 
				
			||||||
 | 
								show_usage(&d, argv[0], true);
 | 
				
			||||||
 | 
								return -1;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	res = process_json(argv[1], data, sbuf.st_size);
 | 
						if (optind < argc)
 | 
				
			||||||
 | 
							d.filename = argv[optind++];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (spa_streq(d.filename, "-"))
 | 
				
			||||||
 | 
							return process_stdin(&d);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						 ((fd = open(d.filename,  O_CLOEXEC | O_RDONLY)) < 0)  {
 | 
				
			||||||
 | 
							fprintf(stderr, "error opening file '%s': %m\n", d.filename);
 | 
				
			||||||
 | 
							goto error;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if (fstat(fd, &sbuf) < 0) {
 | 
				
			||||||
 | 
							fprintf(stderr, "error statting file '%s': %m\n", d.filename);
 | 
				
			||||||
 | 
							goto error_close;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if ((d.data = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
 | 
				
			||||||
 | 
							fprintf(stderr, "error mmapping file '%s': %m\n", d.filename);
 | 
				
			||||||
 | 
							goto error_close;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						d.size = sbuf.st_size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						res = process_json(&d);
 | 
				
			||||||
	if (res < 0)
 | 
						if (res < 0)
 | 
				
			||||||
		exit_code = EXIT_FAILURE;
 | 
							exit_code = EXIT_FAILURE;
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
		exit_code = EXIT_SUCCESS;
 | 
							exit_code = EXIT_SUCCESS;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        munmap(data, sbuf.st_size);
 | 
						munmap(d.data, sbuf.st_size);
 | 
				
			||||||
error_close:
 | 
					error_close:
 | 
				
			||||||
        close(fd);
 | 
						close(fd);
 | 
				
			||||||
error:
 | 
					error:
 | 
				
			||||||
	return exit_code;
 | 
						return exit_code;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue