Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Commit

Permalink
Add flag to read JSON from file
Browse files Browse the repository at this point in the history
A new -i flag is introduced to allow users to pretty-print a JSON
dump. Users can now share JSON dumps, and optionally post-process
them, e.g. this can be used to only print the connectors:

    drm_info -j | jq '. | map_values(. | {driver,device,connectors,encoders:[],crtcs:[],planes:[]})' | drm_info -i
  • Loading branch information
emersion committed Jul 9, 2021
1 parent d419934 commit 710e7a4
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@

int main(int argc, char *argv[])
{
bool json = false;
bool json = false, input = false;

int opt;
while ((opt = getopt(argc, argv, "j")) != -1) {
while ((opt = getopt(argc, argv, "ji")) != -1) {
switch (opt) {
case 'j':
json = true;
break;
case 'i':
input = true;
break;
default:
fprintf(stderr, "usage: drm_info [-j] [--] [path]...\n");
fprintf(stderr, "usage: drm_info [-j] [-i] [--] [path]...\n");
exit(opt == '?' ? EXIT_SUCCESS : EXIT_FAILURE);
}
}

struct json_object *obj = drm_info(&argv[optind]);
struct json_object *obj;
if (input) {
obj = json_object_from_fd(STDIN_FILENO);
} else {
obj = drm_info(&argv[optind]);
}
if (!obj) {
exit(EXIT_FAILURE);
}

if (json) {
json_object_to_fd(STDOUT_FILENO, obj,
JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED);
Expand Down

0 comments on commit 710e7a4

Please sign in to comment.