Working with pipes

31 views
Skip to first unread message

Anton Kochkov

unread,
Jun 14, 2018, 6:08:59 AM6/14/18
to SWI-Prolog
Hello everyone!

I am writing a pipe interface to radare2 and currently I have this code:

:- use_module(library(unix)).
:- use_module(library(process)).
:- use_module(library(http/json)).

read_result(Out, Json) :-
%read_string(Out, "", "", End, Json),
%print(Json).
json_read_dict(Out, Json).

send_command(R, Command, Json) :-
write(R.in, Command),
nl(R.in),
flush_output(R.in),
read_result(R.out, Json).

% create structure with pid and in/out pipes
open_file(File, R) :-
process_create(path(radare2),
% r2 -2 -q0 file
["-2", "-q0", file(File)],
[stdin(pipe(In)),
stdout(pipe(Out)),
process(Pid)]),
dict_create(R, r2instance, [pid:Pid,in:In,out:Out]).

% TODO: Make more graceful exit
close_instance(R) :-
close(R.in),
close(R.out),
process_kill(R.pid).

with_command(File, Command, O) :-
open_file(File, R),
send_command(R, Command, O),
close_instance(R).

The problem if I evaluate it it complains of the invalid JSON, while for other tools it is valid JSON:

[r2pipe.prolog]$ swipl r2pipe.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
 
For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
 
?- with_command("/bin/ls", "ij", O).
ERROR: Stream <stream>(0xa16800):1:2 Syntax error: json(illegal_json)
?-
% halt

If I change the "read_result" relation to:

read_result(Out, Json) :-
    read_string(Out, "", "", End, Json),
    print(Json).

It prints JSON here:
 
[r2pipe.prolog]$ swipl r2pipe.pl
Warning: /home/user/radare/r2pipe.prolog/r2pipe.pl:6:
        Singleton variables: [End]
Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.4)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
 
For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
 
?- with_command("/bin/ls", "ij", O).
"{\"core\":{\"type\":\"DYN (Shared object file)\",\"file\":\"/bin/ls\",\"fd\":3,\"size\":157896,\"humansz\":\"154.2K\",\"iorw\":false,\"mode\":\"r-x\",\"obsz\":0,\"block\":256,\"format\":\"elf64\"},\"bin\":{\"arch\":\"x86\",\"binsz\":155969,\"bintype\":\"elf\",\"bits\":64,\"canary\":true,\"class\":\"ELF64\",\"compiled\":\"\",\"crypto\":false,\"dbg_file\":\"\",\"endian\":\"little\",\"havecode\":true,\"guid\":\"\",\"intrp\":\"/lib64/ld-linux-x86-64.so.2\",\"lang\":\"c\",\"linenum\":false,\"lsyms\":false,\"machine\":\"AMD x86-64 architecture\",\"maxopsz\":16,\"minopsz\":1,\"nx\":true,\"os\":\"linux\",\"pcalign\":0,\"pic\":true,\"relocs\":false,\"relro\":\"full\",\"rpath\":\"NONE\",\"static\":false,\"stripped\":true,\"subsys\":\"linux\",\"va\":true,\"checksums\":{}}}\n"
O = "{\"core\":{\"type\":\"DYN (Shared object file)\",\"file\":\"/bin/ls\",\"fd\":3,\"size\":157896,\"humansz\":\"154.2K\",\"iorw\":false,\"mode\":\"r-x\",\"obsz\":0,\"block\":256,\"format\":\"elf64\"},\"bin\":{\"arch\":\"x86\",\"binsz\":155969,\"bintype\":\"elf\",\"bits\":64,\"canary\":true,\"class\":\"ELF64\",\"compiled\":\"\",\"crypto\":false,\"dbg_file\":\"\",\"endian\":\"little\",\"havecode\":true,\"guid\":\"\",\"intrp\":\"/lib64/ld-linux-x86-64.so.2\",\"lang\":\"c\",\"linenum\":false,\"lsyms\":false,\"machine\":\"AMD x86-64 architecture\",\"maxopsz\":16,\"minopsz\":1,\"nx\":true,\"os\":\"linux\",\"pcalign\":0,\"pic\":true,\"relocs\":false,\"relro\":\"full\",\"rpath\":\"NONE\",\"static\":false,\"stripped\":true,\"subsys\":\"linux\",\"va\":true,\"checksums\":{}}}\n".

Not sure what is exactly wrong with this JSON then
File itself here https://github.com/XVilka/r2pipe.prolog/blob/master/r2pipe.pl
Reply all
Reply to author
Forward
0 new messages