1 |
#!/usr/bin/perl |
2 |
|
3 |
#use strict; |
4 |
use strict qw ( vars refs ); |
5 |
use warnings; |
6 |
|
7 |
use Env qw ( HOME ); |
8 |
use Term::ReadLine; |
9 |
use IO::File; |
10 |
|
11 |
my @sh_commands_local = qw ( connect sc exit quit help ? ); |
12 |
my @sh_commands_remote = qw ( tell bye ); |
13 |
|
14 |
my %cfg; |
15 |
$cfg{branding}{console_name} = "jash"; |
16 |
|
17 |
my %console; |
18 |
$console{commands} = \@sh_commands_local; |
19 |
$console{context} = ""; |
20 |
|
21 |
my %conn; |
22 |
$conn{type} = "cmdfd"; |
23 |
$conn{file_name} = ""; |
24 |
$conn{host_name} = ""; |
25 |
$conn{target} = ""; |
26 |
$conn{connected} = 0; |
27 |
$conn{user} = 'anonymous'; |
28 |
$conn{commands} = \@sh_commands_remote; |
29 |
|
30 |
my %helpmsg; |
31 |
|
32 |
$helpmsg{connect} = <<EOH; |
33 |
COMMAND: |
34 |
connect |
35 |
|
36 |
SYNOPSIS: |
37 |
connect |
38 |
connect cmdfile [cmdfile] |
39 |
connect http username[\@hostname] |
40 |
|
41 |
DESCRIPTION: |
42 |
Connect to a local or remote command-engine |
43 |
EOH |
44 |
|
45 |
$helpmsg{sc} = <<EOH; |
46 |
COMMAND: |
47 |
sc (switch context) |
48 |
|
49 |
SYNOPSIS: |
50 |
sc <context> |
51 |
|
52 |
DESCRIPTION: |
53 |
Switches console-context to "<context>". |
54 |
Every remote-command issued from now on will be prefixed by the console-context. |
55 |
Delete a context by just typing "sc". |
56 |
EOH |
57 |
|
58 |
$helpmsg{exit} = <<EOH; |
59 |
COMMAND: |
60 |
exit|quit |
61 |
|
62 |
SYNOPSIS: |
63 |
exit |
64 |
quit |
65 |
|
66 |
DESCRIPTION: |
67 |
Quits from shell. |
68 |
EOH |
69 |
$helpmsg{quit} = $helpmsg{exit}; |
70 |
|
71 |
$helpmsg{tell} = <<EOH; |
72 |
COMMAND: |
73 |
tell |
74 |
|
75 |
SYNOPSIS: |
76 |
tell <target>|<alias> <command> |
77 |
|
78 |
DESCRIPTION: |
79 |
Tells an (aliased) target what to do |
80 |
EOH |
81 |
|
82 |
sub getUsage { |
83 |
my $bool_connected = "no"; |
84 |
$conn{connected} && ($bool_connected = "yes"); |
85 |
my $helpmsg = <<EOH; |
86 |
---------------------------------------------------------- |
87 |
commands: |
88 |
- local-commands: @{$console{commands}} |
89 |
- remote-commands: @{$conn{commands}} |
90 |
---------------------------------------------------------- |
91 |
status: |
92 |
- console-context: $console{context} |
93 |
- connected: $bool_connected |
94 |
- connection-target: $conn{target} |
95 |
---------------------------------------------------------- |
96 |
EOH |
97 |
} |
98 |
|
99 |
sub getHelpMsg { |
100 |
my $helpkey = shift; |
101 |
my $msg = ""; |
102 |
if ($helpmsg{$helpkey}) { |
103 |
$msg .= <<MSG; |
104 |
--------------------------------------------- |
105 |
MSG |
106 |
$msg .= $helpmsg{$helpkey}; |
107 |
} |
108 |
return $msg; |
109 |
} |
110 |
|
111 |
sub lCmd { |
112 |
|
113 |
my $cmd = shift; |
114 |
|
115 |
if ($cmd eq 'quit' || $cmd eq 'exit') { exit; } |
116 |
|
117 |
if ($cmd =~ m/(help|\?)\s*(.*)/) { |
118 |
my $helpkey = $2; |
119 |
if ($helpkey) { |
120 |
return getHelpMsg($helpkey); |
121 |
} else { |
122 |
return getUsage(); |
123 |
} |
124 |
} |
125 |
|
126 |
if ($cmd =~ m/connect\s*(.*)/) { |
127 |
my $param = $1; |
128 |
my $c_where = ""; |
129 |
my $c_args = ""; |
130 |
if ($param =~ m/(cmdfile|http)\s*(.*)/) { |
131 |
$c_where = $1; |
132 |
$c_args = $2; |
133 |
} else { |
134 |
$c_where = "cmdfile"; |
135 |
} |
136 |
rConnect($c_where, $c_args); |
137 |
return; |
138 |
} |
139 |
|
140 |
if ($cmd =~ m/sc\s*(.*)/) { |
141 |
my $context = $1; |
142 |
$console{context} = ''; |
143 |
$console{context} = $1; |
144 |
return; |
145 |
} |
146 |
|
147 |
} |
148 |
|
149 |
sub rConnect { |
150 |
my $where = shift; |
151 |
my $args = shift; |
152 |
if ($where eq 'cmdfile') { |
153 |
if (!$args) { |
154 |
$args = $HOME . '/.mjamcmd'; |
155 |
} |
156 |
my $file = $args; |
157 |
if (-e $file) { |
158 |
print "trying to connect to $where ($args)", "\n"; |
159 |
$conn{file_name} = $args; |
160 |
$conn{connected} = 1; |
161 |
$conn{target} = $conn{file_name}; |
162 |
} |
163 |
} |
164 |
|
165 |
} |
166 |
|
167 |
sub rCmd { |
168 |
my $cmd = shift; |
169 |
if (!$conn{connected}) { |
170 |
print "\"$cmd\" is a remote command, please connect first!", "\n"; |
171 |
return; |
172 |
} |
173 |
#my $fh = $conn{file_handle}; |
174 |
my $handle = IO::File->new($conn{file_name}, "a+"); |
175 |
$handle->print($cmd, "\n"); |
176 |
$handle->close(); |
177 |
return "done???"; |
178 |
} |
179 |
|
180 |
sub in_array { |
181 |
my $search = shift; |
182 |
my $arrayref = shift; |
183 |
my $bool_ok = 0; |
184 |
map { $bool_ok++ if ($_ eq $search); } @{$arrayref}; |
185 |
#map { print "found\n" if ($_ eq $rcmd); } @sh_commands_remote; |
186 |
#map { if print $_, ","; } @sh_commands_remote; |
187 |
$bool_ok && return 1; |
188 |
} |
189 |
|
190 |
|
191 |
sub execCmd { |
192 |
|
193 |
my $cmd_console = shift; |
194 |
my $result; |
195 |
|
196 |
my $cmd_issue; |
197 |
|
198 |
# if the console-command is empty, just return |
199 |
if (!$cmd_console) { return; } |
200 |
|
201 |
# split console-command by whitespace |
202 |
my @cmd = split(' ', $cmd_console); |
203 |
|
204 |
# is it a local command ... |
205 |
$cmd_issue = $cmd_console; |
206 |
if (in_array($cmd[0], $console{commands})) { |
207 |
return lCmd($cmd_console); |
208 |
} |
209 |
|
210 |
# prefix console-command with console-context ;), to build complete remote-command |
211 |
my $context = ''; |
212 |
$console{context} && ($context = $console{context} . " "); |
213 |
my $cmd_remote = $context . $cmd_console; |
214 |
|
215 |
# split remote-command by whitespace |
216 |
my @cmdr = split(' ', $cmd_remote); |
217 |
|
218 |
# ... or is it a remote one? |
219 |
$cmd_issue = $cmd_remote; |
220 |
if (in_array($cmdr[0], $conn{commands})) { |
221 |
return rCmd($cmd_remote); |
222 |
} |
223 |
|
224 |
# unknown command, print this to shell |
225 |
return "unknown command (\"$cmd_issue\"), type ? for help."; |
226 |
} |
227 |
|
228 |
sub getPrompt { |
229 |
my $tmp_login = ''; |
230 |
if ($conn{connected}) { |
231 |
my $target_prompt; |
232 |
if ($conn{type} eq "cmdfd") { |
233 |
$target_prompt = "cmdfd"; |
234 |
} else { |
235 |
$target_prompt = $conn{target}; |
236 |
} |
237 |
$tmp_login .= "(" . $conn{user} . "\@" . $target_prompt . ") "; |
238 |
} |
239 |
if ($console{context}) { |
240 |
$tmp_login .= "[ $console{context} ] "; |
241 |
} |
242 |
return "$cfg{branding}{console_name} $tmp_login#> "; |
243 |
} |
244 |
|
245 |
sub main { |
246 |
|
247 |
my $term = new Term::ReadLine 'jash'; |
248 |
#my $prompt = getPrompt(); |
249 |
my $OUT = $term->OUT || STDOUT; |
250 |
|
251 |
while ( defined ($_ = $term->readline(getPrompt())) ) { |
252 |
#$res = eval($_), "\n"; |
253 |
my $res = execCmd($_); |
254 |
#warn $@ if $@; |
255 |
#print $OUT $res, "\n" unless $@; |
256 |
$res && print $OUT $res, "\n"; |
257 |
$term->addhistory($_) if /\S/; |
258 |
} |
259 |
|
260 |
} |
261 |
|
262 |
main(); |
263 |
print "exit.", "\n"; |