1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2013-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19-module(cdv_term_cb).
20
21-export([get_details/2,
22	 detail_pages/0]).
23
24%% Callbacks for cdv_detail_wx
25get_details({Type, {T,Key}}, _) ->
26    [{Key,Term}] = ets:lookup(T,Key),
27    {ok,{"Expanded Term", {Type,[Term, T]}, []}}.
28
29detail_pages() ->
30    [{"Term", fun init_term_page/2}].
31
32init_term_page(ParentWin, {Type, [Term, Tab]}) ->
33    observer_lib:report_progress({ok,"Expanding term"}),
34    observer_lib:report_progress({ok,start_pulse}),
35    Expanded = expand(Term, true),
36    BinSaved = expand(Term, Tab),
37    observer_lib:report_progress({ok,stop_pulse}),
38    cdv_multi_wx:start_link(
39      ParentWin,
40      [{"Format \~p",cdv_html_wx,{Type, format_term_fun("~p",BinSaved,Tab)}},
41       {"Format \~tp",cdv_html_wx,{Type,format_term_fun("~tp",BinSaved,Tab)}},
42       {"Format \~w",cdv_html_wx,{Type,format_term_fun("~w",BinSaved,Tab)}},
43       {"Format \~tw",cdv_html_wx,{Type,format_term_fun("~tw",BinSaved,Tab)}},
44       {"Format \~s",cdv_html_wx,{Type,format_term_fun("~s",Expanded,Tab)}},
45       {"Format \~ts",cdv_html_wx,{Type,format_term_fun("~ts",Expanded,Tab)}}]).
46
47format_term_fun(Format,Term,Tab) ->
48    fun() ->
49            observer_lib:report_progress({ok,"Formatting term"}),
50            observer_lib:report_progress({ok,start_pulse}),
51	    try io_lib:format(Format,[Term]) of
52		Str -> {expand, plain_html(Str), Tab}
53	    catch error:badarg ->
54		    Warning = "This term can not be formatted with " ++ Format,
55		    observer_html_lib:warning(Warning)
56            after
57                    observer_lib:report_progress({ok,stop_pulse})
58	    end
59    end.
60
61plain_html(Text) ->
62    observer_html_lib:plain_page(Text).
63
64expand(['#CDVBin',Offset,Size,Pos], true) ->
65    {ok,Bin} = crashdump_viewer:expand_binary({Offset,Size,Pos}),
66    Bin;
67expand(Bin, Tab) when is_binary(Bin), not is_boolean(Tab) ->
68    observer_lib:make_obsbin(Bin, Tab);
69expand([H|T], Expand) ->
70    case expand(T, Expand) of
71	ET when is_list(ET) ->
72	    [expand(H, Expand)|ET];
73        ET -> % The tail is an expanded binary - cannot append with |
74	    [expand(H, Expand),ET]
75    end;
76expand(Tuple, Expand) when is_tuple(Tuple) ->
77    list_to_tuple(expand(tuple_to_list(Tuple), Expand));
78expand(Term, _) ->
79    Term.
80