/
shellbeam.erl
203 lines (182 loc) · 7.3 KB
/
shellbeam.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
-module(shellbeam).
-include("magicbeam.hrl").
-export([behaviour_info/1]).
-export([start_shell/2, start/0, spawn_shell/2, spawn_shell/0]).
behaviour_info(callbacks) ->
[
{commands,0}
].
start() ->
application:start(sasl), application:start(crypto), application:start(ssh), application:start(magicbeam),
timer:sleep(2000),
user_drv:start(['tty_sl -c -e', {shellbeam, spawn_shell, []}]).
spawn_shell() ->
spawn_shell(?SHELLBEAM_MODULES, ?SHELLBEAM_PROMPT).
spawn_shell(Modules, Prompt) ->
spawn(fun() -> start_shell(Modules, Prompt) end).
start_shell(Modules, Prompt) when is_list(Modules), is_list(Prompt) ->
io:format("Magicbeam Shell v~s~n", [erlang:system_info(version)]),
handle_shell(0, scan_modules(Modules), Prompt).
handle_shell(I, Commands, Prompt) ->
case io:get_line(colour(green, Prompt) ++ " " ++ colour(red, integer_to_list(I)) ++ " > ") of
eof -> ok;
{error, _} = E -> error_out("Unable to get_line -> ~p", [E]), handle_shell(I, Commands, Prompt);
D when is_list(D) ->
case string:tokens(string:strip(D, right, $\n), " ") of
[] ->
handle_shell(I, Commands, Prompt);
T when is_list(T) ->
case process_tokens(Commands, T) of
{processed, F, A} ->
normal_out(F, A),
handle_shell(I + 1, Commands, Prompt);
syntax ->
error_out("Syntax Error.~n" ++ p_syntax(Commands), []),
handle_shell(I + 1, Commands, Prompt);
{error, F, A} ->
error_out(F, A),
handle_shell(I + 1, Commands, Prompt);
exit ->
ok;
{subshell, M, P} ->
ok = handle_shell(0, scan_modules(M), P),
handle_shell(I + 1, Commands, Prompt)
end
end
end.
scan_modules(Modules) -> scan_modules(Modules, []).
scan_modules([], Commands) ->
Commands;
scan_modules([Module | Tail], Commands) ->
case {command_prefix(Module), Module:commands()} of
{undefined, C} when is_list(C) ->
scan_modules(Tail, C ++ Commands);
{P, C} when is_list(P), is_list(C) ->
Cm = lists:map(fun({T, H, F}) -> {P ++ T, H, F} end, C),
scan_modules(Tail, Cm ++ Commands)
end.
command_prefix(Module) ->
case lists:member({prefix, 0}, Module:module_info(exports)) of
false ->
[];
true ->
case Module:prefix() of
L when is_list(L) ->
L
end
end.
process_tokens(_, ["exit"]) ->
exit;
process_tokens(C, ["help"]) ->
{processed, "Help.~n" ++ p_syntax(C), []};
process_tokens([], _Tokens) -> syntax;
process_tokens([{H, _, {subshell, Mods, _}} | CTail], Tokens) when length(Tokens) > length(H) ->
case lists:split(length(H), Tokens) of
{H, T} ->
C = scan_modules(Mods),
case process_tokens(C, T) of
syntax -> {error, "Syntax Error.~n" ++ p_syntax(C), []};
R -> R
end;
{L, _T} when is_list(L) -> process_tokens(CTail, Tokens)
end;
process_tokens([{Match, Help, C} = E| CTail], Tokens) ->
case command_match(Match, Tokens) of
false ->
process_tokens(CTail, Tokens);
syntax ->
{error, "Syntax Error.~n" ++ p_syntax([E]), []};
A when is_list(A) ->
process_command(Help, C, A)
end;
process_tokens([_ | CTail], Tokens) ->
process_tokens(CTail, Tokens).
command_match(C, T) -> command_match(C, T, []).
command_match([], [], A) -> A;
command_match([H | MT], [H | TT], A) when is_list(H) ->
command_match(MT, TT, A);
command_match([{_, atom} | MT], [H | TT], Ar) ->
case catch list_to_existing_atom(H) of
A when is_atom(A) -> command_match(MT, TT, Ar ++ [A]);
{'EXIT',{badarg,[{erlang,list_to_existing_atom,[H]} | _]}} -> syntax
end;
command_match([{_, bool} | MT], [H | TT], Ar) ->
case catch list_to_existing_atom(string:to_lower(H)) of
A when A == true; A == false -> command_match(MT, TT, Ar ++ [A]);
A when is_atom(A) -> syntax
end;
command_match([{_, integer} | MT], [H | TT], Ar) ->
case catch list_to_integer(H) of
I when is_integer(I) -> command_match(MT, TT, Ar ++ [I]);
{'EXIT', {badarg, [{erlang, list_to_integer, [H]} | _]}} -> syntax
end;
command_match([{_, TY} | MT], [H | TT], Ar) when TY == any; TY == string ->
command_match(MT, TT, Ar ++ [H]);
command_match([{_, _, optional} | MT], [_H | _] = TT, Ar) ->
command_match(MT, TT, Ar);
command_match([{_, auto} | MT], [H | TT], Ar) ->
case catch list_to_integer(H) of
I when is_integer(I) -> command_match(MT, TT, Ar ++ [I]);
{'EXIT',{badarg, _}} ->
case catch list_to_existing_atom(H) of
A when is_atom(A) -> command_match(MT, TT, Ar ++ [A]);
{'EXIT',{badarg,_}} -> command_match(MT, TT, Ar ++ [H])
end
end;
command_match(_, _, _) -> false.
process_command(Help, CFun, Ar) when is_function(CFun) ->
case catch apply(CFun, Ar) of
{ok, F} when is_list(F) ->
{processed, F, []};
{ok, F, A} when is_list(F), is_list(A) ->
{processed, F, A};
syntax -> { error, "Syntax Error. ~s", [Help]};
{error, F} when is_list(F) -> {error, F, []};
{error, F, A} when is_list(F), is_list(A) -> {error, F, A};
{'EXIT', E} ->
?error("process_command exception ~p:~p - ~p", [CFun, Ar, E]),
{error, "Exception while processing command", []}
end;
process_command(_Help, {subshell, M, P}, _Ar) ->
{subshell, M, P}.
error_out(F, A) ->
normal_out(colour(red, "Problems: ") ++ F, A).
normal_out(F, A) ->
case catch io:format(F ++ "~n", A) of
ok ->
ok;
{'EXIT', E} ->
?error("normal_out exception ~p ~p - ~p", [F, A, E]),
error_out("Exception while handling output", [])
end.
-define(COLOURIZE(C, S), "\e[3" ++ integer_to_list(C) ++ "m" ++ S ++ "\e[0m").
colour(Colour, Text) when is_list(Text) ->
case ?SHELLBEAM_ANSI of
false ->
Text;
true -> p_colour1(Colour, Text)
end.
p_colour1(red, Text) -> ?COLOURIZE(1, Text);
p_colour1(green, Text) -> ?COLOURIZE(2, Text);
p_colour1(yellow, Text) -> ?COLOURIZE(3, Text);
p_colour1(blue, Text) -> ?COLOURIZE(4, Text).
%maybe support this again sometime?
%title(Text) when is_list(Text) ->
% case ?SHELLBEAM_ANSI of
% false -> ok;
% true -> io:format("\e]0;" ++ Text ++ "\007")
% end.
p_syntax(C) -> p_syntax(C, "help - this command~nexit - leave current shell~n").
p_syntax([], O) -> O;
p_syntax([{C, H, _} | T], O) ->
p_syntax(T, O ++ p_render_command(C) ++ "- " ++ H ++ "~n");
p_syntax([H | T], O) ->
?warn("unknown command definition ~p", [H]),
p_syntax(T, O).
p_render_command(C) ->
p_render_command(C, "").
p_render_command([], O) -> O;
p_render_command([W | T], O) when is_list(W) ->
p_render_command(T, O ++ W ++ " ");
p_render_command([{W, Ty} | T], O) when is_list(W), is_atom(Ty) ->
p_render_command(T, O ++ "[" ++ W ++ " (" ++ atom_to_list(Ty) ++ ")] ").