Skip to content

Instantly share code, notes, and snippets.

@keynslug
Created February 17, 2015 11:11
Show Gist options
  • Save keynslug/8c98ae28fedcc45b7e86 to your computer and use it in GitHub Desktop.
Save keynslug/8c98ae28fedcc45b7e86 to your computer and use it in GitHub Desktop.
bpr
#!/usr/bin/env escript
%%
%%! -noshell +pc unicode
main([BinTerm]) ->
[Bin] = consult(BinTerm ++ "."),
io:setopts(user, [{encoding, unicode}]),
io:format(user, "~ts~n", [Bin]),
erlang:halt();
main(_) ->
io:format(
"Print Erlang binaries in human consumable representation. For Christ sake.~n"
"~n"
"Usage: bpr \"<<...>>\"~n"
).
consult(Str) when is_list(Str) ->
consult([], Str, []);
consult(Bin) when is_binary(Bin)->
consult([], binary_to_list(Bin), []).
consult(Cont, Str, Acc) ->
case erl_scan:tokens(Cont, Str, 0) of
{done, Result, Remaining} ->
case Result of
{ok, Tokens, _} ->
{ok, Term} = erl_parse:parse_term(Tokens),
consult([], Remaining, [Term | Acc]);
{eof, _Other} ->
lists:reverse(Acc);
{error, Info, _} ->
{error, Info}
end;
{more, Cont1} ->
consult(Cont1, eof, Acc)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment